-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpcap_filter.c
51 lines (44 loc) · 1.43 KB
/
pcap_filter.c
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
#include <stdio.h>
#include <pcap.h>
int main(int argc, char *argv[])
{
pcap_t *handle; /* Session handle */
char dev[] = "lo"; /* Device to sniffer on */
char errbuf[PCAP_ERRBUF_SIZE]; /* Error String */
struct bpf_program fp; /* The compliled filter expression */
char filter_exp[] = "port 80"; /* The filter expression */
bpf_u_int32 mask; /* The netmask of our sniffing device */
bpf_u_int32 net; /* The IP of our sniffering device */
/*
dev = pcap_lookupdev(errbuf);
if(dev == NULL)
{
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
printf("Device: %s\n", dev);
*/
if(pcap_lookupnet(dev, &net, &mask, errbuf) == -1)
{
fprintf(stderr, "Can't get netmask for device %s\n", dev);
net = 0;
mask = 0;
}
handle = pcap_open_live(dev, BUFSIZ, 1, 100, errbuf);
if(handle == NULL)
{
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1)
{
fprintf(stderr, "Counldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
if(pcap_setfilter(handle, &fp) == -1)
{
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
return(0);
}