PageRenderTime 22ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/bind-9.9.1-P1/lib/isc/win32/net.c

#
C | 334 lines | 259 code | 46 blank | 29 comment | 33 complexity | 277d9c670ea28a4c247c9c2b3b480b82 MD5 | raw file
Possible License(s): BSD-3-Clause, ISC
  1. /*
  2. * Copyright (C) 2004, 2005, 2007-2009, 2011, 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. #include <config.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <isc/log.h>
  22. #include <isc/msgs.h>
  23. #include <isc/net.h>
  24. #include <isc/once.h>
  25. #include <isc/strerror.h>
  26. #include <isc/string.h>
  27. #include <isc/util.h>
  28. /*%
  29. * Definitions about UDP port range specification. This is a total mess of
  30. * portability variants: some use sysctl (but the sysctl names vary), some use
  31. * system-specific interfaces, some have the same interface for IPv4 and IPv6,
  32. * some separate them, etc...
  33. */
  34. /*%
  35. * The last resort defaults: use all non well known port space
  36. */
  37. #ifndef ISC_NET_PORTRANGELOW
  38. #define ISC_NET_PORTRANGELOW 1024
  39. #endif /* ISC_NET_PORTRANGELOW */
  40. #ifndef ISC_NET_PORTRANGEHIGH
  41. #define ISC_NET_PORTRANGEHIGH 65535
  42. #endif /* ISC_NET_PORTRANGEHIGH */
  43. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
  44. const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
  45. #endif
  46. static isc_once_t once = ISC_ONCE_INIT;
  47. static isc_once_t once_ipv6only = ISC_ONCE_INIT;
  48. static isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
  49. static isc_result_t ipv4_result = ISC_R_NOTFOUND;
  50. static isc_result_t ipv6_result = ISC_R_NOTFOUND;
  51. static isc_result_t ipv6only_result = ISC_R_NOTFOUND;
  52. static isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
  53. void InitSockets(void);
  54. static isc_result_t
  55. try_proto(int domain) {
  56. SOCKET s;
  57. isc_result_t result = ISC_R_SUCCESS;
  58. char strbuf[ISC_STRERRORSIZE];
  59. int errval;
  60. s = socket(domain, SOCK_STREAM, IPPROTO_TCP);
  61. if (s == INVALID_SOCKET) {
  62. errval = WSAGetLastError();
  63. switch (errval) {
  64. case WSAEAFNOSUPPORT:
  65. case WSAEPROTONOSUPPORT:
  66. case WSAEINVAL:
  67. return (ISC_R_NOTFOUND);
  68. default:
  69. isc__strerror(errval, strbuf, sizeof(strbuf));
  70. UNEXPECTED_ERROR(__FILE__, __LINE__,
  71. "socket() %s: %s",
  72. isc_msgcat_get(isc_msgcat,
  73. ISC_MSGSET_GENERAL,
  74. ISC_MSG_FAILED,
  75. "failed"),
  76. strbuf);
  77. return (ISC_R_UNEXPECTED);
  78. }
  79. }
  80. closesocket(s);
  81. return (ISC_R_SUCCESS);
  82. }
  83. static void
  84. initialize_action(void) {
  85. InitSockets();
  86. ipv4_result = try_proto(PF_INET);
  87. #ifdef ISC_PLATFORM_HAVEIPV6
  88. #ifdef WANT_IPV6
  89. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  90. ipv6_result = try_proto(PF_INET6);
  91. #endif
  92. #endif
  93. #endif
  94. }
  95. static void
  96. initialize(void) {
  97. RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
  98. }
  99. isc_result_t
  100. isc_net_probeipv4(void) {
  101. initialize();
  102. return (ipv4_result);
  103. }
  104. isc_result_t
  105. isc_net_probeipv6(void) {
  106. initialize();
  107. return (ipv6_result);
  108. }
  109. isc_result_t
  110. isc_net_probeunix(void) {
  111. return (ISC_R_NOTFOUND);
  112. }
  113. #ifdef ISC_PLATFORM_HAVEIPV6
  114. #ifdef WANT_IPV6
  115. static void
  116. try_ipv6only(void) {
  117. #ifdef IPV6_V6ONLY
  118. SOCKET s;
  119. int on;
  120. char strbuf[ISC_STRERRORSIZE];
  121. #endif
  122. isc_result_t result;
  123. result = isc_net_probeipv6();
  124. if (result != ISC_R_SUCCESS) {
  125. ipv6only_result = result;
  126. return;
  127. }
  128. #ifndef IPV6_V6ONLY
  129. ipv6only_result = ISC_R_NOTFOUND;
  130. return;
  131. #else
  132. /* check for TCP sockets */
  133. s = socket(PF_INET6, SOCK_STREAM, 0);
  134. if (s == INVALID_SOCKET) {
  135. isc__strerror(errno, strbuf, sizeof(strbuf));
  136. UNEXPECTED_ERROR(__FILE__, __LINE__,
  137. "socket() %s: %s",
  138. isc_msgcat_get(isc_msgcat,
  139. ISC_MSGSET_GENERAL,
  140. ISC_MSG_FAILED,
  141. "failed"),
  142. strbuf);
  143. ipv6only_result = ISC_R_UNEXPECTED;
  144. return;
  145. }
  146. on = 1;
  147. if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&on,
  148. sizeof(on)) < 0) {
  149. ipv6only_result = ISC_R_NOTFOUND;
  150. goto close;
  151. }
  152. closesocket(s);
  153. /* check for UDP sockets */
  154. s = socket(PF_INET6, SOCK_DGRAM, 0);
  155. if (s == INVALID_SOCKET) {
  156. isc__strerror(errno, strbuf, sizeof(strbuf));
  157. UNEXPECTED_ERROR(__FILE__, __LINE__,
  158. "socket() %s: %s",
  159. isc_msgcat_get(isc_msgcat,
  160. ISC_MSGSET_GENERAL,
  161. ISC_MSG_FAILED,
  162. "failed"),
  163. strbuf);
  164. ipv6only_result = ISC_R_UNEXPECTED;
  165. return;
  166. }
  167. on = 1;
  168. if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&on,
  169. sizeof(on)) < 0) {
  170. ipv6only_result = ISC_R_NOTFOUND;
  171. goto close;
  172. }
  173. ipv6only_result = ISC_R_SUCCESS;
  174. close:
  175. closesocket(s);
  176. return;
  177. #endif /* IPV6_V6ONLY */
  178. }
  179. static void
  180. initialize_ipv6only(void) {
  181. RUNTIME_CHECK(isc_once_do(&once_ipv6only,
  182. try_ipv6only) == ISC_R_SUCCESS);
  183. }
  184. static void
  185. try_ipv6pktinfo(void) {
  186. int s, on;
  187. char strbuf[ISC_STRERRORSIZE];
  188. isc_result_t result;
  189. int optname;
  190. result = isc_net_probeipv6();
  191. if (result != ISC_R_SUCCESS) {
  192. ipv6pktinfo_result = result;
  193. return;
  194. }
  195. /* we only use this for UDP sockets */
  196. s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  197. if (s == INVALID_SOCKET) {
  198. isc__strerror(errno, strbuf, sizeof(strbuf));
  199. UNEXPECTED_ERROR(__FILE__, __LINE__,
  200. "socket() %s: %s",
  201. isc_msgcat_get(isc_msgcat,
  202. ISC_MSGSET_GENERAL,
  203. ISC_MSG_FAILED,
  204. "failed"),
  205. strbuf);
  206. ipv6pktinfo_result = ISC_R_UNEXPECTED;
  207. return;
  208. }
  209. #ifdef IPV6_RECVPKTINFO
  210. optname = IPV6_RECVPKTINFO;
  211. #else
  212. optname = IPV6_PKTINFO;
  213. #endif
  214. on = 1;
  215. if (setsockopt(s, IPPROTO_IPV6, optname, (const char *) &on,
  216. sizeof(on)) < 0) {
  217. ipv6pktinfo_result = ISC_R_NOTFOUND;
  218. goto close;
  219. }
  220. ipv6pktinfo_result = ISC_R_SUCCESS;
  221. close:
  222. closesocket(s);
  223. return;
  224. }
  225. static void
  226. initialize_ipv6pktinfo(void) {
  227. RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
  228. try_ipv6pktinfo) == ISC_R_SUCCESS);
  229. }
  230. #endif /* WANT_IPV6 */
  231. #endif /* ISC_PLATFORM_HAVEIPV6 */
  232. isc_result_t
  233. isc_net_probe_ipv6only(void) {
  234. #ifdef ISC_PLATFORM_HAVEIPV6
  235. #ifdef WANT_IPV6
  236. initialize_ipv6only();
  237. #else
  238. ipv6only_result = ISC_R_NOTFOUND;
  239. #endif
  240. #endif
  241. return (ipv6only_result);
  242. }
  243. isc_result_t
  244. isc_net_probe_ipv6pktinfo(void) {
  245. #ifdef ISC_PLATFORM_HAVEIPV6
  246. #ifdef WANT_IPV6
  247. initialize_ipv6pktinfo();
  248. #else
  249. ipv6pktinfo_result = ISC_R_NOTFOUND;
  250. #endif
  251. #endif
  252. return (ipv6pktinfo_result);
  253. }
  254. isc_result_t
  255. isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
  256. int result = ISC_R_FAILURE;
  257. REQUIRE(low != NULL && high != NULL);
  258. UNUSED(af);
  259. if (result != ISC_R_SUCCESS) {
  260. *low = ISC_NET_PORTRANGELOW;
  261. *high = ISC_NET_PORTRANGEHIGH;
  262. }
  263. return (ISC_R_SUCCESS); /* we currently never fail in this function */
  264. }
  265. void
  266. isc_net_disableipv4(void) {
  267. initialize();
  268. if (ipv4_result == ISC_R_SUCCESS)
  269. ipv4_result = ISC_R_DISABLED;
  270. }
  271. void
  272. isc_net_disableipv6(void) {
  273. initialize();
  274. if (ipv6_result == ISC_R_SUCCESS)
  275. ipv6_result = ISC_R_DISABLED;
  276. }
  277. void
  278. isc_net_enableipv4(void) {
  279. initialize();
  280. if (ipv4_result == ISC_R_DISABLED)
  281. ipv4_result = ISC_R_SUCCESS;
  282. }
  283. void
  284. isc_net_enableipv6(void) {
  285. initialize();
  286. if (ipv6_result == ISC_R_DISABLED)
  287. ipv6_result = ISC_R_SUCCESS;
  288. }