The Regents of the University of California. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that: (1) source code distributions
retain the above copyright notice and this paragraph in its entirety, (2)
distributions including binary code include the above copyright notice and
this paragraph in its entirety in the documentation or other materials
provided with the distribution, and (3) all advertising materials mentioning
features or use of this software display the following acknowledgement:
``This product includes software developed by the University of California,
Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
the University nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <pcap/pcap.h>
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:
Under SunOS 3.x or 4.x with NIT or BPF: You must have read access to /dev/nit or /dev/bpf* .
Under Solaris with DLPI: You must have read/write access to the network pseudo device, e.g. /dev/le . On at least some versions of Solaris, however, this is not sufficient to allow tcpdump to capture in promiscuous mode; on those versions of Solaris, you must be root, or the application capturing packets must be installed setuid to root, in order to capture in promiscuous mode. Note that, on many (perhaps all) interfaces, if you don't capture in promiscuous mode, you will not see any outgoing packets, so a capture not done in promiscuous mode may not be very useful.
In newer versions of Solaris, you must have been given the net_rawaccess privilege; this is both necessary and sufficient to give you access to the network pseudo-device - there is no need to change the privileges on that device. A user can be given that privilege by, for example, adding that privilege to the user's defaultpriv key with the usermod (@MAN_ADMIN_COMMANDS@) command.Under HP-UX with DLPI: You must be root or the application capturing packets must be installed setuid to root.
Under IRIX with snoop: You must be root or the application capturing packets must be installed setuid to root.
Under Linux: You must be root or the application capturing packets must be installed setuid to root (unless your distribution has a kernel that supports capability bits such as CAP_NET_RAW and code to allow those capability bits to be given to particular accounts and to cause those bits to be set on a user's initial processes when they log in, in which case you must have CAP_NET_RAW in order to capture and CAP_NET_ADMIN to enumerate network devices with, for example, the -D flag).
Under ULTRIX and Digital UNIX/Tru64 UNIX: Any user may capture network traffic. However, no user (not even the super-user) can capture in promiscuous mode on an interface unless the super-user has enabled promiscuous-mode operation on that interface using pfconfig (8), and no user (not even the super-user) can capture unicast traffic received by or sent by the machine on an interface unless the super-user has enabled copy-all-mode operation on that interface using pfconfig , so useful packet capture on an interface probably requires that either promiscuous-mode or copy-all-mode operation, or both modes of operation, be enabled on that interface.
Under BSD (this includes macOS): You must have read access to /dev/bpf* on systems that don't have a cloning BPF device, or to /dev/bpf on systems that do. On BSDs with a devfs (this includes macOS), this might involve more than just having somebody with super-user access setting the ownership or permissions on the BPF devices - it might involve configuring devfs to set the ownership or permissions every time the system is booted, if the system even supports that; if it doesn't support that, you might have to find some other way to make that happen at boot time.
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 ().
Routines
pcap_create (3PCAP) get a pcap_t for live capture
pcap_activate (3PCAP) activate a pcap_t for live capture
pcap_findalldevs (3PCAP) get a list of devices that can be opened for a live capture
pcap_freealldevs (3PCAP) free list of devices
pcap_lookupdev (3PCAP) get first non-loopback device on that list
pcap_open_offline (3PCAP) open a pcap_t for a ``savefile'', given a pathname
pcap_open_offline_with_tstamp_precision (3PCAP) open a pcap_t for a ``savefile'', given a pathname, and specify the precision to provide for packet time stamps
pcap_fopen_offline (3PCAP) open a pcap_t for a ``savefile'', given a "FILE *"
pcap_fopen_offline_with_tstamp_precision (3PCAP) open a pcap_t for a ``savefile'', given a "FILE *" , and specify the precision to provide for packet time stamps
pcap_open_dead (3PCAP) create a ``fake'' pcap_t
pcap_close (3PCAP) close a pcap_t
pcap_set_snaplen (3PCAP) set the snapshot length for a not-yet-activated pcap_t for live capture
pcap_snapshot (3PCAP) get the snapshot length for a pcap_t
pcap_set_promisc (3PCAP) set promiscuous mode for a not-yet-activated pcap_t for live capture
pcap_set_protocol (3PCAP) set capture protocol for a not-yet-activated pcap_t for live capture (Linux only)
pcap_set_rfmon (3PCAP) set monitor mode for a not-yet-activated pcap_t for live capture
pcap_can_set_rfmon (3PCAP) determine whether monitor mode can be set for a pcap_t for live capture
pcap_set_timeout (3PCAP) set packet buffer timeout for a not-yet-activated pcap_t for live capture
pcap_set_buffer_size (3PCAP) set buffer size for a not-yet-activated pcap_t for live capture
pcap_set_tstamp_type (3PCAP) set time stamp type for a not-yet-activated pcap_t for live capture
pcap_list_tstamp_types (3PCAP) get list of available time stamp types for a not-yet-activated pcap_t for live capture
pcap_free_tstamp_types (3PCAP) free list of available time stamp types
pcap_tstamp_type_val_to_name (3PCAP) get name for a time stamp type
pcap_tstamp_type_val_to_description (3PCAP) get description for a time stamp type
pcap_tstamp_type_name_to_val (3PCAP) get time stamp type corresponding to a name
pcap_set_tstamp_precision (3PCAP) set time stamp precision for a not-yet-activated pcap_t for live capture
pcap_get_tstamp_precision (3PCAP) get the time stamp precision of a pcap_t for live capture
pcap_datalink (3PCAP) get link-layer header type for a pcap_t
pcap_file (3PCAP) get the "FILE *" for a pcap_t opened for a ``savefile''
pcap_is_swapped (3PCAP) determine whether a ``savefile'' being read came from a machine with the opposite byte order
pcap_major_version (3PCAP)
0
pcap_minor_version (3PCAP) get the major and minor version of the file format version for a ``savefile''
Routines
pcap_list_datalinks (3PCAP) get a list of link-layer header types for a device
pcap_free_datalinks (3PCAP) free list of link-layer header types
pcap_set_datalink (3PCAP) set link-layer header type for a device
pcap_datalink_val_to_name (3PCAP) get name for a link-layer header type
pcap_datalink_val_to_description (3PCAP) get description for a link-layer header type
pcap_datalink_name_to_val (3PCAP) get link-layer header type corresponding to a name
ts a struct timeval containing the time when the packet was captured
caplen a bpf_u_int32 giving the number of bytes of the packet that are available from the capture
len a bpf_u_int32 giving the length of the packet, in bytes (which might be more than the number of bytes available from the capture, if the length of the packet is larger than the maximum number of bytes to capture).
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 struct pcap_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 packet buffer timeout was specified, the wait will terminate after the packet buffer 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 packet buffer timeout expires, a select (), poll (), or other such call may, if the packet buffer timeout expires, indicate that a descriptor is ready to read even if there are no packets available to read.
Routines
pcap_dispatch (3PCAP) read a bufferful of packets from a pcap_t open for a live capture or the full set of packets from a pcap_t open for a ``savefile''
pcap_loop (3PCAP) read packets from a pcap_t until an interrupt or error occurs
pcap_next (3PCAP) read the next packet from a pcap_t without an indication whether an error occurred
pcap_next_ex (3PCAP) read the next packet from a pcap_t with an error indication on an error
pcap_breakloop (3PCAP) prematurely terminate the loop in pcap_dispatch () or pcap_loop ()
pcap_setnonblock (3PCAP) set or clear non-blocking mode on a pcap_t
pcap_getnonblock (3PCAP) get the state of non-blocking mode for a pcap_t
pcap_get_selectable_fd (3PCAP) attempt to get a descriptor for a pcap_t that can be used in calls such as select (2) and poll (2)
A filter can be specified as a text string; the syntax and semantics of the string are as described by pcap-filter (@MAN_MISC_INFO@). 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 ().
Routines
pcap_compile (3PCAP) compile filter expression to a pseudo-machine-language code program
pcap_freecode (3PCAP) free a filter program
pcap_setfilter (3PCAP) set filter for a pcap_t
pcap_lookupnet (3PCAP) get network address and network mask for a capture device
pcap_offline_filter (3PCAP) apply a filter program to a packet
Routines
pcap_setdirection (3PCAP) specify whether to capture incoming packets, outgoing packets, or both
Routines
pcap_stats (3PCAP) get capture statistics
Routines
pcap_dump_open (3PCAP) open a pcap_dumper_t for a ``savefile``, given a pathname
pcap_dump_fopen (3PCAP) open a pcap_dumper_t for a ``savefile``, given a "FILE *"
pcap_dump_close (3PCAP) close a pcap_dumper_t
pcap_dump_file (3PCAP) get the "FILE *" for a pcap_dumper_t opened for a ``savefile''
Routines
pcap_dump (3PCAP) write packet to a pcap_dumper_t
pcap_dump_flush (3PCAP) flush buffered packets written to a pcap_dumper_t to the ``savefile''
pcap_dump_ftell (3PCAP) get current file position for a pcap_dumper_t
Routines
pcap_inject (3PCAP)
0
pcap_sendpacket (3PCAP) transmit a packet
Routines
pcap_statustostr (3PCAP) get a string for an error or warning status code
Routines
pcap_lib_version (3PCAP) get library version string
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.
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
http://www.tcpdump.org/
To report bugs and other problems, contribute patches, request a feature, provide generic feedback etc please see the file CONTRIBUTING in the libpcap source tree root.