Skip to content

Commit d21d2b2

Browse files
committed
[net] Change AdvertiseLocal to GetLocalAddrForPeer
Gossiping addresses to peers is the responsibility of net processing. Change AdvertiseLocal() in net to just return an (optional) address for net processing to advertise. Update function name to reflect new responsibility.
1 parent 9bbf08b commit d21d2b2

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

src/net.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ 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
{
207206
if (fListen && pnode->fSuccessfullyConnected)
208207
{
@@ -222,10 +221,12 @@ void AdvertiseLocal(CNode *pnode)
222221
}
223222
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
224223
{
225-
LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
226-
pnode->PushAddress(addrLocal, rng);
224+
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
225+
return addrLocal;
227226
}
228227
}
228+
// Address is unroutable. Don't advertise.
229+
return nullopt;
229230
}
230231

231232
// 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4426,7 +4426,10 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44264426
if (pto->m_next_local_addr_send != 0us) {
44274427
pto->m_addr_known->reset();
44284428
}
4429-
AdvertiseLocal(pto);
4429+
if (Optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
4430+
FastRandomContext insecure_rand;
4431+
pto->PushAddress(*local_addr, insecure_rand);
4432+
}
44304433
pto->m_next_local_addr_send = PoissonNextSend(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
44314434
}
44324435

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)