No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / win32 / net.c
blobdaeb7f24a82657b0980be318ccba713851e06590
1 /* $NetBSD$ */
3 /*
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 */
22 #include <config.h>
24 #include <errno.h>
25 #include <unistd.h>
27 #include <isc/log.h>
28 #include <isc/msgs.h>
29 #include <isc/net.h>
30 #include <isc/once.h>
31 #include <isc/strerror.h>
32 #include <isc/string.h>
33 #include <isc/util.h>
35 /*%
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...
42 /*%
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;
54 #endif
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);
66 static isc_result_t
67 try_proto(int domain) {
68 SOCKET s;
69 isc_result_t result = ISC_R_SUCCESS;
70 char strbuf[ISC_STRERRORSIZE];
71 int errval;
73 s = socket(domain, SOCK_STREAM, IPPROTO_TCP);
74 if (s == INVALID_SOCKET) {
75 errval = WSAGetLastError();
76 switch (errval) {
77 case WSAEAFNOSUPPORT:
78 case WSAEPROTONOSUPPORT:
79 case WSAEINVAL:
80 return (ISC_R_NOTFOUND);
81 default:
82 isc__strerror(errval, strbuf, sizeof(strbuf));
83 UNEXPECTED_ERROR(__FILE__, __LINE__,
84 "socket() %s: %s",
85 isc_msgcat_get(isc_msgcat,
86 ISC_MSGSET_GENERAL,
87 ISC_MSG_FAILED,
88 "failed"),
89 strbuf);
90 return (ISC_R_UNEXPECTED);
94 closesocket(s);
96 return (ISC_R_SUCCESS);
99 static void
100 initialize_action(void) {
101 InitSockets();
102 ipv4_result = try_proto(PF_INET);
103 #ifdef ISC_PLATFORM_HAVEIPV6
104 #ifdef WANT_IPV6
105 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
106 ipv6_result = try_proto(PF_INET6);
107 #endif
108 #endif
109 #endif
112 static void
113 initialize(void) {
114 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
117 isc_result_t
118 isc_net_probeipv4(void) {
119 initialize();
120 return (ipv4_result);
123 isc_result_t
124 isc_net_probeipv6(void) {
125 initialize();
126 return (ipv6_result);
129 isc_result_t
130 isc_net_probeunix(void) {
131 return (ISC_R_NOTFOUND);
134 #ifdef ISC_PLATFORM_HAVEIPV6
135 #ifdef WANT_IPV6
136 static void
137 try_ipv6only(void) {
138 #ifdef IPV6_V6ONLY
139 SOCKET s;
140 int on;
141 char strbuf[ISC_STRERRORSIZE];
142 #endif
143 isc_result_t result;
145 result = isc_net_probeipv6();
146 if (result != ISC_R_SUCCESS) {
147 ipv6only_result = result;
148 return;
151 #ifndef IPV6_V6ONLY
152 ipv6only_result = ISC_R_NOTFOUND;
153 return;
154 #else
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__,
160 "socket() %s: %s",
161 isc_msgcat_get(isc_msgcat,
162 ISC_MSGSET_GENERAL,
163 ISC_MSG_FAILED,
164 "failed"),
165 strbuf);
166 ipv6only_result = ISC_R_UNEXPECTED;
167 return;
170 on = 1;
171 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
172 ipv6only_result = ISC_R_NOTFOUND;
173 goto close;
176 closesocket(s);
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__,
183 "socket() %s: %s",
184 isc_msgcat_get(isc_msgcat,
185 ISC_MSGSET_GENERAL,
186 ISC_MSG_FAILED,
187 "failed"),
188 strbuf);
189 ipv6only_result = ISC_R_UNEXPECTED;
190 return;
193 on = 1;
194 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
195 ipv6only_result = ISC_R_NOTFOUND;
196 goto close;
199 ipv6only_result = ISC_R_SUCCESS;
201 close:
202 closesocket(s);
203 return;
204 #endif /* IPV6_V6ONLY */
207 static void
208 initialize_ipv6only(void) {
209 RUNTIME_CHECK(isc_once_do(&once_ipv6only,
210 try_ipv6only) == ISC_R_SUCCESS);
213 static void
214 try_ipv6pktinfo(void) {
215 int s, on;
216 char strbuf[ISC_STRERRORSIZE];
217 isc_result_t result;
218 int optname;
220 result = isc_net_probeipv6();
221 if (result != ISC_R_SUCCESS) {
222 ipv6pktinfo_result = result;
223 return;
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__,
231 "socket() %s: %s",
232 isc_msgcat_get(isc_msgcat,
233 ISC_MSGSET_GENERAL,
234 ISC_MSG_FAILED,
235 "failed"),
236 strbuf);
237 ipv6pktinfo_result = ISC_R_UNEXPECTED;
238 return;
241 #ifdef IPV6_RECVPKTINFO
242 optname = IPV6_RECVPKTINFO;
243 #else
244 optname = IPV6_PKTINFO;
245 #endif
246 on = 1;
247 if (setsockopt(s, IPPROTO_IPV6, optname, (const char *) &on,
248 sizeof(on)) < 0) {
249 ipv6pktinfo_result = ISC_R_NOTFOUND;
250 goto close;
253 ipv6pktinfo_result = ISC_R_SUCCESS;
255 close:
256 closesocket(s);
257 return;
260 static void
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 */
268 isc_result_t
269 isc_net_probe_ipv6only(void) {
270 #ifdef ISC_PLATFORM_HAVEIPV6
271 #ifdef WANT_IPV6
272 initialize_ipv6only();
273 #else
274 ipv6only_result = ISC_R_NOTFOUND;
275 #endif
276 #endif
277 return (ipv6only_result);
280 isc_result_t
281 isc_net_probe_ipv6pktinfo(void) {
282 #ifdef ISC_PLATFORM_HAVEIPV6
283 #ifdef WANT_IPV6
284 initialize_ipv6pktinfo();
285 #else
286 ipv6pktinfo_result = ISC_R_NOTFOUND;
287 #endif
288 #endif
289 return (ipv6pktinfo_result);
292 isc_result_t
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);
298 UNUSED(af);
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 */
308 void
309 isc_net_disableipv4(void) {
310 initialize();
311 if (ipv4_result == ISC_R_SUCCESS)
312 ipv4_result = ISC_R_DISABLED;
315 void
316 isc_net_disableipv6(void) {
317 initialize();
318 if (ipv6_result == ISC_R_SUCCESS)
319 ipv6_result = ISC_R_DISABLED;
322 void
323 isc_net_enableipv4(void) {
324 initialize();
325 if (ipv4_result == ISC_R_DISABLED)
326 ipv4_result = ISC_R_SUCCESS;
329 void
330 isc_net_enableipv6(void) {
331 initialize();
332 if (ipv6_result == ISC_R_DISABLED)
333 ipv6_result = ISC_R_SUCCESS;