From 50f94eb3dc304d992db335fc725a717b2071c28f Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 28 Oct 2014 21:59:21 -0500 Subject: [PATCH] Handling ipv4 addressses in to maintain_keepalive. --- rai/core/core.cpp | 61 +++++++++++++++++++++++++------------------- rai/core/core.hpp | 3 +++ rai/test/network.cpp | 16 ++++++++++++ 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/rai/core/core.cpp b/rai/core/core.cpp index 367e132b..a721326f 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -125,35 +125,44 @@ void rai::network::stop () void rai::network::maintain_keepalive (boost::asio::ip::udp::endpoint const & endpoint_a) { - if (endpoint_a != rai::endpoint (boost::asio::ip::address_v6::any (), 0) && !client.peers.contacting_peer (endpoint_a)) + auto endpoint_l (endpoint_a); + if (endpoint_l.address ().is_v4 ()) { - rai::keepalive message; - client.peers.random_fill (message.peers); - message.checksum = client.ledger.checksum (0, std::numeric_limits ::max ()); - std::shared_ptr > bytes (new std::vector ); - { - rai::vectorstream stream (*bytes); - message.serialize (stream); - } - if (network_keepalive_logging ()) - { - client.log.add (boost::str (boost::format ("Keepalive req sent from %1% to %2%") % endpoint ()% endpoint_a)); - } - auto client_l (client.shared ()); - send_buffer (bytes->data (), bytes->size (), endpoint_a, [bytes, client_l, endpoint_a] (boost::system::error_code const & ec, size_t) - { - if (network_logging ()) - { - if (ec) - { - client_l->log.add (boost::str (boost::format ("Error sending keepalive from %1% to %2% %3%") % client_l->network.endpoint () % endpoint_a % ec.message ())); - } - } - }); + endpoint_l = rai::endpoint (boost::asio::ip::address_v6::v4_mapped (endpoint_l.address ().to_v4 ()), endpoint_l.port ()); } - else + assert (endpoint_l.address ().is_v6 ()); + if (endpoint_l != rai::endpoint (boost::asio::ip::address_v6::any (), 0)) { - // Skipping due to keepalive limiting + if (!client.peers.contacting_peer (endpoint_l)) + { + rai::keepalive message; + client.peers.random_fill (message.peers); + message.checksum = client.ledger.checksum (0, std::numeric_limits ::max ()); + std::shared_ptr > bytes (new std::vector ); + { + rai::vectorstream stream (*bytes); + message.serialize (stream); + } + if (network_keepalive_logging ()) + { + client.log.add (boost::str (boost::format ("Keepalive req sent from %1% to %2%") % endpoint () % endpoint_l)); + } + auto client_l (client.shared ()); + send_buffer (bytes->data (), bytes->size (), endpoint_l, [bytes, client_l, endpoint_l] (boost::system::error_code const & ec, size_t) + { + if (network_logging ()) + { + if (ec) + { + client_l->log.add (boost::str (boost::format ("Error sending keepalive from %1% to %2% %3%") % client_l->network.endpoint () % endpoint_l % ec.message ())); + } + } + }); + } + else + { + // Skipping due to keepalive limiting + } } } diff --git a/rai/core/core.hpp b/rai/core/core.hpp index 77ad1f2e..e3b8eb4e 100644 --- a/rai/core/core.hpp +++ b/rai/core/core.hpp @@ -150,6 +150,9 @@ namespace rai { bool deserialize (rai::stream &); void serialize (rai::stream &) override; bool operator == (rai::keepalive const &) const; + uint8_t version_running; + uint8_t version_supported; + std::bitset <16> features; std::array peers; rai::uint256_union checksum; }; diff --git a/rai/test/network.cpp b/rai/test/network.cpp index cd99a14b..265a558f 100644 --- a/rai/test/network.cpp +++ b/rai/test/network.cpp @@ -103,6 +103,22 @@ TEST (network, send_keepalive) ASSERT_NE (peers2.end (), std::find_if (peers2.begin (), peers2.end (), [&system] (rai::peer_information const & information_a) {return information_a.endpoint == system.clients [0]->network.endpoint ();})); } +TEST (network, keepalive_ipv4) +{ + rai::system system (24000, 1); + auto list1 (system.clients [0]->peers.list ()); + ASSERT_EQ (0, list1.size ()); + rai::client_init init1; + auto client1 (std::make_shared (init1, system.service, 24001, system.processor, rai::test_genesis_key.pub)); + client1->start (); + system.clients [0]->network.maintain_keepalive (rai::endpoint (boost::asio::ip::address_v4::loopback (), 24000)); + auto initial (system.clients [0]->network.keepalive_count); + while (system.clients [0]->network.keepalive_count == initial) + { + system.service->run_one (); + } +} + TEST (network, multi_keepalive) { rai::system system (24000, 1);