xref: /dflybsd-src/contrib/tcpdump/netdissect-stdinc.h (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
1411677aeSAaron LI /*
2411677aeSAaron LI  * Copyright (c) 2002 - 2003
3411677aeSAaron LI  * NetGroup, Politecnico di Torino (Italy)
4411677aeSAaron LI  * All rights reserved.
5411677aeSAaron LI  *
6411677aeSAaron LI  * Redistribution and use in source and binary forms, with or without
7411677aeSAaron LI  * modification, are permitted provided that the following conditions
8411677aeSAaron LI  * are met:
9411677aeSAaron LI  *
10411677aeSAaron LI  * 1. Redistributions of source code must retain the above copyright
11411677aeSAaron LI  * notice, this list of conditions and the following disclaimer.
12411677aeSAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
13411677aeSAaron LI  * notice, this list of conditions and the following disclaimer in the
14411677aeSAaron LI  * documentation and/or other materials provided with the distribution.
15411677aeSAaron LI  * 3. Neither the name of the Politecnico di Torino nor the names of its
16411677aeSAaron LI  * contributors may be used to endorse or promote products derived from
17411677aeSAaron LI  * this software without specific prior written permission.
18411677aeSAaron LI  *
19411677aeSAaron LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20411677aeSAaron LI  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21411677aeSAaron LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22411677aeSAaron LI  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23411677aeSAaron LI  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24411677aeSAaron LI  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25411677aeSAaron LI  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26411677aeSAaron LI  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27411677aeSAaron LI  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28411677aeSAaron LI  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29411677aeSAaron LI  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30411677aeSAaron LI  */
31411677aeSAaron LI 
32411677aeSAaron LI /*
33411677aeSAaron LI  * Include the appropriate OS header files on Windows and various flavors
34411677aeSAaron LI  * of UNIX, include various non-OS header files on Windows, and define
35411677aeSAaron LI  * various items as needed, to isolate most of netdissect's platform
36411677aeSAaron LI  * differences to this one file.
37411677aeSAaron LI  */
38411677aeSAaron LI 
39411677aeSAaron LI #ifndef netdissect_stdinc_h
40411677aeSAaron LI #define netdissect_stdinc_h
41411677aeSAaron LI 
42*ed775ee7SAntonio Huete Jimenez #include "ftmacros.h"
43*ed775ee7SAntonio Huete Jimenez 
44411677aeSAaron LI #include <errno.h>
45411677aeSAaron LI 
46*ed775ee7SAntonio Huete Jimenez #include "compiler-tests.h"
47*ed775ee7SAntonio Huete Jimenez 
48*ed775ee7SAntonio Huete Jimenez #include "varattrs.h"
49*ed775ee7SAntonio Huete Jimenez 
50*ed775ee7SAntonio Huete Jimenez /*
51*ed775ee7SAntonio Huete Jimenez  * If we're compiling with Visual Studio, make sure we have at least
52*ed775ee7SAntonio Huete Jimenez  * VS 2015 or later, so we have sufficient C99 support.
53*ed775ee7SAntonio Huete Jimenez  *
54*ed775ee7SAntonio Huete Jimenez  * XXX - verify that we have at least C99 support on UN*Xes?
55*ed775ee7SAntonio Huete Jimenez  *
56*ed775ee7SAntonio Huete Jimenez  * What about MinGW or various DOS toolchains?  We're currently assuming
57*ed775ee7SAntonio Huete Jimenez  * sufficient C99 support there.
58*ed775ee7SAntonio Huete Jimenez  */
59*ed775ee7SAntonio Huete Jimenez #if defined(_MSC_VER)
60*ed775ee7SAntonio Huete Jimenez   /*
61*ed775ee7SAntonio Huete Jimenez    * Make sure we have VS 2015 or later.
62*ed775ee7SAntonio Huete Jimenez    */
63*ed775ee7SAntonio Huete Jimenez   #if _MSC_VER < 1900
64*ed775ee7SAntonio Huete Jimenez     #error "Building tcpdump requires VS 2015 or later"
65*ed775ee7SAntonio Huete Jimenez   #endif
66*ed775ee7SAntonio Huete Jimenez #endif
67*ed775ee7SAntonio Huete Jimenez 
68*ed775ee7SAntonio Huete Jimenez /*
69*ed775ee7SAntonio Huete Jimenez  * Get the C99 types, and the PRI[doux]64 format strings, defined.
70*ed775ee7SAntonio Huete Jimenez  */
71*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_PCAP_PCAP_INTTYPES_H
72*ed775ee7SAntonio Huete Jimenez   /*
73*ed775ee7SAntonio Huete Jimenez    * We have pcap/pcap-inttypes.h; use that, as it'll do all the
74*ed775ee7SAntonio Huete Jimenez    * work, and won't cause problems if a file includes this file
75*ed775ee7SAntonio Huete Jimenez    * and later includes a pcap header file that also includes
76*ed775ee7SAntonio Huete Jimenez    * pcap/pcap-inttypes.h.
77*ed775ee7SAntonio Huete Jimenez    */
78*ed775ee7SAntonio Huete Jimenez   #include <pcap/pcap-inttypes.h>
79*ed775ee7SAntonio Huete Jimenez #else
80*ed775ee7SAntonio Huete Jimenez   /*
81*ed775ee7SAntonio Huete Jimenez    * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
82*ed775ee7SAntonio Huete Jimenez    * do the work ourselves, but at least we don't have to
83*ed775ee7SAntonio Huete Jimenez    * worry about other headers including it and causing
84*ed775ee7SAntonio Huete Jimenez    * clashes.
85*ed775ee7SAntonio Huete Jimenez    */
86*ed775ee7SAntonio Huete Jimenez 
87*ed775ee7SAntonio Huete Jimenez   /*
88*ed775ee7SAntonio Huete Jimenez    * Include <inttypes.h> to get the integer types and PRi[doux]64 values
89*ed775ee7SAntonio Huete Jimenez    * defined.
90*ed775ee7SAntonio Huete Jimenez    *
91*ed775ee7SAntonio Huete Jimenez    * If the compiler is MSVC, we require VS 2015 or newer, so we
92*ed775ee7SAntonio Huete Jimenez    * have <inttypes.h> - and support for %zu in the formatted
93*ed775ee7SAntonio Huete Jimenez    * printing functions.
94*ed775ee7SAntonio Huete Jimenez    *
95*ed775ee7SAntonio Huete Jimenez    * If the compiler is MinGW, we assume we have <inttypes.h> - and
96*ed775ee7SAntonio Huete Jimenez    * support for %zu in the formatted printing functions.
97*ed775ee7SAntonio Huete Jimenez    *
98*ed775ee7SAntonio Huete Jimenez    * If the target is UN*X, we assume we have a C99-or-later development
99*ed775ee7SAntonio Huete Jimenez    * environment, and thus have <inttypes.h> - and support for %zu in
100*ed775ee7SAntonio Huete Jimenez    * the formatted printing functions.
101*ed775ee7SAntonio Huete Jimenez    *
102*ed775ee7SAntonio Huete Jimenez    * If the target is MS-DOS, we assume we have <inttypes.h> - and support
103*ed775ee7SAntonio Huete Jimenez    * for %zu in the formatted printing functions.
104*ed775ee7SAntonio Huete Jimenez    */
105*ed775ee7SAntonio Huete Jimenez   #include <inttypes.h>
106*ed775ee7SAntonio Huete Jimenez 
107*ed775ee7SAntonio Huete Jimenez   #if defined(_MSC_VER)
108*ed775ee7SAntonio Huete Jimenez     /*
109*ed775ee7SAntonio Huete Jimenez      * Suppress definition of intN_t in bittypes.h, which might be included
110*ed775ee7SAntonio Huete Jimenez      * by <pcap/pcap.h> in older versions of WinPcap.
111*ed775ee7SAntonio Huete Jimenez      * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented.)
112*ed775ee7SAntonio Huete Jimenez      */
113*ed775ee7SAntonio Huete Jimenez     #define HAVE_U_INT8_T
114*ed775ee7SAntonio Huete Jimenez     #define HAVE_U_INT16_T
115*ed775ee7SAntonio Huete Jimenez     #define HAVE_U_INT32_T
116*ed775ee7SAntonio Huete Jimenez     #define HAVE_U_INT64_T
117*ed775ee7SAntonio Huete Jimenez   #endif
118*ed775ee7SAntonio Huete Jimenez #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
119*ed775ee7SAntonio Huete Jimenez 
120411677aeSAaron LI #ifdef _WIN32
121411677aeSAaron LI 
122411677aeSAaron LI /*
123411677aeSAaron LI  * Includes and definitions for Windows.
124411677aeSAaron LI  */
125411677aeSAaron LI 
126411677aeSAaron LI #include <stdio.h>
127411677aeSAaron LI #include <winsock2.h>
128411677aeSAaron LI #include <ws2tcpip.h>
129411677aeSAaron LI #include <time.h>
130411677aeSAaron LI #include <io.h>
131411677aeSAaron LI #include <fcntl.h>
132411677aeSAaron LI #include <sys/types.h>
133411677aeSAaron LI 
134*ed775ee7SAntonio Huete Jimenez #ifdef _MSC_VER
135*ed775ee7SAntonio Huete Jimenez   /*
136*ed775ee7SAntonio Huete Jimenez    * Compiler is MSVC.
137*ed775ee7SAntonio Huete Jimenez    *
138*ed775ee7SAntonio Huete Jimenez    * We require VS 2015 or newer, so we have strtoll().  Use that for
139*ed775ee7SAntonio Huete Jimenez    * strtoint64_t().
140*ed775ee7SAntonio Huete Jimenez    */
141*ed775ee7SAntonio Huete Jimenez   #define strtoint64_t	strtoll
142411677aeSAaron LI 
143411677aeSAaron LI   /*
144*ed775ee7SAntonio Huete Jimenez    * And we have LL as a suffix for constants, so use that.
145411677aeSAaron LI    */
146*ed775ee7SAntonio Huete Jimenez   #define INT64_T_CONSTANT(constant)	(constant##LL)
147*ed775ee7SAntonio Huete Jimenez #else
148*ed775ee7SAntonio Huete Jimenez   /*
149*ed775ee7SAntonio Huete Jimenez    * Non-Microsoft compiler.
150*ed775ee7SAntonio Huete Jimenez    *
151*ed775ee7SAntonio Huete Jimenez    * XXX - should we use strtoll or should we use _strtoi64()?
152*ed775ee7SAntonio Huete Jimenez    */
153*ed775ee7SAntonio Huete Jimenez   #define strtoint64_t		strtoll
154*ed775ee7SAntonio Huete Jimenez 
155*ed775ee7SAntonio Huete Jimenez   /*
156*ed775ee7SAntonio Huete Jimenez    * Assume LL works.
157*ed775ee7SAntonio Huete Jimenez    */
158*ed775ee7SAntonio Huete Jimenez   #define INT64_T_CONSTANT(constant)	(constant##LL)
159*ed775ee7SAntonio Huete Jimenez #endif
160411677aeSAaron LI 
161411677aeSAaron LI #ifdef _MSC_VER
162*ed775ee7SAntonio Huete Jimenez   /*
163*ed775ee7SAntonio Huete Jimenez    * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
164*ed775ee7SAntonio Huete Jimenez    * by adding a preceding underscore; we *want* the UN*Xisms, so add
165*ed775ee7SAntonio Huete Jimenez    * #defines to let us use them.
166*ed775ee7SAntonio Huete Jimenez    */
167*ed775ee7SAntonio Huete Jimenez   #define isatty _isatty
168411677aeSAaron LI   #define stat _stat
169*ed775ee7SAntonio Huete Jimenez   #define strdup _strdup
170411677aeSAaron LI   #define open _open
171411677aeSAaron LI   #define read _read
172411677aeSAaron LI   #define close _close
173411677aeSAaron LI   #define O_RDONLY _O_RDONLY
174*ed775ee7SAntonio Huete Jimenez 
175*ed775ee7SAntonio Huete Jimenez   /*
176*ed775ee7SAntonio Huete Jimenez    * We define our_fstat64 as _fstati64, and define our_statb as
177*ed775ee7SAntonio Huete Jimenez    * struct _stati64, so we get 64-bit file sizes.
178*ed775ee7SAntonio Huete Jimenez    */
179*ed775ee7SAntonio Huete Jimenez   #define our_fstat _fstati64
180*ed775ee7SAntonio Huete Jimenez   #define our_statb struct _stati64
181*ed775ee7SAntonio Huete Jimenez 
182*ed775ee7SAntonio Huete Jimenez   /*
183*ed775ee7SAntonio Huete Jimenez    * If <crtdbg.h> has been included, and _DEBUG is defined, and
184*ed775ee7SAntonio Huete Jimenez    * __STDC__ is zero, <crtdbg.h> will define strdup() to call
185*ed775ee7SAntonio Huete Jimenez    * _strdup_dbg().  So if it's already defined, don't redefine
186*ed775ee7SAntonio Huete Jimenez    * it.
187*ed775ee7SAntonio Huete Jimenez    */
188*ed775ee7SAntonio Huete Jimenez   #ifndef strdup
189*ed775ee7SAntonio Huete Jimenez     #define strdup _strdup
190*ed775ee7SAntonio Huete Jimenez   #endif
191*ed775ee7SAntonio Huete Jimenez 
192*ed775ee7SAntonio Huete Jimenez   /*
193*ed775ee7SAntonio Huete Jimenez    * Windows doesn't have ssize_t; routines such as _read() return int.
194*ed775ee7SAntonio Huete Jimenez    */
195*ed775ee7SAntonio Huete Jimenez   typedef int ssize_t;
196411677aeSAaron LI #endif  /* _MSC_VER */
197411677aeSAaron LI 
198411677aeSAaron LI /*
199411677aeSAaron LI  * With MSVC, for C, __inline is used to make a function an inline.
200411677aeSAaron LI  */
201411677aeSAaron LI #ifdef _MSC_VER
202411677aeSAaron LI #define inline __inline
203411677aeSAaron LI #endif
204411677aeSAaron LI 
205*ed775ee7SAntonio Huete Jimenez #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
206411677aeSAaron LI #define HAVE_OS_IPV6_SUPPORT
207411677aeSAaron LI #endif
208411677aeSAaron LI 
209411677aeSAaron LI #ifndef INET6_ADDRSTRLEN
210411677aeSAaron LI #define INET6_ADDRSTRLEN 46
211411677aeSAaron LI #endif
212411677aeSAaron LI 
213411677aeSAaron LI /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
214411677aeSAaron LI  */
215411677aeSAaron LI #ifndef EAFNOSUPPORT
216411677aeSAaron LI #define EAFNOSUPPORT WSAEAFNOSUPPORT
217411677aeSAaron LI #endif
218411677aeSAaron LI 
219411677aeSAaron LI #ifndef caddr_t
220411677aeSAaron LI typedef char* caddr_t;
221411677aeSAaron LI #endif /* caddr_t */
222411677aeSAaron LI 
223411677aeSAaron LI #define MAXHOSTNAMELEN	64
224411677aeSAaron LI 
225411677aeSAaron LI #else /* _WIN32 */
226411677aeSAaron LI 
227411677aeSAaron LI /*
228411677aeSAaron LI  * Includes and definitions for various flavors of UN*X.
229411677aeSAaron LI  */
230411677aeSAaron LI 
231411677aeSAaron LI #include <unistd.h>
232411677aeSAaron LI #include <netdb.h>
233411677aeSAaron LI #include <sys/param.h>
234411677aeSAaron LI #include <sys/types.h>			/* concession to AIX */
235411677aeSAaron LI #include <sys/time.h>
236411677aeSAaron LI #include <sys/socket.h>
237411677aeSAaron LI #include <netinet/in.h>
238411677aeSAaron LI 
239411677aeSAaron LI #include <time.h>
240411677aeSAaron LI 
241411677aeSAaron LI #include <arpa/inet.h>
242411677aeSAaron LI 
243*ed775ee7SAntonio Huete Jimenez /*
244*ed775ee7SAntonio Huete Jimenez  * We should have large file support enabled, if it's available,
245*ed775ee7SAntonio Huete Jimenez  * so just use fstat as our_fstat and struct stat as our_statb.
246*ed775ee7SAntonio Huete Jimenez  */
247*ed775ee7SAntonio Huete Jimenez #define our_fstat fstat
248*ed775ee7SAntonio Huete Jimenez #define our_statb struct stat
249411677aeSAaron LI 
250411677aeSAaron LI /*
251*ed775ee7SAntonio Huete Jimenez  * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
252411677aeSAaron LI  */
253*ed775ee7SAntonio Huete Jimenez #define strtoint64_t	strtoll
254*ed775ee7SAntonio Huete Jimenez 
255*ed775ee7SAntonio Huete Jimenez /*
256*ed775ee7SAntonio Huete Jimenez  * Assume LL works.
257*ed775ee7SAntonio Huete Jimenez  */
258*ed775ee7SAntonio Huete Jimenez #define INT64_T_CONSTANT(constant)	(constant##LL)
259*ed775ee7SAntonio Huete Jimenez #endif /* _WIN32 */
260*ed775ee7SAntonio Huete Jimenez 
261*ed775ee7SAntonio Huete Jimenez /*
262*ed775ee7SAntonio Huete Jimenez  * Function attributes, for various compilers.
263*ed775ee7SAntonio Huete Jimenez  */
264*ed775ee7SAntonio Huete Jimenez #include "funcattrs.h"
265411677aeSAaron LI 
266411677aeSAaron LI /*
267411677aeSAaron LI  * fopen() read and write modes for text files and binary files.
268411677aeSAaron LI  */
269411677aeSAaron LI #if defined(_WIN32) || defined(MSDOS)
270411677aeSAaron LI   #define FOPEN_READ_TXT   "rt"
271411677aeSAaron LI   #define FOPEN_READ_BIN   "rb"
272411677aeSAaron LI   #define FOPEN_WRITE_TXT  "wt"
273411677aeSAaron LI   #define FOPEN_WRITE_BIN  "wb"
274411677aeSAaron LI #else
275411677aeSAaron LI   #define FOPEN_READ_TXT   "r"
276411677aeSAaron LI   #define FOPEN_READ_BIN   FOPEN_READ_TXT
277411677aeSAaron LI   #define FOPEN_WRITE_TXT  "w"
278411677aeSAaron LI   #define FOPEN_WRITE_BIN  FOPEN_WRITE_TXT
279411677aeSAaron LI #endif
280411677aeSAaron LI 
281411677aeSAaron LI /*
282411677aeSAaron LI  * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
283411677aeSAaron LI  * defined if the OS doesn't provide them.  These assume no more than
284411677aeSAaron LI  * an 80386, so, for example, it avoids the bswap instruction added in
285411677aeSAaron LI  * the 80486.
286411677aeSAaron LI  *
287*ed775ee7SAntonio Huete Jimenez  * (We don't use them on macOS; Apple provides their own, which *doesn't*
288*ed775ee7SAntonio Huete Jimenez  * avoid the bswap instruction, as macOS only supports machines that
289411677aeSAaron LI  * have it.)
290411677aeSAaron LI  */
291411677aeSAaron LI #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
292411677aeSAaron LI   #undef ntohl
293411677aeSAaron LI   #undef ntohs
294411677aeSAaron LI   #undef htonl
295411677aeSAaron LI   #undef htons
296411677aeSAaron LI 
297411677aeSAaron LI   static __inline__ unsigned long __ntohl (unsigned long x);
298411677aeSAaron LI   static __inline__ unsigned short __ntohs (unsigned short x);
299411677aeSAaron LI 
300411677aeSAaron LI   #define ntohl(x)  __ntohl(x)
301411677aeSAaron LI   #define ntohs(x)  __ntohs(x)
302411677aeSAaron LI   #define htonl(x)  __ntohl(x)
303411677aeSAaron LI   #define htons(x)  __ntohs(x)
304411677aeSAaron LI 
__ntohl(unsigned long x)305411677aeSAaron LI   static __inline__ unsigned long __ntohl (unsigned long x)
306411677aeSAaron LI   {
307411677aeSAaron LI     __asm__ ("xchgb %b0, %h0\n\t"   /* swap lower bytes  */
308411677aeSAaron LI              "rorl  $16, %0\n\t"    /* swap words        */
309411677aeSAaron LI              "xchgb %b0, %h0"       /* swap higher bytes */
310411677aeSAaron LI             : "=q" (x) : "0" (x));
311411677aeSAaron LI     return (x);
312411677aeSAaron LI   }
313411677aeSAaron LI 
__ntohs(unsigned short x)314411677aeSAaron LI   static __inline__ unsigned short __ntohs (unsigned short x)
315411677aeSAaron LI   {
316411677aeSAaron LI     __asm__ ("xchgb %b0, %h0"       /* swap bytes */
317411677aeSAaron LI             : "=q" (x) : "0" (x));
318411677aeSAaron LI     return (x);
319411677aeSAaron LI   }
320411677aeSAaron LI #endif
321411677aeSAaron LI 
322411677aeSAaron LI /*
323411677aeSAaron LI  * If the OS doesn't define AF_INET6 and struct in6_addr:
324411677aeSAaron LI  *
325411677aeSAaron LI  * define AF_INET6, so we can use it internally as a "this is an
326411677aeSAaron LI  * IPv6 address" indication;
327411677aeSAaron LI  *
328411677aeSAaron LI  * define struct in6_addr so that we can use it for IPv6 addresses.
329411677aeSAaron LI  */
330411677aeSAaron LI #ifndef HAVE_OS_IPV6_SUPPORT
331411677aeSAaron LI #ifndef AF_INET6
332411677aeSAaron LI #define AF_INET6	24
333411677aeSAaron LI 
334411677aeSAaron LI struct in6_addr {
335411677aeSAaron LI 	union {
336411677aeSAaron LI 		__uint8_t   __u6_addr8[16];
337411677aeSAaron LI 		__uint16_t  __u6_addr16[8];
338411677aeSAaron LI 		__uint32_t  __u6_addr32[4];
339411677aeSAaron LI 	} __u6_addr;			/* 128-bit IP6 address */
340411677aeSAaron LI };
341411677aeSAaron LI #endif
342411677aeSAaron LI #endif
343411677aeSAaron LI 
344411677aeSAaron LI #ifndef NI_MAXHOST
345411677aeSAaron LI #define	NI_MAXHOST	1025
346411677aeSAaron LI #endif
347411677aeSAaron LI 
348411677aeSAaron LI #ifndef INET_ADDRSTRLEN
349411677aeSAaron LI #define INET_ADDRSTRLEN 16
350411677aeSAaron LI #endif
351411677aeSAaron LI 
352411677aeSAaron LI #ifndef TRUE
353411677aeSAaron LI #define TRUE 1
354411677aeSAaron LI #endif
355411677aeSAaron LI 
356411677aeSAaron LI #ifndef FALSE
357411677aeSAaron LI #define FALSE 0
358411677aeSAaron LI #endif
359411677aeSAaron LI 
360411677aeSAaron LI /*
361411677aeSAaron LI  * The Apple deprecation workaround macros below were adopted from the
362411677aeSAaron LI  * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
363411677aeSAaron LI  */
364411677aeSAaron LI 
365411677aeSAaron LI #define XSTRINGIFY(x) #x
366411677aeSAaron LI 
367411677aeSAaron LI /*
368411677aeSAaron LI  *	Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
369411677aeSAaron LI  */
370411677aeSAaron LI #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
371411677aeSAaron LI #define DIAG_DO_PRAGMA(x) _Pragma (#x)
372411677aeSAaron LI 
373*ed775ee7SAntonio Huete Jimenez /*
374*ed775ee7SAntonio Huete Jimenez  * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
375*ed775ee7SAntonio Huete Jimenez  * thus we need to test the clang case before the GCC one
376*ed775ee7SAntonio Huete Jimenez  */
377*ed775ee7SAntonio Huete Jimenez #if defined(__clang__)
378*ed775ee7SAntonio Huete Jimenez #  if (__clang_major__ * 100) + __clang_minor__ >= 208
379*ed775ee7SAntonio Huete Jimenez #    define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
380*ed775ee7SAntonio Huete Jimenez #    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
381*ed775ee7SAntonio Huete Jimenez #    define DIAG_ON(x) DIAG_PRAGMA(pop)
382*ed775ee7SAntonio Huete Jimenez #  else
383*ed775ee7SAntonio Huete Jimenez #    define DIAG_OFF(x)
384*ed775ee7SAntonio Huete Jimenez #    define DIAG_ON(x)
385*ed775ee7SAntonio Huete Jimenez #  endif
386*ed775ee7SAntonio Huete Jimenez #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
387411677aeSAaron LI #  define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
388411677aeSAaron LI #  if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
389411677aeSAaron LI #    define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
390411677aeSAaron LI #    define DIAG_ON(x) DIAG_PRAGMA(pop)
391411677aeSAaron LI #  else
392411677aeSAaron LI #    define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
393411677aeSAaron LI #    define DIAG_ON(x)  DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
394411677aeSAaron LI #  endif
395411677aeSAaron LI #else
396411677aeSAaron LI #  define DIAG_OFF(x)
397411677aeSAaron LI #  define DIAG_ON(x)
398411677aeSAaron LI #endif
399411677aeSAaron LI 
400*ed775ee7SAntonio Huete Jimenez /* Use for clang specific warnings */
401*ed775ee7SAntonio Huete Jimenez #ifdef __clang__
402*ed775ee7SAntonio Huete Jimenez #  define DIAG_OFF_CLANG(x) DIAG_OFF(x)
403*ed775ee7SAntonio Huete Jimenez #  define DIAG_ON_CLANG(x)  DIAG_ON(x)
404*ed775ee7SAntonio Huete Jimenez #else
405*ed775ee7SAntonio Huete Jimenez #  define DIAG_OFF_CLANG(x)
406*ed775ee7SAntonio Huete Jimenez #  define DIAG_ON_CLANG(x)
407*ed775ee7SAntonio Huete Jimenez #endif
408*ed775ee7SAntonio Huete Jimenez 
409411677aeSAaron LI /*
410411677aeSAaron LI  *	For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
411411677aeSAaron LI  */
412411677aeSAaron LI #ifdef __APPLE__
413411677aeSAaron LI #  define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
414411677aeSAaron LI #  define USES_APPLE_RST DIAG_ON(deprecated-declarations)
415411677aeSAaron LI #else
416411677aeSAaron LI #  define USES_APPLE_DEPRECATED_API
417411677aeSAaron LI #  define USES_APPLE_RST
418411677aeSAaron LI #endif
419411677aeSAaron LI 
420411677aeSAaron LI /*
421411677aeSAaron LI  * end of Apple deprecation workaround macros
422411677aeSAaron LI  */
423411677aeSAaron LI 
424411677aeSAaron LI /*
425*ed775ee7SAntonio Huete Jimenez  * Statement attributes, for various compilers.
426*ed775ee7SAntonio Huete Jimenez  *
427*ed775ee7SAntonio Huete Jimenez  * This was introduced sufficiently recently that compilers implementing
428*ed775ee7SAntonio Huete Jimenez  * it also implement __has_attribute() (for example, GCC 5.0 and later
429*ed775ee7SAntonio Huete Jimenez  * have __has_attribute(), and the "fallthrough" attribute was introduced
430*ed775ee7SAntonio Huete Jimenez  * in GCC 7).
431*ed775ee7SAntonio Huete Jimenez  *
432*ed775ee7SAntonio Huete Jimenez  * Unfortunately, Clang does this wrong - a statement
433*ed775ee7SAntonio Huete Jimenez  *
434*ed775ee7SAntonio Huete Jimenez  *    __attribute__ ((fallthrough));
435*ed775ee7SAntonio Huete Jimenez  *
436*ed775ee7SAntonio Huete Jimenez  * produces bogus -Wmissing-declaration "declaration does not declare
437*ed775ee7SAntonio Huete Jimenez  * anything" warnings (dear Clang: that's not a declaration, it's an
438*ed775ee7SAntonio Huete Jimenez  * empty statement).  GCC, however, has no trouble with this.
439411677aeSAaron LI  */
440*ed775ee7SAntonio Huete Jimenez #if __has_attribute(fallthrough) && !defined(__clang__)
441*ed775ee7SAntonio Huete Jimenez #  define ND_FALL_THROUGH __attribute__ ((fallthrough))
442*ed775ee7SAntonio Huete Jimenez #else
443*ed775ee7SAntonio Huete Jimenez #  define ND_FALL_THROUGH
444*ed775ee7SAntonio Huete Jimenez #endif /*  __has_attribute(fallthrough) */
445411677aeSAaron LI 
446411677aeSAaron LI #endif /* netdissect_stdinc_h */
447