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