From 1d01ad4d73e0eaae36c31153884e3d394ebc66b7 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 23 Jul 2024 12:52:38 +0200 Subject: [PATCH] net: add LogIP() helper, use in net_processing --- src/net.cpp | 7 ++++++- src/net.h | 8 ++++++++ src/net_processing.cpp | 8 ++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 50fe391862..fcf17d3f27 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -695,11 +695,16 @@ bool CNode::ReceiveMsgBytes(Span msg_bytes, bool& complete) return true; } +std::string CNode::LogIP(bool log_ip) const +{ + return log_ip ? strprintf(" peeraddr=%s", addr.ToStringAddrPort()) : ""; +} + std::string CNode::DisconnectMsg(bool log_ip) const { return strprintf("disconnecting peer=%d%s", GetId(), - log_ip ? strprintf(" peeraddr=%s", addr.ToStringAddrPort()) : ""); + LogIP(log_ip)); } V1Transport::V1Transport(const NodeId node_id) noexcept diff --git a/src/net.h b/src/net.h index 77abac8f11..99a9d0da4b 100644 --- a/src/net.h +++ b/src/net.h @@ -947,6 +947,14 @@ public: std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); } + /** + * Helper function to optionally log the IP address. + * + * @param[in] log_ip whether to include the IP address + * @return " peeraddr=..." or "" + */ + std::string LogIP(bool log_ip) const; + /** * Helper function to log disconnects. * diff --git a/src/net_processing.cpp b/src/net_processing.cpp index f3b54c37dc..a19443c0f5 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3568,15 +3568,11 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, m_addrman.Good(pfrom.addr); } - std::string remoteAddr; - if (fLogIPs) - remoteAddr = ", peeraddr=" + pfrom.addr.ToStringAddrPort(); - const auto mapped_as{m_connman.GetMappedAS(pfrom.addr)}; LogDebug(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, txrelay=%d, peer=%d%s%s\n", cleanSubVer, pfrom.nVersion, peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(), - remoteAddr, (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : "")); + pfrom.LogIP(fLogIPs), (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : "")); peer->m_time_offset = NodeSeconds{std::chrono::seconds{nTime}} - Now(); if (!pfrom.IsInboundConn()) { @@ -3620,7 +3616,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, pfrom.ConnectionTypeAsString(), TransportTypeAsString(pfrom.m_transport->GetInfo().transport_type), pfrom.nVersion.load(), peer->m_starting_height, - pfrom.GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToStringAddrPort()) : ""), + pfrom.GetId(), pfrom.LogIP(fLogIPs), (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : "")); }