1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3,v 1.18 2001-01-18 04:41:45 guy Exp $
3 .\" Copyright (c) 1994, 1996, 1997
4 .\" The Regents of the University of California. All rights reserved.
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.
22 .TH PCAP 3 "3 January 2001"
24 pcap \- Packet Capture library
32 pcap_t *pcap_open_live(char *device, int snaplen,
34 int promisc, int to_ms, char *ebuf)
35 pcap_t *pcap_open_dead(int linktype, int snaplen)
36 pcap_t *pcap_open_offline(char *fname, char *ebuf)
37 pcap_dumper_t *pcap_dump_open(pcap_t *p, char *fname)
41 char errbuf[PCAP_ERRBUF_SIZE];
42 char *pcap_lookupdev(char *errbuf)
43 int pcap_lookupnet(char *device, bpf_u_int32 *netp,
45 bpf_u_int32 *maskp, char *errbuf)
49 int pcap_dispatch(pcap_t *p, int cnt,
51 pcap_handler callback, u_char *user)
52 int pcap_loop(pcap_t *p, int cnt,
54 pcap_handler callback, u_char *user)
55 void pcap_dump(u_char *user, struct pcap_pkthdr *h,
61 int pcap_compile(pcap_t *p, struct bpf_program *fp,
63 char *str, int optimize, bpf_u_int32 netmask)
64 int pcap_setfilter(pcap_t *p, struct bpf_program *fp)
65 void pcap_freecode(struct bpf_program *);
69 u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)
73 int pcap_datalink(pcap_t *p)
74 int pcap_snapshot(pcap_t *p)
75 int pcap_is_swapped(pcap_t *p)
76 int pcap_major_version(pcap_t *p)
77 int pcap_minor_version(pcap_t *p)
78 int pcap_stats(pcap_t *p, struct pcap_stat *ps)
79 FILE *pcap_file(pcap_t *p)
80 int pcap_fileno(pcap_t *p)
81 void pcap_perror(pcap_t *p, char *prefix)
82 char *pcap_geterr(pcap_t *p)
83 char *pcap_strerror(int error)
87 void pcap_close(pcap_t *p)
88 void pcap_dump_close(pcap_dumper_t *p)
92 The Packet Capture library
93 provides a high level interface to packet capture systems. All packets
94 on the network, even those destined for other hosts, are accessible
95 through this mechanism.
102 .B pcap_open_offline(),
106 is assumed to be able to hold at least
111 is used to obtain a packet capture descriptor to look
112 at packets on the network.
114 is a string that specifies the network device to open; on Linux systems
115 with 2.2 or later kernels, a
119 can be used to capture packets from all interfaces.
121 specifies the maximum number of bytes to capture.
123 specifies if the interface is to be put into promiscuous mode.
124 (Note that even if this parameter is false, the interface
125 could well be in promiscuous mode for some other reason.) For now, this
126 doesn't work on the "any" device; if an argument of "any" or NULL is
131 specifies the read timeout in milliseconds. The read timeout is used to
132 arrange that the read not necessarily return immediately when a packet
133 is seen, but that it wait for some amount of time to allow more packets
134 to arrive and to read multiple packets from the OS kernel in one
135 operation. Not all platforms support a read timeout; on platforms that
136 don't, the read timeout is ignored.
138 is used to return error text and is only set when
144 is used for creating a
146 structure to use when calling the other functions in libpcap. It is
147 typically used when just using libpcap for compiling BPF code.
149 .B pcap_open_offline()
150 is called to open a ``savefile'' for reading.
152 specifies the name of the file to open. The file has
153 the same format as those used by
157 The name "-" in a synonym for
160 is used to return error text and is only set when
161 .B pcap_open_offline()
166 is called to open a ``savefile'' for writing. The name "-" in a synonym
170 is returned on failure.
174 struct as returned by
175 .B pcap_open_offline()
177 .BR pcap_open_live() .
179 specifies the name of the file to open.
184 can be used to get the error text.
187 returns a pointer to a network device suitable for use with
190 .BR pcap_lookupnet() .
191 If there is an error,
195 is filled in with an appropriate error message.
198 is used to determine the network number and mask
199 associated with the network device
208 A return of \-1 indicates an error in which case
210 is filled in with an appropriate error message.
213 is used to collect and process packets.
215 specifies the maximum number of packets to process before returning.
216 This is not a minimum number; when reading a live capture, only one
217 bufferful of packets is read at a time, so fewer than
219 packets may be processed. A
221 of \-1 processes all the packets received in one buffer when reading a
222 live capture, or all the packets in the file when reading a
225 specifies a routine to be called with three arguments:
228 pointer which is passed in from
229 .BR pcap_dispatch() ,
232 struct (which precede the actual network headers and data),
235 pointer to the packet data.
237 The number of packets read is returned.
238 0 is returned if no packets were read from a live capture (if, for
239 example, they were discarded because they didn't pass the packet filter,
240 or if, on platforms that support a read timeout that starts before any
241 packets arrive, the timeout expires before any packets arrive, or if the
242 file descriptor for the capture device is in non-blocking mode and no
243 packets were available to be read) or if no more packets are available
244 in a ``savefile.'' A return of \-1 indicates
245 an error in which case
249 may be used to display the error text.
252 when reading a live capture,
254 will not necessarily return when the read times out; on some platforms,
255 the read timeout isn't supported, and, on other platforms, the timer
256 doesn't start until at least one packet arrives. This means that the
259 be used in, for example, an interactive application, to allow the packet
260 capture loop to ``poll'' for user input periodically, as there's no
263 will return after the timeout expires.
268 except it keeps reading packets until
270 packets are processed or an error occurs.
273 return when live read timeouts occur.
274 Rather, specifying a non-zero read timeout to
278 allows the reception and processing of any packets that arrive when the
284 to loop forever (or at least until an error occurs).
289 pointer to the next packet.
292 outputs a packet to the ``savefile'' opened with
293 .BR pcap_dump_open() .
294 Note that its calling arguments are suitable for use with
300 is used to compile the string
302 into a filter program.
306 struct and is filled in by
309 controls whether optimization on the resulting code is performed.
311 specifies the netmask of the local net.
312 A return of \-1 indicates an error in which case
314 may be used to display the error text.
316 .B pcap_compile_nopcap()
319 except that instead of passing a pcap structure, one passes the
320 snaplen and linktype explicitly. It is intended to be used for
321 compiling filters for direct BPF usage, without necessarily having
324 A return of \-1 indicates an error; the error text is unavailable.
325 .RB ( pcap_compile_nopcap()
327 .BR pcap_open_dead() ,
331 the latter three routines can be used directly in order to get the error
332 text for a compilation error.)
336 is used to specify a filter program.
340 struct, usually the result of a call to
343 is returned on failure, in which case
345 may be used to display the error text;
347 is returned on success.
350 is used to free up allocated memory pointed to by a
354 when that BPF program is no longer needed, for example after it
355 has been made the filter program for a pcap structure by a call to
356 .BR pcap_setfilter() .
359 returns the link layer type, e.g.
363 returns the snapshot length specified when
368 returns true if the current ``savefile'' uses a different byte order
369 than the current system.
371 .B pcap_major_version()
372 returns the major number of the version of the pcap used to write the
375 .B pcap_minor_version()
376 returns the minor number of the version of the pcap used to write the
380 returns the name of the ``savefile.''
383 returns 0 and fills in a
385 struct. The values represent packet statistics from the start of the
386 run to the time of the call. If there is an error or the under lying
387 packet capture doesn't support packet statistics, \-1 is returned and
388 the error text can be obtained with
394 returns the file descriptor number of the ``savefile.''
397 prints the text of the last pcap library error on
403 returns the error text pertaining to the last pcap library error.
405 the pointer it returns will no longer point to a valid error message
408 passed to it is closed; you must use or copy the string before closing
418 closes the files associated with
420 and deallocates resources.
423 closes the ``savefile.''
426 tcpdump(1), tcpslice(1)
428 The original authors are:
432 Steven McCanne, all of the
433 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
435 The current version is available from "The Tcpdump Group"'s Web site at
438 .I http://www.tcpdump.org/
441 Please send problems, bugs, questions, desirable enhancements, etc. to:
444 tcpdump-workers@tcpdump.org
447 Please send source code contributions, etc. to: