Use the variable name _ for unused return values
[bitcoinplatinum.git] / src / rpc / net.cpp
blobf19b96824413f1441494f59fe0d485f5d41e26ac
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 "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 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", "")
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 < 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.fAddnode));
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 "\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:8333\" \"onetry\"")
209 + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
212 if(!g_connman)
213 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
215 std::string strNode = request.params[0].get_str();
217 if (strCommand == "onetry")
219 CAddress addr;
220 g_connman->OpenNetworkConnection(addr, false, nullptr, strNode.c_str());
221 return NullUniValue;
224 if (strCommand == "add")
226 if(!g_connman->AddNode(strNode))
227 throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
229 else if(strCommand == "remove")
231 if(!g_connman->RemoveAddedNode(strNode))
232 throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
235 return NullUniValue;
238 UniValue disconnectnode(const JSONRPCRequest& request)
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:8333\"")
251 + HelpExampleCli("disconnectnode", "\"\" 1")
252 + HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
253 + HelpExampleRpc("disconnectnode", "\"\", 1")
256 if(!g_connman)
257 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
259 bool success;
260 const UniValue &address_arg = request.params[0];
261 const UniValue &id_arg = request.params[1];
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.");
274 if (!success) {
275 throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");
278 return NullUniValue;
281 UniValue getaddednodeinfo(const JSONRPCRequest& request)
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:8333\", (string) The bitcoin 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\"")
309 if(!g_connman)
310 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
312 std::vector<AddedNodeInfo> vInfo = g_connman->GetAddedNodeInfo();
314 if (!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;
323 if (!found) {
324 throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
328 UniValue ret(UniValue::VARR);
330 for (const AddedNodeInfo& info : vInfo) {
331 UniValue obj(UniValue::VOBJ);
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) {
336 UniValue address(UniValue::VOBJ);
337 address.push_back(Pair("address", info.resolvedAddress.ToString()));
338 address.push_back(Pair("connected", info.fInbound ? "inbound" : "outbound"));
339 addresses.push_back(address);
341 obj.push_back(Pair("addresses", addresses));
342 ret.push_back(obj);
345 return ret;
348 UniValue getnettotals(const JSONRPCRequest& request)
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", "")
374 if(!g_connman)
375 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
377 UniValue obj(UniValue::VOBJ);
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()));
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;
393 static UniValue GetNetworksInfo()
395 UniValue networks(UniValue::VARR);
396 for(int n=0; n<NET_MAX; ++n)
398 enum Network network = static_cast<enum Network>(n);
399 if(network == NET_UNROUTABLE || network == NET_INTERNAL)
400 continue;
401 proxyType proxy;
402 UniValue obj(UniValue::VOBJ);
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);
411 return networks;
414 UniValue getnetworkinfo(const JSONRPCRequest& request)
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", "")
457 LOCK(cs_main);
458 UniValue obj(UniValue::VOBJ);
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)));
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);
475 LOCK(cs_mapLocalHost);
476 for (const std::pair<CNetAddr, LocalServiceInfo> &item : mapLocalHost)
478 UniValue rec(UniValue::VOBJ);
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);
485 obj.push_back(Pair("localaddresses", localAddresses));
486 obj.push_back(Pair("warnings", GetWarnings("statusbar")));
487 return obj;
490 UniValue setban(const JSONRPCRequest& request)
492 std::string strCommand;
493 if (!request.params[1].isNull())
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")
510 if(!g_connman)
511 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
513 CSubNet subNet;
514 CNetAddr netAddr;
515 bool isSubnet = false;
517 if (request.params[0].get_str().find("/") != std::string::npos)
518 isSubnet = true;
520 if (!isSubnet) {
521 CNetAddr resolved;
522 LookupHost(request.params[0].get_str().c_str(), resolved, false);
523 netAddr = resolved;
525 else
526 LookupSubNet(request.params[0].get_str().c_str(), subNet);
528 if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
529 throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
531 if (strCommand == "add")
533 if (isSubnet ? g_connman->IsBanned(subNet) : g_connman->IsBanned(netAddr))
534 throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
536 int64_t banTime = 0; //use standard bantime if not specified
537 if (!request.params[2].isNull())
538 banTime = request.params[2].get_int64();
540 bool absolute = false;
541 if (request.params[3].isTrue())
542 absolute = true;
544 isSubnet ? g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute) : g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
546 else if(strCommand == "remove")
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.");
551 return NullUniValue;
554 UniValue listbanned(const JSONRPCRequest& request)
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", "")
565 if(!g_connman)
566 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
568 banmap_t banMap;
569 g_connman->GetBanned(banMap);
571 UniValue bannedAddresses(UniValue::VARR);
572 for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
574 CBanEntry banEntry = (*it).second;
575 UniValue rec(UniValue::VOBJ);
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()));
581 bannedAddresses.push_back(rec);
584 return bannedAddresses;
587 UniValue clearbanned(const JSONRPCRequest& request)
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", "")
597 if(!g_connman)
598 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
600 g_connman->ClearBanned();
602 return NullUniValue;
605 UniValue setnetworkactive(const JSONRPCRequest& request)
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"
616 if (!g_connman) {
617 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
620 g_connman->SetNetworkActive(request.params[0].get_bool());
622 return g_connman->GetNetworkActive();
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"} },
642 void RegisterNetRPCCommands(CRPCTable &t)
644 for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
645 t.appendCommand(commands[vcidx].name, &commands[vcidx]);