From 01f808d312b93e7a59b7bc2ec7e20aa33244d887 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 5 Jan 2026 20:54:33 -0500 Subject: [PATCH 1/5] Remove unnecessary member. --- include/bitcoin/network/net/connector_socks.hpp | 1 - src/net/connector_socks.cpp | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/bitcoin/network/net/connector_socks.hpp b/include/bitcoin/network/net/connector_socks.hpp index a42af6703..f6c1abb06 100644 --- a/include/bitcoin/network/net/connector_socks.hpp +++ b/include/bitcoin/network/net/connector_socks.hpp @@ -108,7 +108,6 @@ class BCT_API connector_socks // These are thread safe. const settings::socks5& socks5_; const uint8_t method_; - const bool proxied_; }; typedef std_vector socks_connectors; diff --git a/src/net/connector_socks.cpp b/src/net/connector_socks.cpp index 3f5f4f61b..56dddb7ee 100644 --- a/src/net/connector_socks.cpp +++ b/src/net/connector_socks.cpp @@ -100,14 +100,13 @@ code connector_socks::socks_response(uint8_t value) NOEXCEPT }; } -// Caller can avoid proxied_ condition by using connector when not proxied. +// Caller can avoid proxied() condition by using connector when not proxied. connector_socks::connector_socks(const logger& log, asio::strand& strand, asio::io_context& service, const steady_clock::duration& timeout, size_t maximum_request, std::atomic_bool& suspended, const settings::socks5& socks) NOEXCEPT : connector(log, strand, service, timeout, maximum_request, suspended), socks5_(socks), - proxied_(socks.proxied()), method_(socks.authenticated() ? socks::method_basic : socks::method_clear), tracker(log) { @@ -116,7 +115,7 @@ connector_socks::connector_socks(const logger& log, asio::strand& strand, // protected/override bool connector_socks::proxied() const NOEXCEPT { - return proxied_; + return socks5_.proxied(); } // protected/override From 76d78308b84e21dfe0e5895591103a6d3b2342be Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 5 Jan 2026 21:06:08 -0500 Subject: [PATCH 2/5] Simplify connector_socks parameter passing. --- .../bitcoin/network/net/connector_socks.hpp | 41 +++---- src/net/connector_socks.cpp | 107 +++++++++--------- 2 files changed, 68 insertions(+), 80 deletions(-) diff --git a/include/bitcoin/network/net/connector_socks.hpp b/include/bitcoin/network/net/connector_socks.hpp index f6c1abb06..096b5b756 100644 --- a/include/bitcoin/network/net/connector_socks.hpp +++ b/include/bitcoin/network/net/connector_socks.hpp @@ -68,46 +68,39 @@ class BCT_API connector_socks using data_cptr = std::shared_ptr>; // socks5 handshake - void do_socks_greeting_write(const code& ec, const finish_ptr& finish, + void do_socks_greeting_write(const code& ec, const socket::ptr& socket) NOEXCEPT; void handle_socks_greeting_write(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_cptr<3>& greeting) NOEXCEPT; + const socket::ptr& socket, const data_cptr<3>& greeting) NOEXCEPT; void handle_socks_method_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<2>& response) NOEXCEPT; - void handle_socks_authentication_write(const code& ec, - size_t size, const finish_ptr& finish, const socket::ptr& socket, + const socket::ptr& socket, const data_ptr<2>& response) NOEXCEPT; + void handle_socks_authentication_write(const code& ec, size_t size, + const socket::ptr& socket, const system::chunk_ptr& authenticator) NOEXCEPT; - void handle_socks_authentication_read(const code& ec, - size_t size, const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<2>& response) NOEXCEPT; + void handle_socks_authentication_read(const code& ec, size_t size, + const socket::ptr& socket, const data_ptr<2>& response) NOEXCEPT; - void do_socks_connect_write(const finish_ptr& finish, - const socket::ptr& socket) NOEXCEPT; + void do_socks_connect_write(const socket::ptr& socket) NOEXCEPT; void handle_socks_connect_write(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const system::chunk_ptr& request) NOEXCEPT; + const socket::ptr& socket, const system::chunk_ptr& request) NOEXCEPT; void handle_socks_response_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<4>& response) NOEXCEPT; + const socket::ptr& socket, const data_ptr<4>& response) NOEXCEPT; void handle_socks_length_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<1>& host_length) NOEXCEPT; + const socket::ptr& socket, const data_ptr<1>& host_length) NOEXCEPT; void handle_socks_address_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const system::chunk_ptr& address) NOEXCEPT; + const socket::ptr& socket, const system::chunk_ptr& address) NOEXCEPT; - void do_socks_finish(const code& ec, const finish_ptr& finish, - const socket::ptr& socket) NOEXCEPT; - void socks_finish(const code& ec, const finish_ptr& finish, - const socket::ptr& socket) NOEXCEPT; + void do_socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT; + void socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT; // These are thread safe. const settings::socks5& socks5_; const uint8_t method_; + + // This is protected by mutex. + finish_ptr finish_{}; }; typedef std_vector socks_connectors; diff --git a/src/net/connector_socks.cpp b/src/net/connector_socks.cpp index 56dddb7ee..56e6deb4a 100644 --- a/src/net/connector_socks.cpp +++ b/src/net/connector_socks.cpp @@ -140,7 +140,8 @@ void connector_socks::handle_connect(const code& ec, const finish_ptr& finish, if (proxied()) { - do_socks_greeting_write(ec, finish, socket); + finish_ = finish; + do_socks_greeting_write(ec, socket); return; } @@ -153,13 +154,13 @@ void connector_socks::handle_connect(const code& ec, const finish_ptr& finish, // datatracker.ietf.org/doc/html/rfc1929 (basic authentication) void connector_socks::do_socks_greeting_write(const code& ec, - const finish_ptr& finish, const socket::ptr& socket) NOEXCEPT + const socket::ptr& socket) NOEXCEPT { BC_ASSERT(stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - socks_finish(result, finish, socket); + socks_finish(result, socket); return; } @@ -179,24 +180,23 @@ void connector_socks::do_socks_greeting_write(const code& ec, socket->write({ greeting->data(), greeting->size() }, std::bind(&connector_socks::handle_socks_greeting_write, shared_from_base(), - _1, _2, finish, socket, greeting)); + _1, _2, socket, greeting)); } void connector_socks::handle_socks_greeting_write(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_cptr<3>& greeting) NOEXCEPT + const socket::ptr& socket, const data_cptr<3>& greeting) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } if (size != sizeof(*greeting)) { - do_socks_finish(error::operation_failed, finish, socket); + do_socks_finish(error::operation_failed, socket); return; } @@ -205,18 +205,17 @@ void connector_socks::handle_socks_greeting_write(const code& ec, size_t size, socket->read({ response->data(), response->size() }, std::bind(&connector_socks::handle_socks_method_read, shared_from_base(), - _1, _2, finish, socket, response)); + _1, _2, socket, response)); } void connector_socks::handle_socks_method_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<2>& response) NOEXCEPT + const socket::ptr& socket, const data_ptr<2>& response) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } @@ -229,14 +228,14 @@ void connector_socks::handle_socks_method_read(const code& ec, size_t size, response->at(0) != socks::version || response->at(1) != method_) { - do_socks_finish(error::socks_method, finish, socket); + do_socks_finish(error::socks_method, socket); return; } // Bypass authentication. if (method_ == socks::method_clear) { - do_socks_connect_write(finish, socket); + do_socks_connect_write(socket); return; } @@ -246,7 +245,7 @@ void connector_socks::handle_socks_method_read(const code& ec, size_t size, // Socks5 limits valid username length to one byte. if (is_limited(username_length)) { - do_socks_finish(error::socks_username, finish, socket); + do_socks_finish(error::socks_username, socket); return; } @@ -256,7 +255,7 @@ void connector_socks::handle_socks_method_read(const code& ec, size_t size, // Socks5 limits valid password length to one byte. if (is_limited(password_length)) { - do_socks_finish(error::socks_password, finish, socket); + do_socks_finish(error::socks_password, socket); return; } @@ -277,24 +276,24 @@ void connector_socks::handle_socks_method_read(const code& ec, size_t size, socket->write({ authenticator->data(), authenticator->size() }, std::bind(&connector_socks::handle_socks_authentication_write, shared_from_base(), - _1, _2, finish, socket, authenticator)); + _1, _2, socket, authenticator)); } void connector_socks::handle_socks_authentication_write(const code& ec, - size_t size, const finish_ptr& finish, const socket::ptr& socket, + size_t size, const socket::ptr& socket, const chunk_ptr& authenticator) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } if (size != authenticator->size()) { - do_socks_finish(error::operation_failed, finish, socket); + do_socks_finish(error::operation_failed, socket); return; } @@ -303,18 +302,18 @@ void connector_socks::handle_socks_authentication_write(const code& ec, socket->read({ auth_res->data(), auth_res->size() }, std::bind(&connector_socks::handle_socks_authentication_read, shared_from_base(), - _1, _2, finish, socket, auth_res)); + _1, _2, socket, auth_res)); } void connector_socks::handle_socks_authentication_read(const code& ec, - size_t size, const finish_ptr& finish, const socket::ptr& socket, + size_t size, const socket::ptr& socket, const data_ptr<2>& response) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } @@ -327,11 +326,11 @@ void connector_socks::handle_socks_authentication_read(const code& ec, response->at(0) != socks::method_basic_version || response->at(1) != socks::method_basic_success) { - do_socks_finish(error::socks_authentication, finish, socket); + do_socks_finish(error::socks_authentication, socket); return; } - do_socks_connect_write(finish, socket); + do_socks_connect_write(socket); } // A DNS name (e.g. manual endpont) works when not proxied, as the name is @@ -344,7 +343,7 @@ void connector_socks::handle_socks_authentication_read(const code& ec, // is presently passed via socket->address(), which results in a default // address (due to failed lexical conversion from name to address in socket). // This also prevents seeds from resolving via a proxy. -void connector_socks::do_socks_connect_write(const finish_ptr& finish, +void connector_socks::do_socks_connect_write( const socket::ptr& socket) NOEXCEPT { BC_ASSERT(socket->stranded()); @@ -356,7 +355,7 @@ void connector_socks::do_socks_connect_write(const finish_ptr& finish, // Socks5 limits valid host lengths to one byte. if (is_limited(host_length)) { - do_socks_finish(error::socks_server_name, finish, socket); + do_socks_finish(error::socks_server_name, socket); return; } @@ -379,24 +378,23 @@ void connector_socks::do_socks_connect_write(const finish_ptr& finish, socket->write({ request->data(), request->size() }, std::bind(&connector_socks::handle_socks_connect_write, shared_from_base(), - _1, _2, finish, socket, request)); + _1, _2, socket, request)); } void connector_socks::handle_socks_connect_write(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const chunk_ptr& request) NOEXCEPT + const socket::ptr& socket, const chunk_ptr& request) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } if (size != request->size()) { - do_socks_finish(error::operation_failed, finish, socket); + do_socks_finish(error::operation_failed, socket); return; } @@ -405,18 +403,17 @@ void connector_socks::handle_socks_connect_write(const code& ec, size_t size, socket->read({ response->data(), response->size() }, std::bind(&connector_socks::handle_socks_response_read, shared_from_base(), - _1, _2, finish, socket, response)); + _1, _2, socket, response)); } void connector_socks::handle_socks_response_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<4>& response) NOEXCEPT + const socket::ptr& socket, const data_ptr<4>& response) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } @@ -429,14 +426,14 @@ void connector_socks::handle_socks_response_read(const code& ec, size_t size, response->at(0) != socks::version || response->at(2) != socks::reserved) { - do_socks_finish(error::socks_response_invalid, finish, socket); + do_socks_finish(error::socks_response_invalid, socket); return; } // Map response code to error code. if (const auto code = socks_response(response->at(1))) { - do_socks_finish(code, finish, socket); + do_socks_finish(code, socket); return; } @@ -450,7 +447,7 @@ void connector_socks::handle_socks_response_read(const code& ec, size_t size, socket->read({ address->data(), address->size() }, std::bind(&connector_socks::handle_socks_address_read, shared_from_base(), - _1, _2, finish, socket, address)); + _1, _2, socket, address)); return; } case socks::address_ipv6: @@ -461,7 +458,7 @@ void connector_socks::handle_socks_response_read(const code& ec, size_t size, socket->read({ address->data(), address->size() }, std::bind(&connector_socks::handle_socks_address_read, shared_from_base(), - _1, _2, finish, socket, address)); + _1, _2, socket, address)); return; } case socks::address_fqdn: @@ -474,33 +471,32 @@ void connector_socks::handle_socks_response_read(const code& ec, size_t size, socket->read({ length->data(), sizeof(uint8_t) }, std::bind(&connector_socks::handle_socks_length_read, shared_from_base(), - _1, _2, finish, socket, length)); + _1, _2, socket, length)); return; } default: { // There are no other types defined. - do_socks_finish(error::socks_response_invalid, finish, socket); + do_socks_finish(error::socks_response_invalid, socket); return; } } } void connector_socks::handle_socks_length_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const data_ptr<1>& host_length) NOEXCEPT + const socket::ptr& socket, const data_ptr<1>& host_length) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } if (size != sizeof(*host_length)) { - do_socks_finish(error::socks_response_invalid, finish, socket); + do_socks_finish(error::socks_response_invalid, socket); return; } @@ -510,18 +506,17 @@ void connector_socks::handle_socks_length_read(const code& ec, size_t size, socket->read({ address->data(), address->size() }, std::bind(&connector_socks::handle_socks_address_read, shared_from_base(), - _1, _2, finish, socket, address)); + _1, _2, socket, address)); } void connector_socks::handle_socks_address_read(const code& ec, size_t size, - const finish_ptr& finish, const socket::ptr& socket, - const chunk_ptr& address) NOEXCEPT + const socket::ptr& socket, const chunk_ptr& address) NOEXCEPT { BC_ASSERT(socket->stranded()); if (const auto result = (socket->stopped() ? error::channel_stopped : ec)) { - do_socks_finish(result, finish, socket); + do_socks_finish(result, socket); return; } @@ -532,16 +527,16 @@ void connector_socks::handle_socks_address_read(const code& ec, size_t size, // +----------+----------+ if (size != address->size()) { - do_socks_finish(error::socks_response_invalid, finish, socket); + do_socks_finish(error::socks_response_invalid, socket); return; } // The address:port is the local binding by the socks5 server (unused). // The outbound address_/authority_ members are set by connect(). - do_socks_finish(error::success, finish, socket); + do_socks_finish(error::success, socket); } -void connector_socks::do_socks_finish(const code& ec, const finish_ptr& finish, +void connector_socks::do_socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT { BC_ASSERT(socket->stranded()); @@ -549,14 +544,14 @@ void connector_socks::do_socks_finish(const code& ec, const finish_ptr& finish, // End of socket strand sequence. boost::asio::post(strand_, std::bind(&connector_socks::socks_finish, - shared_from_base(), ec, finish, socket)); + shared_from_base(), ec, socket)); } -void connector_socks::socks_finish(const code& ec, const finish_ptr& finish, +void connector_socks::socks_finish(const code& ec, const socket::ptr& socket) NOEXCEPT { BC_ASSERT(stranded()); - connector::handle_connect(ec, finish, socket); + connector::handle_connect(ec, finish_, socket); } BC_POP_WARNING() From 6dbd6188dbe54213a8aa5c7915ce8ff4a1f3a340 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 5 Jan 2026 21:43:12 -0500 Subject: [PATCH 3/5] Rename error::socks_server_name (host not server). --- include/bitcoin/network/error.hpp | 2 +- src/error.cpp | 2 +- src/net/connector_socks.cpp | 2 +- test/error.cpp | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bitcoin/network/error.hpp b/include/bitcoin/network/error.hpp index 0cc468e57..05664177a 100644 --- a/include/bitcoin/network/error.hpp +++ b/include/bitcoin/network/error.hpp @@ -130,7 +130,7 @@ enum error_t : uint8_t socks_method, socks_username, socks_password, - socks_server_name, + socks_host_name, socks_authentication, socks_failure, socks_disallowed, diff --git a/src/error.cpp b/src/error.cpp index fff2305fc..4d66788c9 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -103,7 +103,7 @@ DEFINE_ERROR_T_MESSAGE_MAP(error) { socks_method, "socks method not supported" }, { socks_username, "socks username too long" }, { socks_password, "socks password too long" }, - { socks_server_name, "socks server name too long" }, + { socks_host_name, "socks host name too long" }, { socks_authentication, "socks authentication failed" }, { socks_failure, "socks failure" }, { socks_disallowed, "socks connection disallowed" }, diff --git a/src/net/connector_socks.cpp b/src/net/connector_socks.cpp index 56e6deb4a..91607f1dd 100644 --- a/src/net/connector_socks.cpp +++ b/src/net/connector_socks.cpp @@ -355,7 +355,7 @@ void connector_socks::do_socks_connect_write( // Socks5 limits valid host lengths to one byte. if (is_limited(host_length)) { - do_socks_finish(error::socks_server_name, socket); + do_socks_finish(error::socks_host_name, socket); return; } diff --git a/test/error.cpp b/test/error.cpp index 3e1093f08..a2cc2ee70 100644 --- a/test/error.cpp +++ b/test/error.cpp @@ -529,13 +529,13 @@ BOOST_AUTO_TEST_CASE(error_t__code__socks_password__true_expected_message) BOOST_REQUIRE_EQUAL(ec.message(), "socks password too long"); } -BOOST_AUTO_TEST_CASE(error_t__code__socks_server_name__true_expected_message) +BOOST_AUTO_TEST_CASE(error_t__code__socks_host_name__true_expected_message) { - constexpr auto value = error::socks_server_name; + constexpr auto value = error::socks_host_name; const auto ec = code(value); BOOST_REQUIRE(ec); BOOST_REQUIRE(ec == value); - BOOST_REQUIRE_EQUAL(ec.message(), "socks server name too long"); + BOOST_REQUIRE_EQUAL(ec.message(), "socks host name too long"); } BOOST_AUTO_TEST_CASE(error_t__code__socks_authentication__true_expected_message) From fa33283c221372bbf89a7a47137c35e744d73a3d Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sat, 10 Jan 2026 23:00:11 -0500 Subject: [PATCH 4/5] Refactor proxy/connector to use endpoint vs. authority. --- include/bitcoin/network/config/endpoint.hpp | 1 + .../network/impl/channels/channel_rpc.ipp | 4 +- include/bitcoin/network/net/connector.hpp | 9 +-- .../bitcoin/network/net/connector_socks.hpp | 5 +- include/bitcoin/network/net/hosts.hpp | 16 ++-- include/bitcoin/network/net/proxy.hpp | 8 +- include/bitcoin/network/net/socket.hpp | 24 ++++-- .../bitcoin/network/protocols/protocol.hpp | 8 +- src/channels/channel.cpp | 4 +- src/channels/channel_http.cpp | 8 +- src/channels/channel_peer.cpp | 24 +++--- src/channels/channel_ws.cpp | 10 +-- src/config/endpoint.cpp | 5 ++ src/net.cpp | 12 +-- src/net/connector.cpp | 21 +++--- src/net/connector_socks.cpp | 73 +++++++++++++------ src/net/hosts.cpp | 22 +++--- src/net/proxy.cpp | 20 ++--- src/net/socket.cpp | 57 +++++++-------- src/protocols/protocol.cpp | 4 +- src/protocols/protocol_address_in_209.cpp | 10 +-- src/protocols/protocol_address_out_209.cpp | 12 +-- src/protocols/protocol_alert_311.cpp | 2 +- src/protocols/protocol_ping_60001.cpp | 2 +- src/protocols/protocol_reject_70002.cpp | 4 +- src/protocols/protocol_seed_209.cpp | 4 +- src/protocols/protocol_version_106.cpp | 20 ++--- src/protocols/protocol_version_70002.cpp | 2 +- src/sessions/session_inbound.cpp | 18 ++--- src/sessions/session_outbound.cpp | 4 +- src/sessions/session_seed.cpp | 4 +- src/sessions/session_server.cpp | 18 ++--- test/net/proxy.cpp | 4 +- test/net/socket.cpp | 10 +-- test/protocols/protocol.cpp | 6 +- test/sessions/session_outbound.cpp | 14 ++-- test/sessions/session_seed.cpp | 12 +-- 37 files changed, 258 insertions(+), 223 deletions(-) diff --git a/include/bitcoin/network/config/endpoint.hpp b/include/bitcoin/network/config/endpoint.hpp index 8359eb2f4..30840f36e 100644 --- a/include/bitcoin/network/config/endpoint.hpp +++ b/include/bitcoin/network/config/endpoint.hpp @@ -38,6 +38,7 @@ class BCT_API endpoint DEFAULT_COPY_MOVE_DESTRUCT(endpoint); using system::config::endpoint::endpoint; + endpoint(const address& address) NOEXCEPT; /// Properties. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/network/impl/channels/channel_rpc.ipp b/include/bitcoin/network/impl/channels/channel_rpc.ipp index 5382326bb..9d7c47e79 100644 --- a/include/bitcoin/network/impl/channels/channel_rpc.ipp +++ b/include/bitcoin/network/impl/channels/channel_rpc.ipp @@ -84,7 +84,7 @@ inline void CLASS::handle_receive(const code& ec, size_t bytes, if (stopped()) { - LOGQ("Rpc read abort [" << authority() << "]"); + LOGQ("Rpc read abort [" << endpoint() << "]"); return; } @@ -93,7 +93,7 @@ inline void CLASS::handle_receive(const code& ec, size_t bytes, // Don't log common conditions. if (ec != error::end_of_stream && ec != error::operation_canceled) { - LOGF("Rpc read failure [" << authority() << "] " + LOGF("Rpc read failure [" << endpoint() << "] " << ec.message()); } diff --git a/include/bitcoin/network/net/connector.hpp b/include/bitcoin/network/net/connector.hpp index 051526f92..9c1af8ff2 100644 --- a/include/bitcoin/network/net/connector.hpp +++ b/include/bitcoin/network/net/connector.hpp @@ -67,11 +67,7 @@ class BCT_API connector /// The socket parameter is nullptr unless success is returned. /// Try to connect to the address, starts timer. - virtual void connect(const config::address& host, - socket_handler&& handler) NOEXCEPT; - - /// Try to connect to the authority, starts timer. - virtual void connect(const config::authority& host, + virtual void connect(const config::address& address, socket_handler&& handler) NOEXCEPT; /// Try to connect to the endpoint, starts timer. @@ -84,7 +80,8 @@ class BCT_API connector /// Try to connect to host:port, starts timer. virtual void start(const std::string& hostname, uint16_t port, - const config::address& host, socket_handler&& handler) NOEXCEPT; + const config::address& address, const config::endpoint& endpoint, + socket_handler&& handler) NOEXCEPT; /// Handlers, overridable for proxied connector. virtual void handle_connect(const code& ec, const finish_ptr& finish, diff --git a/include/bitcoin/network/net/connector_socks.hpp b/include/bitcoin/network/net/connector_socks.hpp index 096b5b756..1e58bcf7b 100644 --- a/include/bitcoin/network/net/connector_socks.hpp +++ b/include/bitcoin/network/net/connector_socks.hpp @@ -57,7 +57,8 @@ class BCT_API connector_socks /// Connector overrides. bool proxied() const NOEXCEPT override; void start(const std::string& hostname, uint16_t port, - const config::address& host, socket_handler&& handler) NOEXCEPT override; + const config::address& address, const config::endpoint& endpoint, + socket_handler&& handler) NOEXCEPT override; void handle_connect(const code& ec, const finish_ptr& finish, const socket::ptr& socket) NOEXCEPT override; @@ -67,6 +68,8 @@ class BCT_API connector_socks template using data_cptr = std::shared_ptr>; + // utility + // socks5 handshake void do_socks_greeting_write(const code& ec, const socket::ptr& socket) NOEXCEPT; diff --git a/include/bitcoin/network/net/hosts.hpp b/include/bitcoin/network/net/hosts.hpp index e7ff610fd..3a12def0c 100644 --- a/include/bitcoin/network/net/hosts.hpp +++ b/include/bitcoin/network/net/hosts.hpp @@ -92,11 +92,11 @@ class BCT_API hosts /// Reservation. /// ----------------------------------------------------------------------- - /// Reserve the address (currently connected), false if was reserved. - virtual bool reserve(const config::authority& host) NOEXCEPT; + /// Reserve the endpoint (currently connected), false if was reserved. + virtual bool reserve(const config::endpoint& host) NOEXCEPT; - /// Unreserve the address (no longer connected), false if was not reserved. - virtual bool unreserve(const config::authority& host) NOEXCEPT; + /// Unreserve the endpoint (no longer connected), false if was not reserved. + virtual bool unreserve(const config::endpoint& host) NOEXCEPT; private: typedef boost::circular_buffer buffer; @@ -120,7 +120,7 @@ class BCT_API hosts // Inlines local to translation unit. inline messages::peer::address_item::cptr pop() NOEXCEPT; inline void push(const std::string& line) NOEXCEPT; - inline bool is_reserved(const config::authority& host) const NOEXCEPT; + inline bool is_reserved(const config::endpoint& host) const NOEXCEPT; void do_take(const address_item_handler& handler) NOEXCEPT; void do_restore(const address_item_cptr& host, @@ -132,14 +132,12 @@ class BCT_API hosts // These are thread safe. const settings& settings_; std::atomic hosts_count_{}; - std::atomic authorities_count_{}; + std::atomic endpoints_count_{}; // These are not thread safe. buffer buffer_; bool stopped_{ true }; - - // TODO: optimize, default bucket count is around 8. - std::unordered_set authorities_{}; + std::unordered_set endpoints_{}; }; } // namespace network diff --git a/include/bitcoin/network/net/proxy.hpp b/include/bitcoin/network/net/proxy.hpp index 05bb9f1df..a777b0963 100644 --- a/include/bitcoin/network/net/proxy.hpp +++ b/include/bitcoin/network/net/proxy.hpp @@ -91,12 +91,12 @@ class BCT_API proxy /// The socket was accepted (vs. connected). bool inbound() const NOEXCEPT; - /// Get the authority (incoming) of the remote endpoint. - const config::authority& authority() const NOEXCEPT; - - /// Get the address (outgoing) of the remote endpoint. + /// Get the address of the outgoing endpoint passed via construct. const config::address& address() const NOEXCEPT; + /// Get the endpoint of the remote host. + const config::endpoint& endpoint() const NOEXCEPT; + protected: proxy(const socket::ptr& socket) NOEXCEPT; diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index 0b8545ddc..7a0081921 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -43,14 +43,15 @@ class BCT_API socket DELETE_COPY_MOVE(socket); - /// Use only for incoming connections (defaults outgoing address). + /// Use only for incoming connections. socket(const logger& log, asio::io_context& service, size_t maximum_request) NOEXCEPT; - /// Use only for outgoing connections (retains outgoing address). + /// Use only for outgoing connections. Endpoint represents the peer or + /// client (non-proxy) that the connector attempted to reach. socket(const logger& log, asio::io_context& service, size_t maximum_request, const config::address& address, - bool proxied=false) NOEXCEPT; + const config::endpoint& endpoint, bool proxied=false) NOEXCEPT; /// Asserts/logs stopped. virtual ~socket() NOEXCEPT; @@ -142,13 +143,15 @@ class BCT_API socket /// Properties. /// ----------------------------------------------------------------------- - /// Get the authority (outgoing/incoming) of the remote endpoint. - virtual const config::authority& authority() const NOEXCEPT; - /// TODO: this can be set to the binding for incoming sockets. - /// Get the address (outgoing) of the remote endpoint. + /// Get the address of the outgoing endpoint passed via construct. virtual const config::address& address() const NOEXCEPT; + /// Get the endpoint of the remote host. Established by connection + /// resolution for incoming and non-proxied outgoing. For a proxied + /// connection (outgoing only) this is the value passed via construct. + virtual const config::endpoint& endpoint() const NOEXCEPT; + /// The socket was accepted (vs. connected). virtual bool inbound() const NOEXCEPT; @@ -287,7 +290,12 @@ class BCT_API socket const count_handler& handler) NOEXCEPT; protected: + socket(const logger& log, asio::io_context& service, + size_t maximum_request, const config::address& address, + const config::endpoint& endpoint, bool proxied, bool inbound) NOEXCEPT; + // These are thread safe. + const bool inbound_; const bool proxied_; const size_t maximum_; asio::strand strand_; @@ -297,7 +305,7 @@ class BCT_API socket // These are protected by strand (see also handle_accept). asio::socket socket_; config::address address_; - config::authority authority_{}; + config::endpoint endpoint_; std::optional websocket_{}; }; diff --git a/include/bitcoin/network/protocols/protocol.hpp b/include/bitcoin/network/protocols/protocol.hpp index 0a983552c..1f67c2094 100644 --- a/include/bitcoin/network/protocols/protocol.hpp +++ b/include/bitcoin/network/protocols/protocol.hpp @@ -124,7 +124,7 @@ class BCT_API protocol session_->unsubscribe(channel_->identifier()); } - /// Broadcast a message instance to peers (use BROADCAST). + /// Broadcast a message instance to channels (use BROADCAST). /// Channel identifier allows recipient-sender to self-identify. template inline void broadcast(const typename Message::cptr& message) NOEXCEPT @@ -172,10 +172,10 @@ class BCT_API protocol /// The current thread is on the channel strand. virtual bool stranded() const NOEXCEPT; - /// The authority of the peer. - virtual config::authority authority() const NOEXCEPT; + /// The opposite endpoint of the channel. + virtual config::endpoint opposite() const NOEXCEPT; - /// The outbound address of the peer. + /// The outbound address of the channel. virtual const config::address& outbound() const NOEXCEPT; /// The nonce of the channel. diff --git a/src/channels/channel.cpp b/src/channels/channel.cpp index c00bc5455..00ce79859 100644 --- a/src/channels/channel.cpp +++ b/src/channels/channel.cpp @@ -159,7 +159,7 @@ void channel::handle_expiration(const code& ec) NOEXCEPT if (ec) { - LOGF("Lifetime timer fail [" << authority() << "] " << ec.message()); + LOGF("Lifetime timer fail [" << endpoint() << "] " << ec.message()); stop(ec); return; } @@ -198,7 +198,7 @@ void channel::handle_inactivity(const code& ec) NOEXCEPT if (ec) { - LOGF("Inactivity timer fail [" << authority() << "] " << ec.message()); + LOGF("Inactivity timer fail [" << endpoint() << "] " << ec.message()); stop(ec); return; } diff --git a/src/channels/channel_http.cpp b/src/channels/channel_http.cpp index cce6822f4..4b77cfaba 100644 --- a/src/channels/channel_http.cpp +++ b/src/channels/channel_http.cpp @@ -92,7 +92,7 @@ void channel_http::handle_receive(const code& ec, size_t bytes, if (stopped()) { - LOGQ("Http read abort [" << authority() << "]"); + LOGQ("Http read abort [" << endpoint() << "]"); return; } @@ -101,7 +101,7 @@ void channel_http::handle_receive(const code& ec, size_t bytes, // Don't log common conditions. if (ec != error::end_of_stream && ec != error::operation_canceled) { - LOGF("Http read failure [" << authority() << "] " + LOGF("Http read failure [" << endpoint() << "] " << ec.message()); } @@ -199,7 +199,7 @@ void channel_http::log_message(const request& LOG_ONLY(request), << "] " << version << " (" << (request.chunked() ? "c" : serialize(bytes)) << ") " << (request.keep_alive() ? "keep" : "drop") - << " [" << authority() << "]" + << " [" << endpoint() << "]" << " {" << (split(request[field::accept], ",").front()) << "...}" << " " << request.target()); } @@ -214,7 +214,7 @@ void channel_http::log_message(const response& LOG_ONLY(response), << "] " << version << " (" << (response.chunked() ? "c" : serialize(bytes)) << ") " << (response.keep_alive() ? "keep" : "drop") - << " [" << authority() << "]" + << " [" << endpoint() << "]" << " {" << (response[field::content_type]) << "}"); } diff --git a/src/channels/channel_peer.cpp b/src/channels/channel_peer.cpp index c9e2e7f44..8b9d011d7 100644 --- a/src/channels/channel_peer.cpp +++ b/src/channels/channel_peer.cpp @@ -175,7 +175,7 @@ void channel_peer::handle_read_heading(const code& ec, size_t) NOEXCEPT if (stopped()) { - LOGQ("Heading read abort [" << authority() << "]"); + LOGQ("Heading read abort [" << endpoint() << "]"); return; } @@ -184,7 +184,7 @@ void channel_peer::handle_read_heading(const code& ec, size_t) NOEXCEPT // Don't log common conditions. if (ec != error::peer_disconnect && ec != error::operation_canceled) { - LOGF("Heading read failure [" << authority() << "] " + LOGF("Heading read failure [" << endpoint() << "] " << ec.message()); } @@ -197,7 +197,7 @@ void channel_peer::handle_read_heading(const code& ec, size_t) NOEXCEPT if (!heading_reader_) { - LOGR("Invalid heading from [" << authority() << "]"); + LOGR("Invalid heading from [" << endpoint() << "]"); stop(error::invalid_heading); return; } @@ -206,13 +206,13 @@ void channel_peer::handle_read_heading(const code& ec, size_t) NOEXCEPT { if (head->magic == http_magic || head->magic == https_magic) { - LOGR("Http/s request from [" << authority() << "]"); + LOGR("Http/s request from [" << endpoint() << "]"); } else { LOGR("Invalid heading magic (0x" << encode_base16(to_little_endian(head->magic)) - << ") from [" << authority() << "]"); + << ") from [" << endpoint() << "]"); } stop(error::invalid_magic); @@ -222,7 +222,7 @@ void channel_peer::handle_read_heading(const code& ec, size_t) NOEXCEPT if (head->payload_size > options().maximum_request) { LOGR("Oversized payload indicated by " << head->command - << " heading from [" << authority() << "] (" + << " heading from [" << endpoint() << "] (" << head->payload_size << " bytes)"); stop(error::oversized_payload); @@ -248,7 +248,7 @@ void channel_peer::handle_read_payload(const code& ec, size_t payload_size, if (stopped()) { - LOGQ("Payload read abort [" << authority() << "]"); + LOGQ("Payload read abort [" << endpoint() << "]"); return; } @@ -257,7 +257,7 @@ void channel_peer::handle_read_payload(const code& ec, size_t payload_size, // Don't log common conditions. if (ec != error::peer_disconnect && ec != error::operation_canceled) { - LOGF("Payload read failure [" << authority() << "] " + LOGF("Payload read failure [" << endpoint() << "] " << ec.message()); } @@ -271,7 +271,7 @@ void channel_peer::handle_read_payload(const code& ec, size_t payload_size, if (head->checksum != network_checksum(bitcoin_hash(payload_buffer_))) { LOGR("Invalid " << head->command << " payload from [" - << authority() << "] bad checksum."); + << endpoint() << "] bad checksum."); stop(error::invalid_checksum); return; @@ -310,7 +310,7 @@ void channel_peer::handle_read_payload(const code& ec, size_t payload_size, payload_buffer_.shrink_to_fit(); } - LOGX("Recv " << head->command << " from [" << authority() << "] (" + LOGX("Recv " << head->command << " from [" << endpoint() << "] (" << payload_size << " bytes)"); read_heading(); @@ -335,13 +335,13 @@ void channel_peer::log_message(const std::string_view& name, name == messages::peer::block::command) { LOGR("Invalid " << name << " payload from [" - << authority() << "] with hash [" + << endpoint() << "] with hash [" << encode_hash(bitcoin_hash(payload_buffer_)) << "] "); } else { LOGR("Invalid " << name << " payload from [" - << authority() << "] with bytes (" << encode_base16( + << endpoint() << "] with bytes (" << encode_base16( { payload_buffer_.begin(), std::next(payload_buffer_.begin(), diff --git a/src/channels/channel_ws.cpp b/src/channels/channel_ws.cpp index cfbacf8a7..abcb7b509 100644 --- a/src/channels/channel_ws.cpp +++ b/src/channels/channel_ws.cpp @@ -60,7 +60,7 @@ void channel_ws::handle_receive_ws(const code& ec, size_t bytes) NOEXCEPT if (stopped()) { - LOGQ("Websocket read abort [" << authority() << "]"); + LOGQ("Websocket read abort [" << endpoint() << "]"); return; } @@ -69,7 +69,7 @@ void channel_ws::handle_receive_ws(const code& ec, size_t bytes) NOEXCEPT // Don't log common conditions. if (ec != error::peer_disconnect && ec != error::operation_canceled) { - LOGF("Websocket read failure [" << authority() << "] " + LOGF("Websocket read failure [" << endpoint() << "] " << ec.message()); } @@ -83,7 +83,7 @@ void channel_ws::handle_receive_ws(const code& ec, size_t bytes) NOEXCEPT void channel_ws::dispatch_ws(const http::flat_buffer&, size_t LOG_ONLY(bytes)) NOEXCEPT { - LOGA("Websocket read of " << bytes << " bytes [" << authority() << "]"); + LOGA("Websocket read of " << bytes << " bytes [" << endpoint() << "]"); // TODO: dispatch and restart upon completion. @@ -101,7 +101,7 @@ void channel_ws::handle_receive(const code& ec, size_t bytes, if (upgraded_) { - LOGF("Http request in websocket state [" << authority() << "]"); + LOGF("Http request in websocket state [" << endpoint() << "]"); stop(network::error::operation_failed); return; } @@ -113,7 +113,7 @@ void channel_ws::handle_receive(const code& ec, size_t bytes, } upgraded_ = true; - LOGA("Websocket upgraded [" << authority() << "]"); + LOGA("Websocket upgraded [" << endpoint() << "]"); receive(); } diff --git a/src/config/endpoint.cpp b/src/config/endpoint.cpp index 1900311c0..3c83b5df3 100644 --- a/src/config/endpoint.cpp +++ b/src/config/endpoint.cpp @@ -27,6 +27,11 @@ namespace libbitcoin { namespace network { namespace config { +endpoint::endpoint(const address& address) NOEXCEPT + : endpoint(address.to_ip(), address.port()) +{ +} + bool endpoint::is_address() const NOEXCEPT { // Serialize to address, cast to bool, true if not default (address). diff --git a/src/net.cpp b/src/net.cpp index 15b6bbce3..a8244805a 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -609,7 +609,7 @@ bool net::store_nonce(const channel_peer& channel) NOEXCEPT if (!nonces_.insert(channel.nonce()).second) { - LOGF("Failed to store nonce for [" << channel.authority() << "]."); + LOGF("Failed to store nonce for [" << channel.endpoint() << "]."); return false; } @@ -625,7 +625,7 @@ bool net::unstore_nonce(const channel_peer& channel) NOEXCEPT if (!to_bool(nonces_.erase(channel.nonce()))) { - LOGF("Failed to unstore nonce for [" << channel.authority() << "]."); + LOGF("Failed to unstore nonce for [" << channel.endpoint() << "]."); return false; } @@ -654,7 +654,7 @@ code net::count_channel(const channel_peer& channel) NOEXCEPT if (is_loopback(channel)) { - LOGS("Loopback detected from [" << channel.authority() << "]."); + LOGS("Loopback detected from [" << channel.endpoint() << "]."); return error::accept_failed; } @@ -670,9 +670,9 @@ code net::count_channel(const channel_peer& channel) NOEXCEPT return error::channel_overflow; } - if (!hosts_.reserve(channel.authority())) + if (!hosts_.reserve(channel.endpoint())) { - LOGS("Duplicate connection to [" << channel.authority() << "]."); + LOGS("Duplicate connection to [" << channel.endpoint() << "]."); return error::address_in_use; } @@ -689,7 +689,7 @@ void net::uncount_channel(const channel_peer& channel) NOEXCEPT { BC_ASSERT(stranded()); - hosts_.unreserve(channel.authority()); + hosts_.unreserve(channel.endpoint()); if (channel.inbound() && is_zero(inbound_channel_count_.load())) { diff --git a/src/net/connector.cpp b/src/net/connector.cpp index 3be9d8ecc..6329b5fcc 100644 --- a/src/net/connector.cpp +++ b/src/net/connector.cpp @@ -93,29 +93,26 @@ bool connector::stranded() NOEXCEPT // ---------------------------------------------------------------------------- // This used by outbound (address from pool). -void connector::connect(const address& host, +void connector::connect(const address& address, socket_handler&& handler) NOEXCEPT { - start(host.to_host(), host.port(), host, std::move(handler)); -} - -void connector::connect(const authority& host, - socket_handler&& handler) NOEXCEPT -{ - start(host.to_host(), host.port(), host.to_address_item(), + // Address is fully set (with metadata), endpoint is numeric. + start(address.to_host(), address.port(), address, address, std::move(handler)); } // This used by seed, manual, and socks5 (endpoint from config). -void connector::connect(const endpoint& host, +void connector::connect(const endpoint& endpoint, socket_handler&& handler) NOEXCEPT { - start(host.host(), host.port(), host, std::move(handler)); + // Address is not set (defaulted), endpoint is numeric or qualified name. + start(endpoint.host(), endpoint.port(), {}, endpoint, std::move(handler)); } // protected void connector::start(const std::string& hostname, uint16_t port, - const config::address& host, socket_handler&& handler) NOEXCEPT + const config::address& address, const config::endpoint& endpoint, + socket_handler&& handler) NOEXCEPT { BC_ASSERT(stranded()); @@ -137,7 +134,7 @@ void connector::start(const std::string& hostname, uint16_t port, // Create a socket and shared finish context. const auto finish = std::make_shared(false); const auto socket = std::make_shared(log, service_, - maximum_, host, proxied()); + maximum_, address, endpoint, proxied()); // Posts handle_timer to strand. timer_->start( diff --git a/src/net/connector_socks.cpp b/src/net/connector_socks.cpp index 91607f1dd..f5253666b 100644 --- a/src/net/connector_socks.cpp +++ b/src/net/connector_socks.cpp @@ -120,16 +120,18 @@ bool connector_socks::proxied() const NOEXCEPT // protected/override void connector_socks::start(const std::string& hostname, uint16_t port, - const config::address& host, socket_handler&& handler) NOEXCEPT + const config::address& address, const config::endpoint& endpoint, + socket_handler&& handler) NOEXCEPT { if (proxied()) { const auto& sox = socks5_.socks; - connector::start(sox.host(), sox.port(), host, std::move(handler)); + connector::start(sox.host(), sox.port(), address, endpoint, + std::move(handler)); return; } - connector::start(hostname, port, host, std::move(handler)); + connector::start(hostname, port, address, endpoint, std::move(handler)); } // protected/override @@ -333,24 +335,17 @@ void connector_socks::handle_socks_authentication_read(const code& ec, do_socks_connect_write(socket); } -// A DNS name (e.g. manual endpont) works when not proxied, as the name is -// passed through resolution below. However when the connection is proxied, -// the proxy name goes through resolution, but the host name does not. As a -// result proxied manual connections are currently limited to numeric IP -// addresses (authorities), despite being parsed as names (endpoints). BUT, -// this is easily resolvable by allowing the proxy to perform the secondary -// name resolution (preferred anyway). That would work here, except that host -// is presently passed via socket->address(), which results in a default -// address (due to failed lexical conversion from name to address in socket). -// This also prevents seeds from resolving via a proxy. +// TODO: Replace socket.endpoint() with socket.endpoint() [enable below], as +// TODO: socket.endpoint() is only used for logging. Retain socket.address(). void connector_socks::do_socks_connect_write( const socket::ptr& socket) NOEXCEPT { BC_ASSERT(socket->stranded()); - const auto port = to_big_endian(socket->address().port()); - const auto host = socket->address().to_host(); - const auto host_length = host.length(); + const auto& endpoint = socket->endpoint(); + const config::address address = endpoint; + const auto host_length = address ? (address.is_v4() ? 4u : 16u) : + add1(endpoint.host().length()); // Socks5 limits valid host lengths to one byte. if (is_limited(host_length)) @@ -364,16 +359,52 @@ void connector_socks::do_socks_connect_write( // +----+-----+-------+------+----------+----------+ // | 1 | 1 | X'00' | 1 | Variable | 2 | // +----+-----+-------+------+----------+----------+ - const auto length = 5u + host_length + port_size; + const auto length = 4u + host_length + port_size; const auto request = emplace_shared(length); auto it = request->begin(); *it++ = socks::version; *it++ = socks::command_connect; *it++ = socks::reserved; - *it++ = socks::address_fqdn; - *it++ = narrow_cast(host_length); - it = std::copy(host.begin(), host.end(), it); - it = std::copy(port.begin(), port.end(), it); + + // BUGBUG: this always sees v4 addresses as v6. + if (address) + { + ////const auto native = config::from_address(address.ip()); + const auto port = to_big_endian(address.port()); + + if (address.is_v4()) + { + ////const auto host = native.to_v4().to_bytes(); + const config::authority authority{ address }; + const auto host = authority.ip().to_v4().to_bytes(); + + *it++ = socks::address_ipv4; + it = std::copy(host.begin(), host.end(), it); + it = std::copy(port.begin(), port.end(), it); + + } + else // v6 + { + ////const auto host = native.to_v6().to_bytes(); + const config::authority authority{ address }; + const auto host = authority.ip().to_v6().to_bytes(); + + *it++ = socks::address_ipv6; + it = std::copy(host.begin(), host.end(), it); + it = std::copy(port.begin(), port.end(), it); + } + } + else + { + // An endpoint (fqdn) gets name resolution at the socks proxy. + const auto host = endpoint.host(); + const auto port = to_big_endian(endpoint.port()); + + *it++ = socks::address_fqdn; + *it++ = narrow_cast(host.length()); + it = std::copy(host.begin(), host.end(), it); + it = std::copy(port.begin(), port.end(), it); + } socket->write({ request->data(), request->size() }, std::bind(&connector_socks::handle_socks_connect_write, diff --git a/src/net/hosts.cpp b/src/net/hosts.cpp index 72f38f78b..267b21891 100644 --- a/src/net/hosts.cpp +++ b/src/net/hosts.cpp @@ -133,7 +133,7 @@ size_t hosts::count() const NOEXCEPT size_t hosts::reserved() const NOEXCEPT { - return authorities_count_.load(); + return endpoints_count_.load(); } // Usage. @@ -152,7 +152,7 @@ void hosts::take(address_item_handler&& handler) NOEXCEPT while (!buffer_.empty()) { const auto host = pop(); - if (!is_reserved(*host)) + if (!is_reserved({ *host })) { hosts_count_.store(buffer_.size()); handler(error::success, host); @@ -251,7 +251,7 @@ void hosts::save(const address_cptr& message, count_handler&& handler) NOEXCEPT for (const auto& host: message->addresses) { // O(N) <= could be resolved with O(1) search. - if (!is_reserved(host) && !is_pooled(host)) + if (!is_reserved({ host }) && !is_pooled(host)) { // O(1). buffer_.push_back(host); @@ -329,26 +329,26 @@ inline void hosts::push(const std::string& line) NOEXCEPT // O(1). // private -inline bool hosts::is_reserved(const config::authority& host) const NOEXCEPT +inline bool hosts::is_reserved(const config::endpoint& host) const NOEXCEPT { - return authorities_.contains(host); + return endpoints_.contains(host); } // O(1). // Channel is connected (infrequent). -bool hosts::reserve(const config::authority& host) NOEXCEPT +bool hosts::reserve(const config::endpoint& host) NOEXCEPT { - const auto result = authorities_.insert(host).second; - if (result) ++authorities_count_; + const auto result = endpoints_.insert(host).second; + if (result) ++endpoints_count_; return result; } // O(1). // Channel is unconnected (infrequent). -bool hosts::unreserve(const config::authority& host) NOEXCEPT +bool hosts::unreserve(const config::endpoint& host) NOEXCEPT { - const auto result = to_bool(authorities_.erase(host)); - if (result) --authorities_count_; + const auto result = to_bool(endpoints_.erase(host)); + if (result) --endpoints_count_; return result; } diff --git a/src/net/proxy.cpp b/src/net/proxy.cpp index f6134cadb..b94f0a23d 100644 --- a/src/net/proxy.cpp +++ b/src/net/proxy.cpp @@ -266,7 +266,7 @@ void proxy::do_write(const asio::const_buffer& payload, if (stopped()) { - LOGQ("Payload write abort [" << authority() << "]"); + LOGQ("Payload write abort [" << endpoint() << "]"); handler(error::channel_stopped, {}); return; } @@ -276,7 +276,7 @@ void proxy::do_write(const asio::const_buffer& payload, total_ = ceilinged_add(total_.load(), payload.size()); backlog_ = ceilinged_add(backlog_.load(), payload.size()); - LOGX("Queue for [" << authority() << "]: " << queue_.size() + LOGX("Queue for [" << endpoint() << "]: " << queue_.size() << " (" << backlog_.load() << " of " << total_.load() << " bytes)"); // Start the loop if it wasn't already started. @@ -307,7 +307,7 @@ void proxy::handle_write(const code& ec, size_t bytes, if (stopped()) { - LOGQ("Send abort [" << authority() << "]"); + LOGQ("Send abort [" << endpoint() << "]"); return; } @@ -315,7 +315,7 @@ void proxy::handle_write(const code& ec, size_t bytes, backlog_ = floored_subtract(backlog_.load(), queue_.front().first.size()); queue_.pop_front(); - LOGX("Dequeue for [" << authority() << "]: " << queue_.size() + LOGX("Dequeue for [" << endpoint() << "]: " << queue_.size() << " (" << backlog_.load() << " backlog)"); // All handlers must be invoked, so continue regardless of error state. @@ -331,7 +331,7 @@ void proxy::handle_write(const code& ec, size_t bytes, // BUGBUG: payload changed from data_chunk_ptr to const_buffer. // TODO: messages dependency, move to channel. ////LOGF("Send failure " << heading::get_command(*payload) << " to [" - //// << authority() << "] (" << payload->size() << " bytes) " + //// << endpoint() << "] (" << payload->size() << " bytes) " //// << ec.message()); } @@ -342,7 +342,7 @@ void proxy::handle_write(const code& ec, size_t bytes, // BUGBUG: payload changed from data_chunk_ptr to const_buffer. // TODO: messages dependency, move to channel. ////LOGX("Sent " << heading::get_command(*payload) << " to [" - //// << authority() << "] (" << payload->size() << " bytes)"); + //// << endpoint() << "] (" << payload->size() << " bytes)"); handler(ec, bytes); } @@ -380,14 +380,14 @@ bool proxy::inbound() const NOEXCEPT return socket_->inbound(); } -const config::authority& proxy::authority() const NOEXCEPT +const config::address& proxy::address() const NOEXCEPT { - return socket_->authority(); + return socket_->address(); } -const config::address& proxy::address() const NOEXCEPT +const config::endpoint& proxy::endpoint() const NOEXCEPT { - return socket_->address(); + return socket_->endpoint(); } BC_POP_WARNING() diff --git a/src/net/socket.cpp b/src/net/socket.cpp index 7035c3a10..5ed11d697 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -47,23 +47,31 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) // Construction. // ---------------------------------------------------------------------------- -// authority_.port() zero implies inbound connection. socket::socket(const logger& log, asio::io_context& service, size_t maximum_request) NOEXCEPT - : socket(log, service, maximum_request, config::address{}) + : socket(log, service, maximum_request, {}, {}, false, true) { } -// authority_.port() nonzero implies outbound connection. socket::socket(const logger& log, asio::io_context& service, size_t maximum_request, const config::address& address, - bool proxied) NOEXCEPT - : proxied_(proxied), + const config::endpoint& endpoint, bool proxied) NOEXCEPT + : socket(log, service, maximum_request, address, endpoint, proxied, false) +{ +} + +// protected +socket::socket(const logger& log, asio::io_context& service, + size_t maximum_request, const config::address& address, + const config::endpoint& endpoint, bool proxied, bool inbound) NOEXCEPT + : inbound_(inbound), + proxied_(proxied), maximum_(maximum_request), strand_(service.get_executor()), service_(service), socket_(strand_), address_(address), + endpoint_(endpoint), reporter(log), tracker(log) { @@ -603,11 +611,9 @@ void socket::handle_accept(boost_code ec, const result_handler& handler) NOEXCEPT { // 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. - + // socket_ and endpoint_ are not guarded here, see comments on accept. if (!ec) - authority_ = { socket_.remote_endpoint(ec) }; + endpoint_ = { socket_.remote_endpoint(ec) }; if (error::asio_is_canceled(ec)) { @@ -620,24 +626,14 @@ void socket::handle_accept(boost_code ec, handler(code); } -// The peer is what the socket connected to. For socks proxy this will be the -// proxy server's address:port. address_ is the value set at construct, and for -// outbound this is the intended peer. For inbound this is defaulted (and then -// set to socket_.remote_endpoint in handle_accept). For outbound this is the -// host value of type config::address passed to connector::start. The inbound() -// method depends upon this being defaulted (not set) for incoming connections -// and set for outgoing. void socket::handle_connect(const boost_code& ec, const asio::endpoint& peer, const result_handler& handler) NOEXCEPT { BC_ASSERT(stranded()); - // For socks proxy, peer will be the server's local binding. In this case - // authority_ is set to address_ as passed on start(). When this is a fqdn - // it fails lexical parse and results in a default address (see also: - // do_socks_connect_write). - authority_ = proxied_ ? config::authority{ address_ } : - config::authority{ peer }; + // For socks proxy, peer will be the server's local binding. + if (!proxied_) + endpoint_ = peer; if (error::asio_is_canceled(ec)) { @@ -775,13 +771,13 @@ void socket::handle_ws_event(ws::frame_type kind, switch (kind) { case ws::frame_type::ping: - LOGX("WS ping [" << authority() << "] size: " << data.size()); + LOGX("WS ping [" << endpoint() << "] size: " << data.size()); break; case ws::frame_type::pong: - LOGX("WS pong [" << authority() << "] size: " << data.size()); + LOGX("WS pong [" << endpoint() << "] size: " << data.size()); break; case ws::frame_type::close: - LOGX("WS close [" << authority() << "] " << websocket_->reason()); + LOGX("WS close [" << endpoint() << "] " << websocket_->reason()); break; } } @@ -836,20 +832,19 @@ void socket::handle_http_write(const boost_code& ec, size_t size, // Properties. // ---------------------------------------------------------------------------- -const config::authority& socket::authority() const NOEXCEPT +const config::address& socket::address() const NOEXCEPT { - return authority_; + return address_; } -const config::address& socket::address() const NOEXCEPT +const config::endpoint& socket::endpoint() const NOEXCEPT { - return address_; + return endpoint_; } bool socket::inbound() const NOEXCEPT { - // Relies on construction and address default port of zero. - return is_zero(address_.port()); + return inbound_; } bool socket::stopped() const NOEXCEPT diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp index 15480d273..61ffeacfb 100644 --- a/src/protocols/protocol.cpp +++ b/src/protocols/protocol.cpp @@ -129,9 +129,9 @@ bool protocol::stranded() const NOEXCEPT return channel_->stranded(); } -config::authority protocol::authority() const NOEXCEPT +config::endpoint protocol::opposite() const NOEXCEPT { - return channel_->authority(); + return { channel_->endpoint() }; } const config::address& protocol::outbound() const NOEXCEPT diff --git a/src/protocols/protocol_address_in_209.cpp b/src/protocols/protocol_address_in_209.cpp index 6c40998aa..108377ced 100644 --- a/src/protocols/protocol_address_in_209.cpp +++ b/src/protocols/protocol_address_in_209.cpp @@ -118,7 +118,7 @@ bool protocol_address_in_209::handle_receive_address(const code& ec, if (!outbound_ && !advertisement) { LOGP("Ignoring (" << start_size << ") unsolicited addresses from [" - << authority() << "]."); + << opposite() << "]."); ////stop(error::protocol_violation); return true; } @@ -127,13 +127,13 @@ bool protocol_address_in_209::handle_receive_address(const code& ec, if (advertisement) { BROADCAST(address, message); - LOGP("Relay (" << start_size << ") addresses by [" - << authority() << "]."); + LOGP("Relay (" << start_size << ") addresses by [" << opposite() + << "]."); } if (is_one(start_size) && message->addresses.front() == outbound()) { - ////LOGP("Dropping redundant address from [" << authority() << "]."); + ////LOGP("Dropping redundant address from [" << opposite() << "]."); return true; } @@ -156,7 +156,7 @@ void protocol_address_in_209::handle_save_addresses(const code& ec, return; LOGP("Accepted (" << start_size << ">" << end_size << ">" << accepted << ") " - "addresses from [" << authority() << "]."); + "addresses from [" << opposite() << "]."); } BC_POP_WARNING() diff --git a/src/protocols/protocol_address_out_209.cpp b/src/protocols/protocol_address_out_209.cpp index 025c247b8..aad5db327 100644 --- a/src/protocols/protocol_address_out_209.cpp +++ b/src/protocols/protocol_address_out_209.cpp @@ -79,7 +79,7 @@ bool protocol_address_out_209::handle_receive_get_address(const code& ec, // Limit get_address requests to one per session. if (sent_) { - LOGP("Ignoring duplicate address request from [" << authority() << "]"); + LOGP("Ignoring duplicate address request from [" << opposite() << "]"); ////stop(error::protocol_violation); return true; } @@ -87,7 +87,7 @@ bool protocol_address_out_209::handle_receive_get_address(const code& ec, fetch(BIND(handle_fetch_address, _1, _2)); sent_ = true; - LOGP("Address relay start [" << authority() << "]."); + LOGP("Address relay start [" << opposite() << "]."); SUBSCRIBE_BROADCAST(address, handle_broadcast_address, _1, _2, _3); return true; } @@ -105,7 +105,7 @@ void protocol_address_out_209::handle_fetch_address(const code& ec, return; LOGP("Sending (" << message->addresses.size() << ") addresses to " - "[" << authority() << "]"); + "[" << opposite() << "]"); SEND(*message, handle_send, _1); } @@ -119,18 +119,18 @@ bool protocol_address_out_209::handle_broadcast_address(const code& ec, if (stopped(ec)) { - LOGP("Relay stop [" << authority() << "]."); + LOGP("Relay stop [" << opposite() << "]."); return false; } if (sender == identifier()) { - LOGP("Relay self [" << authority() << "]."); + LOGP("Relay self [" << opposite() << "]."); return true; } LOGP("Relay (" << message->addresses.size() << ") addresses to [" - << authority() << "]."); + << opposite() << "]."); SEND(*message, handle_send, _1); return true; diff --git a/src/protocols/protocol_alert_311.cpp b/src/protocols/protocol_alert_311.cpp index bb83b8965..2fc264c31 100644 --- a/src/protocols/protocol_alert_311.cpp +++ b/src/protocols/protocol_alert_311.cpp @@ -71,7 +71,7 @@ bool protocol_alert_311::handle_receive_alert(const code& ec, // TODO: serialize cancels and sub_versions. // Signature not validated because is not relevant (private key published). - LOGN("Alert from [" << authority() << "]..." + LOGN("Alert from [" << opposite() << "]..." << "\nversion : " << alert->payload.version << "\nrelay_until : " << alert->payload.relay_until << "\nexpiration : " << alert->payload.expiration diff --git a/src/protocols/protocol_ping_60001.cpp b/src/protocols/protocol_ping_60001.cpp index bf151e481..1777ed663 100644 --- a/src/protocols/protocol_ping_60001.cpp +++ b/src/protocols/protocol_ping_60001.cpp @@ -102,7 +102,7 @@ bool protocol_ping_60001::handle_receive_pong(const code& ec, // Both nonce incorrect and already received are protocol violations. if (message->nonce != nonce_) { - LOGR("Incorrect pong nonce from [" << authority() << "]"); + LOGR("Incorrect pong nonce from [" << opposite() << "]"); stop(error::protocol_violation); return false; } diff --git a/src/protocols/protocol_reject_70002.cpp b/src/protocols/protocol_reject_70002.cpp index dce7fd83c..736fa7047 100644 --- a/src/protocols/protocol_reject_70002.cpp +++ b/src/protocols/protocol_reject_70002.cpp @@ -80,13 +80,13 @@ bool protocol_reject_70002::handle_receive_reject(const code& ec, // if received here (outside of handshake), a protocol error is implied. if (message->message == version::command) { - LOGR("Version reject after handshake [" << authority() << "]"); + LOGR("Version reject after handshake [" << opposite() << "]"); stop(error::protocol_violation); return false; } // system::serialize require for uint8_t serialization. - LOGP("Reject from [" << authority() << "]..." + LOGP("Reject from [" << opposite() << "]..." << "\ncode : " << system::serialize(reject::reason_to_byte( message->code)) << "\nmessage: " << message->message diff --git a/src/protocols/protocol_seed_209.cpp b/src/protocols/protocol_seed_209.cpp index 31ea8a204..1ab70f0fb 100644 --- a/src/protocols/protocol_seed_209.cpp +++ b/src/protocols/protocol_seed_209.cpp @@ -156,7 +156,7 @@ bool protocol_seed_209::handle_receive_address(const code& ec, const auto start_size = message->addresses.size(); if (is_one(start_size) && (message->addresses.front() == outbound())) { - ////LOGP("Dropping redundant address from seed [" << authority() << "]"); + ////LOGP("Dropping redundant address from seed [" << endpoint() << "]"); return true; } @@ -185,7 +185,7 @@ void protocol_seed_209::handle_save_addresses(const code& ec, stop(ec); LOGN("Accepted (" << start_size << ">" << end_size << ">" << accepted - << ") addresses from seed [" << authority() << "]."); + << ") addresses from seed [" << opposite() << "]."); // Multiple address messages are allowed, but do not delay session. // Ignore a singleton message, conventional to send self upon connect. diff --git a/src/protocols/protocol_version_106.cpp b/src/protocols/protocol_version_106.cpp index 37f50f75e..e4c49dac4 100644 --- a/src/protocols/protocol_version_106.cpp +++ b/src/protocols/protocol_version_106.cpp @@ -95,8 +95,8 @@ messages::peer::version protocol_version_106::version_factory( { timestamp, service::node_none, - authority().to_ip_address(), - authority().port(), + outbound().ip(), + opposite().port() }, // ******************************************************************** @@ -308,13 +308,13 @@ bool protocol_version_106::handle_receive_version(const code& ec, "/BitcoinFinance:"); LOG_ONLY(const auto prefix = (inbound_ ? "Inbound" : "Outbound");) - LOGN(prefix << " [" << authority() << "] version (" - << message->value << ") " << user_agent); + LOGN(prefix << " [" << opposite() << "] version (" << message->value + << ") " << user_agent); if (to_bool(message->services & invalid_services_)) { LOGR("Unsupported services (" << message->services << ") by [" - << authority() << "] showing (" << outbound().services() << ") " + << opposite() << "] showing (" << outbound().services() << ") " << user_agent); rejection(error::peer_unsupported); @@ -325,7 +325,7 @@ bool protocol_version_106::handle_receive_version(const code& ec, if ((message->services & minimum_services_) != minimum_services_) { LOGR("Insufficient services (" << message->services << ") by [" - << authority() << "] showing (" << outbound().services() << ") " + << opposite() << "] showing (" << outbound().services() << ") " << user_agent); rejection(error::peer_insufficient); @@ -335,7 +335,7 @@ bool protocol_version_106::handle_receive_version(const code& ec, if (message->value < minimum_version_) { LOGP("Insufficient peer protocol version (" << message->value << ") " - "for [" << authority() << "] " << user_agent); + "for [" << opposite() << "] " << user_agent); rejection(error::peer_insufficient); return false; @@ -346,7 +346,7 @@ bool protocol_version_106::handle_receive_version(const code& ec, if (absolute(deviation.count()) > maximum_skew_minutes_) { LOGR("Skewed time (" << deviation.count() << ") minutes " - "for [" << authority() << "] " << user_agent); + "for [" << opposite() << "] " << user_agent); rejection(error::peer_timestamp); return false; @@ -357,10 +357,10 @@ bool protocol_version_106::handle_receive_version(const code& ec, set_peer_version(message); ////LOGP("Negotiated protocol version (" << version << ") " - //// << "for [" << authority() << "]."); + //// << "for [" << opposite() << "]."); ////// TODO: verbose (helpful for identifying own address for config of self). - ////LOGP("Peer [" << authority() << "] " + ////LOGP("Peer [" << opposite() << "] " //// << "as {" << config::authority(message->address_sender) << "} " //// << "us {" << config::authority(message->address_receiver) << "}."); diff --git a/src/protocols/protocol_version_70002.cpp b/src/protocols/protocol_version_70002.cpp index 68f36a631..e223659e9 100644 --- a/src/protocols/protocol_version_70002.cpp +++ b/src/protocols/protocol_version_70002.cpp @@ -117,7 +117,7 @@ bool protocol_version_70002::handle_receive_reject(const code& ec, return false; LOGP("Reject message '" << message->message << "' (" - << static_cast(message->code) << ") from [" << authority() + << static_cast(message->code) << ") from [" << opposite() << "] with reason: " << message->reason); return true; diff --git a/src/sessions/session_inbound.cpp b/src/sessions/session_inbound.cpp index db1dd12bb..0c596f1d9 100644 --- a/src/sessions/session_inbound.cpp +++ b/src/sessions/session_inbound.cpp @@ -161,25 +161,23 @@ void session_inbound::handle_accepted(const code& ec, // Could instead stop listening when at limit, though this is simpler. if (inbound_channel_count() >= network_settings().inbound.connections) { - LOGS("Dropping oversubscribed peer [" << socket->authority() << "]."); + LOGS("Dropping oversubscribed peer [" << socket->endpoint() << "]."); socket->stop(); defer(BIND(start_accept, _1, acceptor)); return; } - const auto address = socket->authority().to_address_item(); - - if (!whitelisted(address)) + if (!whitelisted(socket->address())) { - ////LOGS("Dropping not whitelisted peer [" << socket->authority() << "]."); + ////LOGS("Dropping not whitelisted peer [" << socket->endpoint() << "]."); socket->stop(); start_accept(error::success, acceptor); return; } - if (blacklisted(address)) + if (blacklisted(socket->address())) { - ////LOGS("Dropping blacklisted peer [" << socket->authority() << "]."); + ////LOGS("Dropping blacklisted peer [" << socket->endpoint() << "]."); socket->stop(); start_accept(error::success, acceptor); return; @@ -187,7 +185,7 @@ void session_inbound::handle_accepted(const code& ec, const auto channel = create_channel(socket); - LOGS("Accepted peer connection [" << channel->authority() + LOGS("Accepted peer connection [" << channel->endpoint() << "] on binding [" << acceptor->local() << "]."); // There was no error, so listen again without delay. @@ -264,7 +262,7 @@ void session_inbound::handle_channel_start(const code&, const channel::ptr&) NOEXCEPT { BC_ASSERT(stranded()); - ////LOGS("Inbound channel start [" << channel->authority() << "] " + ////LOGS("Inbound channel start [" << channel->endpoint() << "] " //// << ec.message()); } @@ -313,7 +311,7 @@ void session_inbound::handle_channel_stop(const code& LOG_ONLY(ec), const channel::ptr& LOG_ONLY(channel)) NOEXCEPT { BC_ASSERT(stranded()); - LOGS("Inbound peer channel stop [" << channel->authority() << "] " + LOGS("Inbound peer channel stop [" << channel->endpoint() << "] " << ec.message()); } diff --git a/src/sessions/session_outbound.cpp b/src/sessions/session_outbound.cpp index ecb70bb53..f8967d072 100644 --- a/src/sessions/session_outbound.cpp +++ b/src/sessions/session_outbound.cpp @@ -254,7 +254,7 @@ void session_outbound::handle_channel_start(const code&, { BC_ASSERT(stranded()); - ////LOGS("Outbound channel start [" << channel->authority() << "] " + ////LOGS("Outbound channel start [" << channel->endpoint() << "] " //// << ec.message()); } @@ -269,7 +269,7 @@ void session_outbound::handle_channel_stop(const code& ec, { BC_ASSERT(stranded()); - ////LOGS("Outbound channel stop [" << channel->authority() << "] " + ////LOGS("Outbound channel stop [" << channel->endpoint() << "] " //// << ec.message()); reclaim(ec, channel); diff --git a/src/sessions/session_seed.cpp b/src/sessions/session_seed.cpp index 433f9ebc1..b51b3c875 100644 --- a/src/sessions/session_seed.cpp +++ b/src/sessions/session_seed.cpp @@ -221,7 +221,7 @@ void session_seed::handle_channel_start(const code& ec, if (ec) { - LOGN("Seed start [" << channel->authority() << "] " << ec.message()); + LOGN("Seed start [" << channel->endpoint() << "] " << ec.message()); } } @@ -269,7 +269,7 @@ void session_seed::handle_channel_stop(const code& LOG_ONLY(ec), const channel::ptr& LOG_ONLY(channel), const race::ptr& racer) NOEXCEPT { BC_ASSERT(stranded()); - LOGN("Seed stop [" << channel->authority() << "] " << ec.message()); + LOGN("Seed stop [" << channel->endpoint() << "] " << ec.message()); racer->finish(address_count()); } diff --git a/src/sessions/session_server.cpp b/src/sessions/session_server.cpp index 936a074af..bf6de3292 100644 --- a/src/sessions/session_server.cpp +++ b/src/sessions/session_server.cpp @@ -166,25 +166,23 @@ void session_server::handle_accepted(const code& ec, const socket::ptr& socket, if (channel_count_ >= options_.connections) { LOGS("Dropping oversubscribed " << name_ << " connection [" - << socket->authority() << "]."); + << socket->endpoint() << "]."); socket->stop(); defer(BIND(start_accept, _1, acceptor)); return; } - const auto address = socket->authority().to_address_item(); - - if (!whitelisted(address)) + if (!whitelisted(socket->address())) { - ////LOGS("Dropping not whitelisted peer [" << socket->authority() << "]."); + ////LOGS("Dropping not whitelisted peer [" << socket->endpoint() << "]."); socket->stop(); start_accept(error::success, acceptor); return; } - if (blacklisted(address)) + if (blacklisted(socket->address())) { - ////LOGS("Dropping blacklisted peer [" << socket->authority() << "]."); + ////LOGS("Dropping blacklisted peer [" << socket->endpoint() << "]."); socket->stop(); start_accept(error::success, acceptor); return; @@ -193,7 +191,7 @@ void session_server::handle_accepted(const code& ec, const socket::ptr& socket, // Creates channel_xxxx cast as channel::ptr. const auto channel = create_channel(socket); - LOGS("Accepted " << name_ << " connection [" << channel->authority() + LOGS("Accepted " << name_ << " connection [" << channel->endpoint() << "] on binding [" << acceptor->local() << "]."); // There was no error, so listen again without delay. @@ -246,7 +244,7 @@ void session_server::handle_channel_start(const code& LOG_ONLY(ec), const channel::ptr& LOG_ONLY(channel)) NOEXCEPT { BC_ASSERT(stranded()); - LOGS("Inbound " << name_ << " channel start [" << channel->authority() + LOGS("Inbound " << name_ << " channel start [" << channel->endpoint() << "] " << ec.message()); BC_ASSERT(!is_zero(add1(channel_count_))); @@ -257,7 +255,7 @@ void session_server::handle_channel_stop(const code& LOG_ONLY(ec), const channel::ptr& LOG_ONLY(channel)) NOEXCEPT { BC_ASSERT(stranded()); - LOGS("Inbound " << name_ << " channel stop [" << channel->authority() + LOGS("Inbound " << name_ << " channel stop [" << channel->endpoint() << "] " << ec.message()); BC_ASSERT(!is_zero(channel_count_)); diff --git a/test/net/proxy.cpp b/test/net/proxy.cpp index f485fbcfe..8bf9962c9 100644 --- a/test/net/proxy.cpp +++ b/test/net/proxy.cpp @@ -155,10 +155,10 @@ BOOST_AUTO_TEST_CASE(proxy__authority__default__expected) { const logger log{}; threadpool pool(2); - const config::authority default_authority{}; + const config::endpoint default_endpoint{}; auto socket_ptr = std::make_shared(log, pool.service(), 42); auto proxy_ptr = std::make_shared(socket_ptr); - BOOST_REQUIRE(proxy_ptr->authority() == default_authority); + BOOST_REQUIRE(proxy_ptr->endpoint() == default_endpoint); proxy_ptr->stop(error::invalid_magic); } diff --git a/test/net/socket.cpp b/test/net/socket.cpp index 842713426..034bd6967 100644 --- a/test/net/socket.cpp +++ b/test/net/socket.cpp @@ -36,9 +36,9 @@ class socket_accessor return socket_; } - const config::authority& get_authority() const NOEXCEPT + const config::endpoint& get_endpoint() const NOEXCEPT { - return authority_; + return endpoint_; } const config::address& get_address() const NOEXCEPT @@ -62,8 +62,8 @@ BOOST_AUTO_TEST_CASE(socket__construct__default__closed_not_stopped_expected) BOOST_REQUIRE(!instance->stranded()); BOOST_REQUIRE(!instance->get_socket().is_open()); BOOST_REQUIRE(&instance->get_strand() == &instance->strand()); - BOOST_REQUIRE(instance->get_authority() == instance->authority()); - BOOST_REQUIRE(instance->get_authority().ip().is_unspecified()); + BOOST_REQUIRE(instance->get_endpoint() == instance->endpoint()); + BOOST_REQUIRE(!instance->get_endpoint().is_address()); BOOST_REQUIRE(instance->get_address() == instance->address()); BOOST_REQUIRE(instance->get_address() == config::address{}); BOOST_REQUIRE_EQUAL(instance->get_maximum_request(), maximum); @@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(socket__accept__cancel_acceptor__channel_stopped) { // Acceptor cancellation sets channel_stopped and unspecified address. BOOST_REQUIRE_EQUAL(ec, error::operation_canceled); - BOOST_REQUIRE(instance->get_authority().ip().is_unspecified()); + BOOST_REQUIRE(!instance->get_endpoint().is_address()); }); // Stopping the socket does not cancel the acceptor but precludes assertion. diff --git a/test/protocols/protocol.cpp b/test/protocols/protocol.cpp index 058f806fd..2d72eef88 100644 --- a/test/protocols/protocol.cpp +++ b/test/protocols/protocol.cpp @@ -134,7 +134,7 @@ class mock_connector // Inject mock channel. void start(const std::string&, uint16_t, const config::address&, - socket_handler&& handler) NOEXCEPT override + const config::endpoint&, socket_handler&& handler) NOEXCEPT override { const auto socket = std::make_shared(log, service_, maximum_); @@ -235,9 +235,9 @@ class mock_protocol /// Properties. /// ----------------------------------------------------------------------- - config::authority authority() const NOEXCEPT override + config::endpoint opposite() const NOEXCEPT override { - return protocol::authority(); + return protocol::opposite(); } uint64_t nonce() const NOEXCEPT override diff --git a/test/sessions/session_outbound.cpp b/test/sessions/session_outbound.cpp index b26806dd5..54e12ca2b 100644 --- a/test/sessions/session_outbound.cpp +++ b/test/sessions/session_outbound.cpp @@ -77,7 +77,8 @@ class mock_connector_connect_success // Handle connect, capture first connected hostname and port. void start(const std::string& hostname, uint16_t port, - const config::address&, socket_handler&& handler) NOEXCEPT override + const config::address&, const config::endpoint&, + socket_handler&& handler) NOEXCEPT override { if (is_zero(connects_++)) { @@ -111,8 +112,8 @@ class mock_connector_connect_fail using connector::connector; - void start(const std::string&, uint16_t, - const config::address&, socket_handler&& handler) NOEXCEPT override + void start(const std::string&, uint16_t, const config::address&, + const config::endpoint&, socket_handler&& handler) NOEXCEPT override { boost::asio::post(strand_, [=]() NOEXCEPT { @@ -316,15 +317,16 @@ class mock_connector_stop_connect } void start(const std::string& hostname, uint16_t port, - const config::address& host, socket_handler&& handler) NOEXCEPT override + const config::address& address, const config::endpoint& endpoint, + socket_handler&& handler) NOEXCEPT override { BC_ASSERT_MSG(session_, "call set_session"); // This connector.start_connect is invoked from network stranded method. session_->stop(); - mock_connector_connect_success::start(hostname, port, host, - std::move(handler)); + mock_connector_connect_success::start(hostname, port, address, + endpoint, std::move(handler)); } private: diff --git a/test/sessions/session_seed.cpp b/test/sessions/session_seed.cpp index 4fd53b72c..5c24bac2d 100644 --- a/test/sessions/session_seed.cpp +++ b/test/sessions/session_seed.cpp @@ -70,7 +70,8 @@ class mock_connector_connect_success // Handle connect, capture first connected hostname and port. void start(const std::string& hostname, uint16_t port, - const config::address&, socket_handler&& handler) NOEXCEPT override + const config::address&, const config::endpoint&, + socket_handler&& handler) NOEXCEPT override { if (is_zero(connects_++)) { @@ -105,7 +106,7 @@ class mock_connector_connect_fail using mock_connector_connect_success::mock_connector_connect_success; void start(const std::string&, uint16_t, const config::address&, - socket_handler&& handler) NOEXCEPT override + const config::endpoint&, socket_handler&& handler) NOEXCEPT override { boost::asio::post(strand_, [=]() NOEXCEPT { @@ -331,15 +332,16 @@ class mock_connector_stop_connect } void start(const std::string& hostname, uint16_t port, - const config::address& host, socket_handler&& handler) NOEXCEPT override + const config::address& address, const config::endpoint& endpoint, + socket_handler&& handler) NOEXCEPT override { BC_ASSERT_MSG(session_, "call set_session"); // This connector.start_connect is invoked from network stranded method. session_->stop(); - mock_connector_connect_success::start(hostname, port, host, - std::move(handler)); + mock_connector_connect_success::start(hostname, port, address, + endpoint, std::move(handler)); } private: From c31aae3177e8ae8cee19b2e7180ecbe9f85fc12d Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sat, 10 Jan 2026 23:57:17 -0500 Subject: [PATCH 5/5] Comments. --- include/bitcoin/network/net/socket.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index 7a0081921..f84ebc86b 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -48,7 +48,8 @@ class BCT_API socket size_t maximum_request) NOEXCEPT; /// Use only for outgoing connections. Endpoint represents the peer or - /// client (non-proxy) that the connector attempted to reach. + /// client (non-proxy) that the connector attempted to reach. Address holds + /// a copy of the p2p address associated with the connection (or empty). socket(const logger& log, asio::io_context& service, size_t maximum_request, const config::address& address, const config::endpoint& endpoint, bool proxied=false) NOEXCEPT;