@@ -187,39 +187,34 @@ uint16_t GetListenPort()
187187 return static_cast <uint16_t >(gArgs .GetIntArg (" -port" , Params ().GetDefaultPort ()));
188188}
189189
190- // find ' best' local address for a particular peer
191- bool GetLocal ( CService& addr, const CNode& peer)
190+ // Determine the " best" local address for a particular peer.
191+ [[nodiscard]] static std::optional< CService> GetLocal ( const CNode& peer)
192192{
193- if (!fListen )
194- return false ;
193+ if (!fListen ) return std::nullopt ;
195194
195+ std::optional<CService> addr;
196196 int nBestScore = -1 ;
197197 int nBestReachability = -1 ;
198198 {
199199 LOCK (g_maplocalhost_mutex);
200- for (const auto & entry : mapLocalHost)
201- {
200+ for (const auto & [local_addr, local_service_info] : mapLocalHost) {
202201 // For privacy reasons, don't advertise our privacy-network address
203202 // to other networks and don't advertise our other-network address
204203 // to privacy networks.
205- const Network our_net{entry.first .GetNetwork ()};
206- const Network peers_net{peer.ConnectedThroughNetwork ()};
207- if (our_net != peers_net &&
208- (our_net == NET_ONION || our_net == NET_I2P ||
209- peers_net == NET_ONION || peers_net == NET_I2P)) {
204+ if (local_addr.GetNetwork () != peer.ConnectedThroughNetwork ()
205+ && (local_addr.IsPrivacyNet () || peer.IsConnectedThroughPrivacyNet ())) {
210206 continue ;
211207 }
212- int nScore = entry.second .nScore ;
213- int nReachability = entry.first .GetReachabilityFrom (peer.addr );
214- if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
215- {
216- addr = CService (entry.first , entry.second .nPort );
208+ const int nScore{local_service_info.nScore };
209+ const int nReachability{local_addr.GetReachabilityFrom (peer.addr )};
210+ if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
211+ addr.emplace (CService{local_addr, local_service_info.nPort });
217212 nBestReachability = nReachability;
218213 nBestScore = nScore;
219214 }
220215 }
221216 }
222- return nBestScore >= 0 ;
217+ return addr ;
223218}
224219
225220// ! Convert the serialized seeds into usable address objects.
@@ -244,17 +239,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
244239 return vSeedsOut;
245240}
246241
247- // get best local address for a particular peer as a CAddress
248- // Otherwise , return the unroutable 0.0.0.0 but filled in with
242+ // Determine the " best" local address for a particular peer.
243+ // If none , return the unroutable 0.0.0.0 but filled in with
249244// the normal parameters, since the IP may be changed to a useful
250245// one by discovery.
251246CService GetLocalAddress (const CNode& peer)
252247{
253- CService addr;
254- if (GetLocal (addr, peer)) {
255- return addr;
256- }
257- return CService{CNetAddr (), GetListenPort ()};
248+ return GetLocal (peer).value_or (CService{CNetAddr (), GetListenPort ()});
258249}
259250
260251static int GetnScore (const CService& addr)
@@ -265,7 +256,7 @@ static int GetnScore(const CService& addr)
265256}
266257
267258// Is our peer's addrLocal potentially useful as an external IP source?
268- bool IsPeerAddrLocalGood (CNode *pnode)
259+ [[nodiscard]] static bool IsPeerAddrLocalGood (CNode *pnode)
269260{
270261 CService addrLocal = pnode->GetAddrLocal ();
271262 return fDiscover && pnode->addr .IsRoutable () && addrLocal.IsRoutable () &&
@@ -313,7 +304,7 @@ std::optional<CService> GetLocalAddrForPeer(CNode& node)
313304CService MaybeFlipIPv6toCJDNS (const CService& service)
314305{
315306 CService ret{service};
316- if (ret.m_net == NET_IPV6 && ret.m_addr [ 0 ] == 0xfc && IsReachable (NET_CJDNS)) {
307+ if (ret.IsIPv6 () && ret.HasCJDNSPrefix () && IsReachable (NET_CJDNS)) {
317308 ret.m_net = NET_CJDNS;
318309 }
319310 return ret;
@@ -554,7 +545,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
554545 const bool use_proxy{GetProxy (addrConnect.GetNetwork (), proxy)};
555546 bool proxyConnectionFailed = false ;
556547
557- if (addrConnect.GetNetwork () == NET_I2P && use_proxy) {
548+ if (addrConnect.IsI2P () && use_proxy) {
558549 i2p::Connection conn;
559550
560551 if (m_i2p_sam_session) {
@@ -718,6 +709,11 @@ Network CNode::ConnectedThroughNetwork() const
718709 return m_inbound_onion ? NET_ONION : addr.GetNetClass ();
719710}
720711
712+ bool CNode::IsConnectedThroughPrivacyNet () const
713+ {
714+ return m_inbound_onion || addr.IsPrivacyNet ();
715+ }
716+
721717#undef X
722718#define X (name ) stats.name = name
723719void CNode::CopyStats (CNodeStats& stats)
@@ -5169,10 +5165,11 @@ void CConnman::PerformReconnections()
51695165 }
51705166}
51715167
5172- void CaptureMessageToFile (const CAddress& addr,
5173- const std::string& msg_type,
5174- Span<const unsigned char > data,
5175- bool is_incoming)
5168+ // Dump binary message to file, with timestamp.
5169+ static void CaptureMessageToFile (const CAddress& addr,
5170+ const std::string& msg_type,
5171+ Span<const unsigned char > data,
5172+ bool is_incoming)
51765173{
51775174 // Note: This function captures the message at the time of processing,
51785175 // not at socket receive/send time.
0 commit comments