| 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 <stdlib.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 | int ret;
|
|---|
| 587 |
|
|---|
| 588 | rc = ENOTSUP;
|
|---|
| 589 |
|
|---|
| 590 | switch (addr->version) {
|
|---|
| 591 | case ip_any:
|
|---|
| 592 | ret = asprintf(bufp, "none");
|
|---|
| 593 | if (ret < 0)
|
|---|
| 594 | return ENOMEM;
|
|---|
| 595 | rc = EOK;
|
|---|
| 596 | break;
|
|---|
| 597 | case ip_v4:
|
|---|
| 598 | rc = inet_addr_format_v4(addr->addr, bufp);
|
|---|
| 599 | break;
|
|---|
| 600 | case ip_v6:
|
|---|
| 601 | rc = inet_addr_format_v6(addr->addr6, bufp);
|
|---|
| 602 | break;
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | return rc;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | /** Format network address.
|
|---|
| 609 | *
|
|---|
| 610 | * @param naddr Network address.
|
|---|
| 611 | * @param bufp Place to store pointer to formatted string.
|
|---|
| 612 | *
|
|---|
| 613 | * @return EOK on success.
|
|---|
| 614 | * @return ENOMEM if out of memory.
|
|---|
| 615 | * @return ENOTSUP on unsupported address family.
|
|---|
| 616 | *
|
|---|
| 617 | */
|
|---|
| 618 | int inet_naddr_format(const inet_naddr_t *naddr, char **bufp)
|
|---|
| 619 | {
|
|---|
| 620 | int rc;
|
|---|
| 621 | int ret;
|
|---|
| 622 | char *astr;
|
|---|
| 623 |
|
|---|
| 624 | rc = ENOTSUP;
|
|---|
| 625 |
|
|---|
| 626 | switch (naddr->version) {
|
|---|
| 627 | case ip_any:
|
|---|
| 628 | ret = asprintf(bufp, "none");
|
|---|
| 629 | if (ret < 0)
|
|---|
| 630 | return ENOMEM;
|
|---|
| 631 | rc = EOK;
|
|---|
| 632 | break;
|
|---|
| 633 | case ip_v4:
|
|---|
| 634 | rc = inet_addr_format_v4(naddr->addr, &astr);
|
|---|
| 635 | if (rc != EOK)
|
|---|
| 636 | return ENOMEM;
|
|---|
| 637 |
|
|---|
| 638 | ret = asprintf(bufp, "%s/%" PRIu8, astr, naddr->prefix);
|
|---|
| 639 | if (ret < 0) {
|
|---|
| 640 | free(astr);
|
|---|
| 641 | return ENOMEM;
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | rc = EOK;
|
|---|
| 645 | break;
|
|---|
| 646 | case ip_v6:
|
|---|
| 647 | rc = inet_addr_format_v6(naddr->addr6, &astr);
|
|---|
| 648 | if (rc != EOK)
|
|---|
| 649 | return ENOMEM;
|
|---|
| 650 |
|
|---|
| 651 | ret = asprintf(bufp, "%s/%" PRIu8, astr, naddr->prefix);
|
|---|
| 652 | if (ret < 0) {
|
|---|
| 653 | free(astr);
|
|---|
| 654 | return ENOMEM;
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | rc = EOK;
|
|---|
| 658 | break;
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | return rc;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | ip_ver_t inet_addr_get(const inet_addr_t *addr, addr32_t *v4, addr128_t *v6)
|
|---|
| 665 | {
|
|---|
| 666 | switch (addr->version) {
|
|---|
| 667 | case ip_v4:
|
|---|
| 668 | if (v4 != NULL)
|
|---|
| 669 | *v4 = addr->addr;
|
|---|
| 670 | break;
|
|---|
| 671 | case ip_v6:
|
|---|
| 672 | if (v6 != NULL)
|
|---|
| 673 | memcpy(*v6, addr->addr6, 16);
|
|---|
| 674 | break;
|
|---|
| 675 | default:
|
|---|
| 676 | assert(false);
|
|---|
| 677 | break;
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | return addr->version;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | ip_ver_t inet_naddr_get(const inet_naddr_t *naddr, addr32_t *v4, addr128_t *v6,
|
|---|
| 684 | uint8_t *prefix)
|
|---|
| 685 | {
|
|---|
| 686 | switch (naddr->version) {
|
|---|
| 687 | case ip_v4:
|
|---|
| 688 | if (v4 != NULL)
|
|---|
| 689 | *v4 = naddr->addr;
|
|---|
| 690 | if (prefix != NULL)
|
|---|
| 691 | *prefix = naddr->prefix;
|
|---|
| 692 | break;
|
|---|
| 693 | case ip_v6:
|
|---|
| 694 | if (v6 != NULL)
|
|---|
| 695 | memcpy(*v6, naddr->addr6, 16);
|
|---|
| 696 | if (prefix != NULL)
|
|---|
| 697 | *prefix = naddr->prefix;
|
|---|
| 698 | break;
|
|---|
| 699 | default:
|
|---|
| 700 | assert(false);
|
|---|
| 701 | break;
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | return naddr->version;
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | void inet_addr_set(addr32_t v4, inet_addr_t *addr)
|
|---|
| 708 | {
|
|---|
| 709 | addr->version = ip_v4;
|
|---|
| 710 | addr->addr = v4;
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | void inet_naddr_set(addr32_t v4, uint8_t prefix, inet_naddr_t *naddr)
|
|---|
| 714 | {
|
|---|
| 715 | naddr->version = ip_v4;
|
|---|
| 716 | naddr->addr = v4;
|
|---|
| 717 | naddr->prefix = prefix;
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | void inet_addr_set6(addr128_t v6, inet_addr_t *addr)
|
|---|
| 721 | {
|
|---|
| 722 | addr->version = ip_v6;
|
|---|
| 723 | memcpy(addr->addr6, v6, 16);
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | void inet_naddr_set6(addr128_t v6, uint8_t prefix, inet_naddr_t *naddr)
|
|---|
| 727 | {
|
|---|
| 728 | naddr->version = ip_v6;
|
|---|
| 729 | memcpy(naddr->addr6, v6, 16);
|
|---|
| 730 | naddr->prefix = prefix;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | /** @}
|
|---|
| 734 | */
|
|---|