]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libpcap/pcap_get_selectable_fd.3pcap
sysctl(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / contrib / libpcap / pcap_get_selectable_fd.3pcap
1 .\" Copyright (c) 1994, 1996, 1997
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that: (1) source code distributions
6 .\" retain the above copyright notice and this paragraph in its entirety, (2)
7 .\" distributions including binary code include the above copyright notice and
8 .\" this paragraph in its entirety in the documentation or other materials
9 .\" provided with the distribution, and (3) all advertising materials mentioning
10 .\" features or use of this software display the following acknowledgement:
11 .\" ``This product includes software developed by the University of California,
12 .\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
13 .\" the University nor the names of its contributors may be used to endorse
14 .\" or promote products derived from this software without specific prior
15 .\" written permission.
16 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
17 .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 .\"
20 .TH PCAP_GET_SELECTABLE_FD 3PCAP "25 July 2018"
21 .SH NAME
22 pcap_get_selectable_fd \- get a file descriptor on which a select() can
23 be done for a live capture
24 .SH SYNOPSIS
25 .nf
26 .ft B
27 #include <pcap/pcap.h>
28 .ft
29 .LP
30 .ft B
31 int pcap_get_selectable_fd(pcap_t *p);
32 .ft
33 .fi
34 .SH DESCRIPTION
35 .B pcap_get_selectable_fd()
36 returns, on UNIX, a file descriptor number for a file descriptor on
37 which one can
38 do a
39 .BR select(2) ,
40 .BR poll(2) ,
41 .BR epoll_wait(2) ,
42 .BR kevent() ,
43 or other such call
44 to wait for it to be possible to read packets without blocking, if such
45 a descriptor exists, or
46 .BR PCAP_ERROR ,
47 if no such descriptor exists.
48 .PP
49 Some network devices opened with
50 .B pcap_create(3PCAP)
51 and
52 .BR pcap_activate(3PCAP) ,
53 or with
54 .BR pcap_open_live(3PCAP) ,
55 do not support those calls (for example, regular network devices on
56 FreeBSD 4.3 and 4.4, and Endace DAG devices), so
57 .B PCAP_ERROR
58 is returned for
59 those devices.  In that case, those calls must be given a timeout less
60 than or equal to the timeout returned by
61 .B pcap_get_required_select_timeout(3PCAP)
62 for the device for which
63 .B pcap_get_selectable_fd()
64 returned
65 .BR PCAP_ERROR ,
66 the device must be put in non-blocking mode with a call to
67 .BR \%pcap_setnonblock(3PCAP) ,
68 and an attempt must always be made to read packets from the device
69 when the call returns.  If
70 .B \%pcap_get_required_select_timeout()
71 returns
72 .BR NULL ,
73 it is not possible to wait for packets to arrive on the device in an
74 event loop.
75 .PP
76 Note that a device on which a read can be done without blocking may,
77 on some platforms, not have any packets to read if the packet buffer
78 timeout has expired.  A call to
79 .B pcap_dispatch(3PCAP)
80 or
81 .B pcap_next_ex(3PCAP)
82 will return 0 in this case, but will not block.
83 .PP
84 Note that in:
85 .IP
86 FreeBSD prior to FreeBSD 4.6;
87 .IP
88 NetBSD prior to NetBSD 3.0;
89 .IP
90 OpenBSD prior to OpenBSD 2.4;
91 .IP
92 Mac OS X prior to Mac OS X 10.7;
93 .PP
94 .BR select() ,
95 .BR poll() ,
96 and
97 .B kevent()
98 do not work correctly on BPF devices;
99 .B pcap_get_selectable_fd()
100 will return a file descriptor on most of those versions (the exceptions
101 being FreeBSD 4.3 and 4.4), but a simple
102 .BR select() ,
103 .BR poll() ,
104 or
105 .B kevent()
106 call will not indicate that the descriptor is readable until a full
107 buffer's worth of packets is received, even if the packet timeout
108 expires before then.  To work around this, code that uses
109 those calls to wait for packets to arrive must put the
110 .B pcap_t
111 in non-blocking mode, and must arrange that the call
112 have a timeout less than or equal to the packet buffer timeout,
113 and must try to read packets after that timeout expires, regardless of
114 whether the call indicated that the file descriptor for the
115 .B pcap_t
116 is ready to be read or not.  (That workaround will not work in FreeBSD
117 4.3 and later; however, in FreeBSD 4.6 and later, those calls
118 work correctly on BPF devices, so the workaround isn't necessary,
119 although it does no harm.)
120 .PP
121 Note also that
122 .B poll()
123 and
124 .B kevent()
125 doesn't work on character special files, including BPF devices, in Mac
126 OS X 10.4 and 10.5, so, while
127 .B select()
128 can be used on the descriptor returned by
129 .BR pcap_get_selectable_fd() ,
130 .B poll()
131 and
132 .B kevent()
133 cannot be used on it those versions of Mac OS X.
134 .BR poll() ,
135 but not
136 .BR kevent() ,
137 works on that descriptor in Mac OS X releases prior to
138 10.4;
139 .B poll()
140 and
141 .B kevent()
142 work on that descriptor in Mac OS X 10.6 and later.
143 .PP
144 .B pcap_get_selectable_fd()
145 is not available on Windows.
146 .SH RETURN VALUE
147 A selectable file descriptor is returned if one exists; otherwise,
148 .B PCAP_ERROR
149 is returned.
150 .SH SEE ALSO
151 pcap(3PCAP), kqueue(2)