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