1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * Copyright (c) 2013 Martin Decky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file Internet address parsing and formatting.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <inet/addr.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <stddef.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <bitops.h>
|
---|
43 | #include <inttypes.h>
|
---|
44 | #include <str.h>
|
---|
45 |
|
---|
46 | #define INET_PREFIXSTRSIZE 5
|
---|
47 |
|
---|
48 | #define INET6_ADDRSTRLEN (8 * 4 + 7 + 1)
|
---|
49 |
|
---|
50 | #if !(defined(__BE__) ^ defined(__LE__))
|
---|
51 | #error The architecture must be either big-endian or little-endian.
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | const addr32_t addr32_broadcast_all_hosts = 0xffffffff;
|
---|
55 |
|
---|
56 | const addr48_t addr48_broadcast = {
|
---|
57 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
---|
58 | };
|
---|
59 |
|
---|
60 | static const addr48_t inet_addr48_solicited_node = {
|
---|
61 | 0x33, 0x33, 0xff, 0, 0, 0
|
---|
62 | };
|
---|
63 |
|
---|
64 | static const inet_addr_t inet_addr_any_addr = {
|
---|
65 | .version = ip_v4,
|
---|
66 | .addr = 0
|
---|
67 | };
|
---|
68 |
|
---|
69 | static const inet_addr_t inet_addr_any_addr6 = {
|
---|
70 | .version = ip_v6,
|
---|
71 | .addr6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
---|
72 | };
|
---|
73 |
|
---|
74 | void addr128(const addr128_t src, addr128_t dst)
|
---|
75 | {
|
---|
76 | memcpy(dst, src, 16);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Compare addr48.
|
---|
80 | *
|
---|
81 | * @return Non-zero if equal, zero if not equal.
|
---|
82 | */
|
---|
83 | int addr48_compare(const addr48_t *a, const addr48_t *b)
|
---|
84 | {
|
---|
85 | return memcmp(a->b, b->b, 6) == 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Compare addr128.
|
---|
89 | *
|
---|
90 | * @return Non-zero if equal, zero if not equal.
|
---|
91 | */
|
---|
92 | int addr128_compare(const addr128_t a, const addr128_t b)
|
---|
93 | {
|
---|
94 | return memcmp(a, b, 16) == 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Compute solicited node MAC multicast address from target IPv6 address
|
---|
98 | *
|
---|
99 | * @param ip Target IPv6 address
|
---|
100 | * @param mac Solicited MAC address to be assigned
|
---|
101 | *
|
---|
102 | */
|
---|
103 | void addr48_solicited_node(const addr128_t ip, addr48_t *mac)
|
---|
104 | {
|
---|
105 | memcpy(&mac->b[0], &inet_addr48_solicited_node.b[0], 3);
|
---|
106 | memcpy(&mac->b[3], ip + 13, 3);
|
---|
107 | }
|
---|
108 |
|
---|
109 | void host2addr128_t_be(const addr128_t host, addr128_t be)
|
---|
110 | {
|
---|
111 | memcpy(be, host, 16);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void addr128_t_be2host(const addr128_t be, addr128_t host)
|
---|
115 | {
|
---|
116 | memcpy(host, be, 16);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void inet_addr(inet_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d)
|
---|
120 | {
|
---|
121 | addr->version = ip_v4;
|
---|
122 | addr->addr = ((addr32_t) a << 24) | ((addr32_t) b << 16) |
|
---|
123 | ((addr32_t) c << 8) | ((addr32_t) d);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void inet_naddr(inet_naddr_t *naddr, uint8_t a, uint8_t b, uint8_t c, uint8_t d,
|
---|
127 | uint8_t prefix)
|
---|
128 | {
|
---|
129 | naddr->version = ip_v4;
|
---|
130 | naddr->addr = ((addr32_t) a << 24) | ((addr32_t) b << 16) |
|
---|
131 | ((addr32_t) c << 8) | ((addr32_t) d);
|
---|
132 | naddr->prefix = prefix;
|
---|
133 | }
|
---|
134 |
|
---|
135 | void inet_addr6(inet_addr_t *addr, uint16_t a, uint16_t b, uint16_t c,
|
---|
136 | uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h)
|
---|
137 | {
|
---|
138 | addr->version = ip_v6;
|
---|
139 | addr->addr6[0] = (a >> 8) & 0xff;
|
---|
140 | addr->addr6[1] = a & 0xff;
|
---|
141 | addr->addr6[2] = (b >> 8) & 0xff;
|
---|
142 | addr->addr6[3] = b & 0xff;
|
---|
143 | addr->addr6[4] = (c >> 8) & 0xff;
|
---|
144 | addr->addr6[5] = c & 0xff;
|
---|
145 | addr->addr6[6] = (d >> 8) & 0xff;
|
---|
146 | addr->addr6[7] = d & 0xff;
|
---|
147 | addr->addr6[8] = (e >> 8) & 0xff;
|
---|
148 | addr->addr6[9] = e & 0xff;
|
---|
149 | addr->addr6[10] = (f >> 8) & 0xff;
|
---|
150 | addr->addr6[11] = f & 0xff;
|
---|
151 | addr->addr6[12] = (g >> 8) & 0xff;
|
---|
152 | addr->addr6[13] = g & 0xff;
|
---|
153 | addr->addr6[14] = (h >> 8) & 0xff;
|
---|
154 | addr->addr6[15] = h & 0xff;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void inet_naddr6(inet_naddr_t *naddr, uint16_t a, uint16_t b, uint16_t c,
|
---|
158 | uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h, uint8_t prefix)
|
---|
159 | {
|
---|
160 | naddr->version = ip_v6;
|
---|
161 | naddr->addr6[0] = (a >> 8) & 0xff;
|
---|
162 | naddr->addr6[1] = a & 0xff;
|
---|
163 | naddr->addr6[2] = (b >> 8) & 0xff;
|
---|
164 | naddr->addr6[3] = b & 0xff;
|
---|
165 | naddr->addr6[4] = (c >> 8) & 0xff;
|
---|
166 | naddr->addr6[5] = c & 0xff;
|
---|
167 | naddr->addr6[6] = (d >> 8) & 0xff;
|
---|
168 | naddr->addr6[7] = d & 0xff;
|
---|
169 | naddr->addr6[8] = (e >> 8) & 0xff;
|
---|
170 | naddr->addr6[9] = e & 0xff;
|
---|
171 | naddr->addr6[10] = (f >> 8) & 0xff;
|
---|
172 | naddr->addr6[11] = f & 0xff;
|
---|
173 | naddr->addr6[12] = (g >> 8) & 0xff;
|
---|
174 | naddr->addr6[13] = g & 0xff;
|
---|
175 | naddr->addr6[14] = (h >> 8) & 0xff;
|
---|
176 | naddr->addr6[15] = h & 0xff;
|
---|
177 | naddr->prefix = prefix;
|
---|
178 | }
|
---|
179 |
|
---|
180 | void inet_naddr_addr(const inet_naddr_t *naddr, inet_addr_t *addr)
|
---|
181 | {
|
---|
182 | addr->version = naddr->version;
|
---|
183 | memcpy(addr->addr6, naddr->addr6, 16);
|
---|
184 | }
|
---|
185 |
|
---|
186 | void inet_addr_naddr(const inet_addr_t *addr, uint8_t prefix,
|
---|
187 | inet_naddr_t *naddr)
|
---|
188 | {
|
---|
189 | naddr->version = addr->version;
|
---|
190 | memcpy(naddr->addr6, addr->addr6, 16);
|
---|
191 | naddr->prefix = prefix;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void inet_addr_any(inet_addr_t *addr)
|
---|
195 | {
|
---|
196 | addr->version = ip_any;
|
---|
197 | memset(addr->addr6, 0, 16);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void inet_naddr_any(inet_naddr_t *naddr)
|
---|
201 | {
|
---|
202 | naddr->version = ip_any;
|
---|
203 | memset(naddr->addr6, 0, 16);
|
---|
204 | naddr->prefix = 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | int inet_addr_compare(const inet_addr_t *a, const inet_addr_t *b)
|
---|
208 | {
|
---|
209 | if (a->version != b->version)
|
---|
210 | return 0;
|
---|
211 |
|
---|
212 | switch (a->version) {
|
---|
213 | case ip_v4:
|
---|
214 | return (a->addr == b->addr);
|
---|
215 | case ip_v6:
|
---|
216 | return addr128_compare(a->addr6, b->addr6);
|
---|
217 | default:
|
---|
218 | return 0;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | int inet_addr_is_any(const inet_addr_t *addr)
|
---|
223 | {
|
---|
224 | return ((addr->version == ip_any) ||
|
---|
225 | (inet_addr_compare(addr, &inet_addr_any_addr)) ||
|
---|
226 | (inet_addr_compare(addr, &inet_addr_any_addr6)));
|
---|
227 | }
|
---|
228 |
|
---|
229 | int inet_naddr_compare(const inet_naddr_t *naddr, const inet_addr_t *addr)
|
---|
230 | {
|
---|
231 | if (naddr->version != addr->version)
|
---|
232 | return 0;
|
---|
233 |
|
---|
234 | switch (naddr->version) {
|
---|
235 | case ip_v4:
|
---|
236 | return (naddr->addr == addr->addr);
|
---|
237 | case ip_v6:
|
---|
238 | return addr128_compare(naddr->addr6, addr->addr6);
|
---|
239 | default:
|
---|
240 | return 0;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | int inet_naddr_compare_mask(const inet_naddr_t *naddr, const inet_addr_t *addr)
|
---|
245 | {
|
---|
246 | if (naddr->version != addr->version)
|
---|
247 | return 0;
|
---|
248 |
|
---|
249 | switch (naddr->version) {
|
---|
250 | case ip_v4:
|
---|
251 | if (naddr->prefix > 32)
|
---|
252 | return 0;
|
---|
253 |
|
---|
254 | addr32_t mask =
|
---|
255 | BIT_RANGE(addr32_t, 31, 31 - (naddr->prefix - 1));
|
---|
256 | return ((naddr->addr & mask) == (addr->addr & mask));
|
---|
257 | case ip_v6:
|
---|
258 | if (naddr->prefix > 128)
|
---|
259 | return 0;
|
---|
260 |
|
---|
261 | size_t pos = 0;
|
---|
262 | for (size_t i = 0; i < 16; i++) {
|
---|
263 | /* Further bits do not matter */
|
---|
264 | if (naddr->prefix < pos)
|
---|
265 | break;
|
---|
266 |
|
---|
267 | if (naddr->prefix - pos > 8) {
|
---|
268 | /* Comparison without masking */
|
---|
269 | if (naddr->addr6[i] != addr->addr6[i])
|
---|
270 | return 0;
|
---|
271 | } else {
|
---|
272 | /* Comparison with masking */
|
---|
273 | uint8_t mask =
|
---|
274 | BIT_RANGE(uint8_t, 8, 8 - (naddr->prefix - pos - 1));
|
---|
275 | if ((naddr->addr6[i] & mask) != (addr->addr6[i] & mask))
|
---|
276 | return 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | pos += 8;
|
---|
280 | }
|
---|
281 |
|
---|
282 | return 1;
|
---|
283 | default:
|
---|
284 | return 0;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | static errno_t inet_addr_parse_v4(const char *str, inet_addr_t *raddr,
|
---|
289 | int *prefix, char **endptr)
|
---|
290 | {
|
---|
291 | uint32_t a = 0;
|
---|
292 | uint8_t b;
|
---|
293 | char *cur = (char *)str;
|
---|
294 | size_t i = 0;
|
---|
295 |
|
---|
296 | while (i < 4) {
|
---|
297 | errno_t rc = str_uint8_t(cur, (const char **)&cur, 10, false, &b);
|
---|
298 | if (rc != EOK)
|
---|
299 | return rc;
|
---|
300 |
|
---|
301 | a = (a << 8) + b;
|
---|
302 |
|
---|
303 | i++;
|
---|
304 |
|
---|
305 | if (*cur != '.')
|
---|
306 | break;
|
---|
307 |
|
---|
308 | if (i < 4)
|
---|
309 | cur++;
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (prefix != NULL) {
|
---|
313 | if (*cur != '/')
|
---|
314 | return EINVAL;
|
---|
315 | cur++;
|
---|
316 |
|
---|
317 | *prefix = strtoul(cur, &cur, 10);
|
---|
318 | if (*prefix > 32)
|
---|
319 | return EINVAL;
|
---|
320 | }
|
---|
321 |
|
---|
322 | if (i != 4)
|
---|
323 | return EINVAL;
|
---|
324 |
|
---|
325 | if (endptr == NULL && *cur != '\0')
|
---|
326 | return EINVAL;
|
---|
327 |
|
---|
328 | raddr->version = ip_v4;
|
---|
329 | raddr->addr = a;
|
---|
330 |
|
---|
331 | if (endptr != NULL)
|
---|
332 | *endptr = cur;
|
---|
333 |
|
---|
334 | return EOK;
|
---|
335 | }
|
---|
336 |
|
---|
337 | static errno_t inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix,
|
---|
338 | char **endptr)
|
---|
339 | {
|
---|
340 | uint8_t data[16];
|
---|
341 | int explicit_groups;
|
---|
342 |
|
---|
343 | memset(data, 0, 16);
|
---|
344 |
|
---|
345 | const char *cur = str;
|
---|
346 | size_t i = 0;
|
---|
347 | size_t wildcard_pos = (size_t) -1;
|
---|
348 | size_t wildcard_size = 0;
|
---|
349 |
|
---|
350 | /* Handle initial wildcard */
|
---|
351 | if ((str[0] == ':') && (str[1] == ':')) {
|
---|
352 | cur = str + 2;
|
---|
353 | wildcard_pos = 0;
|
---|
354 | wildcard_size = 16;
|
---|
355 | }
|
---|
356 |
|
---|
357 | while (i < 16) {
|
---|
358 | uint16_t bioctet;
|
---|
359 | const char *gend;
|
---|
360 | errno_t rc = str_uint16_t(cur, &gend, 16, false, &bioctet);
|
---|
361 | if (rc != EOK)
|
---|
362 | break;
|
---|
363 |
|
---|
364 | data[i] = (bioctet >> 8) & 0xff;
|
---|
365 | data[i + 1] = bioctet & 0xff;
|
---|
366 |
|
---|
367 | if (wildcard_pos != (size_t) -1) {
|
---|
368 | if (wildcard_size < 2)
|
---|
369 | return EINVAL;
|
---|
370 |
|
---|
371 | wildcard_size -= 2;
|
---|
372 | }
|
---|
373 |
|
---|
374 | i += 2;
|
---|
375 |
|
---|
376 | if (*gend != ':') {
|
---|
377 | cur = gend;
|
---|
378 | break;
|
---|
379 | }
|
---|
380 |
|
---|
381 | if (i < 16) {
|
---|
382 | /* Handle wildcard */
|
---|
383 | if (gend[1] == ':') {
|
---|
384 | if (wildcard_pos != (size_t) -1)
|
---|
385 | return EINVAL;
|
---|
386 |
|
---|
387 | wildcard_pos = i;
|
---|
388 | wildcard_size = 16 - i;
|
---|
389 | cur = gend + 2;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | /* Number of explicitly specified groups */
|
---|
395 | explicit_groups = i;
|
---|
396 |
|
---|
397 | if (prefix != NULL) {
|
---|
398 | if (*cur != '/')
|
---|
399 | return EINVAL;
|
---|
400 | cur++;
|
---|
401 |
|
---|
402 | *prefix = strtoul(cur, (char **)&cur, 10);
|
---|
403 | if (*prefix > 128)
|
---|
404 | return EINVAL;
|
---|
405 | }
|
---|
406 |
|
---|
407 | if (endptr == NULL && *cur != '\0')
|
---|
408 | return EINVAL;
|
---|
409 |
|
---|
410 | /* Create wildcard positions */
|
---|
411 | if ((wildcard_pos != (size_t) -1) && (wildcard_size > 0)) {
|
---|
412 | size_t wildcard_shift = 16 - wildcard_size;
|
---|
413 |
|
---|
414 | for (i = wildcard_pos + wildcard_shift; i > wildcard_pos; i--) {
|
---|
415 | size_t j = i - 1;
|
---|
416 | data[j + wildcard_size] = data[j];
|
---|
417 | data[j] = 0;
|
---|
418 | }
|
---|
419 | } else {
|
---|
420 | /* Verify that all groups have been specified */
|
---|
421 | if (explicit_groups != 16)
|
---|
422 | return EINVAL;
|
---|
423 | }
|
---|
424 |
|
---|
425 | raddr->version = ip_v6;
|
---|
426 | memcpy(raddr->addr6, data, 16);
|
---|
427 | if (endptr != NULL)
|
---|
428 | *endptr = (char *)cur;
|
---|
429 | return EOK;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /** Parse node address.
|
---|
433 | *
|
---|
434 | * Will fail if @a text contains extra characters at the and and @a endptr
|
---|
435 | * is @c NULL.
|
---|
436 | *
|
---|
437 | * @param text Network address in common notation.
|
---|
438 | * @param addr Place to store node address.
|
---|
439 | * @param endptr Place to store pointer to next character oc @c NULL
|
---|
440 | *
|
---|
441 | * @return EOK on success, EINVAL if input is not in valid format.
|
---|
442 | *
|
---|
443 | */
|
---|
444 | errno_t inet_addr_parse(const char *text, inet_addr_t *addr, char **endptr)
|
---|
445 | {
|
---|
446 | errno_t rc;
|
---|
447 |
|
---|
448 | rc = inet_addr_parse_v4(text, addr, NULL, endptr);
|
---|
449 | if (rc == EOK)
|
---|
450 | return EOK;
|
---|
451 |
|
---|
452 | rc = inet_addr_parse_v6(text, addr, NULL, endptr);
|
---|
453 | if (rc == EOK)
|
---|
454 | return EOK;
|
---|
455 |
|
---|
456 | return EINVAL;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /** Parse network address.
|
---|
460 | *
|
---|
461 | * Will fail if @a text contains extra characters at the and and @a endptr
|
---|
462 | * is @c NULL.
|
---|
463 | *
|
---|
464 | * @param text Network address in common notation.
|
---|
465 | * @param naddr Place to store network address.
|
---|
466 | * @param endptr Place to store pointer to next character oc @c NULL
|
---|
467 | *
|
---|
468 | * @return EOK on success, EINVAL if input is not in valid format.
|
---|
469 | *
|
---|
470 | */
|
---|
471 | errno_t inet_naddr_parse(const char *text, inet_naddr_t *naddr, char **endptr)
|
---|
472 | {
|
---|
473 | errno_t rc;
|
---|
474 | inet_addr_t addr;
|
---|
475 | int prefix;
|
---|
476 |
|
---|
477 | rc = inet_addr_parse_v4(text, &addr, &prefix, endptr);
|
---|
478 | if (rc == EOK) {
|
---|
479 | inet_addr_naddr(&addr, prefix, naddr);
|
---|
480 | return EOK;
|
---|
481 | }
|
---|
482 |
|
---|
483 | rc = inet_addr_parse_v6(text, &addr, &prefix, endptr);
|
---|
484 | if (rc == EOK) {
|
---|
485 | inet_addr_naddr(&addr, prefix, naddr);
|
---|
486 | return EOK;
|
---|
487 | }
|
---|
488 |
|
---|
489 | return EINVAL;
|
---|
490 | }
|
---|
491 |
|
---|
492 | static errno_t inet_addr_format_v4(addr32_t addr, char **bufp)
|
---|
493 | {
|
---|
494 | int rc;
|
---|
495 |
|
---|
496 | rc = asprintf(bufp, "%u.%u.%u.%u", (addr >> 24) & 0xff,
|
---|
497 | (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
|
---|
498 | if (rc < 0)
|
---|
499 | return ENOMEM;
|
---|
500 |
|
---|
501 | return EOK;
|
---|
502 | }
|
---|
503 |
|
---|
504 | static errno_t inet_addr_format_v6(const addr128_t addr, char **bufp)
|
---|
505 | {
|
---|
506 | *bufp = (char *) malloc(INET6_ADDRSTRLEN);
|
---|
507 | if (*bufp == NULL)
|
---|
508 | return ENOMEM;
|
---|
509 |
|
---|
510 | /* Find the longest zero subsequence */
|
---|
511 |
|
---|
512 | uint16_t zeroes[8];
|
---|
513 | uint16_t bioctets[8];
|
---|
514 |
|
---|
515 | for (size_t i = 8; i > 0; i--) {
|
---|
516 | size_t j = i - 1;
|
---|
517 |
|
---|
518 | bioctets[j] = (addr[j << 1] << 8) | addr[(j << 1) + 1];
|
---|
519 |
|
---|
520 | if (bioctets[j] == 0) {
|
---|
521 | zeroes[j] = 1;
|
---|
522 | if (j < 7)
|
---|
523 | zeroes[j] += zeroes[j + 1];
|
---|
524 | } else
|
---|
525 | zeroes[j] = 0;
|
---|
526 | }
|
---|
527 |
|
---|
528 | size_t wildcard_pos = (size_t) -1;
|
---|
529 | size_t wildcard_size = 0;
|
---|
530 |
|
---|
531 | for (size_t i = 0; i < 8; i++) {
|
---|
532 | if (zeroes[i] > wildcard_size) {
|
---|
533 | wildcard_pos = i;
|
---|
534 | wildcard_size = zeroes[i];
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | char *cur = *bufp;
|
---|
539 | size_t rest = INET6_ADDRSTRLEN;
|
---|
540 | bool tail_zero = false;
|
---|
541 | int ret;
|
---|
542 |
|
---|
543 | for (size_t i = 0; i < 8; i++) {
|
---|
544 | if ((i == wildcard_pos) && (wildcard_size > 1)) {
|
---|
545 | ret = snprintf(cur, rest, ":");
|
---|
546 | i += wildcard_size - 1;
|
---|
547 | tail_zero = true;
|
---|
548 | } else if (i == 0) {
|
---|
549 | ret = snprintf(cur, rest, "%" PRIx16, bioctets[i]);
|
---|
550 | tail_zero = false;
|
---|
551 | } else {
|
---|
552 | ret = snprintf(cur, rest, ":%" PRIx16, bioctets[i]);
|
---|
553 | tail_zero = false;
|
---|
554 | }
|
---|
555 |
|
---|
556 | if (ret < 0)
|
---|
557 | return EINVAL;
|
---|
558 |
|
---|
559 | cur += ret;
|
---|
560 | rest -= ret;
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (tail_zero)
|
---|
564 | (void) snprintf(cur, rest, ":");
|
---|
565 |
|
---|
566 | return EOK;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /** Format node address.
|
---|
570 | *
|
---|
571 | * @param addr Node address.
|
---|
572 | * @param bufp Place to store pointer to formatted string.
|
---|
573 | *
|
---|
574 | * @return EOK on success.
|
---|
575 | * @return ENOMEM if out of memory.
|
---|
576 | * @return ENOTSUP on unsupported address family.
|
---|
577 | *
|
---|
578 | */
|
---|
579 | errno_t inet_addr_format(const inet_addr_t *addr, char **bufp)
|
---|
580 | {
|
---|
581 | errno_t rc;
|
---|
582 | int ret;
|
---|
583 |
|
---|
584 | rc = ENOTSUP;
|
---|
585 |
|
---|
586 | switch (addr->version) {
|
---|
587 | case ip_any:
|
---|
588 | ret = asprintf(bufp, "none");
|
---|
589 | if (ret < 0)
|
---|
590 | return ENOMEM;
|
---|
591 | rc = EOK;
|
---|
592 | break;
|
---|
593 | case ip_v4:
|
---|
594 | rc = inet_addr_format_v4(addr->addr, bufp);
|
---|
595 | break;
|
---|
596 | case ip_v6:
|
---|
597 | rc = inet_addr_format_v6(addr->addr6, bufp);
|
---|
598 | break;
|
---|
599 | }
|
---|
600 |
|
---|
601 | return rc;
|
---|
602 | }
|
---|
603 |
|
---|
604 | /** Format network address.
|
---|
605 | *
|
---|
606 | * @param naddr Network address.
|
---|
607 | * @param bufp Place to store pointer to formatted string.
|
---|
608 | *
|
---|
609 | * @return EOK on success.
|
---|
610 | * @return ENOMEM if out of memory.
|
---|
611 | * @return ENOTSUP on unsupported address family.
|
---|
612 | *
|
---|
613 | */
|
---|
614 | errno_t inet_naddr_format(const inet_naddr_t *naddr, char **bufp)
|
---|
615 | {
|
---|
616 | errno_t rc;
|
---|
617 | int ret;
|
---|
618 | char *astr;
|
---|
619 |
|
---|
620 | rc = ENOTSUP;
|
---|
621 |
|
---|
622 | switch (naddr->version) {
|
---|
623 | case ip_any:
|
---|
624 | ret = asprintf(bufp, "none");
|
---|
625 | if (ret < 0)
|
---|
626 | return ENOMEM;
|
---|
627 | rc = EOK;
|
---|
628 | break;
|
---|
629 | case ip_v4:
|
---|
630 | rc = inet_addr_format_v4(naddr->addr, &astr);
|
---|
631 | if (rc != EOK)
|
---|
632 | return ENOMEM;
|
---|
633 |
|
---|
634 | ret = asprintf(bufp, "%s/%" PRIu8, astr, naddr->prefix);
|
---|
635 | if (ret < 0) {
|
---|
636 | free(astr);
|
---|
637 | return ENOMEM;
|
---|
638 | }
|
---|
639 |
|
---|
640 | rc = EOK;
|
---|
641 | break;
|
---|
642 | case ip_v6:
|
---|
643 | rc = inet_addr_format_v6(naddr->addr6, &astr);
|
---|
644 | if (rc != EOK)
|
---|
645 | return ENOMEM;
|
---|
646 |
|
---|
647 | ret = asprintf(bufp, "%s/%" PRIu8, astr, naddr->prefix);
|
---|
648 | if (ret < 0) {
|
---|
649 | free(astr);
|
---|
650 | return ENOMEM;
|
---|
651 | }
|
---|
652 |
|
---|
653 | rc = EOK;
|
---|
654 | break;
|
---|
655 | }
|
---|
656 |
|
---|
657 | return rc;
|
---|
658 | }
|
---|
659 |
|
---|
660 | ip_ver_t inet_addr_get(const inet_addr_t *addr, addr32_t *v4, addr128_t *v6)
|
---|
661 | {
|
---|
662 | switch (addr->version) {
|
---|
663 | case ip_v4:
|
---|
664 | if (v4 != NULL)
|
---|
665 | *v4 = addr->addr;
|
---|
666 | break;
|
---|
667 | case ip_v6:
|
---|
668 | if (v6 != NULL)
|
---|
669 | memcpy(*v6, addr->addr6, 16);
|
---|
670 | break;
|
---|
671 | default:
|
---|
672 | assert(false);
|
---|
673 | break;
|
---|
674 | }
|
---|
675 |
|
---|
676 | return addr->version;
|
---|
677 | }
|
---|
678 |
|
---|
679 | ip_ver_t inet_naddr_get(const inet_naddr_t *naddr, addr32_t *v4, addr128_t *v6,
|
---|
680 | uint8_t *prefix)
|
---|
681 | {
|
---|
682 | switch (naddr->version) {
|
---|
683 | case ip_v4:
|
---|
684 | if (v4 != NULL)
|
---|
685 | *v4 = naddr->addr;
|
---|
686 | if (prefix != NULL)
|
---|
687 | *prefix = naddr->prefix;
|
---|
688 | break;
|
---|
689 | case ip_v6:
|
---|
690 | if (v6 != NULL)
|
---|
691 | memcpy(*v6, naddr->addr6, 16);
|
---|
692 | if (prefix != NULL)
|
---|
693 | *prefix = naddr->prefix;
|
---|
694 | break;
|
---|
695 | default:
|
---|
696 | assert(false);
|
---|
697 | break;
|
---|
698 | }
|
---|
699 |
|
---|
700 | return naddr->version;
|
---|
701 | }
|
---|
702 |
|
---|
703 | void inet_addr_set(addr32_t v4, inet_addr_t *addr)
|
---|
704 | {
|
---|
705 | addr->version = ip_v4;
|
---|
706 | addr->addr = v4;
|
---|
707 | }
|
---|
708 |
|
---|
709 | void inet_naddr_set(addr32_t v4, uint8_t prefix, inet_naddr_t *naddr)
|
---|
710 | {
|
---|
711 | naddr->version = ip_v4;
|
---|
712 | naddr->addr = v4;
|
---|
713 | naddr->prefix = prefix;
|
---|
714 | }
|
---|
715 |
|
---|
716 | void inet_addr_set6(addr128_t v6, inet_addr_t *addr)
|
---|
717 | {
|
---|
718 | addr->version = ip_v6;
|
---|
719 | memcpy(addr->addr6, v6, 16);
|
---|
720 | }
|
---|
721 |
|
---|
722 | void inet_naddr_set6(addr128_t v6, uint8_t prefix, inet_naddr_t *naddr)
|
---|
723 | {
|
---|
724 | naddr->version = ip_v6;
|
---|
725 | memcpy(naddr->addr6, v6, 16);
|
---|
726 | naddr->prefix = prefix;
|
---|
727 | }
|
---|
728 |
|
---|
729 | /** @}
|
---|
730 | */
|
---|