From b6a5119226d66690cea842d3d128e1aac857efc7 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Wed, 15 Jul 2015 15:48:01 -0400 Subject: [PATCH] Allow using hostnames for seed nodes --- libraries/app/application.cpp | 45 +++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 9eb15af7..305b689a 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -101,12 +102,15 @@ namespace detail { if( _options->count("seed-node") ) { auto seeds = _options->at("seed-node").as>(); - for( const string& ep : seeds ) + for( const string& endpoint_string : seeds ) { - fc::ip::endpoint node = fc::ip::endpoint::from_string(ep); - ilog("Adding seed node ${ip}", ("ip", node)); - _p2p_network->add_node(node); - _p2p_network->connect_to_endpoint(node); + std::vector endpoints = resolve_string_to_ip_endpoints(endpoint_string); + for (const fc::ip::endpoint& endpoint : endpoints) + { + ilog("Adding seed node ${endpoint}", ("endpoint", endpoint)); + _p2p_network->add_node(endpoint); + _p2p_network->connect_to_endpoint(endpoint); + } } } @@ -123,6 +127,33 @@ namespace detail { std::vector()); } FC_CAPTURE_AND_RETHROW() } + std::vector resolve_string_to_ip_endpoints(const std::string& endpoint_string) + { + try + { + string::size_type colon_pos = endpoint_string.find(':'); + if (colon_pos == std::string::npos) + FC_THROW("Missing required port number in endpoint string \"${endpoint_string}\"", + ("endpoint_string", endpoint_string)); + std::string port_string = endpoint_string.substr(colon_pos + 1); + try + { + uint16_t port = boost::lexical_cast(port_string); + + std::string hostname = endpoint_string.substr(0, colon_pos); + std::vector endpoints = fc::resolve(hostname, port); + if (endpoints.empty()) + FC_THROW_EXCEPTION(fc::unknown_host_exception, "The host name can not be resolved: ${hostname}", ("hostname", hostname)); + return endpoints; + } + catch (const boost::bad_lexical_cast&) + { + FC_THROW("Bad port: ${port}", ("port", port_string)); + } + } + FC_CAPTURE_AND_RETHROW((endpoint_string)) + } + void reset_websocket_server() { try { if( !_options->count("rpc-endpoint") ) @@ -395,9 +426,9 @@ namespace detail { { try { std::vector result; result.reserve(30); - auto head_block_num = _chain_db->head_block_num(); + uint32_t head_block_num = _chain_db->head_block_num(); result.push_back(_chain_db->head_block_id()); - auto current = 1; + uint32_t current = 1; while( current < head_block_num ) { result.push_back(_chain_db->get_block_id_for_num(head_block_num - current));