get canonical identifier of an address' group no two connections will be attempted to addresses with the same group
| 899 | // get canonical identifier of an address' group |
| 900 | // no two connections will be attempted to addresses with the same group |
| 901 | std::vector<unsigned char> CNetAddr::GetGroup() const |
| 902 | { |
| 903 | std::vector<unsigned char> vchRet; |
| 904 | int nClass = NET_IPV6; |
| 905 | int nStartByte = 0; |
| 906 | int nBits = 16; |
| 907 | |
| 908 | // all local addresses belong to the same group |
| 909 | if (IsLocal()) { |
| 910 | nClass = 255; |
| 911 | nBits = 0; |
| 912 | } |
| 913 | |
| 914 | // all unroutable addresses belong to the same group |
| 915 | if (!IsRoutable()) { |
| 916 | nClass = NET_UNROUTABLE; |
| 917 | nBits = 0; |
| 918 | } |
| 919 | // for IPv4 addresses, '1' + the 16 higher-order bits of the IP |
| 920 | // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix |
| 921 | else if (IsIPv4() || IsRFC6145() || IsRFC6052()) { |
| 922 | nClass = NET_IPV4; |
| 923 | nStartByte = 12; |
| 924 | } |
| 925 | // for 6to4 tunnelled addresses, use the encapsulated IPv4 address |
| 926 | else if (IsRFC3964()) { |
| 927 | nClass = NET_IPV4; |
| 928 | nStartByte = 2; |
| 929 | } |
| 930 | // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address |
| 931 | else if (IsRFC4380()) { |
| 932 | vchRet.push_back(NET_IPV4); |
| 933 | vchRet.push_back(GetByte(3) ^ 0xFF); |
| 934 | vchRet.push_back(GetByte(2) ^ 0xFF); |
| 935 | return vchRet; |
| 936 | } else if (IsTor()) { |
| 937 | nClass = NET_TOR; |
| 938 | nStartByte = 6; |
| 939 | nBits = 4; |
| 940 | } |
| 941 | // for he.net, use /36 groups |
| 942 | else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70) |
| 943 | nBits = 36; |
| 944 | // for the rest of the IPv6 network, use /32 groups |
| 945 | else |
| 946 | nBits = 32; |
| 947 | |
| 948 | vchRet.push_back(nClass); |
| 949 | while (nBits >= 8) { |
| 950 | vchRet.push_back(GetByte(15 - nStartByte)); |
| 951 | nStartByte++; |
| 952 | nBits -= 8; |
| 953 | } |
| 954 | if (nBits > 0) |
| 955 | vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1)); |
| 956 | |
| 957 | return vchRet; |
| 958 | } |
no test coverage detected