Skip to content

Commit

Permalink
net: add LogIP() helper, use in net_processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Sep 18, 2024
1 parent fddfc47 commit 9967e8f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,16 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> 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
Expand Down
8 changes: 8 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,14 @@ class CNode

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.
*
Expand Down
8 changes: 2 additions & 6 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3907,15 +3907,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<NodeSeconds>();
if (!pfrom.IsInboundConn()) {
Expand Down Expand Up @@ -3959,7 +3955,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) : ""));
}

Expand Down

0 comments on commit 9967e8f

Please sign in to comment.