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