From 1f12a80e89843f15ce041c86cb547dc76395638b Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:15:14 +0200 Subject: [PATCH] sentry: handle errors when accessing socket endpoints (#2306) Fixes #2300 --- silkworm/sentry/rlpx/client.cpp | 5 ++--- silkworm/sentry/rlpx/peer.cpp | 2 ++ silkworm/sentry/rlpx/peer.hpp | 6 ++++-- silkworm/sentry/rlpx/server.cpp | 9 +++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/silkworm/sentry/rlpx/client.cpp b/silkworm/sentry/rlpx/client.cpp index 244ca426f2..681cfaafe3 100644 --- a/silkworm/sentry/rlpx/client.cpp +++ b/silkworm/sentry/rlpx/client.cpp @@ -54,6 +54,8 @@ Task> Client::connect( try { attempt_num++; co_await stream.socket().async_connect(endpoint, use_awaitable); + const auto remote_endpoint = stream.socket().remote_endpoint(); + log::Trace("sentry") << "rlpx::Client connected to " << remote_endpoint; is_connected = true; } catch (const boost::system::system_error& ex) { if (ex.code() == boost::system::errc::operation_canceled) @@ -71,9 +73,6 @@ Task> Client::connect( } } - auto remote_endpoint = stream.socket().remote_endpoint(); - log::Trace("sentry") << "rlpx::Client connected to " << remote_endpoint; - co_return std::make_unique( client_context, std::move(stream), diff --git a/silkworm/sentry/rlpx/peer.cpp b/silkworm/sentry/rlpx/peer.cpp index cf60e1c84f..3c3e3c8ce7 100644 --- a/silkworm/sentry/rlpx/peer.cpp +++ b/silkworm/sentry/rlpx/peer.cpp @@ -53,6 +53,8 @@ Peer::Peer( bool is_inbound, bool is_static) : stream_(std::move(stream)), + local_endpoint_(stream_.socket().local_endpoint()), + remote_endpoint_(stream_.socket().remote_endpoint()), node_key_(std::move(node_key)), client_id_(std::move(client_id)), node_listen_port_(node_listen_port), diff --git a/silkworm/sentry/rlpx/peer.hpp b/silkworm/sentry/rlpx/peer.hpp index 8776ed4516..93b87ff977 100644 --- a/silkworm/sentry/rlpx/peer.hpp +++ b/silkworm/sentry/rlpx/peer.hpp @@ -81,11 +81,11 @@ class Peer { } boost::asio::ip::tcp::endpoint local_endpoint() const { - return stream_.socket().local_endpoint(); + return local_endpoint_; } boost::asio::ip::tcp::endpoint remote_endpoint() const { - return stream_.socket().remote_endpoint(); + return remote_endpoint_; } bool is_inbound() const { return is_inbound_; }; @@ -113,6 +113,8 @@ class Peer { Task ping_periodically(framing::MessageStream& message_stream); SocketStream stream_; + boost::asio::ip::tcp::endpoint local_endpoint_; + boost::asio::ip::tcp::endpoint remote_endpoint_; EccKeyPair node_key_; std::string client_id_; uint16_t node_listen_port_; diff --git a/silkworm/sentry/rlpx/server.cpp b/silkworm/sentry/rlpx/server.cpp index 571c07951f..a3372699fa 100644 --- a/silkworm/sentry/rlpx/server.cpp +++ b/silkworm/sentry/rlpx/server.cpp @@ -86,8 +86,13 @@ Task Server::run( throw; } - auto remote_endpoint = stream.socket().remote_endpoint(); - log::Debug("sentry") << "rlpx::Server client connected from " << remote_endpoint; + try { + const auto remote_endpoint = stream.socket().remote_endpoint(); + log::Debug("sentry") << "rlpx::Server client connected from " << remote_endpoint; + } catch (const boost::system::system_error& ex) { + log::Debug("sentry") << "rlpx::Server client immediately disconnected [" + std::string{ex.what()} + "]"; + continue; + } auto peer = std::make_shared( client_executor,