On Tue, 2002-01-08 at 11:39, Marco Slaviero wrote: > Hi all > > The app I'm busy with has the common situation of polling for user input, > and waiting for packets from libpcap. I've been through the archives, and the > select() was removed from pcap-linux.c with good reason. > [http://www.tcpdump.org/lists/workers/2000/msg01674.html] > > However, according to Guy Harris, select()/poll() it still the best method to > accomplish the test for user input/packets. > [http://www.tcpdump.org/lists/workers/2001/12/msg00087.html] > > How about adding a pcap_is_packet_waiting() which will perform the test > for you. The app would call pcap_dispatch() only if > pcap_is_packet_waiting() returns true, and the app can get on with other > things on false. > Arguments could be a pcap_t struct, and perhaps a timeout in > microsecs. It seems that a select()/poll() based test would work on most > systems, so surely this provides a platform-independent way to test?
I disagree. A pcap_t object is related to one, and only one, interface.
Application writters often use select() or poll() on the pcap file
descriptor (accessed throught pcap_fileno()) in order to poll for event
on multiple devices.
Another point is that accessing the real capture fd may permit you to do
much more thing than what the function you suggest would.
<pseudo setup code>
int i;
struct pollfd pfd[interfaces_nbr];
for ( i = 0; i < interfaces_nbr; i++ ) {
pfd[i].events = POLLIN;
pfd[i].fd = pcap_fileno(devices[i]->pd);
}
</pseudo setup code>
<pseudo capture code>
while ( 1 ) {
do {
ret = poll(pfd, interfaces_nbr, -1);
} while ( ret < 0 && errno == EINTR );
for ( i = 0; i < interfaces_nbr; i++ ) {
dev = devices[i];
if ( pfd[i].revents & POLLIN ) {
/* handle event on device corresponding to
* the pollfd entry
*/
handle_device_event_here(devices[i]);
}
}
}
</pseudo capture code>
This code completly lack any error handling, but you get the point.
--
Yoann Vandoorselaere
http://www.prelude-ids.org
msg00875/pgp00000.pgp
Description: PGP signature
