diff --git a/src/ripple/app/main/Main.cpp b/src/ripple/app/main/Main.cpp index ef96d1ccd2..03d9bc61df 100644 --- a/src/ripple/app/main/Main.cpp +++ b/src/ripple/app/main/Main.cpp @@ -608,16 +608,16 @@ int run (int argc, char** argv) // happen after the config file is loaded. 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()); - if (! valid) + if (! endpoint) { std::cerr << "Invalid rpc_ip = " << vm["rpc_ip"].as() << "\n"; return -1; } - if (endpoint.port() == 0) + if (endpoint->port() == 0) { std::cerr << "No port specified in rpc_ip.\n"; if (vm.count ("rpc_port")) @@ -625,9 +625,9 @@ int run (int argc, char** argv) std::cerr << "WARNING: using deprecated rpc_port param.\n"; try { - endpoint = endpoint.at_port( + endpoint = endpoint->at_port( vm["rpc_port"].as()); - if (endpoint.port() == 0) + if (endpoint->port() == 0) throw std::domain_error("0"); } catch(std::exception const& e) @@ -640,7 +640,7 @@ int run (int argc, char** argv) return -1; } - config->rpc_ip = std::move(endpoint); + config->rpc_ip = std::move(*endpoint); } if (vm.count ("quorum")) diff --git a/src/ripple/basics/impl/ResolverAsio.cpp b/src/ripple/basics/impl/ResolverAsio.cpp index 9ad4a81805..b62ee945cc 100644 --- a/src/ripple/basics/impl/ResolverAsio.cpp +++ b/src/ripple/basics/impl/ResolverAsio.cpp @@ -274,13 +274,11 @@ public: // first attempt to parse as an endpoint (IP addr + port). // If that doesn't succeed, fall back to generic name + port parsing - if (auto const [result, validResult] = - beast::IP::Endpoint::from_string_checked(str); - validResult) + if (auto const result = beast::IP::Endpoint::from_string_checked(str)) { return make_pair ( - result.address().to_string(), - std::to_string(result.port())); + result->address().to_string(), + std::to_string(result->port())); } // generic name/port parsing, which doesn't work for diff --git a/src/ripple/basics/impl/StringUtilities.cpp b/src/ripple/basics/impl/StringUtilities.cpp index cb0e91d6ac..e62bb31a4c 100644 --- a/src/ripple/basics/impl/StringUtilities.cpp +++ b/src/ripple/basics/impl/StringUtilities.cpp @@ -126,9 +126,9 @@ bool parseUrl (parsedURL& pUrl, std::string const& strUrl) // We need to use Endpoint to parse the domain to // strip surrounding brackets from IPv6 addresses, // e.g. [::1] => ::1. - const auto [result, validResult] = beast::IP::Endpoint::from_string_checked (domain); - pUrl.domain = validResult - ? result.address().to_string() + const auto result = beast::IP::Endpoint::from_string_checked (domain); + pUrl.domain = result + ? result->address().to_string() : domain; const std::string port = smMatch[5]; if (!port.empty()) diff --git a/src/ripple/beast/net/IPEndpoint.h b/src/ripple/beast/net/IPEndpoint.h index e4ced026d4..74621395e9 100644 --- a/src/ripple/beast/net/IPEndpoint.h +++ b/src/ripple/beast/net/IPEndpoint.h @@ -23,6 +23,9 @@ #include #include #include + +#include + #include #include #include @@ -44,9 +47,9 @@ public: /** Create an Endpoint from a string. 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 from_string_checked (std::string const& s); + static boost::optional from_string_checked (std::string const& s); static Endpoint from_string (std::string const& s); /** Returns a string representing the endpoint. */ diff --git a/src/ripple/beast/net/impl/IPEndpoint.cpp b/src/ripple/beast/net/impl/IPEndpoint.cpp index 95435e8b77..0ea7b11e88 100644 --- a/src/ripple/beast/net/impl/IPEndpoint.cpp +++ b/src/ripple/beast/net/impl/IPEndpoint.cpp @@ -34,23 +34,21 @@ Endpoint::Endpoint (Address const& addr, Port port) { } -std::pair Endpoint::from_string_checked (std::string const& s) +boost::optional Endpoint::from_string_checked (std::string const& s) { std::stringstream is (boost::trim_copy(s)); Endpoint endpoint; is >> endpoint; if (! is.fail() && is.rdbuf()->in_avail() == 0) - return std::make_pair (endpoint, true); - return std::make_pair (Endpoint {}, false); + return endpoint; + return {}; } Endpoint Endpoint::from_string (std::string const& s) { - std::pair const result ( - from_string_checked (s)); - if (result.second) - return result.first; - return Endpoint {}; + if (boost::optional const result = from_string_checked(s)) + return *result; + return Endpoint{}; } std::string Endpoint::to_string () const diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index 6fd867a8bb..88a231e8fc 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -1304,11 +1304,11 @@ PeerImp::onMessage(std::shared_ptr const& m) { if (m->endpoint() != "0") { - auto [result, validResult] = + auto result = beast::IP::Endpoint::from_string_checked(m->endpoint()); - if (!validResult) + if (!result) 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 @@ -1384,8 +1384,8 @@ PeerImp::onMessage (std::shared_ptr const& m) for (auto const& tm : m->endpoints_v2 ()) { // these endpoint strings support ipv4 and ipv6 - auto [result, validResult] = beast::IP::Endpoint::from_string_checked(tm.endpoint()); - if (! validResult) + auto result = beast::IP::Endpoint::from_string_checked(tm.endpoint()); + if (! result) { JLOG(p_journal_.error()) << "failed to parse incoming endpoint: {" << @@ -1402,8 +1402,8 @@ PeerImp::onMessage (std::shared_ptr const& m) endpoints.emplace_back( tm.hops() > 0 ? - result : - remote_address_.at_port(result.port()), + *result : + remote_address_.at_port(result->port()), tm.hops()); JLOG(p_journal_.trace()) << "got v2 EP: " << endpoints.back().address << diff --git a/src/ripple/server/impl/Port.cpp b/src/ripple/server/impl/Port.cpp index ac7eeb14c3..fc342c6ee5 100644 --- a/src/ripple/server/impl/Port.cpp +++ b/src/ripple/server/impl/Port.cpp @@ -85,18 +85,18 @@ populate (Section const& section, std::string const& field, std::ostream& log, while (std::getline (ss, ip, ',')) { auto const addr = beast::IP::Endpoint::from_string_checked (ip); - if (! addr.second) + if (! addr) { log << "Invalid value '" << ip << "' for key '" << field << "' in [" << section.name () << "]"; Throw (); } - if (is_unspecified (addr.first)) + if (is_unspecified (*addr)) { if (! allowAllIps) { - log << addr.first.address() << " not allowed'" << + log << addr->address() << " not allowed'" << "' for key '" << field << "' in [" << section.name () << "]"; Throw (); @@ -109,13 +109,13 @@ populate (Section const& section, std::string const& field, std::ostream& log, 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 [" << section.name () << "]"; Throw (); } - auto const& address = addr.first.address(); + auto const& address = addr->address(); if (std::find_if (admin_ip.begin(), admin_ip.end(), [&address] (beast::IP::Address const& a) { @@ -128,7 +128,7 @@ populate (Section const& section, std::string const& field, std::ostream& log, Throw (); } - ips->emplace_back (addr.first.address ()); + ips->emplace_back (addr->address ()); } } } diff --git a/src/test/beast/IPEndpoint_test.cpp b/src/test/beast/IPEndpoint_test.cpp index 882cc74672..f48b295737 100644 --- a/src/test/beast/IPEndpoint_test.cpp +++ b/src/test/beast/IPEndpoint_test.cpp @@ -169,16 +169,16 @@ public: std::uint16_t p, std::string const& normal = "") { - auto result {Endpoint::from_string_checked (s)}; - if (! BEAST_EXPECT(result.second)) + auto const result = Endpoint::from_string_checked (s); + if (! BEAST_EXPECT(result)) return; - if (! BEAST_EXPECT(result.first.address().is_v4 ())) + if (! BEAST_EXPECT(result->address().is_v4 ())) return; - if (! BEAST_EXPECT(result.first.address().to_v4() == AddressV4 {value})) + if (! BEAST_EXPECT(result->address().to_v4() == AddressV4 {value})) return; - BEAST_EXPECT(result.first.port() == p); - BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal)); + BEAST_EXPECT(result->port() == p); + BEAST_EXPECT(to_string (*result) == (normal.empty() ? s : normal)); } void shouldParseEPV6 ( @@ -187,16 +187,16 @@ public: std::uint16_t p, std::string const& normal = "") { - auto result {Endpoint::from_string_checked (s)}; - if (! BEAST_EXPECT(result.second)) + auto result = Endpoint::from_string_checked (s); + if (! BEAST_EXPECT(result)) return; - if (! BEAST_EXPECT(result.first.address().is_v6 ())) + if (! BEAST_EXPECT(result->address().is_v6 ())) return; - if (! BEAST_EXPECT(result.first.address().to_v6() == AddressV6 {value})) + if (! BEAST_EXPECT(result->address().to_v6() == AddressV6 {value})) return; - BEAST_EXPECT(result.first.port() == p); - BEAST_EXPECT(to_string (result.first) == (normal.empty() ? s : normal)); + BEAST_EXPECT(result->port() == p); + BEAST_EXPECT(to_string (*result) == (normal.empty() ? s : normal)); } void failParseEP (std::string s)