blob: 79d62327e08d04241f02cfde8382c479adbbea94 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 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 ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdio.h>
19#include <string.h>
20
21#if defined(_MSC_VER) && _MSC_VER < 1600
22# include "uv/stdint-msvc2008.h"
23#else
24# include <stdint.h>
25#endif
26
27#include "uv.h"
28#include "uv-common.h"
29
30#define UV__INET_ADDRSTRLEN 16
31#define UV__INET6_ADDRSTRLEN 46
32
33
34static int inet_ntop4(const unsigned char *src, char *dst, size_t size);
35static int inet_ntop6(const unsigned char *src, char *dst, size_t size);
36static int inet_pton4(const char *src, unsigned char *dst);
37static int inet_pton6(const char *src, unsigned char *dst);
38
39
40int uv_inet_ntop(int af, const void* src, char* dst, size_t size) {
41 switch (af) {
42 case AF_INET:
43 return (inet_ntop4((const unsigned char*)src, dst, size));
44 case AF_INET6:
45 return (inet_ntop6((const unsigned char*)src, dst, size));
46 default:
47 return UV_EAFNOSUPPORT;
48 }
49 /* NOTREACHED */
50}
51
52
53static int inet_ntop4(const unsigned char *src, char *dst, size_t size) {
54 static const char fmt[] = "%u.%u.%u.%u";
55 char tmp[UV__INET_ADDRSTRLEN];
56 int l;
57
58 l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
59 if (l <= 0 || (size_t) l >= size) {
60 return UV_ENOSPC;
61 }
62 strncpy(dst, tmp, size);
63 dst[size - 1] = '\0';
64 return 0;
65}
66
67
68static int inet_ntop6(const unsigned char *src, char *dst, size_t size) {
69 /*
70 * Note that int32_t and int16_t need only be "at least" large enough
71 * to contain a value of the specified size. On some systems, like
72 * Crays, there is no such thing as an integer variable with 16 bits.
73 * Keep this in mind if you think this function should have been coded
74 * to use pointer overlays. All the world's not a VAX.
75 */
76 char tmp[UV__INET6_ADDRSTRLEN], *tp;
77 struct { int base, len; } best, cur;
78 unsigned int words[sizeof(struct in6_addr) / sizeof(uint16_t)];
79 int i;
80
81 /*
82 * Preprocess:
83 * Copy the input (bytewise) array into a wordwise array.
84 * Find the longest run of 0x00's in src[] for :: shorthanding.
85 */
86 memset(words, '\0', sizeof words);
87 for (i = 0; i < (int) sizeof(struct in6_addr); i++)
88 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
89 best.base = -1;
90 best.len = 0;
91 cur.base = -1;
92 cur.len = 0;
93 for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
94 if (words[i] == 0) {
95 if (cur.base == -1)
96 cur.base = i, cur.len = 1;
97 else
98 cur.len++;
99 } else {
100 if (cur.base != -1) {
101 if (best.base == -1 || cur.len > best.len)
102 best = cur;
103 cur.base = -1;
104 }
105 }
106 }
107 if (cur.base != -1) {
108 if (best.base == -1 || cur.len > best.len)
109 best = cur;
110 }
111 if (best.base != -1 && best.len < 2)
112 best.base = -1;
113
114 /*
115 * Format the result.
116 */
117 tp = tmp;
118 for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
119 /* Are we inside the best run of 0x00's? */
120 if (best.base != -1 && i >= best.base &&
121 i < (best.base + best.len)) {
122 if (i == best.base)
123 *tp++ = ':';
124 continue;
125 }
126 /* Are we following an initial run of 0x00s or any real hex? */
127 if (i != 0)
128 *tp++ = ':';
129 /* Is this address an encapsulated IPv4? */
130 if (i == 6 && best.base == 0 && (best.len == 6 ||
131 (best.len == 7 && words[7] != 0x0001) ||
132 (best.len == 5 && words[5] == 0xffff))) {
133 int err = inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp));
134 if (err)
135 return err;
136 tp += strlen(tp);
137 break;
138 }
139 tp += sprintf(tp, "%x", words[i]);
140 }
141 /* Was it a trailing run of 0x00's? */
142 if (best.base != -1 && (best.base + best.len) == ARRAY_SIZE(words))
143 *tp++ = ':';
144 *tp++ = '\0';
145
146 /*
147 * Check for overflow, copy, and we're done.
148 */
149 if ((size_t)(tp - tmp) > size) {
150 return UV_ENOSPC;
151 }
152 strcpy(dst, tmp);
153 return 0;
154}
155
156
157int uv_inet_pton(int af, const char* src, void* dst) {
158 if (src == NULL || dst == NULL)
159 return UV_EINVAL;
160
161 switch (af) {
162 case AF_INET:
163 return (inet_pton4(src, (unsigned char*)dst));
164 case AF_INET6: {
165 int len;
166 char tmp[UV__INET6_ADDRSTRLEN], *s;
167 const char *p;
168 s = (char*) src;
169 p = strchr(src, '%');
170 if (p != NULL) {
171 s = tmp;
172 len = p - src;
173 if (len > UV__INET6_ADDRSTRLEN-1)
174 return UV_EINVAL;
175 memcpy(s, src, len);
176 s[len] = '\0';
177 }
178 return inet_pton6(s, (unsigned char*)dst);
179 }
180 default:
181 return UV_EAFNOSUPPORT;
182 }
183 /* NOTREACHED */
184}
185
186
187static int inet_pton4(const char *src, unsigned char *dst) {
188 static const char digits[] = "0123456789";
189 int saw_digit, octets, ch;
190 unsigned char tmp[sizeof(struct in_addr)], *tp;
191
192 saw_digit = 0;
193 octets = 0;
194 *(tp = tmp) = 0;
195 while ((ch = *src++) != '\0') {
196 const char *pch;
197
198 if ((pch = strchr(digits, ch)) != NULL) {
199 unsigned int nw = *tp * 10 + (pch - digits);
200
201 if (saw_digit && *tp == 0)
202 return UV_EINVAL;
203 if (nw > 255)
204 return UV_EINVAL;
205 *tp = nw;
206 if (!saw_digit) {
207 if (++octets > 4)
208 return UV_EINVAL;
209 saw_digit = 1;
210 }
211 } else if (ch == '.' && saw_digit) {
212 if (octets == 4)
213 return UV_EINVAL;
214 *++tp = 0;
215 saw_digit = 0;
216 } else
217 return UV_EINVAL;
218 }
219 if (octets < 4)
220 return UV_EINVAL;
221 memcpy(dst, tmp, sizeof(struct in_addr));
222 return 0;
223}
224
225
226static int inet_pton6(const char *src, unsigned char *dst) {
227 static const char xdigits_l[] = "0123456789abcdef",
228 xdigits_u[] = "0123456789ABCDEF";
229 unsigned char tmp[sizeof(struct in6_addr)], *tp, *endp, *colonp;
230 const char *xdigits, *curtok;
231 int ch, seen_xdigits;
232 unsigned int val;
233
234 memset((tp = tmp), '\0', sizeof tmp);
235 endp = tp + sizeof tmp;
236 colonp = NULL;
237 /* Leading :: requires some special handling. */
238 if (*src == ':')
239 if (*++src != ':')
240 return UV_EINVAL;
241 curtok = src;
242 seen_xdigits = 0;
243 val = 0;
244 while ((ch = *src++) != '\0') {
245 const char *pch;
246
247 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
248 pch = strchr((xdigits = xdigits_u), ch);
249 if (pch != NULL) {
250 val <<= 4;
251 val |= (pch - xdigits);
252 if (++seen_xdigits > 4)
253 return UV_EINVAL;
254 continue;
255 }
256 if (ch == ':') {
257 curtok = src;
258 if (!seen_xdigits) {
259 if (colonp)
260 return UV_EINVAL;
261 colonp = tp;
262 continue;
263 } else if (*src == '\0') {
264 return UV_EINVAL;
265 }
266 if (tp + sizeof(uint16_t) > endp)
267 return UV_EINVAL;
268 *tp++ = (unsigned char) (val >> 8) & 0xff;
269 *tp++ = (unsigned char) val & 0xff;
270 seen_xdigits = 0;
271 val = 0;
272 continue;
273 }
274 if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) {
275 int err = inet_pton4(curtok, tp);
276 if (err == 0) {
277 tp += sizeof(struct in_addr);
278 seen_xdigits = 0;
279 break; /*%< '\\0' was seen by inet_pton4(). */
280 }
281 }
282 return UV_EINVAL;
283 }
284 if (seen_xdigits) {
285 if (tp + sizeof(uint16_t) > endp)
286 return UV_EINVAL;
287 *tp++ = (unsigned char) (val >> 8) & 0xff;
288 *tp++ = (unsigned char) val & 0xff;
289 }
290 if (colonp != NULL) {
291 /*
292 * Since some memmove()'s erroneously fail to handle
293 * overlapping regions, we'll do the shift by hand.
294 */
295 const int n = tp - colonp;
296 int i;
297
298 if (tp == endp)
299 return UV_EINVAL;
300 for (i = 1; i <= n; i++) {
301 endp[- i] = colonp[n - i];
302 colonp[n - i] = 0;
303 }
304 tp = endp;
305 }
306 if (tp != endp)
307 return UV_EINVAL;
308 memcpy(dst, tmp, sizeof tmp);
309 return 0;
310}