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