1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /* Copyright (c) 1996 by Internet Software Consortium.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
9*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
10*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19*0Sstevel@tonic-gate  * SOFTWARE.
20*0Sstevel@tonic-gate  */
21*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #include <stdlib.h>
24*0Sstevel@tonic-gate #include <ctype.h>
25*0Sstevel@tonic-gate #include <string.h>
26*0Sstevel@tonic-gate #include <strings.h>
27*0Sstevel@tonic-gate #include <netdb.h>
28*0Sstevel@tonic-gate #include <stdio.h>
29*0Sstevel@tonic-gate #include <arpa/inet.h>
30*0Sstevel@tonic-gate #include <netinet/in.h>
31*0Sstevel@tonic-gate #include <sys/socket.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate static const char *inet_ntop4(const u_char *, char *, socklen_t);
35*0Sstevel@tonic-gate static const char *inet_ntop6(const u_char *, char *, socklen_t);
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /* char *
38*0Sstevel@tonic-gate  * inet_ntop(af, src, dst, size)
39*0Sstevel@tonic-gate  *	convert a network format address to presentation format.
40*0Sstevel@tonic-gate  * return:
41*0Sstevel@tonic-gate  *	pointer to presentation format address (`dst'), or NULL (see errno).
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate const char *
44*0Sstevel@tonic-gate inet_ntop(int af, const void *src, char *dst, socklen_t size)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	switch (af) {
47*0Sstevel@tonic-gate 	case AF_INET:
48*0Sstevel@tonic-gate 		return (inet_ntop4(src, dst, size));
49*0Sstevel@tonic-gate 	case AF_INET6:
50*0Sstevel@tonic-gate 		return (inet_ntop6(src, dst, size));
51*0Sstevel@tonic-gate 	default:
52*0Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
53*0Sstevel@tonic-gate 		return (NULL);
54*0Sstevel@tonic-gate 	}
55*0Sstevel@tonic-gate 	/* NOTREACHED */
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /* const char *
59*0Sstevel@tonic-gate  * inet_ntop4(src, dst, size)
60*0Sstevel@tonic-gate  *	format an IPv4 address, more or less like inet_ntoa()
61*0Sstevel@tonic-gate  * return:
62*0Sstevel@tonic-gate  *	`dst' (as a const)
63*0Sstevel@tonic-gate  * notes:
64*0Sstevel@tonic-gate  *	(1) uses no statics
65*0Sstevel@tonic-gate  *	(2) takes a u_char* not an in_addr as input
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #ifdef SPRINTF_CHAR
69*0Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
70*0Sstevel@tonic-gate #else
71*0Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
72*0Sstevel@tonic-gate #endif
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static const char *
75*0Sstevel@tonic-gate inet_ntop4(const u_char *src, char *dst, socklen_t size)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	static const char fmt[] = "%u.%u.%u.%u";
78*0Sstevel@tonic-gate 	char tmp[sizeof "255.255.255.255"];
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
81*0Sstevel@tonic-gate 		errno = ENOSPC;
82*0Sstevel@tonic-gate 		return (NULL);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	strcpy(dst, tmp);
85*0Sstevel@tonic-gate 	return (dst);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /* const char *
89*0Sstevel@tonic-gate  * inet_ntop6(src, dst, size)
90*0Sstevel@tonic-gate  *	convert IPv6 binary address into presentation (printable) format
91*0Sstevel@tonic-gate  */
92*0Sstevel@tonic-gate #define INADDRSZ	4
93*0Sstevel@tonic-gate #define	IN6ADDRSZ	16
94*0Sstevel@tonic-gate #define INT16SZ		2
95*0Sstevel@tonic-gate static const char *
96*0Sstevel@tonic-gate inet_ntop6(const u_char *src, char *dst, socklen_t size)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	/*
99*0Sstevel@tonic-gate 	 * Note that int32_t and int16_t need only be "at least" large enough
100*0Sstevel@tonic-gate 	 * to contain a value of the specified size.  On some systems, like
101*0Sstevel@tonic-gate 	 * Crays, there is no such thing as an integer variable with 16 bits.
102*0Sstevel@tonic-gate 	 * Keep this in mind if you think this function should have been coded
103*0Sstevel@tonic-gate 	 * to use pointer overlays.  All the world's not a VAX.
104*0Sstevel@tonic-gate 	 */
105*0Sstevel@tonic-gate 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
106*0Sstevel@tonic-gate 	struct { int base, len; } best, cur;
107*0Sstevel@tonic-gate 	u_int words[IN6ADDRSZ / INT16SZ];
108*0Sstevel@tonic-gate 	int i;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/*
111*0Sstevel@tonic-gate 	 * Preprocess:
112*0Sstevel@tonic-gate 	 *	Copy the input (bytewise) array into a wordwise array.
113*0Sstevel@tonic-gate 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
114*0Sstevel@tonic-gate 	 */
115*0Sstevel@tonic-gate 	memset(words, '\0', sizeof words);
116*0Sstevel@tonic-gate 	for (i = 0; i < IN6ADDRSZ; i++)
117*0Sstevel@tonic-gate 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
118*0Sstevel@tonic-gate 	best.base = -1;
119*0Sstevel@tonic-gate 	cur.base = -1;
120*0Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
121*0Sstevel@tonic-gate 		if (words[i] == 0) {
122*0Sstevel@tonic-gate 			if (cur.base == -1)
123*0Sstevel@tonic-gate 				cur.base = i, cur.len = 1;
124*0Sstevel@tonic-gate 			else
125*0Sstevel@tonic-gate 				cur.len++;
126*0Sstevel@tonic-gate 		} else {
127*0Sstevel@tonic-gate 			if (cur.base != -1) {
128*0Sstevel@tonic-gate 				if (best.base == -1 || cur.len > best.len)
129*0Sstevel@tonic-gate 					best = cur;
130*0Sstevel@tonic-gate 				cur.base = -1;
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 		}
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	if (cur.base != -1) {
135*0Sstevel@tonic-gate 		if (best.base == -1 || cur.len > best.len)
136*0Sstevel@tonic-gate 			best = cur;
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	if (best.base != -1 && best.len < 2)
139*0Sstevel@tonic-gate 		best.base = -1;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * Format the result.
143*0Sstevel@tonic-gate 	 */
144*0Sstevel@tonic-gate 	tp = tmp;
145*0Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
146*0Sstevel@tonic-gate 		/* Are we inside the best run of 0x00's? */
147*0Sstevel@tonic-gate 		if (best.base != -1 && i >= best.base &&
148*0Sstevel@tonic-gate 		    i < (best.base + best.len)) {
149*0Sstevel@tonic-gate 			if (i == best.base)
150*0Sstevel@tonic-gate 				*tp++ = ':';
151*0Sstevel@tonic-gate 			continue;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 		/* Are we following an initial run of 0x00s or any real hex? */
154*0Sstevel@tonic-gate 		if (i != 0)
155*0Sstevel@tonic-gate 			*tp++ = ':';
156*0Sstevel@tonic-gate 		/* Is this address an encapsulated IPv4? */
157*0Sstevel@tonic-gate 		if (i == 6 && best.base == 0 &&
158*0Sstevel@tonic-gate 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
159*0Sstevel@tonic-gate 			if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
160*0Sstevel@tonic-gate 				return (NULL);
161*0Sstevel@tonic-gate 			tp += strlen(tp);
162*0Sstevel@tonic-gate 			break;
163*0Sstevel@tonic-gate 		}
164*0Sstevel@tonic-gate 		tp += SPRINTF((tp, "%x", words[i]));
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 	/* Was it a trailing run of 0x00's? */
167*0Sstevel@tonic-gate 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
168*0Sstevel@tonic-gate 		*tp++ = ':';
169*0Sstevel@tonic-gate 	*tp++ = '\0';
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	/*
172*0Sstevel@tonic-gate 	 * Check for overflow, copy, and we're done.
173*0Sstevel@tonic-gate 	 */
174*0Sstevel@tonic-gate 	if ((int)(tp - tmp) > size) {
175*0Sstevel@tonic-gate 		errno = ENOSPC;
176*0Sstevel@tonic-gate 		return (NULL);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 	strcpy(dst, tmp);
179*0Sstevel@tonic-gate 	return (dst);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182