]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libpcap/pcap.3pcap
MFV r324198: 8081 Compiler warnings in zdb
[FreeBSD/FreeBSD.git] / contrib / libpcap / pcap.3pcap
1 .\" Copyright (c) 1994, 1996, 1997
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
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.
19 .\"
20 .TH PCAP 3PCAP "16 April 2014"
21 .SH NAME
22 pcap \- Packet Capture library
23 .SH SYNOPSIS
24 .nf
25 .ft B
26 #include <pcap/pcap.h>
27 .LP
28 .ft B
29 .ft
30 .fi
31 .SH DESCRIPTION
32 The Packet Capture library
33 provides a high level interface to packet capture systems. All packets
34 on the network, even those destined for other hosts, are accessible
35 through this mechanism.
36 It also supports saving captured packets to a ``savefile'', and reading
37 packets from a ``savefile''.
38 .SS Opening a capture handle for reading
39 To open a handle for a live capture, given the name of the network or
40 other interface on which the capture should be done, call
41 .BR pcap_create (),
42 set the appropriate options on the handle, and then activate it with
43 .BR pcap_activate ().
44 .PP
45 To obtain a list of devices that can be opened for a live capture, call
46 .BR pcap_findalldevs ();
47 to free the list returned by
48 .BR pcap_findalldevs (),
49 call
50 .BR pcap_freealldevs ().
51 .BR pcap_lookupdev ()
52 will return the first device on that list that is not a ``loopback``
53 network interface.
54 .PP
55 To open a handle for a ``savefile'' from which to read packets, given the
56 pathname of the ``savefile'', call
57 .BR pcap_open_offline ();
58 to set up a handle for a ``savefile'', given a
59 .B "FILE\ *"
60 referring to a file already opened for reading, call
61 .BR pcap_fopen_offline ().
62 .PP
63 In order to get a ``fake''
64 .B pcap_t
65 for use in routines that require a
66 .B pcap_t
67 as an argument, such as routines to open a ``savefile'' for writing and
68 to compile a filter expression, call
69 .BR pcap_open_dead ().
70 .PP
71 .BR pcap_create (),
72 .BR pcap_open_offline (),
73 .BR pcap_fopen_offline (),
74 and
75 .BR pcap_open_dead ()
76 return a pointer to a
77 .BR pcap_t ,
78 which is the handle used for reading packets from the capture stream or
79 the ``savefile'', and for finding out information about the capture
80 stream or ``savefile''.
81 To close a handle, use
82 .BR pcap_close ().
83 .PP
84 The options that can be set on a capture handle include
85 .IP "snapshot length"
86 If, when capturing, you capture the entire contents of the packet, that
87 requires more CPU time to copy the packet to your application, more disk
88 and possibly network bandwidth to write the packet data to a file, and
89 more disk space to save the packet.  If you don't need the entire
90 contents of the packet - for example, if you are only interested in the
91 TCP headers of packets - you can set the "snapshot length" for the
92 capture to an appropriate value.  If the snapshot length is set to
93 .IR snaplen ,
94 and
95 .I snaplen
96 is less
97 than the size of a packet that is captured, only the first
98 .I snaplen
99 bytes of that packet will be captured and provided as packet data.
100 .IP
101 A snapshot length of 65535 should be sufficient, on most if not all
102 networks, to capture all the data available from the packet.
103 .IP
104 The snapshot length is set with
105 .BR pcap_set_snaplen ().
106 .IP "promiscuous mode"
107 On broadcast LANs such as Ethernet, if the network isn't switched, or if
108 the adapter is connected to a "mirror port" on a switch to which all
109 packets passing through the switch are sent, a network adapter receives
110 all packets on the LAN, including unicast or multicast packets not sent
111 to a network address that the network adapter isn't configured to
112 recognize.
113 .IP
114 Normally, the adapter will discard those packets; however, many network
115 adapters support "promiscuous mode", which is a mode in which all
116 packets, even if they are not sent to an address that the adapter
117 recognizes, are provided to the host.  This is useful for passively
118 capturing traffic between two or more other hosts for analysis.
119 .IP
120 Note that even if an application does not set promiscuous mode, the
121 adapter could well be in promiscuous mode for some other reason.
122 .IP
123 For now, this doesn't work on the "any" device; if an argument of "any"
124 or NULL is supplied, the setting of promiscuous mode is ignored.
125 .IP
126 Promiscuous mode is set with
127 .BR pcap_set_promisc ().
128 .IP "monitor mode"
129 On IEEE 802.11 wireless LANs, even if an adapter is in promiscuous mode,
130 it will supply to the host only frames for the network with which it's
131 associated.  It might also supply only data frames, not management or
132 control frames, and might not provide the 802.11 header or radio
133 information pseudo-header for those frames.
134 .IP
135 In "monitor mode", sometimes also called "rfmon mode" (for "Radio
136 Frequency MONitor"), the adapter will supply all frames that it
137 receives, with 802.11 headers, and might supply a pseudo-header with
138 radio information about the frame as well.
139 .IP
140 Note that in monitor mode the adapter might disassociate from the
141 network with which it's associated, so that you will not be able to use
142 any wireless networks with that adapter.  This could prevent accessing
143 files on a network server, or resolving host names or network addresses,
144 if you are capturing in monitor mode and are not connected to another
145 network with another adapter.
146 .IP
147 Monitor mode is set with
148 .BR pcap_set_rfmon (),
149 and
150 .BR pcap_can_set_rfmon ()
151 can be used to determine whether an adapter can be put into monitor
152 mode.
153 .IP "read timeout"
154 If, when capturing, packets are delivered as soon as they arrive, the
155 application capturing the packets will be woken up for each packet as it
156 arrives, and might have to make one or more calls to the operating
157 system to fetch each packet.
158 .IP
159 If, instead, packets are not delivered as soon as they arrive, but are
160 delivered after a short delay (called a "read timeout"), more than one
161 packet can be accumulated before the packets are delivered, so that a
162 single wakeup would be done for multiple packets, and each set of calls
163 made to the operating system would supply multiple packets, rather than
164 a single packet.  This reduces the per-packet CPU overhead if packets
165 are arriving at a high rate, increasing the number of packets per second
166 that can be captured.
167 .IP
168 The read timeout is required so that an application won't wait for the
169 operating system's capture buffer to fill up before packets are
170 delivered; if packets are arriving slowly, that wait could take an
171 arbitrarily long period of time.
172 .IP
173 Not all platforms support a read timeout; on platforms that
174 don't, the read timeout is ignored.  A zero value for the timeout,
175 on platforms that support a read timeout,
176 will cause a read to wait forever to allow enough packets to
177 arrive, with no timeout.
178 .IP
179 .BR NOTE :
180 the read timeout cannot be used to cause calls that read
181 packets to return within a limited period of time, because, on some
182 platforms, the read timeout isn't supported, and, on other platforms,
183 the timer doesn't start until at least one packet arrives.  This means
184 that the read timeout should
185 .B NOT
186 be used, for example, in an interactive application to allow the packet
187 capture loop to ``poll'' for user input periodically, as there's no
188 guarantee that a call reading packets will return after the timeout
189 expires even if no packets have arrived.
190 .IP
191 The read timeout is set with
192 .BR pcap_set_timeout ().
193 .IP "buffer size"
194 Packets that arrive for a capture are stored in a buffer, so that they
195 do not have to be read by the application as soon as they arrive.  On
196 some platforms, the buffer's size can be set; a size that's too small
197 could mean that, if too many packets are being captured and the snapshot
198 length doesn't limit the amount of data that's buffered, packets could
199 be dropped if the buffer fills up before the application can read
200 packets from it, while a size that's too large could use more
201 non-pageable operating system memory than is necessary to prevent
202 packets from being dropped.
203 .IP
204 The buffer size is set with
205 .BR pcap_set_buffer_size ().
206 .IP "timestamp type"
207 On some platforms, the time stamp given to packets on live captures can
208 come from different sources that can have different resolutions or that
209 can have different relationships to the time values for the current time
210 supplied by routines on the native operating system.  See
211 .BR pcap-tstamp (7)
212 for a list of time stamp types.
213 .IP
214 The time stamp type is set with
215 .BR pcap_set_tstamp_type ().
216 .PP
217 Reading packets from a network interface may require that you have
218 special privileges:
219 .TP
220 .B Under SunOS 3.x or 4.x with NIT or BPF:
221 You must have read access to
222 .I /dev/nit
223 or
224 .IR /dev/bpf* .
225 .TP
226 .B Under Solaris with DLPI:
227 You must have read/write access to the network pseudo device, e.g.
228 .IR /dev/le .
229 On at least some versions of Solaris, however, this is not sufficient to
230 allow
231 .I tcpdump
232 to capture in promiscuous mode; on those versions of Solaris, you must
233 be root, or the application capturing packets
234 must be installed setuid to root, in order to capture in promiscuous
235 mode.  Note that, on many (perhaps all) interfaces, if you don't capture
236 in promiscuous mode, you will not see any outgoing packets, so a capture
237 not done in promiscuous mode may not be very useful.
238 .IP
239 In newer versions of Solaris, you must have been given the
240 .B net_rawaccess
241 privilege; this is both necessary and sufficient to give you access to the
242 network pseudo-device - there is no need to change the privileges on
243 that device.  A user can be given that privilege by, for example, adding
244 that privilege to the user's
245 .B defaultpriv
246 key with the
247 .B usermod (1M)
248 command.
249 .TP
250 .B Under HP-UX with DLPI:
251 You must be root or the application capturing packets must be installed
252 setuid to root.
253 .TP
254 .B Under IRIX with snoop:
255 You must be root or the application capturing packets must be installed
256 setuid to root.
257 .TP
258 .B Under Linux:
259 You must be root or the application capturing packets must be installed
260 setuid to root (unless your distribution has a kernel
261 that supports capability bits such as CAP_NET_RAW and code to allow
262 those capability bits to be given to particular accounts and to cause
263 those bits to be set on a user's initial processes when they log in, in
264 which case you  must have CAP_NET_RAW in order to capture and
265 CAP_NET_ADMIN to enumerate network devices with, for example, the
266 .B \-D
267 flag).
268 .TP
269 .B Under ULTRIX and Digital UNIX/Tru64 UNIX:
270 Any user may capture network traffic.
271 However, no user (not even the super-user) can capture in promiscuous
272 mode on an interface unless the super-user has enabled promiscuous-mode
273 operation on that interface using
274 .IR pfconfig (8),
275 and no user (not even the super-user) can capture unicast traffic
276 received by or sent by the machine on an interface unless the super-user
277 has enabled copy-all-mode operation on that interface using
278 .IR pfconfig ,
279 so
280 .I useful
281 packet capture on an interface probably requires that either
282 promiscuous-mode or copy-all-mode operation, or both modes of
283 operation, be enabled on that interface.
284 .TP
285 .B Under BSD (this includes Mac OS X):
286 You must have read access to
287 .I /dev/bpf*
288 on systems that don't have a cloning BPF device, or to
289 .I /dev/bpf
290 on systems that do.
291 On BSDs with a devfs (this includes Mac OS X), this might involve more
292 than just having somebody with super-user access setting the ownership
293 or permissions on the BPF devices - it might involve configuring devfs
294 to set the ownership or permissions every time the system is booted,
295 if the system even supports that; if it doesn't support that, you might
296 have to find some other way to make that happen at boot time.
297 .PP
298 Reading a saved packet file doesn't require special privileges.
299 .PP
300 The packets read from the handle may include a ``pseudo-header''
301 containing various forms of packet meta-data, and probably includes a
302 link-layer header whose contents can differ for different network
303 interfaces.  To determine the format of the packets supplied by the
304 handle, call
305 .BR pcap_datalink ();
306 .I http://www.tcpdump.org/linktypes.html
307 lists the values it returns and describes the packet formats that
308 correspond to those values.
309 .PP
310 Do
311 .B NOT
312 assume that the packets for a given capture or ``savefile`` will have
313 any given link-layer header type, such as
314 .B DLT_EN10MB
315 for Ethernet.  For example, the "any" device on Linux will have a
316 link-layer header type of
317 .B DLT_LINUX_SLL
318 even if all devices on the system at the time the "any" device is opened
319 have some other data link type, such as
320 .B DLT_EN10MB
321 for Ethernet.
322 .PP
323 To obtain the
324 .B "FILE\ *"
325 corresponding to a
326 .B pcap_t
327 opened for a ``savefile'', call
328 .BR pcap_file ().
329 .TP
330 .B Routines
331 .RS
332 .TP
333 .BR pcap_create (3PCAP)
334 get a
335 .B pcap_t
336 for live capture
337 .TP
338 .BR pcap_activate (3PCAP)
339 activate a
340 .B pcap_t
341 for live capture
342 .TP
343 .BR pcap_findalldevs (3PCAP)
344 get a list of devices that can be opened for a live capture
345 .TP
346 .BR pcap_freealldevs (3PCAP)
347 free list of devices
348 .TP
349 .BR pcap_lookupdev (3PCAP)
350 get first non-loopback device on that list
351 .TP
352 .BR pcap_open_offline (3PCAP)
353 open a
354 .B pcap_t
355 for a ``savefile'', given a pathname
356 .TP
357 .BR pcap_fopen_offline (3PCAP)
358 open a
359 .B pcap_t
360 for a ``savefile'', given a
361 .B "FILE\ *"
362 .TP
363 .BR pcap_open_dead (3PCAP)
364 create a ``fake''
365 .B pcap_t
366 .TP
367 .BR pcap_close (3PCAP)
368 close a
369 .B pcap_t
370 .TP
371 .BR pcap_set_snaplen (3PCAP)
372 set the snapshot length for a not-yet-activated
373 .B pcap_t
374 for live capture
375 .TP
376 .BR pcap_snapshot (3PCAP)
377 get the snapshot length for a
378 .B pcap_t
379 .TP
380 .BR pcap_set_promisc (3PCAP)
381 set promiscuous mode for a not-yet-activated
382 .B pcap_t
383 for live capture
384 .TP
385 .BR pcap_set_rfmon (3PCAP)
386 set monitor mode for a not-yet-activated
387 .B pcap_t
388 for live capture
389 .TP
390 .BR pcap_can_set_rfmon (3PCAP)
391 determine whether monitor mode can be set for a
392 .B pcap_t
393 for live capture
394 .TP
395 .BR pcap_set_timeout (3PCAP)
396 set read timeout for a not-yet-activated
397 .B pcap_t
398 for live capture
399 .TP
400 .BR pcap_set_buffer_size (3PCAP)
401 set buffer size for a not-yet-activated
402 .B pcap_t
403 for live capture
404 .TP
405 .BR pcap_set_tstamp_type (3PCAP)
406 set time stamp type for a not-yet-activated
407 .B pcap_t
408 for live capture
409 .TP
410 .BR pcap_list_tstamp_types (3PCAP)
411 get list of available time stamp types for a not-yet-activated
412 .B pcap_t
413 for live capture
414 .TP
415 .BR pcap_free_tstamp_types (3PCAP)
416 free list of available time stamp types
417 .TP
418 .BR pcap_tstamp_type_val_to_name (3PCAP)
419 get name for a time stamp type
420 .TP
421 .BR pcap_tstamp_type_val_to_description (3PCAP)
422 get description for a time stamp type
423 .TP
424 .BR pcap_tstamp_type_name_to_val (3PCAP)
425 get time stamp type corresponding to a name
426 .TP
427 .BR pcap_datalink (3PCAP)
428 get link-layer header type for a
429 .B pcap_t
430 .TP
431 .BR pcap_file (3PCAP)
432 get the
433 .B "FILE\ *"
434 for a
435 .B pcap_t
436 opened for a ``savefile''
437 .TP
438 .BR pcap_is_swapped (3PCAP)
439 determine whether a ``savefile'' being read came from a machine with the
440 opposite byte order
441 .TP
442 .BR pcap_major_version (3PCAP)
443 .PD 0
444 .TP
445 .BR pcap_minor_version (3PCAP)
446 get the major and minor version of the file format version for a
447 ``savefile''
448 .PD
449 .RE
450 .SS Selecting a link-layer header type for a live capture
451 Some devices may provide more than one link-layer header type.  To
452 obtain a list of all link-layer header types provided by a device, call
453 .BR pcap_list_datalinks ()
454 on an activated
455 .B pcap_t
456 for the device.
457 To free a list of link-layer header types, call
458 .BR pcap_free_datalinks ().
459 To set the link-layer header type for a device, call
460 .BR pcap_set_datalink ().
461 This should be done after the device has been activated but before any
462 packets are read and before any filters are compiled or installed.
463 .TP
464 .B Routines
465 .RS
466 .TP
467 .BR pcap_list_datalinks (3PCAP)
468 get a list of link-layer header types for a device
469 .TP
470 .BR pcap_free_datalinks (3PCAP)
471 free list of link-layer header types
472 .TP
473 .BR pcap_set_datalink (3PCAP)
474 set link-layer header type for a device
475 .TP
476 .BR pcap_datalink_val_to_name (3PCAP)
477 get name for a link-layer header type
478 .TP
479 .BR pcap_datalink_val_to_description (3PCAP)
480 get description for a link-layer header type
481 .TP
482 .BR pcap_datalink_name_to_val (3PCAP)
483 get link-layer header type corresponding to a name
484 .RE
485 .SS Reading packets
486 Packets are read with
487 .BR pcap_dispatch ()
488 or
489 .BR pcap_loop (),
490 which process one or more packets, calling a callback routine for each
491 packet, or with
492 .BR pcap_next ()
493 or
494 .BR pcap_next_ex (),
495 which return the next packet.
496 The callback for
497 .BR pcap_dispatch ()
498 and
499 .BR pcap_loop ()
500 is supplied a pointer to a
501 .IR "struct pcap_pkthdr" ,
502 which includes the following members:
503 .RS
504 .TP
505 .B ts
506 a
507 .I struct timeval
508 containing the time when the packet was captured
509 .TP
510 .B caplen
511 a
512 .I bpf_u_int32
513 giving the number of bytes of the packet that are available from the
514 capture
515 .TP
516 .B len
517 a
518 .I bpf_u_int32
519 giving the length of the packet, in bytes (which might be more than the
520 number of bytes available from the capture, if the length of the packet
521 is larger than the maximum number of bytes to capture).
522 .RE
523 .PP
524 The callback is also supplied a
525 .I const u_char
526 pointer to the first
527 .B caplen
528 (as given in the
529 .I struct pcap_pkthdr
530 mentioned above)
531 bytes of data from the packet.  This won't necessarily be the entire
532 packet; to capture the entire packet, you will have to provide a value
533 for
534 .I snaplen
535 in your call to
536 .BR pcap_set_snaplen ()
537 that is sufficiently large to get all of the packet's data - a value of
538 65535 should be sufficient on most if not all networks).  When reading
539 from a ``savefile'', the snapshot length specified when the capture was
540 performed will limit the amount of packet data available.
541 .PP
542 .BR pcap_next ()
543 is passed an argument that points to a
544 .I struct pcap_pkthdr
545 structure, and fills it in with the time stamp and length values for the
546 packet.  It returns a
547 .I const u_char
548 to the first
549 .B caplen
550 bytes of the packet on success, and NULL on error.
551 .PP
552 .BR pcap_next_ex ()
553 is passed two pointer arguments, one of which points to a
554 .IR struct pcap_pkthdr *
555 and one of which points to a
556 .IR "const u_char" *.
557 It sets the first pointer to point to a
558 .I struct pcap_pkthdr
559 structure with the time stamp and length values for the packet, and sets
560 the second pointer to point to the first
561 .B caplen
562 bytes of the packet.
563 .PP
564 To force the loop in
565 .BR pcap_dispatch ()
566 or
567 .BR pcap_loop ()
568 to terminate, call
569 .BR pcap_breakloop ().
570 .PP
571 By default, when reading packets from an interface opened for a live
572 capture,
573 .BR pcap_dispatch (),
574 .BR pcap_next (),
575 and
576 .BR pcap_next_ex ()
577 will, if no packets are currently available to be read, block waiting
578 for packets to become available.  On some, but
579 .I not
580 all, platforms, if a read timeout was specified, the wait will terminate
581 after the read timeout expires; applications should be prepared for
582 this, as it happens on some platforms, but should not rely on it, as it
583 does not happen on other platforms.
584 .PP
585 A handle can be put into ``non-blocking mode'', so that those routines
586 will, rather than blocking, return an indication that no packets are
587 available to read.  Call
588 .BR pcap_setnonblock ()
589 to put a handle into non-blocking mode or to take it out of non-blocking
590 mode; call
591 .BR pcap_getnonblock ()
592 to determine whether a handle is in non-blocking mode.  Note that
593 non-blocking mode does not work correctly in Mac OS X 10.6.
594 .PP
595 Non-blocking mode is often combined with routines such as
596 .BR select (2)
597 or
598 .BR poll (2)
599 or other routines a platform offers to wait for the availability of data
600 on any of a set of descriptors.  To obtain, for a handle, a descriptor
601 that can be used in those routines, call
602 .BR pcap_get_selectable_fd ().
603 Not all handles have such a descriptor available;
604 .BR pcap_get_selectable_fd ()
605 will return \-1 if no such descriptor exists.  In addition, for various
606 reasons, one or more of those routines will not work properly with the
607 descriptor; the documentation for
608 .BR pcap_get_selectable_fd ()
609 gives details.
610 .TP
611 .B Routines
612 .RS
613 .TP
614 .BR pcap_dispatch (3PCAP)
615 read a bufferful of packets from a
616 .B pcap_t
617 open for a live capture or the full set of packets from a
618 .B pcap_t
619 open for a ``savefile''
620 .TP
621 .BR pcap_loop (3PCAP)
622 read packets from a
623 .B pcap_t
624 until an interrupt or error occurs
625 .TP
626 .BR pcap_next (3PCAP)
627 read the next packet from a
628 .B pcap_t
629 without an indication whether an error occurred
630 .TP
631 .BR pcap_next_ex (3PCAP)
632 read the next packet from a
633 .B pcap_t
634 with an error indication on an error
635 .TP
636 .BR pcap_breakloop (3PCAP)
637 prematurely terminate the loop in
638 .BR pcap_dispatch ()
639 or
640 .BR pcap_loop ()
641 .TP
642 .BR pcap_setnonblock (3PCAP)
643 set or clear non-blocking mode on a
644 .B pcap_t
645 .TP
646 .BR pcap_getnonblock (3PCAP)
647 get the state of non-blocking mode for a
648 .B pcap_t
649 .TP
650 .BR pcap_get_selectable_fd (3PCAP)
651 attempt to get a descriptor for a
652 .B pcap_t
653 that can be used in calls such as
654 .BR select (2)
655 and
656 .BR poll (2)
657 .RE
658 .SS Filters
659 In order to cause only certain packets to be returned when reading
660 packets, a filter can be set on a handle.  For a live capture, the
661 filtering will be performed in kernel mode, if possible, to avoid
662 copying ``uninteresting'' packets from the kernel to user mode.
663 .PP
664 A filter can be specified as a text string; the syntax and semantics of
665 the string are as described by
666 .BR pcap-filter (7).
667 A filter string is compiled into a program in a pseudo-machine-language
668 by
669 .BR pcap_compile ()
670 and the resulting program can be made a filter for a handle with
671 .BR pcap_setfilter ().
672 The result of
673 .BR pcap_compile ()
674 can be freed with a call to
675 .BR pcap_freecode ().
676 .BR pcap_compile ()
677 may require a network mask for certain expressions in the filter string;
678 .BR pcap_lookupnet ()
679 can be used to find the network address and network mask for a given
680 capture device.
681 .PP
682 A compiled filter can also be applied directly to a packet that has been
683 read using
684 .BR pcap_offline_filter ().
685 .TP
686 .B Routines
687 .RS
688 .TP
689 .BR pcap_compile (3PCAP)
690 compile filter expression to a pseudo-machine-language code program
691 .TP
692 .BR pcap_freecode (3PCAP)
693 free a filter program
694 .TP
695 .BR pcap_setfilter (3PCAP)
696 set filter for a
697 .B pcap_t
698 .TP
699 .BR pcap_lookupnet (3PCAP)
700 get network address and network mask for a capture device
701 .TP
702 .BR pcap_offline_filter (3PCAP)
703 apply a filter program to a packet
704 .RE
705 .SS Incoming and outgoing packets
706 By default, libpcap will attempt to capture both packets sent by the
707 machine and packets received by the machine.  To limit it to capturing
708 only packets received by the machine or, if possible, only packets sent
709 by the machine, call
710 .BR pcap_setdirection ().
711 .TP
712 .BR Routines
713 .RS
714 .TP
715 .BR pcap_setdirection (3PCAP)
716 specify whether to capture incoming packets, outgoing packets, or both
717 .RE
718 .SS Capture statistics
719 To get statistics about packets received and dropped in a live capture,
720 call
721 .BR pcap_stats ().
722 .TP
723 .B Routines
724 .RS
725 .TP
726 .BR pcap_stats (3PCAP)
727 get capture statistics
728 .RE
729 .SS Opening a handle for writing captured packets
730 To open a ``savefile`` to which to write packets, given the pathname the
731 ``savefile'' should have, call
732 .BR pcap_dump_open ().
733 To open a ``savefile`` to which to write packets, given the pathname the
734 ``savefile'' should have, call
735 .BR pcap_dump_open ();
736 to set up a handle for a ``savefile'', given a
737 .B "FILE\ *"
738 referring to a file already opened for writing, call
739 .BR pcap_dump_fopen ().
740 They each return pointers to a
741 .BR pcap_dumper_t ,
742 which is the handle used for writing packets to the ``savefile''.  If it
743 succeeds, it will have created the file if it doesn't exist and
744 truncated the file if it does exist.
745 To close a
746 .BR pcap_dumper_t ,
747 call
748 .BR pcap_dump_close ().
749 .TP
750 .B Routines
751 .RS
752 .TP
753 .BR pcap_dump_open (3PCAP)
754 open a
755 .B pcap_dumper_t
756 for a ``savefile``, given a pathname
757 .TP
758 .BR pcap_dump_fopen (3PCAP)
759 open a
760 .B pcap_dumper_t
761 for a ``savefile``, given a
762 .B "FILE\ *"
763 .TP
764 .BR pcap_dump_close (3PCAP)
765 close a
766 .B pcap_dumper_t
767 .TP
768 .BR pcap_dump_file (3PCAP)
769 get the
770 .B "FILE\ *"
771 for a
772 .B pcap_dumper_t
773 opened for a ``savefile''
774 .RE
775 .SS Writing packets
776 To write a packet to a
777 .BR pcap_dumper_t ,
778 call
779 .BR pcap_dump ().
780 Packets written with
781 .BR pcap_dump ()
782 may be buffered, rather than being immediately written to the
783 ``savefile''.  Closing the
784 .B pcap_dumper_t
785 will cause all buffered-but-not-yet-written packets to be written to the
786 ``savefile''.
787 To force all packets written to the
788 .BR pcap_dumper_t ,
789 and not yet written to the ``savefile'' because they're buffered by the
790 .BR pcap_dumper_t ,
791 to be written to the ``savefile'', without closing the
792 .BR pcap_dumper_t ,
793 call
794 .BR pcap_dump_flush ().
795 .TP
796 .B Routines
797 .RS
798 .TP
799 .BR pcap_dump (3PCAP)
800 write packet to a
801 .B pcap_dumper_t
802 .TP
803 .BR pcap_dump_flush (3PCAP)
804 flush buffered packets written to a
805 .B pcap_dumper_t
806 to the ``savefile''
807 .TP
808 .BR pcap_dump_ftell (3PCAP)
809 get current file position for a
810 .B pcap_dumper_t
811 .RE
812 .SS Injecting packets
813 If you have the required privileges, you can inject packets onto a
814 network with a
815 .B pcap_t
816 for a live capture, using
817 .BR pcap_inject ()
818 or
819 .BR pcap_sendpacket ().
820 (The two routines exist for compatibility with both OpenBSD and WinPcap;
821 they perform the same function, but have different return values.)
822 .TP
823 .B Routines
824 .RS
825 .TP
826 .BR pcap_inject (3PCAP)
827 .PD 0
828 .TP
829 .BR pcap_sendpacket (3PCAP)
830 transmit a packet
831 .PD
832 .RE
833 .SS Reporting errors
834 Some routines return error or warning status codes; to convert them to a
835 string, use
836 .BR pcap_statustostr ().
837 .TP
838 .B Routines
839 .RS
840 .TP
841 .BR pcap_statustostr (3PCAP)
842 get a string for an error or warning status code
843 .RE
844 .SS Getting library version information
845 To get a string giving version information about libpcap, call
846 .BR pcap_lib_version ().
847 .TP
848 .B Routines
849 .RS
850 .TP
851 .BR pcap_lib_version (3PCAP)
852 get library version string
853 .RE
854 .SH BACKWARDS COMPATIBILITY
855 .PP
856 In versions of libpcap prior to 1.0, the
857 .B pcap.h
858 header file was not in a
859 .B pcap
860 directory on most platforms; if you are writing an application that must
861 work on versions of libpcap prior to 1.0, include
862 .BR <pcap.h> ,
863 which will include
864 .B <pcap/pcap.h>
865 for you, rather than including
866 .BR <pcap/pcap.h> .
867 .PP
868 .BR pcap_create ()
869 and
870 .BR pcap_activate ()
871 were not available in versions of libpcap prior to 1.0; if you are
872 writing an application that must work on versions of libpcap prior to
873 1.0, either use
874 .BR pcap_open_live ()
875 to get a handle for a live capture or, if you want to be able to use the
876 additional capabilities offered by using
877 .BR pcap_create ()
878 and
879 .BR pcap_activate (),
880 use an
881 .BR autoconf (1)
882 script or some other configuration script to check whether the libpcap
883 1.0 APIs are available and use them only if they are.
884 .SH SEE ALSO
885 autoconf(1), tcpdump(1), tcpslice(1), pcap-filter(7), pfconfig(8),
886 usermod(1M)
887 .SH AUTHORS
888 The original authors of libpcap are:
889 .LP
890 Van Jacobson,
891 Craig Leres and
892 Steven McCanne, all of the
893 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
894 .LP
895 The current version is available from "The Tcpdump Group"'s Web site at
896 .LP
897 .RS
898 .I http://www.tcpdump.org/
899 .RE
900 .SH BUGS
901 Please send problems, bugs, questions, desirable enhancements, etc. to:
902 .LP
903 .RS
904 tcpdump-workers@lists.tcpdump.org
905 .RE