00001 /* 00002 * Copyright (c) 1994, 1995, 1996 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. All advertising materials mentioning features or use of this software 00014 * must display the following acknowledgement: 00015 * This product includes software developed by the Computer Systems 00016 * Engineering Group at Lawrence Berkeley Laboratory. 00017 * 4. Neither the name of the University nor of the Laboratory may be used 00018 * to endorse or promote products derived from this software without 00019 * specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 * 00033 * @(#) $Header$ (LBL) 00034 */ 00035 00036 #ifndef pcap_int_h 00037 #define pcap_int_h 00038 00039 #ifdef __cplusplus 00040 extern "C" { 00041 #endif 00042 00043 #include <pcap.h> 00044 00045 #ifdef WIN32 00046 #include <packet32.h> 00047 #endif /* WIN32 */ 00048 00049 #ifdef MSDOS 00050 #include <fcntl.h> 00051 #include <io.h> 00052 #endif 00053 00054 /* 00055 * Savefile 00056 */ 00057 typedef enum { 00058 NOT_SWAPPED, 00059 SWAPPED, 00060 MAYBE_SWAPPED 00061 } swapped_type_t; 00062 00063 struct pcap_sf { 00064 FILE *rfile; 00065 int swapped; 00066 int hdrsize; 00067 swapped_type_t lengths_swapped; 00068 int version_major; 00069 int version_minor; 00070 u_char *base; 00071 }; 00072 00073 struct pcap_md { 00074 struct pcap_stat stat; 00075 /*XXX*/ 00076 int use_bpf; /* using kernel filter */ 00077 u_long TotPkts; /* can't oflow for 79 hrs on ether */ 00078 u_long TotAccepted; /* count accepted by filter */ 00079 u_long TotDrops; /* count of dropped packets */ 00080 long TotMissed; /* missed by i/f during this run */ 00081 long OrigMissed; /* missed by i/f before this run */ 00082 char *device; /* device name */ 00083 #ifdef linux 00084 int sock_packet; /* using Linux 2.0 compatible interface */ 00085 int timeout; /* timeout specified to pcap_open_live */ 00086 int clear_promisc; /* must clear promiscuous mode when we close */ 00087 int cooked; /* using SOCK_DGRAM rather than SOCK_RAW */ 00088 int ifindex; /* interface index of device we're bound to */ 00089 int lo_ifindex; /* interface index of the loopback device */ 00090 struct pcap *next; /* list of open promiscuous sock_packet pcaps */ 00091 #endif 00092 00093 #ifdef HAVE_DAG_API 00094 #ifdef HAVE_DAG_STREAMS_API 00095 u_char *dag_mem_bottom; /* DAG card current memory bottom pointer */ 00096 u_char *dag_mem_top; /* DAG card current memory top pointer */ 00097 #else 00098 void *dag_mem_base; /* DAG card memory base address */ 00099 u_int dag_mem_bottom; /* DAG card current memory bottom offset */ 00100 u_int dag_mem_top; /* DAG card current memory top offset */ 00101 #endif /* HAVE_DAG_STREAMS_API */ 00102 int dag_fcs_bits; /* Number of checksum bits from link layer */ 00103 int dag_offset_flags; /* Flags to pass to dag_offset(). */ 00104 int dag_stream; /* DAG stream number */ 00105 int dag_timeout; /* timeout specified to pcap_open_live. 00106 * Same as in linux above, introduce 00107 * generally? */ 00108 #endif /* HAVE_DAG_API */ 00109 00110 #ifdef HAVE_REMOTE 00111 /*! 00112 There is really a mess with previous variables, and it seems to me that they are not used 00113 (they are used in pcap_pf.c only). I think we have to start using them. 00114 The meaning is the following: 00115 00116 - TotPkts: the amount of packets received by the bpf filter, *before* applying the filter 00117 - TotAccepted: the amount of packets that satisfies the filter 00118 - TotDrops: the amount of packet that were dropped into the kernel buffer because of lack of space 00119 - TotMissed: the amount of packets that were dropped by the physical interface; it is basically 00120 the value of the hardware counter into the card. This number is never put to zero, so this number 00121 takes into account the *total* number of interface drops starting from the interface power-on. 00122 - OrigMissed: the amount of packets that were dropped by the interface *when the capture begins*. 00123 This value is used to detect the number of packets dropped by the interface *during the present 00124 capture*, so that (ps_ifdrops= TotMissed - OrigMissed). 00125 */ 00126 unsigned int TotNetDrops; //!< keeps the number of packets that have been dropped by the network 00127 /*! 00128 \brief It keeps the number of packets that have been received by the application. 00129 00130 Packets dropped by the kernel buffer are not counted in this variable. The variable is always 00131 equal to (TotAccepted - TotDrops), exept for the case of remote capture, in which we have also 00132 packets in fligh, i.e. that have been transmitted by the remote host, but that have not been 00133 received (yet) from the client. In this case, (TotAccepted - TotDrops - TotNetDrops) gives a 00134 wrong result, since this number does not corresponds always to the number of packet received by 00135 the application. For this reason, in the remote capture we need another variable that takes 00136 into account of the number of packets actually received by the application. 00137 */ 00138 unsigned int TotCapt; 00139 #endif /* HAVE_REMOTE */ 00140 }; 00141 00142 /* 00143 * Ultrix, DEC OSF/1^H^H^H^H^H^H^H^H^HDigital UNIX^H^H^H^H^H^H^H^H^H^H^H^H 00144 * Tru64 UNIX, and NetBSD pad to make everything line up on a nice boundary. 00145 */ 00146 #if defined(ultrix) || defined(__osf__) || (defined(__NetBSD__) && __NetBSD_Version__ > 106000000) 00147 #define PCAP_FDDIPAD 3 00148 #endif 00149 00150 struct pcap { 00151 #ifdef WIN32 00152 ADAPTER *adapter; 00153 LPPACKET Packet; 00154 int timeout; 00155 int nonblock; 00156 #else 00157 int fd; 00158 int selectable_fd; 00159 int send_fd; 00160 #endif /* WIN32 */ 00161 int snapshot; 00162 int linktype; 00163 int tzoff; /* timezone offset */ 00164 int offset; /* offset for proper alignment */ 00165 00166 int break_loop; /* flag set to force break from packet-reading loop */ 00167 00168 #ifdef PCAP_FDDIPAD 00169 int fddipad; 00170 #endif 00171 00172 #ifdef MSDOS 00173 int inter_packet_wait; /* offline: wait between packets */ 00174 void (*wait_proc)(void); /* call proc while waiting */ 00175 #endif 00176 00177 struct pcap_sf sf; 00178 struct pcap_md md; 00179 00180 /* 00181 * Read buffer. 00182 */ 00183 int bufsize; 00184 u_char *buffer; 00185 u_char *bp; 00186 int cc; 00187 00188 /* 00189 * Place holder for pcap_next(). 00190 */ 00191 u_char *pkt; 00192 00193 /* We're accepting only packets in this direction/these directions. */ 00194 pcap_direction_t direction; 00195 00196 /* 00197 * Methods. 00198 */ 00199 int (*read_op)(pcap_t *, int cnt, pcap_handler, u_char *); 00200 int (*inject_op)(pcap_t *, const void *, size_t); 00201 int (*setfilter_op)(pcap_t *, struct bpf_program *); 00202 int (*setdirection_op)(pcap_t *, pcap_direction_t); 00203 int (*set_datalink_op)(pcap_t *, int); 00204 int (*getnonblock_op)(pcap_t *, char *); 00205 int (*setnonblock_op)(pcap_t *, int, char *); 00206 int (*stats_op)(pcap_t *, struct pcap_stat *); 00207 void (*close_op)(pcap_t *); 00208 00209 /* 00210 * Placeholder for filter code if bpf not in kernel. 00211 */ 00212 struct bpf_program fcode; 00213 00214 char errbuf[PCAP_ERRBUF_SIZE + 1]; 00215 int dlt_count; 00216 u_int *dlt_list; 00217 00218 struct pcap_pkthdr pcap_header; /* This is needed for the pcap_next_ex() to work */ 00219 00220 #ifdef HAVE_REMOTE 00221 #ifndef WIN32 // Win32 already defines 'timeout' 00222 int timeout; //!< timeout to be used in the pcap_open() 00223 #endif 00224 /*! \brief '1' if we're the network client; needed by several functions (like pcap_setfilter() ) to know if 00225 they have to use the socket or they have to open the local adapter. */ 00226 int rmt_clientside; 00227 00228 SOCKET rmt_sockctrl; //!< socket ID of the socket used for the control connection 00229 SOCKET rmt_sockdata; //!< socket ID of the socket used for the data connection 00230 int rmt_flags; //!< we have to save flags, since they are passed by the pcap_open_live(), but they are used by the pcap_startcapture() 00231 int rmt_capstarted; //!< 'true' if the capture is already started (needed to knoe if we have to call the pcap_startcapture() 00232 struct pcap_samp rmt_samp; //!< Keeps the parameters related to the sampling process. 00233 char *currentfilter; //!< Pointer to a buffer (allocated at run-time) that stores the current filter. Needed when flag PCAP_OPENFLAG_NOCAPTURE_RPCAP is turned on. 00234 #endif /* HAVE_REMOTE */ 00235 }; 00236 00237 /* 00238 * This is a timeval as stored in disk in a dumpfile. 00239 * It has to use the same types everywhere, independent of the actual 00240 * `struct timeval' 00241 */ 00242 00243 struct pcap_timeval { 00244 bpf_int32 tv_sec; /* seconds */ 00245 bpf_int32 tv_usec; /* microseconds */ 00246 }; 00247 00248 /* 00249 * How a `pcap_pkthdr' is actually stored in the dumpfile. 00250 * 00251 * Do not change the format of this structure, in any way (this includes 00252 * changes that only affect the length of fields in this structure), 00253 * and do not make the time stamp anything other than seconds and 00254 * microseconds (e.g., seconds and nanoseconds). Instead: 00255 * 00256 * introduce a new structure for the new format; 00257 * 00258 * send mail to "tcpdump-workers@tcpdump.org", requesting a new 00259 * magic number for your new capture file format, and, when 00260 * you get the new magic number, put it in "savefile.c"; 00261 * 00262 * use that magic number for save files with the changed record 00263 * header; 00264 * 00265 * make the code in "savefile.c" capable of reading files with 00266 * the old record header as well as files with the new record header 00267 * (using the magic number to determine the header format). 00268 * 00269 * Then supply the changes to "patches@tcpdump.org", so that future 00270 * versions of libpcap and programs that use it (such as tcpdump) will 00271 * be able to read your new capture file format. 00272 */ 00273 00274 struct pcap_sf_pkthdr { 00275 struct pcap_timeval ts; /* time stamp */ 00276 bpf_u_int32 caplen; /* length of portion present */ 00277 bpf_u_int32 len; /* length this packet (off wire) */ 00278 }; 00279 00280 /* 00281 * How a `pcap_pkthdr' is actually stored in dumpfiles written 00282 * by some patched versions of libpcap (e.g. the ones in Red 00283 * Hat Linux 6.1 and 6.2). 00284 * 00285 * Do not change the format of this structure, in any way (this includes 00286 * changes that only affect the length of fields in this structure). 00287 * Instead, introduce a new structure, as per the above. 00288 */ 00289 00290 struct pcap_sf_patched_pkthdr { 00291 struct pcap_timeval ts; /* time stamp */ 00292 bpf_u_int32 caplen; /* length of portion present */ 00293 bpf_u_int32 len; /* length this packet (off wire) */ 00294 int index; 00295 unsigned short protocol; 00296 unsigned char pkt_type; 00297 }; 00298 00299 int yylex(void); 00300 00301 #ifndef min 00302 #define min(a, b) ((a) > (b) ? (b) : (a)) 00303 #endif 00304 00305 /* XXX should these be in pcap.h? */ 00306 int pcap_offline_read(pcap_t *, int, pcap_handler, u_char *); 00307 int pcap_read(pcap_t *, int cnt, pcap_handler, u_char *); 00308 00309 #ifndef HAVE_STRLCPY 00310 #define strlcpy(x, y, z) \ 00311 (strncpy((x), (y), (z)), \ 00312 ((z) <= 0 ? 0 : ((x)[(z) - 1] = '\0')), \ 00313 strlen((y))) 00314 #endif 00315 00316 #include <stdarg.h> 00317 00318 #if !defined(HAVE_SNPRINTF) 00319 #define snprintf pcap_snprintf 00320 extern int snprintf (char *, size_t, const char *, ...); 00321 #endif 00322 00323 #if !defined(HAVE_VSNPRINTF) 00324 #define vsnprintf pcap_vsnprintf 00325 extern int vsnprintf (char *, size_t, const char *, va_list ap); 00326 #endif 00327 00328 /* 00329 * Routines that most pcap implementations can use for non-blocking mode. 00330 */ 00331 #if !defined(WIN32) && !defined(MSDOS) 00332 int pcap_getnonblock_fd(pcap_t *, char *); 00333 int pcap_setnonblock_fd(pcap_t *p, int, char *); 00334 #endif 00335 00336 void pcap_close_common(pcap_t *); 00337 00338 /* 00339 * Internal interfaces for "pcap_findalldevs()". 00340 * 00341 * "pcap_platform_finddevs()" is a platform-dependent routine to 00342 * add devices not found by the "standard" mechanisms (SIOCGIFCONF, 00343 * "getifaddrs()", etc.. 00344 * 00345 * "pcap_add_if()" adds an interface to the list of interfaces. 00346 */ 00347 int pcap_platform_finddevs(pcap_if_t **, char *); 00348 int add_addr_to_iflist(pcap_if_t **, const char *, u_int, struct sockaddr *, 00349 size_t, struct sockaddr *, size_t, struct sockaddr *, size_t, 00350 struct sockaddr *, size_t, char *); 00351 int pcap_add_if(pcap_if_t **, const char *, u_int, const char *, char *); 00352 struct sockaddr *dup_sockaddr(struct sockaddr *, size_t); 00353 int add_or_find_if(pcap_if_t **, pcap_if_t **, const char *, u_int, 00354 const char *, char *); 00355 00356 #ifdef WIN32 00357 char *pcap_win32strerror(void); 00358 #endif 00359 00360 int install_bpf_program(pcap_t *, struct bpf_program *); 00361 00362 int pcap_strcasecmp(const char *, const char *); 00363 00364 #ifdef __cplusplus 00365 } 00366 #endif 00367 00368 #endif