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