SEARCH
NEW RPMS
DIRECTORIES
ABOUT
FAQ
VARIOUS
BLOG
DONATE


YUM REPOSITORY

 
 

MAN page from Trustix libpcap-0.7.2-1tr.i586.rpm

PCAP

Section: C Library Functions (3)
Updated: 3 January 2001
Index 

NAME

pcap - Packet Capture library 

SYNOPSIS

#include <pcap.h>char errbuf[PCAP_ERRBUF_SIZE];pcap_t *pcap_open_live(char *device, int snaplen,
        int promisc, int to_ms, char *errbuf)pcap_t *pcap_open_dead(int linktype, int snaplen)pcap_t *pcap_open_offline(char *fname, char *errbuf)pcap_dumper_t *pcap_dump_open(pcap_t *p, char *fname)
int pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);int pcap_getnonblock(pcap_t *p, char *errbuf);int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)void pcap_freealldevs(pcap_if_t *)char *pcap_lookupdev(char *errbuf)int pcap_lookupnet(char *device, bpf_u_int32 *netp,
        bpf_u_int32 *maskp, char *errbuf)
int pcap_dispatch(pcap_t *p, int cnt,
        pcap_handler callback, u_char *user)int pcap_loop(pcap_t *p, int cnt,
        pcap_handler callback, u_char *user)void pcap_dump(u_char *user, struct pcap_pkthdr *h,
        u_char *sp)
int pcap_compile(pcap_t *p, struct bpf_program *fp,
        char *str, int optimize, bpf_u_int32 netmask)int pcap_setfilter(pcap_t *p, struct bpf_program *fp)void pcap_freecode(struct bpf_program *);
u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)int pcap_datalink(pcap_t *p)int pcap_snapshot(pcap_t *p)int pcap_is_swapped(pcap_t *p)int pcap_major_version(pcap_t *p)int pcap_minor_version(pcap_t *p)int pcap_stats(pcap_t *p, struct pcap_stat *ps)FILE *pcap_file(pcap_t *p)int pcap_fileno(pcap_t *p)void pcap_perror(pcap_t *p, char *prefix)char *pcap_geterr(pcap_t *p)char *pcap_strerror(int error)void pcap_close(pcap_t *p)void pcap_dump_close(pcap_dumper_t *p)
 

DESCRIPTION

The Packet Capture libraryprovides a high level interface to packet capture systems. All packetson the network, even those destined for other hosts, are accessiblethrough this mechanism.

 

ROUTINES

NOTE:errbufinpcap_open_live(),pcap_open_dead(),pcap_open_offline(),pcap_setnonblock(),pcap_getnonblock(),pcap_findalldevs(),pcap_lookupdev(),andpcap_lookupnet()is assumed to be able to hold at leastPCAP_ERRBUF_SIZEchars.

pcap_open_live()is used to obtain a packet capture descriptor to lookat packets on the network.deviceis a string that specifies the network device to open; on Linux systemswith 2.2 or later kernels, adeviceargument of "any" orNULLcan be used to capture packets from all interfaces.snaplenspecifies the maximum number of bytes to capture.promiscspecifies if the interface is to be put into promiscuous mode.(Note that even if this parameter is false, the interfacecould well be in promiscuous mode for some other reason.) For now, thisdoesn't work on the "any" device; if an argument of "any" or NULL issupplied, thepromiscflag is ignored.to_msspecifies the read timeout in milliseconds. The read timeout is used toarrange that the read not necessarily return immediately when a packetis seen, but that it wait for some amount of time to allow more packetsto arrive and to read multiple packets from the OS kernel in oneoperation. Not all platforms support a read timeout; on platforms thatdon't, the read timeout is ignored.errbufis used to return error or warning text. It will be set to error text whenpcap_open_live()fails and returnsNULL.errbufmay also be set to warning text whenpcap_open_live()succeds; to detect this case the caller should store a zero-length string inerrbufbefore callingpcap_open_live()and display the warning to the user iferrbufis no longer a zero-length string.

pcap_open_dead()is used for creating apcap_tstructure to use when calling the other functions in libpcap. It istypically used when just using libpcap for compiling BPF code.

pcap_open_offline()is called to open a ``savefile'' for reading.fnamespecifies the name of the file to open. The file hasthe same format as those used bytcpdump(1)andtcpslice(1).The name "-" in a synonym forstdin.errbufis used to return error text and is only set whenpcap_open_offline()fails and returnsNULL.

pcap_dump_open()is called to open a ``savefile'' for writing. The name "-" in a synonymforstdout.NULLis returned on failure.pis apcapstruct as returned bypcap_open_offline()orpcap_open_live().fnamespecifies the name of the file to open.IfNULLis returned,pcap_geterr()can be used to get the error text.

pcap_setnonblock()puts a capture descriptor, opened withpcap_open_live(),into ``non-blocking'' mode, or takes it out of ``non-blocking'' mode,depending on whether thenonblockargument is non-zero or zero. It has no effect on ``savefiles''.If there is an error, -1 is returned anderrbufis filled in with an appropriate error message; otherwise, 0 isreturned.In``non-blocking'' mode, an attempt to read from the capture descriptorwithpcap_dispatch()will, if no packets are currently available to be read, return 0immediately rather than blocking waiting for packets to arrive.pcap_loop()andpcap_next()will not work in ``non-blocking'' mode.

pcap_getnonblock()returns the current ``non-blocking'' state of the capture descriptor; italways returns 0 on ``savefiles''.If there is an error, -1 is returned anderrbufis filled in with an appropriate error message.

pcap_findalldevs()constructs a list of network devices that can be opened withpcap_open_live().(Note that there may be network devices that cannot be opened withpcap_open_live()by theprocess callingpcap_findalldevs(),because, for example, that process might not have sufficient privilegesto open them for capturing; if so, those devices will not appear on thelist.)alldevspis set to point to the first element of the list; each element of thelist is of typepcap_if_t,and has the following members:

next
if notNULL,a pointer to the next element in the list;NULLfor the last element of the list
name
a pointer to a string giving a name for the device to pass topcap_open_live()
description
if notNULL,a pointer to a string giving a human-readable description of the device
addresses
a pointer to the first element of a list of addresses for the interface
flags
interface flags:
PCAP_IF_LOOPBACK
set if the interface is a loopback interface

Each element of the list of addresses is of typepcap_addr_t,and has the following members:

next
if notNULL,a pointer to the next element in the list;NULLfor the last element of the list
addr
a pointer to astruct sockaddrcontaining an address
netmask
if notNULL,a pointer to astruct sockaddrthat contains the netmask corresponding to the address pointed to byaddr
broadaddr
if notNULL,a pointer to astruct sockaddrthat contains the broadcast address corresponding to the address pointedto byaddr;may be null if the interface doesn't support broadcasts
dstaddr
if notNULL,a pointer to astruct sockaddrthat contains the destination address corresponding to the address pointedto byaddr;may be null if the interface isn't a point-to-point interface

pcap_freealldevs()is used to free a list allocated bypcap_findalldevs().

pcap_lookupdev()returns a pointer to a network device suitable for use withpcap_open_live()andpcap_lookupnet().If there is an error,NULLis returned anderrbufis filled in with an appropriate error message.

pcap_lookupnet()is used to determine the network number and maskassociated with the network devicedevice.Bothnetpandmaskparebpf_u_int32pointers.A return of -1 indicates an error in which caseerrbufis filled in with an appropriate error message.

pcap_dispatch()is used to collect and process packets.cntspecifies the maximum number of packets to process before returning.This is not a minimum number; when reading a live capture, only onebufferful of packets is read at a time, so fewer thancntpackets may be processed. Acntof -1 processes all the packets received in one buffer when reading alive capture, or all the packets in the file when reading a``savefile''.callbackspecifies a routine to be called with three arguments:au_charpointer which is passed in frompcap_dispatch(),aconst struct pcap_pkthdrpointer to a structure with the following members:

ts
astruct timevalcontaining the time when the packet was captured
caplen
abpf_u_int32giving the number of bytes of the packet that are available from thecapture
len
abpf_u_int32giving the length of the packet, in bytes (which might be more than thenumber of bytes available from the capture, if the length of the packetis larger than the maximum number of bytes to capture)

and aconst u_charpointer to the packet data.

The number of packets read is returned.0 is returned if no packets were read from a live capture (if, forexample, they were discarded because they didn't pass the packet filter,or if, on platforms that support a read timeout that starts before anypackets arrive, the timeout expires before any packets arrive, or if thefile descriptor for the capture device is in non-blocking mode and nopackets were available to be read) or if no more packets are availablein a ``savefile.'' A return of -1 indicatesan error in which casepcap_perror()orpcap_geterr()may be used to display the error text.

NOTE:when reading a live capture,pcap_dispatch()will not necessarily return when the read times out; on some platforms,the read timeout isn't supported, and, on other platforms, the timerdoesn't start until at least one packet arrives. This means that theread timeout shouldNOTbe used in, for example, an interactive application, to allow the packetcapture loop to ``poll'' for user input periodically, as there's noguarantee thatpcap_dispatch()will return after the timeout expires.

pcap_loop()is similar topcap_dispatch()except it keeps reading packets untilcntpackets are processed or an error occurs.It doesnotreturn when live read timeouts occur.Rather, specifying a non-zero read timeout topcap_open_live()and then callingpcap_dispatch()allows the reception and processing of any packets that arrive when thetimeout occurs.A negativecntcausespcap_loop()to loop forever (or at least until an error occurs). A negative numberis returned on an error; 0 is returned ifcntis exhausted.

pcap_next()reads the next packet (by callingpcap_dispatch()with acntof 1) and returns au_charpointer to the data in that packet. (Thepcap_pkthdrstruct for that packet is not supplied.)

pcap_dump()outputs a packet to the ``savefile'' opened withpcap_dump_open().Note that its calling arguments are suitable for use withpcap_dispatch()orpcap_loop().If called directly, the userparameter is of type pcap_dumper_tas returned bypcap_dump_open().

pcap_compile()is used to compile the stringstrinto a filter program.programis a pointer to abpf_programstruct and is filled in bypcap_compile().optimizecontrols whether optimization on the resulting code is performed.netmaskspecifies the netmask of the local net.A return of -1 indicates an error in which casepcap_geterr()may be used to display the error text.

pcap_compile_nopcap()is similar topcap_compile()except that instead of passing a pcap structure, one passes thesnaplen and linktype explicitly. It is intended to be used forcompiling filters for direct BPF usage, without necessarily havingcalledpcap_open().A return of -1 indicates an error; the error text is unavailable.(pcap_compile_nopcap()is a wrapper aroundpcap_open_dead(),pcap_compile(),andpcap_close();the latter three routines can be used directly in order to get the errortext for a compilation error.)

pcap_setfilter()is used to specify a filter program.fpis a pointer to abpf_programstruct, usually the result of a call topcap_compile().-1is returned on failure, in which casepcap_geterr()may be used to display the error text;0is returned on success.

pcap_freecode()is used to free up allocated memory pointed to by abpf_programstruct generated bypcap_compile()when that BPF program is no longer needed, for example after ithas been made the filter program for a pcap structure by a call topcap_setfilter().

pcap_datalink()returns the link layer type; link layer types it can return include:

DLT_NULL
BSD loopback encapsulation; the link layer header is a 4-byte field, inhostbyte order, containing a PF_ value fromsocket.hfor the network-layer protocol of the packet
Note that ``host byte order'' is the byte order of the machine on whichthe packets are captured, and the PF_ values are for the OS of themachine on which the packets are captured; if a live capture is beingdone, ``host byte order'' is the byte order of the machine capturing thepackets, and the PF_ values are those of the OS of the machine capturingthe packets, but if a ``savefile'' is being read, the byte order and PF_values arenotnecessarily those of the machine reading the capture file.
DLT_EN10MB
Ethernet (10Mb, 100Mb, 1000Mb, and up)
DLT_IEEE802
IEEE 802.5 Token Ring
DLT_ARCNET
ARCNET
DLT_SLIP
SLIP; the link layer header contains, in order:

a 1-byte flag, which is 0 for packets received by the machine and 1 forpackets sent by the machine;

a 1-byte field, the upper 4 bits of which indicate the type of packet,as per RFC 1144:

0x40
an unmodified IP datagram (TYPE_IP);
0x70
an uncompressed-TCP IP datagram (UNCOMPRESSED_TCP), with that byte beingthe first byte of the raw IP header on the wire, containing theconnection number in the protocol field;
0x80
a compressed-TCP IP datagram (COMPRESSED_TCP), with that byte being thefirst byte of the compressed TCP/IP datagram header;

for UNCOMPRESSED_TCP, the rest of the modified IP header, and forCOMPRESSED_TCP, the compressed TCP/IP datagram header;

for a total of 16 bytes; the uncompressed IP datagram follows the header

DLT_PPP
PPP; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC-likeframing, with the PPP header following those two bytes, otherwise it'sPPP without framing, and the packet begins with the PPP header
DLT_FDDI
FDDI
DLT_ATM_RFC1483
RFC 1483 LLC/SNAP-encapsulated ATM; the packet begins with an IEEE 802.2LLC header
DLT_RAW
raw IP; the packet begins with an IP header
DLT_PPP_SERIAL
PPP in HDLC-like framing, as per RFC 1662, or Cisco PPP with HDLCframing, as per section 4.3.1 of RFC 1547; the first byte will be 0xFFfor PPP in HDLC-like framing, and will be 0x0F or 0x8F for Cisco PPPwith HDLC framing
DLT_PPP_ETHER
PPPoE; the packet begins with a PPPoE header, as per RFC 2516
DLT_C_HDLC
Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547
DLT_IEEE802_11
IEEE 802.11 wireless LAN
DLT_LOOP
OpenBSD loopback encapsulation; the link layer header is a 4-byte field, innetworkbyte order, containing a PF_ value from OpenBSD'ssocket.hfor the network-layer protocol of the packet
Note that, if a ``savefile'' is being read, those PF_ values arenotnecessarily those of the machine reading the capture file.
DLT_LINUX_SLL
Linux "cooked" capture encapsulation; the link layer header contains, inorder:

a 2-byte "packet type", in network byte order, which is one of:

0
packet was sent to us by somebody else
1
packet was broadcast by somebody else
2
packet was multicast, but not broadcast, by somebody else
3
packet was sent by somebody else to somebody else
4
packet was sent by us

a 2-byte field, in network byte order, containing a Linux ARPHRD_ valuefor the link layer device type;

a 2-byte field, in network byte order, containing the length of thelink layer address of the sender of the packet (which could be 0);

an 8-byte field containing that number of bytes of the link layer header(if there are more than 8 bytes, only the first 8 are present);

a 2-byte field containing an Ethernet protocol type, in network byteorder, or containing 1 for Novell 802.3 frames without an 802.2 LLCheader or 4 for frames beginning with an 802.2 LLC header.

DLT_LTALK
Apple LocalTalk; the packet begins with an AppleTalk LLAP header

pcap_snapshot()returns the snapshot length specified whenpcap_open_livewas called.

pcap_is_swapped()returns true if the current ``savefile'' uses a different byte orderthan the current system.

pcap_major_version()returns the major number of the version of the pcap used to write thesavefile.

pcap_minor_version()returns the minor number of the version of the pcap used to write thesavefile.

pcap_file()returns the standard I/O stream of the ``savefile,'' if a ``savefile''was opened withpcap_open_offline(),or NULL, if a network device was opened withpcap_open_live().

pcap_stats()returns 0 and fills in apcap_statstruct. The values represent packet statistics from the start of therun to the time of the call. If there is an error or the underlyingpacket capture doesn't support packet statistics, -1 is returned andthe error text can be obtained withpcap_perror()orpcap_geterr().pcap_stats()is supported only on live captures, not on ``savefiles''; no statisticsare stored in ``savefiles'', so no statistics are available when readingfrom a ``savefile''.

pcap_fileno()returns the file descriptor number from which captured packets are read,if a network device was opened withpcap_open_live(),or -1, if a ``savefile'' was opened withpcap_open_offline().

pcap_perror()prints the text of the last pcap library error onstderr,prefixed byprefix.

pcap_geterr()returns the error text pertaining to the last pcap library error.NOTE:the pointer it returns will no longer point to a valid error messagestring after thepcap_tpassed to it is closed; you must use or copy the string before closingthepcap_t.

pcap_strerror()is provided in casestrerror(1)isn't available.

pcap_close()closes the files associated withpand deallocates resources.

pcap_dump_close()closes the ``savefile.''

 

SEE ALSO

tcpdump(1), tcpslice(1) 

AUTHORS

The original authors are:

Van Jacobson,Craig Leres andSteven McCanne, all of theLawrence 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/
 

BUGS

Please send problems, bugs, questions, desirable enhancements, etc. to:

tcpdump-workersAATTtcpdump.org

Please send source code contributions, etc. to:

patchesAATTtcpdump.org


 

Index

NAME
SYNOPSIS
DESCRIPTION
ROUTINES
SEE ALSO
AUTHORS
BUGS

This document was created byman2html,using the manual pages.