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