You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcnet.cpp 15KB

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