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