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_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 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1448 case MYSQL_SET_CLIENT_IP:
1449 case MYSQL_REPORT_DATA_TRUNCATION:
1450 #endif
1451 /* currently not supported. Todo!! */
1452 break;
1453 case MYSQL_SET_CHARSET_NAME:
1454 {
1455 char * new_charset_name;
1456 if (!mysqlnd_find_charset_name(value)) {
1457 SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
1458 ret = FAIL;
1459 break;
1460 }
1461
1462 new_charset_name = mnd_pestrdup(value, conn->persistent);
1463 if (conn->options->charset_name) {
1464 mnd_pefree(conn->options->charset_name, conn->persistent);
1465 }
1466 conn->options->charset_name = new_charset_name;
1467 DBG_INF_FMT("charset=%s", conn->options->charset_name);
1468 break;
1469 }
1470 case MYSQL_OPT_NAMED_PIPE:
1471 conn->options->protocol = MYSQL_PROTOCOL_PIPE;
1472 break;
1473 case MYSQL_OPT_PROTOCOL:
1474 if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
1475 conn->options->protocol = *(unsigned int*) value;
1476 }
1477 break;
1478 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1479 case MYSQL_SET_CHARSET_DIR:
1480 case MYSQL_OPT_RECONNECT:
1481 /* we don't need external character sets, all character sets are
1482 compiled in. For compatibility we just ignore this setting.
1483 Same for protocol, we don't support old protocol */
1484 case MYSQL_OPT_USE_REMOTE_CONNECTION:
1485 case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
1486 case MYSQL_OPT_GUESS_CONNECTION:
1487 /* todo: throw an error, we don't support embedded */
1488 break;
1489 #endif
1490 case MYSQLND_OPT_MAX_ALLOWED_PACKET:
1491 if (*(unsigned int*) value > (1<<16)) {
1492 conn->options->max_allowed_packet = *(unsigned int*) value;
1493 }
1494 break;
1495 case MYSQLND_OPT_AUTH_PROTOCOL:
1496 {
1497 char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
1498 if (conn->options->auth_protocol) {
1499 mnd_pefree(conn->options->auth_protocol, conn->persistent);
1500 }
1501 conn->options->auth_protocol = new_auth_protocol;
1502 DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
1503 break;
1504 }
1505 case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
1506 if (value && (*(unsigned int*) value) ? 1 : 0) {
1507 conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1508 } else {
1509 conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1510 }
1511 break;
1512 case MYSQL_OPT_CONNECT_ATTR_RESET:
1513 if (conn->options->connect_attr) {
1514 DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1515 zend_hash_clean(conn->options->connect_attr);
1516 }
1517 break;
1518 case MYSQL_OPT_CONNECT_ATTR_DELETE:
1519 if (conn->options->connect_attr && value) {
1520 DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1521 zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
1522 DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
1523 }
1524 break;
1525 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1526 case MYSQL_SHARED_MEMORY_BASE_NAME:
1527 case MYSQL_OPT_USE_RESULT:
1528 case MYSQL_SECURE_AUTH:
1529 /* not sure, todo ? */
1530 #endif
1531 default:
1532 ret = FAIL;
1533 }
1534 DBG_RETURN(ret);
1535 }
1536 /* }}} */
1537
1538
1539 /* {{{ mysqlnd_conn_data::set_client_option_2d */
1540 static enum_func_status
1541 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
1542 const enum_mysqlnd_client_option option,
1543 const char * const key,
1544 const char * const value
1545 )
1546 {
1547 enum_func_status ret = PASS;
1548 DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
1549 DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1550
1551 switch (option) {
1552 case MYSQL_OPT_CONNECT_ATTR_ADD:
1553 if (!conn->options->connect_attr) {
1554 DBG_INF("Initializing connect_attr hash");
1555 conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
1556 zend_hash_init(conn->options->connect_attr, 0, NULL, conn->persistent ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR, conn->persistent);
1557 }
1558 DBG_INF_FMT("Adding [%s][%s]", key, value);
1559 {
1560 zval attrz;
1561 zend_string *str;
1562
1563 if (conn->persistent) {
1564 str = zend_string_init(key, strlen(key), 1);
1565 GC_MAKE_PERSISTENT_LOCAL(str);
1566 ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
1567 GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(attrz));
1568 } else {
1569 str = zend_string_init(key, strlen(key), 0);
1570 ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 0));
1571 }
1572 zend_hash_update(conn->options->connect_attr, str, &attrz);
1573 zend_string_release_ex(str, 1);
1574 }
1575 break;
1576 default:
1577 ret = FAIL;
1578 }
1579 DBG_RETURN(ret);
1580 }
1581 /* }}} */
1582
1583
1584 /* {{{ mysqlnd_conn_data::use_result */
1585 static MYSQLND_RES *
1586 MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn)
1587 {
1588 DBG_ENTER("mysqlnd_conn_data::use_result");
1589 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
1590
1591 if (!conn->current_result) {
1592 DBG_RETURN(NULL);
1593 }
1594
1595 /* Nothing to store for UPSERT/LOAD DATA */
1596 if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1597 SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1598 DBG_ERR("Command out of sync");
1599 DBG_RETURN(NULL);
1600 }
1601
1602 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
1603
1604 conn->current_result->conn = conn->m->get_reference(conn);
1605
1606 MYSQLND_RES *result = conn->current_result->m.use_result(conn->current_result, FALSE);
1607 if (!result) {
1608 conn->current_result->m.free_result(conn->current_result, TRUE);
1609 }
1610 conn->current_result = NULL;
1611 DBG_RETURN(result);
1612 }
1613 /* }}} */
1614
1615
1616 /* {{{ mysqlnd_conn_data::store_result */
1617 static MYSQLND_RES *
1618 MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn)
1619 {
1620 DBG_ENTER("mysqlnd_conn_data::store_result");
1621 DBG_INF_FMT("conn=%" PRIu64 " conn=%p", conn->thread_id, conn);
1622
1623 if (!conn->current_result) {
1624 DBG_RETURN(NULL);
1625 }
1626
1627 /* Nothing to store for UPSERT/LOAD DATA*/
1628 if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1629 SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1630 DBG_ERR("Command out of sync");
1631 DBG_RETURN(NULL);
1632 }
1633
1634 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
1635 MYSQLND_RES *result = conn->current_result->m.store_result(conn->current_result, conn, NULL);
1636 if (!result) {
1637 conn->current_result->m.free_result(conn->current_result, TRUE);
1638 }
1639 conn->current_result = NULL;
1640 DBG_RETURN(result);
1641 }
1642 /* }}} */
1643
1644
1645 /* {{{ mysqlnd_conn_data::get_connection_stats */
1646 static void
1647 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
1648 zval * return_value ZEND_FILE_LINE_DC)
1649 {
1650 DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
1651 mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
1652 DBG_VOID_RETURN;
1653 }
1654 /* }}} */
1655
1656
1657 /* {{{ mysqlnd_conn_data::set_autocommit */
1658 static enum_func_status
1659 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
1660 {
1661 DBG_ENTER("mysqlnd_conn_data::set_autocommit");
1662 DBG_RETURN(conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1));
1663 }
1664 /* }}} */
1665
1666
1667 /* {{{ mysqlnd_conn_data::tx_commit */
1668 static enum_func_status
1669 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
1670 {
1671 return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
1672 }
1673 /* }}} */
1674
1675
1676 /* {{{ mysqlnd_conn_data::tx_rollback */
1677 static enum_func_status
1678 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
1679 {
1680 return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
1681 }
1682 /* }}} */
1683
1684
1685 /* {{{ mysqlnd_tx_cor_options_to_string */
1686 static void
1687 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
1688 {
1689 if ((mode & TRANS_COR_AND_CHAIN) && !(mode & TRANS_COR_AND_NO_CHAIN)) {
1690 if (str->s && ZSTR_LEN(str->s)) {
1691 smart_str_appendl(str, " ", sizeof(" ") - 1);
1692 }
1693 smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
1694 } else if ((mode & TRANS_COR_AND_NO_CHAIN) && !(mode & TRANS_COR_AND_CHAIN)) {
1695 if (str->s && ZSTR_LEN(str->s)) {
1696 smart_str_appendl(str, " ", sizeof(" ") - 1);
1697 }
1698 smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
1699 }
1700
1701 if ((mode & TRANS_COR_RELEASE) && !(mode & TRANS_COR_NO_RELEASE)) {
1702 if (str->s && ZSTR_LEN(str->s)) {
1703 smart_str_appendl(str, " ", sizeof(" ") - 1);
1704 }
1705 smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
1706 } else if ((mode & TRANS_COR_NO_RELEASE) && !(mode & TRANS_COR_RELEASE)) {
1707 if (str->s && ZSTR_LEN(str->s)) {
1708 smart_str_appendl(str, " ", sizeof(" ") - 1);
1709 }
1710 smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
1711 }
1712 smart_str_0(str);
1713 }
1714 /* }}} */
1715
1716
1717 /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
1718 static char *
1719 mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
1720 {
1721 char * ret = NULL;
1722 DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
1723 if (name) {
1724 bool warned = FALSE;
1725 const char * p_orig = name;
1726 char * p_copy;
1727 p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
1728 *p_copy++ = ' ';
1729 *p_copy++ = '/';
1730 *p_copy++ = '*';
1731 while (1) {
1732 register char v = *p_orig;
1733 if (v == 0) {
1734 break;
1735 }
1736 if ((v >= '0' && v <= '9') ||
1737 (v >= 'a' && v <= 'z') ||
1738 (v >= 'A' && v <= 'Z') ||
1739 v == '-' ||
1740 v == '_' ||
1741 v == ' ' ||
1742 v == '=')
1743 {
1744 *p_copy++ = v;
1745 } else if (warned == FALSE) {
1746 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");
1747 warned = TRUE;
1748 }
1749 ++p_orig;
1750 }
1751 *p_copy++ = '*';
1752 *p_copy++ = '/';
1753 *p_copy++ = 0;
1754 }
1755 DBG_RETURN(ret);
1756 }
1757 /* }}} */
1758
1759
1760 /* {{{ mysqlnd_conn_data::tx_commit_ex */
1761 static enum_func_status
1762 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const bool commit, const unsigned int flags, const char * const name)
1763 {
1764 DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
1765
1766 smart_str tmp_str = {0, 0};
1767 conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
1768 smart_str_0(&tmp_str);
1769
1770 char * query;
1771 size_t query_len;
1772 char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1773
1774 query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
1775 name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1776 smart_str_free(&tmp_str);
1777 if (name_esc) {
1778 mnd_efree(name_esc);
1779 name_esc = NULL;
1780 }
1781 if (!query) {
1782 SET_OOM_ERROR(conn->error_info);
1783 DBG_RETURN(FAIL);
1784 }
1785
1786 enum_func_status ret = conn->m->query(conn, query, query_len);
1787 mnd_sprintf_free(query);
1788 DBG_RETURN(ret);
1789 }
1790 /* }}} */
1791
1792
1793 /* {{{ mysqlnd_conn_data::tx_begin */
1794 static enum_func_status
1795 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
1796 {
1797 DBG_ENTER("mysqlnd_conn_data::tx_begin");
1798
1799 smart_str tmp_str = {0, 0};
1800 if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
1801 if (tmp_str.s) {
1802 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1803 }
1804 smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
1805 }
1806 if (mode & TRANS_START_READ_WRITE) {
1807 if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1808 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1809 }
1810 smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
1811 } else if (mode & TRANS_START_READ_ONLY) {
1812 if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1813 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1814 }
1815 smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
1816 }
1817 smart_str_0(&tmp_str);
1818
1819 char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1820 char * query;
1821 unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1822 smart_str_free(&tmp_str);
1823 if (name_esc) {
1824 mnd_efree(name_esc);
1825 name_esc = NULL;
1826 }
1827 if (!query) {
1828 SET_OOM_ERROR(conn->error_info);
1829 DBG_RETURN(FAIL);
1830 }
1831
1832 enum_func_status ret = conn->m->query(conn, query, query_len);
1833 mnd_sprintf_free(query);
1834 if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) && mysqlnd_stmt_errno(conn) == 1064) {
1835 SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
1836 "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
1837 }
1838
1839 DBG_RETURN(ret);
1840 }
1841 /* }}} */
1842
1843
1844 /* {{{ mysqlnd_conn_data::tx_savepoint */
1845 static enum_func_status
1846 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
1847 {
1848 DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
1849
1850 if (!name) {
1851 SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1852 DBG_RETURN(FAIL);
1853 }
1854
1855 char *query;
1856 size_t query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
1857 if (!query) {
1858 SET_OOM_ERROR(conn->error_info);
1859 DBG_RETURN(FAIL);
1860 }
1861
1862 enum_func_status ret = conn->m->query(conn, query, query_len);
1863 mnd_sprintf_free(query);
1864 DBG_RETURN(ret);
1865 }
1866 /* }}} */
1867
1868
1869 /* {{{ mysqlnd_conn_data::tx_savepoint_release */
1870 static enum_func_status
1871 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
1872 {
1873 DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
1874
1875 if (!name) {
1876 SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1877 DBG_RETURN(FAIL);
1878 }
1879
1880 char *query;
1881 size_t query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
1882 if (!query) {
1883 SET_OOM_ERROR(conn->error_info);
1884 DBG_RETURN(FAIL);
1885 }
1886
1887 enum_func_status ret = conn->m->query(conn, query, query_len);
1888 mnd_sprintf_free(query);
1889 DBG_RETURN(ret);
1890 }
1891 /* }}} */
1892
1893
1894 /* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
1895 static size_t
1896 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const size_t flags)
1897 {
1898 unsigned int ret = 0;
1899 DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
1900 if (conn) {
1901 ret = conn->client_api_capabilities;
1902 conn->client_api_capabilities = flags;
1903 }
1904
1905 DBG_RETURN(ret);
1906 }
1907 /* }}} */
1908
1909
1910 /* {{{ mysqlnd_conn_data::get_client_api_capabilities */
1911 static size_t
1912 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
1913 {
1914 DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
1915 DBG_RETURN(conn? conn->client_api_capabilities : 0);
1916 }
1917 /* }}} */
1918
1919
1920 /* {{{ _mysqlnd_stmt_init */
1921 MYSQLND_STMT *
1922 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init)(MYSQLND_CONN_DATA * const conn)
1923 {
1924 MYSQLND_STMT * ret;
1925 DBG_ENTER("mysqlnd_conn_data::stmt_init");
1926 ret = conn->object_factory.get_prepared_statement(conn);
1927 DBG_RETURN(ret);
1928 }
1929 /* }}} */
1930
1931
1932 MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
1933 MYSQLND_METHOD(mysqlnd_conn_data, connect),
1934
1935 MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
1936 MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
1937 MYSQLND_METHOD(mysqlnd_conn_data, query),
1938 MYSQLND_METHOD(mysqlnd_conn_data, send_query),
1939 MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
1940 MYSQLND_METHOD(mysqlnd_conn_data, use_result),
1941 MYSQLND_METHOD(mysqlnd_conn_data, store_result),
1942 MYSQLND_METHOD(mysqlnd_conn_data, next_result),
1943 MYSQLND_METHOD(mysqlnd_conn_data, more_results),
1944
1945 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init),
1946
1947 MYSQLND_METHOD(mysqlnd_conn_data, refresh),
1948
1949 MYSQLND_METHOD(mysqlnd_conn_data, ping),
1950 MYSQLND_METHOD(mysqlnd_conn_data, kill),
1951 MYSQLND_METHOD(mysqlnd_conn_data, select_db),
1952 MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
1953 MYSQLND_METHOD(mysqlnd_conn_data, change_user),
1954
1955 MYSQLND_METHOD(mysqlnd_conn_data, err_no),
1956 MYSQLND_METHOD(mysqlnd_conn_data, error),
1957 MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
1958 MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
1959
1960 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
1961
1962 MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
1963 MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
1964 MYSQLND_METHOD(mysqlnd_conn_data, statistic),
1965 MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
1966 MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
1967 MYSQLND_METHOD(mysqlnd_conn_data, info),
1968 MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
1969 MYSQLND_METHOD(mysqlnd_conn_data, list_method),
1970
1971 MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
1972 MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
1973 MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
1974 MYSQLND_METHOD(mysqlnd_conn_data, field_count),
1975
1976 MYSQLND_METHOD(mysqlnd_conn_data, server_status),
1977
1978 MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
1979 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
1980 MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
1981 MYSQLND_METHOD(mysqlnd_conn_data, free_options),
1982
1983 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
1984
1985 mysqlnd_query_read_result_set_header,
1986
1987 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
1988 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
1989
1990 MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
1991 MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
1992 MYSQLND_METHOD(mysqlnd_conn_data, send_close),
1993
1994 MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
1995 mysqlnd_result_init,
1996 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
1997 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
1998 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
1999 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
2000 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
2001 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
2002 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
2003 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
2004
2005 MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
2006 MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
2007 MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
2008 MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
2009
2010 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
2011
2012 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
2013 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities),
2014
2015 MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)
2016 MYSQLND_CLASS_METHODS_END;
2017
2018
2019 /* {{{ mysqlnd_conn::get_reference */
2020 static MYSQLND *
2021 MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
2022 {
2023 MYSQLND * ret;
2024 DBG_ENTER("mysqlnd_conn::get_reference");
2025 ret = conn->data->object_factory.clone_connection_object(conn);
2026 DBG_RETURN(ret);
2027 }
2028 /* }}} */
2029
2030
2031 /* {{{ mysqlnd_conn_data::dtor */
2032 static void
2033 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
2034 {
2035 DBG_ENTER("mysqlnd_conn::dtor");
2036 DBG_INF_FMT("conn=%" PRIu64, conn->data->thread_id);
2037
2038 conn->data->m->free_reference(conn->data);
2039
2040 mnd_pefree(conn, conn->persistent);
2041
2042 DBG_VOID_RETURN;
2043 }
2044 /* }}} */
2045
2046
2047 /* {{{ mysqlnd_conn_data::close */
2048 static enum_func_status
2049 MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, const enum_connection_close_type close_type)
2050 {
2051 MYSQLND_CONN_DATA * conn = conn_handle->data;
2052
2053 DBG_ENTER("mysqlnd_conn::close");
2054 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
2055
2056 if (GET_CONNECTION_STATE(&conn->state) >= CONN_READY) {
2057 static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
2058 STAT_CLOSE_EXPLICIT,
2059 STAT_CLOSE_IMPLICIT,
2060 STAT_CLOSE_DISCONNECT
2061 };
2062 MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
2063 }
2064
2065 /*
2066 Close now, free_reference will try,
2067 if we are last, but that's not a problem.
2068 */
2069 enum_func_status ret = conn->m->send_close(conn);
2070
2071 conn_handle->m->dtor(conn_handle);
2072 DBG_RETURN(ret);
2073 }
2074 /* }}} */
2075
2076
2077 MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
2078 MYSQLND_METHOD(mysqlnd_conn, connect),
2079 MYSQLND_METHOD(mysqlnd_conn, clone_object),
2080 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
2081 MYSQLND_METHOD(mysqlnd_conn, close)
2082 MYSQLND_CLASS_METHODS_END;
2083
2084
2085 #include "php_network.h"
2086
2087 /* {{{ mysqlnd_stream_array_to_fd_set */
2088 MYSQLND **
2089 mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
2090 {
2091 unsigned int cnt = 0;
2092 MYSQLND **p = conn_array, **p_p;
2093 MYSQLND **ret = NULL;
2094
2095 while (*p) {
2096 const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2097 if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2098 cnt++;
2099 }
2100 p++;
2101 }
2102 if (cnt) {
2103 MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
2104 p_p = p = conn_array;
2105 while (*p) {
2106 const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2107 if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2108 *ret_p = *p;
2109 *p = NULL;
2110 ret_p++;
2111 } else {
2112 *p_p = *p;
2113 p_p++;
2114 }
2115 p++;
2116 }
2117 *ret_p = NULL;
2118 }
2119 return ret;
2120 }
2121 /* }}} */
2122
2123
2124 /* {{{ mysqlnd_stream_array_to_fd_set */
2125 static unsigned int
2126 mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
2127 {
2128 php_socket_t this_fd;
2129 php_stream *stream = NULL;
2130 unsigned int cnt = 0;
2131 MYSQLND **p = conn_array;
2132 DBG_ENTER("mysqlnd_stream_array_to_fd_set");
2133
2134 while (*p) {
2135 /* get the fd.
2136 * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
2137 * when casting. It is only used here so that the buffered data warning
2138 * is not displayed.
2139 * */
2140 stream = (*p)->data->vio->data->m.get_stream((*p)->data->vio);
2141 DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*p)->data->thread_id, stream);
2142 if (stream != NULL &&
2143 SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) &&
2144 ZEND_VALID_SOCKET(this_fd))
2145 {
2146
2147 PHP_SAFE_FD_SET(this_fd, fds);
2148
2149 if (this_fd > *max_fd) {
2150 *max_fd = this_fd;
2151 }
2152 ++cnt;
2153 }
2154 ++p;
2155 }
2156 DBG_RETURN(cnt ? 1 : 0);
2157 }
2158 /* }}} */
2159
2160
2161 /* {{{ mysqlnd_stream_array_from_fd_set */
2162 static unsigned int
2163 mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
2164 {
2165 php_socket_t this_fd;
2166 php_stream *stream = NULL;
2167 unsigned int ret = 0;
2168 bool disproportion = FALSE;
2169 MYSQLND **fwd = conn_array, **bckwd = conn_array;
2170 DBG_ENTER("mysqlnd_stream_array_from_fd_set");
2171
2172 while (*fwd) {
2173 stream = (*fwd)->data->vio->data->m.get_stream((*fwd)->data->vio);
2174 DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*fwd)->data->thread_id, stream);
2175 if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
2176 (void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
2177 if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
2178 if (disproportion) {
2179 *bckwd = *fwd;
2180 }
2181 ++bckwd;
2182 ++fwd;
2183 ++ret;
2184 continue;
2185 }
2186 }
2187 disproportion = TRUE;
2188 ++fwd;
2189 }
2190 *bckwd = NULL;/* NULL-terminate the list */
2191
2192 DBG_RETURN(ret);
2193 }
2194 /* }}} */
2195
2196
2197 #ifndef PHP_WIN32
2198 #define php_select(m, r, w, e, t) select(m, r, w, e, t)
2199 #else
2200 #include "win32/select.h"
2201 #endif
2202
2203
2204 /* {{{ mysqlnd_poll */
2205 PHPAPI enum_func_status
2206 mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
2207 {
2208 struct timeval tv;
2209 struct timeval *tv_p = NULL;
2210 fd_set rfds, wfds, efds;
2211 php_socket_t max_fd = 0;
2212 int retval, sets = 0;
2213 int set_count, max_set_count = 0;
2214
2215 DBG_ENTER("_mysqlnd_poll");
2216 if (sec < 0 || usec < 0) {
2217 php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
2218 DBG_RETURN(FAIL);
2219 }
2220
2221 FD_ZERO(&rfds);
2222 FD_ZERO(&wfds);
2223 FD_ZERO(&efds);
2224
2225 if (r_array != NULL) {
2226 *dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
2227 set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
2228 if (set_count > max_set_count) {
2229 max_set_count = set_count;
2230 }
2231 sets += set_count;
2232 }
2233
2234 if (e_array != NULL) {
2235 set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
2236 if (set_count > max_set_count) {
2237 max_set_count = set_count;
2238 }
2239 sets += set_count;
2240 }
2241
2242 if (!sets) {
2243 php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2244 DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2245 DBG_RETURN(FAIL);
2246 }
2247
2248 if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
2249 DBG_RETURN(FAIL);
2250 }
2251
2252 /* Solaris + BSD do not like microsecond values which are >= 1 sec */
2253 if (usec > 999999) {
2254 tv.tv_sec = sec + (usec / 1000000);
2255 tv.tv_usec = usec % 1000000;
2256 } else {
2257 tv.tv_sec = sec;
2258 tv.tv_usec = usec;
2259 }
2260
2261 tv_p = &tv;
2262
2263 retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
2264
2265 if (retval == -1) {
2266 php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
2267 errno, strerror(errno), max_fd);
2268 DBG_RETURN(FAIL);
2269 }
2270
2271 if (r_array != NULL) {
2272 mysqlnd_stream_array_from_fd_set(r_array, &rfds);
2273 }
2274 if (e_array != NULL) {
2275 mysqlnd_stream_array_from_fd_set(e_array, &efds);
2276 }
2277
2278 *desc_num = retval;
2279 DBG_RETURN(PASS);
2280 }
2281 /* }}} */
2282
2283
2284 /* {{{ mysqlnd_connect */
2285 PHPAPI MYSQLND * mysqlnd_connection_connect(MYSQLND * conn_handle,
2286 const char * const host,
2287 const char * const user,
2288 const char * const passwd, unsigned int passwd_len,
2289 const char * const db, unsigned int db_len,
2290 unsigned int port,
2291 const char * const sock_or_pipe,
2292 unsigned int mysql_flags,
2293 unsigned int client_api_flags
2294 )
2295 {
2296 enum_func_status ret = FAIL;
2297 bool self_alloced = FALSE;
2298 MYSQLND_CSTRING hostname = { host, host? strlen(host) : 0 };
2299 MYSQLND_CSTRING username = { user, user? strlen(user) : 0 };
2300 MYSQLND_CSTRING password = { passwd, passwd_len };
2301 MYSQLND_CSTRING database = { db, db_len };
2302 MYSQLND_CSTRING socket_or_pipe = { sock_or_pipe, sock_or_pipe? strlen(sock_or_pipe) : 0 };
2303
2304 DBG_ENTER("mysqlnd_connect");
2305 DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host? host:"", user? user:"", db? db:"", port, mysql_flags);
2306
2307 if (!conn_handle) {
2308 self_alloced = TRUE;
2309 if (!(conn_handle = mysqlnd_connection_init(client_api_flags, FALSE, NULL))) {
2310 /* OOM */
2311 DBG_RETURN(NULL);
2312 }
2313 }
2314
2315 ret = conn_handle->m->connect(conn_handle, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
2316
2317 if (ret == FAIL) {
2318 if (self_alloced) {
2319 /*
2320 We have alloced, thus there are no references to this
2321 object - we are free to kill it!
2322 */
2323 conn_handle->m->dtor(conn_handle);
2324 }
2325 DBG_RETURN(NULL);
2326 }
2327 DBG_RETURN(conn_handle);
2328 }
2329 /* }}} */
2330
2331
2332 /* {{{ mysqlnd_connection_init */
2333 PHPAPI MYSQLND *
2334 mysqlnd_connection_init(const size_t client_flags, const bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory)
2335 {
2336 MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
2337 MYSQLND * ret;
2338 DBG_ENTER("mysqlnd_connection_init");
2339 ret = factory->get_connection(factory, persistent);
2340 if (ret && ret->data) {
2341 ret->data->m->negotiate_client_api_capabilities(ret->data, client_flags);
2342 }
2343 DBG_RETURN(ret);
2344 }
2345 /* }}} */
2346