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