PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/mysqlnd/mysqlnd_net.c

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