Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / libisc / netaddr.c
blobb6398a652ae6239dbeb4d9d25bedf6c669eeeede
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2002 Internet Software Consortium.
7 * Permission to use, copy, modify, and 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.
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.
20 /* Id: netaddr.c,v 1.18.12.9 2004/05/15 03:46:12 jinmei Exp */
22 #include <config.h>
24 #define ISC_ONLY_IPV6
26 #include <stdio.h>
28 #include <isc/buffer.h>
29 #include <isc/msgs.h>
30 #include <isc/net.h>
31 #include <isc/netaddr.h>
32 #include <isc/print.h>
33 #include <isc/sockaddr.h>
34 #include <isc/string.h>
35 #include <isc/util.h>
37 isc_boolean_t
38 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b) {
39 REQUIRE(a != NULL && b != NULL);
41 if (a->family != b->family)
42 return (ISC_FALSE);
44 if (a->zone != b->zone)
45 return (ISC_FALSE);
47 switch (a->family) {
48 case AF_INET:
49 if (a->type.in.s_addr != b->type.in.s_addr)
50 return (ISC_FALSE);
51 break;
52 case AF_INET6:
53 if (memcmp(&a->type.in6, &b->type.in6,
54 sizeof(a->type.in6)) != 0 ||
55 a->zone != b->zone)
56 return (ISC_FALSE);
57 break;
58 default:
59 return (ISC_FALSE);
61 return (ISC_TRUE);
64 isc_boolean_t
65 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
66 unsigned int prefixlen)
68 const unsigned char *pa, *pb;
69 unsigned int ipabytes; /* Length of whole IP address in bytes */
70 unsigned int nbytes; /* Number of significant whole bytes */
71 unsigned int nbits; /* Number of significant leftover bits */
73 REQUIRE(a != NULL && b != NULL);
75 if (a->family != b->family)
76 return (ISC_FALSE);
78 if (a->zone != b->zone)
79 return (ISC_FALSE);
81 switch (a->family) {
82 case AF_INET:
83 pa = (const unsigned char *) &a->type.in;
84 pb = (const unsigned char *) &b->type.in;
85 ipabytes = 4;
86 break;
87 case AF_INET6:
88 pa = (const unsigned char *) &a->type.in6;
89 pb = (const unsigned char *) &b->type.in6;
90 ipabytes = 16;
91 break;
92 default:
93 pa = pb = NULL; /* Avoid silly compiler warning. */
94 ipabytes = 0; /* Ditto. */
95 return (ISC_FALSE);
99 * Don't crash if we get a pattern like 10.0.0.1/9999999.
101 if (prefixlen > ipabytes * 8)
102 prefixlen = ipabytes * 8;
104 nbytes = prefixlen / 8;
105 nbits = prefixlen % 8;
107 if (nbytes > 0) {
108 if (memcmp(pa, pb, nbytes) != 0)
109 return (ISC_FALSE);
111 if (nbits > 0) {
112 unsigned int bytea, byteb, mask;
113 INSIST(nbytes < ipabytes);
114 INSIST(nbits < 8);
115 bytea = pa[nbytes];
116 byteb = pb[nbytes];
117 mask = (0xFF << (8-nbits)) & 0xFF;
118 if ((bytea & mask) != (byteb & mask))
119 return (ISC_FALSE);
121 return (ISC_TRUE);
124 isc_result_t
125 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
126 char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
127 char zbuf[sizeof("%4294967295")];
128 unsigned int alen;
129 int zlen;
130 const char *r;
131 const void *type;
133 REQUIRE(netaddr != NULL);
135 switch (netaddr->family) {
136 case AF_INET:
137 type = &netaddr->type.in;
138 break;
139 case AF_INET6:
140 type = &netaddr->type.in6;
141 break;
142 default:
143 return (ISC_R_FAILURE);
145 r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
146 if (r == NULL)
147 return (ISC_R_FAILURE);
149 alen = strlen(abuf);
150 INSIST(alen < sizeof(abuf));
152 zlen = 0;
153 if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
154 zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
155 if (zlen < 0)
156 return (ISC_R_FAILURE);
157 INSIST((unsigned int)zlen < sizeof(zbuf));
160 if (alen + zlen > isc_buffer_availablelength(target))
161 return (ISC_R_NOSPACE);
163 isc_buffer_putmem(target, (unsigned char *)abuf, alen);
164 isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
166 return (ISC_R_SUCCESS);
169 void
170 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
171 isc_result_t result;
172 isc_buffer_t buf;
174 isc_buffer_init(&buf, array, size);
175 result = isc_netaddr_totext(na, &buf);
178 * Null terminate.
180 if (result == ISC_R_SUCCESS) {
181 if (isc_buffer_availablelength(&buf) >= 1)
182 isc_buffer_putuint8(&buf, 0);
183 else
184 result = ISC_R_NOSPACE;
187 if (result != ISC_R_SUCCESS) {
188 snprintf(array, size,
189 isc_msgcat_get(isc_msgcat, ISC_MSGSET_NETADDR,
190 ISC_MSG_UNKNOWNADDR,
191 "<unknown address, family %u>"),
192 na->family);
193 array[size - 1] = '\0';
197 isc_result_t
198 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
199 unsigned int nbits, nbytes, ipbytes, i;
200 const unsigned char *p;
202 switch (s->family) {
203 case AF_INET:
204 p = (const unsigned char *) &s->type.in;
205 ipbytes = 4;
206 break;
207 case AF_INET6:
208 p = (const unsigned char *) &s->type.in6;
209 ipbytes = 16;
210 break;
211 default:
212 ipbytes = 0;
213 return (ISC_R_NOTIMPLEMENTED);
215 nbytes = nbits = 0;
216 for (i = 0; i < ipbytes; i++) {
217 if (p[i] != 0xFF)
218 break;
220 nbytes = i;
221 if (i < ipbytes) {
222 unsigned int c = p[nbytes];
223 while ((c & 0x80) != 0 && nbits < 8) {
224 c <<= 1; nbits++;
226 if ((c & 0xFF) != 0)
227 return (ISC_R_MASKNONCONTIG);
228 i++;
230 for (; i < ipbytes; i++) {
231 if (p[i] != 0)
232 return (ISC_R_MASKNONCONTIG);
233 i++;
235 *lenp = nbytes * 8 + nbits;
236 return (ISC_R_SUCCESS);
239 void
240 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina) {
241 memset(netaddr, 0, sizeof(*netaddr));
242 netaddr->family = AF_INET;
243 netaddr->type.in = *ina;
246 void
247 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
248 memset(netaddr, 0, sizeof(*netaddr));
249 netaddr->family = AF_INET6;
250 netaddr->type.in6 = *ina6;
253 void
254 isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone) {
255 /* we currently only support AF_INET6. */
256 REQUIRE(netaddr->family == AF_INET6);
258 netaddr->zone = zone;
261 isc_uint32_t
262 isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
263 return (netaddr->zone);
266 void
267 isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
268 int family = s->type.sa.sa_family;
269 t->family = family;
270 switch (family) {
271 case AF_INET:
272 t->type.in = s->type.sin.sin_addr;
273 t->zone = 0;
274 break;
275 case AF_INET6:
276 memcpy(&t->type.in6, &s->type.sin6.sin6_addr, 16);
277 #ifdef ISC_PLATFORM_HAVESCOPEID
278 t->zone = s->type.sin6.sin6_scope_id;
279 #else
280 t->zone = 0;
281 #endif
282 break;
283 default:
284 INSIST(0);
288 void
289 isc_netaddr_any(isc_netaddr_t *netaddr) {
290 memset(netaddr, 0, sizeof(*netaddr));
291 netaddr->family = AF_INET;
292 netaddr->type.in.s_addr = INADDR_ANY;
295 #ifdef ISC_PLATFORM_HAVEIPV6
296 void
297 isc_netaddr_any6(isc_netaddr_t *netaddr) {
298 memset(netaddr, 0, sizeof(*netaddr));
299 netaddr->family = AF_INET6;
300 netaddr->type.in6 = in6addr_any;
302 #endif
304 isc_boolean_t
305 isc_netaddr_ismulticast(isc_netaddr_t *na) {
306 switch (na->family) {
307 case AF_INET:
308 return (ISC_TF(ISC_IPADDR_ISMULTICAST(na->type.in.s_addr)));
309 case AF_INET6:
310 return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na->type.in6)));
311 default:
312 return (ISC_FALSE); /* XXXMLG ? */
316 isc_boolean_t
317 isc_netaddr_isexperimental(isc_netaddr_t *na) {
318 switch (na->family) {
319 case AF_INET:
320 return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr)));
321 default:
322 return (ISC_FALSE); /* XXXMLG ? */
326 isc_boolean_t
327 isc_netaddr_islinklocal(isc_netaddr_t *na) {
328 switch (na->family) {
329 case AF_INET:
330 return (ISC_FALSE);
331 case AF_INET6:
332 return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na->type.in6)));
333 default:
334 return (ISC_FALSE);
338 isc_boolean_t
339 isc_netaddr_issitelocal(isc_netaddr_t *na) {
340 switch (na->family) {
341 case AF_INET:
342 return (ISC_FALSE);
343 case AF_INET6:
344 return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na->type.in6)));
345 default:
346 return (ISC_FALSE);
350 #ifdef ISC_PLATFORM_HAVEIPV6
351 void
352 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
353 isc_netaddr_t *src;
355 DE_CONST(s, src); /* Must come before IN6_IS_ADDR_V4MAPPED. */
357 REQUIRE(s->family == AF_INET6);
358 REQUIRE(IN6_IS_ADDR_V4MAPPED(&src->type.in6));
360 memset(t, 0, sizeof(*t));
361 t->family = AF_INET;
362 memcpy(&t->type.in, (char *)&src->type.in6 + 12, 4);
363 return;
365 #endif