]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp
Добавлено игнорирование файлов deps
[stg.git] / projects / stargazer / plugins / capture / ipq_linux / ipq_cap.cpp
1 #include <signal.h>
2 #include <cerrno>
3 #include <netinet/in.h>
4 #include <linux/netfilter.h>
5
6 #include "ipq_cap.h"
7 #include "raw_ip_packet.h"
8
9 extern "C"
10     {
11     #include "libipq.h"
12     }
13
14 class IPQ_CAP_CREATOR
15 {
16 private:
17     IPQ_CAP * ic;
18
19 public:
20     IPQ_CAP_CREATOR()
21         : ic(new IPQ_CAP())
22         {
23         };
24     ~IPQ_CAP_CREATOR()
25         {
26         delete ic;
27         };
28
29     IPQ_CAP * GetCapturer()
30         {
31         return ic;
32         };
33 };
34 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
37 IPQ_CAP_CREATOR icc;
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 BASE_PLUGIN * GetPlugin()
42 {
43 return icc.GetCapturer();
44 }
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 const string IPQ_CAP::GetVersion() const
49 {
50 return "ipq_cap v.1.2";
51 }
52 //-----------------------------------------------------------------------------
53 IPQ_CAP::IPQ_CAP()
54 {
55 isRunning = false;
56 nonstop = false;
57 }
58 //-----------------------------------------------------------------------------
59 void IPQ_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
60 {
61 traffCnt = tc;
62 }
63 //-----------------------------------------------------------------------------
64 const string & IPQ_CAP::GetStrError() const
65 {
66 return errorStr;
67 }
68 //-----------------------------------------------------------------------------
69 int IPQ_CAP::Start()
70 {
71 if (isRunning)
72     return 0;
73 if (IPQCapOpen() < 0)
74     {
75     errorStr = "Cannot open socket!";
76     printfd(__FILE__, "Cannot open socket\n");
77     return -1;
78     }
79 nonstop = true;
80 if (pthread_create(&thread, NULL, Run, this) == 0)
81     {
82     return 0;
83     }
84 errorStr = "Cannot create thread.";
85 printfd(__FILE__, "Cannot create thread\n");
86 return -1;
87 }
88 //-----------------------------------------------------------------------------
89 int IPQ_CAP::Stop()
90 {
91 if (!isRunning)
92     return 0;
93 nonstop = false;
94 //5 seconds to thread stops itself
95 for (int i = 0; i < 25; i++)
96     {
97     if (!isRunning)
98         break;
99     usleep(200000);
100     }
101 //after 5 seconds waiting thread still running. now killing it
102 if (isRunning)
103     {
104     if (pthread_kill(thread, SIGINT))
105         {
106         errorStr = "Cannot kill thread.";
107         return -1;
108         }
109     for (int i = 0; i < 25 && isRunning; ++i)
110         {
111         usleep(200000);
112         }
113     if (isRunning)
114         {
115         printfd(__FILE__, "Thread not stopped\n");
116         }
117     else
118         {
119         pthread_join(thread, NULL);
120         }
121     }
122 IPQCapClose();
123 return 0;
124 }
125 //-----------------------------------------------------------------------------
126 bool IPQ_CAP::IsRunning()
127 {
128 return isRunning;
129 }
130 //-----------------------------------------------------------------------------
131 void * IPQ_CAP::Run(void * d)
132 {
133 RAW_PACKET raw_packet;
134 int status;
135
136 IPQ_CAP * dc = (IPQ_CAP *)d;
137 dc->isRunning = true;
138 memset(&raw_packet, 0, sizeof(raw_packet));
139 raw_packet.dataLen = -1;
140 while (dc->nonstop)
141     {
142     status = dc->IPQCapRead(&raw_packet, 68);
143     if (status == -1 ||
144         status == -2 ||
145         status == -3 ||
146         status == -4)
147         continue;
148     dc->traffCnt->Process(raw_packet);
149     }
150 dc->isRunning = false;
151 return NULL;
152 }
153 //-----------------------------------------------------------------------------
154 uint16_t IPQ_CAP::GetStartPosition() const
155 {
156 return 0;
157 }
158 //-----------------------------------------------------------------------------
159 uint16_t IPQ_CAP::GetStopPosition() const
160 {
161 return 0;
162 }
163 //-----------------------------------------------------------------------------
164 int IPQ_CAP::IPQCapOpen()
165 {
166 int status;
167
168 ipq_h = ipq_create_handle(0, PF_INET);
169 if (ipq_h == NULL)
170     {
171     ipq_destroy_handle(ipq_h);
172     errorStr = "Cannot create ipq handle!";
173     return -1;
174     }
175 status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN);
176 if (status < 0)
177     {
178     ipq_destroy_handle(ipq_h);
179     errorStr = "Cannot set IPQ_COPY_PACKET mode!";
180     return -1;
181     }
182 return 0;
183 }
184 //-----------------------------------------------------------------------------
185 int IPQ_CAP::IPQCapClose()
186 {
187 ipq_destroy_handle(ipq_h);
188 return 0;
189 }
190 //-----------------------------------------------------------------------------
191 int IPQ_CAP::IPQCapRead(void * buffer, int blen)
192 {
193 int status;
194 static ipq_packet_msg_t *m;
195
196 memset(buf, 0, BUFSIZE);
197 status = ipq_read(ipq_h, buf, BUFSIZE, 1);
198 if (status == 0)
199     return -4;
200 if (errno == EINTR)
201     return -3;
202 if (status < 0)
203     return -1;
204 if (ipq_message_type(buf) != IPQM_PACKET)
205     return -2;
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);
209 return 0;
210 }
211 //-----------------------------------------------------------------------------