]> The Tcpdump Group git mirrors - libpcap/blob - pcap.3
fd9dada2c163cc8da7bf6626ef7fc11d8670bfa8
[libpcap] / pcap.3
1 .\" @(#) $Header: /tcpdump/master/libpcap/Attic/pcap.3,v 1.25 2001-10-13 06:28:53 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 If called directly, the
414 .I user
415 parameter is of type
416 .I pcap_dumper_t
417 as returned by
418 .BR pcap_dump_open() .
419 .PP
420 .B pcap_compile()
421 is used to compile the string
422 .I str
423 into a filter program.
424 .I program
425 is a pointer to a
426 .I bpf_program
427 struct and is filled in by
428 .BR pcap_compile() .
429 .I optimize
430 controls whether optimization on the resulting code is performed.
431 .I netmask
432 specifies the netmask of the local net.
433 A return of \-1 indicates an error in which case
434 .BR pcap_geterr()
435 may be used to display the error text.
436 .PP
437 .B pcap_compile_nopcap()
438 is similar to
439 .B pcap_compile()
440 except that instead of passing a pcap structure, one passes the
441 snaplen and linktype explicitly. It is intended to be used for
442 compiling filters for direct BPF usage, without necessarily having
443 called
444 .BR pcap_open() .
445 A return of \-1 indicates an error; the error text is unavailable.
446 .RB ( pcap_compile_nopcap()
447 is a wrapper around
448 .BR pcap_open_dead() ,
449 .BR pcap_compile() ,
450 and
451 .BR pcap_close() ;
452 the latter three routines can be used directly in order to get the error
453 text for a compilation error.)
454 .B
455 .PP
456 .B pcap_setfilter()
457 is used to specify a filter program.
458 .I fp
459 is a pointer to a
460 .I bpf_program
461 struct, usually the result of a call to
462 .BR pcap_compile() .
463 .B \-1
464 is returned on failure, in which case
465 .BR pcap_geterr()
466 may be used to display the error text;
467 .B 0
468 is returned on success.
469 .PP
470 .B pcap_freecode()
471 is used to free up allocated memory pointed to by a
472 .I bpf_program
473 struct generated by
474 .B pcap_compile()
475 when that BPF program is no longer needed, for example after it
476 has been made the filter program for a pcap structure by a call to
477 .BR pcap_setfilter() .
478 .PP
479 .B pcap_datalink()
480 returns the link layer type, e.g.
481 .BR DLT_EN10MB .
482 .PP
483 .B pcap_snapshot()
484 returns the snapshot length specified when
485 .B pcap_open_live
486 was called.
487 .PP
488 .B pcap_is_swapped()
489 returns true if the current ``savefile'' uses a different byte order
490 than the current system.
491 .PP
492 .B pcap_major_version()
493 returns the major number of the version of the pcap used to write the
494 savefile.
495 .PP
496 .B pcap_minor_version()
497 returns the minor number of the version of the pcap used to write the
498 savefile.
499 .PP
500 .B pcap_file()
501 returns the standard I/O stream of the ``savefile,'' if a ``savefile''
502 was opened with
503 .BR pcap_open_offline() ,
504 or NULL, if a network device was opened with
505 .BR pcap_open_live() .
506 .PP
507 .B pcap_stats()
508 returns 0 and fills in a
509 .B pcap_stat
510 struct. The values represent packet statistics from the start of the
511 run to the time of the call. If there is an error or the underlying
512 packet capture doesn't support packet statistics, \-1 is returned and
513 the error text can be obtained with
514 .B pcap_perror()
515 or
516 .BR pcap_geterr() .
517 .PP
518 .B pcap_fileno()
519 returns the file descriptor number from which captured packets are read,
520 if a network device was opened with
521 .BR pcap_open_live() ,
522 or \-1, if a ``savefile'' was opened with
523 .BR pcap_open_offline() .
524 .PP
525 .B pcap_perror()
526 prints the text of the last pcap library error on
527 .BR stderr ,
528 prefixed by
529 .IR prefix .
530 .PP
531 .B pcap_geterr()
532 returns the error text pertaining to the last pcap library error.
533 .BR NOTE :
534 the pointer it returns will no longer point to a valid error message
535 string after the
536 .B pcap_t
537 passed to it is closed; you must use or copy the string before closing
538 the
539 .BR pcap_t .
540 .PP
541 .B pcap_strerror()
542 is provided in case
543 .BR strerror (1)
544 isn't available.
545 .PP
546 .B pcap_close()
547 closes the files associated with
548 .I p
549 and deallocates resources.
550 .PP
551 .B pcap_dump_close()
552 closes the ``savefile.''
553 .PP
554 .SH SEE ALSO
555 tcpdump(1), tcpslice(1)
556 .SH AUTHORS
557 The original authors are:
558 .LP
559 Van Jacobson,
560 Craig Leres and
561 Steven McCanne, all of the
562 Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
563 .LP
564 The current version is available from "The Tcpdump Group"'s Web site at
565 .LP
566 .RS
567 .I http://www.tcpdump.org/
568 .RE
569 .SH BUGS
570 Please send problems, bugs, questions, desirable enhancements, etc. to:
571 .LP
572 .RS
573 tcpdump-workers@tcpdump.org
574 .RE
575 .LP
576 Please send source code contributions, etc. to:
577 .LP
578 .RS
579 patches@tcpdump.org
580 .RE