]> The Tcpdump Group git mirrors - libpcap/blob - pcap_loop.3pcap
Split the pcap(3) man page into a bunch of individual man pages for
[libpcap] / pcap_loop.3pcap
1 .\" @(#) $Header: /tcpdump/master/libpcap/pcap_loop.3pcap,v 1.1 2008-04-05 20:19:42 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 "4 April 2008"
23 .SH NAME
24 pcap_loop, pcap_dispatch \- process packets from a live capture or savefile
25 .SH SYNOPSIS
26 .nf
27 .ft B
28 #include <pcap.h>
29 .ft
30 .LP
31 .ft B
32 typedef 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
38 int pcap_loop(pcap_t *p, int cnt,
39 .ti +8
40 pcap_handler callback, u_char *user);
41 int pcap_dispatch(pcap_t *p, int cnt,
42 .ti +8
43 pcap_handler callback, u_char *user);
44 .ft
45 .fi
46 .SH DESCRIPTION
47 .B pcap_loop()
48 processes packets from a live capture or ``savefile'' until
49 .I cnt
50 packets are processed, the end of the ``savefile'' is
51 reached when reading from a ``savefile'',
52 .B pcap_breakloop()
53 is called, or an error occurs.
54 It does
55 .B not
56 return when live read timeouts occur.
57 A value of \-1 or 0 for
58 .I cnt
59 is equivalent to infinity, so that packets are processed until another
60 ending condition occurs.
61 .PP
62 .B pcap_dispatch()
63 processes packets from a live capture or ``savefile'' until
64 .I cnt
65 packets are processed, the end of the current bufferful of packets is
66 reached when doing a live capture, the end of the ``savefile'' is
67 reached when reading from a ``savefile'',
68 .B pcap_breakloop()
69 is called, or an error occurs.
70 Thus, when doing a live capture,
71 .I cnt
72 is the maximum number of packets to process before returning, but is not
73 a minimum number; when reading a live capture, only one
74 bufferful of packets is read at a time, so fewer than
75 .I cnt
76 packets may be processed. A value of \-1 or 0 for
77 .I cnt
78 causes all the packets received in one buffer to be processed when
79 reading a live capture, and causes all the packets in the file to be
80 processed when reading a ``savefile''.
81 .PP
82 .ft B
83 (In older versions of libpcap, the behavior when
84 \fIcnt\fP
85 was 0 was undefined; different platforms and devices behaved
86 differently, so code that must work with older versions of libpcap
87 should use \-1, nor 0, as the value of
88 \fIcnt\fP.)
89 .ft R
90 .PP
91 .I callback
92 specifies a routine to be called with three arguments:
93 a
94 .I u_char
95 pointer which is passed in the
96 .I user
97 argument to
98 .B pcap_loop()
99 or
100 .BR pcap_dispatch() ,
101 a
102 .I const struct pcap_pkthdr
103 pointer pointing to the packet time stamp and lengths, and a
104 .I const u_char
105 pointer to the first
106 .B caplen
107 (as given in the
108 .I struct pcap_pkthdr
109 a pointer to which is passed to the callback routine)
110 bytes of data from the packet.
111 .PP
112 .BR NOTE :
113 when reading a live capture,
114 .B pcap_dispatch()
115 will not necessarily return when the read times out; on some platforms,
116 the read timeout isn't supported, and, on other platforms, the timer
117 doesn't start until at least one packet arrives. This means that the
118 read timeout should
119 .B NOT
120 be used in, for example, an interactive application, to allow the packet
121 capture loop to ``poll'' for user input periodically, as there's no
122 guarantee that
123 .B pcap_dispatch()
124 will return after the timeout expires.
125 .SH RETURN VALUE
126 .B pcap_loop()
127 returns 0 if
128 .I cnt
129 is exhausted, \-1 if an error occurs, or \-2 if the loop terminated due
130 to a call to
131 .B pcap_breakloop()
132 before any packets were processed.
133 It does
134 .B not
135 return when live read timeouts occur; instead, it attempts to read more
136 packets.
137 .PP
138 .B pcap_dispatch()
139 returns the number of packets processed on success; this can be 0 if no
140 packets were read from a live capture (if, for example, they were
141 discarded because they didn't pass the packet filter, or if, on
142 platforms that support a read timeout that starts before any packets
143 arrive, the timeout expires before any packets arrive, or if the file
144 descriptor for the capture device is in non-blocking mode and no packets
145 were available to be read) or if no more packets are available in a
146 ``savefile.'' It returns \-1 if an error occurs or \-2 if the loop
147 terminated due to a call to
148 .B pcap_breakloop()
149 before any packets were processed.
150 .ft B
151 If your application uses pcap_breakloop(),
152 make sure that you explicitly check for \-1 and \-2, rather than just
153 checking for a return value < 0.
154 .ft R
155 .PP
156 If \-1 is returned,
157 .B pcap_geterr()
158 or
159 .B pcap_perror()
160 may be called with
161 .I p
162 as an argument to fetch or display the error text.
163 .SH SEE ALSO
164 pcap_geterr(3PCAP), pcap_breakloop(3PCAP)