MySQL 8.4.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 <optional>
31
33
34/**
35 * protocol state of a classic protocol connection.
36 */
38 public:
39 enum class HandshakeState {
44 };
45
47
51 std::optional<classic_protocol::message::server::Greeting>
53 std::string username, //
54 std::string schema, //
55 std::string attributes)
56 : server_caps_{server_caps},
57 client_caps_{client_caps},
59 username_{std::move(username)},
60 schema_{std::move(schema)},
62
64 server_caps_ = caps;
65 }
66
68 client_caps_ = caps;
69 }
70
72 return client_caps_;
73 }
74
76 return server_caps_;
77 }
78
81 }
82
83 std::optional<classic_protocol::message::client::Greeting> client_greeting()
84 const {
85 return client_greeting_;
86 }
87
89 std::optional<classic_protocol::message::client::Greeting> msg) {
90 client_greeting_ = std::move(msg);
91 }
92
93 std::optional<classic_protocol::message::server::Greeting> server_greeting()
94 const {
95 return server_greeting_;
96 }
97
99 std::optional<classic_protocol::message::server::Greeting> msg) {
100 server_greeting_ = std::move(msg);
101 }
102
103 uint8_t &seq_id() { return seq_id_; }
104 uint8_t seq_id() const { return seq_id_; }
105
106 void seq_id(uint8_t id) { seq_id_ = id; }
107
108 struct FrameInfo {
109 uint8_t seq_id_; //!< sequence id.
110 size_t frame_size_; //!< size of the whole frame.
111 size_t forwarded_frame_size_; //!< size of the whole frame that's already
112 //!< forwarded.
113 };
114
115 std::optional<FrameInfo> &current_frame() { return current_frame_; }
116 const std::optional<FrameInfo> &current_frame() const {
117 return current_frame_;
118 }
119
120 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
121 const std::optional<uint8_t> &current_msg_type() const { return msg_type_; }
122
123 uint64_t columns_left{};
124 uint32_t params_left{};
125
126 [[nodiscard]] std::string auth_method_name() const {
127 return auth_method_name_;
128 }
129
130 void auth_method_name(std::string name) {
131 auth_method_name_ = std::move(name);
132 }
133
134 [[nodiscard]] std::string auth_method_data() const {
135 return auth_method_data_;
136 }
137
138 void auth_method_data(std::string data) {
139 auth_method_data_ = std::move(data);
140 }
141
142 std::string username() { return username_; }
143 void username(std::string user) { username_ = std::move(user); }
144
145 std::string schema() { return schema_; }
146 void schema(std::string s) { schema_ = std::move(s); }
147
148 // connection attributes there were received.
149 std::string attributes() { return recv_attributes_; }
150 void attributes(std::string attrs) { recv_attributes_ = std::move(attrs); }
151
152 // connection attributes that were sent.
153 std::string sent_attributes() { return sent_attributes_; }
154 void sent_attributes(std::string attrs) {
155 sent_attributes_ = std::move(attrs);
156 }
157
159
161
162#if 0
163 classic_protocol::status::value_type status_flags() const {
164 return status_flags_;
165 }
166
167 void status_flags(classic_protocol::status::value_type val) {
168 status_flags_ = val;
169 }
170#endif
171
172 private:
175
176 std::optional<classic_protocol::message::client::Greeting> client_greeting_{};
177 std::optional<classic_protocol::message::server::Greeting> server_greeting_{};
178
179 std::optional<FrameInfo> current_frame_{};
180 std::optional<uint8_t> msg_type_{};
181
182 uint8_t seq_id_{255}; // next use will increment to 0
183
184 std::string username_;
185 std::string schema_;
186 std::string recv_attributes_;
187 std::string sent_attributes_;
188
189 std::string auth_method_name_;
190 std::string auth_method_data_;
191
192 // status flags of the last statement.
194
196};
197
199 public:
201
202 void password(std::optional<std::string> pw) { password_ = std::move(pw); }
203 const std::optional<std::string> &password() const { return password_; }
204
206 return status_flags_;
207 }
208
210 status_flags_ = val;
211 }
212
213 using PreparedStatements = std::unordered_map<uint32_t, PreparedStatement>;
214
216 return prepared_stmts_;
217 }
219
220 /**
221 * trace the events of the commands.
222 *
223 * - enabled by ROUTER SET trace = 1
224 * - disabled by ROUTER SET trace = 0, change-user or reset-connection.
225 *
226 * @retval true if 'ROUTER SET trace' is '1'
227 * @retval false if 'ROUTER SET trace' is '0'
228 */
229 bool trace_commands() const { return trace_commands_; }
230 void trace_commands(bool val) { trace_commands_ = val; }
231
232 // executed GTIDs for this connection.
233 void gtid_executed(const std::string &gtid_execed) {
234 gtid_executed_ = gtid_execed;
235 }
236 std::string gtid_executed() const { return gtid_executed_; }
237
240
243 }
246 }
247
248 enum class AccessMode {
249 ReadWrite,
250 ReadOnly,
251 };
252
253 std::optional<AccessMode> access_mode() const { return access_mode_; }
254 void access_mode(std::optional<AccessMode> v) { access_mode_ = v; }
255
256 private:
257 std::optional<std::string> password_;
258
259 // status flags of the last statement.
261
263
264 // if commands shall be traced.
265 bool trace_commands_{false};
266
267 std::string gtid_executed_;
268
271
272 std::optional<AccessMode> access_mode_{};
273};
274
276 public:
278};
279
280#endif
protocol state of a classic protocol connection.
Definition: classic_protocol_state.h:37
HandshakeState handshake_state_
Definition: classic_protocol_state.h:195
classic_protocol::capabilities::value_type server_caps_
Definition: classic_protocol_state.h:173
uint8_t seq_id() const
Definition: classic_protocol_state.h:104
std::string schema()
Definition: classic_protocol_state.h:145
ClassicProtocolState()=default
void sent_attributes(std::string attrs)
Definition: classic_protocol_state.h:154
void seq_id(uint8_t id)
Definition: classic_protocol_state.h:106
std::string auth_method_name() const
Definition: classic_protocol_state.h:126
const std::optional< FrameInfo > & current_frame() const
Definition: classic_protocol_state.h:116
void handshake_state(HandshakeState state)
Definition: classic_protocol_state.h:160
uint8_t & seq_id()
Definition: classic_protocol_state.h:103
std::optional< classic_protocol::message::server::Greeting > server_greeting() const
Definition: classic_protocol_state.h:93
HandshakeState handshake_state() const
Definition: classic_protocol_state.h:158
void server_capabilities(classic_protocol::capabilities::value_type caps)
Definition: classic_protocol_state.h:63
std::string auth_method_data() const
Definition: classic_protocol_state.h:134
HandshakeState
Definition: classic_protocol_state.h:39
std::string sent_attributes()
Definition: classic_protocol_state.h:153
std::string auth_method_name_
Definition: classic_protocol_state.h:189
void client_greeting(std::optional< classic_protocol::message::client::Greeting > msg)
Definition: classic_protocol_state.h:88
void server_greeting(std::optional< classic_protocol::message::server::Greeting > msg)
Definition: classic_protocol_state.h:98
std::string sent_attributes_
Definition: classic_protocol_state.h:187
std::optional< FrameInfo > & current_frame()
Definition: classic_protocol_state.h:115
std::optional< uint8_t > msg_type_
Definition: classic_protocol_state.h:180
std::optional< uint8_t > & current_msg_type()
Definition: classic_protocol_state.h:120
classic_protocol::capabilities::value_type client_caps_
Definition: classic_protocol_state.h:174
uint8_t seq_id_
Definition: classic_protocol_state.h:182
void client_capabilities(classic_protocol::capabilities::value_type caps)
Definition: classic_protocol_state.h:67
uint64_t columns_left
Definition: classic_protocol_state.h:123
void schema(std::string s)
Definition: classic_protocol_state.h:146
std::optional< classic_protocol::message::server::Greeting > server_greeting_
Definition: classic_protocol_state.h:177
std::string username_
Definition: classic_protocol_state.h:184
void auth_method_name(std::string name)
Definition: classic_protocol_state.h:130
void attributes(std::string attrs)
Definition: classic_protocol_state.h:150
std::optional< FrameInfo > current_frame_
Definition: classic_protocol_state.h:179
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:48
classic_protocol::capabilities::value_type client_capabilities() const
Definition: classic_protocol_state.h:71
uint32_t params_left
Definition: classic_protocol_state.h:124
classic_protocol::status::value_type status_flags_
Definition: classic_protocol_state.h:193
void auth_method_data(std::string data)
Definition: classic_protocol_state.h:138
std::optional< classic_protocol::message::client::Greeting > client_greeting() const
Definition: classic_protocol_state.h:83
std::string auth_method_data_
Definition: classic_protocol_state.h:190
const std::optional< uint8_t > & current_msg_type() const
Definition: classic_protocol_state.h:121
std::optional< classic_protocol::message::client::Greeting > client_greeting_
Definition: classic_protocol_state.h:176
std::string recv_attributes_
Definition: classic_protocol_state.h:186
classic_protocol::capabilities::value_type server_capabilities() const
Definition: classic_protocol_state.h:75
void username(std::string user)
Definition: classic_protocol_state.h:143
std::string username()
Definition: classic_protocol_state.h:142
std::string attributes()
Definition: classic_protocol_state.h:149
classic_protocol::capabilities::value_type shared_capabilities() const
Definition: classic_protocol_state.h:79
std::string schema_
Definition: classic_protocol_state.h:185
Definition: classic_protocol_state.h:198
bool trace_commands() const
trace the events of the commands.
Definition: classic_protocol_state.h:229
void gtid_executed(const std::string &gtid_execed)
Definition: classic_protocol_state.h:233
classic_protocol::status::value_type status_flags_
Definition: classic_protocol_state.h:260
PreparedStatements prepared_stmts_
Definition: classic_protocol_state.h:262
void wait_for_my_writes(bool v)
Definition: classic_protocol_state.h:238
const PreparedStatements & prepared_statements() const
Definition: classic_protocol_state.h:215
std::optional< AccessMode > access_mode_
Definition: classic_protocol_state.h:272
void access_mode(std::optional< AccessMode > v)
Definition: classic_protocol_state.h:254
std::string gtid_executed() const
Definition: classic_protocol_state.h:236
PreparedStatements & prepared_statements()
Definition: classic_protocol_state.h:218
bool wait_for_my_writes_
Definition: classic_protocol_state.h:269
bool wait_for_my_writes() const
Definition: classic_protocol_state.h:239
std::unordered_map< uint32_t, PreparedStatement > PreparedStatements
Definition: classic_protocol_state.h:213
void password(std::optional< std::string > pw)
Definition: classic_protocol_state.h:202
std::optional< std::string > password_
Definition: classic_protocol_state.h:257
void trace_commands(bool val)
Definition: classic_protocol_state.h:230
classic_protocol::status::value_type status_flags() const
Definition: classic_protocol_state.h:205
std::string gtid_executed_
Definition: classic_protocol_state.h:267
bool trace_commands_
Definition: classic_protocol_state.h:265
AccessMode
Definition: classic_protocol_state.h:248
std::chrono::seconds wait_for_my_writes_timeout_
Definition: classic_protocol_state.h:270
std::chrono::seconds wait_for_my_writes_timeout() const
Definition: classic_protocol_state.h:241
void status_flags(classic_protocol::status::value_type val)
Definition: classic_protocol_state.h:209
const std::optional< std::string > & password() const
Definition: classic_protocol_state.h:203
std::optional< AccessMode > access_mode() const
Definition: classic_protocol_state.h:253
void wait_for_my_writes_timeout(std::chrono::seconds timeout)
Definition: classic_protocol_state.h:244
Definition: classic_protocol_state.h:275
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:108
size_t forwarded_frame_size_
size of the whole frame that's already forwarded.
Definition: classic_protocol_state.h:111
size_t frame_size_
size of the whole frame.
Definition: classic_protocol_state.h:110
uint8_t seq_id_
sequence id.
Definition: classic_protocol_state.h:109
double seconds()
Definition: task.cc:310
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510