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