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