xref: /onnv-gate/usr/src/lib/libnsl/nss/inet_ntop.c (revision 132:e3f7eaf7dde4)
10Sstevel@tonic-gate /*
2*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
6*132Srobinson /*
7*132Srobinson  * Copyright (c) 1996 by Internet Software Consortium.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
100Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
110Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
140Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
160Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
170Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
180Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
190Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
200Sstevel@tonic-gate  * SOFTWARE.
210Sstevel@tonic-gate  */
22*132Srobinson 
230Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <stdlib.h>
260Sstevel@tonic-gate #include <ctype.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <netdb.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <arpa/inet.h>
320Sstevel@tonic-gate #include <netinet/in.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate 
36*132Srobinson static const char *inet_ntop4(const uchar_t *, char *, socklen_t);
37*132Srobinson static const char *inet_ntop6(const uchar_t *, char *, socklen_t);
380Sstevel@tonic-gate 
39*132Srobinson /*
40*132Srobinson  * char *
410Sstevel@tonic-gate  * inet_ntop(af, src, dst, size)
420Sstevel@tonic-gate  *	convert a network format address to presentation format.
430Sstevel@tonic-gate  * return:
440Sstevel@tonic-gate  *	pointer to presentation format address (`dst'), or NULL (see errno).
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate const char *
470Sstevel@tonic-gate inet_ntop(int af, const void *src, char *dst, socklen_t size)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	switch (af) {
500Sstevel@tonic-gate 	case AF_INET:
510Sstevel@tonic-gate 		return (inet_ntop4(src, dst, size));
520Sstevel@tonic-gate 	case AF_INET6:
530Sstevel@tonic-gate 		return (inet_ntop6(src, dst, size));
540Sstevel@tonic-gate 	default:
550Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
560Sstevel@tonic-gate 		return (NULL);
570Sstevel@tonic-gate 	}
580Sstevel@tonic-gate 	/* NOTREACHED */
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
61*132Srobinson /*
62*132Srobinson  * const char *
630Sstevel@tonic-gate  * inet_ntop4(src, dst, size)
640Sstevel@tonic-gate  *	format an IPv4 address, more or less like inet_ntoa()
650Sstevel@tonic-gate  * return:
660Sstevel@tonic-gate  *	`dst' (as a const)
670Sstevel@tonic-gate  * notes:
680Sstevel@tonic-gate  *	(1) uses no statics
69*132Srobinson  *	(2) takes a uchar_t* not an in_addr as input
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #ifdef SPRINTF_CHAR
73*132Srobinson /* CSTYLED */
74*132Srobinson #define	SPRINTF(x) strlen(sprintf/**/x)
750Sstevel@tonic-gate #else
76*132Srobinson #define	SPRINTF(x) ((size_t)sprintf x)
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static const char *
80*132Srobinson inet_ntop4(const uchar_t *src, char *dst, socklen_t size)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	static const char fmt[] = "%u.%u.%u.%u";
83*132Srobinson 	char tmp[sizeof ("255.255.255.255")];
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
860Sstevel@tonic-gate 		errno = ENOSPC;
870Sstevel@tonic-gate 		return (NULL);
880Sstevel@tonic-gate 	}
89*132Srobinson 	(void) strcpy(dst, tmp);
900Sstevel@tonic-gate 	return (dst);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
93*132Srobinson /*
94*132Srobinson  * const char *
950Sstevel@tonic-gate  * inet_ntop6(src, dst, size)
960Sstevel@tonic-gate  *	convert IPv6 binary address into presentation (printable) format
970Sstevel@tonic-gate  */
98*132Srobinson #define	INADDRSZ	4
990Sstevel@tonic-gate #define	IN6ADDRSZ	16
100*132Srobinson #define	INT16SZ		2
1010Sstevel@tonic-gate static const char *
102*132Srobinson inet_ntop6(const uchar_t *src, char *dst, socklen_t size)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 * Note that int32_t and int16_t need only be "at least" large enough
1060Sstevel@tonic-gate 	 * to contain a value of the specified size.  On some systems, like
1070Sstevel@tonic-gate 	 * Crays, there is no such thing as an integer variable with 16 bits.
1080Sstevel@tonic-gate 	 * Keep this in mind if you think this function should have been coded
1090Sstevel@tonic-gate 	 * to use pointer overlays.  All the world's not a VAX.
1100Sstevel@tonic-gate 	 */
111*132Srobinson 	char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
1120Sstevel@tonic-gate 	struct { int base, len; } best, cur;
113*132Srobinson 	uint_t words[IN6ADDRSZ / INT16SZ];
1140Sstevel@tonic-gate 	int i;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * Preprocess:
1180Sstevel@tonic-gate 	 *	Copy the input (bytewise) array into a wordwise array.
1190Sstevel@tonic-gate 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
1200Sstevel@tonic-gate 	 */
121*132Srobinson 	(void) memset(words, '\0', sizeof (words));
1220Sstevel@tonic-gate 	for (i = 0; i < IN6ADDRSZ; i++)
1230Sstevel@tonic-gate 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
1240Sstevel@tonic-gate 	best.base = -1;
1250Sstevel@tonic-gate 	cur.base = -1;
1260Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1270Sstevel@tonic-gate 		if (words[i] == 0) {
1280Sstevel@tonic-gate 			if (cur.base == -1)
1290Sstevel@tonic-gate 				cur.base = i, cur.len = 1;
1300Sstevel@tonic-gate 			else
1310Sstevel@tonic-gate 				cur.len++;
1320Sstevel@tonic-gate 		} else {
1330Sstevel@tonic-gate 			if (cur.base != -1) {
1340Sstevel@tonic-gate 				if (best.base == -1 || cur.len > best.len)
1350Sstevel@tonic-gate 					best = cur;
1360Sstevel@tonic-gate 				cur.base = -1;
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	if (cur.base != -1) {
1410Sstevel@tonic-gate 		if (best.base == -1 || cur.len > best.len)
1420Sstevel@tonic-gate 			best = cur;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	if (best.base != -1 && best.len < 2)
1450Sstevel@tonic-gate 		best.base = -1;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * Format the result.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	tp = tmp;
1510Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1520Sstevel@tonic-gate 		/* Are we inside the best run of 0x00's? */
1530Sstevel@tonic-gate 		if (best.base != -1 && i >= best.base &&
1540Sstevel@tonic-gate 		    i < (best.base + best.len)) {
1550Sstevel@tonic-gate 			if (i == best.base)
1560Sstevel@tonic-gate 				*tp++ = ':';
1570Sstevel@tonic-gate 			continue;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		/* Are we following an initial run of 0x00s or any real hex? */
1600Sstevel@tonic-gate 		if (i != 0)
1610Sstevel@tonic-gate 			*tp++ = ':';
1620Sstevel@tonic-gate 		/* Is this address an encapsulated IPv4? */
1630Sstevel@tonic-gate 		if (i == 6 && best.base == 0 &&
1640Sstevel@tonic-gate 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
165*132Srobinson 			if (!inet_ntop4(src+12, tp, sizeof (tmp) - (tp - tmp)))
1660Sstevel@tonic-gate 				return (NULL);
1670Sstevel@tonic-gate 			tp += strlen(tp);
1680Sstevel@tonic-gate 			break;
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 		tp += SPRINTF((tp, "%x", words[i]));
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	/* Was it a trailing run of 0x00's? */
1730Sstevel@tonic-gate 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
1740Sstevel@tonic-gate 		*tp++ = ':';
1750Sstevel@tonic-gate 	*tp++ = '\0';
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Check for overflow, copy, and we're done.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	if ((int)(tp - tmp) > size) {
1810Sstevel@tonic-gate 		errno = ENOSPC;
1820Sstevel@tonic-gate 		return (NULL);
1830Sstevel@tonic-gate 	}
184*132Srobinson 	(void) strcpy(dst, tmp);
1850Sstevel@tonic-gate 	return (dst);
1860Sstevel@tonic-gate }
187