1PCAP(3PCAP) PCAP(3PCAP)
2
3
4
6 pcap - Packet Capture library
7
9 #include <pcap/pcap.h>
10
12 The Packet Capture library provides a high level interface to packet
13 capture systems. All packets on the network, even those destined for
14 other hosts, are accessible through this mechanism. It also supports
15 saving captured packets to a ``savefile'', and reading packets from a
16 ``savefile''.
17
18 Opening a capture handle for reading
19 To open a handle for a live capture, given the name of the network or
20 other interface on which the capture should be done, call pcap_cre‐
21 ate(), set the appropriate options on the handle, and then activate it
22 with pcap_activate().
23
24 To obtain a list of devices that can be opened for a live capture, call
25 pcap_findalldevs(); to free the list returned by pcap_findalldevs(),
26 call pcap_freealldevs(). pcap_lookupdev() will return the first device
27 on that list that is not a ``loopback`` network interface.
28
29 To open a handle for a ``savefile'' from which to read packets, given
30 the pathname of the ``savefile'', call pcap_open_offline(); to set up a
31 handle for a ``savefile'', given a FILE * referring to a file already
32 opened for reading, call pcap_fopen_offline().
33
34 In order to get a ``fake'' pcap_t for use in routines that require a
35 pcap_t as an argument, such as routines to open a ``savefile'' for
36 writing and to compile a filter expression, call pcap_open_dead().
37
38 pcap_create(), pcap_open_offline(), pcap_fopen_offline(), and
39 pcap_open_dead() return a pointer to a pcap_t, which is the handle used
40 for reading packets from the capture stream or the ``savefile'', and
41 for finding out information about the capture stream or ``savefile''.
42 To close a handle, use pcap_close().
43
44 The options that can be set on a capture handle include
45
46 snapshot length
47 If, when capturing, you capture the entire contents of the
48 packet, that requires more CPU time to copy the packet to your
49 application, more disk and possibly network bandwidth to write
50 the packet data to a file, and more disk space to save the
51 packet. If you don't need the entire contents of the packet -
52 for example, if you are only interested in the TCP headers of
53 packets - you can set the "snapshot length" for the capture to
54 an appropriate value. If the snapshot length is set to snaplen,
55 and snaplen is less than the size of a packet that is captured,
56 only the first snaplen bytes of that packet will be captured and
57 provided as packet data.
58
59 A snapshot length of 65535 should be sufficient, on most if not
60 all networks, to capture all the data available from the packet.
61
62 The snapshot length is set with pcap_set_snaplen().
63
64 promiscuous mode
65 On broadcast LANs such as Ethernet, if the network isn't
66 switched, or if the adapter is connected to a "mirror port" on a
67 switch to which all packets passing through the switch are sent,
68 a network adapter receives all packets on the LAN, including
69 unicast or multicast packets not sent to a network address that
70 the network adapter isn't configured to recognize.
71
72 Normally, the adapter will discard those packets; however, many
73 network adapters support "promiscuous mode", which is a mode in
74 which all packets, even if they are not sent to an address that
75 the adapter recognizes, are provided to the host. This is use‐
76 ful for passively capturing traffic between two or more other
77 hosts for analysis.
78
79 Note that even if an application does not set promiscuous mode,
80 the adapter could well be in promiscuous mode for some other
81 reason.
82
83 For now, this doesn't work on the "any" device; if an argument
84 of "any" or NULL is supplied, the setting of promiscuous mode is
85 ignored.
86
87 Promiscuous mode is set with pcap_set_promisc().
88
89 monitor mode
90 On IEEE 802.11 wireless LANs, even if an adapter is in promiscu‐
91 ous mode, it will supply to the host only frames for the network
92 with which it's associated. It might also supply only data
93 frames, not management or control frames, and might not provide
94 the 802.11 header or radio information pseudo-header for those
95 frames.
96
97 In "monitor mode", sometimes also called "rfmon mode" (for
98 "Radio Frequency MONitor"), the adapter will supply all frames
99 that it receives, with 802.11 headers, and might supply a
100 pseudo-header with radio information about the frame as well.
101
102 Note that in monitor mode the adapter might disassociate from
103 the network with which it's associated, so that you will not be
104 able to use any wireless networks with that adapter. This could
105 prevent accessing files on a network server, or resolving host
106 names or network addresses, if you are capturing in monitor mode
107 and are not connected to another network with another adapter.
108
109 Monitor mode is set with pcap_set_rfmon(), and
110 pcap_can_set_rfmon() can be used to determine whether an adapter
111 can be put into monitor mode.
112
113 read timeout
114 If, when capturing, packets are delivered as soon as they
115 arrive, the application capturing the packets will be woken up
116 for each packet as it arrives, and might have to make one or
117 more calls to the operating system to fetch each packet.
118
119 If, instead, packets are not delivered as soon as they arrive,
120 but are delivered after a short delay (called a "read timeout"),
121 more than one packet can be accumulated before the packets are
122 delivered, so that a single wakeup would be done for multiple
123 packets, and each set of calls made to the operating system
124 would supply multiple packets, rather than a single packet.
125 This reduces the per-packet CPU overhead if packets are arriving
126 at a high rate, increasing the number of packets per second that
127 can be captured.
128
129 The read timeout is required so that an application won't wait
130 for the operating system's capture buffer to fill up before
131 packets are delivered; if packets are arriving slowly, that wait
132 could take an arbitrarily long period of time.
133
134 Not all platforms support a read timeout; on platforms that
135 don't, the read timeout is ignored. A zero value for the time‐
136 out, on platforms that support a read timeout, will cause a read
137 to wait forever to allow enough packets to arrive, with no time‐
138 out.
139
140 NOTE: the read timeout cannot be used to cause calls that read
141 packets to return within a limited period of time, because, on
142 some platforms, the read timeout isn't supported, and, on other
143 platforms, the timer doesn't start until at least one packet
144 arrives. This means that the read timeout should NOT be used,
145 for example, in an interactive application to allow the packet
146 capture loop to ``poll'' for user input periodically, as there's
147 no guarantee that a call reading packets will return after the
148 timeout expires even if no packets have arrived.
149
150 The read timeout is set with pcap_set_timeout().
151
152 buffer size
153 Packets that arrive for a capture are stored in a buffer, so
154 that they do not have to be read by the application as soon as
155 they arrive. On some platforms, the buffer's size can be set; a
156 size that's too small could mean that, if too many packets are
157 being captured and the snapshot length doesn't limit the amount
158 of data that's buffered, packets could be dropped if the buffer
159 fills up before the application can read packets from it, while
160 a size that's too large could use more non-pageable operating
161 system memory than is necessary to prevent packets from being
162 dropped.
163
164 The buffer size is set with pcap_set_buffer_size().
165
166 timestamp type
167 On some platforms, the time stamp given to packets on live cap‐
168 tures can come from different sources that can have different
169 resolutions or that can have different relationships to the time
170 values for the current time supplied by routines on the native
171 operating system. See pcap-tstamp(7) for a list of time stamp
172 types.
173
174 The time stamp type is set with pcap_set_tstamp_type().
175
176 Reading packets from a network interface may require that you have spe‐
177 cial privileges:
178
179 Under SunOS 3.x or 4.x with NIT or BPF:
180 You must have read access to /dev/nit or /dev/bpf*.
181
182 Under Solaris with DLPI:
183 You must have read/write access to the network pseudo device,
184 e.g. /dev/le. On at least some versions of Solaris, however,
185 this is not sufficient to allow tcpdump to capture in promiscu‐
186 ous mode; on those versions of Solaris, you must be root, or the
187 application capturing packets must be installed setuid to root,
188 in order to capture in promiscuous mode. Note that, on many
189 (perhaps all) interfaces, if you don't capture in promiscuous
190 mode, you will not see any outgoing packets, so a capture not
191 done in promiscuous mode may not be very useful.
192
193 In newer versions of Solaris, you must have been given the
194 net_rawaccess privilege; this is both necessary and sufficient
195 to give you access to the network pseudo-device - there is no
196 need to change the privileges on that device. A user can be
197 given that privilege by, for example, adding that privilege to
198 the user's defaultpriv key with the usermod (1M) command.
199
200 Under HP-UX with DLPI:
201 You must be root or the application capturing packets must be
202 installed setuid to root.
203
204 Under IRIX with snoop:
205 You must be root or the application capturing packets must be
206 installed setuid to root.
207
208 Under Linux:
209 You must be root or the application capturing packets must be
210 installed setuid to root (unless your distribution has a kernel
211 that supports capability bits such as CAP_NET_RAW and code to
212 allow those capability bits to be given to particular accounts
213 and to cause those bits to be set on a user's initial processes
214 when they log in, in which case you must have CAP_NET_RAW in
215 order to capture and CAP_NET_ADMIN to enumerate network devices
216 with, for example, the -D flag).
217
218 Under ULTRIX and Digital UNIX/Tru64 UNIX:
219 Any user may capture network traffic. However, no user (not
220 even the super-user) can capture in promiscuous mode on an
221 interface unless the super-user has enabled promiscuous-mode
222 operation on that interface using pfconfig(8), and no user (not
223 even the super-user) can capture unicast traffic received by or
224 sent by the machine on an interface unless the super-user has
225 enabled copy-all-mode operation on that interface using pfcon‐
226 fig, so useful packet capture on an interface probably requires
227 that either promiscuous-mode or copy-all-mode operation, or both
228 modes of operation, be enabled on that interface.
229
230 Under BSD (this includes Mac OS X):
231 You must have read access to /dev/bpf* on systems that don't
232 have a cloning BPF device, or to /dev/bpf on systems that do.
233 On BSDs with a devfs (this includes Mac OS X), this might
234 involve more than just having somebody with super-user access
235 setting the ownership or permissions on the BPF devices - it
236 might involve configuring devfs to set the ownership or permis‐
237 sions every time the system is booted, if the system even sup‐
238 ports that; if it doesn't support that, you might have to find
239 some other way to make that happen at boot time.
240
241 Reading a saved packet file doesn't require special privileges.
242
243 The packets read from the handle may include a ``pseudo-header'' con‐
244 taining various forms of packet meta-data, and probably includes a
245 link-layer header whose contents can differ for different network
246 interfaces. To determine the format of the packets supplied by the
247 handle, call pcap_datalink(); http://www.tcpdump.org/linktypes.html
248 lists the values it returns and describes the packet formats that cor‐
249 respond to those values.
250
251 To obtain the FILE * corresponding to a pcap_t opened for a ``save‐
252 file'', call pcap_file().
253
254 Routines
255
256 pcap_create(3PCAP)
257 get a pcap_t for live capture
258
259 pcap_activate(3PCAP)
260 activate a pcap_t for live capture
261
262 pcap_findalldevs(3PCAP)
263 get a list of devices that can be opened for a live cap‐
264 ture
265
266 pcap_freealldevs(3PCAP)
267 free list of devices
268
269 pcap_lookupdev(3PCAP)
270 get first non-loopback device on that list
271
272 pcap_open_offline(3PCAP)
273 open a pcap_t for a ``savefile'', given a pathname
274
275 pcap_fopen_offline(3PCAP)
276 open a pcap_t for a ``savefile'', given a FILE *
277
278 pcap_open_dead(3PCAP)
279 create a ``fake'' pcap_t
280
281 pcap_close(3PCAP)
282 close a pcap_t
283
284 pcap_set_snaplen(3PCAP)
285 set the snapshot length for a not-yet-activated pcap_t
286 for live capture
287
288 pcap_snapshot(3PCAP)
289 get the snapshot length for a pcap_t
290
291 pcap_set_promisc(3PCAP)
292 set promiscuous mode for a not-yet-activated pcap_t for
293 live capture
294
295 pcap_set_rfmon(3PCAP)
296 set monitor mode for a not-yet-activated pcap_t for live
297 capture
298
299 pcap_can_set_rfmon(3PCAP)
300 determine whether monitor mode can be set for a pcap_t
301 for live capture
302
303 pcap_set_timeout(3PCAP)
304 set read timeout for a not-yet-activated pcap_t for live
305 capture
306
307 pcap_set_buffer_size(3PCAP)
308 set buffer size for a not-yet-activated pcap_t for live
309 capture
310
311 pcap_set_tstamp_type(3PCAP)
312 set time stamp type for a not-yet-activated pcap_t for
313 live capture
314
315 pcap_list_tstamp_types(3PCAP)
316 get list of available time stamp types for a not-yet-
317 activated pcap_t for live capture
318
319 pcap_free_tstamp_types(3PCAP)
320 free list of available time stamp types
321
322 pcap_tstamp_type_val_to_name(3PCAP)
323 get name for a time stamp type
324
325 pcap_tstamp_type_val_to_description(3PCAP)
326 get description for a time stamp type
327
328 pcap_tstamp_name_to_val(3PCAP)
329 get time stamp type corresponding to a name
330
331 pcap_datalink(3PCAP)
332 get link-layer header type for a pcap_t
333
334 pcap_file(3PCAP)
335 get the FILE * for a pcap_t opened for a ``savefile''
336
337 pcap_is_swapped(3PCAP)
338 determine whether a ``savefile'' being read came from a
339 machine with the opposite byte order
340
341 pcap_major_version(3PCAP)
342 pcap_minor_version(3PCAP)
343 get the major and minor version of the file format ver‐
344 sion for a ``savefile''
345
346 Selecting a link-layer header type for a live capture
347 Some devices may provide more than one link-layer header type. To
348 obtain a list of all link-layer header types provided by a device, call
349 pcap_list_datalinks() on an activated pcap_t for the device. To free a
350 list of link-layer header types, call pcap_free_datalinks(). To set
351 the link-layer header type for a device, call pcap_set_datalink().
352 This should be done after the device has been activated but before any
353 packets are read and before any filters are compiled or installed.
354
355 Routines
356
357 pcap_list_datalinks(3PCAP)
358 get a list of link-layer header types for a device
359
360 pcap_free_datalinks(3PCAP)
361 free list of link-layer header types
362
363 pcap_set_datalink(3PCAP)
364 set link-layer header type for a device
365
366 pcap_datalink_val_to_name(3PCAP)
367 get name for a link-layer header type
368
369 pcap_datalink_val_to_description(3PCAP)
370 get description for a link-layer header type
371
372 pcap_datalink_name_to_val(3PCAP)
373 get link-layer header type corresponding to a name
374
375 Reading packets
376 Packets are read with pcap_dispatch() or pcap_loop(), which process one
377 or more packets, calling a callback routine for each packet, or with
378 pcap_next() or pcap_next_ex(), which return the next packet. The call‐
379 back for pcap_dispatch() and pcap_loop() is supplied a pointer to a
380 struct pcap_pkthdr, which includes the following members:
381
382 ts a struct timeval containing the time when the packet was
383 captured
384
385 caplen a bpf_u_int32 giving the number of bytes of the packet
386 that are available from the capture
387
388 len a bpf_u_int32 giving the length of the packet, in bytes
389 (which might be more than the number of bytes available
390 from the capture, if the length of the packet is larger
391 than the maximum number of bytes to capture).
392
393 The callback is also supplied a const u_char pointer to the first
394 caplen (as given in the struct pcap_pkthdr mentioned above) bytes of
395 data from the packet. This won't necessarily be the entire packet; to
396 capture the entire packet, you will have to provide a value for snaplen
397 in your call to pcap_set_snaplen() that is sufficiently large to get
398 all of the packet's data - a value of 65535 should be sufficient on
399 most if not all networks). When reading from a ``savefile'', the snap‐
400 shot length specified when the capture was performed will limit the
401 amount of packet data available.
402
403 pcap_next() is passed an argument that points to a struct pcap_pkthdr
404 structure, and fills it in with the time stamp and length values for
405 the packet. It returns a const u_char to the first caplen bytes of the
406 packet on success, and NULL on error.
407
408 pcap_next_ex() is passed two pointer arguments, one of which points to
409 a structpcap_pkthdr* and one of which points to a const u_char*. It
410 sets the first pointer to point to a struct pcap_pkthdr structure with
411 the time stamp and length values for the packet, and sets the second
412 pointer to point to the first caplen bytes of the packet.
413
414 To force the loop in pcap_dispatch() or pcap_loop() to terminate, call
415 pcap_breakloop().
416
417 By default, when reading packets from an interface opened for a live
418 capture, pcap_dispatch(), pcap_next(), and pcap_next_ex() will, if no
419 packets are currently available to be read, block waiting for packets
420 to become available. On some, but not all, platforms, if a read time‐
421 out was specified, the wait will terminate after the read timeout
422 expires; applications should be prepared for this, as it happens on
423 some platforms, but should not rely on it, as it does not happen on
424 other platforms.
425
426 A handle can be put into ``non-blocking mode'', so that those routines
427 will, rather than blocking, return an indication that no packets are
428 available to read. Call pcap_setnonblock() to put a handle into non-
429 blocking mode or to take it out of non-blocking mode; call pcap_getnon‐
430 block() to determine whether a handle is in non-blocking mode. Note
431 that non-blocking mode does not work correctly in Mac OS X 10.6.
432
433 Non-blocking mode is often combined with routines such as select(2) or
434 poll(2) or other routines a platform offers to wait for the availabil‐
435 ity of data on any of a set of descriptors. To obtain, for a handle, a
436 descriptor that can be used in those routines, call
437 pcap_get_selectable_fd(). Not all handles have such a descriptor
438 available; pcap_get_selectable_fd() will return -1 if no such descrip‐
439 tor exists. In addition, for various reasons, one or more of those
440 routines will not work properly with the descriptor; the documentation
441 for pcap_get_selectable_fd() gives details.
442
443 Routines
444
445 pcap_dispatch(3PCAP)
446 read a bufferful of packets from a pcap_t open for a live
447 capture or the full set of packets from a pcap_t open for
448 a ``savefile''
449
450 pcap_loop(3PCAP)
451 read packets from a pcap_t until an interrupt or error
452 occurs
453
454 pcap_next(3PCAP)
455 read the next packet from a pcap_t without an indication
456 whether an error occurred
457
458 pcap_next_ex(3PCAP)
459 read the next packet from a pcap_t with an error indica‐
460 tion on an error
461
462 pcap_breakloop(3PCAP)
463 prematurely terminate the loop in pcap_dispatch() or
464 pcap_loop()
465
466 pcap_setnonblock(3PCAP)
467 set or clear non-blocking mode on a pcap_t
468
469 pcap_getnonblock(3PCAP)
470 get the state of non-blocking mode for a pcap_t
471
472 pcap_get_selectable_fd(3PCAP)
473 attempt to get a descriptor for a pcap_t that can be used
474 in calls such as select(2) and poll(2)
475
476 Filters
477 In order to cause only certain packets to be returned when reading
478 packets, a filter can be set on a handle. For a live capture, the fil‐
479 tering will be performed in kernel mode, if possible, to avoid copying
480 ``uninteresting'' packets from the kernel to user mode.
481
482 A filter can be specified as a text string; the syntax and semantics of
483 the string are as described by pcap-filter(7). A filter string is com‐
484 piled into a program in a pseudo-machine-language by pcap_compile() and
485 the resulting program can be made a filter for a handle with pcap_set‐
486 filter(). The result of pcap_compile() can be freed with a call to
487 pcap_freecode(). pcap_compile() may require a network mask for certain
488 expressions in the filter string; pcap_lookupnet() can be used to find
489 the network address and network mask for a given capture device.
490
491 A compiled filter can also be applied directly to a packet that has
492 been read using pcap_offline_filter().
493
494 Routines
495
496 pcap_compile(3PCAP)
497 compile filter expression to a pseudo-machine-language
498 code program
499
500 pcap_freecode(3PCAP)
501 free a filter program
502
503 pcap_setfilter(3PCAP)
504 set filter for a pcap_t
505
506 pcap_lookupnet(3PCAP)
507 get network address and network mask for a capture device
508
509 pcap_offline_filter(3PCAP)
510 apply a filter program to a packet
511
512 Incoming and outgoing packets
513 By default, libpcap will attempt to capture both packets sent by the
514 machine and packets received by the machine. To limit it to capturing
515 only packets received by the machine or, if possible, only packets sent
516 by the machine, call pcap_setdirection().
517
518 Routines
519
520 pcap_setdirection(3PCAP)
521 specify whether to capture incoming packets, outgoing
522 packets, or both
523
524 Capture statistics
525 To get statistics about packets received and dropped in a live capture,
526 call pcap_stats().
527
528 Routines
529
530 pcap_stats(3PCAP)
531 get capture statistics
532
533 Opening a handle for writing captured packets
534 To open a ``savefile`` to which to write packets, given the pathname
535 the ``savefile'' should have, call pcap_dump_open(). To open a ``save‐
536 file`` to which to write packets, given the pathname the ``savefile''
537 should have, call pcap_dump_open(); to set up a handle for a ``save‐
538 file'', given a FILE * referring to a file already opened for writing,
539 call pcap_dump_fopen(). They each return pointers to a pcap_dumper_t,
540 which is the handle used for writing packets to the ``savefile''. If
541 it succeeds, it will have created the file if it doesn't exist and
542 truncated the file if it does exist. To close a pcap_dumper_t, call
543 pcap_dump_close().
544
545 Routines
546
547 pcap_dump_open(3PCAP)
548 open a pcap_dumper_t for a ``savefile``, given a pathname
549
550 pcap_dump_fopen(3PCAP)
551 open a pcap_dumper_t for a ``savefile``, given a FILE *
552
553 pcap_dump_close(3PCAP)
554 close a pcap_dumper_t
555
556 pcap_dump_file(3PCAP)
557 get the FILE * for a pcap_dumper_t opened for a ``save‐
558 file''
559
560 Writing packets
561 To write a packet to a pcap_dumper_t, call pcap_dump(). Packets writ‐
562 ten with pcap_dump() may be buffered, rather than being immediately
563 written to the ``savefile''. Closing the pcap_dumper_t will cause all
564 buffered-but-not-yet-written packets to be written to the ``savefile''.
565 To force all packets written to the pcap_dumper_t, and not yet written
566 to the ``savefile'' because they're buffered by the pcap_dumper_t, to
567 be written to the ``savefile'', without closing the pcap_dumper_t, call
568 pcap_dump_flush().
569
570 Routines
571
572 pcap_dump(3PCAP)
573 write packet to a pcap_dumper_t
574
575 pcap_dump_flush(3PCAP)
576 flush buffered packets written to a pcap_dumper_t to the
577 ``savefile''
578
579 pcap_dump_ftell(3PCAP)
580 get current file position for a pcap_dumper_t
581
582 Injecting packets
583 If you have the required privileges, you can inject packets onto a net‐
584 work with a pcap_t for a live capture, using pcap_inject() or
585 pcap_sendpacket(). (The two routines exist for compatibility with both
586 OpenBSD and WinPcap; they perform the same function, but have different
587 return values.)
588
589 Routines
590
591 pcap_inject(3PCAP)
592 pcap_sendpacket(3PCAP)
593 transmit a packet
594
595 Reporting errors
596 Some routines return error or warning status codes; to convert them to
597 a string, use pcap_statustostr().
598
599 Routines
600
601 pcap_statustostr(3PCAP)
602 get a string for an error or warning status code
603
604 Getting library version information
605 To get a string giving version information about libpcap, call
606 pcap_library_version().
607
608 Routines
609
610 pcap_library_version(3PCAP)
611 get library version string
612
614 In versions of libpcap prior to 1.0, the pcap.h header file was not in
615 a pcap directory on most platforms; if you are writing an application
616 that must work on versions of libpcap prior to 1.0, include <pcap.h>,
617 which will include <pcap/pcap.h> for you, rather than including
618 <pcap/pcap.h>.
619
620 pcap_create() and pcap_activate() were not available in versions of
621 libpcap prior to 1.0; if you are writing an application that must work
622 on versions of libpcap prior to 1.0, either use pcap_open_live() to get
623 a handle for a live capture or, if you want to be able to use the addi‐
624 tional capabilities offered by using pcap_create() and pcap_activate(),
625 use an autoconf(1) script or some other configuration script to check
626 whether the libpcap 1.0 APIs are available and use them only if they
627 are.
628
630 autoconf(1), tcpdump(8), tcpslice(8), pcap-filter(7), pfconfig(8),
631 usermod(1M)
632
634 The original authors of libpcap are:
635
636 Van Jacobson, Craig Leres and Steven McCanne, all of the Lawrence
637 Berkeley National Laboratory, University of California, Berkeley, CA.
638
639 The current version is available from "The Tcpdump Group"'s Web site at
640
641 http://www.tcpdump.org/
642
644 Please send problems, bugs, questions, desirable enhancements, etc. to:
645
646 tcpdump-workers@lists.tcpdump.org
647
648
649
650 1 July 2013 PCAP(3PCAP)