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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (c) 2009-2012 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 "net.h"
  5. #include "bitcoinrpc.h"
  6. using namespace json_spirit;
  7. using namespace std;
  8. Value getconnectioncount(const Array& params, bool fHelp)
  9. {
  10. if (fHelp || params.size() != 0)
  11. throw runtime_error(
  12. "getconnectioncount\n"
  13. "Returns the number of connections to other nodes.");
  14. LOCK(cs_vNodes);
  15. return (int)vNodes.size();
  16. }
  17. static void CopyNodeStats(std::vector<CNodeStats>& vstats)
  18. {
  19. vstats.clear();
  20. LOCK(cs_vNodes);
  21. vstats.reserve(vNodes.size());
  22. BOOST_FOREACH(CNode* pnode, vNodes) {
  23. CNodeStats stats;
  24. pnode->copyStats(stats);
  25. vstats.push_back(stats);
  26. }
  27. }
  28. Value getpeerinfo(const Array& params, bool fHelp)
  29. {
  30. if (fHelp || params.size() != 0)
  31. throw runtime_error(
  32. "getpeerinfo\n"
  33. "Returns data about each connected network node.");
  34. vector<CNodeStats> vstats;
  35. CopyNodeStats(vstats);
  36. Array ret;
  37. BOOST_FOREACH(const CNodeStats& stats, vstats) {
  38. Object obj;
  39. obj.push_back(Pair("addr", stats.addrName));
  40. obj.push_back(Pair("services", strprintf("%08"PRI64x, stats.nServices)));
  41. obj.push_back(Pair("lastsend", (boost::int64_t)stats.nLastSend));
  42. obj.push_back(Pair("lastrecv", (boost::int64_t)stats.nLastRecv));
  43. obj.push_back(Pair("bytessent", (boost::int64_t)stats.nSendBytes));
  44. obj.push_back(Pair("bytesrecv", (boost::int64_t)stats.nRecvBytes));
  45. obj.push_back(Pair("conntime", (boost::int64_t)stats.nTimeConnected));
  46. obj.push_back(Pair("version", stats.nVersion));
  47. obj.push_back(Pair("subver", stats.strSubVer));
  48. obj.push_back(Pair("inbound", stats.fInbound));
  49. obj.push_back(Pair("startingheight", stats.nStartingHeight));
  50. obj.push_back(Pair("banscore", stats.nMisbehavior));
  51. if (stats.fSyncNode)
  52. obj.push_back(Pair("syncnode", true));
  53. ret.push_back(obj);
  54. }
  55. return ret;
  56. }
  57. Value addnode(const Array& params, bool fHelp)
  58. {
  59. string strCommand;
  60. if (params.size() == 2)
  61. strCommand = params[1].get_str();
  62. if (fHelp || params.size() != 2 ||
  63. (strCommand != "onetry" && strCommand != "add" && strCommand != "remove"))
  64. throw runtime_error(
  65. "addnode <node> <add|remove|onetry>\n"
  66. "Attempts add or remove <node> from the addnode list or try a connection to <node> once.");
  67. string strNode = params[0].get_str();
  68. if (strCommand == "onetry")
  69. {
  70. CAddress addr;
  71. ConnectNode(addr, strNode.c_str());
  72. return Value::null;
  73. }
  74. LOCK(cs_vAddedNodes);
  75. vector<string>::iterator it = vAddedNodes.begin();
  76. for(; it != vAddedNodes.end(); it++)
  77. if (strNode == *it)
  78. break;
  79. if (strCommand == "add")
  80. {
  81. if (it != vAddedNodes.end())
  82. throw JSONRPCError(-23, "Error: Node already added");
  83. vAddedNodes.push_back(strNode);
  84. }
  85. else if(strCommand == "remove")
  86. {
  87. if (it == vAddedNodes.end())
  88. throw JSONRPCError(-24, "Error: Node has not been added.");
  89. vAddedNodes.erase(it);
  90. }
  91. return Value::null;
  92. }
  93. Value getaddednodeinfo(const Array& params, bool fHelp)
  94. {
  95. if (fHelp || params.size() < 1 || params.size() > 2)
  96. throw runtime_error(
  97. "getaddednodeinfo <dns> [node]\n"
  98. "Returns information about the given added node, or all added nodes\n"
  99. "(note that onetry addnodes are not listed here)\n"
  100. "If dns is false, only a list of added nodes will be provided,\n"
  101. "otherwise connected information will also be available.");
  102. bool fDns = params[0].get_bool();
  103. list<string> laddedNodes(0);
  104. if (params.size() == 1)
  105. {
  106. LOCK(cs_vAddedNodes);
  107. BOOST_FOREACH(string& strAddNode, vAddedNodes)
  108. laddedNodes.push_back(strAddNode);
  109. }
  110. else
  111. {
  112. string strNode = params[1].get_str();
  113. LOCK(cs_vAddedNodes);
  114. BOOST_FOREACH(string& strAddNode, vAddedNodes)
  115. if (strAddNode == strNode)
  116. {
  117. laddedNodes.push_back(strAddNode);
  118. break;
  119. }
  120. if (laddedNodes.size() == 0)
  121. throw JSONRPCError(-24, "Error: Node has not been added.");
  122. }
  123. if (!fDns)
  124. {
  125. Object ret;
  126. BOOST_FOREACH(string& strAddNode, laddedNodes)
  127. ret.push_back(Pair("addednode", strAddNode));
  128. return ret;
  129. }
  130. Array ret;
  131. list<pair<string, vector<CService> > > laddedAddreses(0);
  132. BOOST_FOREACH(string& strAddNode, laddedNodes)
  133. {
  134. vector<CService> vservNode(0);
  135. if(Lookup(strAddNode.c_str(), vservNode, Params().GetDefaultPort(), fNameLookup, 0))
  136. laddedAddreses.push_back(make_pair(strAddNode, vservNode));
  137. else
  138. {
  139. Object obj;
  140. obj.push_back(Pair("addednode", strAddNode));
  141. obj.push_back(Pair("connected", false));
  142. Array addresses;
  143. obj.push_back(Pair("addresses", addresses));
  144. }
  145. }
  146. LOCK(cs_vNodes);
  147. for (list<pair<string, vector<CService> > >::iterator it = laddedAddreses.begin(); it != laddedAddreses.end(); it++)
  148. {
  149. Object obj;
  150. obj.push_back(Pair("addednode", it->first));
  151. Array addresses;
  152. bool fConnected = false;
  153. BOOST_FOREACH(CService& addrNode, it->second)
  154. {
  155. bool fFound = false;
  156. Object node;
  157. node.push_back(Pair("address", addrNode.ToString()));
  158. BOOST_FOREACH(CNode* pnode, vNodes)
  159. if (pnode->addr == addrNode)
  160. {
  161. fFound = true;
  162. fConnected = true;
  163. node.push_back(Pair("connected", pnode->fInbound ? "inbound" : "outbound"));
  164. break;
  165. }
  166. if (!fFound)
  167. node.push_back(Pair("connected", "false"));
  168. addresses.push_back(node);
  169. }
  170. obj.push_back(Pair("connected", fConnected));
  171. obj.push_back(Pair("addresses", addresses));
  172. ret.push_back(obj);
  173. }
  174. return ret;
  175. }