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