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>
44 #include "raw_ip_packet.h"
46 #include "../../../traffcounter.h"
61 NF_CAP * GetCapturer() { return nf; };
68 return cnc.GetCapturer();
90 int NF_CAP::ParseSettings()
92 std::vector<PARAM_VALUE>::iterator it;
93 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
95 if (it->param == "TCPPort")
97 if (str2x(it->value[0], portT))
99 errorStr = "Invalid TCPPort value";
100 printfd(__FILE__, "Error: Invalid TCPPort value\n");
105 if (it->param == "UDPPort")
107 if (str2x(it->value[0], portU))
109 errorStr = "Invalid UDPPort value";
110 printfd(__FILE__, "Error: Invalid UDPPort value\n");
115 printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
129 if (pthread_create(&tidUDP, NULL, RunUDP, this))
133 errorStr = "Cannot create UDP thread";
134 printfd(__FILE__, "Error: Cannot create UDP thread\n");
145 if (pthread_create(&tidTCP, NULL, RunTCP, this))
149 errorStr = "Cannot create TCP thread";
150 printfd(__FILE__, "Error: Cannot create TCP thread\n");
159 runningTCP = runningUDP = false;
160 if (portU && !stoppedUDP)
163 for (int i = 0; i < 25 && !stoppedUDP; ++i)
169 pthread_join(tidUDP, NULL);
173 if (pthread_kill(tidUDP, SIGUSR1))
175 errorStr = "Error sending signal to UDP thread";
176 printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
179 printfd(__FILE__, "UDP thread NOT stopped\n");
182 if (portT && !stoppedTCP)
185 for (int i = 0; i < 25 && !stoppedTCP; ++i)
191 pthread_join(tidTCP, NULL);
195 if (pthread_kill(tidTCP, SIGUSR1))
197 errorStr = "Error sending signal to TCP thread";
198 printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
201 printfd(__FILE__, "TCP thread NOT stopped\n");
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 printfd(__FILE__, "Error: Error opening UDP socket\n");
217 sin.sin_family = AF_INET;
218 sin.sin_port = htons(portU);
219 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
220 if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin)))
222 errorStr = "Error binding UDP socket";
223 printfd(__FILE__, "Error: Error binding UDP socket\n");
229 bool NF_CAP::OpenTCP()
231 struct sockaddr_in sin;
232 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
235 errorStr = "Error opening TCP socket";
236 printfd(__FILE__, "Error: Error opening TCP socket\n");
239 sin.sin_family = AF_INET;
240 sin.sin_port = htons(portT);
241 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
242 if (bind(sockTCP, (struct sockaddr *)&sin, sizeof(sin)))
244 errorStr = "Error binding TCP socket";
245 printfd(__FILE__, "Error: Error binding TCP socket\n");
248 if (listen(sockTCP, 1))
250 errorStr = "Error listening on TCP socket";
251 printfd(__FILE__, "Error: Error listening TCP socket\n");
257 void * NF_CAP::RunUDP(void * c)
259 NF_CAP * cap = static_cast<NF_CAP *>(c);
260 uint8_t buf[BUF_SIZE];
262 struct sockaddr_in sin;
264 cap->stoppedUDP = false;
265 while (cap->runningUDP)
267 if (!cap->WaitPackets(cap->sockUDP))
274 res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
275 if (!cap->runningUDP)
285 // Need to check actual data length and wait all data to receive
290 cap->errorStr = "Invalid data received";
291 printfd(__FILE__, "Error: Invalid data received through UDP\n");
296 cap->ParseBuffer(buf, res);
298 cap->stoppedUDP = true;
302 void * NF_CAP::RunTCP(void * c)
304 NF_CAP * cap = static_cast<NF_CAP *>(c);
305 uint8_t buf[BUF_SIZE];
308 struct sockaddr_in sin;
310 cap->stoppedTCP = false;
311 while (cap->runningTCP)
313 if (!cap->WaitPackets(cap->sockTCP))
320 sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
321 if (!cap->runningTCP)
328 cap->errorStr = "Error accepting connection";
329 printfd(__FILE__, "Error: Error accepting connection\n");
334 if (!cap->WaitPackets(sd))
340 res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
343 if (!cap->runningTCP)
352 // Need to check actual data length and wait all data to receive
357 cap->errorStr = "Invalid data received";
358 printfd(__FILE__, "Error: Invalid data received through TCP\n");
363 cap->ParseBuffer(buf, res);
365 cap->stoppedTCP = true;
369 void NF_CAP::ParseBuffer(uint8_t * buf, int size)
372 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
373 if (htons(hdr->version) != 5)
378 int packets = htons(hdr->count);
380 if (packets < 0 || packets > 30)
385 if (24 + 48 * packets != size)
387 // See 'wrong logic' upper
391 for (int i = 0; i < packets; ++i)
393 NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
395 /*ip.pckt[0] = 4 << 4;
397 ip.pckt[9] = data->proto;
398 ip.dataLen = ntohl(data->octets);
399 *(uint32_t *)(ip.pckt + 12) = data->srcAddr;
400 *(uint32_t *)(ip.pckt + 16) = data->dstAddr;
401 *(uint16_t *)(ip.pckt + 20) = data->srcPort;
402 *(uint16_t *)(ip.pckt + 22) = data->dstPort;*/
403 ip.header.ipHeader.ip_v = 4;
404 ip.header.ipHeader.ip_hl = 5;
405 ip.header.ipHeader.ip_p = data->proto;
406 ip.dataLen = ntohl(data->octets);
407 ip.header.ipHeader.ip_src.s_addr = data->srcAddr;
408 ip.header.ipHeader.ip_dst.s_addr = data->dstAddr;
409 ip.header.sPort = data->srcPort;
410 ip.header.dPort = data->dstPort;
412 traffCnt->Process(ip);
416 bool NF_CAP::WaitPackets(int sd) const
426 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
427 if (res == -1) // Error
431 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
436 if (res == 0) // Timeout