]> The Tcpdump Group git mirrors - tcpdump/blob - tcpdump-stdinc.h
eb6c9428d9064260a68948128a3a8df3921fa656
[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 * @(#) $Header: /tcpdump/master/tcpdump/tcpdump-stdinc.h,v 1.18 2007-11-24 18:13:33 mcr Exp $ (LBL)
33 */
34
35 /*
36 * Include the appropriate OS header files on Windows and various flavors
37 * of UNIX, and also define some additional items and include various
38 * non-OS header files on Windows, and; this isolates most of the platform
39 * differences to this one file.
40 */
41
42 #ifndef tcpdump_stdinc_h
43 #define tcpdump_stdinc_h
44
45 #ifdef WIN32
46
47 #include <stdio.h>
48 #include <winsock2.h>
49 #include <Ws2tcpip.h>
50 #include "bittypes.h"
51 #include <ctype.h>
52 #include <time.h>
53 #include <io.h>
54 #include <fcntl.h>
55 #include <sys/types.h>
56 #include <net/netdb.h> /* in wpcap's Win32/include */
57
58 #ifndef NBBY
59 #define NBBY 8
60 #endif
61
62 #if !defined(__MINGW32__) && !defined(__WATCOMC__)
63 #undef toascii
64 #define isascii __isascii
65 #define toascii __toascii
66 #define stat _stat
67 #define open _open
68 #define fstat _fstat
69 #define read _read
70 #define close _close
71 #define O_RDONLY _O_RDONLY
72 #endif /* __MINGW32__ */
73
74 #ifdef __MINGW32__
75 #include <stdint.h>
76 #endif
77
78 /* Protos for missing/x.c functions (ideally <missing/addrinfo.h>
79 * should be used, but it clashes with <ws2tcpip.h>).
80 */
81 extern const char *inet_ntop (int, const void *, char *, size_t);
82 extern int inet_pton (int, const char *, void *);
83 extern int inet_aton (const char *cp, struct in_addr *addr);
84
85 /*
86 * With MSVC, for C, __inline is used to make a function an inline.
87 */
88 #ifdef _MSC_VER
89 #define inline __inline
90 #endif
91
92 #ifndef INET6_ADDRSTRLEN
93 #define INET6_ADDRSTRLEN 46
94 #endif
95
96 #ifndef toascii
97 #define toascii(c) ((c) & 0x7f)
98 #endif
99
100 #ifndef caddr_t
101 typedef char* caddr_t;
102 #endif /* caddr_t */
103
104 #define MAXHOSTNAMELEN 64
105 #define NI_MAXHOST 1025
106 #define snprintf _snprintf
107 #define vsnprintf _vsnprintf
108 #define RETSIGTYPE void
109
110 #else /* WIN32 */
111
112 #include <ctype.h>
113 #include <unistd.h>
114 #include <netdb.h>
115 #if HAVE_INTTYPES_H
116 #include <inttypes.h>
117 #else
118 #if HAVE_STDINT_H
119 #include <stdint.h>
120 #endif
121 #endif
122 #ifdef HAVE_SYS_BITYPES_H
123 #include <sys/bitypes.h>
124 #endif
125 #include <sys/param.h>
126 #include <sys/types.h> /* concession to AIX */
127 #include <sys/time.h>
128 #include <sys/socket.h>
129 #include <netinet/in.h>
130
131 #ifdef TIME_WITH_SYS_TIME
132 #include <time.h>
133 #endif
134
135 #include <arpa/inet.h>
136
137 #endif /* WIN32 */
138
139 #ifndef HAVE___ATTRIBUTE__
140 #define __attribute__(x)
141 #endif
142
143 /*
144 * Used to declare a structure unaligned, so that the C compiler,
145 * if necessary, generates code that doesn't assume alignment.
146 * This is required because there is no guarantee that the packet
147 * data we get from libpcap/WinPcap is properly aligned.
148 *
149 * This assumes that, for all compilers that support __attribute__:
150 *
151 * 1) they support __attribute__((packed));
152 *
153 * 2) for all instruction set architectures requiring strict
154 * alignment, declaring a structure with that attribute
155 * causes the compiler to generate code that handles
156 * misaligned 2-byte, 4-byte, and 8-byte integral
157 * quantities.
158 *
159 * It does not (yet) handle compilers where you can get the compiler
160 * to generate code of that sort by some other means.
161 *
162 * This is required in order to, for example, keep the compiler from
163 * generating, for
164 *
165 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
166 *
167 * in print-bootp.c, code that loads the first 4-byte word of a
168 * "struct bootp", masking out the bp_hops field, and comparing the result
169 * against 0x01010600.
170 *
171 * Note: this also requires that padding be put into the structure,
172 * at least for compilers where it's implemented as __attribute__((packed)).
173 */
174 #if defined(_MSC_VER) && defined(UNALIGNED)
175 /* MSVC may have its own macro defined with the same name and purpose. */
176 #else
177 #define UNALIGNED __attribute__((packed))
178 #endif
179
180 #if defined(WIN32) || defined(MSDOS)
181 #define FOPEN_READ_TXT "rt"
182 #define FOPEN_READ_BIN "rb"
183 #define FOPEN_WRITE_TXT "wt"
184 #define FOPEN_WRITE_BIN "wb"
185 #else
186 #define FOPEN_READ_TXT "r"
187 #define FOPEN_READ_BIN FOPEN_READ_TXT
188 #define FOPEN_WRITE_TXT "w"
189 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
190 #endif
191
192 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
193 #undef ntohl
194 #undef ntohs
195 #undef htonl
196 #undef htons
197
198 static __inline__ unsigned long __ntohl (unsigned long x);
199 static __inline__ unsigned short __ntohs (unsigned short x);
200
201 #define ntohl(x) __ntohl(x)
202 #define ntohs(x) __ntohs(x)
203 #define htonl(x) __ntohl(x)
204 #define htons(x) __ntohs(x)
205
206 static __inline__ unsigned long __ntohl (unsigned long x)
207 {
208 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
209 "rorl $16, %0\n\t" /* swap words */
210 "xchgb %b0, %h0" /* swap higher bytes */
211 : "=q" (x) : "0" (x));
212 return (x);
213 }
214
215 static __inline__ unsigned short __ntohs (unsigned short x)
216 {
217 __asm__ ("xchgb %b0, %h0" /* swap bytes */
218 : "=q" (x) : "0" (x));
219 return (x);
220 }
221 #endif
222
223 #ifndef INET_ADDRSTRLEN
224 #define INET_ADDRSTRLEN 16
225 #endif
226
227 #ifndef TRUE
228 #define TRUE 1
229 #endif
230
231 #ifndef FALSE
232 #define FALSE 0
233 #endif
234
235 #endif /* tcpdump_stdinc_h */