]>
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 * Get the C99 types, and the PRI[doux]64 format strings, defined.
51 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
53 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
54 * work, and won't cause problems if a file includes this file
55 * and later includes a pcap header file that also includes
56 * pcap/pcap-inttypes.h.
58 #include <pcap/pcap-inttypes.h>
61 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
62 * do the work ourselves, but at least we don't have to
63 * worry about other headers including it and causing
72 * VS 2013 or newer; we have <inttypes.h>.
77 * Earlier VS; we have to define this stuff ourselves.
79 typedef unsigned char uint8_t;
80 typedef signed char int8_t;
81 typedef unsigned short uint16_t;
82 typedef signed short int16_t;
83 typedef unsigned int uint32_t;
84 typedef signed int int32_t;
85 #ifdef _MSC_EXTENSIONS
86 typedef unsigned _int64
uint64_t;
87 typedef _int64
int64_t;
88 #else /* _MSC_EXTENSIONS */
89 typedef unsigned long long uint64_t;
90 typedef long long int64_t;
94 * We have _strtoi64(). Use that for strtoint64_t().
96 #define strtoint64_t _strtoi64
100 * Suppress definition of intN_t in bittypes.h, which might be included
101 * by <pcap/pcap.h> in older versions of WinPcap.
102 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
103 * we check for u_intN_t in the UN*X configure script.)
105 #define HAVE_U_INT8_T
106 #define HAVE_U_INT16_T
107 #define HAVE_U_INT32_T
108 #define HAVE_U_INT64_T
111 * These may be defined by <inttypes.h>. If not, define them
114 * XXX - for MSVC, we always want the _MSC_EXTENSIONS versions.
115 * What about other compilers? If, as the MinGW Web site says MinGW
116 * does, the other compilers just use Microsoft's run-time library,
117 * then they should probably use the _MSC_EXTENSIONS even if the
118 * compiler doesn't define _MSC_EXTENSIONS.
121 #ifdef _MSC_EXTENSIONS
122 #define PRId64 "I64d"
129 #ifdef _MSC_EXTENSIONS
130 #define PRIo64 "I64o"
137 #ifdef _MSC_EXTENSIONS
138 #define PRIx64 "I64x"
145 #ifdef _MSC_EXTENSIONS
146 #define PRIu64 "I64u"
151 #elif defined(__MINGW32__) || !defined(_WIN32)
153 * Compiler is MinGW or target is UN*X or MS-DOS. Just use
156 #include <inttypes.h>
158 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
163 * Includes and definitions for Windows.
167 #include <winsock2.h>
168 #include <ws2tcpip.h>
173 #include <sys/types.h>
181 * VS 2013 or newer; we have strtoll(). Use that for strtoint64_t().
183 #define strtoint64_t strtoll
186 * Earlier VS; we don't have strtoll(), but we do have
187 * _strtoi64(). Use that for strtoint64_t().
189 #define strtoint64_t _strtoi64
193 * Microsoft's documentation doesn't speak of LL as a valid
194 * suffix for 64-bit integers, so we'll just use i64.
196 #define INT64_T_CONSTANT(constant) (constant##i64)
199 * Non-Microsoft compiler.
201 * XXX - should we use strtoll or should we use _strtoi64()?
203 #define strtoint64_t strtoll
208 #define INT64_T_CONSTANT(constant) (constant##LL)
213 * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
214 * by adding a preceding underscore; we *want* the UN*Xisms, so add
215 * #defines to let us use them.
217 #define isascii __isascii
218 #define isatty _isatty
220 #define strdup _strdup
225 #define O_RDONLY _O_RDONLY
228 * If <crtdbg.h> has been included, and _DEBUG is defined, and
229 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
230 * _strdup_dbg(). So if it's already defined, don't redefine
234 #define strdup _strdup
236 #endif /* _MSC_VER */
239 * With MSVC, for C, __inline is used to make a function an inline.
242 #define inline __inline
245 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
246 #define HAVE_OS_IPV6_SUPPORT
249 #ifndef INET6_ADDRSTRLEN
250 #define INET6_ADDRSTRLEN 46
253 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
256 #define EAFNOSUPPORT WSAEAFNOSUPPORT
260 typedef char* caddr_t
;
263 #define MAXHOSTNAMELEN 64
268 * Includes and definitions for various flavors of UN*X.
274 #include <sys/param.h>
275 #include <sys/types.h> /* concession to AIX */
276 #include <sys/time.h>
277 #include <sys/socket.h>
278 #include <netinet/in.h>
282 #include <arpa/inet.h>
285 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
287 #define strtoint64_t strtoll
292 #define INT64_T_CONSTANT(constant) (constant##LL)
297 * Function attributes, for various compilers.
299 #include "funcattrs.h"
302 * On Windows, snprintf(), with that name and with C99 behavior - i.e.,
303 * guaranteeing that the formatted string is null-terminated - didn't
304 * appear until Visual Studio 2015. Prior to that, the C runtime had
305 * only _snprintf(), which *doesn't* guarantee that the string is
306 * null-terminated if it is truncated due to the buffer being too
307 * small. We therefore can't just define snprintf to be _snprintf
308 * and define vsnprintf to be _vsnprintf, as we're relying on null-
309 * termination of strings in all cases.
311 * Furthermore, some versions of Visual Studio prior to Visual
312 * Studio 2015 had vsnprintf() (but not snprintf()!), but those
313 * versions don't guarantee null termination, either.
315 * We assume all UN*Xes that have snprintf() and vsnprintf() provide
318 #if defined(_MSC_VER) || defined(__MINGW32__)
319 #if defined(_MSC_VER) && _MSC_VER >= 1900
321 * VS 2015 or newer; just use the C runtime's snprintf() and
324 #define nd_snprintf snprintf
325 #define nd_vsnprintf vsnprintf
326 #else /* defined(_MSC_VER) && _MSC_VER >= 1900 */
328 * VS prior to 2015, or MingGW; assume we have _snprintf_s() and
329 * _vsnprintf_s(), which guarantee null termination.
331 #define nd_snprintf(buf, buflen, ...) \
332 _snprintf_s(buf, buflen, _TRUNCATE, __VA_ARGS__)
333 #define nd_vsnprintf(buf, buflen, fmt, ap) \
334 _vsnprintf_s(buf, buflen, _TRUNCATE, fmt, ap)
335 #endif /* defined(_MSC_VER) && _MSC_VER >= 1900 */
336 #else /* defined(_MSC_VER) || defined(__MINGW32__) */
338 * Some other compiler, which we assume to be a UN*X compiler.
339 * Use the system's snprintf() if we have it, otherwise use
340 * our own implementation
343 #define nd_snprintf snprintf
344 #else /* HAVE_SNPRINTF */
345 int nd_snprintf (char *str
, size_t sz
, FORMAT_STRING(const char *format
), ...)
347 #endif /* HAVE_SNPRINTF */
349 #ifdef HAVE_VSNPRINTF
350 #define nd_vsnprintf vsnprintf
351 #else /* HAVE_VSNPRINTF */
352 int nd_vsnprintf (char *str
, size_t sz
, FORMAT_STRING(const char *format
),
353 va_list ap
) PRINTFLIKE(3, 0);
354 #endif /* HAVE_VSNPRINTF */
355 #endif /* defined(_MSC_VER) || defined(__MINGW32__) */
358 * fopen() read and write modes for text files and binary files.
360 #if defined(_WIN32) || defined(MSDOS)
361 #define FOPEN_READ_TXT "rt"
362 #define FOPEN_READ_BIN "rb"
363 #define FOPEN_WRITE_TXT "wt"
364 #define FOPEN_WRITE_BIN "wb"
366 #define FOPEN_READ_TXT "r"
367 #define FOPEN_READ_BIN FOPEN_READ_TXT
368 #define FOPEN_WRITE_TXT "w"
369 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
373 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
374 * defined if the OS doesn't provide them. These assume no more than
375 * an 80386, so, for example, it avoids the bswap instruction added in
378 * (We don't use them on macOS; Apple provides their own, which *doesn't*
379 * avoid the bswap instruction, as macOS only supports machines that
382 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
388 static __inline__
unsigned long __ntohl (unsigned long x
);
389 static __inline__
unsigned short __ntohs (unsigned short x
);
391 #define ntohl(x) __ntohl(x)
392 #define ntohs(x) __ntohs(x)
393 #define htonl(x) __ntohl(x)
394 #define htons(x) __ntohs(x)
396 static __inline__
unsigned long __ntohl (unsigned long x
)
398 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
399 "rorl $16, %0\n\t" /* swap words */
400 "xchgb %b0, %h0" /* swap higher bytes */
401 : "=q" (x
) : "0" (x
));
405 static __inline__
unsigned short __ntohs (unsigned short x
)
407 __asm__ ("xchgb %b0, %h0" /* swap bytes */
408 : "=q" (x
) : "0" (x
));
414 * If the OS doesn't define AF_INET6 and struct in6_addr:
416 * define AF_INET6, so we can use it internally as a "this is an
417 * IPv6 address" indication;
419 * define struct in6_addr so that we can use it for IPv6 addresses.
421 #ifndef HAVE_OS_IPV6_SUPPORT
427 __uint8_t __u6_addr8
[16];
428 __uint16_t __u6_addr16
[8];
429 __uint32_t __u6_addr32
[4];
430 } __u6_addr
; /* 128-bit IP6 address */
436 #define NI_MAXHOST 1025
439 #ifndef INET_ADDRSTRLEN
440 #define INET_ADDRSTRLEN 16
452 * The Apple deprecation workaround macros below were adopted from the
453 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
456 #define XSTRINGIFY(x) #x
459 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
461 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
462 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
465 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
466 * thus we need to test the clang case before the GCC one
468 #if defined(__clang__)
469 # if (__clang_major__ * 100) + __clang_minor__ >= 208
470 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
471 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
472 # define DIAG_ON(x) DIAG_PRAGMA(pop)
477 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
478 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
479 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
480 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
481 # define DIAG_ON(x) DIAG_PRAGMA(pop)
483 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
484 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
491 /* Use for clang specific warnings */
493 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
494 # define DIAG_ON_CLANG(x) DIAG_ON(x)
496 # define DIAG_OFF_CLANG(x)
497 # define DIAG_ON_CLANG(x)
501 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
504 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
505 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
507 # define USES_APPLE_DEPRECATED_API
508 # define USES_APPLE_RST
512 * end of Apple deprecation workaround macros
516 * Statement attributes, for various compilers.
518 * This was introduced sufficiently recently that compilers implementing
519 * it also implement __has_attribute() (for example, GCC 5.0 and later
520 * have __has_attribute(), and the "fallthrough" attribute was introduced
523 * Unfortunately, Clang does this wrong - a statement
525 * __attribute__ ((fallthrough));
527 * produces bogus -Wmissing-declaration "declaration does not declare
528 * anything" warnings (dear Clang: that's not a declaration, it's an
529 * empty statement). GCC, however, has no trouble with this.
531 #if __has_attribute(fallthrough) && !defined(__clang__)
532 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
534 # define ND_FALL_THROUGH
535 #endif /* __has_attribute(fallthrough) */
537 #endif /* netdissect_stdinc_h */