[Winpcap-users] Multithreaded programming with WinPCap
Guy Harris
guy at alum.mit.edu
Sat Apr 29 20:55:52 GMT 2006
Loris Degioanni wrote:
> Prachyabrued Mores wrote:
>> Surprisingly, -1 value (no wait, return immediately) is not mentioned
>> in current version of WinPCap help file. I have to browse through the
>> archives.
>
> For pcap functions, we use the libpcap documentation from tcpdump.org,
> which is not very complete.
...but it *does* mention "pcap_setnonblock()", which is the right way to
request non-blocking mode (with WinPcap, it sets the timeout to -1 - and
saves it, so that if you turn non-blocking mode off, it can put the old
timeout back; on UN*Xes, it sets O_NONBLOCK); the libpcap documentation
wouldn't mention -1, as that's not a generic feature of pcap_open_live().
> Note that setting the timeout to -1 essentially switches your
> architecture to polling, which is efficient but wastes a lot of CPU.
Yup - the pcap_setnonblock() documentation says that:
pcap_setnonblock() puts a capture descriptor, opened with
pcap_open_live(), into "non-blocking" mode, or takes it out of
"non-blocking" mode, depending on whether the nonblock argument is
non-zero or zero. It has no effect on "savefiles". If there is an error,
-1 is returned and errbuf is filled in with an appropriate error
message; otherwise, 0 is returned. In "non-blocking" mode, an attempt to
read from the capture descriptor with pcap_dispatch() will, if no
packets are currently available to be read, return 0 immediately rather
than blocking waiting for packets to arrive. pcap_loop() and pcap_next()
will not work in "non-blocking" mode.
More information about the Winpcap-users
mailing list