Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

pcap.h

Go to the documentation of this file.
00001 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
00002 /*
00003  * Copyright (c) 1993, 1994, 1995, 1996, 1997
00004  *      The Regents of the University of California.  All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the Computer Systems
00017  *      Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * @(#) $Header$ (LBL)
00035  */
00036 
00037 #ifndef lib_pcap_h
00038 #define lib_pcap_h
00039 
00040 #if defined(WIN32)
00041   #include <pcap-stdinc.h>
00042 #elif defined(MSDOS)
00043   #include <sys/types.h>
00044   #include <sys/socket.h>  /* u_int, u_char etc. */
00045 #else /* UN*X */
00046   #include <sys/types.h>
00047   #include <sys/time.h>
00048 #endif /* WIN32/MSDOS/UN*X */
00049 
00050 #ifndef PCAP_DONT_INCLUDE_PCAP_BPF_H
00051 #include <pcap-bpf.h>
00052 #endif
00053 
00054 #include <stdio.h>
00055 
00056 #ifdef HAVE_REMOTE
00057         // We have to define the SOCKET here, although it has been defined in sockutils.h
00058         // This is to avoid the distribution of the 'sockutils.h' file around
00059         // (for example in the WinPcap developer's pack)
00060         #ifndef SOCKET
00061                 #ifdef WIN32
00062                         #define SOCKET unsigned int
00063                 #else
00064                         #define SOCKET int
00065                 #endif
00066         #endif
00067 #endif
00068 
00069 #ifdef __cplusplus
00070 extern "C" {
00071 #endif
00072 
00073 #define PCAP_VERSION_MAJOR 2
00074 #define PCAP_VERSION_MINOR 4
00075 
00076 #define PCAP_ERRBUF_SIZE 256
00077 
00078 /*
00079  * Compatibility for systems that have a bpf.h that
00080  * predates the bpf typedefs for 64-bit support.
00081  */
00082 #if BPF_RELEASE - 0 < 199406
00083 typedef int bpf_int32;
00084 typedef u_int bpf_u_int32;
00085 #endif
00086 
00087 typedef struct pcap pcap_t;
00088 typedef struct pcap_dumper pcap_dumper_t;
00089 typedef struct pcap_if pcap_if_t;
00090 typedef struct pcap_addr pcap_addr_t;
00091 
00092 /*
00093  * The first record in the file contains saved values for some
00094  * of the flags used in the printout phases of tcpdump.
00095  * Many fields here are 32 bit ints so compilers won't insert unwanted
00096  * padding; these files need to be interchangeable across architectures.
00097  *
00098  * Do not change the layout of this structure, in any way (this includes
00099  * changes that only affect the length of fields in this structure).
00100  *
00101  * Also, do not change the interpretation of any of the members of this
00102  * structure, in any way (this includes using values other than
00103  * LINKTYPE_ values, as defined in "savefile.c", in the "linktype"
00104  * field).
00105  *
00106  * Instead:
00107  *
00108  *      introduce a new structure for the new format, if the layout
00109  *      of the structure changed;
00110  *
00111  *      send mail to "tcpdump-workers@tcpdump.org", requesting a new
00112  *      magic number for your new capture file format, and, when
00113  *      you get the new magic number, put it in "savefile.c";
00114  *
00115  *      use that magic number for save files with the changed file
00116  *      header;
00117  *
00118  *      make the code in "savefile.c" capable of reading files with
00119  *      the old file header as well as files with the new file header
00120  *      (using the magic number to determine the header format).
00121  *
00122  * Then supply the changes to "patches@tcpdump.org", so that future
00123  * versions of libpcap and programs that use it (such as tcpdump) will
00124  * be able to read your new capture file format.
00125  */
00126 struct pcap_file_header {
00127         bpf_u_int32 magic;
00128         u_short version_major;
00129         u_short version_minor;
00130         bpf_int32 thiszone;     /* gmt to local correction */
00131         bpf_u_int32 sigfigs;    /* accuracy of timestamps */
00132         bpf_u_int32 snaplen;    /* max length saved portion of each pkt */
00133         bpf_u_int32 linktype;   /* data link type (LINKTYPE_*) */
00134 };
00135 
00136 typedef enum {
00137        PCAP_D_INOUT = 0,
00138        PCAP_D_IN,
00139        PCAP_D_OUT
00140 } pcap_direction_t;
00141 
00142 /*
00143  * Each packet in the dump file is prepended with this generic header.
00144  * This gets around the problem of different headers for different
00145  * packet interfaces.
00146  */
00147 struct pcap_pkthdr {
00148         struct timeval ts;      /* time stamp */
00149         bpf_u_int32 caplen;     /* length of portion present */
00150         bpf_u_int32 len;        /* length this packet (off wire) */
00151 };
00152 
00153 /*
00154  * As returned by the pcap_stats()
00155  */
00156 struct pcap_stat {
00157         u_int ps_recv;          /* number of packets received */
00158         u_int ps_drop;          /* number of packets dropped */
00159         u_int ps_ifdrop;        /* drops by interface XXX not yet supported */
00160 #ifdef HAVE_REMOTE
00161         u_int ps_capt;          /* number of packets that are received by the application; please get rid off the Win32 ifdef */
00162         u_int ps_sent;          /* number of packets sent by the server on the network */
00163         u_int ps_netdrop;       /* number of packets lost on the network */
00164 #endif /* HAVE_REMOTE */
00165 };
00166 
00167 #ifdef MSDOS
00168 /*
00169  * As returned by the pcap_stats_ex()
00170  */
00171 struct pcap_stat_ex {
00172        u_long  rx_packets;        /* total packets received       */
00173        u_long  tx_packets;        /* total packets transmitted    */
00174        u_long  rx_bytes;          /* total bytes received         */
00175        u_long  tx_bytes;          /* total bytes transmitted      */
00176        u_long  rx_errors;         /* bad packets received         */
00177        u_long  tx_errors;         /* packet transmit problems     */
00178        u_long  rx_dropped;        /* no space in Rx buffers       */
00179        u_long  tx_dropped;        /* no space available for Tx    */
00180        u_long  multicast;         /* multicast packets received   */
00181        u_long  collisions;
00182 
00183        /* detailed rx_errors: */
00184        u_long  rx_length_errors;
00185        u_long  rx_over_errors;    /* receiver ring buff overflow  */
00186        u_long  rx_crc_errors;     /* recv'd pkt with crc error    */
00187        u_long  rx_frame_errors;   /* recv'd frame alignment error */
00188        u_long  rx_fifo_errors;    /* recv'r fifo overrun          */
00189        u_long  rx_missed_errors;  /* recv'r missed packet         */
00190 
00191        /* detailed tx_errors */
00192        u_long  tx_aborted_errors;
00193        u_long  tx_carrier_errors;
00194        u_long  tx_fifo_errors;
00195        u_long  tx_heartbeat_errors;
00196        u_long  tx_window_errors;
00197      };
00198 #endif
00199 
00200 /*
00201  * Item in a list of interfaces.
00202  */
00203 struct pcap_if {
00204         struct pcap_if *next;
00205         char *name;             /* name to hand to "pcap_open_live()" */
00206         char *description;      /* textual description of interface, or NULL */
00207         struct pcap_addr *addresses;
00208         bpf_u_int32 flags;      /* PCAP_IF_ interface flags */
00209 };
00210 
00211 #define PCAP_IF_LOOPBACK        0x00000001      /* interface is loopback */
00212 
00213 /*
00214  * Representation of an interface address.
00215  */
00216 struct pcap_addr {
00217         struct pcap_addr *next;
00218         struct sockaddr *addr;          /* address */
00219         struct sockaddr *netmask;       /* netmask for that address */
00220         struct sockaddr *broadaddr;     /* broadcast address for that address */
00221         struct sockaddr *dstaddr;       /* P2P destination address for that address */
00222 };
00223 
00224 typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
00225                              const u_char *);
00226 
00227 char    *pcap_lookupdev(char *);
00228 int     pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *);
00229 pcap_t  *pcap_open_live(const char *, int, int, int, char *);
00230 pcap_t  *pcap_open_dead(int, int);
00231 pcap_t  *pcap_open_offline(const char *, char *);
00232 pcap_t  *pcap_fopen_offline(FILE *, char *);
00233 void    pcap_close(pcap_t *);
00234 int     pcap_loop(pcap_t *, int, pcap_handler, u_char *);
00235 int     pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
00236 const u_char*
00237         pcap_next(pcap_t *, struct pcap_pkthdr *);
00238 int     pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
00239 void    pcap_breakloop(pcap_t *);
00240 int     pcap_stats(pcap_t *, struct pcap_stat *);
00241 int     pcap_setfilter(pcap_t *, struct bpf_program *);
00242 int     pcap_setdirection(pcap_t *, pcap_direction_t);
00243 int     pcap_getnonblock(pcap_t *, char *);
00244 int     pcap_setnonblock(pcap_t *, int, char *);
00245 void    pcap_perror(pcap_t *, char *);
00246 int     pcap_inject(pcap_t *, const void *, size_t);
00247 int     pcap_sendpacket(pcap_t *, const u_char *, int);
00248 char    *pcap_strerror(int);
00249 char    *pcap_geterr(pcap_t *);
00250 int     pcap_compile(pcap_t *, struct bpf_program *, char *, int,
00251             bpf_u_int32);
00252 int     pcap_compile_nopcap(int, int, struct bpf_program *,
00253             char *, int, bpf_u_int32);
00254 void    pcap_freecode(struct bpf_program *);
00255 int     pcap_datalink(pcap_t *);
00256 int     pcap_list_datalinks(pcap_t *, int **);
00257 int     pcap_set_datalink(pcap_t *, int);
00258 int     pcap_datalink_name_to_val(const char *);
00259 const char *pcap_datalink_val_to_name(int);
00260 const char *pcap_datalink_val_to_description(int);
00261 int     pcap_snapshot(pcap_t *);
00262 int     pcap_is_swapped(pcap_t *);
00263 int     pcap_major_version(pcap_t *);
00264 int     pcap_minor_version(pcap_t *);
00265 
00266 /* XXX */
00267 FILE    *pcap_file(pcap_t *);
00268 int     pcap_fileno(pcap_t *);
00269 
00270 pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
00271 pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp);
00272 FILE    *pcap_dump_file(pcap_dumper_t *);
00273 long    pcap_dump_ftell(pcap_dumper_t *);
00274 int     pcap_dump_flush(pcap_dumper_t *);
00275 void    pcap_dump_close(pcap_dumper_t *);
00276 void    pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
00277 
00278 int     pcap_findalldevs(pcap_if_t **, char *);
00279 void    pcap_freealldevs(pcap_if_t *);
00280 
00281 const char *pcap_lib_version(void);
00282 
00283 /* XXX this guy lives in the bpf tree */
00284 u_int   bpf_filter(struct bpf_insn *, u_char *, u_int, u_int);
00285 int     bpf_validate(struct bpf_insn *f, int len);
00286 char    *bpf_image(struct bpf_insn *, int);
00287 void    bpf_dump(struct bpf_program *, int);
00288 
00289 #if defined(WIN32)
00290 
00291 /*
00292  * Win32 definitions
00293  */
00294 
00295 int pcap_setbuff(pcap_t *p, int dim);
00296 int pcap_setmode(pcap_t *p, int mode);
00297 int pcap_setmintocopy(pcap_t *p, int size);
00298 
00299 #ifdef WPCAP
00300 /* Include file with the wpcap-specific extensions */
00301 #include <Win32-Extensions.h>
00302 #endif /* WPCAP */
00303 
00304 #define MODE_CAPT 0
00305 #define MODE_STAT 1
00306 #define MODE_MON 2
00307 
00308 #elif defined(MSDOS)
00309 
00310 /*
00311  * MS-DOS definitions
00312  */
00313 
00314 int  pcap_stats_ex (pcap_t *, struct pcap_stat_ex *);
00315 void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait);
00316 u_long pcap_mac_packets (void);
00317 
00318 #else /* UN*X */
00319 
00320 /*
00321  * UN*X definitions
00322  */
00323 
00324 int     pcap_get_selectable_fd(pcap_t *);
00325 
00326 #endif /* WIN32/MSDOS/UN*X */
00327 
00328 #ifdef HAVE_REMOTE
00329 /* Includes most of the public stuff that is needed for the remote capture */
00330 #include "remote-ext.h"
00331 #endif   /* HAVE_REMOTE */
00332 
00333 #ifdef __cplusplus
00334 }
00335 #endif
00336 
00337 #endif

Generated on Sun May 14 14:51:20 2006 by  doxygen 1.4.2