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