net_node: resolve host for node addresses given via command line flags

This commit is contained in:
stoffu 2018-06-11 12:43:18 +09:00
parent 08b85a8e00
commit 0cf80baea4
No known key found for this signature in database
GPG Key ID: 41DAB8343A9EC012
1 changed files with 43 additions and 13 deletions

View File

@ -62,6 +62,7 @@
namespace nodetool namespace nodetool
{ {
inline bool append_net_address(std::vector<epee::net_utils::network_address> & seed_nodes, std::string const & addr, uint16_t default_port);
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
template<class t_payload_net_handler> template<class t_payload_net_handler>
void node_server<t_payload_net_handler>::init_options(boost::program_options::options_description& desc) void node_server<t_payload_net_handler>::init_options(boost::program_options::options_description& desc)
@ -275,8 +276,20 @@ namespace nodetool
pe.id = crypto::rand<uint64_t>(); pe.id = crypto::rand<uint64_t>();
const uint16_t default_port = cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT; const uint16_t default_port = cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT;
bool r = parse_peer_from_string(pe.adr, pr_str, default_port); bool r = parse_peer_from_string(pe.adr, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse address from string: " << pr_str); if (r)
m_command_line_peers.push_back(pe); {
m_command_line_peers.push_back(pe);
continue;
}
std::vector<epee::net_utils::network_address> resolved_addrs;
r = append_net_address(resolved_addrs, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse or resolve address from string: " << pr_str);
for (const epee::net_utils::network_address& addr : resolved_addrs)
{
pe.id = crypto::rand<uint64_t>();
pe.adr = addr;
m_command_line_peers.push_back(pe);
}
} }
} }
@ -327,24 +340,31 @@ namespace nodetool
return true; return true;
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
inline void append_net_address( inline bool append_net_address(
std::vector<epee::net_utils::network_address> & seed_nodes std::vector<epee::net_utils::network_address> & seed_nodes
, std::string const & addr , std::string const & addr
, uint16_t default_port
) )
{ {
using namespace boost::asio; using namespace boost::asio;
std::string host = addr;
std::string port = std::to_string(default_port);
size_t pos = addr.find_last_of(':'); size_t pos = addr.find_last_of(':');
CHECK_AND_ASSERT_MES_NO_RET(std::string::npos != pos && addr.length() - 1 != pos && 0 != pos, "Failed to parse seed address from string: '" << addr << '\''); if (std::string::npos != pos)
std::string host = addr.substr(0, pos); {
std::string port = addr.substr(pos + 1); CHECK_AND_ASSERT_MES(addr.length() - 1 != pos && 0 != pos, false, "Failed to parse seed address from string: '" << addr << '\'');
host = addr.substr(0, pos);
port = addr.substr(pos + 1);
}
MINFO("Resolving node address: host=" << host << ", port=" << port);
io_service io_srv; io_service io_srv;
ip::tcp::resolver resolver(io_srv); ip::tcp::resolver resolver(io_srv);
ip::tcp::resolver::query query(host, port, boost::asio::ip::tcp::resolver::query::canonical_name); ip::tcp::resolver::query query(host, port, boost::asio::ip::tcp::resolver::query::canonical_name);
boost::system::error_code ec; boost::system::error_code ec;
ip::tcp::resolver::iterator i = resolver.resolve(query, ec); ip::tcp::resolver::iterator i = resolver.resolve(query, ec);
CHECK_AND_ASSERT_MES_NO_RET(!ec, "Failed to resolve host name '" << host << "': " << ec.message() << ':' << ec.value()); CHECK_AND_ASSERT_MES(!ec, false, "Failed to resolve host name '" << host << "': " << ec.message() << ':' << ec.value());
ip::tcp::resolver::iterator iend; ip::tcp::resolver::iterator iend;
for (; i != iend; ++i) for (; i != iend; ++i)
@ -354,14 +374,14 @@ namespace nodetool
{ {
epee::net_utils::network_address na{epee::net_utils::ipv4_network_address{boost::asio::detail::socket_ops::host_to_network_long(endpoint.address().to_v4().to_ulong()), endpoint.port()}}; epee::net_utils::network_address na{epee::net_utils::ipv4_network_address{boost::asio::detail::socket_ops::host_to_network_long(endpoint.address().to_v4().to_ulong()), endpoint.port()}};
seed_nodes.push_back(na); seed_nodes.push_back(na);
MINFO("Added seed node: " << na.str()); MINFO("Added node: " << na.str());
} }
else else
{ {
MWARNING("IPv6 unsupported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec)); MWARNING("IPv6 unsupported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec));
throw std::runtime_error("IPv6 unsupported");
} }
} }
return true;
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
@ -505,7 +525,7 @@ namespace nodetool
for (const auto& full_addr : full_addrs) for (const auto& full_addr : full_addrs)
{ {
MDEBUG("Seed node: " << full_addr); MDEBUG("Seed node: " << full_addr);
append_net_address(m_seed_nodes, full_addr); append_net_address(m_seed_nodes, full_addr, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT);
} }
MDEBUG("Number of seed nodes: " << m_seed_nodes.size()); MDEBUG("Number of seed nodes: " << m_seed_nodes.size());
@ -1150,7 +1170,7 @@ namespace nodetool
for (const auto &peer: get_seed_nodes(m_nettype)) for (const auto &peer: get_seed_nodes(m_nettype))
{ {
MDEBUG("Fallback seed node: " << peer); MDEBUG("Fallback seed node: " << peer);
append_net_address(m_seed_nodes, peer); append_net_address(m_seed_nodes, peer, cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT);
} }
fallback_nodes_added = true; fallback_nodes_added = true;
// continue for another few cycles // continue for another few cycles
@ -1809,8 +1829,18 @@ namespace nodetool
epee::net_utils::network_address na = AUTO_VAL_INIT(na); epee::net_utils::network_address na = AUTO_VAL_INIT(na);
const uint16_t default_port = cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT; const uint16_t default_port = cryptonote::get_config(m_nettype).P2P_DEFAULT_PORT;
bool r = parse_peer_from_string(na, pr_str, default_port); bool r = parse_peer_from_string(na, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse address from string: " << pr_str); if (r)
container.push_back(na); {
container.push_back(na);
continue;
}
std::vector<epee::net_utils::network_address> resolved_addrs;
r = append_net_address(resolved_addrs, pr_str, default_port);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse or resolve address from string: " << pr_str);
for (const epee::net_utils::network_address& addr : resolved_addrs)
{
container.push_back(addr);
}
} }
return true; return true;