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>
10 #include <validation.h>
12 #include <net_processing.h>
14 #include <policy/policy.h>
15 #include <rpc/protocol.h>
18 #include <ui_interface.h>
20 #include <utilstrencodings.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"
33 "n (numeric) The connection count\n"
35 + HelpExampleCli("getconnectioncount", "")
36 + HelpExampleRpc("getconnectioncount", "")
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(
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"
54 + HelpExampleCli("ping", "")
55 + HelpExampleRpc("ping", "")
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;
68 UniValue
getpeerinfo(const JSONRPCRequest
& request
)
70 if (request
.fHelp
|| request
.params
.size() != 0)
71 throw std::runtime_error(
73 "\nReturns data about each connected network node as a json array of objects.\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"
101 " n, (numeric) The heights of blocks we're currently asking from this peer\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"
109 " \"bytesrecv_per_msg\": {\n"
110 " \"addr\": n, (numeric) The total bytes received aggregated by message type\n"
117 + HelpExampleCli("getpeerinfo", "")
118 + HelpExampleRpc("getpeerinfo", "")
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
));
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
) {
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
) {
183 recvPerMsgCmd
.push_back(Pair(i
.first
, i
.second
));
185 obj
.push_back(Pair("bytesrecv_per_msg", recvPerMsgCmd
));
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"
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"
210 + HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\"")
211 + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
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")
222 g_connman
->OpenNetworkConnection(addr
, false, nullptr, strNode
.c_str(), false, false, true);
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.");
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"
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"
252 + HelpExampleCli("disconnectnode", "\"192.168.0.6:8333\"")
253 + HelpExampleCli("disconnectnode", "\"\" 1")
254 + HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
255 + HelpExampleRpc("disconnectnode", "\"\", 1")
259 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
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
);
273 throw JSONRPCError(RPC_INVALID_PARAMS
, "Only one of address and nodeid should be provided.");
277 throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED
, "Node not found in connected nodes");
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"
291 "1. \"node\" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned.\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"
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"
307 + HelpExampleCli("getaddednodeinfo", "\"192.168.0.201\"")
308 + HelpExampleRpc("getaddednodeinfo", "\"192.168.0.201\"")
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()) {
318 for (const AddedNodeInfo
& info
: vInfo
) {
319 if (info
.strAddedNode
== request
.params
[0].get_str()) {
320 vInfo
.assign(1, info
);
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
));
350 UniValue
getnettotals(const JSONRPCRequest
& request
)
352 if (request
.fHelp
|| request
.params
.size() > 0)
353 throw std::runtime_error(
355 "\nReturns information about network traffic, including bytes in, bytes out,\n"
356 "and current time.\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"
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"
373 + HelpExampleCli("getnettotals", "")
374 + HelpExampleRpc("getnettotals", "")
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
));
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
)
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
);
416 UniValue
getnetworkinfo(const JSONRPCRequest
& request
)
418 if (request
.fHelp
|| request
.params
.size() != 0)
419 throw std::runtime_error(
421 "Returns an object containing various state info regarding P2P networking.\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"
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"
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"
446 " \"address\": \"xxxx\", (string) network address\n"
447 " \"port\": xxx, (numeric) network port\n"
448 " \"score\": xxx (numeric) relative score\n"
452 " \"warnings\": \"...\" (string) any network and blockchain warnings\n"
455 + HelpExampleCli("getnetworkinfo", "")
456 + HelpExampleRpc("getnetworkinfo", "")
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
));
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()));
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")));
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"
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"
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")
513 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
517 bool isSubnet
= false;
519 if (request
.params
[0].get_str().find("/") != std::string::npos
)
524 LookupHost(request
.params
[0].get_str().c_str(), resolved
, false);
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())
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.");
556 UniValue
listbanned(const JSONRPCRequest
& request
)
558 if (request
.fHelp
|| request
.params
.size() != 0)
559 throw std::runtime_error(
561 "\nList all banned IPs/Subnets.\n"
563 + HelpExampleCli("listbanned", "")
564 + HelpExampleRpc("listbanned", "")
568 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
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(
594 "\nClear all banned IPs.\n"
596 + HelpExampleCli("clearbanned", "")
597 + HelpExampleRpc("clearbanned", "")
600 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED
, "Error: Peer-to-peer functionality missing or disabled");
602 g_connman
->ClearBanned();
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"
614 "1. \"state\" (boolean, required) true to enable networking, false to disable\n"
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
]);