diff --git a/Makefile.am b/Makefile.am index cc5a81347..f97dabf30 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,12 +51,14 @@ src_libbitcoin_network_la_SOURCES = \ src/settings.cpp \ src/socket.cpp \ src/protocols/protocol.cpp \ - src/protocols/protocol_address.cpp \ + src/protocols/protocol_address_31402.cpp \ src/protocols/protocol_events.cpp \ - src/protocols/protocol_ping.cpp \ - src/protocols/protocol_seed.cpp \ + src/protocols/protocol_ping_31402.cpp \ + src/protocols/protocol_ping_60001.cpp \ + src/protocols/protocol_seed_31402.cpp \ src/protocols/protocol_timer.cpp \ - src/protocols/protocol_version.cpp \ + src/protocols/protocol_version_31402.cpp \ + src/protocols/protocol_version_70002.cpp \ src/sessions/session.cpp \ src/sessions/session_batch.cpp \ src/sessions/session_inbound.cpp \ @@ -108,12 +110,14 @@ include_bitcoin_network_HEADERS = \ include_bitcoin_network_protocolsdir = ${includedir}/bitcoin/network/protocols include_bitcoin_network_protocols_HEADERS = \ include/bitcoin/network/protocols/protocol.hpp \ - include/bitcoin/network/protocols/protocol_address.hpp \ + include/bitcoin/network/protocols/protocol_address_31402.hpp \ include/bitcoin/network/protocols/protocol_events.hpp \ - include/bitcoin/network/protocols/protocol_ping.hpp \ - include/bitcoin/network/protocols/protocol_seed.hpp \ + include/bitcoin/network/protocols/protocol_ping_31402.hpp \ + include/bitcoin/network/protocols/protocol_ping_60001.hpp \ + include/bitcoin/network/protocols/protocol_seed_31402.hpp \ include/bitcoin/network/protocols/protocol_timer.hpp \ - include/bitcoin/network/protocols/protocol_version.hpp + include/bitcoin/network/protocols/protocol_version_31402.hpp \ + include/bitcoin/network/protocols/protocol_version_70002.hpp include_bitcoin_network_sessionsdir = ${includedir}/bitcoin/network/sessions include_bitcoin_network_sessions_HEADERS = \ diff --git a/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj b/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj index 344c524ad..30c66b46f 100644 --- a/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj +++ b/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj @@ -80,15 +80,17 @@ + + - + - - + + - + @@ -111,16 +113,18 @@ + + - + - - + + - + diff --git a/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj.filters b/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj.filters index 62f005088..620b94087 100644 --- a/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj.filters +++ b/builds/msvc/vs2013/libbitcoin-network/libbitcoin-network.vcxproj.filters @@ -4,24 +4,12 @@ src\protocols - - src\protocols - src\protocols - - src\protocols - - - src\protocols - src\protocols - - src\protocols - src\sessions @@ -85,6 +73,24 @@ src + + src\protocols + + + src\protocols + + + src\protocols + + + src\protocols + + + src\protocols + + + src\protocols + @@ -93,24 +99,12 @@ include\bitcoin\network\protocols - - include\bitcoin\network\protocols - include\bitcoin\network\protocols - - include\bitcoin\network\protocols - - - include\bitcoin\network\protocols - include\bitcoin\network\protocols - - include\bitcoin\network\protocols - include\bitcoin\network\sessions @@ -177,6 +171,24 @@ include + + include\bitcoin\network\protocols + + + include\bitcoin\network\protocols + + + include\bitcoin\network\protocols + + + include\bitcoin\network\protocols + + + include\bitcoin\network\protocols + + + include\bitcoin\network\protocols + diff --git a/include/bitcoin/network.hpp b/include/bitcoin/network.hpp index d68bd1edb..a57b47829 100644 --- a/include/bitcoin/network.hpp +++ b/include/bitcoin/network.hpp @@ -33,12 +33,14 @@ #include #include #include -#include +#include #include -#include -#include +#include +#include +#include #include -#include +#include +#include #include #include #include diff --git a/include/bitcoin/network/protocols/protocol_address.hpp b/include/bitcoin/network/protocols/protocol_address_31402.hpp similarity index 73% rename from include/bitcoin/network/protocols/protocol_address.hpp rename to include/bitcoin/network/protocols/protocol_address_31402.hpp index f419cc2e6..4ceff93c7 100644 --- a/include/bitcoin/network/protocols/protocol_address.hpp +++ b/include/bitcoin/network/protocols/protocol_address_31402.hpp @@ -17,8 +17,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef LIBBITCOIN_NETWORK_PROTOCOL_ADDRESS_HPP -#define LIBBITCOIN_NETWORK_PROTOCOL_ADDRESS_HPP +#ifndef LIBBITCOIN_NETWORK_PROTOCOL_ADDRESS_31402_HPP +#define LIBBITCOIN_NETWORK_PROTOCOL_ADDRESS_31402_HPP #include #include @@ -35,30 +35,31 @@ class p2p; * Address protocol. * Attach this to a channel immediately following handshake completion. */ -class BCT_API protocol_address - : public protocol_events, track +class BCT_API protocol_address_31402 + : public protocol_events, track { public: - typedef std::shared_ptr ptr; + typedef std::shared_ptr ptr; /** * Construct an address protocol instance. * @param[in] network The network interface. * @param[in] channel The channel on which to start the protocol. */ - protocol_address(p2p& network, channel::ptr channel); + protocol_address_31402(p2p& network, channel::ptr channel); /** * Start the protocol. */ virtual void start(); -private: - void handle_stop(const code& ec); - void handle_store_addresses(const code& ec); +protected: + virtual void handle_stop(const code& ec); + virtual void handle_store_addresses(const code& ec); - bool handle_receive_address(const code& ec, message::address::ptr address); - bool handle_receive_get_address(const code& ec, + virtual bool handle_receive_address(const code& ec, + message::address::ptr address); + virtual bool handle_receive_get_address(const code& ec, message::get_address::ptr message); p2p& network_; diff --git a/include/bitcoin/network/protocols/protocol_ping.hpp b/include/bitcoin/network/protocols/protocol_ping_31402.hpp similarity index 75% rename from include/bitcoin/network/protocols/protocol_ping.hpp rename to include/bitcoin/network/protocols/protocol_ping_31402.hpp index dc5e18d25..0508cddfe 100644 --- a/include/bitcoin/network/protocols/protocol_ping.hpp +++ b/include/bitcoin/network/protocols/protocol_ping_31402.hpp @@ -17,10 +17,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef LIBBITCOIN_NETWORK_PROTOCOL_PING_HPP -#define LIBBITCOIN_NETWORK_PROTOCOL_PING_HPP +#ifndef LIBBITCOIN_NETWORK_PROTOCOL_PING_31402_HPP +#define LIBBITCOIN_NETWORK_PROTOCOL_PING_31402_HPP -#include #include #include #include @@ -37,30 +36,29 @@ class p2p; * Ping-pong protocol. * Attach this to a channel immediately following handshake completion. */ -class BCT_API protocol_ping - : public protocol_timer, track +class BCT_API protocol_ping_31402 + : public protocol_timer, track { public: - typedef std::shared_ptr ptr; + typedef std::shared_ptr ptr; /** * Construct a ping protocol instance. * @param[in] network The network interface. * @param[in] channel The channel on which to start the protocol. */ - protocol_ping(p2p& network, channel::ptr channel); + protocol_ping_31402(p2p& network, channel::ptr channel); /** * Start the protocol. */ virtual void start(); -private: - void send_ping(const code& ec); +protected: + virtual void send_ping(const code& ec); - bool handle_receive_ping(const code& ec, message::ping::ptr message); - bool handle_receive_pong(const code& ec, message::pong::ptr message, - uint64_t nonce); + virtual bool handle_receive_ping(const code& ec, + message::ping::ptr message); const settings& settings_; }; diff --git a/include/bitcoin/network/protocols/protocol_ping_60001.hpp b/include/bitcoin/network/protocols/protocol_ping_60001.hpp new file mode 100644 index 000000000..f83dc9bde --- /dev/null +++ b/include/bitcoin/network/protocols/protocol_ping_60001.hpp @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * libbitcoin is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License with + * additional permissions to the one published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. For more information see LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_PROTOCOL_PING_60001_HPP +#define LIBBITCOIN_NETWORK_PROTOCOL_PING_60001_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace network { + +class p2p; + +/** + * Ping-pong protocol. + * Attach this to a channel immediately following handshake completion. + */ +class BCT_API protocol_ping_60001 + : public protocol_ping_31402, track +{ +public: + typedef std::shared_ptr ptr; + + /** + * Construct a ping protocol instance. + * @param[in] network The network interface. + * @param[in] channel The channel on which to start the protocol. + */ + protocol_ping_60001(p2p& network, channel::ptr channel); + +protected: + void send_ping(const code& ec) override; + + bool handle_receive_ping(const code& ec, + message::ping::ptr message) override; + virtual bool handle_receive_pong(const code& ec, + message::pong::ptr message, uint64_t nonce); +}; + +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/network/protocols/protocol_seed.hpp b/include/bitcoin/network/protocols/protocol_seed_31402.hpp similarity index 68% rename from include/bitcoin/network/protocols/protocol_seed.hpp rename to include/bitcoin/network/protocols/protocol_seed_31402.hpp index dd74db9a5..a5aa91a49 100644 --- a/include/bitcoin/network/protocols/protocol_seed.hpp +++ b/include/bitcoin/network/protocols/protocol_seed_31402.hpp @@ -17,8 +17,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef LIBBITCOIN_NETWORK_PROTOCOL_SEED_HPP -#define LIBBITCOIN_NETWORK_PROTOCOL_SEED_HPP +#ifndef LIBBITCOIN_NETWORK_PROTOCOL_SEED_31402_HPP +#define LIBBITCOIN_NETWORK_PROTOCOL_SEED_31402_HPP #include #include @@ -35,18 +35,18 @@ class p2p; * Seeding protocol. * Attach this to a channel immediately following seed handshake completion. */ -class BCT_API protocol_seed - : public protocol_timer, track +class BCT_API protocol_seed_31402 + : public protocol_timer, track { public: - typedef std::shared_ptr ptr; + typedef std::shared_ptr ptr; /** * Construct a seed protocol instance. * @param[in] network The network interface. * @param[in] channel The channel on which to start the protocol. */ - protocol_seed(p2p& network, channel::ptr channel); + protocol_seed_31402(p2p& network, channel::ptr channel); /** * Start the protocol. @@ -54,16 +54,18 @@ class BCT_API protocol_seed */ virtual void start(event_handler handler); -private: - void send_own_address(const settings& settings); +protected: + virtual void send_own_address(const settings& settings); - void handle_send_address(const code& ec); - void handle_send_get_address(const code& ec); - void handle_store_addresses(const code& ec); - void handle_seeding_complete(const code& ec, event_handler handler); + virtual void handle_send_address(const code& ec); + virtual void handle_send_get_address(const code& ec); + virtual void handle_store_addresses(const code& ec); + virtual void handle_seeding_complete(const code& ec, + event_handler handler); - bool handle_receive_address(const code& ec, message::address::ptr address); - ////bool handle_receive_get_address(const code& ec, + virtual bool handle_receive_address(const code& ec, + message::address::ptr address); + ////virtual bool handle_receive_get_address(const code& ec, //// message::get_address::ptr message); p2p& network_; diff --git a/include/bitcoin/network/protocols/protocol_version.hpp b/include/bitcoin/network/protocols/protocol_version_31402.hpp similarity index 73% rename from include/bitcoin/network/protocols/protocol_version.hpp rename to include/bitcoin/network/protocols/protocol_version_31402.hpp index 04fef483d..bfe34d79e 100644 --- a/include/bitcoin/network/protocols/protocol_version.hpp +++ b/include/bitcoin/network/protocols/protocol_version_31402.hpp @@ -17,8 +17,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef LIBBITCOIN_NETWORK_PROTOCOL_VERSION_HPP -#define LIBBITCOIN_NETWORK_PROTOCOL_VERSION_HPP +#ifndef LIBBITCOIN_NETWORK_PROTOCOL_VERSION_31402_HPP +#define LIBBITCOIN_NETWORK_PROTOCOL_VERSION_31402_HPP #include #include @@ -34,18 +34,18 @@ namespace network { class p2p; -class BCT_API protocol_version - : public protocol_timer, track +class BCT_API protocol_version_31402 + : public protocol_timer, track { public: - typedef std::shared_ptr ptr; + typedef std::shared_ptr ptr; /** * Construct a version protocol instance. * @param[in] network The network interface. * @param[in] channel The channel on which to start the protocol. */ - protocol_version(p2p& network, channel::ptr channel); + protocol_version_31402(p2p& network, channel::ptr channel); /** * Start the protocol. @@ -54,19 +54,18 @@ class BCT_API protocol_version virtual void start(event_handler handler); protected: - /// Override to vary the version message. - virtual void send_version(const message::version& self); - -private: static message::version version_factory( const config::authority& authority, const settings& settings, - uint64_t nonce, size_t height); + uint64_t nonce, size_t height); + + virtual void send_version(const message::version& self); - void handle_version_sent(const code& ec); - void handle_verack_sent(const code& ec); + virtual void handle_version_sent(const code& ec); + virtual void handle_verack_sent(const code& ec); - bool handle_receive_version(const code& ec, message::version::ptr version); - bool handle_receive_verack(const code& ec, message::verack::ptr); + virtual bool handle_receive_version(const code& ec, + message::version::ptr version); + virtual bool handle_receive_verack(const code& ec, message::verack::ptr); p2p& network_; }; diff --git a/include/bitcoin/network/protocols/protocol_version_70002.hpp b/include/bitcoin/network/protocols/protocol_version_70002.hpp new file mode 100644 index 000000000..e06a5fa3d --- /dev/null +++ b/include/bitcoin/network/protocols/protocol_version_70002.hpp @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * libbitcoin is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License with + * additional permissions to the one published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. For more information see LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_PROTOCOL_VERSION_70002_HPP +#define LIBBITCOIN_NETWORK_PROTOCOL_VERSION_70002_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace network { + +class p2p; + +class BCT_API protocol_version_70002 + : public protocol_version_31402, track +{ +public: + typedef std::shared_ptr ptr; + + /** + * Construct a version protocol instance. + * @param[in] network The network interface. + * @param[in] channel The channel on which to start the protocol. + */ + protocol_version_70002(p2p& network, channel::ptr channel); + + /** + * Start the protocol. + * @param[in] handler Invoked upon stop or receipt of version and verack. + */ + virtual void start(event_handler handler); + +protected: + static message::version version_factory( + const config::authority& authority, const settings& settings, + uint64_t nonce, size_t height); +}; + +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/src/p2p.cpp b/src/p2p.cpp index ae4a62ad7..f92292716 100644 --- a/src/p2p.cpp +++ b/src/p2p.cpp @@ -30,10 +30,12 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include #include #include diff --git a/src/protocols/protocol_address.cpp b/src/protocols/protocol_address_31402.cpp similarity index 85% rename from src/protocols/protocol_address.cpp rename to src/protocols/protocol_address_31402.cpp index cfadfabd6..4c8ed7871 100644 --- a/src/protocols/protocol_address.cpp +++ b/src/protocols/protocol_address_31402.cpp @@ -17,7 +17,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include +#include #include #include @@ -29,23 +29,24 @@ namespace libbitcoin { namespace network { -#define NAME "address" -#define CLASS protocol_address +#define NAME "protocol_address_31402" +#define CLASS protocol_address_31402 using namespace bc::message; using namespace std::placeholders; -protocol_address::protocol_address(p2p& network, channel::ptr channel) +protocol_address_31402::protocol_address_31402(p2p& network, + channel::ptr channel) : protocol_events(network, channel, NAME), network_(network), - CONSTRUCT_TRACK(protocol_address) + CONSTRUCT_TRACK(protocol_address_31402) { } // Start sequence. // ---------------------------------------------------------------------------- -void protocol_address::start() +void protocol_address_31402::start() { const auto& settings = network_.network_settings(); @@ -64,13 +65,13 @@ void protocol_address::start() SUBSCRIBE2(address, handle_receive_address, _1, _2); SUBSCRIBE2(get_address, handle_receive_get_address, _1, _2); - SEND2(get_address(), handle_send, _1, get_address::command); + SEND2(get_address{}, handle_send, _1, get_address::command); } // Protocol. // ---------------------------------------------------------------------------- -bool protocol_address::handle_receive_address(const code& ec, +bool protocol_address_31402::handle_receive_address(const code& ec, address::ptr message) { if (stopped()) @@ -96,7 +97,7 @@ bool protocol_address::handle_receive_address(const code& ec, return true; } -bool protocol_address::handle_receive_get_address(const code& ec, +bool protocol_address_31402::handle_receive_get_address(const code& ec, get_address::ptr message) { if (stopped()) @@ -128,7 +129,7 @@ bool protocol_address::handle_receive_get_address(const code& ec, return true; } -void protocol_address::handle_store_addresses(const code& ec) +void protocol_address_31402::handle_store_addresses(const code& ec) { if (stopped()) return; @@ -142,7 +143,7 @@ void protocol_address::handle_store_addresses(const code& ec) } } -void protocol_address::handle_stop(const code&) +void protocol_address_31402::handle_stop(const code&) { log::debug(LOG_NETWORK) << "Stopped addresss protocol"; diff --git a/src/protocols/protocol_ping_31402.cpp b/src/protocols/protocol_ping_31402.cpp new file mode 100644 index 000000000..11e567f8e --- /dev/null +++ b/src/protocols/protocol_ping_31402.cpp @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * libbitcoin is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License with + * additional permissions to the one published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. For more information see LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace network { + +#define NAME "protocol_ping_31402" +#define CLASS protocol_ping_31402 + +using namespace bc::message; +using namespace std::placeholders; + +protocol_ping_31402::protocol_ping_31402(p2p& network, channel::ptr channel) + : protocol_timer(network, channel, true, NAME), + settings_(network.network_settings()), + CONSTRUCT_TRACK(protocol_ping_31402) +{ +} + +void protocol_ping_31402::start() +{ + protocol_timer::start(settings_.channel_heartbeat(), BIND1(send_ping, _1)); + + SUBSCRIBE2(ping, handle_receive_ping, _1, _2); + + // Send initial ping message by simulating first heartbeat. + set_event(error::success); +} + +// This is fired by the callback (i.e. base timer and stop handler). +void protocol_ping_31402::send_ping(const code& ec) +{ + if (stopped()) + return; + + if (ec && ec != error::channel_timeout) + { + log::debug(LOG_NETWORK) + << "Failure in ping timer for [" << authority() << "] " + << ec.message(); + stop(ec); + return; + } + + SEND2(ping{}, handle_send, _1, pong::command); +} + +bool protocol_ping_31402::handle_receive_ping(const code& ec, + message::ping::ptr message) +{ + if (stopped()) + return false; + + if (ec) + { + log::debug(LOG_NETWORK) + << "Failure getting ping from [" << authority() << "] " + << ec.message(); + stop(ec); + return false; + } + + // RESUBSCRIBE + return true; +} + +} // namespace network +} // namespace libbitcoin diff --git a/src/protocols/protocol_ping.cpp b/src/protocols/protocol_ping_60001.cpp similarity index 78% rename from src/protocols/protocol_ping.cpp rename to src/protocols/protocol_ping_60001.cpp index adcd0d069..069bcee7d 100644 --- a/src/protocols/protocol_ping.cpp +++ b/src/protocols/protocol_ping_60001.cpp @@ -17,7 +17,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include +#include #include #include @@ -25,36 +25,26 @@ #include #include #include +#include #include namespace libbitcoin { namespace network { -#define NAME "ping" -#define CLASS protocol_ping +#define NAME "protocol_ping_60001" +#define CLASS protocol_ping_60001 using namespace bc::message; using namespace std::placeholders; -protocol_ping::protocol_ping(p2p& network, channel::ptr channel) - : protocol_timer(network, channel, true, NAME), - settings_(network.network_settings()), - CONSTRUCT_TRACK(protocol_ping) +protocol_ping_60001::protocol_ping_60001(p2p& network, channel::ptr channel) + : protocol_ping_31402(network, channel), + CONSTRUCT_TRACK(protocol_ping_60001) { } -void protocol_ping::start() -{ - protocol_timer::start(settings_.channel_heartbeat(), BIND1(send_ping, _1)); - - SUBSCRIBE2(ping, handle_receive_ping, _1, _2); - - // Send initial ping message by simulating first heartbeat. - set_event(error::success); -} - // This is fired by the callback (i.e. base timer and stop handler). -void protocol_ping::send_ping(const code& ec) +void protocol_ping_60001::send_ping(const code& ec) { if (stopped()) return; @@ -69,12 +59,11 @@ void protocol_ping::send_ping(const code& ec) } const auto nonce = pseudo_random(); - SUBSCRIBE3(pong, handle_receive_pong, _1, _2, nonce); SEND2(ping{ nonce }, handle_send, _1, pong::command); } -bool protocol_ping::handle_receive_ping(const code& ec, +bool protocol_ping_60001::handle_receive_ping(const code& ec, message::ping::ptr message) { if (stopped()) @@ -90,12 +79,10 @@ bool protocol_ping::handle_receive_ping(const code& ec, } SEND2(pong{ message->nonce }, handle_send, _1, pong::command); - - // RESUBSCRIBE return true; } -bool protocol_ping::handle_receive_pong(const code& ec, +bool protocol_ping_60001::handle_receive_pong(const code& ec, message::pong::ptr message, uint64_t nonce) { if (stopped()) diff --git a/src/protocols/protocol_seed.cpp b/src/protocols/protocol_seed_31402.cpp similarity index 84% rename from src/protocols/protocol_seed.cpp rename to src/protocols/protocol_seed_31402.cpp index 4bb8ab86c..c486c406a 100644 --- a/src/protocols/protocol_seed.cpp +++ b/src/protocols/protocol_seed_31402.cpp @@ -17,7 +17,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include +#include #include #include @@ -28,24 +28,24 @@ namespace libbitcoin { namespace network { -#define NAME "seed" -#define CLASS protocol_seed +#define NAME "protocol_seed_31402" +#define CLASS protocol_seed_31402 using namespace bc::message; using namespace std::placeholders; // Require three callbacks (or any error) before calling complete. -protocol_seed::protocol_seed(p2p& network, channel::ptr channel) +protocol_seed_31402::protocol_seed_31402(p2p& network, channel::ptr channel) : protocol_timer(network, channel, false, NAME), network_(network), - CONSTRUCT_TRACK(protocol_seed) + CONSTRUCT_TRACK(protocol_seed_31402) { } // Start sequence. // ---------------------------------------------------------------------------- -void protocol_seed::start(event_handler handler) +void protocol_seed_31402::start(event_handler handler) { const auto& settings = network_.network_settings(); @@ -68,7 +68,7 @@ void protocol_seed::start(event_handler handler) // Protocol. // ---------------------------------------------------------------------------- -void protocol_seed::send_own_address(const settings& settings) +void protocol_seed_31402::send_own_address(const settings& settings) { if (settings.self.port() == 0) { @@ -80,14 +80,14 @@ void protocol_seed::send_own_address(const settings& settings) SEND1(self, handle_send_address, _1); } -void protocol_seed::handle_seeding_complete(const code& ec, +void protocol_seed_31402::handle_seeding_complete(const code& ec, event_handler handler) { handler(ec); stop(ec); } -bool protocol_seed::handle_receive_address(const code& ec, +bool protocol_seed_31402::handle_receive_address(const code& ec, address::ptr message) { if (stopped()) @@ -112,7 +112,7 @@ bool protocol_seed::handle_receive_address(const code& ec, return false; } -void protocol_seed::handle_send_address(const code& ec) +void protocol_seed_31402::handle_send_address(const code& ec) { if (stopped()) return; @@ -130,7 +130,7 @@ void protocol_seed::handle_send_address(const code& ec) set_event(error::success); } -void protocol_seed::handle_send_get_address(const code& ec) +void protocol_seed_31402::handle_send_get_address(const code& ec) { if (stopped()) return; @@ -148,7 +148,7 @@ void protocol_seed::handle_send_get_address(const code& ec) set_event(error::success); } -void protocol_seed::handle_store_addresses(const code& ec) +void protocol_seed_31402::handle_store_addresses(const code& ec) { if (stopped()) return; diff --git a/src/protocols/protocol_version.cpp b/src/protocols/protocol_version_31402.cpp similarity index 88% rename from src/protocols/protocol_version.cpp rename to src/protocols/protocol_version_31402.cpp index 3faac637e..f8d3f9f29 100644 --- a/src/protocols/protocol_version.cpp +++ b/src/protocols/protocol_version_31402.cpp @@ -17,7 +17,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include +#include #include #include @@ -33,8 +33,8 @@ namespace libbitcoin { namespace network { -#define NAME "version" -#define CLASS protocol_version +#define NAME "protocol_version_31402" +#define CLASS protocol_version_31402 using namespace bc::message; using namespace std::placeholders; @@ -48,7 +48,7 @@ static uint64_t time_stamp() return wall_clock::to_time_t(now); } -message::version protocol_version::version_factory( +message::version protocol_version_31402::version_factory( const config::authority& authority, const settings& settings, uint64_t nonce, size_t height) { @@ -64,9 +64,6 @@ message::version protocol_version::version_factory( version.user_agent = BC_USER_AGENT; version.start_height = static_cast(height); - // This is not serialized below version::level::bip61. - version.relay = settings.relay_transactions; - // The peer's services cannot be reflected, so zero it. version.address_recevier.services = version::service::none; @@ -76,17 +73,18 @@ message::version protocol_version::version_factory( return version; } -protocol_version::protocol_version(p2p& network, channel::ptr channel) +protocol_version_31402::protocol_version_31402(p2p& network, + channel::ptr channel) : protocol_timer(network, channel, false, NAME), network_(network), - CONSTRUCT_TRACK(protocol_version) + CONSTRUCT_TRACK(protocol_version_31402) { } // Start sequence. // ---------------------------------------------------------------------------- -void protocol_version::start(event_handler handler) +void protocol_version_31402::start(event_handler handler) { const auto height = network_.height(); const auto& settings = network_.network_settings(); @@ -100,7 +98,7 @@ void protocol_version::start(event_handler handler) send_version(version_factory(authority(), settings, nonce(), height)); } -void protocol_version::send_version(const message::version& self) +void protocol_version_31402::send_version(const message::version& self) { SEND1(self, handle_version_sent, _1); } @@ -108,7 +106,7 @@ void protocol_version::send_version(const message::version& self) // Protocol. // ---------------------------------------------------------------------------- -bool protocol_version::handle_receive_version(const code& ec, +bool protocol_version_31402::handle_receive_version(const code& ec, version::ptr message) { if (stopped()) @@ -168,7 +166,7 @@ bool protocol_version::handle_receive_version(const code& ec, return false; } -bool protocol_version::handle_receive_verack(const code& ec, verack::ptr) +bool protocol_version_31402::handle_receive_verack(const code& ec, verack::ptr) { if (stopped()) return false; @@ -187,7 +185,7 @@ bool protocol_version::handle_receive_verack(const code& ec, verack::ptr) return false; } -void protocol_version::handle_version_sent(const code& ec) +void protocol_version_31402::handle_version_sent(const code& ec) { if (stopped()) return; @@ -202,7 +200,7 @@ void protocol_version::handle_version_sent(const code& ec) } } -void protocol_version::handle_verack_sent(const code& ec) +void protocol_version_31402::handle_verack_sent(const code& ec) { if (stopped()) return; diff --git a/src/protocols/protocol_version_70002.cpp b/src/protocols/protocol_version_70002.cpp new file mode 100644 index 000000000..2ffda411f --- /dev/null +++ b/src/protocols/protocol_version_70002.cpp @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * libbitcoin is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License with + * additional permissions to the one published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. For more information see LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace network { + +#define NAME "protocol_version_70002" +#define CLASS protocol_version_70002 + +using namespace bc::message; +using namespace std::placeholders; + +message::version protocol_version_70002::version_factory( + const config::authority& authority, const settings& settings, + uint64_t nonce, size_t height) +{ + auto version = protocol_version_31402::version_factory(authority, settings, + nonce, height); + + // This is the only difference at protocol level 70001. + version.relay = settings.relay_transactions; + + return version; +} + +protocol_version_70002::protocol_version_70002(p2p& network, + channel::ptr channel) + : protocol_version_31402(network, channel), + CONSTRUCT_TRACK(protocol_version_70002) +{ +} + +// Start sequence. +// ---------------------------------------------------------------------------- + +void protocol_version_70002::start(event_handler handler) +{ + const auto height = network_.height(); + const auto& settings = network_.network_settings(); + + // The handler is invoked in the context of the last message receipt. + protocol_timer::start(settings.channel_handshake(), + synchronize(handler, 2, NAME, false)); + + SUBSCRIBE2(version, handle_receive_version, _1, _2); + SUBSCRIBE2(verack, handle_receive_verack, _1, _2); + send_version(version_factory(authority(), settings, nonce(), height)); +} + +} // namespace network +} // namespace libbitcoin diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 02ab34aca..44067ebe6 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -30,9 +30,8 @@ #include #include #include -#include -#include -#include +#include +#include #include namespace libbitcoin { @@ -240,7 +239,10 @@ void session::handle_channel_start(const code& ec, channel::ptr channel, void session::attach_handshake_protocols(channel::ptr channel, result_handler handle_started) { - attach(channel)->start(handle_started); + if (settings_.protocol_maximum >= message::version::level::bip61) + attach(channel)->start(handle_started); + else + attach(channel)->start(handle_started); } void session::handle_handshake(const code& ec, channel::ptr channel, diff --git a/src/sessions/session_inbound.cpp b/src/sessions/session_inbound.cpp index 770b781b6..54a9946c9 100644 --- a/src/sessions/session_inbound.cpp +++ b/src/sessions/session_inbound.cpp @@ -24,8 +24,9 @@ #include #include #include -#include -#include +#include +#include +#include namespace libbitcoin { namespace network { @@ -165,8 +166,12 @@ void session_inbound::handle_channel_start(const code& ec, void session_inbound::attach_protocols(channel::ptr channel) { - attach(channel)->start(); - attach(channel)->start(); + if (settings_.protocol_maximum >= message::version::level::bip31) + attach(channel)->start(); + else + attach(channel)->start(); + + attach(channel)->start(); } void session_inbound::handle_channel_stop(const code& ec) diff --git a/src/sessions/session_manual.cpp b/src/sessions/session_manual.cpp index 812e49aee..a934b37dc 100644 --- a/src/sessions/session_manual.cpp +++ b/src/sessions/session_manual.cpp @@ -25,8 +25,9 @@ #include #include #include -#include -#include +#include +#include +#include namespace libbitcoin { namespace network { @@ -162,8 +163,12 @@ void session_manual::handle_channel_start(const code& ec, void session_manual::attach_protocols(channel::ptr channel) { - attach(channel)->start(); - attach(channel)->start(); + if (settings_.protocol_maximum >= message::version::level::bip31) + attach(channel)->start(); + else + attach(channel)->start(); + + attach(channel)->start(); } // After a stop we don't use the caller's start handler, but keep connecting. diff --git a/src/sessions/session_outbound.cpp b/src/sessions/session_outbound.cpp index d7215bee2..5e772c838 100644 --- a/src/sessions/session_outbound.cpp +++ b/src/sessions/session_outbound.cpp @@ -23,8 +23,9 @@ #include #include #include -#include -#include +#include +#include +#include namespace libbitcoin { namespace network { @@ -124,8 +125,12 @@ void session_outbound::handle_channel_start(const code& ec, void session_outbound::attach_protocols(channel::ptr channel) { - attach(channel)->start(); - attach(channel)->start(); + if (settings_.protocol_maximum >= message::version::level::bip31) + attach(channel)->start(); + else + attach(channel)->start(); + + attach(channel)->start(); } void session_outbound::handle_channel_stop(const code& ec, diff --git a/src/sessions/session_seed.cpp b/src/sessions/session_seed.cpp index 674d93679..a50e4b4da 100644 --- a/src/sessions/session_seed.cpp +++ b/src/sessions/session_seed.cpp @@ -24,8 +24,9 @@ #include #include #include -#include -#include +#include +#include +#include #include namespace libbitcoin { @@ -170,8 +171,12 @@ void session_seed::handle_channel_start(const code& ec, channel::ptr channel, void session_seed::attach_protocols(channel::ptr channel, result_handler handler) { - attach(channel)->start(); - attach(channel)->start(handler); + if (settings_.protocol_maximum >= message::version::level::bip31) + attach(channel)->start(); + else + attach(channel)->start(); + + attach(channel)->start(handler); } void session_seed::handle_channel_stop(const code& ec)