blob: c2ba33c231a9f3875cd80f62a775e872b272c62c [file] [log] [blame]
JP Abgrall511eca32014-02-12 13:46:45 -08001.\" @(#) $Header: /tcpdump/master/libpcap/pcap_loop.3pcap,v 1.4 2008-12-25 02:01:32 guy Exp $
2.\"
3.\" Copyright (c) 1994, 1996, 1997
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that: (1) source code distributions
8.\" retain the above copyright notice and this paragraph in its entirety, (2)
9.\" distributions including binary code include the above copyright notice and
10.\" this paragraph in its entirety in the documentation or other materials
11.\" provided with the distribution, and (3) all advertising materials mentioning
12.\" features or use of this software display the following acknowledgement:
13.\" ``This product includes software developed by the University of California,
14.\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15.\" the University nor the names of its contributors may be used to endorse
16.\" or promote products derived from this software without specific prior
17.\" written permission.
18.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21.\"
22.TH PCAP_LOOP 3PCAP "24 December 2008"
23.SH NAME
24pcap_loop, pcap_dispatch \- process packets from a live capture or savefile
25.SH SYNOPSIS
26.nf
27.ft B
28#include <pcap/pcap.h>
29.ft
30.LP
31.ft B
32typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
33.ti +8
34 const u_char *bytes);
35.ft
36.LP
37.ft B
38int pcap_loop(pcap_t *p, int cnt,
39.ti +8
40pcap_handler callback, u_char *user);
41int pcap_dispatch(pcap_t *p, int cnt,
42.ti +8
43pcap_handler callback, u_char *user);
44.ft
45.fi
46.SH DESCRIPTION
47.B pcap_loop()
48processes packets from a live capture or ``savefile'' until
49.I cnt
50packets are processed, the end of the ``savefile'' is
51reached when reading from a ``savefile'',
52.B pcap_breakloop()
53is called, or an error occurs.
54It does
55.B not
56return when live read timeouts occur.
57A value of \-1 or 0 for
58.I cnt
59is equivalent to infinity, so that packets are processed until another
60ending condition occurs.
61.PP
62.B pcap_dispatch()
63processes packets from a live capture or ``savefile'' until
64.I cnt
65packets are processed, the end of the current bufferful of packets is
66reached when doing a live capture, the end of the ``savefile'' is
67reached when reading from a ``savefile'',
68.B pcap_breakloop()
69is called, or an error occurs.
70Thus, when doing a live capture,
71.I cnt
72is the maximum number of packets to process before returning, but is not
73a minimum number; when reading a live capture, only one
74bufferful of packets is read at a time, so fewer than
75.I cnt
76packets may be processed. A value of \-1 or 0 for
77.I cnt
78causes all the packets received in one buffer to be processed when
79reading a live capture, and causes all the packets in the file to be
80processed when reading a ``savefile''.
81.PP
82.ft B
83(In older versions of libpcap, the behavior when
84\fIcnt\fP
85was 0 was undefined; different platforms and devices behaved
86differently, so code that must work with older versions of libpcap
87should use \-1, not 0, as the value of
88\fIcnt\fP.)
89.ft R
90.PP
91.I callback
92specifies a
93.I pcap_handler
94routine to be called with three arguments:
95a
96.I u_char
97pointer which is passed in the
98.I user
99argument to
100.B pcap_loop()
101or
102.BR pcap_dispatch() ,
103a
104.I const struct pcap_pkthdr
105pointer pointing to the packet time stamp and lengths, and a
106.I const u_char
107pointer to the first
108.B caplen
109(as given in the
110.I struct pcap_pkthdr
111a pointer to which is passed to the callback routine)
112bytes of data from the packet. The
113.I struct pcap_pkthdr
114and the packet data are not to be freed by the callback routine, and are
115not guaranteed to be valid after the callback routine returns; if the
116code needs them to be valid after the callback, it must make a copy of
117them.
118.PP
119The bytes of data from the packet begin with a link-layer header. The
120format of the link-layer header is indicated by the return value of the
121.B pcap_datalink()
122routine when handed the
123.B pcap_t
124value also passed to
125.B pcap_loop()
126or
127.BR pcap_dispatch() .
128.I http://www.tcpdump.org/linktypes.html
129lists the values
130.B pcap_datalink()
131can return and describes the packet formats that
132correspond to those values. The value it returns will be valid for all
133packets received unless and until
134.B pcap_set_datalink()
135is called; after a successful call to
136.BR pcap_set_datalink() ,
137all subsequent packets will have a link-layer header of the type
138specified by the link-layer header type value passed to
139.BR pcap_set_datalink() .
140.PP
141Do
142.B NOT
143assume that the packets for a given capture or ``savefile`` will have
144any given link-layer header type, such as
145.B DLT_EN10MB
146for Ethernet. For example, the "any" device on Linux will have a
147link-layer header type of
148.B DLT_LINUX_SLL
149even if all devices on the system at the time the "any" device is opened
150have some other data link type, such as
151.B DLT_EN10MB
152for Ethernet.
153.SH RETURN VALUE
154.B pcap_loop()
155returns 0 if
156.I cnt
157is exhausted or if, when reading from a ``savefile'', no more packets
158are available. It returns \-1 if an error occurs or \-2 if the loop
159terminated due to a call to
160.B pcap_breakloop()
161before any packets were processed.
162It does
163.B not
164return when live read timeouts occur; instead, it attempts to read more
165packets.
166.PP
167.B pcap_dispatch()
168returns the number of packets processed on success; this can be 0 if no
169packets were read from a live capture (if, for example, they were
170discarded because they didn't pass the packet filter, or if, on
171platforms that support a read timeout that starts before any packets
172arrive, the timeout expires before any packets arrive, or if the file
173descriptor for the capture device is in non-blocking mode and no packets
174were available to be read) or if no more packets are available in a
175``savefile.'' It returns \-1 if an error occurs or \-2 if the loop
176terminated due to a call to
177.B pcap_breakloop()
178before any packets were processed.
179.ft B
180If your application uses pcap_breakloop(),
181make sure that you explicitly check for \-1 and \-2, rather than just
182checking for a return value < 0.
183.ft R
184.PP
185If \-1 is returned,
186.B pcap_geterr()
187or
188.B pcap_perror()
189may be called with
190.I p
191as an argument to fetch or display the error text.
192.SH SEE ALSO
193pcap(3PCAP), pcap_geterr(3PCAP), pcap_breakloop(3PCAP),
194pcap_datalink(3PCAP)