/contrib/ntp/include/isc/net.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 329 lines · 154 code · 47 blank · 128 comment · 35 complexity · ded0b8ea91ae7ebe4e36a5721d613c89 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: net.h,v 1.31.2.2.10.8 2004/04/29 01:31:23 marka Exp $ */
  18. #ifndef ISC_NET_H
  19. #define ISC_NET_H 1
  20. /*****
  21. ***** Module Info
  22. *****/
  23. /*
  24. * Basic Networking Types
  25. *
  26. * This module is responsible for defining the following basic networking
  27. * types:
  28. *
  29. * struct in_addr
  30. * struct in6_addr
  31. * struct in6_pktinfo
  32. * struct sockaddr
  33. * struct sockaddr_in
  34. * struct sockaddr_in6
  35. * in_port_t
  36. *
  37. * It ensures that the AF_ and PF_ macros are defined.
  38. *
  39. * It declares ntoh[sl]() and hton[sl]().
  40. *
  41. * It declares inet_aton(), inet_ntop(), and inet_pton().
  42. *
  43. * It ensures that INADDR_LOOPBACK, INADDR_ANY, IN6ADDR_ANY_INIT,
  44. * in6addr_any, and in6addr_loopback are available.
  45. *
  46. * It ensures that IN_MULTICAST() is available to check for multicast
  47. * addresses.
  48. *
  49. * MP:
  50. * No impact.
  51. *
  52. * Reliability:
  53. * No anticipated impact.
  54. *
  55. * Resources:
  56. * N/A.
  57. *
  58. * Security:
  59. * No anticipated impact.
  60. *
  61. * Standards:
  62. * BSD Socket API
  63. * RFC 2553
  64. */
  65. /***
  66. *** Imports.
  67. ***/
  68. #include <isc/platform.h>
  69. #include <sys/types.h>
  70. #include <sys/socket.h> /* Contractual promise. */
  71. #include <net/if.h>
  72. #include <netinet/in.h> /* Contractual promise. */
  73. #include <arpa/inet.h> /* Contractual promise. */
  74. #ifdef ISC_PLATFORM_NEEDNETINETIN6H
  75. #include <netinet/in6.h> /* Required on UnixWare. */
  76. #endif
  77. #ifdef ISC_PLATFORM_NEEDNETINET6IN6H
  78. #include <netinet6/in6.h> /* Required on BSD/OS for in6_pktinfo. */
  79. #endif
  80. #ifndef ISC_PLATFORM_HAVEIPV6
  81. #include <isc/ipv6.h> /* Contractual promise. */
  82. #endif
  83. #include <isc/lang.h>
  84. #include <isc/types.h>
  85. #ifdef ISC_PLATFORM_HAVEINADDR6
  86. #define in6_addr in_addr6 /* Required for pre RFC2133 implementations. */
  87. #endif
  88. #ifdef ISC_PLATFORM_HAVEIPV6
  89. /*
  90. * Required for some pre RFC2133 implementations.
  91. * IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT were added in
  92. * draft-ietf-ipngwg-bsd-api-04.txt or draft-ietf-ipngwg-bsd-api-05.txt.
  93. * If 's6_addr' is defined then assume that there is a union and three
  94. * levels otherwise assume two levels required.
  95. */
  96. #ifndef IN6ADDR_ANY_INIT
  97. #ifdef s6_addr
  98. #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  99. #else
  100. #define IN6ADDR_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }
  101. #endif
  102. #endif
  103. #ifndef IN6ADDR_LOOPBACK_INIT
  104. #ifdef s6_addr
  105. #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  106. #else
  107. #define IN6ADDR_LOOPBACK_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } }
  108. #endif
  109. #endif
  110. #ifndef IN6_IS_ADDR_V4MAPPED
  111. #define IN6_IS_ADDR_V4MAPPED(x) \
  112. (memcmp((x)->s6_addr, in6addr_any.s6_addr, 10) == 0 && \
  113. (x)->s6_addr[10] == 0xff && (x)->s6_addr[11] == 0xff)
  114. #endif
  115. #ifndef IN6_IS_ADDR_V4COMPAT
  116. #define IN6_IS_ADDR_V4COMPAT(x) \
  117. (memcmp((x)->s6_addr, in6addr_any.s6_addr, 12) == 0 && \
  118. ((x)->s6_addr[12] != 0 || (x)->s6_addr[13] != 0 || \
  119. (x)->s6_addr[14] != 0 || \
  120. ((x)->s6_addr[15] != 0 && (x)->s6_addr[15] != 1)))
  121. #endif
  122. #ifndef IN6_IS_ADDR_MULTICAST
  123. #define IN6_IS_ADDR_MULTICAST(a) ((a)->s6_addr[0] == 0xff)
  124. #endif
  125. #ifndef IN6_IS_ADDR_LINKLOCAL
  126. #define IN6_IS_ADDR_LINKLOCAL(a) \
  127. (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80))
  128. #endif
  129. #ifndef IN6_IS_ADDR_SITELOCAL
  130. #define IN6_IS_ADDR_SITELOCAL(a) \
  131. (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0))
  132. #endif
  133. #ifndef IN6_IS_ADDR_LOOPBACK
  134. #define IN6_IS_ADDR_LOOPBACK(x) \
  135. (memcmp((x)->s6_addr, in6addr_loopback.s6_addr, 16) == 0)
  136. #endif
  137. #endif
  138. #ifndef AF_INET6
  139. #define AF_INET6 99
  140. #endif
  141. #ifndef PF_INET6
  142. #define PF_INET6 AF_INET6
  143. #endif
  144. #ifndef INADDR_LOOPBACK
  145. #define INADDR_LOOPBACK 0x7f000001UL
  146. #endif
  147. #if 0
  148. #ifndef ISC_PLATFORM_HAVEIN6PKTINFO
  149. struct in6_pktinfo {
  150. struct in6_addr ipi6_addr; /* src/dst IPv6 address */
  151. unsigned int ipi6_ifindex; /* send/recv interface index */
  152. };
  153. #endif
  154. #endif
  155. /*
  156. * Cope with a missing in6addr_any and in6addr_loopback.
  157. */
  158. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
  159. extern const struct in6_addr isc_net_in6addrany;
  160. #define in6addr_any isc_net_in6addrany
  161. #endif
  162. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
  163. extern const struct in6_addr isc_net_in6addrloop;
  164. #define in6addr_loopback isc_net_in6addrloop
  165. #endif
  166. /*
  167. * Fix UnixWare 7.1.1's broken IN6_IS_ADDR_* definitions.
  168. */
  169. #ifdef ISC_PLATFORM_FIXIN6ISADDR
  170. #undef IN6_IS_ADDR_GEOGRAPHIC
  171. #define IN6_IS_ADDR_GEOGRAPHIC(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x80)
  172. #undef IN6_IS_ADDR_IPX
  173. #define IN6_IS_ADDR_IPX(a) (((a)->S6_un.S6_l[0] & 0xFE) == 0x04)
  174. #undef IN6_IS_ADDR_LINKLOCAL
  175. #define IN6_IS_ADDR_LINKLOCAL(a) (((a)->S6_un.S6_l[0] & 0xC0FF) == 0x80FE)
  176. #undef IN6_IS_ADDR_MULTICAST
  177. #define IN6_IS_ADDR_MULTICAST(a) (((a)->S6_un.S6_l[0] & 0xFF) == 0xFF)
  178. #undef IN6_IS_ADDR_NSAP
  179. #define IN6_IS_ADDR_NSAP(a) (((a)->S6_un.S6_l[0] & 0xFE) == 0x02)
  180. #undef IN6_IS_ADDR_PROVIDER
  181. #define IN6_IS_ADDR_PROVIDER(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x40)
  182. #undef IN6_IS_ADDR_SITELOCAL
  183. #define IN6_IS_ADDR_SITELOCAL(a) (((a)->S6_un.S6_l[0] & 0xC0FF) == 0xC0FE)
  184. #endif /* ISC_PLATFORM_FIXIN6ISADDR */
  185. /*
  186. * Ensure type in_port_t is defined.
  187. */
  188. #ifdef ISC_PLATFORM_NEEDPORTT
  189. typedef isc_uint16_t in_port_t;
  190. #endif
  191. /*
  192. * If this system does not have MSG_TRUNC (as returned from recvmsg())
  193. * ISC_PLATFORM_RECVOVERFLOW will be defined. This will enable the MSG_TRUNC
  194. * faking code in socket.c.
  195. */
  196. #ifndef MSG_TRUNC
  197. #define ISC_PLATFORM_RECVOVERFLOW
  198. #endif
  199. #define ISC__IPADDR(x) ((isc_uint32_t)htonl((isc_uint32_t)(x)))
  200. #define ISC_IPADDR_ISMULTICAST(i) \
  201. (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
  202. == ISC__IPADDR(0xe0000000))
  203. #define ISC_IPADDR_ISEXPERIMENTAL(i) \
  204. (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
  205. == ISC__IPADDR(0xf0000000))
  206. /***
  207. *** Functions.
  208. ***/
  209. ISC_LANG_BEGINDECLS
  210. isc_result_t
  211. isc_net_probeipv4(void);
  212. /*
  213. * Check if the system's kernel supports IPv4.
  214. *
  215. * Returns:
  216. *
  217. * ISC_R_SUCCESS IPv4 is supported.
  218. * ISC_R_NOTFOUND IPv4 is not supported.
  219. * ISC_R_DISABLED IPv4 is disabled.
  220. * ISC_R_UNEXPECTED
  221. */
  222. isc_result_t
  223. isc_net_probeipv6(void);
  224. /*
  225. * Check if the system's kernel supports IPv6.
  226. *
  227. * Returns:
  228. *
  229. * ISC_R_SUCCESS IPv6 is supported.
  230. * ISC_R_NOTFOUND IPv6 is not supported.
  231. * ISC_R_DISABLED IPv6 is disabled.
  232. * ISC_R_UNEXPECTED
  233. */
  234. isc_result_t
  235. isc_net_probe_ipv6only(void);
  236. /*
  237. * Check if the system's kernel supports the IPV6_V6ONLY socket option.
  238. *
  239. * Returns:
  240. *
  241. * ISC_R_SUCCESS the option is supported for both TCP and UDP.
  242. * ISC_R_NOTFOUND IPv6 itself or the option is not supported.
  243. * ISC_R_UNEXPECTED
  244. */
  245. isc_result_t
  246. isc_net_probe_ipv6pktinfo(void);
  247. /*
  248. * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option
  249. * for UDP sockets.
  250. *
  251. * Returns:
  252. *
  253. * ISC_R_SUCCESS the option is supported.
  254. * ISC_R_NOTFOUND IPv6 itself or the option is not supported.
  255. * ISC_R_UNEXPECTED
  256. */
  257. void
  258. isc_net_disableipv4(void);
  259. void
  260. isc_net_disableipv6(void);
  261. void
  262. isc_net_enableipv4(void);
  263. void
  264. isc_net_enableipv6(void);
  265. #ifdef ISC_PLATFORM_NEEDNTOP
  266. const char *
  267. isc_net_ntop(int af, const void *src, char *dst, size_t size);
  268. #define inet_ntop isc_net_ntop
  269. #endif
  270. #ifdef ISC_PLATFORM_NEEDPTON
  271. int
  272. isc_net_pton(int af, const char *src, void *dst);
  273. #undef inet_pton
  274. #define inet_pton isc_net_pton
  275. #endif
  276. #ifdef ISC_PLATFORM_NEEDATON
  277. int
  278. isc_net_aton(const char *cp, struct in_addr *addr);
  279. #define inet_aton isc_net_aton
  280. #endif
  281. ISC_LANG_ENDDECLS
  282. #endif /* ISC_NET_H */