blob: 492c227e1b665ec8c6ecfb117ad8d4e2fdf6f2c6 [file] [log] [blame]
JP Abgrall511eca32014-02-12 13:46:45 -08001.\" 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 Huangee759ce2021-01-05 21:34:29 -080020.TH PCAP 3PCAP "9 September 2020"
JP Abgrall511eca32014-02-12 13:46:45 -080021.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''.
Haibo Huangee759ce2021-01-05 21:34:29 -080038.SS Initializing
39.BR pcap_init ()
40initializes the library. It takes an argument giving options;
41currently, the options are:
42.TP
43.B PCAP_CHAR_ENC_LOCAL
44Treat all strings supplied as arguments, and return all strings to the
45caller, as being in the local character encoding.
46.TP
47.B PCAP_CHAR_ENC_UTF_8
48Treat all strings supplied as arguments, and return all strings to the
49caller, as being in UTF-8.
50.PP
51On UNIX-like systems, the local character encoding is assumed to be
52UTF-8, so no character encoding transformations are done.
53.PP
54On Windows, the local character encoding is the local ANSI code page.
55.PP
56If
57.BR pcap_init ()
58is called, the deprecated
59.BR pcap_lookupdev ()
60routine always fails, so it should not be used, and, on Windows,
61.BR pcap_create ()
62does not attempt to handle UTF-16LE strings.
63.PP
64If
65.BR pcap_init ()
66is not called, strings are treated as being in the local ANSI code page
67on Windows,
68.BR pcap_lookupdev ()
69will succeed if there is a device on which to capture, and
70.BR pcap_create ()
71makes an attempt to check whether the string passed as an argument is a
72UTF-16LE string - note that this attempt is unsafe, as it may run past
73the end of the string - to handle
74.BR pcap_lookupdev ()
75returning a UTF-16LE string. Programs that don't call
76.BR pcap_init ()
77should, on Windows, call
78.BR pcap_wsockinit ()
79to initialize Winsock; this is not necessary if
80.BR pcap_init ()
81is called, as
82.BR pcap_init ()
83will initialize Winsock itself on Windows.
84.TP
85.B Routines
86.RS
87.TP
88.BR pcap_init (3PCAP)
89initialize the library
90.RE
JP Abgrall511eca32014-02-12 13:46:45 -080091.SS Opening a capture handle for reading
92To open a handle for a live capture, given the name of the network or
93other interface on which the capture should be done, call
94.BR pcap_create (),
95set the appropriate options on the handle, and then activate it with
96.BR pcap_activate ().
Haibo Huangee759ce2021-01-05 21:34:29 -080097If
98.BR pcap_activate ()
99fails, the handle should be closed with
100.BR pcap_close ().
JP Abgrall511eca32014-02-12 13:46:45 -0800101.PP
102To obtain a list of devices that can be opened for a live capture, call
103.BR pcap_findalldevs ();
104to free the list returned by
105.BR pcap_findalldevs (),
106call
107.BR pcap_freealldevs ().
108.BR pcap_lookupdev ()
109will return the first device on that list that is not a ``loopback``
110network interface.
111.PP
112To open a handle for a ``savefile'' from which to read packets, given the
113pathname of the ``savefile'', call
114.BR pcap_open_offline ();
115to set up a handle for a ``savefile'', given a
116.B "FILE\ *"
117referring to a file already opened for reading, call
118.BR pcap_fopen_offline ().
119.PP
120In order to get a ``fake''
121.B pcap_t
122for use in routines that require a
123.B pcap_t
124as an argument, such as routines to open a ``savefile'' for writing and
125to compile a filter expression, call
126.BR pcap_open_dead ().
127.PP
128.BR pcap_create (),
129.BR pcap_open_offline (),
130.BR pcap_fopen_offline (),
131and
132.BR pcap_open_dead ()
133return a pointer to a
134.BR pcap_t ,
135which is the handle used for reading packets from the capture stream or
136the ``savefile'', and for finding out information about the capture
137stream or ``savefile''.
138To close a handle, use
139.BR pcap_close ().
140.PP
141The options that can be set on a capture handle include
142.IP "snapshot length"
143If, when capturing, you capture the entire contents of the packet, that
144requires more CPU time to copy the packet to your application, more disk
145and possibly network bandwidth to write the packet data to a file, and
146more disk space to save the packet. If you don't need the entire
147contents of the packet - for example, if you are only interested in the
148TCP headers of packets - you can set the "snapshot length" for the
149capture to an appropriate value. If the snapshot length is set to
150.IR snaplen ,
151and
152.I snaplen
153is less
154than the size of a packet that is captured, only the first
155.I snaplen
156bytes of that packet will be captured and provided as packet data.
157.IP
158A snapshot length of 65535 should be sufficient, on most if not all
159networks, to capture all the data available from the packet.
160.IP
161The snapshot length is set with
162.BR pcap_set_snaplen ().
163.IP "promiscuous mode"
164On broadcast LANs such as Ethernet, if the network isn't switched, or if
165the adapter is connected to a "mirror port" on a switch to which all
166packets passing through the switch are sent, a network adapter receives
167all packets on the LAN, including unicast or multicast packets not sent
168to a network address that the network adapter isn't configured to
169recognize.
170.IP
171Normally, the adapter will discard those packets; however, many network
172adapters support "promiscuous mode", which is a mode in which all
173packets, even if they are not sent to an address that the adapter
174recognizes, are provided to the host. This is useful for passively
175capturing traffic between two or more other hosts for analysis.
176.IP
177Note that even if an application does not set promiscuous mode, the
178adapter could well be in promiscuous mode for some other reason.
179.IP
180For now, this doesn't work on the "any" device; if an argument of "any"
Haibo Huangee759ce2021-01-05 21:34:29 -0800181or
182.B NULL
183is supplied, the setting of promiscuous mode is ignored.
JP Abgrall511eca32014-02-12 13:46:45 -0800184.IP
185Promiscuous mode is set with
186.BR pcap_set_promisc ().
187.IP "monitor mode"
188On IEEE 802.11 wireless LANs, even if an adapter is in promiscuous mode,
189it will supply to the host only frames for the network with which it's
190associated. It might also supply only data frames, not management or
191control frames, and might not provide the 802.11 header or radio
192information pseudo-header for those frames.
193.IP
194In "monitor mode", sometimes also called "rfmon mode" (for "Radio
195Frequency MONitor"), the adapter will supply all frames that it
196receives, with 802.11 headers, and might supply a pseudo-header with
197radio information about the frame as well.
198.IP
199Note that in monitor mode the adapter might disassociate from the
200network with which it's associated, so that you will not be able to use
201any wireless networks with that adapter. This could prevent accessing
202files on a network server, or resolving host names or network addresses,
203if you are capturing in monitor mode and are not connected to another
204network with another adapter.
205.IP
206Monitor mode is set with
207.BR pcap_set_rfmon (),
208and
209.BR pcap_can_set_rfmon ()
210can be used to determine whether an adapter can be put into monitor
211mode.
Haibo Huang165065a2018-07-23 17:26:52 -0700212.IP "packet buffer timeout"
JP Abgrall511eca32014-02-12 13:46:45 -0800213If, when capturing, packets are delivered as soon as they arrive, the
214application capturing the packets will be woken up for each packet as it
215arrives, and might have to make one or more calls to the operating
216system to fetch each packet.
217.IP
218If, instead, packets are not delivered as soon as they arrive, but are
Haibo Huang165065a2018-07-23 17:26:52 -0700219delivered after a short delay (called a "packet buffer timeout"), more
220than one packet can be accumulated before the packets are delivered, so
221that a single wakeup would be done for multiple packets, and each set of
222calls made to the operating system would supply multiple packets, rather
223than a single packet. This reduces the per-packet CPU overhead if
224packets are arriving at a high rate, increasing the number of packets
225per second that can be captured.
JP Abgrall511eca32014-02-12 13:46:45 -0800226.IP
Haibo Huang165065a2018-07-23 17:26:52 -0700227The packet buffer timeout is required so that an application won't wait
228for the operating system's capture buffer to fill up before packets are
JP Abgrall511eca32014-02-12 13:46:45 -0800229delivered; if packets are arriving slowly, that wait could take an
230arbitrarily long period of time.
231.IP
Haibo Huang165065a2018-07-23 17:26:52 -0700232Not all platforms support a packet buffer timeout; on platforms that
233don't, the packet buffer timeout is ignored. A zero value for the
234timeout, on platforms that support a packet buffer timeout, will cause a
235read to wait forever to allow enough packets to arrive, with no timeout.
236A negative value is invalid; the result of setting the timeout to a
237negative value is unpredictable.
JP Abgrall511eca32014-02-12 13:46:45 -0800238.IP
239.BR NOTE :
Haibo Huang165065a2018-07-23 17:26:52 -0700240the packet buffer timeout cannot be used to cause calls that read
JP Abgrall511eca32014-02-12 13:46:45 -0800241packets to return within a limited period of time, because, on some
Haibo Huang165065a2018-07-23 17:26:52 -0700242platforms, the packet buffer timeout isn't supported, and, on other
243platforms, the timer doesn't start until at least one packet arrives.
244This means that the packet buffer timeout should
JP Abgrall511eca32014-02-12 13:46:45 -0800245.B NOT
246be used, for example, in an interactive application to allow the packet
247capture loop to ``poll'' for user input periodically, as there's no
248guarantee that a call reading packets will return after the timeout
249expires even if no packets have arrived.
250.IP
Haibo Huang165065a2018-07-23 17:26:52 -0700251The packet buffer timeout is set with
JP Abgrall511eca32014-02-12 13:46:45 -0800252.BR pcap_set_timeout ().
Haibo Huang4ccd6832020-04-23 18:03:48 -0700253.IP "immediate mode"
254In immediate mode, packets are always delivered as soon as they arrive,
255with no buffering. Immediate mode is set with
256.BR pcap_set_immediate_mode ().
JP Abgrall511eca32014-02-12 13:46:45 -0800257.IP "buffer size"
258Packets that arrive for a capture are stored in a buffer, so that they
259do not have to be read by the application as soon as they arrive. On
260some platforms, the buffer's size can be set; a size that's too small
261could mean that, if too many packets are being captured and the snapshot
262length doesn't limit the amount of data that's buffered, packets could
263be dropped if the buffer fills up before the application can read
264packets from it, while a size that's too large could use more
265non-pageable operating system memory than is necessary to prevent
266packets from being dropped.
267.IP
268The buffer size is set with
269.BR pcap_set_buffer_size ().
270.IP "timestamp type"
271On some platforms, the time stamp given to packets on live captures can
272come from different sources that can have different resolutions or that
273can have different relationships to the time values for the current time
274supplied by routines on the native operating system. See
275.BR pcap-tstamp (@MAN_MISC_INFO@)
276for a list of time stamp types.
277.IP
278The time stamp type is set with
279.BR pcap_set_tstamp_type ().
280.PP
281Reading packets from a network interface may require that you have
282special privileges:
283.TP
284.B Under SunOS 3.x or 4.x with NIT or BPF:
285You must have read access to
286.I /dev/nit
287or
288.IR /dev/bpf* .
289.TP
290.B Under Solaris with DLPI:
291You must have read/write access to the network pseudo device, e.g.
292.IR /dev/le .
293On at least some versions of Solaris, however, this is not sufficient to
294allow
295.I tcpdump
296to capture in promiscuous mode; on those versions of Solaris, you must
297be root, or the application capturing packets
298must be installed setuid to root, in order to capture in promiscuous
299mode. Note that, on many (perhaps all) interfaces, if you don't capture
300in promiscuous mode, you will not see any outgoing packets, so a capture
301not done in promiscuous mode may not be very useful.
302.IP
303In newer versions of Solaris, you must have been given the
304.B net_rawaccess
305privilege; this is both necessary and sufficient to give you access to the
306network pseudo-device - there is no need to change the privileges on
307that device. A user can be given that privilege by, for example, adding
308that privilege to the user's
309.B defaultpriv
310key with the
Haibo Huangee759ce2021-01-05 21:34:29 -0800311.BR usermod (@MAN_ADMIN_COMMANDS@)
JP Abgrall511eca32014-02-12 13:46:45 -0800312command.
313.TP
314.B Under HP-UX with DLPI:
315You must be root or the application capturing packets must be installed
316setuid to root.
317.TP
318.B Under IRIX with snoop:
319You must be root or the application capturing packets must be installed
320setuid to root.
321.TP
322.B Under Linux:
323You must be root or the application capturing packets must be installed
Haibo Huangee759ce2021-01-05 21:34:29 -0800324setuid to root, unless your distribution has a kernel
JP Abgrall511eca32014-02-12 13:46:45 -0800325that supports capability bits such as CAP_NET_RAW and code to allow
326those capability bits to be given to particular accounts and to cause
327those bits to be set on a user's initial processes when they log in, in
Haibo Huangee759ce2021-01-05 21:34:29 -0800328which case you must have CAP_NET_RAW in order to capture.
JP Abgrall511eca32014-02-12 13:46:45 -0800329.TP
330.B Under ULTRIX and Digital UNIX/Tru64 UNIX:
331Any user may capture network traffic.
332However, no user (not even the super-user) can capture in promiscuous
333mode on an interface unless the super-user has enabled promiscuous-mode
334operation on that interface using
335.IR pfconfig (8),
336and no user (not even the super-user) can capture unicast traffic
337received by or sent by the machine on an interface unless the super-user
338has enabled copy-all-mode operation on that interface using
339.IR pfconfig ,
340so
341.I useful
342packet capture on an interface probably requires that either
343promiscuous-mode or copy-all-mode operation, or both modes of
344operation, be enabled on that interface.
345.TP
Haibo Huang165065a2018-07-23 17:26:52 -0700346.B Under BSD (this includes macOS):
JP Abgrall511eca32014-02-12 13:46:45 -0800347You must have read access to
348.I /dev/bpf*
349on systems that don't have a cloning BPF device, or to
350.I /dev/bpf
351on systems that do.
Haibo Huang165065a2018-07-23 17:26:52 -0700352On BSDs with a devfs (this includes macOS), this might involve more
JP Abgrall511eca32014-02-12 13:46:45 -0800353than just having somebody with super-user access setting the ownership
354or permissions on the BPF devices - it might involve configuring devfs
355to set the ownership or permissions every time the system is booted,
356if the system even supports that; if it doesn't support that, you might
357have to find some other way to make that happen at boot time.
358.PP
359Reading a saved packet file doesn't require special privileges.
360.PP
361The packets read from the handle may include a ``pseudo-header''
362containing various forms of packet meta-data, and probably includes a
363link-layer header whose contents can differ for different network
364interfaces. To determine the format of the packets supplied by the
365handle, call
366.BR pcap_datalink ();
Haibo Huang165065a2018-07-23 17:26:52 -0700367.I https://www.tcpdump.org/linktypes.html
JP Abgrall511eca32014-02-12 13:46:45 -0800368lists the values it returns and describes the packet formats that
369correspond to those values.
370.PP
371Do
372.B NOT
373assume that the packets for a given capture or ``savefile`` will have
374any given link-layer header type, such as
375.B DLT_EN10MB
376for Ethernet. For example, the "any" device on Linux will have a
377link-layer header type of
378.B DLT_LINUX_SLL
Haibo Huangee759ce2021-01-05 21:34:29 -0800379or
380.B DLT_LINUX_SLL2
JP Abgrall511eca32014-02-12 13:46:45 -0800381even if all devices on the system at the time the "any" device is opened
382have some other data link type, such as
383.B DLT_EN10MB
384for Ethernet.
385.PP
386To obtain the
387.B "FILE\ *"
388corresponding to a
389.B pcap_t
390opened for a ``savefile'', call
391.BR pcap_file ().
392.TP
393.B Routines
394.RS
395.TP
396.BR pcap_create (3PCAP)
397get a
398.B pcap_t
399for live capture
400.TP
401.BR pcap_activate (3PCAP)
402activate a
403.B pcap_t
404for live capture
405.TP
406.BR pcap_findalldevs (3PCAP)
407get a list of devices that can be opened for a live capture
408.TP
409.BR pcap_freealldevs (3PCAP)
410free list of devices
411.TP
412.BR pcap_lookupdev (3PCAP)
413get first non-loopback device on that list
414.TP
415.BR pcap_open_offline (3PCAP)
416open a
417.B pcap_t
418for a ``savefile'', given a pathname
419.TP
Elliott Hughesd8845d72015-10-19 18:07:04 -0700420.BR pcap_open_offline_with_tstamp_precision (3PCAP)
421open a
422.B pcap_t
423for a ``savefile'', given a pathname, and specify the precision to
424provide for packet time stamps
425.TP
JP Abgrall511eca32014-02-12 13:46:45 -0800426.BR pcap_fopen_offline (3PCAP)
427open a
428.B pcap_t
429for a ``savefile'', given a
430.B "FILE\ *"
431.TP
Elliott Hughesd8845d72015-10-19 18:07:04 -0700432.BR pcap_fopen_offline_with_tstamp_precision (3PCAP)
433open a
434.B pcap_t
435for a ``savefile'', given a
436.BR "FILE\ *" ,
437and specify the precision to provide for packet time stamps
438.TP
JP Abgrall511eca32014-02-12 13:46:45 -0800439.BR pcap_open_dead (3PCAP)
440create a ``fake''
441.B pcap_t
442.TP
443.BR pcap_close (3PCAP)
444close a
445.B pcap_t
446.TP
447.BR pcap_set_snaplen (3PCAP)
448set the snapshot length for a not-yet-activated
449.B pcap_t
450for live capture
451.TP
452.BR pcap_snapshot (3PCAP)
453get the snapshot length for a
454.B pcap_t
455.TP
456.BR pcap_set_promisc (3PCAP)
457set promiscuous mode for a not-yet-activated
458.B pcap_t
459for live capture
460.TP
Haibo Huang165065a2018-07-23 17:26:52 -0700461.BR pcap_set_protocol_linux (3PCAP)
462set capture protocol for a not-yet-activated
463.B pcap_t
464for live capture (Linux only)
465.TP
JP Abgrall511eca32014-02-12 13:46:45 -0800466.BR pcap_set_rfmon (3PCAP)
467set monitor mode for a not-yet-activated
468.B pcap_t
469for live capture
470.TP
471.BR pcap_can_set_rfmon (3PCAP)
472determine whether monitor mode can be set for a
473.B pcap_t
474for live capture
475.TP
476.BR pcap_set_timeout (3PCAP)
Haibo Huang165065a2018-07-23 17:26:52 -0700477set packet buffer timeout for a not-yet-activated
JP Abgrall511eca32014-02-12 13:46:45 -0800478.B pcap_t
479for live capture
480.TP
Haibo Huang4ccd6832020-04-23 18:03:48 -0700481.BR pcap_set_immediate_mode (3PCAP)
482set immediate mode for a not-yet-activated
483.B pcap_t
484for live capture
485.TP
JP Abgrall511eca32014-02-12 13:46:45 -0800486.BR pcap_set_buffer_size (3PCAP)
487set buffer size for a not-yet-activated
488.B pcap_t
489for live capture
490.TP
491.BR pcap_set_tstamp_type (3PCAP)
492set time stamp type for a not-yet-activated
493.B pcap_t
494for live capture
495.TP
496.BR pcap_list_tstamp_types (3PCAP)
497get list of available time stamp types for a not-yet-activated
498.B pcap_t
499for live capture
500.TP
501.BR pcap_free_tstamp_types (3PCAP)
502free list of available time stamp types
503.TP
504.BR pcap_tstamp_type_val_to_name (3PCAP)
505get name for a time stamp type
506.TP
507.BR pcap_tstamp_type_val_to_description (3PCAP)
508get description for a time stamp type
509.TP
Elliott Hughesd8845d72015-10-19 18:07:04 -0700510.BR pcap_tstamp_type_name_to_val (3PCAP)
JP Abgrall511eca32014-02-12 13:46:45 -0800511get time stamp type corresponding to a name
512.TP
Elliott Hughesd8845d72015-10-19 18:07:04 -0700513.BR pcap_set_tstamp_precision (3PCAP)
514set time stamp precision for a not-yet-activated
515.B pcap_t
516for live capture
517.TP
518.BR pcap_get_tstamp_precision (3PCAP)
519get the time stamp precision of a
520.B pcap_t
521for live capture
522.TP
JP Abgrall511eca32014-02-12 13:46:45 -0800523.BR pcap_datalink (3PCAP)
524get link-layer header type for a
525.B pcap_t
526.TP
527.BR pcap_file (3PCAP)
528get the
529.B "FILE\ *"
530for a
531.B pcap_t
532opened for a ``savefile''
533.TP
534.BR pcap_is_swapped (3PCAP)
535determine whether a ``savefile'' being read came from a machine with the
536opposite byte order
537.TP
538.BR pcap_major_version (3PCAP)
539.PD 0
540.TP
541.BR pcap_minor_version (3PCAP)
542get the major and minor version of the file format version for a
543``savefile''
544.PD
545.RE
546.SS Selecting a link-layer header type for a live capture
547Some devices may provide more than one link-layer header type. To
548obtain a list of all link-layer header types provided by a device, call
549.BR pcap_list_datalinks ()
550on an activated
551.B pcap_t
552for the device.
553To free a list of link-layer header types, call
554.BR pcap_free_datalinks ().
555To set the link-layer header type for a device, call
556.BR pcap_set_datalink ().
557This should be done after the device has been activated but before any
558packets are read and before any filters are compiled or installed.
559.TP
560.B Routines
561.RS
562.TP
563.BR pcap_list_datalinks (3PCAP)
564get a list of link-layer header types for a device
565.TP
566.BR pcap_free_datalinks (3PCAP)
567free list of link-layer header types
568.TP
569.BR pcap_set_datalink (3PCAP)
570set link-layer header type for a device
571.TP
572.BR pcap_datalink_val_to_name (3PCAP)
573get name for a link-layer header type
574.TP
575.BR pcap_datalink_val_to_description (3PCAP)
Haibo Huangee759ce2021-01-05 21:34:29 -0800576.PD 0
577.TP
578.BR pcap_datalink_val_to_description_or_dlt (3PCAP)
JP Abgrall511eca32014-02-12 13:46:45 -0800579get description for a link-layer header type
Haibo Huangee759ce2021-01-05 21:34:29 -0800580.PD
JP Abgrall511eca32014-02-12 13:46:45 -0800581.TP
582.BR pcap_datalink_name_to_val (3PCAP)
583get link-layer header type corresponding to a name
584.RE
585.SS Reading packets
586Packets are read with
587.BR pcap_dispatch ()
588or
589.BR pcap_loop (),
590which process one or more packets, calling a callback routine for each
591packet, or with
592.BR pcap_next ()
593or
594.BR pcap_next_ex (),
595which return the next packet.
596The callback for
597.BR pcap_dispatch ()
598and
599.BR pcap_loop ()
600is supplied a pointer to a
601.IR "struct pcap_pkthdr" ,
602which includes the following members:
603.RS
604.TP
605.B ts
606a
607.I struct timeval
608containing the time when the packet was captured
609.TP
610.B caplen
611a
612.I bpf_u_int32
613giving the number of bytes of the packet that are available from the
614capture
615.TP
616.B len
617a
618.I bpf_u_int32
619giving the length of the packet, in bytes (which might be more than the
620number of bytes available from the capture, if the length of the packet
621is larger than the maximum number of bytes to capture).
622.RE
623.PP
624The callback is also supplied a
625.I const u_char
626pointer to the first
627.B caplen
628(as given in the
629.I struct pcap_pkthdr
630mentioned above)
631bytes of data from the packet. This won't necessarily be the entire
632packet; to capture the entire packet, you will have to provide a value
633for
634.I snaplen
635in your call to
636.BR pcap_set_snaplen ()
637that is sufficiently large to get all of the packet's data - a value of
63865535 should be sufficient on most if not all networks). When reading
639from a ``savefile'', the snapshot length specified when the capture was
640performed will limit the amount of packet data available.
641.PP
642.BR pcap_next ()
643is passed an argument that points to a
644.I struct pcap_pkthdr
645structure, and fills it in with the time stamp and length values for the
646packet. It returns a
647.I const u_char
648to the first
649.B caplen
Haibo Huangee759ce2021-01-05 21:34:29 -0800650bytes of the packet on success, and
651.B NULL
652on error.
JP Abgrall511eca32014-02-12 13:46:45 -0800653.PP
654.BR pcap_next_ex ()
655is passed two pointer arguments, one of which points to a
656.IR struct pcap_pkthdr *
Elliott Hughesd8845d72015-10-19 18:07:04 -0700657and one of which points to a
JP Abgrall511eca32014-02-12 13:46:45 -0800658.IR "const u_char" *.
659It sets the first pointer to point to a
660.I struct pcap_pkthdr
661structure with the time stamp and length values for the packet, and sets
662the second pointer to point to the first
663.B caplen
664bytes of the packet.
665.PP
666To force the loop in
667.BR pcap_dispatch ()
668or
669.BR pcap_loop ()
670to terminate, call
671.BR pcap_breakloop ().
672.PP
673By default, when reading packets from an interface opened for a live
674capture,
675.BR pcap_dispatch (),
676.BR pcap_next (),
677and
678.BR pcap_next_ex ()
679will, if no packets are currently available to be read, block waiting
680for packets to become available. On some, but
681.I not
Haibo Huang165065a2018-07-23 17:26:52 -0700682all, platforms, if a packet buffer timeout was specified, the wait will
683terminate after the packet buffer timeout expires; applications should
684be prepared for this, as it happens on some platforms, but should not
685rely on it, as it does not happen on other platforms. Note that the
686wait might, or might not, terminate even if no packets are available;
687applications should be prepared for this to happen, but must not rely on
688it happening.
JP Abgrall511eca32014-02-12 13:46:45 -0800689.PP
690A handle can be put into ``non-blocking mode'', so that those routines
691will, rather than blocking, return an indication that no packets are
692available to read. Call
693.BR pcap_setnonblock ()
694to put a handle into non-blocking mode or to take it out of non-blocking
695mode; call
696.BR pcap_getnonblock ()
697to determine whether a handle is in non-blocking mode. Note that
698non-blocking mode does not work correctly in Mac OS X 10.6.
699.PP
700Non-blocking mode is often combined with routines such as
701.BR select (2)
702or
703.BR poll (2)
Elliott Hughesd8845d72015-10-19 18:07:04 -0700704or other routines a platform offers to wait for any of a set of
705descriptors to be ready to read. To obtain, for a handle, a descriptor
JP Abgrall511eca32014-02-12 13:46:45 -0800706that can be used in those routines, call
707.BR pcap_get_selectable_fd ().
Haibo Huang4ccd6832020-04-23 18:03:48 -0700708If the routine indicates that data is
709available to read on the descriptor, an attempt should be made to read
710from the device.
711.PP
JP Abgrall511eca32014-02-12 13:46:45 -0800712Not all handles have such a descriptor available;
713.BR pcap_get_selectable_fd ()
Haibo Huang4ccd6832020-04-23 18:03:48 -0700714will return
Haibo Huangee759ce2021-01-05 21:34:29 -0800715.B \-1
Haibo Huang4ccd6832020-04-23 18:03:48 -0700716if no such descriptor is available. If no such
717descriptor is available, this may be because the device must be polled
718periodically for packets; in that case,
719.BR pcap_get_required_select_timeout ()
720will return a pointer to a
721.B struct timeval
722whose value can be used as a timeout in those routines. When the
723routine returns, an attmept should be made to read packets from the
724device. If
725.BR pcap_get_required_select_timeout ()
Haibo Huangee759ce2021-01-05 21:34:29 -0800726returns
727.BR NULL ,
728no such timeout is available, and those routines cannot be
Haibo Huang4ccd6832020-04-23 18:03:48 -0700729used with the device.
730.PP
731In addition, for various
JP Abgrall511eca32014-02-12 13:46:45 -0800732reasons, one or more of those routines will not work properly with the
733descriptor; the documentation for
734.BR pcap_get_selectable_fd ()
Elliott Hughesd8845d72015-10-19 18:07:04 -0700735gives details. Note that, just as an attempt to read packets from a
736.B pcap_t
Haibo Huang165065a2018-07-23 17:26:52 -0700737may not return any packets if the packet buffer timeout expires, a
Elliott Hughesd8845d72015-10-19 18:07:04 -0700738.BR select (),
739.BR poll (),
Haibo Huang165065a2018-07-23 17:26:52 -0700740or other such call may, if the packet buffer timeout expires, indicate
741that a descriptor is ready to read even if there are no packets
742available to read.
JP Abgrall511eca32014-02-12 13:46:45 -0800743.TP
744.B Routines
745.RS
746.TP
747.BR pcap_dispatch (3PCAP)
748read a bufferful of packets from a
749.B pcap_t
750open for a live capture or the full set of packets from a
751.B pcap_t
752open for a ``savefile''
753.TP
754.BR pcap_loop (3PCAP)
755read packets from a
756.B pcap_t
757until an interrupt or error occurs
758.TP
759.BR pcap_next (3PCAP)
760read the next packet from a
761.B pcap_t
762without an indication whether an error occurred
763.TP
764.BR pcap_next_ex (3PCAP)
765read the next packet from a
766.B pcap_t
767with an error indication on an error
768.TP
769.BR pcap_breakloop (3PCAP)
770prematurely terminate the loop in
771.BR pcap_dispatch ()
772or
773.BR pcap_loop ()
774.TP
775.BR pcap_setnonblock (3PCAP)
776set or clear non-blocking mode on a
777.B pcap_t
778.TP
779.BR pcap_getnonblock (3PCAP)
780get the state of non-blocking mode for a
781.B pcap_t
782.TP
783.BR pcap_get_selectable_fd (3PCAP)
784attempt to get a descriptor for a
785.B pcap_t
786that can be used in calls such as
787.BR select (2)
788and
789.BR poll (2)
Haibo Huang4ccd6832020-04-23 18:03:48 -0700790.TP
791.BR pcap_get_required_select_timeout (3PCAP)
Haibo Huangee759ce2021-01-05 21:34:29 -0800792attempt to get a timeout required for using a
793.B pcap_t
794in calls such as
Haibo Huang4ccd6832020-04-23 18:03:48 -0700795.BR select (2)
796and
797.BR poll (2)
JP Abgrall511eca32014-02-12 13:46:45 -0800798.RE
799.SS Filters
800In order to cause only certain packets to be returned when reading
801packets, a filter can be set on a handle. For a live capture, the
802filtering will be performed in kernel mode, if possible, to avoid
803copying ``uninteresting'' packets from the kernel to user mode.
804.PP
805A filter can be specified as a text string; the syntax and semantics of
806the string are as described by
807.BR pcap-filter (@MAN_MISC_INFO@).
808A filter string is compiled into a program in a pseudo-machine-language
809by
810.BR pcap_compile ()
811and the resulting program can be made a filter for a handle with
812.BR pcap_setfilter ().
813The result of
814.BR pcap_compile ()
815can be freed with a call to
816.BR pcap_freecode ().
817.BR pcap_compile ()
818may require a network mask for certain expressions in the filter string;
819.BR pcap_lookupnet ()
820can be used to find the network address and network mask for a given
821capture device.
822.PP
823A compiled filter can also be applied directly to a packet that has been
824read using
825.BR pcap_offline_filter ().
826.TP
827.B Routines
828.RS
829.TP
830.BR pcap_compile (3PCAP)
831compile filter expression to a pseudo-machine-language code program
832.TP
833.BR pcap_freecode (3PCAP)
834free a filter program
835.TP
836.BR pcap_setfilter (3PCAP)
837set filter for a
838.B pcap_t
839.TP
840.BR pcap_lookupnet (3PCAP)
841get network address and network mask for a capture device
842.TP
843.BR pcap_offline_filter (3PCAP)
844apply a filter program to a packet
845.RE
846.SS Incoming and outgoing packets
847By default, libpcap will attempt to capture both packets sent by the
848machine and packets received by the machine. To limit it to capturing
849only packets received by the machine or, if possible, only packets sent
850by the machine, call
851.BR pcap_setdirection ().
852.TP
853.BR Routines
854.RS
855.TP
856.BR pcap_setdirection (3PCAP)
857specify whether to capture incoming packets, outgoing packets, or both
858.RE
859.SS Capture statistics
860To get statistics about packets received and dropped in a live capture,
861call
862.BR pcap_stats ().
863.TP
864.B Routines
865.RS
866.TP
867.BR pcap_stats (3PCAP)
868get capture statistics
869.RE
870.SS Opening a handle for writing captured packets
871To open a ``savefile`` to which to write packets, given the pathname the
872``savefile'' should have, call
873.BR pcap_dump_open ().
874To open a ``savefile`` to which to write packets, given the pathname the
875``savefile'' should have, call
876.BR pcap_dump_open ();
877to set up a handle for a ``savefile'', given a
878.B "FILE\ *"
879referring to a file already opened for writing, call
880.BR pcap_dump_fopen ().
881They each return pointers to a
882.BR pcap_dumper_t ,
883which is the handle used for writing packets to the ``savefile''. If it
884succeeds, it will have created the file if it doesn't exist and
885truncated the file if it does exist.
886To close a
887.BR pcap_dumper_t ,
888call
889.BR pcap_dump_close ().
890.TP
891.B Routines
892.RS
893.TP
894.BR pcap_dump_open (3PCAP)
895open a
896.B pcap_dumper_t
897for a ``savefile``, given a pathname
898.TP
899.BR pcap_dump_fopen (3PCAP)
900open a
901.B pcap_dumper_t
902for a ``savefile``, given a
903.B "FILE\ *"
904.TP
905.BR pcap_dump_close (3PCAP)
906close a
907.B pcap_dumper_t
908.TP
909.BR pcap_dump_file (3PCAP)
910get the
911.B "FILE\ *"
912for a
913.B pcap_dumper_t
914opened for a ``savefile''
915.RE
916.SS Writing packets
917To write a packet to a
918.BR pcap_dumper_t ,
919call
920.BR pcap_dump ().
921Packets written with
922.BR pcap_dump ()
923may be buffered, rather than being immediately written to the
924``savefile''. Closing the
925.B pcap_dumper_t
926will cause all buffered-but-not-yet-written packets to be written to the
927``savefile''.
928To force all packets written to the
929.BR pcap_dumper_t ,
930and not yet written to the ``savefile'' because they're buffered by the
931.BR pcap_dumper_t ,
932to be written to the ``savefile'', without closing the
933.BR pcap_dumper_t ,
934call
935.BR pcap_dump_flush ().
936.TP
937.B Routines
938.RS
939.TP
940.BR pcap_dump (3PCAP)
941write packet to a
942.B pcap_dumper_t
943.TP
944.BR pcap_dump_flush (3PCAP)
945flush buffered packets written to a
946.B pcap_dumper_t
947to the ``savefile''
948.TP
949.BR pcap_dump_ftell (3PCAP)
950get current file position for a
951.B pcap_dumper_t
952.RE
953.SS Injecting packets
954If you have the required privileges, you can inject packets onto a
955network with a
956.B pcap_t
957for a live capture, using
958.BR pcap_inject ()
959or
960.BR pcap_sendpacket ().
Haibo Huangee759ce2021-01-05 21:34:29 -0800961(The two routines exist for compatibility with both OpenBSD and
962WinPcap/Npcap; they perform the same function, but have different return
963values.)
JP Abgrall511eca32014-02-12 13:46:45 -0800964.TP
965.B Routines
966.RS
967.TP
968.BR pcap_inject (3PCAP)
969.PD 0
970.TP
971.BR pcap_sendpacket (3PCAP)
972transmit a packet
973.PD
974.RE
975.SS Reporting errors
976Some routines return error or warning status codes; to convert them to a
977string, use
978.BR pcap_statustostr ().
979.TP
980.B Routines
981.RS
982.TP
983.BR pcap_statustostr (3PCAP)
984get a string for an error or warning status code
985.RE
986.SS Getting library version information
987To get a string giving version information about libpcap, call
Elliott Hughesd8845d72015-10-19 18:07:04 -0700988.BR pcap_lib_version ().
JP Abgrall511eca32014-02-12 13:46:45 -0800989.TP
990.B Routines
991.RS
992.TP
Elliott Hughesd8845d72015-10-19 18:07:04 -0700993.BR pcap_lib_version (3PCAP)
JP Abgrall511eca32014-02-12 13:46:45 -0800994get library version string
995.RE
Haibo Huang4ccd6832020-04-23 18:03:48 -0700996.SH BACKWARD COMPATIBILITY
JP Abgrall511eca32014-02-12 13:46:45 -0800997.PP
998In versions of libpcap prior to 1.0, the
999.B pcap.h
1000header file was not in a
1001.B pcap
1002directory on most platforms; if you are writing an application that must
1003work on versions of libpcap prior to 1.0, include
1004.BR <pcap.h> ,
1005which will include
1006.B <pcap/pcap.h>
1007for you, rather than including
1008.BR <pcap/pcap.h> .
1009.PP
1010.BR pcap_create ()
1011and
1012.BR pcap_activate ()
1013were not available in versions of libpcap prior to 1.0; if you are
1014writing an application that must work on versions of libpcap prior to
10151.0, either use
1016.BR pcap_open_live ()
1017to get a handle for a live capture or, if you want to be able to use the
1018additional capabilities offered by using
1019.BR pcap_create ()
1020and
1021.BR pcap_activate (),
1022use an
1023.BR autoconf (1)
1024script or some other configuration script to check whether the libpcap
10251.0 APIs are available and use them only if they are.
1026.SH SEE ALSO
Haibo Huangee759ce2021-01-05 21:34:29 -08001027.BR autoconf (1),
1028.BR tcpdump (1),
1029.BR tcpslice (1),
1030.BR pcap-filter (@MAN_MISC_INFO@),
1031.BR pfconfig (8),
1032.BR usermod (@MAN_ADMIN_COMMANDS@)
JP Abgrall511eca32014-02-12 13:46:45 -08001033.SH AUTHORS
1034The original authors of libpcap are:
1035.LP
1036Van Jacobson,
1037Craig Leres and
1038Steven McCanne, all of the
1039Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
1040.LP
1041The current version is available from "The Tcpdump Group"'s Web site at
1042.LP
1043.RS
Haibo Huang165065a2018-07-23 17:26:52 -07001044.I https://www.tcpdump.org/
JP Abgrall511eca32014-02-12 13:46:45 -08001045.RE
1046.SH BUGS
Haibo Huang165065a2018-07-23 17:26:52 -07001047To report a security issue please send an e-mail to security@tcpdump.org.
JP Abgrall511eca32014-02-12 13:46:45 -08001048.LP
Haibo Huang165065a2018-07-23 17:26:52 -07001049To report bugs and other problems, contribute patches, request a
1050feature, provide generic feedback etc please see the file
Haibo Huangee759ce2021-01-05 21:34:29 -08001051.I CONTRIBUTING.md
Haibo Huang165065a2018-07-23 17:26:52 -07001052in the libpcap source tree root.