190
190
char ebuf[PCAP_ERRBUF_SIZE];
191
191
memset(ebuf, 0, PCAP_ERRBUF_SIZE);
193
if ((d_pd = pcap_open_live(dev, d_snaplen, promisc, 500, ebuf)) == NULL) {
194
die(ebuf, STDERR, 1);
196
#ifdef HAVE_PCAP_SETNONBLOCK
197
// make sure pcap_next() does not block
198
if (pcap_setnonblock(d_pd, 1, ebuf) < 0) {
199
die(ebuf, STDERR, 1);
193
if ((d_pd = pcap_open_live(dev, d_snaplen, promisc, 500, ebuf)) == NULL) {
194
die(ebuf, STDERR, 1);
203
197
// Ehem, BSD workarounnd. BSD won't timeout on select()
204
198
// unless we force immediate return for read() (in pcap)
212
206
die(ebuf, STDERR, 1);
215
if (pcap_lookupnet(dev, &d_localnet, &d_netmask, ebuf) < 0) {
216
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_lookupnet: %s\n",
209
if (pcap_lookupnet(dev, &d_localnet, &d_netmask, ebuf) < 0) {
210
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_lookupnet: %s\n",
217
211
pcap_geterr(d_pd));
218
212
die(ebuf, STDERR, 1);
222
216
/* The d_filter_string must be filled by derived classes, such
223
217
* as IP, where the virtual init_device() simply sets d_filter_string
224
218
* to "ip" and then calls Pcap::init_device().
226
if (pcap_compile(d_pd, &d_filter, d_filter_string, 1, d_netmask) < 0) {
227
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_compile: %s\n",
229
die(ebuf, STDERR, 1);
231
if (pcap_setfilter(d_pd, &d_filter) < 0) {
232
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_setfilter: %s\n",
234
die(ebuf, STDERR, 1);
236
if ((d_datalink = pcap_datalink(d_pd)) < 0) {
237
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_d_datalink: %s\n",
239
die(ebuf, STDERR, 1);
220
if (pcap_compile(d_pd, &d_filter, d_filter_string, 1, d_netmask) < 0) {
221
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_compile: %s\n",
223
die(ebuf, STDERR, 1);
225
if (pcap_setfilter(d_pd, &d_filter) < 0) {
226
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_setfilter: %s\n",
228
die(ebuf, STDERR, 1);
230
if ((d_datalink = pcap_datalink(d_pd)) < 0) {
231
snprintf(ebuf, sizeof(ebuf), "Pcap::init_device::pcap_d_datalink: %s\n",
233
die(ebuf, STDERR, 1);
242
// turn d_datalink into d_framelen
243
switch (d_datalink) {
245
d_framelen = sizeof(d_ether);
236
// turn d_datalink into d_framelen
237
switch (d_datalink) {
239
d_framelen = sizeof(d_ether);
248
d_framelen = 4; /* shouldn't be 4 */
243
d_framelen = 4; /* shouldn't be 4 */
267
261
#ifdef DLT_LINUX_SLL
274
printf("%d %d\n", d_datalink, DLT_RAW);
275
fprintf(stderr, "Datalink type: %i not supported. Report!\n", d_datalink);
276
die("Pcap::init_device: Unknown d_datalink.\n", STDERR, 1);
267
printf("%d %d\n", d_datalink, DLT_RAW);
268
fprintf(stderr, "Datalink type: %i not supported. Report!\n", d_datalink);
269
die("Pcap::init_device: Unknown d_datalink.\n", STDERR, 1);
279
272
strncpy(d_dev, dev, sizeof(d_dev));
280
273
d_has_promisc = promisc;
281
274
d_snaplen = d_snaplen;
296
290
memset(d_filter_string, 0, sizeof(d_filter_string));
297
291
snprintf(d_filter_string, sizeof(d_filter_string), "%s", s);
299
if (pcap_compile(d_pd, &d_filter, d_filter_string, 1, d_netmask) < 0) {
300
snprintf(ebuf, sizeof(ebuf), "Pcap::setfilter::pcap_compile: %s\n", pcap_geterr(d_pd));
293
if (pcap_compile(d_pd, &d_filter, d_filter_string, 1, d_netmask) < 0) {
294
snprintf(ebuf, sizeof(ebuf), "Pcap::setfilter::pcap_compile: %s\n", pcap_geterr(d_pd));
301
295
die(ebuf, STDERR, 1);
304
if (pcap_setfilter(d_pd, &d_filter) < 0) {
305
snprintf(ebuf, sizeof(ebuf), "Pcap::setfilter::pcap_setfilter: %s\n", pcap_geterr(d_pd));
298
if (pcap_setfilter(d_pd, &d_filter) < 0) {
299
snprintf(ebuf, sizeof(ebuf), "Pcap::setfilter::pcap_setfilter: %s\n", pcap_geterr(d_pd));
306
300
die(ebuf, STDERR, 1);
311
306
int Pcap::sniffpack(void *s, size_t len)
349
344
start = time(NULL);
351
346
if (d_tv.tv_sec != 0 || d_tv.tv_usec != 0) { // timeout set
352
348
while ((tmp = (char*)pcap_next(d_pd, &d_phdr)) == NULL)
353
349
if ((time(NULL) - start) > d_tv.tv_sec) {
354
350
d_timeout = true;
356
if ((time(NULL) - start) > d_tv.tv_sec) {
361
tmp = (char*)pcap_next(d_pd, &d_phdr);
358
366
} else { // no timeout set, loop until we get some kind of packet
359
367
while ((tmp = (char*)pcap_next(d_pd, &d_phdr)) == NULL)
398
406
// give back layer2 frame
399
407
void *Pcap::get_frame(void *hwframe, size_t len)
402
// switch over the hardware-layer of the packet
409
// switch over the hardware-layer of the packet
403
410
switch (d_datalink) {
405
memcpy(hwframe, &d_ether, (len<sizeof(d_ether)?len:sizeof(d_ether)));
412
memcpy(hwframe, &d_ether, (len<sizeof(d_ether)?len:sizeof(d_ether)));
413
420
int Pcap::timeout(struct timeval tv)
422
429
return d_timeout;
432
bool Pcap::set_nonblock()
434
#ifdef HAVE_PCAP_SETNONBLOCK
435
char ebuf[PCAP_ERRBUF_SIZE];
436
memset(ebuf, 0, PCAP_ERRBUF_SIZE);
437
// make sure pcap_next() does not block
438
if (pcap_setnonblock(d_pd, 1, ebuf) < 0) {
439
die(ebuf, STDERR, 1);
445
int Pcap::get_nonblock()
447
#ifdef HAVE_PCAP_SETNONBLOCK
449
char ebuf[PCAP_ERRBUF_SIZE];
450
memset(ebuf, 0, PCAP_ERRBUF_SIZE);
451
// make sure pcap_next() does not block
452
if ((retval=pcap_getnonblock(d_pd, ebuf)) < 0) {
453
die(ebuf, STDERR, 1);
425
458
} // namespace usipp