xref: /openbsd/lib/libc/net/inet_ntop.c (revision 3240e6a8)
1*3240e6a8Sguenther /*	$OpenBSD: inet_ntop.c,v 1.13 2016/09/21 04:38:56 guenther Exp $	*/
27949d54bSdownsj 
37949d54bSdownsj /* Copyright (c) 1996 by Internet Software Consortium.
47949d54bSdownsj  *
57949d54bSdownsj  * Permission to use, copy, modify, and distribute this software for any
67949d54bSdownsj  * purpose with or without fee is hereby granted, provided that the above
77949d54bSdownsj  * copyright notice and this permission notice appear in all copies.
87949d54bSdownsj  *
97949d54bSdownsj  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
107949d54bSdownsj  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
117949d54bSdownsj  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
127949d54bSdownsj  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
137949d54bSdownsj  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
147949d54bSdownsj  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
157949d54bSdownsj  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
167949d54bSdownsj  * SOFTWARE.
177949d54bSdownsj  */
187949d54bSdownsj 
197949d54bSdownsj #include <sys/types.h>
207949d54bSdownsj #include <sys/socket.h>
217949d54bSdownsj #include <netinet/in.h>
227949d54bSdownsj #include <arpa/inet.h>
237949d54bSdownsj #include <arpa/nameser.h>
247949d54bSdownsj #include <string.h>
257949d54bSdownsj #include <errno.h>
267949d54bSdownsj #include <stdio.h>
277949d54bSdownsj 
287949d54bSdownsj /*
297949d54bSdownsj  * WARNING: Don't even consider trying to compile this on a system where
307949d54bSdownsj  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
317949d54bSdownsj  */
327949d54bSdownsj 
33c72b5b24Smillert static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
34c72b5b24Smillert static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
357949d54bSdownsj 
3637eb9fdaSmillert /* const char *
377949d54bSdownsj  * inet_ntop(af, src, dst, size)
387949d54bSdownsj  *	convert a network format address to presentation format.
397949d54bSdownsj  * return:
407949d54bSdownsj  *	pointer to presentation format address (`dst'), or NULL (see errno).
417949d54bSdownsj  * author:
427949d54bSdownsj  *	Paul Vixie, 1996.
437949d54bSdownsj  */
447949d54bSdownsj const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)45abf3e769Sotto inet_ntop(int af, const void *src, char *dst, socklen_t size)
467949d54bSdownsj {
477949d54bSdownsj 	switch (af) {
487949d54bSdownsj 	case AF_INET:
49*3240e6a8Sguenther 		return (inet_ntop4(src, dst, size));
507949d54bSdownsj 	case AF_INET6:
51*3240e6a8Sguenther 		return (inet_ntop6(src, dst, size));
527949d54bSdownsj 	default:
537949d54bSdownsj 		errno = EAFNOSUPPORT;
547949d54bSdownsj 		return (NULL);
557949d54bSdownsj 	}
567949d54bSdownsj 	/* NOTREACHED */
577949d54bSdownsj }
58b2d13c95Sguenther DEF_WEAK(inet_ntop);
597949d54bSdownsj 
607949d54bSdownsj /* const char *
617949d54bSdownsj  * inet_ntop4(src, dst, size)
627949d54bSdownsj  *	format an IPv4 address, more or less like inet_ntoa()
637949d54bSdownsj  * return:
647949d54bSdownsj  *	`dst' (as a const)
657949d54bSdownsj  * notes:
667949d54bSdownsj  *	(1) uses no statics
677949d54bSdownsj  *	(2) takes a u_char* not an in_addr as input
687949d54bSdownsj  * author:
697949d54bSdownsj  *	Paul Vixie, 1996.
707949d54bSdownsj  */
717949d54bSdownsj static const char *
inet_ntop4(const u_char * src,char * dst,size_t size)72db5b349cSotto inet_ntop4(const u_char *src, char *dst, size_t size)
737949d54bSdownsj {
747949d54bSdownsj 	char tmp[sizeof "255.255.255.255"];
751868dddfSitojun 	int l;
767949d54bSdownsj 
7748772bb8Stedu 	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
7848772bb8Stedu 	    src[0], src[1], src[2], src[3]);
791868dddfSitojun 	if (l <= 0 || l >= size) {
807949d54bSdownsj 		errno = ENOSPC;
817949d54bSdownsj 		return (NULL);
827949d54bSdownsj 	}
835af14704Sderaadt 	strlcpy(dst, tmp, size);
847949d54bSdownsj 	return (dst);
857949d54bSdownsj }
867949d54bSdownsj 
877949d54bSdownsj /* const char *
887949d54bSdownsj  * inet_ntop6(src, dst, size)
897949d54bSdownsj  *	convert IPv6 binary address into presentation (printable) format
907949d54bSdownsj  * author:
917949d54bSdownsj  *	Paul Vixie, 1996.
927949d54bSdownsj  */
937949d54bSdownsj static const char *
inet_ntop6(const u_char * src,char * dst,size_t size)94db5b349cSotto inet_ntop6(const u_char *src, char *dst, size_t size)
957949d54bSdownsj {
967949d54bSdownsj 	/*
977949d54bSdownsj 	 * Note that int32_t and int16_t need only be "at least" large enough
987949d54bSdownsj 	 * to contain a value of the specified size.  On some systems, like
997949d54bSdownsj 	 * Crays, there is no such thing as an integer variable with 16 bits.
1007949d54bSdownsj 	 * Keep this in mind if you think this function should have been coded
1017949d54bSdownsj 	 * to use pointer overlays.  All the world's not a VAX.
1027949d54bSdownsj 	 */
10373bbab47Sitojun 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
10473bbab47Sitojun 	char *tp, *ep;
1057949d54bSdownsj 	struct { int base, len; } best, cur;
1067949d54bSdownsj 	u_int words[IN6ADDRSZ / INT16SZ];
1077949d54bSdownsj 	int i;
10873bbab47Sitojun 	int advance;
1097949d54bSdownsj 
1107949d54bSdownsj 	/*
1117949d54bSdownsj 	 * Preprocess:
1127949d54bSdownsj 	 *	Copy the input (bytewise) array into a wordwise array.
1137949d54bSdownsj 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
1147949d54bSdownsj 	 */
1157949d54bSdownsj 	memset(words, '\0', sizeof words);
1167949d54bSdownsj 	for (i = 0; i < IN6ADDRSZ; i++)
1177949d54bSdownsj 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
1187949d54bSdownsj 	best.base = -1;
1197949d54bSdownsj 	cur.base = -1;
1207949d54bSdownsj 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1217949d54bSdownsj 		if (words[i] == 0) {
1227949d54bSdownsj 			if (cur.base == -1)
1237949d54bSdownsj 				cur.base = i, cur.len = 1;
1247949d54bSdownsj 			else
1257949d54bSdownsj 				cur.len++;
1267949d54bSdownsj 		} else {
1277949d54bSdownsj 			if (cur.base != -1) {
1287949d54bSdownsj 				if (best.base == -1 || cur.len > best.len)
1297949d54bSdownsj 					best = cur;
1307949d54bSdownsj 				cur.base = -1;
1317949d54bSdownsj 			}
1327949d54bSdownsj 		}
1337949d54bSdownsj 	}
1347949d54bSdownsj 	if (cur.base != -1) {
1357949d54bSdownsj 		if (best.base == -1 || cur.len > best.len)
1367949d54bSdownsj 			best = cur;
1377949d54bSdownsj 	}
1387949d54bSdownsj 	if (best.base != -1 && best.len < 2)
1397949d54bSdownsj 		best.base = -1;
1407949d54bSdownsj 
1417949d54bSdownsj 	/*
1427949d54bSdownsj 	 * Format the result.
1437949d54bSdownsj 	 */
1447949d54bSdownsj 	tp = tmp;
14573bbab47Sitojun 	ep = tmp + sizeof(tmp);
14673bbab47Sitojun 	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
1477949d54bSdownsj 		/* Are we inside the best run of 0x00's? */
1487949d54bSdownsj 		if (best.base != -1 && i >= best.base &&
1497949d54bSdownsj 		    i < (best.base + best.len)) {
15073bbab47Sitojun 			if (i == best.base) {
15137eb9fdaSmillert 				if (tp + 1 >= ep) {
15237eb9fdaSmillert 					errno = ENOSPC;
15373bbab47Sitojun 					return (NULL);
15437eb9fdaSmillert 				}
1557949d54bSdownsj 				*tp++ = ':';
15673bbab47Sitojun 			}
1577949d54bSdownsj 			continue;
1587949d54bSdownsj 		}
1597949d54bSdownsj 		/* Are we following an initial run of 0x00s or any real hex? */
16073bbab47Sitojun 		if (i != 0) {
16137eb9fdaSmillert 			if (tp + 1 >= ep) {
16237eb9fdaSmillert 				errno = ENOSPC;
16373bbab47Sitojun 				return (NULL);
16437eb9fdaSmillert 			}
1657949d54bSdownsj 			*tp++ = ':';
16673bbab47Sitojun 		}
1677949d54bSdownsj 		/* Is this address an encapsulated IPv4? */
1687949d54bSdownsj 		if (i == 6 && best.base == 0 &&
1697949d54bSdownsj 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
170*3240e6a8Sguenther 			if (!inet_ntop4(src+12, tp, ep - tp))
1717949d54bSdownsj 				return (NULL);
1727949d54bSdownsj 			tp += strlen(tp);
1737949d54bSdownsj 			break;
1747949d54bSdownsj 		}
17573bbab47Sitojun 		advance = snprintf(tp, ep - tp, "%x", words[i]);
17637eb9fdaSmillert 		if (advance <= 0 || advance >= ep - tp) {
17737eb9fdaSmillert 			errno = ENOSPC;
17873bbab47Sitojun 			return (NULL);
17937eb9fdaSmillert 		}
18073bbab47Sitojun 		tp += advance;
1817949d54bSdownsj 	}
1827949d54bSdownsj 	/* Was it a trailing run of 0x00's? */
18373bbab47Sitojun 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
18437eb9fdaSmillert 		if (tp + 1 >= ep) {
18537eb9fdaSmillert 			errno = ENOSPC;
18673bbab47Sitojun 			return (NULL);
18737eb9fdaSmillert 		}
1887949d54bSdownsj 		*tp++ = ':';
18973bbab47Sitojun 	}
19037eb9fdaSmillert 	if (tp + 1 >= ep) {
19137eb9fdaSmillert 		errno = ENOSPC;
19273bbab47Sitojun 		return (NULL);
19337eb9fdaSmillert 	}
1947949d54bSdownsj 	*tp++ = '\0';
1957949d54bSdownsj 
1967949d54bSdownsj 	/*
1977949d54bSdownsj 	 * Check for overflow, copy, and we're done.
1987949d54bSdownsj 	 */
1997949d54bSdownsj 	if ((size_t)(tp - tmp) > size) {
2007949d54bSdownsj 		errno = ENOSPC;
2017949d54bSdownsj 		return (NULL);
2027949d54bSdownsj 	}
2035af14704Sderaadt 	strlcpy(dst, tmp, size);
2047949d54bSdownsj 	return (dst);
2057949d54bSdownsj }
206