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 : 0 : uint256 NetGroupManager::GetAsmapChecksum() const 11 : : { 12 [ # # ]: 0 : if (!m_asmap.size()) return {}; 13 : : 14 : 0 : return (HashWriter{} << m_asmap).GetHash(); 15 : 0 : } 16 : : 17 : 6993 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const 18 : : { 19 : 6993 : 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 [ + - ]: 6993 : uint32_t asn = GetMappedAS(address); 23 [ - + ]: 6993 : if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR). 24 [ # # ]: 0 : vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket 25 [ # # ]: 0 : for (int i = 0; i < 4; i++) { 26 [ # # ]: 0 : vchRet.push_back((asn >> (8 * i)) & 0xFF); 27 : 0 : } 28 : 0 : return vchRet; 29 : : } 30 : : 31 [ + - + - ]: 6993 : vchRet.push_back(address.GetNetClass()); 32 : 6993 : int nStartByte{0}; 33 : 6993 : int nBits{0}; 34 : : 35 [ + - + + ]: 6993 : if (address.IsLocal()) { 36 : : // all local addresses belong to the same group 37 [ + - + - ]: 6993 : } 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 : 0 : nStartByte = INTERNAL_IN_IPV6_PREFIX.size(); 41 : 0 : nBits = ADDR_INTERNAL_SIZE * 8; 42 [ + - + + ]: 5608 : } else if (!address.IsRoutable()) { 43 : : // all other unroutable addresses belong to the same group 44 [ + - + + ]: 5608 : } else if (address.HasLinkedIPv4()) { 45 : : // IPv4 addresses (and mapped IPv4 addresses) use /16 groups 46 [ + - ]: 3407 : uint32_t ipv4 = address.GetLinkedIPv4(); 47 [ + - ]: 3407 : vchRet.push_back((ipv4 >> 24) & 0xFF); 48 [ + - ]: 3407 : vchRet.push_back((ipv4 >> 16) & 0xFF); 49 : 3407 : return vchRet; 50 [ + - + - : 1824 : } else if (address.IsTor() || address.IsI2P()) { + - + - ] 51 : 0 : nBits = 4; 52 [ + - + + ]: 1824 : } 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 : 9 : nBits = 12; 58 [ + - + + ]: 1824 : } else if (address.IsHeNet()) { 59 : : // for he.net, use /36 groups 60 : 35 : nBits = 36; 61 : 35 : } else { 62 : : // for the rest of the IPv6 network, use /32 groups 63 : 1780 : nBits = 32; 64 : : } 65 : : 66 : : // Push our address onto vchRet. 67 [ + - ]: 3586 : auto addr_bytes = address.GetAddrBytes(); 68 : 3586 : const size_t num_bytes = nBits / 8; 69 [ + - ]: 3586 : vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes); 70 : 3586 : nBits %= 8; 71 : : // ...for the last byte, push nBits and for the rest of the byte push 1's 72 [ + + ]: 3586 : if (nBits > 0) { 73 [ - + ]: 44 : assert(num_bytes < addr_bytes.size()); 74 [ + - ]: 46 : vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1)); 75 : 44 : } 76 : : 77 : 3586 : return vchRet; 78 [ + - ]: 6993 : } 79 : : 80 : 15424 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const 81 : : { 82 : 15424 : uint32_t net_class = address.GetNetClass(); 83 [ - + # # : 15424 : if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) { # # ] 84 : 15424 : return 0; // Indicates not found, safe because AS0 is reserved per RFC7607. 85 : : } 86 [ # # ]: 0 : std::vector<bool> ip_bits(128); 87 [ # # # # ]: 0 : if (address.HasLinkedIPv4()) { 88 : : // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits) 89 [ # # ]: 0 : for (int8_t byte_i = 0; byte_i < 12; ++byte_i) { 90 [ # # ]: 0 : for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { 91 [ # # ]: 0 : ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1; 92 : 0 : } 93 : 0 : } 94 [ # # ]: 0 : uint32_t ipv4 = address.GetLinkedIPv4(); 95 [ # # ]: 0 : for (int i = 0; i < 32; ++i) { 96 [ # # ]: 0 : ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1; 97 : 0 : } 98 : 0 : } else { 99 : : // Use all 128 bits of the IPv6 address otherwise 100 [ # # # # ]: 0 : assert(address.IsIPv6()); 101 [ # # ]: 0 : auto addr_bytes = address.GetAddrBytes(); 102 [ # # ]: 0 : for (int8_t byte_i = 0; byte_i < 16; ++byte_i) { 103 : 0 : uint8_t cur_byte = addr_bytes[byte_i]; 104 [ # # ]: 0 : for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) { 105 [ # # ]: 0 : ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1; 106 : 0 : } 107 : 0 : } 108 : 0 : } 109 [ # # ]: 0 : uint32_t mapped_as = Interpret(m_asmap, ip_bits); 110 : 0 : return mapped_as; 111 : 15424 : }