File: pcaputil.c

package info (click to toggle)
fragroute 1.2-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 692 kB
  • ctags: 520
  • sloc: ansic: 4,331; sh: 2,842; makefile: 74
file content (105 lines) | stat: -rw-r--r-- 1,515 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * pcaputil.c
 *
 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
 *
 * $Id: pcaputil.c,v 1.8 2002/02/25 06:21:59 dugsong Exp $
 */

#include "config.h"

#ifndef WIN32
#include <sys/param.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#endif

#include <pcap.h>
#ifdef notyet
# include <pcap-int.h>
#endif

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pcaputil.h"

pcap_t *
pcap_open(char *device)
{
	char ebuf[PCAP_ERRBUF_SIZE];
	pcap_t *pcap;
	
	if (device == NULL) {
		if ((device = pcap_lookupdev(ebuf)) == NULL)
			return (NULL);
	}
	if ((pcap = pcap_open_live(device, 31337, 0, 10, ebuf)) == NULL)
		return (NULL);
	
#ifdef BSD
	{
		int n = 1;

		if (ioctl(pcap_fileno(pcap), BIOCIMMEDIATE, &n) < 0) {
			pcap_close(pcap);
			return (NULL);
		}
	}
#endif
	return (pcap);
}

int
pcap_dloff(pcap_t *pd)
{
	int i;

	i = pcap_datalink(pd);
	
	switch (i) {
	case DLT_EN10MB:
		i = 14;
		break;
	case DLT_IEEE802:
		i = 22;
		break;
	case DLT_FDDI:
		i = 21;
		break;
#ifdef DLT_LOOP
	case DLT_LOOP:
#endif
	case DLT_NULL:
		i = 4;
		break;
	default:
		i = -1;
		break;
	}
	return (i);
}

int
pcap_filter(pcap_t *pcap, const char *fmt, ...)
{
	struct bpf_program fcode;
	char buf[BUFSIZ];
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);
#ifdef notyet
	pcap_freecode(&pcap->fcode);
#endif
	if (pcap_compile(pcap, &fcode, buf, 1, 0) < 0)
		return (-1);
	
	if (pcap_setfilter(pcap, &fcode) == -1)
		return (-1);

	return (0);
}