MySQL 8.0.40
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
36#include "connection.h" // MySQLRoutingConnectionBase
37
39 public:
40 struct FrameInfo {
41 size_t frame_size_; //!< size of the whole frame
42 size_t forwarded_frame_size_; //!< size of the forwarded part of frame
43 };
44
45 std::optional<FrameInfo> &current_frame() { return current_frame_; }
46 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
47
49 void caps(std::unique_ptr<Mysqlx::Connection::Capabilities> caps) {
50 caps_ = std::move(caps);
51 }
52
53 private:
54 std::optional<FrameInfo> current_frame_{};
55 std::optional<uint8_t> msg_type_{};
56
57 std::unique_ptr<Mysqlx::Connection::Capabilities> caps_;
58};
59
62 public std::enable_shared_from_this<MysqlRoutingXConnection> {
63 private:
64 // constructor
65 //
66 // use ::create() instead.
68 MySQLRoutingContext &context, RouteDestination *route_destination,
69 std::unique_ptr<ConnectionBase> client_connection,
70 std::unique_ptr<RoutingConnectionBase> client_routing_connection,
71 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
72 : MySQLRoutingConnectionBase{context, std::move(remove_callback)},
73 route_destination_{route_destination},
74 destinations_{route_destination_->destinations()},
75 connector_{client_connection->io_ctx(), route_destination,
78 TlsSwitchableConnection{std::move(client_connection),
79 std::move(client_routing_connection),
84
85 public:
87
88 // create a shared_ptr<ThisClass>
89 template <typename... Args>
90 [[nodiscard]] static std::shared_ptr<MysqlRoutingXConnection> create(
91 // clang-format off
92 Args &&... args) {
93 // clang-format on
94
95 // can't use make_unique<> here as the constructor is private.
96 return std::shared_ptr<MysqlRoutingXConnection>(
97 new MysqlRoutingXConnection(std::forward<Args>(args)...));
98 }
99
100 // get a shared-ptr that refers the same 'this'
101 std::shared_ptr<MysqlRoutingXConnection> getptr() {
102 return shared_from_this();
103 }
104
106 std::vector<uint8_t> &error_frame, uint16_t error_code,
107 const std::string &msg, const std::string &sql_state = "HY000",
109
111 return this->socket_splicer()->source_ssl_mode();
112 }
113
115 return this->socket_splicer()->dest_ssl_mode();
116 }
117
118 std::string get_client_address() const override {
119 return socket_splicer()->client_conn().endpoint();
120 }
121
122 std::string get_server_address() const override {
123 return socket_splicer()->server_conn().endpoint();
124 }
125
126 void disconnect() override;
127
128 enum class Function {
130
131 // tls-accept
135
138
141
145
148
149 // cap-get
158
159 // cap-set
164
165 // sess-auth
172
173 // stmt-exec
178
179 // crud::find
184
185 // crud::delete
190
191 // crud::insert
196
197 // crud::update
202
203 // prepare::prepare
208
209 // prepare::deallocate
214
215 // prepare::execute
220
221 // expect::open
226
227 // expect::close
232
233 // crud::create_view
238
239 // crud::modify_view
244
245 // crud::drop_view
250
251 // cursor::open
256
257 // cursor::fetch
262
263 // cursor::close
268
269 // session::close
274
275 // session::reset
280
281 kConnect,
283 kFinish,
284 };
285
287 switch (next) {
289 return client_recv_cmd();
290
292 return tls_accept_init();
294 return tls_accept();
296 return tls_accept_finalize();
297
299 return server_init_tls();
302
304 return tls_connect_init();
306 return tls_connect();
307
309 return server_send_check_caps();
312
314 return client_cap_get();
321
323 return client_cap_set();
338
340 return forward_tls_init();
345
347 return client_sess_auth_start();
358
360 return client_stmt_execute();
367
369 return client_crud_find();
376
378 return client_crud_delete();
385
387 return client_crud_insert();
394
396 return client_crud_update();
403
405 return client_prepare_prepare();
412
421
423 return client_prepare_execute();
430
432 return client_expect_open();
439
441 return client_expect_close();
448
457
466
468 return client_crud_drop_view();
475
477 return client_cursor_open();
484
486 return client_cursor_fetch();
493
495 return client_cursor_close();
502
504 return client_session_close();
511
513 return client_session_reset();
520
522 return connect();
524 return wait_client_close();
526 return finish();
527 }
528 }
529
530 void send_server_failed(std::error_code ec);
531
532 void recv_server_failed(std::error_code ec);
533
534 void send_client_failed(std::error_code ec);
535
536 void recv_client_failed(std::error_code ec);
537
538 void client_socket_failed(std::error_code ec);
539
540 void server_socket_failed(std::error_code ec);
541
542 void async_send_client(Function next);
543
544 void async_recv_client(Function next);
545
546 void async_send_server(Function next);
547
548 void async_recv_server(Function next);
549
550 void async_run();
551
552 void wait_client_close();
553 void finish();
554
555 void server_tls_shutdown();
556 void client_tls_shutdown();
557
558 void done();
559
561 Channel *dst_channel);
562
564
566
567 void forward_tls_init();
568
569 enum class ForwardResult {
574 kFinished,
575 };
576
579
582
583 void forward_client_to_server(Function this_func, Function next_func);
584
585 void forward_server_to_client(Function this_func, Function next_func);
586
589
592 void connect();
593
597
598 void tls_accept_init();
599 void tls_accept();
600 void tls_accept_finalize();
601
602 void server_init_tls();
604 void tls_connect_init();
605 void tls_connect();
606
607 void client_con_close();
608
609 void client_recv_cmd();
610
611 void client_cap_get();
615
616 void client_cap_set();
624
626
633
634 void client_stmt_execute();
638
639 void client_crud_find();
643
644 void client_crud_delete();
648
649 void client_crud_insert();
653
654 void client_crud_update();
658
663
668
673
674 void client_expect_open();
678
679 void client_expect_close();
683
688
693
698
699 void client_cursor_open();
703
704 void client_cursor_fetch();
708
709 void client_cursor_close();
713
718
723
725 return dynamic_cast<XProtocolState *>(
727 }
728
730 return dynamic_cast<XProtocolState *>(
732 }
733
735 return socket_splicer_.get();
736 }
737
739
740 std::string get_destination_id() const override {
741 return connector().destination_id();
742 }
743
744 private:
746
747 using clock_type = std::chrono::steady_clock;
748
749 clock_type::time_point started_{clock_type::now()};
750 clock_type::time_point last_trace_{clock_type::now()};
751
753
755 const connector_type &connector() const { return connector_; }
756
760
761 std::unique_ptr<ProtocolSplicerBase> socket_splicer_;
762};
763
764#endif
SSL aware socket buffers.
Definition: channel.h:64
void destination_id(std::string id)
Definition: connection.h:182
A forward iterable container of destinations.
Definition: destination.h:97
Definition: connection.h:43
std::chrono::system_clock clock_type
Definition: connection.h:81
MySQLRoutingContext & context()
Definition: connection.h:52
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:59
Definition: x_connection.h:62
void client_cap_get()
client wants to get the capabilities of the server.
Definition: x_connection.cc:875
void recv_client_failed(std::error_code ec)
Definition: x_connection.cc:351
void server_recv_switch_tls_response()
Definition: x_connection.cc:935
void async_send_server(Function next)
Definition: x_connection.cc:443
void server_recv_prepare_deallocate_response_forward()
Definition: x_connection.cc:2386
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2591
void server_recv_crud_delete_response()
Definition: x_connection.cc:2085
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:67
void client_crud_modify_view()
Definition: x_connection.cc:2663
void client_cursor_open()
Definition: x_connection.cc:2792
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1336
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:1999
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2458
void client_cursor_fetch()
Definition: x_connection.cc:2859
void client_stmt_execute()
Definition: x_connection.cc:1942
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2131
void server_recv_cap_get_response()
Definition: x_connection.cc:1292
stdx::expected< ForwardResult, std::error_code > forward_frame_from_server_to_client()
Definition: x_connection.cc:726
void client_prepare_execute()
Definition: x_connection.cc:2401
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1841
void server_recv_switch_tls_response_passthrough_forward_last()
Definition: x_connection.cc:1094
void server_recv_crud_modify_view_response_forward()
Definition: x_connection.cc:2714
void async_run()
Definition: x_connection.cc:311
void server_recv_cursor_close_response()
Definition: x_connection.cc:2933
XProtocolState * client_protocol()
Definition: x_connection.h:724
void server_tls_shutdown()
Definition: x_connection.cc:3166
void client_sess_auth_start()
Definition: x_connection.cc:1848
stdx::expected< ForwardResult, std::error_code > forward_frame_from_client_to_server()
Definition: x_connection.cc:694
void client_crud_drop_view()
Definition: x_connection.cc:2728
void connect()
Definition: x_connection.cc:757
void server_recv_crud_find_response()
Definition: x_connection.cc:2016
void client_crud_create_view()
Definition: x_connection.cc:2598
void server_recv_cap_get_response_forward_last()
Definition: x_connection.cc:1341
void server_recv_expect_close_response_forward()
Definition: x_connection.cc:2586
void call_next_function(Function next)
Definition: x_connection.h:286
int active_work_
Definition: x_connection.h:745
void client_tls_shutdown()
Definition: x_connection.cc:3173
SslMode source_ssl_mode() const
Definition: x_connection.h:110
SslMode dest_ssl_mode() const
Definition: x_connection.h:114
connector_type connector_
Definition: x_connection.h:759
void client_crud_update()
Definition: x_connection.cc:2206
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:2004
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3042
void forward_tls_server_to_client()
Definition: x_connection.cc:1177
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2785
std::unique_ptr< ProtocolSplicerBase > socket_splicer_
Definition: x_connection.h:761
void client_recv_auth_continue()
Definition: x_connection.cc:1930
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:286
Function
Definition: x_connection.h:128
const ProtocolSplicerBase * socket_splicer() const
Definition: x_connection.h:734
void server_recv_expect_close_response()
Definition: x_connection.cc:2540
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2393
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2464
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3106
void tls_connect_init()
Definition: x_connection.cc:1214
bool greeting_from_router_
Definition: x_connection.h:752
void client_session_reset()
Definition: x_connection.cc:3055
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3048
void server_recv_prepare_prepare_response_forward_last()
Definition: x_connection.cc:2326
void client_expect_close()
Definition: x_connection.cc:2535
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1836
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2852
void forward_client_to_server(Function this_func, Function next_func)
Definition: x_connection.cc:704
void disconnect() override
Definition: x_connection.cc:245
void server_recv_cursor_fetch_response()
Definition: x_connection.cc:2864
void server_socket_failed(std::error_code ec)
Definition: x_connection.cc:359
std::string get_destination_id() const override
Definition: x_connection.h:740
void server_recv_session_close_response()
Definition: x_connection.cc:2996
void async_send_client(Function next)
Definition: x_connection.cc:408
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2847
void async_send_server_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1364
void send_server_failed(std::error_code ec)
Definition: x_connection.cc:327
void server_recv_server_greeting_from_server()
Definition: x_connection.cc:3119
void send_client_failed(std::error_code ec)
Definition: x_connection.cc:343
void async_send_client_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1346
void client_con_close()
Definition: x_connection.cc:301
void server_recv_crud_insert_response_forward_last()
Definition: x_connection.cc:2199
void server_recv_switch_tls_response_passthrough_forward()
Definition: x_connection.cc:1087
void server_recv_cursor_close_response_forward()
Definition: x_connection.cc:2979
void forward_tls_client_to_server()
Definition: x_connection.cc:1161
void client_session_close()
Definition: x_connection.cc:2991
ForwardResult
Definition: x_connection.h:569
void client_cursor_close()
Definition: x_connection.cc:2928
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2921
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:379
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2274
void server_recv_auth_response_forward()
Definition: x_connection.cc:1920
std::string get_client_address() const override
Returns address of client which connected to router.
Definition: x_connection.h:118
void server_recv_auth_response_continue()
Definition: x_connection.cc:1925
ProtocolSplicerBase * socket_splicer()
Definition: x_connection.h:738
void tls_accept_finalize()
Definition: x_connection.cc:1613
void server_recv_session_reset_response()
Definition: x_connection.cc:3060
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2073
void server_send_switch_to_tls()
Definition: x_connection.cc:1692
void client_send_server_greeting_from_router()
Definition: x_connection.cc:478
void client_recv_cmd()
Definition: x_connection.cc:482
void forward_tls_init()
Definition: x_connection.cc:1193
void client_expect_open()
Definition: x_connection.cc:2472
void server_recv_check_caps_response()
Definition: x_connection.cc:1720
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2649
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2733
stdx::expected< void, std::error_code > forward_tls(Channel *src_channel, Channel *dst_channel)
Definition: x_connection.cc:1107
void server_send_check_caps()
Definition: x_connection.cc:1707
XProtocolState * server_protocol()
Definition: x_connection.h:729
void forward_server_to_client(Function this_func, Function next_func)
Definition: x_connection.cc:736
void recv_server_failed(std::error_code ec)
Definition: x_connection.cc:335
void server_recv_crud_insert_response_forward()
Definition: x_connection.cc:2194
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2068
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2528
void server_recv_auth_response()
Definition: x_connection.cc:1871
std::string get_server_address() const override
Returns address of server to which connection is established.
Definition: x_connection.h:122
void server_recv_expect_open_response()
Definition: x_connection.cc:2477
void done()
Definition: x_connection.cc:3164
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1233
void client_crud_insert()
Definition: x_connection.cc:2143
void server_recv_cursor_open_response()
Definition: x_connection.cc:2797
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2779
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2916
void server_recv_cap_set_response()
Definition: x_connection.cc:1790
void server_recv_crud_update_response()
Definition: x_connection.cc:2211
void tls_accept()
accept a TLS handshake.
Definition: x_connection.cc:1579
void server_recv_crud_update_response_forward_last()
Definition: x_connection.cc:2262
void tls_accept_init()
Definition: x_connection.cc:1558
void server_recv_switch_tls_response_passthrough()
Definition: x_connection.cc:1037
void server_recv_crud_modify_view_response_forward_last()
Definition: x_connection.cc:2720
void client_prepare_prepare()
Definition: x_connection.cc:2269
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1935
void server_recv_crud_insert_response()
Definition: x_connection.cc:2148
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3112
clock_type::time_point last_trace_
Definition: x_connection.h:750
const connector_type & connector() const
Definition: x_connection.h:755
void server_recv_expect_open_response_forward()
Definition: x_connection.cc:2523
void server_recv_switch_tls_response_passthrough_forward_ok()
Definition: x_connection.cc:1101
Destinations destinations_
Definition: x_connection.h:758
void client_cap_set()
client wants to set the capabilities.
Definition: x_connection.cc:1387
void server_recv_cursor_close_response_forward_last()
Definition: x_connection.cc:2984
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2257
void wait_client_close()
Definition: x_connection.cc:3159
void client_crud_find()
Definition: x_connection.cc:2011
void server_init_tls()
Definition: x_connection.cc:1636
void server_recv_prepare_deallocate_response()
Definition: x_connection.cc:2339
void finish()
Definition: x_connection.cc:3123
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2136
void async_recv_server(Function next)
Definition: x_connection.cc:465
void server_recv_crud_create_view_response_forward_last()
Definition: x_connection.cc:2655
static std::shared_ptr< MysqlRoutingXConnection > create(Args &&... args)
Definition: x_connection.h:90
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2603
std::shared_ptr< MysqlRoutingXConnection > getptr()
Definition: x_connection.h:101
void server_recv_prepare_prepare_response_forward()
Definition: x_connection.cc:2320
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2406
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2668
void client_prepare_deallocate()
Definition: x_connection.cc:2334
RouteDestination * route_destination_
Definition: x_connection.h:757
connector_type & connector()
Definition: x_connection.h:754
void async_recv_client(Function next)
Definition: x_connection.cc:430
void server_recv_stmt_execute_response()
Definition: x_connection.cc:1947
void client_crud_delete()
Definition: x_connection.cc:2080
clock_type::time_point started_
Definition: x_connection.h:749
splices two connections together.
Definition: basic_protocol_splicer.h:484
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
TlsSwitchableConnection & server_conn()
Definition: basic_protocol_splicer.h:527
Definition: basic_protocol_splicer.h:291
Manage destinations for a Connection Routing.
Definition: destination.h:188
a Connection that can be switched to TLS.
Definition: basic_protocol_splicer.h:306
std::string endpoint() const
Definition: basic_protocol_splicer.h:427
ProtocolStateBase * protocol()
Definition: basic_protocol_splicer.h:449
Definition: x_connection.h:38
std::optional< uint8_t > msg_type_
Definition: x_connection.h:55
std::optional< FrameInfo > & current_frame()
Definition: x_connection.h:45
Mysqlx::Connection::Capabilities * caps()
Definition: x_connection.h:48
std::optional< FrameInfo > current_frame_
Definition: x_connection.h:54
void caps(std::unique_ptr< Mysqlx::Connection::Capabilities > caps)
Definition: x_connection.h:49
std::unique_ptr< Mysqlx::Connection::Capabilities > caps_
Definition: x_connection.h:57
std::optional< uint8_t > & current_msg_type()
Definition: x_connection.h:46
Definition: buffer.h:135
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
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.
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:40
size_t frame_size_
size of the whole frame
Definition: x_connection.h:41
size_t forwarded_frame_size_
size of the forwarded part of frame
Definition: x_connection.h:42