Skip to content

Commit 5961f8e

Browse files
committed
[net] Return CService from GetLocalAddrForPeer and GetLocalAddress
1 parent d9079fe commit 5961f8e

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

src/net.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
206206
// Otherwise, return the unroutable 0.0.0.0 but filled in with
207207
// the normal parameters, since the IP may be changed to a useful
208208
// one by discovery.
209-
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
209+
CService GetLocalAddress(const CNetAddr& addrPeer)
210210
{
211-
CAddress ret(CService(CNetAddr(),GetListenPort()), nLocalServices);
211+
CService ret{CNetAddr(), GetListenPort()};
212212
CService addr;
213-
if (GetLocal(addr, paddrPeer))
214-
{
215-
ret = CAddress(addr, nLocalServices);
213+
if (GetLocal(addr, &addrPeer)) {
214+
ret = CService{addr};
216215
}
217-
ret.nTime = GetAdjustedTime();
218216
return ret;
219217
}
220218

@@ -233,35 +231,35 @@ bool IsPeerAddrLocalGood(CNode *pnode)
233231
IsReachable(addrLocal.GetNetwork());
234232
}
235233

236-
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
234+
std::optional<CService> GetLocalAddrForPeer(CNode& node)
237235
{
238-
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
236+
CService addrLocal{GetLocalAddress(node.addr)};
239237
if (gArgs.GetBoolArg("-addrmantest", false)) {
240238
// use IPv4 loopback during addrmantest
241-
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
239+
addrLocal = CService(LookupNumeric("127.0.0.1", GetListenPort()));
242240
}
243241
// If discovery is enabled, sometimes give our peer the address it
244242
// tells us that it sees us as in case it has a better idea of our
245243
// address than we do.
246244
FastRandomContext rng;
247-
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
245+
if (IsPeerAddrLocalGood(&node) && (!addrLocal.IsRoutable() ||
248246
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
249247
{
250-
if (pnode->IsInboundConn()) {
248+
if (node.IsInboundConn()) {
251249
// For inbound connections, assume both the address and the port
252250
// as seen from the peer.
253-
addrLocal = CAddress{pnode->GetAddrLocal(), addrLocal.nServices, addrLocal.nTime};
251+
addrLocal = CService{node.GetAddrLocal()};
254252
} else {
255253
// For outbound connections, assume just the address as seen from
256254
// the peer and leave the port in `addrLocal` as returned by
257255
// `GetLocalAddress()` above. The peer has no way to observe our
258256
// listening port when we have initiated the connection.
259-
addrLocal.SetIP(pnode->GetAddrLocal());
257+
addrLocal.SetIP(node.GetAddrLocal());
260258
}
261259
}
262260
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
263261
{
264-
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
262+
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), node.GetId());
265263
return addrLocal;
266264
}
267265
// Address is unroutable. Don't advertise.

src/net.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ enum
144144
};
145145

146146
bool IsPeerAddrLocalGood(CNode *pnode);
147-
/** Returns a local address that we should advertise to this peer */
148-
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode);
147+
/** Returns a local address that we should advertise to this peer. */
148+
std::optional<CService> GetLocalAddrForPeer(CNode& node);
149149

150150
/**
151151
* Mark a network as reachable or unreachable (no automatic connects to it)
@@ -163,7 +163,7 @@ void RemoveLocal(const CService& addr);
163163
bool SeenLocal(const CService& addr);
164164
bool IsLocal(const CService& addr);
165165
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr);
166-
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices);
166+
CService GetLocalAddress(const CNetAddr& addrPeer);
167167

168168

169169
extern bool fDiscover;

src/net_processing.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,7 +2930,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
29302930
// indicate to the peer that we will participate in addr relay.
29312931
if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload())
29322932
{
2933-
CAddress addr = GetLocalAddress(&pfrom.addr, pfrom.GetLocalServices());
2933+
CAddress addr{GetLocalAddress(pfrom.addr), peer->m_our_services, (uint32_t)GetAdjustedTime()};
29342934
FastRandomContext insecure_rand;
29352935
if (addr.IsRoutable())
29362936
{
@@ -4685,9 +4685,10 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
46854685
if (peer.m_next_local_addr_send != 0us) {
46864686
peer.m_addr_known->reset();
46874687
}
4688-
if (std::optional<CAddress> local_addr = GetLocalAddrForPeer(&node)) {
4688+
if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
4689+
CAddress local_addr{*local_service, peer.m_our_services, (uint32_t)GetAdjustedTime()};
46894690
FastRandomContext insecure_rand;
4690-
PushAddress(peer, *local_addr, insecure_rand);
4691+
PushAddress(peer, local_addr, insecure_rand);
46914692
}
46924693
peer.m_next_local_addr_send = GetExponentialRand(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
46934694
}

src/test/net_tests.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
648648
pnode->SetAddrLocal(addrLocal);
649649

650650
// before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
651-
GetLocalAddrForPeer(&*pnode);
651+
GetLocalAddrForPeer(*pnode);
652652

653653
// suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
654654
BOOST_CHECK(1);
@@ -675,13 +675,10 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
675675
const uint16_t bind_port = 20001;
676676
m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
677677

678-
const uint32_t current_time = static_cast<uint32_t>(GetAdjustedTime());
679-
SetMockTime(current_time);
680-
681678
// Our address:port as seen from the peer, completely different from the above.
682679
in_addr peer_us_addr;
683680
peer_us_addr.s_addr = htonl(0x02030405);
684-
const CAddress peer_us{CService{peer_us_addr, 20002}, NODE_NETWORK, current_time};
681+
const CService peer_us{peer_us_addr, 20002};
685682

686683
// Create a peer with a routable IPv4 address (outbound).
687684
in_addr peer_out_in_addr;
@@ -700,9 +697,9 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
700697
peer_out.SetAddrLocal(peer_us);
701698

702699
// Without the fix peer_us:8333 is chosen instead of the proper peer_us:bind_port.
703-
auto chosen_local_addr = GetLocalAddrForPeer(&peer_out);
700+
auto chosen_local_addr = GetLocalAddrForPeer(peer_out);
704701
BOOST_REQUIRE(chosen_local_addr);
705-
const CAddress expected{CService{peer_us_addr, bind_port}, NODE_NETWORK, current_time};
702+
const CService expected{peer_us_addr, bind_port};
706703
BOOST_CHECK(*chosen_local_addr == expected);
707704

708705
// Create a peer with a routable IPv4 address (inbound).
@@ -722,7 +719,7 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
722719
peer_in.SetAddrLocal(peer_us);
723720

724721
// Without the fix peer_us:8333 is chosen instead of the proper peer_us:peer_us.GetPort().
725-
chosen_local_addr = GetLocalAddrForPeer(&peer_in);
722+
chosen_local_addr = GetLocalAddrForPeer(peer_in);
726723
BOOST_REQUIRE(chosen_local_addr);
727724
BOOST_CHECK(*chosen_local_addr == peer_us);
728725

0 commit comments

Comments
 (0)