3 #include <netinet/in.h>
4 #include <linux/netfilter.h>
7 #include "raw_ip_packet.h"
8 #include "../../../traffcounter.h"
15 class IPQ_CAP_CREATOR {
29 IPQ_CAP * GetCapturer()
34 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
43 return icc.GetCapturer();
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 const std::string IPQ_CAP::GetVersion() const
50 return "ipq_cap v.1.2";
52 //-----------------------------------------------------------------------------
60 memset(buf, 0, BUFSIZE);
62 //-----------------------------------------------------------------------------
69 errorStr = "Cannot open socket!";
70 printfd(__FILE__, "Cannot open socket\n");
74 if (pthread_create(&thread, NULL, Run, this) == 0)
78 errorStr = "Cannot create thread.";
79 printfd(__FILE__, "Cannot create thread\n");
82 //-----------------------------------------------------------------------------
88 //5 seconds to thread stops itself
89 for (int i = 0; i < 25; i++)
95 //after 5 seconds waiting thread still running. now killing it
98 if (pthread_kill(thread, SIGINT))
100 errorStr = "Cannot kill thread.";
103 for (int i = 0; i < 25 && isRunning; ++i)
109 printfd(__FILE__, "Thread not stopped\n");
113 pthread_join(thread, NULL);
119 //-----------------------------------------------------------------------------
120 void * IPQ_CAP::Run(void * d)
122 RAW_PACKET raw_packet;
124 IPQ_CAP * dc = (IPQ_CAP *)d;
125 dc->isRunning = true;
126 memset(&raw_packet, 0, sizeof(raw_packet));
127 raw_packet.dataLen = -1;
130 int status = dc->IPQCapRead(&raw_packet, 68);
136 dc->traffCnt->Process(raw_packet);
138 dc->isRunning = false;
141 //-----------------------------------------------------------------------------
142 int IPQ_CAP::IPQCapOpen()
144 ipq_h = ipq_create_handle(0, PF_INET);
147 ipq_destroy_handle(ipq_h);
148 errorStr = "Cannot create ipq handle!";
151 int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
154 ipq_destroy_handle(ipq_h);
155 errorStr = "Cannot set IPQ_COPY_PACKET mode!";
160 //-----------------------------------------------------------------------------
161 int IPQ_CAP::IPQCapClose()
163 ipq_destroy_handle(ipq_h);
166 //-----------------------------------------------------------------------------
167 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
169 memset(buf, 0, BUFSIZE);
170 int status = ipq_read(ipq_h, buf, BUFSIZE, 1);
177 if (ipq_message_type(buf) != IPQM_PACKET)
179 static ipq_packet_msg_t * m = ipq_get_packet(buf);
180 memcpy(buffer, m->payload, blen);
181 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
184 //-----------------------------------------------------------------------------