Replace from_string_checked pair return type with optional<Endpoint>

This commit is contained in:
seelabs
2019-08-14 13:39:49 -07:00
parent 0a256247a0
commit 1eb3753f26
8 changed files with 48 additions and 49 deletions

View File

@@ -608,16 +608,16 @@ int run (int argc, char** argv)
// happen after the config file is loaded. // happen after the config file is loaded.
if (vm.count ("rpc_ip")) if (vm.count ("rpc_ip"))
{ {
auto [endpoint, valid] = beast::IP::Endpoint::from_string_checked( auto endpoint = beast::IP::Endpoint::from_string_checked(
vm["rpc_ip"].as<std::string>()); vm["rpc_ip"].as<std::string>());
if (! valid) if (! endpoint)
{ {
std::cerr << "Invalid rpc_ip = " << std::cerr << "Invalid rpc_ip = " <<
vm["rpc_ip"].as<std::string>() << "\n"; vm["rpc_ip"].as<std::string>() << "\n";
return -1; return -1;
} }
if (endpoint.port() == 0) if (endpoint->port() == 0)
{ {
std::cerr << "No port specified in rpc_ip.\n"; std::cerr << "No port specified in rpc_ip.\n";
if (vm.count ("rpc_port")) if (vm.count ("rpc_port"))
@@ -625,9 +625,9 @@ int run (int argc, char** argv)
std::cerr << "WARNING: using deprecated rpc_port param.\n"; std::cerr << "WARNING: using deprecated rpc_port param.\n";
try try
{ {
endpoint = endpoint.at_port( endpoint = endpoint->at_port(
vm["rpc_port"].as<std::uint16_t>()); vm["rpc_port"].as<std::uint16_t>());
if (endpoint.port() == 0) if (endpoint->port() == 0)
throw std::domain_error("0"); throw std::domain_error("0");
} }
catch(std::exception const& e) catch(std::exception const& e)
@@ -640,7 +640,7 @@ int run (int argc, char** argv)
return -1; return -1;
} }
config->rpc_ip = std::move(endpoint); config->rpc_ip = std::move(*endpoint);
} }
if (vm.count ("quorum")) if (vm.count ("quorum"))

View File

@@ -274,13 +274,11 @@ public:
// first attempt to parse as an endpoint (IP addr + port). // first attempt to parse as an endpoint (IP addr + port).
// If that doesn't succeed, fall back to generic name + port parsing // If that doesn't succeed, fall back to generic name + port parsing
if (auto const [result, validResult] = if (auto const result = beast::IP::Endpoint::from_string_checked(str))
beast::IP::Endpoint::from_string_checked(str);
validResult)
{ {
return make_pair ( return make_pair (
result.address().to_string(), result->address().to_string(),
std::to_string(result.port())); std::to_string(result->port()));
} }
// generic name/port parsing, which doesn't work for // generic name/port parsing, which doesn't work for

View File

@@ -126,9 +126,9 @@ bool parseUrl (parsedURL& pUrl, std::string const& strUrl)
// We need to use Endpoint to parse the domain to // We need to use Endpoint to parse the domain to
// strip surrounding brackets from IPv6 addresses, // strip surrounding brackets from IPv6 addresses,
// e.g. [::1] => ::1. // e.g. [::1] => ::1.
const auto [result, validResult] = beast::IP::Endpoint::from_string_checked (domain); const auto result = beast::IP::Endpoint::from_string_checked (domain);
pUrl.domain = validResult pUrl.domain = result
? result.address().to_string() ? result->address().to_string()
: domain; : domain;
const std::string port = smMatch[5]; const std::string port = smMatch[5];
if (!port.empty()) if (!port.empty())

View File

@@ -23,6 +23,9 @@
#include <ripple/beast/net/IPAddress.h> #include <ripple/beast/net/IPAddress.h>
#include <ripple/beast/hash/hash_append.h> #include <ripple/beast/hash/hash_append.h>
#include <ripple/beast/hash/uhash.h> #include <ripple/beast/hash/uhash.h>
#include <boost/optional.hpp>
#include <cstdint> #include <cstdint>
#include <ios> #include <ios>
#include <string> #include <string>
@@ -44,9 +47,9 @@ public:
/** Create an Endpoint from a string. /** Create an Endpoint from a string.
If the port is omitted, the endpoint will have a zero port. If the port is omitted, the endpoint will have a zero port.
@return A pair with the endpoint, and bool set to `true` on success. @return An optional endpoint; will be `boost::none` on failure
*/ */
static std::pair <Endpoint, bool> from_string_checked (std::string const& s); static boost::optional<Endpoint> from_string_checked (std::string const& s);
static Endpoint from_string (std::string const& s); static Endpoint from_string (std::string const& s);
/** Returns a string representing the endpoint. */ /** Returns a string representing the endpoint. */

View File

@@ -34,23 +34,21 @@ Endpoint::Endpoint (Address const& addr, Port port)
{ {
} }
std::pair <Endpoint, bool> Endpoint::from_string_checked (std::string const& s) boost::optional<Endpoint> Endpoint::from_string_checked (std::string const& s)
{ {
std::stringstream is (boost::trim_copy(s)); std::stringstream is (boost::trim_copy(s));
Endpoint endpoint; Endpoint endpoint;
is >> endpoint; is >> endpoint;
if (! is.fail() && is.rdbuf()->in_avail() == 0) if (! is.fail() && is.rdbuf()->in_avail() == 0)
return std::make_pair (endpoint, true); return endpoint;
return std::make_pair (Endpoint {}, false); return {};
} }
Endpoint Endpoint::from_string (std::string const& s) Endpoint Endpoint::from_string (std::string const& s)
{ {
std::pair <Endpoint, bool> const result ( if (boost::optional<Endpoint> const result = from_string_checked(s))
from_string_checked (s)); return *result;
if (result.second) return Endpoint{};
return result.first;
return Endpoint {};
} }
std::string Endpoint::to_string () const std::string Endpoint::to_string () const

View File

@@ -1304,11 +1304,11 @@ PeerImp::onMessage(std::shared_ptr <protocol::TMPeerShardInfo> const& m)
{ {
if (m->endpoint() != "0") if (m->endpoint() != "0")
{ {
auto [result, validResult] = auto result =
beast::IP::Endpoint::from_string_checked(m->endpoint()); beast::IP::Endpoint::from_string_checked(m->endpoint());
if (!validResult) if (!result)
return badData("Invalid incoming endpoint: " + m->endpoint()); return badData("Invalid incoming endpoint: " + m->endpoint());
endpoint = std::move(result); endpoint = std::move(*result);
} }
} }
else if (crawl()) // Check if peer will share IP publicly else if (crawl()) // Check if peer will share IP publicly
@@ -1384,8 +1384,8 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMEndpoints> const& m)
for (auto const& tm : m->endpoints_v2 ()) for (auto const& tm : m->endpoints_v2 ())
{ {
// these endpoint strings support ipv4 and ipv6 // these endpoint strings support ipv4 and ipv6
auto [result, validResult] = beast::IP::Endpoint::from_string_checked(tm.endpoint()); auto result = beast::IP::Endpoint::from_string_checked(tm.endpoint());
if (! validResult) if (! result)
{ {
JLOG(p_journal_.error()) << JLOG(p_journal_.error()) <<
"failed to parse incoming endpoint: {" << "failed to parse incoming endpoint: {" <<
@@ -1402,8 +1402,8 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMEndpoints> const& m)
endpoints.emplace_back( endpoints.emplace_back(
tm.hops() > 0 ? tm.hops() > 0 ?
result : *result :
remote_address_.at_port(result.port()), remote_address_.at_port(result->port()),
tm.hops()); tm.hops());
JLOG(p_journal_.trace()) << JLOG(p_journal_.trace()) <<
"got v2 EP: " << endpoints.back().address << "got v2 EP: " << endpoints.back().address <<

View File

@@ -85,18 +85,18 @@ populate (Section const& section, std::string const& field, std::ostream& log,
while (std::getline (ss, ip, ',')) while (std::getline (ss, ip, ','))
{ {
auto const addr = beast::IP::Endpoint::from_string_checked (ip); auto const addr = beast::IP::Endpoint::from_string_checked (ip);
if (! addr.second) if (! addr)
{ {
log << "Invalid value '" << ip << "' for key '" << field << log << "Invalid value '" << ip << "' for key '" << field <<
"' in [" << section.name () << "]"; "' in [" << section.name () << "]";
Throw<std::exception> (); Throw<std::exception> ();
} }
if (is_unspecified (addr.first)) if (is_unspecified (*addr))
{ {
if (! allowAllIps) if (! allowAllIps)
{ {
log << addr.first.address() << " not allowed'" << log << addr->address() << " not allowed'" <<
"' for key '" << field << "' in [" << "' for key '" << field << "' in [" <<
section.name () << "]"; section.name () << "]";
Throw<std::exception> (); Throw<std::exception> ();
@@ -109,13 +109,13 @@ populate (Section const& section, std::string const& field, std::ostream& log,
if (has_any && ! ips->empty ()) if (has_any && ! ips->empty ())
{ {
log << "IP specified along with " << addr.first.address() << log << "IP specified along with " << addr->address() <<
" '" << ip << "' for key '" << field << "' in [" << " '" << ip << "' for key '" << field << "' in [" <<
section.name () << "]"; section.name () << "]";
Throw<std::exception> (); Throw<std::exception> ();
} }
auto const& address = addr.first.address(); auto const& address = addr->address();
if (std::find_if (admin_ip.begin(), admin_ip.end(), if (std::find_if (admin_ip.begin(), admin_ip.end(),
[&address] (beast::IP::Address const& a) [&address] (beast::IP::Address const& a)
{ {
@@ -128,7 +128,7 @@ populate (Section const& section, std::string const& field, std::ostream& log,
Throw<std::exception> (); Throw<std::exception> ();
} }
ips->emplace_back (addr.first.address ()); ips->emplace_back (addr->address ());
} }
} }
} }

View File

@@ -169,16 +169,16 @@ public:
std::uint16_t p, std::uint16_t p,
std::string const& normal = "") std::string const& normal = "")
{ {
auto result {Endpoint::from_string_checked (s)}; auto const result = Endpoint::from_string_checked (s);
if (! BEAST_EXPECT(result.second)) if (! BEAST_EXPECT(result))
return; return;
if (! BEAST_EXPECT(result.first.address().is_v4 ())) if (! BEAST_EXPECT(result->address().is_v4 ()))
return; return;
if (! BEAST_EXPECT(result.first.address().to_v4() == AddressV4 {value})) if (! BEAST_EXPECT(result->address().to_v4() == AddressV4 {value}))
return; return;
BEAST_EXPECT(result.first.port() == p); BEAST_EXPECT(result->port() == p);
BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal)); BEAST_EXPECT(to_string (*result) == (normal.empty() ? s : normal));
} }
void shouldParseEPV6 ( void shouldParseEPV6 (
@@ -187,16 +187,16 @@ public:
std::uint16_t p, std::uint16_t p,
std::string const& normal = "") std::string const& normal = "")
{ {
auto result {Endpoint::from_string_checked (s)}; auto result = Endpoint::from_string_checked (s);
if (! BEAST_EXPECT(result.second)) if (! BEAST_EXPECT(result))
return; return;
if (! BEAST_EXPECT(result.first.address().is_v6 ())) if (! BEAST_EXPECT(result->address().is_v6 ()))
return; return;
if (! BEAST_EXPECT(result.first.address().to_v6() == AddressV6 {value})) if (! BEAST_EXPECT(result->address().to_v6() == AddressV6 {value}))
return; return;
BEAST_EXPECT(result.first.port() == p); BEAST_EXPECT(result->port() == p);
BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal)); BEAST_EXPECT(to_string (*result) == (normal.empty() ? s : normal));
} }
void failParseEP (std::string s) void failParseEP (std::string s)