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