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