]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
Добавление исходников
[stg.git] / projects / stargazer / plugins / capture / divert_freebsd / divert_cap.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
19 */
20
21 /*
22 $Revision: 1.13 $
23 $Date: 2010/09/10 06:43:03 $
24 */
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <string.h>
30
31 #include <sys/uio.h>
32 #include <sys/time.h>
33 #include <sys/ioctl.h>
34 #include <sys/poll.h>
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include <unistd.h>
41
42 #include "common.h"
43 #include "divert_cap.h"
44
45 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
46
47 //-----------------------------------------------------------------------------
48 struct DIVERT_DATA
49 {
50 int sock;
51 short int port;
52 unsigned char buffer[BUFF_LEN];
53 char iface[10];
54 };
55 //-----------------------------------------------------------------------------
56 pollfd pollddiv;
57 DIVERT_DATA cddiv;  //capture data
58 //-----------------------------------------------------------------------------
59 class DIVERT_CAP_CREATOR
60 {
61 private:
62     DIVERT_CAP * divc;
63
64 public:
65     DIVERT_CAP_CREATOR()
66         : divc(new DIVERT_CAP())
67         {
68         };
69     ~DIVERT_CAP_CREATOR()
70         {
71         delete divc;
72         };
73
74     DIVERT_CAP * GetCapturer()
75     {
76     return divc;
77     };
78 };
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 DIVERT_CAP_CREATOR dcc;
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 BASE_PLUGIN * GetPlugin()
87 {
88 return dcc.GetCapturer();
89 }
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
93 const string DIVERT_CAP::GetVersion() const
94 {
95 return "Divert_cap v.1.0";
96 }
97 //-----------------------------------------------------------------------------
98 DIVERT_CAP::DIVERT_CAP()
99 {
100 isRunning = false;
101 nonstop = false;
102 }
103 //-----------------------------------------------------------------------------
104 void DIVERT_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
105 {
106 traffCnt = tc;
107 }
108 //-----------------------------------------------------------------------------
109 const string & DIVERT_CAP::GetStrError() const
110 {
111 return errorStr;
112 }
113 //-----------------------------------------------------------------------------
114 int DIVERT_CAP::Start()
115 {
116 if (isRunning)
117     return 0;
118
119 if (DivertCapOpen() < 0)
120     {
121     errorStr = "Cannot open socket!";
122     printfd(__FILE__, "Cannot open socket\n");
123     return -1;
124     }
125
126 nonstop = true;
127
128 if (pthread_create(&thread, NULL, Run, this) == 0)
129     {
130     return 0;
131     }
132
133 errorStr = "Cannot create thread.";
134 printfd(__FILE__, "Cannot create thread\n");
135 return -1;
136 }
137 //-----------------------------------------------------------------------------
138 int DIVERT_CAP::Stop()
139 {
140 if (!isRunning)
141     return 0;
142
143 DivertCapClose();
144
145 nonstop = false;
146
147 //5 seconds to thread stops itself
148 int i;
149 for (i = 0; i < 25; i++)
150     {
151     if (!isRunning)
152         break;
153
154     usleep(200000);
155     }
156
157 //after 5 seconds waiting thread still running. now killing it
158 if (isRunning)
159     {
160     if (pthread_kill(thread, SIGINT))
161         {
162         errorStr = "Cannot kill thread.";
163         printfd(__FILE__, "Cannot kill thread\n");
164         return -1;
165         }
166     }
167
168 return 0;
169 }
170 //-----------------------------------------------------------------------------
171 bool DIVERT_CAP::IsRunning()
172 {
173 return isRunning;
174 }
175 //-----------------------------------------------------------------------------
176 void * DIVERT_CAP::Run(void * d)
177 {
178 DIVERT_CAP * dc = (DIVERT_CAP *)d;
179 dc->isRunning = true;
180
181 /*struct ETH_IP
182 {
183 uint16_t    ethHdr[8];
184 RAW_PACKET  rp;
185 char        padding[4];
186 char        padding1[8];
187 };
188
189 ETH_IP * ethIP;
190
191 char ethip[sizeof(ETH_IP)];
192
193 //memset(&ethIP, 0, sizeof(ethIP));
194 memset(&ethip, 0, sizeof(ETH_IP));
195
196 ethIP = (ETH_IP *)&ethip;
197 ethIP->rp.dataLen = -1;
198 */
199 //char * iface = NULL;
200 char buffer[64];
201 while (dc->nonstop)
202     {
203     RAW_PACKET rp;
204     dc->DivertCapRead(buffer, 64, NULL);
205
206     //printf("%x %x %x %x \n", buffer[0], buffer[4], buffer[8], buffer[12]);
207     //printf("%x %x %x %x \n", buffer[16], buffer[20], buffer[24], buffer[28]);
208     //printf("%x %x %x %x \n", buffer[32], buffer[36], buffer[40], buffer[44]);
209
210     if (buffer[12] != 0x8)
211         continue;
212
213     memcpy(rp.pckt, &buffer[14], pcktSize);
214
215     //dc->traffCnt->Process(*((RAW_PACKET*)( &buffer[14] ))); // - too dirty!
216     dc->traffCnt->Process(rp);
217     }
218
219 dc->isRunning = false;
220 return NULL;
221 }
222 //-----------------------------------------------------------------------------
223 uint16_t DIVERT_CAP::GetStartPosition() const
224 {
225 return 10;
226 }
227 //-----------------------------------------------------------------------------
228 uint16_t DIVERT_CAP::GetStopPosition() const
229 {
230 return 10;
231 }
232 //-----------------------------------------------------------------------------
233 /*****************************************************************************/
234 /*****************************************************************************/
235 /*****************************************************************************/
236 /*****************************************************************************/
237 /*****************************************************************************/
238 //-----------------------------------------------------------------------------
239 int DIVERT_CAP::DivertCapOpen()
240 {
241 memset(&pollddiv, 0, sizeof(pollddiv));
242 memset(&cddiv, 0, sizeof(DIVERT_DATA));
243
244 strcpy(cddiv.iface, "foo");
245 cddiv.port = port;
246
247 DivertCapOpen(0);
248 pollddiv.events = POLLIN;
249 pollddiv.fd = cddiv.sock;
250
251 return 0;
252 }
253 //-----------------------------------------------------------------------------
254 int DIVERT_CAP::DivertCapOpen(int)
255 {
256 int ret;
257 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
258 if (cddiv.sock < 0)
259     {
260     errorStr = "Create divert socket error.";
261     printfd(__FILE__, "Cannot create divert socket\n");
262     return -1;
263     }
264
265 struct sockaddr_in divAddr;
266
267 memset(&divAddr, 0, sizeof(divAddr));
268
269 divAddr.sin_family = AF_INET;
270 divAddr.sin_port = htons(cddiv.port);
271 divAddr.sin_addr.s_addr = INADDR_ANY;
272
273 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
274
275 if (ret < 0)
276     {
277     errorStr = "Bind divert socket error.";
278     printfd(__FILE__, "Cannot bind divert socket\n");
279     return -1;
280     }
281
282 return cddiv.sock;
283 }
284 //-----------------------------------------------------------------------------
285 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
286 {
287 poll(&pollddiv, 1, -1);
288
289 if (pollddiv.revents & POLLIN)
290     {
291     DivertCapRead(b, blen, iface, 0);
292     pollddiv.revents = 0;
293     return 0;
294     }
295
296 return 0;
297 }
298 //-----------------------------------------------------------------------------
299 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
300 {
301 static char buf[BUFF_LEN];
302 static struct sockaddr_in divertaddr;
303 static int bytes;
304 static socklen_t divertaddrSize = sizeof(divertaddr);
305
306 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
307                        0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
308     {
309     memcpy(b + 14, buf, blen - 14);
310     b[12] = 0x8;
311
312     if (iface)
313         *iface = cddiv.iface;
314
315     sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
316     }
317
318 return 0;
319 }
320 //-----------------------------------------------------------------------------
321 int DIVERT_CAP::DivertCapClose()
322 {
323 close(cddiv.sock);
324 return 0;
325 }
326 //-----------------------------------------------------------------------------
327 int DIVERT_CAP::ParseSettings()
328 {
329 int p;
330 PARAM_VALUE pv;
331 vector<PARAM_VALUE>::const_iterator pvi;
332
333 pv.param = "Port";
334 pvi = find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
335 if (pvi == settings.moduleParams.end())
336     {
337     port = 15701;
338     return 0;
339     }
340
341 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
342     {
343     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
344     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
345     return -1;
346     }
347
348 port = p;
349
350 return 0;
351 }
352 //-----------------------------------------------------------------------------
353 int DIVERT_CAP::ParseIntInRange(const string & str, int min, int max, int * val)
354 {
355 if (str2x(str.c_str(), *val))
356     {
357     errorStr = "Incorrect value \'" + str + "\'.";
358     return -1;
359     }
360 if (*val < min || *val > max)
361     {
362     errorStr = "Value \'" + str + "\' out of range.";
363     return -1;
364     }
365 return 0;
366 }
367 //-----------------------------------------------------------------------------
368 void DIVERT_CAP::SetSettings(const MODULE_SETTINGS & s)
369 {
370 settings = s;
371 }
372 //-----------------------------------------------------------------------------
373