Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oleks-rip committed Dec 7, 2024
1 parent ac10c8e commit f02f45a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/test/core/Config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ trustthesevalidators.gov
{
auto const contents = std::regex_replace(
detail::configContents("", ""),
std::regex("port\\s+=\\s+\\d+"),
std::regex("port\\s*=\\s*\\d+"),
"port = 0");

try
Expand Down
9 changes: 9 additions & 0 deletions src/test/jtx/impl/envconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ setupConfigForUnitTests(Config& cfg)
cfg.setupControl(true, true, true);
cfg["server"].append(PORT_PEER);
cfg[PORT_PEER].set("ip", getEnvLocalhostAddr());

// Using port 0 asks the operating system to allocate an unused port, which
// can be obtained after a "bind" call.
// Works for all system (Linux, Windows, Unix, Mac).
// Check https://man7.org/linux/man-pages/man7/ip.7.html
// "ip_local_port_range" section for more info
cfg[PORT_PEER].set("port", "0");
cfg[PORT_PEER].set("protocol", "peer");

Expand Down Expand Up @@ -126,6 +132,9 @@ addGrpcConfigWithSecureGateway(
std::string const& secureGateway)
{
(*cfg)[SECTION_PORT_GRPC].set("ip", getEnvLocalhostAddr());

// Check https://man7.org/linux/man-pages/man7/ip.7.html
// "ip_local_port_range" section for using 0 ports
(*cfg)[SECTION_PORT_GRPC].set("port", "0");
(*cfg)[SECTION_PORT_GRPC].set("secure_gateway", secureGateway);
return cfg;
Expand Down
4 changes: 2 additions & 2 deletions src/xrpld/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2210,8 +2210,8 @@ fixConfigPorts(Config& config, Endpoints const& endpoints)
auto const optPort = section.get("port");
if (optPort)
{
std::uint16_t port = 0;
port = beast::lexicalCast<std::uint16_t>(*optPort);
std::uint16_t const port =
beast::lexicalCast<std::uint16_t>(*optPort);
if (!port)
section.set("port", std::to_string(ep.port()));
}
Expand Down
2 changes: 2 additions & 0 deletions src/xrpld/app/main/GRPCServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,9 @@ GRPCServerImpl::start()
JLOG(journal_.info()) << "Starting gRPC server at " << serverAddress_;

grpc::ServerBuilder builder;

// Listen on the given address without any authentication mechanism.
// Actually binded port will be returned into "port" variable.
int port = 0;
builder.AddListeningPort(
serverAddress_, grpc::InsecureServerCredentials(), &port);
Expand Down
1 change: 1 addition & 0 deletions src/xrpld/app/main/GRPCServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class GRPCServerImpl final
std::vector<std::shared_ptr<Processor>>
setupListeners();

// Obtaining actually binded endpoint (if port 0 was used for server setup).
boost::asio::ip::tcp::endpoint
getEndpoint() const;

Expand Down
10 changes: 2 additions & 8 deletions src/xrpld/core/detail/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,8 @@ checkZeroPorts(Config const& config)
auto const optResult = section.get("port");
if (optResult)
{
try
{
if (auto const port =
beast::lexicalCast<std::uint16_t>(*optResult);
!port)
Throw<std::exception>();
}
catch (std::exception const&)
auto const port = beast::lexicalCast<std::uint16_t>(*optResult);
if (!port)
{
std::stringstream ss;
ss << "Invalid value '" << *optResult << "' for key 'port' in ["
Expand Down

0 comments on commit f02f45a

Please sign in to comment.