1/*
2 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and 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: net.h,v 1.31.2.2.10.8 2004/04/29 01:31:23 marka Exp $ */
19
20#ifndef ISC_NET_H
21#define ISC_NET_H 1
22
23/*****
24 ***** Module Info
25 *****/
26
27/*
28 * Basic Networking Types
29 *
30 * This module is responsible for defining the following basic networking
31 * types:
32 *
33 *		struct in_addr
34 *		struct in6_addr
35 *		struct in6_pktinfo
36 *		struct sockaddr
37 *		struct sockaddr_in
38 *		struct sockaddr_in6
39 *		in_port_t
40 *
41 * It ensures that the AF_ and PF_ macros are defined.
42 *
43 * It declares ntoh[sl]() and hton[sl]().
44 *
45 * It declares inet_aton(), inet_ntop(), and inet_pton().
46 *
47 * It ensures that INADDR_LOOPBACK, INADDR_ANY, IN6ADDR_ANY_INIT,
48 * in6addr_any, and in6addr_loopback are available.
49 *
50 * It ensures that IN_MULTICAST() is available to check for multicast
51 * addresses.
52 *
53 * MP:
54 *	No impact.
55 *
56 * Reliability:
57 *	No anticipated impact.
58 *
59 * Resources:
60 *	N/A.
61 *
62 * Security:
63 *	No anticipated impact.
64 *
65 * Standards:
66 *	BSD Socket API
67 *	RFC 2553
68 */
69
70/***
71 *** Imports.
72 ***/
73#include <isc/platform.h>
74
75#include <sys/types.h>
76#include <sys/socket.h>		/* Contractual promise. */
77
78#include <net/if.h>
79
80#include <netinet/in.h>		/* Contractual promise. */
81#include <arpa/inet.h>		/* Contractual promise. */
82#ifdef ISC_PLATFORM_NEEDNETINETIN6H
83#include <netinet/in6.h>	/* Required on UnixWare. */
84#endif
85#ifdef ISC_PLATFORM_NEEDNETINET6IN6H
86#include <netinet6/in6.h>	/* Required on BSD/OS for in6_pktinfo. */
87#endif
88
89#ifndef ISC_PLATFORM_HAVEIPV6
90#include <isc/ipv6.h>		/* Contractual promise. */
91#endif
92
93#include <isc/lang.h>
94#include <isc/types.h>
95
96#ifdef ISC_PLATFORM_HAVEINADDR6
97#define in6_addr in_addr6	/* Required for pre RFC2133 implementations. */
98#endif
99
100#ifdef ISC_PLATFORM_HAVEIPV6
101/*
102 * Required for some pre RFC2133 implementations.
103 * IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT were added in
104 * draft-ietf-ipngwg-bsd-api-04.txt or draft-ietf-ipngwg-bsd-api-05.txt.
105 * If 's6_addr' is defined then assume that there is a union and three
106 * levels otherwise assume two levels required.
107 */
108#ifndef IN6ADDR_ANY_INIT
109#ifdef s6_addr
110#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
111#else
112#define IN6ADDR_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }
113#endif
114#endif
115
116#ifndef IN6ADDR_LOOPBACK_INIT
117#ifdef s6_addr
118#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
119#else
120#define IN6ADDR_LOOPBACK_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } }
121#endif
122#endif
123
124#ifndef IN6_IS_ADDR_V4MAPPED
125#define IN6_IS_ADDR_V4MAPPED(x) \
126	 (memcmp((x)->s6_addr, in6addr_any.s6_addr, 10) == 0 && \
127	  (x)->s6_addr[10] == 0xff && (x)->s6_addr[11] == 0xff)
128#endif
129
130#ifndef IN6_IS_ADDR_V4COMPAT
131#define IN6_IS_ADDR_V4COMPAT(x) \
132	 (memcmp((x)->s6_addr, in6addr_any.s6_addr, 12) == 0 && \
133	 ((x)->s6_addr[12] != 0 || (x)->s6_addr[13] != 0 || \
134	  (x)->s6_addr[14] != 0 || \
135	  ((x)->s6_addr[15] != 0 && (x)->s6_addr[15] != 1)))
136#endif
137
138#ifndef IN6_IS_ADDR_MULTICAST
139#define IN6_IS_ADDR_MULTICAST(a)        ((a)->s6_addr[0] == 0xff)
140#endif
141
142#ifndef IN6_IS_ADDR_LINKLOCAL
143#define IN6_IS_ADDR_LINKLOCAL(a) \
144	(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80))
145#endif
146
147#ifndef IN6_IS_ADDR_SITELOCAL
148#define IN6_IS_ADDR_SITELOCAL(a) \
149	(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0))
150#endif
151
152
153#ifndef IN6_IS_ADDR_LOOPBACK
154#define IN6_IS_ADDR_LOOPBACK(x) \
155	(memcmp((x)->s6_addr, in6addr_loopback.s6_addr, 16) == 0)
156#endif
157#endif
158
159#ifndef AF_INET6
160#define AF_INET6 99
161#endif
162
163#ifndef PF_INET6
164#define PF_INET6 AF_INET6
165#endif
166
167#ifndef INADDR_LOOPBACK
168#define INADDR_LOOPBACK 0x7f000001UL
169#endif
170
171#if 0
172#ifndef ISC_PLATFORM_HAVEIN6PKTINFO
173struct in6_pktinfo {
174	struct in6_addr ipi6_addr;    /* src/dst IPv6 address */
175	unsigned int    ipi6_ifindex; /* send/recv interface index */
176};
177#endif
178#endif
179
180/*
181 * Cope with a missing in6addr_any and in6addr_loopback.
182 */
183#if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
184extern const struct in6_addr isc_net_in6addrany;
185#define in6addr_any isc_net_in6addrany
186#endif
187
188#if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
189extern const struct in6_addr isc_net_in6addrloop;
190#define in6addr_loopback isc_net_in6addrloop
191#endif
192
193/*
194 * Fix UnixWare 7.1.1's broken IN6_IS_ADDR_* definitions.
195 */
196#ifdef ISC_PLATFORM_FIXIN6ISADDR
197#undef  IN6_IS_ADDR_GEOGRAPHIC
198#define IN6_IS_ADDR_GEOGRAPHIC(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x80)
199#undef  IN6_IS_ADDR_IPX
200#define IN6_IS_ADDR_IPX(a)        (((a)->S6_un.S6_l[0] & 0xFE) == 0x04)
201#undef  IN6_IS_ADDR_LINKLOCAL
202#define IN6_IS_ADDR_LINKLOCAL(a)  (((a)->S6_un.S6_l[0] & 0xC0FF) == 0x80FE)
203#undef  IN6_IS_ADDR_MULTICAST
204#define IN6_IS_ADDR_MULTICAST(a)  (((a)->S6_un.S6_l[0] & 0xFF) == 0xFF)
205#undef  IN6_IS_ADDR_NSAP
206#define IN6_IS_ADDR_NSAP(a)       (((a)->S6_un.S6_l[0] & 0xFE) == 0x02)
207#undef  IN6_IS_ADDR_PROVIDER
208#define IN6_IS_ADDR_PROVIDER(a)   (((a)->S6_un.S6_l[0] & 0xE0) == 0x40)
209#undef  IN6_IS_ADDR_SITELOCAL
210#define IN6_IS_ADDR_SITELOCAL(a)  (((a)->S6_un.S6_l[0] & 0xC0FF) == 0xC0FE)
211#endif /* ISC_PLATFORM_FIXIN6ISADDR */
212
213/*
214 * Ensure type in_port_t is defined.
215 */
216#ifdef ISC_PLATFORM_NEEDPORTT
217typedef isc_uint16_t in_port_t;
218#endif
219
220/*
221 * If this system does not have MSG_TRUNC (as returned from recvmsg())
222 * ISC_PLATFORM_RECVOVERFLOW will be defined.  This will enable the MSG_TRUNC
223 * faking code in socket.c.
224 */
225#ifndef MSG_TRUNC
226#define ISC_PLATFORM_RECVOVERFLOW
227#endif
228
229#define ISC__IPADDR(x)	((isc_uint32_t)htonl((isc_uint32_t)(x)))
230
231#define ISC_IPADDR_ISMULTICAST(i) \
232		(((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
233		 == ISC__IPADDR(0xe0000000))
234
235#define ISC_IPADDR_ISEXPERIMENTAL(i) \
236		(((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
237		 == ISC__IPADDR(0xf0000000))
238
239/***
240 *** Functions.
241 ***/
242
243ISC_LANG_BEGINDECLS
244
245isc_result_t
246isc_net_probeipv4(void);
247/*
248 * Check if the system's kernel supports IPv4.
249 *
250 * Returns:
251 *
252 *	ISC_R_SUCCESS		IPv4 is supported.
253 *	ISC_R_NOTFOUND		IPv4 is not supported.
254 *	ISC_R_DISABLED		IPv4 is disabled.
255 *	ISC_R_UNEXPECTED
256 */
257
258isc_result_t
259isc_net_probeipv6(void);
260/*
261 * Check if the system's kernel supports IPv6.
262 *
263 * Returns:
264 *
265 *	ISC_R_SUCCESS		IPv6 is supported.
266 *	ISC_R_NOTFOUND		IPv6 is not supported.
267 *	ISC_R_DISABLED		IPv6 is disabled.
268 *	ISC_R_UNEXPECTED
269 */
270
271isc_result_t
272isc_net_probe_ipv6only(void);
273/*
274 * Check if the system's kernel supports the IPV6_V6ONLY socket option.
275 *
276 * Returns:
277 *
278 *	ISC_R_SUCCESS		the option is supported for both TCP and UDP.
279 *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
280 *	ISC_R_UNEXPECTED
281 */
282
283isc_result_t
284isc_net_probe_ipv6pktinfo(void);
285/*
286 * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option
287 * for UDP sockets.
288 *
289 * Returns:
290 *
291 *	ISC_R_SUCCESS		the option is supported.
292 *	ISC_R_NOTFOUND		IPv6 itself or the option is not supported.
293 *	ISC_R_UNEXPECTED
294 */
295
296void
297isc_net_disableipv4(void);
298
299void
300isc_net_disableipv6(void);
301
302void
303isc_net_enableipv4(void);
304
305void
306isc_net_enableipv6(void);
307
308#ifdef ISC_PLATFORM_NEEDNTOP
309const char *
310isc_net_ntop(int af, const void *src, char *dst, size_t size);
311#define inet_ntop isc_net_ntop
312#endif
313
314#ifdef ISC_PLATFORM_NEEDPTON
315int
316isc_net_pton(int af, const char *src, void *dst);
317#undef inet_pton
318#define inet_pton isc_net_pton
319#endif
320
321#ifdef ISC_PLATFORM_NEEDATON
322int
323isc_net_aton(const char *cp, struct in_addr *addr);
324#define inet_aton isc_net_aton
325#endif
326
327ISC_LANG_ENDDECLS
328
329#endif /* ISC_NET_H */
330