1 .\" Copyright (c) 1994, 1996, 1997
2 .\" The Regents of the University of California. All rights reserved.
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.
20 .TH PCAP_LOOP 3PCAP "24 December 2008"
22 pcap_loop, pcap_dispatch \- process packets from a live capture or savefile
26 #include <pcap/pcap.h>
30 typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
36 int pcap_loop(pcap_t *p, int cnt,
38 pcap_handler callback, u_char *user);
39 int pcap_dispatch(pcap_t *p, int cnt,
41 pcap_handler callback, u_char *user);
46 processes packets from a live capture or ``savefile'' until
48 packets are processed, the end of the ``savefile'' is
49 reached when reading from a ``savefile'',
51 is called, or an error occurs.
54 return when live read timeouts occur.
55 A value of \-1 or 0 for
57 is equivalent to infinity, so that packets are processed until another
58 ending condition occurs.
61 processes packets from a live capture or ``savefile'' until
63 packets are processed, the end of the current bufferful of packets is
64 reached when doing a live capture, the end of the ``savefile'' is
65 reached when reading from a ``savefile'',
67 is called, or an error occurs.
68 Thus, when doing a live capture,
70 is the maximum number of packets to process before returning, but is not
71 a minimum number; when reading a live capture, only one
72 bufferful of packets is read at a time, so fewer than
74 packets may be processed. A value of \-1 or 0 for
76 causes all the packets received in one buffer to be processed when
77 reading a live capture, and causes all the packets in the file to be
78 processed when reading a ``savefile''.
81 (In older versions of libpcap, the behavior when
83 was 0 was undefined; different platforms and devices behaved
84 differently, so code that must work with older versions of libpcap
85 should use \-1, not 0, as the value of
92 routine to be called with three arguments:
95 pointer which is passed in the
100 .BR pcap_dispatch() ,
102 .I const struct pcap_pkthdr
103 pointer pointing to the packet time stamp and lengths, and a
108 .I struct pcap_pkthdr
109 a pointer to which is passed to the callback routine)
110 bytes of data from the packet. The
111 .I struct pcap_pkthdr
112 and the packet data are not to be freed by the callback routine, and are
113 not guaranteed to be valid after the callback routine returns; if the
114 code needs them to be valid after the callback, it must make a copy of
117 The bytes of data from the packet begin with a link-layer header. The
118 format of the link-layer header is indicated by the return value of the
120 routine when handed the
125 .BR pcap_dispatch() .
126 .I http://www.tcpdump.org/linktypes.html
129 can return and describes the packet formats that
130 correspond to those values. The value it returns will be valid for all
131 packets received unless and until
132 .B pcap_set_datalink()
133 is called; after a successful call to
134 .BR pcap_set_datalink() ,
135 all subsequent packets will have a link-layer header of the type
136 specified by the link-layer header type value passed to
137 .BR pcap_set_datalink() .
141 assume that the packets for a given capture or ``savefile`` will have
142 any given link-layer header type, such as
144 for Ethernet. For example, the "any" device on Linux will have a
145 link-layer header type of
147 even if all devices on the system at the time the "any" device is opened
148 have some other data link type, such as
155 is exhausted or if, when reading from a ``savefile'', no more packets
156 are available. It returns \-1 if an error occurs or \-2 if the loop
157 terminated due to a call to
159 before any packets were processed.
162 return when live read timeouts occur; instead, it attempts to read more
166 returns the number of packets processed on success; this can be 0 if no
167 packets were read from a live capture (if, for example, they were
168 discarded because they didn't pass the packet filter, or if, on
169 platforms that support a read timeout that starts before any packets
170 arrive, the timeout expires before any packets arrive, or if the file
171 descriptor for the capture device is in non-blocking mode and no packets
172 were available to be read) or if no more packets are available in a
173 ``savefile.'' It returns \-1 if an error occurs or \-2 if the loop
174 terminated due to a call to
176 before any packets were processed.
178 If your application uses pcap_breakloop(),
179 make sure that you explicitly check for \-1 and \-2, rather than just
180 checking for a return value < 0.
189 as an argument to fetch or display the error text.
191 pcap(3PCAP), pcap_geterr(3PCAP), pcap_breakloop(3PCAP),