1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
#ifndef PROTOCOL_INCLUDED
#define PROTOCOL_INCLUDED
/* Copyright (c) 2002, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <assert.h>
#include "mysql/com_data.h"
#include "mysql/mysql_lex_string.h" // LEX_STRING
#include "mysql_com.h" // mysql_enum_shutdown_level
#include "mysql_time.h" // MYSQL_TIME
#include "sql_string.h" // String
#include "violite.h" /* SSL && enum_vio_type */
class my_decimal;
class Send_field;
class Item_param;
template <class T>
class List;
class Field;
class Protocol {
private:
/// Pointer to the Protocol below on the stack.
Protocol *m_previous_protocol = nullptr;
public:
virtual ~Protocol() = default;
/**
Remove the reference to the previous protocol and return it.
@returns The new top of the Protocol stack.
*/
Protocol *pop_protocol() {
assert(m_previous_protocol);
Protocol *protocol = m_previous_protocol;
m_previous_protocol = nullptr;
return protocol;
}
/**
Set reference to "this" as the previous protocol on the protocol provided
as argument.
@param protocol Protocol to become the top of Protocol stack.
*/
void push_protocol(Protocol *protocol) {
assert(!protocol->m_previous_protocol);
protocol->m_previous_protocol = this;
}
/**
Read packet from client
@retval -1 fatal error
@retval 0 ok
@retval 1 non-fatal error
*/
virtual int read_packet() = 0;
/**
Reads the command from the protocol and creates a command.
@param com_data out parameter
@param cmd out parameter
@returns
-1 fatal protocol error
0 ok
1 non-fatal protocol or parsing error
*/
virtual int get_command(COM_DATA *com_data, enum_server_command *cmd) = 0;
/**
Enum used by type() to specify the protocol type
*/
enum enum_protocol_type {
PROTOCOL_TEXT = 0, // text Protocol type used mostly
// for the old (MySQL 4.0 protocol)
PROTOCOL_BINARY = 1, // binary protocol type
PROTOCOL_LOCAL = 2, // local protocol type(intercepts communication)
PROTOCOL_ERROR = 3, // error protocol instance
PROTOCOL_PLUGIN = 4 // pluggable protocol type
};
/**
Flags available to alter the way the messages are sent to the client
*/
enum { SEND_NUM_ROWS = 1, SEND_DEFAULTS = 2, SEND_EOF = 4 };
virtual enum enum_protocol_type type() const = 0;
virtual enum enum_vio_type connection_type() const = 0;
/* Data sending functions */
virtual bool store_null() = 0;
virtual bool store_tiny(longlong from, uint32 zerofill) = 0;
virtual bool store_short(longlong from, uint32 zerofill) = 0;
virtual bool store_long(longlong from, uint32 zerofill) = 0;
virtual bool store_longlong(longlong from, bool unsigned_flag,
uint32 zerofill) = 0;
virtual bool store_decimal(const my_decimal *, uint, uint) = 0;
virtual bool store_string(const char *from, size_t length,
const CHARSET_INFO *fromcs) = 0;
virtual bool store_float(float from, uint32 decimals, uint32 zerofill) = 0;
virtual bool store_double(double from, uint32 decimals, uint32 zerofill) = 0;
virtual bool store_datetime(const MYSQL_TIME &time, uint precision) = 0;
virtual bool store_date(const MYSQL_TIME &time) = 0;
virtual bool store_time(const MYSQL_TIME &time, uint precision) = 0;
virtual bool store_field(const Field *field) = 0;
// Convenience wrappers
bool store(int from) { return store_long(longlong{from}, 0); }
bool store(uint32 from) { return store_long(longlong{from}, 0); }
bool store(longlong from) { return store_longlong(from, false, 0); }
bool store(ulonglong from) {
return store_longlong(static_cast<longlong>(from), true, 0);
}
bool store_tiny(longlong from) { return store_tiny(from, 0); }
bool store_short(longlong from) { return store_short(from, 0); }
bool store_long(longlong from) { return store_long(from, 0); }
bool store_longlong(longlong from, bool unsigned_flag) {
return store_longlong(from, unsigned_flag, 0);
}
/**
Send \\0 end terminated string.
@param from NullS or \\0 terminated string.
@param fromcs Character set of the from string.
@note In most cases one should use store(from, length, cs) instead of
this function
@retval false ok
@retval true error
*/
inline bool store(const char *from, const CHARSET_INFO *fromcs) {
return from ? store_string(from, strlen(from), fromcs) : store_null();
}
inline bool store(String *str) {
return store_string(str->ptr(), str->length(), str->charset());
}
inline bool store(const LEX_STRING &s, const CHARSET_INFO *cs) {
return store_string(s.str, s.length, cs);
}
/**
Returns the client capabilities stored on the protocol.
The available capabilities are defined in mysql_com.h
*/
virtual ulong get_client_capabilities() = 0;
/**
Checks if the client capabilities include the one
specified as parameter.
@retval true if it includes the specified capability
@retval false otherwise
*/
virtual bool has_client_capability(unsigned long client_capability) = 0;
/**
Checks if the protocol's connection with the client is still alive.
It should always return true unless the protocol closed the connection.
@retval true if the connection is still alive
@retval false otherwise
*/
virtual bool connection_alive() const = 0;
/**
Result set sending functions
@details Server uses following schema to send result:
... sending metadata ...
| start_result_metadata(...)
| start_row()
| send_field_metadata(...)
| end_row()
... same for each field sent ...
| end_result_metadata(...)
|
... sending result ...
| start_row(...)
| store_xxx(...)
... store_xxx(..) is called for each field ...
| end_row(...)
... same for each row, until all rows are sent ...
| send_ok/eof/error(...)
However, a protocol implementation might use different schema. For
example, Protocol_callback ignores start/end_row when metadata is being
sent.
*/
virtual void start_row() = 0;
virtual bool end_row() = 0;
virtual void abort_row() = 0;
virtual void end_partial_result_set() = 0;
/**
Thread is being shut down, disconnect and free resources
@param server_shutdown If false then this is normal thread shutdown. If
true then the server is shutting down.
*/
virtual int shutdown(bool server_shutdown = false) = 0;
/**
Returns the read/writing status
@retval 1 Read
@retval 2 Write
@retval 0 Other(Idle, Killed)
*/
virtual uint get_rw_status() = 0;
/**
Returns if the protocol is compressed or not.
@retval false Not compressed
@retval true Compressed
*/
virtual bool get_compression() = 0;
/**
Returns compression algorithm name.
@retval string compression method name
@retval NULL if no compression is enabled
*/
virtual char *get_compression_algorithm() = 0;
/**
Returns compression level.
@returns compression level
*/
virtual uint get_compression_level() = 0;
/**
Prepares the server for metadata sending.
Notifies the client that the metadata sending will start.
@param num_cols Number of columns that will be sent
@param flags Flags to alter the metadata sending
Can be any of the following:
SEND_NUM_ROWS, SEND_DEFAULTS, SEND_EOF
@param resultcs Charset to convert to
@retval false Ok
@retval true An error occurred
*/
virtual bool start_result_metadata(uint num_cols, uint flags,
const CHARSET_INFO *resultcs) = 0;
/**
Sends field metadata.
@param field Field metadata to be send to the client
@param charset Field's charset: in case it is different
than the one used by the connection it will
be used to convert the value to
the connection's charset
@retval false The metadata was successfully sent
@retval true An error occurred
*/
virtual bool send_field_metadata(Send_field *field,
const CHARSET_INFO *charset) = 0;
/**
Signals the client that the metadata sending is done.
Clears the server after sending the metadata.
@retval false Ok
@retval true An error occurred
*/
virtual bool end_result_metadata() = 0;
/**
Send ok message to the client.
@param server_status The server status
@param statement_warn_count Total number of warnings
@param affected_rows Number of rows changed by statement
@param last_insert_id Last insert id (Auto_increment id for first
row if used)
@param message Message to send to the client
@retval false The message was successfully sent
@retval true An error occurred and the messages wasn't sent properly
*/
virtual bool send_ok(uint server_status, uint statement_warn_count,
ulonglong affected_rows, ulonglong last_insert_id,
const char *message) = 0;
/**
Send eof message to the client.
@param server_status The server status
@param statement_warn_count Total number of warnings
@retval false The message was successfully sent
@retval true An error occurred and the messages wasn't sent properly
*/
virtual bool send_eof(uint server_status, uint statement_warn_count) = 0;
/**
Send error message to the client.
@param sql_errno The error code to send
@param err_msg A pointer to the error message
@param sql_state SQL state
@retval false The message was successfully sent
@retval true An error occurred and the messages wasn't sent properly
*/
virtual bool send_error(uint sql_errno, const char *err_msg,
const char *sql_state) = 0;
/**
Used for the classic protocol.
Makes the protocol send the messages/data to the client.
@retval false The flush was successful.
@retval true An error occurred.
*/
virtual bool flush() = 0;
/**
Sends prepared statement's id and metadata to the client after prepare.
@param stmt_id Statement id.
@param column_count Number of columns.
@param param_count Number of parameters.
@param cond_count Number of conditions raised by the current statement.
@return Error status.
@retval false The send was successful.
@retval true An error occurred.
*/
virtual bool store_ps_status(ulong stmt_id, uint column_count,
uint param_count, ulong cond_count) = 0;
/**
Sends the OUT-parameters to the client.
@param parameters List of PS/SP parameters (both input and output).
@param is_sql_prepare Used for the legacy protocol. If we're dealing with
sql prepare then text protocol will be used.
@return Error status.
@retval false Success.
@retval true Error.
*/
virtual bool send_parameters(List<Item_param> *parameters,
bool is_sql_prepare) = 0;
};
#endif /* PROTOCOL_INCLUDED */
|