netdissect-stdinc.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. * Include the appropriate OS header files on Windows and various flavors
  33. * of UNIX, include various non-OS header files on Windows, and define
  34. * various items as needed, to isolate most of netdissect's platform
  35. * differences to this one file.
  36. */
  37. #ifndef netdissect_stdinc_h
  38. #define netdissect_stdinc_h
  39. #include <errno.h>
  40. #ifdef _WIN32
  41. /*
  42. * Includes and definitions for Windows.
  43. */
  44. #include <stdint.h>
  45. #include <stdio.h>
  46. #include <winsock2.h>
  47. #include <ws2tcpip.h>
  48. #include <ctype.h>
  49. #include <time.h>
  50. #include <io.h>
  51. #include <fcntl.h>
  52. #include <sys/types.h>
  53. #ifndef uint8_t
  54. #define uint8_t unsigned char
  55. #endif
  56. #ifndef int8_t
  57. #define int8_t signed char
  58. #endif
  59. #ifndef uint16_t
  60. #define uint16_t unsigned short
  61. #endif
  62. #ifndef int16_t
  63. #define int16_t signed short
  64. #endif
  65. #ifndef uint32_t
  66. #define uint32_t unsigned int
  67. #endif
  68. #ifndef int32_t
  69. #define int32_t signed int
  70. #endif
  71. #ifdef _MSC_EXTENSIONS
  72. #ifndef uint64_t
  73. #define uint64_t unsigned _int64
  74. #endif
  75. #ifndef int64_t
  76. #define int64_t _int64
  77. #endif
  78. #ifndef PRId64
  79. #define PRId64 "I64d"
  80. #endif
  81. #ifndef PRIo64
  82. #define PRIo64 "I64o"
  83. #endif
  84. #ifndef PRIu64
  85. #define PRIu64 "I64u"
  86. #endif
  87. #ifndef PRIx64
  88. #define PRIx64 "I64x"
  89. #endif
  90. #else /* _MSC_EXTENSIONS */
  91. #ifndef uint64_t
  92. #define uint64_t unsigned long long
  93. #endif
  94. #ifndef int64_t
  95. #define int64_t long long
  96. #endif
  97. #ifndef PRId64
  98. #define PRId64 "lld"
  99. #endif
  100. #ifndef PRIo64
  101. #define PRIo64 "llo"
  102. #endif
  103. #ifndef PRIu64
  104. #define PRIu64 "llu"
  105. #endif
  106. #ifndef PRIx64
  107. #define PRIx64 "llx"
  108. #endif
  109. #endif /* _MSC_EXTENSIONS */
  110. /*
  111. * Suppress definition of intN_t in bittypes.h, as included by <pcap/pcap.h>
  112. * on Windows.
  113. * (Yes, HAVE_U_INTn_T, as the definition guards are UN*X-oriented, and
  114. * we check for u_intN_t in the UN*X configure script.)
  115. */
  116. #define HAVE_U_INT8_T
  117. #define HAVE_U_INT16_T
  118. #define HAVE_U_INT32_T
  119. #define HAVE_U_INT64_T
  120. #ifdef _MSC_VER
  121. #define stat _stat
  122. #define open _open
  123. #define fstat _fstat
  124. #define read _read
  125. #define close _close
  126. #define O_RDONLY _O_RDONLY
  127. #endif /* _MSC_VER */
  128. /*
  129. * With MSVC, for C, __inline is used to make a function an inline.
  130. */
  131. #ifdef _MSC_VER
  132. #define inline __inline
  133. #endif
  134. #ifdef AF_INET6
  135. #define HAVE_OS_IPV6_SUPPORT
  136. #endif
  137. #ifndef INET6_ADDRSTRLEN
  138. #define INET6_ADDRSTRLEN 46
  139. #endif
  140. /* It is in MSVC's <errno.h>, but not defined in MingW+Watcom.
  141. */
  142. #ifndef EAFNOSUPPORT
  143. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  144. #endif
  145. #ifndef caddr_t
  146. typedef char* caddr_t;
  147. #endif /* caddr_t */
  148. #define MAXHOSTNAMELEN 64
  149. #define snprintf _snprintf
  150. #define vsnprintf _vsnprintf
  151. #define RETSIGTYPE void
  152. #else /* _WIN32 */
  153. /*
  154. * Includes and definitions for various flavors of UN*X.
  155. */
  156. #include <ctype.h>
  157. #include <unistd.h>
  158. #include <netdb.h>
  159. #if HAVE_INTTYPES_H
  160. #include <inttypes.h>
  161. #elif HAVE_STDINT_H
  162. #include <stdint.h>
  163. #endif
  164. #include <sys/param.h>
  165. #include <sys/types.h> /* concession to AIX */
  166. #include <sys/time.h>
  167. #include <sys/socket.h>
  168. #include <netinet/in.h>
  169. #ifdef TIME_WITH_SYS_TIME
  170. #include <time.h>
  171. #endif
  172. #include <arpa/inet.h>
  173. #endif /* _WIN32 */
  174. #ifndef HAVE___ATTRIBUTE__
  175. #define __attribute__(x)
  176. #endif
  177. /*
  178. * Used to declare a structure unaligned, so that the C compiler,
  179. * if necessary, generates code that doesn't assume alignment.
  180. * This is required because there is no guarantee that the packet
  181. * data we get from libpcap/WinPcap is properly aligned.
  182. *
  183. * This assumes that, for all compilers that support __attribute__:
  184. *
  185. * 1) they support __attribute__((packed));
  186. *
  187. * 2) for all instruction set architectures requiring strict
  188. * alignment, declaring a structure with that attribute
  189. * causes the compiler to generate code that handles
  190. * misaligned 2-byte, 4-byte, and 8-byte integral
  191. * quantities.
  192. *
  193. * It does not (yet) handle compilers where you can get the compiler
  194. * to generate code of that sort by some other means.
  195. *
  196. * This is required in order to, for example, keep the compiler from
  197. * generating, for
  198. *
  199. * if (bp->bp_htype == 1 && bp->bp_hlen == 6 && bp->bp_op == BOOTPREQUEST) {
  200. *
  201. * in print-bootp.c, code that loads the first 4-byte word of a
  202. * "struct bootp", masking out the bp_hops field, and comparing the result
  203. * against 0x01010600.
  204. *
  205. * Note: this also requires that padding be put into the structure,
  206. * at least for compilers where it's implemented as __attribute__((packed)).
  207. */
  208. #if !(defined(_MSC_VER) && defined(UNALIGNED))
  209. /* MSVC may have its own macro defined with the same name and purpose. */
  210. #undef UNALIGNED
  211. #define UNALIGNED __attribute__((packed))
  212. #endif
  213. /*
  214. * fopen() read and write modes for text files and binary files.
  215. */
  216. #if defined(_WIN32) || defined(MSDOS)
  217. #define FOPEN_READ_TXT "rt"
  218. #define FOPEN_READ_BIN "rb"
  219. #define FOPEN_WRITE_TXT "wt"
  220. #define FOPEN_WRITE_BIN "wb"
  221. #else
  222. #define FOPEN_READ_TXT "r"
  223. #define FOPEN_READ_BIN FOPEN_READ_TXT
  224. #define FOPEN_WRITE_TXT "w"
  225. #define FOPEN_WRITE_BIN FOPEN_WRITE_TXT
  226. #endif
  227. /*
  228. * Inline x86 assembler-language versions of ntoh[ls]() and hton[ls](),
  229. * defined if the OS doesn't provide them. These assume no more than
  230. * an 80386, so, for example, it avoids the bswap instruction added in
  231. * the 80486.
  232. *
  233. * (We don't use them on OS X; Apple provides their own, which *doesn't*
  234. * avoid the bswap instruction, as OS X only supports machines that
  235. * have it.)
  236. */
  237. #if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
  238. #undef ntohl
  239. #undef ntohs
  240. #undef htonl
  241. #undef htons
  242. static __inline__ unsigned long __ntohl (unsigned long x);
  243. static __inline__ unsigned short __ntohs (unsigned short x);
  244. #define ntohl(x) __ntohl(x)
  245. #define ntohs(x) __ntohs(x)
  246. #define htonl(x) __ntohl(x)
  247. #define htons(x) __ntohs(x)
  248. static __inline__ unsigned long __ntohl (unsigned long x)
  249. {
  250. __asm__ ("xchgb %b0, %h0\n\t" /* swap lower bytes */
  251. "rorl $16, %0\n\t" /* swap words */
  252. "xchgb %b0, %h0" /* swap higher bytes */
  253. : "=q" (x) : "0" (x));
  254. return (x);
  255. }
  256. static __inline__ unsigned short __ntohs (unsigned short x)
  257. {
  258. __asm__ ("xchgb %b0, %h0" /* swap bytes */
  259. : "=q" (x) : "0" (x));
  260. return (x);
  261. }
  262. #endif
  263. /*
  264. * If the OS doesn't define AF_INET6 and struct in6_addr:
  265. *
  266. * define AF_INET6, so we can use it internally as a "this is an
  267. * IPv6 address" indication;
  268. *
  269. * define struct in6_addr so that we can use it for IPv6 addresses.
  270. */
  271. #ifndef HAVE_OS_IPV6_SUPPORT
  272. #ifndef AF_INET6
  273. #define AF_INET6 24
  274. struct in6_addr {
  275. union {
  276. __uint8_t __u6_addr8[16];
  277. __uint16_t __u6_addr16[8];
  278. __uint32_t __u6_addr32[4];
  279. } __u6_addr; /* 128-bit IP6 address */
  280. };
  281. #endif
  282. #endif
  283. #ifndef NI_MAXHOST
  284. #define NI_MAXHOST 1025
  285. #endif
  286. #ifndef INET_ADDRSTRLEN
  287. #define INET_ADDRSTRLEN 16
  288. #endif
  289. #ifndef TRUE
  290. #define TRUE 1
  291. #endif
  292. #ifndef FALSE
  293. #define FALSE 0
  294. #endif
  295. /*
  296. * The Apple deprecation workaround macros below were adopted from the
  297. * FreeRADIUS server code under permission of Alan DeKok and Arran Cudbard-Bell.
  298. */
  299. #define XSTRINGIFY(x) #x
  300. /*
  301. * Macros for controlling warnings in GCC >= 4.2 and clang >= 2.8
  302. */
  303. #define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
  304. #define DIAG_DO_PRAGMA(x) _Pragma (#x)
  305. #if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
  306. # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
  307. # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
  308. # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
  309. # define DIAG_ON(x) DIAG_PRAGMA(pop)
  310. # else
  311. # define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
  312. # define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
  313. # endif
  314. #elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
  315. # define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
  316. # define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
  317. # define DIAG_ON(x) DIAG_PRAGMA(pop)
  318. #else
  319. # define DIAG_OFF(x)
  320. # define DIAG_ON(x)
  321. #endif
  322. /*
  323. * For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
  324. */
  325. #ifdef __APPLE__
  326. # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
  327. # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
  328. #else
  329. # define USES_APPLE_DEPRECATED_API
  330. # define USES_APPLE_RST
  331. #endif
  332. /*
  333. * end of Apple deprecation workaround macros
  334. */
  335. /*
  336. * Function attributes, for various compilers.
  337. */
  338. #include "funcattrs.h"
  339. #ifndef min
  340. #define min(a,b) ((a)>(b)?(b):(a))
  341. #endif
  342. #ifndef max
  343. #define max(a,b) ((b)>(a)?(b):(a))
  344. #endif
  345. #endif /* netdissect_stdinc_h */