2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1998,1999 by 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
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: inet_cidr_ntop.c,v 1.4.18.3 2006/10/11 02:32:47 marka Exp $";
22 #include "port_before.h"
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <arpa/inet.h>
35 #include "port_after.h"
38 # define SPRINTF(x) strlen(sprintf/**/x)
40 # define SPRINTF(x) ((size_t)sprintf x)
44 inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size);
46 inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size);
50 * inet_cidr_ntop(af, src, bits, dst, size)
51 * convert network address from network to presentation format.
52 * "src"'s size is determined from its "af".
54 * pointer to dst, or NULL if an error occurred (check errno).
56 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
57 * as called for by inet_net_ntop() but it can be a host address with
58 * an included netmask.
60 * Paul Vixie (ISC), October 1998
63 inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) {
66 return (inet_cidr_ntop_ipv4(src, bits, dst, size));
68 return (inet_cidr_ntop_ipv6(src, bits, dst, size));
76 decoct(const u_char *src, int bytes, char *dst, size_t size) {
81 for (b = 1; b <= bytes; b++) {
82 if (size < sizeof "255.")
85 dst += SPRINTF((dst, "%u", *src++));
90 size -= (size_t)(dst - t);
97 * inet_cidr_ntop_ipv4(src, bits, dst, size)
98 * convert IPv4 network address from network to presentation format.
99 * "src"'s size is determined from its "af".
101 * pointer to dst, or NULL if an error occurred (check errno).
103 * network byte order assumed. this means 192.5.5.240/28 has
104 * 0b11110000 in its fourth octet.
106 * Paul Vixie (ISC), October 1998
109 inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) {
115 if ((bits < -1) || (bits > 32)) {
120 /* Find number of significant bytes in address. */
124 for (len = 1, b = 1 ; b < 4U; b++)
128 /* Format whole octets plus nonzero trailing octets. */
129 bytes = (((bits <= 0) ? 1 : bits) + 7) / 8;
132 b = decoct(src, bytes, dst, size);
139 /* Format CIDR /width. */
140 if (size < sizeof "/32")
142 dst += SPRINTF((dst, "/%u", bits));
153 inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
155 * Note that int32_t and int16_t need only be "at least" large enough
156 * to contain a value of the specified size. On some systems, like
157 * Crays, there is no such thing as an integer variable with 16 bits.
158 * Keep this in mind if you think this function should have been coded
159 * to use pointer overlays. All the world's not a VAX.
161 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
163 struct { int base, len; } best, cur;
164 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
167 if ((bits < -1) || (bits > 128)) {
174 * Copy the input (bytewise) array into a wordwise array.
175 * Find the longest run of 0x00's in src[] for :: shorthanding.
177 memset(words, '\0', sizeof words);
178 for (i = 0; i < NS_IN6ADDRSZ; i++)
179 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
184 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
187 cur.base = i, cur.len = 1;
191 if (cur.base != -1) {
192 if (best.base == -1 || cur.len > best.len)
198 if (cur.base != -1) {
199 if (best.base == -1 || cur.len > best.len)
202 if (best.base != -1 && best.len < 2)
209 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
210 /* Are we inside the best run of 0x00's? */
211 if (best.base != -1 && i >= best.base &&
212 i < (best.base + best.len)) {
217 /* Are we following an initial run of 0x00s or any real hex? */
220 /* Is this address an encapsulated IPv4? */
221 if (i == 6 && best.base == 0 && (best.len == 6 ||
222 (best.len == 7 && words[7] != 0x0001) ||
223 (best.len == 5 && words[5] == 0xffff))) {
226 if (src[15] || bits == -1 || bits > 120)
228 else if (src[14] || bits > 112)
232 n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp));
240 tp += SPRINTF((tp, "%x", words[i]));
243 /* Was it a trailing run of 0x00's? */
244 if (best.base != -1 && (best.base + best.len) ==
245 (NS_IN6ADDRSZ / NS_INT16SZ))
250 tp += SPRINTF((tp, "/%u", bits));
253 * Check for overflow, copy, and we're done.
255 if ((size_t)(tp - tmp) > size) {