get canonical identifier of an address' group no two connections will be attempted to addresses with the same group
| 755 | // get canonical identifier of an address' group |
| 756 | // no two connections will be attempted to addresses with the same group |
| 757 | vector<unsigned char> CNetAddr::GetGroup() const { |
| 758 | vector<unsigned char> vchRet; |
| 759 | int32_t nClass = NET_IPV6; |
| 760 | int32_t nStartByte = 0; |
| 761 | int32_t nBits = 16; |
| 762 | |
| 763 | // all local addresses belong to the same group |
| 764 | if (IsLocal()) { |
| 765 | nClass = 255; |
| 766 | nBits = 0; |
| 767 | } |
| 768 | |
| 769 | // all unroutable addresses belong to the same group |
| 770 | if (!IsRoutable()) { |
| 771 | nClass = NET_UNROUTABLE; |
| 772 | nBits = 0; |
| 773 | } |
| 774 | // for IPv4 addresses, '1' + the 16 higher-order bits of the IP |
| 775 | // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix |
| 776 | else if (IsIPv4() || IsRFC6145() || IsRFC6052()) { |
| 777 | nClass = NET_IPV4; |
| 778 | nStartByte = 12; |
| 779 | } |
| 780 | // for 6to4 tunnelled addresses, use the encapsulated IPv4 address |
| 781 | else if (IsRFC3964()) { |
| 782 | nClass = NET_IPV4; |
| 783 | nStartByte = 2; |
| 784 | } |
| 785 | // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address |
| 786 | else if (IsRFC4380()) { |
| 787 | vchRet.push_back(NET_IPV4); |
| 788 | vchRet.push_back(GetByte(3) ^ 0xFF); |
| 789 | vchRet.push_back(GetByte(2) ^ 0xFF); |
| 790 | return vchRet; |
| 791 | } else if (IsTor()) { |
| 792 | nClass = NET_TOR; |
| 793 | nStartByte = 6; |
| 794 | nBits = 4; |
| 795 | } |
| 796 | // for he.net, use /36 groups |
| 797 | else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70) |
| 798 | nBits = 36; |
| 799 | // for the rest of the IPv6 network, use /32 groups |
| 800 | else |
| 801 | nBits = 32; |
| 802 | |
| 803 | vchRet.push_back(nClass); |
| 804 | while (nBits >= 8) { |
| 805 | vchRet.push_back(GetByte(15 - nStartByte)); |
| 806 | nStartByte++; |
| 807 | nBits -= 8; |
| 808 | } |
| 809 | if (nBits > 0) vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1)); |
| 810 | |
| 811 | return vchRet; |
| 812 | } |
| 813 | |
| 814 | uint64_t CNetAddr::GetHash() const { |
no test coverage detected