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 enum_func_status
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_RETURN(PASS);
372 }
373 /* }}} */
374
375
376 /* {{{ mysqlnd_conn_data::end_psession */
377 static enum_func_status
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_RETURN(PASS);
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 PASS == conn->protocol_frame_codec->data->m.reset(conn->protocol_frame_codec, conn->stats, conn->error_info))
508 {
509 size_t client_flags = mysql_flags;
510
511 ret = conn->command->handshake(conn, *username, *password, *database, client_flags);
512 }
513 DBG_RETURN(ret);
514 }
515 /* }}} */
516
517 /* {{{ mysqlnd_conn_data::get_scheme */
518 static MYSQLND_STRING
MYSQLND_METHOD(mysqlnd_conn_data,get_scheme)519 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)
520 {
521 MYSQLND_STRING transport;
522 DBG_ENTER("mysqlnd_conn_data::get_scheme");
523 #ifndef PHP_WIN32
524 if (hostname.l == sizeof("localhost") - 1 && !strncasecmp(hostname.s, "localhost", hostname.l)) {
525 DBG_INF_FMT("socket=%s", socket_or_pipe->s? socket_or_pipe->s:"n/a");
526 if (!socket_or_pipe->s) {
527 socket_or_pipe->s = "/tmp/mysql.sock";
528 socket_or_pipe->l = strlen(socket_or_pipe->s);
529 }
530 transport.l = mnd_sprintf(&transport.s, 0, "unix://%s", socket_or_pipe->s);
531 *unix_socket = TRUE;
532 #else
533 if (hostname.l == sizeof(".") - 1 && hostname.s[0] == '.') {
534 /* named pipe in socket */
535 if (!socket_or_pipe->s) {
536 socket_or_pipe->s = "\\\\.\\pipe\\MySQL";
537 socket_or_pipe->l = strlen(socket_or_pipe->s);
538 }
539 transport.l = mnd_sprintf(&transport.s, 0, "pipe://%s", socket_or_pipe->s);
540 *named_pipe = TRUE;
541 #endif
542 } else {
543 if (!port) {
544 port = 3306;
545 }
546 transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s:%u", hostname.s, port);
547 }
548 DBG_INF_FMT("transport=%s", transport.s? transport.s:"OOM");
549 DBG_RETURN(transport);
550 }
551 /* }}} */
552
553
554 /* {{{ mysqlnd_conn_data::connect */
555 static enum_func_status
556 MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
557 MYSQLND_CSTRING hostname,
558 MYSQLND_CSTRING username,
559 MYSQLND_CSTRING password,
560 MYSQLND_CSTRING database,
561 unsigned int port,
562 MYSQLND_CSTRING socket_or_pipe,
563 unsigned int mysql_flags
564 )
565 {
566 bool unix_socket = FALSE;
567 bool named_pipe = FALSE;
568 bool reconnect = FALSE;
569 bool saved_compression = FALSE;
570 MYSQLND_PFC * pfc = conn->protocol_frame_codec;
571 MYSQLND_STRING transport = { NULL, 0 };
572
573 DBG_ENTER("mysqlnd_conn_data::connect");
574 DBG_INF_FMT("conn=%p", conn);
575
576 SET_EMPTY_ERROR(conn->error_info);
577 UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
578
579 DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
580 hostname.s?hostname.s:"", username.s?username.s:"", database.s?database.s:"", port, mysql_flags,
581 conn? conn->persistent:0, conn? (int)GET_CONNECTION_STATE(&conn->state):-1);
582
583 if (GET_CONNECTION_STATE(&conn->state) > CONN_ALLOCED) {
584 DBG_INF("Connecting on a connected handle.");
585
586 if (GET_CONNECTION_STATE(&conn->state) < CONN_QUIT_SENT) {
587 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
588 reconnect = TRUE;
589 conn->m->send_close(conn);
590 }
591
592 conn->m->free_contents(conn);
593 /* Now reconnect using the same handle */
594 if (pfc->data->compressed) {
595 /*
596 we need to save the state. As we will re-connect, pfc->compressed should be off, or
597 we will look for a compression header as part of the greet message, but there will
598 be none.
599 */
600 saved_compression = TRUE;
601 pfc->data->compressed = FALSE;
602 }
603 if (pfc->data->ssl) {
604 pfc->data->ssl = FALSE;
605 }
606 } else {
607 unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
608 conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size);
609 }
610
611 if (!hostname.s || !hostname.s[0]) {
612 hostname.s = "localhost";
613 hostname.l = strlen(hostname.s);
614 }
615 if (!username.s) {
616 DBG_INF_FMT("no user given, using empty string");
617 username.s = "";
618 username.l = 0;
619 }
620 if (!password.s) {
621 DBG_INF_FMT("no password given, using empty string");
622 password.s = "";
623 password.l = 0;
624 }
625 if (!database.s || !database.s[0]) {
626 DBG_INF_FMT("no db given, using empty string");
627 database.s = "";
628 database.l = 0;
629 } else {
630 mysql_flags |= CLIENT_CONNECT_WITH_DB;
631 }
632
633 transport = conn->m->get_scheme(conn, hostname, &socket_or_pipe, port, &unix_socket, &named_pipe);
634
635 mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags);
636
637 {
638 const MYSQLND_CSTRING scheme = { transport.s, transport.l };
639 if (FAIL == conn->m->connect_handshake(conn, &scheme, &username, &password, &database, mysql_flags)) {
640 goto err;
641 }
642 }
643
644 {
645 SET_CONNECTION_STATE(&conn->state, CONN_READY);
646
647 if (saved_compression) {
648 pfc->data->compressed = TRUE;
649 }
650 /*
651 If a connect on a existing handle is performed and mysql_flags is
652 passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
653 which we set based on saved_compression.
654 */
655 pfc->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
656
657
658 mysqlnd_set_persistent_string(&conn->scheme, transport.s, transport.l, conn->persistent);
659 if (transport.s) {
660 mnd_sprintf_free(transport.s);
661 transport.s = NULL;
662 }
663
664 if (!conn->scheme.s) {
665 goto err; /* OOM */
666 }
667
668 mysqlnd_set_persistent_string(&conn->username, username.s, username.l, conn->persistent);
669 mysqlnd_set_persistent_string(&conn->password, username.s, password.l, conn->persistent);
670 conn->port = port;
671 mysqlnd_set_persistent_string(&conn->connect_or_select_db, database.s, database.l, conn->persistent);
672
673 if (!unix_socket && !named_pipe) {
674 mysqlnd_set_persistent_string(&conn->hostname, hostname.s, hostname.l, conn->persistent);
675 {
676 char *p;
677 mnd_sprintf(&p, 0, "%s via TCP/IP", conn->hostname.s);
678 if (!p) {
679 SET_OOM_ERROR(conn->error_info);
680 goto err; /* OOM */
681 }
682 conn->host_info = mnd_pestrdup(p, conn->persistent);
683 mnd_sprintf_free(p);
684 }
685 } else {
686 conn->unix_socket.s = mnd_pestrdup(socket_or_pipe.s, conn->persistent);
687 if (unix_socket) {
688 conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
689 } else if (named_pipe) {
690 char *p;
691 mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket.s);
692 if (!p) {
693 SET_OOM_ERROR(conn->error_info);
694 goto err; /* OOM */
695 }
696 conn->host_info = mnd_pestrdup(p, conn->persistent);
697 mnd_sprintf_free(p);
698 } else {
699 php_error_docref(NULL, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
700 }
701 if (!conn->unix_socket.s || !conn->host_info) {
702 SET_OOM_ERROR(conn->error_info);
703 goto err; /* OOM */
704 }
705 conn->unix_socket.l = strlen(conn->unix_socket.s);
706 }
707
708 SET_EMPTY_ERROR(conn->error_info);
709
710 mysqlnd_local_infile_default(conn);
711
712 if (FAIL == conn->m->execute_init_commands(conn)) {
713 goto err;
714 }
715
716 MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
717 if (reconnect) {
718 MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
719 }
720 if (conn->persistent) {
721 MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
722 }
723
724 DBG_INF_FMT("connection_id=%" PRIu64, conn->thread_id);
725
726 DBG_RETURN(PASS);
727 }
728 err:
729 if (transport.s) {
730 mnd_sprintf_free(transport.s);
731 transport.s = NULL;
732 }
733
734 DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme.s);
735 if (!conn->error_info->error_no) {
736 char * msg;
737 mnd_sprintf(&msg, 0, "%s (trying to connect via %s)",conn->error_info->error, conn->scheme.s);
738 SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, msg);
739 mnd_sprintf_free(msg);
740 }
741
742 conn->m->free_contents(conn);
743 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
744 DBG_RETURN(FAIL);
745 }
746 /* }}} */
747
748
749 /* {{{ mysqlnd_conn::connect */
750 static enum_func_status
751 MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
752 const MYSQLND_CSTRING hostname,
753 const MYSQLND_CSTRING username,
754 const MYSQLND_CSTRING password,
755 const MYSQLND_CSTRING database,
756 unsigned int port,
757 const MYSQLND_CSTRING socket_or_pipe,
758 unsigned int mysql_flags)
759 {
760 MYSQLND_CONN_DATA * conn = conn_handle->data;
761
762 DBG_ENTER("mysqlnd_conn::connect");
763
764 mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
765 if (hostname.l > 0) {
766 mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
767 }
768 DBG_RETURN(conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags));
769 }
770 /* }}} */
771
772
773 /* {{{ mysqlnd_conn_data::query */
774 /*
775 If conn->error_info->error_no is not zero, then we had an error.
776 Still the result from the query is PASS
777 */
778 static enum_func_status
779 MYSQLND_METHOD(mysqlnd_conn_data, query)(MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len)
780 {
781 enum_func_status ret = FAIL;
782 DBG_ENTER("mysqlnd_conn_data::query");
783 DBG_INF_FMT("conn=%p conn=%" PRIu64 " query=%s", conn, conn->thread_id, query);
784
785 if (PASS == conn->m->send_query(conn, query, query_len, NULL, NULL) &&
786 PASS == conn->m->reap_query(conn))
787 {
788 ret = PASS;
789 if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
790 MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
791 }
792 }
793 DBG_RETURN(ret);
794 }
795 /* }}} */
796
797
798 /* {{{ mysqlnd_conn_data::send_query */
799 static enum_func_status
800 MYSQLND_METHOD(mysqlnd_conn_data, send_query)(
801 MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len,
802 zval *read_cb, zval *err_cb)
803 {
804 DBG_ENTER("mysqlnd_conn_data::send_query");
805 DBG_INF_FMT("conn=%" PRIu64 " query=%s", conn->thread_id, query);
806 DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
807
808 const MYSQLND_CSTRING query_string = {query, query_len};
809 enum_func_status ret = conn->command->query(conn, query_string);
810 DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
811 DBG_RETURN(ret);
812 }
813 /* }}} */
814
815
816 /* {{{ mysqlnd_conn_data::reap_query */
817 static enum_func_status
818 MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn)
819 {
820 DBG_ENTER("mysqlnd_conn_data::reap_query");
821 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
822
823 DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
824 enum_func_status ret = conn->command->reap_result(conn);
825 DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
826 DBG_RETURN(ret);
827 }
828 /* }}} */
829
830
831 /* {{{ mysqlnd_conn_data::list_method */
832 MYSQLND_RES *
833 MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * const query, const char * const achtung_wild, const char * const par1)
834 {
835 char * show_query = NULL;
836 size_t show_query_len;
837 MYSQLND_RES * result = NULL;
838
839 DBG_ENTER("mysqlnd_conn_data::list_method");
840 DBG_INF_FMT("conn=%" PRIu64 " query=%s wild=%p", conn->thread_id, query, achtung_wild);
841
842 if (par1) {
843 if (achtung_wild) {
844 show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
845 } else {
846 show_query_len = mnd_sprintf(&show_query, 0, query, par1);
847 }
848 } else {
849 if (achtung_wild) {
850 show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
851 } else {
852 show_query_len = strlen(show_query = (char *)query);
853 }
854 }
855
856 if (PASS == conn->m->query(conn, show_query, show_query_len)) {
857 result = conn->m->store_result(conn);
858 }
859 if (show_query != query) {
860 mnd_sprintf_free(show_query);
861 }
862 DBG_RETURN(result);
863 }
864 /* }}} */
865
866
867 /* {{{ mysqlnd_conn_data::err_no */
868 static unsigned int
869 MYSQLND_METHOD(mysqlnd_conn_data, err_no)(const MYSQLND_CONN_DATA * const conn)
870 {
871 return conn->error_info->error_no;
872 }
873 /* }}} */
874
875
876 /* {{{ mysqlnd_conn_data::error */
877 static const char *
878 MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn)
879 {
880 return conn->error_info->error;
881 }
882 /* }}} */
883
884
885 /* {{{ mysqlnd_conn_data::sqlstate */
886 static const char *
887 MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn)
888 {
889 return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
890 }
891 /* }}} */
892
893
894 /* {{{ mysqlnd_old_escape_string */
895 PHPAPI zend_ulong
896 mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len)
897 {
898 DBG_ENTER("mysqlnd_old_escape_string");
899 DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len));
900 }
901 /* }}} */
902
903
904 /* {{{ mysqlnd_conn_data::ssl_set */
905 static enum_func_status
906 MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
907 const char * const ca, const char * const capath, const char * const cipher)
908 {
909 MYSQLND_VIO * vio = conn->vio;
910 DBG_ENTER("mysqlnd_conn_data::ssl_set");
911
912 enum_func_status ret = (
913 PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_KEY, key) &&
914 PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CERT, cert) &&
915 PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CA, ca) &&
916 PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CAPATH, capath) &&
917 PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CIPHER, cipher)) ? PASS : FAIL;
918 DBG_RETURN(ret);
919 }
920 /* }}} */
921
922
923 /* {{{ mysqlnd_conn_data::escape_string */
924 static zend_ulong
925 MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len)
926 {
927 zend_ulong ret = FAIL;
928 DBG_ENTER("mysqlnd_conn_data::escape_string");
929 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
930
931 DBG_INF_FMT("server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
932 if (UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
933 ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len);
934 } else {
935 ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len);
936 }
937 DBG_RETURN(ret);
938 }
939 /* }}} */
940
941
942 /* {{{ mysqlnd_conn_data::dump_debug_info */
943 static enum_func_status
944 MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn)
945 {
946 DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
947 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
948 DBG_RETURN(conn->command->debug(conn));
949 }
950 /* }}} */
951
952
953 /* {{{ mysqlnd_conn_data::select_db */
954 static enum_func_status
955 MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, const size_t db_len)
956 {
957 DBG_ENTER("mysqlnd_conn_data::select_db");
958 DBG_INF_FMT("conn=%" PRIu64 " db=%s", conn->thread_id, db);
959
960 const MYSQLND_CSTRING database = {db, db_len};
961 DBG_RETURN(conn->command->init_db(conn, database));
962 }
963 /* }}} */
964
965
966 /* {{{ mysqlnd_conn_data::ping */
967 static enum_func_status
968 MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn)
969 {
970 DBG_ENTER("mysqlnd_conn_data::ping");
971 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
972
973 enum_func_status ret = conn->command->ping(conn);
974 DBG_INF_FMT("ret=%u", ret);
975 DBG_RETURN(ret);
976 }
977 /* }}} */
978
979
980 /* {{{ mysqlnd_conn_data::statistic */
981 static enum_func_status
982 MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, zend_string **message)
983 {
984 DBG_ENTER("mysqlnd_conn_data::statistic");
985 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
986 DBG_RETURN(conn->command->statistics(conn, message));
987 }
988 /* }}} */
989
990
991 /* {{{ mysqlnd_conn_data::kill */
992 static enum_func_status
993 MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid)
994 {
995 DBG_ENTER("mysqlnd_conn_data::kill");
996 DBG_INF_FMT("conn=%" PRIu64 " pid=%u", conn->thread_id, pid);
997
998 const unsigned int process_id = pid;
999 /* 'unsigned char' is promoted to 'int' when passed through '...' */
1000 const unsigned int read_response = (pid != conn->thread_id);
1001
1002 DBG_RETURN(conn->command->process_kill(conn, process_id, read_response));
1003 }
1004 /* }}} */
1005
1006
1007 /* {{{ mysqlnd_conn_data::set_charset */
1008 static enum_func_status
1009 MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname)
1010 {
1011 enum_func_status ret = FAIL;
1012 const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
1013
1014 DBG_ENTER("mysqlnd_conn_data::set_charset");
1015 DBG_INF_FMT("conn=%" PRIu64 " cs=%s", conn->thread_id, csname);
1016
1017 if (!charset) {
1018 SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Invalid character set was provided");
1019 DBG_RETURN(ret);
1020 }
1021
1022 char * query;
1023 size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
1024
1025 if (FAIL == (ret = conn->m->query(conn, query, query_len)) || conn->error_info->error_no) {
1026 ret = FAIL;
1027 } else {
1028 conn->charset = charset;
1029 }
1030 mnd_sprintf_free(query);
1031
1032 DBG_INF(ret == PASS? "PASS":"FAIL");
1033 DBG_RETURN(ret);
1034 }
1035 /* }}} */
1036
1037
1038 /* {{{ mysqlnd_conn_data::refresh */
1039 static enum_func_status
1040 MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options)
1041 {
1042 DBG_ENTER("mysqlnd_conn_data::refresh");
1043 DBG_INF_FMT("conn=%" PRIu64 " options=%u", conn->thread_id, options);
1044 DBG_RETURN(conn->command->refresh(conn, options));
1045 }
1046 /* }}} */
1047
1048
1049 /* {{{ mysqlnd_conn_data::shutdown */
1050 static enum_func_status
1051 MYSQLND_METHOD(mysqlnd_conn_data, shutdown)(MYSQLND_CONN_DATA * const conn, uint8_t level)
1052 {
1053 DBG_ENTER("mysqlnd_conn_data::shutdown");
1054 DBG_INF_FMT("conn=%" PRIu64 " level=%u", conn->thread_id, level);
1055 DBG_RETURN(conn->command->shutdown(conn, level));
1056 }
1057 /* }}} */
1058
1059
1060 /* {{{ mysqlnd_send_close */
1061 static enum_func_status
1062 MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn)
1063 {
1064 enum_func_status ret = PASS;
1065 MYSQLND_VIO * vio = conn->vio;
1066 php_stream * net_stream = vio->data->m.get_stream(vio);
1067 enum mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
1068
1069 DBG_ENTER("mysqlnd_send_close");
1070 DBG_INF_FMT("conn=%" PRIu64 " vio->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
1071 DBG_INF_FMT("state=%u", state);
1072
1073 if (state >= CONN_READY) {
1074 MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_CONNECTIONS);
1075 if (conn->persistent) {
1076 MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_PERSISTENT_CONNECTIONS);
1077 }
1078 }
1079 switch (state) {
1080 case CONN_READY:
1081 DBG_INF("Connection clean, sending COM_QUIT");
1082 if (net_stream) {
1083 ret = conn->command->quit(conn);
1084 vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1085 }
1086 SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1087 break;
1088 case CONN_SENDING_LOAD_DATA:
1089 /*
1090 Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
1091 will crash (assert) a debug server.
1092 */
1093 case CONN_NEXT_RESULT_PENDING:
1094 case CONN_QUERY_SENT:
1095 case CONN_FETCHING_DATA:
1096 MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
1097 DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme.s);
1098 /*
1099 Do nothing, the connection will be brutally closed
1100 and the server will catch it and free close from its side.
1101 */
1102 ZEND_FALLTHROUGH;
1103 case CONN_ALLOCED:
1104 /*
1105 Allocated but not connected or there was failure when trying
1106 to connect with pre-allocated connect.
1107
1108 Fall-through
1109 */
1110 SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1111 ZEND_FALLTHROUGH;
1112 case CONN_QUIT_SENT:
1113 /* The user has killed its own connection */
1114 vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1115 break;
1116 }
1117
1118 DBG_RETURN(ret);
1119 }
1120 /* }}} */
1121
1122
1123 /* {{{ mysqlnd_conn_data::get_reference */
1124 static MYSQLND_CONN_DATA *
1125 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn)
1126 {
1127 DBG_ENTER("mysqlnd_conn_data::get_reference");
1128 ++conn->refcount;
1129 DBG_INF_FMT("conn=%" PRIu64 " new_refcount=%u", conn->thread_id, conn->refcount);
1130 DBG_RETURN(conn);
1131 }
1132 /* }}} */
1133
1134
1135 /* {{{ mysqlnd_conn_data::free_reference */
1136 static enum_func_status
1137 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn)
1138 {
1139 enum_func_status ret = PASS;
1140 DBG_ENTER("mysqlnd_conn_data::free_reference");
1141 DBG_INF_FMT("conn=%" PRIu64 " old_refcount=%u", conn->thread_id, conn->refcount);
1142 if (!(--conn->refcount)) {
1143 /*
1144 No multithreading issues as we don't share the connection :)
1145 This will free the object too, of course because references has
1146 reached zero.
1147 */
1148 ret = conn->m->send_close(conn);
1149 conn->m->dtor(conn);
1150 }
1151 DBG_RETURN(ret);
1152 }
1153 /* }}} */
1154
1155
1156 /* {{{ mysqlnd_conn_data::field_count */
1157 static unsigned int
1158 MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn)
1159 {
1160 return conn->field_count;
1161 }
1162 /* }}} */
1163
1164
1165 /* {{{ mysqlnd_conn_data::server_status */
1166 static unsigned int
1167 MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn)
1168 {
1169 return UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status);
1170 }
1171 /* }}} */
1172
1173
1174 /* {{{ mysqlnd_conn_data::insert_id */
1175 static uint64_t
1176 MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn)
1177 {
1178 return UPSERT_STATUS_GET_LAST_INSERT_ID(conn->upsert_status);
1179 }
1180 /* }}} */
1181
1182
1183 /* {{{ mysqlnd_conn_data::affected_rows */
1184 static uint64_t
1185 MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn)
1186 {
1187 return UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status);
1188 }
1189 /* }}} */
1190
1191
1192 /* {{{ mysqlnd_conn_data::warning_count */
1193 static unsigned int
1194 MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn)
1195 {
1196 return UPSERT_STATUS_GET_WARNINGS(conn->upsert_status);
1197 }
1198 /* }}} */
1199
1200
1201 /* {{{ mysqlnd_conn_data::info */
1202 static const char *
1203 MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn)
1204 {
1205 return conn->last_message.s;
1206 }
1207 /* }}} */
1208
1209
1210 /* {{{ mysqlnd_get_client_info */
1211 PHPAPI const char * mysqlnd_get_client_info(void)
1212 {
1213 return PHP_MYSQLND_VERSION;
1214 }
1215 /* }}} */
1216
1217
1218 /* {{{ mysqlnd_get_client_version */
1219 PHPAPI unsigned long mysqlnd_get_client_version(void)
1220 {
1221 return MYSQLND_VERSION_ID;
1222 }
1223 /* }}} */
1224
1225
1226 /* {{{ mysqlnd_conn_data::get_server_info */
1227 static const char *
1228 MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn)
1229 {
1230 return conn->server_version;
1231 }
1232 /* }}} */
1233
1234
1235 /* {{{ mysqlnd_conn_data::get_host_info */
1236 static const char *
1237 MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn)
1238 {
1239 return conn->host_info;
1240 }
1241 /* }}} */
1242
1243
1244 /* {{{ mysqlnd_conn_data::get_proto_info */
1245 static unsigned int
1246 MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn)
1247 {
1248 return conn->protocol_version;
1249 }
1250 /* }}} */
1251
1252
1253 /* {{{ mysqlnd_conn_data::charset_name */
1254 static const char *
1255 MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn)
1256 {
1257 return conn->charset->name;
1258 }
1259 /* }}} */
1260
1261
1262 /* {{{ mysqlnd_conn_data::thread_id */
1263 static uint64_t
1264 MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn)
1265 {
1266 return conn->thread_id;
1267 }
1268 /* }}} */
1269
1270
1271 /* {{{ mysqlnd_conn_data::get_server_version */
1272 static zend_ulong
1273 MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn)
1274 {
1275 zend_long major, minor, patch;
1276 char *p;
1277
1278 if (!(p = conn->server_version)) {
1279 return 0;
1280 }
1281
1282 #define MARIA_DB_VERSION_HACK_PREFIX "5.5.5-"
1283
1284 if ((conn->server_capabilities & CLIENT_PLUGIN_AUTH)
1285 && !strncmp(p, MARIA_DB_VERSION_HACK_PREFIX, sizeof(MARIA_DB_VERSION_HACK_PREFIX)-1))
1286 {
1287 p += sizeof(MARIA_DB_VERSION_HACK_PREFIX)-1;
1288 }
1289
1290 major = ZEND_STRTOL(p, &p, 10);
1291 p += 1; /* consume the dot */
1292 minor = ZEND_STRTOL(p, &p, 10);
1293 p += 1; /* consume the dot */
1294 patch = ZEND_STRTOL(p, &p, 10);
1295
1296 return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
1297 }
1298 /* }}} */
1299
1300
1301 /* {{{ mysqlnd_conn_data::more_results */
1302 static bool
1303 MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn)
1304 {
1305 DBG_ENTER("mysqlnd_conn_data::more_results");
1306 /* (conn->state == CONN_NEXT_RESULT_PENDING) too */
1307 DBG_RETURN(UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
1308 }
1309 /* }}} */
1310
1311
1312 /* {{{ mysqlnd_conn_data::next_result */
1313 static enum_func_status
1314 MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn)
1315 {
1316 DBG_ENTER("mysqlnd_conn_data::next_result");
1317 DBG_INF_FMT("conn=%" PRIu64 "", conn->thread_id);
1318
1319 SET_EMPTY_ERROR(conn->error_info);
1320
1321 if (GET_CONNECTION_STATE(&conn->state) != CONN_NEXT_RESULT_PENDING) {
1322 DBG_RETURN(FAIL);
1323 }
1324
1325 UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1326 /*
1327 We are sure that there is a result set, since conn->state is set accordingly
1328 in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
1329 */
1330 enum_func_status ret = conn->m->query_read_result_set_header(conn, NULL);
1331 if (FAIL == ret) {
1332 /*
1333 There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
1334 So there are no more results and we should just return FALSE, error_no has been set
1335 */
1336 if (!conn->error_info->error_no) {
1337 DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
1338 php_error_docref(NULL, E_WARNING, "Serious error. PID=%d", getpid());
1339 SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1340 conn->m->send_close(conn);
1341 } else {
1342 DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
1343 }
1344 DBG_RETURN(FAIL);
1345 }
1346 if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
1347 MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
1348 }
1349 DBG_RETURN(PASS);
1350 }
1351 /* }}} */
1352
1353
1354 /* {{{ mysqlnd_conn_data::change_user */
1355 static enum_func_status
1356 MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
1357 const char * user,
1358 const char * passwd,
1359 const char * db,
1360 bool silent,
1361 size_t passwd_len
1362 )
1363 {
1364 enum_func_status ret = FAIL;
1365
1366 DBG_ENTER("mysqlnd_conn_data::change_user");
1367 DBG_INF_FMT("conn=%" PRIu64 " user=%s passwd=%s db=%s silent=%u",
1368 conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
1369
1370 SET_EMPTY_ERROR(conn->error_info);
1371 UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1372
1373 if (!user) {
1374 user = "";
1375 }
1376 if (!passwd) {
1377 passwd = "";
1378 passwd_len = 0;
1379 }
1380 if (!db) {
1381 db = "";
1382 }
1383
1384 /* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
1385 ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
1386 conn->authentication_plugin_data, conn->options->auth_protocol,
1387 0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/);
1388
1389 /*
1390 Here we should close all statements. Unbuffered queries should not be a
1391 problem as we won't allow sending COM_CHANGE_USER.
1392 */
1393 DBG_INF(ret == PASS? "PASS":"FAIL");
1394 DBG_RETURN(ret);
1395 }
1396 /* }}} */
1397
1398
1399 /* {{{ mysqlnd_conn_data::set_client_option */
1400 static enum_func_status
1401 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
1402 enum_mysqlnd_client_option option,
1403 const char * const value
1404 )
1405 {
1406 enum_func_status ret = PASS;
1407 DBG_ENTER("mysqlnd_conn_data::set_client_option");
1408 DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1409
1410 switch (option) {
1411 case MYSQL_OPT_READ_TIMEOUT:
1412 case MYSQL_OPT_WRITE_TIMEOUT:
1413 case MYSQLND_OPT_SSL_KEY:
1414 case MYSQLND_OPT_SSL_CERT:
1415 case MYSQLND_OPT_SSL_CA:
1416 case MYSQLND_OPT_SSL_CAPATH:
1417 case MYSQLND_OPT_SSL_CIPHER:
1418 case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
1419 case MYSQL_OPT_CONNECT_TIMEOUT:
1420 case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
1421 ret = conn->vio->data->m.set_client_option(conn->vio, option, value);
1422 break;
1423 case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
1424 case MYSQL_OPT_COMPRESS:
1425 case MYSQL_SERVER_PUBLIC_KEY:
1426 ret = conn->protocol_frame_codec->data->m.set_client_option(conn->protocol_frame_codec, option, value);
1427 break;
1428 case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
1429 conn->options->int_and_float_native = *(unsigned int*) value;
1430 break;
1431 case MYSQL_OPT_LOCAL_INFILE:
1432 if (value && (*(unsigned int*) value) ? 1 : 0) {
1433 conn->options->flags |= CLIENT_LOCAL_FILES;
1434 } else {
1435 conn->options->flags &= ~CLIENT_LOCAL_FILES;
1436 }
1437 break;
1438 case MYSQL_OPT_LOAD_DATA_LOCAL_DIR:
1439 {
1440 if (conn->options->local_infile_directory) {
1441 mnd_pefree(conn->options->local_infile_directory, conn->persistent);
1442 }
1443
1444 if (!value || (*value == '\0')) {
1445 conn->options->local_infile_directory = NULL;
1446 } else {
1447 conn->options->local_infile_directory = mnd_pestrdup(value, conn->persistent);
1448 }
1449 break;
1450 }
1451 case MYSQL_INIT_COMMAND:
1452 {
1453 char ** new_init_commands;
1454 char * new_command;
1455 /* when num_commands is 0, then realloc will be effectively a malloc call, internally */
1456 /* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
1457 new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
1458 conn->options->init_commands = new_init_commands;
1459 new_command = mnd_pestrdup(value, conn->persistent);
1460 conn->options->init_commands[conn->options->num_commands] = new_command;
1461 ++conn->options->num_commands;
1462 break;
1463 }
1464 case MYSQL_READ_DEFAULT_FILE:
1465 case MYSQL_READ_DEFAULT_GROUP:
1466 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1467 case MYSQL_SET_CLIENT_IP:
1468 case MYSQL_REPORT_DATA_TRUNCATION:
1469 #endif
1470 /* currently not supported. Todo!! */
1471 break;
1472 case MYSQL_SET_CHARSET_NAME:
1473 {
1474 char * new_charset_name;
1475 if (!mysqlnd_find_charset_name(value)) {
1476 SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
1477 ret = FAIL;
1478 break;
1479 }
1480
1481 new_charset_name = mnd_pestrdup(value, conn->persistent);
1482 if (conn->options->charset_name) {
1483 mnd_pefree(conn->options->charset_name, conn->persistent);
1484 }
1485 conn->options->charset_name = new_charset_name;
1486 DBG_INF_FMT("charset=%s", conn->options->charset_name);
1487 break;
1488 }
1489 case MYSQL_OPT_NAMED_PIPE:
1490 conn->options->protocol = MYSQL_PROTOCOL_PIPE;
1491 break;
1492 case MYSQL_OPT_PROTOCOL:
1493 if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
1494 conn->options->protocol = *(unsigned int*) value;
1495 }
1496 break;
1497 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1498 case MYSQL_SET_CHARSET_DIR:
1499 case MYSQL_OPT_RECONNECT:
1500 /* we don't need external character sets, all character sets are
1501 compiled in. For compatibility we just ignore this setting.
1502 Same for protocol, we don't support old protocol */
1503 case MYSQL_OPT_USE_REMOTE_CONNECTION:
1504 case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
1505 case MYSQL_OPT_GUESS_CONNECTION:
1506 /* todo: throw an error, we don't support embedded */
1507 break;
1508 #endif
1509 case MYSQLND_OPT_MAX_ALLOWED_PACKET:
1510 if (*(unsigned int*) value > (1<<16)) {
1511 conn->options->max_allowed_packet = *(unsigned int*) value;
1512 }
1513 break;
1514 case MYSQLND_OPT_AUTH_PROTOCOL:
1515 {
1516 char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
1517 if (conn->options->auth_protocol) {
1518 mnd_pefree(conn->options->auth_protocol, conn->persistent);
1519 }
1520 conn->options->auth_protocol = new_auth_protocol;
1521 DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
1522 break;
1523 }
1524 case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
1525 if (value && (*(unsigned int*) value) ? 1 : 0) {
1526 conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1527 } else {
1528 conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1529 }
1530 break;
1531 case MYSQL_OPT_CONNECT_ATTR_RESET:
1532 if (conn->options->connect_attr) {
1533 DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1534 zend_hash_clean(conn->options->connect_attr);
1535 }
1536 break;
1537 case MYSQL_OPT_CONNECT_ATTR_DELETE:
1538 if (conn->options->connect_attr && value) {
1539 DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1540 zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
1541 DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
1542 }
1543 break;
1544 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1545 case MYSQL_SHARED_MEMORY_BASE_NAME:
1546 case MYSQL_OPT_USE_RESULT:
1547 case MYSQL_SECURE_AUTH:
1548 /* not sure, todo ? */
1549 #endif
1550 default:
1551 ret = FAIL;
1552 }
1553 DBG_RETURN(ret);
1554 }
1555 /* }}} */
1556
1557
1558 /* {{{ mysqlnd_conn_data::set_client_option_2d */
1559 static enum_func_status
1560 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
1561 const enum_mysqlnd_client_option option,
1562 const char * const key,
1563 const char * const value
1564 )
1565 {
1566 enum_func_status ret = PASS;
1567 DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
1568 DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
1569
1570 switch (option) {
1571 case MYSQL_OPT_CONNECT_ATTR_ADD:
1572 if (!conn->options->connect_attr) {
1573 DBG_INF("Initializing connect_attr hash");
1574 conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
1575 zend_hash_init(conn->options->connect_attr, 0, NULL, conn->persistent ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR, conn->persistent);
1576 }
1577 DBG_INF_FMT("Adding [%s][%s]", key, value);
1578 {
1579 zval attrz;
1580 zend_string *str;
1581
1582 if (conn->persistent) {
1583 str = zend_string_init(key, strlen(key), 1);
1584 GC_MAKE_PERSISTENT_LOCAL(str);
1585 ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
1586 GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(attrz));
1587 } else {
1588 str = zend_string_init(key, strlen(key), 0);
1589 ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 0));
1590 }
1591 zend_hash_update(conn->options->connect_attr, str, &attrz);
1592 zend_string_release_ex(str, 1);
1593 }
1594 break;
1595 default:
1596 ret = FAIL;
1597 }
1598 DBG_RETURN(ret);
1599 }
1600 /* }}} */
1601
1602
1603 /* {{{ mysqlnd_conn_data::use_result */
1604 static MYSQLND_RES *
1605 MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn)
1606 {
1607 DBG_ENTER("mysqlnd_conn_data::use_result");
1608 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
1609
1610 if (!conn->current_result) {
1611 DBG_RETURN(NULL);
1612 }
1613
1614 /* Nothing to store for UPSERT/LOAD DATA */
1615 if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1616 SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1617 DBG_ERR("Command out of sync");
1618 DBG_RETURN(NULL);
1619 }
1620
1621 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
1622
1623 conn->current_result->conn = conn->m->get_reference(conn);
1624
1625 MYSQLND_RES *result = conn->current_result->m.use_result(conn->current_result, FALSE);
1626 if (!result) {
1627 conn->current_result->m.free_result(conn->current_result, TRUE);
1628 }
1629 conn->current_result = NULL;
1630 DBG_RETURN(result);
1631 }
1632 /* }}} */
1633
1634
1635 /* {{{ mysqlnd_conn_data::store_result */
1636 static MYSQLND_RES *
1637 MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn)
1638 {
1639 DBG_ENTER("mysqlnd_conn_data::store_result");
1640 DBG_INF_FMT("conn=%" PRIu64 " conn=%p", conn->thread_id, conn);
1641
1642 if (!conn->current_result) {
1643 DBG_RETURN(NULL);
1644 }
1645
1646 /* Nothing to store for UPSERT/LOAD DATA*/
1647 if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1648 SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1649 DBG_ERR("Command out of sync");
1650 DBG_RETURN(NULL);
1651 }
1652
1653 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
1654 MYSQLND_RES *result = conn->current_result->m.store_result(conn->current_result, conn, NULL);
1655 if (!result) {
1656 conn->current_result->m.free_result(conn->current_result, TRUE);
1657 }
1658 conn->current_result = NULL;
1659 DBG_RETURN(result);
1660 }
1661 /* }}} */
1662
1663
1664 /* {{{ mysqlnd_conn_data::get_connection_stats */
1665 static void
1666 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
1667 zval * return_value ZEND_FILE_LINE_DC)
1668 {
1669 DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
1670 mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
1671 DBG_VOID_RETURN;
1672 }
1673 /* }}} */
1674
1675
1676 /* {{{ mysqlnd_conn_data::set_autocommit */
1677 static enum_func_status
1678 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
1679 {
1680 DBG_ENTER("mysqlnd_conn_data::set_autocommit");
1681 DBG_RETURN(conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1));
1682 }
1683 /* }}} */
1684
1685
1686 /* {{{ mysqlnd_conn_data::tx_commit */
1687 static enum_func_status
1688 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
1689 {
1690 return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
1691 }
1692 /* }}} */
1693
1694
1695 /* {{{ mysqlnd_conn_data::tx_rollback */
1696 static enum_func_status
1697 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
1698 {
1699 return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
1700 }
1701 /* }}} */
1702
1703
1704 /* {{{ mysqlnd_tx_cor_options_to_string */
1705 static void
1706 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
1707 {
1708 if ((mode & TRANS_COR_AND_CHAIN) && !(mode & TRANS_COR_AND_NO_CHAIN)) {
1709 if (str->s && ZSTR_LEN(str->s)) {
1710 smart_str_appendl(str, " ", sizeof(" ") - 1);
1711 }
1712 smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
1713 } else if ((mode & TRANS_COR_AND_NO_CHAIN) && !(mode & TRANS_COR_AND_CHAIN)) {
1714 if (str->s && ZSTR_LEN(str->s)) {
1715 smart_str_appendl(str, " ", sizeof(" ") - 1);
1716 }
1717 smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
1718 }
1719
1720 if ((mode & TRANS_COR_RELEASE) && !(mode & TRANS_COR_NO_RELEASE)) {
1721 if (str->s && ZSTR_LEN(str->s)) {
1722 smart_str_appendl(str, " ", sizeof(" ") - 1);
1723 }
1724 smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
1725 } else if ((mode & TRANS_COR_NO_RELEASE) && !(mode & TRANS_COR_RELEASE)) {
1726 if (str->s && ZSTR_LEN(str->s)) {
1727 smart_str_appendl(str, " ", sizeof(" ") - 1);
1728 }
1729 smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
1730 }
1731 smart_str_0(str);
1732 }
1733 /* }}} */
1734
1735
1736 /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
1737 static char *
1738 mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
1739 {
1740 char * ret = NULL;
1741 DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
1742 if (name) {
1743 bool warned = FALSE;
1744 const char * p_orig = name;
1745 char * p_copy;
1746 p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
1747 *p_copy++ = ' ';
1748 *p_copy++ = '/';
1749 *p_copy++ = '*';
1750 while (1) {
1751 register char v = *p_orig;
1752 if (v == 0) {
1753 break;
1754 }
1755 if ((v >= '0' && v <= '9') ||
1756 (v >= 'a' && v <= 'z') ||
1757 (v >= 'A' && v <= 'Z') ||
1758 v == '-' ||
1759 v == '_' ||
1760 v == ' ' ||
1761 v == '=')
1762 {
1763 *p_copy++ = v;
1764 } else if (warned == FALSE) {
1765 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");
1766 warned = TRUE;
1767 }
1768 ++p_orig;
1769 }
1770 *p_copy++ = '*';
1771 *p_copy++ = '/';
1772 *p_copy++ = 0;
1773 }
1774 DBG_RETURN(ret);
1775 }
1776 /* }}} */
1777
1778
1779 /* {{{ mysqlnd_conn_data::tx_commit_ex */
1780 static enum_func_status
1781 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const bool commit, const unsigned int flags, const char * const name)
1782 {
1783 DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
1784
1785 smart_str tmp_str = {0, 0};
1786 conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
1787 smart_str_0(&tmp_str);
1788
1789 char * query;
1790 size_t query_len;
1791 char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1792
1793 query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
1794 name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1795 smart_str_free(&tmp_str);
1796 if (name_esc) {
1797 mnd_efree(name_esc);
1798 name_esc = NULL;
1799 }
1800 if (!query) {
1801 SET_OOM_ERROR(conn->error_info);
1802 DBG_RETURN(FAIL);
1803 }
1804
1805 enum_func_status ret = conn->m->query(conn, query, query_len);
1806 mnd_sprintf_free(query);
1807 DBG_RETURN(ret);
1808 }
1809 /* }}} */
1810
1811
1812 /* {{{ mysqlnd_conn_data::tx_begin */
1813 static enum_func_status
1814 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
1815 {
1816 DBG_ENTER("mysqlnd_conn_data::tx_begin");
1817
1818 smart_str tmp_str = {0, 0};
1819 if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
1820 if (tmp_str.s) {
1821 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1822 }
1823 smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
1824 }
1825 if (mode & TRANS_START_READ_WRITE) {
1826 if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1827 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1828 }
1829 smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
1830 } else if (mode & TRANS_START_READ_ONLY) {
1831 if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
1832 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1833 }
1834 smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
1835 }
1836 smart_str_0(&tmp_str);
1837
1838 char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
1839 char * query;
1840 unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1841 smart_str_free(&tmp_str);
1842 if (name_esc) {
1843 mnd_efree(name_esc);
1844 name_esc = NULL;
1845 }
1846 if (!query) {
1847 SET_OOM_ERROR(conn->error_info);
1848 DBG_RETURN(FAIL);
1849 }
1850
1851 enum_func_status ret = conn->m->query(conn, query, query_len);
1852 mnd_sprintf_free(query);
1853 if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) && mysqlnd_stmt_errno(conn) == 1064) {
1854 SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
1855 "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
1856 }
1857
1858 DBG_RETURN(ret);
1859 }
1860 /* }}} */
1861
1862
1863 /* {{{ mysqlnd_conn_data::tx_savepoint */
1864 static enum_func_status
1865 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
1866 {
1867 DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
1868
1869 if (!name) {
1870 SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1871 DBG_RETURN(FAIL);
1872 }
1873
1874 char *query;
1875 size_t query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
1876 if (!query) {
1877 SET_OOM_ERROR(conn->error_info);
1878 DBG_RETURN(FAIL);
1879 }
1880
1881 enum_func_status ret = conn->m->query(conn, query, query_len);
1882 mnd_sprintf_free(query);
1883 DBG_RETURN(ret);
1884 }
1885 /* }}} */
1886
1887
1888 /* {{{ mysqlnd_conn_data::tx_savepoint_release */
1889 static enum_func_status
1890 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
1891 {
1892 DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
1893
1894 if (!name) {
1895 SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
1896 DBG_RETURN(FAIL);
1897 }
1898
1899 char *query;
1900 size_t query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
1901 if (!query) {
1902 SET_OOM_ERROR(conn->error_info);
1903 DBG_RETURN(FAIL);
1904 }
1905
1906 enum_func_status ret = conn->m->query(conn, query, query_len);
1907 mnd_sprintf_free(query);
1908 DBG_RETURN(ret);
1909 }
1910 /* }}} */
1911
1912
1913 /* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
1914 static size_t
1915 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const size_t flags)
1916 {
1917 unsigned int ret = 0;
1918 DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
1919 if (conn) {
1920 ret = conn->client_api_capabilities;
1921 conn->client_api_capabilities = flags;
1922 }
1923
1924 DBG_RETURN(ret);
1925 }
1926 /* }}} */
1927
1928
1929 /* {{{ mysqlnd_conn_data::get_client_api_capabilities */
1930 static size_t
1931 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
1932 {
1933 DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
1934 DBG_RETURN(conn? conn->client_api_capabilities : 0);
1935 }
1936 /* }}} */
1937
1938
1939 /* {{{ _mysqlnd_stmt_init */
1940 MYSQLND_STMT *
1941 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init)(MYSQLND_CONN_DATA * const conn)
1942 {
1943 MYSQLND_STMT * ret;
1944 DBG_ENTER("mysqlnd_conn_data::stmt_init");
1945 ret = conn->object_factory.get_prepared_statement(conn);
1946 DBG_RETURN(ret);
1947 }
1948 /* }}} */
1949
1950
1951 MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
1952 MYSQLND_METHOD(mysqlnd_conn_data, connect),
1953
1954 MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
1955 MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
1956 MYSQLND_METHOD(mysqlnd_conn_data, query),
1957 MYSQLND_METHOD(mysqlnd_conn_data, send_query),
1958 MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
1959 MYSQLND_METHOD(mysqlnd_conn_data, use_result),
1960 MYSQLND_METHOD(mysqlnd_conn_data, store_result),
1961 MYSQLND_METHOD(mysqlnd_conn_data, next_result),
1962 MYSQLND_METHOD(mysqlnd_conn_data, more_results),
1963
1964 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init),
1965
1966 MYSQLND_METHOD(mysqlnd_conn_data, shutdown),
1967 MYSQLND_METHOD(mysqlnd_conn_data, refresh),
1968
1969 MYSQLND_METHOD(mysqlnd_conn_data, ping),
1970 MYSQLND_METHOD(mysqlnd_conn_data, kill),
1971 MYSQLND_METHOD(mysqlnd_conn_data, select_db),
1972 MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
1973 MYSQLND_METHOD(mysqlnd_conn_data, change_user),
1974
1975 MYSQLND_METHOD(mysqlnd_conn_data, err_no),
1976 MYSQLND_METHOD(mysqlnd_conn_data, error),
1977 MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
1978 MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
1979
1980 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
1981
1982 MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
1983 MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
1984 MYSQLND_METHOD(mysqlnd_conn_data, statistic),
1985 MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
1986 MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
1987 MYSQLND_METHOD(mysqlnd_conn_data, info),
1988 MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
1989 MYSQLND_METHOD(mysqlnd_conn_data, list_method),
1990
1991 MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
1992 MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
1993 MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
1994 MYSQLND_METHOD(mysqlnd_conn_data, field_count),
1995
1996 MYSQLND_METHOD(mysqlnd_conn_data, server_status),
1997
1998 MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
1999 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
2000 MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
2001 MYSQLND_METHOD(mysqlnd_conn_data, free_options),
2002
2003 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
2004
2005 mysqlnd_query_read_result_set_header,
2006
2007 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
2008 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
2009
2010 MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
2011 MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
2012 MYSQLND_METHOD(mysqlnd_conn_data, send_close),
2013
2014 MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
2015 mysqlnd_result_init,
2016 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
2017 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
2018 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
2019 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
2020 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
2021 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
2022 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
2023 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
2024
2025 MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
2026 MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
2027 MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
2028 MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
2029
2030 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
2031
2032 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
2033 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities),
2034
2035 MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)
2036 MYSQLND_CLASS_METHODS_END;
2037
2038
2039 /* {{{ mysqlnd_conn::get_reference */
2040 static MYSQLND *
2041 MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
2042 {
2043 MYSQLND * ret;
2044 DBG_ENTER("mysqlnd_conn::get_reference");
2045 ret = conn->data->object_factory.clone_connection_object(conn);
2046 DBG_RETURN(ret);
2047 }
2048 /* }}} */
2049
2050
2051 /* {{{ mysqlnd_conn_data::dtor */
2052 static void
2053 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
2054 {
2055 DBG_ENTER("mysqlnd_conn::dtor");
2056 DBG_INF_FMT("conn=%" PRIu64, conn->data->thread_id);
2057
2058 conn->data->m->free_reference(conn->data);
2059
2060 mnd_pefree(conn, conn->persistent);
2061
2062 DBG_VOID_RETURN;
2063 }
2064 /* }}} */
2065
2066
2067 /* {{{ mysqlnd_conn_data::close */
2068 static enum_func_status
2069 MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, const enum_connection_close_type close_type)
2070 {
2071 MYSQLND_CONN_DATA * conn = conn_handle->data;
2072
2073 DBG_ENTER("mysqlnd_conn::close");
2074 DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
2075
2076 if (GET_CONNECTION_STATE(&conn->state) >= CONN_READY) {
2077 static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
2078 STAT_CLOSE_EXPLICIT,
2079 STAT_CLOSE_IMPLICIT,
2080 STAT_CLOSE_DISCONNECT
2081 };
2082 MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
2083 }
2084
2085 /*
2086 Close now, free_reference will try,
2087 if we are last, but that's not a problem.
2088 */
2089 enum_func_status ret = conn->m->send_close(conn);
2090
2091 conn_handle->m->dtor(conn_handle);
2092 DBG_RETURN(ret);
2093 }
2094 /* }}} */
2095
2096
2097 MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
2098 MYSQLND_METHOD(mysqlnd_conn, connect),
2099 MYSQLND_METHOD(mysqlnd_conn, clone_object),
2100 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
2101 MYSQLND_METHOD(mysqlnd_conn, close)
2102 MYSQLND_CLASS_METHODS_END;
2103
2104
2105 #include "php_network.h"
2106
2107 /* {{{ mysqlnd_stream_array_to_fd_set */
2108 MYSQLND **
2109 mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
2110 {
2111 unsigned int cnt = 0;
2112 MYSQLND **p = conn_array, **p_p;
2113 MYSQLND **ret = NULL;
2114
2115 while (*p) {
2116 const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2117 if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2118 cnt++;
2119 }
2120 p++;
2121 }
2122 if (cnt) {
2123 MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
2124 p_p = p = conn_array;
2125 while (*p) {
2126 const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2127 if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2128 *ret_p = *p;
2129 *p = NULL;
2130 ret_p++;
2131 } else {
2132 *p_p = *p;
2133 p_p++;
2134 }
2135 p++;
2136 }
2137 *ret_p = NULL;
2138 }
2139 return ret;
2140 }
2141 /* }}} */
2142
2143
2144 /* {{{ mysqlnd_stream_array_to_fd_set */
2145 static unsigned int
2146 mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
2147 {
2148 php_socket_t this_fd;
2149 php_stream *stream = NULL;
2150 unsigned int cnt = 0;
2151 MYSQLND **p = conn_array;
2152 DBG_ENTER("mysqlnd_stream_array_to_fd_set");
2153
2154 while (*p) {
2155 /* get the fd.
2156 * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
2157 * when casting. It is only used here so that the buffered data warning
2158 * is not displayed.
2159 * */
2160 stream = (*p)->data->vio->data->m.get_stream((*p)->data->vio);
2161 DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*p)->data->thread_id, stream);
2162 if (stream != NULL &&
2163 SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) &&
2164 ZEND_VALID_SOCKET(this_fd))
2165 {
2166
2167 PHP_SAFE_FD_SET(this_fd, fds);
2168
2169 if (this_fd > *max_fd) {
2170 *max_fd = this_fd;
2171 }
2172 ++cnt;
2173 }
2174 ++p;
2175 }
2176 DBG_RETURN(cnt ? 1 : 0);
2177 }
2178 /* }}} */
2179
2180
2181 /* {{{ mysqlnd_stream_array_from_fd_set */
2182 static unsigned int
2183 mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
2184 {
2185 php_socket_t this_fd;
2186 php_stream *stream = NULL;
2187 unsigned int ret = 0;
2188 bool disproportion = FALSE;
2189 MYSQLND **fwd = conn_array, **bckwd = conn_array;
2190 DBG_ENTER("mysqlnd_stream_array_from_fd_set");
2191
2192 while (*fwd) {
2193 stream = (*fwd)->data->vio->data->m.get_stream((*fwd)->data->vio);
2194 DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*fwd)->data->thread_id, stream);
2195 if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
2196 (void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
2197 if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
2198 if (disproportion) {
2199 *bckwd = *fwd;
2200 }
2201 ++bckwd;
2202 ++fwd;
2203 ++ret;
2204 continue;
2205 }
2206 }
2207 disproportion = TRUE;
2208 ++fwd;
2209 }
2210 *bckwd = NULL;/* NULL-terminate the list */
2211
2212 DBG_RETURN(ret);
2213 }
2214 /* }}} */
2215
2216
2217 #ifndef PHP_WIN32
2218 #define php_select(m, r, w, e, t) select(m, r, w, e, t)
2219 #else
2220 #include "win32/select.h"
2221 #endif
2222
2223
2224 /* {{{ mysqlnd_poll */
2225 PHPAPI enum_func_status
2226 mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
2227 {
2228 struct timeval tv;
2229 struct timeval *tv_p = NULL;
2230 fd_set rfds, wfds, efds;
2231 php_socket_t max_fd = 0;
2232 int retval, sets = 0;
2233 int set_count, max_set_count = 0;
2234
2235 DBG_ENTER("_mysqlnd_poll");
2236 if (sec < 0 || usec < 0) {
2237 php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
2238 DBG_RETURN(FAIL);
2239 }
2240
2241 FD_ZERO(&rfds);
2242 FD_ZERO(&wfds);
2243 FD_ZERO(&efds);
2244
2245 if (r_array != NULL) {
2246 *dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
2247 set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
2248 if (set_count > max_set_count) {
2249 max_set_count = set_count;
2250 }
2251 sets += set_count;
2252 }
2253
2254 if (e_array != NULL) {
2255 set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
2256 if (set_count > max_set_count) {
2257 max_set_count = set_count;
2258 }
2259 sets += set_count;
2260 }
2261
2262 if (!sets) {
2263 php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2264 DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2265 DBG_RETURN(FAIL);
2266 }
2267
2268 PHP_SAFE_MAX_FD(max_fd, max_set_count);
2269
2270 /* Solaris + BSD do not like microsecond values which are >= 1 sec */
2271 if (usec > 999999) {
2272 tv.tv_sec = sec + (usec / 1000000);
2273 tv.tv_usec = usec % 1000000;
2274 } else {
2275 tv.tv_sec = sec;
2276 tv.tv_usec = usec;
2277 }
2278
2279 tv_p = &tv;
2280
2281 retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
2282
2283 if (retval == -1) {
2284 php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
2285 errno, strerror(errno), max_fd);
2286 DBG_RETURN(FAIL);
2287 }
2288
2289 if (r_array != NULL) {
2290 mysqlnd_stream_array_from_fd_set(r_array, &rfds);
2291 }
2292 if (e_array != NULL) {
2293 mysqlnd_stream_array_from_fd_set(e_array, &efds);
2294 }
2295
2296 *desc_num = retval;
2297 DBG_RETURN(PASS);
2298 }
2299 /* }}} */
2300
2301
2302 /* {{{ mysqlnd_connect */
2303 PHPAPI MYSQLND * mysqlnd_connection_connect(MYSQLND * conn_handle,
2304 const char * const host,
2305 const char * const user,
2306 const char * const passwd, unsigned int passwd_len,
2307 const char * const db, unsigned int db_len,
2308 unsigned int port,
2309 const char * const sock_or_pipe,
2310 unsigned int mysql_flags,
2311 unsigned int client_api_flags
2312 )
2313 {
2314 enum_func_status ret = FAIL;
2315 bool self_alloced = FALSE;
2316 MYSQLND_CSTRING hostname = { host, host? strlen(host) : 0 };
2317 MYSQLND_CSTRING username = { user, user? strlen(user) : 0 };
2318 MYSQLND_CSTRING password = { passwd, passwd_len };
2319 MYSQLND_CSTRING database = { db, db_len };
2320 MYSQLND_CSTRING socket_or_pipe = { sock_or_pipe, sock_or_pipe? strlen(sock_or_pipe) : 0 };
2321
2322 DBG_ENTER("mysqlnd_connect");
2323 DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host? host:"", user? user:"", db? db:"", port, mysql_flags);
2324
2325 if (!conn_handle) {
2326 self_alloced = TRUE;
2327 if (!(conn_handle = mysqlnd_connection_init(client_api_flags, FALSE, NULL))) {
2328 /* OOM */
2329 DBG_RETURN(NULL);
2330 }
2331 }
2332
2333 ret = conn_handle->m->connect(conn_handle, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
2334
2335 if (ret == FAIL) {
2336 if (self_alloced) {
2337 /*
2338 We have alloced, thus there are no references to this
2339 object - we are free to kill it!
2340 */
2341 conn_handle->m->dtor(conn_handle);
2342 }
2343 DBG_RETURN(NULL);
2344 }
2345 DBG_RETURN(conn_handle);
2346 }
2347 /* }}} */
2348
2349
2350 /* {{{ mysqlnd_connection_init */
2351 PHPAPI MYSQLND *
2352 mysqlnd_connection_init(const size_t client_flags, const bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory)
2353 {
2354 MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
2355 MYSQLND * ret;
2356 DBG_ENTER("mysqlnd_connection_init");
2357 ret = factory->get_connection(factory, persistent);
2358 if (ret && ret->data) {
2359 ret->data->m->negotiate_client_api_capabilities(ret->data, client_flags);
2360 }
2361 DBG_RETURN(ret);
2362 }
2363 /* }}} */
2364