1PCAP_GET_SELECTABLE_FD(3PCAP) PCAP_GET_SELECTABLE_FD(3PCAP)
2
3
4
6 pcap_get_selectable_fd - get a file descriptor on which a select() can
7 be done for a live capture
8
10 #include <pcap/pcap.h>
11
12 int pcap_get_selectable_fd(pcap_t *p);
13
15 pcap_get_selectable_fd() returns, on UNIX, a file descriptor number for
16 a file descriptor on which one can do a select(), poll(), epoll_wait(),
17 kevent(), or other such call to wait for it to be possible to read
18 packets without blocking, if such a descriptor exists, or -1, if no
19 such descriptor exists.
20
21 Some network devices opened with pcap_create() and pcap_activate(), or
22 with pcap_open_live(), do not support those calls (for example, regular
23 network devices on FreeBSD 4.3 and 4.4, and Endace DAG devices), so -1
24 is returned for those devices. In that case, those calls must be given
25 a timeout less than or equal to the timeout returned by
26 pcap_get_required_select_timeout() for the device for which
27 pcap_get_selectable_fd() returned -1, the device must be put in non-
28 blocking mode with a call to pcap_setnonblock(), and an attempt must
29 always be made to read packets from the device when the call returns.
30 If pcap_get_required_select_timeout() returns NULL, it is not possible
31 to wait for packets to arrive on the device in an event loop.
32
33 Note that a device on which a read can be done without blocking may, on
34 some platforms, not have any packets to read if the packet buffer time‐
35 out has expired. A call to pcap_dispatch() or pcap_next_ex() will
36 return 0 in this case, but will not block.
37
38 Note that in:
39
40 FreeBSD prior to FreeBSD 4.6;
41
42 NetBSD prior to NetBSD 3.0;
43
44 OpenBSD prior to OpenBSD 2.4;
45
46 Mac OS X prior to Mac OS X 10.7;
47
48 select(), poll(), and kevent() do not work correctly on BPF devices;
49 pcap_get_selectable_fd() will return a file descriptor on most of those
50 versions (the exceptions being FreeBSD 4.3 and 4.4), but a simple
51 select(), poll(), or kevent() call will not indicate that the descrip‐
52 tor is readable until a full buffer's worth of packets is received,
53 even if the packet timeout expires before then. To work around this,
54 code that uses those calls to wait for packets to arrive must put the
55 pcap_t in non-blocking mode, and must arrange that the call have a
56 timeout less than or equal to the packet buffer timeout, and must try
57 to read packets after that timeout expires, regardless of whether the
58 call indicated that the file descriptor for the pcap_t is ready to be
59 read or not. (That workaround will not work in FreeBSD 4.3 and later;
60 however, in FreeBSD 4.6 and later, those calls work correctly on BPF
61 devices, so the workaround isn't necessary, although it does no harm.)
62
63 Note also that poll() and kevent() doesn't work on character special
64 files, including BPF devices, in Mac OS X 10.4 and 10.5, so, while
65 select() can be used on the descriptor returned by
66 pcap_get_selectable_fd(), poll() and kevent() cannot be used on it
67 those versions of Mac OS X. poll(), but not kevent(), works on that
68 descriptor in Mac OS X releases prior to 10.4; poll() and kevent() work
69 on that descriptor in Mac OS X 10.6 and later.
70
71 pcap_get_selectable_fd() is not available on Windows.
72
74 A selectable file descriptor is returned if one exists; otherwise, -1
75 is returned.
76
78 pcap(3PCAP), pcap_get_required_select_timeout(3PCAP), pcap_setnon‐
79 block(3PCAP), select(2), poll(2), epoll_wait(2), kqueue(2)
80
81
82
83 20 January 2018 PCAP_GET_SELECTABLE_FD(3PCAP)