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