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