Differences From Artifact [21df00d3c4]:
- File
src/socket.m
— part of check-in
[1833194867]
at
2021-01-14 02:29:31
on branch trunk
— Define _HPUX_ALT_XOPEN_SOCKET_API where necessary
There is a nasty bug in HP-UX: When using the proper socket APIs that
take a socklen_t (which is 64 bit in LP64 mode), it still calls into
methods that expect an int - so there is a type mismatch between the
headers and the compiled system libraries. This leads to all socklen_t
being interpreted as 0 (due to big endian). Defining
_HPUX_ALT_XOPEN_SOCKET_API makes it use the correct symbols in the
system libraries instead. (user: js, size: 21022) [annotate] [blame] [check-ins using] [more...]
To Artifact [36cae0ca62]:
- File src/socket.m — part of check-in [cc3a4a7b43] at 2021-04-17 15:45:07 on branch new-naming-convention — of_tlskey_t -> OFTLSKey (user: js, size: 20567) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
68 69 70 71 72 73 74 | #endif #if !defined(OF_AMIGAOS) || defined(OF_MORPHOS) || !defined(OF_HAVE_THREADS) static bool initSuccessful = false; #endif #ifdef OF_AMIGAOS # if defined(OF_HAVE_THREADS) && !defined(OF_MORPHOS) | | | | | | 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #endif #if !defined(OF_AMIGAOS) || defined(OF_MORPHOS) || !defined(OF_HAVE_THREADS) static bool initSuccessful = false; #endif #ifdef OF_AMIGAOS # if defined(OF_HAVE_THREADS) && !defined(OF_MORPHOS) OFTLSKey of_socket_base_key; # ifdef OF_AMIGAOS4 OFTLSKey of_socket_interface_key; # endif # else struct Library *SocketBase; # ifdef OF_AMIGAOS4 struct SocketIFace *ISocket = NULL; # endif # endif #endif #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS) OF_CONSTRUCTOR() { if (OFTLSKeyNew(&of_socket_base_key) != 0) @throw [OFInitializationFailedException exception]; # ifdef OF_AMIGAOS4 if (OFTLSKeyNew(&of_socket_interface_key) != 0) @throw [OFInitializationFailedException exception]; # endif } #endif #if !defined(OF_AMIGAOS) || defined(OF_MORPHOS) || !defined(OF_HAVE_THREADS) static void |
︙ | ︙ | |||
170 171 172 173 174 175 176 | #else struct Library *socketBase; # ifdef OF_AMIGAOS4 struct SocketIFace *socketInterface; # endif # ifdef OF_AMIGAOS4 | | | | | | | | 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | #else struct Library *socketBase; # ifdef OF_AMIGAOS4 struct SocketIFace *socketInterface; # endif # ifdef OF_AMIGAOS4 if ((socketInterface = OFTLSKeyGet(of_socket_interface_key)) != NULL) # else if ((socketBase = OFTLSKeyGet(of_socket_base_key)) != NULL) # endif return true; if ((socketBase = OpenLibrary("bsdsocket.library", 4)) == NULL) return false; # ifdef OF_AMIGAOS4 if ((socketInterface = (struct SocketIFace *) GetInterface(socketBase, "main", 1, NULL)) == NULL) { CloseLibrary(socketBase); return false; } # endif if (OFTLSKeySet(of_socket_base_key, socketBase) != 0) { CloseLibrary(socketBase); # ifdef OF_AMIGAOS4 DropInterface((struct Interface *)socketInterface); # endif return false; } # ifdef OF_AMIGAOS4 if (OFTLSKeySet(of_socket_interface_key, socketInterface) != 0) { CloseLibrary(socketBase); DropInterface((struct Interface *)socketInterface); return false; } # endif return true; #endif } #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS) void of_socket_deinit(void) { struct Library *socketBase = OFTLSKeyGet(of_socket_base_key); # ifdef OF_AMIGAOS4 struct SocketIFace *socketInterface = OFTLSKeyGet(of_socket_interface_key); if (socketInterface != NULL) DropInterface((struct Interface *)socketInterface); # endif if (socketBase != NULL) CloseLibrary(socketBase); } |
︙ | ︙ | |||
327 328 329 330 331 332 333 | #else return errno; #endif } #ifndef OF_WII int | | | | | | | 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | #else return errno; #endif } #ifndef OF_WII int of_getsockname(OFSocketHandle sock, struct sockaddr *restrict addr, socklen_t *restrict addrLen) { int ret; # if defined(OF_HAVE_THREADS) && (!defined(OF_AMIGAOS) || defined(OF_MORPHOS)) [mutex lock]; # endif ret = getsockname(sock, addr, addrLen); # if defined(OF_HAVE_THREADS) && (!defined(OF_AMIGAOS) || defined(OF_MORPHOS)) [mutex unlock]; # endif return ret; } #endif OFSocketAddress OFSocketAddressParseIPv4(OFString *IPv4, uint16_t port) { void *pool = objc_autoreleasePoolPush(); OFCharacterSet *whitespaceCharacterSet = [OFCharacterSet whitespaceCharacterSet]; OFSocketAddress ret; struct sockaddr_in *addrIn = &ret.sockaddr.in; OFArray OF_GENERIC(OFString *) *components; uint32_t addr; memset(&ret, '\0', sizeof(ret)); ret.family = OFSocketAddressFamilyIPv4; #if defined(OF_WII) || defined(OF_NINTENDO_3DS) ret.length = 8; #else ret.length = sizeof(ret.sockaddr.in); #endif addrIn->sin_family = AF_INET; |
︙ | ︙ | |||
383 384 385 386 387 388 389 | for (OFString *component in components) { unsigned long long number; if (component.length == 0) @throw [OFInvalidFormatException exception]; if ([component indexOfCharacterFromSet: | | | 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | for (OFString *component in components) { unsigned long long number; if (component.length == 0) @throw [OFInvalidFormatException exception]; if ([component indexOfCharacterFromSet: whitespaceCharacterSet] != OFNotFound) @throw [OFInvalidFormatException exception]; number = component.unsignedLongLongValue; if (number > UINT8_MAX) @throw [OFInvalidFormatException exception]; |
︙ | ︙ | |||
407 408 409 410 411 412 413 | static uint16_t parseIPv6Component(OFString *component) { unsigned long long number; if ([component indexOfCharacterFromSet: | | | | | | | | 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | static uint16_t parseIPv6Component(OFString *component) { unsigned long long number; if ([component indexOfCharacterFromSet: [OFCharacterSet whitespaceCharacterSet]] != OFNotFound) @throw [OFInvalidFormatException exception]; number = [component unsignedLongLongValueWithBase: 16]; if (number > UINT16_MAX) @throw [OFInvalidFormatException exception]; return (uint16_t)number; } OFSocketAddress OFSocketAddressParseIPv6(OFString *IPv6, uint16_t port) { void *pool = objc_autoreleasePoolPush(); OFSocketAddress ret; struct sockaddr_in6 *addrIn6 = &ret.sockaddr.in6; size_t doubleColon; memset(&ret, '\0', sizeof(ret)); ret.family = OFSocketAddressFamilyIPv6; ret.length = sizeof(ret.sockaddr.in6); #ifdef AF_INET6 addrIn6->sin6_family = AF_INET6; #else addrIn6->sin6_family = AF_UNSPEC; #endif addrIn6->sin6_port = OF_BSWAP16_IF_LE(port); doubleColon = [IPv6 rangeOfString: @"::"].location; if (doubleColon != OFNotFound) { OFString *left = [IPv6 substringToIndex: doubleColon]; OFString *right = [IPv6 substringFromIndex: doubleColon + 2]; OFArray OF_GENERIC(OFString *) *leftComponents; OFArray OF_GENERIC(OFString *) *rightComponents; size_t i; if ([right hasPrefix: @":"] || [right containsString: @"::"]) |
︙ | ︙ | |||
497 498 499 500 501 502 503 | } objc_autoreleasePoolPop(pool); return ret; } | | | | | | | | | | | | | | | | 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | } objc_autoreleasePoolPop(pool); return ret; } OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port) { OFSocketAddress ret; @try { ret = OFSocketAddressParseIPv6(IP, port); } @catch (OFInvalidFormatException *e) { ret = OFSocketAddressParseIPv4(IP, port); } return ret; } OFSocketAddress OFSocketAddressMakeIPX(const unsigned char node[IPX_NODE_LEN], uint32_t network, uint16_t port) { OFSocketAddress ret; memset(&ret, '\0', sizeof(ret)); ret.family = OFSocketAddressFamilyIPX; ret.length = sizeof(ret.sockaddr.ipx); #ifdef AF_IPX ret.sockaddr.ipx.sipx_family = AF_IPX; #else ret.sockaddr.ipx.sipx_family = AF_UNSPEC; #endif memcpy(ret.sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); network = OF_BSWAP32_IF_LE(network); memcpy(&ret.sockaddr.ipx.sipx_network, &network, sizeof(ret.sockaddr.ipx.sipx_network)); ret.sockaddr.ipx.sipx_port = OF_BSWAP16_IF_LE(port); return ret; } bool OFSocketAddressEqual(const OFSocketAddress *address1, const OFSocketAddress *address2) { const struct sockaddr_in *addrIn1, *addrIn2; const struct sockaddr_in6 *addrIn6_1, *addrIn6_2; const struct sockaddr_ipx *addrIPX1, *addrIPX2; if (address1->family != address2->family) return false; switch (address1->family) { case OFSocketAddressFamilyIPv4: #if defined(OF_WII) || defined(OF_NINTENDO_3DS) if (address1->length < 8 || address2->length < 8) @throw [OFInvalidArgumentException exception]; #else if (address1->length < (socklen_t)sizeof(struct sockaddr_in) || address2->length < (socklen_t)sizeof(struct sockaddr_in)) @throw [OFInvalidArgumentException exception]; #endif addrIn1 = &address1->sockaddr.in; addrIn2 = &address2->sockaddr.in; if (addrIn1->sin_port != addrIn2->sin_port) return false; if (addrIn1->sin_addr.s_addr != addrIn2->sin_addr.s_addr) return false; break; case OFSocketAddressFamilyIPv6: if (address1->length < (socklen_t)sizeof(struct sockaddr_in6) || address2->length < (socklen_t)sizeof(struct sockaddr_in6)) @throw [OFInvalidArgumentException exception]; addrIn6_1 = &address1->sockaddr.in6; addrIn6_2 = &address2->sockaddr.in6; if (addrIn6_1->sin6_port != addrIn6_2->sin6_port) return false; if (memcmp(addrIn6_1->sin6_addr.s6_addr, addrIn6_2->sin6_addr.s6_addr, sizeof(addrIn6_1->sin6_addr.s6_addr)) != 0) return false; break; case OFSocketAddressFamilyIPX: if (address1->length < (socklen_t)sizeof(struct sockaddr_ipx) || address2->length < (socklen_t)sizeof(struct sockaddr_ipx)) @throw [OFInvalidArgumentException exception]; addrIPX1 = &address1->sockaddr.ipx; addrIPX2 = &address2->sockaddr.ipx; |
︙ | ︙ | |||
608 609 610 611 612 613 614 | @throw [OFInvalidArgumentException exception]; } return true; } unsigned long | | | | | | 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | @throw [OFInvalidArgumentException exception]; } return true; } unsigned long OFSocketAddressHash(const OFSocketAddress *address) { uint32_t hash; OF_HASH_INIT(hash); OF_HASH_ADD(hash, address->family); switch (address->family) { case OFSocketAddressFamilyIPv4: #if defined(OF_WII) || defined(OF_NINTENDO_3DS) if (address->length < 8) @throw [OFInvalidArgumentException exception]; #else if (address->length < (socklen_t)sizeof(struct sockaddr_in)) @throw [OFInvalidArgumentException exception]; #endif OF_HASH_ADD(hash, address->sockaddr.in.sin_port >> 8); OF_HASH_ADD(hash, address->sockaddr.in.sin_port); OF_HASH_ADD(hash, address->sockaddr.in.sin_addr.s_addr >> 24); OF_HASH_ADD(hash, address->sockaddr.in.sin_addr.s_addr >> 16); OF_HASH_ADD(hash, address->sockaddr.in.sin_addr.s_addr >> 8); OF_HASH_ADD(hash, address->sockaddr.in.sin_addr.s_addr); break; case OFSocketAddressFamilyIPv6: if (address->length < (socklen_t)sizeof(struct sockaddr_in6)) @throw [OFInvalidArgumentException exception]; OF_HASH_ADD(hash, address->sockaddr.in6.sin6_port >> 8); OF_HASH_ADD(hash, address->sockaddr.in6.sin6_port); for (size_t i = 0; i < sizeof(address->sockaddr.in6.sin6_addr.s6_addr); i++) OF_HASH_ADD(hash, address->sockaddr.in6.sin6_addr.s6_addr[i]); break; case OFSocketAddressFamilyIPX:; unsigned char network[ sizeof(address->sockaddr.ipx.sipx_network)]; if (address->length < (socklen_t)sizeof(struct sockaddr_ipx)) @throw [OFInvalidArgumentException exception]; OF_HASH_ADD(hash, address->sockaddr.ipx.sipx_port >> 8); |
︙ | ︙ | |||
676 677 678 679 680 681 682 | OF_HASH_FINALIZE(hash); return hash; } static OFString * | | < < < | | 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | OF_HASH_FINALIZE(hash); return hash; } static OFString * IPv4String(const OFSocketAddress *address) { const struct sockaddr_in *addrIn = &address->sockaddr.in; uint32_t addr = OF_BSWAP32_IF_LE(addrIn->sin_addr.s_addr); OFString *string; string = [OFString stringWithFormat: @"%u.%u.%u.%u", (addr & 0xFF000000) >> 24, (addr & 0x00FF0000) >> 16, (addr & 0x0000FF00) >> 8, addr & 0x000000FF]; return string; } static OFString * IPv6String(const OFSocketAddress *address) { OFMutableString *string = [OFMutableString string]; const struct sockaddr_in6 *addrIn6 = &address->sockaddr.in6; int_fast8_t zerosStart = -1, maxZerosStart = -1; uint_fast8_t zerosCount = 0, maxZerosCount = 0; bool first = true; |
︙ | ︙ | |||
756 757 758 759 760 761 762 | addrIn6->sin6_addr.s6_addr[i + 1]]; first = false; } } [string makeImmutable]; | < < < | | | | | | | | | | | | | < | | | | | | | | | 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 | addrIn6->sin6_addr.s6_addr[i + 1]]; first = false; } } [string makeImmutable]; return string; } OFString * OFSocketAddressString(const OFSocketAddress *address) { switch (address->family) { case OFSocketAddressFamilyIPv4: return IPv4String(address); case OFSocketAddressFamilyIPv6: return IPv6String(address); default: @throw [OFInvalidArgumentException exception]; } } void OFSocketAddressSetPort(OFSocketAddress *address, uint16_t port) { switch (address->family) { case OFSocketAddressFamilyIPv4: address->sockaddr.in.sin_port = OF_BSWAP16_IF_LE(port); break; case OFSocketAddressFamilyIPv6: address->sockaddr.in6.sin6_port = OF_BSWAP16_IF_LE(port); break; case OFSocketAddressFamilyIPX: address->sockaddr.ipx.sipx_port = OF_BSWAP16_IF_LE(port); break; default: @throw [OFInvalidArgumentException exception]; } } uint16_t OFSocketAddressPort(const OFSocketAddress *address) { switch (address->family) { case OFSocketAddressFamilyIPv4: return OF_BSWAP16_IF_LE(address->sockaddr.in.sin_port); case OFSocketAddressFamilyIPv6: return OF_BSWAP16_IF_LE(address->sockaddr.in6.sin6_port); case OFSocketAddressFamilyIPX: return OF_BSWAP16_IF_LE(address->sockaddr.ipx.sipx_port); default: @throw [OFInvalidArgumentException exception]; } } void OFSocketAddressSetIPXNetwork(OFSocketAddress *address, uint32_t network) { if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; network = OF_BSWAP32_IF_LE(network); memcpy(&address->sockaddr.ipx.sipx_network, &network, sizeof(address->sockaddr.ipx.sipx_network)); } uint32_t OFSocketAddressIPXNetwork(const OFSocketAddress *address) { uint32_t network; if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; memcpy(&network, &address->sockaddr.ipx.sipx_network, sizeof(network)); return OF_BSWAP32_IF_LE(network); } void OFSocketAddressSetIPXNode(OFSocketAddress *address, const unsigned char node[IPX_NODE_LEN]) { if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; memcpy(address->sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); } void OFSocketAddressIPXNode(const OFSocketAddress *address, unsigned char node[IPX_NODE_LEN]) { if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; memcpy(node, address->sockaddr.ipx.sipx_node, IPX_NODE_LEN); } |