diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index 86a72b633..3bde6c745 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -103,12 +103,12 @@ class BCT_API socket protected: // These are thread safe. - const config::address address_; - std::atomic_bool stopped_{}; asio::strand strand_; + std::atomic_bool stopped_{}; // These are protected by strand (see also handle_accept). asio::socket socket_; + config::address address_; config::authority authority_{}; private: diff --git a/src/net/socket.cpp b/src/net/socket.cpp index 27adefcdb..c1c1b8429 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -50,9 +50,9 @@ socket::socket(const logger& log, asio::io_context& service) NOEXCEPT // authority_.port() nonzero implies outbound connection. socket::socket(const logger& log, asio::io_context& service, const config::address& address) NOEXCEPT - : address_(address), - strand_(service.get_executor()), + : strand_(service.get_executor()), socket_(strand_), + address_(address), reporter(log), tracker(log) { @@ -228,8 +228,10 @@ void socket::handle_accept(const error::boost_code& ec, { // This is running in the acceptor (not socket) execution context. // socket_ and authority_ are not guarded here, see comments on accept. + // address_ remains defaulted for inbound (accepted) connections. - if (!ec) authority_ = { socket_.remote_endpoint() }; + if (!ec) + authority_ = { socket_.remote_endpoint() }; if (error::asio_is_canceled(ec)) { @@ -256,6 +258,10 @@ void socket::handle_connect(const error::boost_code& ec, authority_ = peer; + // Outgoing connection requires address_ for .inbound() resolution. + if (is_zero(address_.port())) + address_ = config::endpoint{ peer }.to_address(); + if (error::asio_is_canceled(ec)) { handler(error::operation_canceled); diff --git a/src/sessions/session_inbound.cpp b/src/sessions/session_inbound.cpp index 6009536de..2d8083206 100644 --- a/src/sessions/session_inbound.cpp +++ b/src/sessions/session_inbound.cpp @@ -213,6 +213,10 @@ void session_inbound::attach_handshake(const channel::ptr& channel, const auto reject = settings().enable_reject; const auto address_v2 = settings().enable_address_v2; + // protocol_version_70016 sends and receives send_address_v2 even though + // inbound connections do not accept addresses. There is no message to + // disable address broadcasting, so this is just allowed to upgrade. + // Address v2 can be disabled, independent of version. if (is_configured(messages::level::bip155) && address_v2) channel->attach(self, minimum_services, @@ -248,7 +252,42 @@ void session_inbound::handle_channel_start(const code&, void session_inbound::attach_protocols( const channel::ptr& channel) NOEXCEPT { - session::attach_protocols(channel); + BC_ASSERT_MSG(channel->stranded(), "channel strand"); + BC_ASSERT_MSG(channel->paused(), "channel not paused for protocol attach"); + + const auto self = shared_from_this(); + + // Alert is deprecated, independent of version. + if (channel->is_negotiated(messages::level::alert_message) && + settings().enable_alert) + channel->attach(self)->start(); + + // Reject is deprecated, independent of version. + if (channel->is_negotiated(messages::level::bip61) && + settings().enable_reject) + channel->attach(self)->start(); + + if (channel->is_negotiated(messages::level::bip31)) + channel->attach(self)->start(); + else if (channel->is_negotiated(messages::level::version_message)) + channel->attach(self)->start(); + + // Attach is overridden to disable inbound address protocols. + + if (settings().enable_address_v2) + { + ////// Sending address v2 is enabled in handshake. + ////if (channel->send_address_v2()) + //// channel->attach(self)->start(); + } + + if (settings().enable_address) + { + if (channel->is_negotiated(messages::level::getaddr_message)) + channel->attach(self)->start(); + ////else if (channel->is_negotiated(messages::level::version_message)) + //// channel->attach(self)->start(); + } } void session_inbound::handle_channel_stop(const code& LOG_ONLY(ec),