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