From 9db881864db916310c11270c1939db700689c9a3 Mon Sep 17 00:00:00 2001 From: Zachary Michaels Date: Wed, 20 Aug 2014 11:57:29 -0400 Subject: Fix time_t serialization issue On 32-bit MinGW-w64, time_t is int32_t. The existing code was serializing time_t directly and implicitly assuming that time_t is int64_t. This commit formalizes that assumption by serializing int64_t directly and casting to time_t where appropriate. Thanks go to greatwolf for reporting this issue. monero-project/bitmonero#88 --- src/p2p/net_node.inl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/p2p/net_node.inl') diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 870e7572e..954d794b7 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -624,7 +624,9 @@ namespace nodetool peerlist_entry pe_local = AUTO_VAL_INIT(pe_local); pe_local.adr = na; pe_local.id = pi; - time(&pe_local.last_seen); + time_t last_seen; + time(&last_seen); + pe_local.last_seen = static_cast(last_seen); m_peerlist.append_with_peer_white(pe_local); //update last seen and push it to peerlist manager @@ -1102,7 +1104,9 @@ namespace nodetool peerlist_entry pe; pe.adr.ip = context.m_remote_ip; pe.adr.port = port_l; - time(&pe.last_seen); + time_t last_seen; + time(&last_seen); + pe.last_seen = static_cast(last_seen); pe.id = peer_id_l; this->m_peerlist.append_with_peer_white(pe); LOG_PRINT_CCONTEXT_L2("PING SUCCESS " << epee::string_tools::get_ip_string_from_int32(context.m_remote_ip) << ":" << port_l); -- cgit v1.2.3 From 578050e91d8fad8834a7c180a15824f5a46940f9 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Wed, 17 Sep 2014 15:35:52 -0400 Subject: ipv4 and ipv6 resolution working IPv4 and IPv6 name resolution working. Unit tests written (and passing). net_node.{h,inl} code modified to use DNS seeds. --- src/p2p/net_node.inl | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/p2p/net_node.inl') diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 954d794b7..60bc870e7 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -245,17 +245,33 @@ namespace nodetool } else { - add_hardcoded_seed_node(m_seed_nodes, "62.210.78.186:18080"); - add_hardcoded_seed_node(m_seed_nodes, "195.12.60.154:18080"); - add_hardcoded_seed_node(m_seed_nodes, "54.241.246.125:18080"); - add_hardcoded_seed_node(m_seed_nodes, "107.170.157.169:18080"); - add_hardcoded_seed_node(m_seed_nodes, "54.207.112.216:18080"); - add_hardcoded_seed_node(m_seed_nodes, "78.27.112.54:18080"); - add_hardcoded_seed_node(m_seed_nodes, "209.222.30.57:18080"); - add_hardcoded_seed_node(m_seed_nodes, "80.71.13.55:18080"); - add_hardcoded_seed_node(m_seed_nodes, "107.178.112.126:18080"); - add_hardcoded_seed_node(m_seed_nodes, "107.158.233.98:18080"); - add_hardcoded_seed_node(m_seed_nodes, "64.22.111.2:18080"); + // for each hostname in the seed nodes list, attempt to DNS resolve and + // add the result addresses as seed nodes + // TODO: at some point add IPv6 support, but that won't be relevant + // for some time yet. + for (const std::string& addr_str : m_seed_nodes_list) + { + std::vector addr_list = tools::DNSResolver::instance().get_ipv4(addr_str); + for (const std::string& a : addr_list) + { + append_net_address(m_seed_nodes, a + ":18080"); + } + } + + if (!m_seed_nodes.size()) + { + add_hardcoded_seed_node(m_seed_nodes, "62.210.78.186:18080"); + add_hardcoded_seed_node(m_seed_nodes, "195.12.60.154:18080"); + add_hardcoded_seed_node(m_seed_nodes, "54.241.246.125:18080"); + add_hardcoded_seed_node(m_seed_nodes, "107.170.157.169:18080"); + add_hardcoded_seed_node(m_seed_nodes, "54.207.112.216:18080"); + add_hardcoded_seed_node(m_seed_nodes, "78.27.112.54:18080"); + add_hardcoded_seed_node(m_seed_nodes, "209.222.30.57:18080"); + add_hardcoded_seed_node(m_seed_nodes, "80.71.13.55:18080"); + add_hardcoded_seed_node(m_seed_nodes, "107.178.112.126:18080"); + add_hardcoded_seed_node(m_seed_nodes, "107.158.233.98:18080"); + add_hardcoded_seed_node(m_seed_nodes, "64.22.111.2:18080"); + } } bool res = handle_command_line(vm, testnet); -- cgit v1.2.3 From 3fb0fc020f4af5038ce4b2d99578aecb625cda6a Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Wed, 17 Sep 2014 17:25:19 -0400 Subject: seed node DNS code updated to use DNSResolver Also implemented rudimentary IPv6 support, but commented it out because it's not widely supported by ISPs for now, and thus is not currently supported by Monero. --- src/p2p/net_node.inl | 152 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 111 insertions(+), 41 deletions(-) (limited to 'src/p2p/net_node.inl') diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 60bc870e7..34895a292 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -31,10 +31,13 @@ #pragma once #include +#include +#include #include "version.h" #include "string_tools.h" #include "common/util.h" +#include "common/dns_utils.h" #include "net/net_helper.h" #include "math_helper.h" #include "p2p_protocol_defs.h" @@ -195,41 +198,108 @@ namespace nodetool return true; } //----------------------------------------------------------------------------------- - inline void add_hardcoded_seed_node( - std::vector & seed_nodes - , std::string const & addr - ) + namespace { - using namespace boost::asio; - - 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 << '\''); - std::string host = addr.substr(0, pos); - std::string port = addr.substr(pos + 1); - - io_service io_srv; - ip::tcp::resolver resolver(io_srv); - ip::tcp::resolver::query query(host, port); - boost::system::error_code 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()); - - ip::tcp::resolver::iterator iend; - for (; i != iend; ++i) + template + bool append_net_address(T& nodes, const std::string& addr) { - ip::tcp::endpoint endpoint = *i; - if (endpoint.address().is_v4()) + in_addr_t bytes; + // in6_addr_t bytes6; // for IPv6 support, eventually + + size_t pos = addr.find_last_of(':'); + CHECK_AND_ASSERT_MES(std::string::npos != pos && addr.length() - 1 != pos && 0 != pos, false, "Failed to parse seed address from string: '" << addr << '\''); + std::string host = addr.substr(0, pos); + std::string port = addr.substr(pos + 1); + + // attempt to get port number from string + std::stringstream parser(port); + uint32_t portNum; + if (parser >> portNum) + { + // make sure port in valid range (could check > 1000, really) + if (portNum < 65536 && portNum > 0) + { + return false; + } + } + else + { + return false; + } + + // attempt to get network-bytes for ipv4 address + if (inet_pton(AF_INET, host.c_str(), &bytes) != 1) + { + // if that fails, maybe it's a hostname, try to resolve + std::vector addr_list = tools::DNSResolver::instance().get_ipv4(host); + + // if hostname DNS resolution fails, return false + if (addr_list.size() == 0) + { + return false; + } + + // add each resultant IP to seeds + for (const std::string& a : addr_list) + { + // could call append_net_address recursively here to avoid code repeat + if (inet_pton(AF_INET, a.c_str(), &bytes) == 1) + { + nodetool::net_address na; + na.ip = bytes; + na.port = portNum; + nodes.push_back(na); + } + } + } + // if conversion was success (passed string was IP address, not hostname), + // add IP to seeds + else { nodetool::net_address na; - na.ip = boost::asio::detail::socket_ops::host_to_network_long(endpoint.address().to_v4().to_ulong()); - na.port = endpoint.port(); - seed_nodes.push_back(na); - LOG_PRINT_L4("Added seed node: " << endpoint.address().to_v4().to_string(ec) << ':' << na.port); + na.ip = bytes; + na.port = portNum; + nodes.push_back(na); + } + +/* same as above, but for ipv6. Use when the time comes. + // attempt to get network-bytes for ipv6 address + if (inet_pton(AF_INET6, host.c_str(), &bytes6) != 1) + { + // if that fails, maybe it's a hostname, try to resolve + std::vector addr_list = tools::DNSResolver::instance().get_ipv6(host); + + // if hostname DNS resolution fails, return false + if (addr_list.size() == 0) + { + return false; + } + + // add each resultant IP to seeds + for (const std::string& a : addr_list) + { + // could call append_net_address recursively here to avoid code repeat + if (inet_pton(AF_INET6, a.c_str(), &bytes6) == 1) + { + nodetool::net_address6 na; + na.ip = bytes6; + na.port = portNum; + nodes.push_back(na); + } + } } + // if conversion was success (passed string was IP address, not hostname), + // add IP to seeds else { - LOG_PRINT_L2("IPv6 doesn't supported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec)); + nodetool::net_address6 na; + na.ip = bytes6; + na.port = portNum; + nodes.push_back(na); } +*/ + + return true; } } @@ -239,9 +309,9 @@ namespace nodetool { if (testnet) { - add_hardcoded_seed_node(m_seed_nodes, "107.152.187.202:28080"); - add_hardcoded_seed_node(m_seed_nodes, "197.242.158.240:28080"); - add_hardcoded_seed_node(m_seed_nodes, "107.152.130.98:28080"); + append_net_address(m_seed_nodes, "107.152.187.202:28080"); + append_net_address(m_seed_nodes, "197.242.158.240:28080"); + append_net_address(m_seed_nodes, "107.152.130.98:28080"); } else { @@ -260,17 +330,17 @@ namespace nodetool if (!m_seed_nodes.size()) { - add_hardcoded_seed_node(m_seed_nodes, "62.210.78.186:18080"); - add_hardcoded_seed_node(m_seed_nodes, "195.12.60.154:18080"); - add_hardcoded_seed_node(m_seed_nodes, "54.241.246.125:18080"); - add_hardcoded_seed_node(m_seed_nodes, "107.170.157.169:18080"); - add_hardcoded_seed_node(m_seed_nodes, "54.207.112.216:18080"); - add_hardcoded_seed_node(m_seed_nodes, "78.27.112.54:18080"); - add_hardcoded_seed_node(m_seed_nodes, "209.222.30.57:18080"); - add_hardcoded_seed_node(m_seed_nodes, "80.71.13.55:18080"); - add_hardcoded_seed_node(m_seed_nodes, "107.178.112.126:18080"); - add_hardcoded_seed_node(m_seed_nodes, "107.158.233.98:18080"); - add_hardcoded_seed_node(m_seed_nodes, "64.22.111.2:18080"); + append_net_address(m_seed_nodes, "62.210.78.186:18080"); + append_net_address(m_seed_nodes, "195.12.60.154:18080"); + append_net_address(m_seed_nodes, "54.241.246.125:18080"); + append_net_address(m_seed_nodes, "107.170.157.169:18080"); + append_net_address(m_seed_nodes, "54.207.112.216:18080"); + append_net_address(m_seed_nodes, "78.27.112.54:18080"); + append_net_address(m_seed_nodes, "209.222.30.57:18080"); + append_net_address(m_seed_nodes, "80.71.13.55:18080"); + append_net_address(m_seed_nodes, "107.178.112.126:18080"); + append_net_address(m_seed_nodes, "107.158.233.98:18080"); + append_net_address(m_seed_nodes, "64.22.111.2:18080"); } } -- cgit v1.2.3 From d1a7f699c67fc8c5b43b3119d9e577ef97a22c37 Mon Sep 17 00:00:00 2001 From: Riccardo Spagni Date: Wed, 24 Sep 2014 14:45:34 +0200 Subject: use boost::asio::ip::address because cross-platform plz --- src/p2p/net_node.inl | 123 +++++++++++---------------------------------------- 1 file changed, 27 insertions(+), 96 deletions(-) (limited to 'src/p2p/net_node.inl') diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 34895a292..08fd1d1e6 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -31,8 +31,6 @@ #pragma once #include -#include -#include #include "version.h" #include "string_tools.h" @@ -198,108 +196,41 @@ namespace nodetool return true; } //----------------------------------------------------------------------------------- - namespace + inline void append_net_address( + std::vector & seed_nodes + , std::string const & addr + ) { - template - bool append_net_address(T& nodes, const std::string& addr) + using namespace boost::asio; + + 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 << '\''); + std::string host = addr.substr(0, pos); + std::string port = addr.substr(pos + 1); + + io_service io_srv; + ip::tcp::resolver resolver(io_srv); + ip::tcp::resolver::query query(host, port); + boost::system::error_code 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()); + + ip::tcp::resolver::iterator iend; + for (; i != iend; ++i) { - in_addr_t bytes; - // in6_addr_t bytes6; // for IPv6 support, eventually - - size_t pos = addr.find_last_of(':'); - CHECK_AND_ASSERT_MES(std::string::npos != pos && addr.length() - 1 != pos && 0 != pos, false, "Failed to parse seed address from string: '" << addr << '\''); - std::string host = addr.substr(0, pos); - std::string port = addr.substr(pos + 1); - - // attempt to get port number from string - std::stringstream parser(port); - uint32_t portNum; - if (parser >> portNum) - { - // make sure port in valid range (could check > 1000, really) - if (portNum < 65536 && portNum > 0) - { - return false; - } - } - else - { - return false; - } - - // attempt to get network-bytes for ipv4 address - if (inet_pton(AF_INET, host.c_str(), &bytes) != 1) - { - // if that fails, maybe it's a hostname, try to resolve - std::vector addr_list = tools::DNSResolver::instance().get_ipv4(host); - - // if hostname DNS resolution fails, return false - if (addr_list.size() == 0) - { - return false; - } - - // add each resultant IP to seeds - for (const std::string& a : addr_list) - { - // could call append_net_address recursively here to avoid code repeat - if (inet_pton(AF_INET, a.c_str(), &bytes) == 1) - { - nodetool::net_address na; - na.ip = bytes; - na.port = portNum; - nodes.push_back(na); - } - } - } - // if conversion was success (passed string was IP address, not hostname), - // add IP to seeds - else + ip::tcp::endpoint endpoint = *i; + if (endpoint.address().is_v4()) { nodetool::net_address na; - na.ip = bytes; - na.port = portNum; - nodes.push_back(na); - } - -/* same as above, but for ipv6. Use when the time comes. - // attempt to get network-bytes for ipv6 address - if (inet_pton(AF_INET6, host.c_str(), &bytes6) != 1) - { - // if that fails, maybe it's a hostname, try to resolve - std::vector addr_list = tools::DNSResolver::instance().get_ipv6(host); - - // if hostname DNS resolution fails, return false - if (addr_list.size() == 0) - { - return false; - } - - // add each resultant IP to seeds - for (const std::string& a : addr_list) - { - // could call append_net_address recursively here to avoid code repeat - if (inet_pton(AF_INET6, a.c_str(), &bytes6) == 1) - { - nodetool::net_address6 na; - na.ip = bytes6; - na.port = portNum; - nodes.push_back(na); - } - } + na.ip = boost::asio::detail::socket_ops::host_to_network_long(endpoint.address().to_v4().to_ulong()); + na.port = endpoint.port(); + seed_nodes.push_back(na); + LOG_PRINT_L4("Added seed node: " << endpoint.address().to_v4().to_string(ec) << ':' << na.port); } - // if conversion was success (passed string was IP address, not hostname), - // add IP to seeds else { - nodetool::net_address6 na; - na.ip = bytes6; - na.port = portNum; - nodes.push_back(na); + LOG_PRINT_L2("IPv6 doesn't supported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec)); } -*/ - - return true; } } -- cgit v1.2.3