]> The Tcpdump Group git mirrors - tcpdump/blob - netdissect-stdinc.h
Handle very large -f files by rejecting them.
[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 <ctype.h>
55 #include <time.h>
56 #include <io.h>
57 #include <fcntl.h>
58 #include <sys/types.h>
59
60 #ifndef uint8_t
61 #define uint8_t unsigned char
62 #endif
63
64 #ifndef int8_t
65 #define int8_t signed char
66 #endif
67
68 #ifndef uint16_t
69 #define uint16_t unsigned short
70 #endif
71
72 #ifndef int16_t
73 #define int16_t signed short
74 #endif
75
76 #ifndef uint32_t
77 #define uint32_t unsigned int
78 #endif
79
80 #ifndef int32_t
81 #define int32_t signed int
82 #endif
83
84 #ifdef _MSC_EXTENSIONS
85
86 #ifndef uint64_t
87 #define uint64_t unsigned _int64
88 #endif
89
90 #ifndef int64_t
91 #define int64_t _int64
92 #endif
93
94 #ifndef PRId64
95 #define PRId64 "I64d"
96 #endif
97
98 #ifndef PRIo64
99 #define PRIo64 "I64o"
100 #endif
101
102 #ifndef PRIu64
103 #define PRIu64 "I64u"
104 #endif
105
106 #ifndef PRIx64
107 #define PRIx64 "I64x"
108 #endif
109
110 #else /* _MSC_EXTENSIONS */
111
112 #ifndef uint64_t
113 #define uint64_t unsigned long long
114 #endif
115
116 #ifndef int64_t
117 #define int64_t long long
118 #endif
119
120 #ifndef PRId64
121 #define PRId64 "lld"
122 #endif
123
124 #ifndef PRIo64
125 #define PRIo64 "llo"
126 #endif
127
128 #ifndef PRIu64
129 #define PRIu64 "llu"
130 #endif
131
132 #ifndef PRIx64
133 #define PRIx64 "llx"
134 #endif
135
136 #endif /* _MSC_EXTENSIONS */
137
138 /*
139 * Suppress definition of intN_t in bittypes.h, as included by <pcap/pcap.h>
140 * on Windows.
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.)
143 */
144 #define HAVE_U_INT8_T
145 #define HAVE_U_INT16_T
146 #define HAVE_U_INT32_T
147 #define HAVE_U_INT64_T
148
149 #ifdef _MSC_VER
150 #define stat _stat
151 #define open _open
152 #define read _read
153 #define close _close
154 #define O_RDONLY _O_RDONLY
155
156 /*
157 * We define our_fstat64 as _fstati64, and define our_statb as
158 * struct _stati64, so we get 64-bit file sizes.
159 */
160 #define our_fstat _fstati64
161 #define our_statb struct _stati64
162
163 #endif /* _MSC_VER */
164
165 /*
166 * With MSVC, for C, __inline is used to make a function an inline.
167 */
168 #ifdef _MSC_VER
169 #define inline __inline
170 #endif
171
172 #ifdef AF_INET6
173 #define HAVE_OS_IPV6_SUPPORT
174 #endif
175
176 #ifndef INET6_ADDRSTRLEN
177 #define INET6_ADDRSTRLEN 46
178 #endif
179
180 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
181 */
182 #ifndef EAFNOSUPPORT
183 #define EAFNOSUPPORT WSAEAFNOSUPPORT
184 #endif
185
186 #ifndef caddr_t
187 typedef char* caddr_t;
188 #endif /* caddr_t */
189
190 #define MAXHOSTNAMELEN 64
191 #define snprintf _snprintf
192 #define vsnprintf _vsnprintf
193 #define RETSIGTYPE void
194
195 #else /* _WIN32 */
196
197 /*
198 * Includes and definitions for various flavors of UN*X.
199 */
200
201 #include <ctype.h>
202 #include <unistd.h>
203 #include <netdb.h>
204 #if HAVE_INTTYPES_H
205 #include <inttypes.h>
206 #elif HAVE_STDINT_H
207 #include <stdint.h>
208 #endif
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>
214
215 #ifdef TIME_WITH_SYS_TIME
216 #include <time.h>
217 #endif
218
219 #include <arpa/inet.h>
220
221 /*
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.
224 */
225 #define our_fstat fstat
226 #define our_statb struct stat
227
228 #endif /* _WIN32 */
229
230 #ifndef HAVE___ATTRIBUTE__
231 #define __attribute__(x)
232 #endif
233
234 /*
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.
239 *
240 * This assumes that, for all compilers that support __attribute__:
241 *
242 * 1) they support __attribute__((packed));
243 *
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
248 * quantities.
249 *
250 * It does not (yet) handle compilers where you can get the compiler
251 * to generate code of that sort by some other means.
252 *
253 * This is required in order to, for example, keep the compiler from
254 * generating, for
255 *
256 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
257 *
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.
261 *
262 * Note: this also requires that padding be put into the structure,
263 * at least for compilers where it's implemented as __attribute__((packed)).
264 */
265 #if !(defined(_MSC_VER) && defined(UNALIGNED))
266 /* MSVC may have its own macro defined with the same name and purpose. */
267 #undef UNALIGNED
268 #define UNALIGNED __attribute__((packed))
269 #endif
270
271 /*
272 * fopen() read and write modes for text files and binary files.
273 */
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"
279 #else
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
284 #endif
285
286 /*
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
290 * the 80486.
291 *
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
294 * have it.)
295 */
296 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
297 #undef ntohl
298 #undef ntohs
299 #undef htonl
300 #undef htons
301
302 static __inline__ unsigned long __ntohl (unsigned long x);
303 static __inline__ unsigned short __ntohs (unsigned short x);
304
305 #define ntohl(x) __ntohl(x)
306 #define ntohs(x) __ntohs(x)
307 #define htonl(x) __ntohl(x)
308 #define htons(x) __ntohs(x)
309
310 static __inline__ unsigned long __ntohl (unsigned long x)
311 {
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));
316 return (x);
317 }
318
319 static __inline__ unsigned short __ntohs (unsigned short x)
320 {
321 __asm__ ("xchgb %b0, %h0" /* swap bytes */
322 : "=q" (x) : "0" (x));
323 return (x);
324 }
325 #endif
326
327 /*
328 * If the OS doesn't define AF_INET6 and struct in6_addr:
329 *
330 * define AF_INET6, so we can use it internally as a "this is an
331 * IPv6 address" indication;
332 *
333 * define struct in6_addr so that we can use it for IPv6 addresses.
334 */
335 #ifndef HAVE_OS_IPV6_SUPPORT
336 #ifndef AF_INET6
337 #define AF_INET6 24
338
339 struct in6_addr {
340 union {
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 */
345 };
346 #endif
347 #endif
348
349 #ifndef NI_MAXHOST
350 #define NI_MAXHOST 1025
351 #endif
352
353 #ifndef INET_ADDRSTRLEN
354 #define INET_ADDRSTRLEN 16
355 #endif
356
357 #ifndef TRUE
358 #define TRUE 1
359 #endif
360
361 #ifndef FALSE
362 #define FALSE 0
363 #endif
364
365 /*
366 * The Apple deprecation workaround macros below were adopted from the
367 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
368 */
369
370 #define XSTRINGIFY(x) #x
371
372 /*
373 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
374 */
375 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
376 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
377
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)
383 # else
384 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
385 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
386 # endif
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)
391 #else
392 # define DIAG_OFF(x)
393 # define DIAG_ON(x)
394 #endif
395
396 /*
397 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
398 */
399 #ifdef __APPLE__
400 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
401 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
402 #else
403 # define USES_APPLE_DEPRECATED_API
404 # define USES_APPLE_RST
405 #endif
406
407 /*
408 * end of Apple deprecation workaround macros
409 */
410
411 /*
412 * Function attributes, for various compilers.
413 */
414 #include "funcattrs.h"
415
416 #ifndef min
417 #define min(a,b) ((a)>(b)?(b):(a))
418 #endif
419 #ifndef max
420 #define max(a,b) ((b)>(a)?(b):(a))
421 #endif
422
423 #endif /* netdissect_stdinc_h */