PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/bind9/lib/isc/unix/include/isc/net.h

https://bitbucket.org/freebsd/freebsd-head/
C Header | 364 lines | 155 code | 49 blank | 160 comment | 35 complexity | b00aa15bf7c93c01e32b38ea94c9619b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or 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$ */
  18. #ifndef ISC_NET_H
  19. #define ISC_NET_H 1
  20. /*****
  21. ***** Module Info
  22. *****/
  23. /*! \file
  24. * \brief
  25. * Basic Networking Types
  26. *
  27. * This module is responsible for defining the following basic networking
  28. * types:
  29. *
  30. *\li struct in_addr
  31. *\li struct in6_addr
  32. *\li struct in6_pktinfo
  33. *\li struct sockaddr
  34. *\li struct sockaddr_in
  35. *\li struct sockaddr_in6
  36. *\li in_port_t
  37. *
  38. * It ensures that the AF_ and PF_ macros are defined.
  39. *
  40. * It declares ntoh[sl]() and hton[sl]().
  41. *
  42. * It declares inet_aton(), inet_ntop(), and inet_pton().
  43. *
  44. * It ensures that #INADDR_LOOPBACK, #INADDR_ANY, #IN6ADDR_ANY_INIT,
  45. * in6addr_any, and in6addr_loopback are available.
  46. *
  47. * It ensures that IN_MULTICAST() is available to check for multicast
  48. * addresses.
  49. *
  50. * MP:
  51. *\li No impact.
  52. *
  53. * Reliability:
  54. *\li No anticipated impact.
  55. *
  56. * Resources:
  57. *\li N/A.
  58. *
  59. * Security:
  60. *\li No anticipated impact.
  61. *
  62. * Standards:
  63. *\li BSD Socket API
  64. *\li RFC2553
  65. */
  66. /***
  67. *** Imports.
  68. ***/
  69. #include <isc/platform.h>
  70. #include <sys/types.h>
  71. #include <sys/socket.h> /* Contractual promise. */
  72. #include <net/if.h>
  73. #include <netinet/in.h> /* Contractual promise. */
  74. #include <arpa/inet.h> /* Contractual promise. */
  75. #ifdef ISC_PLATFORM_NEEDNETINETIN6H
  76. #include <netinet/in6.h> /* Required on UnixWare. */
  77. #endif
  78. #ifdef ISC_PLATFORM_NEEDNETINET6IN6H
  79. #include <netinet6/in6.h> /* Required on BSD/OS for in6_pktinfo. */
  80. #endif
  81. #ifndef ISC_PLATFORM_HAVEIPV6
  82. #include <isc/ipv6.h> /* Contractual promise. */
  83. #endif
  84. #include <isc/lang.h>
  85. #include <isc/types.h>
  86. #ifdef ISC_PLATFORM_HAVEINADDR6
  87. #define in6_addr in_addr6 /*%< Required for pre RFC2133 implementations. */
  88. #endif
  89. #ifdef ISC_PLATFORM_HAVEIPV6
  90. #ifndef IN6ADDR_ANY_INIT
  91. #ifdef s6_addr
  92. /*%
  93. * Required for some pre RFC2133 implementations.
  94. * IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT were added in
  95. * draft-ietf-ipngwg-bsd-api-04.txt or draft-ietf-ipngwg-bsd-api-05.txt.
  96. * If 's6_addr' is defined then assume that there is a union and three
  97. * levels otherwise assume two levels required.
  98. */
  99. #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  100. #else
  101. #define IN6ADDR_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }
  102. #endif
  103. #endif
  104. #ifndef IN6ADDR_LOOPBACK_INIT
  105. #ifdef s6_addr
  106. /*% IPv6 address loopback init */
  107. #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  108. #else
  109. #define IN6ADDR_LOOPBACK_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } }
  110. #endif
  111. #endif
  112. #ifndef IN6_IS_ADDR_V4MAPPED
  113. /*% Is IPv6 address V4 mapped? */
  114. #define IN6_IS_ADDR_V4MAPPED(x) \
  115. (memcmp((x)->s6_addr, in6addr_any.s6_addr, 10) == 0 && \
  116. (x)->s6_addr[10] == 0xff && (x)->s6_addr[11] == 0xff)
  117. #endif
  118. #ifndef IN6_IS_ADDR_V4COMPAT
  119. /*% Is IPv6 address V4 compatible? */
  120. #define IN6_IS_ADDR_V4COMPAT(x) \
  121. (memcmp((x)->s6_addr, in6addr_any.s6_addr, 12) == 0 && \
  122. ((x)->s6_addr[12] != 0 || (x)->s6_addr[13] != 0 || \
  123. (x)->s6_addr[14] != 0 || \
  124. ((x)->s6_addr[15] != 0 && (x)->s6_addr[15] != 1)))
  125. #endif
  126. #ifndef IN6_IS_ADDR_MULTICAST
  127. /*% Is IPv6 address multicast? */
  128. #define IN6_IS_ADDR_MULTICAST(a) ((a)->s6_addr[0] == 0xff)
  129. #endif
  130. #ifndef IN6_IS_ADDR_LINKLOCAL
  131. /*% Is IPv6 address linklocal? */
  132. #define IN6_IS_ADDR_LINKLOCAL(a) \
  133. (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80))
  134. #endif
  135. #ifndef IN6_IS_ADDR_SITELOCAL
  136. /*% is IPv6 address sitelocal? */
  137. #define IN6_IS_ADDR_SITELOCAL(a) \
  138. (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0))
  139. #endif
  140. #ifndef IN6_IS_ADDR_LOOPBACK
  141. /*% is IPv6 address loopback? */
  142. #define IN6_IS_ADDR_LOOPBACK(x) \
  143. (memcmp((x)->s6_addr, in6addr_loopback.s6_addr, 16) == 0)
  144. #endif
  145. #endif
  146. #ifndef AF_INET6
  147. /*% IPv6 */
  148. #define AF_INET6 99
  149. #endif
  150. #ifndef PF_INET6
  151. /*% IPv6 */
  152. #define PF_INET6 AF_INET6
  153. #endif
  154. #ifndef INADDR_LOOPBACK
  155. /*% inaddr loopback */
  156. #define INADDR_LOOPBACK 0x7f000001UL
  157. #endif
  158. #ifndef ISC_PLATFORM_HAVEIN6PKTINFO
  159. /*% IPv6 packet info */
  160. struct in6_pktinfo {
  161. struct in6_addr ipi6_addr; /*%< src/dst IPv6 address */
  162. unsigned int ipi6_ifindex; /*%< send/recv interface index */
  163. };
  164. #endif
  165. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
  166. extern const struct in6_addr isc_net_in6addrany;
  167. /*%
  168. * Cope with a missing in6addr_any and in6addr_loopback.
  169. */
  170. #define in6addr_any isc_net_in6addrany
  171. #endif
  172. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
  173. extern const struct in6_addr isc_net_in6addrloop;
  174. #define in6addr_loopback isc_net_in6addrloop
  175. #endif
  176. #ifdef ISC_PLATFORM_FIXIN6ISADDR
  177. #undef IN6_IS_ADDR_GEOGRAPHIC
  178. /*!
  179. * \brief
  180. * Fix UnixWare 7.1.1's broken IN6_IS_ADDR_* definitions.
  181. */
  182. #define IN6_IS_ADDR_GEOGRAPHIC(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x80)
  183. #undef IN6_IS_ADDR_IPX
  184. #define IN6_IS_ADDR_IPX(a) (((a)->S6_un.S6_l[0] & 0xFE) == 0x04)
  185. #undef IN6_IS_ADDR_LINKLOCAL
  186. #define IN6_IS_ADDR_LINKLOCAL(a) (((a)->S6_un.S6_l[0] & 0xC0FF) == 0x80FE)
  187. #undef IN6_IS_ADDR_MULTICAST
  188. #define IN6_IS_ADDR_MULTICAST(a) (((a)->S6_un.S6_l[0] & 0xFF) == 0xFF)
  189. #undef IN6_IS_ADDR_NSAP
  190. #define IN6_IS_ADDR_NSAP(a) (((a)->S6_un.S6_l[0] & 0xFE) == 0x02)
  191. #undef IN6_IS_ADDR_PROVIDER
  192. #define IN6_IS_ADDR_PROVIDER(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x40)
  193. #undef IN6_IS_ADDR_SITELOCAL
  194. #define IN6_IS_ADDR_SITELOCAL(a) (((a)->S6_un.S6_l[0] & 0xC0FF) == 0xC0FE)
  195. #endif /* ISC_PLATFORM_FIXIN6ISADDR */
  196. #ifdef ISC_PLATFORM_NEEDPORTT
  197. /*%
  198. * Ensure type in_port_t is defined.
  199. */
  200. typedef isc_uint16_t in_port_t;
  201. #endif
  202. #ifndef MSG_TRUNC
  203. /*%
  204. * If this system does not have MSG_TRUNC (as returned from recvmsg())
  205. * ISC_PLATFORM_RECVOVERFLOW will be defined. This will enable the MSG_TRUNC
  206. * faking code in socket.c.
  207. */
  208. #define ISC_PLATFORM_RECVOVERFLOW
  209. #endif
  210. /*% IP address. */
  211. #define ISC__IPADDR(x) ((isc_uint32_t)htonl((isc_uint32_t)(x)))
  212. /*% Is IP address multicast? */
  213. #define ISC_IPADDR_ISMULTICAST(i) \
  214. (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
  215. == ISC__IPADDR(0xe0000000))
  216. #define ISC_IPADDR_ISEXPERIMENTAL(i) \
  217. (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
  218. == ISC__IPADDR(0xf0000000))
  219. /***
  220. *** Functions.
  221. ***/
  222. ISC_LANG_BEGINDECLS
  223. isc_result_t
  224. isc_net_probeipv4(void);
  225. /*%<
  226. * Check if the system's kernel supports IPv4.
  227. *
  228. * Returns:
  229. *
  230. *\li #ISC_R_SUCCESS IPv4 is supported.
  231. *\li #ISC_R_NOTFOUND IPv4 is not supported.
  232. *\li #ISC_R_DISABLED IPv4 is disabled.
  233. *\li #ISC_R_UNEXPECTED
  234. */
  235. isc_result_t
  236. isc_net_probeipv6(void);
  237. /*%<
  238. * Check if the system's kernel supports IPv6.
  239. *
  240. * Returns:
  241. *
  242. *\li #ISC_R_SUCCESS IPv6 is supported.
  243. *\li #ISC_R_NOTFOUND IPv6 is not supported.
  244. *\li #ISC_R_DISABLED IPv6 is disabled.
  245. *\li #ISC_R_UNEXPECTED
  246. */
  247. isc_result_t
  248. isc_net_probe_ipv6only(void);
  249. /*%<
  250. * Check if the system's kernel supports the IPV6_V6ONLY socket option.
  251. *
  252. * Returns:
  253. *
  254. *\li #ISC_R_SUCCESS the option is supported for both TCP and UDP.
  255. *\li #ISC_R_NOTFOUND IPv6 itself or the option is not supported.
  256. *\li #ISC_R_UNEXPECTED
  257. */
  258. isc_result_t
  259. isc_net_probe_ipv6pktinfo(void);
  260. /*
  261. * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option
  262. * for UDP sockets.
  263. *
  264. * Returns:
  265. *
  266. * \li #ISC_R_SUCCESS the option is supported.
  267. * \li #ISC_R_NOTFOUND IPv6 itself or the option is not supported.
  268. * \li #ISC_R_UNEXPECTED
  269. */
  270. void
  271. isc_net_disableipv4(void);
  272. void
  273. isc_net_disableipv6(void);
  274. void
  275. isc_net_enableipv4(void);
  276. void
  277. isc_net_enableipv6(void);
  278. isc_result_t
  279. isc_net_probeunix(void);
  280. /*
  281. * Returns whether UNIX domain sockets are supported.
  282. */
  283. isc_result_t
  284. isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high);
  285. /*%<
  286. * Returns system's default range of ephemeral UDP ports, if defined.
  287. * If the range is not available or unknown, ISC_NET_PORTRANGELOW and
  288. * ISC_NET_PORTRANGEHIGH will be returned.
  289. *
  290. * Requires:
  291. *
  292. *\li 'low' and 'high' must be non NULL.
  293. *
  294. * Returns:
  295. *
  296. *\li *low and *high will be the ports specifying the low and high ends of
  297. * the range.
  298. */
  299. #ifdef ISC_PLATFORM_NEEDNTOP
  300. const char *
  301. isc_net_ntop(int af, const void *src, char *dst, size_t size);
  302. #define inet_ntop isc_net_ntop
  303. #endif
  304. #ifdef ISC_PLATFORM_NEEDPTON
  305. int
  306. isc_net_pton(int af, const char *src, void *dst);
  307. #undef inet_pton
  308. #define inet_pton isc_net_pton
  309. #endif
  310. int
  311. isc_net_aton(const char *cp, struct in_addr *addr);
  312. #undef inet_aton
  313. #define inet_aton isc_net_aton
  314. ISC_LANG_ENDDECLS
  315. #endif /* ISC_NET_H */