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