]> The Tcpdump Group git mirrors - libpcap/blob - pcap.3
OK, we'll be putting out 0.9.2 soon, so add the API changes back in
[libpcap] / pcap.3
1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3,v 1.71 2005-07-07 01:57: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, 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 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.
693 .PP
694 .B pcap_sendpacket()
695 is like
696 .BR pcap_inject() ,
697 but it returns 0 on success and \-1 on failure.
698 .RB ( pcap_inject()
699 comes from OpenBSD;
700 .B pcap_sendpacket()
701 comes from WinPcap. Both are provided for compatibility.)
702 .PP
703 .B pcap_dump()
704 outputs a packet to the ``savefile'' opened with
705 .BR pcap_dump_open() .
706 Note that its calling arguments are suitable for use with
707 .B pcap_dispatch()
708 or
709 .BR pcap_loop() .
710 If called directly, the
711 .I user
712 parameter is of type
713 .I pcap_dumper_t
714 as returned by
715 .BR pcap_dump_open() .
716 .PP
717 .B pcap_compile()
718 is used to compile the string
719 .I str
720 into a filter program.
721 .I program
722 is a pointer to a
723 .I bpf_program
724 struct and is filled in by
725 .BR pcap_compile() .
726 .I optimize
727 controls whether optimization on the resulting code is performed.
728 .I netmask
729 specifies the IPv4 netmask of the network on which packets are being
730 captured; it is used only when checking for IPv4 broadcast addresses in
731 the filter program. If the netmask of the network on which packets are
732 being captured isn't known to the program, or if packets are being
733 captured on the Linux "any" pseudo-interface that can capture on more
734 than one network, a value of 0 can be supplied; tests for IPv4 broadcast
735 addreses won't be done correctly, but all other tests in the filter
736 program will be OK. A return of \-1 indicates an error in which case
737 .BR pcap_geterr()
738 may be used to display the error text.
739 .PP
740 .B pcap_compile_nopcap()
741 is similar to
742 .B pcap_compile()
743 except that instead of passing a pcap structure, one passes the
744 snaplen and linktype explicitly. It is intended to be used for
745 compiling filters for direct BPF usage, without necessarily having
746 called
747 .BR pcap_open() .
748 A return of \-1 indicates an error; the error text is unavailable.
749 .RB ( pcap_compile_nopcap()
750 is a wrapper around
751 .BR pcap_open_dead() ,
752 .BR pcap_compile() ,
753 and
754 .BR pcap_close() ;
755 the latter three routines can be used directly in order to get the error
756 text for a compilation error.)
757 .B
758 .PP
759 .B pcap_setfilter()
760 is used to specify a filter program.
761 .I fp
762 is a pointer to a
763 .I bpf_program
764 struct, usually the result of a call to
765 .BR pcap_compile() .
766 .B \-1
767 is returned on failure, in which case
768 .BR pcap_geterr()
769 may be used to display the error text;
770 .B 0
771 is returned on success.
772 .PP
773 .B pcap_freecode()
774 is used to free up allocated memory pointed to by a
775 .I bpf_program
776 struct generated by
777 .B pcap_compile()
778 when that BPF program is no longer needed, for example after it
779 has been made the filter program for a pcap structure by a call to
780 .BR pcap_setfilter() .
781 .PP
782 .B pcap_setdirection()
783 is used to specify a direction that packets will be captured.
784 .I pcap_direction_t
785 is one of the constants
786 .BR PCAP_D_IN ,
787 .B PCAP_D_OUT
788 or
789 .BR PCAP_D_INOUT .
790 .B PCAP_D_IN
791 will only capture packets received by the device,
792 .B PCAP_D_OUT
793 will only capture packets sent by the device and
794 .B PCAP_D_INOUT
795 will capture packets received by or sent by the device.
796 .B PCAP_D_INOUT
797 is the default setting if this function is not called. This isn't
798 necessarily supported on all platforms; some platforms might return an
799 error, and some other platforms might not support
800 .BR PCAP_D_OUT .
801 This operation is not supported if a ``savefile'' is being read.
802 .B \-1
803 is returned on failure,
804 .B 0
805 is returned on success.
806 .PP
807 .B pcap_datalink()
808 returns the link layer type; link layer types it can return include:
809 .PP
810 .RS 5
811 .TP 5
812 .B DLT_NULL
813 BSD loopback encapsulation; the link layer header is a 4-byte field, in
814 .I host
815 byte order, containing a PF_ value from
816 .B socket.h
817 for the network-layer protocol of the packet.
818 .IP
819 Note that ``host byte order'' is the byte order of the machine on which
820 the packets are captured, and the PF_ values are for the OS of the
821 machine on which the packets are captured; if a live capture is being
822 done, ``host byte order'' is the byte order of the machine capturing the
823 packets, and the PF_ values are those of the OS of the machine capturing
824 the packets, but if a ``savefile'' is being read, the byte order and PF_
825 values are
826 .I not
827 necessarily those of the machine reading the capture file.
828 .TP 5
829 .B DLT_EN10MB
830 Ethernet (10Mb, 100Mb, 1000Mb, and up)
831 .TP 5
832 .B DLT_IEEE802
833 IEEE 802.5 Token Ring
834 .TP 5
835 .B DLT_ARCNET
836 ARCNET
837 .TP 5
838 .B DLT_SLIP
839 SLIP; the link layer header contains, in order:
840 .RS 10
841 .LP
842 a 1-byte flag, which is 0 for packets received by the machine and 1 for
843 packets sent by the machine;
844 .LP
845 a 1-byte field, the upper 4 bits of which indicate the type of packet,
846 as per RFC 1144:
847 .RS 5
848 .TP 5
849 0x40
850 an unmodified IP datagram (TYPE_IP);
851 .TP 5
852 0x70
853 an uncompressed-TCP IP datagram (UNCOMPRESSED_TCP), with that byte being
854 the first byte of the raw IP header on the wire, containing the
855 connection number in the protocol field;
856 .TP 5
857 0x80
858 a compressed-TCP IP datagram (COMPRESSED_TCP), with that byte being the
859 first byte of the compressed TCP/IP datagram header;
860 .RE
861 .LP
862 for UNCOMPRESSED_TCP, the rest of the modified IP header, and for
863 COMPRESSED_TCP, the compressed TCP/IP datagram header;
864 .RE
865 .RS 5
866 .LP
867 for a total of 16 bytes; the uncompressed IP datagram follows the header.
868 .RE
869 .TP 5
870 .B DLT_PPP
871 PPP; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC-like
872 framing, with the PPP header following those two bytes, otherwise it's
873 PPP without framing, and the packet begins with the PPP header.
874 .TP 5
875 .B DLT_FDDI
876 FDDI
877 .TP 5
878 .B DLT_ATM_RFC1483
879 RFC 1483 LLC/SNAP-encapsulated ATM; the packet begins with an IEEE 802.2
880 LLC header.
881 .TP 5
882 .B DLT_RAW
883 raw IP; the packet begins with an IP header.
884 .TP 5
885 .B DLT_PPP_SERIAL
886 PPP in HDLC-like framing, as per RFC 1662, or Cisco PPP with HDLC
887 framing, as per section 4.3.1 of RFC 1547; the first byte will be 0xFF
888 for PPP in HDLC-like framing, and will be 0x0F or 0x8F for Cisco PPP
889 with HDLC framing.
890 .TP 5
891 .B DLT_PPP_ETHER
892 PPPoE; the packet begins with a PPPoE header, as per RFC 2516.
893 .TP 5
894 .B DLT_C_HDLC
895 Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547.
896 .TP 5
897 .B DLT_IEEE802_11
898 IEEE 802.11 wireless LAN
899 .TP 5
900 .B DLT_FRELAY
901 Frame Relay
902 .TP 5
903 .B DLT_LOOP
904 OpenBSD loopback encapsulation; the link layer header is a 4-byte field, in
905 .I network
906 byte order, containing a PF_ value from OpenBSD's
907 .B socket.h
908 for the network-layer protocol of the packet.
909 .IP
910 Note that, if a ``savefile'' is being read, those PF_ values are
911 .I not
912 necessarily those of the machine reading the capture file.
913 .TP 5
914 .B DLT_LINUX_SLL
915 Linux "cooked" capture encapsulation; the link layer header contains, in
916 order:
917 .RS 10
918 .LP
919 a 2-byte "packet type", in network byte order, which is one of:
920 .RS 5
921 .TP 5
922 0
923 packet was sent to us by somebody else
924 .TP 5
925 1
926 packet was broadcast by somebody else
927 .TP 5
928 2
929 packet was multicast, but not broadcast, by somebody else
930 .TP 5
931 3
932 packet was sent by somebody else to somebody else
933 .TP 5
934 4
935 packet was sent by us
936 .RE
937 .LP
938 a 2-byte field, in network byte order, containing a Linux ARPHRD_ value
939 for the link layer device type;
940 .LP
941 a 2-byte field, in network byte order, containing the length of the
942 link layer address of the sender of the packet (which could be 0);
943 .LP
944 an 8-byte field containing that number of bytes of the link layer header
945 (if there are more than 8 bytes, only the first 8 are present);
946 .LP
947 a 2-byte field containing an Ethernet protocol type, in network byte
948 order, or containing 1 for Novell 802.3 frames without an 802.2 LLC
949 header or 4 for frames beginning with an 802.2 LLC header.
950 .RE
951 .TP 5
952 .B DLT_LTALK
953 Apple LocalTalk; the packet begins with an AppleTalk LLAP header.
954 .TP 5
955 .B DLT_PFLOG
956 OpenBSD pflog; the link layer header contains, in order:
957 .RS 10
958 .LP
959 a 1-byte header length, in host byte order;
960 .LP
961 a 4-byte PF_ value, in host byte order;
962 .LP
963 a 2-byte action code, in network byte order, which is one of:
964 .RS 5
965 .TP 5
966 0
967 passed
968 .TP 5
969 1
970 dropped
971 .TP 5
972 2
973 scrubbed
974 .RE
975 .LP
976 a 2-byte reason code, in network byte order, which is one of:
977 .RS 5
978 .TP 5
979 0
980 match
981 .TP 5
982 1
983 bad offset
984 .TP 5
985 2
986 fragment
987 .TP 5
988 3
989 short
990 .TP 5
991 4
992 normalize
993 .TP 5
994 5
995 memory
996 .RE
997 .LP
998 a 16-character interface name;
999 .LP
1000 a 16-character ruleset name (only meaningful if subrule is set);
1001 .LP
1002 a 4-byte rule number, in network byte order;
1003 .LP
1004 a 4-byte subrule number, in network byte order;
1005 .LP
1006 a 1-byte direction, in network byte order, which is one of:
1007 .RS 5
1008 .TP 5
1009 0
1010 incoming or outgoing
1011 .TP 5
1012 1
1013 incoming
1014 .TP 5
1015 2
1016 outgoing
1017 .RE
1018 .RE
1019 .TP 5
1020 .B DLT_PRISM_HEADER
1021 Prism monitor mode information followed by an 802.11 header.
1022 .TP 5
1023 .B DLT_IP_OVER_FC
1024 RFC 2625 IP-over-Fibre Channel, with the link-layer header being the
1025 Network_Header as described in that RFC.
1026 .TP 5
1027 .B DLT_SUNATM
1028 SunATM devices; the link layer header contains, in order:
1029 .RS 10
1030 .LP
1031 a 1-byte flag field, containing a direction flag in the uppermost bit,
1032 which is set for packets transmitted by the machine and clear for
1033 packets received by the machine, and a 4-byte traffic type in the
1034 low-order 4 bits, which is one of:
1035 .RS 5
1036 .TP 5
1037 0
1038 raw traffic
1039 .TP 5
1040 1
1041 LANE traffic
1042 .TP 5
1043 2
1044 LLC-encapsulated traffic
1045 .TP 5
1046 3
1047 MARS traffic
1048 .TP 5
1049 4
1050 IFMP traffic
1051 .TP 5
1052 5
1053 ILMI traffic
1054 .TP 5
1055 6
1056 Q.2931 traffic
1057 .RE
1058 .LP
1059 a 1-byte VPI value;
1060 .LP
1061 a 2-byte VCI field, in network byte order.
1062 .RE
1063 .TP 5
1064 .B DLT_IEEE802_11_RADIO
1065 link-layer information followed by an 802.11 header - see
1066 http://www.shaftnet.org/~pizza/software/capturefrm.txt for a description
1067 of the link-layer information.
1068 .TP 5
1069 .B DLT_ARCNET_LINUX
1070 ARCNET, with no exception frames, reassembled packets rather than raw
1071 frames, and an extra 16-bit offset field between the destination host
1072 and type bytes.
1073 .TP 5
1074 .B DLT_LINUX_IRDA
1075 Linux-IrDA packets, with a
1076 .B DLT_LINUX_SLL
1077 header followed by the IrLAP header.
1078 .RE
1079 .PP
1080 .B pcap_list_datalinks()
1081 is used to get a list of the supported data link types of the interface
1082 associated with the pcap descriptor.
1083 .B pcap_list_datalinks()
1084 allocates an array to hold the list and sets
1085 .IR *dlt_buf .
1086 The caller is responsible for freeing the array.
1087 .B \-1
1088 is returned on failure;
1089 otherwise, the number of data link types in the array is returned.
1090 .PP
1091 .B pcap_set_datalink()
1092 is used to set the current data link type of the pcap descriptor
1093 to the type specified by
1094 .IR dlt .
1095 .B \-1
1096 is returned on failure.
1097 .PP
1098 .B pcap_datalink_name_to_val()
1099 translates a data link type name, which is a
1100 .B DLT_
1101 name with the
1102 .B DLT_
1103 removed, to the corresponding data link type value. The translation
1104 is case-insensitive.
1105 .B \-1
1106 is returned on failure.
1107 .PP
1108 .B pcap_datalink_val_to_name()
1109 translates a data link type value to the corresponding data link type
1110 name. NULL is returned on failure.
1111 .PP
1112 .B pcap_datalink_val_to_description()
1113 translates a data link type value to a short description of that data
1114 link type. NULL is returned on failure.
1115 .PP
1116 .B pcap_snapshot()
1117 returns the snapshot length specified when
1118 .B pcap_open_live()
1119 was called.
1120 .PP
1121 .B pcap_is_swapped()
1122 returns true if the current ``savefile'' uses a different byte order
1123 than the current system.
1124 .PP
1125 .B pcap_major_version()
1126 returns the major number of the file format of the savefile;
1127 .B pcap_minor_version()
1128 returns the minor number of the file format of the savefile. The
1129 version number is stored in the header of the savefile.
1130 .PP
1131 .B pcap_file()
1132 returns the standard I/O stream of the ``savefile,'' if a ``savefile''
1133 was opened with
1134 .BR pcap_open_offline() ,
1135 or NULL, if a network device was opened with
1136 .BR pcap_open_live() .
1137 .PP
1138 .B pcap_stats()
1139 returns 0 and fills in a
1140 .B pcap_stat
1141 struct. The values represent packet statistics from the start of the
1142 run to the time of the call. If there is an error or the underlying
1143 packet capture doesn't support packet statistics, \-1 is returned and
1144 the error text can be obtained with
1145 .B pcap_perror()
1146 or
1147 .BR pcap_geterr() .
1148 .B pcap_stats()
1149 is supported only on live captures, not on ``savefiles''; no statistics
1150 are stored in ``savefiles'', so no statistics are available when reading
1151 from a ``savefile''.
1152 .PP
1153 .B pcap_fileno()
1154 returns the file descriptor number from which captured packets are read,
1155 if a network device was opened with
1156 .BR pcap_open_live() ,
1157 or \-1, if a ``savefile'' was opened with
1158 .BR pcap_open_offline() .
1159 .PP
1160 .B pcap_get_selectable_fd()
1161 returns, on UNIX, a file descriptor number for a file descriptor on
1162 which one can
1163 do a
1164 .B select()
1165 or
1166 .B poll()
1167 to wait for it to be possible to read packets without blocking, if such
1168 a descriptor exists, or \-1, if no such descriptor exists. Some network
1169 devices opened with
1170 .B pcap_open_live()
1171 do not support
1172 .B select()
1173 or
1174 .B poll()
1175 (for example, regular network devices on FreeBSD 4.3 and 4.4, and Endace
1176 DAG devices), so \-1 is returned for those devices.
1177 .PP
1178 Note that on most versions of most BSDs (including Mac OS X)
1179 .B select()
1180 and
1181 .B poll()
1182 do not work correctly on BPF devices;
1183 .B pcap_get_selectable_fd()
1184 will return a file descriptor on most of those versions (the exceptions
1185 being FreeBSD 4.3 and 4.4), a simple
1186 .B select()
1187 or
1188 .B poll()
1189 will not return even after a timeout specified in
1190 .B pcap_open_live()
1191 expires. To work around this, an application that uses
1192 .B select()
1193 or
1194 .B poll()
1195 to wait for packets to arrive must put the
1196 .B pcap_t
1197 in non-blocking mode, and must arrange that the
1198 .B select()
1199 or
1200 .B poll()
1201 have a timeout less than or equal to the timeout specified in
1202 .BR pcap_open_live() ,
1203 and must try to read packets after that timeout expires, regardless of
1204 whether
1205 .B select()
1206 or
1207 .B poll()
1208 indicated that the file descriptor for the
1209 .B pcap_t
1210 is ready to be read or not. (That workaround will not work in FreeBSD
1211 4.3 and later; however, in FreeBSD 4.6 and later,
1212 .B select()
1213 and
1214 .B poll()
1215 work correctly on BPF devices, so the workaround isn't necessary,
1216 although it does no harm.)
1217 .PP
1218 .B pcap_get_selectable_fd()
1219 is not available on Windows.
1220 .PP
1221 .B pcap_perror()
1222 prints the text of the last pcap library error on
1223 .BR stderr ,
1224 prefixed by
1225 .IR prefix .
1226 .PP
1227 .B pcap_geterr()
1228 returns the error text pertaining to the last pcap library error.
1229 .BR NOTE :
1230 the pointer it returns will no longer point to a valid error message
1231 string after the
1232 .B pcap_t
1233 passed to it is closed; you must use or copy the string before closing
1234 the
1235 .BR pcap_t .
1236 .PP
1237 .B pcap_strerror()
1238 is provided in case
1239 .BR strerror (1)
1240 isn't available.
1241 .PP
1242 .B pcap_lib_version()
1243 returns a pointer to a string giving information about the version of
1244 the libpcap library being used; note that it contains more information
1245 than just a version number.
1246 .PP
1247 .B pcap_close()
1248 closes the files associated with
1249 .I p
1250 and deallocates resources.
1251 .PP
1252 .B pcap_dump_file()
1253 returns the standard I/O stream of the ``savefile'' opened by
1254 .BR pcap_dump_open().
1255 .PP
1256 .B pcap_dump_flush()
1257 flushes the output buffer to the ``savefile,'' so that any packets
1258 written with
1259 .B pcap_dump()
1260 but not yet written to the ``savefile'' will be written.
1261 .B \-1
1262 is returned on error, 0 on success.
1263 .PP
1264 .B pcap_dump_ftell()
1265 returns the current file position for the ``savefile'', representing the
1266 number of bytes written by
1267 .B pcap_dump_open()
1268 and
1269 .BR pcap_dump() .
1270 .B \-1
1271 is returned on error.
1272 .PP
1273 .B pcap_dump_close()
1274 closes the ``savefile.''
1275 .PP
1276 .SH SEE ALSO
1277 tcpdump(1), tcpslice(1)
1278 .SH AUTHORS
1279 The original authors are:
1280 .LP
1281 Van Jacobson,
1282 Craig Leres and
1283 Steven McCanne, all of the
1284 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
1285 .LP
1286 The current version is available from "The Tcpdump Group"'s Web site at
1287 .LP
1288 .RS
1289 .I http://www.tcpdump.org/
1290 .RE
1291 .SH BUGS
1292 Please send problems, bugs, questions, desirable enhancements, etc. to:
1293 .LP
1294 .RS
1295 tcpdump-workers@tcpdump.org
1296 .RE
1297 .LP
1298 Please send source code contributions, etc. to:
1299 .LP
1300 .RS
1301 patches@tcpdump.org
1302 .RE