1c66508371c071b7e327173846bdf6e9ad7bce5b
[dragonfly.git] / contrib / libpcap / pcap.3pcap.in
1 .\" @(#) $Header: /tcpdump/master/libpcap/pcap.3pcap.in,v 1.1.2.2 2008-10-21 07:44:57 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 Reading packets from a network interface may require that you have
181 special privileges:
182 .TP
183 .B Under SunOS 3.x or 4.x with NIT or BPF:
184 You must have read access to
185 .I /dev/nit
186 or
187 .IR /dev/bpf* .
188 .TP
189 .B Under Solaris with DLPI:
190 You must have read/write access to the network pseudo device, e.g.
191 .IR /dev/le .
192 On at least some versions of Solaris, however, this is not sufficient to
193 allow
194 .I tcpdump
195 to capture in promiscuous mode; on those versions of Solaris, you must
196 be root, or the application capturing packets
197 must be installed setuid to root, in order to capture in promiscuous
198 mode.  Note that, on many (perhaps all) interfaces, if you don't capture
199 in promiscuous mode, you will not see any outgoing packets, so a capture
200 not done in promiscuous mode may not be very useful.
201 .IP
202 In newer versions of Solaris, you must have been given the
203 .B net_rawaccess
204 privilege; this is both necessary and sufficient to give you access to the
205 network pseudo-device - there is no need to change the privileges on
206 that device.  A user can be given that privilege by, for example, adding
207 that privilege to the user's
208 .B defaultpriv
209 key with the
210 .B usermod (1M)
211 command.
212 .TP
213 .B Under HP-UX with DLPI:
214 You must be root or the application capturing packets must be installed
215 setuid to root.
216 .TP
217 .B Under IRIX with snoop:
218 You must be root or the application capturing packets must be installed
219 setuid to root.
220 .TP
221 .B Under Linux:
222 You must be root or the application capturing packets must be installed
223 setuid to root (unless your distribution has a kernel
224 that supports capability bits such as CAP_NET_RAW and code to allow
225 those capability bits to be given to particular accounts and to cause
226 those bits to be set on a user's initial processes when they log in, in
227 which case you  must have CAP_NET_RAW in order to capture and
228 CAP_NET_ADMIN to enumerate network devices with, for example, the
229 .B \-D
230 flag).
231 .TP
232 .B Under ULTRIX and Digital UNIX/Tru64 UNIX:
233 Any user may capture network traffic.
234 However, no user (not even the super-user) can capture in promiscuous
235 mode on an interface unless the super-user has enabled promiscuous-mode
236 operation on that interface using
237 .IR pfconfig (8),
238 and no user (not even the super-user) can capture unicast traffic
239 received by or sent by the machine on an interface unless the super-user
240 has enabled copy-all-mode operation on that interface using
241 .IR pfconfig ,
242 so
243 .I useful
244 packet capture on an interface probably requires that either
245 promiscuous-mode or copy-all-mode operation, or both modes of
246 operation, be enabled on that interface.
247 .TP
248 .B Under BSD (this includes Mac OS X):
249 You must have read access to
250 .I /dev/bpf*
251 on systems that don't have a cloning BPF device, or to
252 .I /dev/bpf
253 on systems that do.
254 On BSDs with a devfs (this includes Mac OS X), this might involve more
255 than just having somebody with super-user access setting the ownership
256 or permissions on the BPF devices - it might involve configuring devfs
257 to set the ownership or permissions every time the system is booted,
258 if the system even supports that; if it doesn't support that, you might
259 have to find some other way to make that happen at boot time.
260 .PP
261 Reading a saved packet file doesn't require special privileges.
262 .PP
263 To open a ``savefile`` to which to write packets, call
264 .BR pcap_dump_open() .
265 It returns a pointer to a
266 .BR pcap_dumper_t ,
267 which is the handle used for writing packets to the ``savefile''.
268 .PP
269 Packets are read with
270 .B pcap_dispatch()
271 or
272 .BR pcap_loop() ,
273 which process one or more packets, calling a callback routine for each
274 packet, or with
275 .B pcap_next()
276 or
277 .BR pcap_next_ex() ,
278 which return the next packet.
279 The callback for
280 .B pcap_dispatch()
281 and
282 .BR pcap_loop()
283 is supplied a pointer to a
284 .IR "struct pcap_pkthdr" ,
285 which includes the following members:
286 .RS
287 .TP
288 .B ts
289 a
290 .I struct timeval
291 containing the time when the packet was captured
292 .TP
293 .B caplen
294 a
295 .I bpf_u_int32
296 giving the number of bytes of the packet that are available from the
297 capture
298 .TP
299 .B len
300 a
301 .I bpf_u_int32
302 giving the length of the packet, in bytes (which might be more than the
303 number of bytes available from the capture, if the length of the packet
304 is larger than the maximum number of bytes to capture).
305 .RE
306 .PP
307 .B pcap_next_ex()
308 supplies that pointer through a pointer argument.
309 .B pcap_next()
310 is passed an argument that points to a
311 .I struct pcap_pkthdr
312 structure, and fills it in.
313 .PP
314 The callback is also supplied a
315 .I const u_char
316 pointer to the first
317 .B caplen
318 (as given in the
319 .I struct pcap_pkthdr
320 a pointer to which is passed to the callback routine)
321 bytes of data from the packet.  This won't necessarily be the entire
322 packet; to capture the entire packet, you will have to provide a value
323 for
324 .I snaplen
325 in your call to
326 .B pcap_open_live()
327 that is sufficiently large to get all of the packet's data - a value of
328 65535 should be sufficient on most if not all networks).  When reading
329 from a ``savefile'', the snapshot length specified when the capture was
330 performed will limit the amount of packet data available.
331 .B pcap_next()
332 returns that pointer;
333 .B pcap_next_ex()
334 supplies that pointer through a pointer argument.
335 .SH BACKWARDS COMPATIBILITY
336 .PP
337 In versions of libpcap prior to 1.0, the
338 .B pcap.h
339 header file was not in a
340 .B pcap
341 directory on most platforms; if you are writing an application that must
342 work on versions of libpcap prior to 1.0, include
343 .BR <pcap.h> ,
344 which will include
345 .B <pcap/pcap.h>
346 for you, rather than including
347 .BR <pcap/pcap.h> .
348 .PP
349 .B pcap_create()
350 and
351 .B pcap_activate()
352 were not available in versions of libpcap prior to 1.0; if you are
353 writing an application that must work on versions of libpcap prior to
354 1.0, either use
355 .B pcap_open_live()
356 to get a handle for a live capture or, if you want to be able to use the
357 additional capabilities offered by using
358 .B pcap_create()
359 and
360 .BR pcap_activate() ,
361 use an
362 .BR autoconf (1)
363 script or some other configuration script to check whether the libpcap
364 1.0 APIs are available and use them only if they are.
365 .SH SEE ALSO
366 autoconf(1), tcpdump(1), tcpslice(1), pcap-filter(@MAN_MISC_INFO@), pfconfig(8),
367 usermod(1M)
368 .SH AUTHORS
369 The original authors of libpcap are:
370 .LP
371 Van Jacobson,
372 Craig Leres and
373 Steven McCanne, all of the
374 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
375 .LP
376 The current version is available from "The Tcpdump Group"'s Web site at
377 .LP
378 .RS
379 .I http://www.tcpdump.org/
380 .RE
381 .SH BUGS
382 Please send problems, bugs, questions, desirable enhancements, etc. to:
383 .LP
384 .RS
385 tcpdump-workers@lists.tcpdump.org
386 .RE