2 * Copyright (C) 2004, 2005, 2007-2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
29 #include <isc/strerror.h>
30 #include <isc/string.h>
34 * Definitions about UDP port range specification. This is a total mess of
35 * portability variants: some use sysctl (but the sysctl names vary), some use
36 * system-specific interfaces, some have the same interface for IPv4 and IPv6,
37 * some separate them, etc...
41 * The last resort defaults: use all non well known port space
43 #ifndef ISC_NET_PORTRANGELOW
44 #define ISC_NET_PORTRANGELOW 1024
45 #endif /* ISC_NET_PORTRANGELOW */
46 #ifndef ISC_NET_PORTRANGEHIGH
47 #define ISC_NET_PORTRANGEHIGH 65535
48 #endif /* ISC_NET_PORTRANGEHIGH */
50 #if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
51 const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
54 #if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
55 const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
59 static isc_once_t once = ISC_ONCE_INIT;
60 static isc_once_t once_ipv6only = ISC_ONCE_INIT;
61 static isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
62 static isc_result_t ipv4_result = ISC_R_NOTFOUND;
63 static isc_result_t ipv6_result = ISC_R_NOTFOUND;
64 static isc_result_t ipv6only_result = ISC_R_NOTFOUND;
65 static isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
67 void InitSockets(void);
70 try_proto(int domain) {
72 char strbuf[ISC_STRERRORSIZE];
75 s = socket(domain, SOCK_STREAM, IPPROTO_TCP);
76 if (s == INVALID_SOCKET) {
77 errval = WSAGetLastError();
80 case WSAEPROTONOSUPPORT:
82 return (ISC_R_NOTFOUND);
84 isc__strerror(errval, strbuf, sizeof(strbuf));
85 UNEXPECTED_ERROR(__FILE__, __LINE__,
87 isc_msgcat_get(isc_msgcat,
92 return (ISC_R_UNEXPECTED);
98 return (ISC_R_SUCCESS);
102 initialize_action(void) {
104 ipv4_result = try_proto(PF_INET);
105 #ifdef ISC_PLATFORM_HAVEIPV6
107 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
108 ipv6_result = try_proto(PF_INET6);
116 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
120 isc_net_probeipv4(void) {
122 return (ipv4_result);
126 isc_net_probeipv6(void) {
128 return (ipv6_result);
132 isc_net_probeunix(void) {
133 return (ISC_R_NOTFOUND);
136 #ifdef ISC_PLATFORM_HAVEIPV6
143 char strbuf[ISC_STRERRORSIZE];
147 result = isc_net_probeipv6();
148 if (result != ISC_R_SUCCESS) {
149 ipv6only_result = result;
154 ipv6only_result = ISC_R_NOTFOUND;
157 /* check for TCP sockets */
158 s = socket(PF_INET6, SOCK_STREAM, 0);
159 if (s == INVALID_SOCKET) {
160 isc__strerror(errno, strbuf, sizeof(strbuf));
161 UNEXPECTED_ERROR(__FILE__, __LINE__,
163 isc_msgcat_get(isc_msgcat,
168 ipv6only_result = ISC_R_UNEXPECTED;
173 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&on,
175 ipv6only_result = ISC_R_NOTFOUND;
181 /* check for UDP sockets */
182 s = socket(PF_INET6, SOCK_DGRAM, 0);
183 if (s == INVALID_SOCKET) {
184 isc__strerror(errno, strbuf, sizeof(strbuf));
185 UNEXPECTED_ERROR(__FILE__, __LINE__,
187 isc_msgcat_get(isc_msgcat,
192 ipv6only_result = ISC_R_UNEXPECTED;
197 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&on,
199 ipv6only_result = ISC_R_NOTFOUND;
203 ipv6only_result = ISC_R_SUCCESS;
208 #endif /* IPV6_V6ONLY */
212 initialize_ipv6only(void) {
213 RUNTIME_CHECK(isc_once_do(&once_ipv6only,
214 try_ipv6only) == ISC_R_SUCCESS);
218 try_ipv6pktinfo(void) {
221 char strbuf[ISC_STRERRORSIZE];
225 result = isc_net_probeipv6();
226 if (result != ISC_R_SUCCESS) {
227 ipv6pktinfo_result = result;
231 /* we only use this for UDP sockets */
232 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
233 if (s == INVALID_SOCKET) {
234 isc__strerror(errno, strbuf, sizeof(strbuf));
235 UNEXPECTED_ERROR(__FILE__, __LINE__,
237 isc_msgcat_get(isc_msgcat,
242 ipv6pktinfo_result = ISC_R_UNEXPECTED;
246 #ifdef IPV6_RECVPKTINFO
247 optname = IPV6_RECVPKTINFO;
249 optname = IPV6_PKTINFO;
252 if (setsockopt(s, IPPROTO_IPV6, optname, (const char *) &on,
254 ipv6pktinfo_result = ISC_R_NOTFOUND;
258 ipv6pktinfo_result = ISC_R_SUCCESS;
266 initialize_ipv6pktinfo(void) {
267 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
268 try_ipv6pktinfo) == ISC_R_SUCCESS);
270 #endif /* WANT_IPV6 */
271 #endif /* ISC_PLATFORM_HAVEIPV6 */
274 isc_net_probe_ipv6only(void) {
275 #ifdef ISC_PLATFORM_HAVEIPV6
277 initialize_ipv6only();
279 ipv6only_result = ISC_R_NOTFOUND;
282 return (ipv6only_result);
286 isc_net_probe_ipv6pktinfo(void) {
287 #ifdef ISC_PLATFORM_HAVEIPV6
289 initialize_ipv6pktinfo();
291 ipv6pktinfo_result = ISC_R_NOTFOUND;
294 return (ipv6pktinfo_result);
298 isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
299 int result = ISC_R_FAILURE;
301 REQUIRE(low != NULL && high != NULL);
305 if (result != ISC_R_SUCCESS) {
306 *low = ISC_NET_PORTRANGELOW;
307 *high = ISC_NET_PORTRANGEHIGH;
310 return (ISC_R_SUCCESS); /* we currently never fail in this function */
314 isc_net_disableipv4(void) {
316 if (ipv4_result == ISC_R_SUCCESS)
317 ipv4_result = ISC_R_DISABLED;
321 isc_net_disableipv6(void) {
323 if (ipv6_result == ISC_R_SUCCESS)
324 ipv6_result = ISC_R_DISABLED;
328 isc_net_enableipv4(void) {
330 if (ipv4_result == ISC_R_DISABLED)
331 ipv4_result = ISC_R_SUCCESS;
335 isc_net_enableipv6(void) {
337 if (ipv6_result == ISC_R_DISABLED)
338 ipv6_result = ISC_R_SUCCESS;