1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2010-2012 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1999-2002 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert
24*a466cc55SCy Schubert #include <stdio.h>
25*a466cc55SCy Schubert
26*a466cc55SCy Schubert #include <isc/buffer.h>
27*a466cc55SCy Schubert #include <isc/msgs.h>
28*a466cc55SCy Schubert #include <isc/net.h>
29*a466cc55SCy Schubert #include <isc/netaddr.h>
30*a466cc55SCy Schubert #include <isc/print.h>
31*a466cc55SCy Schubert #include <isc/sockaddr.h>
32*a466cc55SCy Schubert #include <isc/string.h>
33*a466cc55SCy Schubert #include <isc/util.h>
34*a466cc55SCy Schubert #include "ntp_stdlib.h" /* NTP change for strlcpy, strlcat */
35*a466cc55SCy Schubert
36*a466cc55SCy Schubert isc_boolean_t
isc_netaddr_equal(const isc_netaddr_t * a,const isc_netaddr_t * b)37*a466cc55SCy Schubert isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b) {
38*a466cc55SCy Schubert REQUIRE(a != NULL && b != NULL);
39*a466cc55SCy Schubert
40*a466cc55SCy Schubert if (a->family != b->family)
41*a466cc55SCy Schubert return (ISC_FALSE);
42*a466cc55SCy Schubert
43*a466cc55SCy Schubert if (a->zone != b->zone)
44*a466cc55SCy Schubert return (ISC_FALSE);
45*a466cc55SCy Schubert
46*a466cc55SCy Schubert switch (a->family) {
47*a466cc55SCy Schubert case AF_INET:
48*a466cc55SCy Schubert if (a->type.in.s_addr != b->type.in.s_addr)
49*a466cc55SCy Schubert return (ISC_FALSE);
50*a466cc55SCy Schubert break;
51*a466cc55SCy Schubert case AF_INET6:
52*a466cc55SCy Schubert if (memcmp(&a->type.in6, &b->type.in6,
53*a466cc55SCy Schubert sizeof(a->type.in6)) != 0 ||
54*a466cc55SCy Schubert a->zone != b->zone)
55*a466cc55SCy Schubert return (ISC_FALSE);
56*a466cc55SCy Schubert break;
57*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESYSUNH
58*a466cc55SCy Schubert case AF_UNIX:
59*a466cc55SCy Schubert if (strcmp(a->type.un, b->type.un) != 0)
60*a466cc55SCy Schubert return (ISC_FALSE);
61*a466cc55SCy Schubert break;
62*a466cc55SCy Schubert #endif
63*a466cc55SCy Schubert default:
64*a466cc55SCy Schubert return (ISC_FALSE);
65*a466cc55SCy Schubert }
66*a466cc55SCy Schubert return (ISC_TRUE);
67*a466cc55SCy Schubert }
68*a466cc55SCy Schubert
69*a466cc55SCy Schubert isc_boolean_t
isc_netaddr_eqprefix(const isc_netaddr_t * a,const isc_netaddr_t * b,unsigned int prefixlen)70*a466cc55SCy Schubert isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
71*a466cc55SCy Schubert unsigned int prefixlen)
72*a466cc55SCy Schubert {
73*a466cc55SCy Schubert const unsigned char *pa = NULL, *pb = NULL;
74*a466cc55SCy Schubert unsigned int ipabytes = 0; /* Length of whole IP address in bytes */
75*a466cc55SCy Schubert unsigned int nbytes; /* Number of significant whole bytes */
76*a466cc55SCy Schubert unsigned int nbits; /* Number of significant leftover bits */
77*a466cc55SCy Schubert
78*a466cc55SCy Schubert REQUIRE(a != NULL && b != NULL);
79*a466cc55SCy Schubert
80*a466cc55SCy Schubert if (a->family != b->family)
81*a466cc55SCy Schubert return (ISC_FALSE);
82*a466cc55SCy Schubert
83*a466cc55SCy Schubert if (a->zone != b->zone && b->zone != 0)
84*a466cc55SCy Schubert return (ISC_FALSE);
85*a466cc55SCy Schubert
86*a466cc55SCy Schubert switch (a->family) {
87*a466cc55SCy Schubert case AF_INET:
88*a466cc55SCy Schubert pa = (const unsigned char *) &a->type.in;
89*a466cc55SCy Schubert pb = (const unsigned char *) &b->type.in;
90*a466cc55SCy Schubert ipabytes = 4;
91*a466cc55SCy Schubert break;
92*a466cc55SCy Schubert case AF_INET6:
93*a466cc55SCy Schubert pa = (const unsigned char *) &a->type.in6;
94*a466cc55SCy Schubert pb = (const unsigned char *) &b->type.in6;
95*a466cc55SCy Schubert ipabytes = 16;
96*a466cc55SCy Schubert break;
97*a466cc55SCy Schubert default:
98*a466cc55SCy Schubert return (ISC_FALSE);
99*a466cc55SCy Schubert }
100*a466cc55SCy Schubert
101*a466cc55SCy Schubert /*
102*a466cc55SCy Schubert * Don't crash if we get a pattern like 10.0.0.1/9999999.
103*a466cc55SCy Schubert */
104*a466cc55SCy Schubert if (prefixlen > ipabytes * 8)
105*a466cc55SCy Schubert prefixlen = ipabytes * 8;
106*a466cc55SCy Schubert
107*a466cc55SCy Schubert nbytes = prefixlen / 8;
108*a466cc55SCy Schubert nbits = prefixlen % 8;
109*a466cc55SCy Schubert
110*a466cc55SCy Schubert if (nbytes > 0) {
111*a466cc55SCy Schubert if (memcmp(pa, pb, nbytes) != 0)
112*a466cc55SCy Schubert return (ISC_FALSE);
113*a466cc55SCy Schubert }
114*a466cc55SCy Schubert if (nbits > 0) {
115*a466cc55SCy Schubert unsigned int bytea, byteb, mask;
116*a466cc55SCy Schubert INSIST(nbytes < ipabytes);
117*a466cc55SCy Schubert INSIST(nbits < 8);
118*a466cc55SCy Schubert bytea = pa[nbytes];
119*a466cc55SCy Schubert byteb = pb[nbytes];
120*a466cc55SCy Schubert mask = (0xFF << (8-nbits)) & 0xFF;
121*a466cc55SCy Schubert if ((bytea & mask) != (byteb & mask))
122*a466cc55SCy Schubert return (ISC_FALSE);
123*a466cc55SCy Schubert }
124*a466cc55SCy Schubert return (ISC_TRUE);
125*a466cc55SCy Schubert }
126*a466cc55SCy Schubert
127*a466cc55SCy Schubert isc_result_t
isc_netaddr_totext(const isc_netaddr_t * netaddr,isc_buffer_t * target)128*a466cc55SCy Schubert isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
129*a466cc55SCy Schubert char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
130*a466cc55SCy Schubert char zbuf[sizeof("%4294967295")];
131*a466cc55SCy Schubert unsigned int alen;
132*a466cc55SCy Schubert int zlen;
133*a466cc55SCy Schubert const char *r;
134*a466cc55SCy Schubert const void *type;
135*a466cc55SCy Schubert
136*a466cc55SCy Schubert REQUIRE(netaddr != NULL);
137*a466cc55SCy Schubert
138*a466cc55SCy Schubert switch (netaddr->family) {
139*a466cc55SCy Schubert case AF_INET:
140*a466cc55SCy Schubert type = &netaddr->type.in;
141*a466cc55SCy Schubert break;
142*a466cc55SCy Schubert case AF_INET6:
143*a466cc55SCy Schubert type = &netaddr->type.in6;
144*a466cc55SCy Schubert break;
145*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESYSUNH
146*a466cc55SCy Schubert case AF_UNIX:
147*a466cc55SCy Schubert alen = strlen(netaddr->type.un);
148*a466cc55SCy Schubert if (alen > isc_buffer_availablelength(target))
149*a466cc55SCy Schubert return (ISC_R_NOSPACE);
150*a466cc55SCy Schubert isc_buffer_putmem(target,
151*a466cc55SCy Schubert (const unsigned char *)(netaddr->type.un),
152*a466cc55SCy Schubert alen);
153*a466cc55SCy Schubert return (ISC_R_SUCCESS);
154*a466cc55SCy Schubert #endif
155*a466cc55SCy Schubert default:
156*a466cc55SCy Schubert return (ISC_R_FAILURE);
157*a466cc55SCy Schubert }
158*a466cc55SCy Schubert r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
159*a466cc55SCy Schubert if (r == NULL)
160*a466cc55SCy Schubert return (ISC_R_FAILURE);
161*a466cc55SCy Schubert
162*a466cc55SCy Schubert alen = (unsigned int)strlen(abuf); /* no overflow possible */
163*a466cc55SCy Schubert INSIST(alen < sizeof(abuf));
164*a466cc55SCy Schubert
165*a466cc55SCy Schubert zlen = 0;
166*a466cc55SCy Schubert if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
167*a466cc55SCy Schubert zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
168*a466cc55SCy Schubert if (zlen < 0)
169*a466cc55SCy Schubert return (ISC_R_FAILURE);
170*a466cc55SCy Schubert INSIST((unsigned int)zlen < sizeof(zbuf));
171*a466cc55SCy Schubert }
172*a466cc55SCy Schubert
173*a466cc55SCy Schubert if (alen + zlen > isc_buffer_availablelength(target))
174*a466cc55SCy Schubert return (ISC_R_NOSPACE);
175*a466cc55SCy Schubert
176*a466cc55SCy Schubert isc_buffer_putmem(target, (unsigned char *)abuf, alen);
177*a466cc55SCy Schubert isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
178*a466cc55SCy Schubert
179*a466cc55SCy Schubert return (ISC_R_SUCCESS);
180*a466cc55SCy Schubert }
181*a466cc55SCy Schubert
182*a466cc55SCy Schubert void
isc_netaddr_format(const isc_netaddr_t * na,char * array,unsigned int size)183*a466cc55SCy Schubert isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
184*a466cc55SCy Schubert isc_result_t result;
185*a466cc55SCy Schubert isc_buffer_t buf;
186*a466cc55SCy Schubert
187*a466cc55SCy Schubert isc_buffer_init(&buf, array, size);
188*a466cc55SCy Schubert result = isc_netaddr_totext(na, &buf);
189*a466cc55SCy Schubert
190*a466cc55SCy Schubert if (size == 0)
191*a466cc55SCy Schubert return;
192*a466cc55SCy Schubert
193*a466cc55SCy Schubert /*
194*a466cc55SCy Schubert * Null terminate.
195*a466cc55SCy Schubert */
196*a466cc55SCy Schubert if (result == ISC_R_SUCCESS) {
197*a466cc55SCy Schubert if (isc_buffer_availablelength(&buf) >= 1)
198*a466cc55SCy Schubert isc_buffer_putuint8(&buf, 0);
199*a466cc55SCy Schubert else
200*a466cc55SCy Schubert result = ISC_R_NOSPACE;
201*a466cc55SCy Schubert }
202*a466cc55SCy Schubert
203*a466cc55SCy Schubert if (result != ISC_R_SUCCESS) {
204*a466cc55SCy Schubert snprintf(array, size,
205*a466cc55SCy Schubert "<%s %u>",
206*a466cc55SCy Schubert isc_msgcat_get(isc_msgcat, ISC_MSGSET_NETADDR,
207*a466cc55SCy Schubert ISC_MSG_UNKNOWNADDR,
208*a466cc55SCy Schubert "unknown address, family"),
209*a466cc55SCy Schubert na->family);
210*a466cc55SCy Schubert array[size - 1] = '\0';
211*a466cc55SCy Schubert }
212*a466cc55SCy Schubert }
213*a466cc55SCy Schubert
214*a466cc55SCy Schubert
215*a466cc55SCy Schubert isc_result_t
isc_netaddr_prefixok(const isc_netaddr_t * na,unsigned int prefixlen)216*a466cc55SCy Schubert isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen) {
217*a466cc55SCy Schubert static const unsigned char zeros[16] = { 0 };
218*a466cc55SCy Schubert unsigned int nbits, nbytes, ipbytes = 0;
219*a466cc55SCy Schubert const unsigned char *p;
220*a466cc55SCy Schubert
221*a466cc55SCy Schubert switch (na->family) {
222*a466cc55SCy Schubert case AF_INET:
223*a466cc55SCy Schubert p = (const unsigned char *) &na->type.in;
224*a466cc55SCy Schubert ipbytes = 4;
225*a466cc55SCy Schubert if (prefixlen > 32)
226*a466cc55SCy Schubert return (ISC_R_RANGE);
227*a466cc55SCy Schubert break;
228*a466cc55SCy Schubert case AF_INET6:
229*a466cc55SCy Schubert p = (const unsigned char *) &na->type.in6;
230*a466cc55SCy Schubert ipbytes = 16;
231*a466cc55SCy Schubert if (prefixlen > 128)
232*a466cc55SCy Schubert return (ISC_R_RANGE);
233*a466cc55SCy Schubert break;
234*a466cc55SCy Schubert default:
235*a466cc55SCy Schubert return (ISC_R_NOTIMPLEMENTED);
236*a466cc55SCy Schubert }
237*a466cc55SCy Schubert nbytes = prefixlen / 8;
238*a466cc55SCy Schubert nbits = prefixlen % 8;
239*a466cc55SCy Schubert if (nbits != 0) {
240*a466cc55SCy Schubert if ((p[nbytes] & (0xff>>nbits)) != 0U)
241*a466cc55SCy Schubert return (ISC_R_FAILURE);
242*a466cc55SCy Schubert nbytes++;
243*a466cc55SCy Schubert }
244*a466cc55SCy Schubert if (memcmp(p + nbytes, zeros, ipbytes - nbytes) != 0)
245*a466cc55SCy Schubert return (ISC_R_FAILURE);
246*a466cc55SCy Schubert return (ISC_R_SUCCESS);
247*a466cc55SCy Schubert }
248*a466cc55SCy Schubert
249*a466cc55SCy Schubert isc_result_t
isc_netaddr_masktoprefixlen(const isc_netaddr_t * s,unsigned int * lenp)250*a466cc55SCy Schubert isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
251*a466cc55SCy Schubert unsigned int nbits = 0, nbytes = 0, ipbytes = 0, i;
252*a466cc55SCy Schubert const unsigned char *p;
253*a466cc55SCy Schubert
254*a466cc55SCy Schubert switch (s->family) {
255*a466cc55SCy Schubert case AF_INET:
256*a466cc55SCy Schubert p = (const unsigned char *) &s->type.in;
257*a466cc55SCy Schubert ipbytes = 4;
258*a466cc55SCy Schubert break;
259*a466cc55SCy Schubert case AF_INET6:
260*a466cc55SCy Schubert p = (const unsigned char *) &s->type.in6;
261*a466cc55SCy Schubert ipbytes = 16;
262*a466cc55SCy Schubert break;
263*a466cc55SCy Schubert default:
264*a466cc55SCy Schubert return (ISC_R_NOTIMPLEMENTED);
265*a466cc55SCy Schubert }
266*a466cc55SCy Schubert for (i = 0; i < ipbytes; i++) {
267*a466cc55SCy Schubert if (p[i] != 0xFF)
268*a466cc55SCy Schubert break;
269*a466cc55SCy Schubert }
270*a466cc55SCy Schubert nbytes = i;
271*a466cc55SCy Schubert if (i < ipbytes) {
272*a466cc55SCy Schubert unsigned int c = p[nbytes];
273*a466cc55SCy Schubert while ((c & 0x80) != 0 && nbits < 8) {
274*a466cc55SCy Schubert c <<= 1; nbits++;
275*a466cc55SCy Schubert }
276*a466cc55SCy Schubert if ((c & 0xFF) != 0)
277*a466cc55SCy Schubert return (ISC_R_MASKNONCONTIG);
278*a466cc55SCy Schubert i++;
279*a466cc55SCy Schubert }
280*a466cc55SCy Schubert for (; i < ipbytes; i++) {
281*a466cc55SCy Schubert if (p[i] != 0)
282*a466cc55SCy Schubert return (ISC_R_MASKNONCONTIG);
283*a466cc55SCy Schubert }
284*a466cc55SCy Schubert *lenp = nbytes * 8 + nbits;
285*a466cc55SCy Schubert return (ISC_R_SUCCESS);
286*a466cc55SCy Schubert }
287*a466cc55SCy Schubert
288*a466cc55SCy Schubert void
isc_netaddr_fromin(isc_netaddr_t * netaddr,const struct in_addr * ina)289*a466cc55SCy Schubert isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina) {
290*a466cc55SCy Schubert memset(netaddr, 0, sizeof(*netaddr));
291*a466cc55SCy Schubert netaddr->family = AF_INET;
292*a466cc55SCy Schubert netaddr->type.in = *ina;
293*a466cc55SCy Schubert }
294*a466cc55SCy Schubert
295*a466cc55SCy Schubert void
isc_netaddr_fromin6(isc_netaddr_t * netaddr,const struct in6_addr * ina6)296*a466cc55SCy Schubert isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
297*a466cc55SCy Schubert memset(netaddr, 0, sizeof(*netaddr));
298*a466cc55SCy Schubert netaddr->family = AF_INET6;
299*a466cc55SCy Schubert netaddr->type.in6 = *ina6;
300*a466cc55SCy Schubert }
301*a466cc55SCy Schubert
302*a466cc55SCy Schubert isc_result_t
isc_netaddr_frompath(isc_netaddr_t * netaddr,const char * path)303*a466cc55SCy Schubert isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path) {
304*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESYSUNH
305*a466cc55SCy Schubert if (strlen(path) > sizeof(netaddr->type.un) - 1)
306*a466cc55SCy Schubert return (ISC_R_NOSPACE);
307*a466cc55SCy Schubert
308*a466cc55SCy Schubert memset(netaddr, 0, sizeof(*netaddr));
309*a466cc55SCy Schubert netaddr->family = AF_UNIX;
310*a466cc55SCy Schubert strlcpy(netaddr->type.un, path, sizeof(netaddr->type.un));
311*a466cc55SCy Schubert netaddr->zone = 0;
312*a466cc55SCy Schubert return (ISC_R_SUCCESS);
313*a466cc55SCy Schubert #else
314*a466cc55SCy Schubert UNUSED(netaddr);
315*a466cc55SCy Schubert UNUSED(path);
316*a466cc55SCy Schubert return (ISC_R_NOTIMPLEMENTED);
317*a466cc55SCy Schubert #endif
318*a466cc55SCy Schubert }
319*a466cc55SCy Schubert
320*a466cc55SCy Schubert
321*a466cc55SCy Schubert void
isc_netaddr_setzone(isc_netaddr_t * netaddr,isc_uint32_t zone)322*a466cc55SCy Schubert isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone) {
323*a466cc55SCy Schubert /* we currently only support AF_INET6. */
324*a466cc55SCy Schubert REQUIRE(netaddr->family == AF_INET6);
325*a466cc55SCy Schubert
326*a466cc55SCy Schubert netaddr->zone = zone;
327*a466cc55SCy Schubert }
328*a466cc55SCy Schubert
329*a466cc55SCy Schubert isc_uint32_t
isc_netaddr_getzone(const isc_netaddr_t * netaddr)330*a466cc55SCy Schubert isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
331*a466cc55SCy Schubert return (netaddr->zone);
332*a466cc55SCy Schubert }
333*a466cc55SCy Schubert
334*a466cc55SCy Schubert void
isc_netaddr_fromsockaddr(isc_netaddr_t * t,const isc_sockaddr_t * s)335*a466cc55SCy Schubert isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
336*a466cc55SCy Schubert int family = s->type.sa.sa_family;
337*a466cc55SCy Schubert t->family = family;
338*a466cc55SCy Schubert switch (family) {
339*a466cc55SCy Schubert case AF_INET:
340*a466cc55SCy Schubert t->type.in = s->type.sin.sin_addr;
341*a466cc55SCy Schubert t->zone = 0;
342*a466cc55SCy Schubert break;
343*a466cc55SCy Schubert case AF_INET6:
344*a466cc55SCy Schubert memcpy(&t->type.in6, &s->type.sin6.sin6_addr, 16);
345*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESCOPEID
346*a466cc55SCy Schubert t->zone = s->type.sin6.sin6_scope_id;
347*a466cc55SCy Schubert #else
348*a466cc55SCy Schubert t->zone = 0;
349*a466cc55SCy Schubert #endif
350*a466cc55SCy Schubert break;
351*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESYSUNH
352*a466cc55SCy Schubert case AF_UNIX:
353*a466cc55SCy Schubert memcpy(t->type.un, s->type.sunix.sun_path, sizeof(t->type.un));
354*a466cc55SCy Schubert t->zone = 0;
355*a466cc55SCy Schubert break;
356*a466cc55SCy Schubert #endif
357*a466cc55SCy Schubert default:
358*a466cc55SCy Schubert INSIST(0);
359*a466cc55SCy Schubert }
360*a466cc55SCy Schubert }
361*a466cc55SCy Schubert
362*a466cc55SCy Schubert void
isc_netaddr_any(isc_netaddr_t * netaddr)363*a466cc55SCy Schubert isc_netaddr_any(isc_netaddr_t *netaddr) {
364*a466cc55SCy Schubert memset(netaddr, 0, sizeof(*netaddr));
365*a466cc55SCy Schubert netaddr->family = AF_INET;
366*a466cc55SCy Schubert netaddr->type.in.s_addr = INADDR_ANY;
367*a466cc55SCy Schubert }
368*a466cc55SCy Schubert
369*a466cc55SCy Schubert void
isc_netaddr_any6(isc_netaddr_t * netaddr)370*a466cc55SCy Schubert isc_netaddr_any6(isc_netaddr_t *netaddr) {
371*a466cc55SCy Schubert memset(netaddr, 0, sizeof(*netaddr));
372*a466cc55SCy Schubert netaddr->family = AF_INET6;
373*a466cc55SCy Schubert netaddr->type.in6 = in6addr_any;
374*a466cc55SCy Schubert }
375*a466cc55SCy Schubert
376*a466cc55SCy Schubert isc_boolean_t
isc_netaddr_ismulticast(isc_netaddr_t * na)377*a466cc55SCy Schubert isc_netaddr_ismulticast(isc_netaddr_t *na) {
378*a466cc55SCy Schubert switch (na->family) {
379*a466cc55SCy Schubert case AF_INET:
380*a466cc55SCy Schubert return (ISC_TF(ISC_IPADDR_ISMULTICAST(na->type.in.s_addr)));
381*a466cc55SCy Schubert case AF_INET6:
382*a466cc55SCy Schubert return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na->type.in6)));
383*a466cc55SCy Schubert default:
384*a466cc55SCy Schubert return (ISC_FALSE); /* XXXMLG ? */
385*a466cc55SCy Schubert }
386*a466cc55SCy Schubert }
387*a466cc55SCy Schubert
388*a466cc55SCy Schubert isc_boolean_t
isc_netaddr_isexperimental(isc_netaddr_t * na)389*a466cc55SCy Schubert isc_netaddr_isexperimental(isc_netaddr_t *na) {
390*a466cc55SCy Schubert switch (na->family) {
391*a466cc55SCy Schubert case AF_INET:
392*a466cc55SCy Schubert return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr)));
393*a466cc55SCy Schubert default:
394*a466cc55SCy Schubert return (ISC_FALSE); /* XXXMLG ? */
395*a466cc55SCy Schubert }
396*a466cc55SCy Schubert }
397*a466cc55SCy Schubert
398*a466cc55SCy Schubert isc_boolean_t
isc_netaddr_islinklocal(isc_netaddr_t * na)399*a466cc55SCy Schubert isc_netaddr_islinklocal(isc_netaddr_t *na) {
400*a466cc55SCy Schubert switch (na->family) {
401*a466cc55SCy Schubert case AF_INET:
402*a466cc55SCy Schubert return (ISC_FALSE);
403*a466cc55SCy Schubert case AF_INET6:
404*a466cc55SCy Schubert return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na->type.in6)));
405*a466cc55SCy Schubert default:
406*a466cc55SCy Schubert return (ISC_FALSE);
407*a466cc55SCy Schubert }
408*a466cc55SCy Schubert }
409*a466cc55SCy Schubert
410*a466cc55SCy Schubert isc_boolean_t
isc_netaddr_issitelocal(isc_netaddr_t * na)411*a466cc55SCy Schubert isc_netaddr_issitelocal(isc_netaddr_t *na) {
412*a466cc55SCy Schubert switch (na->family) {
413*a466cc55SCy Schubert case AF_INET:
414*a466cc55SCy Schubert return (ISC_FALSE);
415*a466cc55SCy Schubert case AF_INET6:
416*a466cc55SCy Schubert return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na->type.in6)));
417*a466cc55SCy Schubert default:
418*a466cc55SCy Schubert return (ISC_FALSE);
419*a466cc55SCy Schubert }
420*a466cc55SCy Schubert }
421*a466cc55SCy Schubert
422*a466cc55SCy Schubert void
isc_netaddr_fromv4mapped(isc_netaddr_t * t,const isc_netaddr_t * s)423*a466cc55SCy Schubert isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
424*a466cc55SCy Schubert isc_netaddr_t *src;
425*a466cc55SCy Schubert
426*a466cc55SCy Schubert DE_CONST(s, src); /* Must come before IN6_IS_ADDR_V4MAPPED. */
427*a466cc55SCy Schubert
428*a466cc55SCy Schubert REQUIRE(s->family == AF_INET6);
429*a466cc55SCy Schubert REQUIRE(IN6_IS_ADDR_V4MAPPED(&src->type.in6));
430*a466cc55SCy Schubert
431*a466cc55SCy Schubert memset(t, 0, sizeof(*t));
432*a466cc55SCy Schubert t->family = AF_INET;
433*a466cc55SCy Schubert memcpy(&t->type.in, (char *)&src->type.in6 + 12, 4);
434*a466cc55SCy Schubert return;
435*a466cc55SCy Schubert }
436