@@ -722,34 +722,36 @@ Network CNetAddr::GetNetClass() const
722
722
return m_net;
723
723
}
724
724
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)) {
728
729
return 0 ; // Indicates not found, safe because AS0 is reserved per RFC7607.
729
730
}
730
731
std::vector<bool > ip_bits (128 );
731
- if (HasLinkedIPv4 ()) {
732
+ if (address. HasLinkedIPv4 ()) {
732
733
// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
733
734
for (int8_t byte_i = 0 ; byte_i < 12 ; ++byte_i) {
734
735
for (uint8_t bit_i = 0 ; bit_i < 8 ; ++bit_i) {
735
736
ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1 ;
736
737
}
737
738
}
738
- uint32_t ipv4 = GetLinkedIPv4 ();
739
+ uint32_t ipv4 = address. GetLinkedIPv4 ();
739
740
for (int i = 0 ; i < 32 ; ++i) {
740
741
ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1 ;
741
742
}
742
743
} else {
743
744
// Use all 128 bits of the IPv6 address otherwise
744
- assert (IsIPv6 ());
745
+ assert (address.IsIPv6 ());
746
+ auto addr_bytes = address.GetAddrBytes ();
745
747
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];
747
749
for (uint8_t bit_i = 0 ; bit_i < 8 ; ++bit_i) {
748
750
ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1 ;
749
751
}
750
752
}
751
753
}
752
- uint32_t mapped_as = Interpret (asmap , ip_bits);
754
+ uint32_t mapped_as = Interpret (m_asmap , ip_bits);
753
755
return mapped_as;
754
756
}
755
757
@@ -763,13 +765,13 @@ uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
763
765
* @note No two connections will be attempted to addresses with the same network
764
766
* group.
765
767
*/
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
767
769
{
770
+ const CNetAddr& address = *this ;
768
771
std::vector<unsigned char > vchRet;
769
- uint32_t net_class = GetNetClass ();
770
772
// If non-empty asmap is supplied and the address is IPv4/IPv6,
771
773
// return ASN to be used for bucketing.
772
- uint32_t asn = GetMappedAS (asmap );
774
+ uint32_t asn = GetMappedAS (m_asmap );
773
775
if (asn != 0 ) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
774
776
vchRet.push_back (NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
775
777
for (int i = 0 ; i < 4 ; i++) {
@@ -778,31 +780,34 @@ std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) co
778
780
return vchRet;
779
781
}
780
782
781
- vchRet.push_back (net_class);
783
+ vchRet.push_back (address.GetNetClass ());
784
+ int nStartByte{0 };
782
785
int nBits{0 };
783
786
784
- if (IsLocal ()) {
787
+ if (address. IsLocal ()) {
785
788
// 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 ();
788
793
nBits = ADDR_INTERNAL_SIZE * 8 ;
789
- } else if (!IsRoutable ()) {
794
+ } else if (!address. IsRoutable ()) {
790
795
// all other unroutable addresses belong to the same group
791
- } else if (HasLinkedIPv4 ()) {
796
+ } else if (address. HasLinkedIPv4 ()) {
792
797
// IPv4 addresses (and mapped IPv4 addresses) use /16 groups
793
- uint32_t ipv4 = GetLinkedIPv4 ();
798
+ uint32_t ipv4 = address. GetLinkedIPv4 ();
794
799
vchRet.push_back ((ipv4 >> 24 ) & 0xFF );
795
800
vchRet.push_back ((ipv4 >> 16 ) & 0xFF );
796
801
return vchRet;
797
- } else if (IsTor () || IsI2P ()) {
802
+ } else if (address. IsTor () || address. IsI2P ()) {
798
803
nBits = 4 ;
799
- } else if (IsCJDNS ()) {
804
+ } else if (address. IsCJDNS ()) {
800
805
// Treat in the same way as Tor and I2P because the address in all of
801
806
// them is "random" bytes (derived from a public key). However in CJDNS
802
807
// the first byte is a constant 0xfc, so the random bytes come after it.
803
808
// Thus skip the constant 8 bits at the start.
804
809
nBits = 12 ;
805
- } else if (IsHeNet ()) {
810
+ } else if (address. IsHeNet ()) {
806
811
// for he.net, use /36 groups
807
812
nBits = 36 ;
808
813
} else {
@@ -811,13 +816,14 @@ std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) co
811
816
}
812
817
813
818
// Push our address onto vchRet.
819
+ auto addr_bytes = address.GetAddrBytes ();
814
820
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);
816
822
nBits %= 8 ;
817
823
// ...for the last byte, push nBits and for the rest of the byte push 1's
818
824
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 ));
821
827
}
822
828
823
829
return vchRet;
0 commit comments