]> The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
Update Visual Studio files.
[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 #ifdef _WIN32
45
46 /*
47 * Includes and definitions for Windows.
48 */
49
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <winsock2.h>
53 #include <ws2tcpip.h>
54 #include "bittypes.h" /* in wpcap's Win32/include */
55 #include <ctype.h>
56 #include <time.h>
57 #include <io.h>
58 #include <fcntl.h>
59 #include <sys/types.h>
60 #include <net/netdb.h> /* in wpcap's Win32/include */
61
62 #ifndef uint8_t
63 #define uint8_t unsigned char
64 #endif
65
66 #ifndef int8_t
67 #define int8_t signed char
68 #endif
69
70 #ifndef uint16_t
71 #define uint16_t unsigned short
72 #endif
73
74 #ifndef int16_t
75 #define int16_t signed short
76 #endif
77
78 #ifndef uint32_t
79 #define uint32_t unsigned int
80 #endif
81
82 #ifndef int32_t
83 #define int32_t signed int
84 #endif
85
86 #ifdef _MSC_EXTENSIONS
87
88 #ifndef uint64_t
89 #define uint64_t unsigned _int64
90 #endif
91
92 #ifndef int64_t
93 #define int64_t _int64
94 #endif
95
96 #ifndef PRId64
97 #define PRId64 "I64d"
98 #endif
99
100 #ifndef PRIo64
101 #define PRIo64 "I64o"
102 #endif
103
104 #ifndef PRIu64
105 #define PRIu64 "I64u"
106 #endif
107
108 #ifndef PRIx64
109 #define PRIx64 "I64x"
110 #endif
111
112 #else /* _MSC_EXTENSIONS */
113
114 #ifndef uint64_t
115 #define uint64_t unsigned long long
116 #endif
117
118 #ifndef int64_t
119 #define int64_t long long
120 #endif
121
122 #ifndef PRId64
123 #define PRId64 "lld"
124 #endif
125
126 #ifndef PRIo64
127 #define PRIo64 "llo"
128 #endif
129
130 #ifndef PRIu64
131 #define PRIu64 "llu"
132 #endif
133
134 #ifndef PRIx64
135 #define PRIx64 "llx"
136 #endif
137
138 #endif /* _MSC_EXTENSIONS */
139
140 #ifdef _MSC_VER
141 #define stat _stat
142 #define open _open
143 #define fstat _fstat
144 #define read _read
145 #define close _close
146 #define O_RDONLY _O_RDONLY
147 #endif /* _MSC_VER */
148
149 extern int inet_aton (const char *cp, struct in_addr *addr);
150
151 /*
152 * With MSVC, for C, __inline is used to make a function an inline.
153 */
154 #ifdef _MSC_VER
155 #define inline __inline
156 #endif
157
158 #ifndef INET6_ADDRSTRLEN
159 #define INET6_ADDRSTRLEN 46
160 #endif
161
162 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
163 */
164 #ifndef EAFNOSUPPORT
165 #define EAFNOSUPPORT WSAEAFNOSUPPORT
166 #endif
167
168 #ifndef caddr_t
169 typedef char* caddr_t;
170 #endif /* caddr_t */
171
172 #define MAXHOSTNAMELEN 64
173 #define snprintf _snprintf
174 #define vsnprintf _vsnprintf
175 #define RETSIGTYPE void
176
177 #else /* _WIN32 */
178
179 /*
180 * Includes and definitions for various flavors of UN*X.
181 */
182
183 #include <ctype.h>
184 #include <unistd.h>
185 #include <netdb.h>
186 #if HAVE_INTTYPES_H
187 #include <inttypes.h>
188 #elif HAVE_STDINT_H
189 #include <stdint.h>
190 #endif
191 #include <sys/param.h>
192 #include <sys/types.h> /* concession to AIX */
193 #include <sys/time.h>
194 #include <sys/socket.h>
195 #include <netinet/in.h>
196
197 #ifdef TIME_WITH_SYS_TIME
198 #include <time.h>
199 #endif
200
201 #include <arpa/inet.h>
202
203 #endif /* _WIN32 */
204
205 #ifndef HAVE___ATTRIBUTE__
206 #define __attribute__(x)
207 #endif
208
209 /*
210 * Used to declare a structure unaligned, so that the C compiler,
211 * if necessary, generates code that doesn't assume alignment.
212 * This is required because there is no guarantee that the packet
213 * data we get from libpcap/WinPcap is properly aligned.
214 *
215 * This assumes that, for all compilers that support __attribute__:
216 *
217 * 1) they support __attribute__((packed));
218 *
219 * 2) for all instruction set architectures requiring strict
220 * alignment, declaring a structure with that attribute
221 * causes the compiler to generate code that handles
222 * misaligned 2-byte, 4-byte, and 8-byte integral
223 * quantities.
224 *
225 * It does not (yet) handle compilers where you can get the compiler
226 * to generate code of that sort by some other means.
227 *
228 * This is required in order to, for example, keep the compiler from
229 * generating, for
230 *
231 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
232 *
233 * in print-bootp.c, code that loads the first 4-byte word of a
234 * "struct bootp", masking out the bp_hops field, and comparing the result
235 * against 0x01010600.
236 *
237 * Note: this also requires that padding be put into the structure,
238 * at least for compilers where it's implemented as __attribute__((packed)).
239 */
240 #if !(defined(_MSC_VER) && defined(UNALIGNED))
241 /* MSVC may have its own macro defined with the same name and purpose. */
242 #undef UNALIGNED
243 #define UNALIGNED __attribute__((packed))
244 #endif
245
246 /*
247 * fopen() read and write modes for text files and binary files.
248 */
249 #if defined(_WIN32) || defined(MSDOS)
250 #define FOPEN_READ_TXT "rt"
251 #define FOPEN_READ_BIN "rb"
252 #define FOPEN_WRITE_TXT "wt"
253 #define FOPEN_WRITE_BIN "wb"
254 #else
255 #define FOPEN_READ_TXT "r"
256 #define FOPEN_READ_BIN FOPEN_READ_TXT
257 #define FOPEN_WRITE_TXT "w"
258 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
259 #endif
260
261 /*
262 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
263 * defined if the OS doesn't provide them. These assume no more than
264 * an 80386, so, for example, it avoids the bswap instruction added in
265 * the 80486.
266 *
267 * (We don't use them on OS X; Apple provides their own, which *doesn't*
268 * avoid the bswap instruction, as OS X only supports machines that
269 * have it.)
270 */
271 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
272 #undef ntohl
273 #undef ntohs
274 #undef htonl
275 #undef htons
276
277 static __inline__ unsigned long __ntohl (unsigned long x);
278 static __inline__ unsigned short __ntohs (unsigned short x);
279
280 #define ntohl(x) __ntohl(x)
281 #define ntohs(x) __ntohs(x)
282 #define htonl(x) __ntohl(x)
283 #define htons(x) __ntohs(x)
284
285 static __inline__ unsigned long __ntohl (unsigned long x)
286 {
287 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
288 "rorl $16, %0\n\t" /* swap words */
289 "xchgb %b0, %h0" /* swap higher bytes */
290 : "=q" (x) : "0" (x));
291 return (x);
292 }
293
294 static __inline__ unsigned short __ntohs (unsigned short x)
295 {
296 __asm__ ("xchgb %b0, %h0" /* swap bytes */
297 : "=q" (x) : "0" (x));
298 return (x);
299 }
300 #endif
301
302 /*
303 * If the OS doesn't define AF_INET6 and struct in6_addr:
304 *
305 * define AF_INET6, so we can use it internally as a "this is an
306 * IPv6 address" indication;
307 *
308 * define struct in6_addr so that we can use it for IPv6 addresses.
309 */
310 #ifndef HAVE_OS_IPV6_SUPPORT
311 #define AF_INET6 24
312
313 struct in6_addr {
314 union {
315 __uint8_t __u6_addr8[16];
316 __uint16_t __u6_addr16[8];
317 __uint32_t __u6_addr32[4];
318 } __u6_addr; /* 128-bit IP6 address */
319 };
320 #endif
321
322 #ifndef NI_MAXHOST
323 #define NI_MAXHOST 1025
324 #endif
325
326 #ifndef INET_ADDRSTRLEN
327 #define INET_ADDRSTRLEN 16
328 #endif
329
330 #ifndef TRUE
331 #define TRUE 1
332 #endif
333
334 #ifndef FALSE
335 #define FALSE 0
336 #endif
337
338 /*
339 * The Apple deprecation workaround macros below were adopted from the
340 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
341 */
342
343 #define XSTRINGIFY(x) #x
344
345 /*
346 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
347 */
348 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
349 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
350
351 #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
352 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
353 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
354 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
355 # define DIAG_ON(x) DIAG_PRAGMA(pop)
356 # else
357 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
358 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
359 # endif
360 #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
361 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
362 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
363 # define DIAG_ON(x) DIAG_PRAGMA(pop)
364 #else
365 # define DIAG_OFF(x)
366 # define DIAG_ON(x)
367 #endif
368
369 /*
370 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
371 */
372 #ifdef __APPLE__
373 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
374 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
375 #else
376 # define USES_APPLE_DEPRECATED_API
377 # define USES_APPLE_RST
378 #endif
379
380 /*
381 * end of Apple deprecation workaround macros
382 */
383
384 #ifndef min
385 #define min(a,b) ((a)>(b)?(b):(a))
386 #endif
387 #ifndef max
388 #define max(a,b) ((b)>(a)?(b):(a))
389 #endif
390
391 #endif /* netdissect_stdinc_h */