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