Merge #11667: Add scripts to dumpwallet RPC
[bitcoinplatinum.git] / src / rpc / net.cpp
blob9de249855cc6a0f0226bb4ddcd37ceb4d6c56191
1 // Copyright (c) 2009-2016 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.
5 #include <rpc/server.h>
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 <rpc/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>
24 #include <univalue.h>
26 UniValue getconnectioncount(const JSONRPCRequest& request)
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", "")
39 if(!g_connman)
40 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
42 return (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
45 UniValue ping(const JSONRPCRequest& request)
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", "")
58 if(!g_connman)
59 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
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;
68 UniValue getpeerinfo(const JSONRPCRequest& request)
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/-connect or if it was an automatic/inbound connection\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", "")
121 if(!g_connman)
122 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
124 std::vector<CNodeStats> vstats;
125 g_connman->GetNodeStats(vstats);
127 UniValue ret(UniValue::VARR);
129 for (const CNodeStats& stats : vstats) {
130 UniValue obj(UniValue::VOBJ);
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 < static_cast<double>(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);
169 obj.push_back(Pair("inflight", heights));
171 obj.push_back(Pair("whitelisted", stats.fWhitelisted));
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));
178 obj.push_back(Pair("bytessent_per_msg", sendPerMsgCmd));
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));
185 obj.push_back(Pair("bytesrecv_per_msg", recvPerMsgCmd));
187 ret.push_back(obj);
190 return ret;
193 UniValue addnode(const JSONRPCRequest& request)
195 std::string strCommand;
196 if (!request.params[1].isNull())
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 "Nodes added using addnode (or -connect) are protected from DoS disconnection and are not required to be\n"
205 "full nodes/support SegWit as other outbound peers are (though such peers will not be synced from).\n"
206 "\nArguments:\n"
207 "1. \"node\" (string, required) The node (see getpeerinfo for nodes)\n"
208 "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"
209 "\nExamples:\n"
210 + HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\"")
211 + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
214 if(!g_connman)
215 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
217 std::string strNode = request.params[0].get_str();
219 if (strCommand == "onetry")
221 CAddress addr;
222 g_connman->OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), false, false, true);
223 return NullUniValue;
226 if (strCommand == "add")
228 if(!g_connman->AddNode(strNode))
229 throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
231 else if(strCommand == "remove")
233 if(!g_connman->RemoveAddedNode(strNode))
234 throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
237 return NullUniValue;
240 UniValue disconnectnode(const JSONRPCRequest& request)
242 if (request.fHelp || request.params.size() == 0 || request.params.size() >= 3)
243 throw std::runtime_error(
244 "disconnectnode \"[address]\" [nodeid]\n"
245 "\nImmediately disconnects from the specified peer node.\n"
246 "\nStrictly one out of 'address' and 'nodeid' can be provided to identify the node.\n"
247 "\nTo disconnect by nodeid, either set 'address' to the empty string, or call using the named 'nodeid' argument only.\n"
248 "\nArguments:\n"
249 "1. \"address\" (string, optional) The IP address/port of the node\n"
250 "2. \"nodeid\" (number, optional) The node ID (see getpeerinfo for node IDs)\n"
251 "\nExamples:\n"
252 + HelpExampleCli("disconnectnode", "\"192.168.0.6:8333\"")
253 + HelpExampleCli("disconnectnode", "\"\" 1")
254 + HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
255 + HelpExampleRpc("disconnectnode", "\"\", 1")
258 if(!g_connman)
259 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
261 bool success;
262 const UniValue &address_arg = request.params[0];
263 const UniValue &id_arg = request.params[1];
265 if (!address_arg.isNull() && id_arg.isNull()) {
266 /* handle disconnect-by-address */
267 success = g_connman->DisconnectNode(address_arg.get_str());
268 } else if (!id_arg.isNull() && (address_arg.isNull() || (address_arg.isStr() && address_arg.get_str().empty()))) {
269 /* handle disconnect-by-id */
270 NodeId nodeid = (NodeId) id_arg.get_int64();
271 success = g_connman->DisconnectNode(nodeid);
272 } else {
273 throw JSONRPCError(RPC_INVALID_PARAMS, "Only one of address and nodeid should be provided.");
276 if (!success) {
277 throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");
280 return NullUniValue;
283 UniValue getaddednodeinfo(const JSONRPCRequest& request)
285 if (request.fHelp || request.params.size() > 1)
286 throw std::runtime_error(
287 "getaddednodeinfo ( \"node\" )\n"
288 "\nReturns information about the given added node, or all added nodes\n"
289 "(note that onetry addnodes are not listed here)\n"
290 "\nArguments:\n"
291 "1. \"node\" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.\n"
292 "\nResult:\n"
293 "[\n"
294 " {\n"
295 " \"addednode\" : \"192.168.0.201\", (string) The node IP address or name (as provided to addnode)\n"
296 " \"connected\" : true|false, (boolean) If connected\n"
297 " \"addresses\" : [ (list of objects) Only when connected = true\n"
298 " {\n"
299 " \"address\" : \"192.168.0.201:8333\", (string) The bitcoin server IP and port we're connected to\n"
300 " \"connected\" : \"outbound\" (string) connection, inbound or outbound\n"
301 " }\n"
302 " ]\n"
303 " }\n"
304 " ,...\n"
305 "]\n"
306 "\nExamples:\n"
307 + HelpExampleCli("getaddednodeinfo", "\"192.168.0.201\"")
308 + HelpExampleRpc("getaddednodeinfo", "\"192.168.0.201\"")
311 if(!g_connman)
312 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
314 std::vector<AddedNodeInfo> vInfo = g_connman->GetAddedNodeInfo();
316 if (!request.params[0].isNull()) {
317 bool found = false;
318 for (const AddedNodeInfo& info : vInfo) {
319 if (info.strAddedNode == request.params[0].get_str()) {
320 vInfo.assign(1, info);
321 found = true;
322 break;
325 if (!found) {
326 throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
330 UniValue ret(UniValue::VARR);
332 for (const AddedNodeInfo& info : vInfo) {
333 UniValue obj(UniValue::VOBJ);
334 obj.push_back(Pair("addednode", info.strAddedNode));
335 obj.push_back(Pair("connected", info.fConnected));
336 UniValue addresses(UniValue::VARR);
337 if (info.fConnected) {
338 UniValue address(UniValue::VOBJ);
339 address.push_back(Pair("address", info.resolvedAddress.ToString()));
340 address.push_back(Pair("connected", info.fInbound ? "inbound" : "outbound"));
341 addresses.push_back(address);
343 obj.push_back(Pair("addresses", addresses));
344 ret.push_back(obj);
347 return ret;
350 UniValue getnettotals(const JSONRPCRequest& request)
352 if (request.fHelp || request.params.size() > 0)
353 throw std::runtime_error(
354 "getnettotals\n"
355 "\nReturns information about network traffic, including bytes in, bytes out,\n"
356 "and current time.\n"
357 "\nResult:\n"
358 "{\n"
359 " \"totalbytesrecv\": n, (numeric) Total bytes received\n"
360 " \"totalbytessent\": n, (numeric) Total bytes sent\n"
361 " \"timemillis\": t, (numeric) Current UNIX time in milliseconds\n"
362 " \"uploadtarget\":\n"
363 " {\n"
364 " \"timeframe\": n, (numeric) Length of the measuring timeframe in seconds\n"
365 " \"target\": n, (numeric) Target in bytes\n"
366 " \"target_reached\": true|false, (boolean) True if target is reached\n"
367 " \"serve_historical_blocks\": true|false, (boolean) True if serving historical blocks\n"
368 " \"bytes_left_in_cycle\": t, (numeric) Bytes left in current time cycle\n"
369 " \"time_left_in_cycle\": t (numeric) Seconds left in current time cycle\n"
370 " }\n"
371 "}\n"
372 "\nExamples:\n"
373 + HelpExampleCli("getnettotals", "")
374 + HelpExampleRpc("getnettotals", "")
376 if(!g_connman)
377 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
379 UniValue obj(UniValue::VOBJ);
380 obj.push_back(Pair("totalbytesrecv", g_connman->GetTotalBytesRecv()));
381 obj.push_back(Pair("totalbytessent", g_connman->GetTotalBytesSent()));
382 obj.push_back(Pair("timemillis", GetTimeMillis()));
384 UniValue outboundLimit(UniValue::VOBJ);
385 outboundLimit.push_back(Pair("timeframe", g_connman->GetMaxOutboundTimeframe()));
386 outboundLimit.push_back(Pair("target", g_connman->GetMaxOutboundTarget()));
387 outboundLimit.push_back(Pair("target_reached", g_connman->OutboundTargetReached(false)));
388 outboundLimit.push_back(Pair("serve_historical_blocks", !g_connman->OutboundTargetReached(true)));
389 outboundLimit.push_back(Pair("bytes_left_in_cycle", g_connman->GetOutboundTargetBytesLeft()));
390 outboundLimit.push_back(Pair("time_left_in_cycle", g_connman->GetMaxOutboundTimeLeftInCycle()));
391 obj.push_back(Pair("uploadtarget", outboundLimit));
392 return obj;
395 static UniValue GetNetworksInfo()
397 UniValue networks(UniValue::VARR);
398 for(int n=0; n<NET_MAX; ++n)
400 enum Network network = static_cast<enum Network>(n);
401 if(network == NET_UNROUTABLE || network == NET_INTERNAL)
402 continue;
403 proxyType proxy;
404 UniValue obj(UniValue::VOBJ);
405 GetProxy(network, proxy);
406 obj.push_back(Pair("name", GetNetworkName(network)));
407 obj.push_back(Pair("limited", IsLimited(network)));
408 obj.push_back(Pair("reachable", IsReachable(network)));
409 obj.push_back(Pair("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string()));
410 obj.push_back(Pair("proxy_randomize_credentials", proxy.randomize_credentials));
411 networks.push_back(obj);
413 return networks;
416 UniValue getnetworkinfo(const JSONRPCRequest& request)
418 if (request.fHelp || request.params.size() != 0)
419 throw std::runtime_error(
420 "getnetworkinfo\n"
421 "Returns an object containing various state info regarding P2P networking.\n"
422 "\nResult:\n"
423 "{\n"
424 " \"version\": xxxxx, (numeric) the server version\n"
425 " \"subversion\": \"/Satoshi:x.x.x/\", (string) the server subversion string\n"
426 " \"protocolversion\": xxxxx, (numeric) the protocol version\n"
427 " \"localservices\": \"xxxxxxxxxxxxxxxx\", (string) the services we offer to the network\n"
428 " \"localrelay\": true|false, (bool) true if transaction relay is requested from peers\n"
429 " \"timeoffset\": xxxxx, (numeric) the time offset\n"
430 " \"connections\": xxxxx, (numeric) the number of connections\n"
431 " \"networkactive\": true|false, (bool) whether p2p networking is enabled\n"
432 " \"networks\": [ (array) information per network\n"
433 " {\n"
434 " \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n"
435 " \"limited\": true|false, (boolean) is the network limited using -onlynet?\n"
436 " \"reachable\": true|false, (boolean) is the network reachable?\n"
437 " \"proxy\": \"host:port\" (string) the proxy that is used for this network, or empty if none\n"
438 " \"proxy_randomize_credentials\": true|false, (string) Whether randomized credentials are used\n"
439 " }\n"
440 " ,...\n"
441 " ],\n"
442 " \"relayfee\": x.xxxxxxxx, (numeric) minimum relay fee for transactions in " + CURRENCY_UNIT + "/kB\n"
443 " \"incrementalfee\": x.xxxxxxxx, (numeric) minimum fee increment for mempool limiting or BIP 125 replacement in " + CURRENCY_UNIT + "/kB\n"
444 " \"localaddresses\": [ (array) list of local addresses\n"
445 " {\n"
446 " \"address\": \"xxxx\", (string) network address\n"
447 " \"port\": xxx, (numeric) network port\n"
448 " \"score\": xxx (numeric) relative score\n"
449 " }\n"
450 " ,...\n"
451 " ]\n"
452 " \"warnings\": \"...\" (string) any network and blockchain warnings\n"
453 "}\n"
454 "\nExamples:\n"
455 + HelpExampleCli("getnetworkinfo", "")
456 + HelpExampleRpc("getnetworkinfo", "")
459 LOCK(cs_main);
460 UniValue obj(UniValue::VOBJ);
461 obj.push_back(Pair("version", CLIENT_VERSION));
462 obj.push_back(Pair("subversion", strSubVersion));
463 obj.push_back(Pair("protocolversion",PROTOCOL_VERSION));
464 if(g_connman)
465 obj.push_back(Pair("localservices", strprintf("%016x", g_connman->GetLocalServices())));
466 obj.push_back(Pair("localrelay", fRelayTxes));
467 obj.push_back(Pair("timeoffset", GetTimeOffset()));
468 if (g_connman) {
469 obj.push_back(Pair("networkactive", g_connman->GetNetworkActive()));
470 obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
472 obj.push_back(Pair("networks", GetNetworksInfo()));
473 obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
474 obj.push_back(Pair("incrementalfee", ValueFromAmount(::incrementalRelayFee.GetFeePerK())));
475 UniValue localAddresses(UniValue::VARR);
477 LOCK(cs_mapLocalHost);
478 for (const std::pair<CNetAddr, LocalServiceInfo> &item : mapLocalHost)
480 UniValue rec(UniValue::VOBJ);
481 rec.push_back(Pair("address", item.first.ToString()));
482 rec.push_back(Pair("port", item.second.nPort));
483 rec.push_back(Pair("score", item.second.nScore));
484 localAddresses.push_back(rec);
487 obj.push_back(Pair("localaddresses", localAddresses));
488 obj.push_back(Pair("warnings", GetWarnings("statusbar")));
489 return obj;
492 UniValue setban(const JSONRPCRequest& request)
494 std::string strCommand;
495 if (!request.params[1].isNull())
496 strCommand = request.params[1].get_str();
497 if (request.fHelp || request.params.size() < 2 ||
498 (strCommand != "add" && strCommand != "remove"))
499 throw std::runtime_error(
500 "setban \"subnet\" \"add|remove\" (bantime) (absolute)\n"
501 "\nAttempts to add or remove an IP/Subnet from the banned list.\n"
502 "\nArguments:\n"
503 "1. \"subnet\" (string, required) The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)\n"
504 "2. \"command\" (string, required) 'add' to add an IP/Subnet to the list, 'remove' to remove an IP/Subnet from the list\n"
505 "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"
506 "4. \"absolute\" (boolean, optional) If set, the bantime must be an absolute timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
507 "\nExamples:\n"
508 + HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400")
509 + HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
510 + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
512 if(!g_connman)
513 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
515 CSubNet subNet;
516 CNetAddr netAddr;
517 bool isSubnet = false;
519 if (request.params[0].get_str().find("/") != std::string::npos)
520 isSubnet = true;
522 if (!isSubnet) {
523 CNetAddr resolved;
524 LookupHost(request.params[0].get_str().c_str(), resolved, false);
525 netAddr = resolved;
527 else
528 LookupSubNet(request.params[0].get_str().c_str(), subNet);
530 if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
531 throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
533 if (strCommand == "add")
535 if (isSubnet ? g_connman->IsBanned(subNet) : g_connman->IsBanned(netAddr))
536 throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
538 int64_t banTime = 0; //use standard bantime if not specified
539 if (!request.params[2].isNull())
540 banTime = request.params[2].get_int64();
542 bool absolute = false;
543 if (request.params[3].isTrue())
544 absolute = true;
546 isSubnet ? g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute) : g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
548 else if(strCommand == "remove")
550 if (!( isSubnet ? g_connman->Unban(subNet) : g_connman->Unban(netAddr) ))
551 throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously banned.");
553 return NullUniValue;
556 UniValue listbanned(const JSONRPCRequest& request)
558 if (request.fHelp || request.params.size() != 0)
559 throw std::runtime_error(
560 "listbanned\n"
561 "\nList all banned IPs/Subnets.\n"
562 "\nExamples:\n"
563 + HelpExampleCli("listbanned", "")
564 + HelpExampleRpc("listbanned", "")
567 if(!g_connman)
568 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
570 banmap_t banMap;
571 g_connman->GetBanned(banMap);
573 UniValue bannedAddresses(UniValue::VARR);
574 for (const auto& entry : banMap)
576 const CBanEntry& banEntry = entry.second;
577 UniValue rec(UniValue::VOBJ);
578 rec.push_back(Pair("address", entry.first.ToString()));
579 rec.push_back(Pair("banned_until", banEntry.nBanUntil));
580 rec.push_back(Pair("ban_created", banEntry.nCreateTime));
581 rec.push_back(Pair("ban_reason", banEntry.banReasonToString()));
583 bannedAddresses.push_back(rec);
586 return bannedAddresses;
589 UniValue clearbanned(const JSONRPCRequest& request)
591 if (request.fHelp || request.params.size() != 0)
592 throw std::runtime_error(
593 "clearbanned\n"
594 "\nClear all banned IPs.\n"
595 "\nExamples:\n"
596 + HelpExampleCli("clearbanned", "")
597 + HelpExampleRpc("clearbanned", "")
599 if(!g_connman)
600 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
602 g_connman->ClearBanned();
604 return NullUniValue;
607 UniValue setnetworkactive(const JSONRPCRequest& request)
609 if (request.fHelp || request.params.size() != 1) {
610 throw std::runtime_error(
611 "setnetworkactive true|false\n"
612 "\nDisable/enable all p2p network activity.\n"
613 "\nArguments:\n"
614 "1. \"state\" (boolean, required) true to enable networking, false to disable\n"
618 if (!g_connman) {
619 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
622 g_connman->SetNetworkActive(request.params[0].get_bool());
624 return g_connman->GetNetworkActive();
627 static const CRPCCommand commands[] =
628 { // category name actor (function) argNames
629 // --------------------- ------------------------ ----------------------- ----------
630 { "network", "getconnectioncount", &getconnectioncount, {} },
631 { "network", "ping", &ping, {} },
632 { "network", "getpeerinfo", &getpeerinfo, {} },
633 { "network", "addnode", &addnode, {"node","command"} },
634 { "network", "disconnectnode", &disconnectnode, {"address", "nodeid"} },
635 { "network", "getaddednodeinfo", &getaddednodeinfo, {"node"} },
636 { "network", "getnettotals", &getnettotals, {} },
637 { "network", "getnetworkinfo", &getnetworkinfo, {} },
638 { "network", "setban", &setban, {"subnet", "command", "bantime", "absolute"} },
639 { "network", "listbanned", &listbanned, {} },
640 { "network", "clearbanned", &clearbanned, {} },
641 { "network", "setnetworkactive", &setnetworkactive, {"state"} },
644 void RegisterNetRPCCommands(CRPCTable &t)
646 for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
647 t.appendCommand(commands[vcidx].name, &commands[vcidx]);