Skip to content

Commit

Permalink
Add a getaddednodeinfo RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Jan 27, 2013
1 parent 72a348f commit 67a11bd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bitcoinrpc.cpp
Expand Up @@ -202,6 +202,7 @@ static const CRPCCommand vRPCCommands[] =
{ "getconnectioncount", &getconnectioncount, true, false },
{ "getpeerinfo", &getpeerinfo, true, false },
{ "addnode", &addnode, true, true },
{ "getaddednodeinfo", &getaddednodeinfo, true, true },
{ "getdifficulty", &getdifficulty, true, false },
{ "getgenerate", &getgenerate, true, false },
{ "setgenerate", &setgenerate, true, false },
Expand Down Expand Up @@ -1180,6 +1181,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
// Special case non-string parameter types
//
if (strMethod == "stop" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "getaddednodeinfo" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "setgenerate" && n > 0) ConvertTo<bool>(params[0]);
if (strMethod == "setgenerate" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "sendtoaddress" && n > 1) ConvertTo<double>(params[1]);
Expand Down
1 change: 1 addition & 0 deletions src/bitcoinrpc.h
Expand Up @@ -133,6 +133,7 @@ extern void EnsureWalletIsUnlocked();
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 getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);

Expand Down
92 changes: 92 additions & 0 deletions src/rpcnet.cpp
Expand Up @@ -107,3 +107,95 @@ Value addnode(const Array& params, bool fHelp)
return Value::null;
}

Value getaddednodeinfo(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
"getaddednodeinfo <dns> [node]\n"
"Returns information about the given added node, or all added nodes\n"
"(note that onetry addnodes are not listed here)\n"
"If dns is false, only a list of added nodes will be provided,\n"
"otherwise connected information will also be available.");

bool fDns = params[0].get_bool();

list<string> laddedNodes(0);
if (params.size() == 1)
{
LOCK(cs_vAddedNodes);
BOOST_FOREACH(string& strAddNode, vAddedNodes)
laddedNodes.push_back(strAddNode);
}
else
{
string strNode = params[1].get_str();
LOCK(cs_vAddedNodes);
BOOST_FOREACH(string& strAddNode, vAddedNodes)
if (strAddNode == strNode)
{
laddedNodes.push_back(strAddNode);
break;
}
if (laddedNodes.size() == 0)
throw JSONRPCError(-24, "Error: Node has not been added.");
}

if (!fDns)
{
Object ret;
BOOST_FOREACH(string& strAddNode, laddedNodes)
ret.push_back(Pair("addednode", strAddNode));
return ret;
}

Array ret;

list<pair<string, vector<CService> > > laddedAddreses(0);
BOOST_FOREACH(string& strAddNode, laddedNodes)
{
vector<CService> vservNode(0);
if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fNameLookup, 0))
laddedAddreses.push_back(make_pair(strAddNode, vservNode));
else
{
Object obj;
obj.push_back(Pair("addednode", strAddNode));
obj.push_back(Pair("connected", false));
Array addresses;
obj.push_back(Pair("addresses", addresses));
}
}

LOCK(cs_vNodes);
for (list<pair<string, vector<CService> > >::iterator it = laddedAddreses.begin(); it != laddedAddreses.end(); it++)
{
Object obj;
obj.push_back(Pair("addednode", it->first));

Array addresses;
bool fConnected = false;
BOOST_FOREACH(CService& addrNode, it->second)
{
bool fFound = false;
Object node;
node.push_back(Pair("address", addrNode.ToString()));
BOOST_FOREACH(CNode* pnode, vNodes)
if (pnode->addr == addrNode)
{
fFound = true;
fConnected = true;
node.push_back(Pair("connected", pnode->fInbound ? "inbound" : "outbound"));
break;
}
if (!fFound)
node.push_back(Pair("connected", "false"));
addresses.push_back(node);
}
obj.push_back(Pair("connected", fConnected));
obj.push_back(Pair("addresses", addresses));
ret.push_back(obj);
}

return ret;
}

0 comments on commit 67a11bd

Please sign in to comment.