xref: /netbsd-src/lib/libc/inet/inet_ntop.c (revision 287fdb86f884c5ffc10cfe984c586f2729de434c)
1*287fdb86Slukem /*	$NetBSD: inet_ntop.c,v 1.12 2018/03/02 06:31:53 lukem Exp $	*/
239e7bb71Schristos 
339e7bb71Schristos /*
439e7bb71Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
539e7bb71Schristos  * Copyright (c) 1996-1999 by Internet Software Consortium.
639e7bb71Schristos  *
739e7bb71Schristos  * Permission to use, copy, modify, and distribute this software for any
839e7bb71Schristos  * purpose with or without fee is hereby granted, provided that the above
939e7bb71Schristos  * copyright notice and this permission notice appear in all copies.
1039e7bb71Schristos  *
1139e7bb71Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1239e7bb71Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1339e7bb71Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
1439e7bb71Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1539e7bb71Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1639e7bb71Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1739e7bb71Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1839e7bb71Schristos  */
1939e7bb71Schristos 
20df0952c6Schristos #include <sys/cdefs.h>
2139e7bb71Schristos #if defined(LIBC_SCCS) && !defined(lint)
22df0952c6Schristos #if 0
233873655bSchristos static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp";
24df0952c6Schristos #else
25*287fdb86Slukem __RCSID("$NetBSD: inet_ntop.c,v 1.12 2018/03/02 06:31:53 lukem Exp $");
26df0952c6Schristos #endif
2739e7bb71Schristos #endif /* LIBC_SCCS and not lint */
2839e7bb71Schristos 
2939e7bb71Schristos #include "port_before.h"
3039e7bb71Schristos 
31df0952c6Schristos #include "namespace.h"
3239e7bb71Schristos #include <sys/param.h>
3339e7bb71Schristos #include <sys/types.h>
3439e7bb71Schristos #include <sys/socket.h>
3539e7bb71Schristos 
3639e7bb71Schristos #include <netinet/in.h>
3739e7bb71Schristos #include <arpa/inet.h>
3839e7bb71Schristos #include <arpa/nameser.h>
3939e7bb71Schristos 
40df0952c6Schristos #include <assert.h>
4139e7bb71Schristos #include <errno.h>
4239e7bb71Schristos #include <stdio.h>
43df0952c6Schristos #include <stdlib.h>
4439e7bb71Schristos #include <string.h>
4539e7bb71Schristos 
4639e7bb71Schristos #include "port_after.h"
4739e7bb71Schristos 
48df0952c6Schristos #ifdef __weak_alias
49df0952c6Schristos __weak_alias(inet_ntop,_inet_ntop)
5039e7bb71Schristos #endif
5139e7bb71Schristos 
52d73eb73dSchristos /*%
5339e7bb71Schristos  * WARNING: Don't even consider trying to compile this on a system where
5439e7bb71Schristos  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
5539e7bb71Schristos  */
5639e7bb71Schristos 
57df0952c6Schristos static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
58df0952c6Schristos static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
5939e7bb71Schristos 
6039e7bb71Schristos /* char *
6139e7bb71Schristos  * inet_ntop(af, src, dst, size)
6239e7bb71Schristos  *	convert a network format address to presentation format.
6339e7bb71Schristos  * return:
6439e7bb71Schristos  *	pointer to presentation format address (`dst'), or NULL (see errno).
6539e7bb71Schristos  * author:
6639e7bb71Schristos  *	Paul Vixie, 1996.
6739e7bb71Schristos  */
6839e7bb71Schristos const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)691f043722Smatt inet_ntop(int af, const void *src, char *dst, socklen_t size)
7039e7bb71Schristos {
71df0952c6Schristos 
72df0952c6Schristos 	_DIAGASSERT(src != NULL);
73df0952c6Schristos 	_DIAGASSERT(dst != NULL);
74df0952c6Schristos 
7539e7bb71Schristos 	switch (af) {
7639e7bb71Schristos 	case AF_INET:
773c501c20Schristos 		return inet_ntop4(src, dst, size);
7839e7bb71Schristos 	case AF_INET6:
793c501c20Schristos 		return inet_ntop6(src, dst, size);
8039e7bb71Schristos 	default:
8139e7bb71Schristos 		errno = EAFNOSUPPORT;
823c501c20Schristos 		return NULL;
8339e7bb71Schristos 	}
8439e7bb71Schristos 	/* NOTREACHED */
8539e7bb71Schristos }
8639e7bb71Schristos 
8739e7bb71Schristos /* const char *
8839e7bb71Schristos  * inet_ntop4(src, dst, size)
89df0952c6Schristos  *	format an IPv4 address, more or less like inet_ntoa()
9039e7bb71Schristos  * return:
9139e7bb71Schristos  *	`dst' (as a const)
9239e7bb71Schristos  * notes:
9339e7bb71Schristos  *	(1) uses no statics
9439e7bb71Schristos  *	(2) takes a u_char* not an in_addr as input
9539e7bb71Schristos  * author:
9639e7bb71Schristos  *	Paul Vixie, 1996.
9739e7bb71Schristos  */
9839e7bb71Schristos static const char *
inet_ntop4(const u_char * src,char * dst,socklen_t size)991f043722Smatt inet_ntop4(const u_char *src, char *dst, socklen_t size)
10039e7bb71Schristos {
10139e7bb71Schristos 	char tmp[sizeof "255.255.255.255"];
102df0952c6Schristos 	int l;
10339e7bb71Schristos 
104df0952c6Schristos 	_DIAGASSERT(src != NULL);
105df0952c6Schristos 	_DIAGASSERT(dst != NULL);
106df0952c6Schristos 
107df0952c6Schristos 	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
108df0952c6Schristos 	    src[0], src[1], src[2], src[3]);
109*287fdb86Slukem 	if (l <= 0 || (socklen_t) l >= size) {
110*287fdb86Slukem 		errno = ENOSPC;
1113c501c20Schristos 		return NULL;
112*287fdb86Slukem 	}
113df0952c6Schristos 	strlcpy(dst, tmp, size);
1143c501c20Schristos 	return dst;
11539e7bb71Schristos }
11639e7bb71Schristos 
11739e7bb71Schristos /* const char *
11839e7bb71Schristos  * inet_ntop6(src, dst, size)
11939e7bb71Schristos  *	convert IPv6 binary address into presentation (printable) format
12039e7bb71Schristos  * author:
12139e7bb71Schristos  *	Paul Vixie, 1996.
12239e7bb71Schristos  */
12339e7bb71Schristos static const char *
inet_ntop6(const u_char * src,char * dst,socklen_t size)1241f043722Smatt inet_ntop6(const u_char *src, char *dst, socklen_t size)
12539e7bb71Schristos {
12639e7bb71Schristos 	/*
12739e7bb71Schristos 	 * Note that int32_t and int16_t need only be "at least" large enough
12839e7bb71Schristos 	 * to contain a value of the specified size.  On some systems, like
12939e7bb71Schristos 	 * Crays, there is no such thing as an integer variable with 16 bits.
13039e7bb71Schristos 	 * Keep this in mind if you think this function should have been coded
13139e7bb71Schristos 	 * to use pointer overlays.  All the world's not a VAX.
13239e7bb71Schristos 	 */
133df0952c6Schristos 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
134df0952c6Schristos 	char *tp, *ep;
13539e7bb71Schristos 	struct { int base, len; } best, cur;
13639e7bb71Schristos 	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
13739e7bb71Schristos 	int i;
138df0952c6Schristos 	int advance;
139df0952c6Schristos 
140df0952c6Schristos 	_DIAGASSERT(src != NULL);
141df0952c6Schristos 	_DIAGASSERT(dst != NULL);
14239e7bb71Schristos 
14339e7bb71Schristos 	/*
14439e7bb71Schristos 	 * Preprocess:
14539e7bb71Schristos 	 *	Copy the input (bytewise) array into a wordwise array.
14639e7bb71Schristos 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
14739e7bb71Schristos 	 */
14839e7bb71Schristos 	memset(words, '\0', sizeof words);
14939e7bb71Schristos 	for (i = 0; i < NS_IN6ADDRSZ; i++)
15039e7bb71Schristos 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
15139e7bb71Schristos 	best.base = -1;
152d73eb73dSchristos 	best.len = 0;
15339e7bb71Schristos 	cur.base = -1;
154d73eb73dSchristos 	cur.len = 0;
15539e7bb71Schristos 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
15639e7bb71Schristos 		if (words[i] == 0) {
15739e7bb71Schristos 			if (cur.base == -1)
15839e7bb71Schristos 				cur.base = i, cur.len = 1;
15939e7bb71Schristos 			else
16039e7bb71Schristos 				cur.len++;
16139e7bb71Schristos 		} else {
16239e7bb71Schristos 			if (cur.base != -1) {
16339e7bb71Schristos 				if (best.base == -1 || cur.len > best.len)
16439e7bb71Schristos 					best = cur;
16539e7bb71Schristos 				cur.base = -1;
16639e7bb71Schristos 			}
16739e7bb71Schristos 		}
16839e7bb71Schristos 	}
16939e7bb71Schristos 	if (cur.base != -1) {
17039e7bb71Schristos 		if (best.base == -1 || cur.len > best.len)
17139e7bb71Schristos 			best = cur;
17239e7bb71Schristos 	}
17339e7bb71Schristos 	if (best.base != -1 && best.len < 2)
17439e7bb71Schristos 		best.base = -1;
17539e7bb71Schristos 
17639e7bb71Schristos 	/*
17739e7bb71Schristos 	 * Format the result.
17839e7bb71Schristos 	 */
17939e7bb71Schristos 	tp = tmp;
180df0952c6Schristos 	ep = tmp + sizeof(tmp);
18139e7bb71Schristos 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
18239e7bb71Schristos 		/* Are we inside the best run of 0x00's? */
18339e7bb71Schristos 		if (best.base != -1 && i >= best.base &&
18439e7bb71Schristos 		    i < (best.base + best.len)) {
18539e7bb71Schristos 			if (i == best.base)
18639e7bb71Schristos 				*tp++ = ':';
18739e7bb71Schristos 			continue;
18839e7bb71Schristos 		}
18939e7bb71Schristos 		/* Are we following an initial run of 0x00s or any real hex? */
190df0952c6Schristos 		if (i != 0) {
191df0952c6Schristos 			if (tp + 1 >= ep)
19272d54c17Schristos 				goto out;
19339e7bb71Schristos 			*tp++ = ':';
194df0952c6Schristos 		}
19539e7bb71Schristos 		/* Is this address an encapsulated IPv4? */
196df0952c6Schristos 		if (i == 6 && best.base == 0 &&
197df0952c6Schristos 		    (best.len == 6 ||
19839e7bb71Schristos 		    (best.len == 7 && words[7] != 0x0001) ||
19939e7bb71Schristos 		    (best.len == 5 && words[5] == 0xffff))) {
2007ea3b31bSchristos 			if (!inet_ntop4(src + 12, tp, (socklen_t)(ep - tp)))
20172d54c17Schristos 				goto out;
20239e7bb71Schristos 			tp += strlen(tp);
20339e7bb71Schristos 			break;
20439e7bb71Schristos 		}
205df0952c6Schristos 		advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]);
206df0952c6Schristos 		if (advance <= 0 || advance >= ep - tp)
20772d54c17Schristos 			goto out;
208df0952c6Schristos 		tp += advance;
20939e7bb71Schristos 	}
21039e7bb71Schristos 	/* Was it a trailing run of 0x00's? */
21139e7bb71Schristos 	if (best.base != -1 && (best.base + best.len) ==
212df0952c6Schristos 	    (NS_IN6ADDRSZ / NS_INT16SZ)) {
213df0952c6Schristos 		if (tp + 1 >= ep)
21472d54c17Schristos 			goto out;
21539e7bb71Schristos 		*tp++ = ':';
216df0952c6Schristos 	}
217df0952c6Schristos 	if (tp + 1 >= ep)
21872d54c17Schristos 		goto out;
21939e7bb71Schristos 	*tp++ = '\0';
22039e7bb71Schristos 
22139e7bb71Schristos 	/*
22239e7bb71Schristos 	 * Check for overflow, copy, and we're done.
22339e7bb71Schristos 	 */
22472d54c17Schristos 	if ((size_t)(tp - tmp) > size)
22572d54c17Schristos 		goto out;
226df0952c6Schristos 	strlcpy(dst, tmp, size);
2273c501c20Schristos 	return dst;
22872d54c17Schristos out:
22972d54c17Schristos 	errno = ENOSPC;
23072d54c17Schristos 	return NULL;
23139e7bb71Schristos }
232d73eb73dSchristos 
233d73eb73dSchristos /*! \file */
234