MCPcopy Create free account
hub / github.com/Bitcoin-ABC/bitcoin-abc / GetLocalAddrForPeer

Function GetLocalAddrForPeer

src/net.cpp:246–278  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

244}
245
246std::optional<CService> GetLocalAddrForPeer(CNode &node) {
247 CService addrLocal{GetLocalAddress(node.addr)};
248 if (gArgs.GetBoolArg("-addrmantest", false)) {
249 // use IPv4 loopback during addrmantest
250 addrLocal = CService(LookupNumeric("127.0.0.1", GetListenPort()));
251 }
252 // If discovery is enabled, sometimes give our peer the address it
253 // tells us that it sees us as in case it has a better idea of our
254 // address than we do.
255 FastRandomContext rng;
256 if (IsPeerAddrLocalGood(&node) &&
257 (!addrLocal.IsRoutable() ||
258 rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0)) {
259 if (node.IsInboundConn()) {
260 // For inbound connections, assume both the address and the port
261 // as seen from the peer.
262 addrLocal = CService{node.GetAddrLocal()};
263 } else {
264 // For outbound connections, assume just the address as seen from
265 // the peer and leave the port in `addrLocal` as returned by
266 // `GetLocalAddress()` above. The peer has no way to observe our
267 // listening port when we have initiated the connection.
268 addrLocal.SetIP(node.GetAddrLocal());
269 }
270 }
271 if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false)) {
272 LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n",
273 addrLocal.ToStringAddrPort(), node.GetId());
274 return addrLocal;
275 }
276 // Address is unroutable. Don't advertise.
277 return std::nullopt;
278}
279
280// Learn a new local address.
281bool AddLocal(const CService &addr, int nScore) {

Callers 2

MaybeSendAddrMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 14

GetLocalAddressFunction · 0.85
LookupNumericFunction · 0.85
GetListenPortFunction · 0.85
IsPeerAddrLocalGoodFunction · 0.85
GetnScoreFunction · 0.85
GetBoolArgMethod · 0.80
IsRoutableMethod · 0.80
randbitsMethod · 0.80
IsInboundConnMethod · 0.80
GetAddrLocalMethod · 0.80
SetIPMethod · 0.80
ToStringAddrPortMethod · 0.80

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68