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();
 
  69       logger(GetPluginLogger(GetStgLogger(), "cap_nf"))
 
  77 int NF_CAP::ParseSettings()
 
  79 std::vector<PARAM_VALUE>::iterator it;
 
  80 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
 
  82     if (it->param == "TCPPort")
 
  84         if (str2x(it->value[0], portT))
 
  86             errorStr = "Invalid TCPPort value";
 
  87             printfd(__FILE__, "Error: Invalid TCPPort value\n");
 
  92     if (it->param == "UDPPort")
 
  94         if (str2x(it->value[0], portU))
 
  96             errorStr = "Invalid UDPPort value";
 
  97             printfd(__FILE__, "Error: Invalid UDPPort value\n");
 
 102     printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
 
 116     if (pthread_create(&tidUDP, NULL, RunUDP, this))
 
 120         errorStr = "Cannot create UDP thread";
 
 121         printfd(__FILE__, "Error: Cannot create UDP thread\n");
 
 132     if (pthread_create(&tidTCP, NULL, RunTCP, this))
 
 136         errorStr = "Cannot create TCP thread";
 
 137         printfd(__FILE__, "Error: Cannot create TCP thread\n");
 
 146 runningTCP = runningUDP = false;
 
 147 if (portU && !stoppedUDP)
 
 150     for (int i = 0; i < 25 && !stoppedUDP; ++i)
 
 152         struct timespec ts = {0, 200000000};
 
 153         nanosleep(&ts, NULL);
 
 157         pthread_join(tidUDP, NULL);
 
 161         if (pthread_kill(tidUDP, SIGUSR1))
 
 163             errorStr = "Error sending signal to UDP thread";
 
 164             printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
 
 167         printfd(__FILE__, "UDP thread NOT stopped\n");
 
 170 if (portT && !stoppedTCP)
 
 173     for (int i = 0; i < 25 && !stoppedTCP; ++i)
 
 175         struct timespec ts = {0, 200000000};
 
 176         nanosleep(&ts, NULL);
 
 180         pthread_join(tidTCP, NULL);
 
 184         if (pthread_kill(tidTCP, SIGUSR1))
 
 186             errorStr = "Error sending signal to TCP thread";
 
 187             printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
 
 190         printfd(__FILE__, "TCP thread NOT stopped\n");
 
 196 bool NF_CAP::OpenUDP()
 
 198 struct sockaddr_in sin;
 
 199 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
 
 202     errorStr = "Error opening UDP socket";
 
 203     printfd(__FILE__, "Error: Error opening UDP socket\n");
 
 206 sin.sin_family = AF_INET;
 
 207 sin.sin_port = htons(portU);
 
 208 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
 
 209 if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin)))
 
 211     errorStr = "Error binding UDP socket";
 
 212     printfd(__FILE__, "Error: Error binding UDP socket\n");
 
 218 bool NF_CAP::OpenTCP()
 
 220 struct sockaddr_in sin;
 
 221 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
 
 224     errorStr = "Error opening TCP socket";
 
 225     printfd(__FILE__, "Error: Error opening TCP socket\n");
 
 228 sin.sin_family = AF_INET;
 
 229 sin.sin_port = htons(portT);
 
 230 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
 
 231 if (bind(sockTCP, (struct sockaddr *)&sin, sizeof(sin)))
 
 233     errorStr = "Error binding TCP socket";
 
 234     printfd(__FILE__, "Error: Error binding TCP socket\n");
 
 237 if (listen(sockTCP, 1))
 
 239     errorStr = "Error listening on TCP socket";
 
 240     printfd(__FILE__, "Error: Error listening TCP socket\n");
 
 246 void * NF_CAP::RunUDP(void * c)
 
 249 sigfillset(&signalSet);
 
 250 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
 252 NF_CAP * cap = static_cast<NF_CAP *>(c);
 
 253 uint8_t buf[BUF_SIZE];
 
 255 struct sockaddr_in sin;
 
 257 cap->stoppedUDP = false;
 
 258 while (cap->runningUDP)
 
 260     if (!WaitPackets(cap->sockUDP))
 
 267     res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
 
 268     if (!cap->runningUDP)
 
 280             cap->errorStr = "Invalid data received";
 
 281             printfd(__FILE__, "Error: Invalid data received through UDP\n");
 
 286     cap->ParseBuffer(buf, res);
 
 288 cap->stoppedUDP = true;
 
 292 void * NF_CAP::RunTCP(void * c)
 
 295 sigfillset(&signalSet);
 
 296 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
 298 NF_CAP * cap = static_cast<NF_CAP *>(c);
 
 299 uint8_t buf[BUF_SIZE];
 
 302 struct sockaddr_in sin;
 
 304 cap->stoppedTCP = false;
 
 305 while (cap->runningTCP)
 
 307     if (!WaitPackets(cap->sockTCP))
 
 314     sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
 
 315     if (!cap->runningTCP)
 
 322             cap->errorStr = "Error accepting connection";
 
 323             printfd(__FILE__, "Error: Error accepting connection\n");
 
 328     if (!WaitPackets(sd))
 
 334     res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
 
 337     if (!cap->runningTCP)
 
 346     // Need to check actual data length and wait all data to receive
 
 351             cap->errorStr = "Invalid data received";
 
 352             printfd(__FILE__, "Error: Invalid data received through TCP\n");
 
 357     cap->ParseBuffer(buf, res);
 
 359 cap->stoppedTCP = true;
 
 363 void NF_CAP::ParseBuffer(uint8_t * buf, int size)
 
 366 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
 
 367 if (htons(hdr->version) != 5)
 
 372 int packets = htons(hdr->count);
 
 374 if (packets < 0 || packets > 30)
 
 379 if (24 + 48 * packets != size)
 
 381     // See 'wrong logic' upper
 
 385 for (int i = 0; i < packets; ++i)
 
 387     NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
 
 389     ip.rawPacket.header.ipHeader.ip_v = 4;
 
 390     ip.rawPacket.header.ipHeader.ip_hl = 5;
 
 391     ip.rawPacket.header.ipHeader.ip_p = data->proto;
 
 392     ip.dataLen = ntohl(data->octets);
 
 393     ip.rawPacket.header.ipHeader.ip_src.s_addr = data->srcAddr;
 
 394     ip.rawPacket.header.ipHeader.ip_dst.s_addr = data->dstAddr;
 
 395     ip.rawPacket.header.sPort = data->srcPort;
 
 396     ip.rawPacket.header.dPort = data->dstPort;
 
 398     traffCnt->Process(ip);