MySQL 8.3.0
Source Code Documentation
x_connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef ROUTING_X_CONNECTION_INCLUDED
26#define ROUTING_X_CONNECTION_INCLUDED
27
28#include <memory>
29#include <optional>
30#include <string>
31
32#include <mysqlx.pb.h>
33#include <mysqlx_connection.pb.h>
34
35#include "connection.h" // MySQLRoutingConnectionBase
36
38 public:
39 struct FrameInfo {
40 size_t frame_size_; //!< size of the whole frame
41 size_t forwarded_frame_size_; //!< size of the forwarded part of frame
42 };
43
44 std::optional<FrameInfo> &current_frame() { return current_frame_; }
45 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
46
48 void caps(std::unique_ptr<Mysqlx::Connection::Capabilities> caps) {
49 caps_ = std::move(caps);
50 }
51
52 private:
53 std::optional<FrameInfo> current_frame_{};
54 std::optional<uint8_t> msg_type_{};
55
56 std::unique_ptr<Mysqlx::Connection::Capabilities> caps_;
57};
58
61 public std::enable_shared_from_this<MysqlRoutingXConnection> {
62 private:
63 // constructor
64 //
65 // use ::create() instead.
67 MySQLRoutingContext &context, RouteDestination *route_destination,
68 std::unique_ptr<ConnectionBase> client_connection,
69 std::unique_ptr<RoutingConnectionBase> client_routing_connection,
70 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
71 : MySQLRoutingConnectionBase{context, std::move(remove_callback)},
72 route_destination_{route_destination},
73 destinations_{route_destination_->destinations()},
74 connector_{client_connection->io_ctx(), route_destination,
77 TlsSwitchableConnection{std::move(client_connection),
78 std::move(client_routing_connection),
83
84 public:
86
87 // create a shared_ptr<ThisClass>
88 template <typename... Args>
89 [[nodiscard]] static std::shared_ptr<MysqlRoutingXConnection> create(
90 // clang-format off
91 Args &&... args) {
92 // clang-format on
93
94 // can't use make_unique<> here as the constructor is private.
95 return std::shared_ptr<MysqlRoutingXConnection>(
96 new MysqlRoutingXConnection(std::forward<Args>(args)...));
97 }
98
99 // get a shared-ptr that refers the same 'this'
100 std::shared_ptr<MysqlRoutingXConnection> getptr() {
101 return shared_from_this();
102 }
103
105 std::vector<uint8_t> &error_frame, uint16_t error_code,
106 const std::string &msg, const std::string &sql_state = "HY000",
108
110 return this->socket_splicer()->source_ssl_mode();
111 }
112
114 return this->socket_splicer()->dest_ssl_mode();
115 }
116
119 }
120
121 std::string get_client_address() const override {
122 return socket_splicer()->client_conn().endpoint();
123 }
124
125 std::string get_server_address() const override {
126 return socket_splicer()->server_conn().endpoint();
127 }
128
129 void disconnect() override;
130
131 enum class Function {
133
134 // tls-accept
138
141
144
148
151
152 // cap-get
161
162 // cap-set
167
168 // sess-auth
175
176 // stmt-exec
181
182 // crud::find
187
188 // crud::delete
193
194 // crud::insert
199
200 // crud::update
205
206 // prepare::prepare
211
212 // prepare::deallocate
217
218 // prepare::execute
223
224 // expect::open
229
230 // expect::close
235
236 // crud::create_view
241
242 // crud::modify_view
247
248 // crud::drop_view
253
254 // cursor::open
259
260 // cursor::fetch
265
266 // cursor::close
271
272 // session::close
277
278 // session::reset
283
284 kConnect,
286 kFinish,
287 };
288
290 switch (next) {
292 return client_recv_cmd();
293
295 return tls_accept_init();
297 return tls_accept();
299 return tls_accept_finalize();
300
302 return server_init_tls();
305
307 return tls_connect_init();
309 return tls_connect();
310
312 return server_send_check_caps();
315
317 return client_cap_get();
324
326 return client_cap_set();
341
343 return forward_tls_init();
348
350 return client_sess_auth_start();
361
363 return client_stmt_execute();
370
372 return client_crud_find();
379
381 return client_crud_delete();
388
390 return client_crud_insert();
397
399 return client_crud_update();
406
408 return client_prepare_prepare();
415
424
426 return client_prepare_execute();
433
435 return client_expect_open();
442
444 return client_expect_close();
451
460
469
471 return client_crud_drop_view();
478
480 return client_cursor_open();
487
489 return client_cursor_fetch();
496
498 return client_cursor_close();
505
507 return client_session_close();
514
516 return client_session_reset();
523
525 return connect();
527 return wait_client_close();
529 return finish();
530 }
531 }
532
533 void send_server_failed(std::error_code ec);
534
535 void recv_server_failed(std::error_code ec);
536
537 void send_client_failed(std::error_code ec);
538
539 void recv_client_failed(std::error_code ec);
540
541 void client_socket_failed(std::error_code ec);
542
543 void server_socket_failed(std::error_code ec);
544
545 void async_send_client(Function next);
546
547 void async_recv_client(Function next);
548
549 void async_send_server(Function next);
550
551 void async_recv_server(Function next);
552
553 void async_run();
554
555 void wait_client_close();
556 void finish();
557
558 void server_tls_shutdown();
559 void client_tls_shutdown();
560
561 void done();
562
564 Channel *dst_channel);
565
567
569
570 void forward_tls_init();
571
572 enum class ForwardResult {
577 kFinished,
578 };
579
582
585
586 void forward_client_to_server(Function this_func, Function next_func);
587
588 void forward_server_to_client(Function this_func, Function next_func);
589
592
595 void connect();
596
600
601 void tls_accept_init();
602 void tls_accept();
603 void tls_accept_finalize();
604
605 void server_init_tls();
607 void tls_connect_init();
608 void tls_connect();
609
610 void client_con_close();
611
612 void client_recv_cmd();
613
614 void client_cap_get();
618
619 void client_cap_set();
627
629
636
637 void client_stmt_execute();
641
642 void client_crud_find();
646
647 void client_crud_delete();
651
652 void client_crud_insert();
656
657 void client_crud_update();
661
666
671
676
677 void client_expect_open();
681
682 void client_expect_close();
686
691
696
701
702 void client_cursor_open();
706
707 void client_cursor_fetch();
711
712 void client_cursor_close();
716
721
726
728 return dynamic_cast<XProtocolState *>(
730 }
731
733 return dynamic_cast<XProtocolState *>(
735 }
736
738 return socket_splicer_.get();
739 }
740
742
743 std::string get_destination_id() const override {
744 return connector().destination_id();
745 }
746
747 private:
749
750 using clock_type = std::chrono::steady_clock;
751
752 clock_type::time_point started_{clock_type::now()};
753 clock_type::time_point last_trace_{clock_type::now()};
754
756
758 const connector_type &connector() const { return connector_; }
759
763
764 std::unique_ptr<ProtocolSplicerBase> socket_splicer_;
765};
766
767#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
SSL aware socket buffers.
Definition: channel.h:63
void destination_id(std::string id)
Definition: connection.h:198
A forward iterable container of destinations.
Definition: destination.h:106
Definition: connection.h:42
std::chrono::system_clock clock_type
Definition: connection.h:90
MySQLRoutingContext & context()
Definition: connection.h:51
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:58
Definition: x_connection.h:61
void client_cap_get()
client wants to get the capabilities of the server.
Definition: x_connection.cc:856
void recv_client_failed(std::error_code ec)
Definition: x_connection.cc:350
void server_recv_switch_tls_response()
Definition: x_connection.cc:916
void async_send_server(Function next)
Definition: x_connection.cc:424
void server_recv_prepare_deallocate_response_forward()
Definition: x_connection.cc:2378
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2583
void server_recv_crud_delete_response()
Definition: x_connection.cc:2077
MysqlRoutingXConnection(MySQLRoutingContext &context, RouteDestination *route_destination, std::unique_ptr< ConnectionBase > client_connection, std::unique_ptr< RoutingConnectionBase > client_routing_connection, std::function< void(MySQLRoutingConnectionBase *)> remove_callback)
Definition: x_connection.h:66
void client_crud_modify_view()
Definition: x_connection.cc:2655
void client_cursor_open()
Definition: x_connection.cc:2784
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1328
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:1991
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2450
void client_cursor_fetch()
Definition: x_connection.cc:2851
void client_stmt_execute()
Definition: x_connection.cc:1934
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2123
void server_recv_cap_get_response()
Definition: x_connection.cc:1284
stdx::expected< ForwardResult, std::error_code > forward_frame_from_server_to_client()
Definition: x_connection.cc:707
void client_prepare_execute()
Definition: x_connection.cc:2393
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1833
void server_recv_switch_tls_response_passthrough_forward_last()
Definition: x_connection.cc:1075
void server_recv_crud_modify_view_response_forward()
Definition: x_connection.cc:2706
void async_run()
Definition: x_connection.cc:310
void server_recv_cursor_close_response()
Definition: x_connection.cc:2925
XProtocolState * client_protocol()
Definition: x_connection.h:727
void server_tls_shutdown()
Definition: x_connection.cc:3158
void client_sess_auth_start()
Definition: x_connection.cc:1840
stdx::expected< ForwardResult, std::error_code > forward_frame_from_client_to_server()
Definition: x_connection.cc:675
void client_crud_drop_view()
Definition: x_connection.cc:2720
void connect()
Definition: x_connection.cc:738
void server_recv_crud_find_response()
Definition: x_connection.cc:2008
void client_crud_create_view()
Definition: x_connection.cc:2590
void server_recv_cap_get_response_forward_last()
Definition: x_connection.cc:1333
void server_recv_expect_close_response_forward()
Definition: x_connection.cc:2578
void call_next_function(Function next)
Definition: x_connection.h:289
int active_work_
Definition: x_connection.h:748
void client_tls_shutdown()
Definition: x_connection.cc:3165
SslMode source_ssl_mode() const
Definition: x_connection.h:109
SslMode dest_ssl_mode() const
Definition: x_connection.h:113
connector_type connector_
Definition: x_connection.h:762
void client_crud_update()
Definition: x_connection.cc:2198
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:1996
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3034
void forward_tls_server_to_client()
Definition: x_connection.cc:1158
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2777
std::unique_ptr< ProtocolSplicerBase > socket_splicer_
Definition: x_connection.h:764
void client_recv_auth_continue()
Definition: x_connection.cc:1922
static stdx::expected< size_t, std::error_code > encode_error_packet(std::vector< uint8_t > &error_frame, uint16_t error_code, const std::string &msg, const std::string &sql_state="HY000", Mysqlx::Error::Severity severity=Mysqlx::Error::ERROR)
Definition: x_connection.cc:285
Function
Definition: x_connection.h:131
const ProtocolSplicerBase * socket_splicer() const
Definition: x_connection.h:737
void server_recv_expect_close_response()
Definition: x_connection.cc:2532
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2385
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2456
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3098
void tls_connect_init()
Definition: x_connection.cc:1195
bool greeting_from_router_
Definition: x_connection.h:755
void client_session_reset()
Definition: x_connection.cc:3047
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3040
void server_recv_prepare_prepare_response_forward_last()
Definition: x_connection.cc:2318
void client_expect_close()
Definition: x_connection.cc:2527
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1828
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2844
void forward_client_to_server(Function this_func, Function next_func)
Definition: x_connection.cc:685
void disconnect() override
Definition: x_connection.cc:244
void server_recv_cursor_fetch_response()
Definition: x_connection.cc:2856
void server_socket_failed(std::error_code ec)
Definition: x_connection.cc:358
std::string get_destination_id() const override
Definition: x_connection.h:743
void server_recv_session_close_response()
Definition: x_connection.cc:2988
void async_send_client(Function next)
Definition: x_connection.cc:389
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2839
void async_send_server_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1356
void send_server_failed(std::error_code ec)
Definition: x_connection.cc:326
void server_recv_server_greeting_from_server()
Definition: x_connection.cc:3111
void send_client_failed(std::error_code ec)
Definition: x_connection.cc:342
void async_send_client_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1338
void client_con_close()
Definition: x_connection.cc:300
void server_recv_crud_insert_response_forward_last()
Definition: x_connection.cc:2191
void server_recv_switch_tls_response_passthrough_forward()
Definition: x_connection.cc:1068
void server_recv_cursor_close_response_forward()
Definition: x_connection.cc:2971
void forward_tls_client_to_server()
Definition: x_connection.cc:1142
void client_session_close()
Definition: x_connection.cc:2983
ForwardResult
Definition: x_connection.h:572
void client_cursor_close()
Definition: x_connection.cc:2920
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2913
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:373
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2266
void server_recv_auth_response_forward()
Definition: x_connection.cc:1912
std::string get_client_address() const override
Returns address of client which connected to router.
Definition: x_connection.h:121
void server_recv_auth_response_continue()
Definition: x_connection.cc:1917
ProtocolSplicerBase * socket_splicer()
Definition: x_connection.h:741
void tls_accept_finalize()
Definition: x_connection.cc:1605
void server_recv_session_reset_response()
Definition: x_connection.cc:3052
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2065
void server_send_switch_to_tls()
Definition: x_connection.cc:1684
void client_send_server_greeting_from_router()
Definition: x_connection.cc:459
void client_recv_cmd()
Definition: x_connection.cc:463
void forward_tls_init()
Definition: x_connection.cc:1174
void client_expect_open()
Definition: x_connection.cc:2464
void server_recv_check_caps_response()
Definition: x_connection.cc:1712
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2641
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2725
stdx::expected< void, std::error_code > forward_tls(Channel *src_channel, Channel *dst_channel)
Definition: x_connection.cc:1088
void server_send_check_caps()
Definition: x_connection.cc:1699
XProtocolState * server_protocol()
Definition: x_connection.h:732
void forward_server_to_client(Function this_func, Function next_func)
Definition: x_connection.cc:717
void recv_server_failed(std::error_code ec)
Definition: x_connection.cc:334
void server_recv_crud_insert_response_forward()
Definition: x_connection.cc:2186
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2060
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2520
void server_recv_auth_response()
Definition: x_connection.cc:1863
std::string get_server_address() const override
Returns address of server to which connection is established.
Definition: x_connection.h:125
void server_recv_expect_open_response()
Definition: x_connection.cc:2469
void done()
Definition: x_connection.cc:3156
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1225
void client_crud_insert()
Definition: x_connection.cc:2135
void server_recv_cursor_open_response()
Definition: x_connection.cc:2789
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2771
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2908
void server_recv_cap_set_response()
Definition: x_connection.cc:1782
void server_recv_crud_update_response()
Definition: x_connection.cc:2203
void tls_accept()
accept a TLS handshake.
Definition: x_connection.cc:1571
void server_recv_crud_update_response_forward_last()
Definition: x_connection.cc:2254
void tls_accept_init()
Definition: x_connection.cc:1550
void server_recv_switch_tls_response_passthrough()
Definition: x_connection.cc:1018
void server_recv_crud_modify_view_response_forward_last()
Definition: x_connection.cc:2712
void client_prepare_prepare()
Definition: x_connection.cc:2261
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1927
void server_recv_crud_insert_response()
Definition: x_connection.cc:2140
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3104
clock_type::time_point last_trace_
Definition: x_connection.h:753
const connector_type & connector() const
Definition: x_connection.h:758
void server_recv_expect_open_response_forward()
Definition: x_connection.cc:2515
void server_recv_switch_tls_response_passthrough_forward_ok()
Definition: x_connection.cc:1082
Destinations destinations_
Definition: x_connection.h:761
void client_cap_set()
client wants to set the capabilities.
Definition: x_connection.cc:1379
void server_recv_cursor_close_response_forward_last()
Definition: x_connection.cc:2976
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2249
void wait_client_close()
Definition: x_connection.cc:3151
void client_crud_find()
Definition: x_connection.cc:2003
net::impl::socket::native_handle_type get_client_fd() const override
Definition: x_connection.h:117
void server_init_tls()
Definition: x_connection.cc:1628
void server_recv_prepare_deallocate_response()
Definition: x_connection.cc:2331
void finish()
Definition: x_connection.cc:3115
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2128
void async_recv_server(Function next)
Definition: x_connection.cc:446
void server_recv_crud_create_view_response_forward_last()
Definition: x_connection.cc:2647
static std::shared_ptr< MysqlRoutingXConnection > create(Args &&... args)
Definition: x_connection.h:89
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2595
std::shared_ptr< MysqlRoutingXConnection > getptr()
Definition: x_connection.h:100
void server_recv_prepare_prepare_response_forward()
Definition: x_connection.cc:2312
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2398
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2660
void client_prepare_deallocate()
Definition: x_connection.cc:2326
RouteDestination * route_destination_
Definition: x_connection.h:760
connector_type & connector()
Definition: x_connection.h:757
void async_recv_client(Function next)
Definition: x_connection.cc:411
void server_recv_stmt_execute_response()
Definition: x_connection.cc:1939
void client_crud_delete()
Definition: x_connection.cc:2072
clock_type::time_point started_
Definition: x_connection.h:752
splices two connections together.
Definition: basic_protocol_splicer.h:481
TlsSwitchableConnection & client_conn()
Definition: basic_protocol_splicer.h:518
SslMode dest_ssl_mode() const
Definition: basic_protocol_splicer.h:534
SslMode source_ssl_mode() const
Definition: basic_protocol_splicer.h:530
TlsSwitchableConnection & server_conn()
Definition: basic_protocol_splicer.h:524
Definition: basic_protocol_splicer.h:290
Manage destinations for a Connection Routing.
Definition: destination.h:187
a Connection that can be switched to TLS.
Definition: basic_protocol_splicer.h:305
std::string endpoint() const
Definition: basic_protocol_splicer.h:424
net::impl::socket::native_handle_type native_handle() const
Definition: basic_protocol_splicer.h:405
ProtocolStateBase * protocol()
Definition: basic_protocol_splicer.h:446
Definition: x_connection.h:37
std::optional< uint8_t > msg_type_
Definition: x_connection.h:54
std::optional< FrameInfo > & current_frame()
Definition: x_connection.h:44
Mysqlx::Connection::Capabilities * caps()
Definition: x_connection.h:47
std::optional< FrameInfo > current_frame_
Definition: x_connection.h:53
void caps(std::unique_ptr< Mysqlx::Connection::Capabilities > caps)
Definition: x_connection.h:48
std::unique_ptr< Mysqlx::Connection::Capabilities > caps_
Definition: x_connection.h:56
std::optional< uint8_t > & current_msg_type()
Definition: x_connection.h:45
Definition: buffer.h:134
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.
int native_handle_type
Definition: socket_constants.h:50
Definition: varlen_sort.h:174
SslMode
Definition: ssl_mode.h:28
Capabilities.
Definition: mysqlx_connection.proto:52
Severity
Definition: mysqlx.proto:278
@ ERROR
Definition: mysqlx.proto:279
Definition: x_connection.h:39
size_t frame_size_
size of the whole frame
Definition: x_connection.h:40
size_t forwarded_frame_size_
size of the forwarded part of frame
Definition: x_connection.h:41