2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,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_net_ntop.c,v 1.5 2006/06/20 02:50:14 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/inet.h>
34 #include "port_after.h"
37 # define SPRINTF(x) strlen(sprintf/**/x)
39 # define SPRINTF(x) ((size_t)sprintf x)
42 static char * inet_net_ntop_ipv4 __P((const u_char *src, int bits,
43 char *dst, size_t size));
44 static char * inet_net_ntop_ipv6 __P((const u_char *src, int bits,
45 char *dst, size_t size));
49 * inet_net_ntop(af, src, bits, dst, size)
50 * convert network number from network to presentation format.
51 * generates CIDR style result always.
53 * pointer to dst, or NULL if an error occurred (check errno).
55 * Paul Vixie (ISC), July 1996
58 inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
62 return (inet_net_ntop_ipv4(src, bits, dst, size));
64 return (inet_net_ntop_ipv6(src, bits, dst, size));
73 * inet_net_ntop_ipv4(src, bits, dst, size)
74 * convert IPv4 network number from network to presentation format.
75 * generates CIDR style result always.
77 * pointer to dst, or NULL if an error occurred (check errno).
79 * network byte order assumed. this means 192.5.5.240/28 has
80 * 0b11110000 in its fourth octet.
82 * Paul Vixie (ISC), July 1996
85 inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
92 if (bits < 0 || bits > 32) {
98 if (size < sizeof "0")
105 /* Format whole octets. */
106 for (b = bits / 8; b > 0; b--) {
107 if (size <= sizeof "255.")
110 dst += SPRINTF((dst, "%u", *src++));
115 size -= (size_t)(dst - t);
118 /* Format partial octet. */
121 if (size <= sizeof ".255")
126 m = ((1 << b) - 1) << (8 - b);
127 dst += SPRINTF((dst, "%u", *src & m));
128 size -= (size_t)(dst - t);
131 /* Format CIDR /width. */
132 if (size <= sizeof "/32")
134 dst += SPRINTF((dst, "/%u", bits));
144 * inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
145 * convert IPv6 network number from network to presentation format.
146 * generates CIDR style result always. Picks the shortest representation
147 * unless the IP is really IPv4.
148 * always prints specified number of bits (bits).
150 * pointer to dst, or NULL if an error occurred (check errno).
152 * network byte order assumed. this means 192.5.5.240/28 has
153 * 0x11110000 in its fourth octet.
155 * Vadim Kogan (UCB), June 2001
156 * Original version (IPv4) by Paul Vixie (ISC), July 1996
160 inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
164 int zero_s, zero_l, tmp_zero_s, tmp_zero_l;
167 unsigned char inbuf[16];
168 char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
173 if (bits < 0 || bits > 128) {
185 /* Copy src to private buffer. Zero host part. */
187 memcpy(inbuf, src, p);
188 memset(inbuf + p, 0, 16 - p);
197 /* how many words need to be displayed in output */
198 words = (bits + 15) / 16;
202 /* Find the longest substring of zero's */
203 zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
204 for (i = 0; i < (words * 2); i += 2) {
205 if ((s[i] | s[i+1]) == 0) {
210 if (tmp_zero_l && zero_l < tmp_zero_l) {
218 if (tmp_zero_l && zero_l < tmp_zero_l) {
223 if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
224 ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
225 ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
228 /* Format whole words. */
229 for (p = 0; p < words; p++) {
230 if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) {
231 /* Time to skip some zeros */
241 if (is_ipv4 && p > 5 ) {
242 *cp++ = (p == 6) ? ':' : '.';
243 cp += SPRINTF((cp, "%u", *s++));
244 /* we can potentially drop the last octet */
245 if (p != 7 || bits > 120) {
247 cp += SPRINTF((cp, "%u", *s++));
252 cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
257 /* Format CIDR /width. */
258 sprintf(cp, "/%u", bits);
259 if (strlen(outbuf) + 1 > size)
272 * Weak aliases for applications that use certain private entry points,
273 * and fail to include <arpa/inet.h>.
276 __weak_reference(__inet_net_ntop, inet_net_ntop);