etc/services - sync with NetBSD-8
[minix.git] / external / bsd / tcpdump / dist / tcpdump-stdinc.h
blob68b3e86755bc31d2a52570dbfce011b9de712cea
1 /*
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
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.
39 #ifndef tcpdump_stdinc_h
40 #define tcpdump_stdinc_h
42 #include <errno.h>
44 #ifdef WIN32
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <winsock2.h>
49 #include <ws2tcpip.h>
50 #include "bittypes.h" /* in wpcap's Win32/include */
51 #include <ctype.h>
52 #include <time.h>
53 #include <io.h>
54 #include <fcntl.h>
55 #include <sys/types.h>
56 #include <net/netdb.h> /* in wpcap's Win32/include */
58 #ifndef uint8_t
59 #define uint8_t unsigned char
60 #endif
62 #ifndef int8_t
63 #define int8_t signed char
64 #endif
66 #ifndef uint16_t
67 #define uint16_t unsigned short
68 #endif
70 #ifndef int16_t
71 #define int16_t signed short
72 #endif
74 #ifndef uint32_t
75 #define uint32_t unsigned int
76 #endif
78 #ifndef int32_t
79 #define int32_t signed int
80 #endif
82 #ifdef _MSC_EXTENSIONS
84 #ifndef uint64_t
85 #define uint64_t unsigned _int64
86 #endif
88 #ifndef int64_t
89 #define int64_t _int64
90 #endif
92 #ifndef PRId64
93 #define PRId64 "I64d"
94 #endif
96 #ifndef PRIo64
97 #define PRIo64 "I64o"
98 #endif
100 #ifndef PRIu64
101 #define PRIu64 "I64u"
102 #endif
104 #ifndef PRIx64
105 #define PRIx64 "I64x"
106 #endif
108 #else /* _MSC_EXTENSIONS */
110 #ifndef uint64_t
111 #define uint64_t unsigned long long
112 #endif
114 #ifndef int64_t
115 #define int64_t long long
116 #endif
118 #ifndef PRId64
119 #define PRId64 "lld"
120 #endif
122 #ifndef PRIo64
123 #define PRIo64 "llo"
124 #endif
126 #ifndef PRIu64
127 #define PRIu64 "llu"
128 #endif
130 #ifndef PRIx64
131 #define PRIx64 "llx"
132 #endif
134 #endif /* _MSC_EXTENSIONS */
136 #ifdef _MSC_VER
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 /* _MSC_VER */
145 /* Protos for missing/x.c functions (ideally <missing/addrinfo.h>
146 * should be used, but it clashes with <ws2tcpip.h>).
148 extern const char *inet_ntop (int, const void *, char *, size_t);
149 extern int inet_pton (int, const char *, void *);
150 extern int inet_aton (const char *cp, struct in_addr *addr);
153 * With MSVC, for C, __inline is used to make a function an inline.
155 #ifdef _MSC_VER
156 #define inline __inline
157 #endif
159 #ifndef INET6_ADDRSTRLEN
160 #define INET6_ADDRSTRLEN 46
161 #endif
163 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
165 #ifndef EAFNOSUPPORT
166 #define EAFNOSUPPORT WSAEAFNOSUPPORT
167 #endif
169 #ifndef caddr_t
170 typedef char* caddr_t;
171 #endif /* caddr_t */
173 #define MAXHOSTNAMELEN 64
174 #define NI_MAXHOST 1025
175 #define snprintf _snprintf
176 #define vsnprintf _vsnprintf
177 #define RETSIGTYPE void
179 #else /* WIN32 */
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>
195 #ifdef TIME_WITH_SYS_TIME
196 #include <time.h>
197 #endif
199 #include <arpa/inet.h>
201 #endif /* WIN32 */
203 #ifndef HAVE___ATTRIBUTE__
204 #define __attribute__(x)
205 #endif
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.
213 * This assumes that, for all compilers that support __attribute__:
215 * 1) they support __attribute__((packed));
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.
223 * It does not (yet) handle compilers where you can get the compiler
224 * to generate code of that sort by some other means.
226 * This is required in order to, for example, keep the compiler from
227 * generating, for
229 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
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.
235 * Note: this also requires that padding be put into the structure,
236 * at least for compilers where it's implemented as __attribute__((packed)).
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
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
256 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
257 #undef ntohl
258 #undef ntohs
259 #undef htonl
260 #undef htons
262 static __inline__ unsigned int __ntohl (unsigned int x);
263 static __inline__ unsigned short __ntohs (unsigned short x);
265 #define ntohl(x) __ntohl(x)
266 #define ntohs(x) __ntohs(x)
267 #define htonl(x) __ntohl(x)
268 #define htons(x) __ntohs(x)
270 static __inline__ unsigned int __ntohl (unsigned int x)
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);
279 static __inline__ unsigned short __ntohs (unsigned short x)
281 __asm__ ("xchgb %b0, %h0" /* swap bytes */
282 : "=q" (x) : "0" (x));
283 return (x);
285 #endif
287 #ifndef INET_ADDRSTRLEN
288 #define INET_ADDRSTRLEN 16
289 #endif
291 #ifndef TRUE
292 #define TRUE 1
293 #endif
295 #ifndef FALSE
296 #define FALSE 0
297 #endif
300 * The Apple deprecation workaround macros below were adopted from the
301 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
304 #define XSTRINGIFY(x) #x
307 * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
309 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
310 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
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
331 * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
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
342 * end of Apple deprecation workaround macros
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
352 #endif /* tcpdump_stdinc_h */