diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index c93e268cb2..389733cc42 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -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(); diff --git a/test/functional/p2p_disconnect_ban.py b/test/functional/p2p_disconnect_ban.py index c389ff732f..aa80d2a9ab 100755 --- a/test/functional/p2p_disconnect_ban.py +++ b/test/functional/p2p_disconnect_ban.py @@ -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()