MySQL 8.4.4
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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{}} {
88 }
89
90 public:
92
93 // create a shared_ptr<ThisClass>
94 template <typename... Args>
95 [[nodiscard]] static std::shared_ptr<MysqlRoutingXConnection> create(
96 Args &&...args) {
97 // can't use make_unique<> here as the constructor is private.
98 return std::shared_ptr<MysqlRoutingXConnection>(
99 new MysqlRoutingXConnection(std::forward<Args>(args)...));
100 }
101
102 // get a shared-ptr that refers the same 'this'
103 std::shared_ptr<MysqlRoutingXConnection> getptr() {
104 return shared_from_this();
105 }
106
108 std::vector<uint8_t> &error_frame, uint16_t error_code,
109 const std::string &msg, const std::string &sql_state = "HY000",
111
113
115
117 return client_conn().native_handle();
118 }
119
120 void disconnect() override;
121
122 enum class Function {
124
125 // tls-accept
129
132
135
139
142
143 // cap-get
152
153 // cap-set
158
159 // sess-auth
166
167 // stmt-exec
172
173 // crud::find
178
179 // crud::delete
184
185 // crud::insert
190
191 // crud::update
196
197 // prepare::prepare
202
203 // prepare::deallocate
208
209 // prepare::execute
214
215 // expect::open
220
221 // expect::close
226
227 // crud::create_view
232
233 // crud::modify_view
238
239 // crud::drop_view
244
245 // cursor::open
250
251 // cursor::fetch
256
257 // cursor::close
262
263 // session::close
268
269 // session::reset
274
275 kConnect,
277 kFinish,
278 };
279
281 switch (next) {
283 return client_recv_cmd();
284
286 return tls_accept_init();
288 return tls_accept();
290 return tls_accept_finalize();
291
293 return server_init_tls();
296
298 return tls_connect_init();
300 return tls_connect();
301
303 return server_send_check_caps();
306
308 return client_cap_get();
315
317 return client_cap_set();
332
334 return forward_tls_init();
339
341 return client_sess_auth_start();
352
354 return client_stmt_execute();
361
363 return client_crud_find();
370
372 return client_crud_delete();
379
381 return client_crud_insert();
388
390 return client_crud_update();
397
399 return client_prepare_prepare();
406
415
417 return client_prepare_execute();
424
426 return client_expect_open();
433
435 return client_expect_close();
442
451
460
462 return client_crud_drop_view();
469
471 return client_cursor_open();
478
480 return client_cursor_fetch();
487
489 return client_cursor_close();
496
498 return client_session_close();
505
507 return client_session_reset();
514
516 return connect();
518 return wait_client_close();
520 return finish();
521 }
522 }
523
524 void send_server_failed(std::error_code ec);
525
526 void recv_server_failed(std::error_code ec);
527
528 void send_client_failed(std::error_code ec);
529
530 void recv_client_failed(std::error_code ec);
531
532 void client_socket_failed(std::error_code ec);
533
534 void server_socket_failed(std::error_code ec);
535
536 void async_send_client(Function next);
537
538 void async_recv_client(Function next);
539
540 void async_send_server(Function next);
541
542 void async_recv_server(Function next);
543
544 void async_run();
545
546 void wait_client_close();
547 void finish();
548
549 void server_tls_shutdown();
550 void client_tls_shutdown();
551
552 void done();
553
555 Channel &dst_channel);
556
558
560
561 void forward_tls_init();
562
563 enum class ForwardResult {
568 kFinished,
569 };
570
573
576
577 void forward_client_to_server(Function this_func, Function next_func);
578
579 void forward_server_to_client(Function this_func, Function next_func);
580
583
586 void connect();
587
591
592 void tls_accept_init();
593 void tls_accept();
594 void tls_accept_finalize();
595
596 void server_init_tls();
598 void tls_connect_init();
599 void tls_connect();
600
601 void client_con_close();
602
603 void client_recv_cmd();
604
605 void client_cap_get();
609
610 void client_cap_set();
618
620
627
628 void client_stmt_execute();
632
633 void client_crud_find();
637
638 void client_crud_delete();
642
643 void client_crud_insert();
647
648 void client_crud_update();
652
657
662
667
668 void client_expect_open();
672
673 void client_expect_close();
677
682
687
692
693 void client_cursor_open();
697
698 void client_cursor_fetch();
702
703 void client_cursor_close();
707
712
717
719 return client_conn().protocol();
720 }
722 return client_conn().protocol();
723 }
724
726 return server_conn().protocol();
727 }
729 return server_conn().protocol();
730 }
731
734
737
738 std::string get_destination_id() const override {
739 return connector().destination_id();
740 }
741
742 std::optional<net::ip::tcp::endpoint> destination_endpoint() const override {
743 return std::nullopt;
744 }
745
746 private:
748
749 using clock_type = std::chrono::steady_clock;
750
751 clock_type::time_point started_{clock_type::now()};
752 clock_type::time_point last_trace_{clock_type::now()};
753
755
757 const connector_type &connector() const { return connector_; }
758
762
765};
766
767#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:242
A forward iterable container of destinations.
Definition: destination.h:107
Definition: connection.h:44
std::chrono::system_clock clock_type
Definition: connection.h:117
MySQLRoutingContext & context()
Definition: connection.h:53
void client_address(const std::string &dest)
Definition: connection.h:105
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:860
void recv_client_failed(std::error_code ec)
Definition: x_connection.cc:353
const ServerSideConnection & server_conn() const
Definition: x_connection.h:736
void server_recv_switch_tls_response()
Definition: x_connection.cc:920
void async_send_server(Function next)
Definition: x_connection.cc:426
void server_recv_prepare_deallocate_response_forward()
Definition: x_connection.cc:2391
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2596
void server_recv_crud_delete_response()
Definition: x_connection.cc:2090
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:2668
void client_cursor_open()
Definition: x_connection.cc:2797
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1327
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:2004
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2463
void client_cursor_fetch()
Definition: x_connection.cc:2864
void client_stmt_execute()
Definition: x_connection.cc:1947
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2136
void server_recv_cap_get_response()
Definition: x_connection.cc:1283
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:2406
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1847
ServerSideConnection & server_conn()
Definition: x_connection.h:735
void server_recv_switch_tls_response_passthrough_forward_last()
Definition: x_connection.cc:1079
ClientSideConnection client_conn_
Definition: x_connection.h:763
void server_recv_crud_modify_view_response_forward()
Definition: x_connection.cc:2719
void async_run()
Definition: x_connection.cc:313
void server_recv_cursor_close_response()
Definition: x_connection.cc:2938
void server_tls_shutdown()
Definition: x_connection.cc:3171
void client_sess_auth_start()
Definition: x_connection.cc:1854
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:2733
void connect()
Definition: x_connection.cc:742
void server_recv_crud_find_response()
Definition: x_connection.cc:2021
void client_crud_create_view()
Definition: x_connection.cc:2603
void server_recv_cap_get_response_forward_last()
Definition: x_connection.cc:1332
ServerSideConnection server_conn_
Definition: x_connection.h:764
static std::shared_ptr< MysqlRoutingXConnection > create(Args &&...args)
Definition: x_connection.h:95
void server_recv_expect_close_response_forward()
Definition: x_connection.cc:2591
void call_next_function(Function next)
Definition: x_connection.h:280
stdx::expected< void, std::error_code > forward_tls(Channel &src_channel, Channel &dst_channel)
Definition: x_connection.cc:1092
int active_work_
Definition: x_connection.h:747
void client_tls_shutdown()
Definition: x_connection.cc:3178
SslMode source_ssl_mode() const
Definition: x_connection.h:112
SslMode dest_ssl_mode() const
Definition: x_connection.h:114
connector_type connector_
Definition: x_connection.h:761
void client_crud_update()
Definition: x_connection.cc:2211
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:2009
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3047
void forward_tls_server_to_client()
Definition: x_connection.cc:1161
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2790
void client_recv_auth_continue()
Definition: x_connection.cc:1935
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:122
void server_recv_expect_close_response()
Definition: x_connection.cc:2545
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2398
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2469
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3111
void tls_connect_init()
Definition: x_connection.cc:1196
bool greeting_from_router_
Definition: x_connection.h:754
void client_session_reset()
Definition: x_connection.cc:3060
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3053
void server_recv_prepare_prepare_response_forward_last()
Definition: x_connection.cc:2331
void client_expect_close()
Definition: x_connection.cc:2540
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1842
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2857
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:2869
void server_socket_failed(std::error_code ec)
Definition: x_connection.cc:361
const ClientSideConnection & client_conn() const
Definition: x_connection.h:733
ServerSideConnection::protocol_state_type & server_protocol()
Definition: x_connection.h:725
std::string get_destination_id() const override
Definition: x_connection.h:738
void server_recv_session_close_response()
Definition: x_connection.cc:3001
void async_send_client(Function next)
Definition: x_connection.cc:392
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2852
void async_send_server_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1355
void send_server_failed(std::error_code ec)
Definition: x_connection.cc:329
void server_recv_server_greeting_from_server()
Definition: x_connection.cc:3124
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:1337
void client_con_close()
Definition: x_connection.cc:303
void server_recv_crud_insert_response_forward_last()
Definition: x_connection.cc:2204
ClientSideConnection & client_conn()
Definition: x_connection.h:732
void server_recv_switch_tls_response_passthrough_forward()
Definition: x_connection.cc:1072
void server_recv_cursor_close_response_forward()
Definition: x_connection.cc:2984
void forward_tls_client_to_server()
Definition: x_connection.cc:1146
void client_session_close()
Definition: x_connection.cc:2996
ForwardResult
Definition: x_connection.h:563
void client_cursor_close()
Definition: x_connection.cc:2933
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2926
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:376
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2279
void server_recv_auth_response_forward()
Definition: x_connection.cc:1925
void server_recv_auth_response_continue()
Definition: x_connection.cc:1930
void tls_accept_finalize()
Definition: x_connection.cc:1621
void server_recv_session_reset_response()
Definition: x_connection.cc:3065
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2078
void server_send_switch_to_tls()
Definition: x_connection.cc:1698
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:1176
void client_expect_open()
Definition: x_connection.cc:2477
void server_recv_check_caps_response()
Definition: x_connection.cc:1726
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2654
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2738
void server_send_check_caps()
Definition: x_connection.cc:1713
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:2199
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2073
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2533
void server_recv_auth_response()
Definition: x_connection.cc:1876
void server_recv_expect_open_response()
Definition: x_connection.cc:2482
void done()
Definition: x_connection.cc:3169
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1225
void client_crud_insert()
Definition: x_connection.cc:2148
void server_recv_cursor_open_response()
Definition: x_connection.cc:2802
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2784
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2921
void server_recv_cap_set_response()
Definition: x_connection.cc:1796
void server_recv_crud_update_response()
Definition: x_connection.cc:2216
const ServerSideConnection::protocol_state_type & server_protocol() const
Definition: x_connection.h:728
void tls_accept()
accept a TLS handshake.
Definition: x_connection.cc:1571
void server_recv_crud_update_response_forward_last()
Definition: x_connection.cc:2267
void tls_accept_init()
Definition: x_connection.cc:1551
void server_recv_switch_tls_response_passthrough()
Definition: x_connection.cc:1022
void server_recv_crud_modify_view_response_forward_last()
Definition: x_connection.cc:2725
void client_prepare_prepare()
Definition: x_connection.cc:2274
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1940
std::optional< net::ip::tcp::endpoint > destination_endpoint() const override
Definition: x_connection.h:742
void server_recv_crud_insert_response()
Definition: x_connection.cc:2153
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3117
ClientSideConnection::protocol_state_type & client_protocol()
Definition: x_connection.h:718
clock_type::time_point last_trace_
Definition: x_connection.h:752
const connector_type & connector() const
Definition: x_connection.h:757
void server_recv_expect_open_response_forward()
Definition: x_connection.cc:2528
void server_recv_switch_tls_response_passthrough_forward_ok()
Definition: x_connection.cc:1086
Destinations destinations_
Definition: x_connection.h:760
void client_cap_set()
client wants to set the capabilities.
Definition: x_connection.cc:1378
void server_recv_cursor_close_response_forward_last()
Definition: x_connection.cc:2989
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2262
void wait_client_close()
Definition: x_connection.cc:3164
void client_crud_find()
Definition: x_connection.cc:2016
net::impl::socket::native_handle_type get_client_fd() const override
Definition: x_connection.h:116
const ClientSideConnection::protocol_state_type & client_protocol() const
Definition: x_connection.h:721
void server_init_tls()
Definition: x_connection.cc:1643
void server_recv_prepare_deallocate_response()
Definition: x_connection.cc:2344
void finish()
Definition: x_connection.cc:3128
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2141
void async_recv_server(Function next)
Definition: x_connection.cc:447
void server_recv_crud_create_view_response_forward_last()
Definition: x_connection.cc:2660
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2608
std::shared_ptr< MysqlRoutingXConnection > getptr()
Definition: x_connection.h:103
void server_recv_prepare_prepare_response_forward()
Definition: x_connection.cc:2325
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2411
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2673
void client_prepare_deallocate()
Definition: x_connection.cc:2339
RouteDestination * route_destination_
Definition: x_connection.h:759
connector_type & connector()
Definition: x_connection.h:756
void async_recv_client(Function next)
Definition: x_connection.cc:413
void server_recv_stmt_execute_response()
Definition: x_connection.cc:1952
void client_crud_delete()
Definition: x_connection.cc:2085
clock_type::time_point started_
Definition: x_connection.h:751
Manage destinations for a Connection Routing.
Definition: destination.h:189
typename base_class_::protocol_state_type protocol_state_type
Definition: basic_protocol_splicer.h:91
protocol_state_type & protocol()
Definition: connection_base.h:398
std::unique_ptr< ConnectionBase > & connection()
Definition: connection_base.h:404
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