]>
The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
aed38b31c49935a4b0245682bde3e21422e706ef
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
44 #include "compiler-tests.h"
49 * XXX - verify that we have at least C99 support on UN*Xes?
51 * What about MinGW or various DOS toolchains? We're currently assuming
52 * sufficient C99 support there.
56 * Make sure we have VS 2015 or later.
59 #error "Building tcpdump requires VS 2015 or later"
64 * Get the C99 types, and the PRI[doux]64 format strings, defined.
66 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
68 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
69 * work, and won't cause problems if a file includes this file
70 * and later includes a pcap header file that also includes
71 * pcap/pcap-inttypes.h.
73 #include <pcap/pcap-inttypes.h>
76 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
77 * do the work ourselves, but at least we don't have to
78 * worry about other headers including it and causing
83 * If the compiler is MSVC, we require VS 2015 or newer, so we
84 * have <inttypes.h> - and support for %zu in the formatted
87 * If the compiler is MinGW, we assume we have <inttypes.h> - and
88 * support for %zu in the formatted printing functions.
90 * If the target is UN*X, we assume we have a C99-or-later development
91 * environment, and thus have <inttypes.h> - and support for %zu in
92 * the formatted printing functions.
94 * If the target is MS-DOS, we assume we have <inttypes.h> - and support
95 * for %zu in the formatted printing functions.
101 * Suppress definition of intN_t in bittypes.h, which might be included
102 * by <pcap/pcap.h> in older versions of WinPcap.
103 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
104 * we check for u_intN_t in the UN*X configure script.)
106 #define HAVE_U_INT8_T
107 #define HAVE_U_INT16_T
108 #define HAVE_U_INT32_T
109 #define HAVE_U_INT64_T
112 * These may be defined by <inttypes.h>. If not, define them
115 * XXX - given the assumptions above, will they ever *not* be
116 * defined by <inttypes.h>?
118 * XXX - for MSVC, we always want the _MSC_EXTENSIONS versions.
119 * What about other compilers? If, as the MinGW Web site says MinGW
120 * does, the other compilers just use Microsoft's run-time library,
121 * then they should probably use the _MSC_EXTENSIONS even if the
122 * compiler doesn't define _MSC_EXTENSIONS.
125 #ifdef _MSC_EXTENSIONS
126 #define PRId64 "I64d"
133 #ifdef _MSC_EXTENSIONS
134 #define PRIo64 "I64o"
141 #ifdef _MSC_EXTENSIONS
142 #define PRIx64 "I64x"
149 #ifdef _MSC_EXTENSIONS
150 #define PRIu64 "I64u"
156 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
161 * Includes and definitions for Windows.
165 #include <winsock2.h>
166 #include <ws2tcpip.h>
171 #include <sys/types.h>
177 * We require VS 2015 or newer, so we have strtoll(). Use that for
180 #define strtoint64_t strtoll
183 * And we have LL as a suffix for constants, so use that.
185 #define INT64_T_CONSTANT(constant) (constant##LL)
188 * Non-Microsoft compiler.
190 * XXX - should we use strtoll or should we use _strtoi64()?
192 #define strtoint64_t strtoll
197 #define INT64_T_CONSTANT(constant) (constant##LL)
202 * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
203 * by adding a preceding underscore; we *want* the UN*Xisms, so add
204 * #defines to let us use them.
206 #define isascii __isascii
207 #define isatty _isatty
209 #define strdup _strdup
214 #define O_RDONLY _O_RDONLY
217 * If <crtdbg.h> has been included, and _DEBUG is defined, and
218 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
219 * _strdup_dbg(). So if it's already defined, don't redefine
223 #define strdup _strdup
227 * Windows doesn't have ssize_t; routines such as _read() return int.
230 #endif /* _MSC_VER */
233 * With MSVC, for C, __inline is used to make a function an inline.
236 #define inline __inline
239 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
240 #define HAVE_OS_IPV6_SUPPORT
243 #ifndef INET6_ADDRSTRLEN
244 #define INET6_ADDRSTRLEN 46
247 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
250 #define EAFNOSUPPORT WSAEAFNOSUPPORT
254 typedef char* caddr_t
;
257 #define MAXHOSTNAMELEN 64
262 * Includes and definitions for various flavors of UN*X.
268 #include <sys/param.h>
269 #include <sys/types.h> /* concession to AIX */
270 #include <sys/time.h>
271 #include <sys/socket.h>
272 #include <netinet/in.h>
276 #include <arpa/inet.h>
279 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
281 #define strtoint64_t strtoll
286 #define INT64_T_CONSTANT(constant) (constant##LL)
291 * Function attributes, for various compilers.
293 #include "funcattrs.h"
296 * fopen() read and write modes for text files and binary files.
298 #if defined(_WIN32) || defined(MSDOS)
299 #define FOPEN_READ_TXT "rt"
300 #define FOPEN_READ_BIN "rb"
301 #define FOPEN_WRITE_TXT "wt"
302 #define FOPEN_WRITE_BIN "wb"
304 #define FOPEN_READ_TXT "r"
305 #define FOPEN_READ_BIN FOPEN_READ_TXT
306 #define FOPEN_WRITE_TXT "w"
307 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
311 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
312 * defined if the OS doesn't provide them. These assume no more than
313 * an 80386, so, for example, it avoids the bswap instruction added in
316 * (We don't use them on macOS; Apple provides their own, which *doesn't*
317 * avoid the bswap instruction, as macOS only supports machines that
320 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
326 static __inline__
unsigned long __ntohl (unsigned long x
);
327 static __inline__
unsigned short __ntohs (unsigned short x
);
329 #define ntohl(x) __ntohl(x)
330 #define ntohs(x) __ntohs(x)
331 #define htonl(x) __ntohl(x)
332 #define htons(x) __ntohs(x)
334 static __inline__
unsigned long __ntohl (unsigned long x
)
336 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
337 "rorl $16, %0\n\t" /* swap words */
338 "xchgb %b0, %h0" /* swap higher bytes */
339 : "=q" (x
) : "0" (x
));
343 static __inline__
unsigned short __ntohs (unsigned short x
)
345 __asm__ ("xchgb %b0, %h0" /* swap bytes */
346 : "=q" (x
) : "0" (x
));
352 * If the OS doesn't define AF_INET6 and struct in6_addr:
354 * define AF_INET6, so we can use it internally as a "this is an
355 * IPv6 address" indication;
357 * define struct in6_addr so that we can use it for IPv6 addresses.
359 #ifndef HAVE_OS_IPV6_SUPPORT
365 __uint8_t __u6_addr8
[16];
366 __uint16_t __u6_addr16
[8];
367 __uint32_t __u6_addr32
[4];
368 } __u6_addr
; /* 128-bit IP6 address */
374 #define NI_MAXHOST 1025
377 #ifndef INET_ADDRSTRLEN
378 #define INET_ADDRSTRLEN 16
390 * The Apple deprecation workaround macros below were adopted from the
391 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
394 #define XSTRINGIFY(x) #x
397 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
399 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
400 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
403 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
404 * thus we need to test the clang case before the GCC one
406 #if defined(__clang__)
407 # if (__clang_major__ * 100) + __clang_minor__ >= 208
408 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
409 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
410 # define DIAG_ON(x) DIAG_PRAGMA(pop)
415 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
416 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
417 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
418 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
419 # define DIAG_ON(x) DIAG_PRAGMA(pop)
421 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
422 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
429 /* Use for clang specific warnings */
431 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
432 # define DIAG_ON_CLANG(x) DIAG_ON(x)
434 # define DIAG_OFF_CLANG(x)
435 # define DIAG_ON_CLANG(x)
439 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
442 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
443 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
445 # define USES_APPLE_DEPRECATED_API
446 # define USES_APPLE_RST
450 * end of Apple deprecation workaround macros
454 * Statement attributes, for various compilers.
456 * This was introduced sufficiently recently that compilers implementing
457 * it also implement __has_attribute() (for example, GCC 5.0 and later
458 * have __has_attribute(), and the "fallthrough" attribute was introduced
461 * Unfortunately, Clang does this wrong - a statement
463 * __attribute__ ((fallthrough));
465 * produces bogus -Wmissing-declaration "declaration does not declare
466 * anything" warnings (dear Clang: that's not a declaration, it's an
467 * empty statement). GCC, however, has no trouble with this.
469 #if __has_attribute(fallthrough) && !defined(__clang__)
470 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
472 # define ND_FALL_THROUGH
473 #endif /* __has_attribute(fallthrough) */
475 #endif /* netdissect_stdinc_h */