MCPcopy Create free account
hub / github.com/ElementsProject/elements / setban

Function setban

src/rpc/net.cpp:668–750  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

666}
667
668static RPCHelpMan setban()
669{
670 return RPCHelpMan{"setban",
671 "\nAttempts to add or remove an IP/Subnet from the banned list.\n",
672 {
673 {"subnet", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)"},
674 {"command", RPCArg::Type::STR, RPCArg::Optional::NO, "'add' to add an IP/Subnet to the list, 'remove' to remove an IP/Subnet from the list"},
675 {"bantime", RPCArg::Type::NUM, RPCArg::Default{0}, "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)"},
676 {"absolute", RPCArg::Type::BOOL, RPCArg::Default{false}, "If set, the bantime must be an absolute timestamp expressed in " + UNIX_EPOCH_TIME},
677 },
678 RPCResult{RPCResult::Type::NONE, "", ""},
679 RPCExamples{
680 HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400")
681 + HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
682 + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
683 },
684 [&](const RPCHelpMan& help, const JSONRPCRequest& request) -> UniValue
685{
686 std::string strCommand;
687 if (!request.params[1].isNull())
688 strCommand = request.params[1].get_str();
689 if (strCommand != "add" && strCommand != "remove") {
690 throw std::runtime_error(help.ToString());
691 }
692 NodeContext& node = EnsureAnyNodeContext(request.context);
693 if (!node.banman) {
694 throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
695 }
696
697 CSubNet subNet;
698 CNetAddr netAddr;
699 bool isSubnet = false;
700
701 if (request.params[0].get_str().find('/') != std::string::npos)
702 isSubnet = true;
703
704 if (!isSubnet) {
705 CNetAddr resolved;
706 LookupHost(request.params[0].get_str(), resolved, false);
707 netAddr = resolved;
708 }
709 else
710 LookupSubNet(request.params[0].get_str(), subNet);
711
712 if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
713 throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
714
715 if (strCommand == "add")
716 {
717 if (isSubnet ? node.banman->IsBanned(subNet) : node.banman->IsBanned(netAddr)) {
718 throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
719 }
720
721 int64_t banTime = 0; //use standard bantime if not specified
722 if (!request.params[2].isNull())
723 banTime = request.params[2].get_int64();
724
725 bool absolute = false;

Callers

nothing calls this directly

Calls 15

HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
JSONRPCErrorFunction · 0.85
LookupHostFunction · 0.85
LookupSubNetFunction · 0.85
findMethod · 0.80
IsBannedMethod · 0.80
get_int64Method · 0.80
isTrueMethod · 0.80
BanMethod · 0.80
DisconnectNodeMethod · 0.80
UnbanMethod · 0.80

Tested by

no test coverage detected