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