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();
74 int NF_CAP::ParseSettings()
76 std::vector<PARAM_VALUE>::iterator it;
77 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
79 if (it->param == "TCPPort")
81 if (str2x(it->value[0], portT))
83 errorStr = "Invalid TCPPort value";
84 printfd(__FILE__, "Error: Invalid TCPPort value\n");
89 if (it->param == "UDPPort")
91 if (str2x(it->value[0], portU))
93 errorStr = "Invalid UDPPort value";
94 printfd(__FILE__, "Error: Invalid UDPPort value\n");
99 printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
113 if (pthread_create(&tidUDP, NULL, RunUDP, this))
117 errorStr = "Cannot create UDP thread";
118 printfd(__FILE__, "Error: Cannot create UDP thread\n");
129 if (pthread_create(&tidTCP, NULL, RunTCP, this))
133 errorStr = "Cannot create TCP thread";
134 printfd(__FILE__, "Error: Cannot create TCP thread\n");
143 runningTCP = runningUDP = false;
144 if (portU && !stoppedUDP)
147 for (int i = 0; i < 25 && !stoppedUDP; ++i)
153 pthread_join(tidUDP, NULL);
157 if (pthread_kill(tidUDP, SIGUSR1))
159 errorStr = "Error sending signal to UDP thread";
160 printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
163 printfd(__FILE__, "UDP thread NOT stopped\n");
166 if (portT && !stoppedTCP)
169 for (int i = 0; i < 25 && !stoppedTCP; ++i)
175 pthread_join(tidTCP, NULL);
179 if (pthread_kill(tidTCP, SIGUSR1))
181 errorStr = "Error sending signal to TCP thread";
182 printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
185 printfd(__FILE__, "TCP thread NOT stopped\n");
191 bool NF_CAP::OpenUDP()
193 struct sockaddr_in sin;
194 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
197 errorStr = "Error opening UDP socket";
198 printfd(__FILE__, "Error: Error opening UDP socket\n");
201 sin.sin_family = AF_INET;
202 sin.sin_port = htons(portU);
203 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
204 if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin)))
206 errorStr = "Error binding UDP socket";
207 printfd(__FILE__, "Error: Error binding UDP socket\n");
213 bool NF_CAP::OpenTCP()
215 struct sockaddr_in sin;
216 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
219 errorStr = "Error opening TCP socket";
220 printfd(__FILE__, "Error: Error opening TCP socket\n");
223 sin.sin_family = AF_INET;
224 sin.sin_port = htons(portT);
225 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
226 if (bind(sockTCP, (struct sockaddr *)&sin, sizeof(sin)))
228 errorStr = "Error binding TCP socket";
229 printfd(__FILE__, "Error: Error binding TCP socket\n");
232 if (listen(sockTCP, 1))
234 errorStr = "Error listening on TCP socket";
235 printfd(__FILE__, "Error: Error listening TCP socket\n");
241 void * NF_CAP::RunUDP(void * c)
243 NF_CAP * cap = static_cast<NF_CAP *>(c);
244 uint8_t buf[BUF_SIZE];
246 struct sockaddr_in sin;
248 cap->stoppedUDP = false;
249 while (cap->runningUDP)
251 if (!cap->WaitPackets(cap->sockUDP))
258 res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
259 if (!cap->runningUDP)
271 cap->errorStr = "Invalid data received";
272 printfd(__FILE__, "Error: Invalid data received through UDP\n");
277 cap->ParseBuffer(buf, res);
279 cap->stoppedUDP = true;
283 void * NF_CAP::RunTCP(void * c)
285 NF_CAP * cap = static_cast<NF_CAP *>(c);
286 uint8_t buf[BUF_SIZE];
289 struct sockaddr_in sin;
291 cap->stoppedTCP = false;
292 while (cap->runningTCP)
294 if (!cap->WaitPackets(cap->sockTCP))
301 sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
302 if (!cap->runningTCP)
309 cap->errorStr = "Error accepting connection";
310 printfd(__FILE__, "Error: Error accepting connection\n");
315 if (!cap->WaitPackets(sd))
321 res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
324 if (!cap->runningTCP)
333 // Need to check actual data length and wait all data to receive
338 cap->errorStr = "Invalid data received";
339 printfd(__FILE__, "Error: Invalid data received through TCP\n");
344 cap->ParseBuffer(buf, res);
346 cap->stoppedTCP = true;
350 void NF_CAP::ParseBuffer(uint8_t * buf, int size)
353 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
354 if (htons(hdr->version) != 5)
359 int packets = htons(hdr->count);
361 if (packets < 0 || packets > 30)
366 if (24 + 48 * packets != size)
368 // See 'wrong logic' upper
372 for (int i = 0; i < packets; ++i)
374 NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
376 ip.header.ipHeader.ip_v = 4;
377 ip.header.ipHeader.ip_hl = 5;
378 ip.header.ipHeader.ip_p = data->proto;
379 ip.dataLen = ntohl(data->octets);
380 ip.header.ipHeader.ip_src.s_addr = data->srcAddr;
381 ip.header.ipHeader.ip_dst.s_addr = data->dstAddr;
382 ip.header.sPort = data->srcPort;
383 ip.header.dPort = data->dstPort;
385 traffCnt->Process(ip);
389 bool NF_CAP::WaitPackets(int sd) const
399 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
400 if (res == -1) // Error
404 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
409 if (res == 0) // Timeout