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 module libpcap.pcap; 36 extern (C): 37 38 /+ 39 #if defined(WIN32) 40 #include <pcap-stdinc.h> 41 #elif defined(MSDOS) 42 #include <sys/types.h> 43 #include <sys/socket.h> /* uint, ubyte etc. */ 44 #else /* UN*X */ 45 #include <sys/types.h> 46 #include <sys/time.h> 47 #endif /* WIN32/MSDOS/UN*X */ 48 49 #ifndef PCAP_DONT_INCLUDE_PCAP_BPF_H 50 #include <pcap/bpf.h> 51 #endif 52 +/ 53 54 import libpcap.bpf; 55 import core.stdc.stdio; 56 alias void bpf_insn; 57 58 /* 59 * Version number of the current version of the pcap file format. 60 * 61 * NOTE: this is *NOT* the version number of the libpcap library. 62 * To fetch the version information for the version of libpcap 63 * you're using, use pcap_lib_version(). 64 */ 65 immutable PCAP_VERSION_MAJOR = 2; 66 immutable PCAP_VERSION_MINOR = 4; 67 68 immutable PCAP_ERRBUF_SIZE = 256; 69 70 /+ 71 /* 72 * Compatibility for systems that have a bpf.h that 73 * predates the bpf aliass for 64-bit support. 74 */ 75 #if BPF_RELEASE - 0 < 199406 76 alias int bpf_int32; 77 alias uint uint; 78 #endif 79 +/ 80 81 alias void pcap_t; 82 alias void pcap_dumper_t; 83 alias pcap_if pcap_if_t; 84 alias pcap_addr pcap_addr_t; 85 86 /* 87 * The first record in the file contains saved values for some 88 * of the flags used in the printout phases of tcpdump. 89 * Many fields here are 32 bit ints so compilers won't insert unwanted 90 * padding; these files need to be interchangeable across architectures. 91 * 92 * Do not change the layout of this structure, in any way (this includes 93 * changes that only affect the length of fields in this structure). 94 * 95 * Also, do not change the interpretation of any of the members of this 96 * structure, in any way (this includes using values other than 97 * LINKTYPE_ values, as defined in "savefile.c", in the "linktype" 98 * field). 99 * 100 * Instead: 101 * 102 * introduce a new structure for the new format, if the layout 103 * of the structure changed; 104 * 105 * send mail to "tcpdump-workers@lists.tcpdump.org", requesting 106 * a new magic number for your new capture file format, and, when 107 * you get the new magic number, put it in "savefile.c"; 108 * 109 * use that magic number for save files with the changed file 110 * header; 111 * 112 * make the code in "savefile.c" capable of reading files with 113 * the old file header as well as files with the new file header 114 * (using the magic number to determine the header format). 115 * 116 * Then supply the changes by forking the branch at 117 * 118 * https://github.com/the-tcpdump-group/libpcap/issues 119 * 120 * and issuing a pull request, so that future versions of libpcap and 121 * programs that use it (such as tcpdump) will be able to read your new 122 * capture file format. 123 */ 124 struct pcap_file_header { 125 uint magic; 126 ushort version_major; 127 ushort version_minor; 128 int thiszone; /* gmt to local correction */ 129 uint sigfigs; /* accuracy of timestamps */ 130 uint snaplen; /* max length saved portion of each pkt */ 131 uint linktype; /* data link type (LINKTYPE_*) */ 132 } 133 134 /* 135 * Macros for the value returned by pcap_datalink_ext(). 136 * 137 * If LT_FCS_LENGTH_PRESENT(x) is true, the LT_FCS_LENGTH(x) macro 138 * gives the FCS length of packets in the capture. 139 */ 140 auto LT_FCS_LENGTH_PRESENT(T)(T x) { return x & 0x04000000; } 141 auto LT_FCS_LENGTH(T)(T x) { return ((x & 0xF0000000) >> 28) ; } 142 auto LT_FCS_DATALINK_EXT(T)(T x) { return (((x & 0xF) << 28) | 0x04000000);} 143 144 enum pcap_direction_t { 145 PCAP_D_INOUT = 0, 146 PCAP_D_IN, 147 PCAP_D_OUT 148 } 149 150 /* 151 * Generic per-packet information, as supplied by libpcap. 152 * 153 * The time stamp can and should be a "struct timeval", regardless of 154 * whether your system supports 32-bit tv_sec in "struct timeval", 155 * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit 156 * and 64-bit applications. The on-disk format of savefiles uses 32-bit 157 * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit 158 * and 64-bit versions of libpcap, even if they're on the same platform, 159 * should supply the appropriate version of "struct timeval", even if 160 * that's not what the underlying packet capture mechanism supplies. 161 */ 162 163 struct timeval { 164 uint tv_sec; 165 uint tv_usec; 166 } 167 168 struct pcap_pkthdr { 169 timeval ts; /* time stamp */ 170 uint caplen; /* length of portion present */ 171 uint len; /* length this packet (off wire) */ 172 } 173 174 /* 175 * As returned by the pcap_stats() 176 */ 177 struct pcap_stat { 178 uint ps_recv; /* number of packets received */ 179 uint ps_drop; /* number of packets dropped */ 180 uint ps_ifdrop; /* drops by interface -- only supported on some platforms */ 181 /+ 182 #ifdef WIN32 183 uint bs_capt; /* number of packets that reach the application */ 184 #endif /* WIN32 */ 185 +/ 186 } 187 188 /+ 189 #ifdef MSDOS 190 /* 191 * As returned by the pcap_stats_ex() 192 */ 193 struct pcap_stat_ex { 194 ulong rx_packets; /* total packets received */ 195 ulong tx_packets; /* total packets transmitted */ 196 ulong rx_bytes; /* total bytes received */ 197 ulong tx_bytes; /* total bytes transmitted */ 198 ulong rx_errors; /* bad packets received */ 199 ulong tx_errors; /* packet transmit problems */ 200 ulong rx_dropped; /* no space in Rx buffers */ 201 ulong tx_dropped; /* no space available for Tx */ 202 ulong multicast; /* multicast packets received */ 203 ulong collisions; 204 205 /* detailed rx_errors: */ 206 ulong rx_length_errors; 207 ulong rx_over_errors; /* receiver ring buff overflow */ 208 ulong rx_crc_errors; /* recv'd pkt with crc error */ 209 ulong rx_frame_errors; /* recv'd frame alignment error */ 210 ulong rx_fifo_errors; /* recv'r fifo overrun */ 211 ulong rx_missed_errors; /* recv'r missed packet */ 212 213 /* detailed tx_errors */ 214 ulong tx_aborted_errors; 215 ulong tx_carrier_errors; 216 ulong tx_fifo_errors; 217 ulong tx_heartbeat_errors; 218 ulong tx_window_errors; 219 } 220 #endif 221 +/ 222 223 /* 224 * Item in a list of interfaces. 225 */ 226 struct pcap_if { 227 pcap_if *next; 228 char *name; /* name to hand to "pcap_open_live()" */ 229 char *description; /* textual description of interface, or NULL */ 230 pcap_addr *addresses; 231 uint flags; /* PCAP_IF_ interface flags */ 232 } 233 234 immutable PCAP_IF_LOOPBACK = 0x00000001; /* interface is loopback */ 235 immutable PCAP_IF_UP = 0x00000002; /* interface is up */ 236 immutable PCAP_IF_RUNNING = 0x00000004; /* interface is running */ 237 238 alias void sockaddr; 239 240 /* 241 * Representation of an interface address. 242 */ 243 struct pcap_addr { 244 pcap_addr *next; 245 sockaddr *addr; /* address */ 246 sockaddr *netmask; /* netmask for that address */ 247 sockaddr *broadaddr; /* broadcast address for that address */ 248 sockaddr *dstaddr; /* P2P destination address for that address */ 249 } 250 251 alias void function(ubyte*, const pcap_pkthdr*, const ubyte*) pcap_handler; 252 253 /* 254 * Error codes for the pcap API. 255 * These will all be negative, so you can check for the success or 256 * failure of a call that returns these codes by checking for a 257 * negative value. 258 */ 259 immutable PCAP_ERROR = -1; /* generic error code */ 260 immutable PCAP_ERROR_BREAK = -2; /* loop terminated by pcap_breakloop */ 261 immutable PCAP_ERROR_NOT_ACTIVATED = -3; /* the capture needs to be activated */ 262 immutable PCAP_ERROR_ACTIVATED = -4; /* the operation can't be performed on already activated captures */ 263 immutable PCAP_ERROR_NO_SUCH_DEVICE = -5; /* no such device exists */ 264 immutable PCAP_ERROR_RFMON_NOTSUP = -6; /* this device doesn't support rfmon (monitor) mode */ 265 immutable PCAP_ERROR_NOT_RFMON = -7; /* operation supported only in monitor mode */ 266 immutable PCAP_ERROR_PERM_DENIED = -8; /* no permission to open the device */ 267 immutable PCAP_ERROR_IFACE_NOT_UP = -9; /* interface isn't up */ 268 immutable PCAP_ERROR_CANTSET_TSTAMP_TYPE = -10; /* this device doesn't support setting the time stamp type */ 269 immutable PCAP_ERROR_PROMISC_PERM_DENIED = -11; /* you don't have permission to capture in promiscuous mode */ 270 immutable PCAP_ERROR_TSTAMP_PRECISION_NOTSUP = -12; /* the requested time stamp precision is not supported */ 271 272 /* 273 * Warning codes for the pcap API. 274 * These will all be positive and non-zero, so they won't look like 275 * errors. 276 */ 277 immutable PCAP_WARNING = 1; /* generic warning code */ 278 immutable PCAP_WARNING_PROMISC_NOTSUP = 2; /* this device doesn't support promiscuous mode */ 279 immutable PCAP_WARNING_TSTAMP_TYPE_NOTSUP = 3; /* the requested time stamp type is not supported */ 280 281 /* 282 * Value to pass to pcap_compile() as the netmask if you don't know what 283 * the netmask is. 284 */ 285 immutable PCAP_NETMASK_UNKNOWN = 0xffffffff; 286 287 char *pcap_lookupdev(char *); 288 int pcap_lookupnet(const char *, uint *, uint *, char *); 289 290 pcap_t *pcap_create(const char *, char *); 291 int pcap_set_snaplen(pcap_t *, int); 292 int pcap_set_promisc(pcap_t *, int); 293 int pcap_can_set_rfmon(pcap_t *); 294 int pcap_set_rfmon(pcap_t *, int); 295 int pcap_set_timeout(pcap_t *, int); 296 int pcap_set_tstamp_type(pcap_t *, int); 297 int pcap_set_immediate_mode(pcap_t *, int); 298 int pcap_set_buffer_size(pcap_t *, int); 299 int pcap_set_tstamp_precision(pcap_t *, int); 300 int pcap_get_tstamp_precision(pcap_t *); 301 int pcap_activate(pcap_t *); 302 303 int pcap_list_tstamp_types(pcap_t *, int **); 304 void pcap_free_tstamp_types(int *); 305 int pcap_tstamp_type_name_to_val(const char *); 306 char *pcap_tstamp_type_val_to_name(int); 307 char *pcap_tstamp_type_val_to_description(int); 308 309 /* 310 * Time stamp types. 311 * Not all systems and interfaces will necessarily support all of these. 312 * 313 * A system that supports PCAP_TSTAMP_HOST is offering time stamps 314 * provided by the host machine, rather than by the capture device, 315 * but not committing to any characteristics of the time stamp; 316 * it will not offer any of the PCAP_TSTAMP_HOST_ subtypes. 317 * 318 * PCAP_TSTAMP_HOST_LOWPREC is a time stamp, provided by the host machine, 319 * that's low-precision but relatively cheap to fetch; it's normally done 320 * using the system clock, so it's normally synchronized with times you'd 321 * fetch from system calls. 322 * 323 * PCAP_TSTAMP_HOST_HIPREC is a time stamp, provided by the host machine, 324 * that's high-precision; it might be more expensive to fetch. It might 325 * or might not be synchronized with the system clock, and might have 326 * problems with time stamps for packets received on different CPUs, 327 * depending on the platform. 328 * 329 * PCAP_TSTAMP_ADAPTER is a high-precision time stamp supplied by the 330 * capture device; it's synchronized with the system clock. 331 * 332 * PCAP_TSTAMP_ADAPTER_UNSYNCED is a high-precision time stamp supplied by 333 * the capture device; it's not synchronized with the system clock. 334 * 335 * Note that time stamps synchronized with the system clock can go 336 * backwards, as the system clock can go backwards. If a clock is 337 * not in sync with the system clock, that could be because the 338 * system clock isn't keeping accurate time, because the other 339 * clock isn't keeping accurate time, or both. 340 * 341 * Note that host-provided time stamps generally correspond to the 342 * time when the time-stamping code sees the packet; this could 343 * be some unknown amount of time after the first or last bit of 344 * the packet is received by the network adapter, due to batching 345 * of interrupts for packet arrival, queueing delays, etc.. 346 */ 347 immutable PCAP_TSTAMP_HOST = 0; /* host-provided, unknown characteristics */ 348 immutable PCAP_TSTAMP_HOST_LOWPREC = 1; /* host-provided, low precision */ 349 immutable PCAP_TSTAMP_HOST_HIPREC = 2; /* host-provided, high precision */ 350 immutable PCAP_TSTAMP_ADAPTER = 3; /* device-provided, synced with the system clock */ 351 immutable PCAP_TSTAMP_ADAPTER_UNSYNCED = 4; /* device-provided, not synced with the system clock */ 352 353 /* 354 * Time stamp resolution types. 355 * Not all systems and interfaces will necessarily support all of these 356 * resolutions when doing live captures; all of them can be requested 357 * when reading a savefile. 358 */ 359 immutable PCAP_TSTAMP_PRECISION_MICRO = 0; /* use timestamps with microsecond precision, default */ 360 immutable PCAP_TSTAMP_PRECISION_NANO = 1; /* use timestamps with nanosecond precision */ 361 362 pcap_t *pcap_open_live(const char *, int, int, int, char *); 363 pcap_t *pcap_open_dead(int, int); 364 pcap_t *pcap_open_dead_with_tstamp_precision(int, int, uint); 365 pcap_t *pcap_open_offline_with_tstamp_precision(const char *, uint, char *); 366 pcap_t *pcap_open_offline(const char *, char *); 367 /+ 368 #if defined(WIN32) 369 pcap_t *pcap_hopen_offline_with_tstamp_precision(intptr_t, uint, char *); 370 pcap_t *pcap_hopen_offline(intptr_t, char *); 371 #if !defined(LIBPCAP_EXPORTS) 372 #define pcap_fopen_offline_with_tstamp_precision(f,p,b) \ 373 pcap_hopen_offline_with_tstamp_precision(_get_osfhandle(_fileno(f)), p, b) 374 #define pcap_fopen_offline(f,b) \ 375 pcap_hopen_offline(_get_osfhandle(_fileno(f)), b) 376 #else /*LIBPCAP_EXPORTS*/ 377 static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, uint, char *); 378 static pcap_t *pcap_fopen_offline(FILE *, char *); 379 #endif 380 #else /*WIN32*/ 381 pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, uint, char *); 382 pcap_t *pcap_fopen_offline(FILE *, char *); 383 #endif /*WIN32*/ 384 +/ 385 386 void pcap_close(pcap_t *); 387 int pcap_loop(pcap_t *, int, pcap_handler, ubyte *); 388 int pcap_dispatch(pcap_t *, int, pcap_handler, ubyte *); 389 ubyte* pcap_next(pcap_t *, pcap_pkthdr *); 390 int pcap_next_ex(pcap_t *, pcap_pkthdr **, const ubyte **); 391 void pcap_breakloop(pcap_t *); 392 int pcap_stats(pcap_t *, pcap_stat *); 393 int pcap_setfilter(pcap_t *, bpf_program *); 394 int pcap_setdirection(pcap_t *, pcap_direction_t); 395 int pcap_getnonblock(pcap_t *, char *); 396 int pcap_setnonblock(pcap_t *, int, char *); 397 int pcap_inject(pcap_t *, const void *, size_t); 398 int pcap_sendpacket(pcap_t *, const ubyte *, int); 399 char *pcap_statustostr(int); 400 char *pcap_strerror(int); 401 char *pcap_geterr(pcap_t *); 402 void pcap_perror(pcap_t *, char *); 403 int pcap_compile(pcap_t *, bpf_program *, const char *, int, 404 uint); 405 int pcap_compile_nopcap(int, int, bpf_program *, 406 const char *, int, uint); 407 void pcap_freecode(bpf_program *); 408 int pcap_offline_filter(const bpf_program *, 409 const pcap_pkthdr *, const ubyte *); 410 int pcap_datalink(pcap_t *); 411 int pcap_datalink_ext(pcap_t *); 412 int pcap_list_datalinks(pcap_t *, int **); 413 int pcap_set_datalink(pcap_t *, int); 414 void pcap_free_datalinks(int *); 415 int pcap_datalink_name_to_val(const char *); 416 char *pcap_datalink_val_to_name(int); 417 char *pcap_datalink_val_to_description(int); 418 int pcap_snapshot(pcap_t *); 419 int pcap_is_swapped(pcap_t *); 420 int pcap_major_version(pcap_t *); 421 int pcap_minor_version(pcap_t *); 422 423 /* XXX */ 424 FILE *pcap_file(pcap_t *); 425 int pcap_fileno(pcap_t *); 426 427 pcap_dumper_t *pcap_dump_open(pcap_t *, const char *); 428 pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp); 429 pcap_dumper_t *pcap_dump_open_append(pcap_t *, const char *); 430 FILE *pcap_dump_file(pcap_dumper_t *); 431 long pcap_dump_ftell(pcap_dumper_t *); 432 int pcap_dump_flush(pcap_dumper_t *); 433 void pcap_dump_close(pcap_dumper_t *); 434 void pcap_dump(ubyte *, const pcap_pkthdr *, const ubyte *); 435 436 int pcap_findalldevs(pcap_if_t **, char *); 437 void pcap_freealldevs(pcap_if_t *); 438 439 char *pcap_lib_version(); 440 441 /* 442 * On at least some versions of NetBSD and QNX, we don't want to declare 443 * bpf_filter() here, as it's also be declared in <net/bpf.h>, with a 444 * different signature, but, on other BSD-flavored UN*Xes, it's not 445 * declared in <net/bpf.h>, so we *do* want to declare it here, so it's 446 * declared when we build pcap-bpf.c. 447 */ 448 /+ 449 #if !defined(__NetBSD__) && !defined(__QNX__) 450 uint bpf_filter(const bpf_insn *, const ubyte *, uint, uint); 451 #endif 452 +/ 453 int bpf_validate(const bpf_insn *f, int len); 454 char *bpf_image(const bpf_insn *, int); 455 void bpf_dump(const bpf_program *, int); 456 457 /+ 458 #if defined(WIN32) 459 460 /* 461 * Win32 definitions 462 */ 463 464 int pcap_setbuff(pcap_t *p, int dim); 465 int pcap_setmode(pcap_t *p, int mode); 466 int pcap_setmintocopy(pcap_t *p, int size); 467 Adapter *pcap_get_adapter(pcap_t *p); 468 469 #ifdef WPCAP 470 /* Include file with the wpcap-specific extensions */ 471 #include <Win32-Extensions.h> 472 #endif /* WPCAP */ 473 474 immutable MODE_CAPT = 0; 475 immutable MODE_STAT = 1; 476 immutable MODE_MON = 2; 477 478 #elif defined(MSDOS) 479 480 /* 481 * MS-DOS definitions 482 */ 483 484 int pcap_stats_ex (pcap_t *, pcap_stat_ex *); 485 void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait); 486 ulong pcap_mac_packets (void); 487 488 #else /* UN*X */ 489 490 /* 491 * UN*X definitions 492 */ 493 494 int pcap_get_selectable_fd(pcap_t *); 495 496 #endif /* WIN32/MSDOS/UN*X */ 497 +/