20#include <xrpld/overlay/Cluster.h>
21#include <xrpld/overlay/detail/ConnectAttempt.h>
22#include <xrpld/overlay/detail/PeerImp.h>
23#include <xrpld/overlay/detail/ProtocolVersion.h>
25#include <xrpl/json/json_reader.h>
33 boost::asio::io_context& io_context,
46 , remote_endpoint_(remote_endpoint)
48 , strand_(
boost::asio::make_strand(io_context))
50 , stepTimer_(io_context)
54 , socket_(stream_ptr_->next_layer().socket())
55 , stream_(*stream_ptr_)
71 if (!
strand_.running_in_this_thread())
72 return boost::asio::post(
86 if (!
strand_.running_in_this_thread())
87 return boost::asio::post(
97 stream_.next_layer().async_connect(
99 boost::asio::bind_executor(
104 std::placeholders::_1)));
113 strand_.running_in_this_thread(),
114 "ripple::ConnectAttempt::shutdown: strand in this thread");
120 boost::beast::get_lowest_layer(
stream_).cancel();
129 strand_.running_in_this_thread(),
130 "ripple::ConnectAttempt::tryAsyncShutdown : strand in this thread");
143 return stream_.async_shutdown(bind_executor(
148 std::placeholders::_1)));
168 (ec != boost::asio::error::eof &&
169 ec != boost::asio::error::operation_aborted &&
170 ec.message().find(
"application data after close notify") ==
186 strand_.running_in_this_thread(),
187 "ripple::ConnectAttempt::close : strand in this thread");
217 if (
timer_.expiry() == std::chrono::steady_clock::time_point{})
222 timer_.async_wait(boost::asio::bind_executor(
227 std::placeholders::_1)));
264 stepTimer_.async_wait(boost::asio::bind_executor(
269 std::placeholders::_1)));
272 <<
" timeout=" << stepTimeout.
count() <<
"s";
290 catch (boost::system::system_error
const&)
305 if (ec == boost::asio::error::operation_aborted)
315 bool globalExpired = (
timer_.expiry() <= now);
316 bool stepExpired = (
stepTimer_.expiry() <= now);
323 else if (stepExpired)
330 JLOG(
journal_.
warn()) <<
"onTimer: Unexpected timer callback";
343 if (ec == boost::asio::error::operation_aborted)
346 return fail(
"onConnect", ec);
355 return fail(
"onConnect", ec);
364 stream_.set_verify_mode(boost::asio::ssl::verify_none);
366 boost::asio::ssl::stream_base::client,
367 boost::asio::bind_executor(
372 std::placeholders::_1)));
382 if (ec == boost::asio::error::operation_aborted)
385 return fail(
"onHandshake", ec);
388 auto const local_endpoint =
socket_.local_endpoint(ec);
390 return fail(
"onHandshake", ec);
397 return fail(
"Self connection");
423 boost::beast::http::async_write(
426 boost::asio::bind_executor(
431 std::placeholders::_1)));
441 if (ec == boost::asio::error::operation_aborted)
444 return fail(
"onWrite", ec);
454 boost::beast::http::async_read(
458 boost::asio::bind_executor(
463 std::placeholders::_1)));
475 if (ec == boost::asio::error::eof)
481 if (ec == boost::asio::error::operation_aborted)
484 return fail(
"onRead", ec);
503 boost::beast::http::status::service_unavailable)
506 <<
"Unable to upgrade to peer protocol: " <<
response_.result()
515 for (
auto const buffer :
response_.body().data())
517 static_cast<char const*
>(buffer.data()),
518 boost::asio::buffer_size(buffer));
522 auto const isValidJson = reader.
parse(responseBody, json);
525 auto const isRedirect =
532 <<
" failed to upgrade to peer protocol: " <<
response_.result()
540 return fail(
"processResponse: invalid peer-ips format");
546 for (
auto const& ipValue : peerIps)
548 if (!ipValue.isString())
560 return fail(
"processResponse: failed to connect to peer: redirected");
571 negotiatedProtocol = pvs[0];
573 if (!negotiatedProtocol)
575 "processResponse: Unable to negotiate protocol version");
593 <<
"Protocol: " <<
to_string(*negotiatedProtocol);
605 if (result != PeerFinder::Result::success)
Unserialize a JSON document into a Value.
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
UInt size() const
Number of values in array or object.
bool isMember(char const *key) const
Return true if the object has a member named key.
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
virtual Config & config()=0
virtual Cluster & cluster()=0
std::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
bool VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
bool TX_REDUCE_RELAY_ENABLE
static constexpr std::chrono::seconds connectTimeout
void stop() override
Stop the connection attempt.
boost::asio::ip::tcp::socket socket_type
void cancelTimer()
Cancel both global and step timers.
boost::system::error_code error_code
std::unique_ptr< stream_type > stream_ptr_
std::shared_ptr< PeerFinder::Slot > slot_
ConnectAttempt(Application &app, boost::asio::io_context &io_context, endpoint_type const &remote_endpoint, Resource::Consumer usage, shared_context const &context, Peer::id_t id, std::shared_ptr< PeerFinder::Slot > const &slot, beast::Journal journal, OverlayImpl &overlay)
Construct a new ConnectAttempt object.
ConnectionStep currentStep_
Resource::Consumer usage_
boost::asio::strand< boost::asio::io_context::executor_type > strand_
static std::string stepToString(ConnectionStep step)
void run()
Begin the connection attempt.
void onHandshake(error_code ec)
boost::asio::ip::tcp::endpoint endpoint_type
void onWrite(error_code ec)
void processResponse()
Process the HTTP upgrade response from peer.
void onTimer(error_code ec)
Handle timer expiration events.
void setTimer(ConnectionStep step)
Set timers for the specified connection step.
void onShutdown(error_code ec)
boost::beast::ssl_stream< middle_type > stream_type
boost::beast::multi_buffer read_buf_
void onConnect(error_code ec)
void fail(std::string const &reason)
void onRead(error_code ec)
static boost::asio::ip::tcp::endpoint parse_endpoint(std::string const &s, boost::system::error_code &ec)
beast::Journal const journal_
ConnectionStep
Represents the current phase of the connection establishment process.
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
endpoint_type remote_endpoint_
boost::asio::basic_waitable_timer< std::chrono::steady_clock > stepTimer_
PeerFinder::Manager & peerFinder()
void add_active(std::shared_ptr< PeerImp > const &peer)
static bool isPeerUpgrade(http_request_type const &request)
Setup const & setup() const
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
virtual Config config()=0
Returns the configuration for the manager.
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
virtual void onRedirects(boost::asio::ip::tcp::endpoint const &remote_address, std::vector< boost::asio::ip::tcp::endpoint > const &eps)=0
Called when we received redirect IPs from a busy peer.
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
An endpoint that consumes resources.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::vector< ProtocolVersion > parseProtocolVersions(boost::beast::string_view const &value)
Parse a set of protocol versions.
void buildHandshake(boost::beast::http::fields &h, ripple::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote_ip, Application &app)
Insert fields headers necessary for upgrading the link to the peer protocol.
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
std::string to_string(base_uint< Bits, Tag > const &a)
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
bool isProtocolSupported(ProtocolVersion const &v)
Determine whether we support a specific protocol version.
PublicKey verifyHandshake(boost::beast::http::fields const &headers, ripple::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote, Application &app)
Validate header fields necessary for upgrading the link to the peer protocol.
T shared_from_this(T... args)
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
static constexpr std::chrono::seconds tcpConnect
static constexpr std::chrono::seconds tlsShutdown
static constexpr std::chrono::seconds tlsHandshake
static constexpr std::chrono::seconds httpWrite
static constexpr std::chrono::seconds httpRead
beast::IP::Address public_ip
std::optional< std::uint32_t > networkID
bool peerPrivate
true if we want our IP address kept private.