Merge from vendor branch CVS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / unix / include / isc / net.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  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.4 2004/03/09 06:12:14 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 <netinet/in.h>         /* Contractual promise. */
79 #include <arpa/inet.h>          /* Contractual promise. */
80 #ifdef ISC_PLATFORM_NEEDNETINETIN6H
81 #include <netinet/in6.h>        /* Required on UnixWare. */
82 #endif
83 #ifdef ISC_PLATFORM_NEEDNETINET6IN6H
84 #include <netinet6/in6.h>       /* Required on BSD/OS for in6_pktinfo. */
85 #endif
86
87 #ifndef ISC_PLATFORM_HAVEIPV6
88 #include <isc/ipv6.h>           /* Contractual promise. */
89 #endif
90
91 #include <isc/lang.h>
92 #include <isc/types.h>
93
94 #ifdef ISC_PLATFORM_HAVEINADDR6
95 #define in6_addr in_addr6       /* Required for pre RFC2133 implementations. */
96 #endif
97
98 #ifdef ISC_PLATFORM_HAVEIPV6
99 /*
100  * Required for some pre RFC2133 implementations.
101  * IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT were added in
102  * draft-ietf-ipngwg-bsd-api-04.txt or draft-ietf-ipngwg-bsd-api-05.txt.  
103  * If 's6_addr' is defined then assume that there is a union and three
104  * levels otherwise assume two levels required.
105  */
106 #ifndef IN6ADDR_ANY_INIT
107 #ifdef s6_addr
108 #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
109 #else
110 #define IN6ADDR_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }
111 #endif
112 #endif
113
114 #ifndef IN6ADDR_LOOPBACK_INIT
115 #ifdef s6_addr
116 #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
117 #else
118 #define IN6ADDR_LOOPBACK_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } }
119 #endif
120 #endif
121
122 #ifndef IN6_IS_ADDR_V4MAPPED
123 #define IN6_IS_ADDR_V4MAPPED(x) \
124          (memcmp((x)->s6_addr, in6addr_any.s6_addr, 10) == 0 && \
125           (x)->s6_addr[10] == 0xff && (x)->s6_addr[11] == 0xff)
126 #endif
127
128 #ifndef IN6_IS_ADDR_V4COMPAT
129 #define IN6_IS_ADDR_V4COMPAT(x) \
130          (memcmp((x)->s6_addr, in6addr_any.s6_addr, 12) == 0 && \
131          ((x)->s6_addr[12] != 0 || (x)->s6_addr[13] != 0 || \
132           (x)->s6_addr[14] != 0 || \
133           ((x)->s6_addr[15] != 0 && (x)->s6_addr[15] != 1)))
134 #endif
135
136 #ifndef IN6_IS_ADDR_MULTICAST
137 #define IN6_IS_ADDR_MULTICAST(a)        ((a)->s6_addr[0] == 0xff)
138 #endif
139
140 #ifndef IN6_IS_ADDR_LOOPBACK
141 #define IN6_IS_ADDR_LOOPBACK(x) \
142         (memcmp((x)->s6_addr, in6addr_loopback.s6_addr, 16) == 0)
143 #endif
144 #endif
145
146 #ifndef AF_INET6
147 #define AF_INET6 99
148 #endif
149
150 #ifndef PF_INET6
151 #define PF_INET6 AF_INET6
152 #endif
153
154 #ifndef INADDR_LOOPBACK
155 #define INADDR_LOOPBACK 0x7f000001UL
156 #endif
157
158 #ifndef ISC_PLATFORM_HAVEIN6PKTINFO
159 struct in6_pktinfo {
160         struct in6_addr ipi6_addr;    /* src/dst IPv6 address */
161         unsigned int    ipi6_ifindex; /* send/recv interface index */
162 };
163 #endif
164
165 /*
166  * Cope with a missing in6addr_any and in6addr_loopback.
167  */
168 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
169 extern const struct in6_addr isc_net_in6addrany;
170 #define in6addr_any isc_net_in6addrany
171 #endif
172
173 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
174 extern const struct in6_addr isc_net_in6addrloop;
175 #define in6addr_loopback isc_net_in6addrloop
176 #endif
177
178 /*
179  * Fix UnixWare 7.1.1's broken IN6_IS_ADDR_* definitions.
180  */
181 #ifdef ISC_PLATFORM_FIXIN6ISADDR
182 #undef  IN6_IS_ADDR_GEOGRAPHIC
183 #define IN6_IS_ADDR_GEOGRAPHIC(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x80)
184 #undef  IN6_IS_ADDR_IPX
185 #define IN6_IS_ADDR_IPX(a)        (((a)->S6_un.S6_l[0] & 0xFE) == 0x04)
186 #undef  IN6_IS_ADDR_LINKLOCAL
187 #define IN6_IS_ADDR_LINKLOCAL(a)  (((a)->S6_un.S6_l[0] & 0xC0FF) == 0x80FE)
188 #undef  IN6_IS_ADDR_MULTICAST
189 #define IN6_IS_ADDR_MULTICAST(a)  (((a)->S6_un.S6_l[0] & 0xFF) == 0xFF)
190 #undef  IN6_IS_ADDR_NSAP
191 #define IN6_IS_ADDR_NSAP(a)       (((a)->S6_un.S6_l[0] & 0xFE) == 0x02)
192 #undef  IN6_IS_ADDR_PROVIDER
193 #define IN6_IS_ADDR_PROVIDER(a)   (((a)->S6_un.S6_l[0] & 0xE0) == 0x40)
194 #undef  IN6_IS_ADDR_SITELOCAL
195 #define IN6_IS_ADDR_SITELOCAL(a)  (((a)->S6_un.S6_l[0] & 0xC0FF) == 0xC0FE)
196 #endif /* ISC_PLATFORM_FIXIN6ISADDR */
197
198 /*
199  * Ensure type in_port_t is defined.
200  */
201 #ifdef ISC_PLATFORM_NEEDPORTT
202 typedef isc_uint16_t in_port_t;
203 #endif
204
205 /*
206  * If this system does not have MSG_TRUNC (as returned from recvmsg())
207  * ISC_PLATFORM_RECVOVERFLOW will be defined.  This will enable the MSG_TRUNC
208  * faking code in socket.c.
209  */
210 #ifndef MSG_TRUNC
211 #define ISC_PLATFORM_RECVOVERFLOW
212 #endif
213
214 #define ISC__IPADDR(x)  ((isc_uint32_t)htonl((isc_uint32_t)(x)))
215
216 #define ISC_IPADDR_ISMULTICAST(i) \
217                 (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
218                  == ISC__IPADDR(0xe0000000))
219
220 #define ISC_IPADDR_ISEXPERIMENTAL(i) \
221                 (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \
222                  == ISC__IPADDR(0xf0000000))
223
224 /***
225  *** Functions.
226  ***/
227
228 ISC_LANG_BEGINDECLS
229
230 isc_result_t
231 isc_net_probeipv4(void);
232 /*
233  * Check if the system's kernel supports IPv4.
234  *
235  * Returns:
236  *
237  *      ISC_R_SUCCESS           IPv4 is supported.
238  *      ISC_R_NOTFOUND          IPv4 is not supported.
239  *      ISC_R_UNEXPECTED
240  */
241
242 isc_result_t
243 isc_net_probeipv6(void);
244 /*
245  * Check if the system's kernel supports IPv6.
246  *
247  * Returns:
248  *
249  *      ISC_R_SUCCESS           IPv6 is supported.
250  *      ISC_R_NOTFOUND          IPv6 is not supported.
251  *      ISC_R_UNEXPECTED
252  */
253
254 #ifdef ISC_PLATFORM_NEEDNTOP
255 const char *
256 isc_net_ntop(int af, const void *src, char *dst, size_t size);
257 #define inet_ntop isc_net_ntop
258 #endif
259
260 #ifdef ISC_PLATFORM_NEEDPTON
261 int
262 isc_net_pton(int af, const char *src, void *dst);
263 #undef inet_pton
264 #define inet_pton isc_net_pton
265 #endif
266
267 #ifdef ISC_PLATFORM_NEEDATON
268 int
269 isc_net_aton(const char *cp, struct in_addr *addr);
270 #define inet_aton isc_net_aton
271 #endif
272
273 ISC_LANG_ENDDECLS
274
275 #endif /* ISC_NET_H */