MySQL 26.10.0
Source Code Documentation
sql_common.h
Go to the documentation of this file.
1#ifndef SQL_COMMON_INCLUDED
2#define SQL_COMMON_INCLUDED
3
4/* Copyright (c) 2003, 2026, 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/**
28 @file include/sql_common.h
29*/
30
31#include <mysql.h>
32#include <stddef.h>
33#include <sys/types.h>
34
35#include "errmsg.h"
36#include "my_command.h"
37#include "my_compiler.h"
38#include "my_inttypes.h"
39#include "my_list.h"
40#include "mysql_com.h"
41#ifdef MYSQL_SERVER
42#include "mysql_com_server.h"
43#endif
44
45struct CHARSET_INFO;
46struct MEM_ROOT;
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52#ifdef IMPORT_UNKNOWN_SQLSTATE
53__declspec(dllimport)
54#endif
55 extern const char *unknown_sqlstate;
56extern const char *cant_connect_sqlstate;
57extern const char *not_error_sqlstate;
58
59/*
60 Free all memory allocated in MYSQL handle except the
61 current options.
62*/
64
65/*
66 Clear connection options stored in MYSQL handle and
67 free memory used by them.
68*/
70
71/**
72 The structure is used to hold the state change information
73 received from the server. LIST functions are used for manipulation
74 of the members of the structure.
75*/
77 /** head_node->data is a LEX_STRING which contains the variable name. */
80};
81
82/**
83 Store the change info received from the server in an array of linked lists
84 with STATE_INFO_NODE elements (one per state type).
85*/
86struct STATE_INFO {
87 /** Array of STATE_NODE_INFO elements (one per state type). */
89};
90
91/*
92 Access to MYSQL::extension member.
93
94 Note: functions mysql_extension_{init,free}() are defined
95 in client.c.
96*/
97
98struct st_mysql_trace_info;
100
102 struct st_mysql_trace_info *trace_data;
103 // Per-connection cache of the value returned by VERSION_WITH_UPDATE().
106 /* Struct to track the state of asynchronous operations */
108#ifdef MYSQL_SERVER
109 // Used by replication to pass around compression context data.
111 /* used by mysql_init() api for mysql command service related information */
112 void *mcs_extn;
113#endif
114 struct {
116 char **names;
119};
120
121/* "Constructor/destructor" for MYSQL extension structure. */
124/* cleanup for the MYSQL extension bind structure */
126/* cleanup for the MYSQL extension session state cache */
128/* decode a session-tracker payload into MYSQL extension state */
130 size_t payload_length);
131/*
132 Note: Allocated extension structure is freed in mysql_close_free()
133 called by mysql_close().
134*/
135#define MYSQL_EXTENSION_PTR(H) \
136 ((MYSQL_EXTENSION *)((H)->extension \
137 ? (H)->extension \
138 : ((H)->extension = mysql_extension_init(H))))
139
140#define ASYNC_DATA(M) \
141 (NULL != (M) ? (MYSQL_EXTENSION_PTR(M)->mysql_async_context) : NULL)
142/**
143 Sets the MYSQL_EXTENSION::server_extn attribute by the use of NET_SERVER which
144 contains information about compression context and compression attributes.
145 This attribute needs to be set each time mysql_real_connect() is called to
146 make a connection. When a connection attempt fails or when a connection is
147 closed, as part of the MYSQL handle cleanup, mysql_close_free() is called and
148 that will free MYSQL_EXTENSION::server_extn.
149
150 mysql_close_free() will free all the memory allocated in the MYSQL handle but
151 preserves MYSQL::options. These options are later free'd by
152 mysql_close_free_options() unless the client flag CLIENT_REMEMBER_OPTIONS is
153 set.
154
155 @param mysql The MYSQL handle
156 @param extn The NET_SERVER handle that contains compression context info.
157*/
158#ifdef MYSQL_SERVER
160 MYSQL_EXTENSION_PTR(mysql)->server_extn = extn;
161}
162#endif /* MYSQL_SERVER*/
163
164/*
165 Maximum allowed authentication plugins for a given user account.
166*/
167#define MAX_AUTHENTICATION_FACTOR 3
168/*
169 Placeholder to save plugin name and password of 1st, 2nd and 3rd
170 factor authentication methods.
171*/
174 char *password;
175};
176
180 char *ssl_crl; /* PEM CRL file */
181 char *ssl_crlpath; /* PEM directory of CRL-s? */
186 bool get_server_public_key; /* Former ssl_enforce */
187 char *tls_version; /* TLS version option */
188 long ssl_ctx_flags; /* SSL ctx options flag */
189 unsigned int ssl_mode;
190 unsigned int retry_count;
191 unsigned int ssl_fips_mode; /* SSL fips mode for enforced encryption.*/
199 void *ssl_session_data; /** the session serialization to use */
200 char *tls_sni_servername; /* TLS sni server name */
201 char *tls_kex;
204};
205
206#define MYSQL_OPTIONS_EXTENSION_PTR(H, what) \
207 ((H)->options.extension ? (H)->options.extension->what : nullptr)
208
210 MYSQL *(*connect_method)(mysql_async_connect *connect_args);
213 const unsigned char *header, size_t header_length,
214 const unsigned char *arg, size_t arg_length,
215 bool skip_check, MYSQL_STMT *stmt);
216 MYSQL_DATA *(*read_rows)(MYSQL *mysql, MYSQL_FIELD *mysql_fields,
217 unsigned int fields);
218 MYSQL_RES *(*use_result)(MYSQL *mysql);
220 void (*fetch_lengths)(unsigned long *to, MYSQL_ROW column,
221 unsigned int field_count);
222 void (*flush_use_result)(MYSQL *mysql, bool flush_all_results);
224#if !defined(MYSQL_SERVER) && !defined(MYSQL_COMPONENT)
225 MYSQL_FIELD *(*list_fields)(MYSQL *mysql);
226 bool (*read_prepare_result)(MYSQL *mysql, MYSQL_STMT *stmt);
227 int (*stmt_execute)(MYSQL_STMT *stmt);
228 int (*read_binary_rows)(MYSQL_STMT *stmt);
229 int (*unbuffered_fetch)(MYSQL *mysql, char **row);
230 const char *(*read_statistics)(MYSQL *mysql);
231 bool (*next_result)(MYSQL *mysql);
232 int (*read_rows_from_cursor)(MYSQL_STMT *stmt);
233 void (*free_rows)(MYSQL_DATA *cur);
234#endif
237 MYSQL *mysql, enum enum_server_command command,
238 const unsigned char *header, unsigned long header_length,
239 const unsigned char *arg, unsigned long arg_length, bool skip_check,
240 MYSQL_STMT *stmt, bool *error);
242 MYSQL_FIELD *mysql_fields,
243 unsigned int fields,
246 bool flush_all_results);
249 ulong *res);
250};
251
252#define simple_command(mysql, command, arg, length, skip_check) \
253 ((mysql)->methods \
254 ? (*(mysql)->methods->advanced_command)(mysql, command, nullptr, 0, \
255 arg, length, skip_check, NULL) \
256 : (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), \
257 1))
258#define simple_command_nonblocking(mysql, command, arg, length, skip_check, \
259 error) \
260 (*(mysql)->methods->advanced_command_nonblocking)( \
261 mysql, command, nullptr, 0, arg, length, skip_check, nullptr, error)
262
263#define stmt_command(mysql, command, arg, length, stmt) \
264 ((mysql)->methods \
265 ? (*(mysql)->methods->advanced_command)(mysql, command, nullptr, 0, \
266 arg, length, 1, stmt) \
267 : (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), \
268 1))
269
272 uint fields, bool default_value,
273 uint server_capabilities);
275 unsigned long field_count,
276 unsigned int fields);
277MYSQL_FIELD *cli_read_metadata(MYSQL *mysql, unsigned long field_count,
278 unsigned int fields);
281void free_rows(MYSQL_DATA *cur);
283void end_server(MYSQL *mysql);
286 const char *filename, const char *group);
288 const unsigned char *header, size_t header_length,
289 const unsigned char *arg, size_t arg_length,
290 bool skip_check, MYSQL_STMT *stmt);
291unsigned long cli_safe_read(MYSQL *mysql, bool *is_data_packet);
293 bool *is_data_packet,
294 ulong *res);
295unsigned long cli_safe_read_with_ok(MYSQL *mysql, bool parse_ok,
296 bool *is_data_packet);
297void net_clear_error(NET *net);
298void set_stmt_errmsg(MYSQL_STMT *stmt, NET *net);
299void set_stmt_error(MYSQL_STMT *stmt, int errcode, const char *sqlstate);
300void set_stmt_extended_error(MYSQL_STMT *stmt, int errcode,
301 const char *sqlstate, const char *format, ...)
302 MY_ATTRIBUTE((format(printf, 4, 5)));
303void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate);
304void set_mysql_extended_error(MYSQL *mysql, int errcode, const char *sqlstate,
305 const char *format, ...)
306 MY_ATTRIBUTE((format(printf, 4, 5)));
307
308/* client side of the pluggable authentication */
310
312int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
313 const char *data_plugin, const char *db);
316
318
323void read_ok_ex(MYSQL *mysql, unsigned long len);
324
325bool fix_param_bind(MYSQL_BIND *param, uint idx);
327 NET *net, unsigned int param_count, MYSQL_BIND *params, const char **names,
328 unsigned long n_param_sets, uchar **ret_data, ulong *ret_length,
329 uchar send_types_to_server, bool send_named_params,
330 bool send_parameter_set_count, bool send_parameter_count_when_zero);
331
332#ifdef __cplusplus
333}
334#endif
335
336#define protocol_41(A) ((A)->server_capabilities & CLIENT_PROTOCOL_41)
337
338/*
339 AuthNextFactor packet is defined to have value 2 in 1st byte.
340*/
341#define AUTH_NEXT_FACTOR_PACKETTYPE 2
342#endif /* SQL_COMMON_INCLUDED */
Error messages for MySQL clients.
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 char uchar
Definition: my_inttypes.h:52
static const char * filepath
Definition: myisamlog.cc:97
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
char ** MYSQL_ROW
Definition: mysql.h:145
Common definition between mysql server & client.
#define MYSQL_VIO
Definition: mysql_com.h:902
#define SESSION_TRACK_END
End of enum_session_state_type.
Definition: mysql_com.h:1090
Definitions private to the server, used in the networking layer to notify specific events.
static MYSQL * mysql
Definition: mysqldump.cc:151
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
Definition: buf0block_hint.cc:30
Json_data_extension ext
Definition: backend.cc:50
Definition: system_info_linux.cc:46
Definition: buffer.h:45
Definition: options.cc:57
const char * filename
Definition: pfs_example_component_population.cc:67
struct result result
Definition: result.h:34
net_async_status
Definition: plugin_auth_common.h:157
void mysql_extension_bind_free(MYSQL_EXTENSION *ext)
Definition: client.cc:3469
const char * unknown_sqlstate
Definition: client.cc:219
void set_stmt_extended_error(MYSQL_STMT *stmt, int errcode, const char *sqlstate, const char *format,...)
Set statement error code, sqlstate, and error message from given errcode and sqlstate.
Definition: libmysql.cc:1357
void mysql_close_free(MYSQL *mysql)
Definition: client.cc:7817
void set_stmt_errmsg(MYSQL_STMT *stmt, NET *net)
Set statement error code, sqlstate, and error message from NET.
Definition: libmysql.cc:1378
void end_server(MYSQL *mysql)
Definition: client.cc:1978
void mysql_clear_session_track_info(MYSQL *mysql)
Definition: client.cc:741
void set_mysql_extended_error(MYSQL *mysql, int errcode, const char *sqlstate, const char *format,...)
Set an error message on the client.
Definition: client.cc:390
bool libmysql_cleartext_plugin_enabled
Definition: client.cc:5795
bool fix_param_bind(MYSQL_BIND *param, uint idx)
Definition: bind_params.cc:465
bool mysql_reconnect(MYSQL *mysql)
Definition: client.cc:7486
int mysql_client_plugin_init()
Initializes the client plugin layer.
Definition: client_plugin.cc:336
#define MYSQL_EXTENSION_PTR(H)
Definition: sql_common.h:135
MYSQL_FIELD * cli_read_metadata(MYSQL *mysql, unsigned long field_count, unsigned int fields)
void mpvio_info(MYSQL_VIO vio, MYSQL_PLUGIN_VIO_INFO *info)
void net_clear_error(NET *net)
Clear possible error state of struct NET.
Definition: client.cc:373
const char * cant_connect_sqlstate
Definition: client.cc:221
unsigned long cli_safe_read(MYSQL *mysql, bool *is_data_packet)
Read a packet from server.
Definition: client.cc:1401
MYSQL_RES * use_result(MYSQL *mysql)
Definition: client.cc:8582
void read_ok_ex(MYSQL *mysql, unsigned long len)
enum net_async_status cli_safe_read_nonblocking(MYSQL *mysql, bool *is_data_packet, ulong *res)
Its a non blocking version of cli_safe_read.
Definition: client.cc:1246
bool cli_advanced_command(MYSQL *mysql, enum enum_server_command command, const unsigned char *header, size_t header_length, const unsigned char *arg, size_t arg_length, bool skip_check, MYSQL_STMT *stmt)
Definition: client.cc:1413
void free_rows(MYSQL_DATA *cur)
Definition: client.cc:1405
void mysql_client_plugin_deinit()
Deinitializes the client plugin layer.
Definition: client_plugin.cc:376
unsigned long cli_safe_read_with_ok(MYSQL *mysql, bool parse_ok, bool *is_data_packet)
Read a packet from server.
Definition: client.cc:1268
bool mysql_int_serialize_param_data(NET *net, unsigned int param_count, MYSQL_BIND *params, const char **names, unsigned long n_param_sets, uchar **ret_data, ulong *ret_length, uchar send_types_to_server, bool send_named_params, bool send_parameter_set_count, bool send_parameter_count_when_zero)
Serialize the query parameters.
Definition: bind_params.cc:362
int is_file_or_dir_world_writable(const char *filepath)
Check if a file/dir is world-writable (only on non-Windows platforms)
Definition: my_path_permissions.cc:43
MYSQL_FIELD * cli_read_metadata_ex(MYSQL *mysql, MEM_ROOT *alloc, unsigned long field_count, unsigned int fields)
MYSQL * connect_helper(mysql_async_connect *ctx)
Definition: client.cc:6479
MYSQL_FIELD * unpack_fields(MYSQL *mysql, MYSQL_ROWS *data, MEM_ROOT *alloc, uint fields, bool default_value, uint server_capabilities)
Definition: client.cc:2599
int run_plugin_auth(MYSQL *mysql, char *data, uint data_len, const char *data_plugin, const char *db)
Client side of the plugin driver authentication.
Definition: client.cc:5863
void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate)
Set the internal error message to mysql handler.
Definition: client.cc:342
#define MAX_AUTHENTICATION_FACTOR
Definition: sql_common.h:167
bool mysql_decode_session_track_payload(MYSQL *mysql, const uchar *payload, size_t payload_length)
Definition: client.cc:841
CHARSET_INFO * default_client_charset_info
Definition: client.cc:231
void free_old_query(MYSQL *mysql)
Definition: client.cc:1703
uchar * send_client_connect_attrs(MYSQL *mysql, uchar *buf)
Definition: client.cc:4294
const char * not_error_sqlstate
Definition: client.cc:220
MYSQL_EXTENSION * mysql_extension_init(MYSQL *)
Definition: client.cc:3448
void set_stmt_error(MYSQL_STMT *stmt, int errcode, const char *sqlstate)
Set statement error code, sqlstate, and error message from given errcode and sqlstate.
Definition: libmysql.cc:1342
void mysql_close_free_options(MYSQL *mysql)
Definition: client.cc:7776
void mysql_extension_set_server_extn(MYSQL *mysql, NET_SERVER *extn)
Sets the MYSQL_EXTENSION::server_extn attribute by the use of NET_SERVER which contains information a...
Definition: sql_common.h:159
void mysql_extension_free(MYSQL_EXTENSION *)
Definition: client.cc:3620
struct st_mysql_client_plugin * mysql_client_builtins[]
Definition: client.cc:4266
void mysql_read_default_options(struct st_mysql_options *options, const char *filename, const char *group)
Definition: client.cc:2202
Definition: m_ctype.h:421
Definition: my_list.h:36
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_async.h:235
Definition: mysql.h:670
Definition: mysql.h:163
Definition: sql_common.h:101
char ** names
Definition: sql_common.h:116
uint n_params
Definition: sql_common.h:115
struct MYSQL_ASYNC * mysql_async_context
Definition: sql_common.h:107
NET_SERVER * server_extn
Definition: sql_common.h:110
struct MYSQL_EXTENSION::@9 bind_info
char * server_version_with_update
Definition: sql_common.h:104
struct st_mysql_trace_info * trace_data
Definition: sql_common.h:102
STATE_INFO state_change
Definition: sql_common.h:105
void * mcs_extn
Definition: sql_common.h:112
MYSQL_BIND * bind
Definition: sql_common.h:117
Definition: mysql.h:121
Definition: sql_common.h:209
enum net_async_status(* advanced_command_nonblocking)(MYSQL *mysql, enum enum_server_command command, const unsigned char *header, unsigned long header_length, const unsigned char *arg, unsigned long arg_length, bool skip_check, MYSQL_STMT *stmt, bool *error)
Definition: sql_common.h:236
MYSQL_ROW(* fetch_row)(MYSQL_RES *)
Definition: sql_common.h:219
enum net_async_status(* read_change_user_result_nonblocking)(MYSQL *mysql, ulong *res)
Definition: sql_common.h:248
bool(* read_query_result)(MYSQL *mysql)
Definition: sql_common.h:211
void(* fetch_lengths)(unsigned long *to, MYSQL_ROW column, unsigned int field_count)
Definition: sql_common.h:220
bool(* advanced_command)(MYSQL *mysql, enum enum_server_command command, const unsigned char *header, size_t header_length, const unsigned char *arg, size_t arg_length, bool skip_check, MYSQL_STMT *stmt)
Definition: sql_common.h:212
enum net_async_status(* read_rows_nonblocking)(MYSQL *mysql, MYSQL_FIELD *mysql_fields, unsigned int fields, MYSQL_DATA **result)
Definition: sql_common.h:241
void(* flush_use_result)(MYSQL *mysql, bool flush_all_results)
Definition: sql_common.h:222
enum net_async_status(* read_query_result_nonblocking)(MYSQL *mysql)
Definition: sql_common.h:235
enum net_async_status(* next_result_nonblocking)(MYSQL *mysql)
Definition: sql_common.h:247
int(* read_change_user_result)(MYSQL *mysql)
Definition: sql_common.h:223
enum net_async_status(* flush_use_result_nonblocking)(MYSQL *mysql, bool flush_all_results)
Definition: sql_common.h:245
Definition: plugin_auth_common.h:141
Definition: mysql.h:343
Definition: mysql.h:153
Definition: mysql.h:697
Definition: mysql.h:303
Definition: client.cc:4290
Definition: mysql_com_server.h:59
Definition: mysql_com.h:921
The structure is used to hold the state change information received from the server.
Definition: sql_common.h:76
LIST * head_node
head_node->data is a LEX_STRING which contains the variable name.
Definition: sql_common.h:78
LIST * current_node
Definition: sql_common.h:79
Store the change info received from the server in an array of linked lists with STATE_INFO_NODE eleme...
Definition: sql_common.h:86
STATE_INFO_NODE info_list[SESSION_TRACK_END+1]
Array of STATE_NODE_INFO elements (one per state type).
Definition: sql_common.h:88
Definition: sql_common.h:172
char * plugin_name
Definition: sql_common.h:173
char * password
Definition: sql_common.h:174
Definition: client_async_authentication.h:164
Definition: client_plugin.h:114
Definition: sql_common.h:177
unsigned int ssl_fips_mode
Definition: sql_common.h:191
bool get_server_public_key
Definition: sql_common.h:186
char * tls_kex
Definition: sql_common.h:201
char * default_auth
Definition: sql_common.h:179
char * compression_algorithm
Definition: sql_common.h:193
long ssl_ctx_flags
Definition: sql_common.h:188
char * ssl_crl
Definition: sql_common.h:180
char * tls_version
Definition: sql_common.h:187
void * ssl_session_data
Definition: sql_common.h:199
bool connection_compressed
Definition: sql_common.h:196
bool force_pqc
Definition: sql_common.h:202
char * plugin_dir
Definition: sql_common.h:178
unsigned int retry_count
Definition: sql_common.h:190
bool enable_cleartext_plugin
Definition: sql_common.h:185
char * tls_ciphersuites
Definition: sql_common.h:192
struct client_authentication_info client_auth_info[MAX_AUTHENTICATION_FACTOR]
Definition: sql_common.h:198
unsigned int total_configured_compression_algorithms
Definition: sql_common.h:194
char * server_public_key_path
Definition: sql_common.h:183
size_t connection_attributes_length
Definition: sql_common.h:184
struct My_hash * connection_attributes
Definition: sql_common.h:182
unsigned int ssl_mode
Definition: sql_common.h:189
bool use_pqc_sign
Definition: sql_common.h:203
char * tls_sni_servername
the session serialization to use
Definition: sql_common.h:200
char * ssl_crlpath
Definition: sql_common.h:181
char * load_data_dir
Definition: sql_common.h:197
unsigned int zstd_compression_level
Definition: sql_common.h:195
Definition: mysql.h:229