]> The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
Get rid of obsolescent configure test.
[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 /*
45 * Get the C99 types, and the PRI[doux]64 format strings, defined.
46 */
47 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
48 /*
49 * We have pcap/pcap-inttypes.h; use that, as it'll do all the
50 * work, and won't cause problems if a file includes this file
51 * and later includes a pcap header file that also includes
52 * pcap/pcap-inttypes.h.
53 */
54 #include <pcap/pcap-inttypes.h>
55 #else
56 /*
57 * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
58 * do the work ourselves, but at least we don't have to
59 * worry about other headers including it and causing
60 * clashes.
61 */
62 #if defined(_MSC_VER)
63 /*
64 * Compiler is MSVC.
65 */
66 #if _MSC_VER >= 1800
67 /*
68 * VS 2013 or newer; we have <inttypes.h>.
69 */
70 #include <inttypes.h>
71 #else
72 /*
73 * Earlier VS; we have to define this stuff ourselves.
74 */
75 typedef unsigned char uint8_t;
76 typedef signed char int8_t;
77 typedef unsigned short uint16_t;
78 typedef signed short int16_t;
79 typedef unsigned int uint32_t;
80 typedef signed int int32_t;
81 #ifdef _MSC_EXTENSIONS
82 typedef unsigned _int64 uint64_t;
83 typedef _int64 int64_t;
84 #else /* _MSC_EXTENSIONS */
85 typedef unsigned long long uint64_t;
86 typedef long long int64_t;
87 #endif
88 #endif
89
90 /*
91 * Suppress definition of intN_t in bittypes.h, which might be included
92 * by <pcap/pcap.h> in older versions of WinPcap.
93 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
94 * we check for u_intN_t in the UN*X configure script.)
95 */
96 #define HAVE_U_INT8_T
97 #define HAVE_U_INT16_T
98 #define HAVE_U_INT32_T
99 #define HAVE_U_INT64_T
100
101 /*
102 * These may be defined by <inttypes.h>. If not, define them
103 * ourselves.
104 *
105 * XXX - for MSVC, we always want the _MSC_EXTENSIONS versions.
106 * What about other compilers? If, as the MinGW Web site says MinGW
107 * does, the other compilers just use Microsoft's run-time library,
108 * then they should probably use the _MSC_EXTENSIONS even if the
109 * compiler doesn't define _MSC_EXTENSIONS.
110 */
111 #ifndef PRId64
112 #ifdef _MSC_EXTENSIONS
113 #define PRId64 "I64d"
114 #else
115 #define PRId64 "lld"
116 #endif
117 #endif /* PRId64 */
118
119 #ifndef PRIo64
120 #ifdef _MSC_EXTENSIONS
121 #define PRIo64 "I64o"
122 #else
123 #define PRIo64 "llo"
124 #endif
125 #endif /* PRIo64 */
126
127 #ifndef PRIx64
128 #ifdef _MSC_EXTENSIONS
129 #define PRIx64 "I64x"
130 #else
131 #define PRIx64 "llx"
132 #endif
133 #endif
134
135 #ifndef PRIu64
136 #ifdef _MSC_EXTENSIONS
137 #define PRIu64 "I64u"
138 #else
139 #define PRIu64 "llu"
140 #endif
141 #endif
142 #elif defined(__MINGW32__) || !defined(_WIN32)
143 /*
144 * Compiler is MinGW or target is UN*X or MS-DOS. Just use
145 * <inttypes.h>.
146 */
147 #include <inttypes.h>
148 #endif
149 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
150
151 #ifdef _WIN32
152
153 /*
154 * Includes and definitions for Windows.
155 */
156
157 #include <stdio.h>
158 #include <winsock2.h>
159 #include <ws2tcpip.h>
160 #include <ctype.h>
161 #include <time.h>
162 #include <io.h>
163 #include <fcntl.h>
164 #include <sys/types.h>
165
166 #ifdef _MSC_VER
167 /*
168 * Compiler is MSVC.
169 *
170 * Microsoft's documentation doesn't speak of LL as a valid
171 * suffix for 64-bit integers, so we'll just use i64.
172 */
173 #define INT64_T_CONSTANT(constant) (constant##i64)
174 #else
175 /*
176 * Non-Microsoft compiler; assume LL works.
177 */
178 #define INT64_T_CONSTANT(constant) (constant##LL)
179 #endif
180
181 #ifdef _MSC_VER
182 #define stat _stat
183 #define open _open
184 #define fstat _fstat
185 #define read _read
186 #define close _close
187 #define O_RDONLY _O_RDONLY
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 #ifdef AF_INET6
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 #define snprintf _snprintf
217 #define vsnprintf _vsnprintf
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 LL works.
240 */
241 #define INT64_T_CONSTANT(constant) (constant##LL)
242
243 #endif /* _WIN32 */
244
245 #ifndef HAVE___ATTRIBUTE__
246 #define __attribute__(x)
247 #endif
248
249 /*
250 * Used to declare a structure unaligned, so that the C compiler,
251 * if necessary, generates code that doesn't assume alignment.
252 * This is required because there is no guarantee that the packet
253 * data we get from libpcap/WinPcap is properly aligned.
254 *
255 * This assumes that, for all compilers that support __attribute__:
256 *
257 * 1) they support __attribute__((packed));
258 *
259 * 2) for all instruction set architectures requiring strict
260 * alignment, declaring a structure with that attribute
261 * causes the compiler to generate code that handles
262 * misaligned 2-byte, 4-byte, and 8-byte integral
263 * quantities.
264 *
265 * It does not (yet) handle compilers where you can get the compiler
266 * to generate code of that sort by some other means.
267 *
268 * This is required in order to, for example, keep the compiler from
269 * generating, for
270 *
271 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
272 *
273 * in print-bootp.c, code that loads the first 4-byte word of a
274 * "struct bootp", masking out the bp_hops field, and comparing the result
275 * against 0x01010600.
276 *
277 * Note: this also requires that padding be put into the structure,
278 * at least for compilers where it's implemented as __attribute__((packed)).
279 */
280 #if !(defined(_MSC_VER) && defined(UNALIGNED))
281 /* MSVC may have its own macro defined with the same name and purpose. */
282 #undef UNALIGNED
283 #define UNALIGNED __attribute__((packed))
284 #endif
285
286 /*
287 * fopen() read and write modes for text files and binary files.
288 */
289 #if defined(_WIN32) || defined(MSDOS)
290 #define FOPEN_READ_TXT "rt"
291 #define FOPEN_READ_BIN "rb"
292 #define FOPEN_WRITE_TXT "wt"
293 #define FOPEN_WRITE_BIN "wb"
294 #else
295 #define FOPEN_READ_TXT "r"
296 #define FOPEN_READ_BIN FOPEN_READ_TXT
297 #define FOPEN_WRITE_TXT "w"
298 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
299 #endif
300
301 /*
302 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
303 * defined if the OS doesn't provide them. These assume no more than
304 * an 80386, so, for example, it avoids the bswap instruction added in
305 * the 80486.
306 *
307 * (We don't use them on OS X; Apple provides their own, which *doesn't*
308 * avoid the bswap instruction, as OS X only supports machines that
309 * have it.)
310 */
311 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
312 #undef ntohl
313 #undef ntohs
314 #undef htonl
315 #undef htons
316
317 static __inline__ unsigned long __ntohl (unsigned long x);
318 static __inline__ unsigned short __ntohs (unsigned short x);
319
320 #define ntohl(x) __ntohl(x)
321 #define ntohs(x) __ntohs(x)
322 #define htonl(x) __ntohl(x)
323 #define htons(x) __ntohs(x)
324
325 static __inline__ unsigned long __ntohl (unsigned long x)
326 {
327 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
328 "rorl $16, %0\n\t" /* swap words */
329 "xchgb %b0, %h0" /* swap higher bytes */
330 : "=q" (x) : "0" (x));
331 return (x);
332 }
333
334 static __inline__ unsigned short __ntohs (unsigned short x)
335 {
336 __asm__ ("xchgb %b0, %h0" /* swap bytes */
337 : "=q" (x) : "0" (x));
338 return (x);
339 }
340 #endif
341
342 /*
343 * If the OS doesn't define AF_INET6 and struct in6_addr:
344 *
345 * define AF_INET6, so we can use it internally as a "this is an
346 * IPv6 address" indication;
347 *
348 * define struct in6_addr so that we can use it for IPv6 addresses.
349 */
350 #ifndef HAVE_OS_IPV6_SUPPORT
351 #ifndef AF_INET6
352 #define AF_INET6 24
353
354 struct in6_addr {
355 union {
356 __uint8_t __u6_addr8[16];
357 __uint16_t __u6_addr16[8];
358 __uint32_t __u6_addr32[4];
359 } __u6_addr; /* 128-bit IP6 address */
360 };
361 #endif
362 #endif
363
364 #ifndef NI_MAXHOST
365 #define NI_MAXHOST 1025
366 #endif
367
368 #ifndef INET_ADDRSTRLEN
369 #define INET_ADDRSTRLEN 16
370 #endif
371
372 #ifndef TRUE
373 #define TRUE 1
374 #endif
375
376 #ifndef FALSE
377 #define FALSE 0
378 #endif
379
380 /*
381 * The Apple deprecation workaround macros below were adopted from the
382 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
383 */
384
385 #define XSTRINGIFY(x) #x
386
387 /*
388 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
389 */
390 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
391 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
392
393 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
394 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
395 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
396 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
397 # define DIAG_ON(x) DIAG_PRAGMA(pop)
398 # else
399 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
400 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
401 # endif
402 #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
403 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
404 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
405 # define DIAG_ON(x) DIAG_PRAGMA(pop)
406 #else
407 # define DIAG_OFF(x)
408 # define DIAG_ON(x)
409 #endif
410
411 /*
412 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
413 */
414 #ifdef __APPLE__
415 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
416 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
417 #else
418 # define USES_APPLE_DEPRECATED_API
419 # define USES_APPLE_RST
420 #endif
421
422 /*
423 * end of Apple deprecation workaround macros
424 */
425
426 /*
427 * Function attributes, for various compilers.
428 */
429 #include "funcattrs.h"
430
431 #ifndef min
432 #define min(a,b) ((a)>(b)?(b):(a))
433 #endif
434 #ifndef max
435 #define max(a,b) ((b)>(a)?(b):(a))
436 #endif
437
438 #ifdef __ATTRIBUTE___FALLTHROUGH_OK
439 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
440 #else
441 # define ND_FALL_THROUGH
442 #endif /* __ATTRIBUTE___FALLTHROUGH_OK */
443
444 #endif /* netdissect_stdinc_h */