]> The Tcpdump Group git mirrors - libpcap/blob - pcap/pcap.h
We need to include <io.h>, as we call _get_osfhandle() in macros.
[libpcap] / pcap / pcap.h
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3 * Copyright (c) 1993, 1994, 1995, 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 the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Remote packet capture mechanisms and extensions from WinPcap:
37 *
38 * Copyright (c) 2002 - 2003
39 * NetGroup, Politecnico di Torino (Italy)
40 * All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 *
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the Politecnico di Torino nor the names of its
52 * contributors may be used to endorse or promote products derived from
53 * this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
56 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
57 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
58 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
59 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
61 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
65 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *
67 */
68
69 #ifndef lib_pcap_pcap_h
70 #define lib_pcap_pcap_h
71
72 #include <pcap/export-defs.h>
73
74 #if defined(_WIN32)
75 #include <winsock2.h> /* u_int, u_char etc. */
76 #include <io.h> /* _get_osfhandle() */
77 #elif defined(MSDOS)
78 #include <sys/types.h> /* u_int, u_char etc. */
79 #include <sys/socket.h>
80 #else /* UN*X */
81 #include <sys/types.h> /* u_int, u_char etc. */
82 #include <sys/time.h>
83 #endif /* _WIN32/MSDOS/UN*X */
84
85 #ifndef PCAP_DONT_INCLUDE_PCAP_BPF_H
86 #include <pcap/bpf.h>
87 #endif
88
89 #include <stdio.h>
90
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94
95 /*
96 * Version number of the current version of the pcap file format.
97 *
98 * NOTE: this is *NOT* the version number of the libpcap library.
99 * To fetch the version information for the version of libpcap
100 * you're using, use pcap_lib_version().
101 */
102 #define PCAP_VERSION_MAJOR 2
103 #define PCAP_VERSION_MINOR 4
104
105 #define PCAP_ERRBUF_SIZE 256
106
107 /*
108 * Compatibility for systems that have a bpf.h that
109 * predates the bpf typedefs for 64-bit support.
110 */
111 #if BPF_RELEASE - 0 < 199406
112 typedef int bpf_int32;
113 typedef u_int bpf_u_int32;
114 #endif
115
116 typedef struct pcap pcap_t;
117 typedef struct pcap_dumper pcap_dumper_t;
118 typedef struct pcap_if pcap_if_t;
119 typedef struct pcap_addr pcap_addr_t;
120
121 /*
122 * The first record in the file contains saved values for some
123 * of the flags used in the printout phases of tcpdump.
124 * Many fields here are 32 bit ints so compilers won't insert unwanted
125 * padding; these files need to be interchangeable across architectures.
126 *
127 * Do not change the layout of this structure, in any way (this includes
128 * changes that only affect the length of fields in this structure).
129 *
130 * Also, do not change the interpretation of any of the members of this
131 * structure, in any way (this includes using values other than
132 * LINKTYPE_ values, as defined in "savefile.c", in the "linktype"
133 * field).
134 *
135 * Instead:
136 *
137 * introduce a new structure for the new format, if the layout
138 * of the structure changed;
139 *
140 * send mail to "tcpdump-workers@lists.tcpdump.org", requesting
141 * a new magic number for your new capture file format, and, when
142 * you get the new magic number, put it in "savefile.c";
143 *
144 * use that magic number for save files with the changed file
145 * header;
146 *
147 * make the code in "savefile.c" capable of reading files with
148 * the old file header as well as files with the new file header
149 * (using the magic number to determine the header format).
150 *
151 * Then supply the changes by forking the branch at
152 *
153 * https://github.com/the-tcpdump-group/libpcap/issues
154 *
155 * and issuing a pull request, so that future versions of libpcap and
156 * programs that use it (such as tcpdump) will be able to read your new
157 * capture file format.
158 */
159 struct pcap_file_header {
160 bpf_u_int32 magic;
161 u_short version_major;
162 u_short version_minor;
163 bpf_int32 thiszone; /* gmt to local correction */
164 bpf_u_int32 sigfigs; /* accuracy of timestamps */
165 bpf_u_int32 snaplen; /* max length saved portion of each pkt */
166 bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
167 };
168
169 /*
170 * Macros for the value returned by pcap_datalink_ext().
171 *
172 * If LT_FCS_LENGTH_PRESENT(x) is true, the LT_FCS_LENGTH(x) macro
173 * gives the FCS length of packets in the capture.
174 */
175 #define LT_FCS_LENGTH_PRESENT(x) ((x) & 0x04000000)
176 #define LT_FCS_LENGTH(x) (((x) & 0xF0000000) >> 28)
177 #define LT_FCS_DATALINK_EXT(x) ((((x) & 0xF) << 28) | 0x04000000)
178
179 typedef enum {
180 PCAP_D_INOUT = 0,
181 PCAP_D_IN,
182 PCAP_D_OUT
183 } pcap_direction_t;
184
185 /*
186 * Generic per-packet information, as supplied by libpcap.
187 *
188 * The time stamp can and should be a "struct timeval", regardless of
189 * whether your system supports 32-bit tv_sec in "struct timeval",
190 * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit
191 * and 64-bit applications. The on-disk format of savefiles uses 32-bit
192 * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit
193 * and 64-bit versions of libpcap, even if they're on the same platform,
194 * should supply the appropriate version of "struct timeval", even if
195 * that's not what the underlying packet capture mechanism supplies.
196 */
197 struct pcap_pkthdr {
198 struct timeval ts; /* time stamp */
199 bpf_u_int32 caplen; /* length of portion present */
200 bpf_u_int32 len; /* length this packet (off wire) */
201 };
202
203 /*
204 * As returned by the pcap_stats()
205 */
206 struct pcap_stat {
207 u_int ps_recv; /* number of packets received */
208 u_int ps_drop; /* number of packets dropped */
209 u_int ps_ifdrop; /* drops by interface -- only supported on some platforms */
210 #ifdef _WIN32
211 u_int ps_capt; /* number of packets that reach the application */
212 u_int ps_sent; /* number of packets sent by the server on the network */
213 u_int ps_netdrop; /* number of packets lost on the network */
214 #endif /* _WIN32 */
215 };
216
217 #ifdef MSDOS
218 /*
219 * As returned by the pcap_stats_ex()
220 */
221 struct pcap_stat_ex {
222 u_long rx_packets; /* total packets received */
223 u_long tx_packets; /* total packets transmitted */
224 u_long rx_bytes; /* total bytes received */
225 u_long tx_bytes; /* total bytes transmitted */
226 u_long rx_errors; /* bad packets received */
227 u_long tx_errors; /* packet transmit problems */
228 u_long rx_dropped; /* no space in Rx buffers */
229 u_long tx_dropped; /* no space available for Tx */
230 u_long multicast; /* multicast packets received */
231 u_long collisions;
232
233 /* detailed rx_errors: */
234 u_long rx_length_errors;
235 u_long rx_over_errors; /* receiver ring buff overflow */
236 u_long rx_crc_errors; /* recv'd pkt with crc error */
237 u_long rx_frame_errors; /* recv'd frame alignment error */
238 u_long rx_fifo_errors; /* recv'r fifo overrun */
239 u_long rx_missed_errors; /* recv'r missed packet */
240
241 /* detailed tx_errors */
242 u_long tx_aborted_errors;
243 u_long tx_carrier_errors;
244 u_long tx_fifo_errors;
245 u_long tx_heartbeat_errors;
246 u_long tx_window_errors;
247 };
248 #endif
249
250 /*
251 * Item in a list of interfaces.
252 */
253 struct pcap_if {
254 struct pcap_if *next;
255 char *name; /* name to hand to "pcap_open_live()" */
256 char *description; /* textual description of interface, or NULL */
257 struct pcap_addr *addresses;
258 bpf_u_int32 flags; /* PCAP_IF_ interface flags */
259 };
260
261 #define PCAP_IF_LOOPBACK 0x00000001 /* interface is loopback */
262 #define PCAP_IF_UP 0x00000002 /* interface is up */
263 #define PCAP_IF_RUNNING 0x00000004 /* interface is running */
264
265 /*
266 * Representation of an interface address.
267 */
268 struct pcap_addr {
269 struct pcap_addr *next;
270 struct sockaddr *addr; /* address */
271 struct sockaddr *netmask; /* netmask for that address */
272 struct sockaddr *broadaddr; /* broadcast address for that address */
273 struct sockaddr *dstaddr; /* P2P destination address for that address */
274 };
275
276 typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
277 const u_char *);
278
279 /*
280 * Error codes for the pcap API.
281 * These will all be negative, so you can check for the success or
282 * failure of a call that returns these codes by checking for a
283 * negative value.
284 */
285 #define PCAP_ERROR -1 /* generic error code */
286 #define PCAP_ERROR_BREAK -2 /* loop terminated by pcap_breakloop */
287 #define PCAP_ERROR_NOT_ACTIVATED -3 /* the capture needs to be activated */
288 #define PCAP_ERROR_ACTIVATED -4 /* the operation can't be performed on already activated captures */
289 #define PCAP_ERROR_NO_SUCH_DEVICE -5 /* no such device exists */
290 #define PCAP_ERROR_RFMON_NOTSUP -6 /* this device doesn't support rfmon (monitor) mode */
291 #define PCAP_ERROR_NOT_RFMON -7 /* operation supported only in monitor mode */
292 #define PCAP_ERROR_PERM_DENIED -8 /* no permission to open the device */
293 #define PCAP_ERROR_IFACE_NOT_UP -9 /* interface isn't up */
294 #define PCAP_ERROR_CANTSET_TSTAMP_TYPE -10 /* this device doesn't support setting the time stamp type */
295 #define PCAP_ERROR_PROMISC_PERM_DENIED -11 /* you don't have permission to capture in promiscuous mode */
296 #define PCAP_ERROR_TSTAMP_PRECISION_NOTSUP -12 /* the requested time stamp precision is not supported */
297
298 /*
299 * Warning codes for the pcap API.
300 * These will all be positive and non-zero, so they won't look like
301 * errors.
302 */
303 #define PCAP_WARNING 1 /* generic warning code */
304 #define PCAP_WARNING_PROMISC_NOTSUP 2 /* this device doesn't support promiscuous mode */
305 #define PCAP_WARNING_TSTAMP_TYPE_NOTSUP 3 /* the requested time stamp type is not supported */
306
307 /*
308 * Value to pass to pcap_compile() as the netmask if you don't know what
309 * the netmask is.
310 */
311 #define PCAP_NETMASK_UNKNOWN 0xffffffff
312
313 PCAP_API char *pcap_lookupdev(char *);
314 PCAP_API int pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *);
315
316 PCAP_API pcap_t *pcap_create(const char *, char *);
317 PCAP_API int pcap_set_snaplen(pcap_t *, int);
318 PCAP_API int pcap_set_promisc(pcap_t *, int);
319 PCAP_API int pcap_can_set_rfmon(pcap_t *);
320 PCAP_API int pcap_set_rfmon(pcap_t *, int);
321 PCAP_API int pcap_set_timeout(pcap_t *, int);
322 PCAP_API int pcap_set_tstamp_type(pcap_t *, int);
323 PCAP_API int pcap_set_immediate_mode(pcap_t *, int);
324 PCAP_API int pcap_set_buffer_size(pcap_t *, int);
325 PCAP_API int pcap_set_tstamp_precision(pcap_t *, int);
326 PCAP_API int pcap_get_tstamp_precision(pcap_t *);
327 PCAP_API int pcap_activate(pcap_t *);
328
329 PCAP_API int pcap_list_tstamp_types(pcap_t *, int **);
330 PCAP_API void pcap_free_tstamp_types(int *);
331 PCAP_API int pcap_tstamp_type_name_to_val(const char *);
332 PCAP_API const char *pcap_tstamp_type_val_to_name(int);
333 PCAP_API const char *pcap_tstamp_type_val_to_description(int);
334
335 #ifdef __linux__
336 PCAP_API int pcap_set_protocol(pcap_t *, int);
337 #endif
338
339 /*
340 * Time stamp types.
341 * Not all systems and interfaces will necessarily support all of these.
342 *
343 * A system that supports PCAP_TSTAMP_HOST is offering time stamps
344 * provided by the host machine, rather than by the capture device,
345 * but not committing to any characteristics of the time stamp;
346 * it will not offer any of the PCAP_TSTAMP_HOST_ subtypes.
347 *
348 * PCAP_TSTAMP_HOST_LOWPREC is a time stamp, provided by the host machine,
349 * that's low-precision but relatively cheap to fetch; it's normally done
350 * using the system clock, so it's normally synchronized with times you'd
351 * fetch from system calls.
352 *
353 * PCAP_TSTAMP_HOST_HIPREC is a time stamp, provided by the host machine,
354 * that's high-precision; it might be more expensive to fetch. It might
355 * or might not be synchronized with the system clock, and might have
356 * problems with time stamps for packets received on different CPUs,
357 * depending on the platform.
358 *
359 * PCAP_TSTAMP_ADAPTER is a high-precision time stamp supplied by the
360 * capture device; it's synchronized with the system clock.
361 *
362 * PCAP_TSTAMP_ADAPTER_UNSYNCED is a high-precision time stamp supplied by
363 * the capture device; it's not synchronized with the system clock.
364 *
365 * Note that time stamps synchronized with the system clock can go
366 * backwards, as the system clock can go backwards. If a clock is
367 * not in sync with the system clock, that could be because the
368 * system clock isn't keeping accurate time, because the other
369 * clock isn't keeping accurate time, or both.
370 *
371 * Note that host-provided time stamps generally correspond to the
372 * time when the time-stamping code sees the packet; this could
373 * be some unknown amount of time after the first or last bit of
374 * the packet is received by the network adapter, due to batching
375 * of interrupts for packet arrival, queueing delays, etc..
376 */
377 #define PCAP_TSTAMP_HOST 0 /* host-provided, unknown characteristics */
378 #define PCAP_TSTAMP_HOST_LOWPREC 1 /* host-provided, low precision */
379 #define PCAP_TSTAMP_HOST_HIPREC 2 /* host-provided, high precision */
380 #define PCAP_TSTAMP_ADAPTER 3 /* device-provided, synced with the system clock */
381 #define PCAP_TSTAMP_ADAPTER_UNSYNCED 4 /* device-provided, not synced with the system clock */
382
383 /*
384 * Time stamp resolution types.
385 * Not all systems and interfaces will necessarily support all of these
386 * resolutions when doing live captures; all of them can be requested
387 * when reading a savefile.
388 */
389 #define PCAP_TSTAMP_PRECISION_MICRO 0 /* use timestamps with microsecond precision, default */
390 #define PCAP_TSTAMP_PRECISION_NANO 1 /* use timestamps with nanosecond precision */
391
392 PCAP_API pcap_t *pcap_open_live(const char *, int, int, int, char *);
393 PCAP_API pcap_t *pcap_open_dead(int, int);
394 PCAP_API pcap_t *pcap_open_dead_with_tstamp_precision(int, int, u_int);
395 PCAP_API pcap_t *pcap_open_offline_with_tstamp_precision(const char *, u_int, char *);
396 PCAP_API pcap_t *pcap_open_offline(const char *, char *);
397 #ifdef _WIN32
398 PCAP_API pcap_t *pcap_hopen_offline_with_tstamp_precision(intptr_t, u_int, char *);
399 PCAP_API pcap_t *pcap_hopen_offline(intptr_t, char *);
400 /*
401 * If we're building libpcap, these are internal routines in savefile.c,
402 * so we must not define them as macros.
403 *
404 * If we're not building libpcap, given that the version of the C runtime
405 * with which libpcap was built might be different from the version
406 * of the C runtime with which an application using libpcap was built,
407 * and that a FILE structure may differ between the two versions of the
408 * C runtime, calls to _fileno() must use the version of _fileno() in
409 * the C runtime used to open the FILE *, not the version in the C
410 * runtime with which libpcap was built. (Maybe once the Universal CRT
411 * rules the world, this will cease to be a problem.)
412 */
413 #ifndef BUILDING_PCAP
414 #define pcap_fopen_offline_with_tstamp_precision(f,p,b) \
415 pcap_hopen_offline_with_tstamp_precision(_get_osfhandle(_fileno(f)), p, b)
416 #define pcap_fopen_offline(f,b) \
417 pcap_hopen_offline(_get_osfhandle(_fileno(f)), b)
418 #endif
419 #else /*_WIN32*/
420 PCAP_API pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
421 PCAP_API pcap_t *pcap_fopen_offline(FILE *, char *);
422 #endif /*_WIN32*/
423
424 PCAP_API void pcap_close(pcap_t *);
425 PCAP_API int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
426 PCAP_API int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
427 PCAP_API const u_char *pcap_next(pcap_t *, struct pcap_pkthdr *);
428 PCAP_API int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
429 PCAP_API void pcap_breakloop(pcap_t *);
430 PCAP_API int pcap_stats(pcap_t *, struct pcap_stat *);
431 PCAP_API int pcap_setfilter(pcap_t *, struct bpf_program *);
432 PCAP_API int pcap_setdirection(pcap_t *, pcap_direction_t);
433 PCAP_API int pcap_getnonblock(pcap_t *, char *);
434 PCAP_API int pcap_setnonblock(pcap_t *, int, char *);
435 PCAP_API int pcap_inject(pcap_t *, const void *, size_t);
436 PCAP_API int pcap_sendpacket(pcap_t *, const u_char *, int);
437 PCAP_API const char *pcap_statustostr(int);
438 PCAP_API const char *pcap_strerror(int);
439 PCAP_API char *pcap_geterr(pcap_t *);
440 PCAP_API void pcap_perror(pcap_t *, const char *);
441 PCAP_API int pcap_compile(pcap_t *, struct bpf_program *, const char *, int,
442 bpf_u_int32);
443 PCAP_API int pcap_compile_nopcap(int, int, struct bpf_program *,
444 const char *, int, bpf_u_int32);
445 PCAP_API void pcap_freecode(struct bpf_program *);
446 PCAP_API int pcap_offline_filter(const struct bpf_program *,
447 const struct pcap_pkthdr *, const u_char *);
448 PCAP_API int pcap_datalink(pcap_t *);
449 PCAP_API int pcap_datalink_ext(pcap_t *);
450 PCAP_API int pcap_list_datalinks(pcap_t *, int **);
451 PCAP_API int pcap_set_datalink(pcap_t *, int);
452 PCAP_API void pcap_free_datalinks(int *);
453 PCAP_API int pcap_datalink_name_to_val(const char *);
454 PCAP_API const char *pcap_datalink_val_to_name(int);
455 PCAP_API const char *pcap_datalink_val_to_description(int);
456 PCAP_API int pcap_snapshot(pcap_t *);
457 PCAP_API int pcap_is_swapped(pcap_t *);
458 PCAP_API int pcap_major_version(pcap_t *);
459 PCAP_API int pcap_minor_version(pcap_t *);
460 PCAP_API int pcap_bufsize(pcap_t *);
461
462 /* XXX */
463 PCAP_API FILE *pcap_file(pcap_t *);
464 PCAP_API int pcap_fileno(pcap_t *);
465
466 #ifdef _WIN32
467 PCAP_API int pcap_wsockinit(void);
468 #endif
469
470 PCAP_API pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
471 PCAP_API pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp);
472 PCAP_API pcap_dumper_t *pcap_dump_open_append(pcap_t *, const char *);
473 PCAP_API FILE *pcap_dump_file(pcap_dumper_t *);
474 PCAP_API long pcap_dump_ftell(pcap_dumper_t *);
475 PCAP_API int pcap_dump_flush(pcap_dumper_t *);
476 PCAP_API void pcap_dump_close(pcap_dumper_t *);
477 PCAP_API void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
478
479 PCAP_API int pcap_findalldevs(pcap_if_t **, char *);
480 PCAP_API void pcap_freealldevs(pcap_if_t *);
481
482 PCAP_API const char *pcap_lib_version(void);
483
484 /*
485 * On at least some versions of NetBSD and QNX, we don't want to declare
486 * bpf_filter() here, as it's also be declared in <net/bpf.h>, with a
487 * different signature, but, on other BSD-flavored UN*Xes, it's not
488 * declared in <net/bpf.h>, so we *do* want to declare it here, so it's
489 * declared when we build pcap-bpf.c.
490 */
491 #if !defined(__NetBSD__) && !defined(__QNX__)
492 PCAP_API u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
493 #endif
494 PCAP_API int bpf_validate(const struct bpf_insn *f, int len);
495 PCAP_API char *bpf_image(const struct bpf_insn *, int);
496 PCAP_API void bpf_dump(const struct bpf_program *, int);
497
498 #if defined(_WIN32)
499
500 /*
501 * Win32 definitions
502 */
503
504 /*!
505 \brief A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit().
506 */
507 struct pcap_send_queue
508 {
509 u_int maxlen; /* Maximum size of the queue, in bytes. This
510 variable contains the size of the buffer field. */
511 u_int len; /* Current size of the queue, in bytes. */
512 char *buffer; /* Buffer containing the packets to be sent. */
513 };
514
515 typedef struct pcap_send_queue pcap_send_queue;
516
517 /*!
518 \brief This typedef is a support for the pcap_get_airpcap_handle() function
519 */
520 #if !defined(AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_)
521 #define AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_
522 typedef struct _AirpcapHandle *PAirpcapHandle;
523 #endif
524
525 PCAP_API int pcap_setbuff(pcap_t *p, int dim);
526 PCAP_API int pcap_setmode(pcap_t *p, int mode);
527 PCAP_API int pcap_setmintocopy(pcap_t *p, int size);
528
529 PCAP_API HANDLE pcap_getevent(pcap_t *p);
530
531 PCAP_API int pcap_oid_get_request(pcap_t *, bpf_u_int32, void *, size_t *);
532 PCAP_API int pcap_oid_set_request(pcap_t *, bpf_u_int32, const void *, size_t *);
533
534 PCAP_API pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
535
536 PCAP_API void pcap_sendqueue_destroy(pcap_send_queue* queue);
537
538 PCAP_API int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);
539
540 PCAP_API u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);
541
542 PCAP_API struct pcap_stat *pcap_stats_ex(pcap_t *p, int *pcap_stat_size);
543
544 PCAP_API int pcap_setuserbuffer(pcap_t *p, int size);
545
546 PCAP_API int pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks);
547
548 PCAP_API int pcap_live_dump_ended(pcap_t *p, int sync);
549
550 PCAP_API int pcap_start_oem(char* err_str, int flags);
551
552 PCAP_API PAirpcapHandle pcap_get_airpcap_handle(pcap_t *p);
553
554 #define MODE_CAPT 0
555 #define MODE_STAT 1
556 #define MODE_MON 2
557
558 #elif defined(MSDOS)
559
560 /*
561 * MS-DOS definitions
562 */
563
564 PCAP_API int pcap_stats_ex (pcap_t *, struct pcap_stat_ex *);
565 PCAP_API void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait);
566 PCAP_API u_long pcap_mac_packets (void);
567
568 #else /* UN*X */
569
570 /*
571 * UN*X definitions
572 */
573
574 PCAP_API int pcap_get_selectable_fd(pcap_t *);
575
576 #endif /* _WIN32/MSDOS/UN*X */
577
578 /*
579 * Remote capture definitions.
580 *
581 * These routines are only present if libpcap has been configured to
582 * include remote capture support.
583 */
584
585 /*
586 * The maximum buffer size in which address, port, interface names are kept.
587 *
588 * In case the adapter name or such is larger than this value, it is truncated.
589 * This is not used by the user; however it must be aware that an hostname / interface
590 * name longer than this value will be truncated.
591 */
592 #define PCAP_BUF_SIZE 1024
593
594 /*
595 * The type of input source, passed to pcap_open().
596 */
597 #define PCAP_SRC_FILE 2 /* local savefile */
598 #define PCAP_SRC_IFLOCAL 3 /* local network interface */
599 #define PCAP_SRC_IFREMOTE 4 /* interface on a remote host, using RPCAP */
600
601 /*
602 * The formats allowed by pcap_open() are the following:
603 * - file://path_and_filename [opens a local file]
604 * - rpcap://devicename [opens the selected device devices available on the local host, without using the RPCAP protocol]
605 * - rpcap://host/devicename [opens the selected device available on a remote host]
606 * - rpcap://host:port/devicename [opens the selected device available on a remote host, using a non-standard port for RPCAP]
607 * - adaptername [to open a local adapter; kept for compability, but it is strongly discouraged]
608 * - (NULL) [to open the first local adapter; kept for compability, but it is strongly discouraged]
609 *
610 * The formats allowed by the pcap_findalldevs_ex() are the following:
611 * - file://folder/ [lists all the files in the given folder]
612 * - rpcap:// [lists all local adapters]
613 * - rpcap://host:port/ [lists the devices available on a remote host]
614 *
615 * Referring to the 'host' and 'port' parameters, they can be either numeric or literal. Since
616 * IPv6 is fully supported, these are the allowed formats:
617 *
618 * - host (literal): e.g. host.foo.bar
619 * - host (numeric IPv4): e.g. 10.11.12.13
620 * - host (numeric IPv4, IPv6 style): e.g. [10.11.12.13]
621 * - host (numeric IPv6): e.g. [1:2:3::4]
622 * - port: can be either numeric (e.g. '80') or literal (e.g. 'http')
623 *
624 * Here you find some allowed examples:
625 * - rpcap://host.foo.bar/devicename [everything literal, no port number]
626 * - rpcap://host.foo.bar:1234/devicename [everything literal, with port number]
627 * - rpcap://10.11.12.13/devicename [IPv4 numeric, no port number]
628 * - rpcap://10.11.12.13:1234/devicename [IPv4 numeric, with port number]
629 * - rpcap://[10.11.12.13]:1234/devicename [IPv4 numeric with IPv6 format, with port number]
630 * - rpcap://[1:2:3::4]/devicename [IPv6 numeric, no port number]
631 * - rpcap://[1:2:3::4]:1234/devicename [IPv6 numeric, with port number]
632 * - rpcap://[1:2:3::4]:http/devicename [IPv6 numeric, with literal port number]
633 */
634
635 /*
636 * URL schemes for capture source.
637 */
638 /*
639 * This string indicates that the user wants to open a capture from a
640 * local file.
641 */
642 #define PCAP_SRC_FILE_STRING "file://"
643 /*
644 * This string indicates that the user wants to open a capture from a
645 * network interface. This string does not necessarily involve the use
646 * of the RPCAP protocol. If the interface required resides on the local
647 * host, the RPCAP protocol is not involved and the local functions are used.
648 */
649 #define PCAP_SRC_IF_STRING "rpcap://"
650
651 /*
652 * Flags to pass to pcap_open().
653 */
654
655 /*
656 * Specifies whether promiscuous mode is to be used.
657 */
658 #define PCAP_OPENFLAG_PROMISCUOUS 0x00000001
659
660 /*
661 * Specifies, for an RPCAP capture, whether the data transfer (in
662 * case of a remote capture) has to be done with UDP protocol.
663 *
664 * If it is '1' if you want a UDP data connection, '0' if you want
665 * a TCP data connection; control connection is always TCP-based.
666 * A UDP connection is much lighter, but it does not guarantee that all
667 * the captured packets arrive to the client workstation. Moreover,
668 * it could be harmful in case of network congestion.
669 * This flag is meaningless if the source is not a remote interface.
670 * In that case, it is simply ignored.
671 */
672 #define PCAP_OPENFLAG_DATATX_UDP 0x00000002
673
674 /*
675 * Specifies wheether the remote probe will capture its own generated
676 * traffic.
677 *
678 * In case the remote probe uses the same interface to capture traffic
679 * and to send data back to the caller, the captured traffic includes
680 * the RPCAP traffic as well. If this flag is turned on, the RPCAP
681 * traffic is excluded from the capture, so that the trace returned
682 * back to the collector is does not include this traffic.
683 *
684 * Has no effect on local interfaces or savefiles.
685 */
686 #define PCAP_OPENFLAG_NOCAPTURE_RPCAP 0x00000004
687
688 /*
689 * Specifies whether the local adapter will capture its own generated traffic.
690 *
691 * This flag tells the underlying capture driver to drop the packets
692 * that were sent by itself. This is useful when building applications
693 * such as bridges that should ignore the traffic they just sent.
694 *
695 * Supported only on Windows.
696 */
697 #define PCAP_OPENFLAG_NOCAPTURE_LOCAL 0x00000008
698
699 /*
700 * This flag configures the adapter for maximum responsiveness.
701 *
702 * In presence of a large value for nbytes, WinPcap waits for the arrival
703 * of several packets before copying the data to the user. This guarantees
704 * a low number of system calls, i.e. lower processor usage, i.e. better
705 * performance, which is good for applications like sniffers. If the user
706 * sets the PCAP_OPENFLAG_MAX_RESPONSIVENESS flag, the capture driver will
707 * copy the packets as soon as the application is ready to receive them.
708 * This is suggested for real time applications (such as, for example,
709 * a bridge) that need the best responsiveness.
710 *
711 * The equivalent with pcap_create()/pcap_activate() is "immediate mode".
712 */
713 #define PCAP_OPENFLAG_MAX_RESPONSIVENESS 0x00000010
714
715 /*
716 * Remote authentication methods.
717 * These are used in the 'type' member of the pcap_rmtauth structure.
718 */
719
720 /*
721 * NULL authentication.
722 *
723 * The 'NULL' authentication has to be equal to 'zero', so that old
724 * applications can just put every field of struct pcap_rmtauth to zero,
725 * and it does work.
726 */
727 #define RPCAP_RMTAUTH_NULL 0
728 /*
729 * Username/password authentication.
730 *
731 * With this type of authentication, the RPCAP protocol will use the username/
732 * password provided to authenticate the user on the remote machine. If the
733 * authentication is successful (and the user has the right to open network
734 * devices) the RPCAP connection will continue; otherwise it will be dropped.
735 *
736 * *******NOTE********: the username and password are sent over the network
737 * to the capture server *IN CLEAR TEXT*. Don't use this on a network
738 * that you don't completely control! (And be *really* careful in your
739 * definition of "completely"!)
740 */
741 #define RPCAP_RMTAUTH_PWD 1
742
743 /*
744 * This structure keeps the information needed to autheticate the user
745 * on a remote machine.
746 *
747 * The remote machine can either grant or refuse the access according
748 * to the information provided.
749 * In case the NULL authentication is required, both 'username' and
750 * 'password' can be NULL pointers.
751 *
752 * This structure is meaningless if the source is not a remote interface;
753 * in that case, the functions which requires such a structure can accept
754 * a NULL pointer as well.
755 */
756 struct pcap_rmtauth
757 {
758 /*
759 * \brief Type of the authentication required.
760 *
761 * In order to provide maximum flexibility, we can support different types
762 * of authentication based on the value of this 'type' variable. The currently
763 * supported authentication methods are defined into the
764 * \link remote_auth_methods Remote Authentication Methods Section\endlink.
765 */
766 int type;
767 /*
768 * \brief Zero-terminated string containing the username that has to be
769 * used on the remote machine for authentication.
770 *
771 * This field is meaningless in case of the RPCAP_RMTAUTH_NULL authentication
772 * and it can be NULL.
773 */
774 char *username;
775 /*
776 * \brief Zero-terminated string containing the password that has to be
777 * used on the remote machine for authentication.
778 *
779 * This field is meaningless in case of the RPCAP_RMTAUTH_NULL authentication
780 * and it can be NULL.
781 */
782 char *password;
783 };
784
785 /*
786 * This routine can open a savefile, a local device, or a device on
787 * a remote machine running an RPCAP server.
788 *
789 * For opening a savefile, the pcap_open_offline routines can be used,
790 * and will work just as well; code using them will work on more
791 * platforms than code using pcap_open() to open savefiles.
792 *
793 * For opening a local device, pcap_open_live() can be used; it supports
794 * most of the capabilities that pcap_open() supports, and code using it
795 * will work on more platforms than code using pcap_open(). pcap_create()
796 * and pcap_activate() can also be used; they support all capabilities
797 * that pcap_open() supports, except for the Windows-only
798 * PCAP_OPENFLAG_NOCAPTURE_LOCAL, and support additional capabilities.
799 *
800 * For opening a remote capture, pcap_open() is currently the only
801 * API available.
802 */
803 PCAP_API pcap_t *pcap_open(const char *source, int snaplen, int flags,
804 int read_timeout, struct pcap_rmtauth *auth, char *errbuf);
805 PCAP_API int pcap_createsrcstr(char *source, int type, const char *host,
806 const char *port, const char *name, char *errbuf);
807 PCAP_API int pcap_parsesrcstr(const char *source, int *type, char *host,
808 char *port, char *name, char *errbuf);
809
810 /*
811 * This routine can scan a directory for savefiles, list local capture
812 * devices, or list capture devices on a remote machine running an RPCAP
813 * server.
814 *
815 * For scanning for savefiles, it can be used on both UN*X systems and
816 * Windows systems; for each directory entry it sees, it tries to open
817 * the file as a savefile using pcap_open_offline(), and only includes
818 * it in the list of files if the open succeeds, so it filters out
819 * files for which the user doesn't have read permission, as well as
820 * files that aren't valid savefiles readable by libpcap.
821 *
822 * For listing local capture devices, it's just a wrapper around
823 * pcap_findalldevs(); code using pcap_findalldevs() will work on more
824 * platforms than code using pcap_findalldevs_ex().
825 *
826 * For listing remote capture devices, pcap_findalldevs_ex() is currently
827 * the only API available.
828 */
829 PCAP_API int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth,
830 pcap_if_t **alldevs, char *errbuf);
831
832 /*
833 * Sampling methods.
834 *
835 * These allow pcap_loop(), pcap_dispatch(), pcap_next(), and pcap_next_ex()
836 * to see only a sample of packets, rather than all packets.
837 *
838 * Currently, they work only on Windows local captures.
839 */
840
841 /*
842 * Specifies that no sampling is to be done on the current capture.
843 *
844 * In this case, no sampling algorithms are applied to the current capture.
845 */
846 #define PCAP_SAMP_NOSAMP 0
847
848 /*
849 * Specifies that only 1 out of N packets must be returned to the user.
850 *
851 * In this case, the 'value' field of the 'pcap_samp' structure indicates the
852 * number of packets (minus 1) that must be discarded before one packet got
853 * accepted.
854 * In other words, if 'value = 10', the first packet is returned to the
855 * caller, while the following 9 are discarded.
856 */
857 #define PCAP_SAMP_1_EVERY_N 1
858
859 /*
860 * Specifies that we have to return 1 packet every N milliseconds.
861 *
862 * In this case, the 'value' field of the 'pcap_samp' structure indicates
863 * the 'waiting time' in milliseconds before one packet got accepted.
864 * In other words, if 'value = 10', the first packet is returned to the
865 * caller; the next returned one will be the first packet that arrives
866 * when 10ms have elapsed.
867 */
868 #define PCAP_SAMP_FIRST_AFTER_N_MS 2
869
870 /*
871 * This structure defines the information related to sampling.
872 *
873 * In case the sampling is requested, the capturing device should read
874 * only a subset of the packets coming from the source. The returned packets
875 * depend on the sampling parameters.
876 *
877 * WARNING: The sampling process is applied *after* the filtering process.
878 * In other words, packets are filtered first, then the sampling process
879 * selects a subset of the 'filtered' packets and it returns them to the
880 * caller.
881 */
882 struct pcap_samp
883 {
884 /*
885 * Method used for sampling; see above.
886 */
887 int method;
888
889 /*
890 * This value depends on the sampling method defined.
891 * For its meaning, see above.
892 */
893 int value;
894 };
895
896 /*
897 * New functions.
898 */
899 PCAP_API struct pcap_samp *pcap_setsampling(pcap_t *p);
900
901 /*
902 * RPCAP active mode.
903 */
904
905 /* Maximum length of an host name (needed for the RPCAP active mode) */
906 #define RPCAP_HOSTLIST_SIZE 1024
907
908 /*
909 * Some minor differences between UN*X sockets and and Winsock sockets.
910 */
911 #ifndef _WIN32
912 /*!
913 * \brief In Winsock, a socket handle is of type SOCKET; in UN*X, it's
914 * a file descriptor, and therefore a signed integer.
915 * We define SOCKET to be a signed integer on UN*X, so that it can
916 * be used on both platforms.
917 */
918 #define SOCKET int
919
920 /*!
921 * \brief In Winsock, the error return if socket() fails is INVALID_SOCKET;
922 * in UN*X, it's -1.
923 * We define INVALID_SOCKET to be -1 on UN*X, so that it can be used on
924 * both platforms.
925 */
926 #define INVALID_SOCKET -1
927 #endif
928
929 PCAP_API SOCKET pcap_remoteact_accept(const char *address, const char *port,
930 const char *hostlist, char *connectinghost,
931 struct pcap_rmtauth *auth, char *errbuf);
932 PCAP_API int pcap_remoteact_list(char *hostlist, char sep, int size,
933 char *errbuf);
934 PCAP_API int pcap_remoteact_close(const char *host, char *errbuf);
935 PCAP_API void pcap_remoteact_cleanup(void);
936
937 #ifdef __cplusplus
938 }
939 #endif
940
941 #endif /* lib_pcap_pcap_h */