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