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