nrelease - fix/improve livecd
[dragonfly.git] / contrib / libpcap / pcap_loop.3pcap
CommitLineData
de0d3203
PA
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.\"
ea16f64e 20.TH PCAP_LOOP 3PCAP "22 August 2020"
de0d3203
PA
21.SH NAME
22pcap_loop, pcap_dispatch \- process packets from a live capture or savefile
23.SH SYNOPSIS
24.nf
25.ft B
26#include <pcap/pcap.h>
27.ft
28.LP
29.ft B
30typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
31.ti +8
32 const u_char *bytes);
33.ft
34.LP
35.ft B
36int pcap_loop(pcap_t *p, int cnt,
37.ti +8
38pcap_handler callback, u_char *user);
39int pcap_dispatch(pcap_t *p, int cnt,
40.ti +8
41pcap_handler callback, u_char *user);
42.ft
43.fi
44.SH DESCRIPTION
ea16f64e 45.BR pcap_loop ()
de0d3203
PA
46processes packets from a live capture or ``savefile'' until
47.I cnt
48packets are processed, the end of the ``savefile'' is
49reached when reading from a ``savefile'',
ea16f64e 50.BR pcap_breakloop (3PCAP)
de0d3203
PA
51is called, or an error occurs.
52It does
53.B not
3a289941 54return when live packet buffer timeouts occur.
ea16f64e
AHJ
55A value of
56.B \-1
57or
58.B 0
59for
de0d3203
PA
60.I cnt
61is equivalent to infinity, so that packets are processed until another
62ending condition occurs.
63.PP
ea16f64e 64.BR pcap_dispatch ()
de0d3203
PA
65processes packets from a live capture or ``savefile'' until
66.I cnt
67packets are processed, the end of the current bufferful of packets is
68reached when doing a live capture, the end of the ``savefile'' is
69reached when reading from a ``savefile'',
ea16f64e 70.BR pcap_breakloop ()
de0d3203
PA
71is called, or an error occurs.
72Thus, when doing a live capture,
73.I cnt
74is the maximum number of packets to process before returning, but is not
75a minimum number; when reading a live capture, only one
76bufferful of packets is read at a time, so fewer than
77.I cnt
ea16f64e
AHJ
78packets may be processed. A value of
79.B \-1
80or
81.B 0
82for
de0d3203
PA
83.I cnt
84causes all the packets received in one buffer to be processed when
85reading a live capture, and causes all the packets in the file to be
86processed when reading a ``savefile''.
87.PP
97a9217a
AHJ
88Note that, when doing a live capture on some platforms, if the read
89timeout expires when there are no packets available,
ea16f64e 90.BR pcap_dispatch ()
97a9217a
AHJ
91will return 0, even when not in non-blocking mode, as there are no
92packets to process. Applications should be prepared for this to happen,
93but must not rely on it happening.
94.PP
de0d3203 95.I callback
a85e14b0
PA
96specifies a
97.I pcap_handler
98routine to be called with three arguments:
de0d3203
PA
99a
100.I u_char
101pointer which is passed in the
102.I user
103argument to
ea16f64e 104.BR pcap_loop ()
de0d3203 105or
ea16f64e 106.BR pcap_dispatch (),
de0d3203
PA
107a
108.I const struct pcap_pkthdr
109pointer pointing to the packet time stamp and lengths, and a
110.I const u_char
111pointer to the first
112.B caplen
113(as given in the
114.I struct pcap_pkthdr
115a pointer to which is passed to the callback routine)
a85e14b0
PA
116bytes of data from the packet. The
117.I struct pcap_pkthdr
118and the packet data are not to be freed by the callback routine, and are
119not guaranteed to be valid after the callback routine returns; if the
120code needs them to be valid after the callback, it must make a copy of
121them.
97a9217a
AHJ
122.PP
123The bytes of data from the packet begin with a link-layer header. The
124format of the link-layer header is indicated by the return value of the
ea16f64e 125.BR pcap_datalink (3PCAP)
97a9217a
AHJ
126routine when handed the
127.B pcap_t
128value also passed to
ea16f64e 129.BR pcap_loop ()
97a9217a 130or
ea16f64e 131.BR pcap_dispatch ().
3a289941 132.I https://www.tcpdump.org/linktypes.html
97a9217a 133lists the values
ea16f64e 134.BR pcap_datalink ()
97a9217a
AHJ
135can return and describes the packet formats that
136correspond to those values. The value it returns will be valid for all
137packets received unless and until
ea16f64e 138.BR pcap_set_datalink (3PCAP)
97a9217a 139is called; after a successful call to
ea16f64e 140.BR pcap_set_datalink (),
97a9217a
AHJ
141all subsequent packets will have a link-layer header of the type
142specified by the link-layer header type value passed to
ea16f64e 143.BR pcap_set_datalink ().
97a9217a
AHJ
144.PP
145Do
146.B NOT
147assume that the packets for a given capture or ``savefile`` will have
148any given link-layer header type, such as
149.B DLT_EN10MB
150for Ethernet. For example, the "any" device on Linux will have a
151link-layer header type of
152.B DLT_LINUX_SLL
ea16f64e
AHJ
153or
154.B DLT_LINUX_SLL2
97a9217a
AHJ
155even if all devices on the system at the time the "any" device is opened
156have some other data link type, such as
157.B DLT_EN10MB
158for Ethernet.
de0d3203 159.SH RETURN VALUE
ea16f64e
AHJ
160.BR pcap_loop ()
161returns
162.B 0
163if
de0d3203 164.I cnt
0e381983 165is exhausted or if, when reading from a ``savefile'', no more packets
3a289941
AL
166are available. It returns
167.B PCAP_ERROR
168if an error occurs or
169.B PCAP_ERROR_BREAK
170if the loop terminated due to a call to
ea16f64e 171.BR pcap_breakloop ()
de0d3203
PA
172before any packets were processed.
173It does
174.B not
3a289941
AL
175return when live packet buffer timeouts occur; instead, it attempts to
176read more packets.
de0d3203 177.PP
ea16f64e 178.BR pcap_dispatch ()
de0d3203
PA
179returns the number of packets processed on success; this can be 0 if no
180packets were read from a live capture (if, for example, they were
181discarded because they didn't pass the packet filter, or if, on
3a289941
AL
182platforms that support a packet buffer timeout that starts before any
183packets arrive, the timeout expires before any packets arrive, or if the
184file descriptor for the capture device is in non-blocking mode and no
185packets were available to be read) or if no more packets are available
186in a ``savefile.'' It returns
187.B PCAP_ERROR
188if an error occurs or
189.B PCAP_ERROR_BREAK
190if the loop terminated due to a call to
ea16f64e 191.BR pcap_breakloop ()
de0d3203
PA
192before any packets were processed.
193.ft B
194If your application uses pcap_breakloop(),
3a289941
AL
195make sure that you explicitly check for PCAP_ERROR and PCAP_ERROR_BREAK,
196rather than just checking for a return value < 0.
de0d3203
PA
197.ft R
198.PP
3a289941
AL
199If
200.B PCAP_ERROR
201is returned,
ea16f64e 202.BR pcap_geterr (3PCAP)
de0d3203 203or
ea16f64e 204.BR pcap_perror (3PCAP)
de0d3203
PA
205may be called with
206.I p
207as an argument to fetch or display the error text.
ea16f64e
AHJ
208.SH BACKWARD COMPATIBILITY
209.PP
210In libpcap versions before 1.5.0, the behavior when
211.I cnt
212was
213.B 0
214was undefined; different platforms and devices behaved differently,
215so code that must work with these versions of libpcap should use
216.BR \-1 ,
217not
218.BR 0 ,
219as the value of
220.IR cnt .
de0d3203 221.SH SEE ALSO
ea16f64e 222.BR pcap (3PCAP)