]> The Tcpdump Group git mirrors - libpcap/blob - pcap.3pcap
Document the new API's, move a bunch of information about libpcap
[libpcap] / pcap.3pcap
1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3pcap,v 1.3 2008-04-06 02:53:21 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 3PCAP "4 April 2008"
23 .SH NAME
24 pcap \- Packet Capture library
25 .SH SYNOPSIS
26 .nf
27 .ft B
28 #include <pcap/pcap.h>
29 .LP
30 .ft B
31 .ft
32 .fi
33 .SH DESCRIPTION
34 The Packet Capture library
35 provides a high level interface to packet capture systems. All packets
36 on the network, even those destined for other hosts, are accessible
37 through this mechanism.
38 It also supports saving captured packets to a ``savefile'', and reading
39 packets from a ``savefile''.
40 .PP
41 To open a handle for a live capture, call
42 .BR pcap_create() ,
43 set the appropriate options on the handle, and then activate it with
44 .BR pcap_activate() .
45 To open a handle for a ``savefile'' with captured packets, call
46 .BR pcap_open_offline() .
47 Both
48 .B pcap_create()
49 and
50 .B pcap_open_offline()
51 return a pointer to a
52 .BR pcap_t ,
53 which is the handle used for reading packets from the capture stream or
54 the ``savefile'', and for finding out information about the capture
55 stream or ``savefile''.
56 .PP
57 The options that can be set on a capture handle include
58 .IP "snapshot length"
59 If, when capturing, you capture the entire contents of the packet, that
60 requires more CPU time to copy the packet to your application, more disk
61 and possibly network bandwidth to write the packet data to a file, and
62 more disk space to save the packet. If you don't need the entire
63 contents of the packet - for example, if you are only interested in the
64 TCP headers of packets - you can set the "snapshot length" for the
65 capture to an appropriate value. If the snapshot length is set to
66 .IR snaplen ,
67 and
68 .I snaplen
69 is less
70 than the size of a packet that is captured, only the first
71 .I snaplen
72 bytes of that packet will be captured and provided as packet data.
73 .IP
74 A snapshot length of 65535 should be sufficient, on most if not all
75 networks, to capture all the data available from the packet.
76 .IP
77 The snapshot length is set with
78 .BR pcap_set_snaplen() .
79 .IP "promiscuous mode"
80 On broadcast LANs such as Ethernet, if the network isn't switched, or if
81 the adapter is connected to a "mirror port" on a switch to which all
82 packets passing through the switch are sent, a network adapter receives
83 all packets on the LAN, including unicast or multicast packets not sent
84 to a network address that the network adapter isn't configured to
85 recognize.
86 .IP
87 Normally, the adapter will discard those packets; however, many network
88 adapters support "promiscuous mode", which is a mode in which all
89 packets, even if they are not sent to an address that the adapter
90 recognizes, are provided to the host. This is useful for passively
91 capturing traffic between two or more other hosts for analysis.
92 .IP
93 Note that even if an application does not set promiscuous mode, the
94 adapter could well be in promiscuous mode for some other reason.
95 .IP
96 For now, this doesn't work on the "any" device; if an argument of "any"
97 or NULL is supplied, the setting of promiscuous mode is ignored.
98 .IP
99 Promiscuous mode is set with
100 .BR pcap_set_promisc() .
101 .IP "monitor mode"
102 On IEEE 802.11 wireless LANs, even if an adapter is in promiscuous mode,
103 it will supply to the host only frames for the network with which it's
104 associated. It might also supply only data frames, not management or
105 control frames, and might not provide the 802.11 header or radio
106 information pseudo-header for those frames.
107 .IP
108 In "monitor mode", sometimes also called "rfmon mode" (for "Radio
109 Frequency MONitor"), the adapter will supply all frames that it
110 receives, with 802.11 headers, and might supply a pseudo-header with
111 radio information about the frame as well.
112 .IP
113 Note that in monitor mode the adapter might disassociate from the
114 network with which it's associated, so that you will not be able to use
115 any wireless networks with that adapter. This could prevent accessing
116 files on a network server, or resolving host names or network addresses,
117 if you are capturing in monitor mode and are not connected to another
118 network with another adapter.
119 .IP
120 Monitor mode is set with
121 .BR pcap_set_rfmon() ,
122 and
123 .B pcap_can_set_rfmon()
124 can be used to determine whether an adapter can be put into monitor
125 mode.
126 .IP "read timeout"
127 If, when capturing, packets are delivered as soon as they arrive, the
128 application capturing the packets will be woken up for each packet as it
129 arrives, and might have to make one or more calls to the operating
130 system to fetch each packet.
131 .IP
132 If, instead, packets are not delivered as soon as they arrive, but are
133 delivered after a short delay (called a "read timeout"), more than one
134 packet can be accumulated before the packets are delivered, so that a
135 single wakeup would be done for multiple packets, and each set of calls
136 made to the operating system would supply multiple packets, rather than
137 a single packet. This reduces the per-packet CPU overhead if packets
138 are arriving at a high rate, increasing the number of packets per second
139 that can be captured.
140 .IP
141 The read timeout is required so that an application won't wait for the
142 operating system's capture buffer to fill up before packets are
143 delivered; if packets are arriving slowly, that wait could take an
144 arbitrarily long period of time.
145 .IP
146 Not all platforms support a read timeout; on platforms that
147 don't, the read timeout is ignored. A zero value for the timeout,
148 on platforms that support a read timeout,
149 will cause a read to wait forever to allow enough packets to
150 arrive, with no timeout.
151 .IP
152 .BR NOTE :
153 the read timeout cannot be used to cause calls that read
154 packets to return within a limited period of time, because, on some
155 platforms, the read timeout isn't supported, and, on other platforms,
156 the timer doesn't start until at least one packet arrives. This means
157 that the read timeout should
158 .B NOT
159 be used, for example, in an interactive application to allow the packet
160 capture loop to ``poll'' for user input periodically, as there's no
161 guarantee that a call reading packets will return after the timeout
162 expires even if no packets have arrived.
163 .IP
164 The read timeout is set with
165 .BR pcap_set_timeout() .
166 .IP "buffer size"
167 Packets that arrive for a capture are stored in a buffer, so that they
168 do not have to be read by the application as soon as they arrive. On
169 some platforms, the buffer's size can be set; a size that's too small
170 could mean that, if too many packets are being captured and the snapshot
171 length doesn't limit the amount of data that's buffered, packets could
172 be dropped if the buffer fills up before the application can read
173 packets from it, while a size that's too large could use more
174 non-pageable operating system memory than is necessary to prevent
175 packets from being dropped.
176 .IP
177 The buffer size is set with
178 .BR pcap_set_buffer_size() .
179 .PP
180 To open a ``savefile`` to which to write packets, call
181 .BR pcap_dump_open() .
182 It returns a pointer to a
183 .BR pcap_dumper_t ,
184 which is the handle used for writing packets to the ``savefile''.
185 .PP
186 Packets are read with
187 .B pcap_dispatch()
188 or
189 .BR pcap_loop() ,
190 which process one or more packets, calling a callback routine for each
191 packet, or with
192 .B pcap_next()
193 or
194 .BR pcap_next_ex() ,
195 which return the next packet.
196 The callback for
197 .B pcap_dispatch()
198 and
199 .BR pcap_loop()
200 is supplied a pointer to a
201 .IR "struct pcap_pkthdr" ,
202 which includes the following members:
203 .RS
204 .TP
205 .B ts
206 a
207 .I struct timeval
208 containing the time when the packet was captured
209 .TP
210 .B caplen
211 a
212 .I bpf_u_int32
213 giving the number of bytes of the packet that are available from the
214 capture
215 .TP
216 .B len
217 a
218 .I bpf_u_int32
219 giving the length of the packet, in bytes (which might be more than the
220 number of bytes available from the capture, if the length of the packet
221 is larger than the maximum number of bytes to capture).
222 .RE
223 .PP
224 .B pcap_next_ex()
225 supplies that pointer through a pointer argument.
226 .B pcap_next()
227 is passed an argument that points to a
228 .I struct pcap_pkthdr
229 structure, and fills it in.
230 .PP
231 The callback is also supplied a
232 .I const u_char
233 pointer to the first
234 .B caplen
235 (as given in the
236 .I struct pcap_pkthdr
237 a pointer to which is passed to the callback routine)
238 bytes of data from the packet. This won't necessarily be the entire
239 packet; to capture the entire packet, you will have to provide a value
240 for
241 .I snaplen
242 in your call to
243 .B pcap_open_live()
244 that is sufficiently large to get all of the packet's data - a value of
245 65535 should be sufficient on most if not all networks). When reading
246 from a ``savefile'', the snapshot length specified when the capture was
247 performed will limit the amount of packet data available.
248 .B pcap_next()
249 returns that pointer;
250 .B pcap_next_ex()
251 supplies that pointer through a pointer argument.
252 .SH BACKWARDS COMPATIBILITY
253 .PP
254 In versions of libpcap prior to 1.0, the
255 .B pcap.h
256 header file was not in a
257 .B pcap
258 directory on most platforms; if you are writing an application that must
259 work on versions of libpcap prior to 1.0, include
260 .BR <pcap.h> ,
261 which will include
262 .B <pcap/pcap.h>
263 for you, rather than including
264 .BR <pcap/pcap.h> .
265 .PP
266 .B pcap_create()
267 and
268 .B pcap_activate()
269 were not available in versions of libpcap prior to 1.0; if you are
270 writing an application that must work on versions of libpcap prior to
271 1.0, either use
272 .B pcap_open_live()
273 to get a handle for a live capture or, if you want to be able to use the
274 additional capabilities offered by using
275 .B pcap_create()
276 and
277 .BR pcap_activate() ,
278 use an
279 .BR autoconf (1)
280 script or some other configuration script to check whether the libpcap
281 1.0 APIs are available and use them only if they are.
282 .SH SEE ALSO
283 autoconf(1), tcpdump(1), tcpslice(1), pcap-filter(4)
284 .SH AUTHORS
285 The original authors of libpcap are:
286 .LP
287 Van Jacobson,
288 Craig Leres and
289 Steven McCanne, all of the
290 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
291 .LP
292 The current version is available from "The Tcpdump Group"'s Web site at
293 .LP
294 .RS
295 .I http://www.tcpdump.org/
296 .RE
297 .SH BUGS
298 Please send problems, bugs, questions, desirable enhancements, etc. to:
299 .LP
300 .RS
301 tcpdump-workers@tcpdump.org
302 .RE