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.
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.
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
22 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
27 $Date: 2009/03/24 11:20:15 $
31 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
54 #include "ether_cap.h"
56 #include "raw_ip_packet.h"
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
79 BPF_CAP * GetCapturer()
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 BASE_PLUGIN * GetPlugin()
93 return bcc.GetCapturer();
95 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
98 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
100 //char sep[]= ", \t\n\r";
106 iface.erase(iface.begin(), iface.end());
109 //pv.param = "WorkDir";
110 //vector<PARAM_VALUE>::const_iterator pvi;
112 if (s.moduleParams.empty())
114 errorStr = "Parameter \'iface\' not found.";
115 printfd(__FILE__, "Parameter 'iface' not found\n");
119 for (unsigned i = 0; i < s.moduleParams.size(); i++)
121 if (s.moduleParams[i].param != "iface")
123 errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
124 printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
127 for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
129 iface.push_back(s.moduleParams[i].value[j]);
133 /*if (cf.ReadString("Iface", &ifaces, "NoIface") < 0)
135 errorStr = "Cannot read parameter \'Iface\' from " + cf.GetFileName();
139 str = new char[ifaces.size() + 1];
140 strcpy(str, ifaces.c_str());
143 while((s = strtok(p, sep)))
145 printfd(__FILE__, "iface[] = %s\n", s);
148 //strncpy(iface[i++], s, DEV_NAME_LEN);
155 errorStr = "Error read parameter \'Iface\' from " + cf.GetFileName();
161 //-----------------------------------------------------------------------------
162 string BPF_CAP_SETTINGS::GetIface(unsigned int num)
164 if (num >= iface.size())
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 const string BPF_CAP::GetVersion() const
175 return "bpf_cap v.1.0";
177 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
186 void BPF_CAP::SetSettings(const MODULE_SETTINGS & s)
190 //-----------------------------------------------------------------------------
191 int BPF_CAP::ParseSettings()
193 int ret = capSettings.ParseSettings(settings);
196 errorStr = capSettings.GetStrError();
201 //-----------------------------------------------------------------------------
202 void BPF_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
206 //-----------------------------------------------------------------------------
207 const string & BPF_CAP::GetStrError() const
211 //-----------------------------------------------------------------------------
217 if (BPFCapOpen() < 0)
219 //errorStr = "Cannot open bpf device!";
225 if (pthread_create(&thread, NULL, Run, this) == 0)
230 errorStr = "Cannot create thread.";
231 printfd(__FILE__, "Cannot create thread\n");
234 //-----------------------------------------------------------------------------
244 //5 seconds to thread stops itself
246 for (i = 0; i < 25; i++)
254 //after 5 seconds waiting thread still running. now killing it
257 //TODO pthread_cancel()
258 if (pthread_kill(thread, SIGINT))
260 errorStr = "Cannot kill thread.";
261 printfd(__FILE__, "Cannot kill thread\n");
268 //-----------------------------------------------------------------------------
269 bool BPF_CAP::IsRunning()
273 //-----------------------------------------------------------------------------
274 void * BPF_CAP::Run(void * d)
276 BPF_CAP * dc = (BPF_CAP *)d;
277 dc->isRunning = true;
279 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
281 RAW_PACKET * rpp = (RAW_PACKET *)&hdr[14];
282 memset(hdr, 0, sizeof(hdr));
289 dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
291 if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
296 dc->traffCnt->Process(*rpp);
299 dc->isRunning = false;
302 //-----------------------------------------------------------------------------
303 uint16_t BPF_CAP::GetStartPosition() const
307 //-----------------------------------------------------------------------------
308 uint16_t BPF_CAP::GetStopPosition() const
312 //-----------------------------------------------------------------------------
313 int BPF_CAP::BPFCapOpen()
315 //for (int i = 0; i < settings->devNum; i++)
320 while ((bd.iface = capSettings.GetIface(i)) != "")
322 bpfData.push_back(bd);
323 if (BPFCapOpen(&bpfData[i]) < 0)
329 pd.fd = bpfData[i].fd;
336 //-----------------------------------------------------------------------------
337 //int BPF_CAP::BPFCapOpen(string ifaceToOpen)
338 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
347 sprintf(devbpf, "/dev/bpf%d", i);
349 bd->fd = open(devbpf, O_RDONLY);
350 //cd[n].fd = open(devbpf, O_RDONLY);
351 } while(bd->fd < 0 && errno == EBUSY);
352 //while(cd[n].fd < 0 && errno == EBUSY);
357 errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
358 printfd(__FILE__, "Cannot open BPF device\n");
362 //strncpy(ifr.ifr_name, settings->iface[n], sizeof(ifr.ifr_name));
363 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
365 //if (ioctl(cd[n].fd, BIOCSBLEN, (caddr_t)&l) < 0)
366 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
368 errorStr = bd->iface + " BIOCSBLEN " + string(strerror(errno));
369 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
373 //if (ioctl(cd[n].fd, BIOCSETIF, (caddr_t)&ifr) < 0 )
374 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
376 errorStr = bd->iface + " BIOCSETIF " + string(strerror(errno));
377 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
381 //if (ioctl(cd[n].fd, BIOCIMMEDIATE, &im) < 0 )
382 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
384 errorStr = bd->iface + " BIOCIMMEDIATE " + string(strerror(errno));
385 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
392 //-----------------------------------------------------------------------------
393 int BPF_CAP::BPFCapClose()
395 for (unsigned int i = 0; i < bpfData.size(); i++)
396 close(bpfData[i].fd);
399 //-----------------------------------------------------------------------------
400 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
402 poll(&polld[0], polld.size(), -1);
404 for (unsigned int i = 0; i < polld.size(); i++)
406 if (polld[i].revents & POLLIN)
408 BPFCapRead(buffer, blen, capIface, &bpfData[i]);
409 polld[i].revents = 0;
415 //-----------------------------------------------------------------------------
416 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
420 bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
423 //printfd(__FILE__, " error read\n");
428 bd->bh = (struct bpf_hdr*)bd->p;
434 memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
435 //strncpy(iface, settings->iface[n], 9);
436 //*iface = settings->iface[n];
438 bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
439 bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
440 bd->bh = (struct bpf_hdr*)bd->p;
451 //-----------------------------------------------------------------------------