xref: /PHP-8.2/ext/mysqlnd/mysqlnd_connection.c (revision 06bda992)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Andrey Hristov <andrey@php.net>                             |
14   |          Ulf Wendel <uw@php.net>                                     |
15   +----------------------------------------------------------------------+
16 */
17 
18 #include "php.h"
19 #include "mysqlnd.h"
20 #include "mysqlnd_connection.h"
21 #include "mysqlnd_vio.h"
22 #include "mysqlnd_protocol_frame_codec.h"
23 #include "mysqlnd_auth.h"
24 #include "mysqlnd_wireprotocol.h"
25 #include "mysqlnd_priv.h"
26 #include "mysqlnd_result.h"
27 #include "mysqlnd_statistics.h"
28 #include "mysqlnd_charset.h"
29 #include "mysqlnd_debug.h"
30 #include "mysqlnd_ext_plugin.h"
31 #include "zend_smart_str.h"
32 
33 
34 extern MYSQLND_CHARSET *mysqlnd_charsets;
35 
36 PHPAPI const char * const mysqlnd_server_gone = "MySQL server has gone away";
37 PHPAPI const char * const mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
38 PHPAPI const char * const mysqlnd_out_of_memory = "Out of memory";
39 
40 PHPAPI MYSQLND_STATS * mysqlnd_global_stats = NULL;
41 
42 
43 /* {{{ mysqlnd_upsert_status::reset */
44 void
MYSQLND_METHOD(mysqlnd_upsert_status,reset)45 MYSQLND_METHOD(mysqlnd_upsert_status, reset)(MYSQLND_UPSERT_STATUS * const upsert_status)
46 {
47 	upsert_status->warning_count = 0;
48 	upsert_status->server_status = 0;
49 	upsert_status->affected_rows = 0;
50 	upsert_status->last_insert_id = 0;
51 }
52 /* }}} */
53 
54 
55 /* {{{ mysqlnd_upsert_status::set_affected_rows_to_error */
56 void
MYSQLND_METHOD(mysqlnd_upsert_status,set_affected_rows_to_error)57 MYSQLND_METHOD(mysqlnd_upsert_status, set_affected_rows_to_error)(MYSQLND_UPSERT_STATUS * upsert_status)
58 {
59 	upsert_status->affected_rows = (uint64_t) ~0;
60 }
61 /* }}} */
62 
63 
64 MYSQLND_CLASS_METHODS_START(mysqlnd_upsert_status)
65 	MYSQLND_METHOD(mysqlnd_upsert_status, reset),
66 	MYSQLND_METHOD(mysqlnd_upsert_status, set_affected_rows_to_error),
67 MYSQLND_CLASS_METHODS_END;
68 
69 
70 /* {{{ mysqlnd_upsert_status_init */
71 void
mysqlnd_upsert_status_init(MYSQLND_UPSERT_STATUS * const upsert_status)72 mysqlnd_upsert_status_init(MYSQLND_UPSERT_STATUS * const upsert_status)
73 {
74 	upsert_status->m = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_upsert_status);
75 	upsert_status->m->reset(upsert_status);
76 }
77 /* }}} */
78 
79 
80 /* {{{ mysqlnd_error_list_pdtor */
81 static void
mysqlnd_error_list_pdtor(void * pDest)82 mysqlnd_error_list_pdtor(void * pDest)
83 {
84 	MYSQLND_ERROR_LIST_ELEMENT * element = (MYSQLND_ERROR_LIST_ELEMENT *) pDest;
85 
86 	DBG_ENTER("mysqlnd_error_list_pdtor");
87 	if (element->error) {
88 		mnd_pefree(element->error, TRUE);
89 	}
90 	DBG_VOID_RETURN;
91 }
92 /* }}} */
93 
94 
95 /* {{{ mysqlnd_error_info::reset */
96 static void
MYSQLND_METHOD(mysqlnd_error_info,reset)97 MYSQLND_METHOD(mysqlnd_error_info, reset)(MYSQLND_ERROR_INFO * const info)
98 {
99 	DBG_ENTER("mysqlnd_error_info::reset");
100 
101 	info->error_no = 0;
102 	info->error[0] = '\0';
103 	memset(&info->sqlstate, 0, sizeof(info->sqlstate));
104 	zend_llist_clean(&info->error_list);
105 
106 	DBG_VOID_RETURN;
107 }
108 /* }}} */
109 
110 
111 /* {{{ mysqlnd_error_info::set_client_error */
112 static void
MYSQLND_METHOD(mysqlnd_error_info,set_client_error)113 MYSQLND_METHOD(mysqlnd_error_info, set_client_error)(MYSQLND_ERROR_INFO * const info,
114 													 const unsigned int err_no,
115 													 const char * const sqlstate,
116 													 const char * const error)
117 {
118 	DBG_ENTER("mysqlnd_error_info::set_client_error");
119 	if (err_no) {
120 		MYSQLND_ERROR_LIST_ELEMENT error_for_the_list = {0};
121 
122 		info->error_no = err_no;
123 		strlcpy(info->sqlstate, sqlstate, sizeof(info->sqlstate));
124 		strlcpy(info->error, error, sizeof(info->error));
125 
126 		error_for_the_list.error_no = err_no;
127 		strlcpy(error_for_the_list.sqlstate, sqlstate, sizeof(error_for_the_list.sqlstate));
128 		error_for_the_list.error = mnd_pestrdup(error, TRUE);
129 		if (error_for_the_list.error) {
130 			DBG_INF_FMT("adding error [%s] to the list", error_for_the_list.error);
131 			zend_llist_add_element(&info->error_list, &error_for_the_list);
132 		}
133 	} else {
134 		info->m->reset(info);
135 	}
136 	DBG_VOID_RETURN;
137 }
138 /* }}} */
139 
140 
141 MYSQLND_CLASS_METHODS_START(mysqlnd_error_info)
142 	MYSQLND_METHOD(mysqlnd_error_info, reset),
143 	MYSQLND_METHOD(mysqlnd_error_info, set_client_error),
144 MYSQLND_CLASS_METHODS_END;
145 
146 
147 
148 /* {{{ mysqlnd_error_info_init */
149 PHPAPI void
mysqlnd_error_info_init(MYSQLND_ERROR_INFO * const info,const bool persistent)150 mysqlnd_error_info_init(MYSQLND_ERROR_INFO * const info, const bool persistent)
151 {
152 	DBG_ENTER("mysqlnd_error_info_init");
153 	info->m = mysqlnd_error_info_get_methods();
154 	info->m->reset(info);
155 
156 	zend_llist_init(&info->error_list, sizeof(MYSQLND_ERROR_LIST_ELEMENT), (llist_dtor_func_t) mysqlnd_error_list_pdtor, persistent);
157 	info->persistent = persistent;
158 	DBG_VOID_RETURN;
159 }
160 /* }}} */
161 
162 
163 /* {{{ mysqlnd_error_info_free_contents */
164 PHPAPI void
mysqlnd_error_info_free_contents(MYSQLND_ERROR_INFO * const info)165 mysqlnd_error_info_free_contents(MYSQLND_ERROR_INFO * const info)
166 {
167 	DBG_ENTER("mysqlnd_error_info_free_contents");
168 	info->m->reset(info);
169 	DBG_VOID_RETURN;
170 }
171 /* }}} */
172 
173 
174 
175 
176 /* {{{ mysqlnd_connection_state::get */
177 static enum mysqlnd_connection_state
MYSQLND_METHOD(mysqlnd_connection_state,get)178 MYSQLND_METHOD(mysqlnd_connection_state, get)(const struct st_mysqlnd_connection_state * const state_struct)
179 {
180 	DBG_ENTER("mysqlnd_connection_state::get");
181 	DBG_INF_FMT("State=%u", state_struct->state);
182 	DBG_RETURN(state_struct->state);
183 }
184 /* }}} */
185 
186 
187 /* {{{ mysqlnd_connection_state::set */
188 static void
MYSQLND_METHOD(mysqlnd_connection_state,set)189 MYSQLND_METHOD(mysqlnd_connection_state, set)(struct st_mysqlnd_connection_state * const state_struct, const enum mysqlnd_connection_state state)
190 {
191 	DBG_ENTER("mysqlnd_connection_state::set");
192 	DBG_INF_FMT("New state=%u", state);
193 	state_struct->state = state;
194 	DBG_VOID_RETURN;
195 }
196 /* }}} */
197 
198 
199 MYSQLND_CLASS_METHODS_START(mysqlnd_connection_state)
200 	MYSQLND_METHOD(mysqlnd_connection_state, get),
201 	MYSQLND_METHOD(mysqlnd_connection_state, set),
202 MYSQLND_CLASS_METHODS_END;
203 
204 
205 /* {{{ mysqlnd_connection_state_init */
206 PHPAPI void
mysqlnd_connection_state_init(struct st_mysqlnd_connection_state * const state)207 mysqlnd_connection_state_init(struct st_mysqlnd_connection_state * const state)
208 {
209 	DBG_ENTER("mysqlnd_connection_state_init");
210 	state->m = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_connection_state);
211 	state->state = CONN_ALLOCED;
212 	DBG_VOID_RETURN;
213 }
214 /* }}} */
215 
216 
217 
218 /* {{{ mysqlnd_conn_data::free_options */
219 static void
MYSQLND_METHOD(mysqlnd_conn_data,free_options)220 MYSQLND_METHOD(mysqlnd_conn_data, free_options)(MYSQLND_CONN_DATA * conn)
221 {
222 	bool pers = conn->persistent;
223 
224 	if (conn->options->charset_name) {
225 		mnd_pefree(conn->options->charset_name, pers);
226 		conn->options->charset_name = NULL;
227 	}
228 	if (conn->options->auth_protocol) {
229 		mnd_pefree(conn->options->auth_protocol, pers);
230 		conn->options->auth_protocol = NULL;
231 	}
232 	if (conn->options->num_commands) {
233 		unsigned int i;
234 		for (i = 0; i < conn->options->num_commands; i++) {
235 			/* allocated with pestrdup */
236 			mnd_pefree(conn->options->init_commands[i], pers);
237 		}
238 		mnd_pefree(conn->options->init_commands, pers);
239 		conn->options->init_commands = NULL;
240 	}
241 	if (conn->options->cfg_file) {
242 		mnd_pefree(conn->options->cfg_file, pers);
243 		conn->options->cfg_file = NULL;
244 	}
245 	if (conn->options->cfg_section) {
246 		mnd_pefree(conn->options->cfg_section, pers);
247 		conn->options->cfg_section = NULL;
248 	}
249 	if (conn->options->connect_attr) {
250 		zend_hash_destroy(conn->options->connect_attr);
251 		mnd_pefree(conn->options->connect_attr, pers);
252 		conn->options->connect_attr = NULL;
253 	}
254 	if (conn->options->local_infile_directory) {
255 		mnd_pefree(conn->options->local_infile_directory, pers);
256 		conn->options->local_infile_directory = NULL;
257 	}
258 }
259 /* }}} */
260 
261 
262 /* {{{ mysqlnd_conn_data::free_contents */
263 static void
MYSQLND_METHOD(mysqlnd_conn_data,free_contents)264 MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
265 {
266 	bool pers = conn->persistent;
267 
268 	DBG_ENTER("mysqlnd_conn_data::free_contents");
269 
270 	if (conn->current_result) {
271 		conn->current_result->m.free_result(conn->current_result, TRUE);
272 		conn->current_result = NULL;
273 	}
274 
275 	if (conn->protocol_frame_codec) {
276 		conn->protocol_frame_codec->data->m.free_contents(conn->protocol_frame_codec);
277 	}
278 
279 	if (conn->vio) {
280 		conn->vio->data->m.free_contents(conn->vio);
281 	}
282 
283 	DBG_INF("Freeing memory of members");
284 
285 	mysqlnd_set_persistent_string(&conn->hostname, NULL, 0, pers);
286 	mysqlnd_set_persistent_string(&conn->username, NULL, 0, pers);
287 	mysqlnd_set_persistent_string(&conn->password, NULL, 0, pers);
288 	mysqlnd_set_persistent_string(&conn->connect_or_select_db, NULL, 0, pers);
289 	mysqlnd_set_persistent_string(&conn->unix_socket, NULL, 0, pers);
290 	DBG_INF_FMT("scheme=%s", conn->scheme.s);
291 	mysqlnd_set_persistent_string(&conn->scheme, NULL, 0, pers);
292 
293 	if (conn->server_version) {
294 		mnd_pefree(conn->server_version, pers);
295 		conn->server_version = NULL;
296 	}
297 	if (conn->host_info) {
298 		mnd_pefree(conn->host_info, pers);
299 		conn->host_info = NULL;
300 	}
301 	mysqlnd_set_persistent_string(&conn->authentication_plugin_data, NULL, 0, pers);
302 	mysqlnd_set_string(&conn->last_message, NULL, 0);
303 
304 	conn->charset = NULL;
305 	conn->greet_charset = NULL;
306 
307 	DBG_VOID_RETURN;
308 }
309 /* }}} */
310 
311 
312 /* {{{ mysqlnd_conn_data::dtor */
313 static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data,dtor)314 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor)(MYSQLND_CONN_DATA * conn)
315 {
316 	DBG_ENTER("mysqlnd_conn_data::dtor");
317 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
318 
319 	conn->m->free_contents(conn);
320 	conn->m->free_options(conn);
321 
322 	if (conn->error_info) {
323 		mysqlnd_error_info_free_contents(conn->error_info);
324 		conn->error_info = NULL;
325 	}
326 
327 	if (conn->protocol_frame_codec) {
328 		mysqlnd_pfc_free(conn->protocol_frame_codec, conn->stats, conn->error_info);
329 		conn->protocol_frame_codec = NULL;
330 	}
331 
332 	if (conn->vio) {
333 		mysqlnd_vio_free(conn->vio, conn->stats, conn->error_info);
334 		conn->vio = NULL;
335 	}
336 
337 	if (conn->payload_decoder_factory) {
338 		mysqlnd_protocol_payload_decoder_factory_free(conn->payload_decoder_factory);
339 		conn->payload_decoder_factory = NULL;
340 	}
341 
342 	if (conn->stats) {
343 		mysqlnd_stats_end(conn->stats, conn->persistent);
344 	}
345 
346 	mnd_pefree(conn, conn->persistent);
347 
348 	DBG_VOID_RETURN;
349 }
350 /* }}} */
351 
352 
353 /* {{{ mysqlnd_conn_data::set_server_option */
354 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,set_server_option)355 MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const conn, enum_mysqlnd_server_option option)
356 {
357 	DBG_ENTER("mysqlnd_conn_data::set_server_option");
358 	DBG_RETURN(conn->command->set_option(conn, option));
359 }
360 /* }}} */
361 
362 
363 /* {{{ mysqlnd_conn_data::restart_psession */
364 static void
MYSQLND_METHOD(mysqlnd_conn_data,restart_psession)365 MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn)
366 {
367 	DBG_ENTER("mysqlnd_conn_data::restart_psession");
368 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
369 	conn->current_result = NULL;
370 	conn->last_message.s = NULL;
371 	DBG_VOID_RETURN;
372 }
373 /* }}} */
374 
375 
376 /* {{{ mysqlnd_conn_data::end_psession */
377 static void
MYSQLND_METHOD(mysqlnd_conn_data,end_psession)378 MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn)
379 {
380 	DBG_ENTER("mysqlnd_conn_data::end_psession");
381 	/* Free here what should not be seen by the next script */
382 	if (conn->current_result) {
383 		conn->current_result->m.free_result(conn->current_result, TRUE);
384 		conn->current_result = NULL;
385 	}
386 	mysqlnd_set_string(&conn->last_message, NULL, 0);
387 	conn->error_info = &conn->error_info_impl;
388 	DBG_VOID_RETURN;
389 }
390 /* }}} */
391 
392 
393 /* {{{ mysqlnd_conn_data::fetch_auth_plugin_by_name */
394 static struct st_mysqlnd_authentication_plugin *
MYSQLND_METHOD(mysqlnd_conn_data,fetch_auth_plugin_by_name)395 MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name)(const char * const requested_protocol)
396 {
397 	struct st_mysqlnd_authentication_plugin * auth_plugin;
398 	char * plugin_name = NULL;
399 	DBG_ENTER("mysqlnd_conn_data::fetch_auth_plugin_by_name");
400 
401 	mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
402 	DBG_INF_FMT("looking for %s auth plugin", plugin_name);
403 	auth_plugin = mysqlnd_plugin_find(plugin_name);
404 	mnd_sprintf_free(plugin_name);
405 
406 	DBG_RETURN(auth_plugin);
407 }
408 /* }}} */
409 
410 
411 /* {{{ mysqlnd_conn_data::execute_init_commands */
412 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,execute_init_commands)413 MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands)(MYSQLND_CONN_DATA * conn)
414 {
415 	enum_func_status ret = PASS;
416 
417 	DBG_ENTER("mysqlnd_conn_data::execute_init_commands");
418 	if (conn->options->init_commands) {
419 		unsigned int current_command = 0;
420 		for (; current_command < conn->options->num_commands; ++current_command) {
421 			const char * const command = conn->options->init_commands[current_command];
422 			if (command) {
423 				MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
424 				if (PASS != conn->m->query(conn, command, strlen(command))) {
425 					MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
426 					ret = FAIL;
427 					break;
428 				}
429 				do {
430 					if (conn->last_query_type == QUERY_SELECT) {
431 						MYSQLND_RES * result = conn->m->use_result(conn);
432 						if (result) {
433 							result->m.free_result(result, TRUE);
434 						}
435 					}
436 				} while (conn->m->next_result(conn) != FAIL);
437 			}
438 		}
439 	}
440 	DBG_RETURN(ret);
441 }
442 /* }}} */
443 
444 
445 /* {{{ mysqlnd_conn_data::get_updated_connect_flags */
446 static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data,get_updated_connect_flags)447 MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags)
448 {
449 #ifdef MYSQLND_COMPRESSION_ENABLED
450 	MYSQLND_PFC * pfc = conn->protocol_frame_codec;
451 #endif
452 	MYSQLND_VIO * vio = conn->vio;
453 
454 	DBG_ENTER("mysqlnd_conn_data::get_updated_connect_flags");
455 	/* allow CLIENT_LOCAL_FILES capability, although extensions basing on mysqlnd
456 		shouldn't allow 'load data local infile' by default due to security issues */
457 	mysql_flags |= MYSQLND_CAPABILITIES;
458 
459 	mysql_flags |= conn->options->flags; /* use the flags from set_client_option() */
460 
461 #ifndef MYSQLND_COMPRESSION_ENABLED
462 	if (mysql_flags & CLIENT_COMPRESS) {
463 		mysql_flags &= ~CLIENT_COMPRESS;
464 	}
465 #else
466 	if (pfc && pfc->data->flags & MYSQLND_PROTOCOL_FLAG_USE_COMPRESSION) {
467 		mysql_flags |= CLIENT_COMPRESS;
468 	}
469 #endif
470 #ifndef MYSQLND_SSL_SUPPORTED
471 	if (mysql_flags & CLIENT_SSL) {
472 		mysql_flags &= ~CLIENT_SSL;
473 	}
474 #else
475 	if (vio && (vio->data->options.ssl_key ||
476 				vio->data->options.ssl_cert ||
477 				vio->data->options.ssl_ca ||
478 				vio->data->options.ssl_capath ||
479 				vio->data->options.ssl_cipher))
480 	{
481 		mysql_flags |= CLIENT_SSL;
482 	}
483 #endif
484 
485 	if (conn->options->connect_attr && zend_hash_num_elements(conn->options->connect_attr)) {
486 		mysql_flags |= CLIENT_CONNECT_ATTRS;
487 	}
488 
489 	DBG_RETURN(mysql_flags);
490 }
491 /* }}} */
492 
493 
494 /* {{{ mysqlnd_conn_data::connect_handshake */
495 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,connect_handshake)496 MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
497 						const MYSQLND_CSTRING * const scheme,
498 						const MYSQLND_CSTRING * const username,
499 						const MYSQLND_CSTRING * const password,
500 						const MYSQLND_CSTRING * const database,
501 						const unsigned int mysql_flags)
502 {
503 	enum_func_status ret = FAIL;
504 	DBG_ENTER("mysqlnd_conn_data::connect_handshake");
505 
506 	if (PASS == conn->vio->data->m.connect(conn->vio, *scheme, conn->persistent, conn->stats, conn->error_info)) {
507 		conn->protocol_frame_codec->data->m.reset(conn->protocol_frame_codec, conn->stats, conn->error_info);
508 		size_t client_flags = mysql_flags;
509 
510 		ret = conn->command->handshake(conn, *username, *password, *database, client_flags);
511 	}
512 	DBG_RETURN(ret);
513 }
514 /* }}} */
515 
516 /* {{{ mysqlnd_conn_data::get_scheme */
517 static MYSQLND_STRING
MYSQLND_METHOD(mysqlnd_conn_data,get_scheme)518 MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)(MYSQLND_CONN_DATA * conn, MYSQLND_CSTRING hostname, MYSQLND_CSTRING *socket_or_pipe, unsigned int port, bool * unix_socket, bool * named_pipe)
519 {
520 	MYSQLND_STRING transport;
521 	DBG_ENTER("mysqlnd_conn_data::get_scheme");
522 #ifndef PHP_WIN32
523 	if (hostname.l == sizeof("localhost") - 1 && !strncasecmp(hostname.s, "localhost", hostname.l)) {
524 		DBG_INF_FMT("socket=%s", socket_or_pipe->s? socket_or_pipe->s:"n/a");
525 		if (!socket_or_pipe->s) {
526 			socket_or_pipe->s = "/tmp/mysql.sock";
527 			socket_or_pipe->l = strlen(socket_or_pipe->s);
528 		}
529 		transport.l = mnd_sprintf(&transport.s, 0, "unix://%s", socket_or_pipe->s);
530 		*unix_socket = TRUE;
531 #else
532 	if (hostname.l == sizeof(".") - 1 && hostname.s[0] == '.') {
533 		/* named pipe in socket */
534 		if (!socket_or_pipe->s) {
535 			socket_or_pipe->s = "\\\\.\\pipe\\MySQL";
536 			socket_or_pipe->l = strlen(socket_or_pipe->s);
537 		}
538 		transport.l = mnd_sprintf(&transport.s, 0, "pipe://%s", socket_or_pipe->s);
539 		*named_pipe = TRUE;
540 #endif
541 	} else {
542 		if (!port) {
543 			port = 3306;
544 		}
545 		transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s:%u", hostname.s, port);
546 	}
547 	DBG_INF_FMT("transport=%s", transport.s? transport.s:"OOM");
548 	DBG_RETURN(transport);
549 }
550 /* }}} */
551 
552 
553 /* {{{ mysqlnd_conn_data::connect */
554 static enum_func_status
555 MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
556 						MYSQLND_CSTRING hostname,
557 						MYSQLND_CSTRING username,
558 						MYSQLND_CSTRING password,
559 						MYSQLND_CSTRING database,
560 						unsigned int port,
561 						MYSQLND_CSTRING socket_or_pipe,
562 						unsigned int mysql_flags
563 					)
564 {
565 	bool unix_socket = FALSE;
566 	bool named_pipe = FALSE;
567 	bool reconnect = FALSE;
568 	bool saved_compression = FALSE;
569 	MYSQLND_PFC * pfc = conn->protocol_frame_codec;
570 	MYSQLND_STRING transport = { NULL, 0 };
571 
572 	DBG_ENTER("mysqlnd_conn_data::connect");
573 	DBG_INF_FMT("conn=%p", conn);
574 
575 	SET_EMPTY_ERROR(conn->error_info);
576 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
577 
578 	DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
579 				hostname.s?hostname.s:"", username.s?username.s:"", database.s?database.s:"", port, mysql_flags,
580 				conn? conn->persistent:0, conn? (int)GET_CONNECTION_STATE(&conn->state):-1);
581 
582 	if (GET_CONNECTION_STATE(&conn->state) > CONN_ALLOCED) {
583 		DBG_INF("Connecting on a connected handle.");
584 
585 		if (GET_CONNECTION_STATE(&conn->state) < CONN_QUIT_SENT) {
586 			MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
587 			reconnect = TRUE;
588 			conn->m->send_close(conn);
589 		}
590 
591 		conn->m->free_contents(conn);
592 		/* Now reconnect using the same handle */
593 		if (pfc->data->compressed) {
594 			/*
595 			  we need to save the state. As we will re-connect, pfc->compressed should be off, or
596 			  we will look for a compression header as part of the greet message, but there will
597 			  be none.
598 			*/
599 			saved_compression = TRUE;
600 			pfc->data->compressed = FALSE;
601 		}
602 		if (pfc->data->ssl) {
603 			pfc->data->ssl = FALSE;
604 		}
605 	} else {
606 		unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
607 		conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size);
608 	}
609 
610 	if (!hostname.s || !hostname.s[0]) {
611 		hostname.s = "localhost";
612 		hostname.l = strlen(hostname.s);
613 	}
614 	if (!username.s) {
615 		DBG_INF_FMT("no user given, using empty string");
616 		username.s = "";
617 		username.l = 0;
618 	}
619 	if (!password.s) {
620 		DBG_INF_FMT("no password given, using empty string");
621 		password.s = "";
622 		password.l = 0;
623 	}
624 	if (!database.s || !database.s[0]) {
625 		DBG_INF_FMT("no db given, using empty string");
626 		database.s = "";
627 		database.l = 0;
628 	} else {
629 		mysql_flags |= CLIENT_CONNECT_WITH_DB;
630 	}
631 
632 	transport = conn->m->get_scheme(conn, hostname, &socket_or_pipe, port, &unix_socket, &named_pipe);
633 
634 	mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags);
635 
636 	{
637 		const MYSQLND_CSTRING scheme = { transport.s, transport.l };
638 		if (FAIL == conn->m->connect_handshake(conn, &scheme, &username, &password, &database, mysql_flags)) {
639 			goto err;
640 		}
641 	}
642 
643 	{
644 		SET_CONNECTION_STATE(&conn->state, CONN_READY);
645 
646 		if (saved_compression) {
647 			pfc->data->compressed = TRUE;
648 		}
649 		/*
650 		  If a connect on a existing handle is performed and mysql_flags is
651 		  passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
652 		  which we set based on saved_compression.
653 		*/
654 		pfc->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
655 
656 
657 		mysqlnd_set_persistent_string(&conn->scheme, transport.s, transport.l, conn->persistent);
658 		if (transport.s) {
659 			mnd_sprintf_free(transport.s);
660 			transport.s = NULL;
661 		}
662 
663 		if (!conn->scheme.s) {
664 			goto err; /* OOM */
665 		}
666 
667 		mysqlnd_set_persistent_string(&conn->username, username.s, username.l, conn->persistent);
668 		mysqlnd_set_persistent_string(&conn->password, username.s, password.l, conn->persistent);
669 		conn->port				= port;
670 		mysqlnd_set_persistent_string(&conn->connect_or_select_db, database.s, database.l, conn->persistent);
671 
672 		if (!unix_socket && !named_pipe) {
673 			mysqlnd_set_persistent_string(&conn->hostname, hostname.s, hostname.l, conn->persistent);
674 			{
675 				char *p;
676 				mnd_sprintf(&p, 0, "%s via TCP/IP", conn->hostname.s);
677 				if (!p) {
678 					SET_OOM_ERROR(conn->error_info);
679 					goto err; /* OOM */
680 				}
681 				conn->host_info = mnd_pestrdup(p, conn->persistent);
682 				mnd_sprintf_free(p);
683 			}
684 		} else {
685 			conn->unix_socket.s = mnd_pestrdup(socket_or_pipe.s, conn->persistent);
686 			if (unix_socket) {
687 				conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
688 			} else if (named_pipe) {
689 				char *p;
690 				mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket.s);
691 				if (!p) {
692 					SET_OOM_ERROR(conn->error_info);
693 					goto err; /* OOM */
694 				}
695 				conn->host_info = mnd_pestrdup(p, conn->persistent);
696 				mnd_sprintf_free(p);
697 			} else {
698 				php_error_docref(NULL, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
699 			}
700 			if (!conn->unix_socket.s || !conn->host_info) {
701 				SET_OOM_ERROR(conn->error_info);
702 				goto err; /* OOM */
703 			}
704 			conn->unix_socket.l = strlen(conn->unix_socket.s);
705 		}
706 
707 		SET_EMPTY_ERROR(conn->error_info);
708 
709 		mysqlnd_local_infile_default(conn);
710 
711 		if (FAIL == conn->m->execute_init_commands(conn)) {
712 			goto err;
713 		}
714 
715 		MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
716 		if (reconnect) {
717 			MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
718 		}
719 		if (conn->persistent) {
720 			MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
721 		}
722 
723 		DBG_INF_FMT("connection_id=%" PRIu64, conn->thread_id);
724 
725 		DBG_RETURN(PASS);
726 	}
727 err:
728 	DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, transport.s ? transport.s : conn->scheme.s);
729 	if (!conn->error_info->error_no) {
730 		/* There was an unknown error if the connection failed but we have no error number */
731 		char * msg;
732 		mnd_sprintf(&msg, 0, "Unknown error while trying to connect via %s", transport.s ? transport.s : conn->scheme.s);
733 		SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, msg);
734 		mnd_sprintf_free(msg);
735 	}
736 
737 	if (transport.s) {
738 		mnd_sprintf_free(transport.s);
739 		transport.s = NULL;
740 	}
741 
742 	conn->m->free_contents(conn);
743 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
744 	DBG_RETURN(FAIL);
745 }
746 /* }}} */
747 
748 
749 /* {{{ mysqlnd_conn::connect */
750 static enum_func_status
751 MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
752 						const MYSQLND_CSTRING hostname,
753 						const MYSQLND_CSTRING username,
754 						const MYSQLND_CSTRING password,
755 						const MYSQLND_CSTRING database,
756 						unsigned int port,
757 						const MYSQLND_CSTRING socket_or_pipe,
758 						unsigned int mysql_flags)
759 {
760 	MYSQLND_CONN_DATA * conn = conn_handle->data;
761 
762 	DBG_ENTER("mysqlnd_conn::connect");
763 
764 	mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
765 	if (hostname.l > 0) {
766 		mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
767 	}
768 	DBG_RETURN(conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags));
769 }
770 /* }}} */
771 
772 
773 /* {{{ mysqlnd_conn_data::query */
774 /*
775   If conn->error_info->error_no is not zero, then we had an error.
776   Still the result from the query is PASS
777 */
778 static enum_func_status
779 MYSQLND_METHOD(mysqlnd_conn_data, query)(MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len)
780 {
781 	enum_func_status ret = FAIL;
782 	DBG_ENTER("mysqlnd_conn_data::query");
783 	DBG_INF_FMT("conn=%p conn=%" PRIu64 " query=%s", conn, conn->thread_id, query);
784 
785 	if (PASS == conn->m->send_query(conn, query, query_len, NULL, NULL) &&
786 		PASS == conn->m->reap_query(conn))
787 	{
788 		ret = PASS;
789 		if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
790 			MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
791 		}
792 	}
793 	DBG_RETURN(ret);
794 }
795 /* }}} */
796 
797 
798 /* {{{ mysqlnd_conn_data::send_query */
799 static enum_func_status
800 MYSQLND_METHOD(mysqlnd_conn_data, send_query)(
801 		MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len,
802 		zval *read_cb, zval *err_cb)
803 {
804 	DBG_ENTER("mysqlnd_conn_data::send_query");
805 	DBG_INF_FMT("conn=%" PRIu64 " query=%s", conn->thread_id, query);
806 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
807 
808 	const MYSQLND_CSTRING query_string = {query, query_len};
809 	enum_func_status ret = conn->command->query(conn, query_string);
810 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
811 	DBG_RETURN(ret);
812 }
813 /* }}} */
814 
815 
816 /* {{{ mysqlnd_conn_data::reap_query */
817 static enum_func_status
818 MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn)
819 {
820 	DBG_ENTER("mysqlnd_conn_data::reap_query");
821 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
822 
823 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
824 	enum_func_status ret = conn->command->reap_result(conn);
825 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
826 	DBG_RETURN(ret);
827 }
828 /* }}} */
829 
830 
831 /* {{{ mysqlnd_conn_data::list_method */
832 MYSQLND_RES *
833 MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * const query, const char * const achtung_wild, const char * const par1)
834 {
835 	char * show_query = NULL;
836 	size_t show_query_len;
837 	MYSQLND_RES * result = NULL;
838 
839 	DBG_ENTER("mysqlnd_conn_data::list_method");
840 	DBG_INF_FMT("conn=%" PRIu64 " query=%s wild=%p", conn->thread_id, query, achtung_wild);
841 
842 	if (par1) {
843 		if (achtung_wild) {
844 			show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
845 		} else {
846 			show_query_len = mnd_sprintf(&show_query, 0, query, par1);
847 		}
848 	} else {
849 		if (achtung_wild) {
850 			show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
851 		} else {
852 			show_query_len = strlen(show_query = (char *)query);
853 		}
854 	}
855 
856 	if (PASS == conn->m->query(conn, show_query, show_query_len)) {
857 		result = conn->m->store_result(conn);
858 	}
859 	if (show_query != query) {
860 		mnd_sprintf_free(show_query);
861 	}
862 	DBG_RETURN(result);
863 }
864 /* }}} */
865 
866 
867 /* {{{ mysqlnd_conn_data::err_no */
868 static unsigned int
869 MYSQLND_METHOD(mysqlnd_conn_data, err_no)(const MYSQLND_CONN_DATA * const conn)
870 {
871 	return conn->error_info->error_no;
872 }
873 /* }}} */
874 
875 
876 /* {{{ mysqlnd_conn_data::error */
877 static const char *
878 MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn)
879 {
880 	return conn->error_info->error;
881 }
882 /* }}} */
883 
884 
885 /* {{{ mysqlnd_conn_data::sqlstate */
886 static const char *
887 MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn)
888 {
889 	return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
890 }
891 /* }}} */
892 
893 
894 /* {{{ mysqlnd_old_escape_string */
895 PHPAPI  zend_ulong
896 mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len)
897 {
898 	DBG_ENTER("mysqlnd_old_escape_string");
899 	DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len));
900 }
901 /* }}} */
902 
903 
904 /* {{{ mysqlnd_conn_data::ssl_set */
905 static enum_func_status
906 MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
907 									  const char * const ca, const char * const capath, const char * const cipher)
908 {
909 	MYSQLND_VIO * vio = conn->vio;
910 	DBG_ENTER("mysqlnd_conn_data::ssl_set");
911 
912 	enum_func_status ret = (
913 		PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_KEY, key) &&
914 		PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CERT, cert) &&
915 		PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CA, ca) &&
916 		PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CAPATH, capath) &&
917 		PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CIPHER, cipher)) ? PASS : FAIL;
918 	DBG_RETURN(ret);
919 }
920 /* }}} */
921 
922 
923 /* {{{ mysqlnd_conn_data::escape_string */
924 static zend_ulong
925 MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len)
926 {
927 	zend_ulong ret = FAIL;
928 	DBG_ENTER("mysqlnd_conn_data::escape_string");
929 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
930 
931 	DBG_INF_FMT("server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
932 	if (UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
933 		ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len);
934 	} else {
935 		ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len);
936 	}
937 	DBG_RETURN(ret);
938 }
939 /* }}} */
940 
941 
942 /* {{{ mysqlnd_conn_data::dump_debug_info */
943 static enum_func_status
944 MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn)
945 {
946 	DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
947 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
948 	DBG_RETURN(conn->command->debug(conn));
949 }
950 /* }}} */
951 
952 
953 /* {{{ mysqlnd_conn_data::select_db */
954 static enum_func_status
955 MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, const size_t db_len)
956 {
957 	DBG_ENTER("mysqlnd_conn_data::select_db");
958 	DBG_INF_FMT("conn=%" PRIu64 " db=%s", conn->thread_id, db);
959 
960 	const MYSQLND_CSTRING database = {db, db_len};
961 	DBG_RETURN(conn->command->init_db(conn, database));
962 }
963 /* }}} */
964 
965 
966 /* {{{ mysqlnd_conn_data::ping */
967 static enum_func_status
968 MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn)
969 {
970 	DBG_ENTER("mysqlnd_conn_data::ping");
971 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
972 
973 	enum_func_status ret = conn->command->ping(conn);
974 	DBG_INF_FMT("ret=%u", ret);
975 	DBG_RETURN(ret);
976 }
977 /* }}} */
978 
979 
980 /* {{{ mysqlnd_conn_data::statistic */
981 static enum_func_status
982 MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, zend_string **message)
983 {
984 	DBG_ENTER("mysqlnd_conn_data::statistic");
985 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
986 	DBG_RETURN(conn->command->statistics(conn, message));
987 }
988 /* }}} */
989 
990 
991 /* {{{ mysqlnd_conn_data::kill */
992 static enum_func_status
993 MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid)
994 {
995 	DBG_ENTER("mysqlnd_conn_data::kill");
996 	DBG_INF_FMT("conn=%" PRIu64 " pid=%u", conn->thread_id, pid);
997 
998 	const unsigned int process_id = pid;
999 	/* 'unsigned char' is promoted to 'int' when passed through '...' */
1000 	const unsigned int read_response = (pid != conn->thread_id);
1001 
1002 	DBG_RETURN(conn->command->process_kill(conn, process_id, read_response));
1003 }
1004 /* }}} */
1005 
1006 
1007 /* {{{ mysqlnd_conn_data::set_charset */
1008 static enum_func_status
1009 MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname)
1010 {
1011 	enum_func_status ret = FAIL;
1012 	const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
1013 
1014 	DBG_ENTER("mysqlnd_conn_data::set_charset");
1015 	DBG_INF_FMT("conn=%" PRIu64 " cs=%s", conn->thread_id, csname);
1016 
1017 	if (!charset) {
1018 		SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Invalid character set was provided");
1019 		DBG_RETURN(ret);
1020 	}
1021 
1022 	char * query;
1023 	size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
1024 
1025 	if (FAIL == (ret = conn->m->query(conn, query, query_len)) || conn->error_info->error_no) {
1026 		ret = FAIL;
1027 	} else {
1028 		conn->charset = charset;
1029 	}
1030 	mnd_sprintf_free(query);
1031 
1032 	DBG_INF(ret == PASS? "PASS":"FAIL");
1033 	DBG_RETURN(ret);
1034 }
1035 /* }}} */
1036 
1037 
1038 /* {{{ mysqlnd_conn_data::refresh */
1039 static enum_func_status
1040 MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options)
1041 {
1042 	DBG_ENTER("mysqlnd_conn_data::refresh");
1043 	DBG_INF_FMT("conn=%" PRIu64 " options=%u", conn->thread_id, options);
1044 	DBG_RETURN(conn->command->refresh(conn, options));
1045 }
1046 /* }}} */
1047 
1048 
1049 /* {{{ mysqlnd_conn_data::shutdown */
1050 static enum_func_status
1051 MYSQLND_METHOD(mysqlnd_conn_data, shutdown)(MYSQLND_CONN_DATA * const conn, uint8_t level)
1052 {
1053 	DBG_ENTER("mysqlnd_conn_data::shutdown");
1054 	DBG_INF_FMT("conn=%" PRIu64 " level=%u", conn->thread_id, level);
1055 	DBG_RETURN(conn->command->shutdown(conn, level));
1056 }
1057 /* }}} */
1058 
1059 
1060 /* {{{ mysqlnd_send_close */
1061 static enum_func_status
1062 MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn)
1063 {
1064 	enum_func_status ret = PASS;
1065 	MYSQLND_VIO * vio = conn->vio;
1066 	php_stream * net_stream = vio->data->m.get_stream(vio);
1067 	enum mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
1068 
1069 	DBG_ENTER("mysqlnd_send_close");
1070 	DBG_INF_FMT("conn=%" PRIu64 " vio->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
1071 	DBG_INF_FMT("state=%u", state);
1072 
1073 	if (state >= CONN_READY) {
1074 		MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_CONNECTIONS);
1075 		if (conn->persistent) {
1076 			MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_PERSISTENT_CONNECTIONS);
1077 		}
1078 	}
1079 	switch (state) {
1080 		case CONN_READY:
1081 			DBG_INF("Connection clean, sending COM_QUIT");
1082 			if (net_stream) {
1083 				ret = conn->command->quit(conn);
1084 				vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1085 			}
1086 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1087 			break;
1088 		case CONN_SENDING_LOAD_DATA:
1089 			/*
1090 			  Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
1091 			  will crash (assert) a debug server.
1092 			*/
1093 		case CONN_NEXT_RESULT_PENDING:
1094 		case CONN_QUERY_SENT:
1095 		case CONN_FETCHING_DATA:
1096 			MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
1097 			DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme.s);
1098 			/*
1099 			  Do nothing, the connection will be brutally closed
1100 			  and the server will catch it and free close from its side.
1101 			*/
1102 			ZEND_FALLTHROUGH;
1103 		case CONN_ALLOCED:
1104 			/*
1105 			  Allocated but not connected or there was failure when trying
1106 			  to connect with pre-allocated connect.
1107 
1108 			  Fall-through
1109 			*/
1110 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1111 			ZEND_FALLTHROUGH;
1112 		case CONN_QUIT_SENT:
1113 			/* The user has killed its own connection */
1114 			vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1115 			break;
1116 	}
1117 
1118 	DBG_RETURN(ret);
1119 }
1120 /* }}} */
1121 
1122 
1123 /* {{{ mysqlnd_conn_data::get_reference */
1124 static MYSQLND_CONN_DATA *
1125 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn)
1126 {
1127 	DBG_ENTER("mysqlnd_conn_data::get_reference");
1128 	++conn->refcount;
1129 	DBG_INF_FMT("conn=%" PRIu64 " new_refcount=%u", conn->thread_id, conn->refcount);
1130 	DBG_RETURN(conn);
1131 }
1132 /* }}} */
1133 
1134 
1135 /* {{{ mysqlnd_conn_data::free_reference */
1136 static enum_func_status
1137 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn)
1138 {
1139 	enum_func_status ret = PASS;
1140 	DBG_ENTER("mysqlnd_conn_data::free_reference");
1141 	DBG_INF_FMT("conn=%" PRIu64 " old_refcount=%u", conn->thread_id, conn->refcount);
1142 	if (!(--conn->refcount)) {
1143 		/*
1144 		  No multithreading issues as we don't share the connection :)
1145 		  This will free the object too, of course because references has
1146 		  reached zero.
1147 		*/
1148 		ret = conn->m->send_close(conn);
1149 		conn->m->dtor(conn);
1150 	}
1151 	DBG_RETURN(ret);
1152 }
1153 /* }}} */
1154 
1155 
1156 /* {{{ mysqlnd_conn_data::field_count */
1157 static unsigned int
1158 MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn)
1159 {
1160 	return conn->field_count;
1161 }
1162 /* }}} */
1163 
1164 
1165 /* {{{ mysqlnd_conn_data::server_status */
1166 static unsigned int
1167 MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn)
1168 {
1169 	return UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status);
1170 }
1171 /* }}} */
1172 
1173 
1174 /* {{{ mysqlnd_conn_data::insert_id */
1175 static uint64_t
1176 MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn)
1177 {
1178 	return UPSERT_STATUS_GET_LAST_INSERT_ID(conn->upsert_status);
1179 }
1180 /* }}} */
1181 
1182 
1183 /* {{{ mysqlnd_conn_data::affected_rows */
1184 static uint64_t
1185 MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn)
1186 {
1187 	return UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status);
1188 }
1189 /* }}} */
1190 
1191 
1192 /* {{{ mysqlnd_conn_data::warning_count */
1193 static unsigned int
1194 MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn)
1195 {
1196 	return UPSERT_STATUS_GET_WARNINGS(conn->upsert_status);
1197 }
1198 /* }}} */
1199 
1200 
1201 /* {{{ mysqlnd_conn_data::info */
1202 static const char *
1203 MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn)
1204 {
1205 	return conn->last_message.s;
1206 }
1207 /* }}} */
1208 
1209 
1210 /* {{{ mysqlnd_get_client_info */
1211 PHPAPI const char * mysqlnd_get_client_info(void)
1212 {
1213 	return PHP_MYSQLND_VERSION;
1214 }
1215 /* }}} */
1216 
1217 
1218 /* {{{ mysqlnd_get_client_version */
1219 PHPAPI unsigned long mysqlnd_get_client_version(void)
1220 {
1221 	return MYSQLND_VERSION_ID;
1222 }
1223 /* }}} */
1224 
1225 
1226 /* {{{ mysqlnd_conn_data::get_server_info */
1227 static const char *
1228 MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn)
1229 {
1230 	return conn->server_version;
1231 }
1232 /* }}} */
1233 
1234 
1235 /* {{{ mysqlnd_conn_data::get_host_info */
1236 static const char *
1237 MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn)
1238 {
1239 	return conn->host_info;
1240 }
1241 /* }}} */
1242 
1243 
1244 /* {{{ mysqlnd_conn_data::get_proto_info */
1245 static unsigned int
1246 MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn)
1247 {
1248 	return conn->protocol_version;
1249 }
1250 /* }}} */
1251 
1252 
1253 /* {{{ mysqlnd_conn_data::charset_name */
1254 static const char *
1255 MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn)
1256 {
1257 	return conn->charset->name;
1258 }
1259 /* }}} */
1260 
1261 
1262 /* {{{ mysqlnd_conn_data::thread_id */
1263 static uint64_t
1264 MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn)
1265 {
1266 	return conn->thread_id;
1267 }
1268 /* }}} */
1269 
1270 
1271 /* {{{ mysqlnd_conn_data::get_server_version */
1272 static zend_ulong
1273 MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn)
1274 {
1275 	zend_long major, minor, patch;
1276 	char *p;
1277 
1278 	if (!(p = conn->server_version)) {
1279 		return 0;
1280 	}
1281 
1282 	major = ZEND_STRTOL(p, &p, 10);
1283 	p += 1; /* consume the dot */
1284 	minor = ZEND_STRTOL(p, &p, 10);
1285 	p += 1; /* consume the dot */
1286 	patch = ZEND_STRTOL(p, &p, 10);
1287 
1288 	return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
1289 }
1290 /* }}} */
1291 
1292 
1293 /* {{{ mysqlnd_conn_data::more_results */
1294 static bool
1295 MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn)
1296 {
1297 	DBG_ENTER("mysqlnd_conn_data::more_results");
1298 	/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
1299 	DBG_RETURN(UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
1300 }
1301 /* }}} */
1302 
1303 
1304 /* {{{ mysqlnd_conn_data::next_result */
1305 static enum_func_status
1306 MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn)
1307 {
1308 	DBG_ENTER("mysqlnd_conn_data::next_result");
1309 	DBG_INF_FMT("conn=%" PRIu64 "", conn->thread_id);
1310 
1311 	SET_EMPTY_ERROR(conn->error_info);
1312 
1313 	if (GET_CONNECTION_STATE(&conn->state) != CONN_NEXT_RESULT_PENDING) {
1314 		DBG_RETURN(FAIL);
1315 	}
1316 
1317 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1318 	/*
1319 	  We are sure that there is a result set, since conn->state is set accordingly
1320 	  in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
1321 	*/
1322 	enum_func_status ret = conn->m->query_read_result_set_header(conn, NULL);
1323 	if (FAIL == ret) {
1324 		/*
1325 		  There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
1326 		  So there are no more results and we should just return FALSE, error_no has been set
1327 		*/
1328 		if (!conn->error_info->error_no) {
1329 			DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
1330 			php_error_docref(NULL, E_WARNING, "Serious error. PID=%d", getpid());
1331 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1332 			conn->m->send_close(conn);
1333 		} else {
1334 			DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
1335 		}
1336 		DBG_RETURN(FAIL);
1337 	}
1338 	if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
1339 		MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
1340 	}
1341 	DBG_RETURN(PASS);
1342 }
1343 /* }}} */
1344 
1345 
1346 /* {{{ mysqlnd_conn_data::change_user */
1347 static enum_func_status
1348 MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
1349 											   const char * user,
1350 											   const char * passwd,
1351 											   const char * db,
1352 											   bool silent,
1353 											   size_t passwd_len
1354 			)
1355 {
1356 	enum_func_status ret = FAIL;
1357 
1358 	DBG_ENTER("mysqlnd_conn_data::change_user");
1359 	DBG_INF_FMT("conn=%" PRIu64 " user=%s passwd=%s db=%s silent=%u",
1360 				conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
1361 
1362 	SET_EMPTY_ERROR(conn->error_info);
1363 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1364 
1365 	if (!user) {
1366 		user = "";
1367 	}
1368 	if (!passwd) {
1369 		passwd = "";
1370 		passwd_len = 0;
1371 	}
1372 	if (!db) {
1373 		db = "";
1374 	}
1375 
1376 	/* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
1377 	ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
1378 									 conn->authentication_plugin_data, conn->options->auth_protocol,
1379 									0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/);
1380 
1381 	/*
1382 	  Here we should close all statements. Unbuffered queries should not be a
1383 	  problem as we won't allow sending COM_CHANGE_USER.
1384 	*/
1385 	DBG_INF(ret == PASS? "PASS":"FAIL");
1386 	DBG_RETURN(ret);
1387 }
1388 /* }}} */
1389 
1390 
1391 /* {{{ mysqlnd_conn_data::set_client_option */
1392 static enum_func_status
1393 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
1394 												enum_mysqlnd_client_option option,
1395 												const char * const value
1396 												)
1397 {
1398 	enum_func_status ret = PASS;
1399 	DBG_ENTER("mysqlnd_conn_data::set_client_option");
1400 	DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1401 
1402 	switch (option) {
1403 		case MYSQL_OPT_READ_TIMEOUT:
1404 		case MYSQL_OPT_WRITE_TIMEOUT:
1405 		case MYSQLND_OPT_SSL_KEY:
1406 		case MYSQLND_OPT_SSL_CERT:
1407 		case MYSQLND_OPT_SSL_CA:
1408 		case MYSQLND_OPT_SSL_CAPATH:
1409 		case MYSQLND_OPT_SSL_CIPHER:
1410 		case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
1411 		case MYSQL_OPT_CONNECT_TIMEOUT:
1412 		case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
1413 			ret = conn->vio->data->m.set_client_option(conn->vio, option, value);
1414 			break;
1415 		case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
1416 		case MYSQL_OPT_COMPRESS:
1417 		case MYSQL_SERVER_PUBLIC_KEY:
1418 			ret = conn->protocol_frame_codec->data->m.set_client_option(conn->protocol_frame_codec, option, value);
1419 			break;
1420 		case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
1421 			conn->options->int_and_float_native = *(unsigned int*) value;
1422 			break;
1423 		case MYSQL_OPT_LOCAL_INFILE:
1424 			if (value && (*(unsigned int*) value) ? 1 : 0) {
1425 				conn->options->flags |= CLIENT_LOCAL_FILES;
1426 			} else {
1427 				conn->options->flags &= ~CLIENT_LOCAL_FILES;
1428 			}
1429 			break;
1430 		case MYSQL_OPT_LOAD_DATA_LOCAL_DIR:
1431 		{
1432 			if (conn->options->local_infile_directory) {
1433 				mnd_pefree(conn->options->local_infile_directory, conn->persistent);
1434 			}
1435 
1436 			if (!value || (*value == '\0')) {
1437 				conn->options->local_infile_directory = NULL;
1438 			} else {
1439 				conn->options->local_infile_directory = mnd_pestrdup(value, conn->persistent);
1440 			}
1441 			break;
1442 		}
1443 		case MYSQL_INIT_COMMAND:
1444 		{
1445 			char ** new_init_commands;
1446 			char * new_command;
1447 			/* when num_commands is 0, then realloc will be effectively a malloc call, internally */
1448 			/* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
1449 			new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
1450 			conn->options->init_commands = new_init_commands;
1451 			new_command = mnd_pestrdup(value, conn->persistent);
1452 			conn->options->init_commands[conn->options->num_commands] = new_command;
1453 			++conn->options->num_commands;
1454 			break;
1455 		}
1456 		case MYSQL_READ_DEFAULT_FILE:
1457 		case MYSQL_READ_DEFAULT_GROUP:
1458 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1459 		case MYSQL_SET_CLIENT_IP:
1460 		case MYSQL_REPORT_DATA_TRUNCATION:
1461 #endif
1462 			/* currently not supported. Todo!! */
1463 			break;
1464 		case MYSQL_SET_CHARSET_NAME:
1465 		{
1466 			char * new_charset_name;
1467 			if (!mysqlnd_find_charset_name(value)) {
1468 				SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
1469 				ret = FAIL;
1470 				break;
1471 			}
1472 
1473 			new_charset_name = mnd_pestrdup(value, conn->persistent);
1474 			if (conn->options->charset_name) {
1475 				mnd_pefree(conn->options->charset_name, conn->persistent);
1476 			}
1477 			conn->options->charset_name = new_charset_name;
1478 			DBG_INF_FMT("charset=%s", conn->options->charset_name);
1479 			break;
1480 		}
1481 		case MYSQL_OPT_NAMED_PIPE:
1482 			conn->options->protocol = MYSQL_PROTOCOL_PIPE;
1483 			break;
1484 		case MYSQL_OPT_PROTOCOL:
1485 			if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
1486 				conn->options->protocol = *(unsigned int*) value;
1487 			}
1488 			break;
1489 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1490 		case MYSQL_SET_CHARSET_DIR:
1491 		case MYSQL_OPT_RECONNECT:
1492 			/* we don't need external character sets, all character sets are
1493 			   compiled in. For compatibility we just ignore this setting.
1494 			   Same for protocol, we don't support old protocol */
1495 		case MYSQL_OPT_USE_REMOTE_CONNECTION:
1496 		case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
1497 		case MYSQL_OPT_GUESS_CONNECTION:
1498 			/* todo: throw an error, we don't support embedded */
1499 			break;
1500 #endif
1501 		case MYSQLND_OPT_MAX_ALLOWED_PACKET:
1502 			if (*(unsigned int*) value > (1<<16)) {
1503 				conn->options->max_allowed_packet = *(unsigned int*) value;
1504 			}
1505 			break;
1506 		case MYSQLND_OPT_AUTH_PROTOCOL:
1507 		{
1508 			char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
1509 			if (conn->options->auth_protocol) {
1510 				mnd_pefree(conn->options->auth_protocol, conn->persistent);
1511 			}
1512 			conn->options->auth_protocol = new_auth_protocol;
1513 			DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
1514 			break;
1515 		}
1516 		case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
1517 			if (value && (*(unsigned int*) value) ? 1 : 0) {
1518 				conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1519 			} else {
1520 				conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1521 			}
1522 			break;
1523 		case MYSQL_OPT_CONNECT_ATTR_RESET:
1524 			if (conn->options->connect_attr) {
1525 				DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1526 				zend_hash_clean(conn->options->connect_attr);
1527 			}
1528 			break;
1529 		case MYSQL_OPT_CONNECT_ATTR_DELETE:
1530 			if (conn->options->connect_attr && value) {
1531 				DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1532 				zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
1533 				DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
1534 			}
1535 			break;
1536 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1537 		case MYSQL_SHARED_MEMORY_BASE_NAME:
1538 		case MYSQL_OPT_USE_RESULT:
1539 		case MYSQL_SECURE_AUTH:
1540 			/* not sure, todo ? */
1541 #endif
1542 		default:
1543 			ret = FAIL;
1544 	}
1545 	DBG_RETURN(ret);
1546 }
1547 /* }}} */
1548 
1549 
1550 /* {{{ mysqlnd_conn_data::set_client_option_2d */
1551 static enum_func_status
1552 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
1553 														const enum_mysqlnd_client_option option,
1554 														const char * const key,
1555 														const char * const value
1556 														)
1557 {
1558 	enum_func_status ret = PASS;
1559 	DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
1560 	DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1561 
1562 	switch (option) {
1563 		case MYSQL_OPT_CONNECT_ATTR_ADD:
1564 			if (!conn->options->connect_attr) {
1565 				DBG_INF("Initializing connect_attr hash");
1566 				conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
1567 				zend_hash_init(conn->options->connect_attr, 0, NULL, conn->persistent ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR, conn->persistent);
1568 			}
1569 			DBG_INF_FMT("Adding [%s][%s]", key, value);
1570 			{
1571 				zval attrz;
1572 				zend_string *str;
1573 
1574 				if (conn->persistent) {
1575 					str = zend_string_init(key, strlen(key), 1);
1576 					GC_MAKE_PERSISTENT_LOCAL(str);
1577 					ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
1578 					GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(attrz));
1579 				} else {
1580 					str = zend_string_init(key, strlen(key), 0);
1581 					ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 0));
1582 				}
1583 				zend_hash_update(conn->options->connect_attr, str, &attrz);
1584 				zend_string_release_ex(str, 1);
1585 			}
1586 			break;
1587 		default:
1588 			ret = FAIL;
1589 	}
1590 	DBG_RETURN(ret);
1591 }
1592 /* }}} */
1593 
1594 
1595 /* {{{ mysqlnd_conn_data::use_result */
1596 static MYSQLND_RES *
1597 MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn)
1598 {
1599 	DBG_ENTER("mysqlnd_conn_data::use_result");
1600 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
1601 
1602 	if (!conn->current_result) {
1603 		DBG_RETURN(NULL);
1604 	}
1605 
1606 	/* Nothing to store for UPSERT/LOAD DATA */
1607 	if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1608 		SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1609 		DBG_ERR("Command out of sync");
1610 		DBG_RETURN(NULL);
1611 	}
1612 
1613 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
1614 
1615 	conn->current_result->conn = conn->m->get_reference(conn);
1616 
1617 	MYSQLND_RES *result = conn->current_result->m.use_result(conn->current_result, FALSE);
1618 	if (!result) {
1619 		conn->current_result->m.free_result(conn->current_result, TRUE);
1620 	}
1621 	conn->current_result = NULL;
1622 	DBG_RETURN(result);
1623 }
1624 /* }}} */
1625 
1626 
1627 /* {{{ mysqlnd_conn_data::store_result */
1628 static MYSQLND_RES *
1629 MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn)
1630 {
1631 	DBG_ENTER("mysqlnd_conn_data::store_result");
1632 	DBG_INF_FMT("conn=%" PRIu64 " conn=%p", conn->thread_id, conn);
1633 
1634 	if (!conn->current_result) {
1635 		DBG_RETURN(NULL);
1636 	}
1637 
1638 	/* Nothing to store for UPSERT/LOAD DATA*/
1639 	if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1640 		SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1641 		DBG_ERR("Command out of sync");
1642 		DBG_RETURN(NULL);
1643 	}
1644 
1645 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
1646 	MYSQLND_RES *result = conn->current_result->m.store_result(conn->current_result, conn, NULL);
1647 	if (!result) {
1648 		conn->current_result->m.free_result(conn->current_result, TRUE);
1649 	}
1650 	conn->current_result = NULL;
1651 	DBG_RETURN(result);
1652 }
1653 /* }}} */
1654 
1655 
1656 /* {{{ mysqlnd_conn_data::get_connection_stats */
1657 static void
1658 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
1659 														zval * return_value ZEND_FILE_LINE_DC)
1660 {
1661 	DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
1662 	mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
1663 	DBG_VOID_RETURN;
1664 }
1665 /* }}} */
1666 
1667 
1668 /* {{{ mysqlnd_conn_data::set_autocommit */
1669 static enum_func_status
1670 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
1671 {
1672 	DBG_ENTER("mysqlnd_conn_data::set_autocommit");
1673 	DBG_RETURN(conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1));
1674 }
1675 /* }}} */
1676 
1677 
1678 /* {{{ mysqlnd_conn_data::tx_commit */
1679 static enum_func_status
1680 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
1681 {
1682 	return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
1683 }
1684 /* }}} */
1685 
1686 
1687 /* {{{ mysqlnd_conn_data::tx_rollback */
1688 static enum_func_status
1689 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
1690 {
1691 	return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
1692 }
1693 /* }}} */
1694 
1695 
1696 /* {{{ mysqlnd_tx_cor_options_to_string */
1697 static void
1698 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
1699 {
1700 	if ((mode & TRANS_COR_AND_CHAIN) && !(mode & TRANS_COR_AND_NO_CHAIN)) {
1701 		if (str->s && ZSTR_LEN(str->s)) {
1702 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1703 		}
1704 		smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
1705 	} else if ((mode & TRANS_COR_AND_NO_CHAIN) && !(mode & TRANS_COR_AND_CHAIN)) {
1706 		if (str->s && ZSTR_LEN(str->s)) {
1707 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1708 		}
1709 		smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
1710 	}
1711 
1712 	if ((mode & TRANS_COR_RELEASE) && !(mode & TRANS_COR_NO_RELEASE)) {
1713 		if (str->s && ZSTR_LEN(str->s)) {
1714 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1715 		}
1716 		smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
1717 	} else if ((mode & TRANS_COR_NO_RELEASE) && !(mode & TRANS_COR_RELEASE)) {
1718 		if (str->s && ZSTR_LEN(str->s)) {
1719 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1720 		}
1721 		smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
1722 	}
1723 	smart_str_0(str);
1724 }
1725 /* }}} */
1726 
1727 
1728 /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
1729 static char *
1730 mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
1731 {
1732 	char * ret = NULL;
1733 	DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
1734 	if (name) {
1735 		bool warned = FALSE;
1736 		const char * p_orig = name;
1737 		char * p_copy;
1738 		p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
1739 		*p_copy++ = ' ';
1740 		*p_copy++ = '/';
1741 		*p_copy++ = '*';
1742 		while (1) {
1743 			register char v = *p_orig;
1744 			if (v == 0) {
1745 				break;
1746 			}
1747 			if ((v >= '0' && v <= '9') ||
1748 				(v >= 'a' && v <= 'z') ||
1749 				(v >= 'A' && v <= 'Z') ||
1750 				v == '-' ||
1751 				v == '_' ||
1752 				v == ' ' ||
1753 				v == '=')
1754 			{
1755 				*p_copy++ = v;
1756 			} else if (warned == FALSE) {
1757 				php_error_docref(NULL, E_WARNING, "Transaction name has been truncated, since it can only contain the A-Z, a-z, 0-9, \"\\\", \"-\", \"_\", and \"=\" characters");
1758 				warned = TRUE;
1759 			}
1760 			++p_orig;
1761 		}
1762 		*p_copy++ = '*';
1763 		*p_copy++ = '/';
1764 		*p_copy++ = 0;
1765 	}
1766 	DBG_RETURN(ret);
1767 }
1768 /* }}} */
1769 
1770 
1771 /* {{{ mysqlnd_conn_data::tx_commit_ex */
1772 static enum_func_status
1773 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const bool commit, const unsigned int flags, const char * const name)
1774 {
1775 	DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
1776 
1777 	smart_str tmp_str = {0, 0};
1778 	conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
1779 	smart_str_0(&tmp_str);
1780 
1781 	char * query;
1782 	size_t query_len;
1783 	char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1784 
1785 	query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
1786 							name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1787 	smart_str_free(&tmp_str);
1788 	if (name_esc) {
1789 		mnd_efree(name_esc);
1790 		name_esc = NULL;
1791 	}
1792 	if (!query) {
1793 		SET_OOM_ERROR(conn->error_info);
1794 		DBG_RETURN(FAIL);
1795 	}
1796 
1797 	enum_func_status ret = conn->m->query(conn, query, query_len);
1798 	mnd_sprintf_free(query);
1799 	DBG_RETURN(ret);
1800 }
1801 /* }}} */
1802 
1803 
1804 /* {{{ mysqlnd_conn_data::tx_begin */
1805 static enum_func_status
1806 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
1807 {
1808 	DBG_ENTER("mysqlnd_conn_data::tx_begin");
1809 
1810 	smart_str tmp_str = {0, 0};
1811 	if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
1812 		if (tmp_str.s) {
1813 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1814 		}
1815 		smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
1816 	}
1817 	if (mode & TRANS_START_READ_WRITE) {
1818 		if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1819 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1820 		}
1821 		smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
1822 	} else if (mode & TRANS_START_READ_ONLY) {
1823 		if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1824 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1825 		}
1826 		smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
1827 	}
1828 	smart_str_0(&tmp_str);
1829 
1830 	char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1831 	char * query;
1832 	unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1833 	smart_str_free(&tmp_str);
1834 	if (name_esc) {
1835 		mnd_efree(name_esc);
1836 		name_esc = NULL;
1837 	}
1838 	if (!query) {
1839 		SET_OOM_ERROR(conn->error_info);
1840 		DBG_RETURN(FAIL);
1841 	}
1842 
1843 	enum_func_status ret = conn->m->query(conn, query, query_len);
1844 	mnd_sprintf_free(query);
1845 	if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) && mysqlnd_stmt_errno(conn) == 1064) {
1846 		SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
1847 			"This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
1848 	}
1849 
1850 	DBG_RETURN(ret);
1851 }
1852 /* }}} */
1853 
1854 
1855 /* {{{ mysqlnd_conn_data::tx_savepoint */
1856 static enum_func_status
1857 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
1858 {
1859 	DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
1860 
1861 	if (!name) {
1862 		SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1863 		DBG_RETURN(FAIL);
1864 	}
1865 
1866 	char *query;
1867 	size_t query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
1868 	if (!query) {
1869 		SET_OOM_ERROR(conn->error_info);
1870 		DBG_RETURN(FAIL);
1871 	}
1872 
1873 	enum_func_status ret = conn->m->query(conn, query, query_len);
1874 	mnd_sprintf_free(query);
1875 	DBG_RETURN(ret);
1876 }
1877 /* }}} */
1878 
1879 
1880 /* {{{ mysqlnd_conn_data::tx_savepoint_release */
1881 static enum_func_status
1882 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
1883 {
1884 	DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
1885 
1886 	if (!name) {
1887 		SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1888 		DBG_RETURN(FAIL);
1889 	}
1890 
1891 	char *query;
1892 	size_t query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
1893 	if (!query) {
1894 		SET_OOM_ERROR(conn->error_info);
1895 		DBG_RETURN(FAIL);
1896 	}
1897 
1898 	enum_func_status ret = conn->m->query(conn, query, query_len);
1899 	mnd_sprintf_free(query);
1900 	DBG_RETURN(ret);
1901 }
1902 /* }}} */
1903 
1904 
1905 /* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
1906 static size_t
1907 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const size_t flags)
1908 {
1909 	unsigned int ret = 0;
1910 	DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
1911 	if (conn) {
1912 		ret = conn->client_api_capabilities;
1913 		conn->client_api_capabilities = flags;
1914 	}
1915 
1916 	DBG_RETURN(ret);
1917 }
1918 /* }}} */
1919 
1920 
1921 /* {{{ mysqlnd_conn_data::get_client_api_capabilities */
1922 static size_t
1923 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
1924 {
1925 	DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
1926 	DBG_RETURN(conn? conn->client_api_capabilities : 0);
1927 }
1928 /* }}} */
1929 
1930 
1931 /* {{{ _mysqlnd_stmt_init */
1932 MYSQLND_STMT *
1933 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init)(MYSQLND_CONN_DATA * const conn)
1934 {
1935 	MYSQLND_STMT * ret;
1936 	DBG_ENTER("mysqlnd_conn_data::stmt_init");
1937 	ret = conn->object_factory.get_prepared_statement(conn);
1938 	DBG_RETURN(ret);
1939 }
1940 /* }}} */
1941 
1942 
1943 MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
1944 	MYSQLND_METHOD(mysqlnd_conn_data, connect),
1945 
1946 	MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
1947 	MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
1948 	MYSQLND_METHOD(mysqlnd_conn_data, query),
1949 	MYSQLND_METHOD(mysqlnd_conn_data, send_query),
1950 	MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
1951 	MYSQLND_METHOD(mysqlnd_conn_data, use_result),
1952 	MYSQLND_METHOD(mysqlnd_conn_data, store_result),
1953 	MYSQLND_METHOD(mysqlnd_conn_data, next_result),
1954 	MYSQLND_METHOD(mysqlnd_conn_data, more_results),
1955 
1956 	MYSQLND_METHOD(mysqlnd_conn_data, stmt_init),
1957 
1958 	MYSQLND_METHOD(mysqlnd_conn_data, shutdown),
1959 	MYSQLND_METHOD(mysqlnd_conn_data, refresh),
1960 
1961 	MYSQLND_METHOD(mysqlnd_conn_data, ping),
1962 	MYSQLND_METHOD(mysqlnd_conn_data, kill),
1963 	MYSQLND_METHOD(mysqlnd_conn_data, select_db),
1964 	MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
1965 	MYSQLND_METHOD(mysqlnd_conn_data, change_user),
1966 
1967 	MYSQLND_METHOD(mysqlnd_conn_data, err_no),
1968 	MYSQLND_METHOD(mysqlnd_conn_data, error),
1969 	MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
1970 	MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
1971 
1972 	MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
1973 
1974 	MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
1975 	MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
1976 	MYSQLND_METHOD(mysqlnd_conn_data, statistic),
1977 	MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
1978 	MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
1979 	MYSQLND_METHOD(mysqlnd_conn_data, info),
1980 	MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
1981 	MYSQLND_METHOD(mysqlnd_conn_data, list_method),
1982 
1983 	MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
1984 	MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
1985 	MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
1986 	MYSQLND_METHOD(mysqlnd_conn_data, field_count),
1987 
1988 	MYSQLND_METHOD(mysqlnd_conn_data, server_status),
1989 
1990 	MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
1991 	MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
1992 	MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
1993 	MYSQLND_METHOD(mysqlnd_conn_data, free_options),
1994 
1995 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
1996 
1997 	mysqlnd_query_read_result_set_header,
1998 
1999 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
2000 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
2001 
2002 	MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
2003 	MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
2004 	MYSQLND_METHOD(mysqlnd_conn_data, send_close),
2005 
2006 	MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
2007 	mysqlnd_result_init,
2008 	MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
2009 	MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
2010 	MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
2011 	MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
2012 	MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
2013 	MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
2014 	MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
2015 	MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
2016 
2017 	MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
2018 	MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
2019 	MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
2020 	MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
2021 
2022 	MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
2023 
2024 	MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
2025 	MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities),
2026 
2027 	MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)
2028 MYSQLND_CLASS_METHODS_END;
2029 
2030 
2031 /* {{{ mysqlnd_conn::get_reference */
2032 static MYSQLND *
2033 MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
2034 {
2035 	MYSQLND * ret;
2036 	DBG_ENTER("mysqlnd_conn::get_reference");
2037 	ret = conn->data->object_factory.clone_connection_object(conn);
2038 	DBG_RETURN(ret);
2039 }
2040 /* }}} */
2041 
2042 
2043 /* {{{ mysqlnd_conn_data::dtor */
2044 static void
2045 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
2046 {
2047 	DBG_ENTER("mysqlnd_conn::dtor");
2048 	DBG_INF_FMT("conn=%" PRIu64, conn->data->thread_id);
2049 
2050 	conn->data->m->free_reference(conn->data);
2051 
2052 	mnd_pefree(conn, conn->persistent);
2053 
2054 	DBG_VOID_RETURN;
2055 }
2056 /* }}} */
2057 
2058 
2059 /* {{{ mysqlnd_conn_data::close */
2060 static enum_func_status
2061 MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, const enum_connection_close_type close_type)
2062 {
2063 	MYSQLND_CONN_DATA * conn = conn_handle->data;
2064 
2065 	DBG_ENTER("mysqlnd_conn::close");
2066 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
2067 
2068 	if (GET_CONNECTION_STATE(&conn->state) >= CONN_READY) {
2069 		static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
2070 			STAT_CLOSE_EXPLICIT,
2071 			STAT_CLOSE_IMPLICIT,
2072 			STAT_CLOSE_DISCONNECT
2073 		};
2074 		MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
2075 	}
2076 
2077 	/*
2078 	  Close now, free_reference will try,
2079 	  if we are last, but that's not a problem.
2080 	*/
2081 	enum_func_status ret = conn->m->send_close(conn);
2082 
2083 	conn_handle->m->dtor(conn_handle);
2084 	DBG_RETURN(ret);
2085 }
2086 /* }}} */
2087 
2088 
2089 MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
2090 	MYSQLND_METHOD(mysqlnd_conn, connect),
2091 	MYSQLND_METHOD(mysqlnd_conn, clone_object),
2092 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
2093 	MYSQLND_METHOD(mysqlnd_conn, close)
2094 MYSQLND_CLASS_METHODS_END;
2095 
2096 
2097 #include "php_network.h"
2098 
2099 /* {{{ mysqlnd_stream_array_to_fd_set */
2100 MYSQLND **
2101 mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
2102 {
2103 	unsigned int cnt = 0;
2104 	MYSQLND **p = conn_array, **p_p;
2105 	MYSQLND **ret = NULL;
2106 
2107 	while (*p) {
2108 		const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2109 		if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2110 			cnt++;
2111 		}
2112 		p++;
2113 	}
2114 	if (cnt) {
2115 		MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
2116 		p_p = p = conn_array;
2117 		while (*p) {
2118 			const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2119 			if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2120 				*ret_p = *p;
2121 				*p = NULL;
2122 				ret_p++;
2123 			} else {
2124 				*p_p = *p;
2125 				p_p++;
2126 			}
2127 			p++;
2128 		}
2129 		*ret_p = NULL;
2130 	}
2131 	return ret;
2132 }
2133 /* }}} */
2134 
2135 
2136 /* {{{ mysqlnd_stream_array_to_fd_set */
2137 static unsigned int
2138 mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
2139 {
2140 	php_socket_t this_fd;
2141 	php_stream *stream = NULL;
2142 	unsigned int cnt = 0;
2143 	MYSQLND **p = conn_array;
2144 	DBG_ENTER("mysqlnd_stream_array_to_fd_set");
2145 
2146 	while (*p) {
2147 		/* get the fd.
2148 		 * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
2149 		 * when casting.  It is only used here so that the buffered data warning
2150 		 * is not displayed.
2151 		 * */
2152 		stream = (*p)->data->vio->data->m.get_stream((*p)->data->vio);
2153 		DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*p)->data->thread_id, stream);
2154 		if (stream != NULL &&
2155 			SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) &&
2156 			ZEND_VALID_SOCKET(this_fd))
2157 		{
2158 
2159 			PHP_SAFE_FD_SET(this_fd, fds);
2160 
2161 			if (this_fd > *max_fd) {
2162 				*max_fd = this_fd;
2163 			}
2164 			++cnt;
2165 		}
2166 		++p;
2167 	}
2168 	DBG_RETURN(cnt ? 1 : 0);
2169 }
2170 /* }}} */
2171 
2172 
2173 /* {{{ mysqlnd_stream_array_from_fd_set */
2174 static unsigned int
2175 mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
2176 {
2177 	php_socket_t this_fd;
2178 	php_stream *stream = NULL;
2179 	unsigned int ret = 0;
2180 	bool disproportion = FALSE;
2181 	MYSQLND **fwd = conn_array, **bckwd = conn_array;
2182 	DBG_ENTER("mysqlnd_stream_array_from_fd_set");
2183 
2184 	while (*fwd) {
2185 		stream = (*fwd)->data->vio->data->m.get_stream((*fwd)->data->vio);
2186 		DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*fwd)->data->thread_id, stream);
2187 		if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
2188 										(void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
2189 			if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
2190 				if (disproportion) {
2191 					*bckwd = *fwd;
2192 				}
2193 				++bckwd;
2194 				++fwd;
2195 				++ret;
2196 				continue;
2197 			}
2198 		}
2199 		disproportion = TRUE;
2200 		++fwd;
2201 	}
2202 	*bckwd = NULL;/* NULL-terminate the list */
2203 
2204 	DBG_RETURN(ret);
2205 }
2206 /* }}} */
2207 
2208 
2209 #ifndef PHP_WIN32
2210 #define php_select(m, r, w, e, t)	select(m, r, w, e, t)
2211 #else
2212 #include "win32/select.h"
2213 #endif
2214 
2215 
2216 /* {{{ mysqlnd_poll */
2217 PHPAPI enum_func_status
2218 mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
2219 {
2220 	struct timeval	tv;
2221 	struct timeval *tv_p = NULL;
2222 	fd_set			rfds, wfds, efds;
2223 	php_socket_t	max_fd = 0;
2224 	int				retval, sets = 0;
2225 	int				set_count, max_set_count = 0;
2226 
2227 	DBG_ENTER("_mysqlnd_poll");
2228 	if (sec < 0 || usec < 0) {
2229 		php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
2230 		DBG_RETURN(FAIL);
2231 	}
2232 
2233 	FD_ZERO(&rfds);
2234 	FD_ZERO(&wfds);
2235 	FD_ZERO(&efds);
2236 
2237 	if (r_array != NULL) {
2238 		*dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
2239 		set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
2240 		if (set_count > max_set_count) {
2241 			max_set_count = set_count;
2242 		}
2243 		sets += set_count;
2244 	}
2245 
2246 	if (e_array != NULL) {
2247 		set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
2248 		if (set_count > max_set_count) {
2249 			max_set_count = set_count;
2250 		}
2251 		sets += set_count;
2252 	}
2253 
2254 	if (!sets) {
2255 		php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2256 		DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2257 		DBG_RETURN(FAIL);
2258 	}
2259 
2260 	if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
2261 		DBG_RETURN(FAIL);
2262 	}
2263 
2264 	/* Solaris + BSD do not like microsecond values which are >= 1 sec */
2265 	if (usec > 999999) {
2266 		tv.tv_sec = sec + (usec / 1000000);
2267 		tv.tv_usec = usec % 1000000;
2268 	} else {
2269 		tv.tv_sec = sec;
2270 		tv.tv_usec = usec;
2271 	}
2272 
2273 	tv_p = &tv;
2274 
2275 	retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
2276 
2277 	if (retval == -1) {
2278 		php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
2279 						errno, strerror(errno), max_fd);
2280 		DBG_RETURN(FAIL);
2281 	}
2282 
2283 	if (r_array != NULL) {
2284 		mysqlnd_stream_array_from_fd_set(r_array, &rfds);
2285 	}
2286 	if (e_array != NULL) {
2287 		mysqlnd_stream_array_from_fd_set(e_array, &efds);
2288 	}
2289 
2290 	*desc_num = retval;
2291 	DBG_RETURN(PASS);
2292 }
2293 /* }}} */
2294 
2295 
2296 /* {{{ mysqlnd_connect */
2297 PHPAPI MYSQLND * mysqlnd_connection_connect(MYSQLND * conn_handle,
2298 											const char * const host,
2299 											const char * const user,
2300 											const char * const passwd, unsigned int passwd_len,
2301 											const char * const db, unsigned int db_len,
2302 											unsigned int port,
2303 											const char * const sock_or_pipe,
2304 											unsigned int mysql_flags,
2305 											unsigned int client_api_flags
2306 						)
2307 {
2308 	enum_func_status ret = FAIL;
2309 	bool self_alloced = FALSE;
2310 	MYSQLND_CSTRING hostname = { host, host? strlen(host) : 0 };
2311 	MYSQLND_CSTRING username = { user, user? strlen(user) : 0 };
2312 	MYSQLND_CSTRING password = { passwd, passwd_len };
2313 	MYSQLND_CSTRING database = { db, db_len };
2314 	MYSQLND_CSTRING socket_or_pipe = { sock_or_pipe, sock_or_pipe? strlen(sock_or_pipe) : 0 };
2315 
2316 	DBG_ENTER("mysqlnd_connect");
2317 	DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host? host:"", user? user:"", db? db:"", port, mysql_flags);
2318 
2319 	if (!conn_handle) {
2320 		self_alloced = TRUE;
2321 		if (!(conn_handle = mysqlnd_connection_init(client_api_flags, FALSE, NULL))) {
2322 			/* OOM */
2323 			DBG_RETURN(NULL);
2324 		}
2325 	}
2326 
2327 	ret = conn_handle->m->connect(conn_handle, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
2328 
2329 	if (ret == FAIL) {
2330 		if (self_alloced) {
2331 			/*
2332 			  We have alloced, thus there are no references to this
2333 			  object - we are free to kill it!
2334 			*/
2335 			conn_handle->m->dtor(conn_handle);
2336 		}
2337 		DBG_RETURN(NULL);
2338 	}
2339 	DBG_RETURN(conn_handle);
2340 }
2341 /* }}} */
2342 
2343 
2344 /* {{{ mysqlnd_connection_init */
2345 PHPAPI MYSQLND *
2346 mysqlnd_connection_init(const size_t client_flags, const bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory)
2347 {
2348 	MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
2349 	MYSQLND * ret;
2350 	DBG_ENTER("mysqlnd_connection_init");
2351 	ret = factory->get_connection(factory, persistent);
2352 	if (ret && ret->data) {
2353 		ret->data->m->negotiate_client_api_capabilities(ret->data, client_flags);
2354 	}
2355 	DBG_RETURN(ret);
2356 }
2357 /* }}} */
2358