xref: /onnv-gate/usr/src/lib/libnsl/nss/inet_ntop.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
2*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
6132Srobinson /*
7132Srobinson  * 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  */
22132Srobinson 
230Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
240Sstevel@tonic-gate 
25*1219Sraf #include "mt.h"
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <ctype.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <netdb.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <arpa/inet.h>
330Sstevel@tonic-gate #include <netinet/in.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
37132Srobinson static const char *inet_ntop4(const uchar_t *, char *, socklen_t);
38132Srobinson static const char *inet_ntop6(const uchar_t *, char *, socklen_t);
390Sstevel@tonic-gate 
40132Srobinson /*
41132Srobinson  * char *
420Sstevel@tonic-gate  * inet_ntop(af, src, dst, size)
430Sstevel@tonic-gate  *	convert a network format address to presentation format.
440Sstevel@tonic-gate  * return:
450Sstevel@tonic-gate  *	pointer to presentation format address (`dst'), or NULL (see errno).
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)480Sstevel@tonic-gate inet_ntop(int af, const void *src, char *dst, socklen_t size)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	switch (af) {
510Sstevel@tonic-gate 	case AF_INET:
520Sstevel@tonic-gate 		return (inet_ntop4(src, dst, size));
530Sstevel@tonic-gate 	case AF_INET6:
540Sstevel@tonic-gate 		return (inet_ntop6(src, dst, size));
550Sstevel@tonic-gate 	default:
560Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
570Sstevel@tonic-gate 		return (NULL);
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 	/* NOTREACHED */
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
62132Srobinson /*
63132Srobinson  * const char *
640Sstevel@tonic-gate  * inet_ntop4(src, dst, size)
650Sstevel@tonic-gate  *	format an IPv4 address, more or less like inet_ntoa()
660Sstevel@tonic-gate  * return:
670Sstevel@tonic-gate  *	`dst' (as a const)
680Sstevel@tonic-gate  * notes:
690Sstevel@tonic-gate  *	(1) uses no statics
70132Srobinson  *	(2) takes a uchar_t* not an in_addr as input
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifdef SPRINTF_CHAR
74132Srobinson /* CSTYLED */
75132Srobinson #define	SPRINTF(x) strlen(sprintf/**/x)
760Sstevel@tonic-gate #else
77132Srobinson #define	SPRINTF(x) ((size_t)sprintf x)
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static const char *
inet_ntop4(const uchar_t * src,char * dst,socklen_t size)81132Srobinson inet_ntop4(const uchar_t *src, char *dst, socklen_t size)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	static const char fmt[] = "%u.%u.%u.%u";
84132Srobinson 	char tmp[sizeof ("255.255.255.255")];
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
870Sstevel@tonic-gate 		errno = ENOSPC;
880Sstevel@tonic-gate 		return (NULL);
890Sstevel@tonic-gate 	}
90132Srobinson 	(void) strcpy(dst, tmp);
910Sstevel@tonic-gate 	return (dst);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
94132Srobinson /*
95132Srobinson  * const char *
960Sstevel@tonic-gate  * inet_ntop6(src, dst, size)
970Sstevel@tonic-gate  *	convert IPv6 binary address into presentation (printable) format
980Sstevel@tonic-gate  */
99132Srobinson #define	INADDRSZ	4
1000Sstevel@tonic-gate #define	IN6ADDRSZ	16
101132Srobinson #define	INT16SZ		2
1020Sstevel@tonic-gate static const char *
inet_ntop6(const uchar_t * src,char * dst,socklen_t size)103132Srobinson inet_ntop6(const uchar_t *src, char *dst, socklen_t size)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * Note that int32_t and int16_t need only be "at least" large enough
1070Sstevel@tonic-gate 	 * to contain a value of the specified size.  On some systems, like
1080Sstevel@tonic-gate 	 * Crays, there is no such thing as an integer variable with 16 bits.
1090Sstevel@tonic-gate 	 * Keep this in mind if you think this function should have been coded
1100Sstevel@tonic-gate 	 * to use pointer overlays.  All the world's not a VAX.
1110Sstevel@tonic-gate 	 */
112132Srobinson 	char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
1130Sstevel@tonic-gate 	struct { int base, len; } best, cur;
114132Srobinson 	uint_t words[IN6ADDRSZ / INT16SZ];
1150Sstevel@tonic-gate 	int i;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * Preprocess:
1190Sstevel@tonic-gate 	 *	Copy the input (bytewise) array into a wordwise array.
1200Sstevel@tonic-gate 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
1210Sstevel@tonic-gate 	 */
122132Srobinson 	(void) memset(words, '\0', sizeof (words));
1230Sstevel@tonic-gate 	for (i = 0; i < IN6ADDRSZ; i++)
1240Sstevel@tonic-gate 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
1250Sstevel@tonic-gate 	best.base = -1;
1260Sstevel@tonic-gate 	cur.base = -1;
1270Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1280Sstevel@tonic-gate 		if (words[i] == 0) {
1290Sstevel@tonic-gate 			if (cur.base == -1)
1300Sstevel@tonic-gate 				cur.base = i, cur.len = 1;
1310Sstevel@tonic-gate 			else
1320Sstevel@tonic-gate 				cur.len++;
1330Sstevel@tonic-gate 		} else {
1340Sstevel@tonic-gate 			if (cur.base != -1) {
1350Sstevel@tonic-gate 				if (best.base == -1 || cur.len > best.len)
1360Sstevel@tonic-gate 					best = cur;
1370Sstevel@tonic-gate 				cur.base = -1;
1380Sstevel@tonic-gate 			}
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	if (cur.base != -1) {
1420Sstevel@tonic-gate 		if (best.base == -1 || cur.len > best.len)
1430Sstevel@tonic-gate 			best = cur;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	if (best.base != -1 && best.len < 2)
1460Sstevel@tonic-gate 		best.base = -1;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * Format the result.
1500Sstevel@tonic-gate 	 */
1510Sstevel@tonic-gate 	tp = tmp;
1520Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1530Sstevel@tonic-gate 		/* Are we inside the best run of 0x00's? */
1540Sstevel@tonic-gate 		if (best.base != -1 && i >= best.base &&
1550Sstevel@tonic-gate 		    i < (best.base + best.len)) {
1560Sstevel@tonic-gate 			if (i == best.base)
1570Sstevel@tonic-gate 				*tp++ = ':';
1580Sstevel@tonic-gate 			continue;
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 		/* Are we following an initial run of 0x00s or any real hex? */
1610Sstevel@tonic-gate 		if (i != 0)
1620Sstevel@tonic-gate 			*tp++ = ':';
1630Sstevel@tonic-gate 		/* Is this address an encapsulated IPv4? */
1640Sstevel@tonic-gate 		if (i == 6 && best.base == 0 &&
1650Sstevel@tonic-gate 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
166132Srobinson 			if (!inet_ntop4(src+12, tp, sizeof (tmp) - (tp - tmp)))
1670Sstevel@tonic-gate 				return (NULL);
1680Sstevel@tonic-gate 			tp += strlen(tp);
1690Sstevel@tonic-gate 			break;
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 		tp += SPRINTF((tp, "%x", words[i]));
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	/* Was it a trailing run of 0x00's? */
1740Sstevel@tonic-gate 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
1750Sstevel@tonic-gate 		*tp++ = ':';
1760Sstevel@tonic-gate 	*tp++ = '\0';
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/*
1790Sstevel@tonic-gate 	 * Check for overflow, copy, and we're done.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	if ((int)(tp - tmp) > size) {
1820Sstevel@tonic-gate 		errno = ENOSPC;
1830Sstevel@tonic-gate 		return (NULL);
1840Sstevel@tonic-gate 	}
185132Srobinson 	(void) strcpy(dst, tmp);
1860Sstevel@tonic-gate 	return (dst);
1870Sstevel@tonic-gate }
188