PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysqlnd - mysqlnd.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1000 1223 81.8 %
Date: 2014-09-29 Functions: 69 73 94.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 5                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2006-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Andrey Hristov <andrey@mysql.com>                           |
      16             :   |          Ulf Wendel <uwendel@mysql.com>                              |
      17             :   |          Georg Richter <georg@mysql.com>                             |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : #include "php.h"
      23             : #include "mysqlnd.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             : 
      31             : /*
      32             :   TODO :
      33             :   - Don't bind so tightly the metadata with the result set. This means
      34             :         that the metadata reading should not expect a MYSQLND_RES pointer, it
      35             :         does not need it, but return a pointer to the metadata (MYSQLND_FIELD *).
      36             :         For normal statements we will then just assign it to a member of
      37             :         MYSQLND_RES. For PS statements, it will stay as part of the statement
      38             :         (MYSQLND_STMT) between prepare and execute. At execute the new metadata
      39             :         will be sent by the server, so we will discard the old one and then
      40             :         finally attach it to the result set. This will make the code more clean,
      41             :         as a prepared statement won't have anymore stmt->result != NULL, as it
      42             :         is now, just to have where to store the metadata.
      43             : 
      44             :   - Change mysqlnd_simple_command to accept a heap dynamic array of MYSQLND_STRING
      45             :         terminated by a string with ptr being NULL. Thus, multi-part messages can be
      46             :         sent to the network like writev() and this can save at least for
      47             :         mysqlnd_stmt_send_long_data() new malloc. This change will probably make the
      48             :         code in few other places cleaner.
      49             : */
      50             : 
      51             : extern MYSQLND_CHARSET *mysqlnd_charsets;
      52             : 
      53             : 
      54             : 
      55             : PHPAPI const char * const mysqlnd_old_passwd  = "mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. "
      56             : "Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will "
      57             : "store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords "
      58             : "flag from your my.cnf file";
      59             : 
      60             : PHPAPI const char * const mysqlnd_server_gone = "MySQL server has gone away";
      61             : PHPAPI const char * const mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
      62             : PHPAPI const char * const mysqlnd_out_of_memory = "Out of memory";
      63             : 
      64             : PHPAPI MYSQLND_STATS *mysqlnd_global_stats = NULL;
      65             : 
      66             : 
      67             : /* {{{ mysqlnd_conn_data::free_options */
      68             : static void
      69        1875 : MYSQLND_METHOD(mysqlnd_conn_data, free_options)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
      70             : {
      71        1875 :         zend_bool pers = conn->persistent;
      72             : 
      73        1875 :         if (conn->options->charset_name) {
      74           5 :                 mnd_pefree(conn->options->charset_name, pers);
      75           5 :                 conn->options->charset_name = NULL;
      76             :         }
      77        1875 :         if (conn->options->auth_protocol) {
      78        1766 :                 mnd_pefree(conn->options->auth_protocol, pers);
      79        1766 :                 conn->options->auth_protocol = NULL;
      80             :         }
      81        1875 :         if (conn->options->num_commands) {
      82             :                 unsigned int i;
      83          24 :                 for (i = 0; i < conn->options->num_commands; i++) {
      84             :                         /* allocated with pestrdup */
      85          13 :                         mnd_pefree(conn->options->init_commands[i], pers);
      86             :                 }
      87          11 :                 mnd_pefree(conn->options->init_commands, pers);
      88          11 :                 conn->options->init_commands = NULL;
      89             :         }
      90        1875 :         if (conn->options->cfg_file) {
      91           0 :                 mnd_pefree(conn->options->cfg_file, pers);
      92           0 :                 conn->options->cfg_file = NULL;
      93             :         }
      94        1875 :         if (conn->options->cfg_section) {
      95           0 :                 mnd_pefree(conn->options->cfg_section, pers);
      96           0 :                 conn->options->cfg_section = NULL;
      97             :         }
      98        1875 : }
      99             : /* }}} */
     100             : 
     101             : 
     102             : /* {{{ mysqlnd_conn_data::free_contents */
     103             : static void
     104        1930 : MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     105             : {
     106        1930 :         zend_bool pers = conn->persistent;
     107             : 
     108        1930 :         DBG_ENTER("mysqlnd_conn_data::free_contents");
     109             : 
     110        1930 :         mysqlnd_local_infile_default(conn);
     111        1930 :         if (conn->current_result) {
     112           4 :                 conn->current_result->m.free_result(conn->current_result, TRUE TSRMLS_CC);
     113           4 :                 conn->current_result = NULL;
     114             :         }
     115             : 
     116        1930 :         if (conn->net) {
     117        1930 :                 conn->net->m.free_contents(conn->net TSRMLS_CC);
     118             :         }
     119             : 
     120        1930 :         DBG_INF("Freeing memory of members");
     121             : 
     122        1930 :         if (conn->host) {
     123           2 :                 mnd_pefree(conn->host, pers);
     124           2 :                 conn->host = NULL;
     125             :         }
     126        1930 :         if (conn->user) {
     127        1801 :                 mnd_pefree(conn->user, pers);
     128        1801 :                 conn->user = NULL;
     129             :         }
     130        1930 :         if (conn->passwd) {
     131        1801 :                 mnd_pefree(conn->passwd, pers);
     132        1801 :                 conn->passwd = NULL;
     133             :         }
     134        1930 :         if (conn->connect_or_select_db) {
     135        1801 :                 mnd_pefree(conn->connect_or_select_db, pers);
     136        1801 :                 conn->connect_or_select_db = NULL;
     137             :         }
     138        1930 :         if (conn->unix_socket) {
     139        1799 :                 mnd_pefree(conn->unix_socket, pers);
     140        1799 :                 conn->unix_socket = NULL;
     141             :         }
     142        1930 :         DBG_INF_FMT("scheme=%s", conn->scheme);
     143        1930 :         if (conn->scheme) {
     144        1852 :                 mnd_pefree(conn->scheme, pers);
     145        1852 :                 conn->scheme = NULL;
     146             :         }
     147        1930 :         if (conn->server_version) {
     148        1838 :                 mnd_pefree(conn->server_version, pers);
     149        1838 :                 conn->server_version = NULL;
     150             :         }
     151        1930 :         if (conn->host_info) {
     152        1801 :                 mnd_pefree(conn->host_info, pers);
     153        1801 :                 conn->host_info = NULL;
     154             :         }
     155        1930 :         if (conn->auth_plugin_data) {
     156        1835 :                 mnd_pefree(conn->auth_plugin_data, pers);
     157        1835 :                 conn->auth_plugin_data = NULL;
     158             :         }
     159        1930 :         if (conn->last_message) {
     160          64 :                 mnd_pefree(conn->last_message, pers);
     161          64 :                 conn->last_message = NULL;
     162             :         }
     163        1930 :         if (conn->error_info->error_list) {
     164        1878 :                 zend_llist_clean(conn->error_info->error_list);
     165        1878 :                 mnd_pefree(conn->error_info->error_list, pers);
     166        1878 :                 conn->error_info->error_list = NULL;
     167             :         }
     168        1930 :         conn->charset = NULL;
     169        1930 :         conn->greet_charset = NULL;
     170             : 
     171        1930 :         DBG_VOID_RETURN;
     172             : }
     173             : /* }}} */
     174             : 
     175             : 
     176             : /* {{{ mysqlnd_conn_data::dtor */
     177             : static void
     178        1875 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     179             : {
     180        1875 :         DBG_ENTER("mysqlnd_conn_data::dtor");
     181        1875 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
     182             : 
     183        1875 :         conn->m->free_contents(conn TSRMLS_CC);
     184        1875 :         conn->m->free_options(conn TSRMLS_CC);
     185             : 
     186        1875 :         if (conn->net) {
     187        1875 :                 mysqlnd_net_free(conn->net, conn->stats, conn->error_info TSRMLS_CC);
     188        1875 :                 conn->net = NULL;
     189             :         }
     190             : 
     191        1875 :         if (conn->protocol) {
     192        1875 :                 mysqlnd_protocol_free(conn->protocol TSRMLS_CC);
     193        1875 :                 conn->protocol = NULL;
     194             :         }
     195             : 
     196        1875 :         if (conn->stats) {
     197        1875 :                 mysqlnd_stats_end(conn->stats);
     198             :         }
     199             : 
     200        1875 :         mnd_pefree(conn, conn->persistent);
     201             : 
     202        1875 :         DBG_VOID_RETURN;
     203             : }
     204             : /* }}} */
     205             : 
     206             : 
     207             : /* {{{ mysqlnd_conn_data::simple_command_handle_response */
     208             : static enum_func_status
     209         436 : MYSQLND_METHOD(mysqlnd_conn_data, simple_command_handle_response)(MYSQLND_CONN_DATA * conn, enum mysqlnd_packet_type ok_packet,
     210             :                                                                                                                          zend_bool silent, enum php_mysqlnd_server_command command,
     211             :                                                                                                                          zend_bool ignore_upsert_status TSRMLS_DC)
     212             : {
     213         436 :         enum_func_status ret = FAIL;
     214             : 
     215         436 :         DBG_ENTER("mysqlnd_conn_data::simple_command_handle_response");
     216         436 :         DBG_INF_FMT("silent=%u packet=%u command=%s", silent, ok_packet, mysqlnd_command_to_text[command]);
     217             : 
     218         436 :         switch (ok_packet) {
     219             :                 case PROT_OK_PACKET:{
     220         302 :                         MYSQLND_PACKET_OK * ok_response = conn->protocol->m.get_ok_packet(conn->protocol, FALSE TSRMLS_CC);
     221         302 :                         if (!ok_response) {
     222           0 :                                 SET_OOM_ERROR(*conn->error_info);
     223           0 :                                 break;
     224             :                         }
     225         302 :                         if (FAIL == (ret = PACKET_READ(ok_response, conn))) {
     226           0 :                                 if (!silent) {
     227           0 :                                         DBG_ERR_FMT("Error while reading %s's OK packet", mysqlnd_command_to_text[command]);
     228           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading %s's OK packet. PID=%u",
     229             :                                                                          mysqlnd_command_to_text[command], getpid());
     230             :                                 }
     231             :                         } else {
     232         302 :                                 DBG_INF_FMT("OK from server");
     233         302 :                                 if (0xFF == ok_response->field_count) {
     234             :                                         /* The server signalled error. Set the error */
     235          10 :                                         SET_CLIENT_ERROR(*conn->error_info, ok_response->error_no, ok_response->sqlstate, ok_response->error);
     236          10 :                                         ret = FAIL;
     237             :                                         /*
     238             :                                           Cover a protocol design error: error packet does not
     239             :                                           contain the server status. Therefore, the client has no way
     240             :                                           to find out whether there are more result sets of
     241             :                                           a multiple-result-set statement pending. Luckily, in 5.0 an
     242             :                                           error always aborts execution of a statement, wherever it is
     243             :                                           a multi-statement or a stored procedure, so it should be
     244             :                                           safe to unconditionally turn off the flag here.
     245             :                                         */
     246          10 :                                         conn->upsert_status->server_status &= ~SERVER_MORE_RESULTS_EXISTS;
     247          10 :                                         SET_ERROR_AFF_ROWS(conn);
     248             :                                 } else {
     249         292 :                                         SET_NEW_MESSAGE(conn->last_message, conn->last_message_len,
     250             :                                                                         ok_response->message, ok_response->message_len,
     251             :                                                                         conn->persistent);
     252             : 
     253         292 :                                         if (!ignore_upsert_status) {
     254          13 :                                                 memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
     255          13 :                                                 conn->upsert_status->warning_count = ok_response->warning_count;
     256          13 :                                                 conn->upsert_status->server_status = ok_response->server_status;
     257          13 :                                                 conn->upsert_status->affected_rows = ok_response->affected_rows;
     258          13 :                                                 conn->upsert_status->last_insert_id = ok_response->last_insert_id;
     259             :                                         }
     260             :                                 }
     261             :                         }
     262         302 :                         PACKET_FREE(ok_response);
     263         302 :                         break;
     264             :                 }
     265             :                 case PROT_EOF_PACKET:{
     266         134 :                         MYSQLND_PACKET_EOF * ok_response = conn->protocol->m.get_eof_packet(conn->protocol, FALSE TSRMLS_CC);
     267         134 :                         if (!ok_response) {
     268           0 :                                 SET_OOM_ERROR(*conn->error_info);
     269           0 :                                 break;
     270             :                         }
     271         134 :                         if (FAIL == (ret = PACKET_READ(ok_response, conn))) {
     272           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE,
     273             :                                                                  "Malformed packet");
     274           0 :                                 if (!silent) {
     275           0 :                                         DBG_ERR_FMT("Error while reading %s's EOF packet", mysqlnd_command_to_text[command]);
     276           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading %s's EOF packet. PID=%d",
     277             :                                                                          mysqlnd_command_to_text[command], getpid());
     278             :                                 }
     279         134 :                         } else if (0xFF == ok_response->field_count) {
     280             :                                 /* The server signalled error. Set the error */
     281           2 :                                 SET_CLIENT_ERROR(*conn->error_info, ok_response->error_no, ok_response->sqlstate, ok_response->error);
     282           2 :                                 SET_ERROR_AFF_ROWS(conn);
     283         132 :                         } else if (0xFE != ok_response->field_count) {
     284           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "Malformed packet");
     285           0 :                                 if (!silent) {
     286           0 :                                         DBG_ERR_FMT("EOF packet expected, field count wasn't 0xFE but 0x%2X", ok_response->field_count);
     287           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "EOF packet expected, field count wasn't 0xFE but 0x%2X",
     288           0 :                                                                         ok_response->field_count);
     289             :                                 }
     290             :                         } else {
     291         132 :                                 DBG_INF_FMT("OK from server");
     292             :                         }
     293         134 :                         PACKET_FREE(ok_response);
     294         134 :                         break;
     295             :                 }
     296             :                 default:
     297           0 :                         SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "Malformed packet");
     298           0 :                         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Wrong response packet %u passed to the function", ok_packet);
     299             :                         break;
     300             :         }
     301         436 :         DBG_INF(ret == PASS ? "PASS":"FAIL");
     302         436 :         DBG_RETURN(ret);
     303             : }
     304             : /* }}} */
     305             : 
     306             : 
     307             : /* {{{ mysqlnd_conn_data::simple_command */
     308             : static enum_func_status
     309       36629 : MYSQLND_METHOD(mysqlnd_conn_data, simple_command)(MYSQLND_CONN_DATA * conn, enum php_mysqlnd_server_command command,
     310             :                            const zend_uchar * const arg, size_t arg_len, enum mysqlnd_packet_type ok_packet, zend_bool silent,
     311             :                            zend_bool ignore_upsert_status TSRMLS_DC)
     312             : {
     313       36629 :         enum_func_status ret = PASS;
     314             :         MYSQLND_PACKET_COMMAND * cmd_packet;
     315             : 
     316       36629 :         DBG_ENTER("mysqlnd_conn_data::simple_command");
     317       36629 :         DBG_INF_FMT("command=%s ok_packet=%u silent=%u", mysqlnd_command_to_text[command], ok_packet, silent);
     318       36629 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
     319             : 
     320       36629 :         switch (CONN_GET_STATE(conn)) {
     321             :                 case CONN_READY:
     322       36598 :                         break;
     323             :                 case CONN_QUIT_SENT:
     324          12 :                         SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
     325          12 :                         DBG_ERR("Server is gone");
     326          12 :                         DBG_RETURN(FAIL);
     327             :                 default:
     328          19 :                         SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
     329          19 :                         DBG_ERR_FMT("Command out of sync. State=%u", CONN_GET_STATE(conn));
     330          19 :                         DBG_RETURN(FAIL);
     331             :         }
     332             : 
     333       36598 :         SET_ERROR_AFF_ROWS(conn);
     334       36598 :         SET_EMPTY_ERROR(*conn->error_info);
     335             : 
     336       36598 :         cmd_packet = conn->protocol->m.get_command_packet(conn->protocol, FALSE TSRMLS_CC);
     337       36598 :         if (!cmd_packet) {
     338           0 :                 SET_OOM_ERROR(*conn->error_info);
     339           0 :                 DBG_RETURN(FAIL);
     340             :         }
     341             : 
     342       36598 :         cmd_packet->command = command;
     343       36598 :         if (arg && arg_len) {
     344       34759 :                 cmd_packet->argument = arg;
     345       34759 :                 cmd_packet->arg_len  = arg_len;
     346             :         }
     347             : 
     348       36598 :         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_COM_QUIT + command - 1 /* because of COM_SLEEP */ );
     349             : 
     350       36598 :         if (! PACKET_WRITE(cmd_packet, conn)) {
     351          12 :                 if (!silent) {
     352           3 :                         DBG_ERR_FMT("Error while sending %s packet", mysqlnd_command_to_text[command]);
     353           3 :                         php_error(E_WARNING, "Error while sending %s packet. PID=%d", mysqlnd_command_to_text[command], getpid());
     354             :                 }
     355          12 :                 CONN_SET_STATE(conn, CONN_QUIT_SENT);
     356          12 :                 DBG_ERR("Server is gone");
     357          12 :                 ret = FAIL;
     358       36586 :         } else if (ok_packet != PROT_LAST) {
     359         423 :                 ret = conn->m->simple_command_handle_response(conn, ok_packet, silent, command, ignore_upsert_status TSRMLS_CC);
     360             :         }
     361             : 
     362       36598 :         PACKET_FREE(cmd_packet);
     363       36598 :         DBG_INF(ret == PASS ? "PASS":"FAIL");
     364       36598 :         DBG_RETURN(ret);
     365             : }
     366             : /* }}} */
     367             : 
     368             : 
     369             : /* {{{ mysqlnd_conn_data::set_server_option */
     370             : static enum_func_status
     371         133 : MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const conn, enum_mysqlnd_server_option option TSRMLS_DC)
     372             : {
     373         133 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_server_option);
     374             :         zend_uchar buffer[2];
     375         133 :         enum_func_status ret = FAIL;
     376         133 :         DBG_ENTER("mysqlnd_conn_data::set_server_option");
     377         133 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
     378             : 
     379         133 :                 int2store(buffer, (unsigned int) option);
     380         133 :                 ret = conn->m->simple_command(conn, COM_SET_OPTION, buffer, sizeof(buffer), PROT_EOF_PACKET, FALSE, TRUE TSRMLS_CC);
     381             :         
     382         133 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
     383             :         }
     384         133 :         DBG_RETURN(ret);
     385             : }
     386             : /* }}} */
     387             : 
     388             : 
     389             : /* {{{ mysqlnd_conn_data::restart_psession */
     390             : static enum_func_status
     391          53 : MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     392             : {
     393          53 :         DBG_ENTER("mysqlnd_conn_data::restart_psession");
     394          53 :         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
     395             :         /* Free here what should not be seen by the next script */
     396          53 :         if (conn->last_message) {
     397           0 :                 mnd_pefree(conn->last_message, conn->persistent);
     398           0 :                 conn->last_message = NULL;
     399             :         }
     400          53 :         DBG_RETURN(PASS);
     401             : }
     402             : /* }}} */
     403             : 
     404             : 
     405             : /* {{{ mysqlnd_conn_data::end_psession */
     406             : static enum_func_status
     407          98 : MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     408             : {
     409          98 :         DBG_ENTER("mysqlnd_conn_data::end_psession");
     410          98 :         DBG_RETURN(PASS);
     411             : }
     412             : /* }}} */
     413             : 
     414             : 
     415             : /* {{{ mysqlnd_switch_to_ssl_if_needed */
     416             : static enum_func_status
     417        1837 : mysqlnd_switch_to_ssl_if_needed(
     418             :                         MYSQLND_CONN_DATA * conn,
     419             :                         const MYSQLND_PACKET_GREET * const greet_packet,
     420             :                         const MYSQLND_OPTIONS * const options,
     421             :                         unsigned long mysql_flags
     422             :                         TSRMLS_DC
     423             :                 )
     424             : {
     425        1837 :         enum_func_status ret = FAIL;
     426             :         const MYSQLND_CHARSET * charset;
     427             :         MYSQLND_PACKET_AUTH * auth_packet;
     428        1837 :         DBG_ENTER("mysqlnd_switch_to_ssl_if_needed");
     429             : 
     430        1837 :         auth_packet = conn->protocol->m.get_auth_packet(conn->protocol, FALSE TSRMLS_CC);
     431        1837 :         if (!auth_packet) {
     432           0 :                 SET_OOM_ERROR(*conn->error_info);
     433           0 :                 goto end;
     434             :         }
     435        1837 :         auth_packet->client_flags = mysql_flags;
     436        1837 :         auth_packet->max_packet_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
     437             : 
     438        1841 :         if (options->charset_name && (charset = mysqlnd_find_charset_name(options->charset_name))) {
     439           4 :                 auth_packet->charset_no      = charset->nr;
     440             :         } else {
     441             : #if MYSQLND_UNICODE
     442             :                 auth_packet->charset_no      = 200;/* utf8 - swedish collation, check mysqlnd_charset.c */
     443             : #else
     444        1833 :                 auth_packet->charset_no      = greet_packet->charset_no;
     445             : #endif
     446             :         }
     447             : 
     448             : #ifdef MYSQLND_SSL_SUPPORTED
     449        1837 :         if ((greet_packet->server_capabilities & CLIENT_SSL) && (mysql_flags & CLIENT_SSL)) {
     450           3 :                 zend_bool verify = mysql_flags & CLIENT_SSL_VERIFY_SERVER_CERT? TRUE:FALSE;
     451           3 :                 DBG_INF("Switching to SSL");
     452           3 :                 if (!PACKET_WRITE(auth_packet, conn)) {
     453           0 :                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
     454           0 :                         SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
     455           0 :                         goto end;
     456             :                 }
     457             : 
     458           3 :                 conn->net->m.set_client_option(conn->net, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char *) &verify TSRMLS_CC);
     459             : 
     460           3 :                 if (FAIL == conn->net->m.enable_ssl(conn->net TSRMLS_CC)) {
     461           3 :                         goto end;
     462             :                 }
     463             :         }
     464             : #endif
     465        1834 :         ret = PASS;
     466             : end:
     467        1837 :         PACKET_FREE(auth_packet);
     468        1837 :         DBG_RETURN(ret);
     469             : }
     470             : /* }}} */
     471             : 
     472             : 
     473             : /* {{{ mysqlnd_connect_run_authentication */
     474             : static enum_func_status
     475        1837 : mysqlnd_connect_run_authentication(
     476             :                         MYSQLND_CONN_DATA * conn,
     477             :                         const char * const user,
     478             :                         const char * const passwd,
     479             :                         const char * const db,
     480             :                         size_t db_len,
     481             :                         size_t passwd_len,
     482             :                         const MYSQLND_PACKET_GREET * const greet_packet,
     483             :                         const MYSQLND_OPTIONS * const options,
     484             :                         unsigned long mysql_flags
     485             :                         TSRMLS_DC)
     486             : {
     487        1837 :         enum_func_status ret = FAIL;
     488        1837 :         DBG_ENTER("mysqlnd_connect_run_authentication");
     489             : 
     490        1837 :         ret = mysqlnd_switch_to_ssl_if_needed(conn, greet_packet, options, mysql_flags TSRMLS_CC);
     491        1837 :         if (PASS == ret) {
     492        1834 :                 zend_bool first_call = TRUE;
     493             : 
     494        1834 :                 char * switch_to_auth_protocol = NULL;
     495        1834 :                 size_t switch_to_auth_protocol_len = 0;
     496        1834 :                 char * requested_protocol = NULL;
     497             :                 zend_uchar * plugin_data;
     498             :                 size_t plugin_data_len;
     499             : 
     500        1834 :                 plugin_data_len = greet_packet->auth_plugin_data_len;
     501        1834 :                 plugin_data = mnd_emalloc(plugin_data_len + 1);
     502        1834 :                 if (!plugin_data) {
     503           0 :                         ret = FAIL;
     504           0 :                         goto end;
     505             :                 }
     506        1834 :                 memcpy(plugin_data, greet_packet->auth_plugin_data, plugin_data_len);
     507        1834 :                 plugin_data[plugin_data_len] = '\0';
     508             : 
     509        1834 :                 requested_protocol = mnd_pestrdup(greet_packet->auth_protocol? greet_packet->auth_protocol: "mysql_native_password", FALSE);
     510        1834 :                 if (!requested_protocol) {
     511           0 :                         ret = FAIL;
     512           0 :                         goto end;
     513             :                 }
     514             : 
     515             :                 do {
     516             :                         struct st_mysqlnd_authentication_plugin * auth_plugin;
     517             :                         {
     518        1834 :                                 char * plugin_name = NULL;
     519             : 
     520        1834 :                                 mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
     521             : 
     522        1834 :                                 DBG_INF_FMT("looking for %s auth plugin", plugin_name);
     523        1834 :                                 auth_plugin = mysqlnd_plugin_find(plugin_name);
     524        1834 :                                 mnd_sprintf_free(plugin_name);
     525             : 
     526        1834 :                                 if (!auth_plugin) {
     527           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
     528           0 :                                         SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "The server requested authentication method unknown to the client");
     529           0 :                                         break;
     530             :                                 }
     531             :                         }
     532        1834 :                         DBG_INF("plugin found");
     533             : 
     534             :                         {
     535        1834 :                                 zend_uchar * switch_to_auth_protocol_data = NULL;
     536        1834 :                                 size_t switch_to_auth_protocol_data_len = 0;
     537        1834 :                                 zend_uchar * scrambled_data = NULL;
     538        1834 :                                 size_t scrambled_data_len = 0;
     539             : 
     540        1834 :                                 switch_to_auth_protocol = NULL;
     541        1834 :                                 switch_to_auth_protocol_len = 0;
     542             : 
     543        1834 :                                 if (conn->auth_plugin_data) {
     544           0 :                                         mnd_pefree(conn->auth_plugin_data, conn->persistent);
     545           0 :                                         conn->auth_plugin_data = NULL;
     546             :                                 }
     547        1834 :                                 conn->auth_plugin_data_len = plugin_data_len;
     548        1834 :                                 conn->auth_plugin_data = mnd_pemalloc(conn->auth_plugin_data_len, conn->persistent);
     549        1834 :                                 if (!conn->auth_plugin_data) {
     550           0 :                                         SET_OOM_ERROR(*conn->error_info);
     551           0 :                                         goto end;
     552             :                                 }
     553        1834 :                                 memcpy(conn->auth_plugin_data, plugin_data, plugin_data_len);
     554             : 
     555        1834 :                                 DBG_INF_FMT("salt=[%*s]", plugin_data_len - 1, plugin_data);
     556             :                                 /* The data should be allocated with malloc() */
     557        1834 :                                 scrambled_data =
     558        1834 :                                         auth_plugin->methods.get_auth_data(NULL, &scrambled_data_len, conn, user, passwd, passwd_len,
     559             :                                                                                                            plugin_data, plugin_data_len, options, mysql_flags TSRMLS_CC);
     560             : 
     561             : 
     562        3668 :                                 ret = mysqlnd_auth_handshake(conn, user, passwd, passwd_len, db, db_len, options, mysql_flags,
     563        1834 :                                                                                         greet_packet->charset_no,
     564             :                                                                                         first_call,
     565             :                                                                                         requested_protocol,
     566             :                                                                                         scrambled_data, scrambled_data_len,
     567             :                                                                                         &switch_to_auth_protocol, &switch_to_auth_protocol_len,
     568             :                                                                                         &switch_to_auth_protocol_data, &switch_to_auth_protocol_data_len
     569             :                                                                                         TSRMLS_CC);
     570        1834 :                                 first_call = FALSE;
     571        1834 :                                 free(scrambled_data);
     572             : 
     573        1834 :                                 DBG_INF_FMT("switch_to_auth_protocol=%s", switch_to_auth_protocol? switch_to_auth_protocol:"n/a");
     574        1834 :                                 if (requested_protocol && switch_to_auth_protocol) {
     575           0 :                                         mnd_efree(requested_protocol);
     576           0 :                                         requested_protocol = switch_to_auth_protocol;
     577             :                                 }
     578             : 
     579        1834 :                                 if (plugin_data) {
     580        1834 :                                         mnd_efree(plugin_data);
     581             :                                 }
     582        1834 :                                 plugin_data_len = switch_to_auth_protocol_data_len;
     583        1834 :                                 plugin_data = switch_to_auth_protocol_data;
     584             :                         }
     585        1834 :                         DBG_INF_FMT("conn->error_info->error_no = %d", conn->error_info->error_no);
     586        1834 :                 } while (ret == FAIL && conn->error_info->error_no == 0 && switch_to_auth_protocol != NULL);
     587        1834 :                 if (plugin_data) {
     588           0 :                         mnd_efree(plugin_data);
     589             :                 }
     590             :                 
     591        1834 :                 if (ret == PASS) {
     592        1800 :                         DBG_INF_FMT("saving requested_protocol=%s", requested_protocol);
     593        1800 :                         conn->m->set_client_option(conn, MYSQLND_OPT_AUTH_PROTOCOL, requested_protocol TSRMLS_CC);
     594             :                 }
     595             : 
     596        1834 :                 if (requested_protocol) {
     597        1834 :                         mnd_efree(requested_protocol);
     598             :                 }
     599             :         }
     600             : end:
     601        1837 :         DBG_RETURN(ret);
     602             : }
     603             : /* }}} */
     604             : 
     605             : 
     606             : /* {{{ mysqlnd_conn_data::connect */
     607             : static enum_func_status
     608        1851 : MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
     609             :                                                  const char *host, const char *user,
     610             :                                                  const char *passwd, unsigned int passwd_len,
     611             :                                                  const char *db, unsigned int db_len,
     612             :                                                  unsigned int port,
     613             :                                                  const char *socket_or_pipe,
     614             :                                                  unsigned int mysql_flags
     615             :                                                  TSRMLS_DC)
     616             : {
     617        1851 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, connect);
     618             :         size_t host_len;
     619        1851 :         zend_bool unix_socket = FALSE;
     620        1851 :         zend_bool named_pipe = FALSE;
     621        1851 :         zend_bool reconnect = FALSE;
     622        1851 :         zend_bool saved_compression = FALSE;
     623        1851 :         zend_bool local_tx_started = FALSE;
     624             : 
     625        1851 :         MYSQLND_PACKET_GREET * greet_packet = NULL;
     626             : 
     627        1851 :         DBG_ENTER("mysqlnd_conn_data::connect");
     628             : 
     629        1851 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
     630           0 :                 goto err;
     631             :         }
     632        1851 :         local_tx_started = TRUE;
     633             : 
     634        1851 :         SET_EMPTY_ERROR(*conn->error_info);
     635        1851 :         SET_ERROR_AFF_ROWS(conn);
     636             : 
     637        7404 :         DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
     638             :                                 host?host:"", user?user:"", db?db:"", port, mysql_flags,
     639        5553 :                                 conn? conn->persistent:0, conn? CONN_GET_STATE(conn):-1);
     640             : 
     641        1852 :         if (CONN_GET_STATE(conn) > CONN_ALLOCED && CONN_GET_STATE(conn) ) {
     642           1 :                 DBG_INF("Connecting on a connected handle.");
     643             : 
     644           1 :                 if (CONN_GET_STATE(conn) < CONN_QUIT_SENT) {
     645           0 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
     646           0 :                         reconnect = TRUE;
     647           0 :                         conn->m->send_close(conn TSRMLS_CC);
     648             :                 }
     649             : 
     650           1 :                 conn->m->free_contents(conn TSRMLS_CC);
     651           1 :                 MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_CONNECTIONS);
     652           1 :                 if (conn->persistent) {
     653           1 :                         MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_PERSISTENT_CONNECTIONS);
     654             :                 }
     655             :                 /* Now reconnect using the same handle */
     656           1 :                 if (conn->net->compressed) {
     657             :                         /*
     658             :                           we need to save the state. As we will re-connect, net->compressed should be off, or
     659             :                           we will look for a compression header as part of the greet message, but there will
     660             :                           be none.
     661             :                         */
     662           0 :                         saved_compression = TRUE;
     663           0 :                         conn->net->compressed = FALSE;
     664             :                 }
     665             :         } else {
     666        1850 :                 unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
     667        1850 :                 conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size TSRMLS_CC);
     668             :         }
     669             : 
     670        1851 :         if (!host || !host[0]) {
     671          14 :                 host = "localhost";
     672             :         }
     673        1851 :         if (!user) {
     674          10 :                 DBG_INF_FMT("no user given, using empty string");
     675          10 :                 user = "";
     676             :         }
     677        1851 :         if (!passwd) {
     678          12 :                 DBG_INF_FMT("no password given, using empty string");
     679          12 :                 passwd = "";
     680          12 :                 passwd_len = 0;
     681             :         }
     682        1851 :         if (!db) {
     683         234 :                 DBG_INF_FMT("no db given, using empty string");
     684         234 :                 db = "";
     685         234 :                 db_len = 0;
     686             :         }
     687             : 
     688        1851 :         host_len = strlen(host);
     689             :         {
     690        1851 :                 char * transport = NULL;
     691             :                 int transport_len;
     692             : #ifndef PHP_WIN32
     693        3691 :                 if (host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
     694        1840 :                         DBG_INF_FMT("socket=%s", socket_or_pipe? socket_or_pipe:"n/a");
     695        1840 :                         if (!socket_or_pipe) {
     696           0 :                                 socket_or_pipe = "/tmp/mysql.sock";
     697             :                         }
     698        1840 :                         transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
     699        1840 :                         unix_socket = TRUE;
     700             : #else
     701             :                 if (host_len == sizeof(".") - 1 && host[0] == '.') {
     702             :                         /* named pipe in socket */
     703             :                         if (!socket_or_pipe) {
     704             :                                 socket_or_pipe = "\\\\.\\pipe\\MySQL";
     705             :                         }
     706             :                         transport_len = mnd_sprintf(&transport, 0, "pipe://%s", socket_or_pipe);
     707             :                         named_pipe = TRUE;
     708             : #endif
     709             :                 } else {
     710          11 :                         if (!port) {
     711           1 :                                 port = 3306;
     712             :                         }
     713          11 :                         transport_len = mnd_sprintf(&transport, 0, "tcp://%s:%u", host, port);
     714             :                 }
     715        1851 :                 if (!transport) {
     716           0 :                         SET_OOM_ERROR(*conn->error_info);
     717           0 :                         goto err; /* OOM */
     718             :                 }
     719        1851 :                 DBG_INF_FMT("transport=%s conn->scheme=%s", transport, conn->scheme);
     720        1851 :                 conn->scheme = mnd_pestrndup(transport, transport_len, conn->persistent);
     721        1851 :                 conn->scheme_len = transport_len;
     722        1851 :                 mnd_sprintf_free(transport);
     723        1851 :                 transport = NULL;
     724        1851 :                 if (!conn->scheme) {
     725           0 :                         goto err; /* OOM */
     726             :                 }
     727             :         }
     728             : 
     729        1851 :         greet_packet = conn->protocol->m.get_greet_packet(conn->protocol, FALSE TSRMLS_CC);
     730        1851 :         if (!greet_packet) {
     731           0 :                 SET_OOM_ERROR(*conn->error_info);
     732           0 :                 goto err; /* OOM */
     733             :         }
     734             : 
     735        1851 :         if (FAIL == conn->net->m.connect_ex(conn->net, conn->scheme, conn->scheme_len, conn->persistent,
     736             :                                                                                 conn->stats, conn->error_info TSRMLS_CC))
     737             :         {
     738          14 :                 goto err;
     739             :         }
     740             : 
     741        1837 :         DBG_INF_FMT("stream=%p", conn->net->stream);
     742             : 
     743        1837 :         if (FAIL == PACKET_READ(greet_packet, conn)) {
     744           0 :                 DBG_ERR("Error while reading greeting packet");
     745           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
     746           0 :                 goto err;
     747        1837 :         } else if (greet_packet->error_no) {
     748           0 :                 DBG_ERR_FMT("errorno=%u error=%s", greet_packet->error_no, greet_packet->error);
     749           0 :                 SET_CLIENT_ERROR(*conn->error_info, greet_packet->error_no, greet_packet->sqlstate, greet_packet->error);
     750           0 :                 goto err;
     751        1837 :         } else if (greet_packet->pre41) {
     752           0 :                 DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet->server_version);
     753           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 "
     754             :                                                 " is not supported. Server is %-.32s", greet_packet->server_version);
     755           0 :                 SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
     756             :                                                  "Connecting to 3.22, 3.23 & 4.0 servers is not supported");
     757           0 :                 goto err;
     758             :         }
     759             : 
     760        1837 :         conn->thread_id                      = greet_packet->thread_id;
     761        1837 :         conn->protocol_version       = greet_packet->protocol_version;
     762        1837 :         conn->server_version = mnd_pestrdup(greet_packet->server_version, conn->persistent);
     763             : 
     764        1837 :         conn->greet_charset = mysqlnd_find_charset_nr(greet_packet->charset_no);
     765        1837 :         if (!conn->greet_charset) {
     766           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
     767           0 :                         "Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet->charset_no);
     768           0 :                 SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
     769             :                         "Server sent charset unknown to the client. Please, report to the developers");
     770           0 :                 goto err;
     771             :         }
     772             :         /* we allow load data local infile by default */
     773        1837 :         mysql_flags |= MYSQLND_CAPABILITIES;
     774             : 
     775        1837 :         mysql_flags |= conn->options->flags; /* use the flags from set_client_option() */
     776             : 
     777        1837 :         if (db) {
     778        1837 :                 mysql_flags |= CLIENT_CONNECT_WITH_DB;
     779             :         }
     780             : 
     781        1837 :         if (PG(open_basedir) && strlen(PG(open_basedir))) {
     782           3 :                 mysql_flags ^= CLIENT_LOCAL_FILES;
     783             :         }
     784             : 
     785             : #ifndef MYSQLND_COMPRESSION_ENABLED
     786             :         if (mysql_flags & CLIENT_COMPRESS) {
     787             :                 mysql_flags &= ~CLIENT_COMPRESS;
     788             :         }
     789             : #else
     790        1837 :         if (conn->net->options.flags & MYSQLND_NET_FLAG_USE_COMPRESSION) {
     791           0 :                 mysql_flags |= CLIENT_COMPRESS;
     792             :         }
     793             : #endif
     794             : #ifndef MYSQLND_SSL_SUPPORTED
     795             :         if (mysql_flags & CLIENT_SSL) {
     796             :                 mysql_flags &= ~CLIENT_SSL;
     797             :         }
     798             : #else
     799        7345 :         if (conn->net->options.ssl_key || conn->net->options.ssl_cert ||
     800        5508 :                 conn->net->options.ssl_ca || conn->net->options.ssl_capath || conn->net->options.ssl_cipher)
     801             :         {
     802           3 :                 mysql_flags |= CLIENT_SSL;
     803             :         }
     804             : #endif
     805             : 
     806        3674 :         if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len,
     807        1837 :                                                                                                    greet_packet, conn->options, mysql_flags TSRMLS_CC))
     808             :         {
     809          37 :                 goto err;
     810             :         }
     811             : 
     812             :         {
     813        1800 :                 CONN_SET_STATE(conn, CONN_READY);
     814             : 
     815        1800 :                 if (saved_compression) {
     816           0 :                         conn->net->compressed = TRUE;
     817             :                 }
     818             :                 /*
     819             :                   If a connect on a existing handle is performed and mysql_flags is
     820             :                   passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
     821             :                   which we set based on saved_compression.
     822             :                 */
     823        1800 :                 conn->net->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
     824             : 
     825        1800 :                 conn->user                           = mnd_pestrdup(user, conn->persistent);
     826        1800 :                 conn->user_len                       = strlen(conn->user);
     827        1800 :                 conn->passwd                 = mnd_pestrndup(passwd, passwd_len, conn->persistent);
     828        1800 :                 conn->passwd_len             = passwd_len;
     829        1800 :                 conn->port                           = port;
     830        1800 :                 conn->connect_or_select_db = mnd_pestrndup(db, db_len, conn->persistent);
     831        1800 :                 conn->connect_or_select_db_len = db_len;
     832             : 
     833        1800 :                 if (!conn->user || !conn->passwd || !conn->connect_or_select_db) {
     834           0 :                         SET_OOM_ERROR(*conn->error_info);
     835           0 :                         goto err; /* OOM */
     836             :                 }
     837             : 
     838        1802 :                 if (!unix_socket && !named_pipe) {
     839           2 :                         conn->host = mnd_pestrdup(host, conn->persistent);
     840           2 :                         if (!conn->host) {
     841           0 :                                 SET_OOM_ERROR(*conn->error_info);
     842           0 :                                 goto err; /* OOM */
     843             :                         }
     844           2 :                         conn->host_len = strlen(conn->host);
     845             :                         {
     846             :                                 char *p;
     847           2 :                                 mnd_sprintf(&p, 0, "%s via TCP/IP", conn->host);
     848           2 :                                 if (!p) {
     849           0 :                                         SET_OOM_ERROR(*conn->error_info);
     850           0 :                                         goto err; /* OOM */
     851             :                                 }
     852           2 :                                 conn->host_info =  mnd_pestrdup(p, conn->persistent);
     853           2 :                                 mnd_sprintf_free(p);
     854           2 :                                 if (!conn->host_info) {
     855           0 :                                         SET_OOM_ERROR(*conn->error_info);
     856           0 :                                         goto err; /* OOM */
     857             :                                 }
     858             :                         }
     859             :                 } else {
     860        1798 :                         conn->unix_socket = mnd_pestrdup(socket_or_pipe, conn->persistent);
     861        1798 :                         if (unix_socket) {
     862        1798 :                                 conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
     863           0 :                         } else if (named_pipe) {
     864             :                                 char *p;
     865           0 :                                 mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket);
     866           0 :                                 if (!p) {
     867           0 :                                         SET_OOM_ERROR(*conn->error_info);
     868           0 :                                         goto err; /* OOM */
     869             :                                 }
     870           0 :                                 conn->host_info =  mnd_pestrdup(p, conn->persistent);
     871           0 :                                 mnd_sprintf_free(p);
     872           0 :                                 if (!conn->host_info) {
     873           0 :                                         SET_OOM_ERROR(*conn->error_info);
     874           0 :                                         goto err; /* OOM */
     875             :                                 }
     876             :                         } else {
     877           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
     878             :                         }
     879        1798 :                         if (!conn->unix_socket || !conn->host_info) {
     880           0 :                                 SET_OOM_ERROR(*conn->error_info);
     881           0 :                                 goto err; /* OOM */
     882             :                         }
     883        1798 :                         conn->unix_socket_len = strlen(conn->unix_socket);
     884             :                 }
     885        1800 :                 conn->client_flag            = mysql_flags;
     886        1800 :                 conn->max_packet_size        = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
     887             :                 /* todo: check if charset is available */
     888        1800 :                 conn->server_capabilities = greet_packet->server_capabilities;
     889        1800 :                 memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
     890        1800 :                 conn->upsert_status->warning_count = 0;
     891        1800 :                 conn->upsert_status->server_status = greet_packet->server_status;
     892        1800 :                 conn->upsert_status->affected_rows = 0;
     893             : 
     894        1800 :                 SET_EMPTY_ERROR(*conn->error_info);
     895             : 
     896        1800 :                 mysqlnd_local_infile_default(conn);
     897             : 
     898             : #if MYSQLND_UNICODE
     899             :                 {
     900             :                         unsigned int as_unicode = 1;
     901             :                         conn->m->set_client_option(conn, MYSQLND_OPT_NUMERIC_AND_DATETIME_AS_UNICODE, (char *)&as_unicode TSRMLS_CC);
     902             :                         DBG_INF("unicode set");
     903             :                 }
     904             : #endif
     905        1800 :                 if (conn->options->init_commands) {
     906           9 :                         unsigned int current_command = 0;
     907          15 :                         for (; current_command < conn->options->num_commands; ++current_command) {
     908           9 :                                 const char * const command = conn->options->init_commands[current_command];
     909           9 :                                 if (command) {
     910           9 :                                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
     911           9 :                                         if (PASS != conn->m->query(conn, command, strlen(command) TSRMLS_CC)) {
     912           3 :                                                 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
     913           3 :                                                 goto err;
     914             :                                         }
     915           6 :                                         if (conn->last_query_type == QUERY_SELECT) {
     916           1 :                                                 MYSQLND_RES * result = conn->m->use_result(conn TSRMLS_CC);
     917           1 :                                                 if (result) {
     918           1 :                                                         result->m.free_result(result, TRUE TSRMLS_CC);
     919             :                                                 }
     920             :                                         }
     921             :                                 }
     922             :                         }
     923             :                 }
     924             : 
     925             : 
     926        1797 :                 MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
     927        1797 :                 if (reconnect) {
     928           0 :                         MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
     929             :                 }
     930        1797 :                 if (conn->persistent) {
     931        1091 :                         MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
     932             :                 }
     933             : 
     934        1797 :                 DBG_INF_FMT("connection_id=%llu", conn->thread_id);
     935             : 
     936        1797 :                 PACKET_FREE(greet_packet);
     937             : 
     938        1797 :                 conn->m->local_tx_end(conn, this_func, PASS TSRMLS_CC);
     939        1797 :                 DBG_RETURN(PASS);
     940             :         }
     941             : err:
     942          54 :         PACKET_FREE(greet_packet);
     943             : 
     944          54 :         DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme);
     945          54 :         if (!conn->error_info->error_no) {
     946           3 :                 SET_CLIENT_ERROR(*conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, conn->error_info->error? conn->error_info->error:"Unknown error");
     947           9 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "[%u] %.128s (trying to connect via %s)",
     948           6 :                                                  conn->error_info->error_no, conn->error_info->error, conn->scheme);
     949             :         }
     950             : 
     951          54 :         conn->m->free_contents(conn TSRMLS_CC);
     952          54 :         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
     953          54 :         if (TRUE == local_tx_started) {
     954          54 :                 conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);
     955             :         }
     956             : 
     957          54 :         DBG_RETURN(FAIL);
     958             : }
     959             : /* }}} */
     960             : 
     961             : 
     962             : /* {{{ mysqlnd_conn::connect */
     963             : static enum_func_status
     964        1851 : MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
     965             :                                                  const char * host, const char * user,
     966             :                                                  const char * passwd, unsigned int passwd_len,
     967             :                                                  const char * db, unsigned int db_len,
     968             :                                                  unsigned int port,
     969             :                                                  const char * socket_or_pipe,
     970             :                                                  unsigned int mysql_flags
     971             :                                                  TSRMLS_DC)
     972             : {
     973        1851 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, connect);
     974        1851 :         enum_func_status ret = FAIL;
     975        1851 :         MYSQLND_CONN_DATA * conn = conn_handle->data;
     976             : 
     977        1851 :         DBG_ENTER("mysqlnd_conn::connect");
     978             : 
     979        1851 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
     980        1851 :                 ret = conn->m->connect(conn, host, user, passwd, passwd_len, db, db_len, port, socket_or_pipe, mysql_flags TSRMLS_CC);
     981             : 
     982        1851 :                 conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);
     983             :         }
     984        1851 :         DBG_RETURN(ret);
     985             : }
     986             : /* }}} */
     987             : 
     988             : /* {{{ mysqlnd_connect */
     989        1851 : PHPAPI MYSQLND * mysqlnd_connect(MYSQLND * conn_handle,
     990             :                                                  const char * host, const char * user,
     991             :                                                  const char * passwd, unsigned int passwd_len,
     992             :                                                  const char * db, unsigned int db_len,
     993             :                                                  unsigned int port,
     994             :                                                  const char * socket_or_pipe,
     995             :                                                  unsigned int mysql_flags
     996             :                                                  TSRMLS_DC)
     997             : {
     998        1851 :         enum_func_status ret = FAIL;
     999        1851 :         zend_bool self_alloced = FALSE;
    1000             : 
    1001        1851 :         DBG_ENTER("mysqlnd_connect");
    1002        1851 :         DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host?host:"", user?user:"", db?db:"", port, mysql_flags);
    1003             : 
    1004        1851 :         if (!conn_handle) {
    1005           0 :                 self_alloced = TRUE;
    1006           0 :                 if (!(conn_handle = mysqlnd_init(FALSE))) {
    1007             :                         /* OOM */
    1008           0 :                         DBG_RETURN(NULL);
    1009             :                 }
    1010             :         }
    1011             : 
    1012        1851 :         ret = conn_handle->m->connect(conn_handle, host, user, passwd, passwd_len, db, db_len, port, socket_or_pipe, mysql_flags TSRMLS_CC);
    1013             : 
    1014        1851 :         if (ret == FAIL) {
    1015          54 :                 if (self_alloced) {
    1016             :                         /*
    1017             :                           We have alloced, thus there are no references to this
    1018             :                           object - we are free to kill it!
    1019             :                         */
    1020           0 :                         conn_handle->m->dtor(conn_handle TSRMLS_CC);
    1021             :                 }
    1022          54 :                 DBG_RETURN(NULL);
    1023             :         }
    1024        1797 :         DBG_RETURN(conn_handle);
    1025             : }
    1026             : /* }}} */
    1027             : 
    1028             : 
    1029             : /* {{{ mysqlnd_conn_data::query */
    1030             : /*
    1031             :   If conn->error_info->error_no is not zero, then we had an error.
    1032             :   Still the result from the query is PASS
    1033             : */
    1034             : static enum_func_status
    1035       20937 : MYSQLND_METHOD(mysqlnd_conn_data, query)(MYSQLND_CONN_DATA * conn, const char * query, unsigned int query_len TSRMLS_DC)
    1036             : {
    1037       20937 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, query);
    1038       20937 :         enum_func_status ret = FAIL;
    1039       20937 :         DBG_ENTER("mysqlnd_conn_data::query");
    1040       20937 :         DBG_INF_FMT("conn=%llu query=%s", conn->thread_id, query);
    1041             : 
    1042       20937 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1043       41860 :                 if (PASS == conn->m->send_query(conn, query, query_len TSRMLS_CC) &&
    1044       20923 :                         PASS == conn->m->reap_query(conn TSRMLS_CC))
    1045             :                 {
    1046       20145 :                         ret = PASS;
    1047       20145 :                         if (conn->last_query_type == QUERY_UPSERT && conn->upsert_status->affected_rows) {
    1048       13358 :                                 MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, conn->upsert_status->affected_rows);
    1049             :                         }
    1050             :                 }
    1051       20937 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1052             :         }
    1053       20937 :         DBG_RETURN(ret);
    1054             : }
    1055             : /* }}} */
    1056             : 
    1057             : 
    1058             : /* {{{ mysqlnd_conn_data::send_query */
    1059             : static enum_func_status
    1060       20964 : MYSQLND_METHOD(mysqlnd_conn_data, send_query)(MYSQLND_CONN_DATA * conn, const char * query, unsigned int query_len TSRMLS_DC)
    1061             : {
    1062       20964 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, send_query);
    1063       20964 :         enum_func_status ret = FAIL;
    1064       20964 :         DBG_ENTER("mysqlnd_conn_data::send_query");
    1065       20964 :         DBG_INF_FMT("conn=%llu query=%s", conn->thread_id, query);
    1066       20964 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1067             : 
    1068       20964 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1069       20964 :                 ret = conn->m->simple_command(conn, COM_QUERY, (zend_uchar *) query, query_len,
    1070             :                                                                                          PROT_LAST /* we will handle the OK packet*/,
    1071             :                                                                                          FALSE, FALSE TSRMLS_CC);
    1072       20964 :                 if (PASS == ret) {
    1073       20948 :                         CONN_SET_STATE(conn, CONN_QUERY_SENT);
    1074             :                 }
    1075       20964 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1076             :         }
    1077       20964 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1078       20964 :         DBG_RETURN(ret);
    1079             : }
    1080             : /* }}} */
    1081             : 
    1082             : 
    1083             : /* {{{ mysqlnd_conn_data::reap_query */
    1084             : static enum_func_status
    1085       20947 : MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    1086             : {
    1087       20947 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, reap_query);
    1088       20947 :         enum_mysqlnd_connection_state state = CONN_GET_STATE(conn);
    1089       20947 :         enum_func_status ret = FAIL;
    1090       20947 :         DBG_ENTER("mysqlnd_conn_data::reap_query");
    1091       20947 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1092             : 
    1093       20947 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1094       20947 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1095       20947 :                 if (state <= CONN_READY || state == CONN_QUIT_SENT) {
    1096           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection not opened, clear or has been closed");
    1097           0 :                         DBG_ERR_FMT("Connection not opened, clear or has been closed. State=%u", state);
    1098           0 :                         DBG_RETURN(ret);
    1099             :                 }
    1100       20947 :                 ret = conn->m->query_read_result_set_header(conn, NULL TSRMLS_CC);
    1101             : 
    1102       20947 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1103             :         }
    1104       20947 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1105       20947 :         DBG_RETURN(ret);
    1106             : }
    1107             : /* }}} */
    1108             : 
    1109             : 
    1110             : #include "php_network.h"
    1111             : 
    1112          31 : MYSQLND ** mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array TSRMLS_DC)
    1113             : {
    1114          31 :         int cnt = 0;
    1115          31 :         MYSQLND **p = conn_array, **p_p;
    1116          31 :         MYSQLND **ret = NULL;
    1117             : 
    1118         121 :         while (*p) {
    1119          59 :                 if (CONN_GET_STATE((*p)->data) <= CONN_READY || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT) {
    1120          20 :                         cnt++;
    1121             :                 }
    1122          59 :                 p++;
    1123             :         }
    1124          31 :         if (cnt) {
    1125          18 :                 MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
    1126          18 :                 p_p = p = conn_array;
    1127          65 :                 while (*p) {
    1128          49 :                         if (CONN_GET_STATE((*p)->data) <= CONN_READY || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT) {
    1129          20 :                                 *ret_p = *p;
    1130          20 :                                 *p = NULL;
    1131          20 :                                 ret_p++;
    1132             :                         } else {
    1133           9 :                                 *p_p = *p;
    1134           9 :                                 p_p++;
    1135             :                         }
    1136          29 :                         p++;
    1137             :                 }
    1138          18 :                 *ret_p = NULL;
    1139             :         }
    1140          31 :         return ret;
    1141             : }
    1142             : 
    1143             : 
    1144             : /* {{{ stream_select mysqlnd_stream_array_to_fd_set functions */
    1145          61 : static int mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd TSRMLS_DC)
    1146             : {
    1147             :         php_socket_t this_fd;
    1148          61 :         php_stream *stream = NULL;
    1149          61 :         int cnt = 0;
    1150          61 :         MYSQLND **p = conn_array;
    1151             : 
    1152         226 :         while (*p) {
    1153             :                 /* get the fd.
    1154             :                  * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
    1155             :                  * when casting.  It is only used here so that the buffered data warning
    1156             :                  * is not displayed.
    1157             :                  * */
    1158         104 :                 stream = (*p)->data->net->stream;
    1159         312 :                 if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
    1160         208 :                                                                                 (void*)&this_fd, 1) && this_fd >= 0) {
    1161             : 
    1162         104 :                         PHP_SAFE_FD_SET(this_fd, fds);
    1163             : 
    1164         104 :                         if (this_fd > *max_fd) {
    1165          48 :                                 *max_fd = this_fd;
    1166             :                         }
    1167         104 :                         cnt++;
    1168             :                 }
    1169         104 :                 p++;
    1170             :         }
    1171          61 :         return cnt ? 1 : 0;
    1172             : }
    1173             : 
    1174          56 : static int mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds TSRMLS_DC)
    1175             : {
    1176             :         php_socket_t this_fd;
    1177          56 :         php_stream *stream = NULL;
    1178          56 :         int ret = 0;
    1179          56 :         zend_bool disproportion = FALSE;
    1180             : 
    1181             : 
    1182          56 :         MYSQLND **fwd = conn_array, **bckwd = conn_array;
    1183             : 
    1184         216 :         while (*fwd) {
    1185         104 :                 stream = (*fwd)->data->net->stream;
    1186         312 :                 if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
    1187         208 :                                                                                 (void*)&this_fd, 1) && this_fd >= 0) {
    1188         104 :                         if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
    1189          29 :                                 if (disproportion) {
    1190           0 :                                         *bckwd = *fwd;
    1191             :                                 }
    1192          29 :                                 bckwd++;
    1193          29 :                                 fwd++;
    1194          29 :                                 ret++;
    1195          29 :                                 continue;
    1196             :                         }
    1197             :                 }
    1198          75 :                 disproportion = TRUE;
    1199          75 :                 fwd++;
    1200             :         }
    1201          56 :         *bckwd = NULL;/* NULL-terminate the list */
    1202             : 
    1203          56 :         return ret;
    1204             : }
    1205             : /* }}} */
    1206             : 
    1207             : 
    1208             : #ifndef PHP_WIN32
    1209             : #define php_select(m, r, w, e, t)       select(m, r, w, e, t)
    1210             : #else
    1211             : #include "win32/select.h"
    1212             : #endif
    1213             : 
    1214             : 
    1215             : /* {{{ _mysqlnd_poll */
    1216             : PHPAPI enum_func_status
    1217          31 : _mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num TSRMLS_DC)
    1218             : {
    1219             :         struct timeval  tv;
    1220          31 :         struct timeval *tv_p = NULL;
    1221             :         fd_set                  rfds, wfds, efds;
    1222          31 :         php_socket_t    max_fd = 0;
    1223          31 :         int                             retval, sets = 0;
    1224          31 :         int                             set_count, max_set_count = 0;
    1225             : 
    1226          31 :         DBG_ENTER("_mysqlnd_poll");
    1227          31 :         if (sec < 0 || usec < 0) {
    1228           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative values passed for sec and/or usec");
    1229           0 :                 DBG_RETURN(FAIL);
    1230             :         }
    1231             : 
    1232          31 :         *dont_poll = mysqlnd_stream_array_check_for_readiness(r_array TSRMLS_CC);
    1233             : 
    1234          31 :         FD_ZERO(&rfds);
    1235          31 :         FD_ZERO(&wfds);
    1236          31 :         FD_ZERO(&efds);
    1237             : 
    1238          31 :         if (r_array != NULL) {
    1239          31 :                 set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd TSRMLS_CC);
    1240          31 :                 if (set_count > max_set_count) {
    1241          20 :                         max_set_count = set_count;
    1242             :                 }
    1243          31 :                 sets += set_count;
    1244             :         }
    1245             : 
    1246          31 :         if (e_array != NULL) {
    1247          30 :                 set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd TSRMLS_CC);
    1248          30 :                 if (set_count > max_set_count) {
    1249           8 :                         max_set_count = set_count;
    1250             :                 }
    1251          30 :                 sets += set_count;
    1252             :         }
    1253             : 
    1254          31 :         if (!sets) {
    1255           3 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
    1256           3 :                 DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
    1257           3 :                 DBG_RETURN(FAIL);
    1258             :         }
    1259             : 
    1260          28 :         PHP_SAFE_MAX_FD(max_fd, max_set_count);
    1261             : 
    1262             :         /* Solaris + BSD do not like microsecond values which are >= 1 sec */
    1263          28 :         if (usec > 999999) {
    1264           0 :                 tv.tv_sec = sec + (usec / 1000000);
    1265           0 :                 tv.tv_usec = usec % 1000000;
    1266             :         } else {
    1267          28 :                 tv.tv_sec = sec;
    1268          28 :                 tv.tv_usec = usec;
    1269             :         }
    1270             : 
    1271          28 :         tv_p = &tv;
    1272             : 
    1273          28 :         retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
    1274             : 
    1275          28 :         if (retval == -1) {
    1276           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
    1277             :                                                 errno, strerror(errno), max_fd);
    1278           0 :                 DBG_RETURN(FAIL);
    1279             :         }
    1280             : 
    1281          28 :         if (r_array != NULL) {
    1282          28 :                 mysqlnd_stream_array_from_fd_set(r_array, &rfds TSRMLS_CC);
    1283             :         }
    1284          28 :         if (e_array != NULL) {
    1285          28 :                 mysqlnd_stream_array_from_fd_set(e_array, &efds TSRMLS_CC);
    1286             :         }
    1287             : 
    1288          28 :         *desc_num = retval;
    1289          28 :         DBG_RETURN(PASS);
    1290             : }
    1291             : /* }}} */
    1292             : 
    1293             : 
    1294             : /*
    1295             :   COM_FIELD_LIST is special, different from a SHOW FIELDS FROM :
    1296             :   - There is no result set header - status from the command, which
    1297             :     impacts us to allocate big chunk of memory for reading the metadata.
    1298             :   - The EOF packet is consumed by the metadata packet reader.
    1299             : */
    1300             : 
    1301             : /* {{{ mysqlnd_conn_data::list_fields */
    1302             : MYSQLND_RES *
    1303           4 : MYSQLND_METHOD(mysqlnd_conn_data, list_fields)(MYSQLND_CONN_DATA * conn, const char *table, const char *achtung_wild TSRMLS_DC)
    1304             : {
    1305           4 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, list_fields);
    1306             :         /* db + \0 + wild + \0 (for wild) */
    1307             :         zend_uchar buff[MYSQLND_MAX_ALLOWED_DB_LEN * 2 + 1 + 1], *p;
    1308             :         size_t table_len, wild_len;
    1309           4 :         MYSQLND_RES * result = NULL;
    1310           4 :         DBG_ENTER("mysqlnd_conn_data::list_fields");
    1311           4 :         DBG_INF_FMT("conn=%llu table=%s wild=%s", conn->thread_id, table? table:"",achtung_wild? achtung_wild:"");
    1312             : 
    1313           4 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1314             :                 do {
    1315           4 :                         p = buff;
    1316           4 :                         if (table && (table_len = strlen(table))) {
    1317           4 :                                 size_t to_copy = MIN(table_len, MYSQLND_MAX_ALLOWED_DB_LEN);
    1318           4 :                                 memcpy(p, table, to_copy);
    1319           4 :                                 p += to_copy;
    1320           4 :                                 *p++ = '\0';
    1321             :                         }
    1322             : 
    1323           4 :                         if (achtung_wild && (wild_len = strlen(achtung_wild))) {
    1324           0 :                                 size_t to_copy = MIN(wild_len, MYSQLND_MAX_ALLOWED_DB_LEN);
    1325           0 :                                 memcpy(p, achtung_wild, to_copy);
    1326           0 :                                 p += to_copy;
    1327           0 :                                 *p++ = '\0';
    1328             :                         }
    1329             : 
    1330           4 :                         if (PASS != conn->m->simple_command(conn, COM_FIELD_LIST, buff, p - buff,
    1331             :                                                                                            PROT_LAST /* we will handle the OK packet*/,
    1332             :                                                                                            FALSE, TRUE TSRMLS_CC)) {
    1333           0 :                                 conn->m->local_tx_end(conn, 0, FAIL TSRMLS_CC);
    1334           0 :                                 break;
    1335             :                         }
    1336             : 
    1337             :                         /*
    1338             :                            Prepare for the worst case.
    1339             :                            MyISAM goes to 2500 BIT columns, double it for safety.
    1340             :                         */
    1341           4 :                         result = conn->m->result_init(5000, conn->persistent TSRMLS_CC);
    1342           4 :                         if (!result) {
    1343           0 :                                 break;
    1344             :                         }
    1345             : 
    1346           4 :                         if (FAIL == result->m.read_result_metadata(result, conn TSRMLS_CC)) {
    1347           1 :                                 DBG_ERR("Error occurred while reading metadata");
    1348           1 :                                 result->m.free_result(result, TRUE TSRMLS_CC);
    1349           1 :                                 result = NULL;
    1350           1 :                                 break;
    1351             :                         }
    1352             : 
    1353           3 :                         result->type = MYSQLND_RES_NORMAL;
    1354           3 :                         result->m.fetch_row = result->m.fetch_row_normal_unbuffered;
    1355           3 :                         result->unbuf = mnd_ecalloc(1, sizeof(MYSQLND_RES_UNBUFFERED));
    1356           3 :                         if (!result->unbuf) {
    1357             :                                 /* OOM */
    1358           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1359           0 :                                 result->m.free_result(result, TRUE TSRMLS_CC);
    1360           0 :                                 result = NULL;
    1361           0 :                                 break;
    1362             :                         }
    1363           3 :                         result->unbuf->eof_reached = TRUE;
    1364             :                 } while (0);
    1365           4 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);
    1366             :         }
    1367             : 
    1368           4 :         DBG_RETURN(result);
    1369             : }
    1370             : /* }}} */
    1371             : 
    1372             : 
    1373             : /* {{{ mysqlnd_conn_data::list_method */
    1374             : MYSQLND_RES *
    1375           8 : MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * query, const char *achtung_wild, char *par1 TSRMLS_DC)
    1376             : {
    1377           8 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, list_method);
    1378           8 :         char * show_query = NULL;
    1379             :         size_t show_query_len;
    1380           8 :         MYSQLND_RES * result = NULL;
    1381             : 
    1382           8 :         DBG_ENTER("mysqlnd_conn_data::list_method");
    1383           8 :         DBG_INF_FMT("conn=%llu query=%s wild=%u", conn->thread_id, query, achtung_wild);
    1384             : 
    1385           8 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1386           8 :                 if (par1) {
    1387           0 :                         if (achtung_wild) {
    1388           0 :                                 show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
    1389             :                         } else {
    1390           0 :                                 show_query_len = mnd_sprintf(&show_query, 0, query, par1);
    1391             :                         }
    1392             :                 } else {
    1393           8 :                         if (achtung_wild) {
    1394           0 :                                 show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
    1395             :                         } else {
    1396           8 :                                 show_query_len = strlen(show_query = (char *)query);
    1397             :                         }
    1398             :                 }
    1399             : 
    1400           8 :                 if (PASS == conn->m->query(conn, show_query, show_query_len TSRMLS_CC)) {
    1401           8 :                         result = conn->m->store_result(conn TSRMLS_CC);
    1402             :                 }
    1403           8 :                 if (show_query != query) {
    1404           0 :                         mnd_sprintf_free(show_query);
    1405             :                 }
    1406           8 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);
    1407             :         }
    1408           8 :         DBG_RETURN(result);
    1409             : }
    1410             : /* }}} */
    1411             : 
    1412             : 
    1413             : /* {{{ mysqlnd_conn_data::errno */
    1414             : static unsigned int
    1415        2056 : MYSQLND_METHOD(mysqlnd_conn_data, errno)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1416             : {
    1417        2056 :         return conn->error_info->error_no;
    1418             : }
    1419             : /* }}} */
    1420             : 
    1421             : 
    1422             : /* {{{ mysqlnd_conn_data::error */
    1423             : static const char *
    1424        1995 : MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1425             : {
    1426        1995 :         return conn->error_info->error;
    1427             : }
    1428             : /* }}} */
    1429             : 
    1430             : 
    1431             : /* {{{ mysqlnd_conn_data::sqlstate */
    1432             : static const char *
    1433         769 : MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1434             : {
    1435         769 :         return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
    1436             : }
    1437             : /* }}} */
    1438             : 
    1439             : 
    1440             : /* {{{ mysqlnd_old_escape_string */
    1441             : PHPAPI ulong 
    1442           9 : mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len TSRMLS_DC)
    1443             : {
    1444           9 :         DBG_ENTER("mysqlnd_old_escape_string");
    1445           9 :         DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len TSRMLS_CC));
    1446             : }
    1447             : /* }}} */
    1448             : 
    1449             : 
    1450             : /* {{{ mysqlnd_conn_data::ssl_set */
    1451             : static enum_func_status
    1452           3 : MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
    1453             :                                                                           const char * const ca, const char * const capath, const char * const cipher TSRMLS_DC)
    1454             : {
    1455           3 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, ssl_set);
    1456           3 :         enum_func_status ret = FAIL;
    1457           3 :         DBG_ENTER("mysqlnd_conn_data::ssl_set");
    1458             : 
    1459           3 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1460          15 :                 ret = (PASS == conn->net->m.set_client_option(conn->net, MYSQLND_OPT_SSL_KEY, key TSRMLS_CC) &&
    1461           3 :                         PASS == conn->net->m.set_client_option(conn->net, MYSQLND_OPT_SSL_CERT, cert TSRMLS_CC) &&
    1462           3 :                         PASS == conn->net->m.set_client_option(conn->net, MYSQLND_OPT_SSL_CA, ca TSRMLS_CC) &&
    1463           3 :                         PASS == conn->net->m.set_client_option(conn->net, MYSQLND_OPT_SSL_CAPATH, capath TSRMLS_CC) &&
    1464           3 :                         PASS == conn->net->m.set_client_option(conn->net, MYSQLND_OPT_SSL_CIPHER, cipher TSRMLS_CC)) ? PASS : FAIL;
    1465             : 
    1466           3 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1467             :         }
    1468           3 :         DBG_RETURN(ret);
    1469             : }
    1470             : /* }}} */
    1471             : 
    1472             : 
    1473             : /* {{{ mysqlnd_conn_data::escape_string */
    1474             : static ulong
    1475       10553 : MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len TSRMLS_DC)
    1476             : {
    1477       10553 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, escape_string);
    1478       10553 :         ulong ret = FAIL;
    1479       10553 :         DBG_ENTER("mysqlnd_conn_data::escape_string");
    1480       10553 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1481             : 
    1482       10553 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1483       10553 :                 DBG_INF_FMT("server_status=%u", conn->upsert_status->server_status);
    1484       10553 :                 if (conn->upsert_status->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
    1485           9 :                         ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len TSRMLS_CC);
    1486             :                 } else {
    1487       10544 :                         ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len TSRMLS_CC);
    1488             :                 }
    1489       10553 :                 conn->m->local_tx_end(conn, this_func, PASS TSRMLS_CC);
    1490             :         }
    1491       10553 :         DBG_RETURN(ret);
    1492             : }
    1493             : /* }}} */
    1494             : 
    1495             : 
    1496             : /* {{{ mysqlnd_conn_data::dump_debug_info */
    1497             : static enum_func_status
    1498           2 : MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1499             : {
    1500           2 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, server_dump_debug_information);
    1501           2 :         enum_func_status ret = FAIL;
    1502           2 :         DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
    1503           2 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1504           2 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1505           2 :                 ret = conn->m->simple_command(conn, COM_DEBUG, NULL, 0, PROT_EOF_PACKET, FALSE, TRUE TSRMLS_CC);
    1506             : 
    1507           2 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1508             :         }
    1509             : 
    1510           2 :         DBG_RETURN(ret);
    1511             : }
    1512             : /* }}} */
    1513             : 
    1514             : 
    1515             : /* {{{ mysqlnd_conn_data::select_db */
    1516             : static enum_func_status
    1517         239 : MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, unsigned int db_len TSRMLS_DC)
    1518             : {
    1519         239 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, select_db);
    1520         239 :         enum_func_status ret = FAIL;
    1521             : 
    1522         239 :         DBG_ENTER("mysqlnd_conn_data::select_db");
    1523         239 :         DBG_INF_FMT("conn=%llu db=%s", conn->thread_id, db);
    1524             : 
    1525         239 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1526         239 :                 ret = conn->m->simple_command(conn, COM_INIT_DB, (zend_uchar*) db, db_len, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1527             :                 /*
    1528             :                   The server sends 0 but libmysql doesn't read it and has established
    1529             :                   a protocol of giving back -1. Thus we have to follow it :(
    1530             :                 */
    1531         239 :                 SET_ERROR_AFF_ROWS(conn);
    1532         239 :                 if (ret == PASS) {
    1533         229 :                         if (conn->connect_or_select_db) {
    1534         229 :                                 mnd_pefree(conn->connect_or_select_db, conn->persistent);
    1535             :                         }
    1536         229 :                         conn->connect_or_select_db = mnd_pestrndup(db, db_len, conn->persistent);
    1537         229 :                         conn->connect_or_select_db_len = db_len;
    1538         229 :                         if (!conn->connect_or_select_db) {
    1539             :                                 /* OOM */
    1540           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1541           0 :                                 ret = FAIL;
    1542             :                         }
    1543             :                 }
    1544         239 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1545             :         }
    1546         239 :         DBG_RETURN(ret);
    1547             : }
    1548             : /* }}} */
    1549             : 
    1550             : 
    1551             : /* {{{ mysqlnd_conn_data::ping */
    1552             : static enum_func_status
    1553          47 : MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1554             : {
    1555          47 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, ping);
    1556          47 :         enum_func_status ret = FAIL;
    1557             : 
    1558          47 :         DBG_ENTER("mysqlnd_conn_data::ping");
    1559          47 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1560             : 
    1561          47 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1562          47 :                 ret = conn->m->simple_command(conn, COM_PING, NULL, 0, PROT_OK_PACKET, TRUE, TRUE TSRMLS_CC);
    1563             :                 /*
    1564             :                   The server sends 0 but libmysql doesn't read it and has established
    1565             :                   a protocol of giving back -1. Thus we have to follow it :(
    1566             :                 */
    1567          47 :                 SET_ERROR_AFF_ROWS(conn);
    1568             : 
    1569          47 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1570             :         }
    1571          47 :         DBG_INF_FMT("ret=%u", ret);
    1572          47 :         DBG_RETURN(ret);
    1573             : }
    1574             : /* }}} */
    1575             : 
    1576             : 
    1577             : /* {{{ mysqlnd_conn_data::statistic */
    1578             : static enum_func_status
    1579          19 : MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, char **message, unsigned int * message_len TSRMLS_DC)
    1580             : {
    1581          19 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, get_server_statistics);
    1582          19 :         enum_func_status ret = FAIL;
    1583             :         MYSQLND_PACKET_STATS * stats_header;
    1584             : 
    1585          19 :         DBG_ENTER("mysqlnd_conn_data::statistic");
    1586          19 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1587             : 
    1588          19 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1589             :                 do {
    1590          19 :                         ret = conn->m->simple_command(conn, COM_STATISTICS, NULL, 0, PROT_LAST, FALSE, TRUE TSRMLS_CC);
    1591          19 :                         if (FAIL == ret) {
    1592           3 :                                 break;
    1593             :                         }
    1594          16 :                         stats_header = conn->protocol->m.get_stats_packet(conn->protocol, FALSE TSRMLS_CC);
    1595          16 :                         if (!stats_header) {
    1596           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1597           0 :                                 break;
    1598             :                         }
    1599             : 
    1600          16 :                         if (PASS == (ret = PACKET_READ(stats_header, conn))) {
    1601             :                                 /* will be freed by Zend, thus don't use the mnd_ allocator */
    1602          16 :                                 *message = estrndup(stats_header->message, stats_header->message_len); 
    1603          16 :                                 *message_len = stats_header->message_len;
    1604          16 :                                 DBG_INF(*message);
    1605             :                         }
    1606          16 :                         PACKET_FREE(stats_header);
    1607             :                 } while (0);
    1608             : 
    1609          19 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1610             :         }
    1611          19 :         DBG_RETURN(ret);
    1612             : }
    1613             : /* }}} */
    1614             : 
    1615             : 
    1616             : /* {{{ mysqlnd_conn_data::kill */
    1617             : static enum_func_status
    1618          17 : MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid TSRMLS_DC)
    1619             : {
    1620          17 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, kill_connection);
    1621          17 :         enum_func_status ret = FAIL;
    1622             :         zend_uchar buff[4];
    1623             : 
    1624          17 :         DBG_ENTER("mysqlnd_conn_data::kill");
    1625          17 :         DBG_INF_FMT("conn=%llu pid=%u", conn->thread_id, pid);
    1626             : 
    1627          17 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1628          17 :                 int4store(buff, pid);
    1629             : 
    1630             :                 /* If we kill ourselves don't expect OK packet, PROT_LAST will skip it */
    1631          17 :                 if (pid != conn->thread_id) {
    1632           3 :                         ret = conn->m->simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1633             :                         /*
    1634             :                           The server sends 0 but libmysql doesn't read it and has established
    1635             :                           a protocol of giving back -1. Thus we have to follow it :(
    1636             :                         */
    1637           3 :                         SET_ERROR_AFF_ROWS(conn);
    1638          14 :                 } else if (PASS == (ret = conn->m->simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_LAST, FALSE, TRUE TSRMLS_CC))) {
    1639          11 :                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    1640             :                 }
    1641             : 
    1642          17 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1643             :         }
    1644          17 :         DBG_RETURN(ret);
    1645             : }
    1646             : /* }}} */
    1647             : 
    1648             : 
    1649             : /* {{{ mysqlnd_conn_data::set_charset */
    1650             : static enum_func_status
    1651          95 : MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname TSRMLS_DC)
    1652             : {
    1653          95 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_charset);
    1654          95 :         enum_func_status ret = FAIL;
    1655          95 :         const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
    1656             : 
    1657          95 :         DBG_ENTER("mysqlnd_conn_data::set_charset");
    1658          95 :         DBG_INF_FMT("conn=%llu cs=%s", conn->thread_id, csname);
    1659             : 
    1660          95 :         if (!charset) {
    1661           3 :                 SET_CLIENT_ERROR(*conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE,
    1662             :                                                  "Invalid characterset or character set not supported");
    1663           3 :                 DBG_RETURN(ret);
    1664             :         }
    1665             : 
    1666          92 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1667             :                 char * query;
    1668          92 :                 size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
    1669             : 
    1670          92 :                 if (FAIL == (ret = conn->m->query(conn, query, query_len TSRMLS_CC))) {
    1671           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error executing query");
    1672          92 :                 } else if (conn->error_info->error_no) {
    1673           0 :                         ret = FAIL;
    1674             :                 } else {
    1675          92 :                         conn->charset = charset;
    1676             :                 }
    1677          92 :                 mnd_sprintf_free(query);
    1678             : 
    1679          92 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1680             :         }
    1681             : 
    1682          92 :         DBG_INF(ret == PASS? "PASS":"FAIL");
    1683          92 :         DBG_RETURN(ret);
    1684             : }
    1685             : /* }}} */
    1686             : 
    1687             : 
    1688             : /* {{{ mysqlnd_conn_data::refresh */
    1689             : static enum_func_status
    1690           0 : MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options TSRMLS_DC)
    1691             : {
    1692           0 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, refresh_server);
    1693           0 :         enum_func_status ret = FAIL;
    1694             :         zend_uchar bits[1];
    1695           0 :         DBG_ENTER("mysqlnd_conn_data::refresh");
    1696           0 :         DBG_INF_FMT("conn=%llu options=%lu", conn->thread_id, options);
    1697             : 
    1698           0 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1699           0 :                 int1store(bits, options);
    1700             : 
    1701           0 :                 ret = conn->m->simple_command(conn, COM_REFRESH, bits, 1, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1702             : 
    1703           0 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1704             :         }
    1705           0 :         DBG_RETURN(ret);
    1706             : }
    1707             : /* }}} */
    1708             : 
    1709             : 
    1710             : /* {{{ mysqlnd_conn_data::shutdown */
    1711             : static enum_func_status
    1712           0 : MYSQLND_METHOD(mysqlnd_conn_data, shutdown)(MYSQLND_CONN_DATA * const conn, uint8_t level TSRMLS_DC)
    1713             : {
    1714           0 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, shutdown_server);
    1715           0 :         enum_func_status ret = FAIL;
    1716             :         zend_uchar bits[1];
    1717           0 :         DBG_ENTER("mysqlnd_conn_data::shutdown");
    1718           0 :         DBG_INF_FMT("conn=%llu level=%lu", conn->thread_id, level);
    1719             : 
    1720           0 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1721           0 :                 int1store(bits, level);
    1722             : 
    1723           0 :                 ret = conn->m->simple_command(conn, COM_SHUTDOWN, bits, 1, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1724             : 
    1725           0 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1726             :         }
    1727           0 :         DBG_RETURN(ret);        
    1728             : }
    1729             : /* }}} */
    1730             : 
    1731             : 
    1732             : /* {{{ mysqlnd_send_close */
    1733             : static enum_func_status
    1734        3750 : MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1735             : {
    1736        3750 :         enum_func_status ret = PASS;
    1737             : 
    1738        3750 :         DBG_ENTER("mysqlnd_send_close");
    1739        7418 :         DBG_INF_FMT("conn=%llu conn->net->stream->abstract=%p",
    1740        7418 :                                 conn->thread_id, conn->net->stream? conn->net->stream->abstract:NULL);
    1741             : 
    1742        3750 :         if (CONN_GET_STATE(conn) >= CONN_READY) {
    1743        3675 :                 MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_CONNECTIONS);
    1744        3675 :                 if (conn->persistent) {
    1745        2225 :                         MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_PERSISTENT_CONNECTIONS);
    1746             :                 }
    1747             :         }
    1748        3750 :         switch (CONN_GET_STATE(conn)) {
    1749             :                 case CONN_READY:
    1750        1766 :                         DBG_INF("Connection clean, sending COM_QUIT");
    1751        1766 :                         if (conn->net->stream) {
    1752        1766 :                                 ret = conn->m->simple_command(conn, COM_QUIT, NULL, 0, PROT_LAST, TRUE, TRUE TSRMLS_CC);
    1753             :                         }
    1754             :                         /* Do nothing */
    1755        1766 :                         break;
    1756             :                 case CONN_SENDING_LOAD_DATA:
    1757             :                         /*
    1758             :                           Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
    1759             :                           will crash (assert) a debug server.
    1760             :                         */
    1761             :                 case CONN_NEXT_RESULT_PENDING:
    1762             :                 case CONN_QUERY_SENT:
    1763             :                 case CONN_FETCHING_DATA:
    1764          15 :                         MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
    1765          15 :                         DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme);
    1766             :                         /*
    1767             :                           Do nothing, the connection will be brutally closed
    1768             :                           and the server will catch it and free close from its side.
    1769             :                         */
    1770             :                 case CONN_ALLOCED:
    1771             :                         /*
    1772             :                           Allocated but not connected or there was failure when trying
    1773             :                           to connect with pre-allocated connect.
    1774             : 
    1775             :                           Fall-through
    1776             :                         */
    1777             :                 case CONN_QUIT_SENT:
    1778             :                         /* The user has killed its own connection */
    1779             :                         break;
    1780             :         }
    1781             :         /*
    1782             :           We hold one reference, and every other object which needs the
    1783             :           connection does increase it by 1.
    1784             :         */
    1785        3750 :         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    1786             : 
    1787        3750 :         DBG_RETURN(ret);
    1788             : }
    1789             : /* }}} */
    1790             : 
    1791             : 
    1792             : /* {{{ mysqlnd_conn_data::get_reference */
    1793             : static MYSQLND_CONN_DATA *
    1794       11498 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1795             : {
    1796       11498 :         DBG_ENTER("mysqlnd_conn_data::get_reference");
    1797       11498 :         ++conn->refcount;
    1798       11498 :         DBG_INF_FMT("conn=%llu new_refcount=%u", conn->thread_id, conn->refcount);
    1799       11498 :         DBG_RETURN(conn);
    1800             : }
    1801             : /* }}} */
    1802             : 
    1803             : 
    1804             : /* {{{ mysqlnd_conn_data::free_reference */
    1805             : static enum_func_status
    1806       11459 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1807             : {
    1808       11459 :         enum_func_status ret = PASS;
    1809       11459 :         DBG_ENTER("mysqlnd_conn_data::free_reference");
    1810       11459 :         DBG_INF_FMT("conn=%llu old_refcount=%u", conn->thread_id, conn->refcount);
    1811       11459 :         if (!(--conn->refcount)) {
    1812             :                 /*
    1813             :                   No multithreading issues as we don't share the connection :)
    1814             :                   This will free the object too, of course because references has
    1815             :                   reached zero.
    1816             :                 */
    1817        1875 :                 ret = conn->m->send_close(conn TSRMLS_CC);
    1818        1875 :                 conn->m->dtor(conn TSRMLS_CC);
    1819             :         }
    1820       11459 :         DBG_RETURN(ret);
    1821             : }
    1822             : /* }}} */
    1823             : 
    1824             : 
    1825             : /* {{{ mysqlnd_conn_data::get_state */
    1826             : static enum mysqlnd_connection_state
    1827       99025 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_state)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1828             : {
    1829       99025 :         DBG_ENTER("mysqlnd_conn_data::get_state");
    1830       99025 :         DBG_RETURN(conn->state);
    1831             : }
    1832             : /* }}} */
    1833             : 
    1834             : 
    1835             : /* {{{ mysqlnd_conn_data::set_state */
    1836             : static void
    1837       66967 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, set_state)(MYSQLND_CONN_DATA * const conn, enum mysqlnd_connection_state new_state TSRMLS_DC)
    1838             : {
    1839       66967 :         DBG_ENTER("mysqlnd_conn_data::set_state");
    1840       66967 :         DBG_INF_FMT("New state=%u", new_state);
    1841       66967 :         conn->state = new_state;
    1842       66967 :         DBG_VOID_RETURN;
    1843             : }
    1844             : /* }}} */
    1845             : 
    1846             : 
    1847             : /* {{{ mysqlnd_conn_data::field_count */
    1848             : static unsigned int
    1849       17880 : MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1850             : {
    1851       17880 :         return conn->field_count;
    1852             : }
    1853             : /* }}} */
    1854             : 
    1855             : 
    1856             : /* {{{ mysqlnd_conn_data::server_status */
    1857             : static unsigned int
    1858        6591 : MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1859             : {
    1860        6591 :         return conn->upsert_status->server_status;
    1861             : }
    1862             : /* }}} */
    1863             : 
    1864             : 
    1865             : /* {{{ mysqlnd_conn_data::insert_id */
    1866             : static uint64_t
    1867          58 : MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1868             : {
    1869          58 :         return conn->upsert_status->last_insert_id;
    1870             : }
    1871             : /* }}} */
    1872             : 
    1873             : 
    1874             : /* {{{ mysqlnd_conn_data::affected_rows */
    1875             : static uint64_t
    1876        1989 : MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1877             : {
    1878        1989 :         return conn->upsert_status->affected_rows;
    1879             : }
    1880             : /* }}} */
    1881             : 
    1882             : 
    1883             : /* {{{ mysqlnd_conn_data::warning_count */
    1884             : static unsigned int
    1885          20 : MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1886             : {
    1887          20 :         return conn->upsert_status->warning_count;
    1888             : }
    1889             : /* }}} */
    1890             : 
    1891             : 
    1892             : /* {{{ mysqlnd_conn_data::info */
    1893             : static const char *
    1894          25 : MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1895             : {
    1896          25 :         return conn->last_message;
    1897             : }
    1898             : /* }}} */
    1899             : 
    1900             : /* {{{ mysqlnd_get_client_info */
    1901        3494 : PHPAPI const char * mysqlnd_get_client_info()
    1902             : {
    1903        3494 :         return MYSQLND_VERSION;
    1904             : }
    1905             : /* }}} */
    1906             : 
    1907             : 
    1908             : /* {{{ mysqlnd_get_client_version */
    1909          18 : PHPAPI unsigned int mysqlnd_get_client_version()
    1910             : {
    1911          18 :         return MYSQLND_VERSION_ID;
    1912             : }
    1913             : /* }}} */
    1914             : 
    1915             : 
    1916             : /* {{{ mysqlnd_conn_data::get_server_info */
    1917             : static const char *
    1918          20 : MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1919             : {
    1920          20 :         return conn->server_version;
    1921             : }
    1922             : /* }}} */
    1923             : 
    1924             : 
    1925             : /* {{{ mysqlnd_conn_data::get_host_info */
    1926             : static const char *
    1927           9 : MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1928             : {
    1929           9 :         return conn->host_info;
    1930             : }
    1931             : /* }}} */
    1932             : 
    1933             : 
    1934             : /* {{{ mysqlnd_conn_data::get_proto_info */
    1935             : static unsigned int
    1936          12 : MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1937             : {
    1938          12 :         return conn->protocol_version;
    1939             : }
    1940             : /* }}} */
    1941             : 
    1942             : 
    1943             : /* {{{ mysqlnd_conn_data::charset_name */
    1944             : static const char *
    1945          12 : MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1946             : {
    1947          12 :         return conn->charset->name;
    1948             : }
    1949             : /* }}} */
    1950             : 
    1951             : 
    1952             : /* {{{ mysqlnd_conn_data::thread_id */
    1953             : static uint64_t
    1954         100 : MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1955             : {
    1956         100 :         return conn->thread_id;
    1957             : }
    1958             : /* }}} */
    1959             : 
    1960             : 
    1961             : /* {{{ mysqlnd_conn_data::get_server_version */
    1962             : static unsigned long
    1963        1064 : MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1964             : {
    1965             :         long major, minor, patch;
    1966             :         char *p;
    1967             : 
    1968        1064 :         if (!(p = conn->server_version)) {
    1969           0 :                 return 0;
    1970             :         }
    1971             : 
    1972        1064 :         major = strtol(p, &p, 10);
    1973        1064 :         p += 1; /* consume the dot */
    1974        1064 :         minor = strtol(p, &p, 10);
    1975        1064 :         p += 1; /* consume the dot */
    1976        1064 :         patch = strtol(p, &p, 10);
    1977             : 
    1978        1064 :         return (unsigned long)(major * 10000L + (unsigned long)(minor * 100L + patch));
    1979             : }
    1980             : /* }}} */
    1981             : 
    1982             : 
    1983             : /* {{{ mysqlnd_conn_data::more_results */
    1984             : static zend_bool
    1985        3090 : MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1986             : {
    1987        3090 :         DBG_ENTER("mysqlnd_conn_data::more_results");
    1988             :         /* (conn->state == CONN_NEXT_RESULT_PENDING) too */
    1989        3090 :         DBG_RETURN(conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
    1990             : }
    1991             : /* }}} */
    1992             : 
    1993             : 
    1994             : /* {{{ mysqlnd_conn_data::next_result */
    1995             : static enum_func_status
    1996          70 : MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1997             : {
    1998          70 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, next_result);
    1999          70 :         enum_func_status ret = FAIL;
    2000             : 
    2001          70 :         DBG_ENTER("mysqlnd_conn_data::next_result");
    2002          70 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    2003             : 
    2004          70 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2005             :                 do {
    2006          70 :                         if (CONN_GET_STATE(conn) != CONN_NEXT_RESULT_PENDING) {
    2007          14 :                                 break;
    2008             :                         }
    2009             : 
    2010          56 :                         SET_EMPTY_ERROR(*conn->error_info);
    2011          56 :                         SET_ERROR_AFF_ROWS(conn);
    2012             :                         /*
    2013             :                           We are sure that there is a result set, since conn->state is set accordingly
    2014             :                           in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
    2015             :                         */
    2016          56 :                         if (FAIL == (ret = conn->m->query_read_result_set_header(conn, NULL TSRMLS_CC))) {
    2017             :                                 /*
    2018             :                                   There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
    2019             :                                   So there are no more results and we should just return FALSE, error_no has been set
    2020             :                                 */
    2021           0 :                                 if (!conn->error_info->error_no) {
    2022           0 :                                         DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
    2023           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Serious error. PID=%d", getpid());
    2024           0 :                                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    2025             :                                 } else {
    2026           0 :                                         DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
    2027             :                                 }
    2028           0 :                                 break;
    2029             :                         }
    2030          56 :                         if (conn->last_query_type == QUERY_UPSERT && conn->upsert_status->affected_rows) {
    2031           8 :                                 MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, conn->upsert_status->affected_rows);
    2032             :                         }
    2033             :                 } while (0);
    2034          70 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    2035             :         }
    2036             : 
    2037          70 :         DBG_RETURN(ret);
    2038             : }
    2039             : /* }}} */
    2040             : 
    2041             : 
    2042             : /* {{{ mysqlnd_field_type_name */
    2043           0 : PHPAPI const char *mysqlnd_field_type_name(enum mysqlnd_field_types field_type)
    2044             : {
    2045           0 :         switch(field_type) {
    2046             :                 case FIELD_TYPE_STRING:
    2047             :                 case FIELD_TYPE_VAR_STRING:
    2048           0 :                         return "string";
    2049             :                 case FIELD_TYPE_TINY:
    2050             :                 case FIELD_TYPE_SHORT:
    2051             :                 case FIELD_TYPE_LONG:
    2052             :                 case FIELD_TYPE_LONGLONG:
    2053             :                 case FIELD_TYPE_INT24:
    2054           0 :                         return "int";
    2055             :                 case FIELD_TYPE_FLOAT:
    2056             :                 case FIELD_TYPE_DOUBLE:
    2057             :                 case FIELD_TYPE_DECIMAL:
    2058             :                 case FIELD_TYPE_NEWDECIMAL:
    2059           0 :                         return "real";
    2060             :                 case FIELD_TYPE_TIMESTAMP:
    2061           0 :                         return "timestamp";
    2062             :                 case FIELD_TYPE_YEAR:
    2063           0 :                         return "year";
    2064             :                 case FIELD_TYPE_DATE:
    2065             :                 case FIELD_TYPE_NEWDATE:
    2066           0 :                         return "date";
    2067             :                 case FIELD_TYPE_TIME:
    2068           0 :                         return "time";
    2069             :                 case FIELD_TYPE_SET:
    2070           0 :                         return "set";
    2071             :                 case FIELD_TYPE_ENUM:
    2072           0 :                         return "enum";
    2073             :                 case FIELD_TYPE_GEOMETRY:
    2074           0 :                         return "geometry";
    2075             :                 case FIELD_TYPE_DATETIME:
    2076           0 :                         return "datetime";
    2077             :                 case FIELD_TYPE_TINY_BLOB:
    2078             :                 case FIELD_TYPE_MEDIUM_BLOB:
    2079             :                 case FIELD_TYPE_LONG_BLOB:
    2080             :                 case FIELD_TYPE_BLOB:
    2081           0 :                         return "blob";
    2082             :                 case FIELD_TYPE_NULL:
    2083           0 :                         return "null";
    2084             :                 case FIELD_TYPE_BIT:
    2085           0 :                         return "bit";
    2086             :                 default:
    2087           0 :                         return "unknown";
    2088             :         }
    2089             : }
    2090             : /* }}} */
    2091             : 
    2092             : 
    2093             : /* {{{ mysqlnd_conn_data::change_user */
    2094             : static enum_func_status
    2095          68 : MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
    2096             :                                                                                   const char * user,
    2097             :                                                                                   const char * passwd,
    2098             :                                                                                   const char * db,
    2099             :                                                                                   zend_bool silent,
    2100             :                                                                                   size_t passwd_len
    2101             :                                                                                   TSRMLS_DC)
    2102             : {
    2103          68 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, change_user);
    2104             :         /*
    2105             :           User could be max 16 * 3 (utf8), pass is 20 usually, db is up to 64*3
    2106             :           Stack space is not that expensive, so use a bit more to be protected against
    2107             :           buffer overflows.
    2108             :         */
    2109          68 :         enum_func_status ret = FAIL;
    2110          68 :         zend_bool local_tx_started = FALSE;
    2111             : 
    2112          68 :         DBG_ENTER("mysqlnd_conn_data::change_user");
    2113          68 :         DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u",
    2114             :                                 conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", (silent == TRUE)?1:0 );
    2115             : 
    2116          68 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2117           0 :                 goto end;
    2118             :         }
    2119          68 :         local_tx_started = TRUE;
    2120             : 
    2121          68 :         SET_EMPTY_ERROR(*conn->error_info);
    2122          68 :         SET_ERROR_AFF_ROWS(conn);
    2123             : 
    2124          68 :         if (!user) {
    2125           0 :                 user = "";
    2126             :         }
    2127          68 :         if (!passwd) {
    2128           0 :                 passwd = "";
    2129             :         }
    2130          68 :         if (!db) {
    2131           3 :                 db = "";
    2132             :         }
    2133             : 
    2134             :         {
    2135          68 :                 zend_bool first_call = TRUE;
    2136          68 :                 char * switch_to_auth_protocol = NULL;
    2137          68 :                 size_t switch_to_auth_protocol_len = 0;
    2138          68 :                 char * requested_protocol = NULL;
    2139             :                 zend_uchar * plugin_data;
    2140             :                 size_t plugin_data_len;
    2141             : 
    2142          68 :                 plugin_data_len = conn->auth_plugin_data_len;
    2143          68 :                 plugin_data = mnd_emalloc(plugin_data_len);
    2144          68 :                 if (!plugin_data) {
    2145           0 :                         ret = FAIL;
    2146           0 :                         goto end;
    2147             :                 }
    2148          68 :                 memcpy(plugin_data, conn->auth_plugin_data, plugin_data_len);
    2149             : 
    2150          68 :                 requested_protocol = mnd_pestrdup(conn->options->auth_protocol? conn->options->auth_protocol:"mysql_native_password", FALSE);
    2151          68 :                 if (!requested_protocol) {
    2152           0 :                         ret = FAIL;
    2153           0 :                         goto end;
    2154             :                 }
    2155             : 
    2156             :                 do {
    2157             :                         struct st_mysqlnd_authentication_plugin * auth_plugin;
    2158             :                         {
    2159          68 :                                 char * plugin_name = NULL;
    2160             : 
    2161          68 :                                 mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
    2162             : 
    2163          68 :                                 DBG_INF_FMT("looking for %s auth plugin", plugin_name);
    2164          68 :                                 auth_plugin = mysqlnd_plugin_find(plugin_name);
    2165          68 :                                 mnd_sprintf_free(plugin_name);
    2166             : 
    2167          68 :                                 if (!auth_plugin) {
    2168           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
    2169           0 :                                         SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "The server requested authentication method unknown to the client");
    2170           0 :                                         break;
    2171             :                                 }
    2172             :                         }
    2173          68 :                         DBG_INF("plugin found");
    2174             : 
    2175             :                         {
    2176          68 :                                 zend_uchar * switch_to_auth_protocol_data = NULL;
    2177          68 :                                 size_t switch_to_auth_protocol_data_len = 0;
    2178          68 :                                 zend_uchar * scrambled_data = NULL;
    2179          68 :                                 size_t scrambled_data_len = 0;
    2180             : 
    2181          68 :                                 switch_to_auth_protocol = NULL;
    2182          68 :                                 switch_to_auth_protocol_len = 0;
    2183             : 
    2184          68 :                                 if (conn->auth_plugin_data) {
    2185          68 :                                         mnd_pefree(conn->auth_plugin_data, conn->persistent);
    2186          68 :                                         conn->auth_plugin_data = NULL;
    2187             :                                 }
    2188          68 :                                 conn->auth_plugin_data_len = plugin_data_len;
    2189          68 :                                 conn->auth_plugin_data = mnd_pemalloc(conn->auth_plugin_data_len, conn->persistent);
    2190          68 :                                 if (!conn->auth_plugin_data) {
    2191           0 :                                         SET_OOM_ERROR(*conn->error_info);
    2192           0 :                                         ret = FAIL;
    2193           0 :                                         goto end;
    2194             :                                 }
    2195          68 :                                 memcpy(conn->auth_plugin_data, plugin_data, plugin_data_len);
    2196             : 
    2197          68 :                                 DBG_INF_FMT("salt=[%*.s]", plugin_data_len - 1, plugin_data);
    2198             : 
    2199             :                                 /* The data should be allocated with malloc() */
    2200          68 :                                 scrambled_data =
    2201          68 :                                                 auth_plugin->methods.get_auth_data(NULL, &scrambled_data_len, conn, user, passwd, passwd_len,
    2202             :                                                                                                                    plugin_data, plugin_data_len, 0, conn->server_capabilities TSRMLS_CC);
    2203             : 
    2204             : 
    2205          68 :                                 ret = mysqlnd_auth_change_user(conn, user, strlen(user), passwd, passwd_len, db, strlen(db), silent,
    2206             :                                                                                            first_call,
    2207             :                                                                                            requested_protocol,
    2208             :                                                                                            scrambled_data, scrambled_data_len,
    2209             :                                                                                            &switch_to_auth_protocol, &switch_to_auth_protocol_len,
    2210             :                                                                                            &switch_to_auth_protocol_data, &switch_to_auth_protocol_data_len
    2211             :                                                                                            TSRMLS_CC);
    2212             : 
    2213          68 :                                 first_call = FALSE;
    2214          68 :                                 free(scrambled_data);
    2215             : 
    2216          68 :                                 DBG_INF_FMT("switch_to_auth_protocol=%s", switch_to_auth_protocol? switch_to_auth_protocol:"n/a");
    2217          68 :                                 if (requested_protocol) {
    2218          68 :                                         mnd_efree(requested_protocol);
    2219             :                                 }
    2220          68 :                                 requested_protocol = switch_to_auth_protocol;
    2221             : 
    2222          68 :                                 if (plugin_data) {
    2223          68 :                                         mnd_efree(plugin_data);
    2224             :                                 }
    2225          68 :                                 plugin_data_len = switch_to_auth_protocol_data_len;
    2226          68 :                                 plugin_data = switch_to_auth_protocol_data;
    2227             :                         }
    2228          68 :                         DBG_INF_FMT("conn->error_info->error_no = %d", conn->error_info->error_no);
    2229          68 :                 } while (ret == FAIL && conn->error_info->error_no == 0 && switch_to_auth_protocol != NULL);
    2230          68 :                 if (plugin_data) {
    2231           0 :                         mnd_efree(plugin_data);
    2232             :                 }
    2233          68 :                 if (ret == PASS) {
    2234          55 :                         conn->m->set_client_option(conn, MYSQLND_OPT_AUTH_PROTOCOL, requested_protocol TSRMLS_CC);
    2235             :                 }
    2236          68 :                 if (requested_protocol) {
    2237           0 :                         mnd_efree(requested_protocol);
    2238             :                 }
    2239             :         }
    2240             :         /*
    2241             :           Here we should close all statements. Unbuffered queries should not be a
    2242             :           problem as we won't allow sending COM_CHANGE_USER.
    2243             :         */
    2244             : end:
    2245          68 :         if (TRUE == local_tx_started) {
    2246          68 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2247             :         }
    2248          68 :         DBG_INF(ret == PASS? "PASS":"FAIL");
    2249          68 :         DBG_RETURN(ret);
    2250             : }
    2251             : /* }}} */
    2252             : 
    2253             : 
    2254             : /* {{{ mysqlnd_conn_data::set_client_option */
    2255             : static enum_func_status
    2256        5392 : MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
    2257             :                                                                                                 enum mysqlnd_option option,
    2258             :                                                                                                 const char * const value
    2259             :                                                                                                 TSRMLS_DC)
    2260             : {
    2261        5392 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_client_option);
    2262        5392 :         enum_func_status ret = PASS;
    2263        5392 :         DBG_ENTER("mysqlnd_conn_data::set_client_option");
    2264        5392 :         DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
    2265             : 
    2266        5392 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2267           0 :                 goto end;
    2268             :         }
    2269        5392 :         switch (option) {
    2270             :                 case MYSQL_OPT_COMPRESS:
    2271             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2272             :                 case MYSQL_OPT_READ_TIMEOUT:
    2273             :                 case MYSQL_OPT_WRITE_TIMEOUT:
    2274             : #endif
    2275             :                 case MYSQLND_OPT_SSL_KEY:
    2276             :                 case MYSQLND_OPT_SSL_CERT:
    2277             :                 case MYSQLND_OPT_SSL_CA:
    2278             :                 case MYSQLND_OPT_SSL_CAPATH:
    2279             :                 case MYSQLND_OPT_SSL_CIPHER:
    2280             :                 case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
    2281             :                 case MYSQL_OPT_CONNECT_TIMEOUT:
    2282             :                 case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
    2283             :                 case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
    2284         244 :                         ret = conn->net->m.set_client_option(conn->net, option, value TSRMLS_CC);
    2285         244 :                         break;
    2286             : #if MYSQLND_UNICODE
    2287             :                 case MYSQLND_OPT_NUMERIC_AND_DATETIME_AS_UNICODE:
    2288             :                         conn->options->numeric_and_datetime_as_unicode = *(unsigned int*) value;
    2289             :                         break;
    2290             : #endif
    2291             : #ifdef MYSQLND_STRING_TO_INT_CONVERSION
    2292             :                 case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
    2293          20 :                         conn->options->int_and_float_native = *(unsigned int*) value;
    2294          20 :                         break;
    2295             : #endif
    2296             :                 case MYSQL_OPT_LOCAL_INFILE:
    2297        2694 :                         if (value && (*(unsigned int*) value) ? 1 : 0) {
    2298        1333 :                                 conn->options->flags |= CLIENT_LOCAL_FILES;
    2299             :                         } else {
    2300          28 :                                 conn->options->flags &= ~CLIENT_LOCAL_FILES;
    2301             :                         }
    2302        1361 :                         break;
    2303             :                 case MYSQL_INIT_COMMAND:
    2304             :                 {
    2305             :                         char ** new_init_commands;
    2306             :                         char * new_command;
    2307             :                         /* when num_commands is 0, then realloc will be effectively a malloc call, internally */
    2308             :                         /* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
    2309          13 :                         new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
    2310          13 :                         if (!new_init_commands) {
    2311           0 :                                 goto oom;
    2312             :                         }
    2313          13 :                         conn->options->init_commands = new_init_commands;
    2314          13 :                         new_command = mnd_pestrdup(value, conn->persistent);
    2315          13 :                         if (!new_command) {
    2316           0 :                                 goto oom;
    2317             :                         }
    2318          13 :                         conn->options->init_commands[conn->options->num_commands] = new_command;
    2319          13 :                         ++conn->options->num_commands;
    2320          13 :                         break;
    2321             :                 }
    2322             :                 case MYSQL_READ_DEFAULT_FILE:
    2323             :                 case MYSQL_READ_DEFAULT_GROUP:
    2324             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2325             :                 case MYSQL_SET_CLIENT_IP:
    2326             :                 case MYSQL_REPORT_DATA_TRUNCATION:
    2327             : #endif
    2328             :                         /* currently not supported. Todo!! */
    2329           8 :                         break;
    2330             :                 case MYSQL_SET_CHARSET_NAME:
    2331             :                 {
    2332             :                         char * new_charset_name;
    2333          41 :                         if (!mysqlnd_find_charset_name(value)) {
    2334           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
    2335           0 :                                 ret = FAIL;
    2336           0 :                                 break;
    2337             :                         }
    2338             :                                 
    2339          41 :                         new_charset_name = mnd_pestrdup(value, conn->persistent);
    2340          41 :                         if (!new_charset_name) {
    2341           0 :                                 goto oom;
    2342             :                         }
    2343          41 :                         if (conn->options->charset_name) {
    2344          36 :                                 mnd_pefree(conn->options->charset_name, conn->persistent);
    2345             :                         }
    2346          41 :                         conn->options->charset_name = new_charset_name;
    2347          41 :                         DBG_INF_FMT("charset=%s", conn->options->charset_name);
    2348          41 :                         break;
    2349             :                 }
    2350             :                 case MYSQL_OPT_NAMED_PIPE:
    2351           0 :                         conn->options->protocol = MYSQL_PROTOCOL_PIPE;
    2352           0 :                         break;
    2353             :                 case MYSQL_OPT_PROTOCOL:
    2354           0 :                         if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
    2355           0 :                                 conn->options->protocol = *(unsigned int*) value;
    2356             :                         }
    2357           0 :                         break;
    2358             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2359             :                 case MYSQL_SET_CHARSET_DIR:
    2360             :                 case MYSQL_OPT_RECONNECT:
    2361             :                         /* we don't need external character sets, all character sets are
    2362             :                            compiled in. For compatibility we just ignore this setting.
    2363             :                            Same for protocol, we don't support old protocol */
    2364             :                 case MYSQL_OPT_USE_REMOTE_CONNECTION:
    2365             :                 case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
    2366             :                 case MYSQL_OPT_GUESS_CONNECTION:
    2367             :                         /* todo: throw an error, we don't support embedded */
    2368             :                         break;
    2369             : #endif
    2370             :                 case MYSQLND_OPT_MAX_ALLOWED_PACKET:
    2371        1850 :                         if (*(unsigned int*) value > (1<<16)) {
    2372        1850 :                                 conn->options->max_allowed_packet = *(unsigned int*) value;
    2373             :                         }
    2374        1850 :                         break;
    2375             :                 case MYSQLND_OPT_AUTH_PROTOCOL:
    2376             :                 {
    2377        1855 :                         char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
    2378        1855 :                         if (value && !new_auth_protocol) {
    2379           0 :                                 goto oom;
    2380             :                         }
    2381        1855 :                         if (conn->options->auth_protocol) {
    2382          35 :                                 mnd_pefree(conn->options->auth_protocol, conn->persistent);
    2383             :                         }
    2384        1855 :                         conn->options->auth_protocol = new_auth_protocol;
    2385        1855 :                         DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
    2386        1855 :                         break;
    2387             :                 }
    2388             :                 case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
    2389           0 :                         if (value && (*(unsigned int*) value) ? 1 : 0) {
    2390           0 :                                 conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
    2391             :                         } else {
    2392           0 :                                 conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
    2393             :                         }
    2394           0 :                         break;
    2395             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2396             :                 case MYSQL_SHARED_MEMORY_BASE_NAME:
    2397             :                 case MYSQL_OPT_USE_RESULT:
    2398             :                 case MYSQL_SECURE_AUTH:
    2399             :                         /* not sure, todo ? */
    2400             : #endif
    2401             :                 default:
    2402           0 :                         ret = FAIL;
    2403             :         }
    2404        5392 :         conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2405        5392 :         DBG_RETURN(ret);
    2406             : oom:
    2407           0 :         SET_OOM_ERROR(*conn->error_info);
    2408           0 :         conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);   
    2409             : end:
    2410           0 :         DBG_RETURN(FAIL);
    2411             : }
    2412             : /* }}} */
    2413             : 
    2414             : 
    2415             : /* {{{ mysqlnd_conn_data::use_result */
    2416             : static MYSQLND_RES *
    2417          74 : MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2418             : {
    2419          74 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, use_result);
    2420          74 :         MYSQLND_RES * result = NULL;
    2421             : 
    2422          74 :         DBG_ENTER("mysqlnd_conn_data::use_result");
    2423          74 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    2424             : 
    2425          74 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2426             :                 do {
    2427          74 :                         if (!conn->current_result) {
    2428          10 :                                 break;
    2429             :                         }
    2430             : 
    2431             :                         /* Nothing to store for UPSERT/LOAD DATA */
    2432          64 :                         if (conn->last_query_type != QUERY_SELECT || CONN_GET_STATE(conn) != CONN_FETCHING_DATA) {
    2433           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
    2434           0 :                                 DBG_ERR("Command out of sync");
    2435           0 :                                 break;
    2436             :                         }
    2437             : 
    2438          64 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
    2439             : 
    2440          64 :                         conn->current_result->conn = conn->m->get_reference(conn TSRMLS_CC);
    2441          64 :                         result = conn->current_result->m.use_result(conn->current_result, FALSE TSRMLS_CC);
    2442             : 
    2443          64 :                         if (!result) {
    2444           0 :                                 conn->current_result->m.free_result(conn->current_result, TRUE TSRMLS_CC);
    2445             :                         }
    2446          64 :                         conn->current_result = NULL;
    2447             :                 } while (0);
    2448             : 
    2449          74 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);      
    2450             :         }
    2451             : 
    2452          74 :         DBG_RETURN(result);
    2453             : }
    2454             : /* }}} */
    2455             : 
    2456             : 
    2457             : /* {{{ mysqlnd_conn_data::store_result */
    2458             : static MYSQLND_RES *
    2459        2873 : MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2460             : {
    2461        2873 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, store_result);
    2462        2873 :         MYSQLND_RES * result = NULL;
    2463             : 
    2464        2873 :         DBG_ENTER("mysqlnd_conn_data::store_result");
    2465        2873 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    2466             : 
    2467        2873 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2468             :                 do {
    2469        2873 :                         if (!conn->current_result) {
    2470         631 :                                 break;
    2471             :                         }
    2472             : 
    2473             :                         /* Nothing to store for UPSERT/LOAD DATA*/
    2474        2242 :                         if (conn->last_query_type != QUERY_SELECT || CONN_GET_STATE(conn) != CONN_FETCHING_DATA) {
    2475           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
    2476           0 :                                 DBG_ERR("Command out of sync");
    2477           0 :                                 break;
    2478             :                         }
    2479             : 
    2480        2242 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
    2481             : 
    2482        2242 :                         result = conn->current_result->m.store_result(conn->current_result, conn, FALSE TSRMLS_CC);
    2483        2242 :                         if (!result) {
    2484           0 :                                 conn->current_result->m.free_result(conn->current_result, TRUE TSRMLS_CC);
    2485             :                         }
    2486        2242 :                         conn->current_result = NULL;
    2487             :                 } while (0);
    2488             : 
    2489        2873 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);      
    2490             :         }
    2491        2873 :         DBG_RETURN(result);
    2492             : }
    2493             : /* }}} */
    2494             : 
    2495             : 
    2496             : /* {{{ mysqlnd_conn_data::get_connection_stats */
    2497             : static void
    2498           5 : MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
    2499             :                                                                                                    zval * return_value TSRMLS_DC ZEND_FILE_LINE_DC)
    2500             : {
    2501           5 :         DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
    2502           5 :         mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value TSRMLS_CC ZEND_FILE_LINE_CC);
    2503           5 :         DBG_VOID_RETURN;
    2504             : }
    2505             : /* }}} */
    2506             : 
    2507             : 
    2508             : /* {{{ mysqlnd_conn_data::set_autocommit */
    2509             : static enum_func_status
    2510          32 : MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode TSRMLS_DC)
    2511             : {
    2512          32 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_autocommit);
    2513          32 :         enum_func_status ret = FAIL;
    2514          32 :         DBG_ENTER("mysqlnd_conn_data::set_autocommit");
    2515             : 
    2516          32 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2517          32 :                 ret = conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1 TSRMLS_CC);
    2518          32 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2519             :         }
    2520             : 
    2521          32 :         DBG_RETURN(ret);
    2522             : }
    2523             : /* }}} */
    2524             : 
    2525             : 
    2526             : /* {{{ mysqlnd_conn_data::tx_commit */
    2527             : static enum_func_status
    2528          16 : MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    2529             : {
    2530          16 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_commit);
    2531          16 :         enum_func_status ret = FAIL;
    2532          16 :         DBG_ENTER("mysqlnd_conn_data::tx_commit");
    2533             : 
    2534          16 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2535          16 :                 ret = conn->m->query(conn, "COMMIT", sizeof("COMMIT") - 1 TSRMLS_CC);
    2536          16 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2537             :         }
    2538             : 
    2539          16 :         DBG_RETURN(ret);
    2540             : }
    2541             : /* }}} */
    2542             : 
    2543             : 
    2544             : /* {{{ mysqlnd_conn_data::tx_rollback */
    2545             : static enum_func_status
    2546          14 : MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    2547             : {
    2548          14 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_rollback);
    2549          14 :         enum_func_status ret = FAIL;
    2550          14 :         DBG_ENTER("mysqlnd_conn_data::tx_rollback");
    2551             : 
    2552          14 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2553          14 :                 ret = conn->m->query(conn, "ROLLBACK", sizeof("ROLLBACK") - 1 TSRMLS_CC);
    2554          14 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2555             :         }
    2556             : 
    2557          14 :         DBG_RETURN(ret);
    2558             : }
    2559             : /* }}} */
    2560             : 
    2561             : 
    2562             : /* {{{ mysqlnd_conn_data::local_tx_start */
    2563             : static enum_func_status
    2564       88081 : MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start)(MYSQLND_CONN_DATA * conn, size_t this_func TSRMLS_DC)
    2565             : {
    2566       88081 :         enum_func_status ret = PASS;
    2567       88081 :         DBG_ENTER("mysqlnd_conn_data::local_tx_start");
    2568       88081 :         DBG_RETURN(ret);
    2569             : }
    2570             : /* }}} */
    2571             : 
    2572             : 
    2573             : /* {{{ mysqlnd_conn_data::local_tx_end */
    2574             : static enum_func_status
    2575       88081 : MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end)(MYSQLND_CONN_DATA * conn, size_t this_func, enum_func_status status TSRMLS_DC)
    2576             : {
    2577       88081 :         DBG_ENTER("mysqlnd_conn_data::local_tx_end");
    2578       88081 :         DBG_RETURN(status);
    2579             : }
    2580             : /* }}} */
    2581             : 
    2582             : 
    2583             : /* {{{ mysqlnd_conn_data::init */
    2584             : static enum_func_status
    2585        1915 : MYSQLND_METHOD(mysqlnd_conn_data, init)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    2586             : {
    2587        1915 :         DBG_ENTER("mysqlnd_conn_data::init");
    2588        1915 :         mysqlnd_stats_init(&conn->stats, STAT_LAST);
    2589        1915 :         SET_ERROR_AFF_ROWS(conn);
    2590             : 
    2591        1915 :         conn->net = mysqlnd_net_init(conn->persistent, conn->stats, conn->error_info TSRMLS_CC);
    2592        1915 :         conn->protocol = mysqlnd_protocol_init(conn->persistent TSRMLS_CC);
    2593             : 
    2594        1915 :         DBG_RETURN(conn->stats && conn->net && conn->protocol? PASS:FAIL);
    2595             : }
    2596             : /* }}} */
    2597             : 
    2598             : 
    2599             : MYSQLND_STMT * _mysqlnd_stmt_init(MYSQLND_CONN_DATA * const conn TSRMLS_DC);
    2600             : 
    2601             : 
    2602             : MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
    2603             :         MYSQLND_METHOD(mysqlnd_conn_data, init),
    2604             :         MYSQLND_METHOD(mysqlnd_conn_data, connect),
    2605             : 
    2606             :         MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
    2607             :         MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
    2608             :         MYSQLND_METHOD(mysqlnd_conn_data, query),
    2609             :         MYSQLND_METHOD(mysqlnd_conn_data, send_query),
    2610             :         MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
    2611             :         MYSQLND_METHOD(mysqlnd_conn_data, use_result),
    2612             :         MYSQLND_METHOD(mysqlnd_conn_data, store_result),
    2613             :         MYSQLND_METHOD(mysqlnd_conn_data, next_result),
    2614             :         MYSQLND_METHOD(mysqlnd_conn_data, more_results),
    2615             : 
    2616             :         _mysqlnd_stmt_init,
    2617             : 
    2618             :         MYSQLND_METHOD(mysqlnd_conn_data, shutdown),
    2619             :         MYSQLND_METHOD(mysqlnd_conn_data, refresh),
    2620             : 
    2621             :         MYSQLND_METHOD(mysqlnd_conn_data, ping),
    2622             :         MYSQLND_METHOD(mysqlnd_conn_data, kill),
    2623             :         MYSQLND_METHOD(mysqlnd_conn_data, select_db),
    2624             :         MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
    2625             :         MYSQLND_METHOD(mysqlnd_conn_data, change_user),
    2626             : 
    2627             :         MYSQLND_METHOD(mysqlnd_conn_data, errno),
    2628             :         MYSQLND_METHOD(mysqlnd_conn_data, error),
    2629             :         MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
    2630             :         MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
    2631             : 
    2632             :         MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
    2633             : 
    2634             :         MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
    2635             :         MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
    2636             :         MYSQLND_METHOD(mysqlnd_conn_data, statistic),
    2637             :         MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
    2638             :         MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
    2639             :         MYSQLND_METHOD(mysqlnd_conn_data, info),
    2640             :         MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
    2641             :         MYSQLND_METHOD(mysqlnd_conn_data, list_fields),
    2642             :         MYSQLND_METHOD(mysqlnd_conn_data, list_method),
    2643             : 
    2644             :         MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
    2645             :         MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
    2646             :         MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
    2647             :         MYSQLND_METHOD(mysqlnd_conn_data, field_count),
    2648             : 
    2649             :         MYSQLND_METHOD(mysqlnd_conn_data, server_status),
    2650             : 
    2651             :         MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
    2652             :         MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
    2653             :         MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
    2654             :         MYSQLND_METHOD(mysqlnd_conn_data, free_options),
    2655             : 
    2656             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
    2657             : 
    2658             :         mysqlnd_query_read_result_set_header,
    2659             : 
    2660             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
    2661             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
    2662             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_state),
    2663             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, set_state),
    2664             : 
    2665             :         MYSQLND_METHOD(mysqlnd_conn_data, simple_command),
    2666             :         MYSQLND_METHOD(mysqlnd_conn_data, simple_command_handle_response),
    2667             :         MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
    2668             :         MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
    2669             :         MYSQLND_METHOD(mysqlnd_conn_data, send_close),
    2670             : 
    2671             :         MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
    2672             :         mysqlnd_result_init,
    2673             :         MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
    2674             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
    2675             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
    2676             :         MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start),
    2677             :         MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end)
    2678             : MYSQLND_CLASS_METHODS_END;
    2679             : 
    2680             : 
    2681             : /* {{{ mysqlnd_conn::get_reference */
    2682             : static MYSQLND *
    2683           0 : MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn TSRMLS_DC)
    2684             : {
    2685             :         MYSQLND * ret;
    2686           0 :         DBG_ENTER("mysqlnd_conn::get_reference");
    2687           0 :         ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).clone_connection_object(conn TSRMLS_CC);
    2688           0 :         DBG_RETURN(ret);
    2689             : }
    2690             : /* }}} */
    2691             : 
    2692             : 
    2693             : /* {{{ mysqlnd_conn_data::dtor */
    2694             : static void
    2695        1875 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn TSRMLS_DC)
    2696             : {
    2697        1875 :         DBG_ENTER("mysqlnd_conn::dtor");
    2698        1875 :         DBG_INF_FMT("conn=%llu", conn->data->thread_id);
    2699             : 
    2700        1875 :         conn->data->m->free_reference(conn->data TSRMLS_CC);
    2701             : 
    2702        1875 :         mnd_pefree(conn, conn->persistent);
    2703             : 
    2704        1875 :         DBG_VOID_RETURN;
    2705             : }
    2706             : /* }}} */
    2707             : 
    2708             : 
    2709             : /* {{{ mysqlnd_conn_data::close */
    2710             : static enum_func_status
    2711        1875 : MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, enum_connection_close_type close_type TSRMLS_DC)
    2712             : {
    2713        1875 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_methods, close);
    2714        1875 :         MYSQLND_CONN_DATA * conn = conn_handle->data;
    2715        1875 :         enum_func_status ret = FAIL;
    2716             : 
    2717        1875 :         DBG_ENTER("mysqlnd_conn::close");
    2718        1875 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    2719             : 
    2720        1875 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2721        1875 :                 if (CONN_GET_STATE(conn) >= CONN_READY) {
    2722             :                         static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
    2723             :                                 STAT_CLOSE_EXPLICIT,
    2724             :                                 STAT_CLOSE_IMPLICIT,
    2725             :                                 STAT_CLOSE_DISCONNECT
    2726             :                         };
    2727        1800 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
    2728             :                 }
    2729             : 
    2730             :                 /*
    2731             :                   Close now, free_reference will try,
    2732             :                   if we are last, but that's not a problem.
    2733             :                 */
    2734        1875 :                 ret = conn->m->send_close(conn TSRMLS_CC);
    2735             : 
    2736             :                 /* do it after free_reference/dtor and we might crash */
    2737        1875 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    2738             : 
    2739        1875 :                 conn_handle->m->dtor(conn_handle TSRMLS_CC);
    2740             :         }
    2741        1875 :         DBG_RETURN(ret);
    2742             : }
    2743             : /* }}} */
    2744             : 
    2745             : 
    2746             : MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
    2747             :         MYSQLND_METHOD(mysqlnd_conn, connect),
    2748             :         MYSQLND_METHOD(mysqlnd_conn, clone_object),
    2749             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
    2750             :         MYSQLND_METHOD(mysqlnd_conn, close)
    2751             : MYSQLND_CLASS_METHODS_END;
    2752             : 
    2753             : 
    2754             : /* {{{ _mysqlnd_init */
    2755             : PHPAPI MYSQLND *
    2756        1915 : _mysqlnd_init(zend_bool persistent TSRMLS_DC)
    2757             : {
    2758             :         MYSQLND * ret;
    2759        1915 :         DBG_ENTER("mysqlnd_init");
    2760        1915 :         ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).get_connection(persistent TSRMLS_CC);
    2761        1915 :         DBG_RETURN(ret);
    2762             : }
    2763             : /* }}} */
    2764             : 
    2765             : /*
    2766             :  * Local variables:
    2767             :  * tab-width: 4
    2768             :  * c-basic-offset: 4
    2769             :  * End:
    2770             :  * vim600: noet sw=4 ts=4 fdm=marker
    2771             :  * vim<600: noet sw=4 ts=4
    2772             :  */

Generated by: LCOV version 1.10

Generated at Mon, 29 Sep 2014 14:26:25 +0000 (2 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.