Move port/address parsing to common, where it's declared (#1263)
This commit is contained in:
parent
61eb38241d
commit
8d1a636a5e
2 changed files with 82 additions and 82 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 ();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue