From 5e16a8b47dcc39875b55e578ba1c4dd929fa1332 Mon Sep 17 00:00:00 2001 From: fsb4000 Date: Wed, 31 Dec 2014 03:12:39 +0600 Subject: [PATCH] Add addnode RPC command. --- src/bitcoinrpc.cpp | 1 + src/bitcoinrpc.h | 1 + src/net.cpp | 3 +++ src/net.h | 2 ++ src/rpcnet.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 0 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index dc4b2bb..7ccca22 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -243,6 +243,7 @@ static const CRPCCommand vRPCCommands[] = { "getblockcount", &getblockcount, true, false }, { "getconnectioncount", &getconnectioncount, true, false }, { "getpeerinfo", &getpeerinfo, true, false }, + { "addnode", &addnode, true, true }, { "getdifficulty", &getdifficulty, true, false }, { "getinfo", &getinfo, true, false }, { "getsubsidy", &getsubsidy, true, false }, diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index dcd8a9d..19c7f7c 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -146,6 +146,7 @@ extern std::vector ParseHexO(const json_spirit::Object& o, std::s extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp diff --git a/src/net.cpp b/src/net.cpp index 55d761e..821cff8 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -75,6 +75,9 @@ CCriticalSection cs_vOneShots; set setservAddNodeAddresses; CCriticalSection cs_setservAddNodeAddresses; +vector vAddedNodes; +CCriticalSection cs_vAddedNodes; + static CSemaphore *semOutbound = NULL; void AddOneShot(string strDest) diff --git a/src/net.h b/src/net.h index 2d635fa..56a3391 100644 --- a/src/net.h +++ b/src/net.h @@ -125,6 +125,8 @@ extern CAddrMan addrman; extern std::vector vNodes; extern CCriticalSection cs_vNodes; +extern std::vector vAddedNodes; +extern CCriticalSection cs_vAddedNodes; extern std::map mapRelay; extern std::deque > vRelayExpiration; extern CCriticalSection cs_mapRelay; diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index 3b283d9..5078a79 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -72,6 +72,49 @@ Value getpeerinfo(const Array& params, bool fHelp) return ret; } +Value addnode(const Array& params, bool fHelp) +{ + string strCommand; + if (params.size() == 2) + strCommand = params[1].get_str(); + if (fHelp || params.size() != 2 || + (strCommand != "onetry" && strCommand != "add" && strCommand != "remove")) + throw runtime_error( + "addnode \n" + "Attempts add or remove from the addnode list or try a connection to once."); + + string strNode = params[0].get_str(); + + if (strCommand == "onetry") + { + CAddress addr; + ConnectNode(addr, strNode.c_str()); + return Value::null; + } + + LOCK(cs_vAddedNodes); + vector::iterator it = vAddedNodes.begin(); + for(; it != vAddedNodes.end(); it++) + if (strNode == *it) + break; + + if (strCommand == "add") + { + if (it != vAddedNodes.end()) + throw JSONRPCError(-23, "Error: Node already added"); + vAddedNodes.push_back(strNode); + } + else if(strCommand == "remove") + { + if (it == vAddedNodes.end()) + throw JSONRPCError(-24, "Error: Node has not been added."); + vAddedNodes.erase(it); + } + + return Value::null; +} + + extern CCriticalSection cs_mapAlerts; extern map mapAlerts; -- 1.7.1