From 56748e1d746e3aad68eaa9f6a98b4823aa512fd4 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Tue, 22 Dec 2020 02:11:52 +0000 Subject: [PATCH] ban lists may now include subnets --- src/daemon/command_parser_executor.cpp | 14 ++++++++++---- src/p2p/net_node.inl | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 767ce7bc6..5a7560874 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -694,13 +694,19 @@ bool t_command_parser_executor::ban(const std::vector& args) std::ifstream ifs(ban_list_path.string()); for (std::string line; std::getline(ifs, line); ) { - const expect parsed_addr = net::get_network_address(line, 0); - if (!parsed_addr) + auto subnet = net::get_ipv4_subnet_address(line); + if (subnet) { - std::cout << "Invalid IP address: " << line << " - " << parsed_addr.error() << std::endl; + ret &= m_executor.ban(subnet->str(), seconds); continue; } - ret &= m_executor.ban(parsed_addr->host_str(), seconds); + const expect parsed_addr = net::get_network_address(line, 0); + if (parsed_addr) + { + ret &= m_executor.ban(parsed_addr->host_str(), seconds); + continue; + } + std::cout << "Invalid IP address or IPv4 subnet: " << line << std::endl; } return ret; } diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index ab098b7d4..f1638325c 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -481,13 +481,19 @@ namespace nodetool std::istringstream iss(banned_ips); for (std::string line; std::getline(iss, line); ) { - const expect parsed_addr = net::get_network_address(line, 0); - if (!parsed_addr) + auto subnet = net::get_ipv4_subnet_address(line); + if (subnet) { - MERROR("Invalid IP address: " << line << " - " << parsed_addr.error()); + block_subnet(*subnet, std::numeric_limits::max()); continue; } - block_host(*parsed_addr, std::numeric_limits::max()); + const expect parsed_addr = net::get_network_address(line, 0); + if (parsed_addr) + { + block_host(*parsed_addr, std::numeric_limits::max()); + continue; + } + MERROR("Invalid IP address or IPv4 subnet: " << line); } }