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