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>
51 #include "ether_cap.h"
53 #include "raw_ip_packet.h"
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 class BPF_CAP_CREATOR {
73 BPF_CAP * GetCapturer()
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
87 return bcc.GetCapturer();
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
94 //char sep[]= ", \t\n\r";
100 iface.erase(iface.begin(), iface.end());
103 //pv.param = "WorkDir";
104 //vector<PARAM_VALUE>::const_iterator pvi;
106 if (s.moduleParams.empty())
108 errorStr = "Parameter \'iface\' not found.";
109 printfd(__FILE__, "Parameter 'iface' not found\n");
113 for (unsigned i = 0; i < s.moduleParams.size(); i++)
115 if (s.moduleParams[i].param != "iface")
117 errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
118 printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
121 for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
123 iface.push_back(s.moduleParams[i].value[j]);
127 /*if (cf.ReadString("Iface", &ifaces, "NoIface") < 0)
129 errorStr = "Cannot read parameter \'Iface\' from " + cf.GetFileName();
133 str = new char[ifaces.size() + 1];
134 strcpy(str, ifaces.c_str());
137 while((s = strtok(p, sep)))
139 printfd(__FILE__, "iface[] = %s\n", s);
142 //strncpy(iface[i++], s, DEV_NAME_LEN);
149 errorStr = "Error read parameter \'Iface\' from " + cf.GetFileName();
155 //-----------------------------------------------------------------------------
156 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
158 if (num >= iface.size())
164 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
167 const std::string BPF_CAP::GetVersion() const
169 return "bpf_cap v.1.0";
171 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
180 void BPF_CAP::SetSettings(const MODULE_SETTINGS & s)
184 //-----------------------------------------------------------------------------
185 int BPF_CAP::ParseSettings()
187 int ret = capSettings.ParseSettings(settings);
190 errorStr = capSettings.GetStrError();
195 //-----------------------------------------------------------------------------
196 void BPF_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
200 //-----------------------------------------------------------------------------
201 const std::string & BPF_CAP::GetStrError() const
205 //-----------------------------------------------------------------------------
211 if (BPFCapOpen() < 0)
213 //errorStr = "Cannot open bpf device!";
219 if (pthread_create(&thread, NULL, Run, this) == 0)
224 errorStr = "Cannot create thread.";
225 printfd(__FILE__, "Cannot create thread\n");
228 //-----------------------------------------------------------------------------
238 //5 seconds to thread stops itself
240 for (i = 0; i < 25; i++)
248 //after 5 seconds waiting thread still running. now killing it
251 //TODO pthread_cancel()
252 if (pthread_kill(thread, SIGINT))
254 errorStr = "Cannot kill thread.";
255 printfd(__FILE__, "Cannot kill thread\n");
262 //-----------------------------------------------------------------------------
263 bool BPF_CAP::IsRunning()
267 //-----------------------------------------------------------------------------
268 void * BPF_CAP::Run(void * d)
270 BPF_CAP * dc = (BPF_CAP *)d;
271 dc->isRunning = true;
273 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
275 RAW_PACKET * rpp = (RAW_PACKET *)&hdr[14];
276 memset(hdr, 0, sizeof(hdr));
283 dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
285 if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
290 dc->traffCnt->Process(*rpp);
293 dc->isRunning = false;
296 //-----------------------------------------------------------------------------
297 uint16_t BPF_CAP::GetStartPosition() const
301 //-----------------------------------------------------------------------------
302 uint16_t BPF_CAP::GetStopPosition() const
306 //-----------------------------------------------------------------------------
307 int BPF_CAP::BPFCapOpen()
309 //for (int i = 0; i < settings->devNum; i++)
314 while ((bd.iface = capSettings.GetIface(i)) != "")
316 bpfData.push_back(bd);
317 if (BPFCapOpen(&bpfData[i]) < 0)
323 pd.fd = bpfData[i].fd;
330 //-----------------------------------------------------------------------------
331 //int BPF_CAP::BPFCapOpen(string ifaceToOpen)
332 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
341 sprintf(devbpf, "/dev/bpf%d", 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);
351 errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
352 printfd(__FILE__, "Cannot open BPF device\n");
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));
359 //if (ioctl(cd[n].fd, BIOCSBLEN, (caddr_t)&l) < 0)
360 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
362 errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
363 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
367 //if (ioctl(cd[n].fd, BIOCSETIF, (caddr_t)&ifr) < 0 )
368 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
370 errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
371 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
375 //if (ioctl(cd[n].fd, BIOCIMMEDIATE, &im) < 0 )
376 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
378 errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
379 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
386 //-----------------------------------------------------------------------------
387 int BPF_CAP::BPFCapClose()
389 for (unsigned int i = 0; i < bpfData.size(); i++)
390 close(bpfData[i].fd);
393 //-----------------------------------------------------------------------------
394 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
396 poll(&polld[0], polld.size(), -1);
398 for (unsigned int i = 0; i < polld.size(); i++)
400 if (polld[i].revents & POLLIN)
402 BPFCapRead(buffer, blen, capIface, &bpfData[i]);
403 polld[i].revents = 0;
409 //-----------------------------------------------------------------------------
410 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
414 bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
417 //printfd(__FILE__, " error read\n");
422 bd->bh = (struct bpf_hdr*)bd->p;
428 memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
429 //strncpy(iface, settings->iface[n], 9);
430 //*iface = settings->iface[n];
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;
445 //-----------------------------------------------------------------------------