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>
28 #include "stg/raw_ip_packet.h"
29 #include "stg/traffcounter.h"
30 #include "stg/plugin_creator.h"
31 #include "stg/common.h"
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 PLUGIN_CREATOR<IPQ_CAP> icc;
48 extern "C" PLUGIN * GetPlugin();
49 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
54 return icc.GetPlugin();
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 std::string IPQ_CAP::GetVersion() const
61 return "cap_ipq v.1.2";
63 //-----------------------------------------------------------------------------
73 logger(GetPluginLogger(GetStgLogger(), "cap_ipq"))
75 memset(buf, 0, BUFSIZE);
77 //-----------------------------------------------------------------------------
84 errorStr = "Cannot open socket!";
85 printfd(__FILE__, "Cannot open socket\n");
89 if (pthread_create(&thread, NULL, Run, this) == 0)
93 errorStr = "Cannot create thread.";
94 printfd(__FILE__, "Cannot create thread\n");
97 //-----------------------------------------------------------------------------
103 //5 seconds to thread stops itself
104 for (int i = 0; i < 25; i++)
108 struct timespec ts = {0, 200000000};
109 nanosleep(&ts, NULL);
111 //after 5 seconds waiting thread still running. now killing it
114 if (pthread_kill(thread, SIGINT))
116 errorStr = "Cannot kill thread.";
119 for (int i = 0; i < 25 && isRunning; ++i)
121 struct timespec ts = {0, 200000000};
122 nanosleep(&ts, NULL);
126 printfd(__FILE__, "Thread not stopped\n");
130 pthread_join(thread, NULL);
136 //-----------------------------------------------------------------------------
137 void * IPQ_CAP::Run(void * d)
140 sigfillset(&signalSet);
141 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
143 RAW_PACKET raw_packet;
145 IPQ_CAP * dc = static_cast<IPQ_CAP *>(d);
146 dc->isRunning = true;
147 memset(&raw_packet, 0, sizeof(raw_packet));
148 raw_packet.dataLen = -1;
151 int status = dc->IPQCapRead(&raw_packet, 68);
157 dc->traffCnt->Process(raw_packet);
159 dc->isRunning = false;
162 //-----------------------------------------------------------------------------
163 int IPQ_CAP::IPQCapOpen()
165 ipq_h = ipq_create_handle(0, PF_INET);
168 ipq_destroy_handle(ipq_h);
169 logger("Cannot create IPQ handle. Error: '%s', '%s'", ipq_errstr(), strerror(errno));
170 errorStr = "Cannot create ipq handle!";
173 int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
176 ipq_destroy_handle(ipq_h);
177 logger("Cannot set IPQ_COPY_PACKET mode.");
178 errorStr = "Cannot set IPQ_COPY_PACKET mode!";
183 //-----------------------------------------------------------------------------
184 int IPQ_CAP::IPQCapClose()
186 ipq_destroy_handle(ipq_h);
189 //-----------------------------------------------------------------------------
190 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
192 memset(buf, 0, BUFSIZE);
193 int status = ipq_read(ipq_h, buf, BUFSIZE, 1);
200 if (ipq_message_type(buf) != IPQM_PACKET)
202 static ipq_packet_msg_t * m = ipq_get_packet(buf);
203 memcpy(buffer, m->payload, blen);
204 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
207 //-----------------------------------------------------------------------------