xref: /freebsd/lib/libc/inet/inet_ntop.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1996-1999 by Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "$Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp $";
22 #endif /* LIBC_SCCS and not lint */
23 #include "port_before.h"
24 
25 #include <sys/param.h>
26 #include <sys/socket.h>
27 
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <arpa/nameser.h>
31 
32 #include <errno.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include "port_after.h"
37 
38 /*%
39  * WARNING: Don't even consider trying to compile this on a system where
40  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
41  */
42 
43 static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
44 static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
45 
46 /* const char *
47  * inet_ntop(af, src, dst, size)
48  *	convert a network format address to presentation format.
49  * return:
50  *	pointer to presentation format address (`dst'), or NULL (see errno).
51  * author:
52  *	Paul Vixie, 1996.
53  */
54 const char *
55 inet_ntop(int af, const void * __restrict src, char * __restrict dst,
56     socklen_t size)
57 {
58 	switch (af) {
59 	case AF_INET:
60 		return (inet_ntop4(src, dst, size));
61 	case AF_INET6:
62 		return (inet_ntop6(src, dst, size));
63 	default:
64 		errno = EAFNOSUPPORT;
65 		return (NULL);
66 	}
67 	/* NOTREACHED */
68 }
69 
70 /* const char *
71  * inet_ntop4(src, dst, size)
72  *	format an IPv4 address
73  * return:
74  *	`dst' (as a const)
75  * notes:
76  *	(1) uses no statics
77  *	(2) takes a u_char* not an in_addr as input
78  * author:
79  *	Paul Vixie, 1996.
80  */
81 static const char *
82 inet_ntop4(const u_char *src, char *dst, socklen_t size)
83 {
84 	static const char fmt[] = "%u.%u.%u.%u";
85 	char tmp[sizeof "255.255.255.255"];
86 	int l;
87 
88 	l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
89 	if (l <= 0 || (socklen_t) l >= size) {
90 		errno = ENOSPC;
91 		return (NULL);
92 	}
93 	strlcpy(dst, tmp, size);
94 	return (dst);
95 }
96 
97 /* const char *
98  * inet_ntop6(src, dst, size)
99  *	convert IPv6 binary address into presentation (printable) format
100  * author:
101  *	Paul Vixie, 1996.
102  */
103 static const char *
104 inet_ntop6(const u_char *src, char *dst, socklen_t size)
105 {
106 	/*
107 	 * Note that int32_t and int16_t need only be "at least" large enough
108 	 * to contain a value of the specified size.  On some systems, like
109 	 * Crays, there is no such thing as an integer variable with 16 bits.
110 	 * Keep this in mind if you think this function should have been coded
111 	 * to use pointer overlays.  All the world's not a VAX.
112 	 */
113 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
114 	struct { int base, len; } best, cur;
115 	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
116 	int i;
117 
118 	/*
119 	 * Preprocess:
120 	 *	Copy the input (bytewise) array into a wordwise array.
121 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
122 	 */
123 	memset(words, '\0', sizeof words);
124 	for (i = 0; i < NS_IN6ADDRSZ; i++)
125 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
126 	best.base = -1;
127 	best.len = 0;
128 	cur.base = -1;
129 	cur.len = 0;
130 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
131 		if (words[i] == 0) {
132 			if (cur.base == -1)
133 				cur.base = i, cur.len = 1;
134 			else
135 				cur.len++;
136 		} else {
137 			if (cur.base != -1) {
138 				if (best.base == -1 || cur.len > best.len)
139 					best = cur;
140 				cur.base = -1;
141 			}
142 		}
143 	}
144 	if (cur.base != -1) {
145 		if (best.base == -1 || cur.len > best.len)
146 			best = cur;
147 	}
148 	if (best.base != -1 && best.len < 2)
149 		best.base = -1;
150 
151 	/*
152 	 * Format the result.
153 	 */
154 	tp = tmp;
155 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
156 		/* Are we inside the best run of 0x00's? */
157 		if (best.base != -1 && i >= best.base &&
158 		    i < (best.base + best.len)) {
159 			if (i == best.base)
160 				*tp++ = ':';
161 			continue;
162 		}
163 		/* Are we following an initial run of 0x00s or any real hex? */
164 		if (i != 0)
165 			*tp++ = ':';
166 		/* Is this address an encapsulated IPv4? */
167 		if (i == 6 && best.base == 0 && (best.len == 6 ||
168 		    (best.len == 7 && words[7] != 0x0001) ||
169 		    (best.len == 5 && words[5] == 0xffff))) {
170 			if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) {
171 				errno = ENOSPC;
172 				return (NULL);
173 			}
174 			tp += strlen(tp);
175 			break;
176 		}
177 		tp += sprintf(tp, "%x", words[i]);
178 	}
179 	/* Was it a trailing run of 0x00's? */
180 	if (best.base != -1 && (best.base + best.len) ==
181 	    (NS_IN6ADDRSZ / NS_INT16SZ))
182 		*tp++ = ':';
183 	*tp++ = '\0';
184 
185 	/*
186 	 * Check for overflow, copy, and we're done.
187 	 */
188 	if ((socklen_t)(tp - tmp) > size) {
189 		errno = ENOSPC;
190 		return (NULL);
191 	}
192 	strcpy(dst, tmp);
193 	return (dst);
194 }
195 
196 /*
197  * Weak aliases for applications that use certain private entry points,
198  * and fail to include <arpa/inet.h>.
199  */
200 #undef inet_ntop
201 __weak_reference(__inet_ntop, inet_ntop);
202 
203 /*! \file */
204