Improve Tor support
[novacoin.git] / src / rpcnet.cpp
index c2c22e2..4b4448a 100644 (file)
@@ -36,6 +36,69 @@ static void CopyNodeStats(std::vector<CNodeStats>& vstats)
     }
 }
 
+struct addrManItemSort {
+    bool operator()(const CAddrInfo &leftItem, const CAddrInfo &rightItem) {
+        int64_t nTime = GetTime();
+        return leftItem.GetChance(nTime) > rightItem.GetChance(nTime);
+    }
+};
+
+Value getaddrmaninfo(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() > 1)
+        throw runtime_error(
+            "getaddrmaninfo [networkType]\n"
+            "Returns a dump of addrman data.");
+
+    // Get a full list of "online" address items
+    vector<CAddrInfo> vAddr = addrman.GetOnlineAddr();
+
+    // Sort by the GetChance result backwardly
+    sort(vAddr.begin(), vAddr.end(), addrManItemSort());
+
+    string strFilterNetType = "";
+    if (params.size() == 1)
+        strFilterNetType = params[0].get_str();
+
+    Array ret;
+    BOOST_FOREACH(const CAddrInfo &addr, vAddr) {
+        if (!addr.IsRoutable() || addr.IsLocal())
+            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";
+//            break;
+            case NET_IPV6:
+                strNetType = "ipv6";
+            break;
+            default:
+            case NET_IPV4:
+                strNetType = "ipv4";
+
+        }
+
+        if (strFilterNetType.size() != 0 && strNetType != strFilterNetType)
+            continue;
+
+        addrManItem.push_back(Pair("chance", addr.GetChance(GetTime())));
+        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)
@@ -88,7 +151,7 @@ Value addnode(const Array& params, bool fHelp)
     if (strCommand == "onetry")
     {
         CAddress addr;
-        ConnectNode(addr, strNode.c_str());
+        OpenNetworkConnection(addr, NULL, strNode.c_str());
         return Value::null;
     }