同一节目能成功在openSUSE 12.1(64) 虽然不能在Fedora上运行16(64) 在Fedora 16上运行,它显示“错误调用使用pcap_compile” 我不知道这些操作系统有什么区别,我认为它们是完全一样的,但我确定Opensuse 12.1可以成功过滤并捕获数据包。linux下的libpcap编程
int init_capture() {
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr; /* pcap.h */
struct ether_header *eptr; /* net/ethernet.h */
struct bpf_program fp;
char portfilter[20]= "dst port 1521";
bpf_u_int32 maskp;
bpf_u_int32 netp;
/* grab a device to peak into... */
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
printf("%s\n", errbuf);
exit(1);
}
pcap_lookupnet(dev,&netp,&maskp,errbuf);
/* open device for reading */
descr = pcap_open_live(dev, BUFSIZ, 0, -1, errbuf);
if (descr == NULL) {
printf("pcap_open_live(): %s\n", errbuf);
exit(1);
}
if (pcap_compile(descr,&fp,portfilter,0,netp) == -1)
{
printf("Err calling pcap_compile\n");
exit(1);
}
if (pcap_setfilter(descr,&fp) == -1)
{
printf("Err setting filter \n");
exit(1);
}
/* allright here we call pcap_loop(..) and pass in our callback function */
/* int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)*/
/* If you are wondering what the user argument is all about, so am I!! */
pcap_loop(descr, -1, capture_callback, NULL);
fprintf(stdout, "\nDone processing packets... wheew!\n");
return 0;
}