4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: net.c,v 1.20 2009/09/08 23:41:50 tbox Exp */
31 #include <isc/strerror.h>
32 #include <isc/string.h>
36 * Definitions about UDP port range specification. This is a total mess of
37 * portability variants: some use sysctl (but the sysctl names vary), some use
38 * system-specific interfaces, some have the same interface for IPv4 and IPv6,
39 * some separate them, etc...
43 * The last resort defaults: use all non well known port space
45 #ifndef ISC_NET_PORTRANGELOW
46 #define ISC_NET_PORTRANGELOW 1024
47 #endif /* ISC_NET_PORTRANGELOW */
48 #ifndef ISC_NET_PORTRANGEHIGH
49 #define ISC_NET_PORTRANGEHIGH 65535
50 #endif /* ISC_NET_PORTRANGEHIGH */
52 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
53 const struct in6_addr isc_net_in6addrany
= IN6ADDR_ANY_INIT
;
56 static isc_once_t once
= ISC_ONCE_INIT
;
57 static isc_once_t once_ipv6only
= ISC_ONCE_INIT
;
58 static isc_once_t once_ipv6pktinfo
= ISC_ONCE_INIT
;
59 static isc_result_t ipv4_result
= ISC_R_NOTFOUND
;
60 static isc_result_t ipv6_result
= ISC_R_NOTFOUND
;
61 static isc_result_t ipv6only_result
= ISC_R_NOTFOUND
;
62 static isc_result_t ipv6pktinfo_result
= ISC_R_NOTFOUND
;
64 void InitSockets(void);
67 try_proto(int domain
) {
69 isc_result_t result
= ISC_R_SUCCESS
;
70 char strbuf
[ISC_STRERRORSIZE
];
73 s
= socket(domain
, SOCK_STREAM
, IPPROTO_TCP
);
74 if (s
== INVALID_SOCKET
) {
75 errval
= WSAGetLastError();
78 case WSAEPROTONOSUPPORT
:
80 return (ISC_R_NOTFOUND
);
82 isc__strerror(errval
, strbuf
, sizeof(strbuf
));
83 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
85 isc_msgcat_get(isc_msgcat
,
90 return (ISC_R_UNEXPECTED
);
96 return (ISC_R_SUCCESS
);
100 initialize_action(void) {
102 ipv4_result
= try_proto(PF_INET
);
103 #ifdef ISC_PLATFORM_HAVEIPV6
105 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
106 ipv6_result
= try_proto(PF_INET6
);
114 RUNTIME_CHECK(isc_once_do(&once
, initialize_action
) == ISC_R_SUCCESS
);
118 isc_net_probeipv4(void) {
120 return (ipv4_result
);
124 isc_net_probeipv6(void) {
126 return (ipv6_result
);
130 isc_net_probeunix(void) {
131 return (ISC_R_NOTFOUND
);
134 #ifdef ISC_PLATFORM_HAVEIPV6
141 char strbuf
[ISC_STRERRORSIZE
];
145 result
= isc_net_probeipv6();
146 if (result
!= ISC_R_SUCCESS
) {
147 ipv6only_result
= result
;
152 ipv6only_result
= ISC_R_NOTFOUND
;
155 /* check for TCP sockets */
156 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
157 if (s
== INVALID_SOCKET
) {
158 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
159 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
161 isc_msgcat_get(isc_msgcat
,
166 ipv6only_result
= ISC_R_UNEXPECTED
;
171 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
)) < 0) {
172 ipv6only_result
= ISC_R_NOTFOUND
;
178 /* check for UDP sockets */
179 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
180 if (s
== INVALID_SOCKET
) {
181 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
182 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
184 isc_msgcat_get(isc_msgcat
,
189 ipv6only_result
= ISC_R_UNEXPECTED
;
194 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
)) < 0) {
195 ipv6only_result
= ISC_R_NOTFOUND
;
199 ipv6only_result
= ISC_R_SUCCESS
;
204 #endif /* IPV6_V6ONLY */
208 initialize_ipv6only(void) {
209 RUNTIME_CHECK(isc_once_do(&once_ipv6only
,
210 try_ipv6only
) == ISC_R_SUCCESS
);
214 try_ipv6pktinfo(void) {
216 char strbuf
[ISC_STRERRORSIZE
];
220 result
= isc_net_probeipv6();
221 if (result
!= ISC_R_SUCCESS
) {
222 ipv6pktinfo_result
= result
;
226 /* we only use this for UDP sockets */
227 s
= socket(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
228 if (s
== INVALID_SOCKET
) {
229 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
230 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
232 isc_msgcat_get(isc_msgcat
,
237 ipv6pktinfo_result
= ISC_R_UNEXPECTED
;
241 #ifdef IPV6_RECVPKTINFO
242 optname
= IPV6_RECVPKTINFO
;
244 optname
= IPV6_PKTINFO
;
247 if (setsockopt(s
, IPPROTO_IPV6
, optname
, (const char *) &on
,
249 ipv6pktinfo_result
= ISC_R_NOTFOUND
;
253 ipv6pktinfo_result
= ISC_R_SUCCESS
;
261 initialize_ipv6pktinfo(void) {
262 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo
,
263 try_ipv6pktinfo
) == ISC_R_SUCCESS
);
265 #endif /* WANT_IPV6 */
266 #endif /* ISC_PLATFORM_HAVEIPV6 */
269 isc_net_probe_ipv6only(void) {
270 #ifdef ISC_PLATFORM_HAVEIPV6
272 initialize_ipv6only();
274 ipv6only_result
= ISC_R_NOTFOUND
;
277 return (ipv6only_result
);
281 isc_net_probe_ipv6pktinfo(void) {
282 #ifdef ISC_PLATFORM_HAVEIPV6
284 initialize_ipv6pktinfo();
286 ipv6pktinfo_result
= ISC_R_NOTFOUND
;
289 return (ipv6pktinfo_result
);
293 isc_net_getudpportrange(int af
, in_port_t
*low
, in_port_t
*high
) {
294 int result
= ISC_R_FAILURE
;
296 REQUIRE(low
!= NULL
&& high
!= NULL
);
300 if (result
!= ISC_R_SUCCESS
) {
301 *low
= ISC_NET_PORTRANGELOW
;
302 *high
= ISC_NET_PORTRANGEHIGH
;
305 return (ISC_R_SUCCESS
); /* we currently never fail in this function */
309 isc_net_disableipv4(void) {
311 if (ipv4_result
== ISC_R_SUCCESS
)
312 ipv4_result
= ISC_R_DISABLED
;
316 isc_net_disableipv6(void) {
318 if (ipv6_result
== ISC_R_SUCCESS
)
319 ipv6_result
= ISC_R_DISABLED
;
323 isc_net_enableipv4(void) {
325 if (ipv4_result
== ISC_R_DISABLED
)
326 ipv4_result
= ISC_R_SUCCESS
;
330 isc_net_enableipv6(void) {
332 if (ipv6_result
== ISC_R_DISABLED
)
333 ipv6_result
= ISC_R_SUCCESS
;