Skip to content

Commit e4d541c

Browse files
committed
net, refactor: pass reference for peer address in GetReachabilityFrom
The address of the peer always exists (because addr is a member of CNode), so it was not possible to pass a nullptr before. Also remove NET_UNKNOWN, which is unused now.
1 parent 62d73f5 commit e4d541c

File tree

4 files changed

+9
-13
lines changed

4 files changed

+9
-13
lines changed

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ bool GetLocal(CService& addr, const CNode& peer)
165165
for (const auto& entry : mapLocalHost)
166166
{
167167
int nScore = entry.second.nScore;
168-
int nReachability = entry.first.GetReachabilityFrom(&peer.addr);
168+
int nReachability = entry.first.GetReachabilityFrom(peer.addr);
169169
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
170170
{
171171
addr = CService(entry.first, entry.second.nPort);

src/netaddress.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,16 @@ std::vector<unsigned char> CNetAddr::GetAddrBytes() const
723723

724724
// private extensions to enum Network, only returned by GetExtNetwork,
725725
// and only used in GetReachabilityFrom
726-
static const int NET_UNKNOWN = NET_MAX + 0;
727-
static const int NET_TEREDO = NET_MAX + 1;
728-
int static GetExtNetwork(const CNetAddr *addr)
726+
static const int NET_TEREDO = NET_MAX;
727+
int static GetExtNetwork(const CNetAddr& addr)
729728
{
730-
if (addr == nullptr)
731-
return NET_UNKNOWN;
732-
if (addr->IsRFC4380())
729+
if (addr.IsRFC4380())
733730
return NET_TEREDO;
734-
return addr->GetNetwork();
731+
return addr.GetNetwork();
735732
}
736733

737734
/** Calculates a metric for how reachable (*this) is from a given partner */
738-
int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
735+
int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
739736
{
740737
enum Reachability {
741738
REACH_UNREACHABLE,
@@ -750,7 +747,7 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
750747
if (!IsRoutable() || IsInternal())
751748
return REACH_UNREACHABLE;
752749

753-
int ourNet = GetExtNetwork(this);
750+
int ourNet = GetExtNetwork(*this);
754751
int theirNet = GetExtNetwork(paddrPartner);
755752
bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
756753

@@ -790,7 +787,6 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
790787
case NET_IPV6: return REACH_IPV6_WEAK;
791788
case NET_IPV4: return REACH_IPV4;
792789
}
793-
case NET_UNKNOWN:
794790
case NET_UNROUTABLE:
795791
default:
796792
switch(ourNet) {

src/netaddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class CNetAddr
203203
bool HasLinkedIPv4() const;
204204

205205
std::vector<unsigned char> GetAddrBytes() const;
206-
int GetReachabilityFrom(const CNetAddr* paddrPartner = nullptr) const;
206+
int GetReachabilityFrom(const CNetAddr& paddrPartner) const;
207207

208208
explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
209209
bool GetIn6Addr(struct in6_addr* pipv6Addr) const;

src/test/fuzz/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ FUZZ_TARGET(netaddress)
8484
(void)CServiceHash(0, 0)(service);
8585

8686
const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
87-
(void)net_addr.GetReachabilityFrom(&other_net_addr);
87+
(void)net_addr.GetReachabilityFrom(other_net_addr);
8888
(void)sub_net.Match(other_net_addr);
8989

9090
const CService other_service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};

0 commit comments

Comments
 (0)