]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
Log errors from plugins to server log.
[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       settings(),
58       tidTCP(),
59       tidUDP(),
60       runningTCP(false),
61       runningUDP(false),
62       stoppedTCP(true),
63       stoppedUDP(true),
64       portT(0),
65       portU(0),
66       sockTCP(-1),
67       sockUDP(-1),
68       errorStr(),
69       logger(GetPluginLogger(GetStgLogger(), "cap_nf"))
70 {
71 }
72
73 NF_CAP::~NF_CAP()
74 {
75 }
76
77 int NF_CAP::ParseSettings()
78 {
79 std::vector<PARAM_VALUE>::iterator it;
80 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
81     {
82     if (it->param == "TCPPort")
83         {
84         if (str2x(it->value[0], portT))
85             {
86             errorStr = "Invalid TCPPort value";
87             printfd(__FILE__, "Error: Invalid TCPPort value\n");
88             return -1;
89             }
90         continue;
91         }
92     if (it->param == "UDPPort")
93         {
94         if (str2x(it->value[0], portU))
95             {
96             errorStr = "Invalid UDPPort value";
97             printfd(__FILE__, "Error: Invalid UDPPort value\n");
98             return -1;
99             }
100         continue;
101         }
102     printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
103     }
104 return 0;
105 }
106
107 int NF_CAP::Start()
108 {
109 if (portU > 0)
110     {
111     if (OpenUDP())
112         {
113         return -1;
114         }
115     runningUDP = true;
116     if (pthread_create(&tidUDP, NULL, RunUDP, this))
117         {
118         runningUDP = false;
119         CloseUDP();
120         errorStr = "Cannot create UDP thread";
121         logger("Cannot create UDP thread.");
122         printfd(__FILE__, "Error: Cannot create UDP thread\n");
123         return -1;
124         }
125     }
126 if (portT > 0)
127     {
128     if (OpenTCP())
129         {
130         return -1;
131         }
132     runningTCP = true;
133     if (pthread_create(&tidTCP, NULL, RunTCP, this))
134         {
135         runningTCP = false;
136         CloseTCP();
137         logger("Cannot create TCP thread.");
138         errorStr = "Cannot create TCP thread";
139         printfd(__FILE__, "Error: Cannot create TCP thread\n");
140         return -1;
141         }
142     }
143 return 0;
144 }
145
146 int NF_CAP::Stop()
147 {
148 runningTCP = runningUDP = false;
149 if (portU && !stoppedUDP)
150     {
151     CloseUDP();
152     for (int i = 0; i < 25 && !stoppedUDP; ++i)
153         {
154         struct timespec ts = {0, 200000000};
155         nanosleep(&ts, NULL);
156         }
157     if (stoppedUDP)
158         {
159         pthread_join(tidUDP, NULL);
160         }
161     else
162         {
163         if (pthread_kill(tidUDP, SIGUSR1))
164             {
165             errorStr = "Error sending signal to UDP thread";
166             logger("Error sending sugnal to UDP thread.");
167             printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
168             return -1;
169             }
170         printfd(__FILE__, "UDP thread NOT stopped\n");
171         logger("Cannot stop UDP thread.");
172         }
173     }
174 if (portT && !stoppedTCP)
175     {
176     CloseTCP();
177     for (int i = 0; i < 25 && !stoppedTCP; ++i)
178         {
179         struct timespec ts = {0, 200000000};
180         nanosleep(&ts, NULL);
181         }
182     if (stoppedTCP)
183         {
184         pthread_join(tidTCP, NULL);
185         }
186     else
187         {
188         if (pthread_kill(tidTCP, SIGUSR1))
189             {
190             errorStr = "Error sending signal to TCP thread";
191             logger("Error sending signal to TCP thread.");
192             printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
193             return -1;
194             }
195         printfd(__FILE__, "TCP thread NOT stopped\n");
196         logger("Cannot stop TCP thread.");
197         }
198     }
199 return 0;
200 }
201
202 bool NF_CAP::OpenUDP()
203 {
204 struct sockaddr_in sin;
205 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
206 if (sockUDP <= 0)
207     {
208     errorStr = "Error opening UDP socket";
209     logger("Cannot create UDP socket: %s", strerror(errno));
210     printfd(__FILE__, "Error: Error opening UDP socket\n");
211     return true;
212     }
213 sin.sin_family = AF_INET;
214 sin.sin_port = htons(portU);
215 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
216 if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin)))
217     {
218     errorStr = "Error binding UDP socket";
219     logger("Cannot bind UDP socket: %s", strerror(errno));
220     printfd(__FILE__, "Error: Error binding UDP socket\n");
221     return true;
222     }
223 return false;
224 }
225
226 bool NF_CAP::OpenTCP()
227 {
228 struct sockaddr_in sin;
229 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
230 if (sockTCP <= 0)
231     {
232     errorStr = "Error opening TCP socket";
233     logger("Cannot create TCP socket: %s", strerror(errno));
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     logger("Cannot bind TCP socket: %s", strerror(errno));
244     printfd(__FILE__, "Error: Error binding TCP socket\n");
245     return true;
246     }
247 if (listen(sockTCP, 1))
248     {
249     errorStr = "Error listening on TCP socket";
250     logger("Cannot listen on TCP socket: %s", strerror(errno));
251     printfd(__FILE__, "Error: Error listening TCP socket\n");
252     return true;
253     }
254 return false;
255 }
256
257 void * NF_CAP::RunUDP(void * c)
258 {
259 sigset_t signalSet;
260 sigfillset(&signalSet);
261 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
262
263 NF_CAP * cap = static_cast<NF_CAP *>(c);
264 uint8_t buf[BUF_SIZE];
265 int res;
266 struct sockaddr_in sin;
267 socklen_t slen;
268 cap->stoppedUDP = false;
269 while (cap->runningUDP)
270     {
271     if (!WaitPackets(cap->sockUDP))
272         {
273         continue;
274         }
275
276     // Data
277     slen = sizeof(sin);
278     res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
279     if (!cap->runningUDP)
280         break;
281
282     if (res < 0)
283         {
284         cap->logger("recvfrom error: %s", strerror(errno));
285         continue;
286         }
287
288     if (res == 0) // EOF
289         {
290         continue;
291         }
292
293     if (res < 24)
294         {
295         if (errno != EINTR)
296             {
297             cap->errorStr = "Invalid data received";
298             printfd(__FILE__, "Error: Invalid data received through UDP\n");
299             }
300         continue;
301         }
302
303     cap->ParseBuffer(buf, res);
304     }
305 cap->stoppedUDP = true;
306 return NULL;
307 }
308
309 void * NF_CAP::RunTCP(void * c)
310 {
311 sigset_t signalSet;
312 sigfillset(&signalSet);
313 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
314
315 NF_CAP * cap = static_cast<NF_CAP *>(c);
316 uint8_t buf[BUF_SIZE];
317 int res;
318 int sd;
319 struct sockaddr_in sin;
320 socklen_t slen;
321 cap->stoppedTCP = false;
322 while (cap->runningTCP)
323     {
324     if (!WaitPackets(cap->sockTCP))
325         {
326         continue;
327         }
328
329     // Data
330     slen = sizeof(sin);
331     sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
332     if (!cap->runningTCP)
333         break;
334
335     if (sd <= 0)
336         {
337         if (sd < 0)
338             cap->logger("accept error: %s", strerror(errno));
339         continue;
340         }
341
342     if (!WaitPackets(sd))
343         {
344         close(sd);
345         continue;
346         }
347
348     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, int 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 }