diff --git a/src/net.cpp b/src/net.cpp index 045939c2e..8ca6df0f9 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -91,9 +91,6 @@ std::vector vNodes; CCriticalSection cs_vNodes; limitedmap mapAlreadyAskedFor(MAX_INV_SZ); -std::vector vAddedNodes; -CCriticalSection cs_vAddedNodes; - NodeId nLastNodeId = 0; CCriticalSection cs_nLastNodeId; @@ -1718,7 +1715,7 @@ void CConnman::ThreadOpenConnections() } } -std::vector GetAddedNodeInfo() +std::vector CConnman::GetAddedNodeInfo() { std::vector ret; @@ -2246,6 +2243,30 @@ std::vector CConnman::GetAddresses() return addrman.GetAddr(); } +bool CConnman::AddNode(const std::string& strNode) +{ + LOCK(cs_vAddedNodes); + for(std::vector::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) { + if (strNode == *it) + return false; + } + + vAddedNodes.push_back(strNode); + return true; +} + +bool CConnman::RemoveAddedNode(const std::string& strNode) +{ + LOCK(cs_vAddedNodes); + for(std::vector::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) { + if (strNode == *it) { + vAddedNodes.erase(it); + return true; + } + } + return false; +} + void RelayTransaction(const CTransaction& tx) { CInv inv(MSG_TX, tx.GetHash()); diff --git a/src/net.h b/src/net.h index 81de7f5a9..f343646cb 100644 --- a/src/net.h +++ b/src/net.h @@ -87,6 +87,14 @@ unsigned int SendBufferSize(); typedef int NodeId; +struct AddedNodeInfo +{ + std::string strAddedNode; + CService resolvedAddress; + bool fConnected; + bool fInbound; +}; + CNode* FindNode(const CNetAddr& ip); CNode* FindNode(const CSubNet& subNet); CNode* FindNode(const std::string& addrName); @@ -137,6 +145,11 @@ public: void SetBanned(const banmap_t &banmap); void AddOneShot(const std::string& strDest); + + bool AddNode(const std::string& node); + bool RemoveAddedNode(const std::string& node); + std::vector GetAddedNodeInfo(); + private: struct ListenSocket { SOCKET socket; @@ -173,6 +186,8 @@ private: CAddrMan addrman; std::deque vOneShots; CCriticalSection cs_vOneShots; + std::vector vAddedNodes; + CCriticalSection cs_vAddedNodes; }; extern std::unique_ptr g_connman; void MapPort(bool fUseUPnP); @@ -252,9 +267,6 @@ extern std::vector vNodes; extern CCriticalSection cs_vNodes; extern limitedmap mapAlreadyAskedFor; -extern std::vector vAddedNodes; -extern CCriticalSection cs_vAddedNodes; - extern NodeId nLastNodeId; extern CCriticalSection cs_nLastNodeId; @@ -807,14 +819,4 @@ void RelayTransaction(const CTransaction& tx); /** Return a timestamp in the future (in microseconds) for exponentially distributed events. */ int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds); -struct AddedNodeInfo -{ - std::string strAddedNode; - CService resolvedAddress; - bool fConnected; - bool fInbound; -}; - -std::vector GetAddedNodeInfo(); - #endif // BITCOIN_NET_H diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 0244da9fe..ab475f712 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -226,23 +226,15 @@ UniValue addnode(const UniValue& params, bool fHelp) return NullUniValue; } - LOCK(cs_vAddedNodes); - vector::iterator it = vAddedNodes.begin(); - for(; it != vAddedNodes.end(); it++) - if (strNode == *it) - break; - if (strCommand == "add") { - if (it != vAddedNodes.end()) + if(!g_connman->AddNode(strNode)) throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added"); - vAddedNodes.push_back(strNode); } else if(strCommand == "remove") { - if (it == vAddedNodes.end()) + if(!g_connman->RemoveAddedNode(strNode)) throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added."); - vAddedNodes.erase(it); } return NullUniValue; @@ -299,7 +291,10 @@ UniValue getaddednodeinfo(const UniValue& params, bool fHelp) + HelpExampleRpc("getaddednodeinfo", "true, \"192.168.0.201\"") ); - std::vector vInfo = GetAddedNodeInfo(); + if(!g_connman) + throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); + + std::vector vInfo = g_connman->GetAddedNodeInfo(); if (params.size() == 1) { bool found = false;