2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,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_net_ntop.c,v 1.5 2006/06/20 02:50:14 marka Exp $";
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
38 #include "port_after.h"
41 # define SPRINTF(x) strlen(sprintf/**/x)
43 # define SPRINTF(x) ((size_t)sprintf x)
46 static char * inet_net_ntop_ipv4(const u_char *src, int bits, char *dst,
48 static char * inet_net_ntop_ipv6(const u_char *src, int bits, char *dst,
53 * inet_net_ntop(af, src, bits, dst, size)
54 * convert network number from network to presentation format.
55 * generates CIDR style result always.
57 * pointer to dst, or NULL if an error occurred (check errno).
59 * Paul Vixie (ISC), July 1996
62 inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
66 return (inet_net_ntop_ipv4(src, bits, dst, size));
68 return (inet_net_ntop_ipv6(src, bits, dst, size));
77 * inet_net_ntop_ipv4(src, bits, dst, size)
78 * convert IPv4 network number from network to presentation format.
79 * generates CIDR style result always.
81 * pointer to dst, or NULL if an error occurred (check errno).
83 * network byte order assumed. this means 192.5.5.240/28 has
84 * 0b11110000 in its fourth octet.
86 * Paul Vixie (ISC), July 1996
89 inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
96 if (bits < 0 || bits > 32) {
102 if (size < sizeof "0")
109 /* Format whole octets. */
110 for (b = bits / 8; b > 0; b--) {
111 if (size <= sizeof "255.")
114 dst += SPRINTF((dst, "%u", *src++));
119 size -= (size_t)(dst - t);
122 /* Format partial octet. */
125 if (size <= sizeof ".255")
130 m = ((1 << b) - 1) << (8 - b);
131 dst += SPRINTF((dst, "%u", *src & m));
132 size -= (size_t)(dst - t);
135 /* Format CIDR /width. */
136 if (size <= sizeof "/32")
138 dst += SPRINTF((dst, "/%u", bits));
148 * inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
149 * convert IPv6 network number from network to presentation format.
150 * generates CIDR style result always. Picks the shortest representation
151 * unless the IP is really IPv4.
152 * always prints specified number of bits (bits).
154 * pointer to dst, or NULL if an error occurred (check errno).
156 * network byte order assumed. this means 192.5.5.240/28 has
157 * 0b11110000 in its fourth octet.
159 * Vadim Kogan (UCB), June 2001
160 * Original version (IPv4) by Paul Vixie (ISC), July 1996
164 inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
168 int zero_s, zero_l, tmp_zero_s, tmp_zero_l;
171 unsigned char inbuf[16];
172 char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
177 if (bits < 0 || bits > 128) {
189 /* Copy src to private buffer. Zero host part. */
191 memcpy(inbuf, src, p);
192 memset(inbuf + p, 0, 16 - p);
201 /* how many words need to be displayed in output */
202 words = (bits + 15) / 16;
206 /* Find the longest substring of zero's */
207 zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
208 for (i = 0; i < (words * 2); i += 2) {
209 if ((s[i] | s[i+1]) == 0) {
214 if (tmp_zero_l && zero_l < tmp_zero_l) {
222 if (tmp_zero_l && zero_l < tmp_zero_l) {
227 if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
228 ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
229 ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
232 /* Format whole words. */
233 for (p = 0; p < words; p++) {
234 if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) {
235 /* Time to skip some zeros */
245 if (is_ipv4 && p > 5 ) {
246 *cp++ = (p == 6) ? ':' : '.';
247 cp += SPRINTF((cp, "%u", *s++));
248 /* we can potentially drop the last octet */
249 if (p != 7 || bits > 120) {
251 cp += SPRINTF((cp, "%u", *s++));
256 cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
261 /* Format CIDR /width. */
262 sprintf(cp, "/%u", bits);
263 if (strlen(outbuf) + 1 > size)
275 * Weak aliases for applications that use certain private entry points,
276 * and fail to include <arpa/inet.h>.
279 __weak_reference(__inet_net_ntop, inet_net_ntop);