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 */
28 #include <isc/buffer.h>
31 #include <isc/netaddr.h>
32 #include <isc/print.h>
33 #include <isc/sockaddr.h>
34 #include <isc/string.h>
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
)
44 if (a
->zone
!= b
->zone
)
49 if (a
->type
.in
.s_addr
!= b
->type
.in
.s_addr
)
53 if (memcmp(&a
->type
.in6
, &b
->type
.in6
,
54 sizeof(a
->type
.in6
)) != 0 ||
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
)
78 if (a
->zone
!= b
->zone
)
83 pa
= (const unsigned char *) &a
->type
.in
;
84 pb
= (const unsigned char *) &b
->type
.in
;
88 pa
= (const unsigned char *) &a
->type
.in6
;
89 pb
= (const unsigned char *) &b
->type
.in6
;
93 pa
= pb
= NULL
; /* Avoid silly compiler warning. */
94 ipabytes
= 0; /* Ditto. */
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;
108 if (memcmp(pa
, pb
, nbytes
) != 0)
112 unsigned int bytea
, byteb
, mask
;
113 INSIST(nbytes
< ipabytes
);
117 mask
= (0xFF << (8-nbits
)) & 0xFF;
118 if ((bytea
& mask
) != (byteb
& mask
))
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")];
133 REQUIRE(netaddr
!= NULL
);
135 switch (netaddr
->family
) {
137 type
= &netaddr
->type
.in
;
140 type
= &netaddr
->type
.in6
;
143 return (ISC_R_FAILURE
);
145 r
= inet_ntop(netaddr
->family
, type
, abuf
, sizeof(abuf
));
147 return (ISC_R_FAILURE
);
150 INSIST(alen
< sizeof(abuf
));
153 if (netaddr
->family
== AF_INET6
&& netaddr
->zone
!= 0) {
154 zlen
= snprintf(zbuf
, sizeof(zbuf
), "%%%u", netaddr
->zone
);
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
);
170 isc_netaddr_format(const isc_netaddr_t
*na
, char *array
, unsigned int size
) {
174 isc_buffer_init(&buf
, array
, size
);
175 result
= isc_netaddr_totext(na
, &buf
);
180 if (result
== ISC_R_SUCCESS
) {
181 if (isc_buffer_availablelength(&buf
) >= 1)
182 isc_buffer_putuint8(&buf
, 0);
184 result
= ISC_R_NOSPACE
;
187 if (result
!= ISC_R_SUCCESS
) {
188 snprintf(array
, size
,
189 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_NETADDR
,
191 "<unknown address, family %u>"),
193 array
[size
- 1] = '\0';
198 isc_netaddr_masktoprefixlen(const isc_netaddr_t
*s
, unsigned int *lenp
) {
199 unsigned int nbits
, nbytes
, ipbytes
, i
;
200 const unsigned char *p
;
204 p
= (const unsigned char *) &s
->type
.in
;
208 p
= (const unsigned char *) &s
->type
.in6
;
213 return (ISC_R_NOTIMPLEMENTED
);
216 for (i
= 0; i
< ipbytes
; i
++) {
222 unsigned int c
= p
[nbytes
];
223 while ((c
& 0x80) != 0 && nbits
< 8) {
227 return (ISC_R_MASKNONCONTIG
);
230 for (; i
< ipbytes
; i
++) {
232 return (ISC_R_MASKNONCONTIG
);
235 *lenp
= nbytes
* 8 + nbits
;
236 return (ISC_R_SUCCESS
);
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
;
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
;
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
;
262 isc_netaddr_getzone(const isc_netaddr_t
*netaddr
) {
263 return (netaddr
->zone
);
267 isc_netaddr_fromsockaddr(isc_netaddr_t
*t
, const isc_sockaddr_t
*s
) {
268 int family
= s
->type
.sa
.sa_family
;
272 t
->type
.in
= s
->type
.sin
.sin_addr
;
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
;
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
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
;
305 isc_netaddr_ismulticast(isc_netaddr_t
*na
) {
306 switch (na
->family
) {
308 return (ISC_TF(ISC_IPADDR_ISMULTICAST(na
->type
.in
.s_addr
)));
310 return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na
->type
.in6
)));
312 return (ISC_FALSE
); /* XXXMLG ? */
317 isc_netaddr_isexperimental(isc_netaddr_t
*na
) {
318 switch (na
->family
) {
320 return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na
->type
.in
.s_addr
)));
322 return (ISC_FALSE
); /* XXXMLG ? */
327 isc_netaddr_islinklocal(isc_netaddr_t
*na
) {
328 switch (na
->family
) {
332 return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na
->type
.in6
)));
339 isc_netaddr_issitelocal(isc_netaddr_t
*na
) {
340 switch (na
->family
) {
344 return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na
->type
.in6
)));
350 #ifdef ISC_PLATFORM_HAVEIPV6
352 isc_netaddr_fromv4mapped(isc_netaddr_t
*t
, const isc_netaddr_t
*s
) {
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
));
362 memcpy(&t
->type
.in
, (char *)&src
->type
.in6
+ 12, 4);