Skip to content

Commit

Permalink
Allow port numbers be be specified using a either a colon or a space …
Browse files Browse the repository at this point in the history
…(e.g 1.2.3.4:51235 and 1.2.3.4 51235)
  • Loading branch information
RichardAH committed Oct 24, 2022
1 parent e5275b8 commit bfb9bd6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/ripple/core/impl/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <regex>
#include <thread>

#if BOOST_OS_WINDOWS
Expand Down Expand Up @@ -469,6 +470,25 @@ Config::loadFromString(std::string const& fileContents)
if (auto s = getIniFileSection(secConfig, SECTION_SNTP))
SNTP_SERVERS = *s;

// if the user has specified ip:port then replace : with a space.
{
auto replaceColons = [](std::vector<std::string>& strVec) {
const std::regex e(":([0-9]+)$");

for (size_t i = 0; i < strVec.size(); ++i)
strVec[i] = std::regex_replace(strVec[i], e, " $1");
};

replaceColons(IPS_FIXED);
replaceColons(IPS);
}

for (auto const& s : IPS_FIXED)
std::cout << s << "\n";

for (auto const& s : IPS)
std::cout << s << "\n";

{
std::string dbPath;
if (getSingleSection(secConfig, "database_path", dbPath, j_))
Expand Down
22 changes: 18 additions & 4 deletions src/ripple/net/impl/RPCCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,31 @@ class RPCParser
return jvRequest;
}

// connect <ip> [port]
// connect <ip[:port]> [port]
Json::Value
parseConnect(Json::Value const& jvParams)
{
Json::Value jvRequest(Json::objectValue);

jvRequest[jss::ip] = jvParams[0u].asString();

std::string ip = jvParams[0u].asString();
if (jvParams.size() == 2)
{
jvRequest[jss::ip] = ip;
jvRequest[jss::port] = jvParams[1u].asUInt();
return jvRequest;
}

// handle case where there is one argument of the form ip:port
std::size_t colon = ip.find_last_of(":");
if (colon != std::string::npos)
{
jvRequest[jss::ip] = std::string{ip, 0, colon};
jvRequest[jss::port] =
Json::Value{std::string{ip, colon + 1}}.asUInt();
return jvRequest;
}

// default case, no port
jvRequest[jss::ip] = ip;
return jvRequest;
}

Expand Down
8 changes: 5 additions & 3 deletions src/ripple/rpc/handlers/Connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ doConnect(RPC::JsonContext& context)
else
iPort = DEFAULT_PEER_PORT;

auto ip =
beast::IP::Endpoint::from_string(context.params[jss::ip].asString());
auto const ip_str = context.params[jss::ip].asString();
auto ip = beast::IP::Endpoint::from_string(ip_str);

if (!is_unspecified(ip))
context.app.overlay().connect(ip.at_port(iPort));

return RPC::makeObjectValue("connecting");
return RPC::makeObjectValue(
"attempting connection to " + ip_str + ":" +
Json::Value{iPort}.asString());
}

} // namespace ripple
48 changes: 48 additions & 0 deletions src/test/core/Config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,53 @@ r.ripple.com 51235
cfg.section(SECTION_IPS_FIXED).values().size() == 2);
}

void
testColons()
{
Config cfg;
/* NOTE: this string includes some explicit
* space chars in order to verify proper trimming */
std::string toLoad(R"(
[port_rpc])"
"\x20"
R"(
# comment
# indented comment
)"
"\x20\x20"
R"(
[ips])"
"\x20"
R"(
r.ripple.com:51235
[ips_fixed])"
"\x20\x20"
R"(
# COMMENT
s1.ripple.com:51235
s2.ripple.com 51235
anotherserversansport
)");
cfg.loadFromString(toLoad);
BEAST_EXPECT(
cfg.exists("port_rpc") && cfg.section("port_rpc").lines().empty() &&
cfg.section("port_rpc").values().empty());
BEAST_EXPECT(
cfg.exists(SECTION_IPS) &&
cfg.section(SECTION_IPS).lines().size() == 1 &&
cfg.section(SECTION_IPS).values().size() == 1);
BEAST_EXPECT(
cfg.exists(SECTION_IPS_FIXED) &&
cfg.section(SECTION_IPS_FIXED).lines().size() == 3 &&
cfg.section(SECTION_IPS_FIXED).values().size() == 3);
BEAST_EXPECT(cfg.IPS[0] == "r.ripple.com 51235");
BEAST_EXPECT(cfg.IPS_FIXED[0] == "s1.ripple.com 51235");
BEAST_EXPECT(cfg.IPS_FIXED[1] == "s2.ripple.com 51235");
BEAST_EXPECT(cfg.IPS_FIXED[2] == "anotherserversansport");
}

void
testComments()
{
Expand Down Expand Up @@ -1147,6 +1194,7 @@ r.ripple.com 51235
testSetup(true);
testPort();
testWhitespace();
testColons();
testComments();
testGetters();
testAmendment();
Expand Down

0 comments on commit bfb9bd6

Please sign in to comment.