RPC: getaddrmaninfo
authorMASM fan <masmfan@gmail.com>
Wed, 14 Jan 2015 17:48:52 +0000 (09:48 -0800)
committerMASM fan <masmfan@gmail.com>
Wed, 14 Jan 2015 17:48:52 +0000 (09:48 -0800)
This new call allows user to get a list of random address entries from addrman store.

src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/rpcnet.cpp

index 86c062d..bd711d1 100644 (file)
@@ -242,6 +242,7 @@ static const CRPCCommand vRPCCommands[] =
     { "getbestblockhash",       &getbestblockhash,       true,   false },
     { "getblockcount",          &getblockcount,          true,   false },
     { "getconnectioncount",     &getconnectioncount,     true,   false },
+    { "getaddrmaninfo",         &getaddrmaninfo,         true,   false },
     { "getpeerinfo",            &getpeerinfo,            true,   false },
     { "addnode",                &addnode,                true,   true  },
     { "getaddednodeinfo",       &getaddednodeinfo,       true,   true  },
index 72f7128..ecb14f8 100644 (file)
@@ -146,6 +146,7 @@ extern std::vector<unsigned char> 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 getaddrmaninfo(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp);
index 604db0f..5404c01 100644 (file)
@@ -36,6 +36,52 @@ static void CopyNodeStats(std::vector<CNodeStats>& vstats)
     }
 }
 
+Value getaddrmaninfo(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 0)
+        throw runtime_error(
+            "getaddrmaninfo\n"
+            "Returns a dump of addrman data.");
+
+    vector<CAddress> vAddr = addrman.GetAddr();
+
+    Array ret;
+
+    BOOST_FOREACH(const CAddress &addr, vAddr) {
+        // Don't return addresses older than nCutOff timestamp
+        int64_t nCutOff = GetTime() - (nNodeLifespan * 24 * 60 * 60);
+
+        if (!addr.IsRoutable() || addr.IsLocal() || addr.nTime > nCutOff)
+            continue;
+
+        Object addrManItem;
+        addrManItem.push_back(Pair("address", addr.ToString()));
+
+        string strNetType;
+        switch(addr.GetNetwork())
+        {
+            case NET_TOR:
+                strNetType = "tor";
+            break;
+            case NET_I2P:
+                strNetType = "i2p";
+            case NET_IPV4:
+                strNetType = "ipv4";
+            break;
+            default:
+            case NET_IPV6:
+                strNetType = "ipv6";
+
+        }
+        addrManItem.push_back(Pair("type", strNetType));
+        addrManItem.push_back(Pair("time", (int64_t)addr.nTime));
+
+        ret.push_back(addrManItem);
+    }
+
+    return ret;
+}
+
 Value getpeerinfo(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() != 0)