rpc: add getnetworkinfo
authorsvost <ya.nowa@yandex.ru>
Mon, 6 Feb 2017 18:45:24 +0000 (21:45 +0300)
committersvost <ya.nowa@yandex.ru>
Mon, 6 Feb 2017 18:45:24 +0000 (21:45 +0300)
src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/net.cpp
src/net.h
src/rpcnet.cpp

index e1d38ea..4348d9a 100644 (file)
@@ -236,8 +236,8 @@ Value stop(const Array& params, bool fHelp)
 static const CRPCCommand vRPCCommands[] =
 { //  name                      function                 safemd  unlocked
   //  ------------------------  -----------------------  ------  --------
-    { "help",                       &help,                        true,   true },
-    { "stop",                       &stop,                        true,   true },
+    { "help",                       &help,                        true,   true  },
+    { "stop",                       &stop,                        true,   true  },
     { "getbestblockhash",           &getbestblockhash,            true,   false },
     { "getblockcount",              &getblockcount,               true,   false },
     { "getconnectioncount",         &getconnectioncount,          true,   false },
@@ -249,10 +249,11 @@ static const CRPCCommand vRPCCommands[] =
     { "getinfo",                    &getinfo,                     true,   false },
     { "getsubsidy",                 &getsubsidy,                  true,   false },
     { "getmininginfo",              &getmininginfo,               true,   false },
-    { "scaninput",                  &scaninput,                   true,   true },
+    { "scaninput",                  &scaninput,                   true,   true  },
     { "getnewaddress",              &getnewaddress,               true,   false },
     { "getnettotals",               &getnettotals,                true,   true  },
     { "ntptime",                    &ntptime,                     true,   true  },
+    { "getnetworkinfo",             &getnetworkinfo,              true,   false },
     { "getaccountaddress",          &getaccountaddress,           true,   false },
     { "setaccount",                 &setaccount,                  true,   false },
     { "getaccount",                 &getaccount,                  false,  false },
index d304686..0923ad4 100644 (file)
@@ -162,6 +162,7 @@ extern json_spirit::Value importaddress(const json_spirit::Array& params, bool f
 extern json_spirit::Value removeaddress(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value getnettotals(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value ntptime(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value getnetworkinfo(const json_spirit::Array& params, bool fHelp);
 
 extern json_spirit::Value sendalert(const json_spirit::Array& params, bool fHelp);
 
index f10a09e..20e4c8f 100644 (file)
@@ -33,19 +33,14 @@ void ThreadDNSAddressSeed2(void* parg);
 #endif
 #endif
 
-struct LocalServiceInfo {
-    int nScore;
-    uint16_t nPort;
-};
-
 //
 // Global state variables
 //
 bool fClient = false;
 bool fDiscover = true;
 uint64_t nLocalServices = (fClient ? 0 : NODE_NETWORK);
-static CCriticalSection cs_mapLocalHost;
-static map<CNetAddr, LocalServiceInfo> mapLocalHost;
+CCriticalSection cs_mapLocalHost;
+map<CNetAddr, LocalServiceInfo> mapLocalHost;
 static bool vfReachable[NET_MAX] = {};
 static bool vfLimited[NET_MAX] = {};
 static CNode* pnodeLocalHost = NULL;
index 86bbbd5..e38df98 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -134,6 +134,13 @@ extern std::deque<std::pair<int64_t, CInv> > vRelayExpiration;
 extern CCriticalSection cs_mapRelay;
 extern std::map<CInv, int64_t> mapAlreadyAskedFor;
 
+struct LocalServiceInfo {
+    int nScore;
+    uint16_t nPort;
+};
+
+extern CCriticalSection cs_mapLocalHost;
+extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
 
 class CNodeStats
 {
index a5f9d88..f3fafb0 100644 (file)
@@ -384,3 +384,49 @@ Value ntptime(const Array& params, bool fHelp)
 
     return obj;
 }
+
+Value getnetworkinfo(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 0)
+        throw runtime_error(
+            "getnetworkinfo\n"
+            "Returns an object containing various state info regarding P2P networking.\n"
+            "\nResult:\n"
+            "{\n"
+            "  \"version\": xxxxx,           (numeric) the server version\n"
+            "  \"protocolversion\": xxxxx,   (numeric) the protocol version\n"
+            "  \"timeoffset\": xxxxx,        (numeric) the time offset\n"
+            "  \"connections\": xxxxx,       (numeric) the number of connections\n"
+            "  \"proxy\": \"host:port\",     (string, optional) the proxy used by the server\n"
+            "  \"localaddresses\": [,        (array) list of local addresses\n"
+            "    \"address\": \"xxxx\",      (string) network address\n"
+            "    \"port\": xxx,              (numeric) network port\n"
+            "    \"score\": xxx              (numeric) relative score\n"
+            "  ]\n"
+            "}\n"
+        );
+
+    proxyType proxy;
+    GetProxy(NET_IPV4, proxy);
+
+    Object obj;
+    obj.push_back(Pair("version",         CLIENT_VERSION));
+    obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
+    obj.push_back(Pair("timeoffset",      GetTimeOffset()));
+    obj.push_back(Pair("connections",     (int)vNodes.size()));
+    obj.push_back(Pair("proxy",           (proxy.IsValid() ? proxy.ToStringIPPort() : string())));
+    Array localAddresses;
+    {
+        LOCK(cs_mapLocalHost);
+        for(const auto &item : mapLocalHost)
+        {
+            Object rec;
+            rec.push_back(Pair("address", item.first.ToString()));
+            rec.push_back(Pair("port", (int)item.second.nPort));
+            rec.push_back(Pair("score", item.second.nScore));
+            localAddresses.push_back(rec);
+        }
+    }
+    obj.push_back(Pair("localaddresses", localAddresses));
+    return obj;
+}