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>
23 #include <netinet/in.h>
24 #include <linux/netfilter.h>
26 #include "stg/raw_ip_packet.h"
27 #include "stg/traffcounter.h"
28 #include "stg/plugin_creator.h"
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 PLUGIN_CREATOR<IPQ_CAP> icc;
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 return icc.GetPlugin();
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 const std::string IPQ_CAP::GetVersion() const
52 return "ipq_cap v.1.2";
54 //-----------------------------------------------------------------------------
62 memset(buf, 0, BUFSIZE);
64 //-----------------------------------------------------------------------------
71 errorStr = "Cannot open socket!";
72 printfd(__FILE__, "Cannot open socket\n");
76 if (pthread_create(&thread, NULL, Run, this) == 0)
80 errorStr = "Cannot create thread.";
81 printfd(__FILE__, "Cannot create thread\n");
84 //-----------------------------------------------------------------------------
90 //5 seconds to thread stops itself
91 for (int i = 0; i < 25; i++)
97 //after 5 seconds waiting thread still running. now killing it
100 if (pthread_kill(thread, SIGINT))
102 errorStr = "Cannot kill thread.";
105 for (int i = 0; i < 25 && isRunning; ++i)
111 printfd(__FILE__, "Thread not stopped\n");
115 pthread_join(thread, NULL);
121 //-----------------------------------------------------------------------------
122 void * IPQ_CAP::Run(void * d)
124 RAW_PACKET raw_packet;
126 IPQ_CAP * dc = (IPQ_CAP *)d;
127 dc->isRunning = true;
128 memset(&raw_packet, 0, sizeof(raw_packet));
129 raw_packet.dataLen = -1;
132 int status = dc->IPQCapRead(&raw_packet, 68);
138 dc->traffCnt->Process(raw_packet);
140 dc->isRunning = false;
143 //-----------------------------------------------------------------------------
144 int IPQ_CAP::IPQCapOpen()
146 ipq_h = ipq_create_handle(0, PF_INET);
149 ipq_destroy_handle(ipq_h);
150 errorStr = "Cannot create ipq handle!";
153 int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
156 ipq_destroy_handle(ipq_h);
157 errorStr = "Cannot set IPQ_COPY_PACKET mode!";
162 //-----------------------------------------------------------------------------
163 int IPQ_CAP::IPQCapClose()
165 ipq_destroy_handle(ipq_h);
168 //-----------------------------------------------------------------------------
169 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
171 memset(buf, 0, BUFSIZE);
172 int status = ipq_read(ipq_h, buf, BUFSIZE, 1);
179 if (ipq_message_type(buf) != IPQM_PACKET)
181 static ipq_packet_msg_t * m = ipq_get_packet(buf);
182 memcpy(buffer, m->payload, blen);
183 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
186 //-----------------------------------------------------------------------------