]>
The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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.
39 #ifndef netdissect_stdinc_h
40 #define netdissect_stdinc_h
47 * Includes and definitions for Windows.
58 #include <sys/types.h>
61 #define uint8_t unsigned char
65 #define int8_t signed char
69 #define uint16_t unsigned short
73 #define int16_t signed short
77 #define uint32_t unsigned int
81 #define int32_t signed int
84 #ifdef _MSC_EXTENSIONS
87 #define uint64_t unsigned _int64
91 #define int64_t _int64
103 #define PRIu64 "I64u"
107 #define PRIx64 "I64x"
110 #else /* _MSC_EXTENSIONS */
113 #define uint64_t unsigned long long
117 #define int64_t long long
136 #endif /* _MSC_EXTENSIONS */
139 * Suppress definition of intN_t in bittypes.h, as included by <pcap/pcap.h>
141 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
142 * we check for u_intN_t in the UN*X configure script.)
144 #define HAVE_U_INT8_T
145 #define HAVE_U_INT16_T
146 #define HAVE_U_INT32_T
147 #define HAVE_U_INT64_T
155 #define O_RDONLY _O_RDONLY
156 #endif /* _MSC_VER */
159 * With MSVC, for C, __inline is used to make a function an inline.
162 #define inline __inline
166 #define HAVE_OS_IPV6_SUPPORT
169 #ifndef INET6_ADDRSTRLEN
170 #define INET6_ADDRSTRLEN 46
173 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
176 #define EAFNOSUPPORT WSAEAFNOSUPPORT
180 typedef char* caddr_t
;
183 #define MAXHOSTNAMELEN 64
184 #define snprintf _snprintf
185 #define vsnprintf _vsnprintf
186 #define RETSIGTYPE void
191 * Includes and definitions for various flavors of UN*X.
198 #include <inttypes.h>
202 #include <sys/param.h>
203 #include <sys/types.h> /* concession to AIX */
204 #include <sys/time.h>
205 #include <sys/socket.h>
206 #include <netinet/in.h>
208 #ifdef TIME_WITH_SYS_TIME
212 #include <arpa/inet.h>
216 #ifndef HAVE___ATTRIBUTE__
217 #define __attribute__(x)
221 * Used to declare a structure unaligned, so that the C compiler,
222 * if necessary, generates code that doesn't assume alignment.
223 * This is required because there is no guarantee that the packet
224 * data we get from libpcap/WinPcap is properly aligned.
226 * This assumes that, for all compilers that support __attribute__:
228 * 1) they support __attribute__((packed));
230 * 2) for all instruction set architectures requiring strict
231 * alignment, declaring a structure with that attribute
232 * causes the compiler to generate code that handles
233 * misaligned 2-byte, 4-byte, and 8-byte integral
236 * It does not (yet) handle compilers where you can get the compiler
237 * to generate code of that sort by some other means.
239 * This is required in order to, for example, keep the compiler from
242 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
244 * in print-bootp.c, code that loads the first 4-byte word of a
245 * "struct bootp", masking out the bp_hops field, and comparing the result
246 * against 0x01010600.
248 * Note: this also requires that padding be put into the structure,
249 * at least for compilers where it's implemented as __attribute__((packed)).
251 #if !(defined(_MSC_VER) && defined(UNALIGNED))
252 /* MSVC may have its own macro defined with the same name and purpose. */
254 #define UNALIGNED __attribute__((packed))
258 * fopen() read and write modes for text files and binary files.
260 #if defined(_WIN32) || defined(MSDOS)
261 #define FOPEN_READ_TXT "rt"
262 #define FOPEN_READ_BIN "rb"
263 #define FOPEN_WRITE_TXT "wt"
264 #define FOPEN_WRITE_BIN "wb"
266 #define FOPEN_READ_TXT "r"
267 #define FOPEN_READ_BIN FOPEN_READ_TXT
268 #define FOPEN_WRITE_TXT "w"
269 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
273 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
274 * defined if the OS doesn't provide them. These assume no more than
275 * an 80386, so, for example, it avoids the bswap instruction added in
278 * (We don't use them on OS X; Apple provides their own, which *doesn't*
279 * avoid the bswap instruction, as OS X only supports machines that
282 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
288 static __inline__
unsigned long __ntohl (unsigned long x
);
289 static __inline__
unsigned short __ntohs (unsigned short x
);
291 #define ntohl(x) __ntohl(x)
292 #define ntohs(x) __ntohs(x)
293 #define htonl(x) __ntohl(x)
294 #define htons(x) __ntohs(x)
296 static __inline__
unsigned long __ntohl (unsigned long x
)
298 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
299 "rorl $16, %0\n\t" /* swap words */
300 "xchgb %b0, %h0" /* swap higher bytes */
301 : "=q" (x
) : "0" (x
));
305 static __inline__
unsigned short __ntohs (unsigned short x
)
307 __asm__ ("xchgb %b0, %h0" /* swap bytes */
308 : "=q" (x
) : "0" (x
));
314 * If the OS doesn't define AF_INET6 and struct in6_addr:
316 * define AF_INET6, so we can use it internally as a "this is an
317 * IPv6 address" indication;
319 * define struct in6_addr so that we can use it for IPv6 addresses.
321 #ifndef HAVE_OS_IPV6_SUPPORT
326 __uint8_t __u6_addr8
[16];
327 __uint16_t __u6_addr16
[8];
328 __uint32_t __u6_addr32
[4];
329 } __u6_addr
; /* 128-bit IP6 address */
334 #define NI_MAXHOST 1025
337 #ifndef INET_ADDRSTRLEN
338 #define INET_ADDRSTRLEN 16
350 * The Apple deprecation workaround macros below were adopted from the
351 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
354 #define XSTRINGIFY(x) #x
357 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
359 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
360 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
362 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
363 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
364 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
365 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
366 # define DIAG_ON(x) DIAG_PRAGMA(pop)
368 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
369 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
371 #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
372 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
373 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
374 # define DIAG_ON(x) DIAG_PRAGMA(pop)
381 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
384 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
385 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
387 # define USES_APPLE_DEPRECATED_API
388 # define USES_APPLE_RST
392 * end of Apple deprecation workaround macros
396 #define min(a,b) ((a)>(b)?(b):(a))
399 #define max(a,b) ((b)>(a)?(b):(a))
402 #endif /* netdissect_stdinc_h */