1 /* $NetBSD: net.c,v 1.8 2014/12/10 04:38:01 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007-2009, 2011-2014 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.
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 char strbuf
[ISC_STRERRORSIZE
];
72 s
= socket(domain
, SOCK_STREAM
, IPPROTO_TCP
);
73 if (s
== INVALID_SOCKET
) {
74 errval
= WSAGetLastError();
77 case WSAEPROTONOSUPPORT
:
79 return (ISC_R_NOTFOUND
);
81 isc__strerror(errval
, strbuf
, sizeof(strbuf
));
82 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
84 isc_msgcat_get(isc_msgcat
,
89 return (ISC_R_UNEXPECTED
);
95 return (ISC_R_SUCCESS
);
99 initialize_action(void) {
101 ipv4_result
= try_proto(PF_INET
);
102 #ifdef ISC_PLATFORM_HAVEIPV6
104 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
105 ipv6_result
= try_proto(PF_INET6
);
113 RUNTIME_CHECK(isc_once_do(&once
, initialize_action
) == ISC_R_SUCCESS
);
117 isc_net_probeipv4(void) {
119 return (ipv4_result
);
123 isc_net_probeipv6(void) {
125 return (ipv6_result
);
129 isc_net_probeunix(void) {
130 return (ISC_R_NOTFOUND
);
133 #ifdef ISC_PLATFORM_HAVEIPV6
140 char strbuf
[ISC_STRERRORSIZE
];
144 result
= isc_net_probeipv6();
145 if (result
!= ISC_R_SUCCESS
) {
146 ipv6only_result
= result
;
151 ipv6only_result
= ISC_R_NOTFOUND
;
154 /* check for TCP sockets */
155 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
156 if (s
== INVALID_SOCKET
) {
157 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
158 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
160 isc_msgcat_get(isc_msgcat
,
165 ipv6only_result
= ISC_R_UNEXPECTED
;
170 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, (const char *)&on
,
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
, (const char *)&on
,
196 ipv6only_result
= ISC_R_NOTFOUND
;
200 ipv6only_result
= ISC_R_SUCCESS
;
205 #endif /* IPV6_V6ONLY */
209 initialize_ipv6only(void) {
210 RUNTIME_CHECK(isc_once_do(&once_ipv6only
,
211 try_ipv6only
) == ISC_R_SUCCESS
);
215 try_ipv6pktinfo(void) {
218 char strbuf
[ISC_STRERRORSIZE
];
222 result
= isc_net_probeipv6();
223 if (result
!= ISC_R_SUCCESS
) {
224 ipv6pktinfo_result
= result
;
228 /* we only use this for UDP sockets */
229 s
= socket(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
230 if (s
== INVALID_SOCKET
) {
231 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
232 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
234 isc_msgcat_get(isc_msgcat
,
239 ipv6pktinfo_result
= ISC_R_UNEXPECTED
;
243 #ifdef IPV6_RECVPKTINFO
244 optname
= IPV6_RECVPKTINFO
;
246 optname
= IPV6_PKTINFO
;
249 if (setsockopt(s
, IPPROTO_IPV6
, optname
, (const char *) &on
,
251 ipv6pktinfo_result
= ISC_R_NOTFOUND
;
255 ipv6pktinfo_result
= ISC_R_SUCCESS
;
263 initialize_ipv6pktinfo(void) {
264 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo
,
265 try_ipv6pktinfo
) == ISC_R_SUCCESS
);
267 #endif /* WANT_IPV6 */
268 #endif /* ISC_PLATFORM_HAVEIPV6 */
271 isc_net_probe_ipv6only(void) {
272 #ifdef ISC_PLATFORM_HAVEIPV6
274 initialize_ipv6only();
276 ipv6only_result
= ISC_R_NOTFOUND
;
279 return (ipv6only_result
);
283 isc_net_probe_ipv6pktinfo(void) {
284 #ifdef ISC_PLATFORM_HAVEIPV6
286 initialize_ipv6pktinfo();
288 ipv6pktinfo_result
= ISC_R_NOTFOUND
;
291 return (ipv6pktinfo_result
);
295 isc_net_getudpportrange(int af
, in_port_t
*low
, in_port_t
*high
) {
296 int result
= ISC_R_FAILURE
;
298 REQUIRE(low
!= NULL
&& high
!= NULL
);
302 if (result
!= ISC_R_SUCCESS
) {
303 *low
= ISC_NET_PORTRANGELOW
;
304 *high
= ISC_NET_PORTRANGEHIGH
;
307 return (ISC_R_SUCCESS
); /* we currently never fail in this function */
311 isc_net_disableipv4(void) {
313 if (ipv4_result
== ISC_R_SUCCESS
)
314 ipv4_result
= ISC_R_DISABLED
;
318 isc_net_disableipv6(void) {
320 if (ipv6_result
== ISC_R_SUCCESS
)
321 ipv6_result
= ISC_R_DISABLED
;
325 isc_net_enableipv4(void) {
327 if (ipv4_result
== ISC_R_DISABLED
)
328 ipv4_result
= ISC_R_SUCCESS
;
332 isc_net_enableipv6(void) {
334 if (ipv6_result
== ISC_R_DISABLED
)
335 ipv6_result
= ISC_R_SUCCESS
;
339 isc_net_probedscp(void) {