From a5d7aff867a3df9ac77664deed03e930e2636db0 Mon Sep 17 00:00:00 2001 From: Jadi Date: Thu, 23 May 2024 17:41:45 +0330 Subject: [PATCH 1/2] net: Providing an interface for mapLocalHost Contributes to #564 by providing an interface for mapLocalHost through net -> node interface -> clientModel. Later this value can be read by GUI to show the local addresses. --- src/interfaces/node.h | 3 +++ src/net.cpp | 7 +++++++ src/net.h | 1 + src/node/interfaces.cpp | 7 +++++++ src/qt/clientmodel.cpp | 7 +++++++ src/qt/clientmodel.h | 4 ++++ 6 files changed, 29 insertions(+) diff --git a/src/interfaces/node.h b/src/interfaces/node.h index aeb2612c07850..747ff033e4014 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -168,6 +168,9 @@ class Node //! Get num blocks. virtual int getNumBlocks() = 0; + //! Get network local addresses. + virtual std::map getNetLocalAddresses() = 0; + //! Get best block hash. virtual uint256 getBestBlockHash() = 0; diff --git a/src/net.cpp b/src/net.cpp index f05b517dd23f6..24095adcf4362 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -3533,6 +3533,13 @@ size_t CConnman::GetNodeCount(ConnectionDirection flags) const return nNum; } + +std::map CConnman::getNetLocalAddresses() const +{ + LOCK(g_maplocalhost_mutex); + return mapLocalHost; +} + uint32_t CConnman::GetMappedAS(const CNetAddr& addr) const { return m_netgroupman.GetMappedAS(addr); diff --git a/src/net.h b/src/net.h index 8075975d6201a..6f92eda2d82f4 100644 --- a/src/net.h +++ b/src/net.h @@ -1205,6 +1205,7 @@ class CConnman bool AddConnection(const std::string& address, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex); size_t GetNodeCount(ConnectionDirection) const; + std::map getNetLocalAddresses() const; uint32_t GetMappedAS(const CNetAddr& addr) const; void GetNodeStats(std::vector& vstats) const; bool DisconnectNode(const std::string& node); diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 216f44ab9e4be..1610d7c3d0bee 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -281,6 +281,13 @@ class NodeImpl : public Node } return false; } + std::map getNetLocalAddresses() override + { + if (m_context->connman) + return m_context->connman->getNetLocalAddresses(); + else + return {}; + } int getNumBlocks() override { LOCK(::cs_main); diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index 2f3bad37e6ae8..036a66c16eb2f 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -123,6 +123,13 @@ int64_t ClientModel::getHeaderTipTime() const return cachedBestHeaderTime; } + +std::map ClientModel::getNetLocalAddresses() const +{ + return m_node.getNetLocalAddresses(); +} + + int ClientModel::getNumBlocks() const { if (m_cached_num_blocks == -1) { diff --git a/src/qt/clientmodel.h b/src/qt/clientmodel.h index 624056b5df97e..3252f4af76856 100644 --- a/src/qt/clientmodel.h +++ b/src/qt/clientmodel.h @@ -13,12 +13,15 @@ #include #include +#include + class BanTableModel; class CBlockIndex; class OptionsModel; class PeerTableModel; class PeerTableSortProxy; enum class SynchronizationState; +struct LocalServiceInfo; namespace interfaces { class Handler; @@ -68,6 +71,7 @@ class ClientModel : public QObject //! Return number of connections, default is in- and outbound (total) int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const; + std::map getNetLocalAddresses() const; int getNumBlocks() const; uint256 getBestBlockHash() EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex); int getHeaderTipHeight() const; From 189c987386a0da132d7ef076cdf539f9eb75fc3c Mon Sep 17 00:00:00 2001 From: Jadi Date: Thu, 23 May 2024 17:45:57 +0330 Subject: [PATCH 2/2] Showing local addresses on the Node Window Adds a new row to the Node Window (debugwindow.ui) under the Network section which shows the LocalAddresses. fixes #564 --- src/qt/forms/debugwindow.ui | 57 ++++++++++++++++++++++++++++++------- src/qt/rpcconsole.cpp | 12 ++++++++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/qt/forms/debugwindow.ui b/src/qt/forms/debugwindow.ui index eeea53864a43d..33a452d480089 100644 --- a/src/qt/forms/debugwindow.ui +++ b/src/qt/forms/debugwindow.ui @@ -249,6 +249,41 @@ + + + Local Addresses + + + + + + + IBeamCursor + + + true + + + + 0 + 0 + + + + N/A + + + Qt::PlainText + + + Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + Network addresses that your Bitcoin node is currently using to communicate with other nodes. + + + + @@ -261,14 +296,14 @@ - + Current block height - + IBeamCursor @@ -284,14 +319,14 @@ - + Last block time - + IBeamCursor @@ -307,7 +342,7 @@ - + @@ -320,14 +355,14 @@ - + Current number of transactions - + IBeamCursor @@ -343,14 +378,14 @@ - + Memory usage - + IBeamCursor @@ -366,7 +401,7 @@ - + 3 @@ -406,7 +441,7 @@ - + Qt::Vertical diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 702ca44395df2..e2907ecdd2a2d 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -975,6 +975,18 @@ void RPCConsole::updateNetworkState() } ui->numberOfConnections->setText(connections); + + QString local_addresses; + std::map hosts = clientModel->getNetLocalAddresses(); + for (const auto& [addr, info] : hosts) { + local_addresses += QString::fromStdString(addr.ToStringAddr()); + if (!addr.IsI2P()) local_addresses += ":" + QString::number(info.nPort); + local_addresses += ", "; + } + local_addresses.chop(2); // remove last ", " + if (local_addresses.isEmpty()) local_addresses = tr("None"); + + ui->localAddresses->setText(local_addresses); } void RPCConsole::setNumConnections(int count)