1313024Sglebius/*
2313024Sglebius * Copyright (c) 2002 - 2003
3313024Sglebius * NetGroup, Politecnico di Torino (Italy)
4313024Sglebius * All rights reserved.
5313024Sglebius *
6313024Sglebius * Redistribution and use in source and binary forms, with or without
7313024Sglebius * modification, are permitted provided that the following conditions
8313024Sglebius * are met:
9313024Sglebius *
10313024Sglebius * 1. Redistributions of source code must retain the above copyright
11313024Sglebius * notice, this list of conditions and the following disclaimer.
12313024Sglebius * 2. Redistributions in binary form must reproduce the above copyright
13313024Sglebius * notice, this list of conditions and the following disclaimer in the
14313024Sglebius * documentation and/or other materials provided with the distribution.
15313024Sglebius * 3. Neither the name of the Politecnico di Torino nor the names of its
16313024Sglebius * contributors may be used to endorse or promote products derived from
17313024Sglebius * this software without specific prior written permission.
18313024Sglebius *
19313024Sglebius * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20313024Sglebius * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21313024Sglebius * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22313024Sglebius * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23313024Sglebius * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24313024Sglebius * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25313024Sglebius * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26313024Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27313024Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28313024Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29313024Sglebius * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30313024Sglebius */
31313024Sglebius
32313024Sglebius/*
33313024Sglebius * Include the appropriate OS header files on Windows and various flavors
34313024Sglebius * of UNIX, include various non-OS header files on Windows, and define
35313024Sglebius * various items as needed, to isolate most of netdissect's platform
36313024Sglebius * differences to this one file.
37313024Sglebius */
38313024Sglebius
39313024Sglebius#ifndef netdissect_stdinc_h
40313024Sglebius#define netdissect_stdinc_h
41313024Sglebius
42313024Sglebius#include <errno.h>
43313024Sglebius
44313024Sglebius#ifdef _WIN32
45313024Sglebius
46313024Sglebius/*
47313024Sglebius * Includes and definitions for Windows.
48313024Sglebius */
49313024Sglebius
50313024Sglebius#include <stdint.h>
51313024Sglebius#include <stdio.h>
52313024Sglebius#include <winsock2.h>
53313024Sglebius#include <ws2tcpip.h>
54313024Sglebius#include <ctype.h>
55313024Sglebius#include <time.h>
56313024Sglebius#include <io.h>
57313024Sglebius#include <fcntl.h>
58313024Sglebius#include <sys/types.h>
59313024Sglebius
60313024Sglebius#ifndef uint8_t
61313024Sglebius#define uint8_t		unsigned char
62313024Sglebius#endif
63313024Sglebius
64313024Sglebius#ifndef int8_t
65313024Sglebius#define int8_t		signed char
66313024Sglebius#endif
67313024Sglebius
68313024Sglebius#ifndef uint16_t
69313024Sglebius#define uint16_t	unsigned short
70313024Sglebius#endif
71313024Sglebius
72313024Sglebius#ifndef int16_t
73313024Sglebius#define int16_t		signed short
74313024Sglebius#endif
75313024Sglebius
76313024Sglebius#ifndef uint32_t
77313024Sglebius#define uint32_t	unsigned int
78313024Sglebius#endif
79313024Sglebius
80313024Sglebius#ifndef int32_t
81313024Sglebius#define int32_t		signed int
82313024Sglebius#endif
83313024Sglebius
84313024Sglebius#ifdef _MSC_EXTENSIONS
85313024Sglebius
86313024Sglebius#ifndef uint64_t
87313024Sglebius#define uint64_t	unsigned _int64
88313024Sglebius#endif
89313024Sglebius
90313024Sglebius#ifndef int64_t
91313024Sglebius#define int64_t		_int64
92313024Sglebius#endif
93313024Sglebius
94313024Sglebius#ifndef PRId64
95313024Sglebius#define PRId64		"I64d"
96313024Sglebius#endif
97313024Sglebius
98313024Sglebius#ifndef PRIo64
99313024Sglebius#define PRIo64		"I64o"
100313024Sglebius#endif
101313024Sglebius
102313024Sglebius#ifndef PRIu64
103313024Sglebius#define PRIu64		"I64u"
104313024Sglebius#endif
105313024Sglebius
106313024Sglebius#ifndef PRIx64
107313024Sglebius#define PRIx64		"I64x"
108313024Sglebius#endif
109313024Sglebius
110313024Sglebius#else /* _MSC_EXTENSIONS */
111313024Sglebius
112313024Sglebius#ifndef uint64_t
113313024Sglebius#define uint64_t	unsigned long long
114313024Sglebius#endif
115313024Sglebius
116313024Sglebius#ifndef int64_t
117313024Sglebius#define int64_t		long long
118313024Sglebius#endif
119313024Sglebius
120313024Sglebius#ifndef PRId64
121313024Sglebius#define PRId64		"lld"
122313024Sglebius#endif
123313024Sglebius
124313024Sglebius#ifndef PRIo64
125313024Sglebius#define PRIo64		"llo"
126313024Sglebius#endif
127313024Sglebius
128313024Sglebius#ifndef PRIu64
129313024Sglebius#define PRIu64		"llu"
130313024Sglebius#endif
131313024Sglebius
132313024Sglebius#ifndef PRIx64
133313024Sglebius#define PRIx64		"llx"
134313024Sglebius#endif
135313024Sglebius
136313024Sglebius#endif /* _MSC_EXTENSIONS */
137313024Sglebius
138313024Sglebius/*
139313024Sglebius * Suppress definition of intN_t in bittypes.h, as included by <pcap/pcap.h>
140313024Sglebius * on Windows.
141313024Sglebius * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
142313024Sglebius * we check for u_intN_t in the UN*X configure script.)
143313024Sglebius */
144313024Sglebius#define HAVE_U_INT8_T
145313024Sglebius#define HAVE_U_INT16_T
146313024Sglebius#define HAVE_U_INT32_T
147313024Sglebius#define HAVE_U_INT64_T
148313024Sglebius
149313024Sglebius#ifdef _MSC_VER
150313024Sglebius#define stat _stat
151313024Sglebius#define open _open
152313024Sglebius#define fstat _fstat
153313024Sglebius#define read _read
154313024Sglebius#define close _close
155313024Sglebius#define O_RDONLY _O_RDONLY
156313024Sglebius#endif  /* _MSC_VER */
157313024Sglebius
158313024Sglebius/*
159313024Sglebius * With MSVC, for C, __inline is used to make a function an inline.
160313024Sglebius */
161313024Sglebius#ifdef _MSC_VER
162313024Sglebius#define inline __inline
163313024Sglebius#endif
164313024Sglebius
165313024Sglebius#ifdef AF_INET6
166313024Sglebius#define HAVE_OS_IPV6_SUPPORT
167313024Sglebius#endif
168313024Sglebius
169313024Sglebius#ifndef INET6_ADDRSTRLEN
170313024Sglebius#define INET6_ADDRSTRLEN 46
171313024Sglebius#endif
172313024Sglebius
173313024Sglebius/* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
174313024Sglebius */
175313024Sglebius#ifndef EAFNOSUPPORT
176313024Sglebius#define EAFNOSUPPORT WSAEAFNOSUPPORT
177313024Sglebius#endif
178313024Sglebius
179313024Sglebius#ifndef caddr_t
180313024Sglebiustypedef char* caddr_t;
181313024Sglebius#endif /* caddr_t */
182313024Sglebius
183313024Sglebius#define MAXHOSTNAMELEN	64
184313024Sglebius#define snprintf _snprintf
185313024Sglebius#define vsnprintf _vsnprintf
186313024Sglebius#define RETSIGTYPE void
187313024Sglebius
188313024Sglebius#else /* _WIN32 */
189313024Sglebius
190313024Sglebius/*
191313024Sglebius * Includes and definitions for various flavors of UN*X.
192313024Sglebius */
193313024Sglebius
194313024Sglebius#include <ctype.h>
195313024Sglebius#include <unistd.h>
196313024Sglebius#include <netdb.h>
197313024Sglebius#if HAVE_INTTYPES_H
198313024Sglebius#include <inttypes.h>
199313024Sglebius#elif HAVE_STDINT_H
200313024Sglebius#include <stdint.h>
201313024Sglebius#endif
202313024Sglebius#include <sys/param.h>
203313024Sglebius#include <sys/types.h>			/* concession to AIX */
204313024Sglebius#include <sys/time.h>
205313024Sglebius#include <sys/socket.h>
206313024Sglebius#include <netinet/in.h>
207313024Sglebius
208313024Sglebius#ifdef TIME_WITH_SYS_TIME
209313024Sglebius#include <time.h>
210313024Sglebius#endif
211313024Sglebius
212313024Sglebius#include <arpa/inet.h>
213313024Sglebius
214313024Sglebius#endif /* _WIN32 */
215313024Sglebius
216313024Sglebius#ifndef HAVE___ATTRIBUTE__
217313024Sglebius#define __attribute__(x)
218313024Sglebius#endif
219313024Sglebius
220313024Sglebius/*
221313024Sglebius * Used to declare a structure unaligned, so that the C compiler,
222313024Sglebius * if necessary, generates code that doesn't assume alignment.
223313024Sglebius * This is required because there is no guarantee that the packet
224313024Sglebius * data we get from libpcap/WinPcap is properly aligned.
225313024Sglebius *
226313024Sglebius * This assumes that, for all compilers that support __attribute__:
227313024Sglebius *
228313024Sglebius *	1) they support __attribute__((packed));
229313024Sglebius *
230313024Sglebius *	2) for all instruction set architectures requiring strict
231313024Sglebius *	   alignment, declaring a structure with that attribute
232313024Sglebius *	   causes the compiler to generate code that handles
233313024Sglebius *	   misaligned 2-byte, 4-byte, and 8-byte integral
234313024Sglebius *	   quantities.
235313024Sglebius *
236313024Sglebius * It does not (yet) handle compilers where you can get the compiler
237313024Sglebius * to generate code of that sort by some other means.
238313024Sglebius *
239313024Sglebius * This is required in order to, for example, keep the compiler from
240313024Sglebius * generating, for
241313024Sglebius *
242313024Sglebius *	if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
243313024Sglebius *
244313024Sglebius * in print-bootp.c, code that loads the first 4-byte word of a
245313024Sglebius * "struct bootp", masking out the bp_hops field, and comparing the result
246313024Sglebius * against 0x01010600.
247313024Sglebius *
248313024Sglebius * Note: this also requires that padding be put into the structure,
249313024Sglebius * at least for compilers where it's implemented as __attribute__((packed)).
250313024Sglebius */
251313024Sglebius#if !(defined(_MSC_VER) && defined(UNALIGNED))
252313024Sglebius/* MSVC may have its own macro defined with the same name and purpose. */
253313024Sglebius#undef UNALIGNED
254313024Sglebius#define UNALIGNED	__attribute__((packed))
255313024Sglebius#endif
256313024Sglebius
257313024Sglebius/*
258313024Sglebius * fopen() read and write modes for text files and binary files.
259313024Sglebius */
260313024Sglebius#if defined(_WIN32) || defined(MSDOS)
261313024Sglebius  #define FOPEN_READ_TXT   "rt"
262313024Sglebius  #define FOPEN_READ_BIN   "rb"
263313024Sglebius  #define FOPEN_WRITE_TXT  "wt"
264313024Sglebius  #define FOPEN_WRITE_BIN  "wb"
265313024Sglebius#else
266313024Sglebius  #define FOPEN_READ_TXT   "r"
267313024Sglebius  #define FOPEN_READ_BIN   FOPEN_READ_TXT
268313024Sglebius  #define FOPEN_WRITE_TXT  "w"
269313024Sglebius  #define FOPEN_WRITE_BIN  FOPEN_WRITE_TXT
270313024Sglebius#endif
271313024Sglebius
272313024Sglebius/*
273313024Sglebius * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
274313024Sglebius * defined if the OS doesn't provide them.  These assume no more than
275313024Sglebius * an 80386, so, for example, it avoids the bswap instruction added in
276313024Sglebius * the 80486.
277313024Sglebius *
278313024Sglebius * (We don't use them on OS X; Apple provides their own, which *doesn't*
279313024Sglebius * avoid the bswap instruction, as OS X only supports machines that
280313024Sglebius * have it.)
281313024Sglebius */
282313024Sglebius#if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
283313024Sglebius  #undef ntohl
284313024Sglebius  #undef ntohs
285313024Sglebius  #undef htonl
286313024Sglebius  #undef htons
287313024Sglebius
288313024Sglebius  static __inline__ unsigned long __ntohl (unsigned long x);
289313024Sglebius  static __inline__ unsigned short __ntohs (unsigned short x);
290313024Sglebius
291313024Sglebius  #define ntohl(x)  __ntohl(x)
292313024Sglebius  #define ntohs(x)  __ntohs(x)
293313024Sglebius  #define htonl(x)  __ntohl(x)
294313024Sglebius  #define htons(x)  __ntohs(x)
295313024Sglebius
296313024Sglebius  static __inline__ unsigned long __ntohl (unsigned long x)
297313024Sglebius  {
298313024Sglebius    __asm__ ("xchgb %b0, %h0\n\t"   /* swap lower bytes  */
299313024Sglebius             "rorl  $16, %0\n\t"    /* swap words        */
300313024Sglebius             "xchgb %b0, %h0"       /* swap higher bytes */
301313024Sglebius            : "=q" (x) : "0" (x));
302313024Sglebius    return (x);
303313024Sglebius  }
304313024Sglebius
305313024Sglebius  static __inline__ unsigned short __ntohs (unsigned short x)
306313024Sglebius  {
307313024Sglebius    __asm__ ("xchgb %b0, %h0"       /* swap bytes */
308313024Sglebius            : "=q" (x) : "0" (x));
309313024Sglebius    return (x);
310313024Sglebius  }
311313024Sglebius#endif
312313024Sglebius
313313024Sglebius/*
314313024Sglebius * If the OS doesn't define AF_INET6 and struct in6_addr:
315313024Sglebius *
316313024Sglebius * define AF_INET6, so we can use it internally as a "this is an
317313024Sglebius * IPv6 address" indication;
318313024Sglebius *
319313024Sglebius * define struct in6_addr so that we can use it for IPv6 addresses.
320313024Sglebius */
321313024Sglebius#ifndef HAVE_OS_IPV6_SUPPORT
322313024Sglebius#ifndef AF_INET6
323313024Sglebius#define AF_INET6	24
324313024Sglebius
325313024Sglebiusstruct in6_addr {
326313024Sglebius	union {
327313024Sglebius		__uint8_t   __u6_addr8[16];
328313024Sglebius		__uint16_t  __u6_addr16[8];
329313024Sglebius		__uint32_t  __u6_addr32[4];
330313024Sglebius	} __u6_addr;			/* 128-bit IP6 address */
331313024Sglebius};
332313024Sglebius#endif
333313024Sglebius#endif
334313024Sglebius
335313024Sglebius#ifndef NI_MAXHOST
336313024Sglebius#define	NI_MAXHOST	1025
337313024Sglebius#endif
338313024Sglebius
339313024Sglebius#ifndef INET_ADDRSTRLEN
340313024Sglebius#define INET_ADDRSTRLEN 16
341313024Sglebius#endif
342313024Sglebius
343313024Sglebius#ifndef TRUE
344313024Sglebius#define TRUE 1
345313024Sglebius#endif
346313024Sglebius
347313024Sglebius#ifndef FALSE
348313024Sglebius#define FALSE 0
349313024Sglebius#endif
350313024Sglebius
351313024Sglebius/*
352313024Sglebius * The Apple deprecation workaround macros below were adopted from the
353313024Sglebius * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
354313024Sglebius */
355313024Sglebius
356313024Sglebius#define XSTRINGIFY(x) #x
357313024Sglebius
358313024Sglebius/*
359313024Sglebius *	Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
360313024Sglebius */
361313024Sglebius#define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
362313024Sglebius#define DIAG_DO_PRAGMA(x) _Pragma (#x)
363313024Sglebius
364313024Sglebius#if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
365313024Sglebius#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
366313024Sglebius#  if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
367313024Sglebius#    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
368313024Sglebius#    define DIAG_ON(x) DIAG_PRAGMA(pop)
369313024Sglebius#  else
370313024Sglebius#    define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
371313024Sglebius#    define DIAG_ON(x)  DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
372313024Sglebius#  endif
373313024Sglebius#elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
374313024Sglebius#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
375313024Sglebius#  define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
376313024Sglebius#  define DIAG_ON(x) DIAG_PRAGMA(pop)
377313024Sglebius#else
378313024Sglebius#  define DIAG_OFF(x)
379313024Sglebius#  define DIAG_ON(x)
380313024Sglebius#endif
381313024Sglebius
382313024Sglebius/*
383313024Sglebius *	For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
384313024Sglebius */
385313024Sglebius#ifdef __APPLE__
386313024Sglebius#  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
387313024Sglebius#  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
388313024Sglebius#else
389313024Sglebius#  define USES_APPLE_DEPRECATED_API
390313024Sglebius#  define USES_APPLE_RST
391313024Sglebius#endif
392313024Sglebius
393313024Sglebius/*
394313024Sglebius * end of Apple deprecation workaround macros
395313024Sglebius */
396313024Sglebius
397327234Semaste/*
398327234Semaste * Function attributes, for various compilers.
399327234Semaste */
400327234Semaste#include "funcattrs.h"
401327234Semaste
402313024Sglebius#ifndef min
403313024Sglebius#define min(a,b) ((a)>(b)?(b):(a))
404313024Sglebius#endif
405313024Sglebius#ifndef max
406313024Sglebius#define max(a,b) ((b)>(a)?(b):(a))
407313024Sglebius#endif
408313024Sglebius
409313024Sglebius#endif /* netdissect_stdinc_h */
410