]> The Tcpdump Group git mirrors - libpcap/blob - pcap.3
From Pawel Pokrywka: add support for requesting that only received
[libpcap] / pcap.3
1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3,v 1.66 2005-05-03 18:54:01 guy Exp $
2 .\"
3 .\" Copyright (c) 1994, 1996, 1997
4 .\" The Regents of the University of California. All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that: (1) source code distributions
8 .\" retain the above copyright notice and this paragraph in its entirety, (2)
9 .\" distributions including binary code include the above copyright notice and
10 .\" this paragraph in its entirety in the documentation or other materials
11 .\" provided with the distribution, and (3) all advertising materials mentioning
12 .\" features or use of this software display the following acknowledgement:
13 .\" ``This product includes software developed by the University of California,
14 .\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 .\" the University nor the names of its contributors may be used to endorse
16 .\" or promote products derived from this software without specific prior
17 .\" written permission.
18 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 .\"
22 .TH PCAP 3 "27 February 2004"
23 .SH NAME
24 pcap \- Packet Capture library
25 .SH SYNOPSIS
26 .nf
27 .ft B
28 #include <pcap.h>
29 .ft
30 .LP
31 .nf
32 .ft B
33 char errbuf[PCAP_ERRBUF_SIZE];
34 .ft
35 .LP
36 .ft B
37 pcap_t *pcap_open_live(const char *device, int snaplen,
38 .ti +8
39 int promisc, int to_ms, char *errbuf)
40 pcap_t *pcap_open_dead(int linktype, int snaplen)
41 pcap_t *pcap_open_offline(const char *fname, char *errbuf)
42 pcap_t *pcap_fopen_offline(FILE *fp, char *errbuf)
43 pcap_dumper_t *pcap_dump_open(pcap_t *p, const char *fname)
44 pcap_dumper_t *pcap_dump_fopen(pcap_t *p, FILE *fp)
45 .ft
46 .LP
47 .ft B
48 int pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);
49 int pcap_getnonblock(pcap_t *p, char *errbuf);
50 .ft
51 .LP
52 .ft B
53 int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
54 void pcap_freealldevs(pcap_if_t *alldevs)
55 char *pcap_lookupdev(char *errbuf)
56 int pcap_lookupnet(const char *device, bpf_u_int32 *netp,
57 .ti +8
58 bpf_u_int32 *maskp, char *errbuf)
59 .ft
60 .LP
61 .ft B
62 typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
63 .ti +8
64 const u_char *bytes);
65 .ft B
66 int pcap_dispatch(pcap_t *p, int cnt,
67 .ti +8
68 pcap_handler callback, u_char *user)
69 int pcap_loop(pcap_t *p, int cnt,
70 .ti +8
71 pcap_handler callback, u_char *user)
72 void pcap_dump(u_char *user, struct pcap_pkthdr *h,
73 .ti +8
74 u_char *sp)
75 .ft
76 .LP
77 .ft B
78 int pcap_compile(pcap_t *p, struct bpf_program *fp,
79 .ti +8
80 char *str, int optimize, bpf_u_int32 netmask)
81 int pcap_setfilter(pcap_t *p, struct bpf_program *fp)
82 void pcap_freecode(struct bpf_program *)
83 int pcap_setdirection(pcap_t *p, direction_t d)
84 .ft
85 .LP
86 .ft B
87 const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)
88 int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
89 .ti +8
90 const u_char **pkt_data)
91 .ft
92 .LP
93 .ft B
94 void pcap_breakloop(pcap_t *)
95 .ft
96 .LP
97 .ft B
98 int pcap_inject(pcap_t *p, const void *buf, size_t size)
99 int pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
100 .ft
101 .LP
102 .ft B
103 int pcap_datalink(pcap_t *p)
104 int pcap_list_datalinks(pcap_t *p, int **dlt_buf);
105 int pcap_set_datalink(pcap_t *p, int dlt);
106 int pcap_datalink_name_to_val(const char *name);
107 const char *pcap_datalink_val_to_name(int dlt);
108 const char *pcap_datalink_val_to_description(int dlt);
109 int pcap_snapshot(pcap_t *p)
110 int pcap_is_swapped(pcap_t *p)
111 int pcap_major_version(pcap_t *p)
112 int pcap_minor_version(pcap_t *p)
113 int pcap_stats(pcap_t *p, struct pcap_stat *ps)
114 FILE *pcap_file(pcap_t *p)
115 int pcap_fileno(pcap_t *p)
116 int pcap_get_selectable_fd(pcap_t *p);
117 void pcap_perror(pcap_t *p, char *prefix)
118 char *pcap_geterr(pcap_t *p)
119 char *pcap_strerror(int error)
120 const char *pcap_lib_version(void)
121 .ft
122 .LP
123 .ft B
124 void pcap_close(pcap_t *p)
125 int pcap_dump_flush(pcap_dumper_t *p)
126 FILE *pcap_dump_file(pcap_dumper_t *p)
127 void pcap_dump_close(pcap_dumper_t *p)
128 .ft
129 .fi
130 .SH DESCRIPTION
131 The Packet Capture library
132 provides a high level interface to packet capture systems. All packets
133 on the network, even those destined for other hosts, are accessible
134 through this mechanism.
135 .PP
136 .SH ROUTINES
137 NOTE:
138 .I errbuf
139 in
140 .BR pcap_open_live() ,
141 .BR pcap_open_dead() ,
142 .BR pcap_open_offline() ,
143 .BR pcap_fopen_offline() ,
144 .BR pcap_setnonblock() ,
145 .BR pcap_getnonblock() ,
146 .BR pcap_findalldevs() ,
147 .BR pcap_lookupdev() ,
148 and
149 .B pcap_lookupnet()
150 is assumed to be able to hold at least
151 .B PCAP_ERRBUF_SIZE
152 chars.
153 .PP
154 .B pcap_open_live()
155 is used to obtain a packet capture descriptor to look
156 at packets on the network.
157 .I device
158 is a string that specifies the network device to open; on Linux systems
159 with 2.2 or later kernels, a
160 .I device
161 argument of "any" or
162 .B NULL
163 can be used to capture packets from all interfaces.
164 .I snaplen
165 specifies the maximum number of bytes to capture. If this value is less
166 than the size of a packet that is captured, only the first
167 .I snaplen
168 bytes of that packet will be captured and provided as packet data. A
169 value of 65535 should be sufficient, on most if not all networks, to
170 capture all the data available from the packet.
171 .I promisc
172 specifies if the interface is to be put into promiscuous mode.
173 (Note that even if this parameter is false, the interface
174 could well be in promiscuous mode for some other reason.) For now, this
175 doesn't work on the "any" device; if an argument of "any" or NULL is
176 supplied, the
177 .I promisc
178 flag is ignored.
179 .I to_ms
180 specifies the read timeout in milliseconds. The read timeout is used to
181 arrange that the read not necessarily return immediately when a packet
182 is seen, but that it wait for some amount of time to allow more packets
183 to arrive and to read multiple packets from the OS kernel in one
184 operation. Not all platforms support a read timeout; on platforms that
185 don't, the read timeout is ignored. A zero value for
186 .IR to_ms ,
187 on platforms that support a read timeout,
188 will cause a read to wait forever to allow enough packets to
189 arrive, with no timeout.
190 .I errbuf
191 is used to return error or warning text. It will be set to error text when
192 .B pcap_open_live()
193 fails and returns
194 .BR NULL .
195 .I errbuf
196 may also be set to warning text when
197 .B pcap_open_live()
198 succeds; to detect this case the caller should store a zero-length string in
199 .I errbuf
200 before calling
201 .B pcap_open_live()
202 and display the warning to the user if
203 .I errbuf
204 is no longer a zero-length string.
205 .PP
206 .B pcap_open_dead()
207 is used for creating a
208 .B pcap_t
209 structure to use when calling the other functions in libpcap. It is
210 typically used when just using libpcap for compiling BPF code.
211 .PP
212 .B pcap_open_offline()
213 is called to open a ``savefile'' for reading.
214 .I fname
215 specifies the name of the file to open. The file has
216 the same format as those used by
217 .B tcpdump(1)
218 and
219 .BR tcpslice(1) .
220 The name "-" in a synonym for
221 .BR stdin .
222 Alternatively, you may call
223 .B pcap_fopen_offline()
224 to read dumped data from an existing open stream
225 .IR fp .
226 Note that on Windows, that stream should be opened in binary mode.
227 .I errbuf
228 is used to return error text and is only set when
229 .B pcap_open_offline()
230 or
231 .B pcap_fopen_offline()
232 fails and returns
233 .BR NULL .
234 .PP
235 .B pcap_dump_open()
236 is called to open a ``savefile'' for writing. The name "-" in a synonym
237 for
238 .BR stdout .
239 .B NULL
240 is returned on failure.
241 .I p
242 is a
243 .I pcap
244 struct as returned by
245 .B pcap_open_offline()
246 or
247 .BR pcap_open_live() .
248 .I fname
249 specifies the name of the file to open. Alternatively, you may call
250 .B pcap_dump_fopen()
251 to write data to an existing open stream
252 .IR fp .
253 Note that on Windows, that stream should be opened in binary mode.
254 If
255 .B NULL
256 is returned,
257 .B pcap_geterr()
258 can be used to get the error text.
259 .PP
260 .PP
261 .B pcap_setnonblock()
262 puts a capture descriptor, opened with
263 .BR pcap_open_live() ,
264 into ``non-blocking'' mode, or takes it out of ``non-blocking'' mode,
265 depending on whether the
266 .I nonblock
267 argument is non-zero or zero. It has no effect on ``savefiles''.
268 If there is an error, \-1 is returned and
269 .I errbuf
270 is filled in with an appropriate error message; otherwise, 0 is
271 returned.
272 In
273 ``non-blocking'' mode, an attempt to read from the capture descriptor
274 with
275 .B pcap_dispatch()
276 will, if no packets are currently available to be read, return 0
277 immediately rather than blocking waiting for packets to arrive.
278 .B pcap_loop()
279 and
280 .B pcap_next()
281 will not work in ``non-blocking'' mode.
282 .PP
283 .B pcap_getnonblock()
284 returns the current ``non-blocking'' state of the capture descriptor; it
285 always returns 0 on ``savefiles''.
286 If there is an error, \-1 is returned and
287 .I errbuf
288 is filled in with an appropriate error message.
289 .PP
290 .B pcap_findalldevs()
291 constructs a list of network devices that can be opened with
292 .BR pcap_open_live() .
293 (Note that there may be network devices that cannot be opened with
294 .BR pcap_open_live()
295 by the
296 process calling
297 .BR pcap_findalldevs() ,
298 because, for example, that process might not have sufficient privileges
299 to open them for capturing; if so, those devices will not appear on the
300 list.)
301 .I alldevsp
302 is set to point to the first element of the list; each element of the
303 list is of type
304 .BR pcap_if_t ,
305 and has the following members:
306 .RS
307 .TP
308 .B next
309 if not
310 .BR NULL ,
311 a pointer to the next element in the list;
312 .B NULL
313 for the last element of the list
314 .TP
315 .B name
316 a pointer to a string giving a name for the device to pass to
317 .B pcap_open_live()
318 .TP
319 .B description
320 if not
321 .BR NULL ,
322 a pointer to a string giving a human-readable description of the device
323 .TP
324 .B addresses
325 a pointer to the first element of a list of addresses for the interface
326 .TP
327 .B flags
328 interface flags:
329 .RS
330 .TP
331 .B PCAP_IF_LOOPBACK
332 set if the interface is a loopback interface
333 .RE
334 .RE
335 .PP
336 Each element of the list of addresses is of type
337 .BR pcap_addr_t ,
338 and has the following members:
339 .RS
340 .TP
341 .B next
342 if not
343 .BR NULL ,
344 a pointer to the next element in the list;
345 .B NULL
346 for the last element of the list
347 .TP
348 .B addr
349 a pointer to a
350 .B "struct sockaddr"
351 containing an address
352 .TP
353 .B netmask
354 if not
355 .BR NULL ,
356 a pointer to a
357 .B "struct sockaddr"
358 that contains the netmask corresponding to the address pointed to by
359 .B addr
360 .TP
361 .B broadaddr
362 if not
363 .BR NULL ,
364 a pointer to a
365 .B "struct sockaddr"
366 that contains the broadcast address corresponding to the address pointed
367 to by
368 .BR addr ;
369 may be null if the interface doesn't support broadcasts
370 .TP
371 .B dstaddr
372 if not
373 .BR NULL ,
374 a pointer to a
375 .B "struct sockaddr"
376 that contains the destination address corresponding to the address pointed
377 to by
378 .BR addr ;
379 may be null if the interface isn't a point-to-point interface
380 .RE
381 .PP
382 Note that not all the addresses in the list of addresses are
383 necessarily IPv4 or IPv6 addresses - you must check the
384 .B sa_family
385 member of the
386 .B "struct sockaddr"
387 before interpreting the contents of the address.
388 .PP
389 .B \-1
390 is returned on failure, in which case
391 .B errbuf
392 is filled in with an appropriate error message;
393 .B 0
394 is returned on success.
395 .PP
396 .B pcap_freealldevs()
397 is used to free a list allocated by
398 .BR pcap_findalldevs() .
399 .PP
400 .B pcap_lookupdev()
401 returns a pointer to a network device suitable for use with
402 .B pcap_open_live()
403 and
404 .BR pcap_lookupnet() .
405 If there is an error,
406 .B NULL
407 is returned and
408 .I errbuf
409 is filled in with an appropriate error message.
410 .PP
411 .B pcap_lookupnet()
412 is used to determine the network number and mask
413 associated with the network device
414 .BR device .
415 Both
416 .I netp
417 and
418 .I maskp
419 are
420 .I bpf_u_int32
421 pointers.
422 A return of \-1 indicates an error in which case
423 .I errbuf
424 is filled in with an appropriate error message.
425 .PP
426 .B pcap_dispatch()
427 is used to collect and process packets.
428 .I cnt
429 specifies the maximum number of packets to process before returning.
430 This is not a minimum number; when reading a live capture, only one
431 bufferful of packets is read at a time, so fewer than
432 .I cnt
433 packets may be processed. A
434 .I cnt
435 of \-1 processes all the packets received in one buffer when reading a
436 live capture, or all the packets in the file when reading a
437 ``savefile''.
438 .I callback
439 specifies a routine to be called with three arguments:
440 a
441 .I u_char
442 pointer which is passed in from
443 .BR pcap_dispatch() ,
444 a
445 .I const struct pcap_pkthdr
446 pointer to a structure with the following members:
447 .RS
448 .TP
449 .B ts
450 a
451 .I struct timeval
452 containing the time when the packet was captured
453 .TP
454 .B caplen
455 a
456 .I bpf_u_int32
457 giving the number of bytes of the packet that are available from the
458 capture
459 .TP
460 .B len
461 a
462 .I bpf_u_int32
463 giving the length of the packet, in bytes (which might be more than the
464 number of bytes available from the capture, if the length of the packet
465 is larger than the maximum number of bytes to capture)
466 .RE
467 .PP
468 and a
469 .I const u_char
470 pointer to the first
471 .B caplen
472 (as given in the
473 .I struct pcap_pkthdr
474 a pointer to which is passed to the callback routine)
475 bytes of data from the packet (which won't necessarily be the entire
476 packet; to capture the entire packet, you will have to provide a value
477 for
478 .I snaplen
479 in your call to
480 .B pcap_open_live()
481 that is sufficiently large to get all of the packet's data - a value of
482 65535 should be sufficient on most if not all networks).
483 .PP
484 The number of packets read is returned.
485 0 is returned if no packets were read from a live capture (if, for
486 example, they were discarded because they didn't pass the packet filter,
487 or if, on platforms that support a read timeout that starts before any
488 packets arrive, the timeout expires before any packets arrive, or if the
489 file descriptor for the capture device is in non-blocking mode and no
490 packets were available to be read) or if no more packets are available
491 in a ``savefile.'' A return of \-1 indicates
492 an error in which case
493 .B pcap_perror()
494 or
495 .B pcap_geterr()
496 may be used to display the error text.
497 A return of \-2 indicates that the loop terminated due to a call to
498 .B pcap_breakloop()
499 before any packets were processed.
500 .ft B
501 If your application uses pcap_breakloop(),
502 make sure that you explicitly check for \-1 and \-2, rather than just
503 checking for a return value < 0.
504 .ft R
505 .PP
506 .BR NOTE :
507 when reading a live capture,
508 .B pcap_dispatch()
509 will not necessarily return when the read times out; on some platforms,
510 the read timeout isn't supported, and, on other platforms, the timer
511 doesn't start until at least one packet arrives. This means that the
512 read timeout should
513 .B NOT
514 be used in, for example, an interactive application, to allow the packet
515 capture loop to ``poll'' for user input periodically, as there's no
516 guarantee that
517 .B pcap_dispatch()
518 will return after the timeout expires.
519 .PP
520 .B pcap_loop()
521 is similar to
522 .B pcap_dispatch()
523 except it keeps reading packets until
524 .I cnt
525 packets are processed or an error occurs.
526 It does
527 .B not
528 return when live read timeouts occur.
529 Rather, specifying a non-zero read timeout to
530 .B pcap_open_live()
531 and then calling
532 .B pcap_dispatch()
533 allows the reception and processing of any packets that arrive when the
534 timeout occurs.
535 A negative
536 .I cnt
537 causes
538 .B pcap_loop()
539 to loop forever (or at least until an error occurs). \-1 is returned on
540 an error; 0 is returned if
541 .I cnt
542 is exhausted; \-2 is returned if the loop terminated due to a call to
543 .B pcap_breakloop()
544 before any packets were processed.
545 .ft B
546 If your application uses pcap_breakloop(),
547 make sure that you explicitly check for \-1 and \-2, rather than just
548 checking for a return value < 0.
549 .ft R
550 .PP
551 .B pcap_next()
552 reads the next packet (by calling
553 .B pcap_dispatch()
554 with a
555 .I cnt
556 of 1) and returns a
557 .I u_char
558 pointer to the data in that packet. (The
559 .I pcap_pkthdr
560 struct for that packet is not supplied.)
561 .B NULL
562 is returned if an error occured, or if no packets were read from a live
563 capture (if, for example, they were discarded because they didn't pass
564 the packet filter, or if, on platforms that support a read timeout that
565 starts before any packets arrive, the timeout expires before any packets
566 arrive, or if the file descriptor for the capture device is in
567 non-blocking mode and no packets were available to be read), or if no
568 more packets are available in a ``savefile.'' Unfortunately, there is
569 no way to determine whether an error occured or not.
570 .PP
571 .B pcap_next_ex()
572 reads the next packet and returns a success/failure indication:
573 .RS
574 .TP
575 1
576 the packet was read without problems
577 .TP
578 0
579 packets are being read from a live capture, and the timeout expired
580 .TP
581 \-1
582 an error occurred while reading the packet
583 .TP
584 \-2
585 packets are being read from a ``savefile'', and there are no more
586 packets to read from the savefile.
587 .RE
588 .PP
589 If the packet was read without problems, the pointer pointed to by the
590 .I pkt_header
591 argument is set to point to the
592 .I pcap_pkthdr
593 struct for the packet, and the
594 pointer pointed to by the
595 .I pkt_data
596 argument is set to point to the data in the packet.
597 .PP
598 .B pcap_breakloop()
599 sets a flag that will force
600 .B pcap_dispatch()
601 or
602 .B pcap_loop()
603 to return rather than looping; they will return the number of packets
604 that have been processed so far, or \-2 if no packets have been
605 processed so far.
606 .PP
607 This routine is safe to use inside a signal handler on UNIX or a console
608 control handler on Windows, as it merely sets a flag that is checked
609 within the loop.
610 .PP
611 The flag is checked in loops reading packets from the OS - a signal by
612 itself will not necessarily terminate those loops - as well as in loops
613 processing a set of packets returned by the OS.
614 .ft B
615 Note that if you are catching signals on UNIX systems that support
616 restarting system calls after a signal, and calling pcap_breakloop()
617 in the signal handler, you must specify, when catching those signals,
618 that system calls should NOT be restarted by that signal. Otherwise,
619 if the signal interrupted a call reading packets in a live capture,
620 when your signal handler returns after calling pcap_breakloop(), the
621 call will be restarted, and the loop will not terminate until more
622 packets arrive and the call completes.
623 .PP
624 Note also that, in a multi-threaded application, if one thread is
625 blocked in
626 .BR pcap_dispatch() ,
627 .BR pcap_loop() ,
628 .BR pcap_next() ,
629 or
630 .BR pcap_next_ex() ,
631 a call to
632 .B pcap_breakloop()
633 in a different thread will not unblock that thread; you will need to use
634 whatever mechanism the OS provides for breaking a thread out of blocking
635 calls in order to unblock the thread, such as thread cancellation in
636 systems that support POSIX threads.
637 .ft R
638 .PP
639 Note that
640 .B pcap_next()
641 will, on some platforms, loop reading packets from the OS; that loop
642 will not necessarily be terminated by a signal, so
643 .B pcap_breakloop()
644 should be used to terminate packet processing even if
645 .B pcap_next()
646 is being used.
647 .PP
648 .B pcap_breakloop()
649 does not guarantee that no further packets will be processed by
650 .B pcap_dispatch()
651 or
652 .B pcap_loop()
653 after it is called; at most one more packet might be processed.
654 .PP
655 If \-2 is returned from
656 .B pcap_dispatch()
657 or
658 .BR pcap_loop() ,
659 the flag is cleared, so a subsequent call will resume reading packets.
660 If a positive number is returned, the flag is not cleared, so a
661 subsequent call will return \-2 and clear the flag.
662 .PP
663 .B pcap_inject()
664 sends a raw packet through the network interface;
665 .I buf
666 points to the data of the packet, including the link-layer header, and
667 .I size
668 is the number of bytes in the packet.
669 It returns the number of bytes written on success. A return of \-1
670 indicates an error in which case
671 .B pcap_perror()
672 or
673 .B pcap_geterr()
674 may be used to display the error text.
675 Note that, even if you successfully open the network interface, you
676 might not have permission to send packets on it, or it might not support
677 sending packets; as
678 .I pcap_open_live()
679 doesn't have a flag to indicate whether to open for capturing, sending,
680 or capturing and sending, you cannot request an open that supports
681 sending and be notified at open time whether sending will be possible.
682 Note also that some devices might not support sending packets.
683 .PP
684 Note that, on some platforms, the link-layer header of the packet that's
685 sent might not be the same as the link-layer header of the packet
686 supplied to
687 .BR pcap_inject() ,
688 as the source link-layer address, if the header contains such an
689 address, might be changed to be the address assigned to the interface on
690 which the packet it sent, if the platform doesn't support sending
691 completely raw and unchanged packets.
692 .PP
693 .B pcap_sendpacket()
694 is like
695 .BR pcap_inject() ,
696 but it returns 0 on success and \-1 on failure.
697 .RB ( pcap_inject()
698 comes from OpenBSD;
699 .B pcap_sendpacket()
700 comes from WinPcap. Both are provided for compatibility.)
701 .PP
702 .B pcap_dump()
703 outputs a packet to the ``savefile'' opened with
704 .BR pcap_dump_open() .
705 Note that its calling arguments are suitable for use with
706 .B pcap_dispatch()
707 or
708 .BR pcap_loop() .
709 If called directly, the
710 .I user
711 parameter is of type
712 .I pcap_dumper_t
713 as returned by
714 .BR pcap_dump_open() .
715 .PP
716 .B pcap_compile()
717 is used to compile the string
718 .I str
719 into a filter program.
720 .I program
721 is a pointer to a
722 .I bpf_program
723 struct and is filled in by
724 .BR pcap_compile() .
725 .I optimize
726 controls whether optimization on the resulting code is performed.
727 .I netmask
728 specifies the IPv4 netmask of the network on which packets are being
729 captured; it is used only when checking for IPv4 broadcast addresses in
730 the filter program. If the netmask of the network on which packets are
731 being captured isn't known to the program, or if packets are being
732 captured on the Linux "any" pseudo-interface that can capture on more
733 than one network, a value of 0 can be supplied; tests for IPv4 broadcast
734 addreses won't be done correctly, but all other tests in the filter
735 program will be OK. A return of \-1 indicates an error in which case
736 .BR pcap_geterr()
737 may be used to display the error text.
738 .PP
739 .B pcap_compile_nopcap()
740 is similar to
741 .B pcap_compile()
742 except that instead of passing a pcap structure, one passes the
743 snaplen and linktype explicitly. It is intended to be used for
744 compiling filters for direct BPF usage, without necessarily having
745 called
746 .BR pcap_open() .
747 A return of \-1 indicates an error; the error text is unavailable.
748 .RB ( pcap_compile_nopcap()
749 is a wrapper around
750 .BR pcap_open_dead() ,
751 .BR pcap_compile() ,
752 and
753 .BR pcap_close() ;
754 the latter three routines can be used directly in order to get the error
755 text for a compilation error.)
756 .B
757 .PP
758 .B pcap_setfilter()
759 is used to specify a filter program.
760 .I fp
761 is a pointer to a
762 .I bpf_program
763 struct, usually the result of a call to
764 .BR pcap_compile() .
765 .B \-1
766 is returned on failure, in which case
767 .BR pcap_geterr()
768 may be used to display the error text;
769 .B 0
770 is returned on success.
771 .PP
772 .B pcap_freecode()
773 is used to free up allocated memory pointed to by a
774 .I bpf_program
775 struct generated by
776 .B pcap_compile()
777 when that BPF program is no longer needed, for example after it
778 has been made the filter program for a pcap structure by a call to
779 .BR pcap_setfilter() .
780 .PP
781 .B pcap_setdirection()
782 is used to specify a direction that packets will be captured.
783 .I direction_t
784 is one of the constants
785 .BR D_IN ,
786 .B D_OUT
787 or
788 .BR D_INOUT .
789 .B D_IN
790 will only capture packets received by the device,
791 .B D_OUT
792 will only capture packets sent by the device and
793 .B D_INOUT
794 will capture packets received by or sent by the device.
795 .B D_INOUT
796 is the default setting if this function is not called. This isn't
797 necessarily supported on all platforms; some platforms might return an
798 error, and some other platforms might not support
799 .BR D_OUT .
800 .B \-1
801 is returned on failure,
802 .B 0
803 is returned on success.
804 .PP
805 .B pcap_datalink()
806 returns the link layer type; link layer types it can return include:
807 .PP
808 .RS 5
809 .TP 5
810 .B DLT_NULL
811 BSD loopback encapsulation; the link layer header is a 4-byte field, in
812 .I host
813 byte order, containing a PF_ value from
814 .B socket.h
815 for the network-layer protocol of the packet.
816 .IP
817 Note that ``host byte order'' is the byte order of the machine on which
818 the packets are captured, and the PF_ values are for the OS of the
819 machine on which the packets are captured; if a live capture is being
820 done, ``host byte order'' is the byte order of the machine capturing the
821 packets, and the PF_ values are those of the OS of the machine capturing
822 the packets, but if a ``savefile'' is being read, the byte order and PF_
823 values are
824 .I not
825 necessarily those of the machine reading the capture file.
826 .TP 5
827 .B DLT_EN10MB
828 Ethernet (10Mb, 100Mb, 1000Mb, and up)
829 .TP 5
830 .B DLT_IEEE802
831 IEEE 802.5 Token Ring
832 .TP 5
833 .B DLT_ARCNET
834 ARCNET
835 .TP 5
836 .B DLT_SLIP
837 SLIP; the link layer header contains, in order:
838 .RS 10
839 .LP
840 a 1-byte flag, which is 0 for packets received by the machine and 1 for
841 packets sent by the machine;
842 .LP
843 a 1-byte field, the upper 4 bits of which indicate the type of packet,
844 as per RFC 1144:
845 .RS 5
846 .TP 5
847 0x40
848 an unmodified IP datagram (TYPE_IP);
849 .TP 5
850 0x70
851 an uncompressed-TCP IP datagram (UNCOMPRESSED_TCP), with that byte being
852 the first byte of the raw IP header on the wire, containing the
853 connection number in the protocol field;
854 .TP 5
855 0x80
856 a compressed-TCP IP datagram (COMPRESSED_TCP), with that byte being the
857 first byte of the compressed TCP/IP datagram header;
858 .RE
859 .LP
860 for UNCOMPRESSED_TCP, the rest of the modified IP header, and for
861 COMPRESSED_TCP, the compressed TCP/IP datagram header;
862 .RE
863 .RS 5
864 .LP
865 for a total of 16 bytes; the uncompressed IP datagram follows the header.
866 .RE
867 .TP 5
868 .B DLT_PPP
869 PPP; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC-like
870 framing, with the PPP header following those two bytes, otherwise it's
871 PPP without framing, and the packet begins with the PPP header.
872 .TP 5
873 .B DLT_FDDI
874 FDDI
875 .TP 5
876 .B DLT_ATM_RFC1483
877 RFC 1483 LLC/SNAP-encapsulated ATM; the packet begins with an IEEE 802.2
878 LLC header.
879 .TP 5
880 .B DLT_RAW
881 raw IP; the packet begins with an IP header.
882 .TP 5
883 .B DLT_PPP_SERIAL
884 PPP in HDLC-like framing, as per RFC 1662, or Cisco PPP with HDLC
885 framing, as per section 4.3.1 of RFC 1547; the first byte will be 0xFF
886 for PPP in HDLC-like framing, and will be 0x0F or 0x8F for Cisco PPP
887 with HDLC framing.
888 .TP 5
889 .B DLT_PPP_ETHER
890 PPPoE; the packet begins with a PPPoE header, as per RFC 2516.
891 .TP 5
892 .B DLT_C_HDLC
893 Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547.
894 .TP 5
895 .B DLT_IEEE802_11
896 IEEE 802.11 wireless LAN
897 .TP 5
898 .B DLT_FRELAY
899 Frame Relay
900 .TP 5
901 .B DLT_LOOP
902 OpenBSD loopback encapsulation; the link layer header is a 4-byte field, in
903 .I network
904 byte order, containing a PF_ value from OpenBSD's
905 .B socket.h
906 for the network-layer protocol of the packet.
907 .IP
908 Note that, if a ``savefile'' is being read, those PF_ values are
909 .I not
910 necessarily those of the machine reading the capture file.
911 .TP 5
912 .B DLT_LINUX_SLL
913 Linux "cooked" capture encapsulation; the link layer header contains, in
914 order:
915 .RS 10
916 .LP
917 a 2-byte "packet type", in network byte order, which is one of:
918 .RS 5
919 .TP 5
920 0
921 packet was sent to us by somebody else
922 .TP 5
923 1
924 packet was broadcast by somebody else
925 .TP 5
926 2
927 packet was multicast, but not broadcast, by somebody else
928 .TP 5
929 3
930 packet was sent by somebody else to somebody else
931 .TP 5
932 4
933 packet was sent by us
934 .RE
935 .LP
936 a 2-byte field, in network byte order, containing a Linux ARPHRD_ value
937 for the link layer device type;
938 .LP
939 a 2-byte field, in network byte order, containing the length of the
940 link layer address of the sender of the packet (which could be 0);
941 .LP
942 an 8-byte field containing that number of bytes of the link layer header
943 (if there are more than 8 bytes, only the first 8 are present);
944 .LP
945 a 2-byte field containing an Ethernet protocol type, in network byte
946 order, or containing 1 for Novell 802.3 frames without an 802.2 LLC
947 header or 4 for frames beginning with an 802.2 LLC header.
948 .RE
949 .TP 5
950 .B DLT_LTALK
951 Apple LocalTalk; the packet begins with an AppleTalk LLAP header.
952 .TP 5
953 .B DLT_PFLOG
954 OpenBSD pflog; the link layer header contains, in order:
955 .RS 10
956 .LP
957 a 1-byte header length, in host byte order;
958 .LP
959 a 4-byte PF_ value, in host byte order;
960 .LP
961 a 2-byte action code, in network byte order, which is one of:
962 .RS 5
963 .TP 5
964 0
965 passed
966 .TP 5
967 1
968 dropped
969 .TP 5
970 2
971 scrubbed
972 .RE
973 .LP
974 a 2-byte reason code, in network byte order, which is one of:
975 .RS 5
976 .TP 5
977 0
978 match
979 .TP 5
980 1
981 bad offset
982 .TP 5
983 2
984 fragment
985 .TP 5
986 3
987 short
988 .TP 5
989 4
990 normalize
991 .TP 5
992 5
993 memory
994 .RE
995 .LP
996 a 16-character interface name;
997 .LP
998 a 16-character ruleset name (only meaningful if subrule is set);
999 .LP
1000 a 4-byte rule number, in network byte order;
1001 .LP
1002 a 4-byte subrule number, in network byte order;
1003 .LP
1004 a 1-byte direction, in network byte order, which is one of:
1005 .RS 5
1006 .TP 5
1007 0
1008 incoming or outgoing
1009 .TP 5
1010 1
1011 incoming
1012 .TP 5
1013 2
1014 outgoing
1015 .RE
1016 .RE
1017 .TP 5
1018 .B DLT_PRISM_HEADER
1019 Prism monitor mode information followed by an 802.11 header.
1020 .TP 5
1021 .B DLT_IP_OVER_FC
1022 RFC 2625 IP-over-Fibre Channel, with the link-layer header being the
1023 Network_Header as described in that RFC.
1024 .TP 5
1025 .B DLT_SUNATM
1026 SunATM devices; the link layer header contains, in order:
1027 .RS 10
1028 .LP
1029 a 1-byte flag field, containing a direction flag in the uppermost bit,
1030 which is set for packets transmitted by the machine and clear for
1031 packets received by the machine, and a 4-byte traffic type in the
1032 low-order 4 bits, which is one of:
1033 .RS 5
1034 .TP 5
1035 0
1036 raw traffic
1037 .TP 5
1038 1
1039 LANE traffic
1040 .TP 5
1041 2
1042 LLC-encapsulated traffic
1043 .TP 5
1044 3
1045 MARS traffic
1046 .TP 5
1047 4
1048 IFMP traffic
1049 .TP 5
1050 5
1051 ILMI traffic
1052 .TP 5
1053 6
1054 Q.2931 traffic
1055 .RE
1056 .LP
1057 a 1-byte VPI value;
1058 .LP
1059 a 2-byte VCI field, in network byte order.
1060 .RE
1061 .TP 5
1062 .B DLT_IEEE802_11_RADIO
1063 link-layer information followed by an 802.11 header - see
1064 http://www.shaftnet.org/~pizza/software/capturefrm.txt for a description
1065 of the link-layer information.
1066 .TP 5
1067 .B DLT_ARCNET_LINUX
1068 ARCNET, with no exception frames, reassembled packets rather than raw
1069 frames, and an extra 16-bit offset field between the destination host
1070 and type bytes.
1071 .TP 5
1072 .B DLT_LINUX_IRDA
1073 Linux-IrDA packets, with a
1074 .B DLT_LINUX_SLL
1075 header followed by the IrLAP header.
1076 .RE
1077 .PP
1078 .B pcap_list_datalinks()
1079 is used to get a list of the supported data link types of the interface
1080 associated with the pcap descriptor.
1081 .B pcap_list_datalinks()
1082 allocates an array to hold the list and sets
1083 .IR *dlt_buf .
1084 The caller is responsible for freeing the array.
1085 .B \-1
1086 is returned on failure;
1087 otherwise, the number of data link types in the array is returned.
1088 .PP
1089 .B pcap_set_datalink()
1090 is used to set the current data link type of the pcap descriptor
1091 to the type specified by
1092 .IR dlt .
1093 .B \-1
1094 is returned on failure.
1095 .PP
1096 .B pcap_datalink_name_to_val()
1097 translates a data link type name, which is a
1098 .B DLT_
1099 name with the
1100 .B DLT_
1101 removed, to the corresponding data link type value. The translation
1102 is case-insensitive.
1103 .B \-1
1104 is returned on failure.
1105 .PP
1106 .B pcap_datalink_val_to_name()
1107 translates a data link type value to the corresponding data link type
1108 name. NULL is returned on failure.
1109 .PP
1110 .B pcap_datalink_val_to_description()
1111 translates a data link type value to a short description of that data
1112 link type. NULL is returned on failure.
1113 .PP
1114 .B pcap_snapshot()
1115 returns the snapshot length specified when
1116 .B pcap_open_live()
1117 was called.
1118 .PP
1119 .B pcap_is_swapped()
1120 returns true if the current ``savefile'' uses a different byte order
1121 than the current system.
1122 .PP
1123 .B pcap_major_version()
1124 returns the major number of the file format of the savefile;
1125 .B pcap_minor_version()
1126 returns the minor number of the file format of the savefile. The
1127 version number is stored in the header of the savefile.
1128 .PP
1129 .B pcap_file()
1130 returns the standard I/O stream of the ``savefile,'' if a ``savefile''
1131 was opened with
1132 .BR pcap_open_offline() ,
1133 or NULL, if a network device was opened with
1134 .BR pcap_open_live() .
1135 .PP
1136 .B pcap_stats()
1137 returns 0 and fills in a
1138 .B pcap_stat
1139 struct. The values represent packet statistics from the start of the
1140 run to the time of the call. If there is an error or the underlying
1141 packet capture doesn't support packet statistics, \-1 is returned and
1142 the error text can be obtained with
1143 .B pcap_perror()
1144 or
1145 .BR pcap_geterr() .
1146 .B pcap_stats()
1147 is supported only on live captures, not on ``savefiles''; no statistics
1148 are stored in ``savefiles'', so no statistics are available when reading
1149 from a ``savefile''.
1150 .PP
1151 .B pcap_fileno()
1152 returns the file descriptor number from which captured packets are read,
1153 if a network device was opened with
1154 .BR pcap_open_live() ,
1155 or \-1, if a ``savefile'' was opened with
1156 .BR pcap_open_offline() .
1157 .PP
1158 .B pcap_get_selectable_fd()
1159 returns, on UNIX, a file descriptor number for a file descriptor on
1160 which one can
1161 do a
1162 .B select()
1163 or
1164 .B poll()
1165 to wait for it to be possible to read packets without blocking, if such
1166 a descriptor exists, or \-1, if no such descriptor exists. Some network
1167 devices opened with
1168 .B pcap_open_live()
1169 do not support
1170 .B select()
1171 or
1172 .B poll()
1173 (for example, regular network devices on FreeBSD 4.3 and 4.4, and Endace
1174 DAG devices), so \-1 is returned for those devices.
1175 .PP
1176 Note that on most versions of most BSDs (including Mac OS X)
1177 .B select()
1178 and
1179 .B poll()
1180 do not work correctly on BPF devices;
1181 .B pcap_get_selectable_fd()
1182 will return a file descriptor on most of those versions (the exceptions
1183 being FreeBSD 4.3 and 4.4), a simple
1184 .B select()
1185 or
1186 .B poll()
1187 will not return even after a timeout specified in
1188 .B pcap_open_live()
1189 expires. To work around this, an application that uses
1190 .B select()
1191 or
1192 .B poll()
1193 to wait for packets to arrive must put the
1194 .B pcap_t
1195 in non-blocking mode, and must arrange that the
1196 .B select()
1197 or
1198 .B poll()
1199 have a timeout less than or equal to the timeout specified in
1200 .BR pcap_open_live() ,
1201 and must try to read packets after that timeout expires, regardless of
1202 whether
1203 .B select()
1204 or
1205 .B poll()
1206 indicated that the file descriptor for the
1207 .B pcap_t
1208 is ready to be read or not. (That workaround will not work in FreeBSD
1209 4.3 and later; however, in FreeBSD 4.6 and later,
1210 .B select()
1211 and
1212 .B poll()
1213 work correctly on BPF devices, so the workaround isn't necessary,
1214 although it does no harm.)
1215 .PP
1216 .B pcap_get_selectable_fd()
1217 is not available on Windows.
1218 .PP
1219 .B pcap_perror()
1220 prints the text of the last pcap library error on
1221 .BR stderr ,
1222 prefixed by
1223 .IR prefix .
1224 .PP
1225 .B pcap_geterr()
1226 returns the error text pertaining to the last pcap library error.
1227 .BR NOTE :
1228 the pointer it returns will no longer point to a valid error message
1229 string after the
1230 .B pcap_t
1231 passed to it is closed; you must use or copy the string before closing
1232 the
1233 .BR pcap_t .
1234 .PP
1235 .B pcap_strerror()
1236 is provided in case
1237 .BR strerror (1)
1238 isn't available.
1239 .PP
1240 .B pcap_lib_version()
1241 returns a pointer to a string giving information about the version of
1242 the libpcap library being used; note that it contains more information
1243 than just a version number.
1244 .PP
1245 .B pcap_close()
1246 closes the files associated with
1247 .I p
1248 and deallocates resources.
1249 .PP
1250 .B pcap_dump_file()
1251 returns the standard I/O stream of the ``savefile'' opened by
1252 .BR pcap_dump_open().
1253 .PP
1254 .B pcap_dump_flush()
1255 flushes the output buffer to the ``savefile,'' so that any packets
1256 written with
1257 .B pcap_dump()
1258 but not yet written to the ``savefile'' will be written.
1259 .B \-1
1260 is returned on error, 0 on success.
1261 .PP
1262 .B pcap_dump_close()
1263 closes the ``savefile.''
1264 .PP
1265 .SH SEE ALSO
1266 tcpdump(1), tcpslice(1)
1267 .SH AUTHORS
1268 The original authors are:
1269 .LP
1270 Van Jacobson,
1271 Craig Leres and
1272 Steven McCanne, all of the
1273 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
1274 .LP
1275 The current version is available from "The Tcpdump Group"'s Web site at
1276 .LP
1277 .RS
1278 .I http://www.tcpdump.org/
1279 .RE
1280 .SH BUGS
1281 Please send problems, bugs, questions, desirable enhancements, etc. to:
1282 .LP
1283 .RS
1284 tcpdump-workers@tcpdump.org
1285 .RE
1286 .LP
1287 Please send source code contributions, etc. to:
1288 .LP
1289 .RS
1290 patches@tcpdump.org
1291 .RE