MySQL  8.0.27
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, 2021, 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 
39 class Item_param;
40 class Send_field;
41 class String;
42 class i_string;
43 class my_decimal;
44 template <class T>
45 class I_List;
46 template <class T>
47 class List;
48 union COM_DATA;
49 class THD;
50 
51 class 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:
87  bool bad_packet;
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;
129  bool send_field_metadata(Send_field *field,
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 */
154  uchar get_error();
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 */
164  ulong get_client_capabilities() override { return m_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  /* Set write timeout */
203  virtual void set_write_timeout(ulong write_timeout);
204 
205  /**
206  * Sets the character set expected by the client. This function is for unit
207  * tests. It should usually be set by calling start_result_metadata().
208  */
210  result_cs = charset;
211  }
212 };
213 
214 /** Class used for the old (MySQL 4.0 protocol). */
215 
217  public:
218  Protocol_text() = default;
219  Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
220  bool store_null() override;
221  bool store_tiny(longlong from, uint32 zerofill) override;
222  bool store_short(longlong from, uint32 zerofill) override;
223  bool store_long(longlong from, uint32 zerofill) override;
224  bool store_longlong(longlong from, bool unsigned_flag,
225  uint32 zerofill) override;
226  bool store_decimal(const my_decimal *, uint, uint) final;
227  bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
228  bool store_double(double from, uint32 decimals, uint32 zerofill) override;
229  bool store_datetime(const MYSQL_TIME &time, uint precision) override;
230  bool store_date(const MYSQL_TIME &time) override;
231  bool store_time(const MYSQL_TIME &time, uint precision) override;
232  void start_row() override;
233  bool send_parameters(List<Item_param> *parameters, bool) override;
234 
235  enum enum_protocol_type type() const override { return PROTOCOL_TEXT; }
236 };
237 
238 class Protocol_binary final : public Protocol_text {
239  private:
241 
242  public:
243  Protocol_binary() = default;
244  Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
245  void start_row() override;
246  bool store_null() override;
247  bool store_tiny(longlong from, uint32 zerofill) override;
248  bool store_short(longlong from, uint32 zerofill) override;
249  bool store_long(longlong from, uint32 zerofill) override;
250  bool store_longlong(longlong from, bool unsigned_flag,
251  uint32 zerofill) override;
252  // Decimals are sent as text, also over the binary protocol.
254  bool store_datetime(const MYSQL_TIME &time, uint precision) override;
255  bool store_date(const MYSQL_TIME &time) override;
256  bool store_time(const MYSQL_TIME &time, uint precision) override;
257  bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
258  bool store_double(double from, uint32 decimals, uint32 zerofill) override;
259 
260  bool start_result_metadata(uint num_cols, uint flags,
261  const CHARSET_INFO *resultcs) override;
263  bool is_sql_prepare) override;
264 
265  enum enum_protocol_type type() const override { return PROTOCOL_BINARY; }
266 };
267 
268 bool net_send_error(THD *thd, uint sql_errno, const char *err);
269 bool net_send_error(NET *net, uint sql_errno, const char *err);
270 uchar *net_store_data(uchar *to, const uchar *from, size_t length);
271 bool store(Protocol *prot, I_List<i_string> *str_list);
272 #endif /* PROTOCOL_CLASSIC_INCLUDED */
Definition: field.h:590
Definition: sql_list.h:805
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4434
Definition: sql_list.h:431
Definition: protocol_classic.h:238
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3798
enum enum_protocol_type type() const override
Definition: protocol_classic.h:265
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3936
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3859
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:3661
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3872
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3916
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3820
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3832
bool store_null() override
Definition: protocol_classic.cc:3788
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3846
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3781
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3808
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:244
uint bit_fields
Definition: protocol_classic.h:240
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3773
Protocol_binary()=default
Definition: protocol_classic.h:51
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_classic.cc:3026
void set_vio(Vio *vio)
Definition: protocol_classic.cc:1402
int read_packet() override
Read packet from client.
Definition: protocol_classic.cc:1412
ulong input_packet_length
Definition: protocol_classic.h:71
bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2835
bool flush() override
Used for the classic protocol.
Definition: protocol_classic.cc:3039
void claim_memory_ownership(bool claim)
Definition: protocol_classic.cc:1372
bool write(const uchar *ptr, size_t len)
Definition: protocol_classic.cc:1382
const CHARSET_INFO * result_cs
Definition: protocol_classic.h:73
NET * get_net()
Definition: protocol_classic.cc:1396
virtual void set_write_timeout(ulong write_timeout)
Definition: protocol_classic.cc:1363
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:3081
uchar * get_raw_packet()
Definition: protocol_classic.h:199
virtual void set_read_timeout(ulong read_timeout)
Definition: protocol_classic.cc:1359
bool end_row() override
Definition: protocol_classic.cc:3364
bool connection_alive() const override
Checks if the protocol's connection with the client is still alive.
Definition: protocol_classic.cc:3398
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
void wipe_net()
Definition: protocol_classic.cc:1388
uchar * input_raw_packet
Definition: protocol_classic.h:72
uchar get_error()
Definition: protocol_classic.cc:1386
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:2992
bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset) override
Sends a single column metadata.
Definition: protocol_classic.cc:3270
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:3041
bool store_field(const Field *field) final
Definition: protocol_classic.cc:1292
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:1323
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:3063
Protocol_classic()
Definition: protocol_classic.h:88
void end_net()
Definition: protocol_classic.cc:1376
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:1305
uint sending_flags
Definition: protocol_classic.h:70
void init(THD *thd_arg)
Definition: protocol_classic.cc:1284
Vio * get_vio()
Definition: protocol_classic.cc:1398
bool end_result_metadata() override
Signals the client that the metadata sending is done.
Definition: protocol_classic.cc:3126
bool init_net(Vio *vio)
Definition: protocol_classic.cc:1368
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:3979
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:2983
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:1349
void set_result_character_set(const CHARSET_INFO *charset)
Sets the character set expected by the client.
Definition: protocol_classic.h:209
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) final
Definition: protocol_classic.cc:3418
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:1410
void set_output_pkt_nr(uint pkt_nr)
Definition: protocol_classic.cc:1404
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:1408
int shutdown(bool server_shutdown=false) override
Thread is being shut down, disconnect and free resources.
Definition: protocol_classic.cc:3414
ulong m_client_capabilities
Definition: protocol_classic.h:53
uint get_compression_level() override
Returns compression level.
Definition: protocol_classic.cc:3073
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:3034
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:3065
bool bad_packet
Definition: protocol_classic.h:87
void set_max_packet_size(ulong max_packet_size)
Definition: protocol_classic.cc:1392
Class used for the old (MySQL 4.0 protocol).
Definition: protocol_classic.h:216
bool store_null() override
Definition: protocol_classic.cc:3407
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3470
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:219
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3630
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3588
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3487
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3496
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3478
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3579
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3639
bool send_parameters(List< Item_param > *parameters, bool) override
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3727
bool store_decimal(const my_decimal *, uint, uint) final
Definition: protocol_classic.cc:3505
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3618
Protocol_text()=default
enum enum_protocol_type type() const override
Definition: protocol_classic.h:235
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3402
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:4583
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:165
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
Definition: sql_list.h:692
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:92
Dialog Client Authentication nullptr
Definition: dialog.cc:352
This file contains the field type.
enum_field_types
Column types for MySQL.
Definition: field_types.h:57
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: my_io_bits.h:59
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:269
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:908
Definition: buffer.h:42
uchar * net_store_data(uchar *to, const uchar *from, size_t length)
Definition: protocol_classic.cc:1274
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:3374
Definition: m_ctype.h:354
Definition: mysql_time.h:81
Definition: mysql_com.h:910
Definition: violite.h:323
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:77
@ NO_VIO_TYPE
Type of the connection is unknown.
Definition: violite.h:81