error compiling a simple libpcap example
I am trying to compile a simple libpcap example,
#include<stdio.h>
#include<pcap.h>
int main(int argc, char *argv[])
{
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp ;
char filter_exp[] = "port 23";
bpf_u_int32 mask;
bpf_u_int32 net;
dev = pcap_lookupdev(errbuf);
if (dev ==NULL) {
fprintf(stderr, "couldn't find default device: %s\n", errbuf);
return(2);
}
printf ("Device: %s\n", dev);
//LOOKUP NETMASK and IP
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1){
fprintf(stderr, "can't get netmask for device %s\n", dev);
net =0;
mask =0;
}
printf("lookup\n");
pcap_t *handle;
printf("handle defined\n");
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
printf("opened\n");
if (handle = NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
printf("pcap_open\n");
if ((pcap_compile(handle, &fp, filter_exp, 1, net)) == -1) {
printf("compile error block entered\n");
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp,
pcap_geterr(handle));
return(2);
}
printf("compiled\n");
if(pcap_setfilter(handle, &fp) == -1){
fprintf(stderr, "couldn't install filter %s: %s\n", filter_exp,
pcap_geterr(handle));
return(2);
}
printf("after filter\n");
return(0);
}
It compiles without error, but when I try to run it, I'm getting
segmentation fault message or if I try to run it with root permission, I
don't get the message but the program stops after printing
Device: eth0
lookup
handle defined
opened
pcap_open
Can you please help me with this, I'm confused as to why this error
happens. Thanks in advance.
No comments:
Post a Comment