1238106Sdes/* From openssh 4.3p2 compat/inet_ntop.c */
2238106Sdes/* Copyright (c) 1996 by Internet Software Consortium.
3238106Sdes *
4238106Sdes * Permission to use, copy, modify, and distribute this software for any
5238106Sdes * purpose with or without fee is hereby granted, provided that the above
6238106Sdes * copyright notice and this permission notice appear in all copies.
7238106Sdes *
8238106Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9238106Sdes * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10238106Sdes * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11238106Sdes * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12238106Sdes * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13238106Sdes * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14238106Sdes * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15238106Sdes * SOFTWARE.
16238106Sdes */
17238106Sdes
18238106Sdes/* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */
19238106Sdes
20238106Sdes#include <config.h>
21238106Sdes
22238106Sdes#ifndef HAVE_INET_NTOP
23238106Sdes
24238106Sdes#include <sys/param.h>
25238106Sdes#include <sys/types.h>
26238106Sdes#ifdef HAVE_SYS_SOCKET_H
27238106Sdes#include <sys/socket.h>
28238106Sdes#endif
29238106Sdes#ifdef HAVE_NETINET_IN_H
30238106Sdes#include <netinet/in.h>
31238106Sdes#endif
32238106Sdes#include <string.h>
33238106Sdes#include <errno.h>
34238106Sdes#include <stdio.h>
35238106Sdes
36238106Sdes#ifndef IN6ADDRSZ
37238106Sdes#define IN6ADDRSZ   16   /* IPv6 T_AAAA */
38238106Sdes#endif
39238106Sdes
40238106Sdes#ifndef INT16SZ
41238106Sdes#define INT16SZ     2    /* for systems without 16-bit ints */
42238106Sdes#endif
43238106Sdes
44238106Sdes/*
45238106Sdes * WARNING: Don't even consider trying to compile this on a system where
46238106Sdes * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
47238106Sdes */
48238106Sdes
49238106Sdesstatic const char *inet_ntop4(const u_char *src, char *dst, size_t size);
50238106Sdesstatic const char *inet_ntop6(const u_char *src, char *dst, size_t size);
51238106Sdes
52238106Sdes/* char *
53238106Sdes * inet_ntop(af, src, dst, size)
54238106Sdes *	convert a network format address to presentation format.
55238106Sdes * return:
56238106Sdes *	pointer to presentation format address (`dst'), or NULL (see errno).
57238106Sdes * author:
58238106Sdes *	Paul Vixie, 1996.
59238106Sdes */
60238106Sdesconst char *
61238106Sdesinet_ntop(int af, const void *src, char *dst, size_t size)
62238106Sdes{
63238106Sdes	switch (af) {
64238106Sdes	case AF_INET:
65238106Sdes		return (inet_ntop4(src, dst, size));
66238106Sdes	case AF_INET6:
67238106Sdes		return (inet_ntop6(src, dst, size));
68238106Sdes	default:
69238106Sdes#ifdef EAFNOSUPPORT
70238106Sdes		errno = EAFNOSUPPORT;
71238106Sdes#else
72238106Sdes		errno = ENOSYS;
73238106Sdes#endif
74238106Sdes		return (NULL);
75238106Sdes	}
76238106Sdes	/* NOTREACHED */
77238106Sdes}
78238106Sdes
79238106Sdes/* const char *
80238106Sdes * inet_ntop4(src, dst, size)
81238106Sdes *	format an IPv4 address, more or less like inet_ntoa()
82238106Sdes * return:
83238106Sdes *	`dst' (as a const)
84238106Sdes * notes:
85238106Sdes *	(1) uses no statics
86238106Sdes *	(2) takes a u_char* not an in_addr as input
87238106Sdes * author:
88238106Sdes *	Paul Vixie, 1996.
89238106Sdes */
90238106Sdesstatic const char *
91238106Sdesinet_ntop4(const u_char *src, char *dst, size_t size)
92238106Sdes{
93238106Sdes	static const char fmt[] = "%u.%u.%u.%u";
94238106Sdes	char tmp[sizeof "255.255.255.255"];
95238106Sdes	int l;
96238106Sdes
97238106Sdes	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
98238106Sdes	if (l <= 0 || l >= (int)size) {
99238106Sdes		errno = ENOSPC;
100238106Sdes		return (NULL);
101238106Sdes	}
102238106Sdes	strlcpy(dst, tmp, size);
103238106Sdes	return (dst);
104238106Sdes}
105238106Sdes
106238106Sdes/* const char *
107238106Sdes * inet_ntop6(src, dst, size)
108238106Sdes *	convert IPv6 binary address into presentation (printable) format
109238106Sdes * author:
110238106Sdes *	Paul Vixie, 1996.
111238106Sdes */
112238106Sdesstatic const char *
113238106Sdesinet_ntop6(const u_char *src, char *dst, size_t size)
114238106Sdes{
115238106Sdes	/*
116238106Sdes	 * Note that int32_t and int16_t need only be "at least" large enough
117238106Sdes	 * to contain a value of the specified size.  On some systems, like
118238106Sdes	 * Crays, there is no such thing as an integer variable with 16 bits.
119238106Sdes	 * Keep this in mind if you think this function should have been coded
120238106Sdes	 * to use pointer overlays.  All the world's not a VAX.
121238106Sdes	 */
122238106Sdes	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
123238106Sdes	char *tp, *ep;
124238106Sdes	struct { int base, len; } best, cur;
125238106Sdes	u_int words[IN6ADDRSZ / INT16SZ];
126238106Sdes	int i;
127238106Sdes	int advance;
128238106Sdes
129238106Sdes	/*
130238106Sdes	 * Preprocess:
131238106Sdes	 *	Copy the input (bytewise) array into a wordwise array.
132238106Sdes	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
133238106Sdes	 */
134238106Sdes	memset(words, '\0', sizeof words);
135238106Sdes	for (i = 0; i < IN6ADDRSZ; i++)
136238106Sdes		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
137238106Sdes	best.base = -1;
138238106Sdes	best.len = 0;
139238106Sdes	cur.base = -1;
140238106Sdes	cur.len = 0;
141238106Sdes	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
142238106Sdes		if (words[i] == 0) {
143238106Sdes			if (cur.base == -1)
144238106Sdes				cur.base = i, cur.len = 1;
145238106Sdes			else
146238106Sdes				cur.len++;
147238106Sdes		} else {
148238106Sdes			if (cur.base != -1) {
149238106Sdes				if (best.base == -1 || cur.len > best.len)
150238106Sdes					best = cur;
151238106Sdes				cur.base = -1;
152238106Sdes			}
153238106Sdes		}
154238106Sdes	}
155238106Sdes	if (cur.base != -1) {
156238106Sdes		if (best.base == -1 || cur.len > best.len)
157238106Sdes			best = cur;
158238106Sdes	}
159238106Sdes	if (best.base != -1 && best.len < 2)
160238106Sdes		best.base = -1;
161238106Sdes
162238106Sdes	/*
163238106Sdes	 * Format the result.
164238106Sdes	 */
165238106Sdes	tp = tmp;
166238106Sdes	ep = tmp + sizeof(tmp);
167238106Sdes	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
168238106Sdes		/* Are we inside the best run of 0x00's? */
169238106Sdes		if (best.base != -1 && i >= best.base &&
170238106Sdes		    i < (best.base + best.len)) {
171238106Sdes			if (i == best.base) {
172238106Sdes				if (tp + 1 >= ep)
173238106Sdes					return (NULL);
174238106Sdes				*tp++ = ':';
175238106Sdes			}
176238106Sdes			continue;
177238106Sdes		}
178238106Sdes		/* Are we following an initial run of 0x00s or any real hex? */
179238106Sdes		if (i != 0) {
180238106Sdes			if (tp + 1 >= ep)
181238106Sdes				return (NULL);
182238106Sdes			*tp++ = ':';
183238106Sdes		}
184238106Sdes		/* Is this address an encapsulated IPv4? */
185238106Sdes		if (i == 6 && best.base == 0 &&
186238106Sdes		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
187238106Sdes			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
188238106Sdes				return (NULL);
189238106Sdes			tp += strlen(tp);
190238106Sdes			break;
191238106Sdes		}
192238106Sdes		advance = snprintf(tp, ep - tp, "%x", words[i]);
193238106Sdes		if (advance <= 0 || advance >= ep - tp)
194238106Sdes			return (NULL);
195238106Sdes		tp += advance;
196238106Sdes	}
197238106Sdes	/* Was it a trailing run of 0x00's? */
198238106Sdes	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
199238106Sdes		if (tp + 1 >= ep)
200238106Sdes			return (NULL);
201238106Sdes		*tp++ = ':';
202238106Sdes	}
203238106Sdes	if (tp + 1 >= ep)
204238106Sdes		return (NULL);
205238106Sdes	*tp++ = '\0';
206238106Sdes
207238106Sdes	/*
208238106Sdes	 * Check for overflow, copy, and we're done.
209238106Sdes	 */
210238106Sdes	if ((size_t)(tp - tmp) > size) {
211238106Sdes		errno = ENOSPC;
212238106Sdes		return (NULL);
213238106Sdes	}
214238106Sdes	strlcpy(dst, tmp, size);
215238106Sdes	return (dst);
216238106Sdes}
217238106Sdes
218238106Sdes#endif /* !HAVE_INET_NTOP */
219