20 #include <ripple/overlay/impl/ConnectAttempt.h>
21 #include <ripple/overlay/impl/PeerImp.h>
22 #include <ripple/overlay/impl/ProtocolVersion.h>
23 #include <ripple/overlay/Cluster.h>
24 #include <ripple/json/json_reader.h>
38 , remote_endpoint_ (remote_endpoint)
40 , strand_ (io_service)
44 , socket_ (stream_ptr_->next_layer().socket())
45 , stream_ (*stream_ptr_)
49 "Connect " << remote_endpoint;
63 if (!
strand_.running_in_this_thread())
87 assert(
strand_.running_in_this_thread());
119 "setTimer: " << ec.message();
125 std::placeholders::_1)));
140 if (ec == boost::asio::error::operation_aborted)
146 "onTimer: " << ec.message();
157 if(ec == boost::asio::error::operation_aborted)
161 local_endpoint =
socket_.local_endpoint(ec);
163 return fail(
"onConnect", ec);
170 stream_.set_verify_mode (boost::asio::ssl::verify_none);
171 stream_.async_handshake (boost::asio::ssl::stream_base::client,
182 if(ec == boost::asio::error::operation_aborted)
186 local_endpoint =
socket_.local_endpoint(ec);
188 return fail(
"onHandshake", ec);
194 return fail(
"Duplicate connection");
217 if(ec == boost::asio::error::operation_aborted)
220 return fail(
"onWrite", ec);
233 if(ec == boost::asio::error::operation_aborted)
235 if(ec == boost::asio::error::eof)
242 std::placeholders::_1)));
245 return fail(
"onRead", ec);
256 "onShutdown: expected error condition";
259 if (ec != boost::asio::error::eof)
260 return fail(
"onShutdown", ec);
270 m.method(boost::beast::http::verb::get);
275 m.insert (
"Connection",
"Upgrade");
276 m.insert (
"Connect-As",
"Peer");
277 m.insert (
"Crawl", crawl ?
"public" :
"private");
278 if (compressionEnabled)
279 m.insert(
"X-Offer-Compression",
"lz4");
286 if (
response_.result() == boost::beast::http::status::service_unavailable)
292 for (
auto const& buffer :
response_.body().data())
294 boost::asio::buffer_cast<char const*>(buffer),
295 boost::asio::buffer_size(buffer));
296 auto const success = r.
parse(s, json);
306 for (
auto const& v : ips)
324 JLOG(
journal_.
info()) <<
"Unable to upgrade to peer protocol: " <<
331 boost::optional<ProtocolVersion> negotiatedProtocol;
337 negotiatedProtocol = pvs[0];
339 if (!negotiatedProtocol)
340 return fail(
"processResponse: Unable to negotiate protocol version");
357 "Protocol: " <<
to_string(*negotiatedProtocol);
366 publicKey,
static_cast<bool>(member));
368 return fail(
"Outbound slots full");
370 auto const peer = std::make_shared<PeerImp>(
app_, std::move(
stream_ptr_),
void onHandshake(error_code ec)
endpoint_type remote_endpoint_
virtual Cluster & cluster()=0
boost::asio::io_service::strand strand_
Stream trace() const
Severity stream access functions.
const beast::Journal journal_
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.
boost::beast::ssl_stream< middle_type > stream_type
std::string to_string(ListDisposition disposition)
boost::beast::http::request< boost::beast::http::empty_body > request_type
Unserialize a JSON document into a Value.
void buildHandshake(boost::beast::http::fields &h, ripple::uint256 const &sharedValue, boost::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.
Setup const & setup() const
boost::asio::ip::tcp::socket socket_type
bool peerPrivate
true if we want our IP address kept private.
void onRead(error_code ec)
std::shared_ptr< PeerFinder::Slot > slot_
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
PublicKey verifyHandshake(boost::beast::http::fields const &headers, ripple::uint256 const &sharedValue, boost::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.
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
boost::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
PeerFinder::Manager & peerFinder()
static request_type makeRequest(bool crawl, bool compressionEnabled)
boost::system::error_code error_code
T shared_from_this(T... args)
virtual Config & config()=0
boost::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
void onShutdown(error_code ec)
UInt size() const
Number of values in array or object.
Resource::Consumer usage_
bool isMember(const char *key) const
Return true if the object has a member named key.
A generic endpoint for log messages.
boost::asio::ip::tcp::endpoint endpoint_type
void fail(std::string const &reason)
std::string const & getFullVersionString()
Full server version string.
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
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 Config config()=0
Returns the configuration for the manager.
std::unique_ptr< stream_type > stream_ptr_
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
An endpoint that consumes resources.
boost::asio::basic_waitable_timer< std::chrono::steady_clock > timer_
void onConnect(error_code ec)
bool isProtocolSupported(ProtocolVersion const &v)
Determine whether we support a specific protocol version.
ConnectAttempt(Application &app, boost::asio::io_service &io_service, endpoint_type const &remote_endpoint, Resource::Consumer usage, shared_context const &context, std::uint32_t id, std::shared_ptr< PeerFinder::Slot > const &slot, beast::Journal journal, OverlayImpl &overlay)
static boost::asio::ip::tcp::endpoint parse_endpoint(std::string const &s, boost::system::error_code &ec)
static bool isPeerUpgrade(http_request_type const &request)
void add_active(std::shared_ptr< PeerImp > const &peer)
boost::beast::multi_buffer read_buf_
void onTimer(error_code ec)
void onWrite(error_code ec)