xref: /openbsd-src/sys/netinet/inet_ntop.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: inet_ntop.c,v 1.2 2014/05/20 09:16:43 mpi 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 <net/if.h>
24 #include <netinet/in.h>
25 
26 #define IN6ADDRSZ	16
27 #define INT16SZ		2
28 
29 /*
30  * WARNING: Don't even consider trying to compile this on a system where
31  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
32  */
33 
34 static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
35 #ifdef INET6
36 static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
37 #endif /* INET6 */
38 
39 /* char *
40  * inet_ntop(af, src, dst, size)
41  *	convert a network format address to presentation format.
42  * return:
43  *	pointer to presentation format address (`dst'), or NULL (see errno).
44  * author:
45  *	Paul Vixie, 1996.
46  */
47 const char *
48 inet_ntop(int af, const void *src, char *dst, socklen_t size)
49 {
50 	switch (af) {
51 	case AF_INET:
52 		return (inet_ntop4(src, dst, (size_t)size));
53 #ifdef INET6
54 	case AF_INET6:
55 		return (inet_ntop6(src, dst, (size_t)size));
56 #endif /* INET6 */
57 	default:
58 		return (NULL);
59 	}
60 	/* NOTREACHED */
61 }
62 
63 /* const char *
64  * inet_ntop4(src, dst, size)
65  *	format an IPv4 address, more or less like inet_ntoa()
66  * return:
67  *	`dst' (as a const)
68  * notes:
69  *	(1) uses no statics
70  *	(2) takes a u_char* not an in_addr as input
71  * author:
72  *	Paul Vixie, 1996.
73  */
74 static const char *
75 inet_ntop4(const u_char *src, char *dst, size_t size)
76 {
77 	char tmp[sizeof "255.255.255.255"];
78 	int l;
79 
80 	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
81 	    src[0], src[1], src[2], src[3]);
82 	if (l <= 0 || l >= size) {
83 		return (NULL);
84 	}
85 	strlcpy(dst, tmp, size);
86 	return (dst);
87 }
88 
89 #ifdef INET6
90 /* const char *
91  * inet_ntop6(src, dst, size)
92  *	convert IPv6 binary address into presentation (printable) format
93  * author:
94  *	Paul Vixie, 1996.
95  */
96 static const char *
97 inet_ntop6(const u_char *src, char *dst, size_t size)
98 {
99 	/*
100 	 * Note that int32_t and int16_t need only be "at least" large enough
101 	 * to contain a value of the specified size.  On some systems, like
102 	 * Crays, there is no such thing as an integer variable with 16 bits.
103 	 * Keep this in mind if you think this function should have been coded
104 	 * to use pointer overlays.  All the world's not a VAX.
105 	 */
106 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
107 	char *tp, *ep;
108 	struct { int base, len; } best, cur;
109 	u_int words[IN6ADDRSZ / INT16SZ];
110 	int i;
111 	int advance;
112 
113 	/*
114 	 * Preprocess:
115 	 *	Copy the input (bytewise) array into a wordwise array.
116 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
117 	 */
118 	memset(words, '\0', sizeof words);
119 	for (i = 0; i < IN6ADDRSZ; i++)
120 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
121 	best.base = -1;
122 	cur.base = -1;
123 	for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
124 		if (words[i] == 0) {
125 			if (cur.base == -1)
126 				cur.base = i, cur.len = 1;
127 			else
128 				cur.len++;
129 		} else {
130 			if (cur.base != -1) {
131 				if (best.base == -1 || cur.len > best.len)
132 					best = cur;
133 				cur.base = -1;
134 			}
135 		}
136 	}
137 	if (cur.base != -1) {
138 		if (best.base == -1 || cur.len > best.len)
139 			best = cur;
140 	}
141 	if (best.base != -1 && best.len < 2)
142 		best.base = -1;
143 
144 	/*
145 	 * Format the result.
146 	 */
147 	tp = tmp;
148 	ep = tmp + sizeof(tmp);
149 	for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
150 		/* Are we inside the best run of 0x00's? */
151 		if (best.base != -1 && i >= best.base &&
152 		    i < (best.base + best.len)) {
153 			if (i == best.base) {
154 				if (tp + 1 >= ep)
155 					return (NULL);
156 				*tp++ = ':';
157 			}
158 			continue;
159 		}
160 		/* Are we following an initial run of 0x00s or any real hex? */
161 		if (i != 0) {
162 			if (tp + 1 >= ep)
163 				return (NULL);
164 			*tp++ = ':';
165 		}
166 		/* Is this address an encapsulated IPv4? */
167 		if (i == 6 && best.base == 0 &&
168 		    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
169 			if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
170 				return (NULL);
171 			tp += strlen(tp);
172 			break;
173 		}
174 		advance = snprintf(tp, ep - tp, "%x", words[i]);
175 		if (advance <= 0 || advance >= ep - tp)
176 			return (NULL);
177 		tp += advance;
178 	}
179 	/* Was it a trailing run of 0x00's? */
180 	if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
181 		if (tp + 1 >= ep)
182 			return (NULL);
183 		*tp++ = ':';
184 	}
185 	if (tp + 1 >= ep)
186 		return (NULL);
187 	*tp++ = '\0';
188 
189 	/*
190 	 * Check for overflow, copy, and we're done.
191 	 */
192 	if ((size_t)(tp - tmp) > size) {
193 		return (NULL);
194 	}
195 	strlcpy(dst, tmp, size);
196 	return (dst);
197 }
198 #endif /* INET6 */
199