1127668Sbms/*
2127668Sbms * Copyright (c) 2002 - 2003
3127668Sbms * NetGroup, Politecnico di Torino (Italy)
4127668Sbms * All rights reserved.
5127668Sbms *
6127668Sbms * Redistribution and use in source and binary forms, with or without
7127668Sbms * modification, are permitted provided that the following conditions
8127668Sbms * are met:
9127668Sbms *
10127668Sbms * 1. Redistributions of source code must retain the above copyright
11127668Sbms * notice, this list of conditions and the following disclaimer.
12127668Sbms * 2. Redistributions in binary form must reproduce the above copyright
13127668Sbms * notice, this list of conditions and the following disclaimer in the
14127668Sbms * documentation and/or other materials provided with the distribution.
15127668Sbms * 3. Neither the name of the Politecnico di Torino nor the names of its
16127668Sbms * contributors may be used to endorse or promote products derived from
17127668Sbms * this software without specific prior written permission.
18127668Sbms *
19127668Sbms * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20127668Sbms * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21127668Sbms * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22127668Sbms * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23127668Sbms * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24127668Sbms * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25127668Sbms * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26127668Sbms * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27127668Sbms * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28127668Sbms * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29127668Sbms * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30127668Sbms *
31127668Sbms *
32214478Srpaulo * @(#) $Header: /tcpdump/master/tcpdump/tcpdump-stdinc.h,v 1.18 2007-11-24 18:13:33 mcr Exp $ (LBL)
33127668Sbms */
34127668Sbms
35127668Sbms/*
36127668Sbms * Include the appropriate OS header files on Windows and various flavors
37127668Sbms * of UNIX, and also define some additional items and include various
38127668Sbms * non-OS header files on Windows, and; this isolates most of the platform
39127668Sbms * differences to this one file.
40127668Sbms */
41127668Sbms
42127668Sbms#ifndef tcpdump_stdinc_h
43127668Sbms#define tcpdump_stdinc_h
44127668Sbms
45127668Sbms#ifdef WIN32
46127668Sbms
47127668Sbms#include <stdio.h>
48127668Sbms#include <winsock2.h>
49162021Ssam#include <Ws2tcpip.h>
50127668Sbms#include "bittypes.h"
51127668Sbms#include <ctype.h>
52127668Sbms#include <time.h>
53127668Sbms#include <io.h>
54127668Sbms#include <fcntl.h>
55146773Ssam#include <sys/types.h>
56146773Ssam#include <net/netdb.h>  /* in wpcap's Win32/include */
57127668Sbms
58214478Srpaulo#ifndef NBBY
59214478Srpaulo#define NBBY	8
60214478Srpaulo#endif
61214478Srpaulo
62147904Ssam#if !defined(__MINGW32__) && !defined(__WATCOMC__)
63147904Ssam#undef toascii
64147904Ssam#define isascii __isascii
65147904Ssam#define toascii __toascii
66147904Ssam#define stat _stat
67147904Ssam#define open _open
68147904Ssam#define fstat _fstat
69147904Ssam#define read _read
70147904Ssam#define close _close
71147904Ssam#define O_RDONLY _O_RDONLY
72147904Ssam
73147904Ssamtypedef short ino_t;
74147904Ssam#endif /* __MINGW32__ */
75147904Ssam
76127668Sbms#ifdef __MINGW32__
77127668Sbms#include <stdint.h>
78146773Ssam#endif
79127668Sbms
80146773Ssam/* Protos for missing/x.c functions (ideally <missing/addrinfo.h>
81146773Ssam * should be used, but it clashes with <ws2tcpip.h>).
82146773Ssam */
83146773Ssamextern const char *inet_ntop (int, const void *, char *, size_t);
84146773Ssamextern int inet_pton (int, const char *, void *);
85146773Ssamextern int inet_aton (const char *cp, struct in_addr *addr);
86146773Ssam
87146773Ssam#ifndef INET6_ADDRSTRLEN
88127668Sbms#define INET6_ADDRSTRLEN 46
89146773Ssam#endif
90127668Sbms
91127668Sbms#ifndef toascii
92127668Sbms#define toascii(c) ((c) & 0x7f)
93127668Sbms#endif
94127668Sbms
95127668Sbms#ifndef caddr_t
96127668Sbmstypedef char* caddr_t;
97127668Sbms#endif /* caddr_t */
98127668Sbms
99127668Sbms#define MAXHOSTNAMELEN	64
100127668Sbms#define	NI_MAXHOST	1025
101127668Sbms#define snprintf _snprintf
102127668Sbms#define vsnprintf _vsnprintf
103127668Sbms#define RETSIGTYPE void
104127668Sbms
105127668Sbms#else /* WIN32 */
106127668Sbms
107127668Sbms#include <ctype.h>
108127668Sbms#include <unistd.h>
109127668Sbms#include <netdb.h>
110147904Ssam#if HAVE_INTTYPES_H
111146773Ssam#include <inttypes.h>
112147904Ssam#else
113147904Ssam#if HAVE_STDINT_H
114147904Ssam#include <stdint.h>
115146773Ssam#endif
116147904Ssam#endif
117147904Ssam#ifdef HAVE_SYS_BITYPES_H
118147904Ssam#include <sys/bitypes.h>
119147904Ssam#endif
120127668Sbms#include <sys/param.h>
121127668Sbms#include <sys/types.h>			/* concession to AIX */
122127668Sbms#include <sys/time.h>
123127668Sbms#include <sys/socket.h>
124127668Sbms#include <netinet/in.h>
125127668Sbms
126127668Sbms#ifdef TIME_WITH_SYS_TIME
127127668Sbms#include <time.h>
128127668Sbms#endif
129127668Sbms
130127668Sbms#include <arpa/inet.h>
131127668Sbms
132127668Sbms#endif /* WIN32 */
133127668Sbms
134235530Sdelphij#ifndef HAVE___ATTRIBUTE__
135235530Sdelphij#define __attribute__(x)
136127668Sbms#endif
137127668Sbms
138235530Sdelphij/*
139235530Sdelphij * Used to declare a structure unaligned, so that the C compiler,
140235530Sdelphij * if necessary, generates code that doesn't assume alignment.
141235530Sdelphij * This is required because there is no guarantee that the packet
142235530Sdelphij * data we get from libpcap/WinPcap is properly aligned.
143235530Sdelphij *
144235530Sdelphij * This assumes that, for all compilers that support __attribute__:
145235530Sdelphij *
146235530Sdelphij *	1) they support __attribute__((packed));
147235530Sdelphij *
148235530Sdelphij *	2) for all instruction set architectures requiring strict
149235530Sdelphij *	   alignment, declaring a structure with that attribute
150235530Sdelphij *	   causes the compiler to generate code that handles
151235530Sdelphij *	   misaligned 2-byte, 4-byte, and 8-byte integral
152235530Sdelphij *	   quantities.
153235530Sdelphij *
154235530Sdelphij * It does not (yet) handle compilers where you can get the compiler
155235530Sdelphij * to generate code of that sort by some other means.
156235530Sdelphij *
157235530Sdelphij * This is required in order to, for example, keep the compiler from
158235530Sdelphij * generating, for
159235530Sdelphij *
160235530Sdelphij *	if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
161235530Sdelphij *
162235530Sdelphij * in print-bootp.c, code that loads the first 4-byte word of a
163235530Sdelphij * "struct bootp", masking out the bp_hops field, and comparing the result
164235530Sdelphij * against 0x01010600.
165235530Sdelphij *
166235530Sdelphij * Note: this also requires that padding be put into the structure,
167235530Sdelphij * at least for compilers where it's implemented as __attribute__((packed)).
168235530Sdelphij */
169235530Sdelphij#define UNALIGNED	__attribute__((packed))
170235530Sdelphij
171127668Sbms#if defined(WIN32) || defined(MSDOS)
172127668Sbms  #define FOPEN_READ_TXT   "rt"
173127668Sbms  #define FOPEN_READ_BIN   "rb"
174127668Sbms  #define FOPEN_WRITE_TXT  "wt"
175127668Sbms  #define FOPEN_WRITE_BIN  "wb"
176127668Sbms#else
177127668Sbms  #define FOPEN_READ_TXT   "r"
178127668Sbms  #define FOPEN_READ_BIN   FOPEN_READ_TXT
179127668Sbms  #define FOPEN_WRITE_TXT  "w"
180127668Sbms  #define FOPEN_WRITE_BIN  FOPEN_WRITE_TXT
181127668Sbms#endif
182127668Sbms
183235530Sdelphij#if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
184214478Srpaulo  #undef ntohl
185214478Srpaulo  #undef ntohs
186214478Srpaulo  #undef htonl
187214478Srpaulo  #undef htons
188214478Srpaulo
189214478Srpaulo  static __inline__ unsigned long __ntohl (unsigned long x);
190214478Srpaulo  static __inline__ unsigned short __ntohs (unsigned short x);
191214478Srpaulo
192214478Srpaulo  #define ntohl(x)  __ntohl(x)
193214478Srpaulo  #define ntohs(x)  __ntohs(x)
194214478Srpaulo  #define htonl(x)  __ntohl(x)
195214478Srpaulo  #define htons(x)  __ntohs(x)
196214478Srpaulo
197214478Srpaulo  static __inline__ unsigned long __ntohl (unsigned long x)
198214478Srpaulo  {
199214478Srpaulo    __asm__ ("xchgb %b0, %h0\n\t"   /* swap lower bytes  */
200214478Srpaulo             "rorl  $16, %0\n\t"    /* swap words        */
201214478Srpaulo             "xchgb %b0, %h0"       /* swap higher bytes */
202214478Srpaulo            : "=q" (x) : "0" (x));
203214478Srpaulo    return (x);
204214478Srpaulo  }
205214478Srpaulo
206214478Srpaulo  static __inline__ unsigned short __ntohs (unsigned short x)
207214478Srpaulo  {
208214478Srpaulo    __asm__ ("xchgb %b0, %h0"       /* swap bytes */
209214478Srpaulo            : "=q" (x) : "0" (x));
210214478Srpaulo    return (x);
211214478Srpaulo  }
212214478Srpaulo#endif
213214478Srpaulo
214146773Ssam#ifndef INET_ADDRSTRLEN
215146773Ssam#define INET_ADDRSTRLEN 16
216146773Ssam#endif
217146773Ssam
218172686Smlaier#ifndef TRUE
219172686Smlaier#define TRUE 1
220172686Smlaier#endif
221172686Smlaier
222172686Smlaier#ifndef FALSE
223172686Smlaier#define FALSE 0
224172686Smlaier#endif
225172686Smlaier
226127668Sbms#endif /* tcpdump_stdinc_h */
227