4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1998,1999 by 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
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: inet_cidr_ntop.c,v 1.7 2006/10/11 02:18:18 marka Exp";
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 #include <arpa/inet.h>
37 #include "port_after.h"
40 # define SPRINTF(x) strlen(sprintf/**/x)
42 # define SPRINTF(x) ((size_t)sprintf x)
46 inet_cidr_ntop_ipv4(const u_char
*src
, int bits
, char *dst
, size_t size
);
48 inet_cidr_ntop_ipv6(const u_char
*src
, int bits
, char *dst
, size_t size
);
52 * inet_cidr_ntop(af, src, bits, dst, size)
53 * convert network address from network to presentation format.
54 * "src"'s size is determined from its "af".
56 * pointer to dst, or NULL if an error occurred (check errno).
58 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
59 * as called for by inet_net_ntop() but it can be a host address with
60 * an included netmask.
62 * Paul Vixie (ISC), October 1998
65 inet_cidr_ntop(int af
, const void *src
, int bits
, char *dst
, size_t size
) {
68 return (inet_cidr_ntop_ipv4(src
, bits
, dst
, size
));
70 return (inet_cidr_ntop_ipv6(src
, bits
, dst
, size
));
78 decoct(const u_char
*src
, int bytes
, char *dst
, size_t size
) {
83 for (b
= 1; b
<= bytes
; b
++) {
84 if (size
< sizeof "255.")
87 dst
+= SPRINTF((dst
, "%u", *src
++));
92 size
-= (size_t)(dst
- t
);
99 * inet_cidr_ntop_ipv4(src, bits, dst, size)
100 * convert IPv4 network address from network to presentation format.
101 * "src"'s size is determined from its "af".
103 * pointer to dst, or NULL if an error occurred (check errno).
105 * network byte order assumed. this means 192.5.5.240/28 has
106 * 0b11110000 in its fourth octet.
108 * Paul Vixie (ISC), October 1998
111 inet_cidr_ntop_ipv4(const u_char
*src
, int bits
, char *dst
, size_t size
) {
117 if ((bits
< -1) || (bits
> 32)) {
122 /* Find number of significant bytes in address. */
126 for (len
= 1, b
= 1 ; b
< 4U; b
++)
130 /* Format whole octets plus nonzero trailing octets. */
131 bytes
= (((bits
<= 0) ? 1 : bits
) + 7) / 8;
134 b
= decoct(src
, bytes
, dst
, size
);
141 /* Format CIDR /width. */
142 if (size
< sizeof "/32")
144 dst
+= SPRINTF((dst
, "/%u", bits
));
155 inet_cidr_ntop_ipv6(const u_char
*src
, int bits
, char *dst
, size_t size
) {
157 * Note that int32_t and int16_t need only be "at least" large enough
158 * to contain a value of the specified size. On some systems, like
159 * Crays, there is no such thing as an integer variable with 16 bits.
160 * Keep this in mind if you think this function should have been coded
161 * to use pointer overlays. All the world's not a VAX.
163 char tmp
[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
165 struct { int base
, len
; } best
, cur
;
166 u_int words
[NS_IN6ADDRSZ
/ NS_INT16SZ
];
169 if ((bits
< -1) || (bits
> 128)) {
176 * Copy the input (bytewise) array into a wordwise array.
177 * Find the longest run of 0x00's in src[] for :: shorthanding.
179 memset(words
, '\0', sizeof words
);
180 for (i
= 0; i
< NS_IN6ADDRSZ
; i
++)
181 words
[i
/ 2] |= (src
[i
] << ((1 - (i
% 2)) << 3));
186 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
189 cur
.base
= i
, cur
.len
= 1;
193 if (cur
.base
!= -1) {
194 if (best
.base
== -1 || cur
.len
> best
.len
)
200 if (cur
.base
!= -1) {
201 if (best
.base
== -1 || cur
.len
> best
.len
)
204 if (best
.base
!= -1 && best
.len
< 2)
211 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
212 /* Are we inside the best run of 0x00's? */
213 if (best
.base
!= -1 && i
>= best
.base
&&
214 i
< (best
.base
+ best
.len
)) {
219 /* Are we following an initial run of 0x00s or any real hex? */
222 /* Is this address an encapsulated IPv4? */
223 if (i
== 6 && best
.base
== 0 && (best
.len
== 6 ||
224 (best
.len
== 7 && words
[7] != 0x0001) ||
225 (best
.len
== 5 && words
[5] == 0xffff))) {
228 if (src
[15] || bits
== -1 || bits
> 120)
230 else if (src
[14] || bits
> 112)
234 n
= decoct(src
+12, n
, tp
, sizeof tmp
- (tp
- tmp
));
242 tp
+= SPRINTF((tp
, "%x", words
[i
]));
245 /* Was it a trailing run of 0x00's? */
246 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
247 (NS_IN6ADDRSZ
/ NS_INT16SZ
))
252 tp
+= SPRINTF((tp
, "/%u", bits
));
255 * Check for overflow, copy, and we're done.
257 if ((size_t)(tp
- tmp
) > size
) {