2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 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.2.2 2004/03/09 06:11:49 marka Exp $ */
24 #include <isc/buffer.h>
27 #include <isc/netaddr.h>
28 #include <isc/print.h>
29 #include <isc/sockaddr.h>
30 #include <isc/string.h>
34 isc_netaddr_equal(const isc_netaddr_t
*a
, const isc_netaddr_t
*b
) {
35 REQUIRE(a
!= NULL
&& b
!= NULL
);
37 if (a
->family
!= b
->family
)
42 if (a
->type
.in
.s_addr
!= b
->type
.in
.s_addr
)
46 if (memcmp(&a
->type
.in6
, &b
->type
.in6
, sizeof a
->type
.in6
)
57 isc_netaddr_eqprefix(const isc_netaddr_t
*a
, const isc_netaddr_t
*b
,
58 unsigned int prefixlen
)
60 const unsigned char *pa
, *pb
;
61 unsigned int ipabytes
; /* Length of whole IP address in bytes */
62 unsigned int nbytes
; /* Number of significant whole bytes */
63 unsigned int nbits
; /* Number of significant leftover bits */
65 REQUIRE(a
!= NULL
&& b
!= NULL
);
67 if (a
->family
!= b
->family
)
72 pa
= (const unsigned char *) &a
->type
.in
;
73 pb
= (const unsigned char *) &b
->type
.in
;
77 pa
= (const unsigned char *) &a
->type
.in6
;
78 pb
= (const unsigned char *) &b
->type
.in6
;
82 pa
= pb
= NULL
; /* Avoid silly compiler warning. */
83 ipabytes
= 0; /* Ditto. */
88 * Don't crash if we get a pattern like 10.0.0.1/9999999.
90 if (prefixlen
> ipabytes
* 8)
91 prefixlen
= ipabytes
* 8;
93 nbytes
= prefixlen
/ 8;
94 nbits
= prefixlen
% 8;
97 if (memcmp(pa
, pb
, nbytes
) != 0)
101 unsigned int bytea
, byteb
, mask
;
102 INSIST(nbytes
< ipabytes
);
106 mask
= (0xFF << (8-nbits
)) & 0xFF;
107 if ((bytea
& mask
) != (byteb
& mask
))
114 isc_netaddr_totext(const isc_netaddr_t
*netaddr
, isc_buffer_t
*target
) {
115 char abuf
[sizeof "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255"];
119 REQUIRE(netaddr
!= NULL
);
121 r
= inet_ntop(netaddr
->family
, &netaddr
->type
, abuf
, sizeof abuf
);
123 return (ISC_R_FAILURE
);
126 INSIST(alen
< sizeof(abuf
));
128 if (alen
> isc_buffer_availablelength(target
))
129 return (ISC_R_NOSPACE
);
131 isc_buffer_putmem(target
, (unsigned char *)abuf
, alen
);
133 return (ISC_R_SUCCESS
);
137 isc_netaddr_format(isc_netaddr_t
*na
, char *array
, unsigned int size
) {
141 isc_buffer_init(&buf
, array
, size
);
142 result
= isc_netaddr_totext(na
, &buf
);
147 if (result
== ISC_R_SUCCESS
) {
148 if (isc_buffer_availablelength(&buf
) >= 1)
149 isc_buffer_putuint8(&buf
, 0);
151 result
= ISC_R_NOSPACE
;
154 if (result
!= ISC_R_SUCCESS
) {
155 snprintf(array
, size
,
156 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_NETADDR
,
158 "<unknown address, family %u>"),
160 array
[size
- 1] = '\0';
165 isc_netaddr_masktoprefixlen(const isc_netaddr_t
*s
, unsigned int *lenp
) {
166 unsigned int nbits
, nbytes
, ipbytes
, i
;
167 const unsigned char *p
;
171 p
= (const unsigned char *) &s
->type
.in
;
175 p
= (const unsigned char *) &s
->type
.in6
;
180 return (ISC_R_NOTIMPLEMENTED
);
183 for (i
= 0; i
< ipbytes
; i
++) {
189 unsigned int c
= p
[nbytes
];
190 while ((c
& 0x80) != 0 && nbits
< 8) {
194 return (ISC_R_MASKNONCONTIG
);
197 for (; i
< ipbytes
; i
++) {
199 return (ISC_R_MASKNONCONTIG
);
202 *lenp
= nbytes
* 8 + nbits
;
203 return (ISC_R_SUCCESS
);
207 isc_netaddr_fromin(isc_netaddr_t
*netaddr
, const struct in_addr
*ina
) {
208 memset(netaddr
, 0, sizeof *netaddr
);
209 netaddr
->family
= AF_INET
;
210 netaddr
->type
.in
= *ina
;
214 isc_netaddr_fromin6(isc_netaddr_t
*netaddr
, const struct in6_addr
*ina6
) {
215 memset(netaddr
, 0, sizeof *netaddr
);
216 netaddr
->family
= AF_INET6
;
217 netaddr
->type
.in6
= *ina6
;
221 isc_netaddr_fromsockaddr(isc_netaddr_t
*t
, const isc_sockaddr_t
*s
) {
222 int family
= s
->type
.sa
.sa_family
;
226 t
->type
.in
= s
->type
.sin
.sin_addr
;
229 memcpy(&t
->type
.in6
, &s
->type
.sin6
.sin6_addr
, 16);
237 isc_netaddr_any(isc_netaddr_t
*netaddr
) {
238 memset(netaddr
, 0, sizeof *netaddr
);
239 netaddr
->family
= AF_INET
;
240 netaddr
->type
.in
.s_addr
= INADDR_ANY
;
244 isc_netaddr_any6(isc_netaddr_t
*netaddr
) {
245 memset(netaddr
, 0, sizeof *netaddr
);
246 netaddr
->family
= AF_INET6
;
247 netaddr
->type
.in6
= in6addr_any
;
251 isc_netaddr_ismulticast(isc_netaddr_t
*na
) {
252 switch (na
->family
) {
254 return (ISC_TF(ISC_IPADDR_ISMULTICAST(na
->type
.in
.s_addr
)));
256 return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na
->type
.in6
)));
258 return (ISC_FALSE
); /* XXXMLG ? */
263 isc_netaddr_isexperimental(isc_netaddr_t
*na
) {
264 switch (na
->family
) {
266 return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na
->type
.in
.s_addr
)));
268 return (ISC_FALSE
); /* XXXMLG ? */
273 isc_netaddr_fromv4mapped(isc_netaddr_t
*t
, const isc_netaddr_t
*s
) {
276 DE_CONST(s
, src
); /* Must come before IN6_IS_ADDR_V4MAPPED. */
278 REQUIRE(s
->family
== AF_INET6
);
279 REQUIRE(IN6_IS_ADDR_V4MAPPED(&src
->type
.in6
));
281 memset(t
, 0, sizeof(*t
));
283 memcpy(&t
->type
.in
, (char *)&src
->type
.in6
+ 12, 4);