2012-06-29 05:18:38 +02:00
|
|
|
// Copyright (c) 2009-2012 Bitcoin Developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "net.h"
|
|
|
|
#include "bitcoinrpc.h"
|
|
|
|
|
|
|
|
using namespace json_spirit;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Value getconnectioncount(const Array& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (fHelp || params.size() != 0)
|
|
|
|
throw runtime_error(
|
|
|
|
"getconnectioncount\n"
|
|
|
|
"Returns the number of connections to other nodes.");
|
|
|
|
|
2012-06-29 23:24:53 +02:00
|
|
|
LOCK(cs_vNodes);
|
2012-06-29 05:18:38 +02:00
|
|
|
return (int)vNodes.size();
|
|
|
|
}
|
|
|
|
|
2012-06-29 23:24:53 +02:00
|
|
|
static void CopyNodeStats(std::vector<CNodeStats>& vstats)
|
|
|
|
{
|
|
|
|
vstats.clear();
|
|
|
|
|
|
|
|
LOCK(cs_vNodes);
|
|
|
|
vstats.reserve(vNodes.size());
|
|
|
|
BOOST_FOREACH(CNode* pnode, vNodes) {
|
|
|
|
CNodeStats stats;
|
|
|
|
pnode->copyStats(stats);
|
|
|
|
vstats.push_back(stats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value getpeerinfo(const Array& params, bool fHelp)
|
|
|
|
{
|
|
|
|
if (fHelp || params.size() != 0)
|
|
|
|
throw runtime_error(
|
|
|
|
"getpeerinfo\n"
|
|
|
|
"Returns data about each connected network node.");
|
|
|
|
|
|
|
|
vector<CNodeStats> vstats;
|
|
|
|
CopyNodeStats(vstats);
|
|
|
|
|
|
|
|
Array ret;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CNodeStats& stats, vstats) {
|
|
|
|
Object obj;
|
|
|
|
|
|
|
|
obj.push_back(Pair("addr", stats.addrName));
|
|
|
|
obj.push_back(Pair("services", strprintf("%08"PRI64x, stats.nServices)));
|
|
|
|
obj.push_back(Pair("lastsend", (boost::int64_t)stats.nLastSend));
|
|
|
|
obj.push_back(Pair("lastrecv", (boost::int64_t)stats.nLastRecv));
|
|
|
|
obj.push_back(Pair("conntime", (boost::int64_t)stats.nTimeConnected));
|
|
|
|
obj.push_back(Pair("version", stats.nVersion));
|
|
|
|
obj.push_back(Pair("subver", stats.strSubVer));
|
|
|
|
obj.push_back(Pair("inbound", stats.fInbound));
|
|
|
|
obj.push_back(Pair("releasetime", (boost::int64_t)stats.nReleaseTime));
|
2012-07-01 05:40:26 +02:00
|
|
|
obj.push_back(Pair("startingheight", stats.nStartingHeight));
|
2012-06-29 23:24:53 +02:00
|
|
|
obj.push_back(Pair("banscore", stats.nMisbehavior));
|
|
|
|
|
|
|
|
ret.push_back(obj);
|
|
|
|
}
|
2012-09-18 21:07:58 +02:00
|
|
|
|
2012-06-29 23:24:53 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-06-29 05:18:38 +02:00
|
|
|
|
2013-01-23 17:45:00 +01:00
|
|
|
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 <node> <add|remove|onetry>\n"
|
|
|
|
"Attempts add or remove <node> from the addnode list or try a connection to <node> once.");
|
|
|
|
|
|
|
|
string strNode = params[0].get_str();
|
|
|
|
|
|
|
|
if (strCommand == "onetry")
|
|
|
|
{
|
|
|
|
CAddress addr;
|
|
|
|
ConnectNode(addr, strNode.c_str());
|
|
|
|
return Value::null;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK(cs_vAddedNodes);
|
|
|
|
vector<string>::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;
|
|
|
|
}
|
|
|
|
|