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