MySQL  8.0.18
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, 2019, Oracle and/or its affiliates. All rights reserved.
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 DBUG_OFF
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;
93  input_raw_packet(nullptr),
94  bad_packet(true) {
95  init(thd);
96  }
97  void init(THD *thd_arg);
98  bool store_field(const Field *field) final override;
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  @return
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;
130  bool send_field_metadata(Send_field *field,
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();
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 */
155  uchar get_error();
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  /* 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  */
210  void set_result_character_set(const CHARSET_INFO *charset) {
211  result_cs = charset;
212  }
213 };
214 
215 /** Class used for the old (MySQL 4.0 protocol). */
216 
218  public:
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 
240  private:
242 
243  public:
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 
269 bool net_send_error(THD *thd, uint sql_errno, const char *err);
270 bool net_send_error(NET *net, uint sql_errno, const char *err);
271 uchar *net_store_data(uchar *to, const uchar *from, size_t length);
272 bool store(Protocol *prot, I_List<i_string> *str_list);
273 #endif /* PROTOCOL_CLASSIC_INCLUDED */
void add_client_capability(ulong client_capability)
Definition: protocol_classic.h:171
unsigned long long int ulonglong
Definition: my_inttypes.h:55
Definition: field.h:4920
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3623
This file contains the field type.
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3301
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:98
unsigned char uchar
Definition: my_inttypes.h:51
int read_packet() override
Read packet from client.
Definition: protocol_classic.cc:1408
bool connection_alive() const override
Checks if the protocol&#39;s connection with the client is still alive.
Definition: protocol_classic.cc:3216
void end_net()
Definition: protocol_classic.cc:1372
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3286
ulong get_client_capabilities() override
Returns the client capabilities stored on the protocol.
Definition: protocol_classic.h:165
bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset) override
Sends a single column metadata.
Definition: protocol_classic.cc:3089
Type of the connection is unknown.
Definition: violite.h:79
uchar * get_raw_packet()
Definition: protocol_classic.h:200
Some integer typedefs for easier portability.
void abort_row() override
Definition: protocol_classic.h:132
virtual void set_read_timeout(ulong read_timeout)
Definition: protocol_classic.cc:1355
bool store_decimal(const my_decimal *, uint, uint) final
Definition: protocol_classic.cc:3315
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3651
Definition: protocol_classic.h:51
ulong m_client_capabilities
Definition: protocol_classic.h:53
void set_result_character_set(const CHARSET_INFO *charset)
Sets the character set expected by the client.
Definition: protocol_classic.h:210
Definition: field.h:700
String convert
Definition: protocol_classic.h:62
uint count
Definition: protocol_classic.h:67
String * get_output_packet()
Definition: protocol_classic.cc:1406
uint bit_fields
Definition: protocol_classic.h:241
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3220
Definition: protocol_classic.h:239
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:245
uint get_compression_level() override
Returns compression level.
Definition: protocol_classic.cc:2892
Definition: com_data.h:106
char * get_compression_algorithm() override
Returns compression algorithm name.
Definition: protocol_classic.cc:2884
bool send_parameters(List< Item_param > *parameters, bool) override
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3501
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3392
Common definition between mysql server & client.
Protocol_text()
Definition: protocol_classic.h:219
Definition: protocol.h:99
Placeholder (&#39;?&#39;) of prepared statement.
Definition: item.h:3659
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:2853
enum enum_protocol_type type() const override
Definition: protocol_classic.h:236
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3402
const CHARSET_INFO * result_cs
Definition: protocol_classic.h:73
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:220
Protocol_classic(THD *thd)
Definition: protocol_classic.h:90
bool bad_packet
Definition: protocol_classic.h:87
Vio * get_vio()
Definition: protocol_classic.cc:1394
void remove_client_capability(unsigned long capability)
Definition: protocol_classic.h:175
void set_max_packet_size(ulong max_packet_size)
Definition: protocol_classic.cc:1388
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_classic.cc:2845
enum enum_vio_type vio_type(const MYSQL_VIO vio)
bool store_null() override
Definition: protocol_classic.cc:3225
void set_vio(Vio *vio)
Definition: protocol_classic.cc:1398
Definition: aggregate_check.h:523
bool store_date(const MYSQL_TIME &time) override
Definition: protocol_classic.cc:3695
void wipe_net()
Definition: protocol_classic.cc:1384
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
void start_row() override
Result set sending functions.
Definition: protocol_classic.cc:3555
bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill) override
Definition: protocol_classic.cc:3609
uchar * net_store_data(uchar *to, const uchar *from, size_t length)
Definition: protocol_classic.cc:1270
my_decimal class limits &#39;decimal_t&#39; type to what we need in MySQL.
Definition: my_decimal.h:91
void set_client_capabilities(ulong client_capabilities)
Definition: protocol_classic.h:167
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) final
Definition: protocol_classic.cc:3236
int shutdown(bool server_shutdown=false) override
Thread is being shut down, disconnect and free resources.
Definition: protocol_classic.cc:3232
uchar * input_raw_packet
Definition: protocol_classic.h:72
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
void init(THD *thd_arg)
Definition: protocol_classic.cc:1280
my_socket get_socket()
Definition: protocol_classic.cc:3760
enum enum_field_types * field_types
Definition: protocol_classic.h:66
uint field_count
Definition: protocol_classic.h:69
bool store_long(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3596
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
unsigned int uint
Definition: uca-dump.cc:29
#define final(a, b, c)
Definition: hash.c:109
#define true
Definition: config_static.h:44
long long int longlong
Definition: my_inttypes.h:54
enum_vio_type
Definition: violite.h:75
enum enum_vio_type connection_type() const override
Returns the type of the connection.
Definition: protocol_classic.h:140
uint32_t uint32
Definition: my_inttypes.h:66
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:1319
void claim_memory_ownership()
Definition: protocol_classic.cc:1368
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Definition: violite.h:324
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3259
String * packet
Definition: protocol_classic.h:61
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:3434
uchar get_error()
Definition: protocol_classic.cc:1382
Definition: m_ctype.h:359
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3547
Protocol_classic()
Definition: protocol_classic.h:88
int my_socket
Definition: my_io_bits.h:59
ulong input_packet_length
Definition: protocol_classic.h:71
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2598
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3716
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3637
uint field_pos
Definition: protocol_classic.h:63
bool store_ps_status(ulong stmt_id, uint column_count, uint param_count, ulong cond_count) override
Sends prepared statement&#39;s id and metadata to the client after prepare.
Definition: protocol_classic.cc:2860
bool get_compression() override
Returns if the protocol is compressed or not.
Definition: protocol_classic.cc:2882
bool store_tiny(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3572
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:1345
Definition: mysql_time.h:64
uint get_packet_length()
Definition: protocol_classic.h:198
Definition: protocol.h:34
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3583
bool store_short(longlong from, uint32 zerofill) override
Definition: protocol_classic.cc:3272
char sql_state[64]
Definition: test_sql_9_sessions.cc:297
bool store(Protocol *prot, I_List< i_string > *str_list)
Send a set of strings as one long string with &#39;,&#39; in between.
Definition: protocol_classic.cc:3192
enum enum_protocol_type type() const override
Definition: protocol_classic.h:266
bool flush() override
Used for the classic protocol.
Definition: protocol_classic.cc:2858
bool write(const uchar *ptr, size_t len)
Definition: protocol_classic.cc:1378
THD * m_thd
Definition: protocol_classic.h:60
Common #defines and includes for file and socket I/O.
Definition: protocol_classic.h:45
Vio Lite.
Time declarations shared between the server and client API: you should not add anything to this heade...
bool net_send_error(THD *thd, uint sql_errno, const char *err)
Send a error string to client.
Definition: protocol_classic.cc:611
bool end_result_metadata() override
Signals the client that the metadata sending is done.
Definition: protocol_classic.cc:2945
Protocol_binary()
Definition: protocol_classic.h:244
bool store_float(float nr, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3367
Class used for the old (MySQL 4.0 protocol).
Definition: protocol_classic.h:217
void set_output_pkt_nr(uint pkt_nr)
Definition: protocol_classic.cc:1400
A better implementation of the UNIX ctype(3) library.
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:2811
Definition: protocol.h:101
COM_DATA cmd
Definition: test_session_info.cc:95
virtual void set_write_timeout(ulong write_timeout)
Definition: protocol_classic.cc:1359
bool store_field(const Field *field) final override
Definition: protocol_classic.cc:1288
uint get_output_pkt_nr()
Definition: protocol_classic.cc:1404
bool store_time(const MYSQL_TIME &time, uint precision) override
Definition: protocol_classic.cc:3412
bool store_null() override
Definition: protocol_classic.cc:3562
bool end_row() override
Definition: protocol_classic.cc:3181
static int flags[50]
Definition: hp_test1.cc:39
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_classic.cc:2900
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:1301
uint sending_flags
Definition: protocol_classic.h:70
bool init_net(Vio *vio)
Definition: protocol_classic.cc:1364
unsigned long ulong
Definition: my_inttypes.h:48
#define false
Definition: config_static.h:43
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
Definition: mysql_com.h:858
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:2802
bool store_double(double from, uint32 decimals, uint32 zerofill) override
Definition: protocol_classic.cc:3376
bool send_metadata
Definition: protocol_classic.h:64
Definition: sql_list.h:689
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
NET * get_net()
Definition: protocol_classic.cc:1392