]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
Добавление исходников
[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 "common.h" 
41 #include "cap_nf.h"
42 #include "raw_ip_packet.h"
43
44 #include "../../../traffcounter.h"
45
46 class CAP_NF_CREATOR
47 {
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 BASE_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 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
282     // Wrong logic!
283     // Need to check actual data length and wait all data to receive
284     if (res < 24)
285         {
286         if (errno != EINTR)
287             {
288             cap->errorStr = "Invalid data received";
289             printfd(__FILE__, "Error: Invalid data received through UDP\n");
290             }
291         continue;
292         }
293
294     cap->ParseBuffer(buf, res);
295     }
296 cap->stoppedUDP = true;
297 return NULL;
298 }
299
300 void * NF_CAP::RunTCP(void * c)
301 {
302 NF_CAP * cap = static_cast<NF_CAP *>(c);
303 uint8_t buf[BUF_SIZE];
304 int res;
305 int sd;
306 struct sockaddr_in sin;
307 socklen_t slen;
308 cap->stoppedTCP = false;
309 while (cap->runningTCP)
310     {
311     if (!cap->WaitPackets(cap->sockTCP))
312         {
313         continue;
314         }
315
316     // Data
317     slen = sizeof(sin);
318     sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
319     if (!cap->runningTCP)
320         break;
321
322     if (sd <= 0)
323         {
324         if (errno != EINTR)
325             {
326             cap->errorStr = "Error accepting connection";
327             printfd(__FILE__, "Error: Error accepting connection\n");
328             }
329         continue;
330         }
331
332     if (!cap->WaitPackets(sd))
333         {
334         close(sd);
335         continue;
336         }
337
338     res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
339     close(sd);
340
341     if (!cap->runningTCP)
342         break;
343
344     if (res == 0) // EOF
345         {
346         continue;
347         }
348
349     // Wrong logic!
350     // Need to check actual data length and wait all data to receive
351     if (res < 24)
352         {
353         if (errno != EINTR)
354             {
355             cap->errorStr = "Invalid data received";
356             printfd(__FILE__, "Error: Invalid data received through TCP\n");
357             }
358         continue;
359         }
360
361     cap->ParseBuffer(buf, res);
362     }
363 cap->stoppedTCP = true;
364 return NULL;
365 }
366
367 void NF_CAP::ParseBuffer(uint8_t * buf, int size)
368 {
369 RAW_PACKET ip;
370 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
371 if (htons(hdr->version) != 5)
372     {
373     return;
374     }
375
376 int packets = htons(hdr->count);
377
378 if (packets < 0 || packets > 30)
379     {
380     return;
381     }
382
383 if (24 + 48 * packets != size)
384     {
385     // See 'wrong logic' upper
386     return;
387     }
388
389 for (int i = 0; i < packets; ++i)
390     {
391     NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
392
393     /*ip.pckt[0] = 4 << 4;
394     ip.pckt[0] |= 5;
395     ip.pckt[9] = data->proto;
396     ip.dataLen = ntohl(data->octets);
397     *(uint32_t *)(ip.pckt + 12) = data->srcAddr;
398     *(uint32_t *)(ip.pckt + 16) = data->dstAddr;
399     *(uint16_t *)(ip.pckt + 20) = data->srcPort;
400     *(uint16_t *)(ip.pckt + 22) = data->dstPort;*/
401     ip.ipHeader.ip_v = 4;
402     ip.ipHeader.ip_hl = 5;
403     ip.ipHeader.ip_p = data->proto;
404     ip.dataLen = ntohl(data->octets);
405     ip.ipHeader.ip_src.s_addr = data->srcAddr;
406     ip.ipHeader.ip_dst.s_addr = data->dstAddr;
407     ip.sPort = data->srcPort;
408     ip.dPort = data->dstPort;
409
410     traffCnt->Process(ip);
411     }
412 }
413
414 bool NF_CAP::WaitPackets(int sd) const
415 {
416 fd_set rfds;
417 FD_ZERO(&rfds);
418 FD_SET(sd, &rfds);
419
420 struct timeval tv;
421 tv.tv_sec = 0;
422 tv.tv_usec = 500000;
423
424 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
425 if (res == -1) // Error
426     {
427     if (errno != EINTR)
428         {
429         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
430         }
431     return false;
432     }
433
434 if (res == 0) // Timeout
435     {
436     return false;
437     }
438
439 return true;
440 }