1281e9d7bms/*
2281e9d7bms * Copyright (c) 2002 - 2003
3281e9d7bms * NetGroup, Politecnico di Torino (Italy)
4281e9d7bms * All rights reserved.
5281e9d7bms *
6281e9d7bms * Redistribution and use in source and binary forms, with or without
7281e9d7bms * modification, are permitted provided that the following conditions
8281e9d7bms * are met:
9281e9d7bms *
10281e9d7bms * 1. Redistributions of source code must retain the above copyright
11281e9d7bms * notice, this list of conditions and the following disclaimer.
12281e9d7bms * 2. Redistributions in binary form must reproduce the above copyright
13281e9d7bms * notice, this list of conditions and the following disclaimer in the
14281e9d7bms * documentation and/or other materials provided with the distribution.
15281e9d7bms * 3. Neither the name of the Politecnico di Torino nor the names of its
16281e9d7bms * contributors may be used to endorse or promote products derived from
17281e9d7bms * this software without specific prior written permission.
18281e9d7bms *
19281e9d7bms * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20281e9d7bms * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21281e9d7bms * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22281e9d7bms * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23281e9d7bms * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24281e9d7bms * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25281e9d7bms * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26281e9d7bms * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27281e9d7bms * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28281e9d7bms * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29281e9d7bms * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30281e9d7bms */
31281e9d7bms
32281e9d7bms/*
33281e9d7bms * Include the appropriate OS header files on Windows and various flavors
3408263d1delphij * of UNIX, include various non-OS header files on Windows, and define
35af736e6glebius * various items as needed, to isolate most of netdissect's platform
36281e9d7bms * differences to this one file.
37281e9d7bms */
38281e9d7bms
39af736e6glebius#ifndef netdissect_stdinc_h
40af736e6glebius#define netdissect_stdinc_h
41281e9d7bms
4208263d1delphij#include <errno.h>
4308263d1delphij
44af736e6glebius#ifdef _WIN32
45af736e6glebius
46af736e6glebius/*
47af736e6glebius * Includes and definitions for Windows.
48af736e6glebius */
49281e9d7bms
5008263d1delphij#include <stdint.h>
51281e9d7bms#include <stdio.h>
52281e9d7bms#include <winsock2.h>
5308263d1delphij#include <ws2tcpip.h>
54281e9d7bms#include <ctype.h>
55281e9d7bms#include <time.h>
56281e9d7bms#include <io.h>
57281e9d7bms#include <fcntl.h>
5888a191fsam#include <sys/types.h>
59281e9d7bms
6008263d1delphij#ifndef uint8_t
6108263d1delphij#define uint8_t		unsigned char
6208263d1delphij#endif
6308263d1delphij
6408263d1delphij#ifndef int8_t
6508263d1delphij#define int8_t		signed char
6608263d1delphij#endif
6708263d1delphij
6808263d1delphij#ifndef uint16_t
6908263d1delphij#define uint16_t	unsigned short
7008263d1delphij#endif
7108263d1delphij
7208263d1delphij#ifndef int16_t
7308263d1delphij#define int16_t		signed short
7408263d1delphij#endif
7508263d1delphij
7608263d1delphij#ifndef uint32_t
7708263d1delphij#define uint32_t	unsigned int
7808263d1delphij#endif
7908263d1delphij
8008263d1delphij#ifndef int32_t
8108263d1delphij#define int32_t		signed int
8208263d1delphij#endif
8308263d1delphij
8408263d1delphij#ifdef _MSC_EXTENSIONS
8508263d1delphij
8608263d1delphij#ifndef uint64_t
8708263d1delphij#define uint64_t	unsigned _int64
8808263d1delphij#endif
8908263d1delphij
9008263d1delphij#ifndef int64_t
9108263d1delphij#define int64_t		_int64
9208263d1delphij#endif
9308263d1delphij
9408263d1delphij#ifndef PRId64
9508263d1delphij#define PRId64		"I64d"
9608263d1delphij#endif
9708263d1delphij
9808263d1delphij#ifndef PRIo64
9908263d1delphij#define PRIo64		"I64o"
10008263d1delphij#endif
10108263d1delphij
10208263d1delphij#ifndef PRIu64
10308263d1delphij#define PRIu64		"I64u"
10408263d1delphij#endif
10508263d1delphij
10608263d1delphij#ifndef PRIx64
10708263d1delphij#define PRIx64		"I64x"
10808263d1delphij#endif
10908263d1delphij
11008263d1delphij#else /* _MSC_EXTENSIONS */
11108263d1delphij
11208263d1delphij#ifndef uint64_t
11308263d1delphij#define uint64_t	unsigned long long
11408263d1delphij#endif
11508263d1delphij
11608263d1delphij#ifndef int64_t
11708263d1delphij#define int64_t		long long
11808263d1delphij#endif
11908263d1delphij
12008263d1delphij#ifndef PRId64
12108263d1delphij#define PRId64		"lld"
12208263d1delphij#endif
12308263d1delphij
12408263d1delphij#ifndef PRIo64
12508263d1delphij#define PRIo64		"llo"
12608263d1delphij#endif
12708263d1delphij
12808263d1delphij#ifndef PRIu64
12908263d1delphij#define PRIu64		"llu"
1305f4de8erpaulo#endif
1315f4de8erpaulo
13208263d1delphij#ifndef PRIx64
13308263d1delphij#define PRIx64		"llx"
13408263d1delphij#endif
13508263d1delphij
13608263d1delphij#endif /* _MSC_EXTENSIONS */
13708263d1delphij
138af736e6glebius/*
139af736e6glebius * Suppress definition of intN_t in bittypes.h, as included by <pcap/pcap.h>
140af736e6glebius * on Windows.
141af736e6glebius * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
142af736e6glebius * we check for u_intN_t in the UN*X configure script.)
143af736e6glebius */
144af736e6glebius#define HAVE_U_INT8_T
145af736e6glebius#define HAVE_U_INT16_T
146af736e6glebius#define HAVE_U_INT32_T
147af736e6glebius#define HAVE_U_INT64_T
148af736e6glebius
14908263d1delphij#ifdef _MSC_VER
1501166f90sam#define stat _stat
1511166f90sam#define open _open
1521166f90sam#define fstat _fstat
1531166f90sam#define read _read
1541166f90sam#define close _close
1551166f90sam#define O_RDONLY _O_RDONLY
15608263d1delphij#endif  /* _MSC_VER */
157281e9d7bms
15808263d1delphij/*
15908263d1delphij * With MSVC, for C, __inline is used to make a function an inline.
16008263d1delphij */
16108263d1delphij#ifdef _MSC_VER
16208263d1delphij#define inline __inline
16308263d1delphij#endif
16408263d1delphij
165af736e6glebius#ifdef AF_INET6
166af736e6glebius#define HAVE_OS_IPV6_SUPPORT
167af736e6glebius#endif
168af736e6glebius
16988a191fsam#ifndef INET6_ADDRSTRLEN
17088a191fsam#define INET6_ADDRSTRLEN 46
17188a191fsam#endif
172281e9d7bms
17308263d1delphij/* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
17408263d1delphij */
17508263d1delphij#ifndef EAFNOSUPPORT
17608263d1delphij#define EAFNOSUPPORT WSAEAFNOSUPPORT
177281e9d7bms#endif
178281e9d7bms
179281e9d7bms#ifndef caddr_t
180281e9d7bmstypedef char* caddr_t;
181281e9d7bms#endif /* caddr_t */
182281e9d7bms
183281e9d7bms#define MAXHOSTNAMELEN	64
184281e9d7bms#define snprintf _snprintf
185281e9d7bms#define vsnprintf _vsnprintf
186281e9d7bms#define RETSIGTYPE void
187281e9d7bms
188af736e6glebius#else /* _WIN32 */
189af736e6glebius
190af736e6glebius/*
191af736e6glebius * Includes and definitions for various flavors of UN*X.
192af736e6glebius */
193281e9d7bms
194281e9d7bms#include <ctype.h>
195281e9d7bms#include <unistd.h>
196281e9d7bms#include <netdb.h>
1971166f90sam#if HAVE_INTTYPES_H
19888a191fsam#include <inttypes.h>
19908263d1delphij#elif HAVE_STDINT_H
2001166f90sam#include <stdint.h>
2011166f90sam#endif
202281e9d7bms#include <sys/param.h>
203281e9d7bms#include <sys/types.h>			/* concession to AIX */
204281e9d7bms#include <sys/time.h>
205281e9d7bms#include <sys/socket.h>
206281e9d7bms#include <netinet/in.h>
207281e9d7bms
208281e9d7bms#ifdef TIME_WITH_SYS_TIME
209281e9d7bms#include <time.h>
210281e9d7bms#endif
211281e9d7bms
212281e9d7bms#include <arpa/inet.h>
213281e9d7bms
214af736e6glebius#endif /* _WIN32 */
215281e9d7bms
216d36dcecdelphij#ifndef HAVE___ATTRIBUTE__
217d36dcecdelphij#define __attribute__(x)
218281e9d7bms#endif
219281e9d7bms
220d36dcecdelphij/*
221d36dcecdelphij * Used to declare a structure unaligned, so that the C compiler,
222d36dcecdelphij * if necessary, generates code that doesn't assume alignment.
223d36dcecdelphij * This is required because there is no guarantee that the packet
224d36dcecdelphij * data we get from libpcap/WinPcap is properly aligned.
225d36dcecdelphij *
226d36dcecdelphij * This assumes that, for all compilers that support __attribute__:
227d36dcecdelphij *
228d36dcecdelphij *	1) they support __attribute__((packed));
229d36dcecdelphij *
230d36dcecdelphij *	2) for all instruction set architectures requiring strict
231d36dcecdelphij *	   alignment, declaring a structure with that attribute
232d36dcecdelphij *	   causes the compiler to generate code that handles
233d36dcecdelphij *	   misaligned 2-byte, 4-byte, and 8-byte integral
234d36dcecdelphij *	   quantities.
235d36dcecdelphij *
236d36dcecdelphij * It does not (yet) handle compilers where you can get the compiler
237d36dcecdelphij * to generate code of that sort by some other means.
238d36dcecdelphij *
239d36dcecdelphij * This is required in order to, for example, keep the compiler from
240d36dcecdelphij * generating, for
241d36dcecdelphij *
242d36dcecdelphij *	if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
243d36dcecdelphij *
244d36dcecdelphij * in print-bootp.c, code that loads the first 4-byte word of a
245d36dcecdelphij * "struct bootp", masking out the bp_hops field, and comparing the result
246d36dcecdelphij * against 0x01010600.
247d36dcecdelphij *
248d36dcecdelphij * Note: this also requires that padding be put into the structure,
249d36dcecdelphij * at least for compilers where it's implemented as __attribute__((packed)).
250d36dcecdelphij */
25108263d1delphij#if !(defined(_MSC_VER) && defined(UNALIGNED))
25208263d1delphij/* MSVC may have its own macro defined with the same name and purpose. */
25308263d1delphij#undef UNALIGNED
254d36dcecdelphij#define UNALIGNED	__attribute__((packed))
25508263d1delphij#endif
256d36dcecdelphij
257af736e6glebius/*
258af736e6glebius * fopen() read and write modes for text files and binary files.
259af736e6glebius */
260af736e6glebius#if defined(_WIN32) || defined(MSDOS)
261281e9d7bms  #define FOPEN_READ_TXT   "rt"
262281e9d7bms  #define FOPEN_READ_BIN   "rb"
263281e9d7bms  #define FOPEN_WRITE_TXT  "wt"
264281e9d7bms  #define FOPEN_WRITE_BIN  "wb"
265281e9d7bms#else
266281e9d7bms  #define FOPEN_READ_TXT   "r"
267281e9d7bms  #define FOPEN_READ_BIN   FOPEN_READ_TXT
268281e9d7bms  #define FOPEN_WRITE_TXT  "w"
269281e9d7bms  #define FOPEN_WRITE_BIN  FOPEN_WRITE_TXT
270281e9d7bms#endif
271281e9d7bms
272af736e6glebius/*
273af736e6glebius * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
274af736e6glebius * defined if the OS doesn't provide them.  These assume no more than
275af736e6glebius * an 80386, so, for example, it avoids the bswap instruction added in
276af736e6glebius * the 80486.
277af736e6glebius *
278af736e6glebius * (We don't use them on OS X; Apple provides their own, which *doesn't*
279af736e6glebius * avoid the bswap instruction, as OS X only supports machines that
280af736e6glebius * have it.)
281af736e6glebius */
28208263d1delphij#if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
28388a191fsam  #undef ntohl
28488a191fsam  #undef ntohs
28588a191fsam  #undef htonl
28688a191fsam  #undef htons
28788a191fsam
2885f4de8erpaulo  static __inline__ unsigned long __ntohl (unsigned long x);
2895f4de8erpaulo  static __inline__ unsigned short __ntohs (unsigned short x);
29088a191fsam
29188a191fsam  #define ntohl(x)  __ntohl(x)
29288a191fsam  #define ntohs(x)  __ntohs(x)
29388a191fsam  #define htonl(x)  __ntohl(x)
29488a191fsam  #define htons(x)  __ntohs(x)
29588a191fsam
2965f4de8erpaulo  static __inline__ unsigned long __ntohl (unsigned long x)
29788a191fsam  {
29888a191fsam    __asm__ ("xchgb %b0, %h0\n\t"   /* swap lower bytes  */
29988a191fsam             "rorl  $16, %0\n\t"    /* swap words        */
30088a191fsam             "xchgb %b0, %h0"       /* swap higher bytes */
30188a191fsam            : "=q" (x) : "0" (x));
30288a191fsam    return (x);
30388a191fsam  }
30488a191fsam
3055f4de8erpaulo  static __inline__ unsigned short __ntohs (unsigned short x)
30688a191fsam  {
30788a191fsam    __asm__ ("xchgb %b0, %h0"       /* swap bytes */
30888a191fsam            : "=q" (x) : "0" (x));
30988a191fsam    return (x);
31088a191fsam  }
31188a191fsam#endif
31288a191fsam
313af736e6glebius/*
314af736e6glebius * If the OS doesn't define AF_INET6 and struct in6_addr:
315af736e6glebius *
316af736e6glebius * define AF_INET6, so we can use it internally as a "this is an
317af736e6glebius * IPv6 address" indication;
318af736e6glebius *
319af736e6glebius * define struct in6_addr so that we can use it for IPv6 addresses.
320af736e6glebius */
321af736e6glebius#ifndef HAVE_OS_IPV6_SUPPORT
322af736e6glebius#ifndef AF_INET6
323af736e6glebius#define AF_INET6	24
324af736e6glebius
325af736e6glebiusstruct in6_addr {
326af736e6glebius	union {
327af736e6glebius		__uint8_t   __u6_addr8[16];
328af736e6glebius		__uint16_t  __u6_addr16[8];
329af736e6glebius		__uint32_t  __u6_addr32[4];
330af736e6glebius	} __u6_addr;			/* 128-bit IP6 address */
331af736e6glebius};
332af736e6glebius#endif
333af736e6glebius#endif
334af736e6glebius
335af736e6glebius#ifndef NI_MAXHOST
336af736e6glebius#define	NI_MAXHOST	1025
337af736e6glebius#endif
338af736e6glebius
33988a191fsam#ifndef INET_ADDRSTRLEN
34088a191fsam#define INET_ADDRSTRLEN 16
34188a191fsam#endif
34288a191fsam
3433b74598mlaier#ifndef TRUE
3443b74598mlaier#define TRUE 1
3453b74598mlaier#endif
3463b74598mlaier
3473b74598mlaier#ifndef FALSE
3483b74598mlaier#define FALSE 0
3493b74598mlaier#endif
3503b74598mlaier
35108263d1delphij/*
35208263d1delphij * The Apple deprecation workaround macros below were adopted from the
35308263d1delphij * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
35408263d1delphij */
35508263d1delphij
35608263d1delphij#define XSTRINGIFY(x) #x
35708263d1delphij
35808263d1delphij/*
35908263d1delphij *	Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
36008263d1delphij */
36108263d1delphij#define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
36208263d1delphij#define DIAG_DO_PRAGMA(x) _Pragma (#x)
36308263d1delphij
36408263d1delphij#if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
36508263d1delphij#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
36608263d1delphij#  if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
36708263d1delphij#    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
36808263d1delphij#    define DIAG_ON(x) DIAG_PRAGMA(pop)
36908263d1delphij#  else
37008263d1delphij#    define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
37108263d1delphij#    define DIAG_ON(x)  DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
37208263d1delphij#  endif
37308263d1delphij#elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
37408263d1delphij#  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
37508263d1delphij#  define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
37608263d1delphij#  define DIAG_ON(x) DIAG_PRAGMA(pop)
37708263d1delphij#else
37808263d1delphij#  define DIAG_OFF(x)
37908263d1delphij#  define DIAG_ON(x)
38008263d1delphij#endif
38108263d1delphij
38208263d1delphij/*
38308263d1delphij *	For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
38408263d1delphij */
38508263d1delphij#ifdef __APPLE__
38608263d1delphij#  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
38708263d1delphij#  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
38808263d1delphij#else
38908263d1delphij#  define USES_APPLE_DEPRECATED_API
39008263d1delphij#  define USES_APPLE_RST
39108263d1delphij#endif
39208263d1delphij
39308263d1delphij/*
39408263d1delphij * end of Apple deprecation workaround macros
39508263d1delphij */
39608263d1delphij
3974eec411gordon/*
3984eec411gordon * Function attributes, for various compilers.
3994eec411gordon */
4004eec411gordon#include "funcattrs.h"
4014eec411gordon
40208263d1delphij#ifndef min
40308263d1delphij#define min(a,b) ((a)>(b)?(b):(a))
40408263d1delphij#endif
40508263d1delphij#ifndef max
40608263d1delphij#define max(a,b) ((b)>(a)?(b):(a))
40708263d1delphij#endif
40808263d1delphij
409af736e6glebius#endif /* netdissect_stdinc_h */
410