xref: /PHP-8.4/ext/mysqlnd/mysqlnd_connection.c (revision 2446500d)
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->persistent, (int)GET_CONNECTION_STATE(&conn->state));
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_send_close */
1050 static enum_func_status
1051 MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn)
1052 {
1053 	enum_func_status ret = PASS;
1054 	MYSQLND_VIO * vio = conn->vio;
1055 	php_stream * net_stream = vio->data->m.get_stream(vio);
1056 	enum mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
1057 
1058 	DBG_ENTER("mysqlnd_send_close");
1059 	DBG_INF_FMT("conn=%" PRIu64 " vio->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
1060 	DBG_INF_FMT("state=%u", state);
1061 
1062 	if (state >= CONN_READY) {
1063 		MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_CONNECTIONS);
1064 		if (conn->persistent) {
1065 			MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_PERSISTENT_CONNECTIONS);
1066 		}
1067 	}
1068 	switch (state) {
1069 		case CONN_READY:
1070 			DBG_INF("Connection clean, sending COM_QUIT");
1071 			if (net_stream) {
1072 				ret = conn->command->quit(conn);
1073 				vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1074 			}
1075 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1076 			break;
1077 		case CONN_SENDING_LOAD_DATA:
1078 			/*
1079 			  Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
1080 			  will crash (assert) a debug server.
1081 			*/
1082 		case CONN_NEXT_RESULT_PENDING:
1083 		case CONN_QUERY_SENT:
1084 		case CONN_FETCHING_DATA:
1085 			MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
1086 			DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme.s);
1087 			/*
1088 			  Do nothing, the connection will be brutally closed
1089 			  and the server will catch it and free close from its side.
1090 			*/
1091 			ZEND_FALLTHROUGH;
1092 		case CONN_ALLOCED:
1093 			/*
1094 			  Allocated but not connected or there was failure when trying
1095 			  to connect with pre-allocated connect.
1096 
1097 			  Fall-through
1098 			*/
1099 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1100 			ZEND_FALLTHROUGH;
1101 		case CONN_QUIT_SENT:
1102 			/* The user has killed its own connection */
1103 			vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1104 			break;
1105 	}
1106 
1107 	DBG_RETURN(ret);
1108 }
1109 /* }}} */
1110 
1111 
1112 /* {{{ mysqlnd_conn_data::get_reference */
1113 static MYSQLND_CONN_DATA *
1114 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn)
1115 {
1116 	DBG_ENTER("mysqlnd_conn_data::get_reference");
1117 	++conn->refcount;
1118 	DBG_INF_FMT("conn=%" PRIu64 " new_refcount=%u", conn->thread_id, conn->refcount);
1119 	DBG_RETURN(conn);
1120 }
1121 /* }}} */
1122 
1123 
1124 /* {{{ mysqlnd_conn_data::free_reference */
1125 static enum_func_status
1126 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn)
1127 {
1128 	enum_func_status ret = PASS;
1129 	DBG_ENTER("mysqlnd_conn_data::free_reference");
1130 	DBG_INF_FMT("conn=%" PRIu64 " old_refcount=%u", conn->thread_id, conn->refcount);
1131 	if (!(--conn->refcount)) {
1132 		/*
1133 		  No multithreading issues as we don't share the connection :)
1134 		  This will free the object too, of course because references has
1135 		  reached zero.
1136 		*/
1137 		ret = conn->m->send_close(conn);
1138 		conn->m->dtor(conn);
1139 	}
1140 	DBG_RETURN(ret);
1141 }
1142 /* }}} */
1143 
1144 
1145 /* {{{ mysqlnd_conn_data::field_count */
1146 static unsigned int
1147 MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn)
1148 {
1149 	return conn->field_count;
1150 }
1151 /* }}} */
1152 
1153 
1154 /* {{{ mysqlnd_conn_data::server_status */
1155 static unsigned int
1156 MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn)
1157 {
1158 	return UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status);
1159 }
1160 /* }}} */
1161 
1162 
1163 /* {{{ mysqlnd_conn_data::insert_id */
1164 static uint64_t
1165 MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn)
1166 {
1167 	return UPSERT_STATUS_GET_LAST_INSERT_ID(conn->upsert_status);
1168 }
1169 /* }}} */
1170 
1171 
1172 /* {{{ mysqlnd_conn_data::affected_rows */
1173 static uint64_t
1174 MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn)
1175 {
1176 	return UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status);
1177 }
1178 /* }}} */
1179 
1180 
1181 /* {{{ mysqlnd_conn_data::warning_count */
1182 static unsigned int
1183 MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn)
1184 {
1185 	return UPSERT_STATUS_GET_WARNINGS(conn->upsert_status);
1186 }
1187 /* }}} */
1188 
1189 
1190 /* {{{ mysqlnd_conn_data::info */
1191 static const char *
1192 MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn)
1193 {
1194 	return conn->last_message.s;
1195 }
1196 /* }}} */
1197 
1198 
1199 /* {{{ mysqlnd_get_client_info */
1200 PHPAPI const char * mysqlnd_get_client_info(void)
1201 {
1202 	return PHP_MYSQLND_VERSION;
1203 }
1204 /* }}} */
1205 
1206 
1207 /* {{{ mysqlnd_get_client_version */
1208 PHPAPI unsigned long mysqlnd_get_client_version(void)
1209 {
1210 	return MYSQLND_VERSION_ID;
1211 }
1212 /* }}} */
1213 
1214 
1215 /* {{{ mysqlnd_conn_data::get_server_info */
1216 static const char *
1217 MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn)
1218 {
1219 	return conn->server_version;
1220 }
1221 /* }}} */
1222 
1223 
1224 /* {{{ mysqlnd_conn_data::get_host_info */
1225 static const char *
1226 MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn)
1227 {
1228 	return conn->host_info;
1229 }
1230 /* }}} */
1231 
1232 
1233 /* {{{ mysqlnd_conn_data::get_proto_info */
1234 static unsigned int
1235 MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn)
1236 {
1237 	return conn->protocol_version;
1238 }
1239 /* }}} */
1240 
1241 
1242 /* {{{ mysqlnd_conn_data::charset_name */
1243 static const char *
1244 MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn)
1245 {
1246 	return conn->charset->name;
1247 }
1248 /* }}} */
1249 
1250 
1251 /* {{{ mysqlnd_conn_data::thread_id */
1252 static uint64_t
1253 MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn)
1254 {
1255 	return conn->thread_id;
1256 }
1257 /* }}} */
1258 
1259 
1260 /* {{{ mysqlnd_conn_data::get_server_version */
1261 static zend_ulong
1262 MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn)
1263 {
1264 	zend_long major, minor, patch;
1265 	char *p;
1266 
1267 	if (!(p = conn->server_version)) {
1268 		return 0;
1269 	}
1270 
1271 	major = ZEND_STRTOL(p, &p, 10);
1272 	p += 1; /* consume the dot */
1273 	minor = ZEND_STRTOL(p, &p, 10);
1274 	p += 1; /* consume the dot */
1275 	patch = ZEND_STRTOL(p, &p, 10);
1276 
1277 	return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
1278 }
1279 /* }}} */
1280 
1281 
1282 /* {{{ mysqlnd_conn_data::more_results */
1283 static bool
1284 MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn)
1285 {
1286 	DBG_ENTER("mysqlnd_conn_data::more_results");
1287 	/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
1288 	DBG_RETURN(UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
1289 }
1290 /* }}} */
1291 
1292 
1293 /* {{{ mysqlnd_conn_data::next_result */
1294 static enum_func_status
1295 MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn)
1296 {
1297 	DBG_ENTER("mysqlnd_conn_data::next_result");
1298 	DBG_INF_FMT("conn=%" PRIu64 "", conn->thread_id);
1299 
1300 	SET_EMPTY_ERROR(conn->error_info);
1301 
1302 	if (GET_CONNECTION_STATE(&conn->state) != CONN_NEXT_RESULT_PENDING) {
1303 		DBG_RETURN(FAIL);
1304 	}
1305 
1306 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1307 	/*
1308 	  We are sure that there is a result set, since conn->state is set accordingly
1309 	  in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
1310 	*/
1311 	enum_func_status ret = conn->m->query_read_result_set_header(conn, NULL);
1312 	if (FAIL == ret) {
1313 		/*
1314 		  There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
1315 		  So there are no more results and we should just return FALSE, error_no has been set
1316 		*/
1317 		if (!conn->error_info->error_no) {
1318 			DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
1319 			php_error_docref(NULL, E_WARNING, "Serious error. PID=%d", getpid());
1320 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1321 			conn->m->send_close(conn);
1322 		} else {
1323 			DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
1324 		}
1325 		DBG_RETURN(FAIL);
1326 	}
1327 	if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
1328 		MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
1329 	}
1330 	DBG_RETURN(PASS);
1331 }
1332 /* }}} */
1333 
1334 
1335 /* {{{ mysqlnd_conn_data::change_user */
1336 static enum_func_status
1337 MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
1338 											   const char * user,
1339 											   const char * passwd,
1340 											   const char * db,
1341 											   bool silent,
1342 											   size_t passwd_len
1343 			)
1344 {
1345 	enum_func_status ret = FAIL;
1346 
1347 	DBG_ENTER("mysqlnd_conn_data::change_user");
1348 	DBG_INF_FMT("conn=%" PRIu64 " user=%s passwd=%s db=%s silent=%u",
1349 				conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
1350 
1351 	SET_EMPTY_ERROR(conn->error_info);
1352 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1353 
1354 	if (!user) {
1355 		user = "";
1356 	}
1357 	if (!passwd) {
1358 		passwd = "";
1359 		passwd_len = 0;
1360 	}
1361 	if (!db) {
1362 		db = "";
1363 	}
1364 
1365 	/* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
1366 	ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
1367 									 conn->authentication_plugin_data, conn->options->auth_protocol,
1368 									0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/);
1369 
1370 	/*
1371 	  Here we should close all statements. Unbuffered queries should not be a
1372 	  problem as we won't allow sending COM_CHANGE_USER.
1373 	*/
1374 	DBG_INF(ret == PASS? "PASS":"FAIL");
1375 	DBG_RETURN(ret);
1376 }
1377 /* }}} */
1378 
1379 
1380 /* {{{ mysqlnd_conn_data::set_client_option */
1381 static enum_func_status
1382 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
1383 												enum_mysqlnd_client_option option,
1384 												const char * const value
1385 												)
1386 {
1387 	enum_func_status ret = PASS;
1388 	DBG_ENTER("mysqlnd_conn_data::set_client_option");
1389 	DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1390 
1391 	switch (option) {
1392 		case MYSQL_OPT_READ_TIMEOUT:
1393 		case MYSQL_OPT_WRITE_TIMEOUT:
1394 		case MYSQLND_OPT_SSL_KEY:
1395 		case MYSQLND_OPT_SSL_CERT:
1396 		case MYSQLND_OPT_SSL_CA:
1397 		case MYSQLND_OPT_SSL_CAPATH:
1398 		case MYSQLND_OPT_SSL_CIPHER:
1399 		case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
1400 		case MYSQL_OPT_CONNECT_TIMEOUT:
1401 		case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
1402 			ret = conn->vio->data->m.set_client_option(conn->vio, option, value);
1403 			break;
1404 		case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
1405 		case MYSQL_OPT_COMPRESS:
1406 		case MYSQL_SERVER_PUBLIC_KEY:
1407 			ret = conn->protocol_frame_codec->data->m.set_client_option(conn->protocol_frame_codec, option, value);
1408 			break;
1409 		case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
1410 			conn->options->int_and_float_native = *(unsigned int*) value;
1411 			break;
1412 		case MYSQL_OPT_LOCAL_INFILE:
1413 			if (value && (*(unsigned int*) value) ? 1 : 0) {
1414 				conn->options->flags |= CLIENT_LOCAL_FILES;
1415 			} else {
1416 				conn->options->flags &= ~CLIENT_LOCAL_FILES;
1417 			}
1418 			break;
1419 		case MYSQL_OPT_LOAD_DATA_LOCAL_DIR:
1420 		{
1421 			if (conn->options->local_infile_directory) {
1422 				mnd_pefree(conn->options->local_infile_directory, conn->persistent);
1423 			}
1424 
1425 			if (!value || (*value == '\0')) {
1426 				conn->options->local_infile_directory = NULL;
1427 			} else {
1428 				conn->options->local_infile_directory = mnd_pestrdup(value, conn->persistent);
1429 			}
1430 			break;
1431 		}
1432 		case MYSQL_INIT_COMMAND:
1433 		{
1434 			char ** new_init_commands;
1435 			char * new_command;
1436 			/* when num_commands is 0, then realloc will be effectively a malloc call, internally */
1437 			/* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
1438 			new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
1439 			conn->options->init_commands = new_init_commands;
1440 			new_command = mnd_pestrdup(value, conn->persistent);
1441 			conn->options->init_commands[conn->options->num_commands] = new_command;
1442 			++conn->options->num_commands;
1443 			break;
1444 		}
1445 		case MYSQL_READ_DEFAULT_FILE:
1446 		case MYSQL_READ_DEFAULT_GROUP:
1447 			/* currently not supported. Todo!! */
1448 			break;
1449 		case MYSQL_SET_CHARSET_NAME:
1450 		{
1451 			char * new_charset_name;
1452 			if (!mysqlnd_find_charset_name(value)) {
1453 				SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
1454 				ret = FAIL;
1455 				break;
1456 			}
1457 
1458 			new_charset_name = mnd_pestrdup(value, conn->persistent);
1459 			if (conn->options->charset_name) {
1460 				mnd_pefree(conn->options->charset_name, conn->persistent);
1461 			}
1462 			conn->options->charset_name = new_charset_name;
1463 			DBG_INF_FMT("charset=%s", conn->options->charset_name);
1464 			break;
1465 		}
1466 		case MYSQL_OPT_NAMED_PIPE:
1467 			conn->options->protocol = MYSQL_PROTOCOL_PIPE;
1468 			break;
1469 		case MYSQL_OPT_PROTOCOL:
1470 			if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
1471 				conn->options->protocol = *(unsigned int*) value;
1472 			}
1473 			break;
1474 		case MYSQLND_OPT_MAX_ALLOWED_PACKET:
1475 			if (*(unsigned int*) value > (1<<16)) {
1476 				conn->options->max_allowed_packet = *(unsigned int*) value;
1477 			}
1478 			break;
1479 		case MYSQLND_OPT_AUTH_PROTOCOL:
1480 		{
1481 			char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
1482 			if (conn->options->auth_protocol) {
1483 				mnd_pefree(conn->options->auth_protocol, conn->persistent);
1484 			}
1485 			conn->options->auth_protocol = new_auth_protocol;
1486 			DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
1487 			break;
1488 		}
1489 		case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
1490 			if (value && (*(unsigned int*) value) ? 1 : 0) {
1491 				conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1492 			} else {
1493 				conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1494 			}
1495 			break;
1496 		case MYSQL_OPT_CONNECT_ATTR_RESET:
1497 			if (conn->options->connect_attr) {
1498 				DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1499 				zend_hash_clean(conn->options->connect_attr);
1500 			}
1501 			break;
1502 		case MYSQL_OPT_CONNECT_ATTR_DELETE:
1503 			if (conn->options->connect_attr && value) {
1504 				DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1505 				zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
1506 				DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
1507 			}
1508 			break;
1509 		default:
1510 			ret = FAIL;
1511 	}
1512 	DBG_RETURN(ret);
1513 }
1514 /* }}} */
1515 
1516 
1517 /* {{{ mysqlnd_conn_data::set_client_option_2d */
1518 static enum_func_status
1519 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
1520 														const enum_mysqlnd_client_option option,
1521 														const char * const key,
1522 														const char * const value
1523 														)
1524 {
1525 	enum_func_status ret = PASS;
1526 	DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
1527 	DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1528 
1529 	switch (option) {
1530 		case MYSQL_OPT_CONNECT_ATTR_ADD:
1531 			if (!conn->options->connect_attr) {
1532 				DBG_INF("Initializing connect_attr hash");
1533 				conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
1534 				zend_hash_init(conn->options->connect_attr, 0, NULL, conn->persistent ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR, conn->persistent);
1535 			}
1536 			DBG_INF_FMT("Adding [%s][%s]", key, value);
1537 			{
1538 				zval attrz;
1539 				zend_string *str;
1540 
1541 				if (conn->persistent) {
1542 					str = zend_string_init(key, strlen(key), 1);
1543 					GC_MAKE_PERSISTENT_LOCAL(str);
1544 					ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
1545 					GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(attrz));
1546 				} else {
1547 					str = zend_string_init(key, strlen(key), 0);
1548 					ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 0));
1549 				}
1550 				zend_hash_update(conn->options->connect_attr, str, &attrz);
1551 				zend_string_release_ex(str, 1);
1552 			}
1553 			break;
1554 		default:
1555 			ret = FAIL;
1556 	}
1557 	DBG_RETURN(ret);
1558 }
1559 /* }}} */
1560 
1561 
1562 /* {{{ mysqlnd_conn_data::use_result */
1563 static MYSQLND_RES *
1564 MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn)
1565 {
1566 	DBG_ENTER("mysqlnd_conn_data::use_result");
1567 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
1568 
1569 	if (!conn->current_result) {
1570 		DBG_RETURN(NULL);
1571 	}
1572 
1573 	/* Nothing to store for UPSERT/LOAD DATA */
1574 	if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1575 		SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1576 		DBG_ERR("Command out of sync");
1577 		DBG_RETURN(NULL);
1578 	}
1579 
1580 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
1581 
1582 	conn->current_result->conn = conn->m->get_reference(conn);
1583 
1584 	MYSQLND_RES *result = conn->current_result->m.use_result(conn->current_result, FALSE);
1585 	if (!result) {
1586 		conn->current_result->m.free_result(conn->current_result, TRUE);
1587 	}
1588 	conn->current_result = NULL;
1589 	DBG_RETURN(result);
1590 }
1591 /* }}} */
1592 
1593 
1594 /* {{{ mysqlnd_conn_data::store_result */
1595 static MYSQLND_RES *
1596 MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn)
1597 {
1598 	DBG_ENTER("mysqlnd_conn_data::store_result");
1599 	DBG_INF_FMT("conn=%" PRIu64 " conn=%p", conn->thread_id, conn);
1600 
1601 	if (!conn->current_result) {
1602 		DBG_RETURN(NULL);
1603 	}
1604 
1605 	/* Nothing to store for UPSERT/LOAD DATA*/
1606 	if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1607 		SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1608 		DBG_ERR("Command out of sync");
1609 		DBG_RETURN(NULL);
1610 	}
1611 
1612 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
1613 	MYSQLND_RES *result = conn->current_result->m.store_result(conn->current_result, conn, NULL);
1614 	if (!result) {
1615 		conn->current_result->m.free_result(conn->current_result, TRUE);
1616 	}
1617 	conn->current_result = NULL;
1618 	DBG_RETURN(result);
1619 }
1620 /* }}} */
1621 
1622 
1623 /* {{{ mysqlnd_conn_data::get_connection_stats */
1624 static void
1625 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
1626 														zval * return_value ZEND_FILE_LINE_DC)
1627 {
1628 	DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
1629 	mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
1630 	DBG_VOID_RETURN;
1631 }
1632 /* }}} */
1633 
1634 
1635 /* {{{ mysqlnd_conn_data::set_autocommit */
1636 static enum_func_status
1637 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
1638 {
1639 	DBG_ENTER("mysqlnd_conn_data::set_autocommit");
1640 	DBG_RETURN(conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1));
1641 }
1642 /* }}} */
1643 
1644 
1645 /* {{{ mysqlnd_conn_data::tx_commit */
1646 static enum_func_status
1647 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
1648 {
1649 	return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
1650 }
1651 /* }}} */
1652 
1653 
1654 /* {{{ mysqlnd_conn_data::tx_rollback */
1655 static enum_func_status
1656 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
1657 {
1658 	return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
1659 }
1660 /* }}} */
1661 
1662 
1663 /* {{{ mysqlnd_tx_cor_options_to_string */
1664 static void
1665 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
1666 {
1667 	if ((mode & TRANS_COR_AND_CHAIN) && !(mode & TRANS_COR_AND_NO_CHAIN)) {
1668 		if (str->s && ZSTR_LEN(str->s)) {
1669 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1670 		}
1671 		smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
1672 	} else if ((mode & TRANS_COR_AND_NO_CHAIN) && !(mode & TRANS_COR_AND_CHAIN)) {
1673 		if (str->s && ZSTR_LEN(str->s)) {
1674 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1675 		}
1676 		smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
1677 	}
1678 
1679 	if ((mode & TRANS_COR_RELEASE) && !(mode & TRANS_COR_NO_RELEASE)) {
1680 		if (str->s && ZSTR_LEN(str->s)) {
1681 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1682 		}
1683 		smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
1684 	} else if ((mode & TRANS_COR_NO_RELEASE) && !(mode & TRANS_COR_RELEASE)) {
1685 		if (str->s && ZSTR_LEN(str->s)) {
1686 			smart_str_appendl(str, " ", sizeof(" ") - 1);
1687 		}
1688 		smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
1689 	}
1690 	smart_str_0(str);
1691 }
1692 /* }}} */
1693 
1694 
1695 /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
1696 static char *
1697 mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
1698 {
1699 	char * ret = NULL;
1700 	DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
1701 	if (name) {
1702 		bool warned = FALSE;
1703 		const char * p_orig = name;
1704 		char * p_copy;
1705 		p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
1706 		*p_copy++ = ' ';
1707 		*p_copy++ = '/';
1708 		*p_copy++ = '*';
1709 		while (1) {
1710 			register char v = *p_orig;
1711 			if (v == 0) {
1712 				break;
1713 			}
1714 			if ((v >= '0' && v <= '9') ||
1715 				(v >= 'a' && v <= 'z') ||
1716 				(v >= 'A' && v <= 'Z') ||
1717 				v == '-' ||
1718 				v == '_' ||
1719 				v == ' ' ||
1720 				v == '=')
1721 			{
1722 				*p_copy++ = v;
1723 			} else if (warned == FALSE) {
1724 				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");
1725 				warned = TRUE;
1726 			}
1727 			++p_orig;
1728 		}
1729 		*p_copy++ = '*';
1730 		*p_copy++ = '/';
1731 		*p_copy++ = 0;
1732 	}
1733 	DBG_RETURN(ret);
1734 }
1735 /* }}} */
1736 
1737 
1738 /* {{{ mysqlnd_conn_data::tx_commit_ex */
1739 static enum_func_status
1740 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const bool commit, const unsigned int flags, const char * const name)
1741 {
1742 	DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
1743 
1744 	smart_str tmp_str = {0, 0};
1745 	conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
1746 	smart_str_0(&tmp_str);
1747 
1748 	char * query;
1749 	size_t query_len;
1750 	char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1751 
1752 	query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
1753 							name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1754 	smart_str_free(&tmp_str);
1755 	if (name_esc) {
1756 		mnd_efree(name_esc);
1757 		name_esc = NULL;
1758 	}
1759 	if (!query) {
1760 		SET_OOM_ERROR(conn->error_info);
1761 		DBG_RETURN(FAIL);
1762 	}
1763 
1764 	enum_func_status ret = conn->m->query(conn, query, query_len);
1765 	mnd_sprintf_free(query);
1766 	DBG_RETURN(ret);
1767 }
1768 /* }}} */
1769 
1770 
1771 /* {{{ mysqlnd_conn_data::tx_begin */
1772 static enum_func_status
1773 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
1774 {
1775 	DBG_ENTER("mysqlnd_conn_data::tx_begin");
1776 
1777 	smart_str tmp_str = {0, 0};
1778 	if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
1779 		if (tmp_str.s) {
1780 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1781 		}
1782 		smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
1783 	}
1784 	if (mode & TRANS_START_READ_WRITE) {
1785 		if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1786 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1787 		}
1788 		smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
1789 	} else if (mode & TRANS_START_READ_ONLY) {
1790 		if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1791 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1792 		}
1793 		smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
1794 	}
1795 	smart_str_0(&tmp_str);
1796 
1797 	char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1798 	char * query;
1799 	unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1800 	smart_str_free(&tmp_str);
1801 	if (name_esc) {
1802 		mnd_efree(name_esc);
1803 		name_esc = NULL;
1804 	}
1805 	if (!query) {
1806 		SET_OOM_ERROR(conn->error_info);
1807 		DBG_RETURN(FAIL);
1808 	}
1809 
1810 	enum_func_status ret = conn->m->query(conn, query, query_len);
1811 	mnd_sprintf_free(query);
1812 	if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) && mysqlnd_stmt_errno(conn) == 1064) {
1813 		SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
1814 			"This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
1815 	}
1816 
1817 	DBG_RETURN(ret);
1818 }
1819 /* }}} */
1820 
1821 
1822 /* {{{ mysqlnd_conn_data::tx_savepoint */
1823 static enum_func_status
1824 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
1825 {
1826 	DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
1827 
1828 	if (!name) {
1829 		SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1830 		DBG_RETURN(FAIL);
1831 	}
1832 
1833 	char *query;
1834 	size_t query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
1835 	if (!query) {
1836 		SET_OOM_ERROR(conn->error_info);
1837 		DBG_RETURN(FAIL);
1838 	}
1839 
1840 	enum_func_status ret = conn->m->query(conn, query, query_len);
1841 	mnd_sprintf_free(query);
1842 	DBG_RETURN(ret);
1843 }
1844 /* }}} */
1845 
1846 
1847 /* {{{ mysqlnd_conn_data::tx_savepoint_release */
1848 static enum_func_status
1849 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
1850 {
1851 	DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
1852 
1853 	if (!name) {
1854 		SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1855 		DBG_RETURN(FAIL);
1856 	}
1857 
1858 	char *query;
1859 	size_t query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
1860 	if (!query) {
1861 		SET_OOM_ERROR(conn->error_info);
1862 		DBG_RETURN(FAIL);
1863 	}
1864 
1865 	enum_func_status ret = conn->m->query(conn, query, query_len);
1866 	mnd_sprintf_free(query);
1867 	DBG_RETURN(ret);
1868 }
1869 /* }}} */
1870 
1871 
1872 /* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
1873 static size_t
1874 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const size_t flags)
1875 {
1876 	unsigned int ret = 0;
1877 	DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
1878 	if (conn) {
1879 		ret = conn->client_api_capabilities;
1880 		conn->client_api_capabilities = flags;
1881 	}
1882 
1883 	DBG_RETURN(ret);
1884 }
1885 /* }}} */
1886 
1887 
1888 /* {{{ mysqlnd_conn_data::get_client_api_capabilities */
1889 static size_t
1890 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
1891 {
1892 	DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
1893 	DBG_RETURN(conn? conn->client_api_capabilities : 0);
1894 }
1895 /* }}} */
1896 
1897 
1898 /* {{{ _mysqlnd_stmt_init */
1899 MYSQLND_STMT *
1900 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init)(MYSQLND_CONN_DATA * const conn)
1901 {
1902 	MYSQLND_STMT * ret;
1903 	DBG_ENTER("mysqlnd_conn_data::stmt_init");
1904 	ret = conn->object_factory.get_prepared_statement(conn);
1905 	DBG_RETURN(ret);
1906 }
1907 /* }}} */
1908 
1909 
1910 MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
1911 	MYSQLND_METHOD(mysqlnd_conn_data, connect),
1912 
1913 	MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
1914 	MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
1915 	MYSQLND_METHOD(mysqlnd_conn_data, query),
1916 	MYSQLND_METHOD(mysqlnd_conn_data, send_query),
1917 	MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
1918 	MYSQLND_METHOD(mysqlnd_conn_data, use_result),
1919 	MYSQLND_METHOD(mysqlnd_conn_data, store_result),
1920 	MYSQLND_METHOD(mysqlnd_conn_data, next_result),
1921 	MYSQLND_METHOD(mysqlnd_conn_data, more_results),
1922 
1923 	MYSQLND_METHOD(mysqlnd_conn_data, stmt_init),
1924 
1925 	MYSQLND_METHOD(mysqlnd_conn_data, refresh),
1926 
1927 	MYSQLND_METHOD(mysqlnd_conn_data, ping),
1928 	MYSQLND_METHOD(mysqlnd_conn_data, kill),
1929 	MYSQLND_METHOD(mysqlnd_conn_data, select_db),
1930 	MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
1931 	MYSQLND_METHOD(mysqlnd_conn_data, change_user),
1932 
1933 	MYSQLND_METHOD(mysqlnd_conn_data, err_no),
1934 	MYSQLND_METHOD(mysqlnd_conn_data, error),
1935 	MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
1936 	MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
1937 
1938 	MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
1939 
1940 	MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
1941 	MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
1942 	MYSQLND_METHOD(mysqlnd_conn_data, statistic),
1943 	MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
1944 	MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
1945 	MYSQLND_METHOD(mysqlnd_conn_data, info),
1946 	MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
1947 	MYSQLND_METHOD(mysqlnd_conn_data, list_method),
1948 
1949 	MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
1950 	MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
1951 	MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
1952 	MYSQLND_METHOD(mysqlnd_conn_data, field_count),
1953 
1954 	MYSQLND_METHOD(mysqlnd_conn_data, server_status),
1955 
1956 	MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
1957 	MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
1958 	MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
1959 	MYSQLND_METHOD(mysqlnd_conn_data, free_options),
1960 
1961 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
1962 
1963 	mysqlnd_query_read_result_set_header,
1964 
1965 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
1966 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
1967 
1968 	MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
1969 	MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
1970 	MYSQLND_METHOD(mysqlnd_conn_data, send_close),
1971 
1972 	MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
1973 	mysqlnd_result_init,
1974 	MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
1975 	MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
1976 	MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
1977 	MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
1978 	MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
1979 	MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
1980 	MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
1981 	MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
1982 
1983 	MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
1984 	MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
1985 	MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
1986 	MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
1987 
1988 	MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
1989 
1990 	MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
1991 	MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities),
1992 
1993 	MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)
1994 MYSQLND_CLASS_METHODS_END;
1995 
1996 
1997 /* {{{ mysqlnd_conn::get_reference */
1998 static MYSQLND *
1999 MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
2000 {
2001 	MYSQLND * ret;
2002 	DBG_ENTER("mysqlnd_conn::get_reference");
2003 	ret = conn->data->object_factory.clone_connection_object(conn);
2004 	DBG_RETURN(ret);
2005 }
2006 /* }}} */
2007 
2008 
2009 /* {{{ mysqlnd_conn_data::dtor */
2010 static void
2011 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
2012 {
2013 	DBG_ENTER("mysqlnd_conn::dtor");
2014 	DBG_INF_FMT("conn=%" PRIu64, conn->data->thread_id);
2015 
2016 	conn->data->m->free_reference(conn->data);
2017 
2018 	mnd_pefree(conn, conn->persistent);
2019 
2020 	DBG_VOID_RETURN;
2021 }
2022 /* }}} */
2023 
2024 
2025 /* {{{ mysqlnd_conn_data::close */
2026 static enum_func_status
2027 MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, const enum_connection_close_type close_type)
2028 {
2029 	MYSQLND_CONN_DATA * conn = conn_handle->data;
2030 
2031 	DBG_ENTER("mysqlnd_conn::close");
2032 	DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
2033 
2034 	if (GET_CONNECTION_STATE(&conn->state) >= CONN_READY) {
2035 		static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
2036 			STAT_CLOSE_EXPLICIT,
2037 			STAT_CLOSE_IMPLICIT,
2038 			STAT_CLOSE_DISCONNECT
2039 		};
2040 		MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
2041 	}
2042 
2043 	/*
2044 	  Close now, free_reference will try,
2045 	  if we are last, but that's not a problem.
2046 	*/
2047 	enum_func_status ret = conn->m->send_close(conn);
2048 
2049 	conn_handle->m->dtor(conn_handle);
2050 	DBG_RETURN(ret);
2051 }
2052 /* }}} */
2053 
2054 
2055 MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
2056 	MYSQLND_METHOD(mysqlnd_conn, connect),
2057 	MYSQLND_METHOD(mysqlnd_conn, clone_object),
2058 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
2059 	MYSQLND_METHOD(mysqlnd_conn, close)
2060 MYSQLND_CLASS_METHODS_END;
2061 
2062 
2063 #include "php_network.h"
2064 
2065 /* {{{ mysqlnd_stream_array_to_fd_set */
2066 MYSQLND **
2067 mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
2068 {
2069 	unsigned int cnt = 0;
2070 	MYSQLND **p = conn_array, **p_p;
2071 	MYSQLND **ret = NULL;
2072 
2073 	while (*p) {
2074 		const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2075 		if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2076 			cnt++;
2077 		}
2078 		p++;
2079 	}
2080 	if (cnt) {
2081 		MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
2082 		p_p = p = conn_array;
2083 		while (*p) {
2084 			const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2085 			if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2086 				*ret_p = *p;
2087 				*p = NULL;
2088 				ret_p++;
2089 			} else {
2090 				*p_p = *p;
2091 				p_p++;
2092 			}
2093 			p++;
2094 		}
2095 		*ret_p = NULL;
2096 	}
2097 	return ret;
2098 }
2099 /* }}} */
2100 
2101 
2102 /* {{{ mysqlnd_stream_array_to_fd_set */
2103 static unsigned int
2104 mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
2105 {
2106 	php_socket_t this_fd;
2107 	php_stream *stream = NULL;
2108 	unsigned int cnt = 0;
2109 	MYSQLND **p = conn_array;
2110 	DBG_ENTER("mysqlnd_stream_array_to_fd_set");
2111 
2112 	while (*p) {
2113 		/* get the fd.
2114 		 * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
2115 		 * when casting.  It is only used here so that the buffered data warning
2116 		 * is not displayed.
2117 		 * */
2118 		stream = (*p)->data->vio->data->m.get_stream((*p)->data->vio);
2119 		DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*p)->data->thread_id, stream);
2120 		if (stream != NULL &&
2121 			SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) &&
2122 			ZEND_VALID_SOCKET(this_fd))
2123 		{
2124 
2125 			PHP_SAFE_FD_SET(this_fd, fds);
2126 
2127 			if (this_fd > *max_fd) {
2128 				*max_fd = this_fd;
2129 			}
2130 			++cnt;
2131 		}
2132 		++p;
2133 	}
2134 	DBG_RETURN(cnt ? 1 : 0);
2135 }
2136 /* }}} */
2137 
2138 
2139 /* {{{ mysqlnd_stream_array_from_fd_set */
2140 static unsigned int
2141 mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
2142 {
2143 	php_socket_t this_fd;
2144 	php_stream *stream = NULL;
2145 	unsigned int ret = 0;
2146 	bool disproportion = FALSE;
2147 	MYSQLND **fwd = conn_array, **bckwd = conn_array;
2148 	DBG_ENTER("mysqlnd_stream_array_from_fd_set");
2149 
2150 	while (*fwd) {
2151 		stream = (*fwd)->data->vio->data->m.get_stream((*fwd)->data->vio);
2152 		DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*fwd)->data->thread_id, stream);
2153 		if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
2154 										(void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
2155 			if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
2156 				if (disproportion) {
2157 					*bckwd = *fwd;
2158 				}
2159 				++bckwd;
2160 				++fwd;
2161 				++ret;
2162 				continue;
2163 			}
2164 		}
2165 		disproportion = TRUE;
2166 		++fwd;
2167 	}
2168 	*bckwd = NULL;/* NULL-terminate the list */
2169 
2170 	DBG_RETURN(ret);
2171 }
2172 /* }}} */
2173 
2174 
2175 #ifndef PHP_WIN32
2176 #define php_select(m, r, w, e, t)	select(m, r, w, e, t)
2177 #else
2178 #include "win32/select.h"
2179 #endif
2180 
2181 
2182 /* {{{ mysqlnd_poll */
2183 PHPAPI enum_func_status
2184 mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
2185 {
2186 	struct timeval	tv;
2187 	struct timeval *tv_p = NULL;
2188 	fd_set			rfds, wfds, efds;
2189 	php_socket_t	max_fd = 0;
2190 	int				retval, sets = 0;
2191 	int				set_count, max_set_count = 0;
2192 
2193 	DBG_ENTER("_mysqlnd_poll");
2194 	if (sec < 0 || usec < 0) {
2195 		php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
2196 		DBG_RETURN(FAIL);
2197 	}
2198 
2199 	FD_ZERO(&rfds);
2200 	FD_ZERO(&wfds);
2201 	FD_ZERO(&efds);
2202 
2203 	if (r_array != NULL) {
2204 		*dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
2205 		set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
2206 		if (set_count > max_set_count) {
2207 			max_set_count = set_count;
2208 		}
2209 		sets += set_count;
2210 	}
2211 
2212 	if (e_array != NULL) {
2213 		set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
2214 		if (set_count > max_set_count) {
2215 			max_set_count = set_count;
2216 		}
2217 		sets += set_count;
2218 	}
2219 
2220 	if (!sets) {
2221 		php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2222 		DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2223 		DBG_RETURN(FAIL);
2224 	}
2225 
2226 	if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
2227 		DBG_RETURN(FAIL);
2228 	}
2229 
2230 	/* Solaris + BSD do not like microsecond values which are >= 1 sec */
2231 	if (usec > 999999) {
2232 		tv.tv_sec = sec + (usec / 1000000);
2233 		tv.tv_usec = usec % 1000000;
2234 	} else {
2235 		tv.tv_sec = sec;
2236 		tv.tv_usec = usec;
2237 	}
2238 
2239 	tv_p = &tv;
2240 
2241 	retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
2242 
2243 	if (retval == -1) {
2244 		php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
2245 						errno, strerror(errno), max_fd);
2246 		DBG_RETURN(FAIL);
2247 	}
2248 
2249 	if (r_array != NULL) {
2250 		mysqlnd_stream_array_from_fd_set(r_array, &rfds);
2251 	}
2252 	if (e_array != NULL) {
2253 		mysqlnd_stream_array_from_fd_set(e_array, &efds);
2254 	}
2255 
2256 	*desc_num = retval;
2257 	DBG_RETURN(PASS);
2258 }
2259 /* }}} */
2260 
2261 
2262 /* {{{ mysqlnd_connect */
2263 PHPAPI MYSQLND * mysqlnd_connection_connect(MYSQLND * conn_handle,
2264 											const char * const host,
2265 											const char * const user,
2266 											const char * const passwd, unsigned int passwd_len,
2267 											const char * const db, unsigned int db_len,
2268 											unsigned int port,
2269 											const char * const sock_or_pipe,
2270 											unsigned int mysql_flags,
2271 											unsigned int client_api_flags
2272 						)
2273 {
2274 	enum_func_status ret = FAIL;
2275 	bool self_alloced = FALSE;
2276 	MYSQLND_CSTRING hostname = { host, host? strlen(host) : 0 };
2277 	MYSQLND_CSTRING username = { user, user? strlen(user) : 0 };
2278 	MYSQLND_CSTRING password = { passwd, passwd_len };
2279 	MYSQLND_CSTRING database = { db, db_len };
2280 	MYSQLND_CSTRING socket_or_pipe = { sock_or_pipe, sock_or_pipe? strlen(sock_or_pipe) : 0 };
2281 
2282 	DBG_ENTER("mysqlnd_connect");
2283 	DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host? host:"", user? user:"", db? db:"", port, mysql_flags);
2284 
2285 	if (!conn_handle) {
2286 		self_alloced = TRUE;
2287 		if (!(conn_handle = mysqlnd_connection_init(client_api_flags, FALSE, NULL))) {
2288 			/* OOM */
2289 			DBG_RETURN(NULL);
2290 		}
2291 	}
2292 
2293 	ret = conn_handle->m->connect(conn_handle, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
2294 
2295 	if (ret == FAIL) {
2296 		if (self_alloced) {
2297 			/*
2298 			  We have alloced, thus there are no references to this
2299 			  object - we are free to kill it!
2300 			*/
2301 			conn_handle->m->dtor(conn_handle);
2302 		}
2303 		DBG_RETURN(NULL);
2304 	}
2305 	DBG_RETURN(conn_handle);
2306 }
2307 /* }}} */
2308 
2309 
2310 /* {{{ mysqlnd_connection_init */
2311 PHPAPI MYSQLND *
2312 mysqlnd_connection_init(const size_t client_flags, const bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory)
2313 {
2314 	MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
2315 	MYSQLND * ret;
2316 	DBG_ENTER("mysqlnd_connection_init");
2317 	ret = factory->get_connection(factory, persistent);
2318 	if (ret && ret->data) {
2319 		ret->data->m->negotiate_client_api_capabilities(ret->data, client_flags);
2320 	}
2321 	DBG_RETURN(ret);
2322 }
2323 /* }}} */
2324