MySQL 8.3.0
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, 2023, 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 "my_compiler.h" // include before mysql_com.h because of STDCALL
30#include "mysql_com.h" // TODO: fix mysql_com.h dependency on my_compiler.h
31
32#include "field_types.h" // enum_field_types
33#include "my_command.h"
34#include "my_inttypes.h"
35#include "my_io.h"
37#include "mysql_time.h"
38#include "sql/protocol.h" // Protocol
39#include "violite.h"
40
41class Item_param;
42class Send_field;
43class String;
44class i_string;
45class my_decimal;
46template <class T>
47class I_List;
48template <class T>
49class List;
50union COM_DATA;
51class THD;
52
53class Protocol_classic : public Protocol {
54 private:
56 bool parse_packet(union COM_DATA *data, enum_server_command cmd);
57 bool net_store_data_with_conversion(const uchar *from, size_t length,
58 const CHARSET_INFO *fromcs,
59 const CHARSET_INFO *tocs);
60
61 protected:
67#ifndef NDEBUG
69 uint count;
70#endif
76
77 bool send_ok(uint server_status, uint statement_warn_count,
78 ulonglong affected_rows, ulonglong last_insert_id,
79 const char *message) override;
80
81 bool send_eof(uint server_status, uint statement_warn_count) override;
82
83 bool send_error(uint sql_errno, const char *err_msg,
84 const char *sql_state) override;
85 bool store_ps_status(ulong stmt_id, uint column_count, uint param_count,
86 ulong cond_count) override;
87
88 public:
91 : send_metadata(false), input_packet_length(0), bad_packet(true) {}
93 : send_metadata(false),
96 bad_packet(true) {
97 init(thd);
98 }
99 void init(THD *thd_arg);
100 bool store_field(const Field *field) final;
101 bool store_string(const char *from, size_t length,
102 const CHARSET_INFO *cs) final;
103 int read_packet() override;
104 int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
105 /**
106 Parses the passed parameters and creates a command
107
108 @param com_data out parameter
109 @param cmd in parameter
110 @param pkt packet to be parsed
111 @param length size of the packet
112
113 @retval false ok
114 @retval true error
115 */
116 bool create_command(COM_DATA *com_data, enum_server_command cmd, uchar *pkt,
117 size_t length);
118 bool flush() override;
119 void end_partial_result_set() override;
120
121 bool end_row() override;
122 uint get_rw_status() override;
123 bool get_compression() override;
124
125 char *get_compression_algorithm() override;
126 uint get_compression_level() override;
127
128 bool start_result_metadata(uint num_cols, uint flags,
129 const CHARSET_INFO *resultcs) override;
130 bool end_result_metadata() override;
132 const CHARSET_INFO *item_charset) override;
133 void abort_row() override {}
134
135 /**
136 Returns the type of the connection
137
138 @return
139 enum enum_vio_type
140 */
141 enum enum_vio_type connection_type() const override {
142 const Vio *v = get_vio();
143 return v ? vio_type(v) : NO_VIO_TYPE;
144 }
145
147 // NET interaction functions
148 /* Initialize NET */
149 bool init_net(Vio *vio);
150 void claim_memory_ownership(bool claim);
151 /* Deinitialize NET */
152 void end_net();
153 /* Write data to NET buffer */
154 bool write(const uchar *ptr, size_t len);
155 /* Return last error from NET */
157 /* Set max allowed packet size */
158 void set_max_packet_size(ulong max_packet_size);
159 /* Deinitialize VIO */
160 int shutdown(bool server_shutdown = false) override;
161 /* Wipe NET with zeros */
162 void wipe_net();
163 /* Check whether VIO is healhty */
164 bool connection_alive() const override;
165 /* Returns the client capabilities */
167 /* Sets the client capabilities */
168 void set_client_capabilities(ulong client_capabilities) {
169 this->m_client_capabilities = client_capabilities;
170 }
171 /* Adds client capability */
172 void add_client_capability(ulong client_capability) {
173 m_client_capabilities |= client_capability;
174 }
175 /* Removes a client capability*/
176 void remove_client_capability(unsigned long capability) {
177 m_client_capabilities &= ~capability;
178 }
179 /* Returns true if the client has the capability and false otherwise*/
180 bool has_client_capability(unsigned long client_capability) override {
181 return (bool)(m_client_capabilities & client_capability);
182 }
183 // TODO: temporary functions. Will be removed.
184 // DON'T USE IN ANY NEW FEATURES.
185 /* Return NET */
186 NET *get_net();
187 /* return VIO */
188 Vio *get_vio();
189 const Vio *get_vio() const;
190 /* Set VIO */
191 void set_vio(Vio *vio);
192 /* Set packet number */
193 void set_output_pkt_nr(uint pkt_nr);
194 /* Return packet number */
195 uint get_output_pkt_nr();
196 /* Return packet string */
198 /* return packet length */
200 /* Return raw packet buffer */
202 /* Set read timeout */
203 virtual void set_read_timeout(ulong read_timeout,
204 bool on_full_packet = false);
205 /* Set write timeout */
206 virtual void set_write_timeout(ulong write_timeout);
207
208 /**
209 * Sets the character set expected by the client. This function is for unit
210 * tests. It should usually be set by calling start_result_metadata().
211 */
214 }
215};
216
217/** Class used for the old (MySQL 4.0 protocol). */
218
220 public:
221 Protocol_text() = default;
222 Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
223 bool store_null() override;
224 bool store_tiny(longlong from, uint32 zerofill) override;
225 bool store_short(longlong from, uint32 zerofill) override;
226 bool store_long(longlong from, uint32 zerofill) override;
227 bool store_longlong(longlong from, bool unsigned_flag,
228 uint32 zerofill) override;
229 bool store_decimal(const my_decimal *, uint, uint) final;
230 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
231 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
232 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
233 bool store_date(const MYSQL_TIME &time) override;
234 bool store_time(const MYSQL_TIME &time, uint precision) override;
235 void start_row() override;
236 bool send_parameters(List<Item_param> *parameters, bool) override;
237
238 enum enum_protocol_type type() const override { return PROTOCOL_TEXT; }
239};
240
241class Protocol_binary final : public Protocol_text {
242 private:
244
245 public:
246 Protocol_binary() = default;
247 Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
248 void start_row() override;
249 bool store_null() override;
250 bool store_tiny(longlong from, uint32 zerofill) override;
251 bool store_short(longlong from, uint32 zerofill) override;
252 bool store_long(longlong from, uint32 zerofill) override;
253 bool store_longlong(longlong from, bool unsigned_flag,
254 uint32 zerofill) override;
255 // Decimals are sent as text, also over the binary protocol.
257 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
258 bool store_date(const MYSQL_TIME &time) override;
259 bool store_time(const MYSQL_TIME &time, uint precision) override;
260 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
261 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
262
263 bool start_result_metadata(uint num_cols, uint flags,
264 const CHARSET_INFO *resultcs) override;
265 bool send_parameters(List<Item_param> *parameters,
266 bool is_sql_prepare) override;
267
268 enum enum_protocol_type type() const override { return PROTOCOL_BINARY; }
269};
270
271bool net_send_error(THD *thd, uint sql_errno, const char *err);
272bool net_send_error(NET *net, uint sql_errno, const char *err);
273uchar *net_store_data(uchar *to, const uchar *from, size_t length);
274bool store(Protocol *prot, I_List<i_string> *str_list);
275#endif /* PROTOCOL_CLASSIC_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
Definition: field.h:574
Definition: sql_list.h:811
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4757
Definition: sql_list.h:434
Definition: protocol_classic.h:241
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3643
enum enum_protocol_type type() const override
Definition: protocol_classic.h:268
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3781
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3704
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:3505
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3717
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3761
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3665
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3677
bool store_null() override
Definition: protocol_classic.cc:3632
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3691
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3625
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3653
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:247
uint bit_fields
Definition: protocol_classic.h:243
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3617
Protocol_binary()=default
Definition: protocol_classic.h:53
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_classic.cc:2864
void set_vio(Vio *vio)
Definition: protocol_classic.cc:1396
int read_packet() override
Read packet from client.
Definition: protocol_classic.cc:1406
ulong input_packet_length
Definition: protocol_classic.h:73
bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2698
bool flush() override
Used for the classic protocol.
Definition: protocol_classic.cc:2877
void claim_memory_ownership(bool claim)
Definition: protocol_classic.cc:1366
bool write(const uchar *ptr, size_t len)
Definition: protocol_classic.cc:1376
const CHARSET_INFO * result_cs
Definition: protocol_classic.h:75
NET * get_net()
Definition: protocol_classic.cc:1390
virtual void set_write_timeout(ulong write_timeout)
Definition: protocol_classic.cc:1357
enum enum_field_types * field_types
Definition: protocol_classic.h:68
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:2919
bool end_row() override
Definition: protocol_classic.cc:3202
bool connection_alive() const override
Checks if the protocol's connection with the client is still alive.
Definition: protocol_classic.cc:3236
ulong get_client_capabilities() override
Returns the client capabilities stored on the protocol.
Definition: protocol_classic.h:166
void set_client_capabilities(ulong client_capabilities)
Definition: protocol_classic.h:168
uchar * get_raw_packet()
Definition: protocol_classic.h:201
void wipe_net()
Definition: protocol_classic.cc:1382
uchar * input_raw_packet
Definition: protocol_classic.h:74
uchar get_error()
Definition: protocol_classic.cc:1380
String convert
Definition: protocol_classic.h:64
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:2830
bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset) override
Sends a single column metadata.
Definition: protocol_classic.cc:3108
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:2879
bool store_field(const Field *field) final
Definition: protocol_classic.cc:1282
enum enum_vio_type connection_type() const override
Returns the type of the connection.
Definition: protocol_classic.h:141
void add_client_capability(ulong client_capability)
Definition: protocol_classic.h:172
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:1313
Protocol_classic(THD *thd)
Definition: protocol_classic.h:92
bool get_compression() override
Returns if the protocol is compressed or not.
Definition: protocol_classic.cc:2901
Protocol_classic()
Definition: protocol_classic.h:90
void end_net()
Definition: protocol_classic.cc:1370
String * packet
Definition: protocol_classic.h:63
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:1295
uint sending_flags
Definition: protocol_classic.h:72
void init(THD *thd_arg)
Definition: protocol_classic.cc:1274
Vio * get_vio()
Definition: protocol_classic.cc:1392
bool end_result_metadata() override
Signals the client that the metadata sending is done.
Definition: protocol_classic.cc:2964
bool init_net(Vio *vio)
Definition: protocol_classic.cc:1362
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:548
uint field_count
Definition: protocol_classic.h:71
my_socket get_socket()
Definition: protocol_classic.cc:3824
void remove_client_capability(unsigned long capability)
Definition: protocol_classic.h:176
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:2821
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:1339
void set_result_character_set(const CHARSET_INFO *charset)
Sets the character set expected by the client.
Definition: protocol_classic.h:212
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) final
Definition: protocol_classic.cc:3262
ulong get_packet_length()
Definition: protocol_classic.h:199
void abort_row() override
Definition: protocol_classic.h:133
uint field_pos
Definition: protocol_classic.h:65
uint count
Definition: protocol_classic.h:69
String * get_output_packet()
Definition: protocol_classic.cc:1404
void set_output_pkt_nr(uint pkt_nr)
Definition: protocol_classic.cc:1398
bool has_client_capability(unsigned long client_capability) override
Checks if the client capabilities include the one specified as parameter.
Definition: protocol_classic.h:180
uint get_output_pkt_nr()
Definition: protocol_classic.cc:1402
int shutdown(bool server_shutdown=false) override
Thread is being shut down, disconnect and free resources.
Definition: protocol_classic.cc:3252
ulong m_client_capabilities
Definition: protocol_classic.h:55
virtual void set_read_timeout(ulong read_timeout, bool on_full_packet=false)
Definition: protocol_classic.cc:1349
uint get_compression_level() override
Returns compression level.
Definition: protocol_classic.cc:2911
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:2872
THD * m_thd
Definition: protocol_classic.h:62
bool send_metadata
Definition: protocol_classic.h:66
char * get_compression_algorithm() override
Returns compression algorithm name.
Definition: protocol_classic.cc:2903
bool bad_packet
Definition: protocol_classic.h:89
void set_max_packet_size(ulong max_packet_size)
Definition: protocol_classic.cc:1386
Class used for the old (MySQL 4.0 protocol).
Definition: protocol_classic.h:219
bool store_null() override
Definition: protocol_classic.cc:3245
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3314
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:222
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3474
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3432
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3331
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3340
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3322
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3423
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3483
bool send_parameters(List< Item_param > *parameters, bool) override
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3571
bool store_decimal(const my_decimal *, uint, uint) final
Definition: protocol_classic.cc:3349
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3462
Protocol_text()=default
enum enum_protocol_type type() const override
Definition: protocol_classic.h:238
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3240
Definition: protocol.h:32
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:100
@ PROTOCOL_BINARY
Definition: protocol.h:103
@ PROTOCOL_TEXT
Definition: protocol.h:101
Definition: field.h:4638
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:35
Definition: sql_list.h:698
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:94
This file contains the field type.
enum_field_types
Column types for MySQL Note: Keep include/mysql/components/services/bits/stored_program_bits....
Definition: field_types.h:54
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
Header for compiler-dependent features.
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:273
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:926
Definition: buffer.h:44
uchar * net_store_data(uchar *to, const uchar *from, size_t length)
Definition: protocol_classic.cc:1264
bool net_send_error(THD *thd, uint sql_errno, const char *err)
Send a error string to client.
Definition: protocol_classic.cc:611
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:3212
Definition: m_ctype.h:422
Definition: mysql_time.h:81
Definition: mysql_com.h:919
Definition: violite.h:319
Definition: com_data.h:96
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