Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 The Bitcoin Core developers 2 : : // Distributed under the MIT software license, see the accompanying 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : : 5 : : #include <netgroup.h> 6 : : 7 : : #include <hash.h> 8 : : #include <util/asmap.h> 9 : : 10 : 2717 : uint256 NetGroupManager::GetAsmapChecksum() const 11 : : { 12 [ + + ]: 2717 : if (!m_asmap.size()) return {}; 13 : : 14 : 1043 : return (HashWriter{} << m_asmap).GetHash(); 15 : 2717 : } 16 : : 17 : 18921137 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const 18 : : { 19 : 18921137 : std::vector<unsigned char> vchRet; 20 : : // If non-empty asmap is supplied and the address is IPv4/IPv6, 21 : : // return ASN to be used for bucketing. 22 [ + - ]: 18921137 : uint32_t asn = GetMappedAS(address); 23 [ + + ]: 18921137 : if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR). 24 [ + - ]: 3112475 : vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket 25 [ + + ]: 15562375 : for (int i = 0; i < 4; i++) { 26 [ + - ]: 12449900 : vchRet.push_back((asn >> (8 * i)) & 0xFF); 27 : 12449900 : } 28 : 3112475 : return vchRet; 29 : : } 30 : : 31 [ + - + - ]: 15808662 : vchRet.push_back(address.GetNetClass()); 32 : 15808662 : int nStartByte{0}; 33 : 15808662 : int nBits{0}; 34 : : 35 [ + - + + ]: 15808662 : if (address.IsLocal()) { 36 : : // all local addresses belong to the same group 37 [ + - + + ]: 15808662 : } else if (address.IsInternal()) { 38 : : // All internal-usage addresses get their own group. 39 : : // Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes(). 40 : 107252 : nStartByte = INTERNAL_IN_IPV6_PREFIX.size(); 41 : 107252 : nBits = ADDR_INTERNAL_SIZE * 8; 42 [ + - + + ]: 15750101 : } else if (!address.IsRoutable()) { 43 : : // all other unroutable addresses belong to the same group 44 [ + - + + ]: 15642849 : } else if (address.HasLinkedIPv4()) { 45 : : // IPv4 addresses (and mapped IPv4 addresses) use /16 groups 46 [ + - ]: 3162041 : uint32_t ipv4 = address.GetLinkedIPv4(); 47 [ + - ]: 3162041 : vchRet.push_back((ipv4 >> 24) & 0xFF); 48 [ + - ]: 3162041 : vchRet.push_back((ipv4 >> 16) & 0xFF); 49 : 3162041 : return vchRet; 50 [ + - + + : 12426489 : } else if (address.IsTor() || address.IsI2P()) { + - + + ] 51 : 8499184 : nBits = 4; 52 [ + - + + ]: 12426489 : } else if (address.IsCJDNS()) { 53 : : // Treat in the same way as Tor and I2P because the address in all of 54 : : // them is "random" bytes (derived from a public key). However in CJDNS 55 : : // the first byte is a constant 0xfc, so the random bytes come after it. 56 : : // Thus skip the constant 8 bits at the start. 57 : 14867 : nBits = 12; 58 [ + - + + ]: 3927305 : } else if (address.IsHeNet()) { 59 : : // for he.net, use /36 groups 60 : 199 : nBits = 36; 61 : 199 : } else { 62 : : // for the rest of the IPv6 network, use /32 groups 63 : 3912239 : nBits = 32; 64 : : } 65 : : 66 : : // Push our address onto vchRet. 67 [ + - ]: 12646621 : auto addr_bytes = address.GetAddrBytes(); 68 : 12646621 : const size_t num_bytes = nBits / 8; 69 [ + - ]: 12646621 : vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes); 70 : 12646621 : nBits %= 8; 71 : : // ...for the last byte, push nBits and for the rest of the byte push 1's 72 [ + + ]: 12646621 : if (nBits > 0) { 73 [ - + ]: 8514250 : assert(num_bytes < addr_bytes.size()); 74 [ + - ]: 8514423 : vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1)); 75 : 8514250 : } 76 : : 77 : 12646621 : return vchRet; 78 [ + - ]: 18921137 : } 79 : : 80 : 18936034 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const 81 : : { 82 : 18936034 : uint32_t net_class = address.GetNetClass(); 83 [ + + + + : 18936034 : if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) { + + ] 84 : 15053014 : return 0; // Indicates not found, safe because AS0 is reserved per RFC7607. 85 : : } 86 [ + - ]: 3883020 : std::vector<bool> ip_bits(128); 87 [ + - + + ]: 3883020 : if (address.HasLinkedIPv4()) { 88 : : // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits) 89 [ + + ]: 22802039 : for (int8_t byte_i = 0; byte_i < 12; ++byte_i) { 90 [ + + ]: 189432324 : for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { 91 [ + - ]: 168384288 : ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1; 92 : 168384288 : } 93 : 21048036 : } 94 [ + - ]: 1754003 : uint32_t ipv4 = address.GetLinkedIPv4(); 95 [ + + ]: 57882099 : for (int i = 0; i < 32; ++i) { 96 [ + - ]: 56128096 : ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1; 97 : 56128096 : } 98 : 1754003 : } else { 99 : : // Use all 128 bits of the IPv6 address otherwise 100 [ + - - + ]: 2129017 : assert(address.IsIPv6()); 101 [ + - ]: 2129017 : auto addr_bytes = address.GetAddrBytes(); 102 [ + + ]: 36193289 : for (int8_t byte_i = 0; byte_i < 16; ++byte_i) { 103 : 34064272 : uint8_t cur_byte = addr_bytes[byte_i]; 104 [ + + ]: 306578448 : for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { 105 [ + - ]: 272514176 : ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1; 106 : 272514176 : } 107 : 34064272 : } 108 : 2129017 : } 109 [ + - ]: 3883020 : uint32_t mapped_as = Interpret(m_asmap, ip_bits); 110 : 3883020 : return mapped_as; 111 : 18936034 : }