rpc, p2p: allow disconnectnode with subnet

Github-Pull: #26576
Rebased-From: b2e3edc61c
This commit is contained in:
brunoerg 2022-11-24 10:52:11 -03:00 committed by Luke Dashjr
parent 55bd5d8015
commit 6d2bc57f0e

View File

@ -433,7 +433,7 @@ static RPCHelpMan disconnectnode()
"\nStrictly one out of 'address' and 'nodeid' can be provided to identify the node.\n" "\nStrictly one out of 'address' and 'nodeid' can be provided to identify the node.\n"
"\nTo disconnect by nodeid, either set 'address' to the empty string, or call using the named 'nodeid' argument only.\n", "\nTo disconnect by nodeid, either set 'address' to the empty string, or call using the named 'nodeid' argument only.\n",
{ {
{"address", RPCArg::Type::STR, RPCArg::DefaultHint{"fallback to nodeid"}, "The IP address/port of the node"}, {"address", RPCArg::Type::STR, RPCArg::DefaultHint{"fallback to nodeid"}, "The IP address/port of the node or subnet"},
{"nodeid", RPCArg::Type::NUM, RPCArg::DefaultHint{"fallback to address"}, "The node ID (see getpeerinfo for node IDs)"}, {"nodeid", RPCArg::Type::NUM, RPCArg::DefaultHint{"fallback to address"}, "The node ID (see getpeerinfo for node IDs)"},
}, },
RPCResult{RPCResult::Type::NONE, "", ""}, RPCResult{RPCResult::Type::NONE, "", ""},
@ -454,7 +454,16 @@ static RPCHelpMan disconnectnode()
if (!address_arg.isNull() && id_arg.isNull()) { if (!address_arg.isNull() && id_arg.isNull()) {
/* handle disconnect-by-address */ /* handle disconnect-by-address */
if (address_arg.get_str().find('/') != std::string::npos) {
const CSubNet subnet = LookupSubNet(address_arg.get_str());
if (subnet.IsValid()) {
success = connman.DisconnectNode(subnet);
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid subnet");
}
} else {
success = connman.DisconnectNode(address_arg.get_str()); success = connman.DisconnectNode(address_arg.get_str());
}
} else if (!id_arg.isNull() && (address_arg.isNull() || (address_arg.isStr() && address_arg.get_str().empty()))) { } else if (!id_arg.isNull() && (address_arg.isNull() || (address_arg.isStr() && address_arg.get_str().empty()))) {
/* handle disconnect-by-id */ /* handle disconnect-by-id */
NodeId nodeid = (NodeId) id_arg.getInt<int64_t>(); NodeId nodeid = (NodeId) id_arg.getInt<int64_t>();