MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / GetLocalAddrForPeer

Function GetLocalAddrForPeer

src/net.cpp:241–269  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

239}
240
241std::optional<CService> GetLocalAddrForPeer(CNode& node)
242{
243 CService addrLocal{GetLocalAddress(node)};
244 // If discovery is enabled, sometimes give our peer the address it
245 // tells us that it sees us as in case it has a better idea of our
246 // address than we do.
247 FastRandomContext rng;
248 if (IsPeerAddrLocalGood(&node) && (!addrLocal.IsRoutable() ||
249 rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
250 {
251 if (node.IsInboundConn()) {
252 // For inbound connections, assume both the address and the port
253 // as seen from the peer.
254 addrLocal = CService{node.GetAddrLocal()};
255 } else {
256 // For outbound connections, assume just the address as seen from
257 // the peer and leave the port in `addrLocal` as returned by
258 // `GetLocalAddress()` above. The peer has no way to observe our
259 // listening port when we have initiated the connection.
260 addrLocal.SetIP(node.GetAddrLocal());
261 }
262 }
263 if (addrLocal.IsRoutable()) {
264 LogDebug(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToStringAddrPort(), node.GetId());
265 return addrLocal;
266 }
267 // Address is unroutable. Don't advertise.
268 return std::nullopt;
269}
270
271void ClearLocal()
272{

Callers 2

MaybeSendAddrMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 10

GetLocalAddressFunction · 0.85
IsPeerAddrLocalGoodFunction · 0.85
GetnScoreFunction · 0.85
IsRoutableMethod · 0.80
randbitsMethod · 0.80
IsInboundConnMethod · 0.80
GetAddrLocalMethod · 0.80
SetIPMethod · 0.80
ToStringAddrPortMethod · 0.80
GetIdMethod · 0.80

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68