diff --git a/src/main.cpp b/src/main.cpp index 2c037297b..855f6db0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6950,7 +6950,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, // Advertise our address if (fListen && !IsInitialBlockDownload()) { - CAddress addr = GetLocalAddress(&pfrom->addr); + CService service { GetLocalAddress(*pfrom) }; + CAddress addr(service); if (addr.IsRoutable()) { LogPrintf("ProcessMessages: advertizing address %s\n", addr.ToString()); diff --git a/src/net.cpp b/src/net.cpp index b460f3c37..235317590 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -179,7 +179,7 @@ unsigned short GetListenPort() } // find 'best' local address for a particular peer -bool GetLocal(CService& addr, const CNetAddr *paddrPeer) +bool GetLocal(CService& addr, const CNode& peer) { if (!fListen) return false; @@ -191,7 +191,7 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer) for (map::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++) { int nScore = (*it).second.nScore; - int nReachability = (*it).first.GetReachabilityFrom(paddrPeer); + int nReachability = (*it).first.GetReachabilityFrom(peer.addr); if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) { addr = CService((*it).first, (*it).second.nPort); @@ -228,12 +228,11 @@ static std::vector ConvertSeeds(const std::vector &vSeedsIn) // Otherwise, return the unroutable 0.0.0.0 but filled in with // the normal parameters, since the IP may be changed to a useful // one by discovery. -CAddress GetLocalAddress(const CNetAddr *paddrPeer) +CService GetLocalAddress(const CNode& peer) { CAddress ret(CService(CNetAddr(),GetListenPort()),0); CService addr; - if (GetLocal(addr, paddrPeer)) - { + if (GetLocal(addr, peer)) { ret = CAddress(addr); } ret.nServices = nLocalServices; @@ -261,7 +260,7 @@ void AdvertizeLocal(CNode *pnode) { if (fListen && pnode->fSuccessfullyConnected) { - CAddress addrLocal = GetLocalAddress(&pnode->addr); + CAddress addrLocal { GetLocalAddress(*pnode) }; // If discovery is enabled, sometimes give our peer the address it // tells us that it sees us as in case it has a better idea of our // address than we do. @@ -576,14 +575,15 @@ extern int32_t HUSH_NSPV; #define HUSH_NSPV_SUPERLITE (HUSH_NSPV > 0) #endif // !HUSH_NSPV_SUPERLITE - void CNode::PushVersion() { int nBestHeight = g_signals.GetHeight().get_value_or(0); int64_t nTime = (fInbound ? GetTime() : GetTime()); CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices)); - CAddress addrMe = GetLocalAddress(&addr); + CNode *addr = this; + CService service = GetLocalAddress(*addr); + CAddress addrMe(service); GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); if (fLogIPs) LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id); diff --git a/src/net.h b/src/net.h index 95e1c58f0..7df42b0db 100644 --- a/src/net.h +++ b/src/net.h @@ -228,8 +228,7 @@ void SetReachable(enum Network net, bool reachable); bool IsReachable(enum Network net); /** @returns true if the address is in a reachable network, false otherwise */ bool IsReachable(const CNetAddr& addr); -CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL); - +CService GetLocalAddress(const CNode& peer); extern bool fDiscover; extern bool fListen; diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 2978f9c2b..391bba62b 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -838,19 +838,17 @@ uint64_t CNetAddr::GetHash() const // private extensions to enum Network, only returned by GetExtNetwork, // and only used in GetReachabilityFrom -static const int NET_UNKNOWN = NET_MAX + 0; -static const int NET_TEREDO = NET_MAX + 1; -int static GetExtNetwork(const CNetAddr *addr) +// Teredo is IPv6-over-UDP +static const int NET_TEREDO = NET_MAX; +int static GetExtNetwork(const CNetAddr& addr) { - if (addr == NULL) - return NET_UNKNOWN; - if (addr->IsRFC4380()) + if (addr.IsRFC4380()) return NET_TEREDO; - return addr->GetNetwork(); + return addr.GetNetwork(); } /** Calculates a metric for how reachable (*this) is from a given partner */ -int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const +int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const { enum Reachability { REACH_UNREACHABLE, @@ -865,7 +863,7 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const if (!IsRoutable() || IsInternal()) return REACH_UNREACHABLE; - int ourNet = GetExtNetwork(this); + int ourNet = GetExtNetwork(*this); int theirNet = GetExtNetwork(paddrPartner); bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145(); @@ -886,7 +884,7 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const switch(ourNet) { default: return REACH_DEFAULT; case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well - case NET_ONION: return REACH_PRIVATE; + case NET_ONION: return REACH_PRIVATE; } case NET_TEREDO: switch(ourNet) { @@ -895,7 +893,6 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const case NET_IPV6: return REACH_IPV6_WEAK; case NET_IPV4: return REACH_IPV4; } - case NET_UNKNOWN: case NET_UNROUTABLE: default: switch(ourNet) { @@ -903,7 +900,7 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const case NET_TEREDO: return REACH_TEREDO; case NET_IPV6: return REACH_IPV6_WEAK; case NET_IPV4: return REACH_IPV4; - case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address + case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address } } } diff --git a/src/netaddress.h b/src/netaddress.h index aba2b307f..a53ee78cd 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -204,7 +204,7 @@ class CNetAddr std::vector GetGroup(const std::vector &asmap) const; std::vector GetAddrBytes() const; - int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const; + int GetReachabilityFrom(const CNetAddr& paddrPartner) const; CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0); bool GetIn6Addr(struct in6_addr* pipv6Addr) const;