3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/net/IPAddressConversion.h>
5#include <xrpl/beast/net/IPEndpoint.h>
7#include <boost/asio.hpp>
8#include <boost/asio/ip/tcp.hpp>
9#include <boost/asio/ssl.hpp>
10#include <boost/beast/core/bind_handler.hpp>
27 using SslSocket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
39 boost::asio::io_context& s,
40 boost::asio::ssl::context& c,
44 ,
buffer_((plainOnly || secureOnly) ? 0 : 4)
45 ,
j_{
beast::Journal::getNullSink()}
50 AutoSocket(boost::asio::io_context& s, boost::asio::ssl::context& c)
97 boost::system::error_code
98 cancel(boost::system::error_code& ec)
106 if ((type == SslSocket::client) || (
secure_))
110 socket_->async_handshake(type, cbFunc);
116 post(
socket_->get_executor(), boost::beast::bind_handler(cbFunc,
ErrorCode()));
121 socket_->next_layer().async_receive(
123 boost::asio::socket_base::message_peek,
124 [
this, cbFunc](
ErrorCode const& ec,
size_t bytesTransferred) {
130 template <
typename ShutdownHandler>
136 socket_->async_shutdown(handler);
143 lowestLayer().shutdown(PlainSocket::shutdown_both);
145 catch (boost::system::system_error
const& e)
149 post(
socket_->get_executor(), boost::beast::bind_handler(handler, ec));
153 template <
typename Seq,
typename Handler>
159 socket_->async_read_some(buffers, handler);
167 template <
typename Seq,
typename Condition,
typename Handler>
173 boost::asio::async_read_until(*
socket_, buffers, condition, handler);
177 boost::asio::async_read_until(
plainSocket(), buffers, condition, handler);
181 template <
typename Allocator,
typename Handler>
184 boost::asio::basic_streambuf<Allocator>& buffers,
190 boost::asio::async_read_until(*
socket_, buffers, delim, handler);
194 boost::asio::async_read_until(
plainSocket(), buffers, delim, handler);
198 template <
typename Allocator,
typename MatchCondition,
typename Handler>
201 boost::asio::basic_streambuf<Allocator>& buffers,
207 boost::asio::async_read_until(*
socket_, buffers, cond, handler);
211 boost::asio::async_read_until(
plainSocket(), buffers, cond, handler);
215 template <
typename Buf,
typename Handler>
221 boost::asio::async_write(*
socket_, buffers, handler);
225 boost::asio::async_write(
plainSocket(), buffers, handler);
229 template <
typename Allocator,
typename Handler>
231 asyncWrite(boost::asio::basic_streambuf<Allocator>& buffers, Handler handler)
235 boost::asio::async_write(*
socket_, buffers, handler);
239 boost::asio::async_write(
plainSocket(), buffers, handler);
243 template <
typename Buf,
typename Condition,
typename Handler>
245 asyncRead(Buf
const& buffers, Condition cond, Handler handler)
249 boost::asio::async_read(*
socket_, buffers, cond, handler);
253 boost::asio::async_read(
plainSocket(), buffers, cond, handler);
257 template <
typename Allocator,
typename Condition,
typename Handler>
259 asyncRead(boost::asio::basic_streambuf<Allocator>& buffers, Condition cond, Handler handler)
263 boost::asio::async_read(*
socket_, buffers, cond, handler);
267 boost::asio::async_read(
plainSocket(), buffers, cond, handler);
271 template <
typename Buf,
typename Handler>
277 boost::asio::async_read(*
socket_, buffers, handler);
281 boost::asio::async_read(
plainSocket(), buffers, handler);
285 template <
typename Seq,
typename Handler>
291 socket_->async_write_some(buffers, handler);
303 using namespace xrpl;
307 JLOG(
j_.warn()) <<
"Handle autodetect error: " << ec;
312 ((bytesTransferred < 2) || ((
buffer_[1] < 127) && (
buffer_[1] > 31))) &&
313 ((bytesTransferred < 3) || ((
buffer_[2] < 127) && (
buffer_[2] > 31))) &&
314 ((bytesTransferred < 4) || ((
buffer_[3] < 127) && (
buffer_[3] > 31))))
317 JLOG(
j_.trace()) <<
"non-SSL";
324 JLOG(
j_.trace()) <<
"SSL";
326 socket_->async_handshake(SslSocket::server, cbFunc);
boost::system::error_code ErrorCode
SslSocket::next_layer_type PlainSocket
AutoSocket(boost::asio::io_context &s, boost::asio::ssl::context &c, bool secureOnly, bool plainOnly)
lowest_layer_type & lowestLayer()
beast::ip::Endpoint localEndpoint()
std::vector< char > buffer_
void asyncRead(Buf const &buffers, Condition cond, Handler handler)
AutoSocket(boost::asio::io_context &s, boost::asio::ssl::context &c)
SslSocket::handshake_type HandshakeType
std::function< void(ErrorCode)> Callback
void asyncReadUntil(Seq const &buffers, Condition condition, Handler handler)
void asyncReadUntil(boost::asio::basic_streambuf< Allocator > &buffers, std::string const &delim, Handler handler)
void handleAutodetect(Callback cbFunc, ErrorCode const &ec, size_t bytesTransferred)
void asyncHandshake(HandshakeType type, Callback cbFunc)
void asyncShutdown(ShutdownHandler handler)
void swap(AutoSocket &s) noexcept
std::unique_ptr< SslSocket > SocketPtr
beast::ip::Endpoint remoteEndpoint()
void asyncRead(Buf const &buffers, Handler handler)
void asyncWriteSome(Seq const &buffers, Handler handler)
void asyncRead(boost::asio::basic_streambuf< Allocator > &buffers, Condition cond, Handler handler)
SslSocket::lowest_layer_type lowest_layer_type
boost::asio::ip::tcp::socket::endpoint_type EndpointType
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
void asyncReadSome(Seq const &buffers, Handler handler)
boost::system::error_code cancel(boost::system::error_code &ec)
PlainSocket & plainSocket()
void asyncWrite(Buf const &buffers, Handler handler)
void asyncReadUntil(boost::asio::basic_streambuf< Allocator > &buffers, MatchCondition cond, Handler handler)
void asyncWrite(boost::asio::basic_streambuf< Allocator > &buffers, Handler handler)
A generic endpoint for log messages.
A version-independent IP address and port combination.
Endpoint fromAsio(boost::asio::ip::address const &address)
Convert to Endpoint.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.