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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
21 #include <netinet/in.h>
22 #include <linux/netfilter.h>
27 #include "stg/raw_ip_packet.h"
28 #include "stg/traffcounter.h"
29 #include "stg/plugin_creator.h"
30 #include "stg/common.h"
39 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 PLUGIN_CREATOR<IPQ_CAP> icc;
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
48 return icc.GetPlugin();
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 const std::string IPQ_CAP::GetVersion() const
55 return "ipq_cap v.1.2";
57 //-----------------------------------------------------------------------------
68 memset(buf, 0, BUFSIZE);
70 //-----------------------------------------------------------------------------
77 errorStr = "Cannot open socket!";
78 printfd(__FILE__, "Cannot open socket\n");
82 if (pthread_create(&thread, NULL, Run, this) == 0)
86 errorStr = "Cannot create thread.";
87 printfd(__FILE__, "Cannot create thread\n");
90 //-----------------------------------------------------------------------------
96 //5 seconds to thread stops itself
97 for (int i = 0; i < 25; i++)
101 struct timespec ts = {0, 200000000};
102 nanosleep(&ts, NULL);
104 //after 5 seconds waiting thread still running. now killing it
107 if (pthread_kill(thread, SIGINT))
109 errorStr = "Cannot kill thread.";
112 for (int i = 0; i < 25 && isRunning; ++i)
114 struct timespec ts = {0, 200000000};
115 nanosleep(&ts, NULL);
119 printfd(__FILE__, "Thread not stopped\n");
123 pthread_join(thread, NULL);
129 //-----------------------------------------------------------------------------
130 void * IPQ_CAP::Run(void * d)
133 sigfillset(&signalSet);
134 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
136 RAW_PACKET raw_packet;
138 IPQ_CAP * dc = static_cast<IPQ_CAP *>(d);
139 dc->isRunning = true;
140 memset(&raw_packet, 0, sizeof(raw_packet));
141 raw_packet.dataLen = -1;
144 int status = dc->IPQCapRead(&raw_packet, 68);
150 dc->traffCnt->Process(raw_packet);
152 dc->isRunning = false;
155 //-----------------------------------------------------------------------------
156 int IPQ_CAP::IPQCapOpen()
158 ipq_h = ipq_create_handle(0, PF_INET);
161 ipq_destroy_handle(ipq_h);
162 errorStr = "Cannot create ipq handle!";
165 int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
168 ipq_destroy_handle(ipq_h);
169 errorStr = "Cannot set IPQ_COPY_PACKET mode!";
174 //-----------------------------------------------------------------------------
175 int IPQ_CAP::IPQCapClose()
177 ipq_destroy_handle(ipq_h);
180 //-----------------------------------------------------------------------------
181 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
183 memset(buf, 0, BUFSIZE);
184 int status = ipq_read(ipq_h, buf, BUFSIZE, 1);
191 if (ipq_message_type(buf) != IPQM_PACKET)
193 static ipq_packet_msg_t * m = ipq_get_packet(buf);
194 memcpy(buffer, m->payload, blen);
195 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
198 //-----------------------------------------------------------------------------