]>
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
154 #define O_RDONLY _O_RDONLY
157 * We define our_fstat64 as _fstati64, and define our_statb as
158 * struct _stati64, so we get 64-bit file sizes.
160 #define our_fstat _fstati64
161 #define our_statb struct _stati64
163 #endif /* _MSC_VER */
166 * With MSVC, for C, __inline is used to make a function an inline.
169 #define inline __inline
173 #define HAVE_OS_IPV6_SUPPORT
176 #ifndef INET6_ADDRSTRLEN
177 #define INET6_ADDRSTRLEN 46
180 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
183 #define EAFNOSUPPORT WSAEAFNOSUPPORT
187 typedef char* caddr_t
;
190 #define MAXHOSTNAMELEN 64
191 #define snprintf _snprintf
192 #define vsnprintf _vsnprintf
193 #define RETSIGTYPE void
198 * Includes and definitions for various flavors of UN*X.
205 #include <inttypes.h>
209 #include <sys/param.h>
210 #include <sys/types.h> /* concession to AIX */
211 #include <sys/time.h>
212 #include <sys/socket.h>
213 #include <netinet/in.h>
215 #ifdef TIME_WITH_SYS_TIME
219 #include <arpa/inet.h>
222 * We should have large file support enabled, if it's available,
223 * so just use fstat as our_fstat and struct stat as our_statb.
225 #define our_fstat fstat
226 #define our_statb struct stat
230 #ifndef HAVE___ATTRIBUTE__
231 #define __attribute__(x)
235 * Used to declare a structure unaligned, so that the C compiler,
236 * if necessary, generates code that doesn't assume alignment.
237 * This is required because there is no guarantee that the packet
238 * data we get from libpcap/WinPcap is properly aligned.
240 * This assumes that, for all compilers that support __attribute__:
242 * 1) they support __attribute__((packed));
244 * 2) for all instruction set architectures requiring strict
245 * alignment, declaring a structure with that attribute
246 * causes the compiler to generate code that handles
247 * misaligned 2-byte, 4-byte, and 8-byte integral
250 * It does not (yet) handle compilers where you can get the compiler
251 * to generate code of that sort by some other means.
253 * This is required in order to, for example, keep the compiler from
256 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
258 * in print-bootp.c, code that loads the first 4-byte word of a
259 * "struct bootp", masking out the bp_hops field, and comparing the result
260 * against 0x01010600.
262 * Note: this also requires that padding be put into the structure,
263 * at least for compilers where it's implemented as __attribute__((packed)).
265 #if !(defined(_MSC_VER) && defined(UNALIGNED))
266 /* MSVC may have its own macro defined with the same name and purpose. */
268 #define UNALIGNED __attribute__((packed))
272 * fopen() read and write modes for text files and binary files.
274 #if defined(_WIN32) || defined(MSDOS)
275 #define FOPEN_READ_TXT "rt"
276 #define FOPEN_READ_BIN "rb"
277 #define FOPEN_WRITE_TXT "wt"
278 #define FOPEN_WRITE_BIN "wb"
280 #define FOPEN_READ_TXT "r"
281 #define FOPEN_READ_BIN FOPEN_READ_TXT
282 #define FOPEN_WRITE_TXT "w"
283 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
287 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
288 * defined if the OS doesn't provide them. These assume no more than
289 * an 80386, so, for example, it avoids the bswap instruction added in
292 * (We don't use them on OS X; Apple provides their own, which *doesn't*
293 * avoid the bswap instruction, as OS X only supports machines that
296 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
302 static __inline__
unsigned long __ntohl (unsigned long x
);
303 static __inline__
unsigned short __ntohs (unsigned short x
);
305 #define ntohl(x) __ntohl(x)
306 #define ntohs(x) __ntohs(x)
307 #define htonl(x) __ntohl(x)
308 #define htons(x) __ntohs(x)
310 static __inline__
unsigned long __ntohl (unsigned long x
)
312 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
313 "rorl $16, %0\n\t" /* swap words */
314 "xchgb %b0, %h0" /* swap higher bytes */
315 : "=q" (x
) : "0" (x
));
319 static __inline__
unsigned short __ntohs (unsigned short x
)
321 __asm__ ("xchgb %b0, %h0" /* swap bytes */
322 : "=q" (x
) : "0" (x
));
328 * If the OS doesn't define AF_INET6 and struct in6_addr:
330 * define AF_INET6, so we can use it internally as a "this is an
331 * IPv6 address" indication;
333 * define struct in6_addr so that we can use it for IPv6 addresses.
335 #ifndef HAVE_OS_IPV6_SUPPORT
341 __uint8_t __u6_addr8
[16];
342 __uint16_t __u6_addr16
[8];
343 __uint32_t __u6_addr32
[4];
344 } __u6_addr
; /* 128-bit IP6 address */
350 #define NI_MAXHOST 1025
353 #ifndef INET_ADDRSTRLEN
354 #define INET_ADDRSTRLEN 16
366 * The Apple deprecation workaround macros below were adopted from the
367 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
370 #define XSTRINGIFY(x) #x
373 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
375 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
376 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
378 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
379 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
380 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
381 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
382 # define DIAG_ON(x) DIAG_PRAGMA(pop)
384 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
385 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
387 #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
388 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
389 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
390 # define DIAG_ON(x) DIAG_PRAGMA(pop)
397 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
400 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
401 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
403 # define USES_APPLE_DEPRECATED_API
404 # define USES_APPLE_RST
408 * end of Apple deprecation workaround macros
412 * Function attributes, for various compilers.
414 #include "funcattrs.h"
417 #define min(a,b) ((a)>(b)?(b):(a))
420 #define max(a,b) ((b)>(a)?(b):(a))
423 #endif /* netdissect_stdinc_h */