blob: c6b8891fe12f36e73047af88657745e31ffa07d0 [file] [log] [blame]
Elliott Hughes965a4b52017-05-15 10:37:39 -07001.\" Copyright (c) 1994, 1996, 1997
2.\" The Regents of the University of California. All rights reserved.
3.\"
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.
19.\"
Haibo Huang165065a2018-07-23 17:26:52 -070020.TH PCAP 3PCAP "20 April 2018"
Elliott Hughes965a4b52017-05-15 10:37:39 -070021.SH NAME
22pcap \- Packet Capture library
23.SH SYNOPSIS
24.nf
25.ft B
26#include <pcap/pcap.h>
27.LP
28.ft B
29.ft
30.fi
31.SH DESCRIPTION
32The Packet Capture library
33provides a high level interface to packet capture systems. All packets
34on the network, even those destined for other hosts, are accessible
35through this mechanism.
36It also supports saving captured packets to a ``savefile'', and reading
37packets from a ``savefile''.
38.SS Opening a capture handle for reading
39To open a handle for a live capture, given the name of the network or
40other interface on which the capture should be done, call
41.BR pcap_create (),
42set the appropriate options on the handle, and then activate it with
43.BR pcap_activate ().
44.PP
45To obtain a list of devices that can be opened for a live capture, call
46.BR pcap_findalldevs ();
47to free the list returned by
48.BR pcap_findalldevs (),
49call
50.BR pcap_freealldevs ().
51.BR pcap_lookupdev ()
52will return the first device on that list that is not a ``loopback``
53network interface.
54.PP
55To open a handle for a ``savefile'' from which to read packets, given the
56pathname of the ``savefile'', call
57.BR pcap_open_offline ();
58to set up a handle for a ``savefile'', given a
59.B "FILE\ *"
60referring to a file already opened for reading, call
61.BR pcap_fopen_offline ().
62.PP
63In order to get a ``fake''
64.B pcap_t
65for use in routines that require a
66.B pcap_t
67as an argument, such as routines to open a ``savefile'' for writing and
68to compile a filter expression, call
69.BR pcap_open_dead ().
70.PP
71.BR pcap_create (),
72.BR pcap_open_offline (),
73.BR pcap_fopen_offline (),
74and
75.BR pcap_open_dead ()
76return a pointer to a
77.BR pcap_t ,
78which is the handle used for reading packets from the capture stream or
79the ``savefile'', and for finding out information about the capture
80stream or ``savefile''.
81To close a handle, use
82.BR pcap_close ().
83.PP
84The options that can be set on a capture handle include
85.IP "snapshot length"
86If, when capturing, you capture the entire contents of the packet, that
87requires more CPU time to copy the packet to your application, more disk
88and possibly network bandwidth to write the packet data to a file, and
89more disk space to save the packet. If you don't need the entire
90contents of the packet - for example, if you are only interested in the
91TCP headers of packets - you can set the "snapshot length" for the
92capture to an appropriate value. If the snapshot length is set to
93.IR snaplen ,
94and
95.I snaplen
96is less
97than the size of a packet that is captured, only the first
98.I snaplen
99bytes of that packet will be captured and provided as packet data.
100.IP
101A snapshot length of 65535 should be sufficient, on most if not all
102networks, to capture all the data available from the packet.
103.IP
104The snapshot length is set with
105.BR pcap_set_snaplen ().
106.IP "promiscuous mode"
107On broadcast LANs such as Ethernet, if the network isn't switched, or if
108the adapter is connected to a "mirror port" on a switch to which all
109packets passing through the switch are sent, a network adapter receives
110all packets on the LAN, including unicast or multicast packets not sent
111to a network address that the network adapter isn't configured to
112recognize.
113.IP
114Normally, the adapter will discard those packets; however, many network
115adapters support "promiscuous mode", which is a mode in which all
116packets, even if they are not sent to an address that the adapter
117recognizes, are provided to the host. This is useful for passively
118capturing traffic between two or more other hosts for analysis.
119.IP
120Note that even if an application does not set promiscuous mode, the
121adapter could well be in promiscuous mode for some other reason.
122.IP
123For now, this doesn't work on the "any" device; if an argument of "any"
124or NULL is supplied, the setting of promiscuous mode is ignored.
125.IP
126Promiscuous mode is set with
127.BR pcap_set_promisc ().
128.IP "monitor mode"
129On IEEE 802.11 wireless LANs, even if an adapter is in promiscuous mode,
130it will supply to the host only frames for the network with which it's
131associated. It might also supply only data frames, not management or
132control frames, and might not provide the 802.11 header or radio
133information pseudo-header for those frames.
134.IP
135In "monitor mode", sometimes also called "rfmon mode" (for "Radio
136Frequency MONitor"), the adapter will supply all frames that it
137receives, with 802.11 headers, and might supply a pseudo-header with
138radio information about the frame as well.
139.IP
140Note that in monitor mode the adapter might disassociate from the
141network with which it's associated, so that you will not be able to use
142any wireless networks with that adapter. This could prevent accessing
143files on a network server, or resolving host names or network addresses,
144if you are capturing in monitor mode and are not connected to another
145network with another adapter.
146.IP
147Monitor mode is set with
148.BR pcap_set_rfmon (),
149and
150.BR pcap_can_set_rfmon ()
151can be used to determine whether an adapter can be put into monitor
152mode.
Haibo Huang165065a2018-07-23 17:26:52 -0700153.IP "packet buffer timeout"
Elliott Hughes965a4b52017-05-15 10:37:39 -0700154If, when capturing, packets are delivered as soon as they arrive, the
155application capturing the packets will be woken up for each packet as it
156arrives, and might have to make one or more calls to the operating
157system to fetch each packet.
158.IP
159If, instead, packets are not delivered as soon as they arrive, but are
Haibo Huang165065a2018-07-23 17:26:52 -0700160delivered after a short delay (called a "packet buffer timeout"), more
161than one packet can be accumulated before the packets are delivered, so
162that a single wakeup would be done for multiple packets, and each set of
163calls made to the operating system would supply multiple packets, rather
164than a single packet. This reduces the per-packet CPU overhead if
165packets are arriving at a high rate, increasing the number of packets
166per second that can be captured.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700167.IP
Haibo Huang165065a2018-07-23 17:26:52 -0700168The packet buffer timeout is required so that an application won't wait
169for the operating system's capture buffer to fill up before packets are
Elliott Hughes965a4b52017-05-15 10:37:39 -0700170delivered; if packets are arriving slowly, that wait could take an
171arbitrarily long period of time.
172.IP
Haibo Huang165065a2018-07-23 17:26:52 -0700173Not all platforms support a packet buffer timeout; on platforms that
174don't, the packet buffer timeout is ignored. A zero value for the
175timeout, on platforms that support a packet buffer timeout, will cause a
176read to wait forever to allow enough packets to arrive, with no timeout.
177A negative value is invalid; the result of setting the timeout to a
178negative value is unpredictable.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700179.IP
180.BR NOTE :
Haibo Huang165065a2018-07-23 17:26:52 -0700181the packet buffer timeout cannot be used to cause calls that read
Elliott Hughes965a4b52017-05-15 10:37:39 -0700182packets to return within a limited period of time, because, on some
Haibo Huang165065a2018-07-23 17:26:52 -0700183platforms, the packet buffer timeout isn't supported, and, on other
184platforms, the timer doesn't start until at least one packet arrives.
185This means that the packet buffer timeout should
Elliott Hughes965a4b52017-05-15 10:37:39 -0700186.B NOT
187be used, for example, in an interactive application to allow the packet
188capture loop to ``poll'' for user input periodically, as there's no
189guarantee that a call reading packets will return after the timeout
190expires even if no packets have arrived.
191.IP
Haibo Huang165065a2018-07-23 17:26:52 -0700192The packet buffer timeout is set with
Elliott Hughes965a4b52017-05-15 10:37:39 -0700193.BR pcap_set_timeout ().
194.IP "buffer size"
195Packets that arrive for a capture are stored in a buffer, so that they
196do not have to be read by the application as soon as they arrive. On
197some platforms, the buffer's size can be set; a size that's too small
198could mean that, if too many packets are being captured and the snapshot
199length doesn't limit the amount of data that's buffered, packets could
200be dropped if the buffer fills up before the application can read
201packets from it, while a size that's too large could use more
202non-pageable operating system memory than is necessary to prevent
203packets from being dropped.
204.IP
205The buffer size is set with
206.BR pcap_set_buffer_size ().
207.IP "timestamp type"
208On some platforms, the time stamp given to packets on live captures can
209come from different sources that can have different resolutions or that
210can have different relationships to the time values for the current time
211supplied by routines on the native operating system. See
212.BR pcap-tstamp (7)
213for a list of time stamp types.
214.IP
215The time stamp type is set with
216.BR pcap_set_tstamp_type ().
217.PP
218Reading packets from a network interface may require that you have
219special privileges:
220.TP
221.B Under SunOS 3.x or 4.x with NIT or BPF:
222You must have read access to
223.I /dev/nit
224or
225.IR /dev/bpf* .
226.TP
227.B Under Solaris with DLPI:
228You must have read/write access to the network pseudo device, e.g.
229.IR /dev/le .
230On at least some versions of Solaris, however, this is not sufficient to
231allow
232.I tcpdump
233to capture in promiscuous mode; on those versions of Solaris, you must
234be root, or the application capturing packets
235must be installed setuid to root, in order to capture in promiscuous
236mode. Note that, on many (perhaps all) interfaces, if you don't capture
237in promiscuous mode, you will not see any outgoing packets, so a capture
238not done in promiscuous mode may not be very useful.
239.IP
240In newer versions of Solaris, you must have been given the
241.B net_rawaccess
242privilege; this is both necessary and sufficient to give you access to the
243network pseudo-device - there is no need to change the privileges on
244that device. A user can be given that privilege by, for example, adding
245that privilege to the user's
246.B defaultpriv
247key with the
Haibo Huang165065a2018-07-23 17:26:52 -0700248.B usermod (8)
Elliott Hughes965a4b52017-05-15 10:37:39 -0700249command.
250.TP
251.B Under HP-UX with DLPI:
252You must be root or the application capturing packets must be installed
253setuid to root.
254.TP
255.B Under IRIX with snoop:
256You must be root or the application capturing packets must be installed
257setuid to root.
258.TP
259.B Under Linux:
260You must be root or the application capturing packets must be installed
261setuid to root (unless your distribution has a kernel
262that supports capability bits such as CAP_NET_RAW and code to allow
263those capability bits to be given to particular accounts and to cause
264those bits to be set on a user's initial processes when they log in, in
265which case you must have CAP_NET_RAW in order to capture and
266CAP_NET_ADMIN to enumerate network devices with, for example, the
267.B \-D
268flag).
269.TP
270.B Under ULTRIX and Digital UNIX/Tru64 UNIX:
271Any user may capture network traffic.
272However, no user (not even the super-user) can capture in promiscuous
273mode on an interface unless the super-user has enabled promiscuous-mode
274operation on that interface using
275.IR pfconfig (8),
276and no user (not even the super-user) can capture unicast traffic
277received by or sent by the machine on an interface unless the super-user
278has enabled copy-all-mode operation on that interface using
279.IR pfconfig ,
280so
281.I useful
282packet capture on an interface probably requires that either
283promiscuous-mode or copy-all-mode operation, or both modes of
284operation, be enabled on that interface.
285.TP
Haibo Huang165065a2018-07-23 17:26:52 -0700286.B Under BSD (this includes macOS):
Elliott Hughes965a4b52017-05-15 10:37:39 -0700287You must have read access to
288.I /dev/bpf*
289on systems that don't have a cloning BPF device, or to
290.I /dev/bpf
291on systems that do.
Haibo Huang165065a2018-07-23 17:26:52 -0700292On BSDs with a devfs (this includes macOS), this might involve more
Elliott Hughes965a4b52017-05-15 10:37:39 -0700293than just having somebody with super-user access setting the ownership
294or permissions on the BPF devices - it might involve configuring devfs
295to set the ownership or permissions every time the system is booted,
296if the system even supports that; if it doesn't support that, you might
297have to find some other way to make that happen at boot time.
298.PP
299Reading a saved packet file doesn't require special privileges.
300.PP
301The packets read from the handle may include a ``pseudo-header''
302containing various forms of packet meta-data, and probably includes a
303link-layer header whose contents can differ for different network
304interfaces. To determine the format of the packets supplied by the
305handle, call
306.BR pcap_datalink ();
Haibo Huang165065a2018-07-23 17:26:52 -0700307.I https://www.tcpdump.org/linktypes.html
Elliott Hughes965a4b52017-05-15 10:37:39 -0700308lists the values it returns and describes the packet formats that
309correspond to those values.
310.PP
311Do
312.B NOT
313assume that the packets for a given capture or ``savefile`` will have
314any given link-layer header type, such as
315.B DLT_EN10MB
316for Ethernet. For example, the "any" device on Linux will have a
317link-layer header type of
318.B DLT_LINUX_SLL
319even if all devices on the system at the time the "any" device is opened
320have some other data link type, such as
321.B DLT_EN10MB
322for Ethernet.
323.PP
324To obtain the
325.B "FILE\ *"
326corresponding to a
327.B pcap_t
328opened for a ``savefile'', call
329.BR pcap_file ().
330.TP
331.B Routines
332.RS
333.TP
334.BR pcap_create (3PCAP)
335get a
336.B pcap_t
337for live capture
338.TP
339.BR pcap_activate (3PCAP)
340activate a
341.B pcap_t
342for live capture
343.TP
344.BR pcap_findalldevs (3PCAP)
345get a list of devices that can be opened for a live capture
346.TP
347.BR pcap_freealldevs (3PCAP)
348free list of devices
349.TP
350.BR pcap_lookupdev (3PCAP)
351get first non-loopback device on that list
352.TP
353.BR pcap_open_offline (3PCAP)
354open a
355.B pcap_t
356for a ``savefile'', given a pathname
357.TP
358.BR pcap_open_offline_with_tstamp_precision (3PCAP)
359open a
360.B pcap_t
361for a ``savefile'', given a pathname, and specify the precision to
362provide for packet time stamps
363.TP
364.BR pcap_fopen_offline (3PCAP)
365open a
366.B pcap_t
367for a ``savefile'', given a
368.B "FILE\ *"
369.TP
370.BR pcap_fopen_offline_with_tstamp_precision (3PCAP)
371open a
372.B pcap_t
373for a ``savefile'', given a
374.BR "FILE\ *" ,
375and specify the precision to provide for packet time stamps
376.TP
377.BR pcap_open_dead (3PCAP)
378create a ``fake''
379.B pcap_t
380.TP
381.BR pcap_close (3PCAP)
382close a
383.B pcap_t
384.TP
385.BR pcap_set_snaplen (3PCAP)
386set the snapshot length for a not-yet-activated
387.B pcap_t
388for live capture
389.TP
390.BR pcap_snapshot (3PCAP)
391get the snapshot length for a
392.B pcap_t
393.TP
394.BR pcap_set_promisc (3PCAP)
395set promiscuous mode for a not-yet-activated
396.B pcap_t
397for live capture
398.TP
Haibo Huang165065a2018-07-23 17:26:52 -0700399.BR pcap_set_protocol_linux (3PCAP)
400set capture protocol for a not-yet-activated
401.B pcap_t
402for live capture (Linux only)
403.TP
Elliott Hughes965a4b52017-05-15 10:37:39 -0700404.BR pcap_set_rfmon (3PCAP)
405set monitor mode for a not-yet-activated
406.B pcap_t
407for live capture
408.TP
409.BR pcap_can_set_rfmon (3PCAP)
410determine whether monitor mode can be set for a
411.B pcap_t
412for live capture
413.TP
414.BR pcap_set_timeout (3PCAP)
Haibo Huang165065a2018-07-23 17:26:52 -0700415set packet buffer timeout for a not-yet-activated
Elliott Hughes965a4b52017-05-15 10:37:39 -0700416.B pcap_t
417for live capture
418.TP
419.BR pcap_set_buffer_size (3PCAP)
420set buffer size for a not-yet-activated
421.B pcap_t
422for live capture
423.TP
424.BR pcap_set_tstamp_type (3PCAP)
425set time stamp type for a not-yet-activated
426.B pcap_t
427for live capture
428.TP
429.BR pcap_list_tstamp_types (3PCAP)
430get list of available time stamp types for a not-yet-activated
431.B pcap_t
432for live capture
433.TP
434.BR pcap_free_tstamp_types (3PCAP)
435free list of available time stamp types
436.TP
437.BR pcap_tstamp_type_val_to_name (3PCAP)
438get name for a time stamp type
439.TP
440.BR pcap_tstamp_type_val_to_description (3PCAP)
441get description for a time stamp type
442.TP
443.BR pcap_tstamp_type_name_to_val (3PCAP)
444get time stamp type corresponding to a name
445.TP
446.BR pcap_set_tstamp_precision (3PCAP)
447set time stamp precision for a not-yet-activated
448.B pcap_t
449for live capture
450.TP
451.BR pcap_get_tstamp_precision (3PCAP)
452get the time stamp precision of a
453.B pcap_t
454for live capture
455.TP
456.BR pcap_datalink (3PCAP)
457get link-layer header type for a
458.B pcap_t
459.TP
460.BR pcap_file (3PCAP)
461get the
462.B "FILE\ *"
463for a
464.B pcap_t
465opened for a ``savefile''
466.TP
467.BR pcap_is_swapped (3PCAP)
468determine whether a ``savefile'' being read came from a machine with the
469opposite byte order
470.TP
471.BR pcap_major_version (3PCAP)
472.PD 0
473.TP
474.BR pcap_minor_version (3PCAP)
475get the major and minor version of the file format version for a
476``savefile''
477.PD
478.RE
479.SS Selecting a link-layer header type for a live capture
480Some devices may provide more than one link-layer header type. To
481obtain a list of all link-layer header types provided by a device, call
482.BR pcap_list_datalinks ()
483on an activated
484.B pcap_t
485for the device.
486To free a list of link-layer header types, call
487.BR pcap_free_datalinks ().
488To set the link-layer header type for a device, call
489.BR pcap_set_datalink ().
490This should be done after the device has been activated but before any
491packets are read and before any filters are compiled or installed.
492.TP
493.B Routines
494.RS
495.TP
496.BR pcap_list_datalinks (3PCAP)
497get a list of link-layer header types for a device
498.TP
499.BR pcap_free_datalinks (3PCAP)
500free list of link-layer header types
501.TP
502.BR pcap_set_datalink (3PCAP)
503set link-layer header type for a device
504.TP
505.BR pcap_datalink_val_to_name (3PCAP)
506get name for a link-layer header type
507.TP
508.BR pcap_datalink_val_to_description (3PCAP)
509get description for a link-layer header type
510.TP
511.BR pcap_datalink_name_to_val (3PCAP)
512get link-layer header type corresponding to a name
513.RE
514.SS Reading packets
515Packets are read with
516.BR pcap_dispatch ()
517or
518.BR pcap_loop (),
519which process one or more packets, calling a callback routine for each
520packet, or with
521.BR pcap_next ()
522or
523.BR pcap_next_ex (),
524which return the next packet.
525The callback for
526.BR pcap_dispatch ()
527and
528.BR pcap_loop ()
529is supplied a pointer to a
530.IR "struct pcap_pkthdr" ,
531which includes the following members:
532.RS
533.TP
534.B ts
535a
536.I struct timeval
537containing the time when the packet was captured
538.TP
539.B caplen
540a
541.I bpf_u_int32
542giving the number of bytes of the packet that are available from the
543capture
544.TP
545.B len
546a
547.I bpf_u_int32
548giving the length of the packet, in bytes (which might be more than the
549number of bytes available from the capture, if the length of the packet
550is larger than the maximum number of bytes to capture).
551.RE
552.PP
553The callback is also supplied a
554.I const u_char
555pointer to the first
556.B caplen
557(as given in the
558.I struct pcap_pkthdr
559mentioned above)
560bytes of data from the packet. This won't necessarily be the entire
561packet; to capture the entire packet, you will have to provide a value
562for
563.I snaplen
564in your call to
565.BR pcap_set_snaplen ()
566that is sufficiently large to get all of the packet's data - a value of
56765535 should be sufficient on most if not all networks). When reading
568from a ``savefile'', the snapshot length specified when the capture was
569performed will limit the amount of packet data available.
570.PP
571.BR pcap_next ()
572is passed an argument that points to a
573.I struct pcap_pkthdr
574structure, and fills it in with the time stamp and length values for the
575packet. It returns a
576.I const u_char
577to the first
578.B caplen
579bytes of the packet on success, and NULL on error.
580.PP
581.BR pcap_next_ex ()
582is passed two pointer arguments, one of which points to a
583.IR struct pcap_pkthdr *
584and one of which points to a
585.IR "const u_char" *.
586It sets the first pointer to point to a
587.I struct pcap_pkthdr
588structure with the time stamp and length values for the packet, and sets
589the second pointer to point to the first
590.B caplen
591bytes of the packet.
592.PP
593To force the loop in
594.BR pcap_dispatch ()
595or
596.BR pcap_loop ()
597to terminate, call
598.BR pcap_breakloop ().
599.PP
600By default, when reading packets from an interface opened for a live
601capture,
602.BR pcap_dispatch (),
603.BR pcap_next (),
604and
605.BR pcap_next_ex ()
606will, if no packets are currently available to be read, block waiting
607for packets to become available. On some, but
608.I not
Haibo Huang165065a2018-07-23 17:26:52 -0700609all, platforms, if a packet buffer timeout was specified, the wait will
610terminate after the packet buffer timeout expires; applications should
611be prepared for this, as it happens on some platforms, but should not
612rely on it, as it does not happen on other platforms. Note that the
613wait might, or might not, terminate even if no packets are available;
614applications should be prepared for this to happen, but must not rely on
615it happening.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700616.PP
617A handle can be put into ``non-blocking mode'', so that those routines
618will, rather than blocking, return an indication that no packets are
619available to read. Call
620.BR pcap_setnonblock ()
621to put a handle into non-blocking mode or to take it out of non-blocking
622mode; call
623.BR pcap_getnonblock ()
624to determine whether a handle is in non-blocking mode. Note that
625non-blocking mode does not work correctly in Mac OS X 10.6.
626.PP
627Non-blocking mode is often combined with routines such as
628.BR select (2)
629or
630.BR poll (2)
631or other routines a platform offers to wait for any of a set of
632descriptors to be ready to read. To obtain, for a handle, a descriptor
633that can be used in those routines, call
634.BR pcap_get_selectable_fd ().
635Not all handles have such a descriptor available;
636.BR pcap_get_selectable_fd ()
637will return \-1 if no such descriptor exists. In addition, for various
638reasons, one or more of those routines will not work properly with the
639descriptor; the documentation for
640.BR pcap_get_selectable_fd ()
641gives details. Note that, just as an attempt to read packets from a
642.B pcap_t
Haibo Huang165065a2018-07-23 17:26:52 -0700643may not return any packets if the packet buffer timeout expires, a
Elliott Hughes965a4b52017-05-15 10:37:39 -0700644.BR select (),
645.BR poll (),
Haibo Huang165065a2018-07-23 17:26:52 -0700646or other such call may, if the packet buffer timeout expires, indicate
647that a descriptor is ready to read even if there are no packets
648available to read.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700649.TP
650.B Routines
651.RS
652.TP
653.BR pcap_dispatch (3PCAP)
654read a bufferful of packets from a
655.B pcap_t
656open for a live capture or the full set of packets from a
657.B pcap_t
658open for a ``savefile''
659.TP
660.BR pcap_loop (3PCAP)
661read packets from a
662.B pcap_t
663until an interrupt or error occurs
664.TP
665.BR pcap_next (3PCAP)
666read the next packet from a
667.B pcap_t
668without an indication whether an error occurred
669.TP
670.BR pcap_next_ex (3PCAP)
671read the next packet from a
672.B pcap_t
673with an error indication on an error
674.TP
675.BR pcap_breakloop (3PCAP)
676prematurely terminate the loop in
677.BR pcap_dispatch ()
678or
679.BR pcap_loop ()
680.TP
681.BR pcap_setnonblock (3PCAP)
682set or clear non-blocking mode on a
683.B pcap_t
684.TP
685.BR pcap_getnonblock (3PCAP)
686get the state of non-blocking mode for a
687.B pcap_t
688.TP
689.BR pcap_get_selectable_fd (3PCAP)
690attempt to get a descriptor for a
691.B pcap_t
692that can be used in calls such as
693.BR select (2)
694and
695.BR poll (2)
696.RE
697.SS Filters
698In order to cause only certain packets to be returned when reading
699packets, a filter can be set on a handle. For a live capture, the
700filtering will be performed in kernel mode, if possible, to avoid
701copying ``uninteresting'' packets from the kernel to user mode.
702.PP
703A filter can be specified as a text string; the syntax and semantics of
704the string are as described by
705.BR pcap-filter (7).
706A filter string is compiled into a program in a pseudo-machine-language
707by
708.BR pcap_compile ()
709and the resulting program can be made a filter for a handle with
710.BR pcap_setfilter ().
711The result of
712.BR pcap_compile ()
713can be freed with a call to
714.BR pcap_freecode ().
715.BR pcap_compile ()
716may require a network mask for certain expressions in the filter string;
717.BR pcap_lookupnet ()
718can be used to find the network address and network mask for a given
719capture device.
720.PP
721A compiled filter can also be applied directly to a packet that has been
722read using
723.BR pcap_offline_filter ().
724.TP
725.B Routines
726.RS
727.TP
728.BR pcap_compile (3PCAP)
729compile filter expression to a pseudo-machine-language code program
730.TP
731.BR pcap_freecode (3PCAP)
732free a filter program
733.TP
734.BR pcap_setfilter (3PCAP)
735set filter for a
736.B pcap_t
737.TP
738.BR pcap_lookupnet (3PCAP)
739get network address and network mask for a capture device
740.TP
741.BR pcap_offline_filter (3PCAP)
742apply a filter program to a packet
743.RE
744.SS Incoming and outgoing packets
745By default, libpcap will attempt to capture both packets sent by the
746machine and packets received by the machine. To limit it to capturing
747only packets received by the machine or, if possible, only packets sent
748by the machine, call
749.BR pcap_setdirection ().
750.TP
751.BR Routines
752.RS
753.TP
754.BR pcap_setdirection (3PCAP)
755specify whether to capture incoming packets, outgoing packets, or both
756.RE
757.SS Capture statistics
758To get statistics about packets received and dropped in a live capture,
759call
760.BR pcap_stats ().
761.TP
762.B Routines
763.RS
764.TP
765.BR pcap_stats (3PCAP)
766get capture statistics
767.RE
768.SS Opening a handle for writing captured packets
769To open a ``savefile`` to which to write packets, given the pathname the
770``savefile'' should have, call
771.BR pcap_dump_open ().
772To open a ``savefile`` to which to write packets, given the pathname the
773``savefile'' should have, call
774.BR pcap_dump_open ();
775to set up a handle for a ``savefile'', given a
776.B "FILE\ *"
777referring to a file already opened for writing, call
778.BR pcap_dump_fopen ().
779They each return pointers to a
780.BR pcap_dumper_t ,
781which is the handle used for writing packets to the ``savefile''. If it
782succeeds, it will have created the file if it doesn't exist and
783truncated the file if it does exist.
784To close a
785.BR pcap_dumper_t ,
786call
787.BR pcap_dump_close ().
788.TP
789.B Routines
790.RS
791.TP
792.BR pcap_dump_open (3PCAP)
793open a
794.B pcap_dumper_t
795for a ``savefile``, given a pathname
796.TP
797.BR pcap_dump_fopen (3PCAP)
798open a
799.B pcap_dumper_t
800for a ``savefile``, given a
801.B "FILE\ *"
802.TP
803.BR pcap_dump_close (3PCAP)
804close a
805.B pcap_dumper_t
806.TP
807.BR pcap_dump_file (3PCAP)
808get the
809.B "FILE\ *"
810for a
811.B pcap_dumper_t
812opened for a ``savefile''
813.RE
814.SS Writing packets
815To write a packet to a
816.BR pcap_dumper_t ,
817call
818.BR pcap_dump ().
819Packets written with
820.BR pcap_dump ()
821may be buffered, rather than being immediately written to the
822``savefile''. Closing the
823.B pcap_dumper_t
824will cause all buffered-but-not-yet-written packets to be written to the
825``savefile''.
826To force all packets written to the
827.BR pcap_dumper_t ,
828and not yet written to the ``savefile'' because they're buffered by the
829.BR pcap_dumper_t ,
830to be written to the ``savefile'', without closing the
831.BR pcap_dumper_t ,
832call
833.BR pcap_dump_flush ().
834.TP
835.B Routines
836.RS
837.TP
838.BR pcap_dump (3PCAP)
839write packet to a
840.B pcap_dumper_t
841.TP
842.BR pcap_dump_flush (3PCAP)
843flush buffered packets written to a
844.B pcap_dumper_t
845to the ``savefile''
846.TP
847.BR pcap_dump_ftell (3PCAP)
848get current file position for a
849.B pcap_dumper_t
850.RE
851.SS Injecting packets
852If you have the required privileges, you can inject packets onto a
853network with a
854.B pcap_t
855for a live capture, using
856.BR pcap_inject ()
857or
858.BR pcap_sendpacket ().
859(The two routines exist for compatibility with both OpenBSD and WinPcap;
860they perform the same function, but have different return values.)
861.TP
862.B Routines
863.RS
864.TP
865.BR pcap_inject (3PCAP)
866.PD 0
867.TP
868.BR pcap_sendpacket (3PCAP)
869transmit a packet
870.PD
871.RE
872.SS Reporting errors
873Some routines return error or warning status codes; to convert them to a
874string, use
875.BR pcap_statustostr ().
876.TP
877.B Routines
878.RS
879.TP
880.BR pcap_statustostr (3PCAP)
881get a string for an error or warning status code
882.RE
883.SS Getting library version information
884To get a string giving version information about libpcap, call
885.BR pcap_lib_version ().
886.TP
887.B Routines
888.RS
889.TP
890.BR pcap_lib_version (3PCAP)
891get library version string
892.RE
893.SH BACKWARDS COMPATIBILITY
894.PP
895In versions of libpcap prior to 1.0, the
896.B pcap.h
897header file was not in a
898.B pcap
899directory on most platforms; if you are writing an application that must
900work on versions of libpcap prior to 1.0, include
901.BR <pcap.h> ,
902which will include
903.B <pcap/pcap.h>
904for you, rather than including
905.BR <pcap/pcap.h> .
906.PP
907.BR pcap_create ()
908and
909.BR pcap_activate ()
910were not available in versions of libpcap prior to 1.0; if you are
911writing an application that must work on versions of libpcap prior to
9121.0, either use
913.BR pcap_open_live ()
914to get a handle for a live capture or, if you want to be able to use the
915additional capabilities offered by using
916.BR pcap_create ()
917and
918.BR pcap_activate (),
919use an
920.BR autoconf (1)
921script or some other configuration script to check whether the libpcap
9221.0 APIs are available and use them only if they are.
923.SH SEE ALSO
924autoconf(1), tcpdump(1), tcpslice(1), pcap-filter(7), pfconfig(8),
Haibo Huang165065a2018-07-23 17:26:52 -0700925usermod(8)
Elliott Hughes965a4b52017-05-15 10:37:39 -0700926.SH AUTHORS
927The original authors of libpcap are:
928.LP
929Van Jacobson,
930Craig Leres and
931Steven McCanne, all of the
932Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
933.LP
934The current version is available from "The Tcpdump Group"'s Web site at
935.LP
936.RS
Haibo Huang165065a2018-07-23 17:26:52 -0700937.I https://www.tcpdump.org/
Elliott Hughes965a4b52017-05-15 10:37:39 -0700938.RE
939.SH BUGS
Haibo Huang165065a2018-07-23 17:26:52 -0700940To report a security issue please send an e-mail to security@tcpdump.org.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700941.LP
Haibo Huang165065a2018-07-23 17:26:52 -0700942To report bugs and other problems, contribute patches, request a
943feature, provide generic feedback etc please see the file
944.I CONTRIBUTING
945in the libpcap source tree root.