1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3,v 1.17 2001-01-03 22:52:26 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.)
127 specifies the read timeout in milliseconds. The read timeout is used to
128 arrange that the read not necessarily return immediately when a packet
129 is seen, but that it wait for some amount of time to allow more packets
130 to arrive and to read multiple packets from the OS kernel in one
131 operation. Not all platforms support a read timeout; on platforms that
132 don't, the read timeout is ignored.
134 is used to return error text and is only set when
140 is used for creating a
142 structure to use when calling the other functions in libpcap. It is
143 typically used when just using libpcap for compiling BPF code.
145 .B pcap_open_offline()
146 is called to open a ``savefile'' for reading.
148 specifies the name of the file to open. The file has
149 the same format as those used by
153 The name "-" in a synonym for
156 is used to return error text and is only set when
157 .B pcap_open_offline()
162 is called to open a ``savefile'' for writing. The name "-" in a synonym
166 is returned on failure.
170 struct as returned by
171 .B pcap_open_offline()
173 .BR pcap_open_live() .
175 specifies the name of the file to open.
180 can be used to get the error text.
183 returns a pointer to a network device suitable for use with
186 .BR pcap_lookupnet() .
187 If there is an error,
191 is filled in with an appropriate error message.
194 is used to determine the network number and mask
195 associated with the network device
204 A return of \-1 indicates an error in which case
206 is filled in with an appropriate error message.
209 is used to collect and process packets.
211 specifies the maximum number of packets to process before returning.
212 This is not a minimum number; when reading a live capture, only one
213 bufferful of packets is read at a time, so fewer than
215 packets may be processed. A
217 of \-1 processes all the packets received in one buffer when reading a
218 live capture, or all the packets in the file when reading a
221 specifies a routine to be called with three arguments:
224 pointer which is passed in from
225 .BR pcap_dispatch() ,
228 struct (which precede the actual network headers and data),
231 pointer to the packet data.
233 The number of packets read is returned.
234 0 is returned if no packets were read from a live capture (if, for
235 example, they were discarded because they didn't pass the packet filter,
236 or if, on platforms that support a read timeout that starts before any
237 packets arrive, the timeout expires before any packets arrive, or if the
238 file descriptor for the capture device is in non-blocking mode and no
239 packets were available to be read) or if no more packets are available
240 in a ``savefile.'' A return of \-1 indicates
241 an error in which case
245 may be used to display the error text.
248 when reading a live capture,
250 will not necessarily return when the read times out; on some platforms,
251 the read timeout isn't supported, and, on other platforms, the timer
252 doesn't start until at least one packet arrives. This means that the
255 be used in, for example, an interactive application, to allow the packet
256 capture loop to ``poll'' for user input periodically, as there's no
259 will return after the timeout expires.
264 except it keeps reading packets until
266 packets are processed or an error occurs.
269 return when live read timeouts occur.
270 Rather, specifying a non-zero read timeout to
274 allows the reception and processing of any packets that arrive when the
280 to loop forever (or at least until an error occurs).
285 pointer to the next packet.
288 outputs a packet to the ``savefile'' opened with
289 .BR pcap_dump_open() .
290 Note that its calling arguments are suitable for use with
296 is used to compile the string
298 into a filter program.
302 struct and is filled in by
305 controls whether optimization on the resulting code is performed.
307 specifies the netmask of the local net.
308 A return of \-1 indicates an error in which case
310 may be used to display the error text.
312 .B pcap_compile_nopcap()
315 except that instead of passing a pcap structure, one passes the
316 snaplen and linktype explicitly. It is intended to be used for
317 compiling filters for direct BPF usage, without necessarily having
320 A return of \-1 indicates an error; the error text is unavailable.
321 .RB ( pcap_compile_nopcap()
323 .BR pcap_open_dead() ,
327 the latter three routines can be used directly in order to get the error
328 text for a compilation error.)
332 is used to specify a filter program.
336 struct, usually the result of a call to
339 is returned on failure, in which case
341 may be used to display the error text;
343 is returned on success.
346 is used to free up allocated memory pointed to by a
350 when that BPF program is no longer needed, for example after it
351 has been made the filter program for a pcap structure by a call to
352 .BR pcap_setfilter() .
355 returns the link layer type, e.g.
359 returns the snapshot length specified when
364 returns true if the current ``savefile'' uses a different byte order
365 than the current system.
367 .B pcap_major_version()
368 returns the major number of the version of the pcap used to write the
371 .B pcap_minor_version()
372 returns the minor number of the version of the pcap used to write the
376 returns the name of the ``savefile.''
379 returns 0 and fills in a
381 struct. The values represent packet statistics from the start of the
382 run to the time of the call. If there is an error or the under lying
383 packet capture doesn't support packet statistics, \-1 is returned and
384 the error text can be obtained with
390 returns the file descriptor number of the ``savefile.''
393 prints the text of the last pcap library error on
399 returns the error text pertaining to the last pcap library error.
401 the pointer it returns will no longer point to a valid error message
404 passed to it is closed; you must use or copy the string before closing
414 closes the files associated with
416 and deallocates resources.
419 closes the ``savefile.''
422 tcpdump(1), tcpslice(1)
424 The original authors are:
428 Steven McCanne, all of the
429 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
431 The current version is available from "The Tcpdump Group"'s Web site at
434 .I http://www.tcpdump.org/
437 Please send problems, bugs, questions, desirable enhancements, etc. to:
440 tcpdump-workers@tcpdump.org
443 Please send source code contributions, etc. to: