tcpdump  4.99.1
About: tcpdump is a tool for network monitoring and data acquisition.
  Fossies Dox: tcpdump-4.99.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

netdissect-stdinc.h
Go to the documentation of this file.
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 /*
33  * Include the appropriate OS header files on Windows and various flavors
34  * of UNIX, include various non-OS header files on Windows, and define
35  * various items as needed, to isolate most of netdissect's platform
36  * differences to this one file.
37  */
38 
39 #ifndef netdissect_stdinc_h
40 #define netdissect_stdinc_h
41 
42 #include "ftmacros.h"
43 
44 #include <errno.h>
45 
46 #include "compiler-tests.h"
47 
48 #include "varattrs.h"
49 
50 /*
51  * If we're compiling with Visual Studio, make sure we have at least
52  * VS 2015 or later, so we have sufficient C99 support.
53  *
54  * XXX - verify that we have at least C99 support on UN*Xes?
55  *
56  * What about MinGW or various DOS toolchains? We're currently assuming
57  * sufficient C99 support there.
58  */
59 #if defined(_MSC_VER)
60  /*
61  * Make sure we have VS 2015 or later.
62  */
63  #if _MSC_VER < 1900
64  #error "Building tcpdump requires VS 2015 or later"
65  #endif
66 #endif
67 
68 /*
69  * Get the C99 types, and the PRI[doux]64 format strings, defined.
70  */
71 #ifdef HAVE_PCAP_PCAP_INTTYPES_H
72  /*
73  * We have pcap/pcap-inttypes.h; use that, as it'll do all the
74  * work, and won't cause problems if a file includes this file
75  * and later includes a pcap header file that also includes
76  * pcap/pcap-inttypes.h.
77  */
78  #include <pcap/pcap-inttypes.h>
79 #else
80  /*
81  * OK, we don't have pcap/pcap-inttypes.h, so we'll have to
82  * do the work ourselves, but at least we don't have to
83  * worry about other headers including it and causing
84  * clashes.
85  */
86 
87  /*
88  * Include <inttypes.h> to get the integer types and PRi[doux]64 values
89  * defined.
90  *
91  * If the compiler is MSVC, we require VS 2015 or newer, so we
92  * have <inttypes.h> - and support for %zu in the formatted
93  * printing functions.
94  *
95  * If the compiler is MinGW, we assume we have <inttypes.h> - and
96  * support for %zu in the formatted printing functions.
97  *
98  * If the target is UN*X, we assume we have a C99-or-later development
99  * environment, and thus have <inttypes.h> - and support for %zu in
100  * the formatted printing functions.
101  *
102  * If the target is MS-DOS, we assume we have <inttypes.h> - and support
103  * for %zu in the formatted printing functions.
104  */
105  #include <inttypes.h>
106 
107  #if defined(_MSC_VER)
108  /*
109  * Suppress definition of intN_t in bittypes.h, which might be included
110  * by <pcap/pcap.h> in older versions of WinPcap.
111  * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented.)
112  */
113  #define HAVE_U_INT8_T
114  #define HAVE_U_INT16_T
115  #define HAVE_U_INT32_T
116  #define HAVE_U_INT64_T
117  #endif
118 #endif /* HAVE_PCAP_PCAP_INTTYPES_H */
119 
120 #ifdef _WIN32
121 
122 /*
123  * Includes and definitions for Windows.
124  */
125 
126 #include <stdio.h>
127 #include <winsock2.h>
128 #include <ws2tcpip.h>
129 #include <time.h>
130 #include <io.h>
131 #include <fcntl.h>
132 #include <sys/types.h>
133 
134 #ifdef _MSC_VER
135  /*
136  * Compiler is MSVC.
137  *
138  * We require VS 2015 or newer, so we have strtoll(). Use that for
139  * strtoint64_t().
140  */
141  #define strtoint64_t strtoll
142 
143  /*
144  * And we have LL as a suffix for constants, so use that.
145  */
146  #define INT64_T_CONSTANT(constant) (constant##LL)
147 #else
148  /*
149  * Non-Microsoft compiler.
150  *
151  * XXX - should we use strtoll or should we use _strtoi64()?
152  */
153  #define strtoint64_t strtoll
154 
155  /*
156  * Assume LL works.
157  */
158  #define INT64_T_CONSTANT(constant) (constant##LL)
159 #endif
160 
161 #ifdef _MSC_VER
162  /*
163  * Microsoft tries to avoid polluting the C namespace with UN*Xisms,
164  * by adding a preceding underscore; we *want* the UN*Xisms, so add
165  * #defines to let us use them.
166  */
167  #define isatty _isatty
168  #define stat _stat
169  #define strdup _strdup
170  #define open _open
171  #define read _read
172  #define close _close
173  #define O_RDONLY _O_RDONLY
174 
175  /*
176  * We define our_fstat64 as _fstati64, and define our_statb as
177  * struct _stati64, so we get 64-bit file sizes.
178  */
179  #define our_fstat _fstati64
180  #define our_statb struct _stati64
181 
182  /*
183  * If <crtdbg.h> has been included, and _DEBUG is defined, and
184  * __STDC__ is zero, <crtdbg.h> will define strdup() to call
185  * _strdup_dbg(). So if it's already defined, don't redefine
186  * it.
187  */
188  #ifndef strdup
189  #define strdup _strdup
190  #endif
191 
192  /*
193  * Windows doesn't have ssize_t; routines such as _read() return int.
194  */
195  typedef int ssize_t;
196 #endif /* _MSC_VER */
197 
198 /*
199  * With MSVC, for C, __inline is used to make a function an inline.
200  */
201 #ifdef _MSC_VER
202 #define inline __inline
203 #endif
204 
205 #if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
206 #define HAVE_OS_IPV6_SUPPORT
207 #endif
208 
209 #ifndef INET6_ADDRSTRLEN
210 #define INET6_ADDRSTRLEN 46
211 #endif
212 
213 /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
214  */
215 #ifndef EAFNOSUPPORT
216 #define EAFNOSUPPORT WSAEAFNOSUPPORT
217 #endif
218 
219 #ifndef caddr_t
220 typedef char* caddr_t;
221 #endif /* caddr_t */
222 
223 #define MAXHOSTNAMELEN 64
224 
225 #else /* _WIN32 */
226 
227 /*
228  * Includes and definitions for various flavors of UN*X.
229  */
230 
231 #include <unistd.h>
232 #include <netdb.h>
233 #include <sys/param.h>
234 #include <sys/types.h> /* concession to AIX */
235 #include <sys/time.h>
236 #include <sys/socket.h>
237 #include <netinet/in.h>
238 
239 #include <time.h>
240 
241 #include <arpa/inet.h>
242 
243 /*
244  * We should have large file support enabled, if it's available,
245  * so just use fstat as our_fstat and struct stat as our_statb.
246  */
247 #define our_fstat fstat
248 #define our_statb struct stat
249 
250 /*
251  * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
252  */
253 #define strtoint64_t strtoll
254 
255 /*
256  * Assume LL works.
257  */
258 #define INT64_T_CONSTANT(constant) (constant##LL)
259 #endif /* _WIN32 */
260 
261 /*
262  * Function attributes, for various compilers.
263  */
264 #include "funcattrs.h"
265 
266 /*
267  * fopen() read and write modes for text files and binary files.
268  */
269 #if defined(_WIN32) || defined(MSDOS)
270  #define FOPEN_READ_TXT "rt"
271  #define FOPEN_READ_BIN "rb"
272  #define FOPEN_WRITE_TXT "wt"
273  #define FOPEN_WRITE_BIN "wb"
274 #else
275  #define FOPEN_READ_TXT "r"
276  #define FOPEN_READ_BIN FOPEN_READ_TXT
277  #define FOPEN_WRITE_TXT "w"
278  #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
279 #endif
280 
281 /*
282  * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
283  * defined if the OS doesn't provide them. These assume no more than
284  * an 80386, so, for example, it avoids the bswap instruction added in
285  * the 80486.
286  *
287  * (We don't use them on macOS; Apple provides their own, which *doesn't*
288  * avoid the bswap instruction, as macOS only supports machines that
289  * have it.)
290  */
291 #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
292  #undef ntohl
293  #undef ntohs
294  #undef htonl
295  #undef htons
296 
297  static __inline__ unsigned long __ntohl (unsigned long x);
298  static __inline__ unsigned short __ntohs (unsigned short x);
299 
300  #define ntohl(x) __ntohl(x)
301  #define ntohs(x) __ntohs(x)
302  #define htonl(x) __ntohl(x)
303  #define htons(x) __ntohs(x)
304 
305  static __inline__ unsigned long __ntohl (unsigned long x)
306  {
307  __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
308  "rorl $16, %0\n\t" /* swap words */
309  "xchgb %b0, %h0" /* swap higher bytes */
310  : "=q" (x) : "0" (x));
311  return (x);
312  }
313 
314  static __inline__ unsigned short __ntohs (unsigned short x)
315  {
316  __asm__ ("xchgb %b0, %h0" /* swap bytes */
317  : "=q" (x) : "0" (x));
318  return (x);
319  }
320 #endif
321 
322 /*
323  * If the OS doesn't define AF_INET6 and struct in6_addr:
324  *
325  * define AF_INET6, so we can use it internally as a "this is an
326  * IPv6 address" indication;
327  *
328  * define struct in6_addr so that we can use it for IPv6 addresses.
329  */
330 #ifndef HAVE_OS_IPV6_SUPPORT
331 #ifndef AF_INET6
332 #define AF_INET6 24
333 
334 struct in6_addr {
335  union {
336  __uint8_t __u6_addr8[16];
337  __uint16_t __u6_addr16[8];
338  __uint32_t __u6_addr32[4];
339  } __u6_addr; /* 128-bit IP6 address */
340 };
341 #endif
342 #endif
343 
344 #ifndef NI_MAXHOST
345 #define NI_MAXHOST 1025
346 #endif
347 
348 #ifndef INET_ADDRSTRLEN
349 #define INET_ADDRSTRLEN 16
350 #endif
351 
352 #ifndef TRUE
353 #define TRUE 1
354 #endif
355 
356 #ifndef FALSE
357 #define FALSE 0
358 #endif
359 
360 /*
361  * The Apple deprecation workaround macros below were adopted from the
362  * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
363  */
364 
365 #define XSTRINGIFY(x) #x
366 
367 /*
368  * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
369  */
370 #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
371 #define DIAG_DO_PRAGMA(x) _Pragma (#x)
372 
373 /*
374  * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
375  * thus we need to test the clang case before the GCC one
376  */
377 #if defined(__clang__)
378 # if (__clang_major__ * 100) + __clang_minor__ >= 208
379 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
380 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
381 # define DIAG_ON(x) DIAG_PRAGMA(pop)
382 # else
383 # define DIAG_OFF(x)
384 # define DIAG_ON(x)
385 # endif
386 #elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
387 # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
388 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
389 # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
390 # define DIAG_ON(x) DIAG_PRAGMA(pop)
391 # else
392 # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
393 # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
394 # endif
395 #else
396 # define DIAG_OFF(x)
397 # define DIAG_ON(x)
398 #endif
399 
400 /* Use for clang specific warnings */
401 #ifdef __clang__
402 # define DIAG_OFF_CLANG(x) DIAG_OFF(x)
403 # define DIAG_ON_CLANG(x) DIAG_ON(x)
404 #else
405 # define DIAG_OFF_CLANG(x)
406 # define DIAG_ON_CLANG(x)
407 #endif
408 
409 /*
410  * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
411  */
412 #ifdef __APPLE__
413 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
414 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
415 #else
416 # define USES_APPLE_DEPRECATED_API
417 # define USES_APPLE_RST
418 #endif
419 
420 /*
421  * end of Apple deprecation workaround macros
422  */
423 
424 /*
425  * Statement attributes, for various compilers.
426  *
427  * This was introduced sufficiently recently that compilers implementing
428  * it also implement __has_attribute() (for example, GCC 5.0 and later
429  * have __has_attribute(), and the "fallthrough" attribute was introduced
430  * in GCC 7).
431  *
432  * Unfortunately, Clang does this wrong - a statement
433  *
434  * __attribute__ ((fallthrough));
435  *
436  * produces bogus -Wmissing-declaration "declaration does not declare
437  * anything" warnings (dear Clang: that's not a declaration, it's an
438  * empty statement). GCC, however, has no trouble with this.
439  */
440 #if __has_attribute(fallthrough) && !defined(__clang__)
441 # define ND_FALL_THROUGH __attribute__ ((fallthrough))
442 #else
443 # define ND_FALL_THROUGH
444 #endif /* __has_attribute(fallthrough) */
445 
446 #endif /* netdissect_stdinc_h */
__uint16_t __u6_addr16[8]
__uint8_t __u6_addr8[16]
union in6_addr::@2 __u6_addr
__uint32_t __u6_addr32[4]