]> The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
Another UN*Xism we need - isascii().
[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 #elif defined(__MINGW32__) || !defined(_WIN32)
152 /*
153 * Compiler is MinGW or target is UN*X or MS-DOS. Just use
154 * <inttypes.h>.
155 */
156 #include <inttypes.h>
157 #endif
158 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
159
160 #ifdef _WIN32
161
162 /*
163 * Includes and definitions for Windows.
164 */
165
166 #include <stdio.h>
167 #include <winsock2.h>
168 #include <ws2tcpip.h>
169 #include <ctype.h>
170 #include <time.h>
171 #include <io.h>
172 #include <fcntl.h>
173 #include <sys/types.h>
174
175 #ifdef _MSC_VER
176 /*
177 * Compiler is MSVC.
178 */
179 #if _MSC_VER >= 1800
180 /*
181 * VS 2013 or newer; we have strtoll(). Use that for strtoint64_t().
182 */
183 #define strtoint64_t strtoll
184 #else
185 /*
186 * Earlier VS; we don't have strtoll(), but we do have
187 * _strtoi64(). Use that for strtoint64_t().
188 */
189 #define strtoint64_t _strtoi64
190 #endif
191
192 /*
193 * Microsoft's documentation doesn't speak of LL as a valid
194 * suffix for 64-bit integers, so we'll just use i64.
195 */
196 #define INT64_T_CONSTANT(constant) (constant##i64)
197 #else
198 /*
199 * Non-Microsoft compiler.
200 *
201 * XXX - should we use strtoll or should we use _strtoi64()?
202 */
203 #define strtoint64_t strtoll
204
205 /*
206 * Assume LL works.
207 */
208 #define INT64_T_CONSTANT(constant) (constant##LL)
209 #endif
210
211 #ifdef _MSC_VER
212 /*
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.
216 */
217 #define isascii __isascii
218 #define stat _stat
219 #define strdup _strdup
220 #define open _open
221 #define fstat _fstat
222 #define read _read
223 #define close _close
224 #define O_RDONLY _O_RDONLY
225
226 /*
227 * If <crtdbg.h> has been included, and _DEBUG is defined, and
228 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
229 * _strdup_dbg(). So if it's already defined, don't redefine
230 * it.
231 */
232 #ifndef strdup
233 #define strdup _strdup
234 #endif
235 #endif /* _MSC_VER */
236
237 /*
238 * With MSVC, for C, __inline is used to make a function an inline.
239 */
240 #ifdef _MSC_VER
241 #define inline __inline
242 #endif
243
244 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
245 #define HAVE_OS_IPV6_SUPPORT
246 #endif
247
248 #ifndef INET6_ADDRSTRLEN
249 #define INET6_ADDRSTRLEN 46
250 #endif
251
252 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
253 */
254 #ifndef EAFNOSUPPORT
255 #define EAFNOSUPPORT WSAEAFNOSUPPORT
256 #endif
257
258 #ifndef caddr_t
259 typedef char* caddr_t;
260 #endif /* caddr_t */
261
262 #define MAXHOSTNAMELEN 64
263 #define snprintf _snprintf
264 #define vsnprintf _vsnprintf
265
266 #else /* _WIN32 */
267
268 /*
269 * Includes and definitions for various flavors of UN*X.
270 */
271
272 #include <ctype.h>
273 #include <unistd.h>
274 #include <netdb.h>
275 #include <sys/param.h>
276 #include <sys/types.h> /* concession to AIX */
277 #include <sys/time.h>
278 #include <sys/socket.h>
279 #include <netinet/in.h>
280
281 #include <time.h>
282
283 #include <arpa/inet.h>
284
285 /*
286 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
287 */
288 #define strtoint64_t strtoll
289
290 /*
291 * Assume LL works.
292 */
293 #define INT64_T_CONSTANT(constant) (constant##LL)
294
295 #endif /* _WIN32 */
296
297 /*
298 * Function attributes, for various compilers.
299 */
300 #include "funcattrs.h"
301
302 /*
303 * On Windows, snprintf(), with that name and with C99 behavior - i.e.,
304 * guaranteeing that the formatted string is null-terminated - didn't
305 * appear until Visual Studio 2015. Prior to that, the C runtime had
306 * only _snprintf(), which *doesn't* guarantee that the string is
307 * null-terminated if it is truncated due to the buffer being too
308 * small. We therefore can't just define snprintf to be _snprintf
309 * and define vsnprintf to be _vsnprintf, as we're relying on null-
310 * termination of strings in all cases.
311 *
312 * We also want to allow this to be built with versions of Visual Studio
313 * prior to VS 2015, so we can't rely on snprintf() being present.
314 *
315 * And, if there are any UN*Xes out there on which we can run that
316 * don't have snprintf() or don't have vsnprintf(), we define our
317 * own as well.
318 */
319 #if !defined(HAVE_SNPRINTF)
320 int snprintf (char *str, size_t sz, FORMAT_STRING(const char *format), ...)
321 PRINTFLIKE(3, 4);
322 #endif /* !defined(HAVE_SNPRINTF) */
323
324 #if !defined(HAVE_VSNPRINTF)
325 int vsnprintf (char *str, size_t sz, FORMAT_STRING(const char *format),
326 va_list ap) PRINTFLIKE(3, 0);
327 #endif /* !defined(HAVE_VSNPRINTF) */
328
329 /*
330 * fopen() read and write modes for text files and binary files.
331 */
332 #if defined(_WIN32) || defined(MSDOS)
333 #define FOPEN_READ_TXT "rt"
334 #define FOPEN_READ_BIN "rb"
335 #define FOPEN_WRITE_TXT "wt"
336 #define FOPEN_WRITE_BIN "wb"
337 #else
338 #define FOPEN_READ_TXT "r"
339 #define FOPEN_READ_BIN FOPEN_READ_TXT
340 #define FOPEN_WRITE_TXT "w"
341 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
342 #endif
343
344 /*
345 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
346 * defined if the OS doesn't provide them. These assume no more than
347 * an 80386, so, for example, it avoids the bswap instruction added in
348 * the 80486.
349 *
350 * (We don't use them on macOS; Apple provides their own, which *doesn't*
351 * avoid the bswap instruction, as macOS only supports machines that
352 * have it.)
353 */
354 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
355 #undef ntohl
356 #undef ntohs
357 #undef htonl
358 #undef htons
359
360 static __inline__ unsigned long __ntohl (unsigned long x);
361 static __inline__ unsigned short __ntohs (unsigned short x);
362
363 #define ntohl(x) __ntohl(x)
364 #define ntohs(x) __ntohs(x)
365 #define htonl(x) __ntohl(x)
366 #define htons(x) __ntohs(x)
367
368 static __inline__ unsigned long __ntohl (unsigned long x)
369 {
370 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
371 "rorl $16, %0\n\t" /* swap words */
372 "xchgb %b0, %h0" /* swap higher bytes */
373 : "=q" (x) : "0" (x));
374 return (x);
375 }
376
377 static __inline__ unsigned short __ntohs (unsigned short x)
378 {
379 __asm__ ("xchgb %b0, %h0" /* swap bytes */
380 : "=q" (x) : "0" (x));
381 return (x);
382 }
383 #endif
384
385 /*
386 * If the OS doesn't define AF_INET6 and struct in6_addr:
387 *
388 * define AF_INET6, so we can use it internally as a "this is an
389 * IPv6 address" indication;
390 *
391 * define struct in6_addr so that we can use it for IPv6 addresses.
392 */
393 #ifndef HAVE_OS_IPV6_SUPPORT
394 #ifndef AF_INET6
395 #define AF_INET6 24
396
397 struct in6_addr {
398 union {
399 __uint8_t __u6_addr8[16];
400 __uint16_t __u6_addr16[8];
401 __uint32_t __u6_addr32[4];
402 } __u6_addr; /* 128-bit IP6 address */
403 };
404 #endif
405 #endif
406
407 #ifndef NI_MAXHOST
408 #define NI_MAXHOST 1025
409 #endif
410
411 #ifndef INET_ADDRSTRLEN
412 #define INET_ADDRSTRLEN 16
413 #endif
414
415 #ifndef TRUE
416 #define TRUE 1
417 #endif
418
419 #ifndef FALSE
420 #define FALSE 0
421 #endif
422
423 /*
424 * The Apple deprecation workaround macros below were adopted from the
425 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
426 */
427
428 #define XSTRINGIFY(x) #x
429
430 /*
431 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
432 */
433 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
434 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
435
436 /*
437 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
438 * thus we need to test the clang case before the GCC one
439 */
440 #if defined(__clang__)
441 # if (__clang_major__ * 100) + __clang_minor__ >= 208
442 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
443 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
444 # define DIAG_ON(x) DIAG_PRAGMA(pop)
445 # else
446 # define DIAG_OFF(x)
447 # define DIAG_ON(x)
448 # endif
449 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
450 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
451 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
452 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
453 # define DIAG_ON(x) DIAG_PRAGMA(pop)
454 # else
455 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
456 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
457 # endif
458 #else
459 # define DIAG_OFF(x)
460 # define DIAG_ON(x)
461 #endif
462
463 /* Use for clang specific warnings */
464 #ifdef __clang__
465 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
466 # define DIAG_ON_CLANG(x) DIAG_ON(x)
467 #else
468 # define DIAG_OFF_CLANG(x)
469 # define DIAG_ON_CLANG(x)
470 #endif
471
472 /*
473 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
474 */
475 #ifdef __APPLE__
476 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
477 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
478 #else
479 # define USES_APPLE_DEPRECATED_API
480 # define USES_APPLE_RST
481 #endif
482
483 /*
484 * end of Apple deprecation workaround macros
485 */
486
487 /*
488 * Statement attributes, for various compilers.
489 *
490 * This was introduced sufficiently recently that compilers implementing
491 * it also implement __has_attribute() (for example, GCC 5.0 and later
492 * have __has_attribute(), and the "fallthrough" attribute was introduced
493 * in GCC 7).
494 */
495 #if __has_attribute(fallthrough)
496 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
497 #else
498 # define ND_FALL_THROUGH
499 #endif /* __has_attribute(fallthrough) */
500
501 #endif /* netdissect_stdinc_h */