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 $ */
26 #include <isc/buffer.h>
29 #include <isc/netaddr.h>
30 #include <isc/print.h>
31 #include <isc/sockaddr.h>
32 #include <isc/string.h>
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
)
42 if (a
->zone
!= b
->zone
)
47 if (a
->type
.in
.s_addr
!= b
->type
.in
.s_addr
)
51 if (memcmp(&a
->type
.in6
, &b
->type
.in6
,
52 sizeof(a
->type
.in6
)) != 0 ||
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
)
76 if (a
->zone
!= b
->zone
)
81 pa
= (const unsigned char *) &a
->type
.in
;
82 pb
= (const unsigned char *) &b
->type
.in
;
86 pa
= (const unsigned char *) &a
->type
.in6
;
87 pb
= (const unsigned char *) &b
->type
.in6
;
91 pa
= pb
= NULL
; /* Avoid silly compiler warning. */
92 ipabytes
= 0; /* Ditto. */
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;
106 if (memcmp(pa
, pb
, nbytes
) != 0)
110 unsigned int bytea
, byteb
, mask
;
111 INSIST(nbytes
< ipabytes
);
115 mask
= (0xFF << (8-nbits
)) & 0xFF;
116 if ((bytea
& mask
) != (byteb
& mask
))
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")];
131 REQUIRE(netaddr
!= NULL
);
133 switch (netaddr
->family
) {
135 type
= &netaddr
->type
.in
;
138 type
= &netaddr
->type
.in6
;
141 return (ISC_R_FAILURE
);
143 r
= inet_ntop(netaddr
->family
, type
, abuf
, sizeof(abuf
));
145 return (ISC_R_FAILURE
);
148 INSIST(alen
< sizeof(abuf
));
151 if (netaddr
->family
== AF_INET6
&& netaddr
->zone
!= 0) {
152 zlen
= snprintf(zbuf
, sizeof(zbuf
), "%%%u", netaddr
->zone
);
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
);
168 isc_netaddr_format(const isc_netaddr_t
*na
, char *array
, unsigned int size
) {
172 isc_buffer_init(&buf
, array
, size
);
173 result
= isc_netaddr_totext(na
, &buf
);
178 if (result
== ISC_R_SUCCESS
) {
179 if (isc_buffer_availablelength(&buf
) >= 1)
180 isc_buffer_putuint8(&buf
, 0);
182 result
= ISC_R_NOSPACE
;
185 if (result
!= ISC_R_SUCCESS
) {
186 snprintf(array
, size
,
187 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_NETADDR
,
189 "<unknown address, family %u>"),
191 array
[size
- 1] = '\0';
196 isc_netaddr_masktoprefixlen(const isc_netaddr_t
*s
, unsigned int *lenp
) {
197 unsigned int nbits
, nbytes
, ipbytes
, i
;
198 const unsigned char *p
;
202 p
= (const unsigned char *) &s
->type
.in
;
206 p
= (const unsigned char *) &s
->type
.in6
;
211 return (ISC_R_NOTIMPLEMENTED
);
214 for (i
= 0; i
< ipbytes
; i
++) {
220 unsigned int c
= p
[nbytes
];
221 while ((c
& 0x80) != 0 && nbits
< 8) {
225 return (ISC_R_MASKNONCONTIG
);
228 for (; i
< ipbytes
; i
++) {
230 return (ISC_R_MASKNONCONTIG
);
233 *lenp
= nbytes
* 8 + nbits
;
234 return (ISC_R_SUCCESS
);
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
;
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
;
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
;
260 isc_netaddr_getzone(const isc_netaddr_t
*netaddr
) {
261 return (netaddr
->zone
);
265 isc_netaddr_fromsockaddr(isc_netaddr_t
*t
, const isc_sockaddr_t
*s
) {
266 int family
= s
->type
.sa
.sa_family
;
270 t
->type
.in
= s
->type
.sin
.sin_addr
;
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
;
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
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
;
303 isc_netaddr_ismulticast(isc_netaddr_t
*na
) {
304 switch (na
->family
) {
306 return (ISC_TF(ISC_IPADDR_ISMULTICAST(na
->type
.in
.s_addr
)));
308 return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na
->type
.in6
)));
310 return (ISC_FALSE
); /* XXXMLG ? */
315 isc_netaddr_isexperimental(isc_netaddr_t
*na
) {
316 switch (na
->family
) {
318 return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na
->type
.in
.s_addr
)));
320 return (ISC_FALSE
); /* XXXMLG ? */
325 isc_netaddr_islinklocal(isc_netaddr_t
*na
) {
326 switch (na
->family
) {
330 return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na
->type
.in6
)));
337 isc_netaddr_issitelocal(isc_netaddr_t
*na
) {
338 switch (na
->family
) {
342 return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na
->type
.in6
)));
348 #ifdef ISC_PLATFORM_HAVEIPV6
350 isc_netaddr_fromv4mapped(isc_netaddr_t
*t
, const isc_netaddr_t
*s
) {
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
));
360 memcpy(&t
->type
.in
, (char *)&src
->type
.in6
+ 12, 4);