MySQL 9.1.0
Source Code Documentation
classic_greeting_forwarder.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2023, 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_CLASSIC_GREETING_FORWARDER_INCLUDED
27#define ROUTING_CLASSIC_GREETING_FORWARDER_INCLUDED
28
30
31#include "router_require.h"
32
33/**
34 * classic protocol handshake between client<->router and router<->server.
35 */
37 public:
38 /**
39 * construct a server::greeting processor.
40 *
41 * c->r : ...
42 * r->s: connect()
43 * r<-s: server::greeting
44 *
45 * a server greeting may be sent as part of the initial connection
46 * setup between client<->router<->server (in_handshake=true) or
47 * when router starts a connection on its own.
48 *
49 * If `in_handshake` is true, the ServerGreetor expects it can send:
50 *
51 * - server::AuthMethodSwitch and
52 * - server::Ok
53 *
54 * to the client connection.
55 *
56 * @param conn the connection the greeting will be transferred on.
57 * @param in_handshake true if the greeting is part of the initial
58 * handshake.
59 * @param on_error callback called on failure.
60 * @param parent_event parent span for the TraceEvents
61 */
63 MysqlRoutingClassicConnectionBase *conn, bool in_handshake,
64 std::function<void(const classic_protocol::message::server::Error &)>
65 on_error,
66 TraceEvent *parent_event)
67 : ForwardingProcessor(conn),
68 in_handshake_{in_handshake},
69 on_error_(std::move(on_error)),
70 parent_event_(parent_event) {}
71
72 /**
73 * stages of the handshake flow.
74 *
75 * - Client stages are on the client<->router side.
76 * - Server stages are on the router<->server side.
77 */
78 enum class Stage {
90 AuthOk,
92
94 Error,
95 Ok,
96 };
97
99
101 [[nodiscard]] Stage stage() const { return stage_; }
102
103 void failed(
104 const std::optional<classic_protocol::message::server::Error> &err) {
105 failed_ = err;
106 }
107
108 std::optional<classic_protocol::message::server::Error> failed() const {
109 return failed_;
110 }
111
112 private:
127
129 ClassicProtocolState &dst_protocol);
130
132
134
135 std::optional<classic_protocol::message::server::Error> failed_;
136
137 std::function<void(const classic_protocol::message::server::Error &err)>
139
145};
146
147/**
148 * classic protocol handshake between router<->server and client<->router.
149 *
150 * a server::greeting processor which fetches a server::greeting
151 * to send it to the client.
152 *
153 * c->r : accept()
154 * r->s: connect()
155 * r<-s: server::greeting
156 * c<-r : ...
157 *
158 */
160 public:
161 using ForwardingProcessor::ForwardingProcessor;
162
163 /**
164 * stages of the handshake flow.
165 */
166 enum class Stage {
167 Connect,
170
171 Error,
172 Ok,
173 };
174
176
178 [[nodiscard]] Stage stage() const { return stage_; }
179
180 private:
184
186
187 // start timepoint to calculate the connect-retry-timeout.
188 std::chrono::steady_clock::time_point started_{
189 std::chrono::steady_clock::now()};
190};
191
192/**
193 * authenticates a server connection.
194 *
195 * Assumes the server
196 *
197 * 1. sent a server::greeting already
198 * 2. expects to receive a client::greeting
199 */
201 public:
204 std::function<void(const classic_protocol::message::server::Error &)>
205 on_error)
206 : ForwardingProcessor(conn), on_error_(std::move(on_error)) {}
207
208 /**
209 * stages of the handshake flow.
210 */
211 enum class Stage {
222 AuthOk,
223 AuthError,
226
227 Error,
228 Ok,
229 };
230
232
234 [[nodiscard]] Stage stage() const { return stage_; }
235
236 void failed(
237 const std::optional<classic_protocol::message::server::Error> &err) {
238 failed_ = err;
239 }
240
241 std::optional<classic_protocol::message::server::Error> failed() const {
242 return failed_;
243 }
244
245 private:
260
262 ClassicProtocolState &st_protocol);
263
268
269 std::optional<classic_protocol::message::server::Error> failed_;
270
272
274
275 std::function<void(const classic_protocol::message::server::Error &err)>
277};
278
279#endif
protocol state of a classic protocol connection.
Definition: classic_protocol_state.h:40
a processor base class with helper functions.
Definition: forwarding_processor.h:38
Definition: classic_connection_base.h:56
authenticates a server connection.
Definition: classic_greeting_forwarder.h:200
void failed(const std::optional< classic_protocol::message::server::Error > &err)
Definition: classic_greeting_forwarder.h:236
stdx::expected< Result, std::error_code > tls_forward()
Definition: classic_greeting_forwarder.cc:1769
size_t server_last_recv_buf_size_
Definition: classic_greeting_forwarder.h:266
size_t server_last_send_buf_size_
Definition: classic_greeting_forwarder.h:267
RouterRequireFetcher::Result required_connection_attributes_fetcher_result_
Definition: classic_greeting_forwarder.h:271
stdx::expected< Result, std::error_code > auth_ok()
server-side: auth is ok.
Definition: classic_greeting_forwarder.cc:2134
stdx::expected< Result, std::error_code > client_greeting()
Definition: classic_greeting_forwarder.cc:1482
size_t client_last_send_buf_size_
Definition: classic_greeting_forwarder.h:265
stdx::expected< Result, std::error_code > fetch_user_attrs()
Definition: classic_greeting_forwarder.cc:2170
std::optional< classic_protocol::message::server::Error > failed() const
Definition: classic_greeting_forwarder.h:241
stdx::expected< Result, std::error_code > tls_connect_init()
Definition: classic_greeting_forwarder.cc:1836
stdx::expected< Result, std::error_code > fetch_user_attrs_done()
Definition: classic_greeting_forwarder.cc:2183
Stage stage_
Definition: classic_greeting_forwarder.h:273
void client_greeting_server_adjust_caps(ClassicProtocolState &rc_protocol, ClassicProtocolState &st_protocol)
Definition: classic_greeting_forwarder.cc:1432
stdx::expected< Result, std::error_code > client_greeting_full()
Definition: classic_greeting_forwarder.cc:1643
stdx::expected< Result, std::error_code > process() override
Definition: classic_greeting_forwarder.cc:1387
stdx::expected< Result, std::error_code > tls_forward_init()
Definition: classic_greeting_forwarder.cc:1815
std::optional< classic_protocol::message::server::Error > failed_
Definition: classic_greeting_forwarder.h:269
stdx::expected< Result, std::error_code > tls_connect()
Definition: classic_greeting_forwarder.cc:1880
Stage stage() const
Definition: classic_greeting_forwarder.h:234
ServerFirstAuthenticator(MysqlRoutingClassicConnectionBase *conn, std::function< void(const classic_protocol::message::server::Error &)> on_error)
Definition: classic_greeting_forwarder.h:202
stdx::expected< Result, std::error_code > client_greeting_start_tls()
Definition: classic_greeting_forwarder.cc:1565
Stage
stages of the handshake flow.
Definition: classic_greeting_forwarder.h:211
stdx::expected< Result, std::error_code > auth_error()
router<-server: auth error.
Definition: classic_greeting_forwarder.cc:2104
stdx::expected< Result, std::error_code > initial_response()
Definition: classic_greeting_forwarder.cc:2042
size_t client_last_recv_buf_size_
Definition: classic_greeting_forwarder.h:264
stdx::expected< Result, std::error_code > final_response()
Definition: classic_greeting_forwarder.cc:2058
stdx::expected< Result, std::error_code > client_greeting_after_tls()
Definition: classic_greeting_forwarder.cc:1972
void stage(Stage stage)
Definition: classic_greeting_forwarder.h:233
std::function< void(const classic_protocol::message::server::Error &err)> on_error_
Definition: classic_greeting_forwarder.h:276
classic protocol handshake between router<->server and client<->router.
Definition: classic_greeting_forwarder.h:159
stdx::expected< Result, std::error_code > server_greeted()
received an server::greeting or server::error from the server.
Definition: classic_greeting_forwarder.cc:1343
stdx::expected< Result, std::error_code > process() override
Definition: classic_greeting_forwarder.cc:1274
Stage stage() const
Definition: classic_greeting_forwarder.h:178
stdx::expected< Result, std::error_code > server_greeting()
Definition: classic_greeting_forwarder.cc:1302
Stage stage_
Definition: classic_greeting_forwarder.h:185
stdx::expected< Result, std::error_code > connect()
Definition: classic_greeting_forwarder.cc:1295
std::chrono::steady_clock::time_point started_
Definition: classic_greeting_forwarder.h:188
void stage(Stage stage)
Definition: classic_greeting_forwarder.h:177
Stage
stages of the handshake flow.
Definition: classic_greeting_forwarder.h:166
classic protocol handshake between client<->router and router<->server.
Definition: classic_greeting_forwarder.h:36
stdx::expected< Result, std::error_code > error()
Definition: classic_greeting_forwarder.cc:352
ServerGreetor(MysqlRoutingClassicConnectionBase *conn, bool in_handshake, std::function< void(const classic_protocol::message::server::Error &)> on_error, TraceEvent *parent_event)
construct a server::greeting processor.
Definition: classic_greeting_forwarder.h:62
void stage(Stage stage)
Definition: classic_greeting_forwarder.h:100
void failed(const std::optional< classic_protocol::message::server::Error > &err)
Definition: classic_greeting_forwarder.h:103
stdx::expected< Result, std::error_code > client_greeting_start_tls()
Definition: classic_greeting_forwarder.cc:732
stdx::expected< Result, std::error_code > client_greeting_after_tls()
a TLS client greeting.
Definition: classic_greeting_forwarder.cc:1046
bool in_handshake_
Definition: classic_greeting_forwarder.h:131
stdx::expected< Result, std::error_code > process() override
Definition: classic_greeting_forwarder.cc:308
stdx::expected< Result, std::error_code > tls_connect_init()
Definition: classic_greeting_forwarder.cc:889
TraceEvent * trace_event_server_greeting_
Definition: classic_greeting_forwarder.h:142
TraceEvent * trace_event_greeting_
Definition: classic_greeting_forwarder.h:141
stdx::expected< Result, std::error_code > client_greeting_full()
send a non-TLS client greeting to the server.
Definition: classic_greeting_forwarder.cc:795
std::optional< classic_protocol::message::server::Error > failed() const
Definition: classic_greeting_forwarder.h:108
stdx::expected< Result, std::error_code > server_greeting_error()
received an server::error from the server.
Definition: classic_greeting_forwarder.cc:411
std::function< void(const classic_protocol::message::server::Error &err)> on_error_
Definition: classic_greeting_forwarder.h:138
Stage stage() const
Definition: classic_greeting_forwarder.h:101
stdx::expected< Result, std::error_code > server_greeting_greeting()
received a server::greeting from the server.
Definition: classic_greeting_forwarder.cc:503
stdx::expected< Result, std::error_code > client_greeting()
Definition: classic_greeting_forwarder.cc:626
stdx::expected< Result, std::error_code > initial_response()
Definition: classic_greeting_forwarder.cc:1134
TraceEvent * parent_event_
Definition: classic_greeting_forwarder.h:140
TraceEvent * trace_event_client_greeting_
Definition: classic_greeting_forwarder.h:143
stdx::expected< Result, std::error_code > tls_connect()
Definition: classic_greeting_forwarder.cc:937
Stage
stages of the handshake flow.
Definition: classic_greeting_forwarder.h:78
Stage stage_
Definition: classic_greeting_forwarder.h:133
std::optional< classic_protocol::message::server::Error > failed_
Definition: classic_greeting_forwarder.h:135
TraceEvent * trace_event_tls_connect_
Definition: classic_greeting_forwarder.h:144
stdx::expected< Result, std::error_code > auth_ok()
server-side: auth is ok.
Definition: classic_greeting_forwarder.cc:1234
stdx::expected< Result, std::error_code > server_greeting()
Definition: classic_greeting_forwarder.cc:379
stdx::expected< Result, std::error_code > auth_error()
router<-server: auth error.
Definition: classic_greeting_forwarder.cc:1198
stdx::expected< Result, std::error_code > final_response()
Definition: classic_greeting_forwarder.cc:1153
void client_greeting_server_adjust_caps(ClassicProtocolState &src_protocol, ClassicProtocolState &dst_protocol)
Definition: classic_greeting_forwarder.cc:449
Definition: expected.h:286
borrowable::message::server::Error< false > Error
Definition: classic_protocol_message.h:1411
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Definition: gcs_xcom_synode.h:64
Definition: trace_span.h:35