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