Skip to content

Commit

Permalink
Check tcp port range
Browse files Browse the repository at this point in the history
  • Loading branch information
bamboo committed Dec 1, 2024
1 parent e2c7f37 commit ff9470b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/scheme_repl_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "scheme.hpp"
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <limits>

using namespace godot;
using gd = UtilityFunctions;
Expand Down Expand Up @@ -49,14 +50,21 @@ void SchemeReplServer::server_loop(int tcp_port, const String &tcp_bind_address)
tcp_server->stop();
}

std::optional<std::pair<uint16_t, String>> parse_repl_args() {
using tcp_port_t = uint16_t;

std::optional<std::pair<tcp_port_t, String>> parse_repl_args() {
// TODO: accept --s7-tcp-address=<address>
String tcp_bind_address = "127.0.0.1";
String tcp_port_option = "--s7-tcp-port";
for (const auto &arg : OS::get_singleton()->get_cmdline_args()) {
if (arg.begins_with("--s7-tcp-port")) {
if (arg.begins_with(tcp_port_option)) {
auto parts = arg.split("=");
auto tcp_port = parts.size() > 1 ? parts[1].to_int() : 0;
return std::make_pair(tcp_port, tcp_bind_address);
auto max_tcp_port = std::numeric_limits<tcp_port_t>::max();
if (tcp_port < 0 || tcp_port >= max_tcp_port) {
gd::printerr(tcp_port_option, " argument must be a value between 0 and ", max_tcp_port, ".");
}
return std::make_pair(static_cast<tcp_port_t>(tcp_port), std::move(tcp_bind_address));
}
}
return std::nullopt;
Expand Down

0 comments on commit ff9470b

Please sign in to comment.