Blame pcap_get_selectable_fd.3pcap

Packit 209cc3
.\" Copyright (c) 1994, 1996, 1997
Packit 209cc3
.\"	The Regents of the University of California.  All rights reserved.
Packit 209cc3
.\"
Packit 209cc3
.\" Redistribution and use in source and binary forms, with or without
Packit 209cc3
.\" modification, are permitted provided that: (1) source code distributions
Packit 209cc3
.\" retain the above copyright notice and this paragraph in its entirety, (2)
Packit 209cc3
.\" distributions including binary code include the above copyright notice and
Packit 209cc3
.\" this paragraph in its entirety in the documentation or other materials
Packit 209cc3
.\" provided with the distribution, and (3) all advertising materials mentioning
Packit 209cc3
.\" features or use of this software display the following acknowledgement:
Packit 209cc3
.\" ``This product includes software developed by the University of California,
Packit 209cc3
.\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
Packit 209cc3
.\" the University nor the names of its contributors may be used to endorse
Packit 209cc3
.\" or promote products derived from this software without specific prior
Packit 209cc3
.\" written permission.
Packit 209cc3
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
Packit 209cc3
.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
Packit 209cc3
.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit 209cc3
.\"
Packit 209cc3
.TH PCAP_GET_SELECTABLE_FD 3PCAP "25 July 2018"
Packit 209cc3
.SH NAME
Packit 209cc3
pcap_get_selectable_fd \- get a file descriptor on which a select() can
Packit 209cc3
be done for a live capture
Packit 209cc3
.SH SYNOPSIS
Packit 209cc3
.nf
Packit 209cc3
.ft B
Packit 209cc3
#include <pcap/pcap.h>
Packit 209cc3
.ft
Packit 209cc3
.LP
Packit 209cc3
.ft B
Packit 209cc3
int pcap_get_selectable_fd(pcap_t *p);
Packit 209cc3
.ft
Packit 209cc3
.fi
Packit 209cc3
.SH DESCRIPTION
Packit 209cc3
.B pcap_get_selectable_fd()
Packit 209cc3
returns, on UNIX, a file descriptor number for a file descriptor on
Packit 209cc3
which one can
Packit 209cc3
do a
Packit 209cc3
.BR select(2) ,
Packit 209cc3
.BR poll(2) ,
Packit 209cc3
.BR epoll_wait(2) ,
Packit 209cc3
.BR kevent() ,
Packit 209cc3
or other such call
Packit 209cc3
to wait for it to be possible to read packets without blocking, if such
Packit 209cc3
a descriptor exists, or
Packit 209cc3
.BR PCAP_ERROR ,
Packit 209cc3
if no such descriptor exists.
Packit 209cc3
.PP
Packit 209cc3
Some network devices opened with
Packit 209cc3
.B pcap_create(3PCAP)
Packit 209cc3
and
Packit 209cc3
.BR pcap_activate(3PCAP) ,
Packit 209cc3
or with
Packit 209cc3
.BR pcap_open_live(3PCAP) ,
Packit 209cc3
do not support those calls (for example, regular network devices on
Packit 209cc3
FreeBSD 4.3 and 4.4, and Endace DAG devices), so
Packit 209cc3
.B PCAP_ERROR
Packit 209cc3
is returned for
Packit 209cc3
those devices.  In that case, those calls must be given a timeout less
Packit 209cc3
than or equal to the timeout returned by
Packit 209cc3
.B pcap_get_required_select_timeout(3PCAP)
Packit 209cc3
for the device for which
Packit 209cc3
.B pcap_get_selectable_fd()
Packit 209cc3
returned
Packit 209cc3
.BR PCAP_ERROR ,
Packit 209cc3
the device must be put in non-blocking mode with a call to
Packit 209cc3
.BR \%pcap_setnonblock(3PCAP) ,
Packit 209cc3
and an attempt must always be made to read packets from the device
Packit 209cc3
when the call returns.  If
Packit 209cc3
.B \%pcap_get_required_select_timeout()
Packit 209cc3
returns
Packit 209cc3
.BR NULL ,
Packit 209cc3
it is not possible to wait for packets to arrive on the device in an
Packit 209cc3
event loop.
Packit 209cc3
.PP
Packit 209cc3
Note that a device on which a read can be done without blocking may,
Packit 209cc3
on some platforms, not have any packets to read if the packet buffer
Packit 209cc3
timeout has expired.  A call to
Packit 209cc3
.B pcap_dispatch(3PCAP)
Packit 209cc3
or
Packit 209cc3
.B pcap_next_ex(3PCAP)
Packit 209cc3
will return 0 in this case, but will not block.
Packit 209cc3
.PP
Packit 209cc3
Note that in:
Packit 209cc3
.IP
Packit 209cc3
FreeBSD prior to FreeBSD 4.6;
Packit 209cc3
.IP
Packit 209cc3
NetBSD prior to NetBSD 3.0;
Packit 209cc3
.IP
Packit 209cc3
OpenBSD prior to OpenBSD 2.4;
Packit 209cc3
.IP
Packit 209cc3
Mac OS X prior to Mac OS X 10.7;
Packit 209cc3
.PP
Packit 209cc3
.BR select() ,
Packit 209cc3
.BR poll() ,
Packit 209cc3
and
Packit 209cc3
.B kevent()
Packit 209cc3
do not work correctly on BPF devices;
Packit 209cc3
.B pcap_get_selectable_fd()
Packit 209cc3
will return a file descriptor on most of those versions (the exceptions
Packit 209cc3
being FreeBSD 4.3 and 4.4), but a simple
Packit 209cc3
.BR select() ,
Packit 209cc3
.BR poll() ,
Packit 209cc3
or
Packit 209cc3
.B kevent()
Packit 209cc3
call will not indicate that the descriptor is readable until a full
Packit 209cc3
buffer's worth of packets is received, even if the packet timeout
Packit 209cc3
expires before then.  To work around this, code that uses
Packit 209cc3
those calls to wait for packets to arrive must put the
Packit 209cc3
.B pcap_t
Packit 209cc3
in non-blocking mode, and must arrange that the call
Packit 209cc3
have a timeout less than or equal to the packet buffer timeout,
Packit 209cc3
and must try to read packets after that timeout expires, regardless of
Packit 209cc3
whether the call indicated that the file descriptor for the
Packit 209cc3
.B pcap_t
Packit 209cc3
is ready to be read or not.  (That workaround will not work in FreeBSD
Packit 209cc3
4.3 and later; however, in FreeBSD 4.6 and later, those calls
Packit 209cc3
work correctly on BPF devices, so the workaround isn't necessary,
Packit 209cc3
although it does no harm.)
Packit 209cc3
.PP
Packit 209cc3
Note also that
Packit 209cc3
.B poll()
Packit 209cc3
and
Packit 209cc3
.B kevent()
Packit 209cc3
doesn't work on character special files, including BPF devices, in Mac
Packit 209cc3
OS X 10.4 and 10.5, so, while
Packit 209cc3
.B select()
Packit 209cc3
can be used on the descriptor returned by
Packit 209cc3
.BR pcap_get_selectable_fd() ,
Packit 209cc3
.B poll()
Packit 209cc3
and
Packit 209cc3
.B kevent()
Packit 209cc3
cannot be used on it those versions of Mac OS X.
Packit 209cc3
.BR poll() ,
Packit 209cc3
but not
Packit 209cc3
.BR kevent() ,
Packit 209cc3
works on that descriptor in Mac OS X releases prior to
Packit 209cc3
10.4;
Packit 209cc3
.B poll()
Packit 209cc3
and
Packit 209cc3
.B kevent()
Packit 209cc3
work on that descriptor in Mac OS X 10.6 and later.
Packit 209cc3
.PP
Packit 209cc3
.B pcap_get_selectable_fd()
Packit 209cc3
is not available on Windows.
Packit 209cc3
.SH RETURN VALUE
Packit 209cc3
A selectable file descriptor is returned if one exists; otherwise,
Packit 209cc3
.B PCAP_ERROR
Packit 209cc3
is returned.
Packit 209cc3
.SH SEE ALSO
Packit 209cc3
pcap(3PCAP), kqueue(2)