c++ - How do I make libpcap/pcap_loop non-blocking? -
i'm using libpcap sniff traffic in promiscuous mode
int main() { // stuff printf("opening device: %s\n", devname.c_str()); handle = pcap_open_live(devname.c_str(), 65536 , 1 , 0 , errbuf); if (handle == null) { fprintf(stderr, "couldn't open device %s : %s..." , devname.c_str(), errbuf); return 1; } printf(" done\n"); pcap_loop(handle , -1 , process_packet , null); // here run thread stuff. however, pcap_loop blocking return 0; }
i'd add external thread other stuff. how change code above make non-blocking?
when use non-blocking mode on libpcap have use pcap_dispatch, note, pcap_dispatch
can work in blocking or in non-blocking mode, depends how set libpcap, set libpcap work in non-blocking have use function pcap_setnonblock
:
int pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);
the difference between blocking , non-blocking not loop runs forever, in blocking function pcap_dispatch
waits packet , returns when packet received, however, in non-blocking mode function returns , callback must process packet.
in "non-blocking" mode, attempt read capture descriptor pcap_dispatch() will, if no packets available read, return 0 rather blocking waiting packets arrive. pcap_loop() , pcap_next() not work in "non-blocking" mode.
Comments
Post a Comment