Skip to content

Commit ddb4101

Browse files
committed
[net] Only use public CNetAddr functions and data in GetMappedAS() and GetGroup()
Also change parameter/variable names. This makes the next commit mostly move-only.
1 parent 6b22681 commit ddb4101

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/netaddress.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -722,34 +722,36 @@ Network CNetAddr::GetNetClass() const
722722
return m_net;
723723
}
724724

725-
uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
726-
uint32_t net_class = GetNetClass();
727-
if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
725+
uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &m_asmap) const {
726+
const CNetAddr& address = *this;
727+
uint32_t net_class = address.GetNetClass();
728+
if (m_asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
728729
return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
729730
}
730731
std::vector<bool> ip_bits(128);
731-
if (HasLinkedIPv4()) {
732+
if (address.HasLinkedIPv4()) {
732733
// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
733734
for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
734735
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
735736
ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
736737
}
737738
}
738-
uint32_t ipv4 = GetLinkedIPv4();
739+
uint32_t ipv4 = address.GetLinkedIPv4();
739740
for (int i = 0; i < 32; ++i) {
740741
ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
741742
}
742743
} else {
743744
// Use all 128 bits of the IPv6 address otherwise
744-
assert(IsIPv6());
745+
assert(address.IsIPv6());
746+
auto addr_bytes = address.GetAddrBytes();
745747
for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
746-
uint8_t cur_byte = m_addr[byte_i];
748+
uint8_t cur_byte = addr_bytes[byte_i];
747749
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
748750
ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
749751
}
750752
}
751753
}
752-
uint32_t mapped_as = Interpret(asmap, ip_bits);
754+
uint32_t mapped_as = Interpret(m_asmap, ip_bits);
753755
return mapped_as;
754756
}
755757

@@ -763,13 +765,13 @@ uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
763765
* @note No two connections will be attempted to addresses with the same network
764766
* group.
765767
*/
766-
std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
768+
std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &m_asmap) const
767769
{
770+
const CNetAddr& address = *this;
768771
std::vector<unsigned char> vchRet;
769-
uint32_t net_class = GetNetClass();
770772
// If non-empty asmap is supplied and the address is IPv4/IPv6,
771773
// return ASN to be used for bucketing.
772-
uint32_t asn = GetMappedAS(asmap);
774+
uint32_t asn = GetMappedAS(m_asmap);
773775
if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
774776
vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
775777
for (int i = 0; i < 4; i++) {
@@ -778,31 +780,34 @@ std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) co
778780
return vchRet;
779781
}
780782

781-
vchRet.push_back(net_class);
783+
vchRet.push_back(address.GetNetClass());
784+
int nStartByte{0};
782785
int nBits{0};
783786

784-
if (IsLocal()) {
787+
if (address.IsLocal()) {
785788
// all local addresses belong to the same group
786-
} else if (IsInternal()) {
787-
// all internal-usage addresses get their own group
789+
} else if (address.IsInternal()) {
790+
// All internal-usage addresses get their own group.
791+
// Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes().
792+
nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
788793
nBits = ADDR_INTERNAL_SIZE * 8;
789-
} else if (!IsRoutable()) {
794+
} else if (!address.IsRoutable()) {
790795
// all other unroutable addresses belong to the same group
791-
} else if (HasLinkedIPv4()) {
796+
} else if (address.HasLinkedIPv4()) {
792797
// IPv4 addresses (and mapped IPv4 addresses) use /16 groups
793-
uint32_t ipv4 = GetLinkedIPv4();
798+
uint32_t ipv4 = address.GetLinkedIPv4();
794799
vchRet.push_back((ipv4 >> 24) & 0xFF);
795800
vchRet.push_back((ipv4 >> 16) & 0xFF);
796801
return vchRet;
797-
} else if (IsTor() || IsI2P()) {
802+
} else if (address.IsTor() || address.IsI2P()) {
798803
nBits = 4;
799-
} else if (IsCJDNS()) {
804+
} else if (address.IsCJDNS()) {
800805
// Treat in the same way as Tor and I2P because the address in all of
801806
// them is "random" bytes (derived from a public key). However in CJDNS
802807
// the first byte is a constant 0xfc, so the random bytes come after it.
803808
// Thus skip the constant 8 bits at the start.
804809
nBits = 12;
805-
} else if (IsHeNet()) {
810+
} else if (address.IsHeNet()) {
806811
// for he.net, use /36 groups
807812
nBits = 36;
808813
} else {
@@ -811,13 +816,14 @@ std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) co
811816
}
812817

813818
// Push our address onto vchRet.
819+
auto addr_bytes = address.GetAddrBytes();
814820
const size_t num_bytes = nBits / 8;
815-
vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
821+
vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
816822
nBits %= 8;
817823
// ...for the last byte, push nBits and for the rest of the byte push 1's
818824
if (nBits > 0) {
819-
assert(num_bytes < m_addr.size());
820-
vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
825+
assert(num_bytes < addr_bytes.size());
826+
vchRet.push_back(addr_bytes[num_bytes] | ((1 << (8 - nBits)) - 1));
821827
}
822828

823829
return vchRet;

0 commit comments

Comments
 (0)