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