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"
48 PLUGIN_CREATOR<NF_CAP> cnc;
52 return cnc.GetPlugin();
76 int NF_CAP::ParseSettings()
78 std::vector<PARAM_VALUE>::iterator it;
79 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
81 if (it->param == "TCPPort")
83 if (str2x(it->value[0], portT))
85 errorStr = "Invalid TCPPort value";
86 printfd(__FILE__, "Error: Invalid TCPPort value\n");
91 if (it->param == "UDPPort")
93 if (str2x(it->value[0], portU))
95 errorStr = "Invalid UDPPort value";
96 printfd(__FILE__, "Error: Invalid UDPPort value\n");
101 printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
115 if (pthread_create(&tidUDP, NULL, RunUDP, this))
119 errorStr = "Cannot create UDP thread";
120 printfd(__FILE__, "Error: Cannot create UDP thread\n");
131 if (pthread_create(&tidTCP, NULL, RunTCP, this))
135 errorStr = "Cannot create TCP thread";
136 printfd(__FILE__, "Error: Cannot create TCP thread\n");
145 runningTCP = runningUDP = false;
146 if (portU && !stoppedUDP)
149 for (int i = 0; i < 25 && !stoppedUDP; ++i)
151 struct timespec ts = {0, 200000000};
152 nanosleep(&ts, NULL);
156 pthread_join(tidUDP, NULL);
160 if (pthread_kill(tidUDP, SIGUSR1))
162 errorStr = "Error sending signal to UDP thread";
163 printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
166 printfd(__FILE__, "UDP thread NOT stopped\n");
169 if (portT && !stoppedTCP)
172 for (int i = 0; i < 25 && !stoppedTCP; ++i)
174 struct timespec ts = {0, 200000000};
175 nanosleep(&ts, NULL);
179 pthread_join(tidTCP, NULL);
183 if (pthread_kill(tidTCP, SIGUSR1))
185 errorStr = "Error sending signal to TCP thread";
186 printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
189 printfd(__FILE__, "TCP thread NOT stopped\n");
195 bool NF_CAP::OpenUDP()
197 struct sockaddr_in sin;
198 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
201 errorStr = "Error opening UDP socket";
202 printfd(__FILE__, "Error: Error opening UDP socket\n");
205 sin.sin_family = AF_INET;
206 sin.sin_port = htons(portU);
207 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
208 if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin)))
210 errorStr = "Error binding UDP socket";
211 printfd(__FILE__, "Error: Error binding UDP socket\n");
217 bool NF_CAP::OpenTCP()
219 struct sockaddr_in sin;
220 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
223 errorStr = "Error opening TCP socket";
224 printfd(__FILE__, "Error: Error opening TCP socket\n");
227 sin.sin_family = AF_INET;
228 sin.sin_port = htons(portT);
229 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
230 if (bind(sockTCP, (struct sockaddr *)&sin, sizeof(sin)))
232 errorStr = "Error binding TCP socket";
233 printfd(__FILE__, "Error: Error binding TCP socket\n");
236 if (listen(sockTCP, 1))
238 errorStr = "Error listening on TCP socket";
239 printfd(__FILE__, "Error: Error listening TCP socket\n");
245 void * NF_CAP::RunUDP(void * c)
247 NF_CAP * cap = static_cast<NF_CAP *>(c);
248 uint8_t buf[BUF_SIZE];
250 struct sockaddr_in sin;
252 cap->stoppedUDP = false;
253 while (cap->runningUDP)
255 if (!WaitPackets(cap->sockUDP))
262 res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
263 if (!cap->runningUDP)
275 cap->errorStr = "Invalid data received";
276 printfd(__FILE__, "Error: Invalid data received through UDP\n");
281 cap->ParseBuffer(buf, res);
283 cap->stoppedUDP = true;
287 void * NF_CAP::RunTCP(void * c)
289 NF_CAP * cap = static_cast<NF_CAP *>(c);
290 uint8_t buf[BUF_SIZE];
293 struct sockaddr_in sin;
295 cap->stoppedTCP = false;
296 while (cap->runningTCP)
298 if (!WaitPackets(cap->sockTCP))
305 sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
306 if (!cap->runningTCP)
313 cap->errorStr = "Error accepting connection";
314 printfd(__FILE__, "Error: Error accepting connection\n");
319 if (!WaitPackets(sd))
325 res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
328 if (!cap->runningTCP)
337 // Need to check actual data length and wait all data to receive
342 cap->errorStr = "Invalid data received";
343 printfd(__FILE__, "Error: Invalid data received through TCP\n");
348 cap->ParseBuffer(buf, res);
350 cap->stoppedTCP = true;
354 void NF_CAP::ParseBuffer(uint8_t * buf, int size)
357 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
358 if (htons(hdr->version) != 5)
363 int packets = htons(hdr->count);
365 if (packets < 0 || packets > 30)
370 if (24 + 48 * packets != size)
372 // See 'wrong logic' upper
376 for (int i = 0; i < packets; ++i)
378 NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
380 ip.rawPacket.header.ipHeader.ip_v = 4;
381 ip.rawPacket.header.ipHeader.ip_hl = 5;
382 ip.rawPacket.header.ipHeader.ip_p = data->proto;
383 ip.dataLen = ntohl(data->octets);
384 ip.rawPacket.header.ipHeader.ip_src.s_addr = data->srcAddr;
385 ip.rawPacket.header.ipHeader.ip_dst.s_addr = data->dstAddr;
386 ip.rawPacket.header.sPort = data->srcPort;
387 ip.rawPacket.header.dPort = data->dstPort;
389 traffCnt->Process(ip);