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