Differences From Artifact [b9058d4f4d]:
- File
src/OFSocket.m
— part of check-in
[ccc6a77c55]
at
2022-08-06 14:43:01
on branch trunk
— Reorder node and network of IPX sockets
This is more standard, albeit -[connectToNetwork:node:port:] sounding a
little bit more like you connect to a network rather than a node. (user: js, size: 22537) [annotate] [blame] [check-ins using] [more...]
To Artifact [3facc57fe7]:
- File src/OFSocket.m — part of check-in [df7580d5e1] at 2022-11-05 19:47:32 on branch trunk — Always support IPv6 addresses with numeric scope (user: js, size: 26808) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #import "OFException.h" /* For some E* -> WSAE* defines */ #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFLockFailedException.h" #import "OFOutOfRangeException.h" #import "OFUnlockFailedException.h" #ifdef OF_AMIGAOS # include <proto/exec.h> #endif #ifdef OF_NINTENDO_3DS # include <3ds/types.h> | > > > > | 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #import "OFException.h" /* For some E* -> WSAE* defines */ #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFLockFailedException.h" #import "OFOutOfRangeException.h" #import "OFUnlockFailedException.h" #ifdef HAVE_NET_IF_H # include <net/if.h> #endif #ifdef OF_AMIGAOS # include <proto/exec.h> #endif #ifdef OF_NINTENDO_3DS # include <3ds/types.h> |
︙ | ︙ | |||
230 231 232 233 234 235 236 | # endif if (socketBase != NULL) CloseLibrary(socketBase); } #endif int | | | 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | # endif if (socketBase != NULL) CloseLibrary(socketBase); } #endif int OFSocketErrNo(void) { #if defined(OF_WINDOWS) switch (WSAGetLastError()) { case WSAEACCES: return EACCES; case WSAEADDRINUSE: return EADDRINUSE; |
︙ | ︙ | |||
429 430 431 432 433 434 435 | OFSocketAddress OFSocketAddressParseIPv6(OFString *IPv6, uint16_t port) { void *pool = objc_autoreleasePoolPush(); OFSocketAddress ret; struct sockaddr_in6 *addrIn6 = &ret.sockaddr.in6; | | | > > > > > > > > > > > > > > > > > | 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | OFSocketAddress OFSocketAddressParseIPv6(OFString *IPv6, uint16_t port) { void *pool = objc_autoreleasePoolPush(); OFSocketAddress ret; struct sockaddr_in6 *addrIn6 = &ret.sockaddr.in6; size_t doubleColon, percent; 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 = OFToBigEndian16(port); if ((percent = [IPv6 rangeOfString: @"%"].location) != OFNotFound) { OFString *interface = [IPv6 substringFromIndex: percent + 1]; IPv6 = [IPv6 substringToIndex: percent]; @try { addrIn6->sin6_scope_id = (uint32_t)[interface unsignedLongLongValueWithBase: 10]; } @catch (OFInvalidFormatException *e) { #if defined(HAVE_IF_NAMETOINDEX) && !defined(OF_WINDOWS) addrIn6->sin6_scope_id = if_nametoindex([interface cStringWithEncoding: [OFLocale encoding]]); #endif } if (addrIn6->sin6_scope_id == 0) @throw [OFInvalidArgumentException exception]; } 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; |
︙ | ︙ | |||
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | memcpy(&ret.sockaddr.ipx.sipx_network, &network, sizeof(ret.sockaddr.ipx.sipx_network)); memcpy(ret.sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); ret.sockaddr.ipx.sipx_port = OFToBigEndian16(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; void *pool; OFString *path1, *path2; bool ret; if (address1->family != address2->family) return false; | > > > > > > > > > > > > > > > > > > > > > > > > > > | 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 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 | memcpy(&ret.sockaddr.ipx.sipx_network, &network, sizeof(ret.sockaddr.ipx.sipx_network)); memcpy(ret.sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); ret.sockaddr.ipx.sipx_port = OFToBigEndian16(port); return ret; } OFSocketAddress OFSocketAddressMakeAppleTalk(uint16_t network, uint8_t node, uint8_t port) { OFSocketAddress ret; memset(&ret, '\0', sizeof(ret)); ret.family = OFSocketAddressFamilyAppleTalk; ret.length = sizeof(ret.sockaddr.at); #ifdef AF_APPLETALK ret.sockaddr.at.sat_family = AF_APPLETALK; #else ret.sockaddr.at.sat_family = AF_UNSPEC; #endif #ifdef OF_WINDOWS ret.sockaddr.at.sat_net = network; #else ret.sockaddr.at.sat_net = OFToBigEndian16(network); #endif ret.sockaddr.at.sat_node = node; ret.sockaddr.at.sat_port = 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; const struct sockaddr_at *addrAT1, *addrAT2; void *pool; OFString *path1, *path2; bool ret; if (address1->family != address2->family) return false; |
︙ | ︙ | |||
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | if (memcmp(&addrIPX1->sipx_network, &addrIPX2->sipx_network, 4) != 0) return false; if (memcmp(addrIPX1->sipx_node, addrIPX2->sipx_node, IPX_NODE_LEN) != 0) return false; return true; default: @throw [OFInvalidArgumentException exception]; } } unsigned long OFSocketAddressHash(const OFSocketAddress *address) { unsigned long hash; OFHashInit(&hash); | > > > > > > > > > > > > > > > > | | | > | > | | | | | | | | | > | > > > > > > > > > | 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 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 | if (memcmp(&addrIPX1->sipx_network, &addrIPX2->sipx_network, 4) != 0) return false; if (memcmp(addrIPX1->sipx_node, addrIPX2->sipx_node, IPX_NODE_LEN) != 0) return false; return true; case OFSocketAddressFamilyAppleTalk: if (address1->length < (socklen_t)sizeof(struct sockaddr_at) || address2->length < (socklen_t)sizeof(struct sockaddr_at)) @throw [OFInvalidArgumentException exception]; addrAT1 = &address1->sockaddr.at; addrAT2 = &address2->sockaddr.at; if (addrAT1->sat_net != addrAT2->sat_net) return false; if (addrAT1->sat_node != addrAT2->sat_node) return false; if (addrAT1->sat_port != addrAT2->sat_port) return false; return true; default: @throw [OFInvalidArgumentException exception]; } } unsigned long OFSocketAddressHash(const OFSocketAddress *address) { unsigned long hash; OFHashInit(&hash); OFHashAddByte(&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 OFHashAddByte(&hash, address->sockaddr.in.sin_port >> 8); OFHashAddByte(&hash, address->sockaddr.in.sin_port); OFHashAddByte(&hash, address->sockaddr.in.sin_addr.s_addr >> 24); OFHashAddByte(&hash, address->sockaddr.in.sin_addr.s_addr >> 16); OFHashAddByte(&hash, address->sockaddr.in.sin_addr.s_addr >> 8); OFHashAddByte(&hash, address->sockaddr.in.sin_addr.s_addr); break; case OFSocketAddressFamilyIPv6: if (address->length < (socklen_t)sizeof(struct sockaddr_in6)) @throw [OFInvalidArgumentException exception]; OFHashAddByte(&hash, address->sockaddr.in6.sin6_port >> 8); OFHashAddByte(&hash, address->sockaddr.in6.sin6_port); for (size_t i = 0; i < sizeof(address->sockaddr.in6.sin6_addr.s6_addr); i++) OFHashAddByte(&hash, address->sockaddr.in6.sin6_addr.s6_addr[i]); break; case OFSocketAddressFamilyUNIX:; void *pool = objc_autoreleasePoolPush(); OFString *path = OFSocketAddressUNIXPath(address); hash = path.hash; objc_autoreleasePoolPop(pool); return hash; case OFSocketAddressFamilyIPX:; unsigned char network[ sizeof(address->sockaddr.ipx.sipx_network)]; if (address->length < (socklen_t)sizeof(struct sockaddr_ipx)) @throw [OFInvalidArgumentException exception]; OFHashAddByte(&hash, address->sockaddr.ipx.sipx_port >> 8); OFHashAddByte(&hash, address->sockaddr.ipx.sipx_port); memcpy(network, &address->sockaddr.ipx.sipx_network, sizeof(network)); for (size_t i = 0; i < sizeof(network); i++) OFHashAddByte(&hash, network[i]); for (size_t i = 0; i < IPX_NODE_LEN; i++) OFHashAddByte(&hash, address->sockaddr.ipx.sipx_node[i]); break; case OFSocketAddressFamilyAppleTalk: if (address->length < (socklen_t)sizeof(struct sockaddr_at)) @throw [OFInvalidArgumentException exception]; OFHashAddByte(&hash, address->sockaddr.at.sat_net >> 8); OFHashAddByte(&hash, address->sockaddr.at.sat_net); OFHashAddByte(&hash, address->sockaddr.at.sat_port); break; default: @throw [OFInvalidArgumentException exception]; } OFHashFinalize(&hash); |
︙ | ︙ | |||
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 | [string appendFormat: (first ? @"%x" : @":%x"), (addrIn6->sin6_addr.s6_addr[i] << 8) | 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 | > > > > > > > > > > > | < < < | < < | 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 | [string appendFormat: (first ? @"%x" : @":%x"), (addrIn6->sin6_addr.s6_addr[i] << 8) | addrIn6->sin6_addr.s6_addr[i + 1]]; first = false; } } if (addrIn6->sin6_scope_id != 0) { #if defined(HAVE_IF_INDEXTONAME) && !defined(OF_WINDOWS) char interface[IF_NAMESIZE]; if (if_indextoname(addrIn6->sin6_scope_id, interface) != NULL) [string appendFormat: @"%%%s", interface]; else # endif [string appendFormat: @"%%%u", addrIn6->sin6_scope_id]; } [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 OFSocketAddressSetIPPort(OFSocketAddress *address, uint16_t port) { switch (address->family) { case OFSocketAddressFamilyIPv4: address->sockaddr.in.sin_port = OFToBigEndian16(port); break; case OFSocketAddressFamilyIPv6: address->sockaddr.in6.sin6_port = OFToBigEndian16(port); break; default: @throw [OFInvalidArgumentException exception]; } } uint16_t OFSocketAddressIPPort(const OFSocketAddress *address) { switch (address->family) { case OFSocketAddressFamilyIPv4: return OFFromBigEndian16(address->sockaddr.in.sin_port); case OFSocketAddressFamilyIPv6: return OFFromBigEndian16(address->sockaddr.in6.sin6_port); default: @throw [OFInvalidArgumentException exception]; } } OFString * OFSocketAddressUNIXPath(const OFSocketAddress *_Nonnull address) |
︙ | ︙ | |||
920 921 922 923 924 925 926 | if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; memcpy(address->sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); } void | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; memcpy(address->sockaddr.ipx.sipx_node, node, IPX_NODE_LEN); } void OFSocketAddressGetIPXNode(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); } void OFSocketAddressSetIPXPort(OFSocketAddress *address, uint16_t port) { if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; address->sockaddr.ipx.sipx_port = OFToBigEndian16(port); } uint16_t OFSocketAddressIPXPort(const OFSocketAddress *address) { if (address->family != OFSocketAddressFamilyIPX) @throw [OFInvalidArgumentException exception]; return OFFromBigEndian16(address->sockaddr.ipx.sipx_port); } void OFSocketAddressSetAppleTalkNetwork(OFSocketAddress *address, uint16_t network) { if (address->family != OFSocketAddressFamilyAppleTalk) @throw [OFInvalidArgumentException exception]; #ifdef OF_WINDOWS address->sockaddr.at.sat_net = network; #else address->sockaddr.at.sat_net = OFToBigEndian16(network); #endif } uint16_t OFSocketAddressAppleTalkNetwork(const OFSocketAddress *address) { if (address->family != OFSocketAddressFamilyAppleTalk) @throw [OFInvalidArgumentException exception]; #ifdef OF_WINDOWS return address->sockaddr.at.sat_net; #else return OFFromBigEndian16(address->sockaddr.at.sat_net); #endif } void OFSocketAddressSetAppleTalkNode(OFSocketAddress *address, uint8_t node) { if (address->family != OFSocketAddressFamilyAppleTalk) @throw [OFInvalidArgumentException exception]; address->sockaddr.at.sat_node = node; } uint8_t OFSocketAddressAppleTalkNode(const OFSocketAddress *address) { if (address->family != OFSocketAddressFamilyAppleTalk) @throw [OFInvalidArgumentException exception]; return address->sockaddr.at.sat_node; } void OFSocketAddressSetAppleTalkPort(OFSocketAddress *address, uint8_t port) { if (address->family != OFSocketAddressFamilyAppleTalk) @throw [OFInvalidArgumentException exception]; address->sockaddr.at.sat_port = port; } uint8_t OFSocketAddressAppleTalkPort(const OFSocketAddress *address) { if (address->family != OFSocketAddressFamilyAppleTalk) @throw [OFInvalidArgumentException exception]; return address->sockaddr.at.sat_port; } |