]>
The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
b214be5fba1a4f51e29aacab36d32330d7d5ccc7
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
45 * Get the C99 types, and the PRI[doux]64 format strings, defined.
47 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
49 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
50 * work, and won't cause problems if a file includes this file
51 * and later includes a pcap header file that also includes
52 * pcap/pcap-inttypes.h.
54 #include <pcap/pcap-inttypes.h>
57 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
58 * do the work ourselves, but at least we don't have to
59 * worry about other headers including it and causing
68 * VS 2013 or newer; we have <inttypes.h>.
73 * Earlier VS; we have to define this stuff ourselves.
75 typedef unsigned char uint8_t;
76 typedef signed char int8_t;
77 typedef unsigned short uint16_t;
78 typedef signed short int16_t;
79 typedef unsigned int uint32_t;
80 typedef signed int int32_t;
81 #ifdef _MSC_EXTENSIONS
82 typedef unsigned _int64
uint64_t;
83 typedef _int64
int64_t;
84 #else /* _MSC_EXTENSIONS */
85 typedef unsigned long long uint64_t;
86 typedef long long int64_t;
90 * We have _strtoi64(). Use that for strtoint64_t().
92 #define strtoint64_t _strtoi64
96 * Suppress definition of intN_t in bittypes.h, which might be included
97 * by <pcap/pcap.h> in older versions of WinPcap.
98 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
99 * we check for u_intN_t in the UN*X configure script.)
101 #define HAVE_U_INT8_T
102 #define HAVE_U_INT16_T
103 #define HAVE_U_INT32_T
104 #define HAVE_U_INT64_T
107 * These may be defined by <inttypes.h>. If not, define them
110 * XXX - for MSVC, we always want the _MSC_EXTENSIONS versions.
111 * What about other compilers? If, as the MinGW Web site says MinGW
112 * does, the other compilers just use Microsoft's run-time library,
113 * then they should probably use the _MSC_EXTENSIONS even if the
114 * compiler doesn't define _MSC_EXTENSIONS.
117 #ifdef _MSC_EXTENSIONS
118 #define PRId64 "I64d"
125 #ifdef _MSC_EXTENSIONS
126 #define PRIo64 "I64o"
133 #ifdef _MSC_EXTENSIONS
134 #define PRIx64 "I64x"
141 #ifdef _MSC_EXTENSIONS
142 #define PRIu64 "I64u"
147 #elif defined(__MINGW32__) || !defined(_WIN32)
149 * Compiler is MinGW or target is UN*X or MS-DOS. Just use
152 #include <inttypes.h>
154 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
159 * Includes and definitions for Windows.
163 #include <winsock2.h>
164 #include <ws2tcpip.h>
169 #include <sys/types.h>
177 * VS 2013 or newer; we have strtoll(). Use that for strtoint64_t().
179 #define strtoint64_t strtoll
182 * Earlier VS; we don't have strtoll(), but we do have
183 * _strtoi64(). Use that for strtoint64_t().
185 #define strtoint64_t _strtoi64
189 * Microsoft's documentation doesn't speak of LL as a valid
190 * suffix for 64-bit integers, so we'll just use i64.
192 #define INT64_T_CONSTANT(constant) (constant##i64)
195 * Non-Microsoft compiler.
197 * XXX - should we use strtoll or should we use _strtoi64()?
199 #define strtoint64_t strtoll
204 #define INT64_T_CONSTANT(constant) (constant##LL)
213 #define O_RDONLY _O_RDONLY
214 #endif /* _MSC_VER */
217 * With MSVC, for C, __inline is used to make a function an inline.
220 #define inline __inline
223 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
224 #define HAVE_OS_IPV6_SUPPORT
227 #ifndef INET6_ADDRSTRLEN
228 #define INET6_ADDRSTRLEN 46
231 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
234 #define EAFNOSUPPORT WSAEAFNOSUPPORT
238 typedef char* caddr_t
;
241 #define MAXHOSTNAMELEN 64
242 #define snprintf _snprintf
243 #define vsnprintf _vsnprintf
248 * Includes and definitions for various flavors of UN*X.
254 #include <sys/param.h>
255 #include <sys/types.h> /* concession to AIX */
256 #include <sys/time.h>
257 #include <sys/socket.h>
258 #include <netinet/in.h>
262 #include <arpa/inet.h>
265 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
267 #define strtoint64_t strtoll
272 #define INT64_T_CONSTANT(constant) (constant##LL)
276 #ifndef HAVE___ATTRIBUTE__
277 #define __attribute__(x)
281 * Used to declare a structure unaligned, so that the C compiler,
282 * if necessary, generates code that doesn't assume alignment.
283 * This is required because there is no guarantee that the packet
284 * data we get from libpcap/WinPcap is properly aligned.
286 * This assumes that, for all compilers that support __attribute__:
288 * 1) they support __attribute__((packed));
290 * 2) for all instruction set architectures requiring strict
291 * alignment, declaring a structure with that attribute
292 * causes the compiler to generate code that handles
293 * misaligned 2-byte, 4-byte, and 8-byte integral
296 * It does not (yet) handle compilers where you can get the compiler
297 * to generate code of that sort by some other means.
299 * This is required in order to, for example, keep the compiler from
302 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
304 * in print-bootp.c, code that loads the first 4-byte word of a
305 * "struct bootp", masking out the bp_hops field, and comparing the result
306 * against 0x01010600.
308 * Note: this also requires that padding be put into the structure,
309 * at least for compilers where it's implemented as __attribute__((packed)).
311 #if !(defined(_MSC_VER) && defined(UNALIGNED))
312 /* MSVC may have its own macro defined with the same name and purpose. */
314 #define UNALIGNED __attribute__((packed))
318 * fopen() read and write modes for text files and binary files.
320 #if defined(_WIN32) || defined(MSDOS)
321 #define FOPEN_READ_TXT "rt"
322 #define FOPEN_READ_BIN "rb"
323 #define FOPEN_WRITE_TXT "wt"
324 #define FOPEN_WRITE_BIN "wb"
326 #define FOPEN_READ_TXT "r"
327 #define FOPEN_READ_BIN FOPEN_READ_TXT
328 #define FOPEN_WRITE_TXT "w"
329 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
333 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
334 * defined if the OS doesn't provide them. These assume no more than
335 * an 80386, so, for example, it avoids the bswap instruction added in
338 * (We don't use them on OS X; Apple provides their own, which *doesn't*
339 * avoid the bswap instruction, as OS X only supports machines that
342 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
348 static __inline__
unsigned long __ntohl (unsigned long x
);
349 static __inline__
unsigned short __ntohs (unsigned short x
);
351 #define ntohl(x) __ntohl(x)
352 #define ntohs(x) __ntohs(x)
353 #define htonl(x) __ntohl(x)
354 #define htons(x) __ntohs(x)
356 static __inline__
unsigned long __ntohl (unsigned long x
)
358 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
359 "rorl $16, %0\n\t" /* swap words */
360 "xchgb %b0, %h0" /* swap higher bytes */
361 : "=q" (x
) : "0" (x
));
365 static __inline__
unsigned short __ntohs (unsigned short x
)
367 __asm__ ("xchgb %b0, %h0" /* swap bytes */
368 : "=q" (x
) : "0" (x
));
374 * If the OS doesn't define AF_INET6 and struct in6_addr:
376 * define AF_INET6, so we can use it internally as a "this is an
377 * IPv6 address" indication;
379 * define struct in6_addr so that we can use it for IPv6 addresses.
381 #ifndef HAVE_OS_IPV6_SUPPORT
387 __uint8_t __u6_addr8
[16];
388 __uint16_t __u6_addr16
[8];
389 __uint32_t __u6_addr32
[4];
390 } __u6_addr
; /* 128-bit IP6 address */
396 #define NI_MAXHOST 1025
399 #ifndef INET_ADDRSTRLEN
400 #define INET_ADDRSTRLEN 16
412 * The Apple deprecation workaround macros below were adopted from the
413 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
416 #define XSTRINGIFY(x) #x
419 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
421 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
422 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
425 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
426 * thus we need to test the clang case before the GCC one
428 #if defined(__clang__)
429 # if (__clang_major__ * 100) + __clang_minor__ >= 208
430 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
431 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
432 # define DIAG_ON(x) DIAG_PRAGMA(pop)
437 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
438 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
439 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
440 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
441 # define DIAG_ON(x) DIAG_PRAGMA(pop)
443 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
444 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
451 /* Use for clang specific warnings */
453 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
454 # define DIAG_ON_CLANG(x) DIAG_ON(x)
456 # define DIAG_OFF_CLANG(x)
457 # define DIAG_ON_CLANG(x)
461 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
464 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
465 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
467 # define USES_APPLE_DEPRECATED_API
468 # define USES_APPLE_RST
472 * end of Apple deprecation workaround macros
476 * Function attributes, for various compilers.
478 #include "funcattrs.h"
481 #define min(a,b) ((a)>(b)?(b):(a))
484 #define max(a,b) ((b)>(a)?(b):(a))
487 #ifdef __ATTRIBUTE___FALLTHROUGH_OK
488 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
490 # define ND_FALL_THROUGH
491 #endif /* __ATTRIBUTE___FALLTHROUGH_OK */
493 #endif /* netdissect_stdinc_h */