MySQL 9.1.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, 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 "my_compiler.h" // include before mysql_com.h because of STDCALL
31#include "mysql_com.h" // TODO: fix mysql_com.h dependency on my_compiler.h
32
33#include "field_types.h" // enum_field_types
34#include "my_command.h"
35#include "my_inttypes.h"
36#include "my_io.h"
38#include "mysql_time.h"
39#include "sql/protocol.h" // Protocol
40#include "violite.h"
41
42class Item_param;
43class Send_field;
44class String;
45class i_string;
46class my_decimal;
47template <class T>
48class I_List;
49template <class T>
50class List;
51union COM_DATA;
52class THD;
53
54class Protocol_classic : public Protocol {
55 private:
57 bool parse_packet(union COM_DATA *data, enum_server_command cmd);
58 bool net_store_data_with_conversion(const uchar *from, size_t length,
59 const CHARSET_INFO *fromcs,
60 const CHARSET_INFO *tocs);
61
62 protected:
68#ifndef NDEBUG
70 uint count;
71#endif
77
78 bool send_ok(uint server_status, uint statement_warn_count,
79 ulonglong affected_rows, ulonglong last_insert_id,
80 const char *message) override;
81
82 bool send_eof(uint server_status, uint statement_warn_count) override;
83
84 bool send_error(uint sql_errno, const char *err_msg,
85 const char *sql_state) override;
86 bool store_ps_status(ulong stmt_id, uint column_count, uint param_count,
87 ulong cond_count) override;
88
89 public:
92 : send_metadata(false), input_packet_length(0), bad_packet(true) {}
94 : send_metadata(false),
97 bad_packet(true) {
98 init(thd);
99 }
100 void init(THD *thd_arg);
101 bool store_field(const Field *field) final;
102 bool store_string(const char *from, size_t length,
103 const CHARSET_INFO *cs) final;
104 int read_packet() override;
105 int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
106 /**
107 Parses the passed parameters and creates a command
108
109 @param com_data out parameter
110 @param cmd in parameter
111 @param pkt packet to be parsed
112 @param length size of the packet
113
114 @retval false ok
115 @retval true error
116 */
117 bool create_command(COM_DATA *com_data, enum_server_command cmd, uchar *pkt,
118 size_t length);
119 bool flush() override;
120 void end_partial_result_set() override;
121
122 bool end_row() override;
123 uint get_rw_status() override;
124 bool get_compression() override;
125
126 char *get_compression_algorithm() override;
127 uint get_compression_level() override;
128
129 bool start_result_metadata(uint num_cols, uint flags,
130 const CHARSET_INFO *resultcs) override;
131 bool end_result_metadata() override;
133 const CHARSET_INFO *item_charset) override;
134 void abort_row() override {}
135
136 /**
137 Returns the type of the connection
138
139 @return
140 enum enum_vio_type
141 */
142 enum enum_vio_type connection_type() const override {
143 const Vio *v = get_vio();
144 return v ? vio_type(v) : NO_VIO_TYPE;
145 }
146
148 // NET interaction functions
149 /* Initialize NET */
150 bool init_net(Vio *vio);
151 void claim_memory_ownership(bool claim);
152 /* Deinitialize NET */
153 void end_net();
154 /* Write data to NET buffer */
155 bool write(const uchar *ptr, size_t len);
156 /* Return last error from NET */
158 /* Set max allowed packet size */
159 void set_max_packet_size(ulong max_packet_size);
160 /* Deinitialize VIO */
161 int shutdown(bool server_shutdown = false) override;
162 /* Wipe NET with zeros */
163 void wipe_net();
164 /* Check whether VIO is healhty */
165 bool connection_alive() const override;
166 /* Returns the client capabilities */
168 /* Sets the client capabilities */
169 void set_client_capabilities(ulong client_capabilities) {
170 this->m_client_capabilities = client_capabilities;
171 }
172 /* Adds client capability */
173 void add_client_capability(ulong client_capability) {
174 m_client_capabilities |= client_capability;
175 }
176 /* Removes a client capability*/
177 void remove_client_capability(unsigned long capability) {
178 m_client_capabilities &= ~capability;
179 }
180 /* Returns true if the client has the capability and false otherwise*/
181 bool has_client_capability(unsigned long client_capability) override {
182 return (bool)(m_client_capabilities & client_capability);
183 }
184 // TODO: temporary functions. Will be removed.
185 // DON'T USE IN ANY NEW FEATURES.
186 /* Return NET */
187 NET *get_net();
188 /* return VIO */
189 Vio *get_vio();
190 const Vio *get_vio() const;
191 /* Set VIO */
192 void set_vio(Vio *vio);
193 /* Set packet number */
194 void set_output_pkt_nr(uint pkt_nr);
195 /* Return packet number */
196 uint get_output_pkt_nr();
197 /* Return packet string */
199 /* return packet length */
201 /* Return raw packet buffer */
203 /* Set read timeout */
204 virtual void set_read_timeout(ulong read_timeout,
205 bool on_full_packet = false);
206 /* Set write timeout */
207 virtual void set_write_timeout(ulong write_timeout);
208
209 /**
210 * Sets the character set expected by the client. This function is for unit
211 * tests. It should usually be set by calling start_result_metadata().
212 */
215 }
216};
217
218/** Class used for the old (MySQL 4.0 protocol). */
219
221 public:
222 Protocol_text() = default;
223 Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
224 bool store_null() override;
225 bool store_tiny(longlong from, uint32 zerofill) override;
226 bool store_short(longlong from, uint32 zerofill) override;
227 bool store_long(longlong from, uint32 zerofill) override;
228 bool store_longlong(longlong from, bool unsigned_flag,
229 uint32 zerofill) override;
230 bool store_decimal(const my_decimal *, uint, uint) final;
231 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
232 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
233 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
234 bool store_date(const MYSQL_TIME &time) override;
235 bool store_time(const MYSQL_TIME &time, uint precision) override;
236 void start_row() override;
237 bool send_parameters(List<Item_param> *parameters, bool) override;
238
239 enum enum_protocol_type type() const override { return PROTOCOL_TEXT; }
240};
241
242class Protocol_binary final : public Protocol_text {
243 private:
245
246 public:
247 Protocol_binary() = default;
248 Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
249 void start_row() override;
250 bool store_null() override;
251 bool store_tiny(longlong from, uint32 zerofill) override;
252 bool store_short(longlong from, uint32 zerofill) override;
253 bool store_long(longlong from, uint32 zerofill) override;
254 bool store_longlong(longlong from, bool unsigned_flag,
255 uint32 zerofill) override;
256 // Decimals are sent as text, also over the binary protocol.
258 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
259 bool store_date(const MYSQL_TIME &time) override;
260 bool store_time(const MYSQL_TIME &time, uint precision) override;
261 bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
262 bool store_double(double from, uint32 decimals, uint32 zerofill) override;
263
264 bool start_result_metadata(uint num_cols, uint flags,
265 const CHARSET_INFO *resultcs) override;
266 bool send_parameters(List<Item_param> *parameters,
267 bool is_sql_prepare) override;
268
269 enum enum_protocol_type type() const override { return PROTOCOL_BINARY; }
270};
271
272bool net_send_error(THD *thd, uint sql_errno, const char *err);
273bool net_send_error(NET *net, uint sql_errno, const char *err);
274uchar *net_store_data(uchar *to, const uchar *from, size_t length);
275bool store(Protocol *prot, I_List<i_string> *str_list);
276#endif /* PROTOCOL_CLASSIC_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Definition: field.h:577
Definition: sql_list.h:871
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4789
Definition: sql_list.h:494
Definition: protocol_classic.h:242
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3701
enum enum_protocol_type type() const override
Definition: protocol_classic.h:269
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3839
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3762
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:3563
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3775
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3819
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3723
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3735
bool store_null() override
Definition: protocol_classic.cc:3690
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3749
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3683
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3711
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:248
uint bit_fields
Definition: protocol_classic.h:244
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3675
Protocol_binary()=default
Definition: protocol_classic.h:54
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_classic.cc:2921
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:74
bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2742
bool flush() override
Used for the classic protocol.
Definition: protocol_classic.cc:2934
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:76
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:69
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:2976
bool end_row() override
Definition: protocol_classic.cc:3259
bool connection_alive() const override
Checks if the protocol's connection with the client is still alive.
Definition: protocol_classic.cc:3293
ulong get_client_capabilities() override
Returns the client capabilities stored on the protocol.
Definition: protocol_classic.h:167
void set_client_capabilities(ulong client_capabilities)
Definition: protocol_classic.h:169
uchar * get_raw_packet()
Definition: protocol_classic.h:202
void wipe_net()
Definition: protocol_classic.cc:1384
uchar * input_raw_packet
Definition: protocol_classic.h:75
uchar get_error()
Definition: protocol_classic.cc:1382
String convert
Definition: protocol_classic.h:65
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:2887
bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset) override
Sends a single column metadata.
Definition: protocol_classic.cc:3165
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:2936
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:142
void add_client_capability(ulong client_capability)
Definition: protocol_classic.h:173
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:93
bool get_compression() override
Returns if the protocol is compressed or not.
Definition: protocol_classic.cc:2958
Protocol_classic()
Definition: protocol_classic.h:91
void end_net()
Definition: protocol_classic.cc:1372
String * packet
Definition: protocol_classic.h:64
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:73
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:3021
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:72
my_socket get_socket()
Definition: protocol_classic.cc:3882
void remove_client_capability(unsigned long capability)
Definition: protocol_classic.h:177
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:2878
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:213
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) final
Definition: protocol_classic.cc:3319
ulong get_packet_length()
Definition: protocol_classic.h:200
void abort_row() override
Definition: protocol_classic.h:134
uint field_pos
Definition: protocol_classic.h:66
uint count
Definition: protocol_classic.h:70
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:181
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:3309
ulong m_client_capabilities
Definition: protocol_classic.h:56
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:2968
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:2929
THD * m_thd
Definition: protocol_classic.h:63
bool send_metadata
Definition: protocol_classic.h:67
char * get_compression_algorithm() override
Returns compression algorithm name.
Definition: protocol_classic.cc:2960
bool bad_packet
Definition: protocol_classic.h:90
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:220
bool store_null() override
Definition: protocol_classic.cc:3302
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3372
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:223
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3532
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3490
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3389
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3398
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3380
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3481
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3541
bool send_parameters(List< Item_param > *parameters, bool) override
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3629
bool store_decimal(const my_decimal *, uint, uint) final
Definition: protocol_classic.cc:3407
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3520
Protocol_text()=default
enum enum_protocol_type type() const override
Definition: protocol_classic.h:239
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3297
Definition: protocol.h:33
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:101
@ PROTOCOL_BINARY
Definition: protocol.h:104
@ PROTOCOL_TEXT
Definition: protocol.h:102
Definition: field.h:4712
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: sql_list.h:758
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:96
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:55
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
Header for compiler-dependent features.
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:908
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:3269
Definition: m_ctype.h:421
Definition: mysql_time.h:82
Definition: mysql_com.h:914
Definition: violite.h:320
Definition: com_data.h:104
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