turns printfs back on
[freebsd-src/fkvm-freebsd.git] / contrib / ntp / libisc / netaddr.c
blob1fcd1027b39dd37945c1bf0ab97b7819c7e26511
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
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.
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.
18 /* $Id: netaddr.c,v 1.18.12.9 2004/05/15 03:46:12 jinmei Exp $ */
20 #include <config.h>
22 #define ISC_ONLY_IPV6
24 #include <stdio.h>
26 #include <isc/buffer.h>
27 #include <isc/msgs.h>
28 #include <isc/net.h>
29 #include <isc/netaddr.h>
30 #include <isc/print.h>
31 #include <isc/sockaddr.h>
32 #include <isc/string.h>
33 #include <isc/util.h>
35 isc_boolean_t
36 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b) {
37 REQUIRE(a != NULL && b != NULL);
39 if (a->family != b->family)
40 return (ISC_FALSE);
42 if (a->zone != b->zone)
43 return (ISC_FALSE);
45 switch (a->family) {
46 case AF_INET:
47 if (a->type.in.s_addr != b->type.in.s_addr)
48 return (ISC_FALSE);
49 break;
50 case AF_INET6:
51 if (memcmp(&a->type.in6, &b->type.in6,
52 sizeof(a->type.in6)) != 0 ||
53 a->zone != b->zone)
54 return (ISC_FALSE);
55 break;
56 default:
57 return (ISC_FALSE);
59 return (ISC_TRUE);
62 isc_boolean_t
63 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
64 unsigned int prefixlen)
66 const unsigned char *pa, *pb;
67 unsigned int ipabytes; /* Length of whole IP address in bytes */
68 unsigned int nbytes; /* Number of significant whole bytes */
69 unsigned int nbits; /* Number of significant leftover bits */
71 REQUIRE(a != NULL && b != NULL);
73 if (a->family != b->family)
74 return (ISC_FALSE);
76 if (a->zone != b->zone)
77 return (ISC_FALSE);
79 switch (a->family) {
80 case AF_INET:
81 pa = (const unsigned char *) &a->type.in;
82 pb = (const unsigned char *) &b->type.in;
83 ipabytes = 4;
84 break;
85 case AF_INET6:
86 pa = (const unsigned char *) &a->type.in6;
87 pb = (const unsigned char *) &b->type.in6;
88 ipabytes = 16;
89 break;
90 default:
91 pa = pb = NULL; /* Avoid silly compiler warning. */
92 ipabytes = 0; /* Ditto. */
93 return (ISC_FALSE);
97 * Don't crash if we get a pattern like 10.0.0.1/9999999.
99 if (prefixlen > ipabytes * 8)
100 prefixlen = ipabytes * 8;
102 nbytes = prefixlen / 8;
103 nbits = prefixlen % 8;
105 if (nbytes > 0) {
106 if (memcmp(pa, pb, nbytes) != 0)
107 return (ISC_FALSE);
109 if (nbits > 0) {
110 unsigned int bytea, byteb, mask;
111 INSIST(nbytes < ipabytes);
112 INSIST(nbits < 8);
113 bytea = pa[nbytes];
114 byteb = pb[nbytes];
115 mask = (0xFF << (8-nbits)) & 0xFF;
116 if ((bytea & mask) != (byteb & mask))
117 return (ISC_FALSE);
119 return (ISC_TRUE);
122 isc_result_t
123 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
124 char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
125 char zbuf[sizeof("%4294967295")];
126 unsigned int alen;
127 int zlen;
128 const char *r;
129 const void *type;
131 REQUIRE(netaddr != NULL);
133 switch (netaddr->family) {
134 case AF_INET:
135 type = &netaddr->type.in;
136 break;
137 case AF_INET6:
138 type = &netaddr->type.in6;
139 break;
140 default:
141 return (ISC_R_FAILURE);
143 r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
144 if (r == NULL)
145 return (ISC_R_FAILURE);
147 alen = strlen(abuf);
148 INSIST(alen < sizeof(abuf));
150 zlen = 0;
151 if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
152 zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
153 if (zlen < 0)
154 return (ISC_R_FAILURE);
155 INSIST((unsigned int)zlen < sizeof(zbuf));
158 if (alen + zlen > isc_buffer_availablelength(target))
159 return (ISC_R_NOSPACE);
161 isc_buffer_putmem(target, (unsigned char *)abuf, alen);
162 isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
164 return (ISC_R_SUCCESS);
167 void
168 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
169 isc_result_t result;
170 isc_buffer_t buf;
172 isc_buffer_init(&buf, array, size);
173 result = isc_netaddr_totext(na, &buf);
176 * Null terminate.
178 if (result == ISC_R_SUCCESS) {
179 if (isc_buffer_availablelength(&buf) >= 1)
180 isc_buffer_putuint8(&buf, 0);
181 else
182 result = ISC_R_NOSPACE;
185 if (result != ISC_R_SUCCESS) {
186 snprintf(array, size,
187 isc_msgcat_get(isc_msgcat, ISC_MSGSET_NETADDR,
188 ISC_MSG_UNKNOWNADDR,
189 "<unknown address, family %u>"),
190 na->family);
191 array[size - 1] = '\0';
195 isc_result_t
196 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
197 unsigned int nbits, nbytes, ipbytes, i;
198 const unsigned char *p;
200 switch (s->family) {
201 case AF_INET:
202 p = (const unsigned char *) &s->type.in;
203 ipbytes = 4;
204 break;
205 case AF_INET6:
206 p = (const unsigned char *) &s->type.in6;
207 ipbytes = 16;
208 break;
209 default:
210 ipbytes = 0;
211 return (ISC_R_NOTIMPLEMENTED);
213 nbytes = nbits = 0;
214 for (i = 0; i < ipbytes; i++) {
215 if (p[i] != 0xFF)
216 break;
218 nbytes = i;
219 if (i < ipbytes) {
220 unsigned int c = p[nbytes];
221 while ((c & 0x80) != 0 && nbits < 8) {
222 c <<= 1; nbits++;
224 if ((c & 0xFF) != 0)
225 return (ISC_R_MASKNONCONTIG);
226 i++;
228 for (; i < ipbytes; i++) {
229 if (p[i] != 0)
230 return (ISC_R_MASKNONCONTIG);
231 i++;
233 *lenp = nbytes * 8 + nbits;
234 return (ISC_R_SUCCESS);
237 void
238 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina) {
239 memset(netaddr, 0, sizeof(*netaddr));
240 netaddr->family = AF_INET;
241 netaddr->type.in = *ina;
244 void
245 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
246 memset(netaddr, 0, sizeof(*netaddr));
247 netaddr->family = AF_INET6;
248 netaddr->type.in6 = *ina6;
251 void
252 isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone) {
253 /* we currently only support AF_INET6. */
254 REQUIRE(netaddr->family == AF_INET6);
256 netaddr->zone = zone;
259 isc_uint32_t
260 isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
261 return (netaddr->zone);
264 void
265 isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
266 int family = s->type.sa.sa_family;
267 t->family = family;
268 switch (family) {
269 case AF_INET:
270 t->type.in = s->type.sin.sin_addr;
271 t->zone = 0;
272 break;
273 case AF_INET6:
274 memcpy(&t->type.in6, &s->type.sin6.sin6_addr, 16);
275 #ifdef ISC_PLATFORM_HAVESCOPEID
276 t->zone = s->type.sin6.sin6_scope_id;
277 #else
278 t->zone = 0;
279 #endif
280 break;
281 default:
282 INSIST(0);
286 void
287 isc_netaddr_any(isc_netaddr_t *netaddr) {
288 memset(netaddr, 0, sizeof(*netaddr));
289 netaddr->family = AF_INET;
290 netaddr->type.in.s_addr = INADDR_ANY;
293 #ifdef ISC_PLATFORM_HAVEIPV6
294 void
295 isc_netaddr_any6(isc_netaddr_t *netaddr) {
296 memset(netaddr, 0, sizeof(*netaddr));
297 netaddr->family = AF_INET6;
298 netaddr->type.in6 = in6addr_any;
300 #endif
302 isc_boolean_t
303 isc_netaddr_ismulticast(isc_netaddr_t *na) {
304 switch (na->family) {
305 case AF_INET:
306 return (ISC_TF(ISC_IPADDR_ISMULTICAST(na->type.in.s_addr)));
307 case AF_INET6:
308 return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na->type.in6)));
309 default:
310 return (ISC_FALSE); /* XXXMLG ? */
314 isc_boolean_t
315 isc_netaddr_isexperimental(isc_netaddr_t *na) {
316 switch (na->family) {
317 case AF_INET:
318 return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr)));
319 default:
320 return (ISC_FALSE); /* XXXMLG ? */
324 isc_boolean_t
325 isc_netaddr_islinklocal(isc_netaddr_t *na) {
326 switch (na->family) {
327 case AF_INET:
328 return (ISC_FALSE);
329 case AF_INET6:
330 return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na->type.in6)));
331 default:
332 return (ISC_FALSE);
336 isc_boolean_t
337 isc_netaddr_issitelocal(isc_netaddr_t *na) {
338 switch (na->family) {
339 case AF_INET:
340 return (ISC_FALSE);
341 case AF_INET6:
342 return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na->type.in6)));
343 default:
344 return (ISC_FALSE);
348 #ifdef ISC_PLATFORM_HAVEIPV6
349 void
350 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
351 isc_netaddr_t *src;
353 DE_CONST(s, src); /* Must come before IN6_IS_ADDR_V4MAPPED. */
355 REQUIRE(s->family == AF_INET6);
356 REQUIRE(IN6_IS_ADDR_V4MAPPED(&src->type.in6));
358 memset(t, 0, sizeof(*t));
359 t->family = AF_INET;
360 memcpy(&t->type.in, (char *)&src->type.in6 + 12, 4);
361 return;
363 #endif