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 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 void BPF_CAP::SetSettings(const MODULE_SETTINGS & s)
188 //-----------------------------------------------------------------------------
189 int BPF_CAP::ParseSettings()
191 int ret = capSettings.ParseSettings(settings);
194 errorStr = capSettings.GetStrError();
199 //-----------------------------------------------------------------------------
200 void BPF_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
204 //-----------------------------------------------------------------------------
205 const string & BPF_CAP::GetStrError() const
209 //-----------------------------------------------------------------------------
215 if (BPFCapOpen() < 0)
217 //errorStr = "Cannot open bpf device!";
223 if (pthread_create(&thread, NULL, Run, this) == 0)
228 errorStr = "Cannot create thread.";
229 printfd(__FILE__, "Cannot create thread\n");
232 //-----------------------------------------------------------------------------
242 //5 seconds to thread stops itself
244 for (i = 0; i < 25; i++)
252 //after 5 seconds waiting thread still running. now killing it
255 //TODO pthread_cancel()
256 if (pthread_kill(thread, SIGINT))
258 errorStr = "Cannot kill thread.";
259 printfd(__FILE__, "Cannot kill thread\n");
266 //-----------------------------------------------------------------------------
267 bool BPF_CAP::IsRunning()
271 //-----------------------------------------------------------------------------
272 void * BPF_CAP::Run(void * d)
274 BPF_CAP * dc = (BPF_CAP *)d;
275 dc->isRunning = true;
277 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
279 RAW_PACKET * rpp = (RAW_PACKET *)&hdr[14];
280 memset(hdr, 0, sizeof(hdr));
287 dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
289 if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
294 dc->traffCnt->Process(*rpp);
297 dc->isRunning = false;
300 //-----------------------------------------------------------------------------
301 uint16_t BPF_CAP::GetStartPosition() const
305 //-----------------------------------------------------------------------------
306 uint16_t BPF_CAP::GetStopPosition() const
310 //-----------------------------------------------------------------------------
311 int BPF_CAP::BPFCapOpen()
313 //for (int i = 0; i < settings->devNum; i++)
318 while ((bd.iface = capSettings.GetIface(i)) != "")
320 bpfData.push_back(bd);
321 if (BPFCapOpen(&bpfData[i]) < 0)
327 pd.fd = bpfData[i].fd;
334 //-----------------------------------------------------------------------------
335 //int BPF_CAP::BPFCapOpen(string ifaceToOpen)
336 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
345 sprintf(devbpf, "/dev/bpf%d", i);
347 bd->fd = open(devbpf, O_RDONLY);
348 //cd[n].fd = open(devbpf, O_RDONLY);
349 } while(bd->fd < 0 && errno == EBUSY);
350 //while(cd[n].fd < 0 && errno == EBUSY);
355 errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
356 printfd(__FILE__, "Cannot open BPF device\n");
360 //strncpy(ifr.ifr_name, settings->iface[n], sizeof(ifr.ifr_name));
361 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
363 //if (ioctl(cd[n].fd, BIOCSBLEN, (caddr_t)&l) < 0)
364 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
366 errorStr = bd->iface + " BIOCSBLEN " + string(strerror(errno));
367 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
371 //if (ioctl(cd[n].fd, BIOCSETIF, (caddr_t)&ifr) < 0 )
372 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
374 errorStr = bd->iface + " BIOCSETIF " + string(strerror(errno));
375 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
379 //if (ioctl(cd[n].fd, BIOCIMMEDIATE, &im) < 0 )
380 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
382 errorStr = bd->iface + " BIOCIMMEDIATE " + string(strerror(errno));
383 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
390 //-----------------------------------------------------------------------------
391 int BPF_CAP::BPFCapClose()
393 for (unsigned int i = 0; i < bpfData.size(); i++)
394 close(bpfData[i].fd);
397 //-----------------------------------------------------------------------------
398 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
400 poll(&polld[0], polld.size(), -1);
402 for (unsigned int i = 0; i < polld.size(); i++)
404 if (polld[i].revents & POLLIN)
406 BPFCapRead(buffer, blen, capIface, &bpfData[i]);
407 polld[i].revents = 0;
413 //-----------------------------------------------------------------------------
414 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
418 bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
421 //printfd(__FILE__, " error read\n");
426 bd->bh = (struct bpf_hdr*)bd->p;
432 memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
433 //strncpy(iface, settings->iface[n], 9);
434 //*iface = settings->iface[n];
436 bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
437 bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
438 bd->bh = (struct bpf_hdr*)bd->p;
449 //-----------------------------------------------------------------------------