Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow port numbers be be specified using a either a colon or a space #4328

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 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,28 @@ 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 static std::regex e(":([0-9]+)$");
for (size_t i = 0; i < strVec.size(); ++i)
intelliot marked this conversation as resolved.
Show resolved Hide resolved
{
// skip anything that might be an ipv6 address
if (std::count(strVec[i].begin(), strVec[i].end(), ':') != 1)
continue;

std::string result = std::regex_replace(strVec[i], e, " $1");
// sanity check the result of the replace, should be same length
// as input
if (result.size() == strVec[i].size())
strVec[i] = result;
}
};

replaceColons(IPS_FIXED);
replaceColons(IPS);
}

{
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
if (std::count(ip.begin(), ip.end(), ':') == 1)
{
std::size_t colon = ip.find_last_of(":");
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:" + ip_str +
" port: " + Json::Value{iPort}.asString());
intelliot marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace ripple
79 changes: 79 additions & 0 deletions src/test/core/Config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,84 @@ 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
anotherserverwithport:12
1.1.1.1:1
1.1.1.1 1
12.34.12.123:12345
12.34.12.123 12345
::
2001:db8::
::1
::1:12345
[::1]:12345
2001:db8:3333:4444:5555:6666:7777:8888:12345
[2001:db8:3333:4444:5555:6666:7777:8888]:1


)");
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() == 15 &&
cfg.section(SECTION_IPS_FIXED).values().size() == 15);
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");
BEAST_EXPECT(cfg.IPS_FIXED[3] == "anotherserverwithport 12");
BEAST_EXPECT(cfg.IPS_FIXED[4] == "1.1.1.1 1");
BEAST_EXPECT(cfg.IPS_FIXED[5] == "1.1.1.1 1");
BEAST_EXPECT(cfg.IPS_FIXED[6] == "12.34.12.123 12345");
BEAST_EXPECT(cfg.IPS_FIXED[7] == "12.34.12.123 12345");

// all ipv6 should be ignored by colon replacer, howsoever formated
BEAST_EXPECT(cfg.IPS_FIXED[8] == "::");
BEAST_EXPECT(cfg.IPS_FIXED[9] == "2001:db8::");
BEAST_EXPECT(cfg.IPS_FIXED[10] == "::1");
BEAST_EXPECT(cfg.IPS_FIXED[11] == "::1:12345");
BEAST_EXPECT(cfg.IPS_FIXED[12] == "[::1]:12345");
BEAST_EXPECT(
cfg.IPS_FIXED[13] ==
"2001:db8:3333:4444:5555:6666:7777:8888:12345");
BEAST_EXPECT(
cfg.IPS_FIXED[14] == "[2001:db8:3333:4444:5555:6666:7777:8888]:1");
}

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