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