module pcap; /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */ /* * Copyright (c) 1993, 1994, 1995, 1996, 1997 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the Computer Systems * Engineering Group at Lawrence Berkeley Laboratory. * 4. Neither the name of the University nor of the Laboratory may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#) $Header: /tcpdump/master/libpcap/pcap/pcap.h,v 1.4.2.11 2008-10-06 15:38:39 gianluca Exp $ (LBL) */ import bpf; import std.c.time; version(Windows){ //import pcapStdinc; import std.c.windows.winsock; } version(OSX){ import std.c.osx.socket; } version(linux){ import std.c.linux.socket; } version(FreeBSD){ import std.c.freebsd.socket; } //import pcap.bpf; import std.stdio; extern (C) { const PCAP_VERSION_MAJOR=2; const PCAP_VERSION_MINOR=4; const PCAP_ERRBUF_SIZE=256; //incomplete c types, void * does the job? alias void * pcap_t; alias void * pcap_dumper_t; alias pcap_if pcap_if_t; alias pcap_addr pcap_addr_t; /* * The first record in the file contains saved values for some * of the flags used in the printout phases of tcpdump. * Many fields here are 32 bit ints so compilers won't insert unwanted * padding; these files need to be interchangeable across architectures. * * Do not change the layout of this structure, in any way (this includes * changes that only affect the length of fields in this structure). * * Also, do not change the interpretation of any of the members of this * structure, in any way (this includes using values other than * LINKTYPE_ values, as defined in "savefile.c", in the "linktype" * field). * * Instead: * * introduce a new structure for the new format, if the layout * of the structure changed; * * send mail to "tcpdump-workers@lists.tcpdump.org", requesting * a new magic number for your new capture file format, and, when * you get the new magic number, put it in "savefile.c"; * * use that magic number for save files with the changed file * header; * * make the code in "savefile.c" capable of reading files with * the old file header as well as files with the new file header * (using the magic number to determine the header format). * * Then supply the changes as a patch at * * http://sourceforge.net/projects/libpcap/ * * so that future versions of libpcap and programs that use it (such as * tcpdump) will be able to read your new capture file format. */ struct pcap_file_header { uint magic; ushort version_major; ushort version_minor; int thiszone; /* gmt to local correction */ uint sigfigs; /* accuracy of timestamps */ uint snaplen; /* max length saved portion of each pkt */ uint linktype; /* data link type (LINKTYPE_*) */ } /* * Macros for the value returned by pcap_datalink_ext(). * * If LT_FCS_LENGTH_PRESENT(x) is true, the LT_FCS_LENGTH(x) macro * gives the FCS length of packets in the capture. */ //not even going to try... /*#define LT_FCS_LENGTH_PRESENT(x) ((x) & 0x04000000) #define LT_FCS_LENGTH(x) (((x) & 0xF0000000) >> 28) #define LT_FCS_DATALINK_EXT(x) ((((x) & 0xF) << 28) | 0x04000000)*/ enum pcap_direction_t{ PCAP_D_INOUT = 0, PCAP_D_IN, PCAP_D_OUT } /* * Generic per-packet information, as supplied by libpcap. * * The time stamp can and should be a "struct timeval", regardless of * whether your system supports 32-bit tv_sec in "struct timeval", * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit * and 64-bit applications. The on-disk format of savefiles uses 32-bit * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit * and 64-bit versions of libpcap, even if they're on the same platform, * should supply the appropriate version of "struct timeval", even if * that's not what the underlying packet capture mechanism supplies. */ struct pcap_pkthdr { timeval ts; /* time stamp */ uint caplen; /* length of portion present */ uint len; /* length this packet (off wire) */ } /* * As returned by the pcap_stats() */ struct pcap_stat { uint ps_recv; /* number of packets received */ uint ps_drop; /* number of packets dropped */ uint ps_ifdrop; /* drops by interface XXX not yet supported */ //NOT SUPPORTED version(HAVE_REMOTE){ uint ps_capt; /* number of packets that are received by the application; please get rid off the Win32 ifdef */ uint ps_sent; /* number of packets sent by the server on the network */ uint ps_netdrop; /* number of packets lost on the network */ } } /* * Item in a list of interfaces. */ struct pcap_if { pcap_if *next; char *name; /* name to hand to "pcap_open_live()" */ char *description; /* textual description of interface, or NULL */ pcap_addr *addresses; uint flags; /* PCAP_IF_ interface flags */ } const PCAP_IF_LOOPBACK= 0x00000001; /* interface is loopback */ /* * Representation of an interface address. */ struct pcap_addr { pcap_addr *next; sockaddr *addr; /* address */ sockaddr *netmask; /* netmask for that address */ sockaddr *broadaddr; /* broadcast address for that address */ sockaddr *dstaddr; /* P2P destination address for that address */ } /*alias void (*pcap_handler)(ubyte *, const struct pcap_pkthdr *, const ubyte *);*/ /* * Error codes for the pcap API. * These will all be negative, so you can check for the success or * failure of a call that returns these codes by checking for a * negative value. */ const PCAP_ERROR = -1 ;/* generic error code */ const PCAP_ERROR_BREAK = -2 ;/* loop terminated by pcap_breakloop */ const PCAP_ERROR_NOT_ACTIVATED =-3 ;/* the capture needs to be activated */ const PCAP_ERROR_ACTIVATED =-4 ;/* the operation can't be performed on already activated captures */ const PCAP_ERROR_NO_SUCH_DEVICE =-5 ;/* no such device exists */ const PCAP_ERROR_RFMON_NOTSUP = -6; /* this device doesn't support rfmon (monitor) mode */ const PCAP_ERROR_NOT_RFMON =-7 ;/* operation supported only in monitor mode */ const PCAP_ERROR_PERM_DENIED = -8; /* no permission to open the device */ const PCAP_ERROR_IFACE_NOT_UP = -9; /* interface isn't up */ /* * Warning codes for the pcap API. * These will all be positive and non-zero, so they won't look like * errors. */ const PCAP_WARNING = 1; /* generic warning code */ const PCAP_WARNING_PROMISC_NOTSUP =2; /* this device doesn't support promiscuous mode */ char *pcap_lookupdev(char *); int pcap_lookupnet(const char *, uint *, uint *, char *); pcap_t *pcap_create(const char *, char *); int pcap_set_snaplen(pcap_t *, int); int pcap_set_promisc(pcap_t *, int); int pcap_can_set_rfmon(pcap_t *); int pcap_set_rfmon(pcap_t *, int); int pcap_set_timeout(pcap_t *, int); int pcap_set_buffer_size(pcap_t *, int); int pcap_activate(pcap_t *); pcap_t *pcap_open_live(const char *, int, int, int, char *); pcap_t *pcap_open_dead(int, int); pcap_t *pcap_open_offline(const char *, char *); version(Windows){ pcap_t *pcap_hopen_offline(void *_t, char *); static pcap_t *pcap_fopen_offline(FILE *, char *); } else{ pcap_t *pcap_fopen_offline(FILE *, char *); } void pcap_close(pcap_t *); //we don't have a definitio for pcap_handler so alias void * pcap_handler; int pcap_loop(pcap_t *, int, pcap_handler, ubyte *); int pcap_dispatch(pcap_t *, int, pcap_handler, ubyte *); ubyte* pcap_next(pcap_t *, pcap_pkthdr *); int pcap_next_ex(pcap_t *, pcap_pkthdr **, const ubyte **); void pcap_breakloop(pcap_t *); int pcap_stats(pcap_t *, pcap_stat *); int pcap_setfilter(pcap_t *, bpf_program *); int pcap_setdirection(pcap_t *, pcap_direction_t); int pcap_getnonblock(pcap_t *, char *); int pcap_setnonblock(pcap_t *, int, char *); int pcap_inject(pcap_t *, const void *, size_t); int pcap_sendpacket(pcap_t *, const ubyte *, int); char *pcap_statustostr(int); char *pcap_strerror(int); char *pcap_geterr(pcap_t *); void pcap_perror(pcap_t *, char *); int pcap_compile(pcap_t *, bpf_program *, const char *, int, uint); int pcap_compile_nopcap(int, int, bpf_program *, const char *, int, uint); void pcap_freecode( bpf_program *); int pcap_offline_filter( bpf_program *, const pcap_pkthdr *, const ubyte *); int pcap_datalink(pcap_t *); int pcap_datalink_ext(pcap_t *); int pcap_list_datalinks(pcap_t *, int **); int pcap_set_datalink(pcap_t *, int); void pcap_free_datalinks(int *); int pcap_datalink_name_to_val(const char *); char *pcap_datalink_val_to_name(int); char *pcap_datalink_val_to_description(int); int pcap_snapshot(pcap_t *); int pcap_is_swapped(pcap_t *); int pcap_major_version(pcap_t *); int pcap_minor_version(pcap_t *); /* XXX */ FILE *pcap_file(pcap_t *); int pcap_fileno(pcap_t *); pcap_dumper_t *pcap_dump_open(pcap_t *, const char *); pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp); FILE *pcap_dump_file(pcap_dumper_t *); long pcap_dump_ftell(pcap_dumper_t *); int pcap_dump_flush(pcap_dumper_t *); void pcap_dump_close(pcap_dumper_t *); void pcap_dump(ubyte *, const pcap_pkthdr *, const ubyte *); int pcap_findalldevs(pcap_if_t **, char *); void pcap_freealldevs(pcap_if_t *); char *pcap_lib_version(); /* XXX this guy lives in the bpf tree */ uint bpf_filter(const bpf_insn *, const ubyte *, uint, uint); int bpf_validate(const bpf_insn *f, int len); char *bpf_image(const bpf_insn *, int); void bpf_dump(const bpf_program *, int); version(Windows){ /* * Win32 definitions */ int pcap_setbuff(pcap_t *p, int dim); int pcap_setmode(pcap_t *p, int mode); int pcap_setmintocopy(pcap_t *p, int size); } const MODE_CAPT =0; const MODE_STAT =1; const MODE_MON =2; version(Posix){ /* * UN*X definitions */ int pcap_get_selectable_fd(pcap_t *); } //NOT SUPPORTED version(HAVE_REMOTE){ import remoteExt; } }