blob: 0256a6a5e2f80133789b4fe6f082acbabaf7c9e1 [file] [log] [blame]
Haibo Huang165065a2018-07-23 17:26:52 -07001.\" 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.\"
Haibo Huangee759ce2021-01-05 21:34:29 -080020.TH PCAP_GET_REQUIRED_SELECT_TIMEOUT 3PCAP "29 January 2020"
Haibo Huang165065a2018-07-23 17:26:52 -070021.SH NAME
Haibo Huangee759ce2021-01-05 21:34:29 -080022pcap_get_required_select_timeout \- get a timeout to be used when doing
23select() for a live capture
Haibo Huang165065a2018-07-23 17:26:52 -070024.SH SYNOPSIS
25.nf
26.ft B
27#include <pcap/pcap.h>
28.ft
29.LP
30.ft B
Haibo Huangee759ce2021-01-05 21:34:29 -080031const struct timeval *pcap_get_required_select_timeout(pcap_t *p);
Haibo Huang165065a2018-07-23 17:26:52 -070032.ft
33.fi
34.SH DESCRIPTION
Haibo Huangee759ce2021-01-05 21:34:29 -080035.BR pcap_get_required_select_timeout ()
Haibo Huang165065a2018-07-23 17:26:52 -070036returns, on UNIX, a pointer to a
37.B struct timeval
38containing a value that must be used as the minimum timeout in
Haibo Huangee759ce2021-01-05 21:34:29 -080039.BR select (2),
40.BR poll (2),
41.BR epoll_wait (2),
Haibo Huang165065a2018-07-23 17:26:52 -070042and
Haibo Huangee759ce2021-01-05 21:34:29 -080043.BR kevent (2)
44calls, or
45.B NULL
46if there is no such timeout.
47If a
48.RB non- NULL
49value is returned, it must be used regardless of whether
50.BR pcap_get_selectable_fd (3PCAP)
Haibo Huang4ccd6832020-04-23 18:03:48 -070051returns
Haibo Huangee759ce2021-01-05 21:34:29 -080052.B \-1
53for any descriptor on which those calls are being done.
54.BR pcap_get_required_select_timeout ()
55should be called for all
56.BR pcap_t s
57before a call to
58.BR select (),
59.BR poll (),
60.BR epoll_wait (),
Haibo Huang165065a2018-07-23 17:26:52 -070061or
Haibo Huangee759ce2021-01-05 21:34:29 -080062.BR kevent (),
63and any timeouts used for those calls should be updated as appropriate
64given the new value of the timeout.
65.PP
66For
67.BR kevent (),
68one
69.B EVFILT_TIMER
70filter per selectable descriptor can be used, rather than using the
71timeout argument to
72.BR kevent ();
73if the
74.B EVFILT_TIMER
75event for a particular selectable descriptor signals an event,
76.BR pcap_dispatch (3PCAP)
77should be called for the corresponding
78.BR pcap_t .
79.PP
80On Linux systems with
81.BR timerfd_create (2),
82one timer object created by
83.BR timerfd_create ()
84per selectable descriptor can be used, rather than using the timeout
85argument to
86.BR epoll_wait ();
87if the
88timer object for a particular selectable descriptor signals an event,
89.BR pcap_dispatch (3PCAP)
90should be called for the corresponding
91.BR pcap_t .
92.PP
93Otherwise, a timeout value no larger than
94the smallest of all timeouts returned by
95.BR \%pcap_get_required_select_timeout ()
96for devices from which packets will be captured and any other timeouts
97to be used in the call should be used as the timeout for the call, and,
98when the call returns,
99.BR pcap_dispatch (3PCAP)
100should be called for all
101.BR pcap_t s
102for which a
103.RB non- NULL
104timeout was returned, regardless of whether it's indicated as having
105anything to read from it or not.
106.PP
107All devices with a
108.RB non- NULL
109timeout must be put in non-blocking mode with
110.BR pcap_setnonblock (3PCAP).
Haibo Huang165065a2018-07-23 17:26:52 -0700111.PP
112Note that a device on which a read can be done without blocking may,
113on some platforms, not have any packets to read if the packet buffer
114timeout has expired. A call to
Haibo Huangee759ce2021-01-05 21:34:29 -0800115.BR pcap_dispatch ()
Haibo Huang165065a2018-07-23 17:26:52 -0700116or
Haibo Huangee759ce2021-01-05 21:34:29 -0800117.BR pcap_next_ex (3PCAP)
118will return
119.B 0
120in this case, but will not block.
Haibo Huang165065a2018-07-23 17:26:52 -0700121.PP
Haibo Huangee759ce2021-01-05 21:34:29 -0800122.BR pcap_get_required_select_timeout ()
Haibo Huang165065a2018-07-23 17:26:52 -0700123is not available on Windows.
124.SH RETURN VALUE
125A pointer to a
126.B struct timeval
127is returned if the timeout is required; otherwise
128.B NULL
129is returned.
Haibo Huang4ccd6832020-04-23 18:03:48 -0700130.SH BACKWARD COMPATIBILITY
131This function became available in libpcap release 1.9.0. In previous
132releases,
Haibo Huangee759ce2021-01-05 21:34:29 -0800133.BR select (),
134.BR poll (),
135.BR epoll_wait (),
Haibo Huang4ccd6832020-04-23 18:03:48 -0700136and
Haibo Huangee759ce2021-01-05 21:34:29 -0800137.BR kevent ()
Haibo Huang4ccd6832020-04-23 18:03:48 -0700138cannot be used on any capture source for which
Haibo Huangee759ce2021-01-05 21:34:29 -0800139.BR pcap_get_selectable_fd ()
140returns
141.BR \-1 .
142.PP
143In libpcap release 1.10.0 and later, the timeout value can change from
144call to call, so
145.BR pcap_get_required_select_timeout ()
146must be called before each call to
147.BR select (),
148.BR poll (),
149.BR epoll_wait (),
150or
151.BR kevent (),
152and the new value must be used to calculate timeouts for the call. Code
153that does that will also work with libpcap 1.9.x releases, so code
154using
155.BR pcap_get_required_select_timeout ()
156should be changed to call it for each call to
157.BR select (),
158.BR poll (),
159.BR epoll_wait (),
160or
161.BR kevent ()
162even if the code must also work with libpcap 1.9.x.
163.SH BACKWARD COMPATIBILITY
164This function became available in libpcap release 1.9.0. In previous
165releases,
166.BR select (),
167.BR poll (),
168.BR epoll_wait (),
169and
170.BR kevent ()
171could not be used for devices that don't provide a selectable file
172descriptor.
Haibo Huang165065a2018-07-23 17:26:52 -0700173.SH SEE ALSO
Haibo Huangee759ce2021-01-05 21:34:29 -0800174.BR pcap (3PCAP),
175.BR pcap_get_selectable_fd (3PCAP),
176.BR select (2),
177.BR poll (2),
178.BR epoll_wait (2),
179.BR kqueue (2)