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