1 /* 2 * Copyright (c) 2002 - 2003 3 * NetGroup, Politecnico di Torino (Italy) 4 * 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 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the Politecnico di Torino nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * 32 * @(#) $Header: /tcpdump/master/tcpdump/tcpdump-stdinc.h,v 1.18 2007-11-24 18:13:33 mcr Exp $ (LBL) 33 */ 34 35 /* 36 * Include the appropriate OS header files on Windows and various flavors 37 * of UNIX, and also define some additional items and include various 38 * non-OS header files on Windows, and; this isolates most of the platform 39 * differences to this one file. 40 */ 41 42 #ifndef tcpdump_stdinc_h 43 #define tcpdump_stdinc_h 44 45 #ifdef WIN32 46 47 #include <stdio.h> 48 #include <winsock2.h> 49 #include <Ws2tcpip.h> 50 #include "bittypes.h" 51 #include <ctype.h> 52 #include <time.h> 53 #include <io.h> 54 #include <fcntl.h> 55 #include <sys/types.h> 56 #include <net/netdb.h> /* in wpcap's Win32/include */ 57 58 #ifndef NBBY 59 #define NBBY 8 60 #endif 61 62 #if !defined(__MINGW32__) && !defined(__WATCOMC__) 63 #define stat _stat 64 #define open _open 65 #define fstat _fstat 66 #define read _read 67 #define close _close 68 #define O_RDONLY _O_RDONLY 69 #endif /* __MINGW32__ */ 70 71 #ifdef __MINGW32__ 72 #include <stdint.h> 73 #endif 74 75 /* Protos for missing/x.c functions (ideally <missing/addrinfo.h> 76 * should be used, but it clashes with <ws2tcpip.h>). 77 */ 78 extern const char *inet_ntop (int, const void *, char *, size_t); 79 extern int inet_pton (int, const char *, void *); 80 extern int inet_aton (const char *cp, struct in_addr *addr); 81 82 /* 83 * With MSVC, for C, __inline is used to make a function an inline. 84 */ 85 #ifdef _MSC_VER 86 #define inline __inline 87 #endif 88 89 #ifndef INET6_ADDRSTRLEN 90 #define INET6_ADDRSTRLEN 46 91 #endif 92 93 #ifndef caddr_t 94 typedef char* caddr_t; 95 #endif /* caddr_t */ 96 97 #define MAXHOSTNAMELEN 64 98 #define NI_MAXHOST 1025 99 #define snprintf _snprintf 100 #define vsnprintf _vsnprintf 101 #define RETSIGTYPE void 102 103 #else /* WIN32 */ 104 105 #include <ctype.h> 106 #include <unistd.h> 107 #include <netdb.h> 108 #if HAVE_INTTYPES_H 109 #include <inttypes.h> 110 #else 111 #if HAVE_STDINT_H 112 #include <stdint.h> 113 #endif 114 #endif 115 #ifdef HAVE_SYS_BITYPES_H 116 #include <sys/bitypes.h> 117 #endif 118 #include <sys/param.h> 119 #include <sys/types.h> /* concession to AIX */ 120 #include <sys/time.h> 121 #include <sys/socket.h> 122 #include <netinet/in.h> 123 124 #ifdef TIME_WITH_SYS_TIME 125 #include <time.h> 126 #endif 127 128 #include <arpa/inet.h> 129 130 #ifndef NBBY 131 #define NBBY 8 132 #endif 133 134 /* Doesn't exist on Android. */ 135 #define setprotoent(...) 136 #define endprotoent(...) 137 138 #endif /* WIN32 */ 139 140 #ifndef HAVE___ATTRIBUTE__ 141 #define __attribute__(x) 142 #endif 143 144 /* 145 * Used to declare a structure unaligned, so that the C compiler, 146 * if necessary, generates code that doesn't assume alignment. 147 * This is required because there is no guarantee that the packet 148 * data we get from libpcap/WinPcap is properly aligned. 149 * 150 * This assumes that, for all compilers that support __attribute__: 151 * 152 * 1) they support __attribute__((packed)); 153 * 154 * 2) for all instruction set architectures requiring strict 155 * alignment, declaring a structure with that attribute 156 * causes the compiler to generate code that handles 157 * misaligned 2-byte, 4-byte, and 8-byte integral 158 * quantities. 159 * 160 * It does not (yet) handle compilers where you can get the compiler 161 * to generate code of that sort by some other means. 162 * 163 * This is required in order to, for example, keep the compiler from 164 * generating, for 165 * 166 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) { 167 * 168 * in print-bootp.c, code that loads the first 4-byte word of a 169 * "struct bootp", masking out the bp_hops field, and comparing the result 170 * against 0x01010600. 171 * 172 * Note: this also requires that padding be put into the structure, 173 * at least for compilers where it's implemented as __attribute__((packed)). 174 */ 175 #if defined(_MSC_VER) && defined(UNALIGNED) 176 /* MSVC may have its own macro defined with the same name and purpose. */ 177 #else 178 #define UNALIGNED __attribute__((packed)) 179 #endif 180 181 #if defined(WIN32) || defined(MSDOS) 182 #define FOPEN_READ_TXT "rt" 183 #define FOPEN_READ_BIN "rb" 184 #define FOPEN_WRITE_TXT "wt" 185 #define FOPEN_WRITE_BIN "wb" 186 #else 187 #define FOPEN_READ_TXT "r" 188 #define FOPEN_READ_BIN FOPEN_READ_TXT 189 #define FOPEN_WRITE_TXT "w" 190 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT 191 #endif 192 193 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl) 194 #undef ntohl 195 #undef ntohs 196 #undef htonl 197 #undef htons 198 199 static __inline__ unsigned long __ntohl (unsigned long x); 200 static __inline__ unsigned short __ntohs (unsigned short x); 201 202 #define ntohl(x) __ntohl(x) 203 #define ntohs(x) __ntohs(x) 204 #define htonl(x) __ntohl(x) 205 #define htons(x) __ntohs(x) 206 __ntohl(unsigned long x)207 static __inline__ unsigned long __ntohl (unsigned long x) 208 { 209 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */ 210 "rorl $16, %0\n\t" /* swap words */ 211 "xchgb %b0, %h0" /* swap higher bytes */ 212 : "=q" (x) : "0" (x)); 213 return (x); 214 } 215 __ntohs(unsigned short x)216 static __inline__ unsigned short __ntohs (unsigned short x) 217 { 218 __asm__ ("xchgb %b0, %h0" /* swap bytes */ 219 : "=q" (x) : "0" (x)); 220 return (x); 221 } 222 #endif 223 224 #ifndef INET_ADDRSTRLEN 225 #define INET_ADDRSTRLEN 16 226 #endif 227 228 #ifndef TRUE 229 #define TRUE 1 230 #endif 231 232 #ifndef FALSE 233 #define FALSE 0 234 #endif 235 236 /* 237 * The Apple deprecation workaround macros below were adopted from the 238 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell. 239 */ 240 241 #define XSTRINGIFY(x) #x 242 243 /* 244 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8 245 */ 246 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y) 247 #define DIAG_DO_PRAGMA(x) _Pragma (#x) 248 249 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402 250 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x) 251 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406 252 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x)) 253 # define DIAG_ON(x) DIAG_PRAGMA(pop) 254 # else 255 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x)) 256 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x)) 257 # endif 258 #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208) 259 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x) 260 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x)) 261 # define DIAG_ON(x) DIAG_PRAGMA(pop) 262 #else 263 # define DIAG_OFF(x) 264 # define DIAG_ON(x) 265 #endif 266 267 /* 268 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API) 269 */ 270 #ifdef __APPLE__ 271 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations) 272 # define USES_APPLE_RST DIAG_ON(deprecated-declarations) 273 #else 274 # define USES_APPLE_DEPRECATED_API 275 # define USES_APPLE_RST 276 #endif 277 278 /* 279 * end of Apple deprecation workaround macros 280 */ 281 282 #endif /* tcpdump_stdinc_h */ 283