aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2020-07-09 07:42:11 +0100
committerJohn Newbery <john@johnnewbery.com>2021-02-18 09:28:06 +0000
commitd21d2b264cd77c027a06f68289cf4c3f177d1ed0 (patch)
tree8af47d2c169f527e619efae3c9c979874e3a486e
parent9bbf08bf98487eeb75f143c120cfd544ea3135fb (diff)
[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.
-rw-r--r--src/net.cpp9
-rw-r--r--src/net.h3
-rw-r--r--src/net_processing.cpp5
-rw-r--r--src/test/net_tests.cpp2
4 files changed, 12 insertions, 7 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 81176785a2..bd89df63bf 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -201,8 +201,7 @@ bool IsPeerAddrLocalGood(CNode *pnode)
IsReachable(addrLocal.GetNetwork());
}
-// pushes our own address to a peer
-void AdvertiseLocal(CNode *pnode)
+Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
{
if (fListen && pnode->fSuccessfullyConnected)
{
@@ -222,10 +221,12 @@ void AdvertiseLocal(CNode *pnode)
}
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
{
- LogPrint(BCLog::NET, "AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
- pnode->PushAddress(addrLocal, rng);
+ LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
+ return addrLocal;
}
}
+ // Address is unroutable. Don't advertise.
+ return nullopt;
}
// learn a new local address
diff --git a/src/net.h b/src/net.h
index 682150d65a..67d1cf0e55 100644
--- a/src/net.h
+++ b/src/net.h
@@ -197,7 +197,8 @@ enum
};
bool IsPeerAddrLocalGood(CNode *pnode);
-void AdvertiseLocal(CNode *pnode);
+/** Returns a local address that we should advertise to this peer */
+Optional<CAddress> GetLocalAddrForPeer(CNode *pnode);
/**
* Mark a network as reachable or unreachable (no automatic connects to it)
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 881f5d7297..60b2975b67 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -4426,7 +4426,10 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
if (pto->m_next_local_addr_send != 0us) {
pto->m_addr_known->reset();
}
- AdvertiseLocal(pto);
+ if (Optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
+ FastRandomContext insecure_rand;
+ pto->PushAddress(*local_addr, insecure_rand);
+ }
pto->m_next_local_addr_send = PoissonNextSend(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
}
diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index f52905e1ef..1c7c35528e 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -691,7 +691,7 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
pnode->SetAddrLocal(addrLocal);
// before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
- AdvertiseLocal(&*pnode);
+ GetLocalAddrForPeer(&*pnode);
// suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
BOOST_CHECK(1);