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 netdissect's platform
36 * differences to this one file.
37 */
38
39#ifndef netdissect_stdinc_h
40#define netdissect_stdinc_h
41
42#include <errno.h>
43
44#ifdef _WIN32
45
46/*
47 * Includes and definitions for Windows.
48 */
49
50#include <stdint.h>
51#include <stdio.h>
52#include <winsock2.h>
53#include <ws2tcpip.h>
54#include <ctype.h>
55#include <time.h>
56#include <io.h>
57#include <fcntl.h>
58#include <sys/types.h>
59
60#ifndef uint8_t
61#define uint8_t		unsigned char
62#endif
63
64#ifndef int8_t
65#define int8_t		signed char
66#endif
67
68#ifndef uint16_t
69#define uint16_t	unsigned short
70#endif
71
72#ifndef int16_t
73#define int16_t		signed short
74#endif
75
76#ifndef uint32_t
77#define uint32_t	unsigned int
78#endif
79
80#ifndef int32_t
81#define int32_t		signed int
82#endif
83
84#ifdef _MSC_EXTENSIONS
85
86#ifndef uint64_t
87#define uint64_t	unsigned _int64
88#endif
89
90#ifndef int64_t
91#define int64_t		_int64
92#endif
93
94#ifndef PRId64
95#define PRId64		"I64d"
96#endif
97
98#ifndef PRIo64
99#define PRIo64		"I64o"
100#endif
101
102#ifndef PRIu64
103#define PRIu64		"I64u"
104#endif
105
106#ifndef PRIx64
107#define PRIx64		"I64x"
108#endif
109
110#else /* _MSC_EXTENSIONS */
111
112#ifndef uint64_t
113#define uint64_t	unsigned long long
114#endif
115
116#ifndef int64_t
117#define int64_t		long long
118#endif
119
120#ifndef PRId64
121#define PRId64		"lld"
122#endif
123
124#ifndef PRIo64
125#define PRIo64		"llo"
126#endif
127
128#ifndef PRIu64
129#define PRIu64		"llu"
130#endif
131
132#ifndef PRIx64
133#define PRIx64		"llx"
134#endif
135
136#endif /* _MSC_EXTENSIONS */
137
138/*
139 * Suppress definition of intN_t in bittypes.h, as included by <pcap/pcap.h>
140 * on Windows.
141 * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
142 * we check for u_intN_t in the UN*X configure script.)
143 */
144#define HAVE_U_INT8_T
145#define HAVE_U_INT16_T
146#define HAVE_U_INT32_T
147#define HAVE_U_INT64_T
148
149#ifdef _MSC_VER
150#define stat _stat
151#define open _open
152#define fstat _fstat
153#define read _read
154#define close _close
155#define O_RDONLY _O_RDONLY
156#endif  /* _MSC_VER */
157
158/*
159 * With MSVC, for C, __inline is used to make a function an inline.
160 */
161#ifdef _MSC_VER
162#define inline __inline
163#endif
164
165#ifdef AF_INET6
166#define HAVE_OS_IPV6_SUPPORT
167#endif
168
169#ifndef INET6_ADDRSTRLEN
170#define INET6_ADDRSTRLEN 46
171#endif
172
173/* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
174 */
175#ifndef EAFNOSUPPORT
176#define EAFNOSUPPORT WSAEAFNOSUPPORT
177#endif
178
179#ifndef caddr_t
180typedef char* caddr_t;
181#endif /* caddr_t */
182
183#define MAXHOSTNAMELEN	64
184#define snprintf _snprintf
185#define vsnprintf _vsnprintf
186#define RETSIGTYPE void
187
188#else /* _WIN32 */
189
190/*
191 * Includes and definitions for various flavors of UN*X.
192 */
193
194#include <ctype.h>
195#include <unistd.h>
196#include <netdb.h>
197#if HAVE_INTTYPES_H
198#include <inttypes.h>
199#elif HAVE_STDINT_H
200#include <stdint.h>
201#endif
202#include <sys/param.h>
203#include <sys/types.h>			/* concession to AIX */
204#include <sys/time.h>
205#include <sys/socket.h>
206#include <netinet/in.h>
207
208#ifdef TIME_WITH_SYS_TIME
209#include <time.h>
210#endif
211
212#include <arpa/inet.h>
213
214#endif /* _WIN32 */
215
216#ifndef HAVE___ATTRIBUTE__
217#define __attribute__(x)
218#endif
219
220/*
221 * Used to declare a structure unaligned, so that the C compiler,
222 * if necessary, generates code that doesn't assume alignment.
223 * This is required because there is no guarantee that the packet
224 * data we get from libpcap/WinPcap is properly aligned.
225 *
226 * This assumes that, for all compilers that support __attribute__:
227 *
228 *	1) they support __attribute__((packed));
229 *
230 *	2) for all instruction set architectures requiring strict
231 *	   alignment, declaring a structure with that attribute
232 *	   causes the compiler to generate code that handles
233 *	   misaligned 2-byte, 4-byte, and 8-byte integral
234 *	   quantities.
235 *
236 * It does not (yet) handle compilers where you can get the compiler
237 * to generate code of that sort by some other means.
238 *
239 * This is required in order to, for example, keep the compiler from
240 * generating, for
241 *
242 *	if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
243 *
244 * in print-bootp.c, code that loads the first 4-byte word of a
245 * "struct bootp", masking out the bp_hops field, and comparing the result
246 * against 0x01010600.
247 *
248 * Note: this also requires that padding be put into the structure,
249 * at least for compilers where it's implemented as __attribute__((packed)).
250 */
251#if !(defined(_MSC_VER) && defined(UNALIGNED))
252/* MSVC may have its own macro defined with the same name and purpose. */
253#undef UNALIGNED
254#define UNALIGNED	__attribute__((packed))
255#endif
256
257/*
258 * fopen() read and write modes for text files and binary files.
259 */
260#if defined(_WIN32) || defined(MSDOS)
261  #define FOPEN_READ_TXT   "rt"
262  #define FOPEN_READ_BIN   "rb"
263  #define FOPEN_WRITE_TXT  "wt"
264  #define FOPEN_WRITE_BIN  "wb"
265#else
266  #define FOPEN_READ_TXT   "r"
267  #define FOPEN_READ_BIN   FOPEN_READ_TXT
268  #define FOPEN_WRITE_TXT  "w"
269  #define FOPEN_WRITE_BIN  FOPEN_WRITE_TXT
270#endif
271
272/*
273 * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
274 * defined if the OS doesn't provide them.  These assume no more than
275 * an 80386, so, for example, it avoids the bswap instruction added in
276 * the 80486.
277 *
278 * (We don't use them on OS X; Apple provides their own, which *doesn't*
279 * avoid the bswap instruction, as OS X only supports machines that
280 * have it.)
281 */
282#if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
283  #undef ntohl
284  #undef ntohs
285  #undef htonl
286  #undef htons
287
288  static __inline__ unsigned long __ntohl (unsigned long x);
289  static __inline__ unsigned short __ntohs (unsigned short x);
290
291  #define ntohl(x)  __ntohl(x)
292  #define ntohs(x)  __ntohs(x)
293  #define htonl(x)  __ntohl(x)
294  #define htons(x)  __ntohs(x)
295
296  static __inline__ unsigned long __ntohl (unsigned long x)
297  {
298    __asm__ ("xchgb %b0, %h0\n\t"   /* swap lower bytes  */
299             "rorl  $16, %0\n\t"    /* swap words        */
300             "xchgb %b0, %h0"       /* swap higher bytes */
301            : "=q" (x) : "0" (x));
302    return (x);
303  }
304
305  static __inline__ unsigned short __ntohs (unsigned short x)
306  {
307    __asm__ ("xchgb %b0, %h0"       /* swap bytes */
308            : "=q" (x) : "0" (x));
309    return (x);
310  }
311#endif
312
313/*
314 * If the OS doesn't define AF_INET6 and struct in6_addr:
315 *
316 * define AF_INET6, so we can use it internally as a "this is an
317 * IPv6 address" indication;
318 *
319 * define struct in6_addr so that we can use it for IPv6 addresses.
320 */
321#ifndef HAVE_OS_IPV6_SUPPORT
322#ifndef AF_INET6
323#define AF_INET6	24
324
325struct in6_addr {
326	union {
327		__uint8_t   __u6_addr8[16];
328		__uint16_t  __u6_addr16[8];
329		__uint32_t  __u6_addr32[4];
330	} __u6_addr;			/* 128-bit IP6 address */
331};
332#endif
333#endif
334
335#ifndef NI_MAXHOST
336#define	NI_MAXHOST	1025
337#endif
338
339#ifndef INET_ADDRSTRLEN
340#define INET_ADDRSTRLEN 16
341#endif
342
343#ifndef TRUE
344#define TRUE 1
345#endif
346
347#ifndef FALSE
348#define FALSE 0
349#endif
350
351/*
352 * The Apple deprecation workaround macros below were adopted from the
353 * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
354 */
355
356#define XSTRINGIFY(x) #x
357
358/*
359 *	Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
360 */
361#define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
362#define DIAG_DO_PRAGMA(x) _Pragma (#x)
363
364#if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
365#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
366#  if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
367#    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
368#    define DIAG_ON(x) DIAG_PRAGMA(pop)
369#  else
370#    define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
371#    define DIAG_ON(x)  DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
372#  endif
373#elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
374#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
375#  define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
376#  define DIAG_ON(x) DIAG_PRAGMA(pop)
377#else
378#  define DIAG_OFF(x)
379#  define DIAG_ON(x)
380#endif
381
382/*
383 *	For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
384 */
385#ifdef __APPLE__
386#  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
387#  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
388#else
389#  define USES_APPLE_DEPRECATED_API
390#  define USES_APPLE_RST
391#endif
392
393/*
394 * end of Apple deprecation workaround macros
395 */
396
397/*
398 * Function attributes, for various compilers.
399 */
400#include "funcattrs.h"
401
402#ifndef min
403#define min(a,b) ((a)>(b)?(b):(a))
404#endif
405#ifndef max
406#define max(a,b) ((b)>(a)?(b):(a))
407#endif
408
409#endif /* netdissect_stdinc_h */
410