0
已經跟着例子http://www.tcpdump.org/pcap.htm,探索的文件,我看不到有什麼我做錯了用下面的代碼編譯器警告libpcap的
// main.c
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);
/// ...snip...
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet) {
printf("Received Packet\n");
}
/// ...snip...
pcap_dispatch(handle, 100, got_packet, NULL);
,以保證我看到了編譯器警告:
gcc -g -O3 -I../sensor/ -c -o sensordatad.o sensordatad.c
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
main.c:146:3: warning: passing argument 3 of ‘pcap_dispatch’ from incompatible pointer
type [enabled by default]
這可能是我太過挑剔,看起來像我見過的每一個C代碼都有編譯時警告的頁面,但是我至少要完成這段代碼而沒有警告!
我也試過這樣做,以某種方式類型化函數指針,這是我在任何示例中都沒有看到的;但我覺得這值得一試:
pcap_handler callback = &got_packet;
/// ...snip...
pcap_dispatch(handle, 100, &callback, NULL);
這導致了同樣令人困惑的錯誤:
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
這是我第一次在函數指針的特殊Typedef的類型運行,該男子對於pcap_{loop,dispatch}
頁上寫着:
NAME pcap_loop, pcap_dispatch - process packets from a live capture or savefile
SYNOPSIS #include
typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes); int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user); int pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user);