26#ifndef ROUTING_BASIC_PROTOCOL_SPLICER_INCLUDED
27#define ROUTING_BASIC_PROTOCOL_SPLICER_INCLUDED
42#include <openssl/ssl.h>
69 return "change-cipher-spec";
99 [[nodiscard]]
virtual std::vector<std::pair<std::string, std::string>>
107template <
class Protocol>
110#ifdef NET_TS_HAS_UNIX_SOCKET
125template <
class Protocol>
144 return sock_.release().and_then(
150 if (!assign_res)
return assign_res;
159 std::function<
void(std::error_code ec,
size_t transferred)>
160 completion)
override {
162 std::move(completion));
166 std::function<
void(std::error_code ec,
size_t transferred)>
167 completion)
override {
168 if (
sock_.native_non_blocking()) {
175 transferred = *write_res]() {
176 completion({}, transferred);
181 const auto ec = write_res.error();
187 ec]() { completion(ec, 0); });
195 std::move(completion));
199 std::function<
void(std::error_code ec)> completion)
override {
204 std::function<
void(std::error_code ec)> completion)
override {
209 std::function<
void(std::error_code ec)> completion)
override {
213 [[nodiscard]]
bool is_open()
const override {
return sock_.is_open(); }
217 return sock_.native_handle();
221 return sock_.close();
225 return sock_.cancel();
230 return sock_.shutdown(st);
233 [[nodiscard]] std::string
endpoint()
const override {
241 template <
class GettableSocketOption>
243 GettableSocketOption &opt)
const {
244 return sock_.get_option(opt);
261template <
class Protocol>
269 [[nodiscard]] std::vector<std::pair<std::string, std::string>>
287#ifdef NET_TS_HAS_UNIX_SOCKET
316 std::unique_ptr<RoutingConnectionBase> routing_conn,
318 std::unique_ptr<ProtocolStateBase> state)
328 std::unique_ptr<RoutingConnectionBase> routing_conn,
330 std::unique_ptr<ProtocolStateBase> state)
339 [[nodiscard]] std::vector<std::pair<std::string, std::string>>
348 conn_ = std::move(conn);
356 template <
class Func>
365 [
this, func = std::forward<Func>(func)](
366 std::error_code ec,
size_t transferred) {
367 if (ec == std::error_code()) {
368 channel_->view_sync_raw();
371 func(ec, transferred);
380 template <
class Func>
382 conn_->async_send(channel_->send_buffer(), std::forward<Func>(func));
390 template <
class Func>
392 conn_->async_wait_send(std::forward<Func>(func));
395 template <
class Func>
397 conn_->async_wait_error(std::forward<Func>(func));
406 [[nodiscard]]
bool is_open()
const {
return conn_ && conn_->is_open(); }
409 return conn_->native_handle();
416 return conn_->close();
424 return conn_->shutdown(st);
428 if (!is_open())
return "";
430 return conn_->endpoint();
435 return routing_conn_->reset_error_count(blocked_endpoints);
440 return routing_conn_->increment_error_count(blocked_endpoints);
444 if (!conn_)
return {};
446 return conn_->cancel();
452 return protocol_.get();
455 std::unique_ptr<ConnectionBase> &
connection() {
return conn_; }
464 return conn_->is_secure_transport() || channel_->ssl();
469 std::unique_ptr<ConnectionBase>
conn_;
491 template <
class Func>
496 template <
class Func>
501 template <
class Func>
506 template <
class Func>
511 template <
class Func>
516 template <
class Func>
557 const auto flush_res =
channel->flush_from_recv_buf();
558 if (!flush_res)
return flush_res.get_unexpected();
561 if (!
channel->tls_init_is_finished()) {
562 const auto res =
channel->tls_accept();
566 const auto flush_res =
channel->flush_to_send_buf();
568 const auto ec = flush_res.error();
570 return flush_res.get_unexpected();
576 return res.get_unexpected();
std::string tls_content_type_to_string(TlsContentType v)
Definition: basic_protocol_splicer.h:66
TlsContentType
Definition: basic_protocol_splicer.h:58
basic connection which wraps a net-ts Protocol.
Definition: basic_protocol_splicer.h:126
stdx::expected< void, std::error_code > set_io_context(net::io_context &new_ctx) override
Definition: basic_protocol_splicer.h:139
stdx::expected< void, std::error_code > get_option(GettableSocketOption &opt) const
Definition: basic_protocol_splicer.h:242
stdx::expected< void, std::error_code > close() override
Definition: basic_protocol_splicer.h:220
void async_wait_error(std::function< void(std::error_code ec)> completion) override
Definition: basic_protocol_splicer.h:208
std::string endpoint() const override
Definition: basic_protocol_splicer.h:233
void async_wait_recv(std::function< void(std::error_code ec)> completion) override
Definition: basic_protocol_splicer.h:203
net::impl::socket::native_handle_type native_handle() const override
Definition: basic_protocol_splicer.h:215
void async_recv(recv_buffer_type &buf, std::function< void(std::error_code ec, size_t transferred)> completion) override
Definition: basic_protocol_splicer.h:158
stdx::expected< void, std::error_code > cancel() override
Definition: basic_protocol_splicer.h:224
bool is_open() const override
Definition: basic_protocol_splicer.h:213
socket_type sock_
Definition: basic_protocol_splicer.h:257
typename protocol_type::socket socket_type
Definition: basic_protocol_splicer.h:129
BasicConnection(socket_type sock, endpoint_type ep)
Definition: basic_protocol_splicer.h:134
bool is_secure_transport() const override
check if the underlying transport is secure.
Definition: basic_protocol_splicer.h:252
net::io_context & io_ctx() override
Definition: basic_protocol_splicer.h:137
endpoint_type ep_
Definition: basic_protocol_splicer.h:258
stdx::expected< void, std::error_code > shutdown(net::socket_base::shutdown_type st) override
Definition: basic_protocol_splicer.h:228
void async_send(recv_buffer_type &buf, std::function< void(std::error_code ec, size_t transferred)> completion) override
Definition: basic_protocol_splicer.h:165
void async_wait_send(std::function< void(std::error_code ec)> completion) override
Definition: basic_protocol_splicer.h:198
typename protocol_type::endpoint endpoint_type
Definition: basic_protocol_splicer.h:130
Definition: blocked_endpoints.h:34
uint64_t increment_error_count(const net::ip::tcp::endpoint &endpoint)
increments the error count of an endpoint.
Definition: blocked_endpoints.cc:45
uint64_t reset_error_count(const net::ip::tcp::endpoint &endpoint)
resets error counter for an endpoint.
Definition: blocked_endpoints.cc:58
SSL aware socket buffers.
Definition: channel.h:64
virtual base-class of BasicConnection.
Definition: connection_base.h:42
std::vector< uint8_t, default_init_allocator< uint8_t > > recv_buffer_type
Definition: connection_base.h:47
splices two connections together.
Definition: basic_protocol_splicer.h:484
void async_send_client(Func &&func)
Definition: basic_protocol_splicer.h:512
void async_client_wait_error(Func &&func)
Definition: basic_protocol_splicer.h:517
TlsSwitchableConnection client_conn_
Definition: basic_protocol_splicer.h:584
void async_send_server(Func &&func)
Definition: basic_protocol_splicer.h:502
const TlsSwitchableConnection & server_conn() const
Definition: basic_protocol_splicer.h:529
void async_wait_send_server(Func &&func)
Definition: basic_protocol_splicer.h:492
Channel * server_channel()
Definition: basic_protocol_splicer.h:547
void async_recv_server(Func &&func)
Definition: basic_protocol_splicer.h:497
const Channel * client_channel() const
Definition: basic_protocol_splicer.h:543
Channel * client_channel()
Definition: basic_protocol_splicer.h:541
void async_recv_client(Func &&func)
Definition: basic_protocol_splicer.h:507
stdx::expected< void, std::error_code > tls_accept()
accept a TLS connection from the client_channel_.
Definition: basic_protocol_splicer.h:552
TlsSwitchableConnection & client_conn()
Definition: basic_protocol_splicer.h:521
SslMode dest_ssl_mode() const
Definition: basic_protocol_splicer.h:537
SslMode source_ssl_mode() const
Definition: basic_protocol_splicer.h:533
ProtocolSplicerBase(TlsSwitchableConnection client_conn, TlsSwitchableConnection server_conn)
Definition: basic_protocol_splicer.h:486
TlsSwitchableConnection server_conn_
Definition: basic_protocol_splicer.h:585
TlsSwitchableConnection & server_conn()
Definition: basic_protocol_splicer.h:527
const TlsSwitchableConnection & client_conn() const
Definition: basic_protocol_splicer.h:523
Definition: basic_protocol_splicer.h:291
virtual ~ProtocolStateBase()=default
Definition: protocol.h:33
Definition: basic_protocol_splicer.h:95
virtual std::vector< std::pair< std::string, std::string > > initial_connection_attributes() const =0
virtual ~RoutingConnectionBase()=default
virtual uint64_t increment_error_count(BlockedEndpoints &blocked_endpoints)=0
virtual uint64_t reset_error_count(BlockedEndpoints &blocked_endpoints)=0
Definition: basic_protocol_splicer.h:262
typename protocol_type::endpoint endpoint_type
Definition: basic_protocol_splicer.h:265
std::vector< std::pair< std::string, std::string > > initial_connection_attributes() const override
Definition: basic_protocol_splicer.h:270
uint64_t increment_error_count(BlockedEndpoints &blocked_endpoints) override
Definition: basic_protocol_splicer.h:278
RoutingConnection(endpoint_type ep)
Definition: basic_protocol_splicer.h:267
endpoint_type ep_
Definition: basic_protocol_splicer.h:283
uint64_t reset_error_count(BlockedEndpoints &blocked_endpoints) override
Definition: basic_protocol_splicer.h:274
a Connection that can be switched to TLS.
Definition: basic_protocol_splicer.h:306
TlsSwitchableConnection(std::unique_ptr< ConnectionBase > conn, std::unique_ptr< RoutingConnectionBase > routing_conn, SslMode ssl_mode, std::unique_ptr< Channel > channel, std::unique_ptr< ProtocolStateBase > state)
Definition: basic_protocol_splicer.h:327
stdx::expected< void, std::error_code > shutdown(net::socket_base::shutdown_type st) const
Definition: basic_protocol_splicer.h:419
std::unique_ptr< ProtocolStateBase > protocol_
Definition: basic_protocol_splicer.h:478
TlsSwitchableConnection(std::unique_ptr< ConnectionBase > conn, std::unique_ptr< RoutingConnectionBase > routing_conn, SslMode ssl_mode, std::unique_ptr< ProtocolStateBase > state)
Definition: basic_protocol_splicer.h:315
void async_send(Func &&func)
async send data from the channel's send buffer to the connection.
Definition: basic_protocol_splicer.h:381
void assign_connection(std::unique_ptr< ConnectionBase > conn)
assign a low-level connection.
Definition: basic_protocol_splicer.h:347
std::unique_ptr< RoutingConnectionBase > routing_conn_
Definition: basic_protocol_splicer.h:470
bool is_secure_transport() const
check if the channel is secure.
Definition: basic_protocol_splicer.h:463
void async_wait_error(Func &&func)
Definition: basic_protocol_splicer.h:396
std::string endpoint() const
Definition: basic_protocol_splicer.h:427
stdx::expected< void, std::error_code > close() const
Definition: basic_protocol_splicer.h:412
const Channel * channel() const
Definition: basic_protocol_splicer.h:402
void async_recv(Func &&func)
async receive data from connection into the channel's receive buffer.
Definition: basic_protocol_splicer.h:357
SslMode ssl_mode_
Definition: basic_protocol_splicer.h:472
const ProtocolStateBase * protocol() const
Definition: basic_protocol_splicer.h:451
Channel * channel()
Definition: basic_protocol_splicer.h:400
void async_wait_send(Func &&func)
async wait until connection allows to send data.
Definition: basic_protocol_splicer.h:391
uint64_t reset_error_count(BlockedEndpoints &blocked_endpoints)
Definition: basic_protocol_splicer.h:433
std::vector< std::pair< std::string, std::string > > initial_connection_attributes() const
Definition: basic_protocol_splicer.h:340
std::unique_ptr< ConnectionBase > & connection()
Definition: basic_protocol_splicer.h:455
net::impl::socket::native_handle_type native_handle() const
Definition: basic_protocol_splicer.h:408
std::unique_ptr< Channel > channel_
Definition: basic_protocol_splicer.h:475
bool is_open() const
Definition: basic_protocol_splicer.h:406
uint64_t increment_error_count(BlockedEndpoints &blocked_endpoints)
Definition: basic_protocol_splicer.h:438
static constexpr size_t kRecvBufferSize
Definition: basic_protocol_splicer.h:313
ProtocolStateBase * protocol()
Definition: basic_protocol_splicer.h:449
std::unique_ptr< ConnectionBase > conn_
Definition: basic_protocol_splicer.h:469
SslMode ssl_mode() const
Definition: basic_protocol_splicer.h:404
stdx::expected< void, std::error_code > cancel()
Definition: basic_protocol_splicer.h:443
Definition: basic_protocol_splicer.h:83
SslMode ssl_mode() const
Definition: basic_protocol_splicer.h:89
SslMode ssl_mode_
Definition: basic_protocol_splicer.h:92
std::function< SSL_CTX *(const std::string &id)> ssl_ctx_gettor_type
Definition: basic_protocol_splicer.h:85
TlsSwitchable(SslMode ssl_mode)
Definition: basic_protocol_splicer.h:87
Definition: io_context.h:61
executor_type get_executor() noexcept
Definition: io_context.h:1080
static constexpr wait_type wait_read
Definition: socket.h:168
shutdown_type
Definition: socket.h:172
static constexpr wait_type wait_write
Definition: socket.h:169
static constexpr wait_type wait_error
Definition: socket.h:170
Definition: expected.h:944
#define harness_assert(COND)
Improved assert()
Definition: harness_assert.h:37
std::vector< std::pair< std::string, std::string > > initial_connection_attributes(const net::ip::tcp::endpoint &ep)
TCP/IP socket related connection attributes.
Definition: initial_connection_attributes.h:43
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
static MYSQL * sock
Definition: mysqlcheck.cc:56
Definition: buf0block_hint.cc:30
Unique_ptr< T, std::nullptr_t > make_unique(size_t size)
In-place constructs a new unique pointer with no specific allocator and with array type T.
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
int native_handle_type
Definition: socket_constants.h:51
dynamic_vector_buffer< T, Allocator > dynamic_buffer(std::vector< T, Allocator > &vec) noexcept
Definition: buffer.h:663
auto defer(CompletionToken &&token)
Definition: executor.h:804
std::enable_if_t< is_const_buffer_sequence< ConstBufferSequence >::value, stdx::expected< size_t, std::error_code > > write(SyncWriteStream &stream, const ConstBufferSequence &buffers)
Definition: buffer.h:992
std::enable_if_t< is_dynamic_buffer< DynamicBuffer >::value, void > async_read(AsyncReadStream &stream, DynamicBuffer &&b, CompletionCondition completion_condition, CompletionToken &&token)
Definition: buffer.h:928
std::error_condition make_error_condition(net::stream_errc e) noexcept
Definition: buffer.h:107
std::enable_if_t< is_dynamic_buffer< DynamicBuffer >::value, void > async_write(AsyncWriteStream &stream, DynamicBuffer &&b, CompletionCondition cond, CompletionToken &&token)
Definition: buffer.h:1074
Definition: gcs_xcom_synode.h:64
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:125
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:642
SslMode
Definition: ssl_mode.h:29
Definition: basic_protocol_splicer.h:108
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510