Move port/address parsing to common, where it's declared (#1263)

This commit is contained in:
cryptocode 2018-10-09 17:42:24 +02:00 committed by Roy Keene
commit 8d1a636a5e
2 changed files with 82 additions and 82 deletions

View file

@ -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<uint16_t>::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;
}

View file

@ -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<uint16_t>::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 ();