Hey, I am trying to do some programming using the pcap library on my FreeBSD box. I am using the tutorial here ( Programming with pcap ) as a guiideline. I have hit a few snags along the way but was able to correct them. However there is one error that I seem unable to work out. I can compile the following code fine, but when I fun it I keep getting the error
Code:
Couldn't install filter host any:BIOCSETF: Bad file descriptor
pcap_setfilter: BIOCSETF: Bad file descriptor
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <pcap/pcap.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/bpf.h>
int main(int argc, char *argv[])
{
char errbuf[PCAP_ERRBUF_SIZE], *dev;
char filter_exp[] = "host any";
pcap_t *handle;
int ret = 0;
struct bpf_program *bpf_ptr;
bpf_u_int32 mask; /* The netmask of our sniffing device */
bpf_u_int32 net; /* The IP of our sniffing device */
struct pcap_pkthdr header; /* The header that pcap gives us */
const u_char *packet; /* The actual packet */
dev = argv[1];
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, 0, errbuf);
if(handle == NULL){
fprintf(stderr, "pcap error %s", errbuf);
exit(1);
}
ret = pcap_compile(handle, bpf_ptr, filter_exp, 1, (bpf_u_int32) 0);
if(ret == -1){
pcap_perror(handle, "pcap_compile");
exit(1);
}
ret = pcap_setfilter(handle, bpf_ptr);
if(ret == -1){
fprintf(stderr, "Couldn't install filter %s:\n%s\n", filter_exp, pcap_geterr(handle));
pcap_perror(handle, "pcap_setfilter");
return(2);
}
/* Grab a packet */
packet = pcap_next(handle, &header);
if(packet == NULL){
pcap_perror(handle, "pcap_next");
}
/* Print its length */
printf("Jacked a packet with length of [%d]\n", header.len);
/* And close the session */
pcap_close(handle);
return(0);
}
Obviously it is an error in which I have a bad file descriptor, but I don't know where to go from there. Thank you in advance.