From 28ad9837b0ac37f5f9383852bc6f4c423c07ae82 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 28 Oct 2014 20:48:50 -0500 Subject: [PATCH] Converted everything to ipv6. --- rai/core/core.cpp | 68 ++++++++++++++++++++++++------------- rai/core/core.hpp | 11 ++++-- rai/secure.hpp | 1 + rai/test/network.cpp | 4 ++- rai/test/peer_container.cpp | 48 +++++++++++++------------- 5 files changed, 81 insertions(+), 51 deletions(-) diff --git a/rai/core/core.cpp b/rai/core/core.cpp index bf0cda92..488842b0 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -49,7 +49,7 @@ namespace } bool constexpr log_to_cerr () { - return false; + return true; } } @@ -89,7 +89,7 @@ void rai::genesis::initialize (rai::block_store & store_a) const } rai::network::network (boost::asio::io_service & service_a, uint16_t port, rai::client & client_a) : -socket (service_a, boost::asio::ip::udp::endpoint (boost::asio::ip::address_v4::any (), port)), +socket (service_a, boost::asio::ip::udp::endpoint (boost::asio::ip::address_v6::any (), port)), service (service_a), resolver (service_a), client (client_a), @@ -125,7 +125,7 @@ void rai::network::stop () void rai::network::maintain_keepalive (boost::asio::ip::udp::endpoint const & endpoint_a) { - if (!client.peers.contacting_peer (endpoint_a) && endpoint_a != endpoint ()) + if (endpoint_a != rai::endpoint (boost::asio::ip::address_v6::any (), 0) && !client.peers.contacting_peer (endpoint_a)) { rai::keepalive message; client.peers.random_fill (message.peers); @@ -1274,24 +1274,33 @@ std::vector rai::peer_container::list () return result; } -void rai::keepalive::visit (rai::message_visitor & visitor_a) const -{ - visitor_a.keepalive (*this); -} - void rai::publish::visit (rai::message_visitor & visitor_a) const { visitor_a.publish (*this); } +rai::keepalive::keepalive () +{ + boost::asio::ip::udp::endpoint endpoint (boost::asio::ip::address_v6 {}, 0); + for (auto i (peers.begin ()), n (peers.end ()); i != n; ++i) + { + *i = endpoint; + } +} + +void rai::keepalive::visit (rai::message_visitor & visitor_a) const +{ + visitor_a.keepalive (*this); +} + void rai::keepalive::serialize (rai::stream & stream_a) { write (stream_a, rai::message_type::keepalive); for (auto i (peers.begin ()), j (peers.end ()); i != j; ++i) { - assert (i->address ().is_v4 ()); - uint32_t address (i->address ().to_v4 ().to_ulong ()); - write (stream_a, address); + assert (i->address ().is_v6 ()); + auto bytes (i->address ().to_v6 ().to_bytes ()); + write (stream_a, bytes); write (stream_a, i->port ()); } write (stream_a, checksum); @@ -1304,11 +1313,11 @@ bool rai::keepalive::deserialize (rai::stream & stream_a) assert (type == rai::message_type::keepalive); for (auto i (peers.begin ()), j (peers.end ()); i != j; ++i) { - uint32_t address; + std::array address; uint16_t port; read (stream_a, address); read (stream_a, port); - *i = rai::endpoint (boost::asio::ip::address_v4 (address), port); + *i = rai::endpoint (boost::asio::ip::address_v6 (address), port); } read (stream_a, checksum); return result; @@ -2008,7 +2017,7 @@ void rai::processor::connect_bootstrap (std::vector const & peers_ rai::bootstrap_receiver::bootstrap_receiver (boost::asio::io_service & service_a, uint16_t port_a, rai::client & client_a) : acceptor (service_a), -local (boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v4::any (), port_a)), +local (boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v6::any (), port_a)), service (service_a), client (client_a) { @@ -2729,12 +2738,12 @@ void rai::block_store::bootstrap_del (rai::block_hash const & hash_a) rai::endpoint rai::network::endpoint () { - return rai::endpoint (boost::asio::ip::address_v4::loopback (), socket.local_endpoint ().port ()); + return rai::endpoint (boost::asio::ip::address_v6::loopback (), socket.local_endpoint ().port ()); } boost::asio::ip::tcp::endpoint rai::bootstrap_receiver::endpoint () { - return boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v4::loopback (), local.port ()); + return boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v6::loopback (), local.port ()); } rai::bootstrap_initiator::~bootstrap_initiator () @@ -2766,9 +2775,12 @@ void rai::peer_container::random_fill (std::array & target_a auto k (target_a.begin ()); for (auto i (peers.begin ()), j (peers.begin () + std::min (peers.size (), target_a.size ())); i != j; ++i, ++k) { + assert (i->endpoint.address ().is_v6 ()); *k = i->endpoint; } - std::fill (target_a.begin () + std::min (peers.size (), target_a.size ()), target_a.end (), rai::endpoint ()); + auto endpoint (rai::endpoint (boost::asio::ip::address_v6 {}, 0)); + assert (endpoint.address ().is_v6 ()); + std::fill (target_a.begin () + std::min (peers.size (), target_a.size ()), target_a.end (), endpoint); } void rai::processor::ongoing_keepalive () @@ -2823,31 +2835,39 @@ bool rai::peer_container::contacting_peer (rai::endpoint const & endpoint_a) return result; } +namespace { +boost::asio::ip::address_v6 mapped_from_v4_bytes (unsigned long address_a) +{ + return boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (address_a)); +} +} + bool rai::reserved_address (rai::endpoint const & endpoint_a) { - auto bytes (endpoint_a.address ().to_v4().to_ulong ()); + assert (endpoint_a.address ().is_v6 ()); + auto bytes (endpoint_a.address ().to_v6 ()); auto result (false); - if (bytes <= 0x00ffffffu) + if (bytes >= mapped_from_v4_bytes (0x00000000ul) && bytes <= mapped_from_v4_bytes (0x00fffffful)) // Broadcast RFC1700 { result = true; } - else if (bytes >= 0xc0000200ul && bytes <= 0xc00002fful) + else if (bytes >= mapped_from_v4_bytes (0xc0000200ul) && bytes <= mapped_from_v4_bytes (0xc00002fful)) // TEST-NET RFC5737 { result = true; } - else if (bytes >= 0xc6336400ul && bytes <= 0xc63364fful) + else if (bytes >= mapped_from_v4_bytes (0xc6336400ul) && bytes <= mapped_from_v4_bytes (0xc63364fful)) // TEST-NET-2 RFC5737 { result = true; } - else if (bytes >= 0xcb007100ul && bytes <= 0xcb0071fful) + else if (bytes >= mapped_from_v4_bytes (0xcb007100ul) && bytes <= mapped_from_v4_bytes (0xcb0071fful)) // TEST-NET-3 RFC5737 { result = true; } - else if (bytes >= 0xe9fc0000ul && bytes <= 0xe9fc00fful) + else if (bytes >= mapped_from_v4_bytes (0xe9fc0000ul) && bytes <= mapped_from_v4_bytes (0xe9fc00fful)) { result = true; } - else if (bytes >= 0xf0000000ul) + else if (bytes >= mapped_from_v4_bytes (0xf0000000ul)) // Reserver RFC6890 { result = true; } diff --git a/rai/core/core.hpp b/rai/core/core.hpp index c0fcf196..584b1091 100644 --- a/rai/core/core.hpp +++ b/rai/core/core.hpp @@ -44,7 +44,10 @@ namespace std { size_t operator () (rai::endpoint const & endpoint_a) const { - auto result (endpoint_a.address ().to_v4 ().to_ulong () ^ endpoint_a.port ()); + assert (endpoint_a.address ().is_v6 ()); + rai::uint128_union address; + address.bytes = endpoint_a.address ().to_v6 ().to_bytes (); + auto result (address.dwords [0] ^ address.dwords [1] ^ address.dwords [2] ^ address.dwords [3] ^ endpoint_a.port ()); return result; } }; @@ -53,7 +56,10 @@ namespace std { size_t operator () (rai::endpoint const & endpoint_a) const { - auto result ((endpoint_a.address ().to_v4 ().to_ulong () << 2) | endpoint_a.port ()); + assert (endpoint_a.address ().is_v6 ()); + rai::uint128_union address; + address.bytes = endpoint_a.address ().to_v6 ().to_bytes (); + auto result (address.qwords [0] ^ address.qwords [1] ^ endpoint_a.port ()); return result; } }; @@ -139,6 +145,7 @@ namespace rai { class keepalive : public message { public: + keepalive (); void visit (rai::message_visitor &) const override; bool deserialize (rai::stream &); void serialize (rai::stream &) override; diff --git a/rai/secure.hpp b/rai/secure.hpp index af465715..5581fef8 100644 --- a/rai/secure.hpp +++ b/rai/secure.hpp @@ -49,6 +49,7 @@ namespace rai void clear (); std::array bytes; std::array chars; + std::array dwords; std::array qwords; }; using amount = uint128_union; diff --git a/rai/test/network.cpp b/rai/test/network.cpp index eb3792b5..cd99a14b 100644 --- a/rai/test/network.cpp +++ b/rai/test/network.cpp @@ -66,7 +66,7 @@ TEST (network, self_discard) TEST (keepalive, deserialize) { rai::keepalive message1; - message1.peers [0] = rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); + message1.peers [0] = rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); message1.checksum = 1; std::vector bytes; { @@ -89,6 +89,8 @@ TEST (network, send_keepalive) client1->start (); system.clients [0]->network.maintain_keepalive (client1->network.endpoint ()); auto initial (system.clients [0]->network.keepalive_count); + ASSERT_EQ (1, system.clients [0]->peers.list ().size ()); + ASSERT_EQ (0, client1->peers.list ().size ()); while (system.clients [0]->network.keepalive_count == initial) { system.service->run_one (); diff --git a/rai/test/peer_container.cpp b/rai/test/peer_container.cpp index c91cbec8..f08c7aa2 100644 --- a/rai/test/peer_container.cpp +++ b/rai/test/peer_container.cpp @@ -11,7 +11,7 @@ TEST (peer_container, empty_peers) TEST (peer_container, no_recontact) { rai::peer_container peers (rai::endpoint {}); - rai::endpoint endpoint1 (boost::asio::ip::address_v4 (0x7f000001), 10000); + rai::endpoint endpoint1 (boost::asio::ip::address_v6::loopback (), 10000); ASSERT_EQ (0, peers.size ()); ASSERT_FALSE (peers.contacting_peer (endpoint1)); ASSERT_EQ (1, peers.size ()); @@ -20,7 +20,7 @@ TEST (peer_container, no_recontact) TEST (peer_container, no_self_incoming) { - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); + rai::endpoint self (boost::asio::ip::address_v6::loopback (), 10000); rai::peer_container peers (self); peers.incoming_from_peer (self); ASSERT_TRUE (peers.peers.empty ()); @@ -28,7 +28,7 @@ TEST (peer_container, no_self_incoming) TEST (peer_container, no_self_contacting) { - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); + rai::endpoint self (boost::asio::ip::address_v6::loopback (), 10000); rai::peer_container peers (self); peers.contacting_peer (self); ASSERT_TRUE (peers.peers.empty ()); @@ -36,8 +36,8 @@ TEST (peer_container, no_self_contacting) TEST (peer_container, old_known) { - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); - rai::endpoint other (boost::asio::ip::address_v4 (0x7f000001), 10001); + rai::endpoint self (boost::asio::ip::address_v6::loopback (), 10000); + rai::endpoint other (boost::asio::ip::address_v6::loopback (), 10001); rai::peer_container peers (self); peers.contacting_peer (other); ASSERT_FALSE (peers.known_peer (other)); @@ -48,13 +48,13 @@ TEST (peer_container, old_known) TEST (peer_container, reserved_peers_no_contact) { rai::peer_container peers (rai::endpoint {}); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0x00000001), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xc0000201), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xc6336401), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xcb007101), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xe9fc0001), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xf0000001), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xffffffff), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0x00000001)), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xc0000201)), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xc6336401)), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xcb007101)), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xe9fc0001)), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xf0000001)), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xffffffff)), 10000))); ASSERT_EQ (0, peers.size ()); } @@ -62,8 +62,8 @@ TEST (peer_container, split) { rai::peer_container peers (rai::endpoint {}); auto now (std::chrono::system_clock::now ()); - rai::endpoint endpoint1 (boost::asio::ip::address_v4::any (), 100); - rai::endpoint endpoint2 (boost::asio::ip::address_v4::any (), 101); + rai::endpoint endpoint1 (boost::asio::ip::address_v6::any (), 100); + rai::endpoint endpoint2 (boost::asio::ip::address_v6::any (), 101); peers.peers.insert ({endpoint1, now - std::chrono::seconds (1), now - std::chrono::seconds (1)}); peers.peers.insert ({endpoint2, now + std::chrono::seconds (1), now + std::chrono::seconds (1)}); auto list (peers.purge_list (now)); @@ -75,9 +75,9 @@ TEST (peer_container, fill_random_clear) { rai::peer_container peers (rai::endpoint {}); std::array target; - std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); + std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000)); peers.random_fill (target); - ASSERT_TRUE (std::all_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0), 0); })); + ASSERT_TRUE (std::all_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::any (), 0); })); } TEST (peer_container, fill_random_full) @@ -85,12 +85,12 @@ TEST (peer_container, fill_random_full) rai::peer_container peers (rai::endpoint {}); for (auto i (0); i < 100; ++i) { - peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), i)); + peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v6::loopback (), i)); } std::array target; - std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); + std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000)); peers.random_fill (target); - ASSERT_TRUE (std::none_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); })); + ASSERT_TRUE (std::none_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); })); } TEST (peer_container, fill_random_part) @@ -98,12 +98,12 @@ TEST (peer_container, fill_random_part) rai::peer_container peers (rai::endpoint {}); for (auto i (0); i < 16; ++i) { - peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), i + 1)); + peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v6::loopback (), i + 1)); } std::array target; - std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); + std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000)); peers.random_fill (target); - ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); })); - ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 0); })); - ASSERT_TRUE (std::all_of (target.begin () + 16, target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0), 0); })); + ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); })); + ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 0); })); + ASSERT_TRUE (std::all_of (target.begin () + 16, target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::any (), 0); })); } \ No newline at end of file