MySQL 8.4.2
Source Code Documentation
classic_protocol_state.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_PROTOCOL_STATE_INCLUDED
27#define ROUTING_CLASSIC_PROTOCOL_STATE_INCLUDED
28
29#include <chrono>
30#include <map>
31#include <optional>
32#include <string>
33
35
36/**
37 * protocol state of a classic protocol connection.
38 */
40 public:
41 enum class HandshakeState {
46 };
47
48 /**
49 * system-variables as returned by the server.
50 *
51 * can be queried from the server with:
52 *
53 * - SELECT @@SESSION.{k}
54 * - SELECT @@LOCAL.{k}
55 *
56 * can be set on the server with:
57 *
58 * - SET k = v;
59 * - SET @@SESSION.k = v;
60 * - SET @@LOCAL.k = v;
61 * - SET SESSION k = v;
62 * - SET LOCAL k = v;
63 *
64 * changes to system-vars on the server are returned via
65 * the sesssion-tracker for system-variables.
66 */
68 public:
69 using key_type = std::string;
70 using key_view_type = std::string_view;
71 using value_type = std::optional<std::string>;
72
73 /**
74 * set k to v.
75 *
76 * if k doesn't exist in the system-vars yet, it gets inserted.
77 */
78 void set(key_type k, value_type v) {
79 vars_.insert_or_assign(std::move(k), std::move(v));
80 }
81
82 /**
83 * find 'k' in sytem-vars.
84 *
85 * @param k key
86 *
87 * if 'k' does not exist in system-vars, a NULL-like value is returned.
88 * otherwise return the value for the system-var referenced by 'k'
89 *
90 * @returns std::nullopt if key is not found, the found value otherwise.
91 */
92 std::optional<value_type> find(const key_view_type &k) const {
93 const auto it = vars_.find(k);
94 if (it == vars_.end()) return {std::nullopt};
95
96 return it->second;
97 }
98
99 /**
100 * get 'k' from system-vars.
101 *
102 * @param k key
103 *
104 * if 'k' does not exist in system-vars, a NULL-like value is returned.
105 * otherwise return the value for the system-var referenced by 'k' which may
106 * be NULL-like or a string.
107 *
108 * @returns std::nullopt if key is not found or value is NULL-like, the
109 * found value otherwise
110 */
111 value_type get(const key_view_type &k) const {
112 const auto it = vars_.find(k);
113 if (it == vars_.end()) return {std::nullopt};
114
115 return it->second;
116 }
117
118 using iterator = std::map<key_type, value_type>::iterator;
119 using const_iterator = std::map<key_type, value_type>::const_iterator;
120
121 iterator begin() { return vars_.begin(); }
122 const_iterator begin() const { return vars_.begin(); }
123 iterator end() { return vars_.end(); }
124 const_iterator end() const { return vars_.end(); }
125
126 /**
127 * check if there is no system-var.
128 */
129 bool empty() const { return vars_.empty(); }
130
131 /**
132 * clear the system-vars.
133 */
134 void clear() { vars_.clear(); }
135
136 private:
137 std::map<key_type, value_type, std::less<>> vars_;
138 };
139
141
145 std::optional<classic_protocol::message::server::Greeting>
147 std::string username, //
148 std::string schema, //
149 std::string attributes)
150 : server_caps_{server_caps},
151 client_caps_{client_caps},
153 username_{std::move(username)},
154 schema_{std::move(schema)},
156
158 server_caps_ = caps;
159 }
160
162 client_caps_ = caps;
163 }
164
166 return client_caps_;
167 }
168
170 return server_caps_;
171 }
172
174 return server_caps_ & client_caps_;
175 }
176
177 std::optional<classic_protocol::message::client::Greeting> client_greeting()
178 const {
179 return client_greeting_;
180 }
181
183 std::optional<classic_protocol::message::client::Greeting> msg) {
184 client_greeting_ = std::move(msg);
185 }
186
187 std::optional<classic_protocol::message::server::Greeting> server_greeting()
188 const {
189 return server_greeting_;
190 }
191
193 std::optional<classic_protocol::message::server::Greeting> msg) {
194 server_greeting_ = std::move(msg);
195 }
196
197 uint8_t &seq_id() { return seq_id_; }
198 uint8_t seq_id() const { return seq_id_; }
199
200 void seq_id(uint8_t id) { seq_id_ = id; }
201
202 struct FrameInfo {
203 uint8_t seq_id_; //!< sequence id.
204 size_t frame_size_; //!< size of the whole frame.
205 size_t forwarded_frame_size_; //!< size of the whole frame that's already
206 //!< forwarded.
207 };
208
209 std::optional<FrameInfo> &current_frame() { return current_frame_; }
210 const std::optional<FrameInfo> &current_frame() const {
211 return current_frame_;
212 }
213
214 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
215 const std::optional<uint8_t> &current_msg_type() const { return msg_type_; }
216
217 uint64_t columns_left{};
218 uint32_t params_left{};
219
220 [[nodiscard]] std::string auth_method_name() const {
221 return auth_method_name_;
222 }
223
224 void auth_method_name(std::string name) {
225 auth_method_name_ = std::move(name);
226 }
227
228 [[nodiscard]] std::string auth_method_data() const {
229 return auth_method_data_;
230 }
231
232 void auth_method_data(std::string data) {
233 auth_method_data_ = std::move(data);
234 }
235
236 std::string username() { return username_; }
237 void username(std::string user) { username_ = std::move(user); }
238
239 std::string schema() { return schema_; }
240 void schema(std::string s) { schema_ = std::move(s); }
241
242 // connection attributes there were received.
243 std::string attributes() { return recv_attributes_; }
244 void attributes(std::string attrs) { recv_attributes_ = std::move(attrs); }
245
246 // connection attributes that were sent.
247 std::string sent_attributes() { return sent_attributes_; }
248 void sent_attributes(std::string attrs) {
249 sent_attributes_ = std::move(attrs);
250 }
251
253
255
256#if 0
257 classic_protocol::status::value_type status_flags() const {
258 return status_flags_;
259 }
260
261 void status_flags(classic_protocol::status::value_type val) {
262 status_flags_ = val;
263 }
264#endif
265
267
269
270 private:
273
274 std::optional<classic_protocol::message::client::Greeting> client_greeting_{};
275 std::optional<classic_protocol::message::server::Greeting> server_greeting_{};
276
277 std::optional<FrameInfo> current_frame_{};
278 std::optional<uint8_t> msg_type_{};
279
280 uint8_t seq_id_{255}; // next use will increment to 0
281
282 std::string username_;
283 std::string schema_;
284 std::string recv_attributes_;
285 std::string sent_attributes_;
286
287 std::string auth_method_name_;
288 std::string auth_method_data_;
289
290 // status flags of the last statement.
292
294
296};
297
299 public:
301
302 void password(std::optional<std::string> pw) { password_ = std::move(pw); }
303 const std::optional<std::string> &password() const { return password_; }
304
306 return status_flags_;
307 }
308
310 status_flags_ = val;
311 }
312
313 using PreparedStatements = std::unordered_map<uint32_t, PreparedStatement>;
314
316 return prepared_stmts_;
317 }
319
320 /**
321 * trace the events of the commands.
322 *
323 * - enabled by ROUTER SET trace = 1
324 * - disabled by ROUTER SET trace = 0, change-user or reset-connection.
325 *
326 * @retval true if 'ROUTER SET trace' is '1'
327 * @retval false if 'ROUTER SET trace' is '0'
328 */
329 bool trace_commands() const { return trace_commands_; }
330 void trace_commands(bool val) { trace_commands_ = val; }
331
332 // executed GTIDs for this connection.
333 void gtid_executed(const std::string &gtid_execed) {
334 gtid_executed_ = gtid_execed;
335 }
336 std::string gtid_executed() const { return gtid_executed_; }
337
340
343 }
346 }
347
348 enum class AccessMode {
349 ReadWrite,
350 ReadOnly,
351 };
352
353 std::optional<AccessMode> access_mode() const { return access_mode_; }
354 void access_mode(std::optional<AccessMode> v) { access_mode_ = v; }
355
356 private:
357 std::optional<std::string> password_;
358
359 // status flags of the last statement.
361
363
364 // if commands shall be traced.
365 bool trace_commands_{false};
366
367 std::string gtid_executed_;
368
371
372 std::optional<AccessMode> access_mode_{};
373};
374
376 public:
378};
379
380#endif
system-variables as returned by the server.
Definition: classic_protocol_state.h:67
const_iterator begin() const
Definition: classic_protocol_state.h:122
std::map< key_type, value_type >::const_iterator const_iterator
Definition: classic_protocol_state.h:119
std::string_view key_view_type
Definition: classic_protocol_state.h:70
void clear()
clear the system-vars.
Definition: classic_protocol_state.h:134
void set(key_type k, value_type v)
set k to v.
Definition: classic_protocol_state.h:78
std::map< key_type, value_type, std::less<> > vars_
Definition: classic_protocol_state.h:137
std::optional< value_type > find(const key_view_type &k) const
find 'k' in sytem-vars.
Definition: classic_protocol_state.h:92
std::map< key_type, value_type >::iterator iterator
Definition: classic_protocol_state.h:118
iterator end()
Definition: classic_protocol_state.h:123
iterator begin()
Definition: classic_protocol_state.h:121
bool empty() const
check if there is no system-var.
Definition: classic_protocol_state.h:129
std::string key_type
Definition: classic_protocol_state.h:69
const_iterator end() const
Definition: classic_protocol_state.h:124
value_type get(const key_view_type &k) const
get 'k' from system-vars.
Definition: classic_protocol_state.h:111
std::optional< std::string > value_type
Definition: classic_protocol_state.h:71
protocol state of a classic protocol connection.
Definition: classic_protocol_state.h:39
HandshakeState handshake_state_
Definition: classic_protocol_state.h:293
classic_protocol::capabilities::value_type server_caps_
Definition: classic_protocol_state.h:271
uint8_t seq_id() const
Definition: classic_protocol_state.h:198
std::string schema()
Definition: classic_protocol_state.h:239
ClassicProtocolState()=default
void sent_attributes(std::string attrs)
Definition: classic_protocol_state.h:248
void seq_id(uint8_t id)
Definition: classic_protocol_state.h:200
std::string auth_method_name() const
Definition: classic_protocol_state.h:220
const std::optional< FrameInfo > & current_frame() const
Definition: classic_protocol_state.h:210
void handshake_state(HandshakeState state)
Definition: classic_protocol_state.h:254
uint8_t & seq_id()
Definition: classic_protocol_state.h:197
std::optional< classic_protocol::message::server::Greeting > server_greeting() const
Definition: classic_protocol_state.h:187
HandshakeState handshake_state() const
Definition: classic_protocol_state.h:252
void server_capabilities(classic_protocol::capabilities::value_type caps)
Definition: classic_protocol_state.h:157
std::string auth_method_data() const
Definition: classic_protocol_state.h:228
HandshakeState
Definition: classic_protocol_state.h:41
std::string sent_attributes()
Definition: classic_protocol_state.h:247
std::string auth_method_name_
Definition: classic_protocol_state.h:287
void client_greeting(std::optional< classic_protocol::message::client::Greeting > msg)
Definition: classic_protocol_state.h:182
void server_greeting(std::optional< classic_protocol::message::server::Greeting > msg)
Definition: classic_protocol_state.h:192
std::string sent_attributes_
Definition: classic_protocol_state.h:285
std::optional< FrameInfo > & current_frame()
Definition: classic_protocol_state.h:209
std::optional< uint8_t > msg_type_
Definition: classic_protocol_state.h:278
std::optional< uint8_t > & current_msg_type()
Definition: classic_protocol_state.h:214
classic_protocol::capabilities::value_type client_caps_
Definition: classic_protocol_state.h:272
SystemVariables system_variables_
Definition: classic_protocol_state.h:295
uint8_t seq_id_
Definition: classic_protocol_state.h:280
void client_capabilities(classic_protocol::capabilities::value_type caps)
Definition: classic_protocol_state.h:161
uint64_t columns_left
Definition: classic_protocol_state.h:217
void schema(std::string s)
Definition: classic_protocol_state.h:240
std::optional< classic_protocol::message::server::Greeting > server_greeting_
Definition: classic_protocol_state.h:275
std::string username_
Definition: classic_protocol_state.h:282
void auth_method_name(std::string name)
Definition: classic_protocol_state.h:224
void attributes(std::string attrs)
Definition: classic_protocol_state.h:244
std::optional< FrameInfo > current_frame_
Definition: classic_protocol_state.h:277
ClassicProtocolState(classic_protocol::capabilities::value_type server_caps, classic_protocol::capabilities::value_type client_caps, std::optional< classic_protocol::message::server::Greeting > server_greeting, std::string username, std::string schema, std::string attributes)
Definition: classic_protocol_state.h:142
classic_protocol::capabilities::value_type client_capabilities() const
Definition: classic_protocol_state.h:165
uint32_t params_left
Definition: classic_protocol_state.h:218
classic_protocol::status::value_type status_flags_
Definition: classic_protocol_state.h:291
void auth_method_data(std::string data)
Definition: classic_protocol_state.h:232
std::optional< classic_protocol::message::client::Greeting > client_greeting() const
Definition: classic_protocol_state.h:177
std::string auth_method_data_
Definition: classic_protocol_state.h:288
const SystemVariables & system_variables() const
Definition: classic_protocol_state.h:268
const std::optional< uint8_t > & current_msg_type() const
Definition: classic_protocol_state.h:215
std::optional< classic_protocol::message::client::Greeting > client_greeting_
Definition: classic_protocol_state.h:274
std::string recv_attributes_
Definition: classic_protocol_state.h:284
classic_protocol::capabilities::value_type server_capabilities() const
Definition: classic_protocol_state.h:169
void username(std::string user)
Definition: classic_protocol_state.h:237
std::string username()
Definition: classic_protocol_state.h:236
std::string attributes()
Definition: classic_protocol_state.h:243
classic_protocol::capabilities::value_type shared_capabilities() const
Definition: classic_protocol_state.h:173
SystemVariables & system_variables()
Definition: classic_protocol_state.h:266
std::string schema_
Definition: classic_protocol_state.h:283
Definition: classic_protocol_state.h:298
bool trace_commands() const
trace the events of the commands.
Definition: classic_protocol_state.h:329
void gtid_executed(const std::string &gtid_execed)
Definition: classic_protocol_state.h:333
classic_protocol::status::value_type status_flags_
Definition: classic_protocol_state.h:360
PreparedStatements prepared_stmts_
Definition: classic_protocol_state.h:362
void wait_for_my_writes(bool v)
Definition: classic_protocol_state.h:338
const PreparedStatements & prepared_statements() const
Definition: classic_protocol_state.h:315
std::optional< AccessMode > access_mode_
Definition: classic_protocol_state.h:372
void access_mode(std::optional< AccessMode > v)
Definition: classic_protocol_state.h:354
std::string gtid_executed() const
Definition: classic_protocol_state.h:336
PreparedStatements & prepared_statements()
Definition: classic_protocol_state.h:318
bool wait_for_my_writes_
Definition: classic_protocol_state.h:369
bool wait_for_my_writes() const
Definition: classic_protocol_state.h:339
std::unordered_map< uint32_t, PreparedStatement > PreparedStatements
Definition: classic_protocol_state.h:313
void password(std::optional< std::string > pw)
Definition: classic_protocol_state.h:302
std::optional< std::string > password_
Definition: classic_protocol_state.h:357
void trace_commands(bool val)
Definition: classic_protocol_state.h:330
classic_protocol::status::value_type status_flags() const
Definition: classic_protocol_state.h:305
std::string gtid_executed_
Definition: classic_protocol_state.h:367
bool trace_commands_
Definition: classic_protocol_state.h:365
AccessMode
Definition: classic_protocol_state.h:348
std::chrono::seconds wait_for_my_writes_timeout_
Definition: classic_protocol_state.h:370
std::chrono::seconds wait_for_my_writes_timeout() const
Definition: classic_protocol_state.h:341
void status_flags(classic_protocol::status::value_type val)
Definition: classic_protocol_state.h:309
const std::optional< std::string > & password() const
Definition: classic_protocol_state.h:303
std::optional< AccessMode > access_mode() const
Definition: classic_protocol_state.h:353
void wait_for_my_writes_timeout(std::chrono::seconds timeout)
Definition: classic_protocol_state.h:344
Definition: classic_protocol_state.h:375
char * user
Definition: mysqladmin.cc:66
std::bitset< 32 > value_type
Definition: classic_protocol_constants.h:73
std::bitset< 16 > value_type
Definition: classic_protocol_constants.h:168
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
Definition: gcs_xcom_synode.h:64
case opt name
Definition: sslopt-case.h:29
Definition: classic_protocol_state.h:202
size_t forwarded_frame_size_
size of the whole frame that's already forwarded.
Definition: classic_protocol_state.h:205
size_t frame_size_
size of the whole frame.
Definition: classic_protocol_state.h:204
uint8_t seq_id_
sequence id.
Definition: classic_protocol_state.h:203
double seconds()
Definition: task.cc:310
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510