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"
52 #include "stg/common.h"
53 #include "stg/raw_ip_packet.h"
54 #include "stg/traffcounter.h"
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 class BPF_CAP_CREATOR {
74 BPF_CAP * GetCapturer()
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
88 return bcc.GetCapturer();
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
93 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
97 iface.erase(iface.begin(), iface.end());
99 if (s.moduleParams.empty())
101 errorStr = "Parameter \'iface\' not found.";
102 printfd(__FILE__, "Parameter 'iface' not found\n");
106 for (unsigned i = 0; i < s.moduleParams.size(); i++)
108 if (s.moduleParams[i].param != "iface")
110 errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
111 printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
114 for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
116 iface.push_back(s.moduleParams[i].value[j]);
122 //-----------------------------------------------------------------------------
123 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
125 if (num >= iface.size())
131 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
134 const std::string BPF_CAP::GetVersion() const
136 return "bpf_cap v.1.0";
138 //-----------------------------------------------------------------------------
146 //-----------------------------------------------------------------------------
147 int BPF_CAP::ParseSettings()
149 int ret = capSettings.ParseSettings(settings);
152 errorStr = capSettings.GetStrError();
157 //-----------------------------------------------------------------------------
163 if (BPFCapOpen() < 0)
165 //errorStr = "Cannot open bpf device!";
171 if (pthread_create(&thread, NULL, Run, this) == 0)
176 errorStr = "Cannot create thread.";
177 printfd(__FILE__, "Cannot create thread\n");
180 //-----------------------------------------------------------------------------
190 //5 seconds to thread stops itself
192 for (i = 0; i < 25; i++)
200 //after 5 seconds waiting thread still running. now killing it
203 //TODO pthread_cancel()
204 if (pthread_kill(thread, SIGINT))
206 errorStr = "Cannot kill thread.";
207 printfd(__FILE__, "Cannot kill thread\n");
214 //-----------------------------------------------------------------------------
215 void * BPF_CAP::Run(void * d)
217 BPF_CAP * dc = (BPF_CAP *)d;
218 dc->isRunning = true;
220 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
222 RAW_PACKET * rpp = (RAW_PACKET *)&hdr[14];
223 memset(hdr, 0, sizeof(hdr));
230 dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
232 if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
237 dc->traffCnt->Process(*rpp);
240 dc->isRunning = false;
243 //-----------------------------------------------------------------------------
244 int BPF_CAP::BPFCapOpen()
250 while ((bd.iface = capSettings.GetIface(i)) != "")
252 bpfData.push_back(bd);
253 if (BPFCapOpen(&bpfData[i]) < 0)
259 pd.fd = bpfData[i].fd;
266 //-----------------------------------------------------------------------------
267 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
277 sprintf(devbpf, "/dev/bpf%d", i);
279 bd->fd = open(devbpf, O_RDONLY);
280 } while(bd->fd < 0 && errno == EBUSY);
284 errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
285 printfd(__FILE__, "Cannot open BPF device\n");
289 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
291 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
293 errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
294 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
298 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
300 errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
301 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
305 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
307 errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
308 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
314 //-----------------------------------------------------------------------------
315 int BPF_CAP::BPFCapClose()
317 for (unsigned int i = 0; i < bpfData.size(); i++)
318 close(bpfData[i].fd);
321 //-----------------------------------------------------------------------------
322 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
324 poll(&polld[0], polld.size(), -1);
326 for (unsigned int i = 0; i < polld.size(); i++)
328 if (polld[i].revents & POLLIN)
330 BPFCapRead(buffer, blen, capIface, &bpfData[i]);
331 polld[i].revents = 0;
337 //-----------------------------------------------------------------------------
338 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
342 bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
345 //printfd(__FILE__, " error read\n");
350 bd->bh = (struct bpf_hdr*)bd->p;
356 memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
358 bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
359 bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
360 bd->bh = (struct bpf_hdr*)bd->p;
371 //-----------------------------------------------------------------------------