Correct math and limerick.
[dragonfly.git] / contrib / tcpdump / tcpdump-stdinc.h
blobd350d1be126b3c7529162fc95eeef83434cd8be2
1 /*
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
32 * @(#) $Header: /tcpdump/master/tcpdump/tcpdump-stdinc.h,v 1.18 2007-11-24 18:13:33 mcr Exp $ (LBL)
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.
42 #ifndef tcpdump_stdinc_h
43 #define tcpdump_stdinc_h
45 #ifdef WIN32
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 */
58 #ifndef NBBY
59 #define NBBY 8
60 #endif
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
73 typedef short ino_t;
74 #endif /* __MINGW32__ */
76 #ifdef __MINGW32__
77 #include <stdint.h>
78 #endif
80 /* Protos for missing/x.c functions (ideally <missing/addrinfo.h>
81 * should be used, but it clashes with <ws2tcpip.h>).
83 extern const char *inet_ntop (int, const void *, char *, size_t);
84 extern int inet_pton (int, const char *, void *);
85 extern int inet_aton (const char *cp, struct in_addr *addr);
87 #ifndef INET6_ADDRSTRLEN
88 #define INET6_ADDRSTRLEN 46
89 #endif
91 #ifndef toascii
92 #define toascii(c) ((c) & 0x7f)
93 #endif
95 #ifndef caddr_t
96 typedef char* caddr_t;
97 #endif /* caddr_t */
99 #define MAXHOSTNAMELEN 64
100 #define NI_MAXHOST 1025
101 #define snprintf _snprintf
102 #define vsnprintf _vsnprintf
103 #define RETSIGTYPE void
105 #else /* WIN32 */
107 #include <ctype.h>
108 #include <unistd.h>
109 #include <netdb.h>
110 #if HAVE_INTTYPES_H
111 #include <inttypes.h>
112 #else
113 #if HAVE_STDINT_H
114 #include <stdint.h>
115 #endif
116 #endif
117 #ifdef HAVE_SYS_BITYPES_H
118 #include <sys/bitypes.h>
119 #endif
120 #include <sys/param.h>
121 #include <sys/types.h> /* concession to AIX */
122 #include <sys/time.h>
123 #include <sys/socket.h>
124 #include <netinet/in.h>
126 #ifdef TIME_WITH_SYS_TIME
127 #include <time.h>
128 #endif
130 #include <arpa/inet.h>
132 #endif /* WIN32 */
134 #ifndef HAVE___ATTRIBUTE__
135 #define __attribute__(x)
136 #endif
139 * Used to declare a structure unaligned, so that the C compiler,
140 * if necessary, generates code that doesn't assume alignment.
141 * This is required because there is no guarantee that the packet
142 * data we get from libpcap/WinPcap is properly aligned.
144 * This assumes that, for all compilers that support __attribute__:
146 * 1) they support __attribute__((packed));
148 * 2) for all instruction set architectures requiring strict
149 * alignment, declaring a structure with that attribute
150 * causes the compiler to generate code that handles
151 * misaligned 2-byte, 4-byte, and 8-byte integral
152 * quantities.
154 * It does not (yet) handle compilers where you can get the compiler
155 * to generate code of that sort by some other means.
157 * This is required in order to, for example, keep the compiler from
158 * generating, for
160 * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
162 * in print-bootp.c, code that loads the first 4-byte word of a
163 * "struct bootp", masking out the bp_hops field, and comparing the result
164 * against 0x01010600.
166 * Note: this also requires that padding be put into the structure,
167 * at least for compilers where it's implemented as __attribute__((packed)).
169 #define UNALIGNED __attribute__((packed))
171 #if defined(WIN32) || defined(MSDOS)
172 #define FOPEN_READ_TXT "rt"
173 #define FOPEN_READ_BIN "rb"
174 #define FOPEN_WRITE_TXT "wt"
175 #define FOPEN_WRITE_BIN "wb"
176 #else
177 #define FOPEN_READ_TXT "r"
178 #define FOPEN_READ_BIN FOPEN_READ_TXT
179 #define FOPEN_WRITE_TXT "w"
180 #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
181 #endif
183 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
184 #undef ntohl
185 #undef ntohs
186 #undef htonl
187 #undef htons
189 static __inline__ unsigned long __ntohl (unsigned long x);
190 static __inline__ unsigned short __ntohs (unsigned short x);
192 #define ntohl(x) __ntohl(x)
193 #define ntohs(x) __ntohs(x)
194 #define htonl(x) __ntohl(x)
195 #define htons(x) __ntohs(x)
197 static __inline__ unsigned long __ntohl (unsigned long x)
199 __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
200 "rorl $16, %0\n\t" /* swap words */
201 "xchgb %b0, %h0" /* swap higher bytes */
202 : "=q" (x) : "0" (x));
203 return (x);
206 static __inline__ unsigned short __ntohs (unsigned short x)
208 __asm__ ("xchgb %b0, %h0" /* swap bytes */
209 : "=q" (x) : "0" (x));
210 return (x);
212 #endif
214 #ifndef INET_ADDRSTRLEN
215 #define INET_ADDRSTRLEN 16
216 #endif
218 #ifndef TRUE
219 #define TRUE 1
220 #endif
222 #ifndef FALSE
223 #define FALSE 0
224 #endif
226 #endif /* tcpdump_stdinc_h */