[3495654] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Internet address parsing and formatting.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
[a2e3ee6] | 36 | #include <unistd.h>
|
---|
| 37 | #include <net/socket_codes.h>
|
---|
[3495654] | 38 | #include <inet/addr.h>
|
---|
[a2e3ee6] | 39 | #include <net/inet.h>
|
---|
[3495654] | 40 | #include <stdio.h>
|
---|
[02a09ed] | 41 | #include <malloc.h>
|
---|
| 42 | #include <bitops.h>
|
---|
[3495654] | 43 |
|
---|
[02a09ed] | 44 | #define INET_PREFIXSTRSIZE 5
|
---|
| 45 |
|
---|
| 46 | #if !(defined(__BE__) ^ defined(__LE__))
|
---|
| 47 | #error The architecture must be either big-endian or little-endian.
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
| 50 | const addr48_t addr48_broadcast = {
|
---|
| 51 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | static const inet_addr_t inet_addr_any_addr = {
|
---|
[a2e3ee6] | 55 | .family = AF_INET,
|
---|
[02a09ed] | 56 | .addr = 0
|
---|
[a2e3ee6] | 57 | };
|
---|
| 58 |
|
---|
[02a09ed] | 59 | static const inet_addr_t inet_addr_any_addr6 = {
|
---|
[a2e3ee6] | 60 | .family = AF_INET6,
|
---|
[02a09ed] | 61 | .addr6 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
---|
[a2e3ee6] | 62 | };
|
---|
| 63 |
|
---|
[02a09ed] | 64 | void addr48(const addr48_t src, addr48_t dst)
|
---|
| 65 | {
|
---|
| 66 | memcpy(dst, src, 6);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void addr128(const addr128_t src, addr128_t dst)
|
---|
| 70 | {
|
---|
| 71 | memcpy(dst, src, 16);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void host2addr128_t_be(const addr128_t host, addr128_t be)
|
---|
| 75 | {
|
---|
| 76 | #ifdef __BE__
|
---|
| 77 | memcpy(be, host, 16);
|
---|
| 78 | #else
|
---|
| 79 | be[0] = host[15];
|
---|
| 80 | be[1] = host[14];
|
---|
| 81 | be[2] = host[13];
|
---|
| 82 | be[3] = host[12];
|
---|
| 83 | be[4] = host[11];
|
---|
| 84 | be[5] = host[10];
|
---|
| 85 | be[6] = host[9];
|
---|
| 86 | be[7] = host[8];
|
---|
| 87 | be[8] = host[7];
|
---|
| 88 | be[9] = host[6];
|
---|
| 89 | be[10] = host[5];
|
---|
| 90 | be[11] = host[4];
|
---|
| 91 | be[12] = host[3];
|
---|
| 92 | be[13] = host[2];
|
---|
| 93 | be[14] = host[1];
|
---|
| 94 | be[15] = host[0];
|
---|
| 95 | #endif
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void addr128_t_be2host(const addr128_t be, addr128_t host)
|
---|
| 99 | {
|
---|
| 100 | #ifdef __BE__
|
---|
| 101 | memcpy(host, be, 16);
|
---|
| 102 | #else
|
---|
| 103 | host[0] = be[15];
|
---|
| 104 | host[1] = be[14];
|
---|
| 105 | host[2] = be[13];
|
---|
| 106 | host[3] = be[12];
|
---|
| 107 | host[4] = be[11];
|
---|
| 108 | host[5] = be[10];
|
---|
| 109 | host[6] = be[9];
|
---|
| 110 | host[7] = be[8];
|
---|
| 111 | host[8] = be[7];
|
---|
| 112 | host[9] = be[6];
|
---|
| 113 | host[10] = be[5];
|
---|
| 114 | host[11] = be[4];
|
---|
| 115 | host[12] = be[3];
|
---|
| 116 | host[13] = be[2];
|
---|
| 117 | host[14] = be[1];
|
---|
| 118 | host[15] = be[0];
|
---|
| 119 | #endif
|
---|
| 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->family = AF_INET;
|
---|
| 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->family = AF_INET;
|
---|
| 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->family = AF_INET6;
|
---|
| 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->family = AF_INET6;
|
---|
| 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 |
|
---|
[a2e3ee6] | 183 | /** Parse network address family.
|
---|
[3495654] | 184 | *
|
---|
[a2e3ee6] | 185 | * @param text Network address in common notation.
|
---|
| 186 | * @param af Place to store network address family.
|
---|
| 187 | *
|
---|
| 188 | * @return EOK on success, EINVAL if input is not in valid format.
|
---|
[3495654] | 189 | *
|
---|
| 190 | */
|
---|
[a2e3ee6] | 191 | int inet_addr_family(const char *text, uint16_t *af)
|
---|
[3495654] | 192 | {
|
---|
[a2e3ee6] | 193 | char *dot = str_chr(text, '.');
|
---|
| 194 | if (dot != NULL) {
|
---|
| 195 | *af = AF_INET;
|
---|
| 196 | return EOK;
|
---|
[3495654] | 197 | }
|
---|
[a2e3ee6] | 198 |
|
---|
| 199 | char *collon = str_chr(text, ':');
|
---|
| 200 | if (collon != NULL) {
|
---|
| 201 | *af = AF_INET6;
|
---|
| 202 | return EOK;
|
---|
[3495654] | 203 | }
|
---|
[a2e3ee6] | 204 |
|
---|
| 205 | return EINVAL;
|
---|
[3495654] | 206 | }
|
---|
| 207 |
|
---|
[02a09ed] | 208 | void inet_naddr_addr(const inet_naddr_t *naddr, inet_addr_t *addr)
|
---|
| 209 | {
|
---|
| 210 | addr->family = naddr->family;
|
---|
| 211 | memcpy(addr->addr6, naddr->addr6, 16);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | void inet_addr_any(inet_addr_t *addr)
|
---|
| 215 | {
|
---|
| 216 | addr->family = AF_NONE;
|
---|
| 217 | memset(addr->addr6, 0, 16);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | void inet_naddr_any(inet_naddr_t *naddr)
|
---|
| 221 | {
|
---|
| 222 | naddr->family = AF_NONE;
|
---|
| 223 | memset(naddr->addr6, 0, 16);
|
---|
| 224 | naddr->prefix = 0;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | int inet_addr_compare(const inet_addr_t *a, const inet_addr_t *b)
|
---|
| 228 | {
|
---|
| 229 | if (a->family != b->family)
|
---|
| 230 | return 0;
|
---|
| 231 |
|
---|
| 232 | switch (a->family) {
|
---|
| 233 | case AF_INET:
|
---|
| 234 | return (a->addr == b->addr);
|
---|
| 235 | case AF_INET6:
|
---|
| 236 | return memcmp(&a->addr6, &b->addr6, 16);
|
---|
| 237 | default:
|
---|
| 238 | return 0;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | int inet_addr_is_any(const inet_addr_t *addr)
|
---|
| 243 | {
|
---|
| 244 | return ((addr->family == 0) ||
|
---|
| 245 | (inet_addr_compare(addr, &inet_addr_any_addr)) ||
|
---|
| 246 | (inet_addr_compare(addr, &inet_addr_any_addr6)));
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | int inet_naddr_compare_mask(const inet_naddr_t *naddr, const inet_addr_t *addr)
|
---|
| 250 | {
|
---|
| 251 | if (naddr->family != addr->family)
|
---|
| 252 | return 0;
|
---|
| 253 |
|
---|
| 254 | switch (naddr->family) {
|
---|
| 255 | case AF_INET:
|
---|
| 256 | if (naddr->prefix > 32)
|
---|
| 257 | return 0;
|
---|
| 258 |
|
---|
| 259 | addr32_t mask =
|
---|
| 260 | BIT_RANGE(addr32_t, 31, 31 - (naddr->prefix - 1));
|
---|
| 261 | return ((naddr->addr & mask) == (addr->addr & mask));
|
---|
| 262 | case AF_INET6:
|
---|
| 263 | if (naddr->prefix > 128)
|
---|
| 264 | return 0;
|
---|
| 265 |
|
---|
| 266 | size_t pos = 0;
|
---|
| 267 | for (size_t i = 0; i < 16; i++) {
|
---|
| 268 | /* Further bits do not matter */
|
---|
| 269 | if (naddr->prefix < pos)
|
---|
| 270 | break;
|
---|
| 271 |
|
---|
| 272 | if (naddr->prefix - pos > 8) {
|
---|
| 273 | /* Comparison without masking */
|
---|
| 274 | if (naddr->addr6[i] != addr->addr6[i])
|
---|
| 275 | return 0;
|
---|
| 276 | } else {
|
---|
| 277 | /* Comparison with masking */
|
---|
| 278 | uint8_t mask =
|
---|
| 279 | BIT_RANGE(uint8_t, 8, 8 - (naddr->prefix - pos - 1));
|
---|
| 280 | if ((naddr->addr6[i] & mask) != (addr->addr6[i] & mask))
|
---|
| 281 | return 0;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | pos += 8;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return 1;
|
---|
| 288 | default:
|
---|
| 289 | return 0;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[3495654] | 293 | /** Parse node address.
|
---|
| 294 | *
|
---|
[a2e3ee6] | 295 | * @param text Network address in common notation.
|
---|
| 296 | * @param addr Place to store node address.
|
---|
| 297 | *
|
---|
| 298 | * @return EOK on success, EINVAL if input is not in valid format.
|
---|
[3495654] | 299 | *
|
---|
| 300 | */
|
---|
| 301 | int inet_addr_parse(const char *text, inet_addr_t *addr)
|
---|
| 302 | {
|
---|
[a2e3ee6] | 303 | int rc = inet_addr_family(text, &addr->family);
|
---|
| 304 | if (rc != EOK)
|
---|
| 305 | return rc;
|
---|
| 306 |
|
---|
[02a09ed] | 307 | uint8_t buf[16];
|
---|
| 308 | rc = inet_pton(addr->family, text, buf);
|
---|
[a2e3ee6] | 309 | if (rc != EOK)
|
---|
| 310 | return rc;
|
---|
| 311 |
|
---|
[02a09ed] | 312 | switch (addr->family) {
|
---|
| 313 | case AF_INET:
|
---|
| 314 | addr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) |
|
---|
| 315 | buf[3];
|
---|
| 316 | break;
|
---|
| 317 | case AF_INET6:
|
---|
| 318 | memcpy(addr->addr6, buf, 16);
|
---|
| 319 | break;
|
---|
| 320 | default:
|
---|
| 321 | return EINVAL;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[a2e3ee6] | 324 | return EOK;
|
---|
| 325 | }
|
---|
[3495654] | 326 |
|
---|
[a2e3ee6] | 327 | /** Parse network address.
|
---|
| 328 | *
|
---|
| 329 | * @param text Network address in common notation.
|
---|
| 330 | * @param naddr Place to store network address.
|
---|
| 331 | *
|
---|
| 332 | * @return EOK on success, EINVAL if input is not in valid format.
|
---|
| 333 | *
|
---|
| 334 | */
|
---|
| 335 | int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
|
---|
| 336 | {
|
---|
| 337 | char *slash = str_chr(text, '/');
|
---|
| 338 | if (slash == NULL)
|
---|
[3495654] | 339 | return EINVAL;
|
---|
[a2e3ee6] | 340 |
|
---|
| 341 | *slash = 0;
|
---|
| 342 |
|
---|
| 343 | int rc = inet_addr_family(text, &naddr->family);
|
---|
| 344 | if (rc != EOK)
|
---|
| 345 | return rc;
|
---|
| 346 |
|
---|
[02a09ed] | 347 | uint8_t buf[16];
|
---|
| 348 | rc = inet_pton(naddr->family, text, buf);
|
---|
[a2e3ee6] | 349 | *slash = '/';
|
---|
| 350 |
|
---|
| 351 | if (rc != EOK)
|
---|
| 352 | return rc;
|
---|
| 353 |
|
---|
| 354 | slash++;
|
---|
[02a09ed] | 355 | uint8_t prefix;
|
---|
[a2e3ee6] | 356 |
|
---|
| 357 | switch (naddr->family) {
|
---|
| 358 | case AF_INET:
|
---|
[02a09ed] | 359 | prefix = strtoul(slash, &slash, 10);
|
---|
| 360 | if (prefix > 32)
|
---|
[a2e3ee6] | 361 | return EINVAL;
|
---|
[02a09ed] | 362 |
|
---|
| 363 | naddr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) |
|
---|
| 364 | buf[3];
|
---|
| 365 | naddr->prefix = prefix;
|
---|
| 366 |
|
---|
[a2e3ee6] | 367 | break;
|
---|
| 368 | case AF_INET6:
|
---|
[02a09ed] | 369 | prefix = strtoul(slash, &slash, 10);
|
---|
| 370 | if (prefix > 128)
|
---|
[3495654] | 371 | return EINVAL;
|
---|
[02a09ed] | 372 |
|
---|
| 373 | memcpy(naddr->addr6, buf, 16);
|
---|
| 374 | naddr->prefix = prefix;
|
---|
| 375 |
|
---|
[a2e3ee6] | 376 | break;
|
---|
| 377 | default:
|
---|
| 378 | return ENOTSUP;
|
---|
[3495654] | 379 | }
|
---|
[a2e3ee6] | 380 |
|
---|
| 381 | return EOK;
|
---|
| 382 | }
|
---|
[3495654] | 383 |
|
---|
[a2e3ee6] | 384 | /** Format node address.
|
---|
| 385 | *
|
---|
| 386 | * @param addr Node address.
|
---|
| 387 | * @param bufp Place to store pointer to formatted string.
|
---|
| 388 | *
|
---|
| 389 | * @return EOK on success.
|
---|
| 390 | * @return ENOMEM if out of memory.
|
---|
| 391 | * @return ENOTSUP on unsupported address family.
|
---|
| 392 | *
|
---|
| 393 | */
|
---|
[02a09ed] | 394 | int inet_addr_format(const inet_addr_t *addr, char **bufp)
|
---|
[a2e3ee6] | 395 | {
|
---|
[02a09ed] | 396 | int rc = 0;
|
---|
[a2e3ee6] | 397 |
|
---|
| 398 | switch (addr->family) {
|
---|
| 399 | case AF_NONE:
|
---|
| 400 | rc = asprintf(bufp, "none");
|
---|
| 401 | break;
|
---|
| 402 | case AF_INET:
|
---|
[02a09ed] | 403 | rc = asprintf(bufp, "%u.%u.%u.%u", (addr->addr >> 24) & 0xff,
|
---|
| 404 | (addr->addr >> 16) & 0xff, (addr->addr >> 8) & 0xff,
|
---|
| 405 | addr->addr & 0xff);
|
---|
[a2e3ee6] | 406 | break;
|
---|
| 407 | case AF_INET6:
|
---|
[02a09ed] | 408 | *bufp = (char *) malloc(INET6_ADDRSTRLEN);
|
---|
| 409 | if (*bufp == NULL)
|
---|
| 410 | return ENOMEM;
|
---|
| 411 |
|
---|
| 412 | return inet_ntop(AF_INET6, addr->addr6, *bufp, INET6_ADDRSTRLEN);
|
---|
[a2e3ee6] | 413 | default:
|
---|
| 414 | return ENOTSUP;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | if (rc < 0)
|
---|
| 418 | return ENOMEM;
|
---|
| 419 |
|
---|
[3495654] | 420 | return EOK;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /** Format network address.
|
---|
| 424 | *
|
---|
[a2e3ee6] | 425 | * @param naddr Network address.
|
---|
| 426 | * @param bufp Place to store pointer to formatted string.
|
---|
| 427 | *
|
---|
| 428 | * @return EOK on success.
|
---|
| 429 | * @return ENOMEM if out of memory.
|
---|
| 430 | * @return ENOTSUP on unsupported address family.
|
---|
[3495654] | 431 | *
|
---|
| 432 | */
|
---|
[02a09ed] | 433 | int inet_naddr_format(const inet_naddr_t *naddr, char **bufp)
|
---|
[3495654] | 434 | {
|
---|
[02a09ed] | 435 | int rc = 0;
|
---|
| 436 | char prefix[INET_PREFIXSTRSIZE];
|
---|
[a2e3ee6] | 437 |
|
---|
| 438 | switch (naddr->family) {
|
---|
| 439 | case AF_NONE:
|
---|
| 440 | rc = asprintf(bufp, "none");
|
---|
| 441 | break;
|
---|
| 442 | case AF_INET:
|
---|
[02a09ed] | 443 | rc = asprintf(bufp, "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
|
---|
| 444 | "/%" PRIu8, (naddr->addr >> 24) & 0xff,
|
---|
| 445 | (naddr->addr >> 16) & 0xff, (naddr->addr >> 8) & 0xff,
|
---|
| 446 | naddr->addr & 0xff, naddr->prefix);
|
---|
[a2e3ee6] | 447 | break;
|
---|
| 448 | case AF_INET6:
|
---|
[02a09ed] | 449 | *bufp = (char *) malloc(INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE);
|
---|
| 450 | if (*bufp == NULL)
|
---|
| 451 | return ENOMEM;
|
---|
| 452 |
|
---|
| 453 | rc = inet_ntop(AF_INET6, naddr->addr6, *bufp,
|
---|
| 454 | INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE);
|
---|
| 455 | if (rc != EOK) {
|
---|
| 456 | free(*bufp);
|
---|
| 457 | return rc;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | rc = snprintf(prefix, INET_PREFIXSTRSIZE, "/%" PRIu8,
|
---|
| 461 | naddr->prefix);
|
---|
| 462 | if (rc < 0) {
|
---|
| 463 | free(*bufp);
|
---|
| 464 | return ENOMEM;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | str_append(*bufp, INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE, prefix);
|
---|
| 468 |
|
---|
| 469 | break;
|
---|
[a2e3ee6] | 470 | default:
|
---|
| 471 | return ENOTSUP;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[3495654] | 474 | if (rc < 0)
|
---|
| 475 | return ENOMEM;
|
---|
[a2e3ee6] | 476 |
|
---|
| 477 | return EOK;
|
---|
| 478 | }
|
---|
[3495654] | 479 |
|
---|
[02a09ed] | 480 | uint16_t inet_addr_get(const inet_addr_t *addr, addr32_t *v4, addr128_t *v6)
|
---|
[a2e3ee6] | 481 | {
|
---|
[02a09ed] | 482 | switch (addr->family) {
|
---|
| 483 | case AF_INET:
|
---|
| 484 | if (v4 != NULL)
|
---|
| 485 | *v4 = addr->addr;
|
---|
| 486 |
|
---|
| 487 | break;
|
---|
| 488 | case AF_INET6:
|
---|
| 489 | if (v6 != NULL)
|
---|
| 490 | memcpy(*v6, addr->addr6, 16);
|
---|
| 491 |
|
---|
| 492 | break;
|
---|
| 493 | }
|
---|
[a2e3ee6] | 494 |
|
---|
[02a09ed] | 495 | return addr->family;
|
---|
[3495654] | 496 | }
|
---|
| 497 |
|
---|
[02a09ed] | 498 | uint16_t inet_naddr_get(const inet_naddr_t *naddr, addr32_t *v4, addr128_t *v6,
|
---|
| 499 | uint8_t *prefix)
|
---|
[3495654] | 500 | {
|
---|
[02a09ed] | 501 | switch (naddr->family) {
|
---|
| 502 | case AF_INET:
|
---|
| 503 | if (v4 != NULL)
|
---|
| 504 | *v4 = naddr->addr;
|
---|
| 505 |
|
---|
| 506 | if (prefix != NULL)
|
---|
| 507 | *prefix = naddr->prefix;
|
---|
| 508 |
|
---|
| 509 | break;
|
---|
| 510 | case AF_INET6:
|
---|
| 511 | if (v6 != NULL)
|
---|
| 512 | memcpy(*v6, naddr->addr6, 16);
|
---|
| 513 |
|
---|
| 514 | if (prefix != NULL)
|
---|
| 515 | *prefix = naddr->prefix;
|
---|
| 516 |
|
---|
| 517 | break;
|
---|
| 518 | }
|
---|
[a2e3ee6] | 519 |
|
---|
[02a09ed] | 520 | return naddr->family;
|
---|
[a2e3ee6] | 521 | }
|
---|
[3495654] | 522 |
|
---|
[02a09ed] | 523 | void inet_addr_set(addr32_t v4, inet_addr_t *addr)
|
---|
[a2e3ee6] | 524 | {
|
---|
| 525 | addr->family = AF_INET;
|
---|
[02a09ed] | 526 | addr->addr = v4;
|
---|
[a2e3ee6] | 527 | }
|
---|
[3495654] | 528 |
|
---|
[02a09ed] | 529 | void inet_naddr_set(addr32_t v4, uint8_t prefix, inet_naddr_t *naddr)
|
---|
[a2e3ee6] | 530 | {
|
---|
| 531 | naddr->family = AF_INET;
|
---|
[02a09ed] | 532 | naddr->addr = v4;
|
---|
[a2e3ee6] | 533 | naddr->prefix = prefix;
|
---|
| 534 | }
|
---|
[3495654] | 535 |
|
---|
[02a09ed] | 536 | void inet_sockaddr_in_addr(const sockaddr_in_t *sockaddr_in, inet_addr_t *addr)
|
---|
[a2e3ee6] | 537 | {
|
---|
| 538 | addr->family = AF_INET;
|
---|
[02a09ed] | 539 | addr->addr = uint32_t_be2host(sockaddr_in->sin_addr.s_addr);
|
---|
[a2e3ee6] | 540 | }
|
---|
| 541 |
|
---|
[02a09ed] | 542 | void inet_addr_set6(addr128_t v6, inet_addr_t *addr)
|
---|
[a2e3ee6] | 543 | {
|
---|
[02a09ed] | 544 | addr->family = AF_INET6;
|
---|
| 545 | memcpy(addr->addr6, v6, 16);
|
---|
[a2e3ee6] | 546 | }
|
---|
| 547 |
|
---|
[02a09ed] | 548 | void inet_naddr_set6(addr128_t v6, uint8_t prefix, inet_naddr_t *naddr)
|
---|
[a2e3ee6] | 549 | {
|
---|
[02a09ed] | 550 | naddr->family = AF_INET6;
|
---|
| 551 | memcpy(naddr->addr6, v6, 16);
|
---|
| 552 | naddr->prefix = prefix;
|
---|
[a2e3ee6] | 553 | }
|
---|
| 554 |
|
---|
[02a09ed] | 555 | void inet_sockaddr_in6_addr(const sockaddr_in6_t *sockaddr_in6,
|
---|
| 556 | inet_addr_t *addr)
|
---|
[a2e3ee6] | 557 | {
|
---|
[02a09ed] | 558 | addr->family = AF_INET6;
|
---|
| 559 | addr128_t_be2host(sockaddr_in6->sin6_addr.s6_addr, addr->addr6);
|
---|
[a2e3ee6] | 560 | }
|
---|
| 561 |
|
---|
[02a09ed] | 562 | uint16_t inet_addr_sockaddr_in(const inet_addr_t *addr,
|
---|
| 563 | sockaddr_in_t *sockaddr_in, sockaddr_in6_t *sockaddr_in6)
|
---|
[a2e3ee6] | 564 | {
|
---|
[02a09ed] | 565 | switch (addr->family) {
|
---|
[a2e3ee6] | 566 | case AF_INET:
|
---|
[02a09ed] | 567 | if (sockaddr_in != NULL) {
|
---|
| 568 | sockaddr_in->sin_family = AF_INET;
|
---|
| 569 | sockaddr_in->sin_addr.s_addr = host2uint32_t_be(addr->addr);
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | break;
|
---|
[a2e3ee6] | 573 | case AF_INET6:
|
---|
[02a09ed] | 574 | if (sockaddr_in6 != NULL) {
|
---|
| 575 | sockaddr_in6->sin6_family = AF_INET6;
|
---|
| 576 | host2addr128_t_be(addr->addr6, sockaddr_in6->sin6_addr.s6_addr);
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | break;
|
---|
[a2e3ee6] | 580 | }
|
---|
[02a09ed] | 581 |
|
---|
| 582 | return addr->family;
|
---|
[a2e3ee6] | 583 | }
|
---|
| 584 |
|
---|
[3495654] | 585 | /** @}
|
---|
| 586 | */
|
---|