]>
The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
33 * Include the appropriate OS header files on Windows and various flavors
34 * of UNIX, include various non-OS header files on Windows, and define
35 * various items as needed, to isolate most of netdissect's platform
36 * differences to this one file.
39 #ifndef netdissect_stdinc_h
40 #define netdissect_stdinc_h
47 * Includes and definitions for Windows.
54 #include "bittypes.h" /* in wpcap's Win32/include */
59 #include <sys/types.h>
60 #include <net/netdb.h> /* in wpcap's Win32/include */
63 #define uint8_t unsigned char
67 #define int8_t signed char
71 #define uint16_t unsigned short
75 #define int16_t signed short
79 #define uint32_t unsigned int
83 #define int32_t signed int
86 #ifdef _MSC_EXTENSIONS
89 #define uint64_t unsigned _int64
93 #define int64_t _int64
101 #define PRIo64 "I64o"
105 #define PRIu64 "I64u"
109 #define PRIx64 "I64x"
112 #else /* _MSC_EXTENSIONS */
115 #define uint64_t unsigned long long
119 #define int64_t long long
138 #endif /* _MSC_EXTENSIONS */
146 #define O_RDONLY _O_RDONLY
147 #endif /* _MSC_VER */
150 * With MSVC, for C, __inline is used to make a function an inline.
153 #define inline __inline
156 #ifndef INET6_ADDRSTRLEN
157 #define INET6_ADDRSTRLEN 46
160 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
163 #define EAFNOSUPPORT WSAEAFNOSUPPORT
167 typedef char* caddr_t
;
170 #define MAXHOSTNAMELEN 64
171 #define snprintf _snprintf
172 #define vsnprintf _vsnprintf
173 #define RETSIGTYPE void
178 * Includes and definitions for various flavors of UN*X.
185 #include <inttypes.h>
189 #include <sys/param.h>
190 #include <sys/types.h> /* concession to AIX */
191 #include <sys/time.h>
192 #include <sys/socket.h>
193 #include <netinet/in.h>
195 #ifdef TIME_WITH_SYS_TIME
199 #include <arpa/inet.h>
203 #ifndef HAVE___ATTRIBUTE__
204 #define __attribute__(x)
208 * Used to declare a structure unaligned, so that the C compiler,
209 * if necessary, generates code that doesn't assume alignment.
210 * This is required because there is no guarantee that the packet
211 * data we get from libpcap/WinPcap is properly aligned.
213 * This assumes that, for all compilers that support __attribute__:
215 * 1) they support __attribute__((packed));
217 * 2) for all instruction set architectures requiring strict
218 * alignment, declaring a structure with that attribute
219 * causes the compiler to generate code that handles
220 * misaligned 2-byte, 4-byte, and 8-byte integral
223 * It does not (yet) handle compilers where you can get the compiler
224 * to generate code of that sort by some other means.
226 * This is required in order to, for example, keep the compiler from
229 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
231 * in print-bootp.c, code that loads the first 4-byte word of a
232 * "struct bootp", masking out the bp_hops field, and comparing the result
233 * against 0x01010600.
235 * Note: this also requires that padding be put into the structure,
236 * at least for compilers where it's implemented as __attribute__((packed)).
238 #if !(defined(_MSC_VER) && defined(UNALIGNED))
239 /* MSVC may have its own macro defined with the same name and purpose. */
241 #define UNALIGNED __attribute__((packed))
245 * fopen() read and write modes for text files and binary files.
247 #if defined(_WIN32) || defined(MSDOS)
248 #define FOPEN_READ_TXT "rt"
249 #define FOPEN_READ_BIN "rb"
250 #define FOPEN_WRITE_TXT "wt"
251 #define FOPEN_WRITE_BIN "wb"
253 #define FOPEN_READ_TXT "r"
254 #define FOPEN_READ_BIN FOPEN_READ_TXT
255 #define FOPEN_WRITE_TXT "w"
256 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
260 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
261 * defined if the OS doesn't provide them. These assume no more than
262 * an 80386, so, for example, it avoids the bswap instruction added in
265 * (We don't use them on OS X; Apple provides their own, which *doesn't*
266 * avoid the bswap instruction, as OS X only supports machines that
269 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
275 static __inline__
unsigned long __ntohl (unsigned long x
);
276 static __inline__
unsigned short __ntohs (unsigned short x
);
278 #define ntohl(x) __ntohl(x)
279 #define ntohs(x) __ntohs(x)
280 #define htonl(x) __ntohl(x)
281 #define htons(x) __ntohs(x)
283 static __inline__
unsigned long __ntohl (unsigned long x
)
285 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
286 "rorl $16, %0\n\t" /* swap words */
287 "xchgb %b0, %h0" /* swap higher bytes */
288 : "=q" (x
) : "0" (x
));
292 static __inline__
unsigned short __ntohs (unsigned short x
)
294 __asm__ ("xchgb %b0, %h0" /* swap bytes */
295 : "=q" (x
) : "0" (x
));
301 * If the OS doesn't define AF_INET6 and struct in6_addr:
303 * define AF_INET6, so we can use it internally as a "this is an
304 * IPv6 address" indication;
306 * define struct in6_addr so that we can use it for IPv6 addresses.
308 #ifndef HAVE_OS_IPV6_SUPPORT
313 __uint8_t __u6_addr8
[16];
314 __uint16_t __u6_addr16
[8];
315 __uint32_t __u6_addr32
[4];
316 } __u6_addr
; /* 128-bit IP6 address */
321 #define NI_MAXHOST 1025
324 #ifndef INET_ADDRSTRLEN
325 #define INET_ADDRSTRLEN 16
337 * The Apple deprecation workaround macros below were adopted from the
338 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
341 #define XSTRINGIFY(x) #x
344 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
346 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
347 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
349 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
350 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
351 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
352 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
353 # define DIAG_ON(x) DIAG_PRAGMA(pop)
355 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
356 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
358 #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
359 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
360 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
361 # define DIAG_ON(x) DIAG_PRAGMA(pop)
368 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
371 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
372 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
374 # define USES_APPLE_DEPRECATED_API
375 # define USES_APPLE_RST
379 * end of Apple deprecation workaround macros
383 #define min(a,b) ((a)>(b)?(b):(a))
386 #define max(a,b) ((b)>(a)?(b):(a))
389 #endif /* netdissect_stdinc_h */