]> The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
Require VS 2015 or later for Windows builds using Visual Studio.
[tcpdump] / netdissect-stdinc.h
1 /*
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
18 *
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.
30 */
31
32 /*
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.
37 */
38
39 #ifndef netdissect_stdinc_h
40 #define netdissect_stdinc_h
41
42 #include <errno.h>
43
44 #include "compiler-tests.h"
45
46 #include "varattrs.h"
47
48 /*
49 * XXX - verify that we have at least C99 support on UN*Xes?
50 *
51 * What about MinGW or various DOS toolchains? We're currently assuming
52 * sufficient C99 support there.
53 */
54 #if defined(_MSC_VER)
55 /*
56 * Make sure we have VS 2015 or later.
57 */
58 #if _MSC_VER < 1900
59 #error "Building tcpdump requires VS 2015 or later"
60 #endif
61 #endif
62
63 /*
64 * Get the C99 types, and the PRI[doux]64 format strings, defined.
65 */
66 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
67 /*
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.
72 */
73 #include <pcap/pcap-inttypes.h>
74 #else
75 /*
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
79 * clashes.
80 */
81
82 /*
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
85 * printing functions.
86 *
87 * If the compiler is MinGW, we assume we have <inttypes.h> - and
88 * support for %zu in the formatted printing functions.
89 *
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.
93 *
94 * If the target is MS-DOS, we assume we have <inttypes.h> - and support
95 * for %zu in the formatted printing functions.
96 */
97 #include <inttypes.h>
98
99 #if defined(_MSC_VER)
100 /*
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.)
105 */
106 #define HAVE_U_INT8_T
107 #define HAVE_U_INT16_T
108 #define HAVE_U_INT32_T
109 #define HAVE_U_INT64_T
110
111 /*
112 * These may be defined by <inttypes.h>. If not, define them
113 * ourselves.
114 *
115 * XXX - given the assumptions above, will they ever *not* be
116 * defined by <inttypes.h>?
117 *
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.
123 */
124 #ifndef PRId64
125 #ifdef _MSC_EXTENSIONS
126 #define PRId64 "I64d"
127 #else
128 #define PRId64 "lld"
129 #endif
130 #endif /* PRId64 */
131
132 #ifndef PRIo64
133 #ifdef _MSC_EXTENSIONS
134 #define PRIo64 "I64o"
135 #else
136 #define PRIo64 "llo"
137 #endif
138 #endif /* PRIo64 */
139
140 #ifndef PRIx64
141 #ifdef _MSC_EXTENSIONS
142 #define PRIx64 "I64x"
143 #else
144 #define PRIx64 "llx"
145 #endif
146 #endif
147
148 #ifndef PRIu64
149 #ifdef _MSC_EXTENSIONS
150 #define PRIu64 "I64u"
151 #else
152 #define PRIu64 "llu"
153 #endif
154 #endif
155 #endif
156 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
157
158 #ifdef _WIN32
159
160 /*
161 * Includes and definitions for Windows.
162 */
163
164 #include <stdio.h>
165 #include <winsock2.h>
166 #include <ws2tcpip.h>
167 #include <ctype.h>
168 #include <time.h>
169 #include <io.h>
170 #include <fcntl.h>
171 #include <sys/types.h>
172
173 #ifdef _MSC_VER
174 /*
175 * Compiler is MSVC.
176 *
177 * We require VS 2015 or newer, so we have strtoll(). Use that for
178 * strtoint64_t().
179 */
180 #define strtoint64_t strtoll
181
182 /*
183 * Microsoft's documentation doesn't speak of LL as a valid
184 * suffix for 64-bit integers, so we'll just use i64.
185 *
186 * XXX - is that still the case as of VS 2015?
187 */
188 #define INT64_T_CONSTANT(constant) (constant##i64)
189 #else
190 /*
191 * Non-Microsoft compiler.
192 *
193 * XXX - should we use strtoll or should we use _strtoi64()?
194 */
195 #define strtoint64_t strtoll
196
197 /*
198 * Assume LL works.
199 */
200 #define INT64_T_CONSTANT(constant) (constant##LL)
201 #endif
202
203 #ifdef _MSC_VER
204 /*
205 * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
206 * by adding a preceding underscore; we *want* the UN*Xisms, so add
207 * #defines to let us use them.
208 */
209 #define isascii __isascii
210 #define isatty _isatty
211 #define stat _stat
212 #define strdup _strdup
213 #define open _open
214 #define fstat _fstat
215 #define read _read
216 #define close _close
217 #define O_RDONLY _O_RDONLY
218
219 /*
220 * If <crtdbg.h> has been included, and _DEBUG is defined, and
221 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
222 * _strdup_dbg(). So if it's already defined, don't redefine
223 * it.
224 */
225 #ifndef strdup
226 #define strdup _strdup
227 #endif
228
229 /*
230 * Windows doesn't have ssize_t; routines such as _read() return int.
231 */
232 typedef int ssize_t;
233 #endif /* _MSC_VER */
234
235 /*
236 * With MSVC, for C, __inline is used to make a function an inline.
237 */
238 #ifdef _MSC_VER
239 #define inline __inline
240 #endif
241
242 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
243 #define HAVE_OS_IPV6_SUPPORT
244 #endif
245
246 #ifndef INET6_ADDRSTRLEN
247 #define INET6_ADDRSTRLEN 46
248 #endif
249
250 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
251 */
252 #ifndef EAFNOSUPPORT
253 #define EAFNOSUPPORT WSAEAFNOSUPPORT
254 #endif
255
256 #ifndef caddr_t
257 typedef char* caddr_t;
258 #endif /* caddr_t */
259
260 #define MAXHOSTNAMELEN 64
261
262 #else /* _WIN32 */
263
264 /*
265 * Includes and definitions for various flavors of UN*X.
266 */
267
268 #include <ctype.h>
269 #include <unistd.h>
270 #include <netdb.h>
271 #include <sys/param.h>
272 #include <sys/types.h> /* concession to AIX */
273 #include <sys/time.h>
274 #include <sys/socket.h>
275 #include <netinet/in.h>
276
277 #include <time.h>
278
279 #include <arpa/inet.h>
280
281 /*
282 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
283 */
284 #define strtoint64_t strtoll
285
286 /*
287 * Assume LL works.
288 */
289 #define INT64_T_CONSTANT(constant) (constant##LL)
290
291 #endif /* _WIN32 */
292
293 /*
294 * Function attributes, for various compilers.
295 */
296 #include "funcattrs.h"
297
298 /*
299 * On Windows, snprintf(), with that name and with C99 behavior - i.e.,
300 * guaranteeing that the formatted string is null-terminated - didn't
301 * appear until Visual Studio 2015. Prior to that, the C runtime had
302 * only _snprintf(), which *doesn't* guarantee that the string is
303 * null-terminated if it is truncated due to the buffer being too
304 * small. We therefore can't just define snprintf to be _snprintf
305 * and define vsnprintf to be _vsnprintf, as we're relying on null-
306 * termination of strings in all cases.
307 *
308 * Furthermore, some versions of Visual Studio prior to Visual
309 * Studio 2015 had vsnprintf() (but not snprintf()!), but those
310 * versions don't guarantee null termination, either.
311 *
312 * We assume all UN*Xes that have snprintf() and vsnprintf() provide
313 * C99 behavior.
314 */
315 #if defined(_MSC_VER) || defined(__MINGW32__)
316 #if defined(_MSC_VER) && _MSC_VER >= 1900
317 /*
318 * VS 2015 or newer; just use the C runtime's snprintf() and
319 * vsnprintf().
320 */
321 #define nd_snprintf snprintf
322 #define nd_vsnprintf vsnprintf
323 #else /* defined(_MSC_VER) && _MSC_VER >= 1900 */
324 /*
325 * VS prior to 2015, or MingGW; assume we have _snprintf_s() and
326 * _vsnprintf_s(), which guarantee null termination.
327 */
328 #define nd_snprintf(buf, buflen, ...) \
329 _snprintf_s(buf, buflen, _TRUNCATE, __VA_ARGS__)
330 #define nd_vsnprintf(buf, buflen, fmt, ap) \
331 _vsnprintf_s(buf, buflen, _TRUNCATE, fmt, ap)
332 #endif /* defined(_MSC_VER) && _MSC_VER >= 1900 */
333 #else /* defined(_MSC_VER) || defined(__MINGW32__) */
334 /*
335 * Some other compiler, which we assume to be a UN*X compiler.
336 * Use the system's snprintf() if we have it, otherwise use
337 * our own implementation
338 */
339 #ifdef HAVE_SNPRINTF
340 #define nd_snprintf snprintf
341 #else /* HAVE_SNPRINTF */
342 int nd_snprintf (char *str, size_t sz, FORMAT_STRING(const char *format), ...)
343 PRINTFLIKE(3, 4);
344 #endif /* HAVE_SNPRINTF */
345
346 #ifdef HAVE_VSNPRINTF
347 #define nd_vsnprintf vsnprintf
348 #else /* HAVE_VSNPRINTF */
349 int nd_vsnprintf (char *str, size_t sz, FORMAT_STRING(const char *format),
350 va_list ap) PRINTFLIKE(3, 0);
351 #endif /* HAVE_VSNPRINTF */
352 #endif /* defined(_MSC_VER) || defined(__MINGW32__) */
353
354 /*
355 * fopen() read and write modes for text files and binary files.
356 */
357 #if defined(_WIN32) || defined(MSDOS)
358 #define FOPEN_READ_TXT "rt"
359 #define FOPEN_READ_BIN "rb"
360 #define FOPEN_WRITE_TXT "wt"
361 #define FOPEN_WRITE_BIN "wb"
362 #else
363 #define FOPEN_READ_TXT "r"
364 #define FOPEN_READ_BIN FOPEN_READ_TXT
365 #define FOPEN_WRITE_TXT "w"
366 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
367 #endif
368
369 /*
370 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
371 * defined if the OS doesn't provide them. These assume no more than
372 * an 80386, so, for example, it avoids the bswap instruction added in
373 * the 80486.
374 *
375 * (We don't use them on macOS; Apple provides their own, which *doesn't*
376 * avoid the bswap instruction, as macOS only supports machines that
377 * have it.)
378 */
379 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
380 #undef ntohl
381 #undef ntohs
382 #undef htonl
383 #undef htons
384
385 static __inline__ unsigned long __ntohl (unsigned long x);
386 static __inline__ unsigned short __ntohs (unsigned short x);
387
388 #define ntohl(x) __ntohl(x)
389 #define ntohs(x) __ntohs(x)
390 #define htonl(x) __ntohl(x)
391 #define htons(x) __ntohs(x)
392
393 static __inline__ unsigned long __ntohl (unsigned long x)
394 {
395 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
396 "rorl $16, %0\n\t" /* swap words */
397 "xchgb %b0, %h0" /* swap higher bytes */
398 : "=q" (x) : "0" (x));
399 return (x);
400 }
401
402 static __inline__ unsigned short __ntohs (unsigned short x)
403 {
404 __asm__ ("xchgb %b0, %h0" /* swap bytes */
405 : "=q" (x) : "0" (x));
406 return (x);
407 }
408 #endif
409
410 /*
411 * If the OS doesn't define AF_INET6 and struct in6_addr:
412 *
413 * define AF_INET6, so we can use it internally as a "this is an
414 * IPv6 address" indication;
415 *
416 * define struct in6_addr so that we can use it for IPv6 addresses.
417 */
418 #ifndef HAVE_OS_IPV6_SUPPORT
419 #ifndef AF_INET6
420 #define AF_INET6 24
421
422 struct in6_addr {
423 union {
424 __uint8_t __u6_addr8[16];
425 __uint16_t __u6_addr16[8];
426 __uint32_t __u6_addr32[4];
427 } __u6_addr; /* 128-bit IP6 address */
428 };
429 #endif
430 #endif
431
432 #ifndef NI_MAXHOST
433 #define NI_MAXHOST 1025
434 #endif
435
436 #ifndef INET_ADDRSTRLEN
437 #define INET_ADDRSTRLEN 16
438 #endif
439
440 #ifndef TRUE
441 #define TRUE 1
442 #endif
443
444 #ifndef FALSE
445 #define FALSE 0
446 #endif
447
448 /*
449 * The Apple deprecation workaround macros below were adopted from the
450 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
451 */
452
453 #define XSTRINGIFY(x) #x
454
455 /*
456 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
457 */
458 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
459 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
460
461 /*
462 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
463 * thus we need to test the clang case before the GCC one
464 */
465 #if defined(__clang__)
466 # if (__clang_major__ * 100) + __clang_minor__ >= 208
467 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
468 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
469 # define DIAG_ON(x) DIAG_PRAGMA(pop)
470 # else
471 # define DIAG_OFF(x)
472 # define DIAG_ON(x)
473 # endif
474 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
475 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
476 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
477 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
478 # define DIAG_ON(x) DIAG_PRAGMA(pop)
479 # else
480 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
481 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
482 # endif
483 #else
484 # define DIAG_OFF(x)
485 # define DIAG_ON(x)
486 #endif
487
488 /* Use for clang specific warnings */
489 #ifdef __clang__
490 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
491 # define DIAG_ON_CLANG(x) DIAG_ON(x)
492 #else
493 # define DIAG_OFF_CLANG(x)
494 # define DIAG_ON_CLANG(x)
495 #endif
496
497 /*
498 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
499 */
500 #ifdef __APPLE__
501 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
502 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
503 #else
504 # define USES_APPLE_DEPRECATED_API
505 # define USES_APPLE_RST
506 #endif
507
508 /*
509 * end of Apple deprecation workaround macros
510 */
511
512 /*
513 * Statement attributes, for various compilers.
514 *
515 * This was introduced sufficiently recently that compilers implementing
516 * it also implement __has_attribute() (for example, GCC 5.0 and later
517 * have __has_attribute(), and the "fallthrough" attribute was introduced
518 * in GCC 7).
519 *
520 * Unfortunately, Clang does this wrong - a statement
521 *
522 * __attribute__ ((fallthrough));
523 *
524 * produces bogus -Wmissing-declaration "declaration does not declare
525 * anything" warnings (dear Clang: that's not a declaration, it's an
526 * empty statement). GCC, however, has no trouble with this.
527 */
528 #if __has_attribute(fallthrough) && !defined(__clang__)
529 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
530 #else
531 # define ND_FALL_THROUGH
532 #endif /* __has_attribute(fallthrough) */
533
534 #endif /* netdissect_stdinc_h */