aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2012-06-29 17:34:02 -0700
committerGavin Andresen <gavinandresen@gmail.com>2012-06-29 17:34:02 -0700
commit3a906d45dc4ed9b6ac5f8422c41e07767b557cb6 (patch)
tree2428f40e938a18a376a355fe31722b6ed606ccf0 /src
parentac8d2b1df761f13b53a11333c6f7c4474f6e55e8 (diff)
parent1006f0707e34f8903f247195dabd86243ae61f05 (diff)
downloadbitcoin-3a906d45dc4ed9b6ac5f8422c41e07767b557cb6.tar.xz
Merge pull request #1531 from jgarzik/peerinfo
RPC: add 'getpeerinfo', returning easy-to-retrieve per-CNode data
Diffstat (limited to 'src')
-rw-r--r--src/bitcoinrpc.cpp14
-rw-r--r--src/makefile.linux-mingw1
-rw-r--r--src/makefile.mingw1
-rw-r--r--src/makefile.osx1
-rw-r--r--src/makefile.unix1
-rw-r--r--src/net.cpp18
-rw-r--r--src/net.h19
-rw-r--r--src/rpcnet.cpp67
8 files changed, 110 insertions, 12 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index de6db53982..51690243bc 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -46,6 +46,8 @@ static std::string strRPCUserColonPass;
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);
@@ -456,17 +458,6 @@ Value getblockcount(const Array& params, bool fHelp)
}
-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.");
-
- return (int)vNodes.size();
-}
-
-
Value getdifficulty(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
@@ -2310,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/makefile.linux-mingw b/src/makefile.linux-mingw
index cd8e97080c..5afb5c78a1 100644
--- a/src/makefile.linux-mingw
+++ b/src/makefile.linux-mingw
@@ -60,6 +60,7 @@ OBJS= \
obj/protocol.o \
obj/bitcoinrpc.o \
obj/rpcdump.o \
+ obj/rpcnet.o \
obj/script.o \
obj/sync.o \
obj/util.o \
diff --git a/src/makefile.mingw b/src/makefile.mingw
index 919be007b6..907a15a3f1 100644
--- a/src/makefile.mingw
+++ b/src/makefile.mingw
@@ -57,6 +57,7 @@ OBJS= \
obj/protocol.o \
obj/bitcoinrpc.o \
obj/rpcdump.o \
+ obj/rpcnet.o \
obj/script.o \
obj/sync.o \
obj/util.o \
diff --git a/src/makefile.osx b/src/makefile.osx
index 9728733122..cbb269cef1 100644
--- a/src/makefile.osx
+++ b/src/makefile.osx
@@ -84,6 +84,7 @@ OBJS= \
obj/protocol.o \
obj/bitcoinrpc.o \
obj/rpcdump.o \
+ obj/rpcnet.o \
obj/script.o \
obj/sync.o \
obj/util.o \
diff --git a/src/makefile.unix b/src/makefile.unix
index 9052891b4f..420c7ac3fa 100644
--- a/src/makefile.unix
+++ b/src/makefile.unix
@@ -104,6 +104,7 @@ OBJS= \
obj/protocol.o \
obj/bitcoinrpc.o \
obj/rpcdump.o \
+ obj/rpcnet.o \
obj/script.o \
obj/sync.o \
obj/util.o \
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
diff --git a/src/net.h b/src/net.h
index fa6f700867..21ecaef848 100644
--- a/src/net.h
+++ b/src/net.h
@@ -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
new file mode 100644
index 0000000000..c5746651c1
--- /dev/null
+++ b/src/rpcnet.cpp
@@ -0,0 +1,67 @@
+// 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.");
+
+ 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;
+}
+