MySQL 8.4.11
Source Code Documentation
classic_greeting_forwarder.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2023, 2026, 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_SRC_PROCESSORS_SESSION_CLASSIC_GREETING_FORWARDER_H_
27#define ROUTING_SRC_PROCESSORS_SESSION_CLASSIC_GREETING_FORWARDER_H_
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 std::optional<std::string_view> diagnostic_stage_name() const override;
104
105 void failed(
106 const std::optional<classic_protocol::message::server::Error> &err) {
107 failed_ = err;
108 }
109
110 std::optional<classic_protocol::message::server::Error> failed() const {
111 return failed_;
112 }
113
114 private:
129
131 ClassicProtocolState &dst_protocol);
132
134
136
137 std::optional<classic_protocol::message::server::Error> failed_;
138
139 std::function<void(const classic_protocol::message::server::Error &err)>
141
147};
148
149/**
150 * classic protocol handshake between router<->server and client<->router.
151 *
152 * a server::greeting processor which fetches a server::greeting
153 * to send it to the client.
154 *
155 * c->r : accept()
156 * r->s: connect()
157 * r<-s: server::greeting
158 * c<-r : ...
159 *
160 */
162 public:
163 using ForwardingProcessor::ForwardingProcessor;
164
165 /**
166 * stages of the handshake flow.
167 */
168 enum class Stage {
169 Connect,
172
173 Error,
174 Ok,
175 };
176
178
180 [[nodiscard]] Stage stage() const { return stage_; }
181
182 std::optional<std::string_view> diagnostic_stage_name() const override;
183
184 private:
188
190
191 // start timepoint to calculate the connect-retry-timeout.
192 std::chrono::steady_clock::time_point started_{
193 std::chrono::steady_clock::now()};
194};
195
196/**
197 * authenticates a server connection.
198 *
199 * Assumes the server
200 *
201 * 1. sent a server::greeting already
202 * 2. expects to receive a client::greeting
203 */
205 public:
208 std::function<void(const classic_protocol::message::server::Error &)>
209 on_error)
210 : ForwardingProcessor(conn), on_error_(std::move(on_error)) {}
211
212 /**
213 * stages of the handshake flow.
214 */
215 enum class Stage {
226 AuthOk,
227 AuthError,
230
231 Error,
232 Ok,
233 };
234
236
238 [[nodiscard]] Stage stage() const { return stage_; }
239
240 std::optional<std::string_view> diagnostic_stage_name() const override;
241
242 void failed(
243 const std::optional<classic_protocol::message::server::Error> &err) {
244 failed_ = err;
245 }
246
247 std::optional<classic_protocol::message::server::Error> failed() const {
248 return failed_;
249 }
250
251 private:
266
268 ClassicProtocolState &st_protocol);
269
274
275 std::optional<classic_protocol::message::server::Error> failed_;
276
278
280
281 std::function<void(const classic_protocol::message::server::Error &err)>
283};
284
285#endif // ROUTING_SRC_PROCESSORS_SESSION_CLASSIC_GREETING_FORWARDER_H_
protocol state of a classic protocol connection.
Definition: classic_protocol_state.h:39
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:204
void failed(const std::optional< classic_protocol::message::server::Error > &err)
Definition: classic_greeting_forwarder.h:242
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:272
size_t server_last_send_buf_size_
Definition: classic_greeting_forwarder.h:273
std::optional< std::string_view > diagnostic_stage_name() const override
stage name of a staged processor.
Definition: classic_greeting_forwarder.cc:2280
RouterRequireFetcher::Result required_connection_attributes_fetcher_result_
Definition: classic_greeting_forwarder.h:277
stdx::expected< Result, std::error_code > auth_ok()
server-side: auth is ok.
Definition: classic_greeting_forwarder.cc:2131
stdx::expected< Result, std::error_code > client_greeting()
Definition: classic_greeting_forwarder.cc:1478
size_t client_last_send_buf_size_
Definition: classic_greeting_forwarder.h:271
stdx::expected< Result, std::error_code > fetch_user_attrs()
Definition: classic_greeting_forwarder.cc:2167
std::optional< classic_protocol::message::server::Error > failed() const
Definition: classic_greeting_forwarder.h:247
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:2180
Stage stage_
Definition: classic_greeting_forwarder.h:279
void client_greeting_server_adjust_caps(ClassicProtocolState &rc_protocol, ClassicProtocolState &st_protocol)
Definition: classic_greeting_forwarder.cc:1428
stdx::expected< Result, std::error_code > client_greeting_full()
Definition: classic_greeting_forwarder.cc:1639
stdx::expected< Result, std::error_code > process() override
Definition: classic_greeting_forwarder.cc:1383
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:275
stdx::expected< Result, std::error_code > tls_connect()
Definition: classic_greeting_forwarder.cc:1880
Stage stage() const
Definition: classic_greeting_forwarder.h:238
ServerFirstAuthenticator(MysqlRoutingClassicConnectionBase *conn, std::function< void(const classic_protocol::message::server::Error &)> on_error)
Definition: classic_greeting_forwarder.h:206
stdx::expected< Result, std::error_code > client_greeting_start_tls()
Definition: classic_greeting_forwarder.cc:1561
Stage
stages of the handshake flow.
Definition: classic_greeting_forwarder.h:215
stdx::expected< Result, std::error_code > auth_error()
router<-server: auth error.
Definition: classic_greeting_forwarder.cc:2101
stdx::expected< Result, std::error_code > initial_response()
Definition: classic_greeting_forwarder.cc:2041
size_t client_last_recv_buf_size_
Definition: classic_greeting_forwarder.h:270
stdx::expected< Result, std::error_code > final_response()
Definition: classic_greeting_forwarder.cc:2055
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:237
std::function< void(const classic_protocol::message::server::Error &err)> on_error_
Definition: classic_greeting_forwarder.h:282
classic protocol handshake between router<->server and client<->router.
Definition: classic_greeting_forwarder.h:161
stdx::expected< Result, std::error_code > server_greeted()
received an server::greeting or server::error from the server.
Definition: classic_greeting_forwarder.cc:1339
stdx::expected< Result, std::error_code > process() override
Definition: classic_greeting_forwarder.cc:1270
Stage stage() const
Definition: classic_greeting_forwarder.h:180
stdx::expected< Result, std::error_code > server_greeting()
Definition: classic_greeting_forwarder.cc:1298
Stage stage_
Definition: classic_greeting_forwarder.h:189
stdx::expected< Result, std::error_code > connect()
Definition: classic_greeting_forwarder.cc:1291
std::chrono::steady_clock::time_point started_
Definition: classic_greeting_forwarder.h:192
std::optional< std::string_view > diagnostic_stage_name() const override
stage name of a staged processor.
Definition: classic_greeting_forwarder.cc:2260
void stage(Stage stage)
Definition: classic_greeting_forwarder.h:179
Stage
stages of the handshake flow.
Definition: classic_greeting_forwarder.h:168
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:105
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:1045
bool in_handshake_
Definition: classic_greeting_forwarder.h:133
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:888
TraceEvent * trace_event_server_greeting_
Definition: classic_greeting_forwarder.h:144
TraceEvent * trace_event_greeting_
Definition: classic_greeting_forwarder.h:143
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:110
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:140
std::optional< std::string_view > diagnostic_stage_name() const override
stage name of a staged processor.
Definition: classic_greeting_forwarder.cc:2220
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:1132
TraceEvent * parent_event_
Definition: classic_greeting_forwarder.h:142
TraceEvent * trace_event_client_greeting_
Definition: classic_greeting_forwarder.h:145
stdx::expected< Result, std::error_code > tls_connect()
Definition: classic_greeting_forwarder.cc:936
Stage
stages of the handshake flow.
Definition: classic_greeting_forwarder.h:78
Stage stage_
Definition: classic_greeting_forwarder.h:135
std::optional< classic_protocol::message::server::Error > failed_
Definition: classic_greeting_forwarder.h:137
TraceEvent * trace_event_tls_connect_
Definition: classic_greeting_forwarder.h:146
stdx::expected< Result, std::error_code > auth_ok()
server-side: auth is ok.
Definition: classic_greeting_forwarder.cc:1230
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:1194
stdx::expected< Result, std::error_code > final_response()
Definition: classic_greeting_forwarder.cc:1149
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:927
Definition: gcs_xcom_synode.h:64
Definition: trace_span.h:35