/contrib/ntp/libisc/net.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 311 lines · 247 code · 41 blank · 23 comment · 37 complexity · 31e5b139d5f9e2d5da133a130fca111d 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.c,v 1.22.2.2.10.7 2004/04/29 01:31:22 marka Exp $ */
  18. #include <config.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <isc/net.h>
  22. #include <isc/once.h>
  23. #include <isc/strerror.h>
  24. #include <isc/string.h>
  25. #include <isc/util.h>
  26. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
  27. const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
  28. #endif
  29. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
  30. const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
  31. #endif
  32. static isc_boolean_t once = ISC_FALSE;
  33. static isc_once_t once_ipv6only = ISC_ONCE_INIT;
  34. static isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
  35. static isc_result_t ipv4_result = ISC_R_NOTFOUND;
  36. static isc_result_t ipv6_result = ISC_R_NOTFOUND;
  37. static isc_result_t ipv6only_result = ISC_R_NOTFOUND;
  38. static isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
  39. static isc_result_t
  40. try_proto(int domain) {
  41. int s;
  42. isc_result_t result = ISC_R_SUCCESS;
  43. char strbuf[ISC_STRERRORSIZE];
  44. s = socket(domain, SOCK_STREAM, 0);
  45. if (s == -1) {
  46. switch (errno) {
  47. #ifdef EAFNOSUPPORT
  48. case EAFNOSUPPORT:
  49. #endif
  50. #ifdef EPROTONOSUPPORT
  51. case EPROTONOSUPPORT:
  52. #endif
  53. #ifdef EINVAL
  54. case EINVAL:
  55. #endif
  56. return (ISC_R_NOTFOUND);
  57. default:
  58. isc__strerror(errno, strbuf, sizeof(strbuf));
  59. UNEXPECTED_ERROR(__FILE__, __LINE__,
  60. "socket() failed: %s",
  61. strbuf);
  62. return (ISC_R_UNEXPECTED);
  63. }
  64. }
  65. #ifdef ISC_PLATFORM_HAVEIPV6
  66. #ifdef WANT_IPV6
  67. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  68. if (domain == PF_INET6) {
  69. struct sockaddr_in6 sin6;
  70. GETSOCKNAME_SOCKLEN_TYPE len;
  71. /*
  72. * Check to see if IPv6 is broken, as is common on Linux.
  73. */
  74. len = sizeof(sin6);
  75. if (getsockname(s, (struct sockaddr *)&sin6, &len) < 0)
  76. {
  77. result = ISC_R_NOTFOUND;
  78. } else {
  79. if (len == sizeof(struct sockaddr_in6))
  80. result = ISC_R_SUCCESS;
  81. else {
  82. result = ISC_R_NOTFOUND;
  83. }
  84. }
  85. }
  86. #endif
  87. #endif
  88. #endif
  89. (void)close(s);
  90. return (result);
  91. }
  92. static void
  93. initialize_action(void) {
  94. ipv4_result = try_proto(PF_INET);
  95. #ifdef ISC_PLATFORM_HAVEIPV6
  96. #ifdef WANT_IPV6
  97. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  98. ipv6_result = try_proto(PF_INET6);
  99. #endif
  100. #endif
  101. #endif
  102. }
  103. static void
  104. initialize(void) {
  105. if(once == ISC_FALSE) {
  106. initialize_action();
  107. once = ISC_TRUE;
  108. }
  109. }
  110. isc_result_t
  111. isc_net_probeipv4(void) {
  112. initialize();
  113. return (ipv4_result);
  114. }
  115. isc_result_t
  116. isc_net_probeipv6(void) {
  117. initialize();
  118. return (ipv6_result);
  119. }
  120. #ifdef ISC_PLATFORM_HAVEIPV6
  121. #ifdef WANT_IPV6
  122. static void
  123. try_ipv6only(void) {
  124. #ifdef IPV6_V6ONLY
  125. int s, on;
  126. char strbuf[ISC_STRERRORSIZE];
  127. #endif
  128. isc_result_t result;
  129. result = isc_net_probeipv6();
  130. if (result != ISC_R_SUCCESS) {
  131. ipv6only_result = result;
  132. return;
  133. }
  134. #ifndef IPV6_V6ONLY
  135. ipv6only_result = ISC_R_NOTFOUND;
  136. return;
  137. #else
  138. /* check for TCP sockets */
  139. s = socket(PF_INET6, SOCK_STREAM, 0);
  140. if (s == -1) {
  141. isc__strerror(errno, strbuf, sizeof(strbuf));
  142. UNEXPECTED_ERROR(__FILE__, __LINE__,
  143. "socket() failed: %s",
  144. strbuf);
  145. ipv6only_result = ISC_R_UNEXPECTED;
  146. return;
  147. }
  148. on = 1;
  149. if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
  150. ipv6only_result = ISC_R_NOTFOUND;
  151. goto close;
  152. }
  153. close(s);
  154. /* check for UDP sockets */
  155. s = socket(PF_INET6, SOCK_DGRAM, 0);
  156. if (s == -1) {
  157. isc__strerror(errno, strbuf, sizeof(strbuf));
  158. UNEXPECTED_ERROR(__FILE__, __LINE__,
  159. "socket() failed: %s",
  160. strbuf);
  161. ipv6only_result = ISC_R_UNEXPECTED;
  162. return;
  163. }
  164. on = 1;
  165. if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
  166. ipv6only_result = ISC_R_NOTFOUND;
  167. goto close;
  168. }
  169. close(s);
  170. ipv6only_result = ISC_R_SUCCESS;
  171. close:
  172. close(s);
  173. return;
  174. #endif /* IPV6_V6ONLY */
  175. }
  176. static void
  177. initialize_ipv6only(void) {
  178. RUNTIME_CHECK(isc_once_do(&once_ipv6only,
  179. try_ipv6only) == ISC_R_SUCCESS);
  180. }
  181. static void
  182. try_ipv6pktinfo(void) {
  183. int s, on;
  184. char strbuf[ISC_STRERRORSIZE];
  185. isc_result_t result;
  186. int optname;
  187. result = isc_net_probeipv6();
  188. if (result != ISC_R_SUCCESS) {
  189. ipv6pktinfo_result = result;
  190. return;
  191. }
  192. /* we only use this for UDP sockets */
  193. s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  194. if (s == -1) {
  195. isc__strerror(errno, strbuf, sizeof(strbuf));
  196. UNEXPECTED_ERROR(__FILE__, __LINE__,
  197. "socket() failed: %s",
  198. strbuf);
  199. ipv6pktinfo_result = ISC_R_UNEXPECTED;
  200. return;
  201. }
  202. #ifdef IPV6_RECVPKTINFO
  203. optname = IPV6_RECVPKTINFO;
  204. #else
  205. optname = IPV6_PKTINFO;
  206. #endif
  207. on = 1;
  208. if (setsockopt(s, IPPROTO_IPV6, optname, &on, sizeof(on)) < 0) {
  209. ipv6pktinfo_result = ISC_R_NOTFOUND;
  210. goto close;
  211. }
  212. close(s);
  213. ipv6pktinfo_result = ISC_R_SUCCESS;
  214. close:
  215. close(s);
  216. return;
  217. }
  218. static void
  219. initialize_ipv6pktinfo(void) {
  220. RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
  221. try_ipv6pktinfo) == ISC_R_SUCCESS);
  222. }
  223. #endif /* WANT_IPV6 */
  224. #endif /* ISC_PLATFORM_HAVEIPV6 */
  225. isc_result_t
  226. isc_net_probe_ipv6only(void) {
  227. #ifdef ISC_PLATFORM_HAVEIPV6
  228. #ifdef WANT_IPV6
  229. initialize_ipv6only();
  230. #else
  231. ipv6only_result = ISC_R_NOTFOUND;
  232. #endif
  233. #endif
  234. return (ipv6only_result);
  235. }
  236. isc_result_t
  237. isc_net_probe_ipv6pktinfo(void) {
  238. #ifdef ISC_PLATFORM_HAVEIPV6
  239. #ifdef WANT_IPV6
  240. initialize_ipv6pktinfo();
  241. #else
  242. ipv6pktinfo_result = ISC_R_NOTFOUND;
  243. #endif
  244. #endif
  245. return (ipv6pktinfo_result);
  246. }
  247. void
  248. isc_net_disableipv4(void) {
  249. initialize();
  250. if (ipv4_result == ISC_R_SUCCESS)
  251. ipv4_result = ISC_R_DISABLED;
  252. }
  253. void
  254. isc_net_disableipv6(void) {
  255. initialize();
  256. if (ipv6_result == ISC_R_SUCCESS)
  257. ipv6_result = ISC_R_DISABLED;
  258. }
  259. void
  260. isc_net_enableipv4(void) {
  261. initialize();
  262. if (ipv4_result == ISC_R_DISABLED)
  263. ipv4_result = ISC_R_SUCCESS;
  264. }
  265. void
  266. isc_net_enableipv6(void) {
  267. initialize();
  268. if (ipv6_result == ISC_R_DISABLED)
  269. ipv6_result = ISC_R_SUCCESS;
  270. }