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