1*3e6fe87eSDamien Miller /*	$OpenBSD: inet_ntop.c,v 1.8 2008/12/09 19:38:38 otto Exp $	*/
2e991e3cfSTim Rice 
3e991e3cfSTim Rice /* Copyright (c) 1996 by Internet Software Consortium.
4e991e3cfSTim Rice  *
5e991e3cfSTim Rice  * Permission to use, copy, modify, and distribute this software for any
6e991e3cfSTim Rice  * purpose with or without fee is hereby granted, provided that the above
7e991e3cfSTim Rice  * copyright notice and this permission notice appear in all copies.
8e991e3cfSTim Rice  *
9e991e3cfSTim Rice  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10e991e3cfSTim Rice  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11e991e3cfSTim Rice  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12e991e3cfSTim Rice  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13e991e3cfSTim Rice  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14e991e3cfSTim Rice  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15e991e3cfSTim Rice  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16e991e3cfSTim Rice  * SOFTWARE.
17e991e3cfSTim Rice  */
18e991e3cfSTim Rice 
197f24a0e6SDarren Tucker /* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */
207f24a0e6SDarren Tucker 
21dd21fe9cSBen Lindstrom #include "includes.h"
22e991e3cfSTim Rice 
23e991e3cfSTim Rice #ifndef HAVE_INET_NTOP
24e991e3cfSTim Rice 
25e991e3cfSTim Rice #include <sys/param.h>
26e991e3cfSTim Rice #include <sys/types.h>
27e991e3cfSTim Rice #include <sys/socket.h>
28e991e3cfSTim Rice #include <netinet/in.h>
29e991e3cfSTim Rice #include <arpa/inet.h>
30e991e3cfSTim Rice #include <arpa/nameser.h>
31e991e3cfSTim Rice #include <string.h>
32e991e3cfSTim Rice #include <errno.h>
33e991e3cfSTim Rice #include <stdio.h>
34e991e3cfSTim Rice 
35e991e3cfSTim Rice #ifndef IN6ADDRSZ
36e991e3cfSTim Rice #define IN6ADDRSZ   16   /* IPv6 T_AAAA */
37e991e3cfSTim Rice #endif
38e991e3cfSTim Rice 
39e991e3cfSTim Rice #ifndef INT16SZ
40e991e3cfSTim Rice #define INT16SZ     2    /* for systems without 16-bit ints */
41e991e3cfSTim Rice #endif
42e991e3cfSTim Rice 
43e991e3cfSTim Rice /*
44e991e3cfSTim Rice  * WARNING: Don't even consider trying to compile this on a system where
45e991e3cfSTim Rice  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
46e991e3cfSTim Rice  */
47e991e3cfSTim Rice 
4871eb0c15SDamien Miller static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
4971eb0c15SDamien Miller static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
50e991e3cfSTim Rice 
51e991e3cfSTim Rice /* char *
52e991e3cfSTim Rice  * inet_ntop(af, src, dst, size)
53e991e3cfSTim Rice  *	convert a network format address to presentation format.
54e991e3cfSTim Rice  * return:
55e991e3cfSTim Rice  *	pointer to presentation format address (`dst'), or NULL (see errno).
56e991e3cfSTim Rice  * author:
57e991e3cfSTim Rice  *	Paul Vixie, 1996.
58e991e3cfSTim Rice  */
59e991e3cfSTim Rice const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)60*3e6fe87eSDamien Miller inet_ntop(int af, const void *src, char *dst, socklen_t size)
61e991e3cfSTim Rice {
62e991e3cfSTim Rice 	switch (af) {
63e991e3cfSTim Rice 	case AF_INET:
64*3e6fe87eSDamien Miller 		return (inet_ntop4(src, dst, (size_t)size));
65e991e3cfSTim Rice 	case AF_INET6:
66*3e6fe87eSDamien Miller 		return (inet_ntop6(src, dst, (size_t)size));
67e991e3cfSTim Rice 	default:
68e991e3cfSTim Rice 		errno = EAFNOSUPPORT;
69e991e3cfSTim Rice 		return (NULL);
70e991e3cfSTim Rice 	}
71e991e3cfSTim Rice 	/* NOTREACHED */
72e991e3cfSTim Rice }
73e991e3cfSTim Rice 
74e991e3cfSTim Rice /* const char *
75e991e3cfSTim Rice  * inet_ntop4(src, dst, size)
76e991e3cfSTim Rice  *	format an IPv4 address, more or less like inet_ntoa()
77e991e3cfSTim Rice  * return:
78e991e3cfSTim Rice  *	`dst' (as a const)
79e991e3cfSTim Rice  * notes:
80e991e3cfSTim Rice  *	(1) uses no statics
81e991e3cfSTim Rice  *	(2) takes a u_char* not an in_addr as input
82e991e3cfSTim Rice  * author:
83e991e3cfSTim Rice  *	Paul Vixie, 1996.
84e991e3cfSTim Rice  */
85e991e3cfSTim Rice static const char *
inet_ntop4(const u_char * src,char * dst,size_t size)862864039aSDarren Tucker inet_ntop4(const u_char *src, char *dst, size_t size)
87e991e3cfSTim Rice {
88e991e3cfSTim Rice 	static const char fmt[] = "%u.%u.%u.%u";
89e991e3cfSTim Rice 	char tmp[sizeof "255.255.255.255"];
9071eb0c15SDamien Miller 	int l;
91e991e3cfSTim Rice 
9271eb0c15SDamien Miller 	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
9371eb0c15SDamien Miller 	if (l <= 0 || l >= size) {
94e991e3cfSTim Rice 		errno = ENOSPC;
95e991e3cfSTim Rice 		return (NULL);
96e991e3cfSTim Rice 	}
9771eb0c15SDamien Miller 	strlcpy(dst, tmp, size);
98e991e3cfSTim Rice 	return (dst);
99e991e3cfSTim Rice }
100e991e3cfSTim Rice 
101e991e3cfSTim Rice /* const char *
102e991e3cfSTim Rice  * inet_ntop6(src, dst, size)
103e991e3cfSTim Rice  *	convert IPv6 binary address into presentation (printable) format
104e991e3cfSTim Rice  * author:
105e991e3cfSTim Rice  *	Paul Vixie, 1996.
106e991e3cfSTim Rice  */
107e991e3cfSTim Rice static const char *
inet_ntop6(const u_char * src,char * dst,size_t size)1082864039aSDarren Tucker inet_ntop6(const u_char *src, char *dst, size_t size)
109e991e3cfSTim Rice {
110e991e3cfSTim Rice 	/*
111e991e3cfSTim Rice 	 * Note that int32_t and int16_t need only be "at least" large enough
112e991e3cfSTim Rice 	 * to contain a value of the specified size.  On some systems, like
113e991e3cfSTim Rice 	 * Crays, there is no such thing as an integer variable with 16 bits.
114e991e3cfSTim Rice 	 * Keep this in mind if you think this function should have been coded
115e991e3cfSTim Rice 	 * to use pointer overlays.  All the world's not a VAX.
116e991e3cfSTim Rice 	 */
11771eb0c15SDamien Miller 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
11871eb0c15SDamien Miller 	char *tp, *ep;
119e991e3cfSTim Rice 	struct { int base, len; } best, cur;
120e991e3cfSTim Rice 	u_int words[IN6ADDRSZ / INT16SZ];
121e991e3cfSTim Rice 	int i;
12271eb0c15SDamien Miller 	int advance;
123e991e3cfSTim Rice 
124e991e3cfSTim Rice 	/*
125e991e3cfSTim Rice 	 * Preprocess:
126e991e3cfSTim Rice 	 *	Copy the input (bytewise) array into a wordwise array.
127e991e3cfSTim Rice 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
128e991e3cfSTim Rice 	 */
129e991e3cfSTim Rice 	memset(words, '\0', sizeof words);
130e991e3cfSTim Rice 	for (i = 0; i < IN6ADDRSZ; i++)
131e991e3cfSTim Rice 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
132e991e3cfSTim Rice 	best.base = -1;
133e991e3cfSTim Rice 	cur.base = -1;
134e991e3cfSTim Rice 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
135e991e3cfSTim Rice 		if (words[i] == 0) {
136e991e3cfSTim Rice 			if (cur.base == -1)
137e991e3cfSTim Rice 				cur.base = i, cur.len = 1;
138e991e3cfSTim Rice 			else
139e991e3cfSTim Rice 				cur.len++;
140e991e3cfSTim Rice 		} else {
141e991e3cfSTim Rice 			if (cur.base != -1) {
142e991e3cfSTim Rice 				if (best.base == -1 || cur.len > best.len)
143e991e3cfSTim Rice 					best = cur;
144e991e3cfSTim Rice 				cur.base = -1;
145e991e3cfSTim Rice 			}
146e991e3cfSTim Rice 		}
147e991e3cfSTim Rice 	}
148e991e3cfSTim Rice 	if (cur.base != -1) {
149e991e3cfSTim Rice 		if (best.base == -1 || cur.len > best.len)
150e991e3cfSTim Rice 			best = cur;
151e991e3cfSTim Rice 	}
152e991e3cfSTim Rice 	if (best.base != -1 && best.len < 2)
153e991e3cfSTim Rice 		best.base = -1;
154e991e3cfSTim Rice 
155e991e3cfSTim Rice 	/*
156e991e3cfSTim Rice 	 * Format the result.
157e991e3cfSTim Rice 	 */
158e991e3cfSTim Rice 	tp = tmp;
15971eb0c15SDamien Miller 	ep = tmp + sizeof(tmp);
16071eb0c15SDamien Miller 	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
161e991e3cfSTim Rice 		/* Are we inside the best run of 0x00's? */
162e991e3cfSTim Rice 		if (best.base != -1 && i >= best.base &&
163e991e3cfSTim Rice 		    i < (best.base + best.len)) {
16471eb0c15SDamien Miller 			if (i == best.base) {
16571eb0c15SDamien Miller 				if (tp + 1 >= ep)
16671eb0c15SDamien Miller 					return (NULL);
167e991e3cfSTim Rice 				*tp++ = ':';
16871eb0c15SDamien Miller 			}
169e991e3cfSTim Rice 			continue;
170e991e3cfSTim Rice 		}
171e991e3cfSTim Rice 		/* Are we following an initial run of 0x00s or any real hex? */
17271eb0c15SDamien Miller 		if (i != 0) {
17371eb0c15SDamien Miller 			if (tp + 1 >= ep)
17471eb0c15SDamien Miller 				return (NULL);
175e991e3cfSTim Rice 			*tp++ = ':';
17671eb0c15SDamien Miller 		}
177e991e3cfSTim Rice 		/* Is this address an encapsulated IPv4? */
178e991e3cfSTim Rice 		if (i == 6 && best.base == 0 &&
179e991e3cfSTim Rice 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
18071eb0c15SDamien Miller 			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
181e991e3cfSTim Rice 				return (NULL);
182e991e3cfSTim Rice 			tp += strlen(tp);
183e991e3cfSTim Rice 			break;
184e991e3cfSTim Rice 		}
18571eb0c15SDamien Miller 		advance = snprintf(tp, ep - tp, "%x", words[i]);
18671eb0c15SDamien Miller 		if (advance <= 0 || advance >= ep - tp)
18771eb0c15SDamien Miller 			return (NULL);
18871eb0c15SDamien Miller 		tp += advance;
189e991e3cfSTim Rice 	}
190e991e3cfSTim Rice 	/* Was it a trailing run of 0x00's? */
19171eb0c15SDamien Miller 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
19271eb0c15SDamien Miller 		if (tp + 1 >= ep)
19371eb0c15SDamien Miller 			return (NULL);
194e991e3cfSTim Rice 		*tp++ = ':';
19571eb0c15SDamien Miller 	}
19671eb0c15SDamien Miller 	if (tp + 1 >= ep)
19771eb0c15SDamien Miller 		return (NULL);
198e991e3cfSTim Rice 	*tp++ = '\0';
199e991e3cfSTim Rice 
200e991e3cfSTim Rice 	/*
201e991e3cfSTim Rice 	 * Check for overflow, copy, and we're done.
202e991e3cfSTim Rice 	 */
203e991e3cfSTim Rice 	if ((size_t)(tp - tmp) > size) {
204e991e3cfSTim Rice 		errno = ENOSPC;
205e991e3cfSTim Rice 		return (NULL);
206e991e3cfSTim Rice 	}
20771eb0c15SDamien Miller 	strlcpy(dst, tmp, size);
208e991e3cfSTim Rice 	return (dst);
209e991e3cfSTim Rice }
210e991e3cfSTim Rice 
211e991e3cfSTim Rice #endif /* !HAVE_INET_NTOP */
212