1/*
2 * Copyright (C) 2004, 2005, 2007, 2008, 2012  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id$ */
19
20#ifndef ISC_NET_H
21#define ISC_NET_H 1
22
23#include <errno.h>
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_net_in6addrany
124#define in6addr_loopback isc_net_in6addrloop
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 (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 * Starting with VC9 (VC++ 2010) many of these are defined incompatibly
193 * by errno.h -- #undef the conflicting values, which are not relevant to
194 * Win32.
195 */
196
197#undef EWOULDBLOCK
198#define EWOULDBLOCK             WSAEWOULDBLOCK
199#undef EINPROGRESS
200#define EINPROGRESS             WSAEINPROGRESS
201#undef EALREADY
202#define EALREADY                WSAEALREADY
203#undef ENOTSOCK
204#define ENOTSOCK                WSAENOTSOCK
205#undef EDESTADDRREQ
206#define EDESTADDRREQ            WSAEDESTADDRREQ
207#undef EMSGSIZE
208#define EMSGSIZE                WSAEMSGSIZE
209#undef EPROTOTYPE
210#define EPROTOTYPE              WSAEPROTOTYPE
211#undef ENOPROTOOPT
212#define ENOPROTOOPT             WSAENOPROTOOPT
213#undef EPROTONOSUPPORT
214#define EPROTONOSUPPORT         WSAEPROTONOSUPPORT
215#define ESOCKTNOSUPPORT         WSAESOCKTNOSUPPORT
216#undef EOPNOTSUPP
217#define EOPNOTSUPP              WSAEOPNOTSUPP
218#define EPFNOSUPPORT            WSAEPFNOSUPPORT
219#undef EAFNOSUPPORT
220#define EAFNOSUPPORT            WSAEAFNOSUPPORT
221#undef EADDRINUSE
222#define EADDRINUSE              WSAEADDRINUSE
223#undef EADDRNOTAVAIL
224#define EADDRNOTAVAIL           WSAEADDRNOTAVAIL
225#undef ENETDOWN
226#define ENETDOWN                WSAENETDOWN
227#undef ENETUNREACH
228#define ENETUNREACH             WSAENETUNREACH
229#undef ENETRESET
230#define ENETRESET               WSAENETRESET
231#undef ECONNABORTED
232#define ECONNABORTED            WSAECONNABORTED
233#undef ECONNRESET
234#define ECONNRESET              WSAECONNRESET
235#undef ENOBUFS
236#define ENOBUFS                 WSAENOBUFS
237#undef EISCONN
238#define EISCONN                 WSAEISCONN
239#undef ENOTCONN
240#define ENOTCONN                WSAENOTCONN
241#define ESHUTDOWN               WSAESHUTDOWN
242#define ETOOMANYREFS            WSAETOOMANYREFS
243#undef ETIMEDOUT
244#define ETIMEDOUT               WSAETIMEDOUT
245#undef ECONNREFUSED
246#define ECONNREFUSED            WSAECONNREFUSED
247#undef ELOOP
248#define ELOOP                   WSAELOOP
249#define EHOSTDOWN               WSAEHOSTDOWN
250#undef EHOSTUNREACH
251#define EHOSTUNREACH            WSAEHOSTUNREACH
252#define EPROCLIM                WSAEPROCLIM
253#define EUSERS                  WSAEUSERS
254#define EDQUOT                  WSAEDQUOT
255#define ESTALE                  WSAESTALE
256#define EREMOTE                 WSAEREMOTE
257
258
259/***
260 *** Functions.
261 ***/
262
263ISC_LANG_BEGINDECLS
264
265isc_result_t
266isc_net_probeipv4(void);
267/*
268 * Check if the system's kernel supports IPv4.
269 *
270 * Returns:
271 *
272 *	ISC_R_SUCCESS		IPv4 is supported.
273 *	ISC_R_NOTFOUND		IPv4 is not supported.
274 *	ISC_R_DISABLED		IPv4 is disabled.
275 *	ISC_R_UNEXPECTED
276 */
277
278isc_result_t
279isc_net_probeipv6(void);
280/*
281 * Check if the system's kernel supports IPv6.
282 *
283 * Returns:
284 *
285 *	ISC_R_SUCCESS		IPv6 is supported.
286 *	ISC_R_NOTFOUND		IPv6 is not supported.
287 *	ISC_R_DISABLED		IPv6 is disabled.
288 *	ISC_R_UNEXPECTED
289 */
290
291isc_result_t
292isc_net_probeunix(void);
293/*
294 * Check if UNIX domain sockets are supported.
295 *
296 * Returns:
297 *
298 *	ISC_R_SUCCESS
299 *	ISC_R_NOTFOUND
300 */
301
302isc_result_t
303isc_net_probe_ipv6only(void);
304/*
305 * Check if the system's kernel supports the IPV6_V6ONLY socket option.
306 *
307 * Returns:
308 *
309 *	ISC_R_SUCCESS		the option is supported for both TCP and UDP.
310 *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
311 *	ISC_R_UNEXPECTED
312 */
313
314isc_result_t
315isc_net_probe_ipv6pktinfo(void);
316/*
317 * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option
318 * for UDP sockets.
319 *
320 * Returns:
321 *
322 *	ISC_R_SUCCESS		the option is supported.
323 *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
324 *	ISC_R_UNEXPECTED
325 */
326
327void
328isc_net_disableipv4(void);
329
330void
331isc_net_disableipv6(void);
332
333void
334isc_net_enableipv4(void);
335
336void
337isc_net_enableipv6(void);
338
339isc_result_t
340isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high);
341/*%<
342 * Returns system's default range of ephemeral UDP ports, if defined.
343 * If the range is not available or unknown, ISC_NET_PORTRANGELOW and
344 * ISC_NET_PORTRANGEHIGH will be returned.
345 *
346 * Requires:
347 *
348 *\li	'low' and 'high' must be non NULL.
349 *
350 * Returns:
351 *
352 *\li	*low and *high will be the ports specifying the low and high ends of
353 *	the range.
354 */
355
356#ifdef ISC_PLATFORM_NEEDNTOP
357const char *
358isc_net_ntop(int af, const void *src, char *dst, size_t size);
359#define inet_ntop isc_net_ntop
360#endif
361
362#ifdef ISC_PLATFORM_NEEDPTON
363int
364isc_net_pton(int af, const char *src, void *dst);
365#define inet_pton isc_net_pton
366#endif
367
368int
369isc_net_aton(const char *cp, struct in_addr *addr);
370#define inet_aton isc_net_aton
371
372ISC_LANG_ENDDECLS
373
374#endif /* ISC_NET_H */
375