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 0 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const 18 : { 19 0 : 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 0 : uint32_t asn = GetMappedAS(address); 23 0 : 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 0 : vchRet.push_back(address.GetNetClass()); 32 0 : int nStartByte{0}; 33 0 : int nBits{0}; 34 : 35 0 : if (address.IsLocal()) { 36 : // all local addresses belong to the same group 37 0 : } 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 0 : } else if (!address.IsRoutable()) { 43 : // all other unroutable addresses belong to the same group 44 0 : } else if (address.HasLinkedIPv4()) { 45 : // IPv4 addresses (and mapped IPv4 addresses) use /16 groups 46 0 : uint32_t ipv4 = address.GetLinkedIPv4(); 47 0 : vchRet.push_back((ipv4 >> 24) & 0xFF); 48 0 : vchRet.push_back((ipv4 >> 16) & 0xFF); 49 0 : return vchRet; 50 0 : } else if (address.IsTor() || address.IsI2P()) { 51 0 : nBits = 4; 52 0 : } 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 0 : nBits = 12; 58 0 : } else if (address.IsHeNet()) { 59 : // for he.net, use /36 groups 60 0 : nBits = 36; 61 0 : } else { 62 : // for the rest of the IPv6 network, use /32 groups 63 0 : nBits = 32; 64 : } 65 : 66 : // Push our address onto vchRet. 67 0 : auto addr_bytes = address.GetAddrBytes(); 68 0 : const size_t num_bytes = nBits / 8; 69 0 : vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes); 70 0 : nBits %= 8; 71 : // ...for the last byte, push nBits and for the rest of the byte push 1's 72 0 : if (nBits > 0) { 73 0 : assert(num_bytes < addr_bytes.size()); 74 2 : vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1)); 75 0 : } 76 : 77 0 : return vchRet; 78 0 : } 79 : 80 0 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const 81 : { 82 0 : uint32_t net_class = address.GetNetClass(); 83 0 : if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) { 84 0 : 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 0 : }