2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
18 /* $Id: net.c,v 1.22.2.2.10.7 2004/04/29 01:31:22 marka Exp $ */
27 #include <isc/strerror.h>
28 #include <isc/string.h>
31 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
32 const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
35 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
36 const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
39 static isc_boolean_t once = ISC_FALSE;
40 static isc_once_t once_ipv6only = ISC_ONCE_INIT;
41 static isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
42 static isc_result_t ipv4_result = ISC_R_NOTFOUND;
43 static isc_result_t ipv6_result = ISC_R_NOTFOUND;
44 static isc_result_t ipv6only_result = ISC_R_NOTFOUND;
45 static isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
48 try_proto(int domain) {
50 isc_result_t result = ISC_R_SUCCESS;
51 char strbuf[ISC_STRERRORSIZE];
53 s = socket(domain, SOCK_STREAM, 0);
59 #ifdef EPROTONOSUPPORT
65 return (ISC_R_NOTFOUND);
67 isc__strerror(errno, strbuf, sizeof(strbuf));
68 UNEXPECTED_ERROR(__FILE__, __LINE__,
69 "socket() failed: %s",
71 return (ISC_R_UNEXPECTED);
75 #ifdef ISC_PLATFORM_HAVEIPV6
77 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
78 if (domain == PF_INET6) {
79 struct sockaddr_in6 sin6;
80 GETSOCKNAME_SOCKLEN_TYPE len;
83 * Check to see if IPv6 is broken, as is common on Linux.
86 if (getsockname(s, (struct sockaddr *)&sin6, &len) < 0)
88 result = ISC_R_NOTFOUND;
90 if (len == sizeof(struct sockaddr_in6))
91 result = ISC_R_SUCCESS;
93 result = ISC_R_NOTFOUND;
107 initialize_action(void) {
108 ipv4_result = try_proto(PF_INET);
109 #ifdef ISC_PLATFORM_HAVEIPV6
111 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
112 ipv6_result = try_proto(PF_INET6);
120 if(once == ISC_FALSE) {
127 isc_net_probeipv4(void) {
129 return (ipv4_result);
133 isc_net_probeipv6(void) {
135 return (ipv6_result);
138 #ifdef ISC_PLATFORM_HAVEIPV6
144 char strbuf[ISC_STRERRORSIZE];
148 result = isc_net_probeipv6();
149 if (result != ISC_R_SUCCESS) {
150 ipv6only_result = result;
155 ipv6only_result = ISC_R_NOTFOUND;
158 /* check for TCP sockets */
159 s = socket(PF_INET6, SOCK_STREAM, 0);
161 isc__strerror(errno, strbuf, sizeof(strbuf));
162 UNEXPECTED_ERROR(__FILE__, __LINE__,
163 "socket() failed: %s",
165 ipv6only_result = ISC_R_UNEXPECTED;
170 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
171 ipv6only_result = ISC_R_NOTFOUND;
177 /* check for UDP sockets */
178 s = socket(PF_INET6, SOCK_DGRAM, 0);
180 isc__strerror(errno, strbuf, sizeof(strbuf));
181 UNEXPECTED_ERROR(__FILE__, __LINE__,
182 "socket() failed: %s",
184 ipv6only_result = ISC_R_UNEXPECTED;
189 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
190 ipv6only_result = ISC_R_NOTFOUND;
196 ipv6only_result = ISC_R_SUCCESS;
201 #endif /* IPV6_V6ONLY */
205 initialize_ipv6only(void) {
206 RUNTIME_CHECK(isc_once_do(&once_ipv6only,
207 try_ipv6only) == ISC_R_SUCCESS);
211 try_ipv6pktinfo(void) {
213 char strbuf[ISC_STRERRORSIZE];
217 result = isc_net_probeipv6();
218 if (result != ISC_R_SUCCESS) {
219 ipv6pktinfo_result = result;
223 /* we only use this for UDP sockets */
224 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
226 isc__strerror(errno, strbuf, sizeof(strbuf));
227 UNEXPECTED_ERROR(__FILE__, __LINE__,
228 "socket() failed: %s",
230 ipv6pktinfo_result = ISC_R_UNEXPECTED;
234 #ifdef IPV6_RECVPKTINFO
235 optname = IPV6_RECVPKTINFO;
237 optname = IPV6_PKTINFO;
240 if (setsockopt(s, IPPROTO_IPV6, optname, &on, sizeof(on)) < 0) {
241 ipv6pktinfo_result = ISC_R_NOTFOUND;
246 ipv6pktinfo_result = ISC_R_SUCCESS;
254 initialize_ipv6pktinfo(void) {
255 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
256 try_ipv6pktinfo) == ISC_R_SUCCESS);
258 #endif /* WANT_IPV6 */
259 #endif /* ISC_PLATFORM_HAVEIPV6 */
262 isc_net_probe_ipv6only(void) {
263 #ifdef ISC_PLATFORM_HAVEIPV6
265 initialize_ipv6only();
267 ipv6only_result = ISC_R_NOTFOUND;
270 return (ipv6only_result);
274 isc_net_probe_ipv6pktinfo(void) {
275 #ifdef ISC_PLATFORM_HAVEIPV6
277 initialize_ipv6pktinfo();
279 ipv6pktinfo_result = ISC_R_NOTFOUND;
282 return (ipv6pktinfo_result);
286 isc_net_disableipv4(void) {
288 if (ipv4_result == ISC_R_SUCCESS)
289 ipv4_result = ISC_R_DISABLED;
293 isc_net_disableipv6(void) {
295 if (ipv6_result == ISC_R_SUCCESS)
296 ipv6_result = ISC_R_DISABLED;
300 isc_net_enableipv4(void) {
302 if (ipv4_result == ISC_R_DISABLED)
303 ipv4_result = ISC_R_SUCCESS;
307 isc_net_enableipv6(void) {
309 if (ipv6_result == ISC_R_DISABLED)
310 ipv6_result = ISC_R_SUCCESS;