1*0Sstevel@tonic-gate /*	$OpenBSD: inet_ntop.c,v 1.5 2002/08/23 16:27:31 itojun Exp $	*/
2*0Sstevel@tonic-gate 
3*0Sstevel@tonic-gate /* Copyright (c) 1996 by Internet Software Consortium.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
6*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
7*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16*0Sstevel@tonic-gate  * SOFTWARE.
17*0Sstevel@tonic-gate  */
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #include "includes.h"
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #ifndef HAVE_INET_NTOP
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
24*0Sstevel@tonic-gate #if 0
25*0Sstevel@tonic-gate static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $";
26*0Sstevel@tonic-gate #else
27*0Sstevel@tonic-gate static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.5 2002/08/23 16:27:31 itojun Exp $";
28*0Sstevel@tonic-gate #endif
29*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <sys/param.h>
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/socket.h>
34*0Sstevel@tonic-gate #include "fake-socket.h"
35*0Sstevel@tonic-gate #include <netinet/in.h>
36*0Sstevel@tonic-gate #include <arpa/inet.h>
37*0Sstevel@tonic-gate #ifndef HAVE_CYGWIN
38*0Sstevel@tonic-gate #include <arpa/nameser.h>
39*0Sstevel@tonic-gate #endif
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #ifndef IN6ADDRSZ
45*0Sstevel@tonic-gate #define IN6ADDRSZ   16   /* IPv6 T_AAAA */
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #ifndef INT16SZ
49*0Sstevel@tonic-gate #define INT16SZ     2    /* for systems without 16-bit ints */
50*0Sstevel@tonic-gate #endif
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * WARNING: Don't even consider trying to compile this on a system where
54*0Sstevel@tonic-gate  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
58*0Sstevel@tonic-gate static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /* char *
61*0Sstevel@tonic-gate  * inet_ntop(af, src, dst, size)
62*0Sstevel@tonic-gate  *	convert a network format address to presentation format.
63*0Sstevel@tonic-gate  * return:
64*0Sstevel@tonic-gate  *	pointer to presentation format address (`dst'), or NULL (see errno).
65*0Sstevel@tonic-gate  * author:
66*0Sstevel@tonic-gate  *	Paul Vixie, 1996.
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate const char *
inet_ntop(af,src,dst,size)69*0Sstevel@tonic-gate inet_ntop(af, src, dst, size)
70*0Sstevel@tonic-gate 	int af;
71*0Sstevel@tonic-gate 	const void *src;
72*0Sstevel@tonic-gate 	char *dst;
73*0Sstevel@tonic-gate 	size_t size;
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	switch (af) {
76*0Sstevel@tonic-gate 	case AF_INET:
77*0Sstevel@tonic-gate 		return (inet_ntop4(src, dst, size));
78*0Sstevel@tonic-gate 	case AF_INET6:
79*0Sstevel@tonic-gate 		return (inet_ntop6(src, dst, size));
80*0Sstevel@tonic-gate 	default:
81*0Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
82*0Sstevel@tonic-gate 		return (NULL);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	/* NOTREACHED */
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /* const char *
88*0Sstevel@tonic-gate  * inet_ntop4(src, dst, size)
89*0Sstevel@tonic-gate  *	format an IPv4 address, more or less like inet_ntoa()
90*0Sstevel@tonic-gate  * return:
91*0Sstevel@tonic-gate  *	`dst' (as a const)
92*0Sstevel@tonic-gate  * notes:
93*0Sstevel@tonic-gate  *	(1) uses no statics
94*0Sstevel@tonic-gate  *	(2) takes a u_char* not an in_addr as input
95*0Sstevel@tonic-gate  * author:
96*0Sstevel@tonic-gate  *	Paul Vixie, 1996.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate static const char *
inet_ntop4(src,dst,size)99*0Sstevel@tonic-gate inet_ntop4(src, dst, size)
100*0Sstevel@tonic-gate 	const u_char *src;
101*0Sstevel@tonic-gate 	char *dst;
102*0Sstevel@tonic-gate 	size_t size;
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	static const char fmt[] = "%u.%u.%u.%u";
105*0Sstevel@tonic-gate 	char tmp[sizeof "255.255.255.255"];
106*0Sstevel@tonic-gate 	int l;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
109*0Sstevel@tonic-gate 	if (l <= 0 || l >= size) {
110*0Sstevel@tonic-gate 		errno = ENOSPC;
111*0Sstevel@tonic-gate 		return (NULL);
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 	strlcpy(dst, tmp, size);
114*0Sstevel@tonic-gate 	return (dst);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /* const char *
118*0Sstevel@tonic-gate  * inet_ntop6(src, dst, size)
119*0Sstevel@tonic-gate  *	convert IPv6 binary address into presentation (printable) format
120*0Sstevel@tonic-gate  * author:
121*0Sstevel@tonic-gate  *	Paul Vixie, 1996.
122*0Sstevel@tonic-gate  */
123*0Sstevel@tonic-gate static const char *
inet_ntop6(src,dst,size)124*0Sstevel@tonic-gate inet_ntop6(src, dst, size)
125*0Sstevel@tonic-gate 	const u_char *src;
126*0Sstevel@tonic-gate 	char *dst;
127*0Sstevel@tonic-gate 	size_t size;
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	/*
130*0Sstevel@tonic-gate 	 * Note that int32_t and int16_t need only be "at least" large enough
131*0Sstevel@tonic-gate 	 * to contain a value of the specified size.  On some systems, like
132*0Sstevel@tonic-gate 	 * Crays, there is no such thing as an integer variable with 16 bits.
133*0Sstevel@tonic-gate 	 * Keep this in mind if you think this function should have been coded
134*0Sstevel@tonic-gate 	 * to use pointer overlays.  All the world's not a VAX.
135*0Sstevel@tonic-gate 	 */
136*0Sstevel@tonic-gate 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
137*0Sstevel@tonic-gate 	char *tp, *ep;
138*0Sstevel@tonic-gate 	struct { int base, len; } best, cur;
139*0Sstevel@tonic-gate 	u_int words[IN6ADDRSZ / INT16SZ];
140*0Sstevel@tonic-gate 	int i;
141*0Sstevel@tonic-gate 	int advance;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	/*
144*0Sstevel@tonic-gate 	 * Preprocess:
145*0Sstevel@tonic-gate 	 *	Copy the input (bytewise) array into a wordwise array.
146*0Sstevel@tonic-gate 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
147*0Sstevel@tonic-gate 	 */
148*0Sstevel@tonic-gate 	memset(words, '\0', sizeof words);
149*0Sstevel@tonic-gate 	for (i = 0; i < IN6ADDRSZ; i++)
150*0Sstevel@tonic-gate 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
151*0Sstevel@tonic-gate 	best.base = -1;
152*0Sstevel@tonic-gate 	cur.base = -1;
153*0Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
154*0Sstevel@tonic-gate 		if (words[i] == 0) {
155*0Sstevel@tonic-gate 			if (cur.base == -1)
156*0Sstevel@tonic-gate 				cur.base = i, cur.len = 1;
157*0Sstevel@tonic-gate 			else
158*0Sstevel@tonic-gate 				cur.len++;
159*0Sstevel@tonic-gate 		} else {
160*0Sstevel@tonic-gate 			if (cur.base != -1) {
161*0Sstevel@tonic-gate 				if (best.base == -1 || cur.len > best.len)
162*0Sstevel@tonic-gate 					best = cur;
163*0Sstevel@tonic-gate 				cur.base = -1;
164*0Sstevel@tonic-gate 			}
165*0Sstevel@tonic-gate 		}
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 	if (cur.base != -1) {
168*0Sstevel@tonic-gate 		if (best.base == -1 || cur.len > best.len)
169*0Sstevel@tonic-gate 			best = cur;
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 	if (best.base != -1 && best.len < 2)
172*0Sstevel@tonic-gate 		best.base = -1;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	/*
175*0Sstevel@tonic-gate 	 * Format the result.
176*0Sstevel@tonic-gate 	 */
177*0Sstevel@tonic-gate 	tp = tmp;
178*0Sstevel@tonic-gate 	ep = tmp + sizeof(tmp);
179*0Sstevel@tonic-gate 	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
180*0Sstevel@tonic-gate 		/* Are we inside the best run of 0x00's? */
181*0Sstevel@tonic-gate 		if (best.base != -1 && i >= best.base &&
182*0Sstevel@tonic-gate 		    i < (best.base + best.len)) {
183*0Sstevel@tonic-gate 			if (i == best.base) {
184*0Sstevel@tonic-gate 				if (tp + 1 >= ep)
185*0Sstevel@tonic-gate 					return (NULL);
186*0Sstevel@tonic-gate 				*tp++ = ':';
187*0Sstevel@tonic-gate 			}
188*0Sstevel@tonic-gate 			continue;
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 		/* Are we following an initial run of 0x00s or any real hex? */
191*0Sstevel@tonic-gate 		if (i != 0) {
192*0Sstevel@tonic-gate 			if (tp + 1 >= ep)
193*0Sstevel@tonic-gate 				return (NULL);
194*0Sstevel@tonic-gate 			*tp++ = ':';
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 		/* Is this address an encapsulated IPv4? */
197*0Sstevel@tonic-gate 		if (i == 6 && best.base == 0 &&
198*0Sstevel@tonic-gate 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
199*0Sstevel@tonic-gate 			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
200*0Sstevel@tonic-gate 				return (NULL);
201*0Sstevel@tonic-gate 			tp += strlen(tp);
202*0Sstevel@tonic-gate 			break;
203*0Sstevel@tonic-gate 		}
204*0Sstevel@tonic-gate 		advance = snprintf(tp, ep - tp, "%x", words[i]);
205*0Sstevel@tonic-gate 		if (advance <= 0 || advance >= ep - tp)
206*0Sstevel@tonic-gate 			return (NULL);
207*0Sstevel@tonic-gate 		tp += advance;
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	/* Was it a trailing run of 0x00's? */
210*0Sstevel@tonic-gate 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
211*0Sstevel@tonic-gate 		if (tp + 1 >= ep)
212*0Sstevel@tonic-gate 			return (NULL);
213*0Sstevel@tonic-gate 		*tp++ = ':';
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 	if (tp + 1 >= ep)
216*0Sstevel@tonic-gate 		return (NULL);
217*0Sstevel@tonic-gate 	*tp++ = '\0';
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	/*
220*0Sstevel@tonic-gate 	 * Check for overflow, copy, and we're done.
221*0Sstevel@tonic-gate 	 */
222*0Sstevel@tonic-gate 	if ((size_t)(tp - tmp) > size) {
223*0Sstevel@tonic-gate 		errno = ENOSPC;
224*0Sstevel@tonic-gate 		return (NULL);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 	strlcpy(dst, tmp, size);
227*0Sstevel@tonic-gate 	return (dst);
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate #endif /* !HAVE_INET_NTOP */
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
233