3 #include <netinet/in.h>
4 #include <linux/netfilter.h>
7 #include "raw_ip_packet.h"
14 class IPQ_CAP_CREATOR {
28 IPQ_CAP * GetCapturer()
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 return icc.GetCapturer();
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 const std::string IPQ_CAP::GetVersion() const
49 return "ipq_cap v.1.2";
51 //-----------------------------------------------------------------------------
59 memset(buf, 0, BUFSIZE);
61 //-----------------------------------------------------------------------------
62 void IPQ_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
66 //-----------------------------------------------------------------------------
67 const std::string & IPQ_CAP::GetStrError() const
71 //-----------------------------------------------------------------------------
78 errorStr = "Cannot open socket!";
79 printfd(__FILE__, "Cannot open socket\n");
83 if (pthread_create(&thread, NULL, Run, this) == 0)
87 errorStr = "Cannot create thread.";
88 printfd(__FILE__, "Cannot create thread\n");
91 //-----------------------------------------------------------------------------
97 //5 seconds to thread stops itself
98 for (int i = 0; i < 25; i++)
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)
118 printfd(__FILE__, "Thread not stopped\n");
122 pthread_join(thread, NULL);
128 //-----------------------------------------------------------------------------
129 bool IPQ_CAP::IsRunning()
133 //-----------------------------------------------------------------------------
134 void * IPQ_CAP::Run(void * d)
136 RAW_PACKET raw_packet;
139 IPQ_CAP * dc = (IPQ_CAP *)d;
140 dc->isRunning = true;
141 memset(&raw_packet, 0, sizeof(raw_packet));
142 raw_packet.dataLen = -1;
145 status = dc->IPQCapRead(&raw_packet, 68);
151 dc->traffCnt->Process(raw_packet);
153 dc->isRunning = false;
156 //-----------------------------------------------------------------------------
157 uint16_t IPQ_CAP::GetStartPosition() const
161 //-----------------------------------------------------------------------------
162 uint16_t IPQ_CAP::GetStopPosition() const
166 //-----------------------------------------------------------------------------
167 int IPQ_CAP::IPQCapOpen()
171 ipq_h = ipq_create_handle(0, PF_INET);
174 ipq_destroy_handle(ipq_h);
175 errorStr = "Cannot create ipq handle!";
178 status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
181 ipq_destroy_handle(ipq_h);
182 errorStr = "Cannot set IPQ_COPY_PACKET mode!";
187 //-----------------------------------------------------------------------------
188 int IPQ_CAP::IPQCapClose()
190 ipq_destroy_handle(ipq_h);
193 //-----------------------------------------------------------------------------
194 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
197 static ipq_packet_msg_t *m;
199 memset(buf, 0, BUFSIZE);
200 status = ipq_read(ipq_h, buf, BUFSIZE, 1);
207 if (ipq_message_type(buf) != IPQM_PACKET)
209 m = ipq_get_packet(buf);
210 memcpy(buffer, m->payload, blen);
211 ipq_set_verdict(ipq_h, m->packet_id, NF_ACCEPT, 0, NULL);
214 //-----------------------------------------------------------------------------