MySQL 8.0.32
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, 2022, 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 also distributed 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 included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <stddef.h>
27#include <sys/types.h>
28
29#include "field_types.h" // enum_field_types
30#include "m_ctype.h"
31#include "my_command.h"
32#include "my_inttypes.h"
33#include "my_io.h"
34#include "mysql_com.h"
35#include "mysql_time.h"
36#include "sql/protocol.h" // Protocol
37#include "violite.h"
38
39class Item_param;
40class Send_field;
41class String;
42class i_string;
43class my_decimal;
44template <class T>
45class I_List;
46template <class T>
47class List;
48union COM_DATA;
49class THD;
50
51class Protocol_classic : public Protocol {
52 private:
54 bool parse_packet(union COM_DATA *data, enum_server_command cmd);
55 bool net_store_data_with_conversion(const uchar *from, size_t length,
56 const CHARSET_INFO *fromcs,
57 const CHARSET_INFO *tocs);
58
59 protected:
65#ifndef NDEBUG
68#endif
74
75 bool send_ok(uint server_status, uint statement_warn_count,
76 ulonglong affected_rows, ulonglong last_insert_id,
77 const char *message) override;
78
79 bool send_eof(uint server_status, uint statement_warn_count) override;
80
81 bool send_error(uint sql_errno, const char *err_msg,
82 const char *sql_state) override;
83 bool store_ps_status(ulong stmt_id, uint column_count, uint param_count,
84 ulong cond_count) override;
85
86 public:
89 : send_metadata(false), input_packet_length(0), bad_packet(true) {}
91 : send_metadata(false),
94 bad_packet(true) {
95 init(thd);
96 }
97 void init(THD *thd_arg);
98 bool store_field(const Field *field) final;
99 bool store_string(const char *from, size_t length,
100 const CHARSET_INFO *cs) final;
101 int read_packet() override;
102 int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
103 /**
104 Parses the passed parameters and creates a command
105
106 @param com_data out parameter
107 @param cmd in parameter
108 @param pkt packet to be parsed
109 @param length size of the packet
110
111 @retval false ok
112 @retval true error
113 */
114 bool create_command(COM_DATA *com_data, enum_server_command cmd, uchar *pkt,
115 size_t length);
116 bool flush() override;
117 void end_partial_result_set() override;
118
119 bool end_row() override;
120 uint get_rw_status() override;
121 bool get_compression() override;
122
123 char *get_compression_algorithm() override;
124 uint get_compression_level() override;
125
126 bool start_result_metadata(uint num_cols, uint flags,
127 const CHARSET_INFO *resultcs) override;
128 bool end_result_metadata() override;
130 const CHARSET_INFO *item_charset) override;
131 void abort_row() override {}
132
133 /**
134 Returns the type of the connection
135
136 @return
137 enum enum_vio_type
138 */
139 enum enum_vio_type connection_type() const override {
140 const Vio *v = get_vio();
141 return v ? vio_type(v) : NO_VIO_TYPE;
142 }
143
145 // NET interaction functions
146 /* Initialize NET */
147 bool init_net(Vio *vio);
148 void claim_memory_ownership(bool claim);
149 /* Deinitialize NET */
150 void end_net();
151 /* Write data to NET buffer */
152 bool write(const uchar *ptr, size_t len);
153 /* Return last error from NET */
155 /* Set max allowed packet size */
156 void set_max_packet_size(ulong max_packet_size);
157 /* Deinitialize VIO */
158 int shutdown(bool server_shutdown = false) override;
159 /* Wipe NET with zeros */
160 void wipe_net();
161 /* Check whether VIO is healhty */
162 bool connection_alive() const override;
163 /* Returns the client capabilities */
165 /* Sets the client capabilities */
166 void set_client_capabilities(ulong client_capabilities) {
167 this->m_client_capabilities = client_capabilities;
168 }
169 /* Adds client capability */
170 void add_client_capability(ulong client_capability) {
171 m_client_capabilities |= client_capability;
172 }
173 /* Removes a client capability*/
174 void remove_client_capability(unsigned long capability) {
175 m_client_capabilities &= ~capability;
176 }
177 /* Returns true if the client has the capability and false otherwise*/
178 bool has_client_capability(unsigned long client_capability) override {
179 return (bool)(m_client_capabilities & client_capability);
180 }
181 // TODO: temporary functions. Will be removed.
182 // DON'T USE IN ANY NEW FEATURES.
183 /* Return NET */
184 NET *get_net();
185 /* return VIO */
186 Vio *get_vio();
187 const Vio *get_vio() const;
188 /* Set VIO */
189 void set_vio(Vio *vio);
190 /* Set packet number */
191 void set_output_pkt_nr(uint pkt_nr);
192 /* Return packet number */
194 /* Return packet string */
196 /* return packet length */
198 /* Return raw packet buffer */
200 /* Set read timeout */
201 virtual void set_read_timeout(ulong read_timeout,
202 bool on_full_packet = false);
203 /* Set write timeout */
204 virtual void set_write_timeout(ulong write_timeout);
205
206 /**
207 * Sets the character set expected by the client. This function is for unit
208 * tests. It should usually be set by calling start_result_metadata().
209 */
212 }
213};
214
215/** Class used for the old (MySQL 4.0 protocol). */
216
218 public:
219 Protocol_text() = default;
220 Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
221 bool store_null() override;
222 bool store_tiny(longlong from, uint32 zerofill) override;
223 bool store_short(longlong from, uint32 zerofill) override;
224 bool store_long(longlong from, uint32 zerofill) override;
225 bool store_longlong(longlong from, bool unsigned_flag,
226 uint32 zerofill) override;
227 bool store_decimal(const my_decimal *, uint, uint) final;
228 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
229 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
230 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
231 bool store_date(const MYSQL_TIME &time) override;
232 bool store_time(const MYSQL_TIME &time, uint precision) override;
233 void start_row() override;
234 bool send_parameters(List<Item_param> *parameters, bool) override;
235
236 enum enum_protocol_type type() const override { return PROTOCOL_TEXT; }
237};
238
239class Protocol_binary final : public Protocol_text {
240 private:
242
243 public:
244 Protocol_binary() = default;
245 Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
246 void start_row() override;
247 bool store_null() override;
248 bool store_tiny(longlong from, uint32 zerofill) override;
249 bool store_short(longlong from, uint32 zerofill) override;
250 bool store_long(longlong from, uint32 zerofill) override;
251 bool store_longlong(longlong from, bool unsigned_flag,
252 uint32 zerofill) override;
253 // Decimals are sent as text, also over the binary protocol.
255 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
256 bool store_date(const MYSQL_TIME &time) override;
257 bool store_time(const MYSQL_TIME &time, uint precision) override;
258 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
259 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
260
261 bool start_result_metadata(uint num_cols, uint flags,
262 const CHARSET_INFO *resultcs) override;
263 bool send_parameters(List<Item_param> *parameters,
264 bool is_sql_prepare) override;
265
266 enum enum_protocol_type type() const override { return PROTOCOL_BINARY; }
267};
268
269bool net_send_error(THD *thd, uint sql_errno, const char *err);
270bool net_send_error(NET *net, uint sql_errno, const char *err);
271uchar *net_store_data(uchar *to, const uchar *from, size_t length);
272bool store(Protocol *prot, I_List<i_string> *str_list);
273#endif /* PROTOCOL_CLASSIC_INCLUDED */
Definition: field.h:574
Definition: sql_list.h:810
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4512
Definition: sql_list.h:433
Definition: protocol_classic.h:239
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3794
enum enum_protocol_type type() const override
Definition: protocol_classic.h:266
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3932
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3855
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:3657
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3868
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3912
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3816
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3828
bool store_null() override
Definition: protocol_classic.cc:3784
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3842
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3777
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3804
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:245
uint bit_fields
Definition: protocol_classic.h:241
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3769
Protocol_binary()=default
Definition: protocol_classic.h:51
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_classic.cc:3022
void set_vio(Vio *vio)
Definition: protocol_classic.cc:1397
int read_packet() override
Read packet from client.
Definition: protocol_classic.cc:1407
ulong input_packet_length
Definition: protocol_classic.h:71
bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2831
bool flush() override
Used for the classic protocol.
Definition: protocol_classic.cc:3035
void claim_memory_ownership(bool claim)
Definition: protocol_classic.cc:1367
bool write(const uchar *ptr, size_t len)
Definition: protocol_classic.cc:1377
const CHARSET_INFO * result_cs
Definition: protocol_classic.h:73
NET * get_net()
Definition: protocol_classic.cc:1391
virtual void set_write_timeout(ulong write_timeout)
Definition: protocol_classic.cc:1358
enum enum_field_types * field_types
Definition: protocol_classic.h:66
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3077
bool end_row() override
Definition: protocol_classic.cc:3360
bool connection_alive() const override
Checks if the protocol's connection with the client is still alive.
Definition: protocol_classic.cc:3394
ulong get_client_capabilities() override
Returns the client capabilities stored on the protocol.
Definition: protocol_classic.h:164
void set_client_capabilities(ulong client_capabilities)
Definition: protocol_classic.h:166
uchar * get_raw_packet()
Definition: protocol_classic.h:199
void wipe_net()
Definition: protocol_classic.cc:1383
uchar * input_raw_packet
Definition: protocol_classic.h:72
uchar get_error()
Definition: protocol_classic.cc:1381
String convert
Definition: protocol_classic.h:62
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:2988
bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset) override
Sends a single column metadata.
Definition: protocol_classic.cc:3266
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:3037
bool store_field(const Field *field) final
Definition: protocol_classic.cc:1283
enum enum_vio_type connection_type() const override
Returns the type of the connection.
Definition: protocol_classic.h:139
void add_client_capability(ulong client_capability)
Definition: protocol_classic.h:170
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:1314
Protocol_classic(THD *thd)
Definition: protocol_classic.h:90
bool get_compression() override
Returns if the protocol is compressed or not.
Definition: protocol_classic.cc:3059
Protocol_classic()
Definition: protocol_classic.h:88
void end_net()
Definition: protocol_classic.cc:1371
String * packet
Definition: protocol_classic.h:61
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:1296
uint sending_flags
Definition: protocol_classic.h:70
void init(THD *thd_arg)
Definition: protocol_classic.cc:1275
Vio * get_vio()
Definition: protocol_classic.cc:1393
bool end_result_metadata() override
Signals the client that the metadata sending is done.
Definition: protocol_classic.cc:3122
bool init_net(Vio *vio)
Definition: protocol_classic.cc:1363
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:549
uint field_count
Definition: protocol_classic.h:69
my_socket get_socket()
Definition: protocol_classic.cc:3975
void remove_client_capability(unsigned long capability)
Definition: protocol_classic.h:174
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:2979
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:1340
void set_result_character_set(const CHARSET_INFO *charset)
Sets the character set expected by the client.
Definition: protocol_classic.h:210
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) final
Definition: protocol_classic.cc:3414
ulong get_packet_length()
Definition: protocol_classic.h:197
void abort_row() override
Definition: protocol_classic.h:131
uint field_pos
Definition: protocol_classic.h:63
uint count
Definition: protocol_classic.h:67
String * get_output_packet()
Definition: protocol_classic.cc:1405
void set_output_pkt_nr(uint pkt_nr)
Definition: protocol_classic.cc:1399
bool has_client_capability(unsigned long client_capability) override
Checks if the client capabilities include the one specified as parameter.
Definition: protocol_classic.h:178
uint get_output_pkt_nr()
Definition: protocol_classic.cc:1403
int shutdown(bool server_shutdown=false) override
Thread is being shut down, disconnect and free resources.
Definition: protocol_classic.cc:3410
ulong m_client_capabilities
Definition: protocol_classic.h:53
virtual void set_read_timeout(ulong read_timeout, bool on_full_packet=false)
Definition: protocol_classic.cc:1350
uint get_compression_level() override
Returns compression level.
Definition: protocol_classic.cc:3069
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:3030
THD * m_thd
Definition: protocol_classic.h:60
bool send_metadata
Definition: protocol_classic.h:64
char * get_compression_algorithm() override
Returns compression algorithm name.
Definition: protocol_classic.cc:3061
bool bad_packet
Definition: protocol_classic.h:87
void set_max_packet_size(ulong max_packet_size)
Definition: protocol_classic.cc:1387
Class used for the old (MySQL 4.0 protocol).
Definition: protocol_classic.h:217
bool store_null() override
Definition: protocol_classic.cc:3403
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3466
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:220
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3626
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3584
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3483
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3492
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3474
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3575
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3635
bool send_parameters(List< Item_param > *parameters, bool) override
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3723
bool store_decimal(const my_decimal *, uint, uint) final
Definition: protocol_classic.cc:3501
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3614
Protocol_text()=default
enum enum_protocol_type type() const override
Definition: protocol_classic.h:236
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3398
Definition: protocol.h:32
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:98
@ PROTOCOL_BINARY
Definition: protocol.h:101
@ PROTOCOL_TEXT
Definition: protocol.h:99
Definition: field.h:4555
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Definition: sql_list.h:697
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:93
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
This file contains the field type.
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
static int flags[50]
Definition: hp_test1.cc:39
A better implementation of the UNIX ctype(3) library.
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
long long int longlong
Definition: my_inttypes.h:54
uint32_t uint32
Definition: my_inttypes.h:66
Common #defines and includes for file and socket I/O.
int my_socket
Definition: mysql.h:64
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:271
const std::string charset("charset")
Definition: commit_order_queue.h:33
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:75
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:909
Definition: buffer.h:43
uchar * net_store_data(uchar *to, const uchar *from, size_t length)
Definition: protocol_classic.cc:1265
bool net_send_error(THD *thd, uint sql_errno, const char *err)
Send a error string to client.
Definition: protocol_classic.cc:612
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:3370
Definition: m_ctype.h:382
Definition: mysql_time.h:81
Definition: mysql_com.h:912
Definition: violite.h:317
unsigned int uint
Definition: uca-dump.cc:29
Definition: com_data.h:111
Vio Lite.
enum enum_vio_type vio_type(const MYSQL_VIO vio)
enum_vio_type
Definition: violite.h:78
@ NO_VIO_TYPE
Type of the connection is unknown.
Definition: violite.h:82