MySQL 8.4.2
Source Code Documentation
x_connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, 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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTING_X_CONNECTION_INCLUDED
27#define ROUTING_X_CONNECTION_INCLUDED
28
29#include <memory>
30#include <optional>
31#include <string>
32
33#include <mysqlx.pb.h>
34#include <mysqlx_connection.pb.h>
35
37#include "connection.h" // MySQLRoutingConnectionBase
38
40 public:
41 struct FrameInfo {
42 size_t frame_size_; //!< size of the whole frame
43 size_t forwarded_frame_size_; //!< size of the forwarded part of frame
44 };
45
46 std::optional<FrameInfo> &current_frame() { return current_frame_; }
47 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
48
50 void caps(std::unique_ptr<Mysqlx::Connection::Capabilities> caps) {
51 caps_ = std::move(caps);
52 }
53
54 private:
55 std::optional<FrameInfo> current_frame_{};
56 std::optional<uint8_t> msg_type_{};
57
58 std::unique_ptr<Mysqlx::Connection::Capabilities> caps_;
59};
60
63 public std::enable_shared_from_this<MysqlRoutingXConnection> {
64 private:
67
68 // constructor
69 //
70 // use ::create() instead.
72 MySQLRoutingContext &context, RouteDestination *route_destination,
73 std::unique_ptr<ConnectionBase> client_connection,
74 std::unique_ptr<RoutingConnectionBase> client_routing_connection,
75 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
76 : MySQLRoutingConnectionBase{context, std::move(remove_callback)},
77 route_destination_{route_destination},
78 destinations_{route_destination_->destinations()},
79 connector_{client_connection->io_ctx(), route_destination,
81 client_conn_{std::move(client_connection),
82 std::move(client_routing_connection),
84 ClientSideConnection::protocol_state_type{}},
86 ServerSideConnection::protocol_state_type{}} {}
87
88 public:
90
91 // create a shared_ptr<ThisClass>
92 template <typename... Args>
93 [[nodiscard]] static std::shared_ptr<MysqlRoutingXConnection> create(
94 // clang-format off
95 Args &&... args) {
96 // clang-format on
97
98 // can't use make_unique<> here as the constructor is private.
99 return std::shared_ptr<MysqlRoutingXConnection>(
100 new MysqlRoutingXConnection(std::forward<Args>(args)...));
101 }
102
103 // get a shared-ptr that refers the same 'this'
104 std::shared_ptr<MysqlRoutingXConnection> getptr() {
105 return shared_from_this();
106 }
107
109 std::vector<uint8_t> &error_frame, uint16_t error_code,
110 const std::string &msg, const std::string &sql_state = "HY000",
112
114
116
118 return client_conn().native_handle();
119 }
120
121 std::string get_client_address() const override {
122 return client_conn().endpoint();
123 }
124
125 std::string get_server_address() const override {
126 return 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 client_conn().protocol();
729 }
731 return client_conn().protocol();
732 }
733
735 return server_conn().protocol();
736 }
738 return server_conn().protocol();
739 }
740
743
746
747 std::string get_destination_id() const override {
748 return connector().destination_id();
749 }
750
751 std::optional<net::ip::tcp::endpoint> destination_endpoint() const override {
752 return std::nullopt;
753 }
754
755 private:
757
758 using clock_type = std::chrono::steady_clock;
759
760 clock_type::time_point started_{clock_type::now()};
761 clock_type::time_point last_trace_{clock_type::now()};
762
764
766 const connector_type &connector() const { return connector_; }
767
771
774};
775
776#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
SSL aware socket buffers.
Definition: channel.h:65
void destination_id(std::string id)
Definition: connection.h:213
A forward iterable container of destinations.
Definition: destination.h:107
Definition: connection.h:44
std::chrono::system_clock clock_type
Definition: connection.h:105
MySQLRoutingContext & context()
Definition: connection.h:53
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:59
Definition: x_connection.h:63
void client_cap_get()
client wants to get the capabilities of the server.
Definition: x_connection.cc:858
void recv_client_failed(std::error_code ec)
Definition: x_connection.cc:353
const ServerSideConnection & server_conn() const
Definition: x_connection.h:745
void server_recv_switch_tls_response()
Definition: x_connection.cc:918
void async_send_server(Function next)
Definition: x_connection.cc:426
void server_recv_prepare_deallocate_response_forward()
Definition: x_connection.cc:2389
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2594
void server_recv_crud_delete_response()
Definition: x_connection.cc:2088
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:71
void client_crud_modify_view()
Definition: x_connection.cc:2666
void client_cursor_open()
Definition: x_connection.cc:2795
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1325
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:2002
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2461
void client_cursor_fetch()
Definition: x_connection.cc:2862
void client_stmt_execute()
Definition: x_connection.cc:1945
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2134
void server_recv_cap_get_response()
Definition: x_connection.cc:1281
stdx::expected< ForwardResult, std::error_code > forward_frame_from_server_to_client()
Definition: x_connection.cc:709
void client_prepare_execute()
Definition: x_connection.cc:2404
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1845
ServerSideConnection & server_conn()
Definition: x_connection.h:744
void server_recv_switch_tls_response_passthrough_forward_last()
Definition: x_connection.cc:1077
ClientSideConnection client_conn_
Definition: x_connection.h:772
void server_recv_crud_modify_view_response_forward()
Definition: x_connection.cc:2717
void async_run()
Definition: x_connection.cc:313
void server_recv_cursor_close_response()
Definition: x_connection.cc:2936
void server_tls_shutdown()
Definition: x_connection.cc:3169
void client_sess_auth_start()
Definition: x_connection.cc:1852
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:2731
void connect()
Definition: x_connection.cc:742
void server_recv_crud_find_response()
Definition: x_connection.cc:2019
void client_crud_create_view()
Definition: x_connection.cc:2601
void server_recv_cap_get_response_forward_last()
Definition: x_connection.cc:1330
ServerSideConnection server_conn_
Definition: x_connection.h:773
void server_recv_expect_close_response_forward()
Definition: x_connection.cc:2589
void call_next_function(Function next)
Definition: x_connection.h:289
stdx::expected< void, std::error_code > forward_tls(Channel &src_channel, Channel &dst_channel)
Definition: x_connection.cc:1090
int active_work_
Definition: x_connection.h:756
void client_tls_shutdown()
Definition: x_connection.cc:3176
SslMode source_ssl_mode() const
Definition: x_connection.h:113
SslMode dest_ssl_mode() const
Definition: x_connection.h:115
connector_type connector_
Definition: x_connection.h:770
void client_crud_update()
Definition: x_connection.cc:2209
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:2007
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3045
void forward_tls_server_to_client()
Definition: x_connection.cc:1159
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2788
void client_recv_auth_continue()
Definition: x_connection.cc:1933
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:288
Function
Definition: x_connection.h:131
void server_recv_expect_close_response()
Definition: x_connection.cc:2543
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2396
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2467
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3109
void tls_connect_init()
Definition: x_connection.cc:1194
bool greeting_from_router_
Definition: x_connection.h:763
void client_session_reset()
Definition: x_connection.cc:3058
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3051
void server_recv_prepare_prepare_response_forward_last()
Definition: x_connection.cc:2329
void client_expect_close()
Definition: x_connection.cc:2538
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1840
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2855
void forward_client_to_server(Function this_func, Function next_func)
Definition: x_connection.cc:687
void disconnect() override
Definition: x_connection.cc:247
void server_recv_cursor_fetch_response()
Definition: x_connection.cc:2867
void server_socket_failed(std::error_code ec)
Definition: x_connection.cc:361
const ClientSideConnection & client_conn() const
Definition: x_connection.h:742
ServerSideConnection::protocol_state_type & server_protocol()
Definition: x_connection.h:734
std::string get_destination_id() const override
Definition: x_connection.h:747
void server_recv_session_close_response()
Definition: x_connection.cc:2999
void async_send_client(Function next)
Definition: x_connection.cc:392
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2850
void async_send_server_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1353
void send_server_failed(std::error_code ec)
Definition: x_connection.cc:329
void server_recv_server_greeting_from_server()
Definition: x_connection.cc:3122
void send_client_failed(std::error_code ec)
Definition: x_connection.cc:345
void async_send_client_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1335
void client_con_close()
Definition: x_connection.cc:303
void server_recv_crud_insert_response_forward_last()
Definition: x_connection.cc:2202
ClientSideConnection & client_conn()
Definition: x_connection.h:741
void server_recv_switch_tls_response_passthrough_forward()
Definition: x_connection.cc:1070
void server_recv_cursor_close_response_forward()
Definition: x_connection.cc:2982
void forward_tls_client_to_server()
Definition: x_connection.cc:1144
void client_session_close()
Definition: x_connection.cc:2994
ForwardResult
Definition: x_connection.h:572
void client_cursor_close()
Definition: x_connection.cc:2931
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2924
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:376
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2277
void server_recv_auth_response_forward()
Definition: x_connection.cc:1923
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:1928
void tls_accept_finalize()
Definition: x_connection.cc:1619
void server_recv_session_reset_response()
Definition: x_connection.cc:3063
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2076
void server_send_switch_to_tls()
Definition: x_connection.cc:1696
void client_send_server_greeting_from_router()
Definition: x_connection.cc:460
void client_recv_cmd()
Definition: x_connection.cc:464
void forward_tls_init()
Definition: x_connection.cc:1174
void client_expect_open()
Definition: x_connection.cc:2475
void server_recv_check_caps_response()
Definition: x_connection.cc:1724
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2652
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2736
void server_send_check_caps()
Definition: x_connection.cc:1711
void forward_server_to_client(Function this_func, Function next_func)
Definition: x_connection.cc:721
void recv_server_failed(std::error_code ec)
Definition: x_connection.cc:337
void server_recv_crud_insert_response_forward()
Definition: x_connection.cc:2197
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2071
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2531
void server_recv_auth_response()
Definition: x_connection.cc:1874
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:2480
void done()
Definition: x_connection.cc:3167
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1223
void client_crud_insert()
Definition: x_connection.cc:2146
void server_recv_cursor_open_response()
Definition: x_connection.cc:2800
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2782
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2919
void server_recv_cap_set_response()
Definition: x_connection.cc:1794
void server_recv_crud_update_response()
Definition: x_connection.cc:2214
const ServerSideConnection::protocol_state_type & server_protocol() const
Definition: x_connection.h:737
void tls_accept()
accept a TLS handshake.
Definition: x_connection.cc:1569
void server_recv_crud_update_response_forward_last()
Definition: x_connection.cc:2265
void tls_accept_init()
Definition: x_connection.cc:1549
void server_recv_switch_tls_response_passthrough()
Definition: x_connection.cc:1020
void server_recv_crud_modify_view_response_forward_last()
Definition: x_connection.cc:2723
void client_prepare_prepare()
Definition: x_connection.cc:2272
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1938
std::optional< net::ip::tcp::endpoint > destination_endpoint() const override
Definition: x_connection.h:751
void server_recv_crud_insert_response()
Definition: x_connection.cc:2151
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3115
ClientSideConnection::protocol_state_type & client_protocol()
Definition: x_connection.h:727
clock_type::time_point last_trace_
Definition: x_connection.h:761
const connector_type & connector() const
Definition: x_connection.h:766
void server_recv_expect_open_response_forward()
Definition: x_connection.cc:2526
void server_recv_switch_tls_response_passthrough_forward_ok()
Definition: x_connection.cc:1084
Destinations destinations_
Definition: x_connection.h:769
void client_cap_set()
client wants to set the capabilities.
Definition: x_connection.cc:1376
void server_recv_cursor_close_response_forward_last()
Definition: x_connection.cc:2987
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2260
void wait_client_close()
Definition: x_connection.cc:3162
void client_crud_find()
Definition: x_connection.cc:2014
net::impl::socket::native_handle_type get_client_fd() const override
Definition: x_connection.h:117
const ClientSideConnection::protocol_state_type & client_protocol() const
Definition: x_connection.h:730
void server_init_tls()
Definition: x_connection.cc:1641
void server_recv_prepare_deallocate_response()
Definition: x_connection.cc:2342
void finish()
Definition: x_connection.cc:3126
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2139
void async_recv_server(Function next)
Definition: x_connection.cc:447
void server_recv_crud_create_view_response_forward_last()
Definition: x_connection.cc:2658
static std::shared_ptr< MysqlRoutingXConnection > create(Args &&... args)
Definition: x_connection.h:93
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2606
std::shared_ptr< MysqlRoutingXConnection > getptr()
Definition: x_connection.h:104
void server_recv_prepare_prepare_response_forward()
Definition: x_connection.cc:2323
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2409
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2671
void client_prepare_deallocate()
Definition: x_connection.cc:2337
RouteDestination * route_destination_
Definition: x_connection.h:768
connector_type & connector()
Definition: x_connection.h:765
void async_recv_client(Function next)
Definition: x_connection.cc:413
void server_recv_stmt_execute_response()
Definition: x_connection.cc:1950
void client_crud_delete()
Definition: x_connection.cc:2083
clock_type::time_point started_
Definition: x_connection.h:760
Manage destinations for a Connection Routing.
Definition: destination.h:188
typename base_class_::protocol_state_type protocol_state_type
Definition: basic_protocol_splicer.h:91
std::string endpoint() const
Definition: connection_base.h:386
protocol_state_type & protocol()
Definition: connection_base.h:398
net::impl::socket::native_handle_type native_handle() const
Definition: connection_base.h:367
SslMode ssl_mode() const
Definition: connection_base.h:363
Definition: x_connection.h:39
std::optional< uint8_t > msg_type_
Definition: x_connection.h:56
std::optional< FrameInfo > & current_frame()
Definition: x_connection.h:46
Mysqlx::Connection::Capabilities * caps()
Definition: x_connection.h:49
std::optional< FrameInfo > current_frame_
Definition: x_connection.h:55
void caps(std::unique_ptr< Mysqlx::Connection::Capabilities > caps)
Definition: x_connection.h:50
std::unique_ptr< Mysqlx::Connection::Capabilities > caps_
Definition: x_connection.h:58
std::optional< uint8_t > & current_msg_type()
Definition: x_connection.h:47
Definition: buffer.h:135
int native_handle_type
Definition: socket_constants.h:51
Definition: gcs_xcom_synode.h:64
SslMode
Definition: ssl_mode.h:29
Capabilities.
Definition: mysqlx_connection.proto:53
Severity
Definition: mysqlx.proto:279
@ ERROR
Definition: mysqlx.proto:280
Definition: x_connection.h:41
size_t frame_size_
size of the whole frame
Definition: x_connection.h:42
size_t forwarded_frame_size_
size of the forwarded part of frame
Definition: x_connection.h:43