]>
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
46 #include "compiler-tests.h"
51 * If we're compiling with Visual Studio, make sure we have at least
52 * VS 2015 or later, so we have sufficient C99 support.
54 * XXX - verify that we have at least C99 support on UN*Xes?
56 * What about MinGW or various DOS toolchains? We're currently assuming
57 * sufficient C99 support there.
61 * Make sure we have VS 2015 or later.
64 #error "Building tcpdump requires VS 2015 or later"
69 * Get the C99 types, and the PRI[doux]64 format strings, defined.
71 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
73 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
74 * work, and won't cause problems if a file includes this file
75 * and later includes a pcap header file that also includes
76 * pcap/pcap-inttypes.h.
78 #include <pcap/pcap-inttypes.h>
81 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
82 * do the work ourselves, but at least we don't have to
83 * worry about other headers including it and causing
88 * Include <inttypes.h> to get the integer types and PRi[doux]64 values
91 * If the compiler is MSVC, we require VS 2015 or newer, so we
92 * have <inttypes.h> - and support for %zu in the formatted
95 * If the compiler is MinGW, we assume we have <inttypes.h> - and
96 * support for %zu in the formatted printing functions.
98 * If the target is UN*X, we assume we have a C99-or-later development
99 * environment, and thus have <inttypes.h> - and support for %zu in
100 * the formatted printing functions.
102 * If the target is MS-DOS, we assume we have <inttypes.h> - and support
103 * for %zu in the formatted printing functions.
105 #include <inttypes.h>
107 #if defined(_MSC_VER)
109 * Suppress definition of intN_t in bittypes.h, which might be included
110 * by <pcap/pcap.h> in older versions of WinPcap.
111 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented.)
113 #define HAVE_U_INT8_T
114 #define HAVE_U_INT16_T
115 #define HAVE_U_INT32_T
116 #define HAVE_U_INT64_T
118 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
123 * Includes and definitions for Windows.
127 #include <winsock2.h>
128 #include <ws2tcpip.h>
132 #include <sys/types.h>
138 * We require VS 2015 or newer, so we have strtoll(). Use that for
141 #define strtoint64_t strtoll
144 * And we have LL as a suffix for constants, so use that.
146 #define INT64_T_CONSTANT(constant) (constant##LL)
149 * Non-Microsoft compiler.
151 * XXX - should we use strtoll or should we use _strtoi64()?
153 #define strtoint64_t strtoll
158 #define INT64_T_CONSTANT(constant) (constant##LL)
163 * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
164 * by adding a preceding underscore; we *want* the UN*Xisms, so add
165 * #defines to let us use them.
167 #define isatty _isatty
169 #define strdup _strdup
173 #define O_RDONLY _O_RDONLY
176 * We define our_fstat64 as _fstati64, and define our_statb as
177 * struct _stati64, so we get 64-bit file sizes.
179 #define our_fstat _fstati64
180 #define our_statb struct _stati64
183 * If <crtdbg.h> has been included, and _DEBUG is defined, and
184 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
185 * _strdup_dbg(). So if it's already defined, don't redefine
189 #define strdup _strdup
193 * Windows doesn't have ssize_t; routines such as _read() return int.
196 #endif /* _MSC_VER */
199 * With MSVC, for C, __inline is used to make a function an inline.
202 #define inline __inline
205 #ifndef INET6_ADDRSTRLEN
206 #define INET6_ADDRSTRLEN 46
209 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
212 #define EAFNOSUPPORT WSAEAFNOSUPPORT
216 typedef char *caddr_t
;
219 #define MAXHOSTNAMELEN 64
224 * Includes and definitions for various flavors of UN*X.
229 #include <sys/param.h>
230 #include <sys/types.h> /* concession to AIX */
231 #include <sys/time.h>
232 #include <sys/socket.h>
233 #include <netinet/in.h>
237 #include <arpa/inet.h>
240 * We should have large file support enabled, if it's available,
241 * so just use fstat as our_fstat and struct stat as our_statb.
243 #define our_fstat fstat
244 #define our_statb struct stat
247 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
249 #define strtoint64_t strtoll
254 #define INT64_T_CONSTANT(constant) (constant##LL)
258 * Function attributes, for various compilers.
260 #include "funcattrs.h"
263 * fopen() read and write modes for text files and binary files.
265 #if defined(_WIN32) || defined(MSDOS)
266 #define FOPEN_READ_TXT "rt"
267 #define FOPEN_READ_BIN "rb"
268 #define FOPEN_WRITE_TXT "wt"
269 #define FOPEN_WRITE_BIN "wb"
271 #define FOPEN_READ_TXT "r"
272 #define FOPEN_READ_BIN FOPEN_READ_TXT
273 #define FOPEN_WRITE_TXT "w"
274 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
278 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
279 * defined if the OS doesn't provide them. These assume no more than
280 * an 80386, so, for example, it avoids the bswap instruction added in
283 * (We don't use them on macOS; Apple provides their own, which *doesn't*
284 * avoid the bswap instruction, as macOS only supports machines that
287 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
293 static __inline__
unsigned long __ntohl (unsigned long x
);
294 static __inline__
unsigned short __ntohs (unsigned short x
);
296 #define ntohl(x) __ntohl(x)
297 #define ntohs(x) __ntohs(x)
298 #define htonl(x) __ntohl(x)
299 #define htons(x) __ntohs(x)
301 static __inline__
unsigned long __ntohl (unsigned long x
)
303 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
304 "rorl $16, %0\n\t" /* swap words */
305 "xchgb %b0, %h0" /* swap higher bytes */
306 : "=q" (x
) : "0" (x
));
310 static __inline__
unsigned short __ntohs (unsigned short x
)
312 __asm__ ("xchgb %b0, %h0" /* swap bytes */
313 : "=q" (x
) : "0" (x
));
319 #define NI_MAXHOST 1025
322 #ifndef INET_ADDRSTRLEN
323 #define INET_ADDRSTRLEN 16
335 * Statement attributes, for various compilers.
337 * This was introduced sufficiently recently that compilers implementing
338 * it also implement __has_attribute() (for example, GCC 5.0 and later
339 * have __has_attribute(), and the "fallthrough" attribute was introduced
342 * Unfortunately, Clang does this wrong - a statement
344 * __attribute__ ((fallthrough));
346 * produces bogus -Wmissing-declaration "declaration does not declare
347 * anything" warnings (dear Clang: that's not a declaration, it's an
348 * empty statement). GCC, however, has no trouble with this.
350 #if __has_attribute(fallthrough) && !defined(__clang__)
351 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
353 # define ND_FALL_THROUGH
354 #endif /* __has_attribute(fallthrough) */
356 #endif /* netdissect_stdinc_h */