]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp
Unused variable removed
[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 #include <fcntl.h>
43 #include <unistd.h>
44
45 #include <cerrno>
46 #include <cstdio>
47 #include <cstring>
48 #include <cstdlib>
49 #include <csignal>
50
51 #include "stg/common.h"
52 #include "stg/raw_ip_packet.h"
53 #include "stg/traffcounter.h"
54 #include "stg/plugin_creator.h"
55
56 #include "ether_cap.h"
57
58 //#define CAP_DEBUG 1
59
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 PLUGIN_CREATOR<BPF_CAP> bcc;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 PLUGIN * GetPlugin()
68 {
69 return bcc.GetPlugin();
70 }
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
75 {
76 iface.erase(iface.begin(), iface.end());
77
78 if (s.moduleParams.empty())
79     {
80     errorStr = "Parameter \'iface\' not found.";
81     printfd(__FILE__, "Parameter 'iface' not found\n");
82     return -1;
83     }
84
85 for (unsigned i = 0; i < s.moduleParams.size(); i++)
86     {
87     if (s.moduleParams[i].param != "iface")
88         {
89         errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
90         printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
91         return -1;
92         }
93     for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
94         {
95         iface.push_back(s.moduleParams[i].value[j]);
96         }
97     }
98
99 return 0;
100 }
101 //-----------------------------------------------------------------------------
102 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
103 {
104 if (num >= iface.size())
105     {
106     return "";
107     }
108 return iface[num];
109 }
110 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
113 const std::string BPF_CAP::GetVersion() const
114 {
115 return "bpf_cap v.1.0";
116 }
117 //-----------------------------------------------------------------------------
118 BPF_CAP::BPF_CAP()
119     : capSettings(),
120       errorStr(),
121       bpfData(),
122       polld(),
123       thread(),
124       nonstop(false),
125       isRunning(false),
126       capSock(-1),
127       settings(),
128       traffCnt(NULL)
129 {
130 }
131 //-----------------------------------------------------------------------------
132 int BPF_CAP::ParseSettings()
133 {
134 int ret = capSettings.ParseSettings(settings);
135 if (ret)
136     {
137     errorStr = capSettings.GetStrError();
138     return ret;
139     }
140 return 0;
141 }
142 //-----------------------------------------------------------------------------
143 int BPF_CAP::Start()
144 {
145 if (isRunning)
146     return 0;
147
148 if (BPFCapOpen() < 0)
149     {
150     //errorStr = "Cannot open bpf device!";
151     return -1;
152     }
153
154 nonstop = true;
155
156 if (pthread_create(&thread, NULL, Run, this) == 0)
157     {
158     return 0;
159     }
160
161 errorStr = "Cannot create thread.";
162 printfd(__FILE__, "Cannot create thread\n");
163 return -1;
164 }
165 //-----------------------------------------------------------------------------
166 int BPF_CAP::Stop()
167 {
168 if (!isRunning)
169     return 0;
170
171 BPFCapClose();
172
173 nonstop = false;
174
175 //5 seconds to thread stops itself
176 int i;
177 for (i = 0; i < 25; i++)
178     {
179     if (!isRunning)
180         break;
181
182     struct timespec ts = {0, 200000000};
183     nanosleep(&ts, NULL);
184     }
185
186 //after 5 seconds waiting thread still running. now killing it
187 if (isRunning)
188     {
189     //TODO pthread_cancel()
190     if (pthread_kill(thread, SIGINT))
191         {
192         errorStr = "Cannot kill thread.";
193         printfd(__FILE__, "Cannot kill thread\n");
194         return -1;
195         }
196     }
197
198 return 0;
199 }
200 //-----------------------------------------------------------------------------
201 void * BPF_CAP::Run(void * d)
202 {
203 sigset_t signalSet;
204 sigfillset(&signalSet);
205 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
206
207 BPF_CAP * dc = static_cast<BPF_CAP *>(d);
208 dc->isRunning = true;
209
210 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
211
212 RAW_PACKET *  rpp = (RAW_PACKET *)&hdr[14];
213 memset(hdr, 0, sizeof(hdr));
214
215 rpp->dataLen = -1;
216 char * iface;
217
218 while (dc->nonstop)
219     {
220     if (dc->BPFCapRead((char*)&hdr, 68 + 14, &iface))
221         continue;
222
223     if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
224         continue;
225
226     dc->traffCnt->Process(*rpp);
227     }
228
229 dc->isRunning = false;
230 return NULL;
231 }
232 //-----------------------------------------------------------------------------
233 int BPF_CAP::BPFCapOpen()
234 {
235 int i = 0;
236 BPF_DATA bd;
237 pollfd pd;
238
239 while ((bd.iface = capSettings.GetIface(i)) != "")
240     {
241     bpfData.push_back(bd);
242     if (BPFCapOpen(&bpfData[i]) < 0)
243         {
244         return -1;
245         }
246
247     pd.events = POLLIN;
248     pd.fd = bpfData[i].fd;
249     polld.push_back(pd);
250     i++;
251     }
252
253 return 0;
254 }
255 //-----------------------------------------------------------------------------
256 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
257 {
258 char devbpf[20];
259 int i = 0;
260 int l = BUFF_LEN;
261 int im = 1;
262 struct ifreq ifr;
263
264 do
265     {
266     sprintf(devbpf, "/dev/bpf%d", i);
267     i++;
268     bd->fd = open(devbpf, O_RDONLY);
269     } while(bd->fd < 0 && errno == EBUSY);
270
271 if (bd->fd < 0)
272     {
273     errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
274     printfd(__FILE__, "Cannot open BPF device\n");
275     return -1;
276     }
277
278 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
279
280 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
281     {
282     errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
283     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
284     return -1;
285     }
286
287 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
288     {
289     errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
290     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
291     return -1;
292     }
293
294 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
295     {
296     errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
297     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
298     return -1;
299     }
300
301 return bd->fd;
302 }
303 //-----------------------------------------------------------------------------
304 int BPF_CAP::BPFCapClose()
305 {
306 for (unsigned int i = 0; i < bpfData.size(); i++)
307     close(bpfData[i].fd);
308 return 0;
309 }
310 //-----------------------------------------------------------------------------
311 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
312 {
313 poll(&polld[0], polld.size(), -1);
314
315 for (unsigned int i = 0; i < polld.size(); i++)
316     {
317     if (polld[i].revents & POLLIN)
318         {
319         if (BPFCapRead(buffer, blen, capIface, &bpfData[i]))
320             {
321             polld[i].revents = 0;
322             continue;
323             }
324         polld[i].revents = 0;
325         return 0;
326         }
327     }
328 return -1;
329 }
330 //-----------------------------------------------------------------------------
331 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
332 {
333 if (bd->canRead)
334     {
335     bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
336     if (bd->r < 0)
337         {
338         struct timespec ts = {0, 20000000};
339         nanosleep(&ts, NULL);
340         return -1;
341         }
342
343     bd->p = bd->buffer;
344     bd->bh = (struct bpf_hdr*)bd->p;
345     bd->canRead = 0;
346     }
347
348 if(bd->r > bd->sum)
349     {
350     memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
351
352     bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
353     bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
354     bd->bh = (struct bpf_hdr*)bd->p;
355     }
356
357 if(bd->r <= bd->sum)
358     {
359     bd->canRead = 1;
360     bd->sum = 0;
361     }
362
363 return 0;
364 }
365 //-----------------------------------------------------------------------------