Merge 26576 via brunoerg/2022-11-disconnectnode-subnet^

This commit is contained in:
Luke Dashjr 2024-03-25 17:26:53 +00:00
commit cf9ba030a1
2 changed files with 18 additions and 2 deletions

View File

@ -409,7 +409,7 @@ static RPCHelpMan disconnectnode()
"\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",
{
{"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)"},
},
RPCResult{RPCResult::Type::NONE, "", ""},
@ -430,7 +430,16 @@ static RPCHelpMan disconnectnode()
if (!address_arg.isNull() && id_arg.isNull()) {
/* handle disconnect-by-address */
success = connman.DisconnectNode(address_arg.get_str());
if (address_arg.get_str().find('/') != std::string::npos) {
CSubNet subnet;
if (LookupSubNet(address_arg.get_str(), subnet)) {
success = connman.DisconnectNode(subnet);
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid subnet");
}
} else {
success = connman.DisconnectNode(address_arg.get_str());
}
} else if (!id_arg.isNull() && (address_arg.isNull() || (address_arg.isStr() && address_arg.get_str().empty()))) {
/* handle disconnect-by-id */
NodeId nodeid = (NodeId) id_arg.getInt<int64_t>();

View File

@ -125,6 +125,9 @@ class DisconnectBanTest(BitcoinTestFramework):
self.log.info("disconnectnode: fail to disconnect when calling with junk address")
assert_raises_rpc_error(-29, "Node not found in connected nodes", self.nodes[0].disconnectnode, address="221B Baker Street")
self.log.info("disconnectnode: fail to disconnect when calling with invalid subnet")
assert_raises_rpc_error(-8, "Invalid subnet", self.nodes[0].disconnectnode, address="1.2.3.0/24\0")
self.log.info("disconnectnode: successfully disconnect node by address")
address1 = self.nodes[0].getpeerinfo()[0]['addr']
self.nodes[0].disconnectnode(address=address1)
@ -142,5 +145,9 @@ class DisconnectBanTest(BitcoinTestFramework):
self.wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 1, timeout=10)
assert not [node for node in self.nodes[0].getpeerinfo() if node['id'] == id1]
self.log.info("disconnectnode: successfully disconnect node by subnet")
self.nodes[0].disconnectnode(address='127.0.0.1/24')
self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0, timeout=10)
if __name__ == '__main__':
DisconnectBanTest().main()