NAME
pcap - Packet Capture library
SYNOPSIS
##iinncclluuddee <
cchhaarr eerrrrbbuuff[[PPCCAAPPEERRRRBBUUFFSSIIZZEE]];; ppccaapptt **ppccaappooppeennlliivvee((ccoonnsstt cchhaarr **ddeevviiccee,, iinntt ssnnaapplleenn,, iinntt pprroommiisscc,, iinntt ttoommss,, cchhaarr **eerrrrbbuuff)) ppccaapptt **ppccaappooppeennddeeaadd((iinntt lliinnkkttyyppee,, iinntt ssnnaapplleenn)) ppccaapptt **ppccaappooppeennoofffflliinnee((ccoonnsstt cchhaarr **ffnnaammee,, cchhaarr **eerrrrbbuuff)) ppccaapptt **ppccaappffooppeennoofffflliinnee((FFIILLEE **ffpp,, cchhaarr **eerrrrbbuuff)) ppccaappdduummppeerrtt **ppccaappdduummppooppeenn((ppccaapptt **pp,, ccoonnsstt cchhaarr **ffnnaammee)) ppccaappdduummppeerrtt **ppccaappdduummppffooppeenn((ppccaapptt **pp,, FFIILLEE **ffpp)) iinntt ppccaappsseettnnoonnbblloocckk((ppccaapptt **pp,, iinntt nnoonnbblloocckk,, cchhaarr **eerrrrbbuuff));; iinntt ppccaappggeettnnoonnbblloocckk((ppccaapptt **pp,, cchhaarr **eerrrrbbuuff));; iinntt ppccaappffiinnddaallllddeevvss((ppccaappiifftt ****aallllddeevvsspp,, cchhaarr **eerrrrbbuuff)) vvooiidd ppccaappffrreeeeaallllddeevvss((ppccaappiifftt **aallllddeevvss)) cchhaarr **ppccaappllooookkuuppddeevv((cchhaarr **eerrrrbbuuff)) iinntt ppccaappllooookkuuppnneett((ccoonnsstt cchhaarr **ddeevviiccee,, bbppffuuiinntt3322 **nneettpp,, bbppffuuiinntt3322 **mmaasskkpp,, cchhaarr **eerrrrbbuuff)) ttyyppeeddeeff vvooiidd ((**ppccaapphhaannddlleerr))((uucchhaarr **uusseerr,, ccoonnsstt ssttrruucctt ppccaappppkktthhddrr **hh,, ccoonnsstt uucchhaarr **bbyytteess));; iinntt ppccaappddiissppaattcchh((ppccaapptt **pp,, iinntt ccnntt,, ppccaapphhaannddlleerr ccaallllbbaacckk,, uucchhaarr **uusseerr)) iinntt ppccaapplloooopp((ppccaapptt **pp,, iinntt ccnntt,, ppccaapphhaannddlleerr ccaallllbbaacckk,, uucchhaarr **uusseerr)) vvooiidd ppccaappdduummpp((uucchhaarr **uusseerr,, ssttrruucctt ppccaappppkktthhddrr **hh,, uucchhaarr **sspp)) iinntt ppccaappccoommppiillee((ppccaapptt **pp,, ssttrruucctt bbppffpprrooggrraamm **ffpp,, cchhaarr **ssttrr,, iinntt ooppttiimmiizzee,, bbppffuuiinntt3322 nneettmmaasskk)) iinntt ppccaappsseettffiilltteerr((ppccaapptt **pp,, ssttrruucctt bbppffpprrooggrraamm **ffpp)) vvooiidd ppccaappffrreeeeccooddee((ssttrruucctt bbppffpprrooggrraamm **)) iinntt ppccaappsseettddiirreeccttiioonn((ppccaapptt **pp,, ppccaappddiirreeccttiioonntt dd)) ccoonnsstt uucchhaarr **ppccaappnneexxtt((ppccaapptt **pp,, ssttrruucctt ppccaappppkktthhddrr **hh)) iinntt ppccaappnneexxtteexx((ppccaapptt **pp,, ssttrruucctt ppccaappppkktthhddrr ****ppkktthheeaaddeerr,, ccoonnsstt uucchhaarr ****ppkkttddaattaa)) vvooiidd ppccaappbbrreeaakklloooopp((ppccaapptt **)) iinntt ppccaappiinnjjeecctt((ppccaapptt **pp,, ccoonnsstt vvooiidd **bbuuff,, ssiizzeett ssiizzee)) iinntt ppccaappsseennddppaacckkeett((ppccaapptt **pp,, ccoonnsstt uucchhaarr **bbuuff,, iinntt ssiizzee)) iinntt ppccaappddaattaalliinnkk((ppccaapptt **pp)) iinntt ppccaapplliissttddaattaalliinnkkss((ppccaapptt **pp,, iinntt ****ddllttbbuuff));; iinntt ppccaappsseettddaattaalliinnkk((ppccaapptt **pp,, iinntt ddlltt));; iinntt ppccaappddaattaalliinnkknnaammeettoovvaall((ccoonnsstt cchhaarr **nnaammee));; ccoonnsstt cchhaarr **ppccaappddaattaalliinnkkvvaallttoonnaammee((iinntt ddlltt));; ccoonnsstt cchhaarr **ppccaappddaattaalliinnkkvvaallttooddeessccrriippttiioonn((iinntt ddlltt));; iinntt ppccaappssnnaappsshhoott((ppccaapptt **pp)) iinntt ppccaappiisssswwaappppeedd((ppccaapptt **pp)) iinntt ppccaappmmaajjoorrvveerrssiioonn((ppccaapptt **pp)) iinntt ppccaappmmiinnoorrvveerrssiioonn((ppccaapptt **pp)) iinntt ppccaappssttaattss((ppccaapptt **pp,, ssttrruucctt ppccaappssttaatt **ppss)) FFIILLEE **ppccaappffiillee((ppccaapptt **pp)) iinntt ppccaappffiilleennoo((ppccaapptt **pp)) iinntt ppccaappggeettsseelleeccttaabblleeffdd((ppccaapptt **pp));; vvooiidd ppccaappppeerrrroorr((ppccaapptt **pp,, cchhaarr **pprreeffiixx)) cchhaarr **ppccaappggeetteerrrr((ppccaapptt **pp)) cchhaarr **ppccaappssttrreerrrroorr((iinntt eerrrroorr)) ccoonnsstt cchhaarr **ppccaapplliibbvveerrssiioonn((vvooiidd)) vvooiidd ppccaappcclloossee((ppccaapptt **pp)) iinntt ppccaappdduummppfflluusshh((ppccaappdduummppeerrtt **pp)) lloonngg ppccaappdduummppfftteellll((ppccaappdduummppeerrtt **pp)) FFIILLEE **ppccaappdduummppffiillee((ppccaappdduummppeerrtt **pp)) vvooiidd ppccaappdduummppcclloossee((ppccaappdduummppeerrtt **pp))> DESCRIPTION
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. RROOUUTTIINNEESSNOTE: errbuf in ppccaappooppeennlliivvee(()), ppccaappooppeennddeeaadd(()),
ppccaappooppeennoofffflliinnee(()), ppccaappffooppeennoofffflliinnee(()), ppccaappsseettnnoonnbblloocckk(()), ppccaappggeettnnoonnbblloocckk(()), ppccaappffiinnddaallllddeevvss(()), ppccaappllooookkuuppddeevv(()), and ppccaappllooookkuuppnneett(()) is assumed to be able to hold at least PPCCAAPPEERRRRBBUUFFSSIIZZEE chars. ppccaappooppeennlliivvee(()) is used to obtain a packet capture descriptor to lookat packets on the network. device is a string that specifies the net-
work device to open; on Linux systems with 2.2 or later kernels, a device argument of "any" or NNUULLLL can be used to capture packets fromall interfaces. snaplen specifies the maximum number of bytes to cap-
ture. If this value is less than the size of a packet that is cap-
tured, only the first snaplen bytes of that packet will be captured and provided as packet data. A value of 65535 should be sufficient, on most if not all networks, to capture all the data available from thepacket. promisc specifies if the interface is to be put into promiscu-
ous mode. (Note that even if this parameter is false, the interface could well be in promiscuous mode for some other reason.) For now, this doesn't work on the "any" device; if an argument of "any" or NULL is supplied, the promisc flag is ignored. toms specifies the read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily return immediately when a packet is seen, but that it wait for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. Not all platforms support a read timeout; on platforms that don't, the read timeout is ignored. A zero value for toms, on platforms that supporta read timeout, will cause a read to wait forever to allow enough pack-
ets to arrive, with no timeout. errbuf is used to return error or warning text. It will be set to error text when ppccaappooppeennlliivvee(()) fails and returns NNUULLLL. errbuf may also be set to warning text when ppccaappooppeennlliivvee(()) succeds; to detect this case the caller should store azero-length string in errbuf before calling ppccaappooppeennlliivvee(()) and dis-
play the warning to the user if errbuf is no longer a zero-length
string. ppccaappooppeennddeeaadd(()) is used for creating a ppccaapptt structure to use when calling the other functions in libpcap. It is typically used when just using libpcap for compiling BPF code. ppccaappooppeennoofffflliinnee(()) is called to open a ``savefile'' for reading. fname specifies the name of the file to open. The file has the sameformat as those used by ttccppdduummpp((11)) and ttccppsslliiccee((11)). The name "-" in a
synonym for ssttddiinn. Alternatively, you may call ppccaappffooppeennoofffflliinnee(()) toread dumped data from an existing open stream fp. Note that on Win-
dows, that stream should be opened in binary mode. errbuf is used to return error text and is only set when ppccaappooppeennoofffflliinnee(()) or ppccaappffooppeennoofffflliinnee(()) fails and returns NNUULLLL. ppccaappdduummppooppeenn(()) is called to open a ``savefile'' for writing. The name"-" in a synonym for ssttddoouutt. NNUULLLL is returned on failure. p is a pcap
struct as returned by ppccaappooppeennoofffflliinnee(()) or ppccaappooppeennlliivvee(()). fname specifies the name of the file to open. Alternatively, you may call ppccaappdduummppffooppeenn(()) to write data to an existing open stream fp. Note that on Windows, that stream should be opened in binary mode. If NNUULLLL is returned, ppccaappggeetteerrrr(()) can be used to get the error text. ppccaappsseettnnoonnbblloocckk(()) puts a capture descriptor, opened withppccaappooppeennlliivvee(()), into ``non-blocking'' mode, or takes it out of ``non-
blocking'' mode, depending on whether the nonblock argument is non-zero
or zero. It has no effect on ``savefiles''. If there is an error, -1
is returned and errbuf is filled in with an appropriate error message;otherwise, 0 is returned. In ``non-blocking'' mode, an attempt to read
from the capture descriptor with ppccaappddiissppaattcchh(()) will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. ppccaapplloooopp(()) and ppccaappnneexxtt(())will not work in ``non-blocking'' mode.
ppccaappggeettnnoonnbblloocckk(()) returns the current ``non-blocking'' state of the
capture descriptor; it always returns 0 on ``savefiles''. If there isan error, -1 is returned and errbuf is filled in with an appropriate
error message. ppccaappffiinnddaallllddeevvss(()) constructs a list of network devices that can be opened with ppccaappooppeennlliivvee(()). (Note that there may be network devices that cannot be opened with ppccaappooppeennlliivvee(()) by the process calling ppccaappffiinnddaallllddeevvss(()), because, for example, that process might not have sufficient privileges to open them for capturing; if so, those devices will not appear on the list.) alldevsp is set to point to the first element of the list; each element of the list is of type ppccaappiifftt, and has the following members: nneexxtt if not NNUULLLL, a pointer to the next element in the list; NNUULLLL for the last element of the list nnaammee a pointer to a string giving a name for the device to pass to ppccaappooppeennlliivvee(()) ddeessccrriippttiioonnif not NNUULLLL, a pointer to a string giving a human-read-
able description of the device aaddddrreesssseess a pointer to the first element of a list of addresses for the interface ffllaaggss interface flags: PPCCAAPPIIFFLLOOOOPPBBAACCKK set if the interface is a loopback interface Each element of the list of addresses is of type ppccaappaaddddrrtt, and has the following members: nneexxtt if not NNUULLLL, a pointer to the next element in the list; NNUULLLL for the last element of the list aaddddrr a pointer to a ssttrruucctt ssoocckkaaddddrr containing an address nneettmmaasskk if not NNUULLLL, a pointer to a ssttrruucctt ssoocckkaaddddrr that contains the netmask corresponding to the address pointed to by aaddddrr bbrrooaaddaaddddrr if not NNUULLLL, a pointer to a ssttrruucctt ssoocckkaaddddrr that contains the broadcast address corresponding to the address pointed to by aaddddrr; may be null if the interface doesn't support broadcasts ddssttaaddddrr if not NNUULLLL, a pointer to a ssttrruucctt ssoocckkaaddddrr that contains the destination address corresponding to the address pointed to by aaddddrr; may be null if the interface isn't apoint-to-point interface
Note that not all the addresses in the list of addresses are necessar-
ily IPv4 or IPv6 addresses - you must check the ssaaffaammiillyy member of the
ssttrruucctt ssoocckkaaddddrr before interpreting the contents of the address.-11 is returned on failure, in which case eerrrrbbuuff is filled in with an
appropriate error message; 00 is returned on success.ppccaappffrreeeeaallllddeevvss(()) is used to free a list allocated by ppccaappffiinndd-
aallllddeevvss(()). ppccaappllooookkuuppddeevv(()) returns a pointer to a network device suitable for use with ppccaappooppeennlliivvee(()) and ppccaappllooookkuuppnneett(()). If there is an error, NNUULLLL is returned and errbuf is filled in with an appropriate error message.ppccaappllooookkuuppnneett(()) is used to determine the network number and mask asso-
ciated with the network device ddeevviiccee. Both netp and maskp arebpfuint32 pointers. A return of -1 indicates an error in which case
errbuf is filled in with an appropriate error message. ppccaappddiissppaattcchh(()) is used to collect and process packets. cnt specifies the maximum number of packets to process before returning. This is not a minimum number; when reading a live capture, only one bufferful of packets is read at a time, so fewer than cnt packets may be processed.A cnt of -1 processes all the packets received in one buffer when read-
ing a live capture, or all the packets in the file when reading a ``savefile''. callback specifies a routine to be called with three arguments: a uchar pointer which is passed in from ppccaappddiissppaattcchh(()), aconst struct pcappkthdr pointer to a structure with the following mem-
bers: ttss a struct timeval containing the time when the packet was captured ccaapplleenn a bpfuint32 giving the number of bytes of the packet that are available from the capture lleenn a bpfuint32 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) and a const uchar pointer to the first ccaapplleenn (as given in the struct pcappkthdr a pointer to which is passed to the callback routine) bytes of data from the packet (which 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 ppccaappooppeennlliivvee(()) that is sufficiently large toget all of the packet's data - a value of 65535 should be sufficient on
most if not all networks). The number of packets read is returned. 0 is returned if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms thatsupport a read timeout that starts before any packets arrive, the time-
out expires before any packets arrive, or if the file descriptor forthe capture device is in non-blocking mode and no packets were avail-
able to be read) or if no more packets are available in a ``savefile.''A return of -1 indicates an error in which case ppccaappppeerrrroorr(()) or
ppccaappggeetteerrrr(()) may be used to display the error text. A return of -2
indicates that the loop terminated due to a call to ppccaappbbrreeaakklloooopp(()) before any packets were processed. IIff yyoouurr aapppplliiccaattiioonn uusseessppccaappbbrreeaakklloooopp(()),, mmaakkee ssuurree tthhaatt yyoouu eexxpplliicciittllyy cchheecckk ffoorr -11 aanndd -22,,
rraatthheerr tthhaann jjuusstt cchheecckkiinngg ffoorr aa rreettuurrnn vvaalluuee << 00.. NNOOTTEE: when reading a live capture, ppccaappddiissppaattcchh(()) will not necessarily return when the read times out; on some platforms, the read timeout isn't supported, and, on other platforms, the timer doesn't start until at least one packet arrives. This means that the read timeout should NNOOTT be used in, for example, an interactive application, to allow the packet capture loop to ``poll'' for user input periodically, as there's no guarantee that ppccaappddiissppaattcchh(()) will return after the timeout expires.ppccaapplloooopp(()) is similar to ppccaappddiissppaattcchh(()) except it keeps reading pack-
ets until cnt packets are processed or an error occurs. It does nnoottreturn when live read timeouts occur. Rather, specifying a non-zero
read timeout to ppccaappooppeennlliivvee(()) and then calling ppccaappddiissppaattcchh(()) allows the reception and processing of any packets that arrive when the timeout occurs. A negative cnt causes ppccaapplloooopp(()) to loop forever (orat least until an error occurs). -1 is returned on an error; 0 is
returned if cnt is exhausted; -2 is returned if the loop terminated due
to a call to ppccaappbbrreeaakklloooopp(()) before any packets were processed. IIff yyoouurr aapppplliiccaattiioonn uusseess ppccaappbbrreeaakklloooopp(()),, mmaakkee ssuurree tthhaatt yyoouu eexxpplliicciittllyycchheecckk ffoorr -11 aanndd -22,, rraatthheerr tthhaann jjuusstt cchheecckkiinngg ffoorr aa rreettuurrnn vvaalluuee << 00..
ppccaappnneexxtt(()) reads the next packet (by calling ppccaappddiissppaattcchh(()) with a cnt of 1) and returns a uchar pointer to the data in that packet. (The pcappkthdr struct for that packet is not supplied.) NNUULLLL is returned if an error occured, or if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms that support a read timeout thatstarts before any packets arrive, the timeout expires before any pack-
ets arrive, or if the file descriptor for the capture device is in non-
blocking mode and no packets were available to be read), or if no more packets are available in a ``savefile.'' Unfortunately, there is no way to determine whether an error occured or not. ppccaappnneexxtteexx(()) reads the next packet and returns a success/failure indication: 1 the packet was read without problems0 packets are being read from a live capture, and the time-
out expired-1 an error occurred while reading the packet
-2 packets are being read from a ``savefile'', and there are
no more packets to read from the savefile. If the packet was read without problems, the pointer pointed to by the pktheader argument is set to point to the pcappkthdr struct for the packet, and the pointer pointed to by the pktdata argument is set to point to the data in the packet. ppccaappbbrreeaakklloooopp(()) sets a flag that will force ppccaappddiissppaattcchh(()) or ppccaapplloooopp(()) to return rather than looping; they will return the numberof packets that have been processed so far, or -2 if no packets have
been processed so far.This routine is safe to use inside a signal handler on UNIX or a con-
sole control handler on Windows, as it merely sets a flag that is checked within the loop.The flag is checked in loops reading packets from the OS - a signal by
itself will not necessarily terminate those loops - as well as in loops
processing a set of packets returned by the OS. NNoottee tthhaatt iiff yyoouu aarree ccaattcchhiinngg ssiiggnnaallss oonn UUNNIIXX ssyysstteemmss tthhaatt ssuuppppoorrtt rreessttaarrttiinngg ssyysstteemm ccaallllss aafftteerr aa ssiiggnnaall,, aanndd ccaalllliinngg ppccaappbbrreeaakklloooopp(()) iinn tthhee ssiiggnnaall hhaannddlleerr,, yyoouu mmuusstt ssppeecciiffyy,, wwhheenn ccaattcchhiinngg tthhoossee ssiiggnnaallss,, tthhaatt ssyysstteemm ccaallllss sshhoouulldd NNOOTT bbee rreessttaarrtteedd bbyy tthhaatt ssiiggnnaall.. OOtthheerrwwiissee,, iiff tthhee ssiiggnnaall iinntteerrrruupptteedd aa ccaallll rreeaaddiinngg ppaacckkeettss iinn aa lliivvee ccaappttuurree,, wwhheenn yyoouurr ssiiggnnaall hhaannddlleerr rreettuurrnnss aafftteerr ccaalllliinngg ppccaappbbrreeaakklloooopp(()),, tthhee ccaallll wwiillll bbee rreessttaarrtteedd,, aannddtthhee lloooopp wwiillll nnoott tteerrmmiinnaattee uunnttiill mmoorree ppaacckkeettss aarrrriivvee aanndd tthhee ccaallll ccoomm-
pplleetteess..Note also that, in a multi-threaded application, if one thread is
blocked in ppccaappddiissppaattcchh(()), ppccaapplloooopp(()), ppccaappnneexxtt(()), or ppccaappnneexxtteexx(()), a call to ppccaappbbrreeaakklloooopp(()) in a different thread will not unblock that thread; you will need to use whatever mechanism the OS provides for breaking a thread out of blocking calls in order to unblock the thread, such as thread cancellation in systems that support POSIX threads. Note that ppccaappnneexxtt(()) will, on some platforms, loop reading packets from the OS; that loop will not necessarily be terminated by a signal, so ppccaappbbrreeaakklloooopp(()) should be used to terminate packet processing even if ppccaappnneexxtt(()) is being used. ppccaappbbrreeaakklloooopp(()) does not guarantee that no further packets will be processed by ppccaappddiissppaattcchh(()) or ppccaapplloooopp(()) after it is called; at most one more packet might be processed.If -2 is returned from ppccaappddiissppaattcchh(()) or ppccaapplloooopp(()), the flag is
cleared, so a subsequent call will resume reading packets. If a posi-
tive number is returned, the flag is not cleared, so a subsequent callwill return -2 and clear the flag.
ppccaappiinnjjeecctt(()) sends a raw packet through the network interface; bufpoints to the data of the packet, including the link-layer header, and
size is the number of bytes in the packet. It returns the number ofbytes written on success. A return of -1 indicates an error in which
case ppccaappppeerrrroorr(()) or ppccaappggeetteerrrr(()) may be used to display the error text. Note that, even if you successfully open the network interface, you might not have permission to send packets on it, or it might not support sending packets; as pcapopenlive() doesn't have a flag toindicate whether to open for capturing, sending, or capturing and send-
ing, you cannot request an open that supports sending and be notified at open time whether sending will be possible. Note also that some devices might not support sending packets.Note that, on some platforms, the link-layer header of the packet
that's sent might not be the same as the link-layer header of the
packet supplied to ppccaappiinnjjeecctt(()), as the source link-layer address, if
the header contains such an address, might be changed to be the address assigned to the interface on which the packet it sent, if the platform doesn't support sending completely raw and unchanged packets. Evenworse, some drivers on some platforms might change the link-layer type
field to whatever value libpcap used when attaching to the device, even on platforms that do nominally support sending completely raw and unchanged packets. ppccaappsseennddppaacckkeett(()) is like ppccaappiinnjjeecctt(()), but it returns 0 on successand -1 on failure. (ppccaappiinnjjeecctt(()) comes from OpenBSD; ppccaappsseenndd-
ppaacckkeett(()) comes from WinPcap. Both are provided for compatibility.) ppccaappdduummpp(()) outputs a packet to the ``savefile'' opened with ppccaappdduummppooppeenn(()). Note that its calling arguments are suitable for use with ppccaappddiissppaattcchh(()) or ppccaapplloooopp(()). If called directly, the user parameter is of type pcapdumpert as returned by ppccaappdduummppooppeenn(()). ppccaappccoommppiillee(()) is used to compile the string str into a filter program. program is a pointer to a bpfprogram struct and is filled in byppccaappccoommppiillee(()). optimize controls whether optimization on the result-
ing code is performed. netmask specifies the IPv4 netmask of the net-
work on which packets are being captured; it is used only when checking for IPv4 broadcast addresses in the filter program. If the netmask ofthe network on which packets are being captured isn't known to the pro-
gram, or if packets are being captured on the Linux "any" pseudo-inter-
face that can capture on more than one network, a value of 0 can be supplied; tests for IPv4 broadcast addreses won't be done correctly,but all other tests in the filter program will be OK. A return of -1
indicates an error in which case ppccaappggeetteerrrr(()) may be used to display the error text. ppccaappccoommppiilleennooppccaapp(()) is similar to ppccaappccoommppiillee(()) except that instead of passing a pcap structure, one passes the snaplen and linktype explicitly. It is intended to be used for compiling filters for direct BPF usage, without necessarily having called ppccaappooppeenn(()). A return of-1 indicates an error; the error text is unavailable. (ppccaappccoomm-
ppiilleennooppccaapp(()) is a wrapper around ppccaappooppeennddeeaadd(()), ppccaappccoommppiillee(()), and ppccaappcclloossee(()); the latter three routines can be used directly in order to get the error text for a compilation error.) ppccaappsseettffiilltteerr(()) is used to specify a filter program. fp is a pointerto a bpfprogram struct, usually the result of a call to ppccaappccoomm-
ppiillee(()). -11 is returned on failure, in which case ppccaappggeetteerrrr(()) may be
used to display the error text; 00 is returned on success. ppccaappffrreeeeccooddee(()) is used to free up allocated memory pointed to by a bpfprogram struct generated by ppccaappccoommppiillee(()) when that BPF program is no longer needed, for example after it has been made the filter program for a pcap structure by a call to ppccaappsseettffiilltteerr(()). ppccaappsseettddiirreeccttiioonn(()) is used to specify a direction that packets will be captured. pcapdirectiont is one of the constants PPCCAAPPDDIINN, PPCCAAPPDDOOUUTT or PPCCAAPPDDIINNOOUUTT. PPCCAAPPDDIINN will only capture packets received by the device, PPCCAAPPDDOOUUTT will only capture packets sent by the device and PPCCAAPPDDIINNOOUUTT will capture packets received by or sent by the device. PPCCAAPPDDIINNOOUUTT is the default setting if this function is not called. This isn't necessarily supported on all platforms; some platforms might return an error, and some other platforms might not support PPCCAAPPDDOOUUTT. This operation is not supported if a ``savefile''is being read. -11 is returned on failure, 00 is returned on success.
ppccaappddaattaalliinnkk(()) returns the link layer type; link layer types it can return include: DDLLTTNNUULLLLBSD loopback encapsulation; the link layer header is a 4-byte
field, in host byte order, containing a PF value fromssoocckkeett..hh for the network-layer protocol of the packet.
Note that ``host byte order'' is the byte order of the machine on which the packets are captured, and the PF valuesare for the OS of the machine on which the packets are cap-
tured; if a live capture is being done, ``host byte order'' is the byte order of the machine capturing the packets, and the PF values are those of the OS of the machine capturing the packets, but if a ``savefile'' is being read, the byte order and PF values are not necessarily those of the machine reading the capture file. DDLLTTEENN1100MMBB Ethernet (10Mb, 100Mb, 1000Mb, and up) DDLLTTIIEEEEEE880022 IEEE 802.5 Token Ring DDLLTTAARRCCNNEETT ARCNET DDLLTTSSLLIIPP SLIP; the link layer header contains, in order:a 1-byte flag, which is 0 for packets received by the
machine and 1 for packets 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 (TYPEIP);0x70 an uncompressed-TCP IP datagram (UNCOM-
PRESSEDTCP), with that byte being the firstbyte of the raw IP header on the wire, con-
taining the connection number in the protocol field;0x80 a compressed-TCP IP datagram (COMPRESSEDTCP),
with that byte being the first byte of the compressed TCP/IP datagram header; for UNCOMPRESSEDTCP, the rest of the modified IP header, and for COMPRESSEDTCP, the compressed TCP/IP datagram header; for a total of 16 bytes; the uncompressed IP datagram follows the header. DDLLTTPPPPPP PPP; if the first 2 bytes are 0xff and 0x03, it's PPP inHDLC-like framing, with the PPP header following those two
bytes, otherwise it's PPP without framing, and the packet begins with the PPP header. DDLLTTFFDDDDII FDDI DDLLTTAATTMMRRFFCC11448833RFC 1483 LLC/SNAP-encapsulated ATM; the packet begins with an
IEEE 802.2 LLC header. DDLLTTRRAAWW raw IP; the packet begins with an IP header. DDLLTTPPPPPPSSEERRIIAALLPPP in HDLC-like framing, as per RFC 1662, or Cisco PPP with
HDLC framing, as per section 4.3.1 of RFC 1547; the firstbyte will be 0xFF for PPP in HDLC-like framing, and will be
0x0F or 0x8F for Cisco PPP with HDLC framing. DDLLTTPPPPPPEETTHHEERR PPPoE; the packet begins with a PPPoE header, as per RFC 2516. DDLLTTCCHHDDLLCC Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547. DDLLTTIIEEEEEE8800221111 IEEE 802.11 wireless LAN DDLLTTFFRREELLAAYY Frame Relay DDLLTTLLOOOOPP OpenBSD loopback encapsulation; the link layer header is a4-byte field, in network byte order, containing a PF value
from OpenBSD's ssoocckkeett..hh for the network-layer protocol of the
packet. Note that, if a ``savefile'' is being read, those PF values are not necessarily those of the machine reading the capture file. DDLLTTLLIINNUUXXSSLLLL Linux "cooked" capture encapsulation; the link layer header contains, in order: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 usa 2-byte field, in network byte order, containing a
Linux ARPHRD value for the link layer device type;a 2-byte field, in network byte order, containing the
length of the link 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 byte order, or containing 1 for Novell 802.3 frames without an 802.2 LLC header or 4 for frames beginning with an 802.2 LLC header. DDLLTTLLTTAALLKK Apple LocalTalk; the packet begins with an AppleTalk LLAP header. DDLLTTPPFFLLOOGG OpenBSD pflog; the link layer header contains, in order:a 1-byte header length, in host byte order;
a 4-byte PF value, in host byte order;
a 2-byte action code, in network byte order, which is
one of: 0 passed 1 dropped 2 scrubbeda 2-byte reason code, in network byte order, which is
one of: 0 match 1 bad offset 2 fragment 3 short 4 normalize 5 memorya 16-character interface name;
a 16-character ruleset name (only meaningful if subrule
is set);a 4-byte rule number, in network byte order;
a 4-byte subrule number, in network byte order;
a 1-byte direction, in network byte order, which is one
of: 0 incoming or outgoing 1 incoming 2 outgoing DDLLTTPPRRIISSMMHHEEAADDEERR Prism monitor mode information followed by an 802.11 header. DDLLTTIIPPOOVVEERRFFCCRFC 2625 IP-over-Fibre Channel, with the link-layer header
being the NetworkHeader as described in that RFC. DDLLTTSSUUNNAATTMM SunATM devices; the link layer header contains, in order:a 1-byte flag field, containing a direction flag in the
uppermost bit, which is set for packets transmitted by the machine and clear for packets received by themachine, and a 4-byte traffic type in the low-order 4
bits, which is one of: 0 raw traffic 1 LANE traffic2 LLC-encapsulated traffic
3 MARS traffic 4 IFMP traffic 5 ILMI traffic 6 Q.2931 traffica 1-byte VPI value;
a 2-byte VCI field, in network byte order.
DDLLTTIIEEEEEE8800221111RRAADDIIOOlink-layer information followed by an 802.11 header - see
http://www.shaftnet.org/~pizza/software/capturefrm.txt for adescription of the link-layer information.
DDLLTTAARRCCNNEETTLLIINNUUXX ARCNET, with no exception frames, reassembled packets ratherthan raw frames, and an extra 16-bit offset field between the
destination host and type bytes. DDLLTTLLIINNUUXXIIRRDDAALinux-IrDA packets, with a DDLLTTLLIINNUUXXSSLLLL header followed by
the IrLAP header. DDLLTTLLIINNUUXXLLAAPPDD LAPD (Q.921) frames, with a DDLLTTLLIINNUUXXSSLLLL header captured via vISDN. ppccaapplliissttddaattaalliinnkkss(()) is used to get a list of the supported data link types of the interface associated with the pcap descriptor. ppccaapplliissttddaattaalliinnkkss(()) allocates an array to hold the list and sets*dltbuf. The caller is responsible for freeing the array. -11 is
returned on failure; otherwise, the number of data link types in the array is returned. ppccaappsseettddaattaalliinnkk(()) is used to set the current data link type of thepcap descriptor to the type specified by dlt. -11 is returned on fail-
ure. ppccaappddaattaalliinnkknnaammeettoovvaall(()) translates a data link type name, which is a DDLLTT name with the DDLLTT removed, to the corresponding data link typevalue. The translation is case-insensitive. -11 is returned on fail-
ure. ppccaappddaattaalliinnkkvvaallttoonnaammee(()) translates a data link type value to the corresponding data link type name. NULL is returned on failure. ppccaappddaattaalliinnkkvvaallttooddeessccrriippttiioonn(()) translates a data link type value toa short description of that data link type. NULL is returned on fail-
ure. ppccaappssnnaappsshhoott(()) returns the snapshot length specified when ppccaappooppeennlliivvee(()) was called.ppccaappiisssswwaappppeedd(()) returns true if the current ``savefile'' uses a dif-
ferent byte order than the current system. ppccaappmmaajjoorrvveerrssiioonn(()) returns the major number of the file format of the savefile; ppccaappmmiinnoorrvveerrssiioonn(()) returns the minor number of the file format of the savefile. The version number is stored in the header of the savefile. ppccaappffiillee(()) returns the standard I/O stream of the ``savefile,'' if a ``savefile'' was opened with ppccaappooppeennoofffflliinnee(()), or NULL, if a network device was opened with ppccaappooppeennlliivvee(()).ppccaappssttaattss(()) returns 0 and fills in a ppccaappssttaatt struct. The values rep-
resent packet statistics from the start of the run to the time of the call. If there is an error or the underlying packet capture doesn'tsupport packet statistics, -1 is returned and the error text can be
obtained with ppccaappppeerrrroorr(()) or ppccaappggeetteerrrr(()). ppccaappssttaattss(()) is sup-
ported only on live captures, not on ``savefiles''; no statistics are stored in ``savefiles'', so no statistics are available when reading from a ``savefile''. ppccaappffiilleennoo(()) returns the file descriptor number from which captured packets are read, if a network device was opened with ppccaappooppeennlliivvee(()),or -1, if a ``savefile'' was opened with ppccaappooppeennoofffflliinnee(()).
ppccaappggeettsseelleeccttaabblleeffdd(()) returns, on UNIX, a file descriptor number for a file descriptor on which one can do a sseelleecctt(()) or ppoollll(()) to wait forit to be possible to read packets without blocking, if such a descrip-
tor exists, or -1, if no such descriptor exists. Some network devices
opened with ppccaappooppeennlliivvee(()) do not support sseelleecctt(()) or ppoollll(()) (for example, regular network devices on FreeBSD 4.3 and 4.4, and Endace DAGdevices), so -1 is returned for those devices.
Note that on most versions of most BSDs (including Mac OS X) sseelleecctt(()) and ppoollll(()) do not work correctly on BPF devices; ppccaappggeettsseelleeccttaabblleeffdd(()) will return a file descriptor on most of those versions (the exceptions being FreeBSD 4.3 and 4.4), a simple sseelleecctt(()) or ppoollll(()) will not return even after a timeout specified in ppccaappooppeennlliivvee(()) expires. To work around this, an application that uses sseelleecctt(()) or ppoollll(()) to wait for packets to arrive must put theppccaapptt in non-blocking mode, and must arrange that the sseelleecctt(()) or
ppoollll(()) have a timeout less than or equal to the timeout specified in ppccaappooppeennlliivvee(()), and must try to read packets after that timeout expires, regardless of whether sseelleecctt(()) or ppoollll(()) indicated that thefile descriptor for the ppccaapptt is ready to be read or not. (That work-
around will not work in FreeBSD 4.3 and later; however, in FreeBSD 4.6 and later, sseelleecctt(()) and ppoollll(()) work correctly on BPF devices, so the workaround isn't necessary, although it does no harm.) ppccaappggeettsseelleeccttaabblleeffdd(()) is not available on Windows. ppccaappppeerrrroorr(()) prints the text of the last pcap library error on ssttddeerrrr, prefixed by prefix. ppccaappggeetteerrrr(()) returns the error text pertaining to the last pcap library error. NNOOTTEE: the pointer it returns will no longer point to a valid error message string after the ppccaapptt passed to it is closed; you must use or copy the string before closing the ppccaapptt. ppccaappssttrreerrrroorr(()) is provided in case ssttrreerrrroorr(1) isn't available. ppccaapplliibbvveerrssiioonn(()) returns a pointer to a string giving informationabout the version of the libpcap library being used; note that it con-
tains more information than just a version number. ppccaappcclloossee(()) closes the files associated with p and deallocates resources. ppccaappdduummppffiillee(()) returns the standard I/O stream of the ``savefile'' opened by ppccaappdduummppooppeenn(()).. ppccaappdduummppfflluusshh(()) flushes the output buffer to the ``savefile,'' so that any packets written with ppccaappdduummpp(()) but not yet written to the``savefile'' will be written. -11 is returned on error, 0 on success.
ppccaappdduummppfftteellll(()) returns the current file position for the ``save-
file'', representing the number of bytes written by ppccaappdduummppooppeenn(())and ppccaappdduummpp(()). -11 is returned on error.
ppccaappdduummppcclloossee(()) closes the ``savefile.''SEE ALSO
tcpdump(1), tcpslice(1) AUTHORS The original authors 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/BUGS
Please send problems, bugs, questions, desirable enhancements, etc. to:tcpdump-workers@tcpdump.org
Please send source code contributions, etc. to: patches@tcpdump.org 27 February 2004 PCAP(3)