Dash Core  0.12.2.1
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2017 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "rpc/server.h"
7 
8 #include "chainparams.h"
9 #include "clientversion.h"
10 #include "validation.h"
11 #include "net.h"
12 #include "net_processing.h"
13 #include "netbase.h"
14 #include "protocol.h"
15 #include "sync.h"
16 #include "timedata.h"
17 #include "ui_interface.h"
18 #include "util.h"
19 #include "utilstrencodings.h"
20 #include "version.h"
21 
22 #include <boost/foreach.hpp>
23 
24 #include <univalue.h>
25 
26 using namespace std;
27 
28 UniValue getconnectioncount(const UniValue& params, bool fHelp)
29 {
30  if (fHelp || params.size() != 0)
31  throw runtime_error(
32  "getconnectioncount\n"
33  "\nReturns the number of connections to other nodes.\n"
34  "\nResult:\n"
35  "n (numeric) The connection count\n"
36  "\nExamples:\n"
37  + HelpExampleCli("getconnectioncount", "")
38  + HelpExampleRpc("getconnectioncount", "")
39  );
40 
41  if(!g_connman)
42  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
43 
44  return (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
45 }
46 
47 UniValue ping(const UniValue& params, bool fHelp)
48 {
49  if (fHelp || params.size() != 0)
50  throw runtime_error(
51  "ping\n"
52  "\nRequests that a ping be sent to all other nodes, to measure ping time.\n"
53  "Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds.\n"
54  "Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping.\n"
55  "\nExamples:\n"
56  + HelpExampleCli("ping", "")
57  + HelpExampleRpc("ping", "")
58  );
59 
60  if(!g_connman)
61  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
62 
63  // Request that each node send a ping during next message processing pass
64  g_connman->ForEachNode([](CNode* pnode) {
65  pnode->fPingQueued = true;
66  });
67  return NullUniValue;
68 }
69 
70 UniValue getpeerinfo(const UniValue& params, bool fHelp)
71 {
72  if (fHelp || params.size() != 0)
73  throw runtime_error(
74  "getpeerinfo\n"
75  "\nReturns data about each connected network node as a json array of objects.\n"
76  "\nResult:\n"
77  "[\n"
78  " {\n"
79  " \"id\": n, (numeric) Peer index\n"
80  " \"addr\":\"host:port\", (string) The ip address and port of the peer\n"
81  " \"addrlocal\":\"ip:port\", (string) local address\n"
82  " \"services\":\"xxxxxxxxxxxxxxxx\", (string) The services offered\n"
83  " \"relaytxes\":true|false, (boolean) Whether peer has asked us to relay transactions to it\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  " \"timeoffset\": ttt, (numeric) The time offset in seconds\n"
90  " \"pingtime\": n, (numeric) ping time (if available)\n"
91  " \"minping\": n, (numeric) minimum observed ping time (if any at all)\n"
92  " \"pingwait\": n, (numeric) ping wait (if non-zero)\n"
93  " \"version\": v, (numeric) The peer version, such as 7001\n"
94  " \"subver\": \"/Dash Core:x.x.x/\", (string) The string version\n"
95  " \"inbound\": true|false, (boolean) Inbound (true) or Outbound (false)\n"
96  " \"startingheight\": n, (numeric) The starting height (block) of the peer\n"
97  " \"banscore\": n, (numeric) The ban score\n"
98  " \"synced_headers\": n, (numeric) The last header we have in common with this peer\n"
99  " \"synced_blocks\": n, (numeric) The last block we have in common with this peer\n"
100  " \"inflight\": [\n"
101  " n, (numeric) The heights of blocks we're currently asking from this peer\n"
102  " ...\n"
103  " ]\n"
104  " \"bytessent_per_msg\": {\n"
105  " \"addr\": n, (numeric) The total bytes sent aggregated by message type\n"
106  " ...\n"
107  " }\n"
108  " \"bytesrecv_per_msg\": {\n"
109  " \"addr\": n, (numeric) The total bytes received aggregated by message type\n"
110  " ...\n"
111  " }\n"
112  " }\n"
113  " ,...\n"
114  "]\n"
115  "\nExamples:\n"
116  + HelpExampleCli("getpeerinfo", "")
117  + HelpExampleRpc("getpeerinfo", "")
118  );
119 
120  if(!g_connman)
121  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
122 
123  vector<CNodeStats> vstats;
124  g_connman->GetNodeStats(vstats);
125 
127 
128  BOOST_FOREACH(const CNodeStats& stats, vstats) {
130  CNodeStateStats statestats;
131  bool fStateStats = GetNodeStateStats(stats.nodeid, statestats);
132  obj.push_back(Pair("id", stats.nodeid));
133  obj.push_back(Pair("addr", stats.addrName));
134  if (!(stats.addrLocal.empty()))
135  obj.push_back(Pair("addrlocal", stats.addrLocal));
136  obj.push_back(Pair("services", strprintf("%016x", stats.nServices)));
137  obj.push_back(Pair("relaytxes", stats.fRelayTxes));
138  obj.push_back(Pair("lastsend", stats.nLastSend));
139  obj.push_back(Pair("lastrecv", stats.nLastRecv));
140  obj.push_back(Pair("bytessent", stats.nSendBytes));
141  obj.push_back(Pair("bytesrecv", stats.nRecvBytes));
142  obj.push_back(Pair("conntime", stats.nTimeConnected));
143  obj.push_back(Pair("timeoffset", stats.nTimeOffset));
144  if (stats.dPingTime > 0.0)
145  obj.push_back(Pair("pingtime", stats.dPingTime));
146  if (stats.dMinPing < std::numeric_limits<int64_t>::max()/1e6)
147  obj.push_back(Pair("minping", stats.dMinPing));
148  if (stats.dPingWait > 0.0)
149  obj.push_back(Pair("pingwait", stats.dPingWait));
150  obj.push_back(Pair("version", stats.nVersion));
151  // Use the sanitized form of subver here, to avoid tricksy remote peers from
152  // corrupting or modifiying the JSON output by putting special characters in
153  // their ver message.
154  obj.push_back(Pair("subver", stats.cleanSubVer));
155  obj.push_back(Pair("inbound", stats.fInbound));
156  obj.push_back(Pair("startingheight", stats.nStartingHeight));
157  if (fStateStats) {
158  obj.push_back(Pair("banscore", statestats.nMisbehavior));
159  obj.push_back(Pair("synced_headers", statestats.nSyncHeight));
160  obj.push_back(Pair("synced_blocks", statestats.nCommonHeight));
161  UniValue heights(UniValue::VARR);
162  BOOST_FOREACH(int height, statestats.vHeightInFlight) {
163  heights.push_back(height);
164  }
165  obj.push_back(Pair("inflight", heights));
166  }
167  obj.push_back(Pair("whitelisted", stats.fWhitelisted));
168 
169  UniValue sendPerMsgCmd(UniValue::VOBJ);
170  BOOST_FOREACH(const mapMsgCmdSize::value_type &i, stats.mapSendBytesPerMsgCmd) {
171  if (i.second > 0)
172  sendPerMsgCmd.push_back(Pair(i.first, i.second));
173  }
174  obj.push_back(Pair("bytessent_per_msg", sendPerMsgCmd));
175 
176  UniValue recvPerMsgCmd(UniValue::VOBJ);
177  BOOST_FOREACH(const mapMsgCmdSize::value_type &i, stats.mapRecvBytesPerMsgCmd) {
178  if (i.second > 0)
179  recvPerMsgCmd.push_back(Pair(i.first, i.second));
180  }
181  obj.push_back(Pair("bytesrecv_per_msg", recvPerMsgCmd));
182 
183  ret.push_back(obj);
184  }
185 
186  return ret;
187 }
188 
189 UniValue addnode(const UniValue& params, bool fHelp)
190 {
191  string strCommand;
192  if (params.size() == 2)
193  strCommand = params[1].get_str();
194  if (fHelp || params.size() != 2 ||
195  (strCommand != "onetry" && strCommand != "add" && strCommand != "remove"))
196  throw runtime_error(
197  "addnode \"node\" \"add|remove|onetry\"\n"
198  "\nAttempts add or remove a node from the addnode list.\n"
199  "Or try a connection to a node once.\n"
200  "\nArguments:\n"
201  "1. \"node\" (string, required) The node (see getpeerinfo for nodes)\n"
202  "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"
203  "\nExamples:\n"
204  + HelpExampleCli("addnode", "\"192.168.0.6:9999\" \"onetry\"")
205  + HelpExampleRpc("addnode", "\"192.168.0.6:9999\", \"onetry\"")
206  );
207 
208  if(!g_connman)
209  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
210 
211  string strNode = params[0].get_str();
212 
213  if (strCommand == "onetry")
214  {
215  CAddress addr;
216  g_connman->OpenNetworkConnection(addr, NULL, strNode.c_str());
217  return NullUniValue;
218  }
219 
220  if (strCommand == "add")
221  {
222  if(!g_connman->AddNode(strNode))
223  throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
224  }
225  else if(strCommand == "remove")
226  {
227  if(!g_connman->RemoveAddedNode(strNode))
228  throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
229  }
230 
231  return NullUniValue;
232 }
233 
234 UniValue disconnectnode(const UniValue& params, bool fHelp)
235 {
236  if (fHelp || params.size() != 1)
237  throw runtime_error(
238  "disconnectnode \"node\" \n"
239  "\nImmediately disconnects from the specified node.\n"
240  "\nArguments:\n"
241  "1. \"node\" (string, required) The node (see getpeerinfo for nodes)\n"
242  "\nExamples:\n"
243  + HelpExampleCli("disconnectnode", "\"192.168.0.6:8333\"")
244  + HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
245  );
246 
247  if(!g_connman)
248  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
249 
250  bool ret = g_connman->DisconnectNode(params[0].get_str());
251  if (!ret)
252  throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");
253 
254  return NullUniValue;
255 }
256 
257 UniValue getaddednodeinfo(const UniValue& params, bool fHelp)
258 {
259  if (fHelp || params.size() < 1 || params.size() > 2)
260  throw runtime_error(
261  "getaddednodeinfo dummy ( \"node\" )\n"
262  "\nReturns information about the given added node, or all added nodes\n"
263  "(note that onetry addnodes are not listed here)\n"
264  "\nArguments:\n"
265  "1. dummy (boolean, required) Kept for historical purposes but ignored\n"
266  "2. \"node\" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.\n"
267  "\nResult:\n"
268  "[\n"
269  " {\n"
270  " \"addednode\" : \"192.168.0.201\", (string) The node ip address or name (as provided to addnode)\n"
271  " \"connected\" : true|false, (boolean) If connected\n"
272  " \"addresses\" : [ (list of objects) Only when connected = true\n"
273  " {\n"
274  " \"address\" : \"192.168.0.201:9999\", (string) The dash server IP and port we're connected to\n"
275  " \"connected\" : \"outbound\" (string) connection, inbound or outbound\n"
276  " }\n"
277  " ]\n"
278  " }\n"
279  " ,...\n"
280  "]\n"
281  "\nExamples:\n"
282  + HelpExampleCli("getaddednodeinfo", "true")
283  + HelpExampleCli("getaddednodeinfo", "true \"192.168.0.201\"")
284  + HelpExampleRpc("getaddednodeinfo", "true, \"192.168.0.201\"")
285  );
286 
287  if(!g_connman)
288  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
289 
290  std::vector<AddedNodeInfo> vInfo = g_connman->GetAddedNodeInfo();
291 
292  if (params.size() == 2) {
293  bool found = false;
294  for (const AddedNodeInfo& info : vInfo) {
295  if (info.strAddedNode == params[1].get_str()) {
296  vInfo.assign(1, info);
297  found = true;
298  break;
299  }
300  }
301  if (!found) {
302  throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
303  }
304  }
305 
307 
308  for (const AddedNodeInfo& info : vInfo) {
310  obj.push_back(Pair("addednode", info.strAddedNode));
311  obj.push_back(Pair("connected", info.fConnected));
312  UniValue addresses(UniValue::VARR);
313  if (info.fConnected) {
314  UniValue address(UniValue::VOBJ);
315  address.push_back(Pair("address", info.resolvedAddress.ToString()));
316  address.push_back(Pair("connected", info.fInbound ? "inbound" : "outbound"));
317  addresses.push_back(address);
318  }
319  obj.push_back(Pair("addresses", addresses));
320  ret.push_back(obj);
321  }
322 
323  return ret;
324 }
325 
326 UniValue getnettotals(const UniValue& params, bool fHelp)
327 {
328  if (fHelp || params.size() > 0)
329  throw runtime_error(
330  "getnettotals\n"
331  "\nReturns information about network traffic, including bytes in, bytes out,\n"
332  "and current time.\n"
333  "\nResult:\n"
334  "{\n"
335  " \"totalbytesrecv\": n, (numeric) Total bytes received\n"
336  " \"totalbytessent\": n, (numeric) Total bytes sent\n"
337  " \"timemillis\": t, (numeric) Total cpu time\n"
338  " \"uploadtarget\":\n"
339  " {\n"
340  " \"timeframe\": n, (numeric) Length of the measuring timeframe in seconds\n"
341  " \"target\": n, (numeric) Target in bytes\n"
342  " \"target_reached\": true|false, (boolean) True if target is reached\n"
343  " \"serve_historical_blocks\": true|false, (boolean) True if serving historical blocks\n"
344  " \"bytes_left_in_cycle\": t, (numeric) Bytes left in current time cycle\n"
345  " \"time_left_in_cycle\": t (numeric) Seconds left in current time cycle\n"
346  " }\n"
347  "}\n"
348  "\nExamples:\n"
349  + HelpExampleCli("getnettotals", "")
350  + HelpExampleRpc("getnettotals", "")
351  );
352  if(!g_connman)
353  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
354 
356  obj.push_back(Pair("totalbytesrecv", g_connman->GetTotalBytesRecv()));
357  obj.push_back(Pair("totalbytessent", g_connman->GetTotalBytesSent()));
358  obj.push_back(Pair("timemillis", GetTimeMillis()));
359 
360  UniValue outboundLimit(UniValue::VOBJ);
361  outboundLimit.push_back(Pair("timeframe", g_connman->GetMaxOutboundTimeframe()));
362  outboundLimit.push_back(Pair("target", g_connman->GetMaxOutboundTarget()));
363  outboundLimit.push_back(Pair("target_reached", g_connman->OutboundTargetReached(false)));
364  outboundLimit.push_back(Pair("serve_historical_blocks", !g_connman->OutboundTargetReached(true)));
365  outboundLimit.push_back(Pair("bytes_left_in_cycle", g_connman->GetOutboundTargetBytesLeft()));
366  outboundLimit.push_back(Pair("time_left_in_cycle", g_connman->GetMaxOutboundTimeLeftInCycle()));
367  obj.push_back(Pair("uploadtarget", outboundLimit));
368  return obj;
369 }
370 
372 {
373  UniValue networks(UniValue::VARR);
374  for(int n=0; n<NET_MAX; ++n)
375  {
376  enum Network network = static_cast<enum Network>(n);
377  if(network == NET_UNROUTABLE)
378  continue;
379  proxyType proxy;
381  GetProxy(network, proxy);
382  obj.push_back(Pair("name", GetNetworkName(network)));
383  obj.push_back(Pair("limited", IsLimited(network)));
384  obj.push_back(Pair("reachable", IsReachable(network)));
385  obj.push_back(Pair("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()));
386  obj.push_back(Pair("proxy_randomize_credentials", proxy.randomize_credentials));
387  networks.push_back(obj);
388  }
389  return networks;
390 }
391 
392 UniValue getnetworkinfo(const UniValue& params, bool fHelp)
393 {
394  if (fHelp || params.size() != 0)
395  throw runtime_error(
396  "getnetworkinfo\n"
397  "Returns an object containing various state info regarding P2P networking.\n"
398  "\nResult:\n"
399  "{\n"
400  " \"version\": xxxxx, (numeric) the server version\n"
401  " \"subversion\": \"/Dash Core:x.x.x/\", (string) the server subversion string\n"
402  " \"protocolversion\": xxxxx, (numeric) the protocol version\n"
403  " \"localservices\": \"xxxxxxxxxxxxxxxx\", (string) the services we offer to the network\n"
404  " \"localrelay\": true|false, (bool) true if transaction relay is requested from peers\n"
405  " \"timeoffset\": xxxxx, (numeric) the time offset\n"
406  " \"connections\": xxxxx, (numeric) the number of connections\n"
407  " \"networkactive\": true|false, (bool) whether p2p networking is enabled\n"
408  " \"networks\": [ (array) information per network\n"
409  " {\n"
410  " \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n"
411  " \"limited\": true|false, (boolean) is the network limited using -onlynet?\n"
412  " \"reachable\": true|false, (boolean) is the network reachable?\n"
413  " \"proxy\": \"host:port\" (string) the proxy that is used for this network, or empty if none\n"
414  " }\n"
415  " ,...\n"
416  " ],\n"
417  " \"relayfee\": x.xxxxxxxx, (numeric) minimum relay fee for non-free transactions in " + CURRENCY_UNIT + "/kB\n"
418  " \"localaddresses\": [ (array) list of local addresses\n"
419  " {\n"
420  " \"address\": \"xxxx\", (string) network address\n"
421  " \"port\": xxx, (numeric) network port\n"
422  " \"score\": xxx (numeric) relative score\n"
423  " }\n"
424  " ,...\n"
425  " ]\n"
426  " \"warnings\": \"...\" (string) any network warnings (such as alert messages) \n"
427  "}\n"
428  "\nExamples:\n"
429  + HelpExampleCli("getnetworkinfo", "")
430  + HelpExampleRpc("getnetworkinfo", "")
431  );
432 
433  LOCK(cs_main);
435  obj.push_back(Pair("version", CLIENT_VERSION));
436  obj.push_back(Pair("subversion", strSubVersion));
437  obj.push_back(Pair("protocolversion",PROTOCOL_VERSION));
438  if(g_connman)
439  obj.push_back(Pair("localservices", strprintf("%016x", g_connman->GetLocalServices())));
440  obj.push_back(Pair("localrelay", fRelayTxes));
441  obj.push_back(Pair("timeoffset", GetTimeOffset()));
442  if (g_connman) {
443  obj.push_back(Pair("networkactive", g_connman->GetNetworkActive()));
444  obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
445  }
446  obj.push_back(Pair("networks", GetNetworksInfo()));
447  obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
448  UniValue localAddresses(UniValue::VARR);
449  {
451  BOOST_FOREACH(const PAIRTYPE(CNetAddr, LocalServiceInfo) &item, mapLocalHost)
452  {
454  rec.push_back(Pair("address", item.first.ToString()));
455  rec.push_back(Pair("port", item.second.nPort));
456  rec.push_back(Pair("score", item.second.nScore));
457  localAddresses.push_back(rec);
458  }
459  }
460  obj.push_back(Pair("localaddresses", localAddresses));
461  obj.push_back(Pair("warnings", GetWarnings("statusbar")));
462  return obj;
463 }
464 
465 UniValue setban(const UniValue& params, bool fHelp)
466 {
467  string strCommand;
468  if (params.size() >= 2)
469  strCommand = params[1].get_str();
470  if (fHelp || params.size() < 2 ||
471  (strCommand != "add" && strCommand != "remove"))
472  throw runtime_error(
473  "setban \"ip(/netmask)\" \"add|remove\" (bantime) (absolute)\n"
474  "\nAttempts add or remove a IP/Subnet from the banned list.\n"
475  "\nArguments:\n"
476  "1. \"ip(/netmask)\" (string, required) The IP/Subnet (see getpeerinfo for nodes ip) with a optional netmask (default is /32 = single ip)\n"
477  "2. \"command\" (string, required) 'add' to add a IP/Subnet to the list, 'remove' to remove a IP/Subnet from the list\n"
478  "3. \"bantime\" (numeric, optional) time in seconds how long (or until when if [absolute] is set) the ip is banned (0 or empty means using the default time of 24h which can also be overwritten by the -bantime startup argument)\n"
479  "4. \"absolute\" (boolean, optional) If set, the bantime must be a absolute timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
480  "\nExamples:\n"
481  + HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400")
482  + HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
483  + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\" 86400")
484  );
485  if(!g_connman)
486  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
487 
488  CSubNet subNet;
489  CNetAddr netAddr;
490  bool isSubnet = false;
491 
492  if (params[0].get_str().find("/") != string::npos)
493  isSubnet = true;
494 
495  if (!isSubnet) {
496  CNetAddr resolved;
497  LookupHost(params[0].get_str().c_str(), resolved, false);
498  netAddr = resolved;
499  }
500  else
501  LookupSubNet(params[0].get_str().c_str(), subNet);
502 
503  if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
504  throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Invalid IP/Subnet");
505 
506  if (strCommand == "add")
507  {
508  if (isSubnet ? g_connman->IsBanned(subNet) : g_connman->IsBanned(netAddr))
509  throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
510 
511  int64_t banTime = 0; //use standard bantime if not specified
512  if (params.size() >= 3 && !params[2].isNull())
513  banTime = params[2].get_int64();
514 
515  bool absolute = false;
516  if (params.size() == 4 && params[3].isTrue())
517  absolute = true;
518 
519  isSubnet ? g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute) : g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
520  }
521  else if(strCommand == "remove")
522  {
523  if (!( isSubnet ? g_connman->Unban(subNet) : g_connman->Unban(netAddr) ))
524  throw JSONRPCError(RPC_MISC_ERROR, "Error: Unban failed");
525  }
526  return NullUniValue;
527 }
528 
529 UniValue listbanned(const UniValue& params, bool fHelp)
530 {
531  if (fHelp || params.size() != 0)
532  throw runtime_error(
533  "listbanned\n"
534  "\nList all banned IPs/Subnets.\n"
535  "\nExamples:\n"
536  + HelpExampleCli("listbanned", "")
537  + HelpExampleRpc("listbanned", "")
538  );
539 
540  if(!g_connman)
541  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
542 
543  banmap_t banMap;
544  g_connman->GetBanned(banMap);
545 
546  UniValue bannedAddresses(UniValue::VARR);
547  for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
548  {
549  CBanEntry banEntry = (*it).second;
551  rec.push_back(Pair("address", (*it).first.ToString()));
552  rec.push_back(Pair("banned_until", banEntry.nBanUntil));
553  rec.push_back(Pair("ban_created", banEntry.nCreateTime));
554  rec.push_back(Pair("ban_reason", banEntry.banReasonToString()));
555 
556  bannedAddresses.push_back(rec);
557  }
558 
559  return bannedAddresses;
560 }
561 
562 UniValue clearbanned(const UniValue& params, bool fHelp)
563 {
564  if (fHelp || params.size() != 0)
565  throw runtime_error(
566  "clearbanned\n"
567  "\nClear all banned IPs.\n"
568  "\nExamples:\n"
569  + HelpExampleCli("clearbanned", "")
570  + HelpExampleRpc("clearbanned", "")
571  );
572  if(!g_connman)
573  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
574 
575  g_connman->ClearBanned();
576 
577  return NullUniValue;
578 }
579 
580 UniValue setnetworkactive(const UniValue& params, bool fHelp)
581 {
582  if (fHelp || params.size() != 1) {
583  throw runtime_error(
584  "setnetworkactive true|false\n"
585  "Disable/enable all p2p network activity."
586  );
587  }
588 
589  if (!g_connman) {
590  throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
591  }
592 
593  g_connman->SetNetworkActive(params[0].get_bool());
594 
595  return g_connman->GetNetworkActive();
596 }
double dPingTime
Definition: net.h:611
Node is already added.
Definition: protocol.h:63
double dPingWait
Definition: net.h:612
bool randomize_credentials
Definition: netbase.h:37
std::string addrName
Definition: net.h:601
bool IsValid() const
Definition: netbase.h:34
#define strprintf
Definition: tinyformat.h:1011
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
std::string addrLocal
Definition: net.h:614
const std::string CURRENCY_UNIT
Definition: amount.cpp:10
CCriticalSection cs_main
Definition: validation.cpp:62
static UniValue GetNetworksInfo()
Definition: net.cpp:371
bool IsValid() const
Definition: netaddress.cpp:186
UniValue clearbanned(const UniValue &params, bool fHelp)
Definition: net.cpp:562
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: server.cpp:586
int64_t GetTimeOffset()
Definition: timedata.cpp:27
std::vector< int > vHeightInFlight
bool push_back(const UniValue &val)
Definition: univalue.cpp:176
UniValue ValueFromAmount(const CAmount &amount)
Definition: server.cpp:122
UniValue listbanned(const UniValue &params, bool fHelp)
Definition: net.cpp:529
CService proxy
Definition: netbase.h:36
mapMsgCmdSize mapRecvBytesPerMsgCmd
Definition: net.h:609
std::string cleanSubVer
Definition: net.h:603
bool fPingQueued
Definition: net.h:767
UniValue getaddednodeinfo(const UniValue &params, bool fHelp)
Definition: net.cpp:257
UniValue disconnectnode(const UniValue &params, bool fHelp)
Definition: net.cpp:234
std::map< CSubNet, CBanEntry > banmap_t
Definition: addrdb.h:78
UniValue setban(const UniValue &params, bool fHelp)
Definition: net.cpp:465
bool fRelayTxes
Definition: net.cpp:78
std::string ToStringIPPort(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:559
Definition: net.h:661
CFeeRate minRelayTxFee
Definition: validation.cpp:94
int64_t nTimeOffset
Definition: net.h:600
#define LOCK(cs)
Definition: sync.h:168
int nStartingHeight
Definition: net.h:605
CCriticalSection cs_mapLocalHost
Definition: net.cpp:79
bool fWhitelisted
Definition: net.h:610
CAmount GetFeePerK() const
Definition: amount.h:47
bool isNull() const
Definition: univalue.h:77
std::map< CNetAddr, LocalServiceInfo > mapLocalHost
Definition: net.cpp:80
UniValue getconnectioncount(const UniValue &params, bool fHelp)
Definition: net.cpp:28
bool fInbound
Definition: net.h:604
uint64_t nSendBytes
Definition: net.h:606
Network
Definition: netaddress.h:19
bool fRelayTxes
Definition: net.h:596
General application defined errors.
Definition: protocol.h:41
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: server.cpp:581
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:166
std::string strSubVersion
Definition: net.cpp:83
UniValue getnettotals(const UniValue &params, bool fHelp)
Definition: net.cpp:326
bool LookupSubNet(const char *pszName, CSubNet &ret)
Definition: netbase.cpp:640
bool IsValid() const
Definition: netaddress.cpp:698
bool IsReachable(enum Network net)
Definition: net.cpp:285
int64_t nLastRecv
Definition: net.h:598
double dMinPing
Definition: net.h:613
int64_t nTimeConnected
Definition: net.h:599
std::string GetWarnings(const std::string &strFor)
int nScore
Definition: net.h:583
mapMsgCmdSize mapSendBytesPerMsgCmd
Definition: net.h:607
ServiceFlags nServices
Definition: net.h:595
std::string banReasonToString()
Definition: addrdb.h:65
static const int PROTOCOL_VERSION
Definition: version.h:13
int64_t GetTimeMillis()
Definition: utiltime.cpp:34
int nVersion
Definition: net.h:602
UniValue setnetworkactive(const UniValue &params, bool fHelp)
Definition: net.cpp:580
Node has not been added before.
Definition: protocol.h:64
UniValue ping(const UniValue &params, bool fHelp)
Definition: net.cpp:47
Still downloading initial blocks.
Definition: protocol.h:62
int64_t nLastSend
Definition: net.h:597
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:103
const UniValue NullUniValue
Definition: univalue.cpp:78
bool isTrue() const
Definition: univalue.h:78
UniValue getpeerinfo(const UniValue &params, bool fHelp)
Definition: net.cpp:70
NodeId nodeid
Definition: net.h:594
UniValue getnetworkinfo(const UniValue &params, bool fHelp)
Definition: net.cpp:392
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:536
Invalid IP/Subnet.
Definition: protocol.h:66
int64_t nCreateTime
Definition: addrdb.h:31
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:51
bool LookupHost(const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup)
Definition: netbase.cpp:177
UniValue addnode(const UniValue &params, bool fHelp)
Definition: net.cpp:189
static const int CLIENT_VERSION
Definition: clientversion.h:54
uint64_t nRecvBytes
Definition: net.h:608
#define PAIRTYPE(t1, t2)
UniValue JSONRPCError(int code, const string &message)
Definition: protocol.cpp:57
size_t size() const
Definition: univalue.h:69
bool IsLimited(enum Network net)
Definition: net.cpp:253
std::string get_str() const
Definition: univalue.cpp:310
int64_t nBanUntil
Definition: addrdb.h:32