Skip to content

Commit 09eb46c

Browse files
author
MarcoFalke
committed
Merge #21187: Net processing: Only call PushAddress() from net_processing
3e68efa [net] Move checks from GetLocalAddrForPeer to caller (John Newbery) d21d2b2 [net] Change AdvertiseLocal to GetLocalAddrForPeer (John Newbery) Pull request description: This is the first part of #21186. It slightly disentangles addr handling in net/net_processing by making it explicit that net_processing is responsible for pushing addr records into `vAddrToSend`. ACKs for top commit: MarcoFalke: re-ACK 3e68efa 🍅 Tree-SHA512: 9af50c41f5a977e2e277f24a589db38e2980b353401def5e74b108ac5f493d9b5d6b1b8bf15323a4d66321495f04bc271450fcef7aa7d1c095f051a4f8e9b15f
2 parents 6a680a6 + 3e68efa commit 09eb46c

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

src/net.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,31 +201,29 @@ bool IsPeerAddrLocalGood(CNode *pnode)
201201
IsReachable(addrLocal.GetNetwork());
202202
}
203203

204-
// pushes our own address to a peer
205-
void AdvertiseLocal(CNode *pnode)
204+
Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
206205
{
207-
if (fListen && pnode->fSuccessfullyConnected)
206+
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
207+
if (gArgs.GetBoolArg("-addrmantest", false)) {
208+
// use IPv4 loopback during addrmantest
209+
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
210+
}
211+
// If discovery is enabled, sometimes give our peer the address it
212+
// tells us that it sees us as in case it has a better idea of our
213+
// address than we do.
214+
FastRandomContext rng;
215+
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
216+
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
208217
{
209-
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
210-
if (gArgs.GetBoolArg("-addrmantest", false)) {
211-
// use IPv4 loopback during addrmantest
212-
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
213-
}
214-
// If discovery is enabled, sometimes give our peer the address it
215-
// tells us that it sees us as in case it has a better idea of our
216-
// address than we do.
217-
FastRandomContext rng;
218-
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
219-
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
220-
{
221-
addrLocal.SetIP(pnode->GetAddrLocal());
222-
}
223-
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
224-
{
225-
LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
226-
pnode->PushAddress(addrLocal, rng);
227-
}
218+
addrLocal.SetIP(pnode->GetAddrLocal());
219+
}
220+
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
221+
{
222+
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
223+
return addrLocal;
228224
}
225+
// Address is unroutable. Don't advertise.
226+
return nullopt;
229227
}
230228

231229
// learn a new local address

src/net.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ enum
197197
};
198198

199199
bool IsPeerAddrLocalGood(CNode *pnode);
200-
void AdvertiseLocal(CNode *pnode);
200+
/** Returns a local address that we should advertise to this peer */
201+
Optional<CAddress> GetLocalAddrForPeer(CNode *pnode);
201202

202203
/**
203204
* Mark a network as reachable or unreachable (no automatic connects to it)

src/net_processing.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,7 +4416,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44164416
// Address refresh broadcast
44174417
auto current_time = GetTime<std::chrono::microseconds>();
44184418

4419-
if (pto->RelayAddrsWithConn() && !::ChainstateActive().IsInitialBlockDownload() && pto->m_next_local_addr_send < current_time) {
4419+
if (fListen && pto->RelayAddrsWithConn() &&
4420+
!::ChainstateActive().IsInitialBlockDownload() &&
4421+
pto->m_next_local_addr_send < current_time) {
44204422
// If we've sent before, clear the bloom filter for the peer, so that our
44214423
// self-announcement will actually go out.
44224424
// This might be unnecessary if the bloom filter has already rolled
@@ -4426,7 +4428,10 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44264428
if (pto->m_next_local_addr_send != 0us) {
44274429
pto->m_addr_known->reset();
44284430
}
4429-
AdvertiseLocal(pto);
4431+
if (Optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
4432+
FastRandomContext insecure_rand;
4433+
pto->PushAddress(*local_addr, insecure_rand);
4434+
}
44304435
pto->m_next_local_addr_send = PoissonNextSend(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
44314436
}
44324437

src/test/net_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
691691
pnode->SetAddrLocal(addrLocal);
692692

693693
// before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
694-
AdvertiseLocal(&*pnode);
694+
GetLocalAddrForPeer(&*pnode);
695695

696696
// suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
697697
BOOST_CHECK(1);

0 commit comments

Comments
 (0)