MySQL 8.0.40
Source Code Documentation
protocol_classic.h
Go to the documentation of this file.
1#ifndef PROTOCOL_CLASSIC_INCLUDED
2#define PROTOCOL_CLASSIC_INCLUDED
3
4/* Copyright (c) 2002, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <stddef.h>
28#include <sys/types.h>
29
30#include "field_types.h" // enum_field_types
31#include "m_ctype.h"
32#include "my_command.h"
33#include "my_inttypes.h"
34#include "my_io.h"
35#include "mysql_com.h"
36#include "mysql_time.h"
37#include "sql/protocol.h" // Protocol
38#include "violite.h"
39
40class Item_param;
41class Send_field;
42class String;
43class i_string;
44class my_decimal;
45template <class T>
46class I_List;
47template <class T>
48class List;
49union COM_DATA;
50class THD;
51
52class Protocol_classic : public Protocol {
53 private:
55 bool parse_packet(union COM_DATA *data, enum_server_command cmd);
56 bool net_store_data_with_conversion(const uchar *from, size_t length,
57 const CHARSET_INFO *fromcs,
58 const CHARSET_INFO *tocs);
59
60 protected:
66#ifndef NDEBUG
69#endif
75
76 bool send_ok(uint server_status, uint statement_warn_count,
77 ulonglong affected_rows, ulonglong last_insert_id,
78 const char *message) override;
79
80 bool send_eof(uint server_status, uint statement_warn_count) override;
81
82 bool send_error(uint sql_errno, const char *err_msg,
83 const char *sql_state) override;
84 bool store_ps_status(ulong stmt_id, uint column_count, uint param_count,
85 ulong cond_count) override;
86
87 public:
90 : send_metadata(false), input_packet_length(0), bad_packet(true) {}
92 : send_metadata(false),
95 bad_packet(true) {
96 init(thd);
97 }
98 void init(THD *thd_arg);
99 bool store_field(const Field *field) final;
100 bool store_string(const char *from, size_t length,
101 const CHARSET_INFO *cs) final;
102 int read_packet() override;
103 int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
104 /**
105 Parses the passed parameters and creates a command
106
107 @param com_data out parameter
108 @param cmd in parameter
109 @param pkt packet to be parsed
110 @param length size of the packet
111
112 @retval false ok
113 @retval true error
114 */
115 bool create_command(COM_DATA *com_data, enum_server_command cmd, uchar *pkt,
116 size_t length);
117 bool flush() override;
118 void end_partial_result_set() override;
119
120 bool end_row() override;
121 uint get_rw_status() override;
122 bool get_compression() override;
123
124 char *get_compression_algorithm() override;
125 uint get_compression_level() override;
126
127 bool start_result_metadata(uint num_cols, uint flags,
128 const CHARSET_INFO *resultcs) override;
129 bool end_result_metadata() override;
131 const CHARSET_INFO *item_charset) override;
132 void abort_row() override {}
133
134 /**
135 Returns the type of the connection
136
137 @return
138 enum enum_vio_type
139 */
140 enum enum_vio_type connection_type() const override {
141 const Vio *v = get_vio();
142 return v ? vio_type(v) : NO_VIO_TYPE;
143 }
144
146 // NET interaction functions
147 /* Initialize NET */
148 bool init_net(Vio *vio);
149 void claim_memory_ownership(bool claim);
150 /* Deinitialize NET */
151 void end_net();
152 /* Write data to NET buffer */
153 bool write(const uchar *ptr, size_t len);
154 /* Return last error from NET */
156 /* Set max allowed packet size */
157 void set_max_packet_size(ulong max_packet_size);
158 /* Deinitialize VIO */
159 int shutdown(bool server_shutdown = false) override;
160 /* Wipe NET with zeros */
161 void wipe_net();
162 /* Check whether VIO is healhty */
163 bool connection_alive() const override;
164 /* Returns the client capabilities */
166 /* Sets the client capabilities */
167 void set_client_capabilities(ulong client_capabilities) {
168 this->m_client_capabilities = client_capabilities;
169 }
170 /* Adds client capability */
171 void add_client_capability(ulong client_capability) {
172 m_client_capabilities |= client_capability;
173 }
174 /* Removes a client capability*/
175 void remove_client_capability(unsigned long capability) {
176 m_client_capabilities &= ~capability;
177 }
178 /* Returns true if the client has the capability and false otherwise*/
179 bool has_client_capability(unsigned long client_capability) override {
180 return (bool)(m_client_capabilities & client_capability);
181 }
182 // TODO: temporary functions. Will be removed.
183 // DON'T USE IN ANY NEW FEATURES.
184 /* Return NET */
185 NET *get_net();
186 /* return VIO */
187 Vio *get_vio();
188 const Vio *get_vio() const;
189 /* Set VIO */
190 void set_vio(Vio *vio);
191 /* Set packet number */
192 void set_output_pkt_nr(uint pkt_nr);
193 /* Return packet number */
195 /* Return packet string */
197 /* return packet length */
199 /* Return raw packet buffer */
201 /* Set read timeout */
202 virtual void set_read_timeout(ulong read_timeout,
203 bool on_full_packet = false);
204 /* Set write timeout */
205 virtual void set_write_timeout(ulong write_timeout);
206
207 /**
208 * Sets the character set expected by the client. This function is for unit
209 * tests. It should usually be set by calling start_result_metadata().
210 */
213 }
214};
215
216/** Class used for the old (MySQL 4.0 protocol). */
217
219 public:
220 Protocol_text() = default;
221 Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
222 bool store_null() override;
223 bool store_tiny(longlong from, uint32 zerofill) override;
224 bool store_short(longlong from, uint32 zerofill) override;
225 bool store_long(longlong from, uint32 zerofill) override;
226 bool store_longlong(longlong from, bool unsigned_flag,
227 uint32 zerofill) override;
228 bool store_decimal(const my_decimal *, uint, uint) final;
229 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
230 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
231 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
232 bool store_date(const MYSQL_TIME &time) override;
233 bool store_time(const MYSQL_TIME &time, uint precision) override;
234 void start_row() override;
235 bool send_parameters(List<Item_param> *parameters, bool) override;
236
237 enum enum_protocol_type type() const override { return PROTOCOL_TEXT; }
238};
239
240class Protocol_binary final : public Protocol_text {
241 private:
243
244 public:
245 Protocol_binary() = default;
246 Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
247 void start_row() override;
248 bool store_null() override;
249 bool store_tiny(longlong from, uint32 zerofill) override;
250 bool store_short(longlong from, uint32 zerofill) override;
251 bool store_long(longlong from, uint32 zerofill) override;
252 bool store_longlong(longlong from, bool unsigned_flag,
253 uint32 zerofill) override;
254 // Decimals are sent as text, also over the binary protocol.
256 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
257 bool store_date(const MYSQL_TIME &time) override;
258 bool store_time(const MYSQL_TIME &time, uint precision) override;
259 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
260 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
261
262 bool start_result_metadata(uint num_cols, uint flags,
263 const CHARSET_INFO *resultcs) override;
264 bool send_parameters(List<Item_param> *parameters,
265 bool is_sql_prepare) override;
266
267 enum enum_protocol_type type() const override { return PROTOCOL_BINARY; }
268};
269
270bool net_send_error(THD *thd, uint sql_errno, const char *err);
271bool net_send_error(NET *net, uint sql_errno, const char *err);
272uchar *net_store_data(uchar *to, const uchar *from, size_t length);
273bool store(Protocol *prot, I_List<i_string> *str_list);
274#endif /* PROTOCOL_CLASSIC_INCLUDED */
Definition: field.h:575
Definition: sql_list.h:811
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4574
Definition: sql_list.h:434
Definition: protocol_classic.h:240
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3801
enum enum_protocol_type type() const override
Definition: protocol_classic.h:267
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3939
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3862
bool send_parameters(List< Item_param > *parameters, bool is_sql_prepare) override
Sends OUT-parameters by writing the values to the protocol.
Definition: protocol_classic.cc:3664
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3875
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3919
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3823
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3835
bool store_null() override
Definition: protocol_classic.cc:3791
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3849
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3784
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3811
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:246
uint bit_fields
Definition: protocol_classic.h:242
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3776
Protocol_binary()=default
Definition: protocol_classic.h:52
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_classic.cc:3023
void set_vio(Vio *vio)
Definition: protocol_classic.cc:1398
int read_packet() override
Read packet from client.
Definition: protocol_classic.cc:1408
ulong input_packet_length
Definition: protocol_classic.h:72
bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2832
bool flush() override
Used for the classic protocol.
Definition: protocol_classic.cc:3036
void claim_memory_ownership(bool claim)
Definition: protocol_classic.cc:1368
bool write(const uchar *ptr, size_t len)
Definition: protocol_classic.cc:1378
const CHARSET_INFO * result_cs
Definition: protocol_classic.h:74
NET * get_net()
Definition: protocol_classic.cc:1392
virtual void set_write_timeout(ulong write_timeout)
Definition: protocol_classic.cc:1359
enum enum_field_types * field_types
Definition: protocol_classic.h:67
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3078
bool end_row() override
Definition: protocol_classic.cc:3361
bool connection_alive() const override
Checks if the protocol's connection with the client is still alive.
Definition: protocol_classic.cc:3395
ulong get_client_capabilities() override
Returns the client capabilities stored on the protocol.
Definition: protocol_classic.h:165
void set_client_capabilities(ulong client_capabilities)
Definition: protocol_classic.h:167
uchar * get_raw_packet()
Definition: protocol_classic.h:200
void wipe_net()
Definition: protocol_classic.cc:1384
uchar * input_raw_packet
Definition: protocol_classic.h:73
uchar get_error()
Definition: protocol_classic.cc:1382
String convert
Definition: protocol_classic.h:63
int get_command(COM_DATA *com_data, enum_server_command *cmd) override
Reads the command from the protocol and creates a command.
Definition: protocol_classic.cc:2989
bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset) override
Sends a single column metadata.
Definition: protocol_classic.cc:3267
bool store_ps_status(ulong stmt_id, uint column_count, uint param_count, ulong cond_count) override
Sends prepared statement's id and metadata to the client after prepare.
Definition: protocol_classic.cc:3038
bool store_field(const Field *field) final
Definition: protocol_classic.cc:1284
enum enum_vio_type connection_type() const override
Returns the type of the connection.
Definition: protocol_classic.h:140
void add_client_capability(ulong client_capability)
Definition: protocol_classic.h:171
bool send_eof(uint server_status, uint statement_warn_count) override
A default implementation of "EOF" packet response to the client.
Definition: protocol_classic.cc:1315
Protocol_classic(THD *thd)
Definition: protocol_classic.h:91
bool get_compression() override
Returns if the protocol is compressed or not.
Definition: protocol_classic.cc:3060
Protocol_classic()
Definition: protocol_classic.h:89
void end_net()
Definition: protocol_classic.cc:1372
String * packet
Definition: protocol_classic.h:62
bool send_ok(uint server_status, uint statement_warn_count, ulonglong affected_rows, ulonglong last_insert_id, const char *message) override
A default implementation of "OK" packet response to the client.
Definition: protocol_classic.cc:1297
uint sending_flags
Definition: protocol_classic.h:71
void init(THD *thd_arg)
Definition: protocol_classic.cc:1276
Vio * get_vio()
Definition: protocol_classic.cc:1394
bool end_result_metadata() override
Signals the client that the metadata sending is done.
Definition: protocol_classic.cc:3123
bool init_net(Vio *vio)
Definition: protocol_classic.cc:1364
bool net_store_data_with_conversion(const uchar *from, size_t length, const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs)
net_store_data() - extended version with character set conversion.
Definition: protocol_classic.cc:550
uint field_count
Definition: protocol_classic.h:70
my_socket get_socket()
Definition: protocol_classic.cc:3982
void remove_client_capability(unsigned long capability)
Definition: protocol_classic.h:175
bool create_command(COM_DATA *com_data, enum_server_command cmd, uchar *pkt, size_t length)
Parses the passed parameters and creates a command.
Definition: protocol_classic.cc:2980
bool send_error(uint sql_errno, const char *err_msg, const char *sql_state) override
A default implementation of "ERROR" packet response to the client.
Definition: protocol_classic.cc:1341
void set_result_character_set(const CHARSET_INFO *charset)
Sets the character set expected by the client.
Definition: protocol_classic.h:211
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) final
Definition: protocol_classic.cc:3421
ulong get_packet_length()
Definition: protocol_classic.h:198
void abort_row() override
Definition: protocol_classic.h:132
uint field_pos
Definition: protocol_classic.h:64
uint count
Definition: protocol_classic.h:68
String * get_output_packet()
Definition: protocol_classic.cc:1406
void set_output_pkt_nr(uint pkt_nr)
Definition: protocol_classic.cc:1400
bool has_client_capability(unsigned long client_capability) override
Checks if the client capabilities include the one specified as parameter.
Definition: protocol_classic.h:179
uint get_output_pkt_nr()
Definition: protocol_classic.cc:1404
int shutdown(bool server_shutdown=false) override
Thread is being shut down, disconnect and free resources.
Definition: protocol_classic.cc:3411
ulong m_client_capabilities
Definition: protocol_classic.h:54
virtual void set_read_timeout(ulong read_timeout, bool on_full_packet=false)
Definition: protocol_classic.cc:1351
uint get_compression_level() override
Returns compression level.
Definition: protocol_classic.cc:3070
void end_partial_result_set() override
Finish the result set with EOF packet, as is expected by the client, if there is an error evaluating ...
Definition: protocol_classic.cc:3031
THD * m_thd
Definition: protocol_classic.h:61
bool send_metadata
Definition: protocol_classic.h:65
char * get_compression_algorithm() override
Returns compression algorithm name.
Definition: protocol_classic.cc:3062
bool bad_packet
Definition: protocol_classic.h:88
void set_max_packet_size(ulong max_packet_size)
Definition: protocol_classic.cc:1388
Class used for the old (MySQL 4.0 protocol).
Definition: protocol_classic.h:218
bool store_null() override
Definition: protocol_classic.cc:3404
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3473
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:221
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3633
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3591
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3490
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3499
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3481
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3582
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3642
bool send_parameters(List< Item_param > *parameters, bool) override
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3730
bool store_decimal(const my_decimal *, uint, uint) final
Definition: protocol_classic.cc:3508
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3621
Protocol_text()=default
enum enum_protocol_type type() const override
Definition: protocol_classic.h:237
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3399
Definition: protocol.h:33
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:99
@ PROTOCOL_BINARY
Definition: protocol.h:102
@ PROTOCOL_TEXT
Definition: protocol.h:100
Definition: field.h:4637
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: sql_list.h:698
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:94
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
This file contains the field type.
enum_field_types
Column types for MySQL.
Definition: field_types.h:53
static int flags[50]
Definition: hp_test1.cc:40
A better implementation of the UNIX ctype(3) library.
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
uint32_t uint32
Definition: my_inttypes.h:67
Common #defines and includes for file and socket I/O.
int my_socket
Definition: mysql.h:65
Common definition between mysql server & client.
Time declarations shared between the server and client API: you should not add anything to this heade...
constexpr value_type zerofill
Definition: classic_protocol_constants.h:274
const std::string charset("charset")
Definition: commit_order_queue.h:34
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:910
Definition: buffer.h:45
uchar * net_store_data(uchar *to, const uchar *from, size_t length)
Definition: protocol_classic.cc:1266
bool net_send_error(THD *thd, uint sql_errno, const char *err)
Send a error string to client.
Definition: protocol_classic.cc:613
bool store(Protocol *prot, I_List< i_string > *str_list)
Send a set of strings as one long string with ',' in between.
Definition: protocol_classic.cc:3371
Definition: m_ctype.h:385
Definition: mysql_time.h:82
Definition: mysql_com.h:913
Definition: violite.h:318
unsigned int uint
Definition: uca9-dump.cc:75
Definition: com_data.h:112
Vio Lite.
enum enum_vio_type vio_type(const MYSQL_VIO vio)
enum_vio_type
Definition: violite.h:79
@ NO_VIO_TYPE
Type of the connection is unknown.
Definition: violite.h:83