同一節目能成功在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;
}