diff --git a/src/command_network.cc b/src/command_network.cc index 79e81193..e05c2933 100644 --- a/src/command_network.cc +++ b/src/command_network.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -33,6 +34,26 @@ #include #endif +torrent::Object +listen_port_range() { + auto port_range = torrent::runtime::client_config()->listen_port_range(); + + return std::to_string(port_range.first) + "-" + std::to_string(port_range.second); +} + +void +set_listen_port_range(const std::string& arg) { + unsigned int port_first{}, port_last{}; + + if (std::sscanf(arg.c_str(), "%i-%i", &port_first, &port_last) != 2) + throw torrent::input_error("Invalid port_range argument."); + + if (port_first >= (1 << 16) || port_last >= (1 << 16)) + throw torrent::input_error("Port range out-of-bounds."); + + torrent::runtime::client_config()->set_listen_port_range(port_first, port_last); +} + torrent::Object apply_encryption(const torrent::Object::list_type& args) { uint32_t options_mask = torrent::runtime::NetworkConfig::encryption_none; @@ -222,9 +243,16 @@ initialize_command_network() { auto nw_config = torrent::runtime::network_config(); // Isn't port_open used? - CMD_VAR_BOOL ("network.port_open", true); - CMD_VAR_BOOL ("network.port_random", true); - CMD_VAR_STRING ("network.port_range", "6881-6999"); + + // TODO: Remove port_open + // CMD_VAR_BOOL ("network.port_open", true); + // CMD_VAR_BOOL ("network.port_random", true); + // CMD_VAR_STRING ("network.port_range", "6881-6999"); + + CMD_ANY ("network.port_random", [](auto, auto) { return torrent::runtime::client_config()->listen_port_random(); }); + CMD_ANY_VALUE_V ("network.port_random.set", [](auto, auto& value) { return torrent::runtime::client_config()->set_listen_port_random(value); }); + CMD_ANY ("network.port_range", [](auto, auto) { return listen_port_range(); }); + CMD_ANY_STRING_V("network.port_range.set", [](auto, auto& value) { return set_listen_port_range(value); }); CMD_ANY ("network.listen.port", [](auto, auto) { return torrent::runtime::network_manager()->listen_port(); }); CMD_ANY_VALUE_V ("network.listen.port.set", [](auto, auto& value) { return torrent::runtime::network_manager()->set_listen_port(value); }); diff --git a/src/core/manager.cc b/src/core/manager.cc index 8325f8be..2868d315 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -33,6 +33,8 @@ #include "core/http_queue.h" #include "core/view.h" +#include + namespace core { const int Manager::create_start; @@ -164,30 +166,32 @@ void Manager::listen_open() { // This stuff really should be moved outside of manager, make it // part of the init script. - if (!rpc::call_command_value("network.port_open")) - return; + // if (!rpc::call_command_value("network.port_open")) + // return; - int portFirst, portLast; - torrent::Object portRange = rpc::call_command("network.port_range"); + // int portFirst, portLast; + // torrent::Object portRange = rpc::call_command("network.port_range"); - if (!portRange.is_string()) - throw torrent::input_error("Invalid port_range argument type."); + // if (!portRange.is_string()) + // throw torrent::input_error("Invalid port_range argument type."); - if (std::sscanf(portRange.as_string().c_str(), "%i-%i", &portFirst, &portLast) != 2) - throw torrent::input_error("Invalid port_range argument."); + // if (std::sscanf(portRange.as_string().c_str(), "%i-%i", &portFirst, &portLast) != 2) + // throw torrent::input_error("Invalid port_range argument."); - if (portFirst > portLast || portLast >= (1 << 16)) - throw torrent::input_error("Invalid port range."); + // if (portFirst > portLast || portLast >= (1 << 16)) + // throw torrent::input_error("Invalid port range."); - if (rpc::call_command_value("network.port_random")) { - int boundary = portFirst + random() % (portLast - portFirst + 1); + auto port_range = torrent::runtime::client_config()->port_range(); - if (torrent::runtime::network_manager()->listen_open(boundary, portLast) || - torrent::runtime::network_manager()->listen_open(portFirst, boundary)) + if (torrent::runtime::client_config()->port_random()) { + uint16_t boundary = port_range.first + (random() % (port_range.second - port_range.first + 1)); + + if (torrent::runtime::network_manager()->listen_open(boundary, port_range.second) || + torrent::runtime::network_manager()->listen_open(port_range.first, boundary)) return; } else { - if (torrent::runtime::network_manager()->listen_open(portFirst, portLast)) + if (torrent::runtime::network_manager()->listen_open(port_range.first, port_range.second)) return; }