xref: /netbsd-src/external/bsd/tcpdump/dist/netdissect-stdinc.h (revision c74ad2514f801c840bd3203347d1246afccaa98b)
1784088dfSchristos /*
2784088dfSchristos  * Copyright (c) 2002 - 2003
3784088dfSchristos  * NetGroup, Politecnico di Torino (Italy)
4784088dfSchristos  * All rights reserved.
5784088dfSchristos  *
6784088dfSchristos  * Redistribution and use in source and binary forms, with or without
7784088dfSchristos  * modification, are permitted provided that the following conditions
8784088dfSchristos  * are met:
9784088dfSchristos  *
10784088dfSchristos  * 1. Redistributions of source code must retain the above copyright
11784088dfSchristos  * notice, this list of conditions and the following disclaimer.
12784088dfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
13784088dfSchristos  * notice, this list of conditions and the following disclaimer in the
14784088dfSchristos  * documentation and/or other materials provided with the distribution.
15784088dfSchristos  * 3. Neither the name of the Politecnico di Torino nor the names of its
16784088dfSchristos  * contributors may be used to endorse or promote products derived from
17784088dfSchristos  * this software without specific prior written permission.
18784088dfSchristos  *
19784088dfSchristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20784088dfSchristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21784088dfSchristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22784088dfSchristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23784088dfSchristos  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24784088dfSchristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25784088dfSchristos  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26784088dfSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27784088dfSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28784088dfSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29784088dfSchristos  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30784088dfSchristos  */
31784088dfSchristos 
32784088dfSchristos /*
33784088dfSchristos  * Include the appropriate OS header files on Windows and various flavors
34784088dfSchristos  * of UNIX, include various non-OS header files on Windows, and define
35784088dfSchristos  * various items as needed, to isolate most of netdissect's platform
36784088dfSchristos  * differences to this one file.
37784088dfSchristos  */
38784088dfSchristos 
39784088dfSchristos #ifndef netdissect_stdinc_h
40784088dfSchristos #define netdissect_stdinc_h
41784088dfSchristos 
42*c74ad251Schristos #include "ftmacros.h"
43*c74ad251Schristos 
44784088dfSchristos #include <errno.h>
45784088dfSchristos 
46*c74ad251Schristos #include "compiler-tests.h"
47*c74ad251Schristos 
48*c74ad251Schristos #include "varattrs.h"
49*c74ad251Schristos 
50*c74ad251Schristos /*
51*c74ad251Schristos  * If we're compiling with Visual Studio, make sure we have at least
52*c74ad251Schristos  * VS 2015 or later, so we have sufficient C99 support.
53*c74ad251Schristos  *
54*c74ad251Schristos  * XXX - verify that we have at least C99 support on UN*Xes?
55*c74ad251Schristos  *
56*c74ad251Schristos  * What about MinGW or various DOS toolchains?  We're currently assuming
57*c74ad251Schristos  * sufficient C99 support there.
58*c74ad251Schristos  */
59*c74ad251Schristos #if defined(_MSC_VER)
60*c74ad251Schristos   /*
61*c74ad251Schristos    * Make sure we have VS 2015 or later.
62*c74ad251Schristos    */
63*c74ad251Schristos   #if _MSC_VER < 1900
64*c74ad251Schristos     #error "Building tcpdump requires VS 2015 or later"
65*c74ad251Schristos   #endif
66*c74ad251Schristos #endif
67*c74ad251Schristos 
68*c74ad251Schristos /*
69*c74ad251Schristos  * Get the C99 types, and the PRI[doux]64 format strings, defined.
70*c74ad251Schristos  */
71*c74ad251Schristos #ifdef HAVE_PCAP_PCAP_INTTYPES_H
72*c74ad251Schristos   /*
73*c74ad251Schristos    * We have pcap/pcap-inttypes.h; use that, as it'll do all the
74*c74ad251Schristos    * work, and won't cause problems if a file includes this file
75*c74ad251Schristos    * and later includes a pcap header file that also includes
76*c74ad251Schristos    * pcap/pcap-inttypes.h.
77*c74ad251Schristos    */
78*c74ad251Schristos   #include <pcap/pcap-inttypes.h>
79*c74ad251Schristos #else
80*c74ad251Schristos   /*
81*c74ad251Schristos    * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
82*c74ad251Schristos    * do the work ourselves, but at least we don't have to
83*c74ad251Schristos    * worry about other headers including it and causing
84*c74ad251Schristos    * clashes.
85*c74ad251Schristos    */
86*c74ad251Schristos 
87*c74ad251Schristos   /*
88*c74ad251Schristos    * Include <inttypes.h> to get the integer types and PRi[doux]64 values
89*c74ad251Schristos    * defined.
90*c74ad251Schristos    *
91*c74ad251Schristos    * If the compiler is MSVC, we require VS 2015 or newer, so we
92*c74ad251Schristos    * have <inttypes.h> - and support for %zu in the formatted
93*c74ad251Schristos    * printing functions.
94*c74ad251Schristos    *
95*c74ad251Schristos    * If the compiler is MinGW, we assume we have <inttypes.h> - and
96*c74ad251Schristos    * support for %zu in the formatted printing functions.
97*c74ad251Schristos    *
98*c74ad251Schristos    * If the target is UN*X, we assume we have a C99-or-later development
99*c74ad251Schristos    * environment, and thus have <inttypes.h> - and support for %zu in
100*c74ad251Schristos    * the formatted printing functions.
101*c74ad251Schristos    *
102*c74ad251Schristos    * If the target is MS-DOS, we assume we have <inttypes.h> - and support
103*c74ad251Schristos    * for %zu in the formatted printing functions.
104*c74ad251Schristos    */
105*c74ad251Schristos   #include <inttypes.h>
106*c74ad251Schristos 
107*c74ad251Schristos   #if defined(_MSC_VER)
108*c74ad251Schristos     /*
109*c74ad251Schristos      * Suppress definition of intN_t in bittypes.h, which might be included
110*c74ad251Schristos      * by <pcap/pcap.h> in older versions of WinPcap.
111*c74ad251Schristos      * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented.)
112*c74ad251Schristos      */
113*c74ad251Schristos     #define HAVE_U_INT8_T
114*c74ad251Schristos     #define HAVE_U_INT16_T
115*c74ad251Schristos     #define HAVE_U_INT32_T
116*c74ad251Schristos     #define HAVE_U_INT64_T
117*c74ad251Schristos   #endif
118*c74ad251Schristos #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
119*c74ad251Schristos 
120784088dfSchristos #ifdef _WIN32
121784088dfSchristos 
122784088dfSchristos /*
123784088dfSchristos  * Includes and definitions for Windows.
124784088dfSchristos  */
125784088dfSchristos 
126784088dfSchristos #include <stdio.h>
127784088dfSchristos #include <winsock2.h>
128784088dfSchristos #include <ws2tcpip.h>
129784088dfSchristos #include <time.h>
130784088dfSchristos #include <io.h>
131784088dfSchristos #include <fcntl.h>
132784088dfSchristos #include <sys/types.h>
133784088dfSchristos 
134*c74ad251Schristos #ifdef _MSC_VER
135*c74ad251Schristos   /*
136*c74ad251Schristos    * Compiler is MSVC.
137*c74ad251Schristos    *
138*c74ad251Schristos    * We require VS 2015 or newer, so we have strtoll().  Use that for
139*c74ad251Schristos    * strtoint64_t().
140*c74ad251Schristos    */
141*c74ad251Schristos   #define strtoint64_t	strtoll
142784088dfSchristos 
143784088dfSchristos   /*
144*c74ad251Schristos    * And we have LL as a suffix for constants, so use that.
145784088dfSchristos    */
146*c74ad251Schristos   #define INT64_T_CONSTANT(constant)	(constant##LL)
147*c74ad251Schristos #else
148*c74ad251Schristos   /*
149*c74ad251Schristos    * Non-Microsoft compiler.
150*c74ad251Schristos    *
151*c74ad251Schristos    * XXX - should we use strtoll or should we use _strtoi64()?
152*c74ad251Schristos    */
153*c74ad251Schristos   #define strtoint64_t		strtoll
154*c74ad251Schristos 
155*c74ad251Schristos   /*
156*c74ad251Schristos    * Assume LL works.
157*c74ad251Schristos    */
158*c74ad251Schristos   #define INT64_T_CONSTANT(constant)	(constant##LL)
159*c74ad251Schristos #endif
160784088dfSchristos 
161784088dfSchristos #ifdef _MSC_VER
162*c74ad251Schristos   /*
163*c74ad251Schristos    * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
164*c74ad251Schristos    * by adding a preceding underscore; we *want* the UN*Xisms, so add
165*c74ad251Schristos    * #defines to let us use them.
166*c74ad251Schristos    */
167*c74ad251Schristos   #define isatty _isatty
168784088dfSchristos   #define stat _stat
169*c74ad251Schristos   #define strdup _strdup
170784088dfSchristos   #define open _open
171784088dfSchristos   #define read _read
172784088dfSchristos   #define close _close
173784088dfSchristos   #define O_RDONLY _O_RDONLY
174*c74ad251Schristos 
175*c74ad251Schristos   /*
176*c74ad251Schristos    * We define our_fstat64 as _fstati64, and define our_statb as
177*c74ad251Schristos    * struct _stati64, so we get 64-bit file sizes.
178*c74ad251Schristos    */
179*c74ad251Schristos   #define our_fstat _fstati64
180*c74ad251Schristos   #define our_statb struct _stati64
181*c74ad251Schristos 
182*c74ad251Schristos   /*
183*c74ad251Schristos    * If <crtdbg.h> has been included, and _DEBUG is defined, and
184*c74ad251Schristos    * __STDC__ is zero, <crtdbg.h> will define strdup() to call
185*c74ad251Schristos    * _strdup_dbg().  So if it's already defined, don't redefine
186*c74ad251Schristos    * it.
187*c74ad251Schristos    */
188*c74ad251Schristos   #ifndef strdup
189*c74ad251Schristos     #define strdup _strdup
190*c74ad251Schristos   #endif
191*c74ad251Schristos 
192*c74ad251Schristos   /*
193*c74ad251Schristos    * Windows doesn't have ssize_t; routines such as _read() return int.
194*c74ad251Schristos    */
195*c74ad251Schristos   typedef int ssize_t;
196784088dfSchristos #endif  /* _MSC_VER */
197784088dfSchristos 
198784088dfSchristos /*
199784088dfSchristos  * With MSVC, for C, __inline is used to make a function an inline.
200784088dfSchristos  */
201784088dfSchristos #ifdef _MSC_VER
202784088dfSchristos #define inline __inline
203784088dfSchristos #endif
204784088dfSchristos 
205*c74ad251Schristos #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
206784088dfSchristos #define HAVE_OS_IPV6_SUPPORT
207784088dfSchristos #endif
208784088dfSchristos 
209784088dfSchristos #ifndef INET6_ADDRSTRLEN
210784088dfSchristos #define INET6_ADDRSTRLEN 46
211784088dfSchristos #endif
212784088dfSchristos 
213784088dfSchristos /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
214784088dfSchristos  */
215784088dfSchristos #ifndef EAFNOSUPPORT
216784088dfSchristos #define EAFNOSUPPORT WSAEAFNOSUPPORT
217784088dfSchristos #endif
218784088dfSchristos 
219784088dfSchristos #ifndef caddr_t
220784088dfSchristos typedef char *caddr_t;
221784088dfSchristos #endif /* caddr_t */
222784088dfSchristos 
223784088dfSchristos #define MAXHOSTNAMELEN	64
224784088dfSchristos 
225784088dfSchristos #else /* _WIN32 */
226784088dfSchristos 
227784088dfSchristos /*
228784088dfSchristos  * Includes and definitions for various flavors of UN*X.
229784088dfSchristos  */
230784088dfSchristos 
231784088dfSchristos #include <unistd.h>
232784088dfSchristos #include <netdb.h>
233784088dfSchristos #include <sys/param.h>
234784088dfSchristos #include <sys/types.h>			/* concession to AIX */
235784088dfSchristos #include <sys/time.h>
236784088dfSchristos #include <sys/socket.h>
237784088dfSchristos #include <netinet/in.h>
238784088dfSchristos 
239784088dfSchristos #include <time.h>
240784088dfSchristos 
241784088dfSchristos #include <arpa/inet.h>
242784088dfSchristos 
243*c74ad251Schristos /*
244*c74ad251Schristos  * We should have large file support enabled, if it's available,
245*c74ad251Schristos  * so just use fstat as our_fstat and struct stat as our_statb.
246*c74ad251Schristos  */
247*c74ad251Schristos #define our_fstat fstat
248*c74ad251Schristos #define our_statb struct stat
249784088dfSchristos 
250784088dfSchristos /*
251*c74ad251Schristos  * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
252784088dfSchristos  */
253*c74ad251Schristos #define strtoint64_t	strtoll
254*c74ad251Schristos 
255*c74ad251Schristos /*
256*c74ad251Schristos  * Assume LL works.
257*c74ad251Schristos  */
258*c74ad251Schristos #define INT64_T_CONSTANT(constant)	(constant##LL)
259*c74ad251Schristos #endif /* _WIN32 */
260*c74ad251Schristos 
261*c74ad251Schristos /*
262*c74ad251Schristos  * Function attributes, for various compilers.
263*c74ad251Schristos  */
264*c74ad251Schristos #include "funcattrs.h"
265784088dfSchristos 
266784088dfSchristos /*
267784088dfSchristos  * fopen() read and write modes for text files and binary files.
268784088dfSchristos  */
269784088dfSchristos #if defined(_WIN32) || defined(MSDOS)
270784088dfSchristos   #define FOPEN_READ_TXT   "rt"
271784088dfSchristos   #define FOPEN_READ_BIN   "rb"
272784088dfSchristos   #define FOPEN_WRITE_TXT  "wt"
273784088dfSchristos   #define FOPEN_WRITE_BIN  "wb"
274784088dfSchristos #else
275784088dfSchristos   #define FOPEN_READ_TXT   "r"
276784088dfSchristos   #define FOPEN_READ_BIN   FOPEN_READ_TXT
277784088dfSchristos   #define FOPEN_WRITE_TXT  "w"
278784088dfSchristos   #define FOPEN_WRITE_BIN  FOPEN_WRITE_TXT
279784088dfSchristos #endif
280784088dfSchristos 
281784088dfSchristos /*
282784088dfSchristos  * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
283784088dfSchristos  * defined if the OS doesn't provide them.  These assume no more than
284784088dfSchristos  * an 80386, so, for example, it avoids the bswap instruction added in
285784088dfSchristos  * the 80486.
286784088dfSchristos  *
287*c74ad251Schristos  * (We don't use them on macOS; Apple provides their own, which *doesn't*
288*c74ad251Schristos  * avoid the bswap instruction, as macOS only supports machines that
289784088dfSchristos  * have it.)
290784088dfSchristos  */
29100db07f7Schristos #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl) && !defined(ntohl)
292784088dfSchristos   #undef ntohl
293784088dfSchristos   #undef ntohs
294784088dfSchristos   #undef htonl
295784088dfSchristos   #undef htons
296784088dfSchristos 
29700db07f7Schristos   static __inline__ unsigned int __ntohl (unsigned int x);
298784088dfSchristos   static __inline__ unsigned short __ntohs (unsigned short x);
299784088dfSchristos 
300784088dfSchristos   #define ntohl(x)  __ntohl(x)
301784088dfSchristos   #define ntohs(x)  __ntohs(x)
302784088dfSchristos   #define htonl(x)  __ntohl(x)
303784088dfSchristos   #define htons(x)  __ntohs(x)
304784088dfSchristos 
__ntohl(unsigned int x)30500db07f7Schristos   static __inline__ unsigned int __ntohl (unsigned int x)
306784088dfSchristos   {
307784088dfSchristos     __asm__ ("xchgb %b0, %h0\n\t"   /* swap lower bytes  */
308784088dfSchristos              "rorl  $16, %0\n\t"    /* swap words        */
309784088dfSchristos              "xchgb %b0, %h0"       /* swap higher bytes */
310784088dfSchristos             : "=q" (x) : "0" (x));
311784088dfSchristos     return (x);
312784088dfSchristos   }
313784088dfSchristos 
__ntohs(unsigned short x)314784088dfSchristos   static __inline__ unsigned short __ntohs (unsigned short x)
315784088dfSchristos   {
316784088dfSchristos     __asm__ ("xchgb %b0, %h0"       /* swap bytes */
317784088dfSchristos             : "=q" (x) : "0" (x));
318784088dfSchristos     return (x);
319784088dfSchristos   }
320784088dfSchristos #endif
321784088dfSchristos 
322784088dfSchristos /*
323784088dfSchristos  * If the OS doesn't define AF_INET6 and struct in6_addr:
324784088dfSchristos  *
325784088dfSchristos  * define AF_INET6, so we can use it internally as a "this is an
326784088dfSchristos  * IPv6 address" indication;
327784088dfSchristos  *
328784088dfSchristos  * define struct in6_addr so that we can use it for IPv6 addresses.
329784088dfSchristos  */
330784088dfSchristos #ifndef HAVE_OS_IPV6_SUPPORT
331784088dfSchristos #ifndef AF_INET6
332784088dfSchristos #define AF_INET6	24
333784088dfSchristos 
334784088dfSchristos struct in6_addr {
335784088dfSchristos 	union {
336784088dfSchristos 		__uint8_t   __u6_addr8[16];
337784088dfSchristos 		__uint16_t  __u6_addr16[8];
338784088dfSchristos 		__uint32_t  __u6_addr32[4];
339784088dfSchristos 	} __u6_addr;			/* 128-bit IP6 address */
340784088dfSchristos };
341784088dfSchristos #endif
342784088dfSchristos #endif
343784088dfSchristos 
344784088dfSchristos #ifndef NI_MAXHOST
345784088dfSchristos #define	NI_MAXHOST	1025
346784088dfSchristos #endif
347784088dfSchristos 
348784088dfSchristos #ifndef INET_ADDRSTRLEN
349784088dfSchristos #define INET_ADDRSTRLEN 16
350784088dfSchristos #endif
351784088dfSchristos 
352784088dfSchristos #ifndef TRUE
353784088dfSchristos #define TRUE 1
354784088dfSchristos #endif
355784088dfSchristos 
356784088dfSchristos #ifndef FALSE
357784088dfSchristos #define FALSE 0
358784088dfSchristos #endif
359784088dfSchristos 
360784088dfSchristos /*
361*c74ad251Schristos  * Statement attributes, for various compilers.
362*c74ad251Schristos  *
363*c74ad251Schristos  * This was introduced sufficiently recently that compilers implementing
364*c74ad251Schristos  * it also implement __has_attribute() (for example, GCC 5.0 and later
365*c74ad251Schristos  * have __has_attribute(), and the "fallthrough" attribute was introduced
366*c74ad251Schristos  * in GCC 7).
367*c74ad251Schristos  *
368*c74ad251Schristos  * Unfortunately, Clang does this wrong - a statement
369*c74ad251Schristos  *
370*c74ad251Schristos  *    __attribute__ ((fallthrough));
371*c74ad251Schristos  *
372*c74ad251Schristos  * produces bogus -Wmissing-declaration "declaration does not declare
373*c74ad251Schristos  * anything" warnings (dear Clang: that's not a declaration, it's an
374*c74ad251Schristos  * empty statement).  GCC, however, has no trouble with this.
375784088dfSchristos  */
376*c74ad251Schristos #if __has_attribute(fallthrough) && !defined(__clang__)
377*c74ad251Schristos #  define ND_FALL_THROUGH __attribute__ ((fallthrough))
378784088dfSchristos #else
379*c74ad251Schristos #  define ND_FALL_THROUGH
380*c74ad251Schristos #endif /*  __has_attribute(fallthrough) */
381784088dfSchristos 
382784088dfSchristos #endif /* netdissect_stdinc_h */
383