]> The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
remove redundant ND_TCHECK, let GET_ routines handle checks
[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 * If we're compiling with Visual Studio, make sure we have at least
50 * VS 2015 or later, so we have sufficient C99 support.
51 *
52 * XXX - verify that we have at least C99 support on UN*Xes?
53 *
54 * What about MinGW or various DOS toolchains? We're currently assuming
55 * sufficient C99 support there.
56 */
57 #if defined(_MSC_VER)
58 /*
59 * Make sure we have VS 2015 or later.
60 */
61 #if _MSC_VER < 1900
62 #error "Building tcpdump requires VS 2015 or later"
63 #endif
64 #endif
65
66 /*
67 * Get the C99 types, and the PRI[doux]64 format strings, defined.
68 */
69 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
70 /*
71 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
72 * work, and won't cause problems if a file includes this file
73 * and later includes a pcap header file that also includes
74 * pcap/pcap-inttypes.h.
75 */
76 #include <pcap/pcap-inttypes.h>
77 #else
78 /*
79 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
80 * do the work ourselves, but at least we don't have to
81 * worry about other headers including it and causing
82 * clashes.
83 */
84
85 /*
86 * Include <inttypes.h> to get the integer types and PRi[doux]64 values
87 * defined.
88 *
89 * If the compiler is MSVC, we require VS 2015 or newer, so we
90 * have <inttypes.h> - and support for %zu in the formatted
91 * printing functions.
92 *
93 * If the compiler is MinGW, we assume we have <inttypes.h> - and
94 * support for %zu in the formatted printing functions.
95 *
96 * If the target is UN*X, we assume we have a C99-or-later development
97 * environment, and thus have <inttypes.h> - and support for %zu in
98 * the formatted printing functions.
99 *
100 * If the target is MS-DOS, we assume we have <inttypes.h> - and support
101 * for %zu in the formatted printing functions.
102 */
103 #include <inttypes.h>
104
105 #if defined(_MSC_VER)
106 /*
107 * Suppress definition of intN_t in bittypes.h, which might be included
108 * by <pcap/pcap.h> in older versions of WinPcap.
109 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented.)
110 */
111 #define HAVE_U_INT8_T
112 #define HAVE_U_INT16_T
113 #define HAVE_U_INT32_T
114 #define HAVE_U_INT64_T
115 #endif
116 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
117
118 #ifdef _WIN32
119
120 /*
121 * Includes and definitions for Windows.
122 */
123
124 #include <stdio.h>
125 #include <winsock2.h>
126 #include <ws2tcpip.h>
127 #include <time.h>
128 #include <io.h>
129 #include <fcntl.h>
130 #include <sys/types.h>
131
132 #ifdef _MSC_VER
133 /*
134 * Compiler is MSVC.
135 *
136 * We require VS 2015 or newer, so we have strtoll(). Use that for
137 * strtoint64_t().
138 */
139 #define strtoint64_t strtoll
140
141 /*
142 * And we have LL as a suffix for constants, so use that.
143 */
144 #define INT64_T_CONSTANT(constant) (constant##LL)
145 #else
146 /*
147 * Non-Microsoft compiler.
148 *
149 * XXX - should we use strtoll or should we use _strtoi64()?
150 */
151 #define strtoint64_t strtoll
152
153 /*
154 * Assume LL works.
155 */
156 #define INT64_T_CONSTANT(constant) (constant##LL)
157 #endif
158
159 #ifdef _MSC_VER
160 /*
161 * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
162 * by adding a preceding underscore; we *want* the UN*Xisms, so add
163 * #defines to let us use them.
164 */
165 #define isatty _isatty
166 #define stat _stat
167 #define strdup _strdup
168 #define open _open
169 #define fstat _fstat
170 #define read _read
171 #define close _close
172 #define O_RDONLY _O_RDONLY
173
174 /*
175 * If <crtdbg.h> has been included, and _DEBUG is defined, and
176 * __STDC__ is zero, <crtdbg.h> will define strdup() to call
177 * _strdup_dbg(). So if it's already defined, don't redefine
178 * it.
179 */
180 #ifndef strdup
181 #define strdup _strdup
182 #endif
183
184 /*
185 * Windows doesn't have ssize_t; routines such as _read() return int.
186 */
187 typedef int ssize_t;
188 #endif /* _MSC_VER */
189
190 /*
191 * With MSVC, for C, __inline is used to make a function an inline.
192 */
193 #ifdef _MSC_VER
194 #define inline __inline
195 #endif
196
197 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
198 #define HAVE_OS_IPV6_SUPPORT
199 #endif
200
201 #ifndef INET6_ADDRSTRLEN
202 #define INET6_ADDRSTRLEN 46
203 #endif
204
205 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
206 */
207 #ifndef EAFNOSUPPORT
208 #define EAFNOSUPPORT WSAEAFNOSUPPORT
209 #endif
210
211 #ifndef caddr_t
212 typedef char* caddr_t;
213 #endif /* caddr_t */
214
215 #define MAXHOSTNAMELEN 64
216
217 #else /* _WIN32 */
218
219 /*
220 * Includes and definitions for various flavors of UN*X.
221 */
222
223 #include <unistd.h>
224 #include <netdb.h>
225 #include <sys/param.h>
226 #include <sys/types.h> /* concession to AIX */
227 #include <sys/time.h>
228 #include <sys/socket.h>
229 #include <netinet/in.h>
230
231 #include <time.h>
232
233 #include <arpa/inet.h>
234
235 /*
236 * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
237 */
238 #define strtoint64_t strtoll
239
240 /*
241 * Assume LL works.
242 */
243 #define INT64_T_CONSTANT(constant) (constant##LL)
244
245 #endif /* _WIN32 */
246
247 /*
248 * Function attributes, for various compilers.
249 */
250 #include "funcattrs.h"
251
252 /*
253 * fopen() read and write modes for text files and binary files.
254 */
255 #if defined(_WIN32) || defined(MSDOS)
256 #define FOPEN_READ_TXT "rt"
257 #define FOPEN_READ_BIN "rb"
258 #define FOPEN_WRITE_TXT "wt"
259 #define FOPEN_WRITE_BIN "wb"
260 #else
261 #define FOPEN_READ_TXT "r"
262 #define FOPEN_READ_BIN FOPEN_READ_TXT
263 #define FOPEN_WRITE_TXT "w"
264 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
265 #endif
266
267 /*
268 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
269 * defined if the OS doesn't provide them. These assume no more than
270 * an 80386, so, for example, it avoids the bswap instruction added in
271 * the 80486.
272 *
273 * (We don't use them on macOS; Apple provides their own, which *doesn't*
274 * avoid the bswap instruction, as macOS only supports machines that
275 * have it.)
276 */
277 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
278 #undef ntohl
279 #undef ntohs
280 #undef htonl
281 #undef htons
282
283 static __inline__ unsigned long __ntohl (unsigned long x);
284 static __inline__ unsigned short __ntohs (unsigned short x);
285
286 #define ntohl(x) __ntohl(x)
287 #define ntohs(x) __ntohs(x)
288 #define htonl(x) __ntohl(x)
289 #define htons(x) __ntohs(x)
290
291 static __inline__ unsigned long __ntohl (unsigned long x)
292 {
293 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
294 "rorl $16, %0\n\t" /* swap words */
295 "xchgb %b0, %h0" /* swap higher bytes */
296 : "=q" (x) : "0" (x));
297 return (x);
298 }
299
300 static __inline__ unsigned short __ntohs (unsigned short x)
301 {
302 __asm__ ("xchgb %b0, %h0" /* swap bytes */
303 : "=q" (x) : "0" (x));
304 return (x);
305 }
306 #endif
307
308 /*
309 * If the OS doesn't define AF_INET6 and struct in6_addr:
310 *
311 * define AF_INET6, so we can use it internally as a "this is an
312 * IPv6 address" indication;
313 *
314 * define struct in6_addr so that we can use it for IPv6 addresses.
315 */
316 #ifndef HAVE_OS_IPV6_SUPPORT
317 #ifndef AF_INET6
318 #define AF_INET6 24
319
320 struct in6_addr {
321 union {
322 __uint8_t __u6_addr8[16];
323 __uint16_t __u6_addr16[8];
324 __uint32_t __u6_addr32[4];
325 } __u6_addr; /* 128-bit IP6 address */
326 };
327 #endif
328 #endif
329
330 #ifndef NI_MAXHOST
331 #define NI_MAXHOST 1025
332 #endif
333
334 #ifndef INET_ADDRSTRLEN
335 #define INET_ADDRSTRLEN 16
336 #endif
337
338 #ifndef TRUE
339 #define TRUE 1
340 #endif
341
342 #ifndef FALSE
343 #define FALSE 0
344 #endif
345
346 /*
347 * The Apple deprecation workaround macros below were adopted from the
348 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
349 */
350
351 #define XSTRINGIFY(x) #x
352
353 /*
354 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
355 */
356 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
357 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
358
359 /*
360 * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
361 * thus we need to test the clang case before the GCC one
362 */
363 #if defined(__clang__)
364 # if (__clang_major__ * 100) + __clang_minor__ >= 208
365 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
366 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
367 # define DIAG_ON(x) DIAG_PRAGMA(pop)
368 # else
369 # define DIAG_OFF(x)
370 # define DIAG_ON(x)
371 # endif
372 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
373 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
374 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
375 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
376 # define DIAG_ON(x) DIAG_PRAGMA(pop)
377 # else
378 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
379 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
380 # endif
381 #else
382 # define DIAG_OFF(x)
383 # define DIAG_ON(x)
384 #endif
385
386 /* Use for clang specific warnings */
387 #ifdef __clang__
388 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
389 # define DIAG_ON_CLANG(x) DIAG_ON(x)
390 #else
391 # define DIAG_OFF_CLANG(x)
392 # define DIAG_ON_CLANG(x)
393 #endif
394
395 /*
396 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
397 */
398 #ifdef __APPLE__
399 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
400 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
401 #else
402 # define USES_APPLE_DEPRECATED_API
403 # define USES_APPLE_RST
404 #endif
405
406 /*
407 * end of Apple deprecation workaround macros
408 */
409
410 /*
411 * Statement attributes, for various compilers.
412 *
413 * This was introduced sufficiently recently that compilers implementing
414 * it also implement __has_attribute() (for example, GCC 5.0 and later
415 * have __has_attribute(), and the "fallthrough" attribute was introduced
416 * in GCC 7).
417 *
418 * Unfortunately, Clang does this wrong - a statement
419 *
420 * __attribute__ ((fallthrough));
421 *
422 * produces bogus -Wmissing-declaration "declaration does not declare
423 * anything" warnings (dear Clang: that's not a declaration, it's an
424 * empty statement). GCC, however, has no trouble with this.
425 */
426 #if __has_attribute(fallthrough) && !defined(__clang__)
427 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
428 #else
429 # define ND_FALL_THROUGH
430 #endif /* __has_attribute(fallthrough) */
431
432 #endif /* netdissect_stdinc_h */