Master Core  v0.0.9 - 49a5c0d97abf09ef2911ddfe8d9551df59f9efd3-dirty
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rpcnet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "rpcserver.h"
6 
7 #include "main.h"
8 #include "net.h"
9 #include "netbase.h"
10 #include "protocol.h"
11 #include "sync.h"
12 #include "util.h"
13 
14 #include <boost/foreach.hpp>
15 #include "json/json_spirit_value.h"
16 
17 using namespace json_spirit;
18 using namespace std;
19 
20 Value getconnectioncount(const Array& params, bool fHelp)
21 {
22  if (fHelp || params.size() != 0)
23  throw runtime_error(
24  "getconnectioncount\n"
25  "\nReturns the number of connections to other nodes.\n"
26  "\nbResult:\n"
27  "n (numeric) The connection count\n"
28  "\nExamples:\n"
29  + HelpExampleCli("getconnectioncount", "")
30  + HelpExampleRpc("getconnectioncount", "")
31  );
32 
33  LOCK(cs_vNodes);
34  return (int)vNodes.size();
35 }
36 
37 Value ping(const Array& params, bool fHelp)
38 {
39  if (fHelp || params.size() != 0)
40  throw runtime_error(
41  "ping\n"
42  "\nRequests that a ping be sent to all other nodes, to measure ping time.\n"
43  "Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds.\n"
44  "Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping.\n"
45  "\nExamples:\n"
46  + HelpExampleCli("ping", "")
47  + HelpExampleRpc("ping", "")
48  );
49 
50  // Request that each node send a ping during next message processing pass
51  LOCK(cs_vNodes);
52  BOOST_FOREACH(CNode* pNode, vNodes) {
53  pNode->fPingQueued = true;
54  }
55 
56  return Value::null;
57 }
58 
59 static void CopyNodeStats(std::vector<CNodeStats>& vstats)
60 {
61  vstats.clear();
62 
63  LOCK(cs_vNodes);
64  vstats.reserve(vNodes.size());
65  BOOST_FOREACH(CNode* pnode, vNodes) {
66  CNodeStats stats;
67  pnode->copyStats(stats);
68  vstats.push_back(stats);
69  }
70 }
71 
72 Value getpeerinfo(const Array& params, bool fHelp)
73 {
74  if (fHelp || params.size() != 0)
75  throw runtime_error(
76  "getpeerinfo\n"
77  "\nReturns data about each connected network node as a json array of objects.\n"
78  "\nbResult:\n"
79  "[\n"
80  " {\n"
81  " \"addr\":\"host:port\", (string) The ip address and port of the peer\n"
82  " \"addrlocal\":\"ip:port\", (string) local address\n"
83  " \"services\":\"00000001\", (string) The services\n"
84  " \"lastsend\": ttt, (numeric) The time in seconds since epoch (Jan 1 1970 GMT) of the last send\n"
85  " \"lastrecv\": ttt, (numeric) The time in seconds since epoch (Jan 1 1970 GMT) of the last receive\n"
86  " \"bytessent\": n, (numeric) The total bytes sent\n"
87  " \"bytesrecv\": n, (numeric) The total bytes received\n"
88  " \"conntime\": ttt, (numeric) The connection time in seconds since epoch (Jan 1 1970 GMT)\n"
89  " \"pingtime\": n, (numeric) ping time\n"
90  " \"pingwait\": n, (numeric) ping wait\n"
91  " \"version\": v, (numeric) The peer version, such as 7001\n"
92  " \"subver\": \"/Satoshi:0.8.5/\", (string) The string version\n"
93  " \"inbound\": true|false, (boolean) Inbound (true) or Outbound (false)\n"
94  " \"startingheight\": n, (numeric) The starting height (block) of the peer\n"
95  " \"banscore\": n, (numeric) The ban score (stats.nMisbehavior)\n"
96  " \"syncnode\" : true|false (booleamn) if sync node\n"
97  " }\n"
98  " ,...\n"
99  "}\n"
100 
101  "\nExamples:\n"
102  + HelpExampleCli("getpeerinfo", "")
103  + HelpExampleRpc("getpeerinfo", "")
104  );
105 
106  vector<CNodeStats> vstats;
107  CopyNodeStats(vstats);
108 
109  Array ret;
110 
111  BOOST_FOREACH(const CNodeStats& stats, vstats) {
112  Object obj;
113  CNodeStateStats statestats;
114  bool fStateStats = GetNodeStateStats(stats.nodeid, statestats);
115  obj.push_back(Pair("addr", stats.addrName));
116  if (!(stats.addrLocal.empty()))
117  obj.push_back(Pair("addrlocal", stats.addrLocal));
118  obj.push_back(Pair("services", strprintf("%08x", stats.nServices)));
119  obj.push_back(Pair("lastsend", stats.nLastSend));
120  obj.push_back(Pair("lastrecv", stats.nLastRecv));
121  obj.push_back(Pair("bytessent", stats.nSendBytes));
122  obj.push_back(Pair("bytesrecv", stats.nRecvBytes));
123  obj.push_back(Pair("conntime", stats.nTimeConnected));
124  obj.push_back(Pair("pingtime", stats.dPingTime));
125  if (stats.dPingWait > 0.0)
126  obj.push_back(Pair("pingwait", stats.dPingWait));
127  obj.push_back(Pair("version", stats.nVersion));
128  // Use the sanitized form of subver here, to avoid tricksy remote peers from
129  // corrupting or modifiying the JSON output by putting special characters in
130  // their ver message.
131  obj.push_back(Pair("subver", stats.cleanSubVer));
132  obj.push_back(Pair("inbound", stats.fInbound));
133  obj.push_back(Pair("startingheight", stats.nStartingHeight));
134  if (fStateStats) {
135  obj.push_back(Pair("banscore", statestats.nMisbehavior));
136  }
137  obj.push_back(Pair("syncnode", stats.fSyncNode));
138 
139  ret.push_back(obj);
140  }
141 
142  return ret;
143 }
144 
145 Value addnode(const Array& params, bool fHelp)
146 {
147  string strCommand;
148  if (params.size() == 2)
149  strCommand = params[1].get_str();
150  if (fHelp || params.size() != 2 ||
151  (strCommand != "onetry" && strCommand != "add" && strCommand != "remove"))
152  throw runtime_error(
153  "addnode \"node\" \"add|remove|onetry\"\n"
154  "\nAttempts add or remove a node from the addnode list.\n"
155  "Or try a connection to a node once.\n"
156  "\nArguments:\n"
157  "1. \"node\" (string, required) The node (see getpeerinfo for nodes)\n"
158  "2. \"command\" (string, required) 'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once\n"
159  "\nExamples:\n"
160  + HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\"")
161  + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
162  );
163 
164  string strNode = params[0].get_str();
165 
166  if (strCommand == "onetry")
167  {
168  CAddress addr;
169  ConnectNode(addr, strNode.c_str());
170  return Value::null;
171  }
172 
174  vector<string>::iterator it = vAddedNodes.begin();
175  for(; it != vAddedNodes.end(); it++)
176  if (strNode == *it)
177  break;
178 
179  if (strCommand == "add")
180  {
181  if (it != vAddedNodes.end())
182  throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
183  vAddedNodes.push_back(strNode);
184  }
185  else if(strCommand == "remove")
186  {
187  if (it == vAddedNodes.end())
188  throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
189  vAddedNodes.erase(it);
190  }
191 
192  return Value::null;
193 }
194 
195 Value getaddednodeinfo(const Array& params, bool fHelp)
196 {
197  if (fHelp || params.size() < 1 || params.size() > 2)
198  throw runtime_error(
199  "getaddednodeinfo dns ( \"node\" )\n"
200  "\nReturns information about the given added node, or all added nodes\n"
201  "(note that onetry addnodes are not listed here)\n"
202  "If dns is false, only a list of added nodes will be provided,\n"
203  "otherwise connected information will also be available.\n"
204  "\nArguments:\n"
205  "1. dns (boolean, required) If false, only a list of added nodes will be provided, otherwise connected information will also be available.\n"
206  "2. \"node\" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.\n"
207  "\nResult:\n"
208  "[\n"
209  " {\n"
210  " \"addednode\" : \"192.168.0.201\", (string) The node ip address\n"
211  " \"connected\" : true|false, (boolean) If connected\n"
212  " \"addresses\" : [\n"
213  " {\n"
214  " \"address\" : \"192.168.0.201:8333\", (string) The bitcoin server host and port\n"
215  " \"connected\" : \"outbound\" (string) connection, inbound or outbound\n"
216  " }\n"
217  " ,...\n"
218  " ]\n"
219  " }\n"
220  " ,...\n"
221  "]\n"
222  "\nExamples:\n"
223  + HelpExampleCli("getaddednodeinfo", "true")
224  + HelpExampleCli("getaddednodeinfo", "true \"192.168.0.201\"")
225  + HelpExampleRpc("getaddednodeinfo", "true, \"192.168.0.201\"")
226  );
227 
228  bool fDns = params[0].get_bool();
229 
230  list<string> laddedNodes(0);
231  if (params.size() == 1)
232  {
234  BOOST_FOREACH(string& strAddNode, vAddedNodes)
235  laddedNodes.push_back(strAddNode);
236  }
237  else
238  {
239  string strNode = params[1].get_str();
241  BOOST_FOREACH(string& strAddNode, vAddedNodes)
242  if (strAddNode == strNode)
243  {
244  laddedNodes.push_back(strAddNode);
245  break;
246  }
247  if (laddedNodes.size() == 0)
248  throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
249  }
250 
251  Array ret;
252  if (!fDns)
253  {
254  BOOST_FOREACH(string& strAddNode, laddedNodes)
255  {
256  Object obj;
257  obj.push_back(Pair("addednode", strAddNode));
258  ret.push_back(obj);
259  }
260  return ret;
261  }
262 
263  list<pair<string, vector<CService> > > laddedAddreses(0);
264  BOOST_FOREACH(string& strAddNode, laddedNodes)
265  {
266  vector<CService> vservNode(0);
267  if(Lookup(strAddNode.c_str(), vservNode, Params().GetDefaultPort(), fNameLookup, 0))
268  laddedAddreses.push_back(make_pair(strAddNode, vservNode));
269  else
270  {
271  Object obj;
272  obj.push_back(Pair("addednode", strAddNode));
273  obj.push_back(Pair("connected", false));
274  Array addresses;
275  obj.push_back(Pair("addresses", addresses));
276  }
277  }
278 
279  LOCK(cs_vNodes);
280  for (list<pair<string, vector<CService> > >::iterator it = laddedAddreses.begin(); it != laddedAddreses.end(); it++)
281  {
282  Object obj;
283  obj.push_back(Pair("addednode", it->first));
284 
285  Array addresses;
286  bool fConnected = false;
287  BOOST_FOREACH(CService& addrNode, it->second)
288  {
289  bool fFound = false;
290  Object node;
291  node.push_back(Pair("address", addrNode.ToString()));
292  BOOST_FOREACH(CNode* pnode, vNodes)
293  if (pnode->addr == addrNode)
294  {
295  fFound = true;
296  fConnected = true;
297  node.push_back(Pair("connected", pnode->fInbound ? "inbound" : "outbound"));
298  break;
299  }
300  if (!fFound)
301  node.push_back(Pair("connected", "false"));
302  addresses.push_back(node);
303  }
304  obj.push_back(Pair("connected", fConnected));
305  obj.push_back(Pair("addresses", addresses));
306  ret.push_back(obj);
307  }
308 
309  return ret;
310 }
311 
312 Value getnettotals(const Array& params, bool fHelp)
313 {
314  if (fHelp || params.size() > 0)
315  throw runtime_error(
316  "getnettotals\n"
317  "\nReturns information about network traffic, including bytes in, bytes out,\n"
318  "and current time.\n"
319  "\nResult:\n"
320  "{\n"
321  " \"totalbytesrecv\": n, (numeric) Total bytes received\n"
322  " \"totalbytessent\": n, (numeric) Total bytes sent\n"
323  " \"timemillis\": t (numeric) Total cpu time\n"
324  "}\n"
325  "\nExamples:\n"
326  + HelpExampleCli("getnettotals", "")
327  + HelpExampleRpc("getnettotals", "")
328  );
329 
330  Object obj;
331  obj.push_back(Pair("totalbytesrecv", CNode::GetTotalBytesRecv()));
332  obj.push_back(Pair("totalbytessent", CNode::GetTotalBytesSent()));
333  obj.push_back(Pair("timemillis", GetTimeMillis()));
334  return obj;
335 }
336 
337 Value getnetworkinfo(const Array& params, bool fHelp)
338 {
339  if (fHelp || params.size() != 0)
340  throw runtime_error(
341  "getnetworkinfo\n"
342  "Returns an object containing various state info regarding P2P networking.\n"
343  "\nResult:\n"
344  "{\n"
345  " \"version\": xxxxx, (numeric) the server version\n"
346  " \"protocolversion\": xxxxx, (numeric) the protocol version\n"
347  " \"timeoffset\": xxxxx, (numeric) the time offset\n"
348  " \"connections\": xxxxx, (numeric) the number of connections\n"
349  " \"proxy\": \"host:port\", (string, optional) the proxy used by the server\n"
350  " \"relayfee\": x.xxxx, (numeric) minimum relay fee for non-free transactions in btc/kb\n"
351  " \"localaddresses\": [, (array) list of local addresses\n"
352  " \"address\": \"xxxx\", (string) network address\n"
353  " \"port\": xxx, (numeric) network port\n"
354  " \"score\": xxx (numeric) relative score\n"
355  " ]\n"
356  "}\n"
357  "\nExamples:\n"
358  + HelpExampleCli("getnetworkinfo", "")
359  + HelpExampleRpc("getnetworkinfo", "")
360  );
361 
362  proxyType proxy;
363  GetProxy(NET_IPV4, proxy);
364 
365  Object obj;
366  obj.push_back(Pair("version", (int)CLIENT_VERSION));
367  obj.push_back(Pair("protocolversion",(int)PROTOCOL_VERSION));
368  obj.push_back(Pair("timeoffset", GetTimeOffset()));
369  obj.push_back(Pair("connections", (int)vNodes.size()));
370  obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string())));
371  obj.push_back(Pair("relayfee", ValueFromAmount(CTransaction::nMinRelayTxFee)));
372  Array localAddresses;
373  {
375  BOOST_FOREACH(const PAIRTYPE(CNetAddr, LocalServiceInfo) &item, mapLocalHost)
376  {
377  Object rec;
378  rec.push_back(Pair("address", item.first.ToString()));
379  rec.push_back(Pair("port", item.second.nPort));
380  rec.push_back(Pair("score", item.second.nScore));
381  localAddresses.push_back(rec);
382  }
383  }
384  obj.push_back(Pair("localaddresses", localAddresses));
385  return obj;
386 }
static uint64_t GetTotalBytesRecv()
Definition: net.cpp:1858
int nStartingHeight
Definition: net.h:141
Value addnode(const Array &params, bool fHelp)
Definition: rpcnet.cpp:145
int nMisbehavior
Definition: main.h:200
#define PAIRTYPE(t1, t2)
Definition: util.h:48
CAddress addr
Definition: net.h:216
std::string HelpExampleRpc(string methodname, string args)
Definition: rpcserver.cpp:923
static const int CLIENT_VERSION
Definition: version.h:15
std::string cleanSubVer
Definition: net.h:139
#define strprintf
Definition: util.h:116
STL namespace.
int64_t nTimeConnected
Definition: net.h:136
Object JSONRPCError(int code, const string &message)
Value getnetworkinfo(const Array &params, bool fHelp)
Definition: rpcnet.cpp:337
vector< std::string > vAddedNodes
Definition: net.cpp:76
bool fSyncNode
Definition: net.h:144
vector< CNode * > vNodes
Definition: net.cpp:63
int nVersion
Definition: net.h:138
bool fPingQueued
Definition: net.h:274
Value ValueFromAmount(int64_t amount)
Definition: rpcserver.cpp:94
bool fInbound
Definition: net.h:227
int GetDefaultPort() const
Definition: chainparams.h:58
#define LOCK(cs)
Definition: sync.h:156
CCriticalSection cs_mapLocalHost
Definition: net.cpp:52
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:94
Value getconnectioncount(const Array &params, bool fHelp)
Definition: rpcnet.cpp:20
bool fInbound
Definition: net.h:140
A CService with information about it as peer.
Definition: protocol.h:68
uint64_t nRecvBytes
Definition: net.h:143
double dPingTime
Definition: net.h:145
std::string addrName
Definition: net.h:137
std::string ToString() const
Definition: netbase.cpp:1111
int64_t GetTimeMillis()
Definition: util.h:311
uint64_t nSendBytes
Definition: net.h:142
static uint64_t GetTotalBytesSent()
Definition: net.cpp:1864
map< CNetAddr, LocalServiceInfo > mapLocalHost
Definition: net.cpp:53
CNode * ConnectNode(CAddress addrConnect, const char *pszDest)
Definition: net.cpp:440
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
Definition: main.cpp:327
Value getnettotals(const Array &params, bool fHelp)
Definition: rpcnet.cpp:312
static void CopyNodeStats(std::vector< CNodeStats > &vstats)
Definition: rpcnet.cpp:59
CCriticalSection cs_vAddedNodes
Definition: net.cpp:77
Value getpeerinfo(const Array &params, bool fHelp)
Definition: rpcnet.cpp:72
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netbase.h:40
int nScore
Definition: net.h:122
const CChainParams & Params()
Return the currently selected parameters.
static const int PROTOCOL_VERSION
Definition: version.h:29
std::string addrLocal
Definition: net.h:147
int64_t GetTimeOffset()
Definition: util.cpp:1230
bool Lookup(const char *pszName, std::vector< CService > &vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
Definition: netbase.cpp:133
uint64_t nServices
Definition: net.h:133
std::pair< CService, int > proxyType
Definition: netbase.h:136
Value getaddednodeinfo(const Array &params, bool fHelp)
Definition: rpcnet.cpp:195
static int64_t nMinRelayTxFee
Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) ...
Definition: core.h:187
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:430
std::string HelpExampleCli(string methodname, string args)
Definition: rpcserver.cpp:919
Information about a peer.
Definition: net.h:193
Value ping(const Array &params, bool fHelp)
Definition: rpcnet.cpp:37
void copyStats(CNodeStats &stats)
Definition: net.cpp:576
double dPingWait
Definition: net.h:146
CCriticalSection cs_vNodes
Definition: net.cpp:64
bool fNameLookup
Definition: netbase.cpp:31
int64_t nLastSend
Definition: net.h:134
NodeId nodeid
Definition: net.h:132
int64_t nLastRecv
Definition: net.h:135