PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/mysqlnd/mysqlnd_net.c

http://github.com/php/php-src
C | 1232 lines | 930 code | 138 blank | 164 comment | 131 complexity | 54c016fb0f544f94ca0c9627d16825ce MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2006-2016 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@php.net> |
  16. | Ulf Wendel <uw@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include "php_globals.h"
  21. #include "mysqlnd.h"
  22. #include "mysqlnd_priv.h"
  23. #include "mysqlnd_wireprotocol.h"
  24. #include "mysqlnd_statistics.h"
  25. #include "mysqlnd_debug.h"
  26. #include "mysqlnd_ext_plugin.h"
  27. #include "php_network.h"
  28. #include "zend_ini.h"
  29. #ifdef MYSQLND_COMPRESSION_ENABLED
  30. #include <zlib.h>
  31. #endif
  32. #ifndef PHP_WIN32
  33. #include <netinet/tcp.h>
  34. #else
  35. #include <winsock.h>
  36. #endif
  37. /* {{{ mysqlnd_set_sock_no_delay */
  38. static int
  39. mysqlnd_set_sock_no_delay(php_stream * stream)
  40. {
  41. int socketd = ((php_netstream_data_t*)stream->abstract)->socket;
  42. int ret = SUCCESS;
  43. int flag = 1;
  44. int result = setsockopt(socketd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
  45. DBG_ENTER("mysqlnd_set_sock_no_delay");
  46. if (result == -1) {
  47. ret = FAILURE;
  48. }
  49. DBG_RETURN(ret);
  50. }
  51. /* }}} */
  52. /* {{{ mysqlnd_set_sock_keepalive */
  53. static int
  54. mysqlnd_set_sock_keepalive(php_stream * stream)
  55. {
  56. int socketd = ((php_netstream_data_t*)stream->abstract)->socket;
  57. int ret = SUCCESS;
  58. int flag = 1;
  59. int result = setsockopt(socketd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, sizeof(int));
  60. DBG_ENTER("mysqlnd_set_sock_keepalive");
  61. if (result == -1) {
  62. ret = FAILURE;
  63. }
  64. DBG_RETURN(ret);
  65. }
  66. /* }}} */
  67. /* {{{ mysqlnd_net::network_read_ex */
  68. static enum_func_status
  69. MYSQLND_METHOD(mysqlnd_net, network_read_ex)(MYSQLND_NET * const net, zend_uchar * const buffer, const size_t count,
  70. MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  71. {
  72. enum_func_status return_value = PASS;
  73. php_stream * net_stream = net->data->m.get_stream(net);
  74. size_t old_chunk_size = net_stream->chunk_size;
  75. size_t to_read = count, ret;
  76. zend_uchar * p = buffer;
  77. DBG_ENTER("mysqlnd_net::network_read_ex");
  78. DBG_INF_FMT("count="MYSQLND_SZ_T_SPEC, count);
  79. net_stream->chunk_size = MIN(to_read, net->data->options.net_read_buffer_size);
  80. while (to_read) {
  81. if (!(ret = php_stream_read(net_stream, (char *) p, to_read))) {
  82. DBG_ERR_FMT("Error while reading header from socket");
  83. return_value = FAIL;
  84. break;
  85. }
  86. p += ret;
  87. to_read -= ret;
  88. }
  89. MYSQLND_INC_CONN_STATISTIC_W_VALUE(stats, STAT_BYTES_RECEIVED, count - to_read);
  90. net_stream->chunk_size = old_chunk_size;
  91. DBG_RETURN(return_value);
  92. }
  93. /* }}} */
  94. /* {{{ mysqlnd_net::network_write_ex */
  95. static size_t
  96. MYSQLND_METHOD(mysqlnd_net, network_write_ex)(MYSQLND_NET * const net, const zend_uchar * const buffer, const size_t count,
  97. MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  98. {
  99. size_t ret;
  100. DBG_ENTER("mysqlnd_net::network_write_ex");
  101. DBG_INF_FMT("sending %u bytes", count);
  102. ret = php_stream_write(net->data->m.get_stream(net), (char *)buffer, count);
  103. DBG_RETURN(ret);
  104. }
  105. /* }}} */
  106. /* {{{ mysqlnd_net::open_pipe */
  107. static php_stream *
  108. MYSQLND_METHOD(mysqlnd_net, open_pipe)(MYSQLND_NET * const net, const char * const scheme, const size_t scheme_len,
  109. const zend_bool persistent,
  110. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  111. {
  112. unsigned int streams_options = 0;
  113. dtor_func_t origin_dtor;
  114. php_stream * net_stream = NULL;
  115. DBG_ENTER("mysqlnd_net::open_pipe");
  116. if (persistent) {
  117. streams_options |= STREAM_OPEN_PERSISTENT;
  118. }
  119. streams_options |= IGNORE_URL;
  120. net_stream = php_stream_open_wrapper((char*) scheme + sizeof("pipe://") - 1, "r+", streams_options, NULL);
  121. if (!net_stream) {
  122. SET_CLIENT_ERROR(*error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, "Unknown errror while connecting");
  123. DBG_RETURN(NULL);
  124. }
  125. /*
  126. Streams are not meant for C extensions! Thus we need a hack. Every connected stream will
  127. be registered as resource (in EG(regular_list). So far, so good. However, it won't be
  128. unregistered until the script ends. So, we need to take care of that.
  129. */
  130. origin_dtor = EG(regular_list).pDestructor;
  131. EG(regular_list).pDestructor = NULL;
  132. zend_hash_index_del(&EG(regular_list), net_stream->res->handle); /* ToDO: should it be res->handle, do streams register with addref ?*/
  133. EG(regular_list).pDestructor = origin_dtor;
  134. net_stream->res = NULL;
  135. DBG_RETURN(net_stream);
  136. }
  137. /* }}} */
  138. /* {{{ mysqlnd_net::open_tcp_or_unix */
  139. static php_stream *
  140. MYSQLND_METHOD(mysqlnd_net, open_tcp_or_unix)(MYSQLND_NET * const net, const char * const scheme, const size_t scheme_len,
  141. const zend_bool persistent,
  142. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  143. {
  144. unsigned int streams_options = 0;
  145. unsigned int streams_flags = STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT;
  146. char * hashed_details = NULL;
  147. int hashed_details_len = 0;
  148. zend_string *errstr = NULL;
  149. int errcode = 0;
  150. struct timeval tv;
  151. dtor_func_t origin_dtor;
  152. php_stream * net_stream = NULL;
  153. DBG_ENTER("mysqlnd_net::open_tcp_or_unix");
  154. net->data->stream = NULL;
  155. if (persistent) {
  156. hashed_details_len = mnd_sprintf(&hashed_details, 0, "%p", net);
  157. DBG_INF_FMT("hashed_details=%s", hashed_details);
  158. }
  159. if (net->data->options.timeout_connect) {
  160. tv.tv_sec = net->data->options.timeout_connect;
  161. tv.tv_usec = 0;
  162. }
  163. DBG_INF_FMT("calling php_stream_xport_create");
  164. net_stream = php_stream_xport_create(scheme, scheme_len, streams_options, streams_flags,
  165. hashed_details, (net->data->options.timeout_connect) ? &tv : NULL,
  166. NULL /*ctx*/, &errstr, &errcode);
  167. if (errstr || !net_stream) {
  168. DBG_ERR("Error");
  169. if (hashed_details) {
  170. mnd_sprintf_free(hashed_details);
  171. }
  172. errcode = CR_CONNECTION_ERROR;
  173. SET_CLIENT_ERROR(*error_info,
  174. CR_CONNECTION_ERROR,
  175. UNKNOWN_SQLSTATE,
  176. errstr? ZSTR_VAL(errstr):"Unknown error while connecting");
  177. if (errstr) {
  178. zend_string_release(errstr);
  179. }
  180. DBG_RETURN(NULL);
  181. }
  182. if (hashed_details) {
  183. /*
  184. If persistent, the streams register it in EG(persistent_list).
  185. This is unwanted. ext/mysql or ext/mysqli are responsible to clean,
  186. whatever they have to.
  187. */
  188. zend_resource *le;
  189. if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashed_details, hashed_details_len))) {
  190. origin_dtor = EG(persistent_list).pDestructor;
  191. /*
  192. in_free will let streams code skip destructing - big HACK,
  193. but STREAMS suck big time regarding persistent streams.
  194. Just not compatible for extensions that need persistency.
  195. */
  196. EG(persistent_list).pDestructor = NULL;
  197. zend_hash_str_del(&EG(persistent_list), hashed_details, hashed_details_len);
  198. EG(persistent_list).pDestructor = origin_dtor;
  199. pefree(le, 1);
  200. }
  201. #if ZEND_DEBUG
  202. /* Shut-up the streams, they don't know what they are doing */
  203. net_stream->__exposed = 1;
  204. #endif
  205. mnd_sprintf_free(hashed_details);
  206. }
  207. /*
  208. Streams are not meant for C extensions! Thus we need a hack. Every connected stream will
  209. be registered as resource (in EG(regular_list). So far, so good. However, it won't be
  210. unregistered until the script ends. So, we need to take care of that.
  211. */
  212. origin_dtor = EG(regular_list).pDestructor;
  213. EG(regular_list).pDestructor = NULL;
  214. zend_hash_index_del(&EG(regular_list), net_stream->res->handle); /* ToDO: should it be res->handle, do streams register with addref ?*/
  215. efree(net_stream->res);
  216. net_stream->res = NULL;
  217. EG(regular_list).pDestructor = origin_dtor;
  218. DBG_RETURN(net_stream);
  219. }
  220. /* }}} */
  221. /* {{{ mysqlnd_net::post_connect_set_opt */
  222. static void
  223. MYSQLND_METHOD(mysqlnd_net, post_connect_set_opt)(MYSQLND_NET * const net,
  224. const char * const scheme, const size_t scheme_len,
  225. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  226. {
  227. php_stream * net_stream = net->data->m.get_stream(net);
  228. DBG_ENTER("mysqlnd_net::post_connect_set_opt");
  229. if (net_stream) {
  230. if (net->data->options.timeout_read) {
  231. struct timeval tv;
  232. DBG_INF_FMT("setting %u as PHP_STREAM_OPTION_READ_TIMEOUT", net->data->options.timeout_read);
  233. tv.tv_sec = net->data->options.timeout_read;
  234. tv.tv_usec = 0;
  235. php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
  236. }
  237. if (!memcmp(scheme, "tcp://", sizeof("tcp://") - 1)) {
  238. /* TCP -> Set TCP_NODELAY */
  239. mysqlnd_set_sock_no_delay(net_stream);
  240. /* TCP -> Set SO_KEEPALIVE */
  241. mysqlnd_set_sock_keepalive(net_stream);
  242. }
  243. }
  244. DBG_VOID_RETURN;
  245. }
  246. /* }}} */
  247. /* {{{ mysqlnd_net::get_open_stream */
  248. static func_mysqlnd_net__open_stream
  249. MYSQLND_METHOD(mysqlnd_net, get_open_stream)(MYSQLND_NET * const net, const char * const scheme, const size_t scheme_len,
  250. MYSQLND_ERROR_INFO * const error_info)
  251. {
  252. func_mysqlnd_net__open_stream ret = NULL;
  253. DBG_ENTER("mysqlnd_net::get_open_stream");
  254. if (scheme_len > (sizeof("pipe://") - 1) && !memcmp(scheme, "pipe://", sizeof("pipe://") - 1)) {
  255. ret = net->data->m.open_pipe;
  256. } else if ((scheme_len > (sizeof("tcp://") - 1) && !memcmp(scheme, "tcp://", sizeof("tcp://") - 1))
  257. ||
  258. (scheme_len > (sizeof("unix://") - 1) && !memcmp(scheme, "unix://", sizeof("unix://") - 1)))
  259. {
  260. ret = net->data->m.open_tcp_or_unix;
  261. }
  262. if (!ret) {
  263. SET_CLIENT_ERROR(*error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, "No handler for this scheme");
  264. }
  265. DBG_RETURN(ret);
  266. }
  267. /* }}} */
  268. /* {{{ mysqlnd_net::connect_ex */
  269. static enum_func_status
  270. MYSQLND_METHOD(mysqlnd_net, connect_ex)(MYSQLND_NET * const net, const char * const scheme, const size_t scheme_len,
  271. const zend_bool persistent,
  272. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  273. {
  274. enum_func_status ret = FAIL;
  275. func_mysqlnd_net__open_stream open_stream = NULL;
  276. DBG_ENTER("mysqlnd_net::connect_ex");
  277. net->packet_no = net->compressed_envelope_packet_no = 0;
  278. net->data->m.close_stream(net, conn_stats, error_info);
  279. open_stream = net->data->m.get_open_stream(net, scheme, scheme_len, error_info);
  280. if (open_stream) {
  281. php_stream * net_stream = open_stream(net, scheme, scheme_len, persistent, conn_stats, error_info);
  282. if (net_stream) {
  283. (void) net->data->m.set_stream(net, net_stream);
  284. net->data->m.post_connect_set_opt(net, scheme, scheme_len, conn_stats, error_info);
  285. ret = PASS;
  286. }
  287. }
  288. DBG_RETURN(ret);
  289. }
  290. /* }}} */
  291. /* We assume that MYSQLND_HEADER_SIZE is 4 bytes !! */
  292. #define COPY_HEADER(T,A) do { \
  293. *(((char *)(T))) = *(((char *)(A)));\
  294. *(((char *)(T))+1) = *(((char *)(A))+1);\
  295. *(((char *)(T))+2) = *(((char *)(A))+2);\
  296. *(((char *)(T))+3) = *(((char *)(A))+3); } while (0)
  297. #define STORE_HEADER_SIZE(safe_storage, buffer) COPY_HEADER((safe_storage), (buffer))
  298. #define RESTORE_HEADER_SIZE(buffer, safe_storage) STORE_HEADER_SIZE((safe_storage), (buffer))
  299. /* {{{ mysqlnd_net::send_ex */
  300. /*
  301. IMPORTANT : It's expected that buffer has place in the beginning for MYSQLND_HEADER_SIZE !!!!
  302. This is done for performance reasons in the caller of this function.
  303. Otherwise we will have to do send two TCP packets, or do new alloc and memcpy.
  304. Neither are quick, thus the clients of this function are obligated to do
  305. what they are asked for.
  306. `count` is actually the length of the payload data. Thus :
  307. count + MYSQLND_HEADER_SIZE = sizeof(buffer) (not the pointer but the actual buffer)
  308. */
  309. static size_t
  310. MYSQLND_METHOD(mysqlnd_net, send_ex)(MYSQLND_NET * const net, zend_uchar * const buffer, const size_t count,
  311. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  312. {
  313. zend_uchar safe_buf[((MYSQLND_HEADER_SIZE) + (sizeof(zend_uchar)) - 1) / (sizeof(zend_uchar))];
  314. zend_uchar * safe_storage = safe_buf;
  315. size_t bytes_sent, packets_sent = 1;
  316. size_t left = count;
  317. zend_uchar * p = (zend_uchar *) buffer;
  318. zend_uchar * compress_buf = NULL;
  319. size_t to_be_sent;
  320. DBG_ENTER("mysqlnd_net::send_ex");
  321. DBG_INF_FMT("count=" MYSQLND_SZ_T_SPEC " compression=%u", count, net->data->compressed);
  322. if (net->data->compressed == TRUE) {
  323. size_t comp_buf_size = MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE + MIN(left, MYSQLND_MAX_PACKET_SIZE);
  324. DBG_INF_FMT("compress_buf_size="MYSQLND_SZ_T_SPEC, comp_buf_size);
  325. compress_buf = mnd_emalloc(comp_buf_size);
  326. }
  327. do {
  328. to_be_sent = MIN(left, MYSQLND_MAX_PACKET_SIZE);
  329. DBG_INF_FMT("to_be_sent=%u", to_be_sent);
  330. DBG_INF_FMT("packets_sent=%u", packets_sent);
  331. DBG_INF_FMT("compressed_envelope_packet_no=%u", net->compressed_envelope_packet_no);
  332. DBG_INF_FMT("packet_no=%u", net->packet_no);
  333. #ifdef MYSQLND_COMPRESSION_ENABLED
  334. if (net->data->compressed == TRUE) {
  335. /* here we need to compress the data and then write it, first comes the compressed header */
  336. size_t tmp_complen = to_be_sent;
  337. size_t payload_size;
  338. zend_uchar * uncompressed_payload = p; /* should include the header */
  339. STORE_HEADER_SIZE(safe_storage, uncompressed_payload);
  340. int3store(uncompressed_payload, to_be_sent);
  341. int1store(uncompressed_payload + 3, net->packet_no);
  342. if (PASS == net->data->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
  343. uncompressed_payload, to_be_sent + MYSQLND_HEADER_SIZE))
  344. {
  345. int3store(compress_buf + MYSQLND_HEADER_SIZE, to_be_sent + MYSQLND_HEADER_SIZE);
  346. payload_size = tmp_complen;
  347. } else {
  348. int3store(compress_buf + MYSQLND_HEADER_SIZE, 0);
  349. memcpy(compress_buf + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, uncompressed_payload, to_be_sent + MYSQLND_HEADER_SIZE);
  350. payload_size = to_be_sent + MYSQLND_HEADER_SIZE;
  351. }
  352. RESTORE_HEADER_SIZE(uncompressed_payload, safe_storage);
  353. int3store(compress_buf, payload_size);
  354. int1store(compress_buf + 3, net->packet_no);
  355. DBG_INF_FMT("writing "MYSQLND_SZ_T_SPEC" bytes to the network", payload_size + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE);
  356. bytes_sent = net->data->m.network_write_ex(net, compress_buf, payload_size + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE,
  357. conn_stats, error_info);
  358. net->compressed_envelope_packet_no++;
  359. #if WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY
  360. if (res == Z_OK) {
  361. size_t decompressed_size = left + MYSQLND_HEADER_SIZE;
  362. zend_uchar * decompressed_data = mnd_malloc(decompressed_size);
  363. int error = net->data->m.decode(decompressed_data, decompressed_size,
  364. compress_buf + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, payload_size);
  365. if (error == Z_OK) {
  366. int i;
  367. DBG_INF("success decompressing");
  368. for (i = 0 ; i < decompressed_size; i++) {
  369. if (i && (i % 30 == 0)) {
  370. printf("\n\t\t");
  371. }
  372. printf("%.2X ", (int)*((char*)&(decompressed_data[i])));
  373. DBG_INF_FMT("%.2X ", (int)*((char*)&(decompressed_data[i])));
  374. }
  375. } else {
  376. DBG_INF("error decompressing");
  377. }
  378. mnd_free(decompressed_data);
  379. }
  380. #endif /* WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY */
  381. } else
  382. #endif /* MYSQLND_COMPRESSION_ENABLED */
  383. {
  384. DBG_INF("no compression");
  385. STORE_HEADER_SIZE(safe_storage, p);
  386. int3store(p, to_be_sent);
  387. int1store(p + 3, net->packet_no);
  388. bytes_sent = net->data->m.network_write_ex(net, p, to_be_sent + MYSQLND_HEADER_SIZE, conn_stats, error_info);
  389. RESTORE_HEADER_SIZE(p, safe_storage);
  390. net->compressed_envelope_packet_no++;
  391. }
  392. net->packet_no++;
  393. p += to_be_sent;
  394. left -= to_be_sent;
  395. packets_sent++;
  396. /*
  397. if left is 0 then there is nothing more to send, but if the last packet was exactly
  398. with the size MYSQLND_MAX_PACKET_SIZE we need to send additional packet, which has
  399. empty payload. Thus if left == 0 we check for to_be_sent being the max size. If it is
  400. indeed it then loop once more, then to_be_sent will become 0, left will stay 0. Empty
  401. packet will be sent and this loop will end.
  402. */
  403. } while (bytes_sent && (left > 0 || to_be_sent == MYSQLND_MAX_PACKET_SIZE));
  404. DBG_INF_FMT("packet_size="MYSQLND_SZ_T_SPEC" packet_no=%u", left, net->packet_no);
  405. MYSQLND_INC_CONN_STATISTIC_W_VALUE3(conn_stats,
  406. STAT_BYTES_SENT, count + packets_sent * MYSQLND_HEADER_SIZE,
  407. STAT_PROTOCOL_OVERHEAD_OUT, packets_sent * MYSQLND_HEADER_SIZE,
  408. STAT_PACKETS_SENT, packets_sent);
  409. if (compress_buf) {
  410. mnd_efree(compress_buf);
  411. }
  412. /* Even for zero size payload we have to send a packet */
  413. if (!bytes_sent) {
  414. DBG_ERR_FMT("Can't %u send bytes", count);
  415. SET_CLIENT_ERROR(*error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  416. }
  417. DBG_RETURN(bytes_sent);
  418. }
  419. /* }}} */
  420. #ifdef MYSQLND_COMPRESSION_ENABLED
  421. /* {{{ php_mysqlnd_read_buffer_is_empty */
  422. static zend_bool
  423. php_mysqlnd_read_buffer_is_empty(MYSQLND_READ_BUFFER * buffer)
  424. {
  425. return buffer->len? FALSE:TRUE;
  426. }
  427. /* }}} */
  428. /* {{{ php_mysqlnd_read_buffer_read */
  429. static void
  430. php_mysqlnd_read_buffer_read(MYSQLND_READ_BUFFER * buffer, size_t count, zend_uchar * dest)
  431. {
  432. if (buffer->len >= count) {
  433. memcpy(dest, buffer->data + buffer->offset, count);
  434. buffer->offset += count;
  435. buffer->len -= count;
  436. }
  437. }
  438. /* }}} */
  439. /* {{{ php_mysqlnd_read_buffer_bytes_left */
  440. static size_t
  441. php_mysqlnd_read_buffer_bytes_left(MYSQLND_READ_BUFFER * buffer)
  442. {
  443. return buffer->len;
  444. }
  445. /* }}} */
  446. /* {{{ php_mysqlnd_read_buffer_free */
  447. static void
  448. php_mysqlnd_read_buffer_free(MYSQLND_READ_BUFFER ** buffer)
  449. {
  450. DBG_ENTER("php_mysqlnd_read_buffer_free");
  451. if (*buffer) {
  452. mnd_efree((*buffer)->data);
  453. mnd_efree(*buffer);
  454. *buffer = NULL;
  455. }
  456. DBG_VOID_RETURN;
  457. }
  458. /* }}} */
  459. /* {{{ php_mysqlnd_create_read_buffer */
  460. static MYSQLND_READ_BUFFER *
  461. mysqlnd_create_read_buffer(size_t count)
  462. {
  463. MYSQLND_READ_BUFFER * ret = mnd_emalloc(sizeof(MYSQLND_READ_BUFFER));
  464. DBG_ENTER("mysqlnd_create_read_buffer");
  465. ret->is_empty = php_mysqlnd_read_buffer_is_empty;
  466. ret->read = php_mysqlnd_read_buffer_read;
  467. ret->bytes_left = php_mysqlnd_read_buffer_bytes_left;
  468. ret->free_buffer = php_mysqlnd_read_buffer_free;
  469. ret->data = mnd_emalloc(count);
  470. ret->size = ret->len = count;
  471. ret->offset = 0;
  472. DBG_RETURN(ret);
  473. }
  474. /* }}} */
  475. /* {{{ mysqlnd_net::read_compressed_packet_from_stream_and_fill_read_buffer */
  476. static enum_func_status
  477. MYSQLND_METHOD(mysqlnd_net, read_compressed_packet_from_stream_and_fill_read_buffer)
  478. (MYSQLND_NET * net, size_t net_payload_size, MYSQLND_STATS * conn_stats, MYSQLND_ERROR_INFO * error_info)
  479. {
  480. size_t decompressed_size;
  481. enum_func_status retval = PASS;
  482. zend_uchar * compressed_data = NULL;
  483. zend_uchar comp_header[COMPRESSED_HEADER_SIZE];
  484. DBG_ENTER("mysqlnd_net::read_compressed_packet_from_stream_and_fill_read_buffer");
  485. /* Read the compressed header */
  486. if (FAIL == net->data->m.network_read_ex(net, comp_header, COMPRESSED_HEADER_SIZE, conn_stats, error_info)) {
  487. DBG_RETURN(FAIL);
  488. }
  489. decompressed_size = uint3korr(comp_header);
  490. /* When decompressed_size is 0, then the data is not compressed, and we have wasted 3 bytes */
  491. /* we need to decompress the data */
  492. if (decompressed_size) {
  493. compressed_data = mnd_emalloc(net_payload_size);
  494. if (FAIL == net->data->m.network_read_ex(net, compressed_data, net_payload_size, conn_stats, error_info)) {
  495. retval = FAIL;
  496. goto end;
  497. }
  498. net->uncompressed_data = mysqlnd_create_read_buffer(decompressed_size);
  499. retval = net->data->m.decode(net->uncompressed_data->data, decompressed_size, compressed_data, net_payload_size);
  500. if (FAIL == retval) {
  501. goto end;
  502. }
  503. } else {
  504. DBG_INF_FMT("The server decided not to compress the data. Our job is easy. Copying %u bytes", net_payload_size);
  505. net->uncompressed_data = mysqlnd_create_read_buffer(net_payload_size);
  506. if (FAIL == net->data->m.network_read_ex(net, net->uncompressed_data->data, net_payload_size, conn_stats, error_info)) {
  507. retval = FAIL;
  508. goto end;
  509. }
  510. }
  511. end:
  512. if (compressed_data) {
  513. mnd_efree(compressed_data);
  514. }
  515. DBG_RETURN(retval);
  516. }
  517. /* }}} */
  518. #endif /* MYSQLND_COMPRESSION_ENABLED */
  519. /* {{{ mysqlnd_net::decode */
  520. static enum_func_status
  521. MYSQLND_METHOD(mysqlnd_net, decode)(zend_uchar * uncompressed_data, const size_t uncompressed_data_len,
  522. const zend_uchar * const compressed_data, const size_t compressed_data_len)
  523. {
  524. #ifdef MYSQLND_COMPRESSION_ENABLED
  525. int error;
  526. uLongf tmp_complen = uncompressed_data_len;
  527. DBG_ENTER("mysqlnd_net::decode");
  528. error = uncompress(uncompressed_data, &tmp_complen, compressed_data, compressed_data_len);
  529. DBG_INF_FMT("compressed data: decomp_len=%lu compressed_size="MYSQLND_SZ_T_SPEC, tmp_complen, compressed_data_len);
  530. if (error != Z_OK) {
  531. DBG_INF_FMT("decompression NOT successful. error=%d Z_OK=%d Z_BUF_ERROR=%d Z_MEM_ERROR=%d", error, Z_OK, Z_BUF_ERROR, Z_MEM_ERROR);
  532. }
  533. DBG_RETURN(error == Z_OK? PASS:FAIL);
  534. #else
  535. DBG_ENTER("mysqlnd_net::decode");
  536. DBG_RETURN(FAIL);
  537. #endif
  538. }
  539. /* }}} */
  540. /* {{{ mysqlnd_net::encode */
  541. static enum_func_status
  542. MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len,
  543. const zend_uchar * const uncompressed_data, const size_t uncompressed_data_len)
  544. {
  545. #ifdef MYSQLND_COMPRESSION_ENABLED
  546. int error;
  547. uLongf tmp_complen = *compress_buffer_len;
  548. DBG_ENTER("mysqlnd_net::encode");
  549. error = compress(compress_buffer, &tmp_complen, uncompressed_data, uncompressed_data_len);
  550. if (error != Z_OK) {
  551. DBG_INF_FMT("compression NOT successful. error=%d Z_OK=%d Z_BUF_ERROR=%d Z_MEM_ERROR=%d", error, Z_OK, Z_BUF_ERROR, Z_MEM_ERROR);
  552. } else {
  553. *compress_buffer_len = tmp_complen;
  554. DBG_INF_FMT("compression successful. compressed size=%lu", tmp_complen);
  555. }
  556. DBG_RETURN(error == Z_OK? PASS:FAIL);
  557. #else
  558. DBG_ENTER("mysqlnd_net::encode");
  559. DBG_RETURN(FAIL);
  560. #endif
  561. }
  562. /* }}} */
  563. /* {{{ mysqlnd_net::receive_ex */
  564. static enum_func_status
  565. MYSQLND_METHOD(mysqlnd_net, receive_ex)(MYSQLND_NET * const net, zend_uchar * const buffer, const size_t count,
  566. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  567. {
  568. size_t to_read = count;
  569. zend_uchar * p = buffer;
  570. DBG_ENTER("mysqlnd_net::receive_ex");
  571. #ifdef MYSQLND_COMPRESSION_ENABLED
  572. if (net->data->compressed) {
  573. if (net->uncompressed_data) {
  574. size_t to_read_from_buffer = MIN(net->uncompressed_data->bytes_left(net->uncompressed_data), to_read);
  575. DBG_INF_FMT("reading "MYSQLND_SZ_T_SPEC" from uncompressed_data buffer", to_read_from_buffer);
  576. if (to_read_from_buffer) {
  577. net->uncompressed_data->read(net->uncompressed_data, to_read_from_buffer, (zend_uchar *) p);
  578. p += to_read_from_buffer;
  579. to_read -= to_read_from_buffer;
  580. }
  581. DBG_INF_FMT("left "MYSQLND_SZ_T_SPEC" to read", to_read);
  582. if (TRUE == net->uncompressed_data->is_empty(net->uncompressed_data)) {
  583. /* Everything was consumed. This should never happen here, but for security */
  584. net->uncompressed_data->free_buffer(&net->uncompressed_data);
  585. }
  586. }
  587. if (to_read) {
  588. zend_uchar net_header[MYSQLND_HEADER_SIZE];
  589. size_t net_payload_size;
  590. zend_uchar packet_no;
  591. if (FAIL == net->data->m.network_read_ex(net, net_header, MYSQLND_HEADER_SIZE, conn_stats, error_info)) {
  592. DBG_RETURN(FAIL);
  593. }
  594. net_payload_size = uint3korr(net_header);
  595. packet_no = uint1korr(net_header + 3);
  596. if (net->compressed_envelope_packet_no != packet_no) {
  597. DBG_ERR_FMT("Transport level: packets out of order. Expected %u received %u. Packet size="MYSQLND_SZ_T_SPEC,
  598. net->compressed_envelope_packet_no, packet_no, net_payload_size);
  599. php_error(E_WARNING, "Packets out of order. Expected %u received %u. Packet size="MYSQLND_SZ_T_SPEC,
  600. net->compressed_envelope_packet_no, packet_no, net_payload_size);
  601. DBG_RETURN(FAIL);
  602. }
  603. net->compressed_envelope_packet_no++;
  604. #ifdef MYSQLND_DUMP_HEADER_N_BODY
  605. DBG_INF_FMT("HEADER: hwd_packet_no=%u size=%3u", packet_no, (zend_ulong) net_payload_size);
  606. #endif
  607. /* Now let's read from the wire, decompress it and fill the read buffer */
  608. net->data->m.read_compressed_packet_from_stream_and_fill_read_buffer(net, net_payload_size, conn_stats, error_info);
  609. /*
  610. Now a bit of recursion - read from the read buffer,
  611. if the data which we have just read from the wire
  612. is not enough, then the recursive call will try to
  613. satisfy it until it is satisfied.
  614. */
  615. DBG_RETURN(net->data->m.receive_ex(net, p, to_read, conn_stats, error_info));
  616. }
  617. DBG_RETURN(PASS);
  618. }
  619. #endif /* MYSQLND_COMPRESSION_ENABLED */
  620. DBG_RETURN(net->data->m.network_read_ex(net, p, to_read, conn_stats, error_info));
  621. }
  622. /* }}} */
  623. /* {{{ mysqlnd_net::set_client_option */
  624. static enum_func_status
  625. MYSQLND_METHOD(mysqlnd_net, set_client_option)(MYSQLND_NET * const net, enum mysqlnd_option option, const char * const value)
  626. {
  627. DBG_ENTER("mysqlnd_net::set_client_option");
  628. DBG_INF_FMT("option=%u", option);
  629. switch (option) {
  630. case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
  631. DBG_INF("MYSQLND_OPT_NET_CMD_BUFFER_SIZE");
  632. if (*(unsigned int*) value < MYSQLND_NET_CMD_BUFFER_MIN_SIZE) {
  633. DBG_RETURN(FAIL);
  634. }
  635. net->cmd_buffer.length = *(unsigned int*) value;
  636. DBG_INF_FMT("new_length="MYSQLND_SZ_T_SPEC, net->cmd_buffer.length);
  637. if (!net->cmd_buffer.buffer) {
  638. net->cmd_buffer.buffer = mnd_pemalloc(net->cmd_buffer.length, net->persistent);
  639. } else {
  640. net->cmd_buffer.buffer = mnd_perealloc(net->cmd_buffer.buffer, net->cmd_buffer.length, net->persistent);
  641. }
  642. break;
  643. case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
  644. DBG_INF("MYSQLND_OPT_NET_READ_BUFFER_SIZE");
  645. net->data->options.net_read_buffer_size = *(unsigned int*) value;
  646. DBG_INF_FMT("new_length="MYSQLND_SZ_T_SPEC, net->data->options.net_read_buffer_size);
  647. break;
  648. case MYSQL_OPT_CONNECT_TIMEOUT:
  649. DBG_INF("MYSQL_OPT_CONNECT_TIMEOUT");
  650. net->data->options.timeout_connect = *(unsigned int*) value;
  651. break;
  652. case MYSQLND_OPT_SSL_KEY:
  653. {
  654. zend_bool pers = net->persistent;
  655. if (net->data->options.ssl_key) {
  656. mnd_pefree(net->data->options.ssl_key, pers);
  657. }
  658. net->data->options.ssl_key = value? mnd_pestrdup(value, pers) : NULL;
  659. break;
  660. }
  661. case MYSQLND_OPT_SSL_CERT:
  662. {
  663. zend_bool pers = net->persistent;
  664. if (net->data->options.ssl_cert) {
  665. mnd_pefree(net->data->options.ssl_cert, pers);
  666. }
  667. net->data->options.ssl_cert = value? mnd_pestrdup(value, pers) : NULL;
  668. break;
  669. }
  670. case MYSQLND_OPT_SSL_CA:
  671. {
  672. zend_bool pers = net->persistent;
  673. if (net->data->options.ssl_ca) {
  674. mnd_pefree(net->data->options.ssl_ca, pers);
  675. }
  676. net->data->options.ssl_ca = value? mnd_pestrdup(value, pers) : NULL;
  677. break;
  678. }
  679. case MYSQLND_OPT_SSL_CAPATH:
  680. {
  681. zend_bool pers = net->persistent;
  682. if (net->data->options.ssl_capath) {
  683. mnd_pefree(net->data->options.ssl_capath, pers);
  684. }
  685. net->data->options.ssl_capath = value? mnd_pestrdup(value, pers) : NULL;
  686. break;
  687. }
  688. case MYSQLND_OPT_SSL_CIPHER:
  689. {
  690. zend_bool pers = net->persistent;
  691. if (net->data->options.ssl_cipher) {
  692. mnd_pefree(net->data->options.ssl_cipher, pers);
  693. }
  694. net->data->options.ssl_cipher = value? mnd_pestrdup(value, pers) : NULL;
  695. break;
  696. }
  697. case MYSQLND_OPT_SSL_PASSPHRASE:
  698. {
  699. zend_bool pers = net->persistent;
  700. if (net->data->options.ssl_passphrase) {
  701. mnd_pefree(net->data->options.ssl_passphrase, pers);
  702. }
  703. net->data->options.ssl_passphrase = value? mnd_pestrdup(value, pers) : NULL;
  704. break;
  705. }
  706. case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
  707. {
  708. enum mysqlnd_ssl_peer val = *((enum mysqlnd_ssl_peer *)value);
  709. switch (val) {
  710. case MYSQLND_SSL_PEER_VERIFY:
  711. DBG_INF("MYSQLND_SSL_PEER_VERIFY");
  712. break;
  713. case MYSQLND_SSL_PEER_DONT_VERIFY:
  714. DBG_INF("MYSQLND_SSL_PEER_DONT_VERIFY");
  715. break;
  716. case MYSQLND_SSL_PEER_DEFAULT:
  717. DBG_INF("MYSQLND_SSL_PEER_DEFAULT");
  718. val = MYSQLND_SSL_PEER_DEFAULT;
  719. break;
  720. default:
  721. DBG_INF("default = MYSQLND_SSL_PEER_DEFAULT_ACTION");
  722. val = MYSQLND_SSL_PEER_DEFAULT;
  723. break;
  724. }
  725. net->data->options.ssl_verify_peer = val;
  726. break;
  727. }
  728. case MYSQL_OPT_READ_TIMEOUT:
  729. net->data->options.timeout_read = *(unsigned int*) value;
  730. break;
  731. #ifdef WHEN_SUPPORTED_BY_MYSQLI
  732. case MYSQL_OPT_WRITE_TIMEOUT:
  733. net->data->options.timeout_write = *(unsigned int*) value;
  734. break;
  735. #endif
  736. case MYSQL_OPT_COMPRESS:
  737. net->data->options.flags |= MYSQLND_NET_FLAG_USE_COMPRESSION;
  738. break;
  739. case MYSQL_SERVER_PUBLIC_KEY:
  740. {
  741. zend_bool pers = net->persistent;
  742. if (net->data->options.sha256_server_public_key) {
  743. mnd_pefree(net->data->options.sha256_server_public_key, pers);
  744. }
  745. net->data->options.sha256_server_public_key = value? mnd_pestrdup(value, pers) : NULL;
  746. break;
  747. }
  748. default:
  749. DBG_RETURN(FAIL);
  750. }
  751. DBG_RETURN(PASS);
  752. }
  753. /* }}} */
  754. /* {{{ mysqlnd_net::consume_uneaten_data */
  755. size_t
  756. MYSQLND_METHOD(mysqlnd_net, consume_uneaten_data)(MYSQLND_NET * const net, enum php_mysqlnd_server_command cmd)
  757. {
  758. #ifdef MYSQLND_DO_WIRE_CHECK_BEFORE_COMMAND
  759. /*
  760. Switch to non-blocking mode and try to consume something from
  761. the line, if possible, then continue. This saves us from looking for
  762. the actual place where out-of-order packets have been sent.
  763. If someone is completely sure that everything is fine, he can switch it
  764. off.
  765. */
  766. char tmp_buf[256];
  767. size_t skipped_bytes = 0;
  768. int opt = PHP_STREAM_OPTION_BLOCKING;
  769. php_stream * net_stream = net->data->get_stream(net);
  770. int was_blocked = net_stream->ops->set_option(net_stream, opt, 0, NULL);
  771. DBG_ENTER("mysqlnd_net::consume_uneaten_data");
  772. if (PHP_STREAM_OPTION_RETURN_ERR != was_blocked) {
  773. /* Do a read of 1 byte */
  774. int bytes_consumed;
  775. do {
  776. skipped_bytes += (bytes_consumed = php_stream_read(net_stream, tmp_buf, sizeof(tmp_buf)));
  777. } while (bytes_consumed == sizeof(tmp_buf));
  778. if (was_blocked) {
  779. net_stream->ops->set_option(net_stream, opt, 1, NULL);
  780. }
  781. if (bytes_consumed) {
  782. DBG_ERR_FMT("Skipped %u bytes. Last command %s hasn't consumed all the output from the server",
  783. bytes_consumed, mysqlnd_command_to_text[net->last_command]);
  784. php_error_docref(NULL, E_WARNING, "Skipped %u bytes. Last command %s hasn't "
  785. "consumed all the output from the server",
  786. bytes_consumed, mysqlnd_command_to_text[net->last_command]);
  787. }
  788. }
  789. net->last_command = cmd;
  790. DBG_RETURN(skipped_bytes);
  791. #else
  792. return 0;
  793. #endif
  794. }
  795. /* }}} */
  796. /*
  797. in libmyusql, if cert and !key then key=cert
  798. */
  799. /* {{{ mysqlnd_net::enable_ssl */
  800. static enum_func_status
  801. MYSQLND_METHOD(mysqlnd_net, enable_ssl)(MYSQLND_NET * const net)
  802. {
  803. #ifdef MYSQLND_SSL_SUPPORTED
  804. php_stream_context * context = php_stream_context_alloc();
  805. php_stream * net_stream = net->data->m.get_stream(net);
  806. zend_bool any_flag = FALSE;
  807. DBG_ENTER("mysqlnd_net::enable_ssl");
  808. if (!context) {
  809. DBG_RETURN(FAIL);
  810. }
  811. if (net->data->options.ssl_key) {
  812. zval key_zval;
  813. ZVAL_STRING(&key_zval, net->data->options.ssl_key);
  814. php_stream_context_set_option(context, "ssl", "local_pk", &key_zval);
  815. zval_ptr_dtor(&key_zval);
  816. any_flag = TRUE;
  817. }
  818. if (net->data->options.ssl_cert) {
  819. zval cert_zval;
  820. ZVAL_STRING(&cert_zval, net->data->options.ssl_cert);
  821. php_stream_context_set_option(context, "ssl", "local_cert", &cert_zval);
  822. if (!net->data->options.ssl_key) {
  823. php_stream_context_set_option(context, "ssl", "local_pk", &cert_zval);
  824. }
  825. zval_ptr_dtor(&cert_zval);
  826. any_flag = TRUE;
  827. }
  828. if (net->data->options.ssl_ca) {
  829. zval cafile_zval;
  830. ZVAL_STRING(&cafile_zval, net->data->options.ssl_ca);
  831. php_stream_context_set_option(context, "ssl", "cafile", &cafile_zval);
  832. any_flag = TRUE;
  833. }
  834. if (net->data->options.ssl_capath) {
  835. zval capath_zval;
  836. ZVAL_STRING(&capath_zval, net->data->options.ssl_capath);
  837. php_stream_context_set_option(context, "ssl", "capath", &capath_zval);
  838. zval_ptr_dtor(&capath_zval);
  839. any_flag = TRUE;
  840. }
  841. if (net->data->options.ssl_passphrase) {
  842. zval passphrase_zval;
  843. ZVAL_STRING(&passphrase_zval, net->data->options.ssl_passphrase);
  844. php_stream_context_set_option(context, "ssl", "passphrase", &passphrase_zval);
  845. zval_ptr_dtor(&passphrase_zval);
  846. any_flag = TRUE;
  847. }
  848. if (net->data->options.ssl_cipher) {
  849. zval cipher_zval;
  850. ZVAL_STRING(&cipher_zval, net->data->options.ssl_cipher);
  851. php_stream_context_set_option(context, "ssl", "ciphers", &cipher_zval);
  852. zval_ptr_dtor(&cipher_zval);
  853. any_flag = TRUE;
  854. }
  855. {
  856. zval verify_peer_zval;
  857. zend_bool verify;
  858. if (net->data->options.ssl_verify_peer == MYSQLND_SSL_PEER_DEFAULT) {
  859. net->data->options.ssl_verify_peer = any_flag? MYSQLND_SSL_PEER_DEFAULT_ACTION:MYSQLND_SSL_PEER_DONT_VERIFY;
  860. }
  861. verify = net->data->options.ssl_verify_peer == MYSQLND_SSL_PEER_VERIFY? TRUE:FALSE;
  862. DBG_INF_FMT("VERIFY=%d", verify);
  863. ZVAL_BOOL(&verify_peer_zval, verify);
  864. php_stream_context_set_option(context, "ssl", "verify_peer", &verify_peer_zval);
  865. php_stream_context_set_option(context, "ssl", "verify_peer_name", &verify_peer_zval);
  866. if (net->data->options.ssl_verify_peer == MYSQLND_SSL_PEER_DONT_VERIFY) {
  867. ZVAL_TRUE(&verify_peer_zval);
  868. php_stream_context_set_option(context, "ssl", "allow_self_signed", &verify_peer_zval);
  869. }
  870. }
  871. php_stream_context_set(net_stream, context);
  872. if (php_stream_xport_crypto_setup(net_stream, STREAM_CRYPTO_METHOD_TLS_CLIENT, NULL) < 0 ||
  873. php_stream_xport_crypto_enable(net_stream, 1) < 0)
  874. {
  875. DBG_ERR("Cannot connect to MySQL by using SSL");
  876. php_error_docref(NULL, E_WARNING, "Cannot connect to MySQL by using SSL");
  877. DBG_RETURN(FAIL);
  878. }
  879. net->data->ssl = TRUE;
  880. /*
  881. get rid of the context. we are persistent and if this is a real pconn used by mysql/mysqli,
  882. then the context would not survive cleaning of EG(regular_list), where it is registered, as a
  883. resource. What happens is that after this destruction any use of the network will mean usage
  884. of the context, which means usage of already freed memory, bad. Actually we don't need this
  885. context anymore after we have enabled SSL on the connection. Thus it is very simple, we remove it.
  886. */
  887. php_stream_context_set(net_stream, NULL);
  888. if (net->data->options.timeout_read) {
  889. struct timeval tv;
  890. DBG_INF_FMT("setting %u as PHP_STREAM_OPTION_READ_TIMEOUT", net->data->options.timeout_read);
  891. tv.tv_sec = net->data->options.timeout_read;
  892. tv.tv_usec = 0;
  893. php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
  894. }
  895. DBG_RETURN(PASS);
  896. #else
  897. DBG_ENTER("mysqlnd_net::enable_ssl");
  898. DBG_INF("MYSQLND_SSL_SUPPORTED is not defined");
  899. DBG_RETURN(PASS);
  900. #endif
  901. }
  902. /* }}} */
  903. /* {{{ mysqlnd_net::disable_ssl */
  904. static enum_func_status
  905. MYSQLND_METHOD(mysqlnd_net, disable_ssl)(MYSQLND_NET * const net)
  906. {
  907. DBG_ENTER("mysqlnd_net::disable_ssl");
  908. DBG_RETURN(PASS);
  909. }
  910. /* }}} */
  911. /* {{{ mysqlnd_net::free_contents */
  912. static void
  913. MYSQLND_METHOD(mysqlnd_net, free_contents)(MYSQLND_NET * net)
  914. {
  915. zend_bool pers = net->persistent;
  916. DBG_ENTER("mysqlnd_net::free_contents");
  917. #ifdef MYSQLND_COMPRESSION_ENABLED
  918. if (net->uncompressed_data) {
  919. net->uncompressed_data->free_buffer(&net->uncompressed_data);
  920. }
  921. #endif
  922. if (net->data->options.ssl_key) {
  923. mnd_pefree(net->data->options.ssl_key, pers);
  924. net->data->options.ssl_key = NULL;
  925. }
  926. if (net->data->options.ssl_cert) {
  927. mnd_pefree(net->data->options.ssl_cert, pers);
  928. net->data->options.ssl_cert = NULL;
  929. }
  930. if (net->data->options.ssl_ca) {
  931. mnd_pefree(net->data->options.ssl_ca, pers);
  932. net->data->options.ssl_ca = NULL;
  933. }
  934. if (net->data->options.ssl_capath) {
  935. mnd_pefree(net->data->options.ssl_capath, pers);
  936. net->data->options.ssl_capath = NULL;
  937. }
  938. if (net->data->options.ssl_cipher) {
  939. mnd_pefree(net->data->options.ssl_cipher, pers);
  940. net->data->options.ssl_cipher = NULL;
  941. }
  942. if (net->data->options.sha256_server_public_key) {
  943. mnd_pefree(net->data->options.sha256_server_public_key, pers);
  944. net->data->options.sha256_server_public_key = NULL;
  945. }
  946. DBG_VOID_RETURN;
  947. }
  948. /* }}} */
  949. /* {{{ mysqlnd_net::close_stream */
  950. static void
  951. MYSQLND_METHOD(mysqlnd_net, close_stream)(MYSQLND_NET * const net, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  952. {
  953. php_stream * net_stream;
  954. DBG_ENTER("mysqlnd_net::close_stream");
  955. if (net && (net_stream = net->data->m.get_stream(net))) {
  956. zend_bool pers = net->persistent;
  957. DBG_INF_FMT("Freeing stream. abstract=%p", net_stream->abstract);
  958. if (pers) {
  959. if (EG(active)) {
  960. php_stream_free(net_stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
  961. } else {
  962. /*
  963. otherwise we will crash because the EG(persistent_list) has been freed already,
  964. before the modules are shut down
  965. */
  966. php_stream_free(net_stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
  967. }
  968. } else {
  969. php_stream_free(net_stream, PHP_STREAM_FREE_CLOSE);
  970. }
  971. (void) net->data->m.set_stream(net, NULL);
  972. }
  973. DBG_VOID_RETURN;
  974. }
  975. /* }}} */
  976. /* {{{ mysqlnd_net::init */
  977. static enum_func_status
  978. MYSQLND_METHOD(mysqlnd_net, init)(MYSQLND_NET * const net, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  979. {
  980. unsigned int buf_size;
  981. DBG_ENTER("mysqlnd_net::init");
  982. buf_size = MYSQLND_G(net_cmd_buffer_size); /* this is long, cast to unsigned int*/
  983. net->data->m.set_client_option(net, MYSQLND_OPT_NET_CMD_BUFFER_SIZE, (char *) &buf_size);
  984. buf_size = MYSQLND_G(net_read_buffer_size); /* this is long, cast to unsigned int*/
  985. net->data->m.set_client_option(net, MYSQLND_OPT_NET_READ_BUFFER_SIZE, (char *)&buf_size);
  986. buf_size = MYSQLND_G(net_read_timeout); /* this is long, cast to unsigned int*/
  987. net->data->m.set_client_option(net, MYSQL_OPT_READ_TIMEOUT, (char *)&buf_size);
  988. DBG_RETURN(PASS);
  989. }
  990. /* }}} */
  991. /* {{{ mysqlnd_net::dtor */
  992. static void
  993. MYSQLND_METHOD(mysqlnd_net, dtor)(MYSQLND_NET * const net, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  994. {
  995. DBG_ENTER("mysqlnd_net::dtor");
  996. if (net) {
  997. net->data->m.free_contents(net);
  998. net->data->m.close_stream(net, stats, error_info);
  999. if (net->cmd_buffer.buffer) {
  1000. DBG_INF("Freeing cmd buffer");
  1001. mnd_pefree(net->cmd_buffer.buffer, net->persistent);
  1002. net->cmd_buffer.buffer = NULL;
  1003. }
  1004. mnd_pefree(net->data, net->data->persistent);
  1005. mnd_pefree(net, net->persistent);
  1006. }
  1007. DBG_VOID_RETURN;
  1008. }
  1009. /* }}} */
  1010. /* {{{ mysqlnd_net::get_stream */
  1011. static php_stream *
  1012. MYSQLND_METHOD(mysqlnd_net, get_stream)(const MYSQLND_NET * const net)
  1013. {
  1014. DBG_ENTER("mysqlnd_net::get_stream");
  1015. DBG_INF_FMT("%p", net? net->data->stream:NULL);
  1016. DBG_RETURN(net? net->data->stream:NULL);
  1017. }
  1018. /* }}} */
  1019. /* {{{ mysqlnd_net::set_stream */
  1020. static php_stream *
  1021. MYSQLND_METHOD(mysqlnd_net, set_stream)(MYSQLND_NET * const net, php_stream * net_stream)
  1022. {
  1023. php_stream * ret = NULL;
  1024. DBG_ENTER("mysqlnd_net::set_stream");
  1025. if (net) {
  1026. net->data->stream = net_stream;
  1027. ret = net->data->stream;
  1028. }
  1029. DBG_RETURN(ret);
  1030. }
  1031. /* }}} */
  1032. MYSQLND_CLASS_METHODS_START(mysqlnd_net)
  1033. MYSQLND_METHOD(mysqlnd_net, init),
  1034. MYSQLND_METHOD(mysqlnd_net, dtor),
  1035. MYSQLND_METHOD(mysqlnd_net, connect_ex),
  1036. MYSQLND_METHOD(mysqlnd_net, close_stream),
  1037. MYSQLND_METHOD(mysqlnd_net, open_pipe),
  1038. MYSQLND_METHOD(mysqlnd_net, open_tcp_or_unix),
  1039. MYSQLND_METHOD(mysqlnd_net, get_stream),
  1040. MYSQLND_METHOD(mysqlnd_net, set_stream),
  1041. MYSQLND_METHOD(mysqlnd_net, get_open_stream),
  1042. MYSQLND_METHOD(mysqlnd_net, post_connect_set_opt),
  1043. MYSQLND_METHOD(mysqlnd_net, set_client_option),
  1044. MYSQLND_METHOD(mysqlnd_net, decode),
  1045. MYSQLND_METHOD(mysqlnd_net, encode),
  1046. MYSQLND_METHOD(mysqlnd_net, consume_uneaten_data),
  1047. MYSQLND_METHOD(mysqlnd_net, free_contents),
  1048. MYSQLND_METHOD(mysqlnd_net, enable_ssl),
  1049. MYSQLND_METHOD(mysqlnd_net, disable_ssl),
  1050. MYSQLND_METHOD(mysqlnd_net, network_read_ex),
  1051. MYSQLND_METHOD(mysqlnd_net, network_write_ex),
  1052. MYSQLND_METHOD(mysqlnd_net, send_ex),
  1053. MYSQLND_METHOD(mysqlnd_net, receive_ex),
  1054. #ifdef MYSQLND_COMPRESSION_ENABLED
  1055. MYSQLND_METHOD(mysqlnd_net, read_compressed_packet_from_stream_and_fill_read_buffer),
  1056. #else
  1057. NULL,
  1058. #endif
  1059. NULL, /* unused 1 */
  1060. NULL, /* unused 2 */
  1061. NULL, /* unused 3 */
  1062. NULL, /* unused 4 */
  1063. NULL /* unused 5 */
  1064. MYSQLND_CLASS_METHODS_END;
  1065. /* {{{ mysqlnd_net_init */
  1066. PHPAPI MYSQLND_NET *
  1067. mysqlnd_net_init(zend_bool persistent, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
  1068. {
  1069. MYSQLND_NET * net;
  1070. DBG_ENTER("mysqlnd_net_init");
  1071. net = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).get_io_channel(persistent, stats, error_info);
  1072. DBG_RETURN(net);
  1073. }
  1074. /* }}} */
  1075. /* {{{ mysqlnd_net_free */
  1076. PHPAPI void
  1077. mysqlnd_net_free(MYSQLND_NET * const net, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
  1078. {
  1079. DBG_ENTER("mysqlnd_net_free");
  1080. if (net) {
  1081. net->data->m.dtor(net, stats, error_info);
  1082. }
  1083. DBG_VOID_RETURN;
  1084. }
  1085. /* }}} */
  1086. /*
  1087. * Local variables:
  1088. * tab-width: 4
  1089. * c-basic-offset: 4
  1090. * End:
  1091. * vim600: noet sw=4 ts=4 fdm=marker
  1092. * vim<600: noet sw=4 ts=4
  1093. */