]> The Tcpdump Group git mirrors - libpcap/blob - pcap.3
From Scott Gifford:
[libpcap] / pcap.3
1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3,v 1.23 2001-10-08 01:06:21 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 "3 January 2001"
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(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(char *fname, char *errbuf)
42 pcap_dumper_t *pcap_dump_open(pcap_t *p, char *fname)
43 .ft
44 .LP
45 .ft B
46 int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
47 void pcap_freealldevs(pcap_if_t *)
48 char *pcap_lookupdev(char *errbuf)
49 int pcap_lookupnet(char *device, bpf_u_int32 *netp,
50 .ti +8
51 bpf_u_int32 *maskp, char *errbuf)
52 .ft
53 .LP
54 .ft B
55 int pcap_dispatch(pcap_t *p, int cnt,
56 .ti +8
57 pcap_handler callback, u_char *user)
58 int pcap_loop(pcap_t *p, int cnt,
59 .ti +8
60 pcap_handler callback, u_char *user)
61 void pcap_dump(u_char *user, struct pcap_pkthdr *h,
62 .ti +8
63 u_char *sp)
64 .ft
65 .LP
66 .ft B
67 int pcap_compile(pcap_t *p, struct bpf_program *fp,
68 .ti +8
69 char *str, int optimize, bpf_u_int32 netmask)
70 int pcap_setfilter(pcap_t *p, struct bpf_program *fp)
71 void pcap_freecode(struct bpf_program *);
72 .ft
73 .LP
74 .ft B
75 u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)
76 .ft
77 .LP
78 .ft B
79 int pcap_datalink(pcap_t *p)
80 int pcap_snapshot(pcap_t *p)
81 int pcap_is_swapped(pcap_t *p)
82 int pcap_major_version(pcap_t *p)
83 int pcap_minor_version(pcap_t *p)
84 int pcap_stats(pcap_t *p, struct pcap_stat *ps)
85 FILE *pcap_file(pcap_t *p)
86 int pcap_fileno(pcap_t *p)
87 void pcap_perror(pcap_t *p, char *prefix)
88 char *pcap_geterr(pcap_t *p)
89 char *pcap_strerror(int error)
90 .ft
91 .LP
92 .ft B
93 void pcap_close(pcap_t *p)
94 void pcap_dump_close(pcap_dumper_t *p)
95 .ft
96 .fi
97 .SH DESCRIPTION
98 The Packet Capture library
99 provides a high level interface to packet capture systems. All packets
100 on the network, even those destined for other hosts, are accessible
101 through this mechanism.
102 .PP
103 .SH ROUTINES
104 NOTE:
105 .I errbuf
106 in
107 .B pcap_open_live(),
108 .B pcap_open_dead(),
109 .B pcap_open_offline(),
110 .B pcap_findalldevs(),
111 .B pcap_lookupdev(),
112 and
113 .B pcap_lookupnet()
114 is assumed to be able to hold at least
115 .B PCAP_ERRBUF_SIZE
116 chars.
117 .PP
118 .B pcap_open_live()
119 is used to obtain a packet capture descriptor to look
120 at packets on the network.
121 .I device
122 is a string that specifies the network device to open; on Linux systems
123 with 2.2 or later kernels, a
124 .I device
125 argument of "any" or
126 .B NULL
127 can be used to capture packets from all interfaces.
128 .I snaplen
129 specifies the maximum number of bytes to capture.
130 .I promisc
131 specifies if the interface is to be put into promiscuous mode.
132 (Note that even if this parameter is false, the interface
133 could well be in promiscuous mode for some other reason.) For now, this
134 doesn't work on the "any" device; if an argument of "any" or NULL is
135 supplied, the
136 .I promisc
137 flag is ignored.
138 .I to_ms
139 specifies the read timeout in milliseconds. The read timeout is used to
140 arrange that the read not necessarily return immediately when a packet
141 is seen, but that it wait for some amount of time to allow more packets
142 to arrive and to read multiple packets from the OS kernel in one
143 operation. Not all platforms support a read timeout; on platforms that
144 don't, the read timeout is ignored.
145 .I errbuf
146 is used to return error or warning text. It will be set to error text when
147 .B pcap_open_live()
148 fails and returns
149 .BR NULL .
150 .I errbuf
151 may also be set to warning text when
152 .B pcap_open_live()
153 succeds; to detect this case the caller should store a zero-length string in
154 .I errbuf
155 before calling
156 .B pcap_open_live()
157 and display the warning to the user if
158 .I errbuf
159 is no longer a zero-length string.
160 .PP
161 .B pcap_open_dead()
162 is used for creating a
163 .B pcap_t
164 structure to use when calling the other functions in libpcap. It is
165 typically used when just using libpcap for compiling BPF code.
166 .PP
167 .B pcap_open_offline()
168 is called to open a ``savefile'' for reading.
169 .I fname
170 specifies the name of the file to open. The file has
171 the same format as those used by
172 .B tcpdump(1)
173 and
174 .BR tcpslice(1) .
175 The name "-" in a synonym for
176 .BR stdin .
177 .I errbuf
178 is used to return error text and is only set when
179 .B pcap_open_offline()
180 fails and returns
181 .BR NULL .
182 .PP
183 .B pcap_dump_open()
184 is called to open a ``savefile'' for writing. The name "-" in a synonym
185 for
186 .BR stdout .
187 .B NULL
188 is returned on failure.
189 .I p
190 is a
191 .I pcap
192 struct as returned by
193 .B pcap_open_offline()
194 or
195 .BR pcap_open_live() .
196 .I fname
197 specifies the name of the file to open.
198 If
199 .B NULL
200 is returned,
201 .B pcap_geterr()
202 can be used to get the error text.
203 .PP
204 .B pcap_findalldevs()
205 constructs a list of network devices that can be opened with
206 .BR pcap_open_live() .
207 (Note that there may be network devices that cannot be opened with
208 .BR pcap_open_live()
209 by the
210 process calling
211 .BR pcap_findalldevs() ,
212 because, for example, that process might not have sufficient privileges
213 to open them for capturing; if so, those devices will not appear on the
214 list.)
215 .I alldevsp
216 is set to point to the first element of the list; each element of the
217 list is of type
218 .BR pcap_if_t ,
219 and has the following members:
220 .RS
221 .TP
222 .B next
223 if not
224 .BR NULL ,
225 a pointer to the next element in the list;
226 .B NULL
227 for the last element of the list
228 .TP
229 .B name
230 a pointer to a string giving a name for the device to pass to
231 .B pcap_open_live()
232 .TP
233 .B description
234 if not
235 .BR NULL ,
236 a pointer to a string giving a human-readable description of the device
237 .TP
238 .B addresses
239 a pointer to the first element of a list of addresses for the interface
240 .TP
241 .B is_loopback
242 non-zero if the interface is a loopback interface
243 .RE
244 .PP
245 Each element of the list of addresses is of type
246 .BR pcap_addr_t ,
247 and has the following members:
248 .RS
249 .TP
250 .B next
251 if not
252 .BR NULL ,
253 a pointer to the next element in the list;
254 .B NULL
255 for the last element of the list
256 .TP
257 .B addr
258 a pointer to a
259 .B "struct sockaddr"
260 containing an address
261 .TP
262 .B netmask
263 if not
264 .BR NULL ,
265 a pointer to a
266 .B "struct sockaddr"
267 that contains the netmask corresponding to the address pointed to by
268 .B addr
269 .TP
270 .B broadaddr
271 if not
272 .BR NULL ,
273 a pointer to a
274 .B "struct sockaddr"
275 that contains the broadcast address corresponding to the address pointed
276 to by
277 .BR addr ;
278 may be null if the interface doesn't support broadcasts
279 .TP
280 .B dstaddr
281 if not
282 .BR NULL ,
283 a pointer to a
284 .B "struct sockaddr"
285 that contains the destination address corresponding to the address pointed
286 to by
287 .BR addr ;
288 may be null if the interface isn't a point-to-point interface
289 .RE
290 .PP
291 .B pcap_freealldevs()
292 is used to free a list allocated by
293 .BR pcap_findalldevs() .
294 .PP
295 .B pcap_lookupdev()
296 returns a pointer to a network device suitable for use with
297 .B pcap_open_live()
298 and
299 .BR pcap_lookupnet() .
300 If there is an error,
301 .B NULL
302 is returned and
303 .I errbuf
304 is filled in with an appropriate error message.
305 .PP
306 .B pcap_lookupnet()
307 is used to determine the network number and mask
308 associated with the network device
309 .BR device .
310 Both
311 .I netp
312 and
313 .I maskp
314 are
315 .I bpf_u_int32
316 pointers.
317 A return of \-1 indicates an error in which case
318 .I errbuf
319 is filled in with an appropriate error message.
320 .PP
321 .B pcap_dispatch()
322 is used to collect and process packets.
323 .I cnt
324 specifies the maximum number of packets to process before returning.
325 This is not a minimum number; when reading a live capture, only one
326 bufferful of packets is read at a time, so fewer than
327 .I cnt
328 packets may be processed. A
329 .I cnt
330 of \-1 processes all the packets received in one buffer when reading a
331 live capture, or all the packets in the file when reading a
332 ``savefile''.
333 .I callback
334 specifies a routine to be called with three arguments:
335 a
336 .I u_char
337 pointer which is passed in from
338 .BR pcap_dispatch() ,
339 a pointer to the
340 .I pcap_pkthdr
341 struct (which precede the actual network headers and data),
342 and a
343 .I u_char
344 pointer to the packet data.
345 .PP
346 The number of packets read is returned.
347 0 is returned if no packets were read from a live capture (if, for
348 example, they were discarded because they didn't pass the packet filter,
349 or if, on platforms that support a read timeout that starts before any
350 packets arrive, the timeout expires before any packets arrive, or if the
351 file descriptor for the capture device is in non-blocking mode and no
352 packets were available to be read) or if no more packets are available
353 in a ``savefile.'' A return of \-1 indicates
354 an error in which case
355 .B pcap_perror()
356 or
357 .B pcap_geterr()
358 may be used to display the error text.
359 .PP
360 .BR NOTE :
361 when reading a live capture,
362 .B pcap_dispatch()
363 will not necessarily return when the read times out; on some platforms,
364 the read timeout isn't supported, and, on other platforms, the timer
365 doesn't start until at least one packet arrives. This means that the
366 read timeout should
367 .B NOT
368 be used in, for example, an interactive application, to allow the packet
369 capture loop to ``poll'' for user input periodically, as there's no
370 guarantee that
371 .B pcap_dispatch()
372 will return after the timeout expires.
373 .PP
374 .B pcap_loop()
375 is similar to
376 .B pcap_dispatch()
377 except it keeps reading packets until
378 .I cnt
379 packets are processed or an error occurs.
380 It does
381 .B not
382 return when live read timeouts occur.
383 Rather, specifying a non-zero read timeout to
384 .B pcap_open_live()
385 and then calling
386 .B pcap_dispatch()
387 allows the reception and processing of any packets that arrive when the
388 timeout occurs.
389 A negative
390 .I cnt
391 causes
392 .B pcap_loop()
393 to loop forever (or at least until an error occurs).
394 .PP
395 .B pcap_next()
396 reads the next packet (by calling
397 .B pcap_dispatch()
398 with a
399 .I cnt
400 of 1) and returns a
401 .I u_char
402 pointer to the data in that packet. (The
403 .I pcap_pkthdr
404 struct for that packet is not supplied.)
405 .PP
406 .B pcap_dump()
407 outputs a packet to the ``savefile'' opened with
408 .BR pcap_dump_open() .
409 Note that its calling arguments are suitable for use with
410 .B pcap_dispatch()
411 or
412 .BR pcap_loop() .
413 .PP
414 .B pcap_compile()
415 is used to compile the string
416 .I str
417 into a filter program.
418 .I program
419 is a pointer to a
420 .I bpf_program
421 struct and is filled in by
422 .BR pcap_compile() .
423 .I optimize
424 controls whether optimization on the resulting code is performed.
425 .I netmask
426 specifies the netmask of the local net.
427 A return of \-1 indicates an error in which case
428 .BR pcap_geterr()
429 may be used to display the error text.
430 .PP
431 .B pcap_compile_nopcap()
432 is similar to
433 .B pcap_compile()
434 except that instead of passing a pcap structure, one passes the
435 snaplen and linktype explicitly. It is intended to be used for
436 compiling filters for direct BPF usage, without necessarily having
437 called
438 .BR pcap_open() .
439 A return of \-1 indicates an error; the error text is unavailable.
440 .RB ( pcap_compile_nopcap()
441 is a wrapper around
442 .BR pcap_open_dead() ,
443 .BR pcap_compile() ,
444 and
445 .BR pcap_close() ;
446 the latter three routines can be used directly in order to get the error
447 text for a compilation error.)
448 .B
449 .PP
450 .B pcap_setfilter()
451 is used to specify a filter program.
452 .I fp
453 is a pointer to a
454 .I bpf_program
455 struct, usually the result of a call to
456 .BR pcap_compile() .
457 .B \-1
458 is returned on failure, in which case
459 .BR pcap_geterr()
460 may be used to display the error text;
461 .B 0
462 is returned on success.
463 .PP
464 .B pcap_freecode()
465 is used to free up allocated memory pointed to by a
466 .I bpf_program
467 struct generated by
468 .B pcap_compile()
469 when that BPF program is no longer needed, for example after it
470 has been made the filter program for a pcap structure by a call to
471 .BR pcap_setfilter() .
472 .PP
473 .B pcap_datalink()
474 returns the link layer type, e.g.
475 .BR DLT_EN10MB .
476 .PP
477 .B pcap_snapshot()
478 returns the snapshot length specified when
479 .B pcap_open_live
480 was called.
481 .PP
482 .B pcap_is_swapped()
483 returns true if the current ``savefile'' uses a different byte order
484 than the current system.
485 .PP
486 .B pcap_major_version()
487 returns the major number of the version of the pcap used to write the
488 savefile.
489 .PP
490 .B pcap_minor_version()
491 returns the minor number of the version of the pcap used to write the
492 savefile.
493 .PP
494 .B pcap_file()
495 returns the standard I/O stream of the ``savefile,'' if a ``savefile''
496 was opened with
497 .BR pcap_open_offline() ,
498 or NULL, if a network device was opened with
499 .BR pcap_open_live() .
500 .PP
501 .B pcap_stats()
502 returns 0 and fills in a
503 .B pcap_stat
504 struct. The values represent packet statistics from the start of the
505 run to the time of the call. If there is an error or the underlying
506 packet capture doesn't support packet statistics, \-1 is returned and
507 the error text can be obtained with
508 .B pcap_perror()
509 or
510 .BR pcap_geterr() .
511 .PP
512 .B pcap_fileno()
513 returns the file descriptor number from which captured packets are read,
514 if a network device was opened with
515 .BR pcap_open_live() ,
516 or \-1, if a ``savefile'' was opened with
517 .BR pcap_open_offline() .
518 .PP
519 .B pcap_perror()
520 prints the text of the last pcap library error on
521 .BR stderr ,
522 prefixed by
523 .IR prefix .
524 .PP
525 .B pcap_geterr()
526 returns the error text pertaining to the last pcap library error.
527 .BR NOTE :
528 the pointer it returns will no longer point to a valid error message
529 string after the
530 .B pcap_t
531 passed to it is closed; you must use or copy the string before closing
532 the
533 .BR pcap_t .
534 .PP
535 .B pcap_strerror()
536 is provided in case
537 .BR strerror (1)
538 isn't available.
539 .PP
540 .B pcap_close()
541 closes the files associated with
542 .I p
543 and deallocates resources.
544 .PP
545 .B pcap_dump_close()
546 closes the ``savefile.''
547 .PP
548 .SH SEE ALSO
549 tcpdump(1), tcpslice(1)
550 .SH AUTHORS
551 The original authors are:
552 .LP
553 Van Jacobson,
554 Craig Leres and
555 Steven McCanne, all of the
556 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
557 .LP
558 The current version is available from "The Tcpdump Group"'s Web site at
559 .LP
560 .RS
561 .I http://www.tcpdump.org/
562 .RE
563 .SH BUGS
564 Please send problems, bugs, questions, desirable enhancements, etc. to:
565 .LP
566 .RS
567 tcpdump-workers@tcpdump.org
568 .RE
569 .LP
570 Please send source code contributions, etc. to:
571 .LP
572 .RS
573 patches@tcpdump.org
574 .RE