]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/pcap/pcap_cap.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / capture / pcap / pcap_cap.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
19 */
20
21 #include "pcap_cap.h"
22
23 #include "stg/traffcounter.h"
24 #include "stg/plugin_creator.h"
25 #include "stg/common.h"
26 #include "stg/raw_ip_packet.h"
27
28 #include <signal.h>
29
30 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
33 namespace
34 {
35 PLUGIN_CREATOR<PCAP_CAP> pcc;
36
37 const size_t SNAP_LEN = 1518;
38 const size_t ETHER_ADDR_LEN = 6;
39
40 struct ETH
41 {
42 u_char     ether_dhost[ETHER_ADDR_LEN];    /* destination host address */
43 u_char     ether_shost[ETHER_ADDR_LEN];    /* source host address */
44 u_short    ether_type;                     /* IP? ARP? RARP? etc */
45 };
46
47 }
48
49 extern "C" PLUGIN * GetPlugin();
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 PLUGIN * GetPlugin()
54 {
55 return pcc.GetPlugin();
56 }
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 std::string PCAP_CAP::GetVersion() const
61 {
62 return "pcap_cap v.1.0";
63 }
64 //-----------------------------------------------------------------------------
65 PCAP_CAP::PCAP_CAP()
66     : nonstop(false),
67       isRunning(false),
68       traffCnt(NULL),
69       logger(GetPluginLogger(GetStgLogger(), "pcap_cap"))
70 {
71 }
72 //-----------------------------------------------------------------------------
73 int PCAP_CAP::ParseSettings()
74 {
75 devices.erase(devices.begin(), devices.end());
76
77 if (settings.moduleParams.empty())
78     {
79     devices.push_back(DEV());
80     logger("Defaulting to pseudo-device 'any'.");
81     return 0;
82     }
83
84 for (size_t i = 0; i < settings.moduleParams.size(); i++)
85     if (settings.moduleParams[i].param == "interfaces")
86         for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
87             devices.push_back(DEV(settings.moduleParams[i].value[j]));
88
89 for (size_t i = 0; i < settings.moduleParams.size(); i++)
90     if (settings.moduleParams[i].param == "filters")
91         for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
92             if (j < devices.size())
93                 devices[j].filterExpression = settings.moduleParams[i].value[j];
94
95 if (devices.empty())
96     {
97     devices.push_back(DEV());
98     logger("Defaulting to pseudo-device 'all'.");
99     return 0;
100     }
101
102 return 0;
103 }
104 //-----------------------------------------------------------------------------
105 int PCAP_CAP::Start()
106 {
107 if (isRunning)
108     return 0;
109
110 DEV_MAP::iterator it(devices.begin());
111 while (it != devices.end())
112     {
113     bpf_u_int32 mask;
114     bpf_u_int32 net;
115     char errbuf[PCAP_ERRBUF_SIZE];
116
117     /* get network number and mask associated with capture device */
118     if (pcap_lookupnet(it->device.c_str(), &net, &mask, errbuf) == -1)
119         {
120         errorStr = "Couldn't get netmask for device " + it->device + ": " + errbuf;
121         logger(errorStr);
122         printfd(__FILE__, "%s\n", errorStr.c_str());
123         return -1;
124         }
125
126     /* open capture device */
127     it->handle = pcap_open_live(it->device.c_str(), SNAP_LEN, 1, 1000, errbuf);
128     if (it->handle == NULL)
129         {
130         errorStr = "Couldn't open device " + it->device + ": " + errbuf;
131         logger(errorStr);
132         printfd(__FILE__, "%s\n", errorStr.c_str());
133         return -1;
134         }
135
136     if (pcap_setnonblock(it->handle, true, errbuf) == -1)
137         {
138         errorStr = "Couldn't put device " + it->device + " into non-blocking mode: " + errbuf;
139         logger(errorStr);
140         printfd(__FILE__, "%s\n", errorStr.c_str());
141         return -1;
142         }
143
144     /* make sure we're capturing on an Ethernet device [2] */
145     if (pcap_datalink(it->handle) != DLT_EN10MB)
146         {
147         errorStr = it->device + " is not an Ethernet";
148         logger(errorStr);
149         printfd(__FILE__, "%s\n", errorStr.c_str());
150         return -1;
151         }
152
153     /* compile the filter expression */
154     if (pcap_compile(it->handle, &it->filter, it->filterExpression.c_str(), 0, net) == -1)
155         {
156         errorStr = "Couldn't parse filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
157         logger(errorStr);
158         printfd(__FILE__, "%s\n", errorStr.c_str());
159         return -1;
160         }
161
162     /* apply the compiled filter */
163     if (pcap_setfilter(it->handle, &it->filter) == -1)
164         {
165         errorStr = "Couldn't install filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
166         logger(errorStr);
167         printfd(__FILE__, "%s\n", errorStr.c_str());
168         return -1;
169         }
170
171     it->fd = pcap_get_selectable_fd(it->handle);
172     if (it->fd == -1)
173         {
174         errorStr = "Couldn't get a file descriptor for " + it->device + ": " + pcap_geterr(it->handle);
175         logger(errorStr);
176         printfd(__FILE__, "%s\n", errorStr.c_str());
177         return -1;
178         }
179
180     ++it;
181     }
182
183 nonstop = true;
184
185 if (pthread_create(&thread, NULL, Run, this))
186     {
187     errorStr = "Cannot create thread.";
188     logger("Cannot create thread.");
189     printfd(__FILE__, "Cannot create thread\n");
190     return -1;
191     }
192
193 return 0;
194 }
195 //-----------------------------------------------------------------------------
196 int PCAP_CAP::Stop()
197 {
198 if (!isRunning)
199     return 0;
200
201 nonstop = false;
202
203 //5 seconds to thread stops itself
204 for (int i = 0; i < 25 && isRunning; i++)
205     {
206     struct timespec ts = {0, 200000000};
207     nanosleep(&ts, NULL);
208     }
209 //after 5 seconds waiting thread still running. now killing it
210 if (isRunning)
211     {
212     if (pthread_kill(thread, SIGUSR1))
213         {
214         errorStr = "Cannot kill thread.";
215         logger("Cannot send signal to thread.");
216         return -1;
217         }
218     for (int i = 0; i < 25 && isRunning; ++i)
219         {
220         struct timespec ts = {0, 200000000};
221         nanosleep(&ts, NULL);
222         }
223     if (isRunning)
224         {
225         errorStr = "PCAP_CAP not stopped.";
226         logger("Cannot stop thread.");
227         printfd(__FILE__, "Cannot stop thread\n");
228         return -1;
229         }
230     }
231
232 pthread_join(thread, NULL);
233
234 for (DEV_MAP::iterator it(devices.begin()); it != devices.end(); ++it)
235     {
236     pcap_freecode(&it->filter);
237     pcap_close(it->handle);
238     }
239
240 return 0;
241 }
242 //-----------------------------------------------------------------------------
243 void * PCAP_CAP::Run(void * d)
244 {
245 sigset_t signalSet;
246 sigfillset(&signalSet);
247 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
248
249 PCAP_CAP * dc = static_cast<PCAP_CAP *>(d);
250 dc->isRunning = true;
251
252 fd_set fds;
253 FD_ZERO(&fds);
254 int maxFd = 0;
255 for (DEV_MAP::const_iterator it(dc->devices.begin()); it != dc->devices.end(); ++it)
256     {
257     FD_SET(it->fd, &fds);
258     maxFd = std::max(maxFd, it->fd);
259     }
260
261 while (dc->nonstop)
262     {
263     fd_set rfds = fds;
264     struct timeval tv = {0, 500000};
265
266     if (select(maxFd + 1, &rfds, NULL, NULL, &tv) > 0)
267         dc->TryRead(rfds);
268     }
269
270 dc->isRunning = false;
271 return NULL;
272 }
273
274 void PCAP_CAP::TryRead(const fd_set & set)
275 {
276 for (DEV_MAP::const_iterator it(devices.begin()); it != devices.end(); ++it)
277     if (FD_ISSET(it->fd, &set))
278         TryReadDev(*it);
279 }
280
281 void PCAP_CAP::TryReadDev(const DEV & dev)
282 {
283 struct pcap_pkthdr * header;
284 const u_char * packet;
285 if (pcap_next_ex(dev.handle, &header, &packet) == -1)
286     {
287     printfd(__FILE__, "Failed to read data from '%s': %s\n", dev.device.c_str(), pcap_geterr(dev.handle));
288     return;
289     }
290
291 const ETH * eth = reinterpret_cast<const ETH *>(packet);
292 if (eth->ether_type != 0x8)
293     return;
294
295 RAW_PACKET ip;
296 memcpy(&ip.rawPacket, packet + 14, sizeof(ip.rawPacket));
297 traffCnt->Process(ip);
298 }