1 /*	$NetBSD: net.h,v 1.9 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2008, 2012, 2013  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2003  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id */
21 
22 #ifndef ISC_NET_H
23 #define ISC_NET_H 1
24 
25 /*
26  * Also define LWRES_IPV6_H to keep it from being included if liblwres is
27  * being used, or redefinition errors will occur.
28  */
29 #define LWRES_IPV6_H 1
30 
31 
32 
33 /*****
34  ***** Module Info
35  *****/
36 
37 /*
38  * Basic Networking Types
39  *
40  * This module is responsible for defining the following basic networking
41  * types:
42  *
43  *		struct in_addr
44  *		struct in6_addr
45  *		struct in6_pktinfo
46  *		struct sockaddr
47  *		struct sockaddr_in
48  *		struct sockaddr_in6
49  *		in_port_t
50  *
51  * It ensures that the AF_ and PF_ macros are defined.
52  *
53  * It declares ntoh[sl]() and hton[sl]().
54  *
55  * It declares inet_aton(), inet_ntop(), and inet_pton().
56  *
57  * It ensures that INADDR_ANY, IN6ADDR_ANY_INIT, in6addr_any, and
58  * in6addr_loopback are available.
59  *
60  * It ensures that IN_MULTICAST() is available to check for multicast
61  * addresses.
62  *
63  * MP:
64  *	No impact.
65  *
66  * Reliability:
67  *	No anticipated impact.
68  *
69  * Resources:
70  *	N/A.
71  *
72  * Security:
73  *	No anticipated impact.
74  *
75  * Standards:
76  *	BSD Socket API
77  *	RFC2553
78  */
79 
80 /***
81  *** Imports.
82  ***/
83 #include <isc/platform.h>
84 
85 /*
86  * Because of some sort of problem in the MS header files, this cannot
87  * be simple "#include <winsock2.h>", because winsock2.h tries to include
88  * windows.h, which then generates an error out of mswsock.h.  _You_
89  * figure it out.
90  */
91 #ifndef _WINSOCKAPI_
92 #define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */
93 #endif
94 
95 #include <winsock2.h>
96 
97 #include <sys/types.h>
98 
99 #include <isc/lang.h>
100 #include <isc/types.h>
101 
102 #include <ws2tcpip.h>
103 #include <isc/ipv6.h>
104 
105 /*
106  * This is here because named client, interfacemgr.c, etc. use the name as
107  * a variable
108  */
109 #undef interface
110 
111 #ifndef INADDR_LOOPBACK
112 #define INADDR_LOOPBACK 0x7f000001UL
113 #endif
114 
115 #ifndef ISC_PLATFORM_HAVEIN6PKTINFO
116 struct in6_pktinfo {
117 	struct in6_addr ipi6_addr;    /* src/dst IPv6 address */
118 	unsigned int    ipi6_ifindex; /* send/recv interface index */
119 };
120 #endif
121 
122 #if _MSC_VER < 1300
123 #define in6addr_any isc_in6addr_any
124 #define in6addr_loopback isc_in6addr_loopback
125 #endif
126 
127 /*
128  * Ensure type in_port_t is defined.
129  */
130 #ifdef ISC_PLATFORM_NEEDPORTT
131 typedef isc_uint16_t in_port_t;
132 #endif
133 
134 /*
135  * If this system does not have MSG_TRUNC (as returned from recvmsg())
136  * ISC_PLATFORM_RECVOVERFLOW will be defined.  This will enable the MSG_TRUNC
137  * faking code in socket.c.
138  */
139 #ifndef MSG_TRUNC
140 #define ISC_PLATFORM_RECVOVERFLOW
141 #endif
142 
143 #define ISC__IPADDR(x)	((isc_uint32_t)htonl((isc_uint32_t)(x)))
144 
145 #define ISC_IPADDR_ISMULTICAST(i) \
146 		(((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
147 		 == ISC__IPADDR(0xe0000000))
148 
149 #define ISC_IPADDR_ISEXPERIMENTAL(i) \
150 		(((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
151 		 == ISC__IPADDR(0xf0000000))
152 
153 /*
154  * Fix the FD_SET and FD_CLR Macros to properly cast
155  */
156 #undef FD_CLR
157 #define FD_CLR(fd, set) do { \
158     u_int __i; \
159     for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
160 	if (((fd_set FAR *)(set))->fd_array[__i] == (SOCKET) fd) { \
161 	    while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
162 		((fd_set FAR *)(set))->fd_array[__i] = \
163 		    ((fd_set FAR *)(set))->fd_array[__i+1]; \
164 		__i++; \
165 	    } \
166 	    ((fd_set FAR *)(set))->fd_count--; \
167 	    break; \
168 	} \
169     } \
170 } while (0)
171 
172 #undef FD_SET
173 #define FD_SET(fd, set) do { \
174     u_int __i; \
175     for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
176 	if (((fd_set FAR *)(set))->fd_array[__i] == (SOCKET)(fd)) { \
177 	    break; \
178 	} \
179     } \
180     if (__i == ((fd_set FAR *)(set))->fd_count) { \
181 	if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
182 	    ((fd_set FAR *)(set))->fd_array[__i] = (SOCKET)(fd); \
183 	    ((fd_set FAR *)(set))->fd_count++; \
184 	} \
185     } \
186 } while (0)
187 
188 /*
189  * Windows Sockets errors redefined as regular Berkeley error constants.
190  * These are usually commented out in Windows NT to avoid conflicts with errno.h.
191  * Use the WSA constants instead.
192  */
193 
194 #include <errno.h>
195 
196 #ifndef EWOULDBLOCK
197 #define EWOULDBLOCK             WSAEWOULDBLOCK
198 #endif
199 #ifndef EINPROGRESS
200 #define EINPROGRESS             WSAEINPROGRESS
201 #endif
202 #ifndef EALREADY
203 #define EALREADY                WSAEALREADY
204 #endif
205 #ifndef ENOTSOCK
206 #define ENOTSOCK                WSAENOTSOCK
207 #endif
208 #ifndef EDESTADDRREQ
209 #define EDESTADDRREQ            WSAEDESTADDRREQ
210 #endif
211 #ifndef EMSGSIZE
212 #define EMSGSIZE                WSAEMSGSIZE
213 #endif
214 #ifndef EPROTOTYPE
215 #define EPROTOTYPE              WSAEPROTOTYPE
216 #endif
217 #ifndef ENOPROTOOPT
218 #define ENOPROTOOPT             WSAENOPROTOOPT
219 #endif
220 #ifndef EPROTONOSUPPORT
221 #define EPROTONOSUPPORT         WSAEPROTONOSUPPORT
222 #endif
223 #ifndef ESOCKTNOSUPPORT
224 #define ESOCKTNOSUPPORT         WSAESOCKTNOSUPPORT
225 #endif
226 #ifndef EOPNOTSUPP
227 #define EOPNOTSUPP              WSAEOPNOTSUPP
228 #endif
229 #ifndef EPFNOSUPPORT
230 #define EPFNOSUPPORT            WSAEPFNOSUPPORT
231 #endif
232 #ifndef EAFNOSUPPORT
233 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
234 #endif
235 #ifndef EADDRINUSE
236 #define EADDRINUSE              WSAEADDRINUSE
237 #endif
238 #ifndef EADDRNOTAVAIL
239 #define EADDRNOTAVAIL           WSAEADDRNOTAVAIL
240 #endif
241 #ifndef ENETDOWN
242 #define ENETDOWN                WSAENETDOWN
243 #endif
244 #ifndef ENETUNREACH
245 #define ENETUNREACH             WSAENETUNREACH
246 #endif
247 #ifndef ENETRESET
248 #define ENETRESET               WSAENETRESET
249 #endif
250 #ifndef ECONNABORTED
251 #define ECONNABORTED            WSAECONNABORTED
252 #endif
253 #ifndef ECONNRESET
254 #define ECONNRESET              WSAECONNRESET
255 #endif
256 #ifndef ENOBUFS
257 #define ENOBUFS                 WSAENOBUFS
258 #endif
259 #ifndef EISCONN
260 #define EISCONN                 WSAEISCONN
261 #endif
262 #ifndef ENOTCONN
263 #define ENOTCONN                WSAENOTCONN
264 #endif
265 #ifndef ESHUTDOWN
266 #define ESHUTDOWN               WSAESHUTDOWN
267 #endif
268 #ifndef ETOOMANYREFS
269 #define ETOOMANYREFS            WSAETOOMANYREFS
270 #endif
271 #ifndef ETIMEDOUT
272 #define ETIMEDOUT               WSAETIMEDOUT
273 #endif
274 #ifndef ECONNREFUSED
275 #define ECONNREFUSED            WSAECONNREFUSED
276 #endif
277 #ifndef ELOOP
278 #define ELOOP                   WSAELOOP
279 #endif
280 #ifndef EHOSTDOWN
281 #define EHOSTDOWN               WSAEHOSTDOWN
282 #endif
283 #ifndef EHOSTUNREACH
284 #define EHOSTUNREACH            WSAEHOSTUNREACH
285 #endif
286 #ifndef EPROCLIM
287 #define EPROCLIM                WSAEPROCLIM
288 #endif
289 #ifndef EUSERS
290 #define EUSERS                  WSAEUSERS
291 #endif
292 #ifndef EDQUOT
293 #define EDQUOT                  WSAEDQUOT
294 #endif
295 #ifndef ESTALE
296 #define ESTALE                  WSAESTALE
297 #endif
298 #ifndef EREMOTE
299 #define EREMOTE                 WSAEREMOTE
300 #endif
301 
302 
303 /***
304  *** Functions.
305  ***/
306 
307 ISC_LANG_BEGINDECLS
308 
309 isc_result_t
310 isc_net_probeipv4(void);
311 /*
312  * Check if the system's kernel supports IPv4.
313  *
314  * Returns:
315  *
316  *	ISC_R_SUCCESS		IPv4 is supported.
317  *	ISC_R_NOTFOUND		IPv4 is not supported.
318  *	ISC_R_DISABLED		IPv4 is disabled.
319  *	ISC_R_UNEXPECTED
320  */
321 
322 isc_result_t
323 isc_net_probeipv6(void);
324 /*
325  * Check if the system's kernel supports IPv6.
326  *
327  * Returns:
328  *
329  *	ISC_R_SUCCESS		IPv6 is supported.
330  *	ISC_R_NOTFOUND		IPv6 is not supported.
331  *	ISC_R_DISABLED		IPv6 is disabled.
332  *	ISC_R_UNEXPECTED
333  */
334 
335 isc_result_t
336 isc_net_probeunix(void);
337 /*
338  * Check if UNIX domain sockets are supported.
339  *
340  * Returns:
341  *
342  *	ISC_R_SUCCESS
343  *	ISC_R_NOTFOUND
344  */
345 
346 #define ISC_NET_DSCPRECVV4      0x01    /* Can receive sent DSCP value IPv4 */
347 #define ISC_NET_DSCPRECVV6      0x02    /* Can receive sent DSCP value IPv6 */
348 #define ISC_NET_DSCPSETV4       0x04    /* Can set DSCP on socket IPv4 */
349 #define ISC_NET_DSCPSETV6       0x08    /* Can set DSCP on socket IPv6 */
350 #define ISC_NET_DSCPPKTV4       0x10    /* Can set DSCP on per packet IPv4 */
351 #define ISC_NET_DSCPPKTV6       0x20    /* Can set DSCP on per packet IPv6 */
352 #define ISC_NET_DSCPALL         0x3f    /* All valid flags */
353 
354 unsigned int
355 isc_net_probedscp(void);
356 /*%<
357  * Probe the level of DSCP support.
358  */
359 
360 isc_result_t
361 isc_net_probe_ipv6only(void);
362 /*
363  * Check if the system's kernel supports the IPV6_V6ONLY socket option.
364  *
365  * Returns:
366  *
367  *	ISC_R_SUCCESS		the option is supported for both TCP and UDP.
368  *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
369  *	ISC_R_UNEXPECTED
370  */
371 
372 isc_result_t
373 isc_net_probe_ipv6pktinfo(void);
374 /*
375  * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option
376  * for UDP sockets.
377  *
378  * Returns:
379  *
380  *	ISC_R_SUCCESS		the option is supported.
381  *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
382  *	ISC_R_UNEXPECTED
383  */
384 
385 void
386 isc_net_disableipv4(void);
387 
388 void
389 isc_net_disableipv6(void);
390 
391 void
392 isc_net_enableipv4(void);
393 
394 void
395 isc_net_enableipv6(void);
396 
397 isc_result_t
398 isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high);
399 /*%<
400  * Returns system's default range of ephemeral UDP ports, if defined.
401  * If the range is not available or unknown, ISC_NET_PORTRANGELOW and
402  * ISC_NET_PORTRANGEHIGH will be returned.
403  *
404  * Requires:
405  *
406  *\li	'low' and 'high' must be non NULL.
407  *
408  * Returns:
409  *
410  *\li	*low and *high will be the ports specifying the low and high ends of
411  *	the range.
412  */
413 
414 #ifdef ISC_PLATFORM_NEEDNTOP
415 const char *
416 isc_net_ntop(int af, const void *src, char *dst, size_t size);
417 #define inet_ntop isc_net_ntop
418 #endif
419 
420 #ifdef ISC_PLATFORM_NEEDPTON
421 int
422 isc_net_pton(int af, const char *src, void *dst);
423 #define inet_pton isc_net_pton
424 #endif
425 
426 int
427 isc_net_aton(const char *cp, struct in_addr *addr);
428 #define inet_aton isc_net_aton
429 
430 ISC_LANG_ENDDECLS
431 
432 #endif /* ISC_NET_H */
433