]>
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
44 #include "compiler-tests.h"
49 * If we're compiling with Visual Studio, make sure we have at least
50 * VS 2015 or later, so we have sufficient C99 support.
52 * XXX - verify that we have at least C99 support on UN*Xes?
54 * What about MinGW or various DOS toolchains? We're currently assuming
55 * sufficient C99 support there.
59 * Make sure we have VS 2015 or later.
62 #error "Building tcpdump requires VS 2015 or later"
67 * Get the C99 types, and the PRI[doux]64 format strings, defined.
69 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
71 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
72 * work, and won't cause problems if a file includes this file
73 * and later includes a pcap header file that also includes
74 * pcap/pcap-inttypes.h.
76 #include <pcap/pcap-inttypes.h>
79 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
80 * do the work ourselves, but at least we don't have to
81 * worry about other headers including it and causing
86 * Include <inttypes.h> to get the integer types and PRi[doux]64 values
89 * If the compiler is MSVC, we require VS 2015 or newer, so we
90 * have <inttypes.h> - and support for %zu in the formatted
93 * If the compiler is MinGW, we assume we have <inttypes.h> - and
94 * support for %zu in the formatted printing functions.
96 * If the target is UN*X, we assume we have a C99-or-later development
97 * environment, and thus have <inttypes.h> - and support for %zu in
98 * the formatted printing functions.
100 * If the target is MS-DOS, we assume we have <inttypes.h> - and support
101 * for %zu in the formatted printing functions.
103 #include <inttypes.h>
105 #if defined(_MSC_VER)
107 * Suppress definition of intN_t in bittypes.h, which might be included
108 * by <pcap/pcap.h> in older versions of WinPcap.
109 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented.)
111 #define HAVE_U_INT8_T
112 #define HAVE_U_INT16_T
113 #define HAVE_U_INT32_T
114 #define HAVE_U_INT64_T
116 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
121 * Includes and definitions for Windows.
125 #include <winsock2.h>
126 #include <ws2tcpip.h>
130 #include <sys/types.h>
136 * We require VS 2015 or newer, so we have strtoll(). Use that for
139 #define strtoint64_t strtoll
142 * And we have LL as a suffix for constants, so use that.
144 #define INT64_T_CONSTANT(constant) (constant##LL)
147 * Non-Microsoft compiler.
149 * XXX - should we use strtoll or should we use _strtoi64()?
151 #define strtoint64_t strtoll
156 #define INT64_T_CONSTANT(constant) (constant##LL)
161 * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
162 * by adding a preceding underscore; we *want* the UN*Xisms, so add
163 * #defines to let us use them.
165 #define isatty _isatty
167 #define strdup _strdup
171 #define O_RDONLY _O_RDONLY
174 * We define our_fstat64 as _fstati64, and define our_statb as
175 * struct _stati64, so we get 64-bit file sizes.
177 #define our_fstat _fstati64
178 #define our_statb struct _stati64
181 * If <crtdbg.h> has been included, and _DEBUG is defined, and
182 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
183 * _strdup_dbg(). So if it's already defined, don't redefine
187 #define strdup _strdup
191 * Windows doesn't have ssize_t; routines such as _read() return int.
194 #endif /* _MSC_VER */
197 * With MSVC, for C, __inline is used to make a function an inline.
200 #define inline __inline
203 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
204 #define HAVE_OS_IPV6_SUPPORT
207 #ifndef INET6_ADDRSTRLEN
208 #define INET6_ADDRSTRLEN 46
211 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
214 #define EAFNOSUPPORT WSAEAFNOSUPPORT
218 typedef char* caddr_t
;
221 #define MAXHOSTNAMELEN 64
226 * Includes and definitions for various flavors of UN*X.
231 #include <sys/param.h>
232 #include <sys/types.h> /* concession to AIX */
233 #include <sys/time.h>
234 #include <sys/socket.h>
235 #include <netinet/in.h>
239 #include <arpa/inet.h>
242 * We should have large file support enabled, if it's available,
243 * so just use fstat as our_fstat and struct stat as our_statb.
245 #define our_fstat fstat
246 #define our_statb struct stat
249 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
251 #define strtoint64_t strtoll
256 #define INT64_T_CONSTANT(constant) (constant##LL)
260 * Function attributes, for various compilers.
262 #include "funcattrs.h"
265 * fopen() read and write modes for text files and binary files.
267 #if defined(_WIN32) || defined(MSDOS)
268 #define FOPEN_READ_TXT "rt"
269 #define FOPEN_READ_BIN "rb"
270 #define FOPEN_WRITE_TXT "wt"
271 #define FOPEN_WRITE_BIN "wb"
273 #define FOPEN_READ_TXT "r"
274 #define FOPEN_READ_BIN FOPEN_READ_TXT
275 #define FOPEN_WRITE_TXT "w"
276 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
280 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
281 * defined if the OS doesn't provide them. These assume no more than
282 * an 80386, so, for example, it avoids the bswap instruction added in
285 * (We don't use them on macOS; Apple provides their own, which *doesn't*
286 * avoid the bswap instruction, as macOS only supports machines that
289 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
295 static __inline__
unsigned long __ntohl (unsigned long x
);
296 static __inline__
unsigned short __ntohs (unsigned short x
);
298 #define ntohl(x) __ntohl(x)
299 #define ntohs(x) __ntohs(x)
300 #define htonl(x) __ntohl(x)
301 #define htons(x) __ntohs(x)
303 static __inline__
unsigned long __ntohl (unsigned long x
)
305 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
306 "rorl $16, %0\n\t" /* swap words */
307 "xchgb %b0, %h0" /* swap higher bytes */
308 : "=q" (x
) : "0" (x
));
312 static __inline__
unsigned short __ntohs (unsigned short x
)
314 __asm__ ("xchgb %b0, %h0" /* swap bytes */
315 : "=q" (x
) : "0" (x
));
321 * If the OS doesn't define AF_INET6 and struct in6_addr:
323 * define AF_INET6, so we can use it internally as a "this is an
324 * IPv6 address" indication;
326 * define struct in6_addr so that we can use it for IPv6 addresses.
328 #ifndef HAVE_OS_IPV6_SUPPORT
334 __uint8_t __u6_addr8
[16];
335 __uint16_t __u6_addr16
[8];
336 __uint32_t __u6_addr32
[4];
337 } __u6_addr
; /* 128-bit IP6 address */
343 #define NI_MAXHOST 1025
346 #ifndef INET_ADDRSTRLEN
347 #define INET_ADDRSTRLEN 16
359 * The Apple deprecation workaround macros below were adopted from the
360 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
363 #define XSTRINGIFY(x) #x
366 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
368 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
369 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
372 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
373 * thus we need to test the clang case before the GCC one
375 #if defined(__clang__)
376 # if (__clang_major__ * 100) + __clang_minor__ >= 208
377 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
378 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
379 # define DIAG_ON(x) DIAG_PRAGMA(pop)
384 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
385 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
386 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
387 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
388 # define DIAG_ON(x) DIAG_PRAGMA(pop)
390 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
391 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
398 /* Use for clang specific warnings */
400 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
401 # define DIAG_ON_CLANG(x) DIAG_ON(x)
403 # define DIAG_OFF_CLANG(x)
404 # define DIAG_ON_CLANG(x)
408 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
411 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
412 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
414 # define USES_APPLE_DEPRECATED_API
415 # define USES_APPLE_RST
419 * end of Apple deprecation workaround macros
423 * Statement attributes, for various compilers.
425 * This was introduced sufficiently recently that compilers implementing
426 * it also implement __has_attribute() (for example, GCC 5.0 and later
427 * have __has_attribute(), and the "fallthrough" attribute was introduced
430 * Unfortunately, Clang does this wrong - a statement
432 * __attribute__ ((fallthrough));
434 * produces bogus -Wmissing-declaration "declaration does not declare
435 * anything" warnings (dear Clang: that's not a declaration, it's an
436 * empty statement). GCC, however, has no trouble with this.
438 #if __has_attribute(fallthrough) && !defined(__clang__)
439 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
441 # define ND_FALL_THROUGH
442 #endif /* __has_attribute(fallthrough) */
444 #endif /* netdissect_stdinc_h */