On Tue, Jul 16, 2002 at 11:29:42AM -0400, Przemyslaw Karwasiecki wrote:
> My major problem is, how (if at all possible) use pcap in nonblocking
> mode,

In libpcap 0.7 and later, there are "pcap_setnonblock()" and
"pcap_getnonblock()" calls, to turn non-blocking mode on and off on a
live-capture pcap_t.

In earlier versions, on UNIX, you can probably just set the O_NONBLOCK
flag on the file descriptor for the pcap_t - use "pcap_fileno()" to get
the file descriptor.  (That is not guaranteed to work in the future -
for example, it won't work if libpcap is using the memory-mapped capture
buffer feature from Linux 2.4, as reads aren't done on the file
descriptor - so, for UNIX, you should use autoconf to determine whether
the libpcap you're using has "pcap_setnonblock()" and
"pcap_getnonblock()" and, if it doesn't, supply your own routines that
use the O_NONBLOCK stuff.)

In non-blocking mode, reads from a live-capture "pcap_t" will be
non-blocking.

> so my application will have main event loop processing 
> in non-blocking mode, both pcap_next packets, and SNMP requests?

On UNIX, you'd do a "select()" or "poll()" on the file descriptor from
"pcap_fileno()" and the file descriptor of the socket on which the SNMP
requests arrive.

Note, however, that "select()" and "poll()" don't necessarily work on
BPF devices in BSD.  A workaround is to have a timeout on the "select()"
or "poll()", equal to the timeout you supplied to "pcap_open_live()",
and attempt to read from the pcap_t if either

        1) its file descriptor can be read, according to "select()" or
           "poll()"

or

        2) the timeout expired.

Note also that this might not work on FreeBSD 4.5 - I've yet to figure
out whether it's possible to make that type of non-blocking I/O work
there.
-
This is the TCPDUMP workers list. It is archived at
http://www.tcpdump.org/lists/workers/index.html
To unsubscribe use mailto:[EMAIL PROTECTED]?body=unsubscribe

Reply via email to