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