3 #include <netinet/in.h>
4 #include <linux/netfilter.h>
7 #include "raw_ip_packet.h"
29 IPQ_CAP * GetCapturer()
34 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 BASE_PLUGIN * GetPlugin()
43 return icc.GetCapturer();
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 const string IPQ_CAP::GetVersion() const
50 return "ipq_cap v.1.2";
52 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 void IPQ_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
63 //-----------------------------------------------------------------------------
64 const string & IPQ_CAP::GetStrError() const
68 //-----------------------------------------------------------------------------
75 errorStr = "Cannot open socket!";
76 printfd(__FILE__, "Cannot open socket\n");
80 if (pthread_create(&thread, NULL, Run, this) == 0)
84 errorStr = "Cannot create thread.";
85 printfd(__FILE__, "Cannot create thread\n");
88 //-----------------------------------------------------------------------------
94 //5 seconds to thread stops itself
95 for (int i = 0; i < 25; i++)
101 //after 5 seconds waiting thread still running. now killing it
104 if (pthread_kill(thread, SIGINT))
106 errorStr = "Cannot kill thread.";
109 for (int i = 0; i < 25 && isRunning; ++i)
115 printfd(__FILE__, "Thread not stopped\n");
119 pthread_join(thread, NULL);
125 //-----------------------------------------------------------------------------
126 bool IPQ_CAP::IsRunning()
130 //-----------------------------------------------------------------------------
131 void * IPQ_CAP::Run(void * d)
133 RAW_PACKET raw_packet;
136 IPQ_CAP * dc = (IPQ_CAP *)d;
137 dc->isRunning = true;
138 memset(&raw_packet, 0, sizeof(raw_packet));
139 raw_packet.dataLen = -1;
142 status = dc->IPQCapRead(&raw_packet, 68);
148 dc->traffCnt->Process(raw_packet);
150 dc->isRunning = false;
153 //-----------------------------------------------------------------------------
154 uint16_t IPQ_CAP::GetStartPosition() const
158 //-----------------------------------------------------------------------------
159 uint16_t IPQ_CAP::GetStopPosition() const
163 //-----------------------------------------------------------------------------
164 int IPQ_CAP::IPQCapOpen()
168 ipq_h = ipq_create_handle(0, PF_INET);
171 ipq_destroy_handle(ipq_h);
172 errorStr = "Cannot create ipq handle!";
175 status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
178 ipq_destroy_handle(ipq_h);
179 errorStr = "Cannot set IPQ_COPY_PACKET mode!";
184 //-----------------------------------------------------------------------------
185 int IPQ_CAP::IPQCapClose()
187 ipq_destroy_handle(ipq_h);
190 //-----------------------------------------------------------------------------
191 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
194 static ipq_packet_msg_t *m;
196 memset(buf, 0, BUFSIZE);
197 status = ipq_read(ipq_h, buf, BUFSIZE, 1);
204 if (ipq_message_type(buf) != IPQM_PACKET)
206 m = ipq_get_packet(buf);
207 memcpy(buffer, m->payload, blen);
208 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
211 //-----------------------------------------------------------------------------