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