MySQL  8.0.17
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 Proto_field;
41 class Send_field;
42 class String;
43 class i_string;
44 class my_decimal;
45 template <class T>
46 class I_List;
47 template <class T>
48 class List;
49 union COM_DATA;
50 class THD;
51 
52 class Protocol_classic : public Protocol {
53  private:
55  virtual bool parse_packet(union COM_DATA *data, enum_server_command cmd);
56 
57  protected:
63 #ifndef DBUG_OFF
66 #endif
72  bool net_store_data(const uchar *from, size_t length);
73  virtual bool net_store_data(const uchar *from, size_t length,
74  const CHARSET_INFO *fromcs,
75  const CHARSET_INFO *tocs);
76  bool store_string_aux(const char *from, size_t length,
77  const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
78 
79  virtual bool send_ok(uint server_status, uint statement_warn_count,
80  ulonglong affected_rows, ulonglong last_insert_id,
81  const char *message);
82 
83  virtual bool send_eof(uint server_status, uint statement_warn_count);
84 
85  virtual bool send_error(uint sql_errno, const char *err_msg,
86  const char *sql_state);
87  virtual bool store_ps_status(ulong stmt_id, uint column_count,
88  uint param_count, ulong cond_count);
89 
90  public:
91  bool bad_packet;
97  input_raw_packet(nullptr),
98  bad_packet(true) {
99  init(thd);
100  }
101  virtual ~Protocol_classic() {}
102  void init(THD *thd_arg);
103  virtual int read_packet();
104  virtual int get_command(COM_DATA *com_data, enum_server_command *cmd);
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  @return
114  @retval false ok
115  @retval true error
116  */
117  virtual bool create_command(COM_DATA *com_data, enum_server_command cmd,
118  uchar *pkt, size_t length);
119  virtual bool flush();
120  virtual void end_partial_result_set();
121 
122  virtual void start_row() = 0;
123  virtual bool end_row();
124  virtual uint get_rw_status();
125  virtual bool get_compression();
126 
127  virtual bool start_result_metadata(uint num_cols, uint flags,
128  const CHARSET_INFO *resultcs);
129  virtual bool end_result_metadata();
130  virtual bool send_field_metadata(Send_field *field,
131  const CHARSET_INFO *item_charset);
132  virtual void abort_row() {}
133  virtual enum enum_protocol_type type() const = 0;
134 
135  /**
136  Returns the type of the connection
137 
138  @return
139  enum enum_vio_type
140  */
141  virtual enum enum_vio_type connection_type() const {
142  const Vio *v = get_vio();
143  return v ? vio_type(v) : NO_VIO_TYPE;
144  }
145 
146  virtual my_socket get_socket();
147  // NET interaction functions
148  /* Initialize NET */
149  bool init_net(Vio *vio);
150  void claim_memory_ownership();
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 */
156  uchar get_error();
157  /* Set max allowed packet size */
158  void set_max_packet_size(ulong max_packet_size);
159  /* Deinitialize VIO */
160  virtual int shutdown(bool server_shutdown = false);
161  /* Wipe NET with zeros */
162  void wipe_net();
163  /* Check whether VIO is healhty */
164  virtual bool connection_alive() const;
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  virtual bool has_client_capability(unsigned long client_capability) {
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 */
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  /* Set write timeout */
205  virtual void set_write_timeout(ulong write_timeout);
206 };
207 
208 /** Class used for the old (MySQL 4.0 protocol). */
209 
211  public:
213  Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
214  virtual bool store_null();
215  virtual bool store_tiny(longlong from);
216  virtual bool store_short(longlong from);
217  virtual bool store_long(longlong from);
218  virtual bool store_longlong(longlong from, bool unsigned_flag);
219  virtual bool store_decimal(const my_decimal *, uint, uint);
220  virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs) {
221  return store(from, length, cs, result_cs);
222  }
223  virtual bool store(float nr, uint32 decimals, String *buffer);
224  virtual bool store(double from, uint32 decimals, String *buffer);
225  virtual bool store(MYSQL_TIME *time, uint precision);
226  virtual bool store_date(MYSQL_TIME *time);
227  virtual bool store_time(MYSQL_TIME *time, uint precision);
228  virtual bool store(Proto_field *field);
229  virtual void start_row();
230  virtual bool send_parameters(List<Item_param> *parameters, bool);
231 
232  virtual enum enum_protocol_type type() const { return PROTOCOL_TEXT; }
233 
234  protected:
235  virtual bool store(const char *from, size_t length,
236  const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
237 };
238 
240  private:
242 
243  public:
245  Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
246  virtual void start_row();
247  virtual bool store_null();
248  virtual bool store_tiny(longlong from);
249  virtual bool store_short(longlong from);
250  virtual bool store_long(longlong from);
251  virtual bool store_longlong(longlong from, bool unsigned_flag);
252  virtual bool store_decimal(const my_decimal *, uint, uint);
253  virtual bool store(MYSQL_TIME *time, uint precision);
254  virtual bool store_date(MYSQL_TIME *time);
255  virtual bool store_time(MYSQL_TIME *time, uint precision);
256  virtual bool store(float nr, uint32 decimals, String *buffer);
257  virtual bool store(double from, uint32 decimals, String *buffer);
258  virtual bool store(Proto_field *field);
259  virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs) {
260  return store(from, length, cs, result_cs);
261  }
262 
263  virtual bool start_result_metadata(uint num_cols, uint flags,
264  const CHARSET_INFO *resultcs);
265  virtual bool send_parameters(List<Item_param> *parameters,
266  bool is_sql_prepare);
267 
268  virtual enum enum_protocol_type type() const { return PROTOCOL_BINARY; }
269 
270  protected:
271  virtual bool store(const char *from, size_t length,
272  const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs);
273 };
274 
275 bool net_send_error(THD *thd, uint sql_errno, const char *err);
276 bool net_send_error(NET *net, uint sql_errno, const char *err);
277 uchar *net_store_data(uchar *to, const uchar *from, size_t length);
278 uchar *net_store_data(uchar *to, int32 from);
279 uchar *net_store_data(uchar *to, longlong from);
280 bool store(Protocol *prot, I_List<i_string> *str_list);
281 bool net_send_ok(THD *, uint, uint, ulonglong, ulonglong, const char *, bool);
282 bool net_send_eof(THD *thd, uint server_status, uint statement_warn_count);
283 bool net_send_error_packet(THD *, uint, const char *, const char *);
284 #endif /* PROTOCOL_CLASSIC_INCLUDED */
void add_client_capability(ulong client_capability)
Definition: protocol_classic.h:172
virtual bool store_decimal(const my_decimal *, uint, uint)
Definition: protocol_classic.cc:3282
unsigned long long int ulonglong
Definition: my_inttypes.h:74
Definition: field.h:4906
This file contains the field type.
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:111
unsigned char uchar
Definition: my_inttypes.h:51
virtual bool store_tiny(longlong from)
Definition: protocol_classic.cc:3530
void end_net()
Definition: protocol_classic.cc:1337
virtual void end_partial_result_set()
Finish the result set with EOF packet, as is expected by the client, if there is an error evaluating ...
Definition: protocol_classic.cc:2818
virtual bool store_time(MYSQL_TIME *time, uint precision)
Definition: protocol_classic.cc:3681
virtual enum enum_protocol_type type() const =0
char buffer[STRING_BUFFER]
Definition: test_sql_9_sessions.cc:57
Type of the connection is unknown.
Definition: violite.h:79
virtual bool store_null()
Definition: protocol_classic.cc:3520
uchar * get_raw_packet()
Definition: protocol_classic.h:201
Some integer typedefs for easier portability.
virtual void set_read_timeout(ulong read_timeout)
Definition: protocol_classic.cc:1320
virtual bool get_compression()
Returns if the protocol is compressed or not.
Definition: protocol_classic.cc:2847
Definition: protocol_classic.h:52
virtual void start_row()=0
Result set sending functions.
ulong m_client_capabilities
Definition: protocol_classic.h:54
virtual int read_packet()
Read packet from client.
Definition: protocol_classic.cc:1373
virtual bool store_short(longlong from)
Definition: protocol_classic.cc:3241
virtual void start_row()
Result set sending functions.
Definition: protocol_classic.cc:3494
virtual bool flush()
Used for the classic protocol.
Definition: protocol_classic.cc:2823
String convert
Definition: protocol_classic.h:60
virtual bool store_longlong(longlong from, bool unsigned_flag)
Definition: protocol_classic.cc:3269
virtual bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs)
Prepares the server for metadata sending.
Definition: protocol_classic.cc:3486
uint count
Definition: protocol_classic.h:65
String * get_output_packet()
Definition: protocol_classic.cc:1371
uint bit_fields
Definition: protocol_classic.h:241
virtual bool store_long(longlong from)
Definition: protocol_classic.cc:3254
Definition: protocol_classic.h:239
Protocol_binary(THD *thd_arg)
Definition: protocol_classic.h:245
virtual bool store_time(MYSQL_TIME *time, uint precision)
Definition: protocol_classic.cc:3349
virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs)
Definition: protocol_classic.h:220
Definition: com_data.h:106
bool net_send_ok(THD *, uint, uint, ulonglong, ulonglong, const char *, bool)
Return OK to the client.
Definition: protocol_classic.cc:813
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:159
virtual bool send_field_metadata(Send_field *field, const CHARSET_INFO *item_charset)
Sends a single column metadata.
Definition: protocol_classic.cc:3038
virtual bool store(MYSQL_TIME *time, uint precision)
Definition: protocol_classic.cc:3636
virtual enum enum_protocol_type type() const
Definition: protocol_classic.h:232
Common definition between mysql server & client.
virtual bool send_parameters(List< Item_param > *parameters, bool is_sql_prepare)
Sends OUT-parameters by writing the values to the protocol.
Definition: protocol_classic.cc:3373
virtual int get_command(COM_DATA *com_data, enum_server_command *cmd)
Reads the command from the protocol and creates a command.
Definition: protocol_classic.cc:2776
virtual ulong get_client_capabilities()
Returns the client capabilities stored on the protocol.
Definition: protocol_classic.h:166
Protocol_text()
Definition: protocol_classic.h:212
Definition: protocol.h:112
Placeholder (&#39;?&#39;) of prepared statement.
Definition: item.h:3667
virtual bool send_parameters(List< Item_param > *parameters, bool)
Sets OUT-parameters to user variables.
Definition: protocol_classic.cc:3440
const CHARSET_INFO * result_cs
Definition: protocol_classic.h:71
virtual bool send_ok(uint server_status, uint statement_warn_count, ulonglong affected_rows, ulonglong last_insert_id, const char *message)
A default implementation of "OK" packet response to the client.
Definition: protocol_classic.cc:1266
Protocol_text(THD *thd_arg)
Definition: protocol_classic.h:213
Protocol_classic(THD *thd)
Definition: protocol_classic.h:94
bool bad_packet
Definition: protocol_classic.h:91
Vio * get_vio()
Definition: protocol_classic.cc:1359
void remove_client_capability(unsigned long capability)
Definition: protocol_classic.h:176
void set_max_packet_size(ulong max_packet_size)
Definition: protocol_classic.cc:1353
enum enum_vio_type vio_type(const MYSQL_VIO vio)
virtual void abort_row()
Definition: protocol_classic.h:132
void set_vio(Vio *vio)
Definition: protocol_classic.cc:1363
Definition: aggregate_check.h:523
void wipe_net()
Definition: protocol_classic.cc:1349
uchar * net_store_data(uchar *to, const uchar *from, size_t length)
Definition: protocol_classic.cc:1223
my_decimal class limits &#39;decimal_t&#39; type to what we need in MySQL.
Definition: my_decimal.h:100
void set_client_capabilities(ulong client_capabilities)
Definition: protocol_classic.h:168
uchar * input_raw_packet
Definition: protocol_classic.h:70
virtual bool end_result_metadata()
Signals the client that the metadata sending is done.
Definition: protocol_classic.cc:2894
void init(THD *thd_arg)
Definition: protocol_classic.cc:1249
virtual my_socket get_socket()
Definition: protocol_classic.cc:3718
enum enum_field_types * field_types
Definition: protocol_classic.h:64
uint field_count
Definition: protocol_classic.h:67
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
unsigned int len
Definition: dbug_analyze.cc:216
unsigned int uint
Definition: uca-dump.cc:29
bool store_string_aux(const char *from, size_t length, const CHARSET_INFO *fromcs, const CHARSET_INFO *tocs)
Auxilary function to convert string to the given character set and store in network buffer...
Definition: protocol_classic.cc:3193
#define true
Definition: config_static.h:44
long long int longlong
Definition: my_inttypes.h:73
enum_vio_type
Definition: violite.h:75
virtual bool store_null()
Definition: protocol_classic.cc:3179
uint32_t uint32
Definition: my_inttypes.h:62
void claim_memory_ownership()
Definition: protocol_classic.cc:1333
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Definition: violite.h:332
virtual void start_row()
Result set sending functions.
Definition: protocol_classic.cc:3172
virtual bool end_row()
Definition: protocol_classic.cc:3132
String * packet
Definition: protocol_classic.h:59
uchar get_error()
Definition: protocol_classic.cc:1347
Definition: m_ctype.h:359
virtual bool send_error(uint sql_errno, const char *err_msg, const char *sql_state)
A default implementation of "ERROR" packet response to the client.
Definition: protocol_classic.cc:1310
virtual bool store_short(longlong from)
Definition: protocol_classic.cc:3543
Protocol_classic()
Definition: protocol_classic.h:92
int my_socket
Definition: my_io_bits.h:59
ulong input_packet_length
Definition: protocol_classic.h:69
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
virtual bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs)
Prepares the server for metadata sending.
Definition: protocol_classic.cc:2849
virtual bool parse_packet(union COM_DATA *data, enum_server_command cmd)
Definition: protocol_classic.cc:2563
virtual bool send_eof(uint server_status, uint statement_warn_count)
A default implementation of "EOF" packet response to the client.
Definition: protocol_classic.cc:1284
bool net_store_data(const uchar *from, size_t length)
Definition: protocol_classic.cc:474
Definition: field.h:700
uint field_pos
Definition: protocol_classic.h:61
virtual bool store_long(longlong from)
Definition: protocol_classic.cc:3558
virtual bool store_date(MYSQL_TIME *time)
Definition: protocol_classic.cc:3669
Definition: mysql_time.h:64
uint get_packet_length()
Definition: protocol_classic.h:199
virtual bool store_ps_status(ulong stmt_id, uint column_count, uint param_count, ulong cond_count)
Sends prepared statement&#39;s id and metadata to the client after prepare.
Definition: protocol_classic.cc:2825
Definition: protocol.h:34
char sql_state[64]
Definition: test_sql_9_sessions.cc:299
virtual bool has_client_capability(unsigned long client_capability)
Checks if the client capabilities include the one specified as parameter.
Definition: protocol_classic.h:180
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:3144
virtual bool store_tiny(longlong from)
Definition: protocol_classic.cc:3229
bool write(const uchar *ptr, size_t len)
Definition: protocol_classic.cc:1343
virtual ~Protocol_classic()
Definition: protocol_classic.h:101
THD * m_thd
Definition: protocol_classic.h:58
Common #defines and includes for file and socket I/O.
Definition: protocol_classic.h:46
Vio Lite.
Time declarations shared between the server and client API: you should not add anything to this heade...
virtual bool store_decimal(const my_decimal *, uint, uint)
Definition: protocol_classic.cc:3588
int32_t int32
Definition: my_inttypes.h:61
bool net_send_error(THD *thd, uint sql_errno, const char *err)
Send a error string to client.
Definition: protocol_classic.cc:564
Protocol_binary()
Definition: protocol_classic.h:244
Class used for the old (MySQL 4.0 protocol).
Definition: protocol_classic.h:210
void set_output_pkt_nr(uint pkt_nr)
Definition: protocol_classic.cc:1365
A better implementation of the UNIX ctype(3) library.
Definition: protocol.h:114
COM_DATA cmd
Definition: test_session_info.cc:95
virtual void set_write_timeout(ulong write_timeout)
Definition: protocol_classic.cc:1324
virtual enum enum_protocol_type type() const
Definition: protocol_classic.h:268
uint get_output_pkt_nr()
Definition: protocol_classic.cc:1369
virtual bool connection_alive() const
Checks if the protocol&#39;s connection with the client is still alive.
Definition: protocol_classic.cc:3168
static int flags[50]
Definition: hp_test1.cc:39
uint sending_flags
Definition: protocol_classic.h:68
virtual enum enum_vio_type connection_type() const
Returns the type of the connection.
Definition: protocol_classic.h:141
bool init_net(Vio *vio)
Definition: protocol_classic.cc:1329
bool net_send_eof(THD *thd, uint server_status, uint statement_warn_count)
Send eof (= end of result set) to the client.
Definition: protocol_classic.cc:1006
virtual uint get_rw_status()
Returns the read/writing status.
Definition: protocol_classic.cc:2810
unsigned long ulong
Definition: my_inttypes.h:48
t time
Definition: dbug_analyze.cc:149
virtual bool store_date(MYSQL_TIME *time)
Definition: protocol_classic.cc:3337
#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:829
virtual bool store(const char *from, size_t length, const CHARSET_INFO *cs)
Definition: protocol_classic.h:259
virtual 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:2767
bool send_metadata
Definition: protocol_classic.h:62
virtual int shutdown(bool server_shutdown=false)
Thread is being shut down, disconnect and free resources.
Definition: protocol_classic.cc:3207
virtual bool store_longlong(longlong from, bool unsigned_flag)
Definition: protocol_classic.cc:3573
Definition: sql_list.h:685
bool net_send_error_packet(THD *, uint, const char *, const char *)
Definition: protocol_classic.cc:1128
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
NET * get_net()
Definition: protocol_classic.cc:1357