Changes in uspace/lib/c/generic/inet/addr.c [26de91a:83781a22] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/inet/addr.c
r26de91a r83781a22 33 33 */ 34 34 35 #include <assert.h>36 35 #include <errno.h> 37 36 #include <unistd.h> … … 49 48 #endif 50 49 51 const addr32_t addr32_broadcast_all_hosts = 0xffffffff;52 53 50 const addr48_t addr48_broadcast = { 54 51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff … … 60 57 61 58 static const inet_addr_t inet_addr_any_addr = { 62 . version = ip_v4,59 .family = AF_INET, 63 60 .addr = 0 64 61 }; 65 62 66 63 static const inet_addr_t inet_addr_any_addr6 = { 67 . version = ip_v6,64 .family = AF_INET6, 68 65 .addr6 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 69 66 }; … … 79 76 } 80 77 81 /** Compare addr48.82 *83 * @return Non-zero if equal, zero if not equal.84 */85 78 int addr48_compare(const addr48_t a, const addr48_t b) 86 79 { 87 return memcmp(a, b, 6) == 0; 88 } 89 90 /** Compare addr128. 91 * 92 * @return Non-zero if equal, zero if not equal. 93 */ 80 return memcmp(a, b, 6); 81 } 82 94 83 int addr128_compare(const addr128_t a, const addr128_t b) 95 84 { 96 return memcmp(a, b, 16) == 0;85 return memcmp(a, b, 16); 97 86 } 98 87 … … 111 100 void host2addr128_t_be(const addr128_t host, addr128_t be) 112 101 { 102 #ifdef __BE__ 113 103 memcpy(be, host, 16); 104 #else 105 be[0] = host[15]; 106 be[1] = host[14]; 107 be[2] = host[13]; 108 be[3] = host[12]; 109 be[4] = host[11]; 110 be[5] = host[10]; 111 be[6] = host[9]; 112 be[7] = host[8]; 113 be[8] = host[7]; 114 be[9] = host[6]; 115 be[10] = host[5]; 116 be[11] = host[4]; 117 be[12] = host[3]; 118 be[13] = host[2]; 119 be[14] = host[1]; 120 be[15] = host[0]; 121 #endif 114 122 } 115 123 116 124 void addr128_t_be2host(const addr128_t be, addr128_t host) 117 125 { 126 #ifdef __BE__ 118 127 memcpy(host, be, 16); 128 #else 129 host[0] = be[15]; 130 host[1] = be[14]; 131 host[2] = be[13]; 132 host[3] = be[12]; 133 host[4] = be[11]; 134 host[5] = be[10]; 135 host[6] = be[9]; 136 host[7] = be[8]; 137 host[8] = be[7]; 138 host[9] = be[6]; 139 host[10] = be[5]; 140 host[11] = be[4]; 141 host[12] = be[3]; 142 host[13] = be[2]; 143 host[14] = be[1]; 144 host[15] = be[0]; 145 #endif 119 146 } 120 147 121 148 void inet_addr(inet_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d) 122 149 { 123 addr-> version = ip_v4;150 addr->family = AF_INET; 124 151 addr->addr = ((addr32_t) a << 24) | ((addr32_t) b << 16) | 125 152 ((addr32_t) c << 8) | ((addr32_t) d); … … 129 156 uint8_t prefix) 130 157 { 131 naddr-> version = ip_v4;158 naddr->family = AF_INET; 132 159 naddr->addr = ((addr32_t) a << 24) | ((addr32_t) b << 16) | 133 160 ((addr32_t) c << 8) | ((addr32_t) d); … … 138 165 uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h) 139 166 { 140 addr-> version = ip_v6;167 addr->family = AF_INET6; 141 168 addr->addr6[0] = (a >> 8) & 0xff; 142 169 addr->addr6[1] = a & 0xff; … … 160 187 uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h, uint8_t prefix) 161 188 { 162 naddr-> version = ip_v6;189 naddr->family = AF_INET6; 163 190 naddr->addr6[0] = (a >> 8) & 0xff; 164 191 naddr->addr6[1] = a & 0xff; … … 180 207 } 181 208 182 /** Determine address version.183 * 184 * @param text Address in common notation.185 * @param af Place to store address version.209 /** Parse network address family. 210 * 211 * @param text Network address in common notation. 212 * @param af Place to store network address family. 186 213 * 187 214 * @return EOK on success, EINVAL if input is not in valid format. 188 215 * 189 216 */ 190 static int inet_addr_version(const char *text, ip_ver_t *ver)217 int inet_addr_family(const char *text, uint16_t *af) 191 218 { 192 219 char *dot = str_chr(text, '.'); 193 220 if (dot != NULL) { 194 * ver = ip_v4;221 *af = AF_INET; 195 222 return EOK; 196 223 } 197 224 198 225 char *collon = str_chr(text, ':'); 199 226 if (collon != NULL) { 200 * ver = ip_v6;227 *af = AF_INET6; 201 228 return EOK; 202 229 } 203 230 204 231 return EINVAL; 205 232 } 206 233 207 static int ipver_af(ip_ver_t ver)208 {209 switch (ver) {210 case ip_any:211 return AF_NONE;212 case ip_v4:213 return AF_INET;214 case ip_v6:215 return AF_INET6;216 default:217 assert(false);218 return EINVAL;219 }220 }221 222 ip_ver_t ipver_from_af(int af)223 {224 switch (af) {225 case AF_NONE:226 return ip_any;227 case AF_INET:228 return ip_v4;229 case AF_INET6:230 return ip_v6;231 default:232 assert(false);233 return EINVAL;234 }235 }236 237 234 void inet_naddr_addr(const inet_naddr_t *naddr, inet_addr_t *addr) 238 235 { 239 addr-> version = naddr->version;236 addr->family = naddr->family; 240 237 memcpy(addr->addr6, naddr->addr6, 16); 241 238 } … … 244 241 inet_naddr_t *naddr) 245 242 { 246 naddr-> version = addr->version;243 naddr->family = addr->family; 247 244 memcpy(naddr->addr6, addr->addr6, 16); 248 245 naddr->prefix = prefix; … … 251 248 void inet_addr_any(inet_addr_t *addr) 252 249 { 253 addr-> version = ip_any;250 addr->family = AF_NONE; 254 251 memset(addr->addr6, 0, 16); 255 252 } … … 257 254 void inet_naddr_any(inet_naddr_t *naddr) 258 255 { 259 naddr-> version = ip_any;256 naddr->family = AF_NONE; 260 257 memset(naddr->addr6, 0, 16); 261 258 naddr->prefix = 0; … … 264 261 int inet_addr_compare(const inet_addr_t *a, const inet_addr_t *b) 265 262 { 266 if (a-> version != b->version)263 if (a->family != b->family) 267 264 return 0; 268 269 switch (a-> version) {270 case ip_v4:265 266 switch (a->family) { 267 case AF_INET: 271 268 return (a->addr == b->addr); 272 case ip_v6:269 case AF_INET6: 273 270 return addr128_compare(a->addr6, b->addr6); 274 271 default: … … 279 276 int inet_addr_is_any(const inet_addr_t *addr) 280 277 { 281 return ((addr-> version == ip_any) ||278 return ((addr->family == 0) || 282 279 (inet_addr_compare(addr, &inet_addr_any_addr)) || 283 280 (inet_addr_compare(addr, &inet_addr_any_addr6))); … … 286 283 int inet_naddr_compare(const inet_naddr_t *naddr, const inet_addr_t *addr) 287 284 { 288 if (naddr-> version != addr->version)285 if (naddr->family != addr->family) 289 286 return 0; 290 287 291 switch (naddr-> version) {292 case ip_v4:288 switch (naddr->family) { 289 case AF_INET: 293 290 return (naddr->addr == addr->addr); 294 case ip_v6:291 case AF_INET6: 295 292 return addr128_compare(naddr->addr6, addr->addr6); 296 293 default: … … 301 298 int inet_naddr_compare_mask(const inet_naddr_t *naddr, const inet_addr_t *addr) 302 299 { 303 if (naddr-> version != addr->version)300 if (naddr->family != addr->family) 304 301 return 0; 305 306 switch (naddr-> version) {307 case ip_v4:302 303 switch (naddr->family) { 304 case AF_INET: 308 305 if (naddr->prefix > 32) 309 306 return 0; 310 307 311 308 addr32_t mask = 312 309 BIT_RANGE(addr32_t, 31, 31 - (naddr->prefix - 1)); 313 310 return ((naddr->addr & mask) == (addr->addr & mask)); 314 case ip_v6:311 case AF_INET6: 315 312 if (naddr->prefix > 128) 316 313 return 0; … … 353 350 int inet_addr_parse(const char *text, inet_addr_t *addr) 354 351 { 355 int rc = inet_addr_ version(text, &addr->version);352 int rc = inet_addr_family(text, &addr->family); 356 353 if (rc != EOK) 357 354 return rc; 358 355 359 356 uint8_t buf[16]; 360 rc = inet_pton( ipver_af(addr->version), text, buf);357 rc = inet_pton(addr->family, text, buf); 361 358 if (rc != EOK) 362 359 return rc; 363 360 364 switch (addr-> version) {365 case ip_v4:361 switch (addr->family) { 362 case AF_INET: 366 363 addr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | 367 364 buf[3]; 368 365 break; 369 case ip_v6:366 case AF_INET6: 370 367 memcpy(addr->addr6, buf, 16); 371 368 break; … … 393 390 *slash = 0; 394 391 395 int rc = inet_addr_ version(text, &naddr->version);392 int rc = inet_addr_family(text, &naddr->family); 396 393 if (rc != EOK) 397 394 return rc; 398 395 399 396 uint8_t buf[16]; 400 rc = inet_pton( ipver_af(naddr->version), text, buf);397 rc = inet_pton(naddr->family, text, buf); 401 398 *slash = '/'; 402 399 … … 407 404 uint8_t prefix; 408 405 409 switch (naddr-> version) {410 case ip_v4:406 switch (naddr->family) { 407 case AF_INET: 411 408 prefix = strtoul(slash, &slash, 10); 412 409 if (prefix > 32) … … 418 415 419 416 break; 420 case ip_v6:417 case AF_INET6: 421 418 prefix = strtoul(slash, &slash, 10); 422 419 if (prefix > 128) … … 448 445 int rc = 0; 449 446 450 switch (addr-> version) {451 case ip_any:447 switch (addr->family) { 448 case AF_NONE: 452 449 rc = asprintf(bufp, "none"); 453 450 break; 454 case ip_v4:451 case AF_INET: 455 452 rc = asprintf(bufp, "%u.%u.%u.%u", (addr->addr >> 24) & 0xff, 456 453 (addr->addr >> 16) & 0xff, (addr->addr >> 8) & 0xff, 457 454 addr->addr & 0xff); 458 455 break; 459 case ip_v6:456 case AF_INET6: 460 457 *bufp = (char *) malloc(INET6_ADDRSTRLEN); 461 458 if (*bufp == NULL) … … 488 485 char prefix[INET_PREFIXSTRSIZE]; 489 486 490 switch (naddr-> version) {491 case ip_any:487 switch (naddr->family) { 488 case AF_NONE: 492 489 rc = asprintf(bufp, "none"); 493 490 break; 494 case ip_v4:491 case AF_INET: 495 492 rc = asprintf(bufp, "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 496 493 "/%" PRIu8, (naddr->addr >> 24) & 0xff, … … 498 495 naddr->addr & 0xff, naddr->prefix); 499 496 break; 500 case ip_v6:497 case AF_INET6: 501 498 *bufp = (char *) malloc(INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE); 502 499 if (*bufp == NULL) … … 530 527 } 531 528 532 ip_ver_t inet_addr_get(const inet_addr_t *addr, addr32_t *v4, addr128_t *v6)533 { 534 switch (addr-> version) {535 case ip_v4:529 uint16_t inet_addr_get(const inet_addr_t *addr, addr32_t *v4, addr128_t *v6) 530 { 531 switch (addr->family) { 532 case AF_INET: 536 533 if (v4 != NULL) 537 534 *v4 = addr->addr; 538 break; 539 case ip_v6: 535 536 break; 537 case AF_INET6: 540 538 if (v6 != NULL) 541 539 memcpy(*v6, addr->addr6, 16); 542 break; 543 default: 544 assert(false); 545 break; 546 } 547 548 return addr->version; 549 } 550 551 ip_ver_t inet_naddr_get(const inet_naddr_t *naddr, addr32_t *v4, addr128_t *v6, 540 541 break; 542 } 543 544 return addr->family; 545 } 546 547 uint16_t inet_naddr_get(const inet_naddr_t *naddr, addr32_t *v4, addr128_t *v6, 552 548 uint8_t *prefix) 553 549 { 554 switch (naddr-> version) {555 case ip_v4:550 switch (naddr->family) { 551 case AF_INET: 556 552 if (v4 != NULL) 557 553 *v4 = naddr->addr; 554 558 555 if (prefix != NULL) 559 556 *prefix = naddr->prefix; 560 break; 561 case ip_v6: 557 558 break; 559 case AF_INET6: 562 560 if (v6 != NULL) 563 561 memcpy(*v6, naddr->addr6, 16); 562 564 563 if (prefix != NULL) 565 564 *prefix = naddr->prefix; 566 break; 567 default: 568 assert(false); 569 break; 570 } 571 572 return naddr->version; 565 566 break; 567 } 568 569 return naddr->family; 573 570 } 574 571 575 572 void inet_addr_set(addr32_t v4, inet_addr_t *addr) 576 573 { 577 addr-> version = ip_v4;574 addr->family = AF_INET; 578 575 addr->addr = v4; 579 576 } … … 581 578 void inet_naddr_set(addr32_t v4, uint8_t prefix, inet_naddr_t *naddr) 582 579 { 583 naddr-> version = ip_v4;580 naddr->family = AF_INET; 584 581 naddr->addr = v4; 585 582 naddr->prefix = prefix; … … 588 585 void inet_sockaddr_in_addr(const sockaddr_in_t *sockaddr_in, inet_addr_t *addr) 589 586 { 590 addr-> version = ip_v4;587 addr->family = AF_INET; 591 588 addr->addr = uint32_t_be2host(sockaddr_in->sin_addr.s_addr); 592 589 } … … 594 591 void inet_addr_set6(addr128_t v6, inet_addr_t *addr) 595 592 { 596 addr-> version = ip_v6;593 addr->family = AF_INET6; 597 594 memcpy(addr->addr6, v6, 16); 598 595 } … … 600 597 void inet_naddr_set6(addr128_t v6, uint8_t prefix, inet_naddr_t *naddr) 601 598 { 602 naddr-> version = ip_v6;599 naddr->family = AF_INET6; 603 600 memcpy(naddr->addr6, v6, 16); 604 601 naddr->prefix = prefix; … … 608 605 inet_addr_t *addr) 609 606 { 610 addr-> version = ip_v6;607 addr->family = AF_INET6; 611 608 addr128_t_be2host(sockaddr_in6->sin6_addr.s6_addr, addr->addr6); 612 609 } … … 615 612 sockaddr_in_t *sockaddr_in, sockaddr_in6_t *sockaddr_in6) 616 613 { 617 switch (addr-> version) {618 case ip_v4:614 switch (addr->family) { 615 case AF_INET: 619 616 if (sockaddr_in != NULL) { 620 617 sockaddr_in->sin_family = AF_INET; 621 618 sockaddr_in->sin_addr.s_addr = host2uint32_t_be(addr->addr); 622 619 } 623 break; 624 case ip_v6: 620 621 break; 622 case AF_INET6: 625 623 if (sockaddr_in6 != NULL) { 626 624 sockaddr_in6->sin6_family = AF_INET6; 627 625 host2addr128_t_be(addr->addr6, sockaddr_in6->sin6_addr.s6_addr); 628 626 } 629 break; 630 default: 631 assert(false); 632 break; 633 } 634 635 return ipver_af(addr->version); 636 } 637 638 int inet_addr_sockaddr(const inet_addr_t *addr, uint16_t port, 639 sockaddr_t **nsockaddr, socklen_t *naddrlen) 640 { 641 sockaddr_in_t *sa4; 642 sockaddr_in6_t *sa6; 643 644 switch (addr->version) { 645 case ip_v4: 646 sa4 = calloc(1, sizeof(sockaddr_in_t)); 647 if (sa4 == NULL) 648 return ENOMEM; 649 650 sa4->sin_family = AF_INET; 651 sa4->sin_port = host2uint16_t_be(port); 652 sa4->sin_addr.s_addr = host2uint32_t_be(addr->addr); 653 if (nsockaddr != NULL) 654 *nsockaddr = (sockaddr_t *)sa4; 655 if (naddrlen != NULL) 656 *naddrlen = sizeof(*sa4); 657 break; 658 case ip_v6: 659 sa6 = calloc(1, sizeof(sockaddr_in6_t)); 660 if (sa6 == NULL) 661 return ENOMEM; 662 663 sa6->sin6_family = AF_INET6; 664 sa6->sin6_port = host2uint16_t_be(port); 665 host2addr128_t_be(addr->addr6, sa6->sin6_addr.s6_addr); 666 if (nsockaddr != NULL) 667 *nsockaddr = (sockaddr_t *)sa6; 668 if (naddrlen != NULL) 669 *naddrlen = sizeof(*sa6); 670 break; 671 default: 672 assert(false); 673 break; 674 } 675 676 return EOK; 627 628 break; 629 } 630 631 return addr->family; 677 632 } 678 633
Note:
See TracChangeset
for help on using the changeset viewer.