From 8d1a636a5ec2a61fef6924aea54b49a3fbce8985 Mon Sep 17 00:00:00 2001 From: cryptocode <34946442+cryptocode@users.noreply.github.com> Date: Tue, 9 Oct 2018 17:42:24 +0200 Subject: [PATCH] Move port/address parsing to common, where it's declared (#1263) --- rai/node/common.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++ rai/node/node.cpp | 82 --------------------------------------------- 2 files changed, 82 insertions(+), 82 deletions(-) diff --git a/rai/node/common.cpp b/rai/node/common.cpp index 2865b754..0496639c 100644 --- a/rai/node/common.cpp +++ b/rai/node/common.cpp @@ -739,3 +739,85 @@ void rai::node_id_handshake::visit (rai::message_visitor & visitor_a) const rai::message_visitor::~message_visitor () { } + +bool rai::parse_port (std::string const & string_a, uint16_t & port_a) +{ + bool result; + size_t converted; + try + { + port_a = std::stoul (string_a, &converted); + result = converted != string_a.size () || converted > std::numeric_limits::max (); + } + catch (...) + { + result = true; + } + return result; +} + +bool rai::parse_address_port (std::string const & string, boost::asio::ip::address & address_a, uint16_t & port_a) +{ + auto result (false); + auto port_position (string.rfind (':')); + if (port_position != std::string::npos && port_position > 0) + { + std::string port_string (string.substr (port_position + 1)); + try + { + uint16_t port; + result = parse_port (port_string, port); + if (!result) + { + boost::system::error_code ec; + auto address (boost::asio::ip::address_v6::from_string (string.substr (0, port_position), ec)); + if (!ec) + { + address_a = address; + port_a = port; + } + else + { + result = true; + } + } + else + { + result = true; + } + } + catch (...) + { + result = true; + } + } + else + { + result = true; + } + return result; +} + +bool rai::parse_endpoint (std::string const & string, rai::endpoint & endpoint_a) +{ + boost::asio::ip::address address; + uint16_t port; + auto result (parse_address_port (string, address, port)); + if (!result) + { + endpoint_a = rai::endpoint (address, port); + } + return result; +} + +bool rai::parse_tcp_endpoint (std::string const & string, rai::tcp_endpoint & endpoint_a) +{ + boost::asio::ip::address address; + uint16_t port; + auto result (parse_address_port (string, address, port)); + if (!result) + { + endpoint_a = rai::tcp_endpoint (address, port); + } + return result; +} diff --git a/rai/node/node.cpp b/rai/node/node.cpp index fcfc6ae5..d1188d2a 100644 --- a/rai/node/node.cpp +++ b/rai/node/node.cpp @@ -2122,88 +2122,6 @@ bool rai::peer_container::validate_syn_cookie (rai::endpoint const & endpoint, r return result; } -bool rai::parse_port (std::string const & string_a, uint16_t & port_a) -{ - bool result; - size_t converted; - try - { - port_a = std::stoul (string_a, &converted); - result = converted != string_a.size () || converted > std::numeric_limits::max (); - } - catch (...) - { - result = true; - } - return result; -} - -bool rai::parse_address_port (std::string const & string, boost::asio::ip::address & address_a, uint16_t & port_a) -{ - auto result (false); - auto port_position (string.rfind (':')); - if (port_position != std::string::npos && port_position > 0) - { - std::string port_string (string.substr (port_position + 1)); - try - { - uint16_t port; - result = parse_port (port_string, port); - if (!result) - { - boost::system::error_code ec; - auto address (boost::asio::ip::address_v6::from_string (string.substr (0, port_position), ec)); - if (!ec) - { - address_a = address; - port_a = port; - } - else - { - result = true; - } - } - else - { - result = true; - } - } - catch (...) - { - result = true; - } - } - else - { - result = true; - } - return result; -} - -bool rai::parse_endpoint (std::string const & string, rai::endpoint & endpoint_a) -{ - boost::asio::ip::address address; - uint16_t port; - auto result (parse_address_port (string, address, port)); - if (!result) - { - endpoint_a = rai::endpoint (address, port); - } - return result; -} - -bool rai::parse_tcp_endpoint (std::string const & string, rai::tcp_endpoint & endpoint_a) -{ - boost::asio::ip::address address; - uint16_t port; - auto result (parse_address_port (string, address, port)); - if (!result) - { - endpoint_a = rai::tcp_endpoint (address, port); - } - return result; -} - void rai::node::start () { network.start ();