@@ -158,39 +158,34 @@ uint16_t GetListenPort()
158
158
return static_cast <uint16_t >(gArgs .GetIntArg (" -port" , Params ().GetDefaultPort ()));
159
159
}
160
160
161
- // find ' best' local address for a particular peer
162
- bool GetLocal ( CService& addr, const CNode& peer)
161
+ // Determine the " best" local address for a particular peer.
162
+ [[nodiscard]] static std::optional< CService> GetLocal ( const CNode& peer)
163
163
{
164
- if (!fListen )
165
- return false ;
164
+ if (!fListen ) return std::nullopt;
166
165
166
+ std::optional<CService> addr;
167
167
int nBestScore = -1 ;
168
168
int nBestReachability = -1 ;
169
169
{
170
170
LOCK (g_maplocalhost_mutex);
171
- for (const auto & entry : mapLocalHost)
172
- {
171
+ for (const auto & [local_addr, local_service_info] : mapLocalHost) {
173
172
// For privacy reasons, don't advertise our privacy-network address
174
173
// to other networks and don't advertise our other-network address
175
174
// to privacy networks.
176
- const Network our_net{entry.first .GetNetwork ()};
177
- const Network peers_net{peer.ConnectedThroughNetwork ()};
178
- if (our_net != peers_net &&
179
- (our_net == NET_ONION || our_net == NET_I2P ||
180
- peers_net == NET_ONION || peers_net == NET_I2P)) {
175
+ if (local_addr.GetNetwork () != peer.ConnectedThroughNetwork ()
176
+ && (local_addr.IsPrivacyNet () || peer.IsConnectedThroughPrivacyNet ())) {
181
177
continue ;
182
178
}
183
- int nScore = entry.second .nScore ;
184
- int nReachability = entry.first .GetReachabilityFrom (peer.addr );
185
- if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
186
- {
187
- addr = CService (entry.first , entry.second .nPort );
179
+ const int nScore{local_service_info.nScore };
180
+ const int nReachability{local_addr.GetReachabilityFrom (peer.addr )};
181
+ if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
182
+ addr.emplace (CService{local_addr, local_service_info.nPort });
188
183
nBestReachability = nReachability;
189
184
nBestScore = nScore;
190
185
}
191
186
}
192
187
}
193
- return nBestScore >= 0 ;
188
+ return addr ;
194
189
}
195
190
196
191
// ! Convert the serialized seeds into usable address objects.
@@ -216,17 +211,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
216
211
return vSeedsOut;
217
212
}
218
213
219
- // get best local address for a particular peer as a CAddress
220
- // Otherwise , return the unroutable 0.0.0.0 but filled in with
214
+ // Determine the " best" local address for a particular peer.
215
+ // If none , return the unroutable 0.0.0.0 but filled in with
221
216
// the normal parameters, since the IP may be changed to a useful
222
217
// one by discovery.
223
218
CService GetLocalAddress (const CNode& peer)
224
219
{
225
- CService addr;
226
- if (GetLocal (addr, peer)) {
227
- return addr;
228
- }
229
- return CService{CNetAddr (), GetListenPort ()};
220
+ return GetLocal (peer).value_or (CService{CNetAddr (), GetListenPort ()});
230
221
}
231
222
232
223
static int GetnScore (const CService& addr)
@@ -237,7 +228,7 @@ static int GetnScore(const CService& addr)
237
228
}
238
229
239
230
// Is our peer's addrLocal potentially useful as an external IP source?
240
- bool IsPeerAddrLocalGood (CNode *pnode)
231
+ [[nodiscard]] static bool IsPeerAddrLocalGood (CNode *pnode)
241
232
{
242
233
CService addrLocal = pnode->GetAddrLocal ();
243
234
return fDiscover && pnode->addr .IsRoutable () && addrLocal.IsRoutable () &&
@@ -289,7 +280,7 @@ std::optional<CService> GetLocalAddrForPeer(CNode& node)
289
280
CService MaybeFlipIPv6toCJDNS (const CService& service)
290
281
{
291
282
CService ret{service};
292
- if (ret.m_net == NET_IPV6 && ret.m_addr [ 0 ] == 0xfc && IsReachable (NET_CJDNS)) {
283
+ if (ret.IsIPv6 () && ret.HasCJDNSPrefix () && IsReachable (NET_CJDNS)) {
293
284
ret.m_net = NET_CJDNS;
294
285
}
295
286
return ret;
@@ -505,7 +496,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
505
496
const bool use_proxy{GetProxy (addrConnect.GetNetwork (), proxy)};
506
497
bool proxyConnectionFailed = false ;
507
498
508
- if (addrConnect.GetNetwork () == NET_I2P && use_proxy) {
499
+ if (addrConnect.IsI2P () && use_proxy) {
509
500
i2p::Connection conn;
510
501
511
502
if (m_i2p_sam_session) {
@@ -637,6 +628,11 @@ Network CNode::ConnectedThroughNetwork() const
637
628
return m_inbound_onion ? NET_ONION : addr.GetNetClass ();
638
629
}
639
630
631
+ bool CNode::IsConnectedThroughPrivacyNet () const
632
+ {
633
+ return m_inbound_onion || addr.IsPrivacyNet ();
634
+ }
635
+
640
636
#undef X
641
637
#define X (name ) stats.name = name
642
638
void CNode::CopyStats (CNodeStats& stats)
@@ -3753,10 +3749,11 @@ uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& address) const
3753
3749
return GetDeterministicRandomizer (RANDOMIZER_ID_NETGROUP).Write (vchNetGroup).Finalize ();
3754
3750
}
3755
3751
3756
- void CaptureMessageToFile (const CAddress& addr,
3757
- const std::string& msg_type,
3758
- Span<const unsigned char > data,
3759
- bool is_incoming)
3752
+ // Dump binary message to file, with timestamp.
3753
+ static void CaptureMessageToFile (const CAddress& addr,
3754
+ const std::string& msg_type,
3755
+ Span<const unsigned char > data,
3756
+ bool is_incoming)
3760
3757
{
3761
3758
// Note: This function captures the message at the time of processing,
3762
3759
// not at socket receive/send time.
0 commit comments