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.
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.
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
22 * Author : Maxim Mamontov <faust@stg.dp.ua>
27 $Date: 2010/09/10 06:41:06 $
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
42 #include "stg/common.h"
43 #include "stg/raw_ip_packet.h"
44 #include "stg/traffcounter.h"
45 #include "stg/plugin_creator.h"
50 PLUGIN_CREATOR<NF_CAP> cnc;
53 extern "C" PLUGIN * GetPlugin();
57 return cnc.GetPlugin();
74 logger(GetPluginLogger(GetStgLogger(), "cap_nf"))
82 int NF_CAP::ParseSettings()
84 std::vector<PARAM_VALUE>::iterator it;
85 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
87 if (it->param == "TCPPort")
89 if (str2x(it->value[0], portT))
91 errorStr = "Invalid TCPPort value";
92 printfd(__FILE__, "Error: Invalid TCPPort value\n");
97 if (it->param == "UDPPort")
99 if (str2x(it->value[0], portU))
101 errorStr = "Invalid UDPPort value";
102 printfd(__FILE__, "Error: Invalid UDPPort value\n");
107 printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
121 if (pthread_create(&tidUDP, NULL, RunUDP, this))
125 errorStr = "Cannot create UDP thread";
126 logger("Cannot create UDP thread.");
127 printfd(__FILE__, "Error: Cannot create UDP thread\n");
138 if (pthread_create(&tidTCP, NULL, RunTCP, this))
142 logger("Cannot create TCP thread.");
143 errorStr = "Cannot create TCP thread";
144 printfd(__FILE__, "Error: Cannot create TCP thread\n");
153 runningTCP = runningUDP = false;
154 if (portU && !stoppedUDP)
157 for (int i = 0; i < 25 && !stoppedUDP; ++i)
159 struct timespec ts = {0, 200000000};
160 nanosleep(&ts, NULL);
164 pthread_join(tidUDP, NULL);
168 if (pthread_kill(tidUDP, SIGUSR1))
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");
175 printfd(__FILE__, "UDP thread NOT stopped\n");
176 logger("Cannot stop UDP thread.");
179 if (portT && !stoppedTCP)
182 for (int i = 0; i < 25 && !stoppedTCP; ++i)
184 struct timespec ts = {0, 200000000};
185 nanosleep(&ts, NULL);
189 pthread_join(tidTCP, NULL);
193 if (pthread_kill(tidTCP, SIGUSR1))
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");
200 printfd(__FILE__, "TCP thread NOT stopped\n");
201 logger("Cannot stop TCP thread.");
207 bool NF_CAP::OpenUDP()
209 struct sockaddr_in sin;
210 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
213 errorStr = "Error opening UDP socket";
214 logger("Cannot create UDP socket: %s", strerror(errno));
215 printfd(__FILE__, "Error: Error opening UDP socket\n");
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)))
223 errorStr = "Error binding UDP socket";
224 logger("Cannot bind UDP socket: %s", strerror(errno));
225 printfd(__FILE__, "Error: Error binding UDP socket\n");
231 bool NF_CAP::OpenTCP()
233 struct sockaddr_in sin;
234 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
237 errorStr = "Error opening TCP socket";
238 logger("Cannot create TCP socket: %s", strerror(errno));
239 printfd(__FILE__, "Error: Error opening TCP socket\n");
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)))
247 errorStr = "Error binding TCP socket";
248 logger("Cannot bind TCP socket: %s", strerror(errno));
249 printfd(__FILE__, "Error: Error binding TCP socket\n");
252 if (listen(sockTCP, 1))
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");
262 void * NF_CAP::RunUDP(void * c)
265 sigfillset(&signalSet);
266 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
268 NF_CAP * cap = static_cast<NF_CAP *>(c);
269 cap->stoppedUDP = false;
270 while (cap->runningUDP)
272 if (!WaitPackets(cap->sockUDP))
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)
287 cap->logger("recvfrom error: %s", strerror(errno));
300 cap->errorStr = "Invalid data received";
301 printfd(__FILE__, "Error: Invalid data received through UDP\n");
306 cap->ParseBuffer(buf, res);
308 cap->stoppedUDP = true;
312 void * NF_CAP::RunTCP(void * c)
315 sigfillset(&signalSet);
316 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
318 NF_CAP * cap = static_cast<NF_CAP *>(c);
319 cap->stoppedTCP = false;
320 while (cap->runningTCP)
322 if (!WaitPackets(cap->sockTCP))
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)
337 cap->logger("accept error: %s", strerror(errno));
341 if (!WaitPackets(sd))
347 uint8_t buf[BUF_SIZE];
348 ssize_t res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
351 cap->logger("recv error: %s", strerror(errno));
355 if (!cap->runningTCP)
364 // Need to check actual data length and wait all data to receive
370 cap->ParseBuffer(buf, res);
372 cap->stoppedTCP = true;
376 void NF_CAP::ParseBuffer(uint8_t * buf, ssize_t size)
379 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
380 if (htons(hdr->version) != 5)
385 int packets = htons(hdr->count);
387 if (packets < 0 || packets > 30)
392 if (24 + 48 * packets != size)
394 // See 'wrong logic' upper
398 for (int i = 0; i < packets; ++i)
400 NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
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;
411 traffCnt->Process(ip);