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 3PCAP "20 January 2017"
22 pcap \- Packet Capture library
26 #include <pcap/pcap.h>
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
42 set the appropriate options on the handle, and then activate it with
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 (),
50 .BR pcap_freealldevs ().
52 will return the first device on that list that is not a ``loopback``
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
60 referring to a file already opened for reading, call
61 .BR pcap_fopen_offline ().
63 In order to get a ``fake''
65 for use in routines that require a
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 ().
72 .BR pcap_open_offline (),
73 .BR pcap_fopen_offline (),
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
84 The options that can be set on a capture handle include
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
97 than the size of a packet that is captured, only the first
99 bytes of that packet will be captured and provided as packet data.
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.
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
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.
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.
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.
126 Promiscuous mode is set with
127 .BR pcap_set_promisc ().
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.
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.
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.
147 Monitor mode is set with
148 .BR pcap_set_rfmon (),
150 .BR pcap_can_set_rfmon ()
151 can be used to determine whether an adapter can be put into monitor
153 .IP "packet buffer 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.
159 If, instead, packets are not delivered as soon as they arrive, but are
160 delivered after a short delay (called a "packet buffer timeout"), more
161 than one packet can be accumulated before the packets are delivered, so
162 that a single wakeup would be done for multiple packets, and each set of
163 calls made to the operating system would supply multiple packets, rather
164 than a single packet. This reduces the per-packet CPU overhead if
165 packets are arriving at a high rate, increasing the number of packets
166 per second that can be captured.
168 The packet buffer timeout is required so that an application won't wait
169 for the 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.
173 Not all platforms support a packet buffer timeout; on platforms that
174 don't, the packet buffer timeout is ignored. A zero value for the
175 timeout, on platforms that support a packet buffer timeout, will cause a
176 read to wait forever to allow enough packets to arrive, with no timeout.
177 A negative value is invalid; the result of setting the timeout to a
178 negative value is unpredictable.
181 the packet buffer timeout cannot be used to cause calls that read
182 packets to return within a limited period of time, because, on some
183 platforms, the packet buffer timeout isn't supported, and, on other
184 platforms, the timer doesn't start until at least one packet arrives.
185 This means that the packet buffer timeout should
187 be used, for example, in an interactive application to allow the packet
188 capture loop to ``poll'' for user input periodically, as there's no
189 guarantee that a call reading packets will return after the timeout
190 expires even if no packets have arrived.
192 The packet buffer timeout is set with
193 .BR pcap_set_timeout ().
195 Packets that arrive for a capture are stored in a buffer, so that they
196 do not have to be read by the application as soon as they arrive. On
197 some platforms, the buffer's size can be set; a size that's too small
198 could mean that, if too many packets are being captured and the snapshot
199 length doesn't limit the amount of data that's buffered, packets could
200 be dropped if the buffer fills up before the application can read
201 packets from it, while a size that's too large could use more
202 non-pageable operating system memory than is necessary to prevent
203 packets from being dropped.
205 The buffer size is set with
206 .BR pcap_set_buffer_size ().
208 On some platforms, the time stamp given to packets on live captures can
209 come from different sources that can have different resolutions or that
210 can have different relationships to the time values for the current time
211 supplied by routines on the native operating system. See
212 .BR pcap-tstamp (@MAN_MISC_INFO@)
213 for a list of time stamp types.
215 The time stamp type is set with
216 .BR pcap_set_tstamp_type ().
218 Reading packets from a network interface may require that you have
221 .B Under SunOS 3.x or 4.x with NIT or BPF:
222 You must have read access to
227 .B Under Solaris with DLPI:
228 You must have read/write access to the network pseudo device, e.g.
230 On at least some versions of Solaris, however, this is not sufficient to
233 to capture in promiscuous mode; on those versions of Solaris, you must
234 be root, or the application capturing packets
235 must be installed setuid to root, in order to capture in promiscuous
236 mode. Note that, on many (perhaps all) interfaces, if you don't capture
237 in promiscuous mode, you will not see any outgoing packets, so a capture
238 not done in promiscuous mode may not be very useful.
240 In newer versions of Solaris, you must have been given the
242 privilege; this is both necessary and sufficient to give you access to the
243 network pseudo-device - there is no need to change the privileges on
244 that device. A user can be given that privilege by, for example, adding
245 that privilege to the user's
248 .B usermod (@MAN_ADMIN_COMMANDS@)
251 .B Under HP-UX with DLPI:
252 You must be root or the application capturing packets must be installed
255 .B Under IRIX with snoop:
256 You must be root or the application capturing packets must be installed
260 You must be root or the application capturing packets must be installed
261 setuid to root (unless your distribution has a kernel
262 that supports capability bits such as CAP_NET_RAW and code to allow
263 those capability bits to be given to particular accounts and to cause
264 those bits to be set on a user's initial processes when they log in, in
265 which case you must have CAP_NET_RAW in order to capture and
266 CAP_NET_ADMIN to enumerate network devices with, for example, the
270 .B Under ULTRIX and Digital UNIX/Tru64 UNIX:
271 Any user may capture network traffic.
272 However, no user (not even the super-user) can capture in promiscuous
273 mode on an interface unless the super-user has enabled promiscuous-mode
274 operation on that interface using
276 and no user (not even the super-user) can capture unicast traffic
277 received by or sent by the machine on an interface unless the super-user
278 has enabled copy-all-mode operation on that interface using
282 packet capture on an interface probably requires that either
283 promiscuous-mode or copy-all-mode operation, or both modes of
284 operation, be enabled on that interface.
286 .B Under BSD (this includes macOS):
287 You must have read access to
289 on systems that don't have a cloning BPF device, or to
292 On BSDs with a devfs (this includes macOS), this might involve more
293 than just having somebody with super-user access setting the ownership
294 or permissions on the BPF devices - it might involve configuring devfs
295 to set the ownership or permissions every time the system is booted,
296 if the system even supports that; if it doesn't support that, you might
297 have to find some other way to make that happen at boot time.
299 Reading a saved packet file doesn't require special privileges.
301 The packets read from the handle may include a ``pseudo-header''
302 containing various forms of packet meta-data, and probably includes a
303 link-layer header whose contents can differ for different network
304 interfaces. To determine the format of the packets supplied by the
306 .BR pcap_datalink ();
307 .I http://www.tcpdump.org/linktypes.html
308 lists the values it returns and describes the packet formats that
309 correspond to those values.
313 assume that the packets for a given capture or ``savefile`` will have
314 any given link-layer header type, such as
316 for Ethernet. For example, the "any" device on Linux will have a
317 link-layer header type of
319 even if all devices on the system at the time the "any" device is opened
320 have some other data link type, such as
328 opened for a ``savefile'', call
334 .BR pcap_create (3PCAP)
339 .BR pcap_activate (3PCAP)
344 .BR pcap_findalldevs (3PCAP)
345 get a list of devices that can be opened for a live capture
347 .BR pcap_freealldevs (3PCAP)
350 .BR pcap_lookupdev (3PCAP)
351 get first non-loopback device on that list
353 .BR pcap_open_offline (3PCAP)
356 for a ``savefile'', given a pathname
358 .BR pcap_open_offline_with_tstamp_precision (3PCAP)
361 for a ``savefile'', given a pathname, and specify the precision to
362 provide for packet time stamps
364 .BR pcap_fopen_offline (3PCAP)
367 for a ``savefile'', given a
370 .BR pcap_fopen_offline_with_tstamp_precision (3PCAP)
373 for a ``savefile'', given a
375 and specify the precision to provide for packet time stamps
377 .BR pcap_open_dead (3PCAP)
381 .BR pcap_close (3PCAP)
385 .BR pcap_set_snaplen (3PCAP)
386 set the snapshot length for a not-yet-activated
390 .BR pcap_snapshot (3PCAP)
391 get the snapshot length for a
394 .BR pcap_set_promisc (3PCAP)
395 set promiscuous mode for a not-yet-activated
399 .BR pcap_set_protocol (3PCAP)
400 set capture protocol for a not-yet-activated
402 for live capture (Linux only)
404 .BR pcap_set_rfmon (3PCAP)
405 set monitor mode for a not-yet-activated
409 .BR pcap_can_set_rfmon (3PCAP)
410 determine whether monitor mode can be set for a
414 .BR pcap_set_timeout (3PCAP)
415 set packet buffer timeout for a not-yet-activated
419 .BR pcap_set_buffer_size (3PCAP)
420 set buffer size for a not-yet-activated
424 .BR pcap_set_tstamp_type (3PCAP)
425 set time stamp type for a not-yet-activated
429 .BR pcap_list_tstamp_types (3PCAP)
430 get list of available time stamp types for a not-yet-activated
434 .BR pcap_free_tstamp_types (3PCAP)
435 free list of available time stamp types
437 .BR pcap_tstamp_type_val_to_name (3PCAP)
438 get name for a time stamp type
440 .BR pcap_tstamp_type_val_to_description (3PCAP)
441 get description for a time stamp type
443 .BR pcap_tstamp_type_name_to_val (3PCAP)
444 get time stamp type corresponding to a name
446 .BR pcap_set_tstamp_precision (3PCAP)
447 set time stamp precision for a not-yet-activated
451 .BR pcap_get_tstamp_precision (3PCAP)
452 get the time stamp precision of a
456 .BR pcap_datalink (3PCAP)
457 get link-layer header type for a
460 .BR pcap_file (3PCAP)
465 opened for a ``savefile''
467 .BR pcap_is_swapped (3PCAP)
468 determine whether a ``savefile'' being read came from a machine with the
471 .BR pcap_major_version (3PCAP)
474 .BR pcap_minor_version (3PCAP)
475 get the major and minor version of the file format version for a
479 .SS Selecting a link-layer header type for a live capture
480 Some devices may provide more than one link-layer header type. To
481 obtain a list of all link-layer header types provided by a device, call
482 .BR pcap_list_datalinks ()
486 To free a list of link-layer header types, call
487 .BR pcap_free_datalinks ().
488 To set the link-layer header type for a device, call
489 .BR pcap_set_datalink ().
490 This should be done after the device has been activated but before any
491 packets are read and before any filters are compiled or installed.
496 .BR pcap_list_datalinks (3PCAP)
497 get a list of link-layer header types for a device
499 .BR pcap_free_datalinks (3PCAP)
500 free list of link-layer header types
502 .BR pcap_set_datalink (3PCAP)
503 set link-layer header type for a device
505 .BR pcap_datalink_val_to_name (3PCAP)
506 get name for a link-layer header type
508 .BR pcap_datalink_val_to_description (3PCAP)
509 get description for a link-layer header type
511 .BR pcap_datalink_name_to_val (3PCAP)
512 get link-layer header type corresponding to a name
515 Packets are read with
519 which process one or more packets, calling a callback routine for each
524 which return the next packet.
529 is supplied a pointer to a
530 .IR "struct pcap_pkthdr" ,
531 which includes the following members:
537 containing the time when the packet was captured
542 giving the number of bytes of the packet that are available from the
548 giving the length of the packet, in bytes (which might be more than the
549 number of bytes available from the capture, if the length of the packet
550 is larger than the maximum number of bytes to capture).
553 The callback is also supplied a
558 .I struct pcap_pkthdr
560 bytes of data from the packet. This won't necessarily be the entire
561 packet; to capture the entire packet, you will have to provide a value
565 .BR pcap_set_snaplen ()
566 that is sufficiently large to get all of the packet's data - a value of
567 65535 should be sufficient on most if not all networks). When reading
568 from a ``savefile'', the snapshot length specified when the capture was
569 performed will limit the amount of packet data available.
572 is passed an argument that points to a
573 .I struct pcap_pkthdr
574 structure, and fills it in with the time stamp and length values for the
579 bytes of the packet on success, and NULL on error.
582 is passed two pointer arguments, one of which points to a
583 .IR struct pcap_pkthdr *
584 and one of which points to a
585 .IR "const u_char" *.
586 It sets the first pointer to point to a
587 .I struct pcap_pkthdr
588 structure with the time stamp and length values for the packet, and sets
589 the second pointer to point to the first
598 .BR pcap_breakloop ().
600 By default, when reading packets from an interface opened for a live
602 .BR pcap_dispatch (),
606 will, if no packets are currently available to be read, block waiting
607 for packets to become available. On some, but
609 all, platforms, if a packet buffer timeout was specified, the wait will
610 terminate after the packet buffer timeout expires; applications should
611 be prepared for this, as it happens on some platforms, but should not
612 rely on it, as it does not happen on other platforms. Note that the
613 wait might, or might not, terminate even if no packets are available;
614 applications should be prepared for this to happen, but must not rely on
617 A handle can be put into ``non-blocking mode'', so that those routines
618 will, rather than blocking, return an indication that no packets are
619 available to read. Call
620 .BR pcap_setnonblock ()
621 to put a handle into non-blocking mode or to take it out of non-blocking
623 .BR pcap_getnonblock ()
624 to determine whether a handle is in non-blocking mode. Note that
625 non-blocking mode does not work correctly in Mac OS X 10.6.
627 Non-blocking mode is often combined with routines such as
631 or other routines a platform offers to wait for any of a set of
632 descriptors to be ready to read. To obtain, for a handle, a descriptor
633 that can be used in those routines, call
634 .BR pcap_get_selectable_fd ().
635 Not all handles have such a descriptor available;
636 .BR pcap_get_selectable_fd ()
637 will return \-1 if no such descriptor exists. In addition, for various
638 reasons, one or more of those routines will not work properly with the
639 descriptor; the documentation for
640 .BR pcap_get_selectable_fd ()
641 gives details. Note that, just as an attempt to read packets from a
643 may not return any packets if the packet buffer timeout expires, a
646 or other such call may, if the packet buffer timeout expires, indicate
647 that a descriptor is ready to read even if there are no packets
653 .BR pcap_dispatch (3PCAP)
654 read a bufferful of packets from a
656 open for a live capture or the full set of packets from a
658 open for a ``savefile''
660 .BR pcap_loop (3PCAP)
663 until an interrupt or error occurs
665 .BR pcap_next (3PCAP)
666 read the next packet from a
668 without an indication whether an error occurred
670 .BR pcap_next_ex (3PCAP)
671 read the next packet from a
673 with an error indication on an error
675 .BR pcap_breakloop (3PCAP)
676 prematurely terminate the loop in
681 .BR pcap_setnonblock (3PCAP)
682 set or clear non-blocking mode on a
685 .BR pcap_getnonblock (3PCAP)
686 get the state of non-blocking mode for a
689 .BR pcap_get_selectable_fd (3PCAP)
690 attempt to get a descriptor for a
692 that can be used in calls such as
698 In order to cause only certain packets to be returned when reading
699 packets, a filter can be set on a handle. For a live capture, the
700 filtering will be performed in kernel mode, if possible, to avoid
701 copying ``uninteresting'' packets from the kernel to user mode.
703 A filter can be specified as a text string; the syntax and semantics of
704 the string are as described by
705 .BR pcap-filter (@MAN_MISC_INFO@).
706 A filter string is compiled into a program in a pseudo-machine-language
709 and the resulting program can be made a filter for a handle with
710 .BR pcap_setfilter ().
713 can be freed with a call to
714 .BR pcap_freecode ().
716 may require a network mask for certain expressions in the filter string;
717 .BR pcap_lookupnet ()
718 can be used to find the network address and network mask for a given
721 A compiled filter can also be applied directly to a packet that has been
723 .BR pcap_offline_filter ().
728 .BR pcap_compile (3PCAP)
729 compile filter expression to a pseudo-machine-language code program
731 .BR pcap_freecode (3PCAP)
732 free a filter program
734 .BR pcap_setfilter (3PCAP)
738 .BR pcap_lookupnet (3PCAP)
739 get network address and network mask for a capture device
741 .BR pcap_offline_filter (3PCAP)
742 apply a filter program to a packet
744 .SS Incoming and outgoing packets
745 By default, libpcap will attempt to capture both packets sent by the
746 machine and packets received by the machine. To limit it to capturing
747 only packets received by the machine or, if possible, only packets sent
749 .BR pcap_setdirection ().
754 .BR pcap_setdirection (3PCAP)
755 specify whether to capture incoming packets, outgoing packets, or both
757 .SS Capture statistics
758 To get statistics about packets received and dropped in a live capture,
765 .BR pcap_stats (3PCAP)
766 get capture statistics
768 .SS Opening a handle for writing captured packets
769 To open a ``savefile`` to which to write packets, given the pathname the
770 ``savefile'' should have, call
771 .BR pcap_dump_open ().
772 To open a ``savefile`` to which to write packets, given the pathname the
773 ``savefile'' should have, call
774 .BR pcap_dump_open ();
775 to set up a handle for a ``savefile'', given a
777 referring to a file already opened for writing, call
778 .BR pcap_dump_fopen ().
779 They each return pointers to a
781 which is the handle used for writing packets to the ``savefile''. If it
782 succeeds, it will have created the file if it doesn't exist and
783 truncated the file if it does exist.
787 .BR pcap_dump_close ().
792 .BR pcap_dump_open (3PCAP)
795 for a ``savefile``, given a pathname
797 .BR pcap_dump_fopen (3PCAP)
800 for a ``savefile``, given a
803 .BR pcap_dump_close (3PCAP)
807 .BR pcap_dump_file (3PCAP)
812 opened for a ``savefile''
815 To write a packet to a
821 may be buffered, rather than being immediately written to the
822 ``savefile''. Closing the
824 will cause all buffered-but-not-yet-written packets to be written to the
826 To force all packets written to the
828 and not yet written to the ``savefile'' because they're buffered by the
830 to be written to the ``savefile'', without closing the
833 .BR pcap_dump_flush ().
838 .BR pcap_dump (3PCAP)
842 .BR pcap_dump_flush (3PCAP)
843 flush buffered packets written to a
847 .BR pcap_dump_ftell (3PCAP)
848 get current file position for a
851 .SS Injecting packets
852 If you have the required privileges, you can inject packets onto a
855 for a live capture, using
858 .BR pcap_sendpacket ().
859 (The two routines exist for compatibility with both OpenBSD and WinPcap;
860 they perform the same function, but have different return values.)
865 .BR pcap_inject (3PCAP)
868 .BR pcap_sendpacket (3PCAP)
873 Some routines return error or warning status codes; to convert them to a
875 .BR pcap_statustostr ().
880 .BR pcap_statustostr (3PCAP)
881 get a string for an error or warning status code
883 .SS Getting library version information
884 To get a string giving version information about libpcap, call
885 .BR pcap_lib_version ().
890 .BR pcap_lib_version (3PCAP)
891 get library version string
893 .SH BACKWARDS COMPATIBILITY
895 In versions of libpcap prior to 1.0, the
897 header file was not in a
899 directory on most platforms; if you are writing an application that must
900 work on versions of libpcap prior to 1.0, include
904 for you, rather than including
910 were not available in versions of libpcap prior to 1.0; if you are
911 writing an application that must work on versions of libpcap prior to
913 .BR pcap_open_live ()
914 to get a handle for a live capture or, if you want to be able to use the
915 additional capabilities offered by using
918 .BR pcap_activate (),
921 script or some other configuration script to check whether the libpcap
922 1.0 APIs are available and use them only if they are.
924 autoconf(1), tcpdump(1), tcpslice(1), pcap-filter(@MAN_MISC_INFO@), pfconfig(8),
925 usermod(@MAN_ADMIN_COMMANDS@)
927 The original authors of libpcap are:
931 Steven McCanne, all of the
932 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
934 The current version is available from "The Tcpdump Group"'s Web site at
937 .I http://www.tcpdump.org/
940 To report a security issue please send an e-mail to security@tcpdump.org.
942 To report bugs and other problems, contribute patches, request a
943 feature, provide generic feedback etc please see the file
945 in the libpcap source tree root.