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