]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
Fix TRAFFCOUNTER in plugins
[stg.git] / projects / stargazer / plugins / capture / cap_nf / cap_nf.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 Date: 16.05.2008
19 */
20
21 /*
22 * Author : Maxim Mamontov <faust@stg.dp.ua>
23 */
24
25 /*
26 $Revision: 1.11 $
27 $Date: 2010/09/10 06:41:06 $
28 $Author: faust $
29 */
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <unistd.h>
35
36 #include <csignal>
37 #include <cerrno>
38 #include <cstring>
39
40 #include <vector>
41
42 #include "common.h" 
43 #include "cap_nf.h"
44 #include "raw_ip_packet.h"
45 #include "traffcounter.h"
46
47 class CAP_NF_CREATOR {
48 public:
49     CAP_NF_CREATOR()
50         : nf(new NF_CAP())
51         {
52         }
53
54     ~CAP_NF_CREATOR()
55         {
56         delete nf;
57         }
58
59     NF_CAP * GetCapturer() { return nf; }
60 private:
61     NF_CAP * nf;
62 } cnc;
63
64 PLUGIN * GetPlugin()
65 {
66 return cnc.GetCapturer();
67 }
68
69 NF_CAP::NF_CAP()
70     : traffCnt(NULL),
71       tidTCP(0),
72       tidUDP(0),
73       runningTCP(false),
74       runningUDP(false),
75       stoppedTCP(true),
76       stoppedUDP(true),
77       portT(0),
78       portU(0),
79       sockTCP(-1),
80       sockUDP(-1)
81 {
82 }
83
84 NF_CAP::~NF_CAP()
85 {
86 }
87
88 int NF_CAP::ParseSettings()
89 {
90 std::vector<PARAM_VALUE>::iterator it;
91 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
92     {
93     if (it->param == "TCPPort")
94         {
95         if (str2x(it->value[0], portT))
96             {
97             errorStr = "Invalid TCPPort value";
98             printfd(__FILE__, "Error: Invalid TCPPort value\n");
99             return -1;
100             }
101         continue;
102         }
103     if (it->param == "UDPPort")
104         {
105         if (str2x(it->value[0], portU))
106             {
107             errorStr = "Invalid UDPPort value";
108             printfd(__FILE__, "Error: Invalid UDPPort value\n");
109             return -1;
110             }
111         continue;
112         }
113     printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
114     }
115 return 0;
116 }
117
118 int NF_CAP::Start()
119 {
120 if (portU > 0)
121     {
122     if (OpenUDP())
123         {
124         return -1;
125         }
126     runningUDP = true;
127     if (pthread_create(&tidUDP, NULL, RunUDP, this))
128         {
129         runningUDP = false;
130         CloseUDP();
131         errorStr = "Cannot create UDP thread";
132         printfd(__FILE__, "Error: Cannot create UDP thread\n");
133         return -1;
134         }
135     }
136 if (portT > 0)
137     {
138     if (OpenTCP())
139         {
140         return -1;
141         }
142     runningTCP = true;
143     if (pthread_create(&tidTCP, NULL, RunTCP, this))
144         {
145         runningTCP = false;
146         CloseTCP();
147         errorStr = "Cannot create TCP thread";
148         printfd(__FILE__, "Error: Cannot create TCP thread\n");
149         return -1;
150         }
151     }
152 return 0;
153 }
154
155 int NF_CAP::Stop()
156 {
157 runningTCP = runningUDP = false;
158 if (portU && !stoppedUDP)
159     {
160     CloseUDP();
161     for (int i = 0; i < 25 && !stoppedUDP; ++i)
162         {
163         usleep(200000);
164         }
165     if (stoppedUDP)
166         {
167         pthread_join(tidUDP, NULL);
168         }
169     else
170         {
171         if (pthread_kill(tidUDP, SIGUSR1))
172             {
173             errorStr = "Error sending signal to UDP thread";
174             printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
175             return -1;
176             }
177         printfd(__FILE__, "UDP thread NOT stopped\n");
178         }
179     }
180 if (portT && !stoppedTCP)
181     {
182     CloseTCP();
183     for (int i = 0; i < 25 && !stoppedTCP; ++i)
184         {
185         usleep(200000);
186         }
187     if (stoppedTCP)
188         {
189         pthread_join(tidTCP, NULL);
190         }
191     else
192         {
193         if (pthread_kill(tidTCP, SIGUSR1))
194             {
195             errorStr = "Error sending signal to TCP thread";
196             printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
197             return -1;
198             }
199         printfd(__FILE__, "TCP thread NOT stopped\n");
200         }
201     }
202 return 0;
203 }
204
205 bool NF_CAP::OpenUDP()
206 {
207 struct sockaddr_in sin;
208 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
209 if (sockUDP <= 0)
210     {
211     errorStr = "Error opening UDP socket";
212     printfd(__FILE__, "Error: Error opening UDP socket\n");
213     return true;
214     }
215 sin.sin_family = AF_INET;
216 sin.sin_port = htons(portU);
217 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
218 if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin)))
219     {
220     errorStr = "Error binding UDP socket";
221     printfd(__FILE__, "Error: Error binding UDP socket\n");
222     return true;
223     }
224 return false;
225 }
226
227 bool NF_CAP::OpenTCP()
228 {
229 struct sockaddr_in sin;
230 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
231 if (sockTCP <= 0)
232     {
233     errorStr = "Error opening TCP socket";
234     printfd(__FILE__, "Error: Error opening TCP socket\n");
235     return true;
236     }
237 sin.sin_family = AF_INET;
238 sin.sin_port = htons(portT);
239 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
240 if (bind(sockTCP, (struct sockaddr *)&sin, sizeof(sin)))
241     {
242     errorStr = "Error binding TCP socket";
243     printfd(__FILE__, "Error: Error binding TCP socket\n");
244     return true;
245     }
246 if (listen(sockTCP, 1))
247     {
248     errorStr = "Error listening on TCP socket";
249     printfd(__FILE__, "Error: Error listening TCP socket\n");
250     return true;
251     }
252 return false;
253 }
254
255 void * NF_CAP::RunUDP(void * c)
256 {
257 NF_CAP * cap = static_cast<NF_CAP *>(c);
258 uint8_t buf[BUF_SIZE];
259 int res;
260 struct sockaddr_in sin;
261 socklen_t slen;
262 cap->stoppedUDP = false;
263 while (cap->runningUDP)
264     {
265     if (!cap->WaitPackets(cap->sockUDP))
266         {
267         continue;
268         }
269
270     // Data
271     slen = sizeof(sin);
272     res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
273     if (!cap->runningUDP)
274         break;
275
276     if (res == 0) // EOF
277         {
278         continue;
279         }
280
281     if (res < 24)
282         {
283         if (errno != EINTR)
284             {
285             cap->errorStr = "Invalid data received";
286             printfd(__FILE__, "Error: Invalid data received through UDP\n");
287             }
288         continue;
289         }
290
291     cap->ParseBuffer(buf, res);
292     }
293 cap->stoppedUDP = true;
294 return NULL;
295 }
296
297 void * NF_CAP::RunTCP(void * c)
298 {
299 NF_CAP * cap = static_cast<NF_CAP *>(c);
300 uint8_t buf[BUF_SIZE];
301 int res;
302 int sd;
303 struct sockaddr_in sin;
304 socklen_t slen;
305 cap->stoppedTCP = false;
306 while (cap->runningTCP)
307     {
308     if (!cap->WaitPackets(cap->sockTCP))
309         {
310         continue;
311         }
312
313     // Data
314     slen = sizeof(sin);
315     sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
316     if (!cap->runningTCP)
317         break;
318
319     if (sd <= 0)
320         {
321         if (errno != EINTR)
322             {
323             cap->errorStr = "Error accepting connection";
324             printfd(__FILE__, "Error: Error accepting connection\n");
325             }
326         continue;
327         }
328
329     if (!cap->WaitPackets(sd))
330         {
331         close(sd);
332         continue;
333         }
334
335     res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
336     close(sd);
337
338     if (!cap->runningTCP)
339         break;
340
341     if (res == 0) // EOF
342         {
343         continue;
344         }
345
346     // Wrong logic!
347     // Need to check actual data length and wait all data to receive
348     if (res < 24)
349         {
350         if (errno != EINTR)
351             {
352             cap->errorStr = "Invalid data received";
353             printfd(__FILE__, "Error: Invalid data received through TCP\n");
354             }
355         continue;
356         }
357
358     cap->ParseBuffer(buf, res);
359     }
360 cap->stoppedTCP = true;
361 return NULL;
362 }
363
364 void NF_CAP::ParseBuffer(uint8_t * buf, int size)
365 {
366 RAW_PACKET ip;
367 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
368 if (htons(hdr->version) != 5)
369     {
370     return;
371     }
372
373 int packets = htons(hdr->count);
374
375 if (packets < 0 || packets > 30)
376     {
377     return;
378     }
379
380 if (24 + 48 * packets != size)
381     {
382     // See 'wrong logic' upper
383     return;
384     }
385
386 for (int i = 0; i < packets; ++i)
387     {
388     NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
389
390     ip.header.ipHeader.ip_v = 4;
391     ip.header.ipHeader.ip_hl = 5;
392     ip.header.ipHeader.ip_p = data->proto;
393     ip.dataLen = ntohl(data->octets);
394     ip.header.ipHeader.ip_src.s_addr = data->srcAddr;
395     ip.header.ipHeader.ip_dst.s_addr = data->dstAddr;
396     ip.header.sPort = data->srcPort;
397     ip.header.dPort = data->dstPort;
398
399     traffCnt->Process(ip);
400     }
401 }
402
403 bool NF_CAP::WaitPackets(int sd) const
404 {
405 fd_set rfds;
406 FD_ZERO(&rfds);
407 FD_SET(sd, &rfds);
408
409 struct timeval tv;
410 tv.tv_sec = 0;
411 tv.tv_usec = 500000;
412
413 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
414 if (res == -1) // Error
415     {
416     if (errno != EINTR)
417         {
418         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
419         }
420     return false;
421     }
422
423 if (res == 0) // Timeout
424     {
425     return false;
426     }
427
428 return true;
429 }