diff options
author | Jeff Garzik <jgarzik@exmulti.com> | 2012-06-29 17:24:53 -0400 |
---|---|---|
committer | Jeff Garzik <jgarzik@redhat.com> | 2012-06-29 17:24:53 -0400 |
commit | 1006f0707e34f8903f247195dabd86243ae61f05 (patch) | |
tree | cc1539a2e0d8fe72e3545c617d0bc1a9e34c0330 | |
parent | 70ab73a0087cbb0d6b26c9ad58146ae542b1b9be (diff) |
RPC: add 'getpeerinfo', returning easy-to-retrieve per-CNode data
-rw-r--r-- | src/bitcoinrpc.cpp | 2 | ||||
-rw-r--r-- | src/net.cpp | 18 | ||||
-rw-r--r-- | src/net.h | 19 | ||||
-rw-r--r-- | src/rpcnet.cpp | 46 |
4 files changed, 84 insertions, 1 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 3c61121dab..51690243bc 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -47,6 +47,7 @@ static int64 nWalletUnlockTime; static CCriticalSection cs_nWalletUnlockTime; extern Value getconnectioncount(const Array& params, bool fHelp); +extern Value getpeerinfo(const Array& params, bool fHelp); extern Value dumpprivkey(const Array& params, bool fHelp); extern Value importprivkey(const Array& params, bool fHelp); @@ -2300,6 +2301,7 @@ static const CRPCCommand vRPCCommands[] = { "stop", &stop, true }, { "getblockcount", &getblockcount, true }, { "getconnectioncount", &getconnectioncount, true }, + { "getpeerinfo", &getpeerinfo, true }, { "getdifficulty", &getdifficulty, true }, { "getgenerate", &getgenerate, true }, { "setgenerate", &setgenerate, true }, diff --git a/src/net.cpp b/src/net.cpp index f1073e0a3e..2a09d20dea 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -606,7 +606,23 @@ bool CNode::Misbehaving(int howmuch) return false; } - +#undef X +#define X(name) stats.name = name +void CNode::copyStats(CNodeStats &stats) +{ + X(nServices); + X(nLastSend); + X(nLastRecv); + X(nTimeConnected); + X(addrName); + X(nVersion); + X(strSubVer); + X(fInbound); + X(nReleaseTime); + X(nStartingHeight); + X(nMisbehavior); +} +#undef X @@ -128,6 +128,24 @@ extern std::map<CInv, int64> mapAlreadyAskedFor; +class CNodeStats +{ +public: + uint64 nServices; + int64 nLastSend; + int64 nLastRecv; + int64 nTimeConnected; + std::string addrName; + int nVersion; + std::string strSubVer; + bool fInbound; + int64 nReleaseTime; + int nStartingHeight; + int nMisbehavior; +}; + + + /** Information about a peer */ @@ -617,6 +635,7 @@ public: static void ClearBanned(); // needed for unit testing static bool IsBanned(CNetAddr ip); bool Misbehaving(int howmuch); // 1 == a little, 100 == a lot + void copyStats(CNodeStats &stats); }; diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index 1c27d0ef8c..c5746651c1 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -15,7 +15,53 @@ Value getconnectioncount(const Array& params, bool fHelp) "getconnectioncount\n" "Returns the number of connections to other nodes."); + LOCK(cs_vNodes); return (int)vNodes.size(); } +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)); + obj.push_back(Pair("height", stats.nStartingHeight)); + obj.push_back(Pair("banscore", stats.nMisbehavior)); + + ret.push_back(obj); + } + + return ret; +} |