Handling ipv4 addressses in to maintain_keepalive.

This commit is contained in:
clemahieu 2014-10-28 21:59:21 -05:00
commit 50f94eb3dc
3 changed files with 54 additions and 26 deletions

View file

@ -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 <rai::uint256_t>::max ());
std::shared_ptr <std::vector <uint8_t>> bytes (new std::vector <uint8_t>);
{
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 <rai::uint256_t>::max ());
std::shared_ptr <std::vector <uint8_t>> bytes (new std::vector <uint8_t>);
{
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
}
}
}

View file

@ -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 <rai::endpoint, 8> peers;
rai::uint256_union checksum;
};

View file

@ -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 <rai::client> (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);