PCAP(3PCAP) | PCAP(3PCAP) |
pcap - Packet Capture library
#include <pcap/pcap.h>
The Packet Capture library provides a high level interface to packet capture systems. All packets on the network, even those destined for other hosts, are accessible through this mechanism. It also supports saving captured packets to a ``savefile'', and reading packets from a ``savefile''.
To open a handle for a live capture, given the name of the network or other interface on which the capture should be done, call pcap_create(), set the appropriate options on the handle, and then activate it with pcap_activate().
To obtain a list of devices that can be opened for a live capture, call pcap_findalldevs(); to free the list returned by pcap_findalldevs(), call pcap_freealldevs(). pcap_lookupdev() will return the first device on that list that is not a ``loopback`` network interface.
To open a handle for a ``savefile'' from which to read packets, given the pathname of the ``savefile'', call pcap_open_offline(); to set up a handle for a ``savefile'', given a FILE * referring to a file already opened for reading, call pcap_fopen_offline().
In order to get a ``fake'' pcap_t for use in routines that require a pcap_t as an argument, such as routines to open a ``savefile'' for writing and to compile a filter expression, call pcap_open_dead().
pcap_create(), pcap_open_offline(), pcap_fopen_offline(), and pcap_open_dead() return a pointer to a pcap_t, which is the handle used for reading packets from the capture stream or the ``savefile'', and for finding out information about the capture stream or ``savefile''. To close a handle, use pcap_close().
The options that can be set on a capture handle include
Reading packets from a network interface may require that you have special privileges:
Reading a saved packet file doesn't require special privileges.
The packets read from the handle may include a ``pseudo-header'' containing various forms of packet meta-data, and probably includes a link-layer header whose contents can differ for different network interfaces. To determine the format of the packets supplied by the handle, call pcap_datalink(); http://www.tcpdump.org/linktypes.html lists the values it returns and describes the packet formats that correspond to those values.
Do NOT assume that the packets for a given capture or ``savefile`` will have any given link-layer header type, such as DLT_EN10MB for Ethernet. For example, the "any" device on Linux will have a link-layer header type of DLT_LINUX_SLL even if all devices on the system at the time the "any" device is opened have some other data link type, such as DLT_EN10MB for Ethernet.
To obtain the FILE * corresponding to a pcap_t opened for a ``savefile'', call pcap_file().
Some devices may provide more than one link-layer header type. To obtain a list of all link-layer header types provided by a device, call pcap_list_datalinks() on an activated pcap_t for the device. To free a list of link-layer header types, call pcap_free_datalinks(). To set the link-layer header type for a device, call pcap_set_datalink(). This should be done after the device has been activated but before any packets are read and before any filters are compiled or installed.
Packets are read with pcap_dispatch() or pcap_loop(), which process one or more packets, calling a callback routine for each packet, or with pcap_next() or pcap_next_ex(), which return the next packet. The callback for pcap_dispatch() and pcap_loop() is supplied a pointer to a struct pcap_pkthdr, which includes the following members:
The callback is also supplied a const u_char pointer to the first caplen (as given in the struct pcap_pkthdr mentioned above) bytes of data from the packet. This won't necessarily be the entire packet; to capture the entire packet, you will have to provide a value for snaplen in your call to pcap_set_snaplen() that is sufficiently large to get all of the packet's data - a value of 65535 should be sufficient on most if not all networks). When reading from a ``savefile'', the snapshot length specified when the capture was performed will limit the amount of packet data available.
pcap_next() is passed an argument that points to a struct pcap_pkthdr structure, and fills it in with the time stamp and length values for the packet. It returns a const u_char to the first caplen bytes of the packet on success, and NULL on error.
pcap_next_ex() is passed two pointer arguments, one of which points to a structpcap_pkthdr* and one of which points to a const u_char*. It sets the first pointer to point to a struct pcap_pkthdr structure with the time stamp and length values for the packet, and sets the second pointer to point to the first caplen bytes of the packet.
To force the loop in pcap_dispatch() or pcap_loop() to terminate, call pcap_breakloop().
By default, when reading packets from an interface opened for a live capture, pcap_dispatch(), pcap_next(), and pcap_next_ex() will, if no packets are currently available to be read, block waiting for packets to become available. On some, but not all, platforms, if a read timeout was specified, the wait will terminate after the read timeout expires; applications should be prepared for this, as it happens on some platforms, but should not rely on it, as it does not happen on other platforms. Note that the wait might, or might not, terminate even if no packets are available; applications should be prepared for this to happen, but must not rely on it happening.
A handle can be put into ``non-blocking mode'', so that those routines will, rather than blocking, return an indication that no packets are available to read. Call pcap_setnonblock() to put a handle into non-blocking mode or to take it out of non-blocking mode; call pcap_getnonblock() to determine whether a handle is in non-blocking mode. Note that non-blocking mode does not work correctly in Mac OS X 10.6.
Non-blocking mode is often combined with routines such as select(2) or poll(2) or other routines a platform offers to wait for any of a set of descriptors to be ready to read. To obtain, for a handle, a descriptor that can be used in those routines, call pcap_get_selectable_fd(). Not all handles have such a descriptor available; pcap_get_selectable_fd() will return -1 if no such descriptor exists. In addition, for various reasons, one or more of those routines will not work properly with the descriptor; the documentation for pcap_get_selectable_fd() gives details. Note that, just as an attempt to read packets from a pcap_t may not return any packets if the read timeout expires, a select(), poll(), or other such call may, if the read timeout expires, indicate that a descriptor is ready to read even if there are no packets available to read.
In order to cause only certain packets to be returned when reading packets, a filter can be set on a handle. For a live capture, the filtering will be performed in kernel mode, if possible, to avoid copying ``uninteresting'' packets from the kernel to user mode.
A filter can be specified as a text string; the syntax and semantics of the string are as described by pcap-filter(7). A filter string is compiled into a program in a pseudo-machine-language by pcap_compile() and the resulting program can be made a filter for a handle with pcap_setfilter(). The result of pcap_compile() can be freed with a call to pcap_freecode(). pcap_compile() may require a network mask for certain expressions in the filter string; pcap_lookupnet() can be used to find the network address and network mask for a given capture device.
A compiled filter can also be applied directly to a packet that has been read using pcap_offline_filter().
By default, libpcap will attempt to capture both packets sent by the machine and packets received by the machine. To limit it to capturing only packets received by the machine or, if possible, only packets sent by the machine, call pcap_setdirection().
To get statistics about packets received and dropped in a live capture, call pcap_stats().
To open a ``savefile`` to which to write packets, given the pathname the ``savefile'' should have, call pcap_dump_open(). To open a ``savefile`` to which to write packets, given the pathname the ``savefile'' should have, call pcap_dump_open(); to set up a handle for a ``savefile'', given a FILE * referring to a file already opened for writing, call pcap_dump_fopen(). They each return pointers to a pcap_dumper_t, which is the handle used for writing packets to the ``savefile''. If it succeeds, it will have created the file if it doesn't exist and truncated the file if it does exist. To close a pcap_dumper_t, call pcap_dump_close().
To write a packet to a pcap_dumper_t, call pcap_dump(). Packets written with pcap_dump() may be buffered, rather than being immediately written to the ``savefile''. Closing the pcap_dumper_t will cause all buffered-but-not-yet-written packets to be written to the ``savefile''. To force all packets written to the pcap_dumper_t, and not yet written to the ``savefile'' because they're buffered by the pcap_dumper_t, to be written to the ``savefile'', without closing the pcap_dumper_t, call pcap_dump_flush().
If you have the required privileges, you can inject packets onto a network with a pcap_t for a live capture, using pcap_inject() or pcap_sendpacket(). (The two routines exist for compatibility with both OpenBSD and WinPcap; they perform the same function, but have different return values.)
Some routines return error or warning status codes; to convert them to a string, use pcap_statustostr().
To get a string giving version information about libpcap, call pcap_lib_version().
In versions of libpcap prior to 1.0, the pcap.h header file was not in a pcap directory on most platforms; if you are writing an application that must work on versions of libpcap prior to 1.0, include <pcap.h>, which will include <pcap/pcap.h> for you, rather than including <pcap/pcap.h>.
pcap_create() and pcap_activate() were not available in versions of libpcap prior to 1.0; if you are writing an application that must work on versions of libpcap prior to 1.0, either use pcap_open_live() to get a handle for a live capture or, if you want to be able to use the additional capabilities offered by using pcap_create() and pcap_activate(), use an autoconf(1) script or some other configuration script to check whether the libpcap 1.0 APIs are available and use them only if they are.
autoconf(1), tcpdump(8), tcpslice(1), pcap-filter(7), pfconfig(8), usermod(1M)
The original authors of libpcap are:
Van Jacobson, Craig Leres and Steven McCanne, all of the Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
The current version is available from "The Tcpdump Group"'s Web site at
Please send problems, bugs, questions, desirable enhancements, etc. to:
8 March 2015 |