1/*	$NetBSD$	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007, 2008, 2012  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
116struct 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
131typedef 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 (/*CONSTCOND*/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 (/*CONSTCOND*/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#define EWOULDBLOCK             WSAEWOULDBLOCK
195#define EINPROGRESS             WSAEINPROGRESS
196#define EALREADY                WSAEALREADY
197#define ENOTSOCK                WSAENOTSOCK
198#define EDESTADDRREQ            WSAEDESTADDRREQ
199#define EMSGSIZE                WSAEMSGSIZE
200#define EPROTOTYPE              WSAEPROTOTYPE
201#define ENOPROTOOPT             WSAENOPROTOOPT
202#define EPROTONOSUPPORT         WSAEPROTONOSUPPORT
203#define ESOCKTNOSUPPORT         WSAESOCKTNOSUPPORT
204#define EOPNOTSUPP              WSAEOPNOTSUPP
205#define EPFNOSUPPORT            WSAEPFNOSUPPORT
206#define EAFNOSUPPORT            WSAEAFNOSUPPORT
207#define EADDRINUSE              WSAEADDRINUSE
208#define EADDRNOTAVAIL           WSAEADDRNOTAVAIL
209#define ENETDOWN                WSAENETDOWN
210#define ENETUNREACH             WSAENETUNREACH
211#define ENETRESET               WSAENETRESET
212#define ECONNABORTED            WSAECONNABORTED
213#define ECONNRESET              WSAECONNRESET
214#define ENOBUFS                 WSAENOBUFS
215#define EISCONN                 WSAEISCONN
216#define ENOTCONN                WSAENOTCONN
217#define ESHUTDOWN               WSAESHUTDOWN
218#define ETOOMANYREFS            WSAETOOMANYREFS
219#define ETIMEDOUT               WSAETIMEDOUT
220#define ECONNREFUSED            WSAECONNREFUSED
221#define ELOOP                   WSAELOOP
222#define EHOSTDOWN               WSAEHOSTDOWN
223#define EHOSTUNREACH            WSAEHOSTUNREACH
224#define EPROCLIM                WSAEPROCLIM
225#define EUSERS                  WSAEUSERS
226#define EDQUOT                  WSAEDQUOT
227#define ESTALE                  WSAESTALE
228#define EREMOTE                 WSAEREMOTE
229
230
231/***
232 *** Functions.
233 ***/
234
235ISC_LANG_BEGINDECLS
236
237isc_result_t
238isc_net_probeipv4(void);
239/*
240 * Check if the system's kernel supports IPv4.
241 *
242 * Returns:
243 *
244 *	ISC_R_SUCCESS		IPv4 is supported.
245 *	ISC_R_NOTFOUND		IPv4 is not supported.
246 *	ISC_R_DISABLED		IPv4 is disabled.
247 *	ISC_R_UNEXPECTED
248 */
249
250isc_result_t
251isc_net_probeipv6(void);
252/*
253 * Check if the system's kernel supports IPv6.
254 *
255 * Returns:
256 *
257 *	ISC_R_SUCCESS		IPv6 is supported.
258 *	ISC_R_NOTFOUND		IPv6 is not supported.
259 *	ISC_R_DISABLED		IPv6 is disabled.
260 *	ISC_R_UNEXPECTED
261 */
262
263isc_result_t
264isc_net_probeunix(void);
265/*
266 * Check if UNIX domain sockets are supported.
267 *
268 * Returns:
269 *
270 *	ISC_R_SUCCESS
271 *	ISC_R_NOTFOUND
272 */
273
274isc_result_t
275isc_net_probe_ipv6only(void);
276/*
277 * Check if the system's kernel supports the IPV6_V6ONLY socket option.
278 *
279 * Returns:
280 *
281 *	ISC_R_SUCCESS		the option is supported for both TCP and UDP.
282 *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
283 *	ISC_R_UNEXPECTED
284 */
285
286isc_result_t
287isc_net_probe_ipv6pktinfo(void);
288/*
289 * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option
290 * for UDP sockets.
291 *
292 * Returns:
293 *
294 *	ISC_R_SUCCESS		the option is supported.
295 *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
296 *	ISC_R_UNEXPECTED
297 */
298
299void
300isc_net_disableipv4(void);
301
302void
303isc_net_disableipv6(void);
304
305void
306isc_net_enableipv4(void);
307
308void
309isc_net_enableipv6(void);
310
311isc_result_t
312isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high);
313/*%<
314 * Returns system's default range of ephemeral UDP ports, if defined.
315 * If the range is not available or unknown, ISC_NET_PORTRANGELOW and
316 * ISC_NET_PORTRANGEHIGH will be returned.
317 *
318 * Requires:
319 *
320 *\li	'low' and 'high' must be non NULL.
321 *
322 * Returns:
323 *
324 *\li	*low and *high will be the ports specifying the low and high ends of
325 *	the range.
326 */
327
328#ifdef ISC_PLATFORM_NEEDNTOP
329const char *
330isc_net_ntop(int af, const void *src, char *dst, size_t size);
331#define inet_ntop isc_net_ntop
332#endif
333
334#ifdef ISC_PLATFORM_NEEDPTON
335int
336isc_net_pton(int af, const char *src, void *dst);
337#define inet_pton isc_net_pton
338#endif
339
340int
341isc_net_aton(const char *cp, struct in_addr *addr);
342#define inet_aton isc_net_aton
343
344ISC_LANG_ENDDECLS
345
346#endif /* ISC_NET_H */
347