]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp
Rename BASE_AUTH and BASE_STORE to AUTH and STORE
[stg.git] / projects / stargazer / plugins / capture / ether_freebsd / ether_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 Date: 18.09.2002
19 */
20
21 /*
22 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
23 */
24
25 /*
26 $Revision: 1.19 $
27 $Date: 2009/03/24 11:20:15 $
28 $Author: faust $
29 */
30
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <sys/ioctl.h>
36 #include <sys/poll.h>
37
38 #include <net/bpf.h>
39 #include <net/if.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <signal.h>
49 #include <unistd.h>
50
51 #include "ether_cap.h"
52 #include "common.h"
53 #include "raw_ip_packet.h"
54
55 //#define CAP_DEBUG 1
56 //-----------------------------------------------------------------------------
57
58 //-----------------------------------------------------------------------------
59 class BPF_CAP_CREATOR {
60 private:
61     BPF_CAP * bpfc;
62
63 public:
64     BPF_CAP_CREATOR()
65         : bpfc(new BPF_CAP())
66         {
67         };
68     ~BPF_CAP_CREATOR()
69         {
70         delete bpfc;
71         };
72
73     BPF_CAP * GetCapturer()
74     {
75     return bpfc;
76     };
77 };
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 BPF_CAP_CREATOR bcc;
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 PLUGIN * GetPlugin()
86 {
87 return bcc.GetCapturer();
88 }
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
93 {
94 //char sep[]= ", \t\n\r";
95 //char *s;
96 std::string ifaces;
97 //char * str;
98 //char *p;
99
100 iface.erase(iface.begin(), iface.end());
101
102 //PARAM_VALUE pv;
103 //pv.param = "WorkDir";
104 //vector<PARAM_VALUE>::const_iterator pvi;
105
106 if (s.moduleParams.empty())
107     {
108     errorStr = "Parameter \'iface\' not found.";
109     printfd(__FILE__, "Parameter 'iface' not found\n");
110     return -1;
111     }
112
113 for (unsigned i = 0; i < s.moduleParams.size(); i++)
114     {
115     if (s.moduleParams[i].param != "iface")
116         {
117         errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
118         printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
119         return -1;
120         }
121     for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
122         {
123         iface.push_back(s.moduleParams[i].value[j]);
124         }
125     }
126
127 /*if (cf.ReadString("Iface", &ifaces, "NoIface") < 0)
128     {
129     errorStr = "Cannot read parameter \'Iface\' from " + cf.GetFileName();
130     return -1;
131     }
132
133 str = new char[ifaces.size() + 1];
134 strcpy(str, ifaces.c_str());
135 p = str;
136
137 while((s = strtok(p, sep)))
138     {
139     printfd(__FILE__, "iface[] = %s\n", s);
140     p = NULL;
141     iface.push_back(s);
142     //strncpy(iface[i++], s, DEV_NAME_LEN);
143     //devNum++;
144     }
145
146 delete[] str;
147 if (!ifaces.size())
148     {
149     errorStr = "Error read parameter \'Iface\' from " + cf.GetFileName();
150     return -1;
151     }*/
152
153 return 0;
154 }
155 //-----------------------------------------------------------------------------
156 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
157 {
158 if (num >= iface.size())
159     {
160     return "";
161     }
162 return iface[num];
163 }
164 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
167 const std::string BPF_CAP::GetVersion() const
168 {
169 return "bpf_cap v.1.0";
170 }
171 //-----------------------------------------------------------------------------
172 BPF_CAP::BPF_CAP()
173     : nonstop(false),
174       isRunning(false),
175       capSock(-1),
176       traffCnt(NULL)
177 {
178 }
179 //-----------------------------------------------------------------------------
180 void BPF_CAP::SetSettings(const MODULE_SETTINGS & s)
181 {
182 settings = s;
183 }
184 //-----------------------------------------------------------------------------
185 int BPF_CAP::ParseSettings()
186 {
187 int ret = capSettings.ParseSettings(settings);
188 if (ret)
189     {
190     errorStr = capSettings.GetStrError();
191     return ret;
192     }
193 return 0;
194 }
195 //-----------------------------------------------------------------------------
196 void BPF_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
197 {
198 traffCnt = tc;
199 }
200 //-----------------------------------------------------------------------------
201 const std::string & BPF_CAP::GetStrError() const
202 {
203 return errorStr;
204 }
205 //-----------------------------------------------------------------------------
206 int BPF_CAP::Start()
207 {
208 if (isRunning)
209     return 0;
210
211 if (BPFCapOpen() < 0)
212     {
213     //errorStr = "Cannot open bpf device!";
214     return -1;
215     }
216
217 nonstop = true;
218
219 if (pthread_create(&thread, NULL, Run, this) == 0)
220     {
221     return 0;
222     }
223
224 errorStr = "Cannot create thread.";
225 printfd(__FILE__, "Cannot create thread\n");
226 return -1;
227 }
228 //-----------------------------------------------------------------------------
229 int BPF_CAP::Stop()
230 {
231 if (!isRunning)
232     return 0;
233
234 BPFCapClose();
235
236 nonstop = false;
237
238 //5 seconds to thread stops itself
239 int i;
240 for (i = 0; i < 25; i++)
241     {
242     if (!isRunning)
243         break;
244
245     usleep(200000);
246     }
247
248 //after 5 seconds waiting thread still running. now killing it
249 if (isRunning)
250     {
251     //TODO pthread_cancel()
252     if (pthread_kill(thread, SIGINT))
253         {
254         errorStr = "Cannot kill thread.";
255         printfd(__FILE__, "Cannot kill thread\n");
256         return -1;
257         }
258     }
259
260 return 0;
261 }
262 //-----------------------------------------------------------------------------
263 bool BPF_CAP::IsRunning()
264 {
265 return isRunning;
266 }
267 //-----------------------------------------------------------------------------
268 void * BPF_CAP::Run(void * d)
269 {
270 BPF_CAP * dc = (BPF_CAP *)d;
271 dc->isRunning = true;
272
273 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
274
275 RAW_PACKET *  rpp = (RAW_PACKET *)&hdr[14];
276 memset(hdr, 0, sizeof(hdr));
277
278 rpp->dataLen = -1;
279 char * iface;
280
281 while (dc->nonstop)
282     {
283     dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
284
285     if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
286     {
287         continue;
288     }
289
290     dc->traffCnt->Process(*rpp);
291     }
292
293 dc->isRunning = false;
294 return NULL;
295 }
296 //-----------------------------------------------------------------------------
297 uint16_t BPF_CAP::GetStartPosition() const
298 {
299 return 0;
300 }
301 //-----------------------------------------------------------------------------
302 uint16_t BPF_CAP::GetStopPosition() const
303 {
304 return 0;
305 }
306 //-----------------------------------------------------------------------------
307 int BPF_CAP::BPFCapOpen()
308 {
309 //for (int i = 0; i < settings->devNum; i++)
310 int i = 0;
311 BPF_DATA bd;
312 pollfd pd;
313
314 while ((bd.iface = capSettings.GetIface(i)) != "")
315     {
316     bpfData.push_back(bd);
317     if (BPFCapOpen(&bpfData[i]) < 0)
318     {
319     return -1;
320     }
321
322     pd.events = POLLIN;
323     pd.fd = bpfData[i].fd;
324     polld.push_back(pd);
325     i++;
326     }
327
328 return 0;
329 }
330 //-----------------------------------------------------------------------------
331 //int BPF_CAP::BPFCapOpen(string ifaceToOpen)
332 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
333 {
334 char devbpf[20];
335 int i = 0;
336 int l = BUFF_LEN;
337 int im = 1;
338 struct ifreq ifr;
339
340 do  {
341     sprintf(devbpf, "/dev/bpf%d", i);
342     i++;
343     bd->fd = open(devbpf, O_RDONLY);
344     //cd[n].fd = open(devbpf, O_RDONLY);
345     } while(bd->fd < 0 && errno == EBUSY);
346       //while(cd[n].fd < 0 && errno == EBUSY);
347
348 //if (cd[n].fd < 0)
349 if (bd->fd < 0)
350     {
351     errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
352     printfd(__FILE__, "Cannot open BPF device\n");
353     return -1;
354     }
355
356 //strncpy(ifr.ifr_name, settings->iface[n], sizeof(ifr.ifr_name));
357 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
358
359 //if (ioctl(cd[n].fd, BIOCSBLEN, (caddr_t)&l) < 0)
360 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
361     {
362     errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
363     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
364     return -1;
365     }
366
367 //if (ioctl(cd[n].fd, BIOCSETIF, (caddr_t)&ifr) < 0 )
368 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
369     {
370     errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
371     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
372     return -1;
373     }
374
375 //if (ioctl(cd[n].fd, BIOCIMMEDIATE, &im) < 0 )
376 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
377     {
378     errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
379     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
380     return -1;
381     }
382
383 return bd->fd;
384 //return 0;
385 }
386 //-----------------------------------------------------------------------------
387 int BPF_CAP::BPFCapClose()
388 {
389 for (unsigned int i = 0; i < bpfData.size(); i++)
390   close(bpfData[i].fd);
391 return 0;
392 }
393 //-----------------------------------------------------------------------------
394 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
395 {
396 poll(&polld[0], polld.size(), -1);
397
398 for (unsigned int i = 0; i < polld.size(); i++)
399     {
400     if (polld[i].revents & POLLIN)
401         {
402         BPFCapRead(buffer, blen, capIface, &bpfData[i]);
403         polld[i].revents = 0;
404         return 0;
405         }
406     }
407 return 0;
408 }
409 //-----------------------------------------------------------------------------
410 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
411 {
412 if (bd->canRead)
413     {
414     bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
415     if (bd->r < 0)
416         {
417         //printfd(__FILE__, " error read\n");
418         usleep(20000);
419         }
420
421     bd->p = bd->buffer;
422     bd->bh = (struct bpf_hdr*)bd->p;
423     bd->canRead = 0;
424     }
425
426 if(bd->r > bd->sum)
427     {
428     memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
429     //strncpy(iface, settings->iface[n], 9);
430     //*iface = settings->iface[n];
431
432     bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
433     bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
434     bd->bh = (struct bpf_hdr*)bd->p;
435     }
436
437 if(bd->r <= bd->sum)
438     {
439     bd->canRead = 1;
440     bd->sum = 0;
441     }
442
443 return 0;
444 }
445 //-----------------------------------------------------------------------------
446