Skip to content

Commit 821c11f

Browse files
committed
merge bitcoin#28078: remove unneeded exports, use helpers over low-level code, use optional
1 parent d051929 commit 821c11f

File tree

4 files changed

+45
-66
lines changed

4 files changed

+45
-66
lines changed

src/net.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -187,39 +187,34 @@ uint16_t GetListenPort()
187187
return static_cast<uint16_t>(gArgs.GetIntArg("-port", Params().GetDefaultPort()));
188188
}
189189

190-
// find 'best' local address for a particular peer
191-
bool GetLocal(CService& addr, const CNode& peer)
190+
// Determine the "best" local address for a particular peer.
191+
[[nodiscard]] static std::optional<CService> GetLocal(const CNode& peer)
192192
{
193-
if (!fListen)
194-
return false;
193+
if (!fListen) return std::nullopt;
195194

195+
std::optional<CService> addr;
196196
int nBestScore = -1;
197197
int nBestReachability = -1;
198198
{
199199
LOCK(g_maplocalhost_mutex);
200-
for (const auto& entry : mapLocalHost)
201-
{
200+
for (const auto& [local_addr, local_service_info] : mapLocalHost) {
202201
// For privacy reasons, don't advertise our privacy-network address
203202
// to other networks and don't advertise our other-network address
204203
// to privacy networks.
205-
const Network our_net{entry.first.GetNetwork()};
206-
const Network peers_net{peer.ConnectedThroughNetwork()};
207-
if (our_net != peers_net &&
208-
(our_net == NET_ONION || our_net == NET_I2P ||
209-
peers_net == NET_ONION || peers_net == NET_I2P)) {
204+
if (local_addr.GetNetwork() != peer.ConnectedThroughNetwork()
205+
&& (local_addr.IsPrivacyNet() || peer.IsConnectedThroughPrivacyNet())) {
210206
continue;
211207
}
212-
int nScore = entry.second.nScore;
213-
int nReachability = entry.first.GetReachabilityFrom(peer.addr);
214-
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
215-
{
216-
addr = CService(entry.first, entry.second.nPort);
208+
const int nScore{local_service_info.nScore};
209+
const int nReachability{local_addr.GetReachabilityFrom(peer.addr)};
210+
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
211+
addr.emplace(CService{local_addr, local_service_info.nPort});
217212
nBestReachability = nReachability;
218213
nBestScore = nScore;
219214
}
220215
}
221216
}
222-
return nBestScore >= 0;
217+
return addr;
223218
}
224219

225220
//! Convert the serialized seeds into usable address objects.
@@ -244,17 +239,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
244239
return vSeedsOut;
245240
}
246241

247-
// get best local address for a particular peer as a CAddress
248-
// Otherwise, return the unroutable 0.0.0.0 but filled in with
242+
// Determine the "best" local address for a particular peer.
243+
// If none, return the unroutable 0.0.0.0 but filled in with
249244
// the normal parameters, since the IP may be changed to a useful
250245
// one by discovery.
251246
CService GetLocalAddress(const CNode& peer)
252247
{
253-
CService addr;
254-
if (GetLocal(addr, peer)) {
255-
return addr;
256-
}
257-
return CService{CNetAddr(), GetListenPort()};
248+
return GetLocal(peer).value_or(CService{CNetAddr(), GetListenPort()});
258249
}
259250

260251
static int GetnScore(const CService& addr)
@@ -265,7 +256,7 @@ static int GetnScore(const CService& addr)
265256
}
266257

267258
// Is our peer's addrLocal potentially useful as an external IP source?
268-
bool IsPeerAddrLocalGood(CNode *pnode)
259+
[[nodiscard]] static bool IsPeerAddrLocalGood(CNode *pnode)
269260
{
270261
CService addrLocal = pnode->GetAddrLocal();
271262
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
@@ -313,7 +304,7 @@ std::optional<CService> GetLocalAddrForPeer(CNode& node)
313304
CService MaybeFlipIPv6toCJDNS(const CService& service)
314305
{
315306
CService ret{service};
316-
if (ret.m_net == NET_IPV6 && ret.m_addr[0] == 0xfc && IsReachable(NET_CJDNS)) {
307+
if (ret.IsIPv6() && ret.HasCJDNSPrefix() && IsReachable(NET_CJDNS)) {
317308
ret.m_net = NET_CJDNS;
318309
}
319310
return ret;
@@ -554,7 +545,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
554545
const bool use_proxy{GetProxy(addrConnect.GetNetwork(), proxy)};
555546
bool proxyConnectionFailed = false;
556547

557-
if (addrConnect.GetNetwork() == NET_I2P && use_proxy) {
548+
if (addrConnect.IsI2P() && use_proxy) {
558549
i2p::Connection conn;
559550

560551
if (m_i2p_sam_session) {
@@ -718,6 +709,11 @@ Network CNode::ConnectedThroughNetwork() const
718709
return m_inbound_onion ? NET_ONION : addr.GetNetClass();
719710
}
720711

712+
bool CNode::IsConnectedThroughPrivacyNet() const
713+
{
714+
return m_inbound_onion || addr.IsPrivacyNet();
715+
}
716+
721717
#undef X
722718
#define X(name) stats.name = name
723719
void CNode::CopyStats(CNodeStats& stats)
@@ -5169,10 +5165,11 @@ void CConnman::PerformReconnections()
51695165
}
51705166
}
51715167

5172-
void CaptureMessageToFile(const CAddress& addr,
5173-
const std::string& msg_type,
5174-
Span<const unsigned char> data,
5175-
bool is_incoming)
5168+
// Dump binary message to file, with timestamp.
5169+
static void CaptureMessageToFile(const CAddress& addr,
5170+
const std::string& msg_type,
5171+
Span<const unsigned char> data,
5172+
bool is_incoming)
51765173
{
51775174
// Note: This function captures the message at the time of processing,
51785175
// not at socket receive/send time.

src/net.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ enum
188188
LOCAL_MAX
189189
};
190190

191-
bool IsPeerAddrLocalGood(CNode *pnode);
192191
/** Returns a local address that we should advertise to this peer. */
193192
std::optional<CService> GetLocalAddrForPeer(CNode& node);
194193

@@ -207,7 +206,6 @@ bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
207206
void RemoveLocal(const CService& addr);
208207
bool SeenLocal(const CService& addr);
209208
bool IsLocal(const CService& addr);
210-
bool GetLocal(CService& addr, const CNode& peer);
211209
CService GetLocalAddress(const CNode& peer);
212210
CService MaybeFlipIPv6toCJDNS(const CService& service);
213211

@@ -939,6 +937,9 @@ class CNode
939937
}
940938

941939
public:
940+
/** Whether this peer connected through a privacy network. */
941+
[[nodiscard]] bool IsConnectedThroughPrivacyNet() const;
942+
942943
// We selected peer as (compact blocks) high-bandwidth peer (BIP152)
943944
std::atomic<bool> m_bip152_highbandwidth_to{false};
944945
// Peer selected us as (compact blocks) high-bandwidth peer (BIP152)
@@ -2004,12 +2005,6 @@ friend class CNode;
20042005
friend struct ConnmanTestMsg;
20052006
};
20062007

2007-
/** Dump binary message to file, with timestamp */
2008-
void CaptureMessageToFile(const CAddress& addr,
2009-
const std::string& msg_type,
2010-
Span<const unsigned char> data,
2011-
bool is_incoming);
2012-
20132008
/** Defaults to `CaptureMessageToFile()`, but can be overridden by unit tests. */
20142009
extern std::function<void(const CAddress& addr,
20152010
const std::string& msg_type,

src/netaddress.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,6 @@ bool CNetAddr::IsBindAny() const
310310
return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
311311
}
312312

313-
bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
314-
315-
bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
316-
317313
bool CNetAddr::IsRFC1918() const
318314
{
319315
return IsIPv4() && (
@@ -401,22 +397,6 @@ bool CNetAddr::IsHeNet() const
401397
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
402398
}
403399

404-
/**
405-
* Check whether this object represents a TOR address.
406-
* @see CNetAddr::SetSpecial(const std::string &)
407-
*/
408-
bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
409-
410-
/**
411-
* Check whether this object represents an I2P address.
412-
*/
413-
bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
414-
415-
/**
416-
* Check whether this object represents a CJDNS address.
417-
*/
418-
bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
419-
420400
bool CNetAddr::IsLocal() const
421401
{
422402
// IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
@@ -451,8 +431,7 @@ bool CNetAddr::IsValid() const
451431
return false;
452432
}
453433

454-
// CJDNS addresses always start with 0xfc
455-
if (IsCJDNS() && (m_addr[0] != 0xFC)) {
434+
if (IsCJDNS() && !HasCJDNSPrefix()) {
456435
return false;
457436
}
458437

src/netaddress.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class CNetAddr
171171
bool SetSpecial(const std::string& addr);
172172

173173
bool IsBindAny() const; // INADDR_ANY equivalent
174-
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
175-
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
174+
[[nodiscard]] bool IsIPv4() const { return m_net == NET_IPV4; } // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
175+
[[nodiscard]] bool IsIPv6() const { return m_net == NET_IPV6; } // IPv6 address (not mapped IPv4, not Tor)
176176
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
177177
bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
178178
bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
@@ -188,14 +188,22 @@ class CNetAddr
188188
bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
189189
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
190190
bool IsHeNet() const; // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
191-
bool IsTor() const;
192-
bool IsI2P() const;
193-
bool IsCJDNS() const;
191+
[[nodiscard]] bool IsTor() const { return m_net == NET_ONION; }
192+
[[nodiscard]] bool IsI2P() const { return m_net == NET_I2P; }
193+
[[nodiscard]] bool IsCJDNS() const { return m_net == NET_CJDNS; }
194+
[[nodiscard]] bool HasCJDNSPrefix() const { return m_addr[0] == 0xfc; }
194195
bool IsLocal() const;
195196
bool IsRoutable() const;
196197
bool IsInternal() const;
197198
bool IsValid() const;
198199

200+
/**
201+
* Whether this object is a privacy network.
202+
* TODO: consider adding IsCJDNS() here when more peers adopt CJDNS, see:
203+
* https://github.com/bitcoin/bitcoin/pull/27411#issuecomment-1497176155
204+
*/
205+
[[nodiscard]] bool IsPrivacyNet() const { return IsTor() || IsI2P(); }
206+
199207
/**
200208
* Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
201209
*/

0 commit comments

Comments
 (0)