mysqlnd_protocol_frame_codec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Andrey Hristov <andrey@php.net> |
  14. | Ulf Wendel <uw@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php.h"
  18. #include "mysqlnd.h"
  19. #include "mysqlnd_connection.h"
  20. #include "mysqlnd_priv.h"
  21. #include "mysqlnd_read_buffer.h"
  22. #include "mysqlnd_wireprotocol.h"
  23. #include "mysqlnd_statistics.h"
  24. #include "mysqlnd_debug.h"
  25. #ifdef MYSQLND_COMPRESSION_ENABLED
  26. #include <zlib.h>
  27. #endif
  28. /* {{{ mysqlnd_pfc::reset */
  29. static enum_func_status
  30. MYSQLND_METHOD(mysqlnd_pfc, reset)(MYSQLND_PFC * const pfc, MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  31. {
  32. DBG_ENTER("mysqlnd_pfc::reset");
  33. pfc->data->packet_no = pfc->data->compressed_envelope_packet_no = 0;
  34. DBG_RETURN(PASS);
  35. }
  36. /* }}} */
  37. /* We assume that MYSQLND_HEADER_SIZE is 4 bytes !! */
  38. #define COPY_HEADER(T,A) do { \
  39. *(((char *)(T))) = *(((char *)(A)));\
  40. *(((char *)(T))+1) = *(((char *)(A))+1);\
  41. *(((char *)(T))+2) = *(((char *)(A))+2);\
  42. *(((char *)(T))+3) = *(((char *)(A))+3); } while (0)
  43. #define STORE_HEADER_SIZE(safe_storage, buffer) COPY_HEADER((safe_storage), (buffer))
  44. #define RESTORE_HEADER_SIZE(buffer, safe_storage) STORE_HEADER_SIZE((safe_storage), (buffer))
  45. #ifdef MYSQLND_COMPRESSION_ENABLED
  46. static ssize_t write_compressed_packet(
  47. const MYSQLND_PFC *pfc, MYSQLND_VIO *vio,
  48. MYSQLND_STATS *conn_stats, MYSQLND_ERROR_INFO *error_info,
  49. zend_uchar *uncompressed_payload, size_t to_be_sent, zend_uchar *compress_buf) {
  50. DBG_ENTER("write_compressed_packet");
  51. /* here we need to compress the data and then write it, first comes the compressed header */
  52. size_t tmp_complen = to_be_sent;
  53. size_t payload_size;
  54. if (PASS == pfc->data->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
  55. uncompressed_payload, to_be_sent))
  56. {
  57. int3store(compress_buf + MYSQLND_HEADER_SIZE, to_be_sent);
  58. payload_size = tmp_complen;
  59. } else {
  60. int3store(compress_buf + MYSQLND_HEADER_SIZE, 0);
  61. memcpy(compress_buf + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, uncompressed_payload, to_be_sent);
  62. payload_size = to_be_sent;
  63. }
  64. int3store(compress_buf, payload_size);
  65. int1store(compress_buf + 3, pfc->data->compressed_envelope_packet_no);
  66. DBG_INF_FMT("writing %zu bytes to the network", payload_size + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE);
  67. ssize_t bytes_sent = vio->data->m.network_write(vio, compress_buf, payload_size + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, conn_stats, error_info);
  68. pfc->data->compressed_envelope_packet_no++;
  69. #ifdef WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY
  70. if (res == Z_OK) {
  71. size_t decompressed_size = left + MYSQLND_HEADER_SIZE;
  72. zend_uchar * decompressed_data = mnd_emalloc(decompressed_size);
  73. int error = pfc->data->m.decode(decompressed_data, decompressed_size,
  74. compress_buf + MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE, payload_size);
  75. if (error == Z_OK) {
  76. int i;
  77. DBG_INF("success decompressing");
  78. for (i = 0 ; i < decompressed_size; i++) {
  79. if (i && (i % 30 == 0)) {
  80. printf("\n\t\t");
  81. }
  82. printf("%.2X ", (int)*((char*)&(decompressed_data[i])));
  83. DBG_INF_FMT("%.2X ", (int)*((char*)&(decompressed_data[i])));
  84. }
  85. } else {
  86. DBG_INF("error decompressing");
  87. }
  88. mnd_efree(decompressed_data);
  89. }
  90. #endif /* WHEN_WE_NEED_TO_CHECK_WHETHER_COMPRESSION_WORKS_CORRECTLY */
  91. DBG_RETURN(bytes_sent);
  92. }
  93. #endif
  94. /* {{{ mysqlnd_pfc::send */
  95. /*
  96. IMPORTANT : It's expected that buffer has place in the beginning for MYSQLND_HEADER_SIZE !!!!
  97. This is done for performance reasons in the caller of this function.
  98. Otherwise we will have to do send two TCP packets, or do new alloc and memcpy.
  99. Neither are quick, thus the clients of this function are obligated to do
  100. what they are asked for.
  101. `count` is actually the length of the payload data. Thus :
  102. count + MYSQLND_HEADER_SIZE = sizeof(buffer) (not the pointer but the actual buffer)
  103. */
  104. static size_t
  105. MYSQLND_METHOD(mysqlnd_pfc, send)(MYSQLND_PFC * const pfc, MYSQLND_VIO * const vio, zend_uchar * const buffer, const size_t count,
  106. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  107. {
  108. zend_uchar safe_buf[((MYSQLND_HEADER_SIZE) + (sizeof(zend_uchar)) - 1) / (sizeof(zend_uchar))];
  109. zend_uchar * safe_storage = safe_buf;
  110. size_t packets_sent = 1;
  111. size_t left = count;
  112. zend_uchar * p = (zend_uchar *) buffer;
  113. zend_uchar * compress_buf = NULL;
  114. size_t to_be_sent;
  115. ssize_t bytes_sent;
  116. DBG_ENTER("mysqlnd_pfc::send");
  117. DBG_INF_FMT("count=%zu compression=%u", count, pfc->data->compressed);
  118. if (pfc->data->compressed == TRUE) {
  119. size_t comp_buf_size = MYSQLND_HEADER_SIZE + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE + MIN(left, MYSQLND_MAX_PACKET_SIZE);
  120. DBG_INF_FMT("compress_buf_size=%zu", comp_buf_size);
  121. compress_buf = mnd_emalloc(comp_buf_size);
  122. }
  123. do {
  124. to_be_sent = MIN(left, MYSQLND_MAX_PACKET_SIZE);
  125. DBG_INF_FMT("to_be_sent=%zu", to_be_sent);
  126. DBG_INF_FMT("packets_sent=%zu", packets_sent);
  127. DBG_INF_FMT("compressed_envelope_packet_no=%u", pfc->data->compressed_envelope_packet_no);
  128. DBG_INF_FMT("packet_no=%u", pfc->data->packet_no);
  129. #ifdef MYSQLND_COMPRESSION_ENABLED
  130. if (pfc->data->compressed == TRUE) {
  131. zend_uchar * uncompressed_payload = p; /* should include the header */
  132. STORE_HEADER_SIZE(safe_storage, uncompressed_payload);
  133. int3store(uncompressed_payload, to_be_sent);
  134. int1store(uncompressed_payload + 3, pfc->data->packet_no);
  135. if (to_be_sent <= MYSQLND_MAX_PACKET_SIZE - MYSQLND_HEADER_SIZE) {
  136. bytes_sent = write_compressed_packet(
  137. pfc, vio, conn_stats, error_info,
  138. uncompressed_payload, to_be_sent + MYSQLND_HEADER_SIZE, compress_buf);
  139. } else {
  140. /* The uncompressed size including the header would overflow. Split into two
  141. * compressed packets. The size of the first one is relatively arbitrary here. */
  142. const size_t split_off_bytes = 8192;
  143. bytes_sent = write_compressed_packet(
  144. pfc, vio, conn_stats, error_info,
  145. uncompressed_payload, split_off_bytes, compress_buf);
  146. bytes_sent = write_compressed_packet(
  147. pfc, vio, conn_stats, error_info,
  148. uncompressed_payload + split_off_bytes,
  149. to_be_sent + MYSQLND_HEADER_SIZE - split_off_bytes, compress_buf);
  150. }
  151. RESTORE_HEADER_SIZE(uncompressed_payload, safe_storage);
  152. } else
  153. #endif /* MYSQLND_COMPRESSION_ENABLED */
  154. {
  155. DBG_INF("no compression");
  156. STORE_HEADER_SIZE(safe_storage, p);
  157. int3store(p, to_be_sent);
  158. int1store(p + 3, pfc->data->packet_no);
  159. bytes_sent = vio->data->m.network_write(vio, p, to_be_sent + MYSQLND_HEADER_SIZE, conn_stats, error_info);
  160. RESTORE_HEADER_SIZE(p, safe_storage);
  161. pfc->data->compressed_envelope_packet_no++;
  162. }
  163. pfc->data->packet_no++;
  164. p += to_be_sent;
  165. left -= to_be_sent;
  166. packets_sent++;
  167. /*
  168. if left is 0 then there is nothing more to send, but if the last packet was exactly
  169. with the size MYSQLND_MAX_PACKET_SIZE we need to send additional packet, which has
  170. empty payload. Thus if left == 0 we check for to_be_sent being the max size. If it is
  171. indeed it then loop once more, then to_be_sent will become 0, left will stay 0. Empty
  172. packet will be sent and this loop will end.
  173. */
  174. } while (bytes_sent > 0 && (left > 0 || to_be_sent == MYSQLND_MAX_PACKET_SIZE));
  175. DBG_INF_FMT("packet_size=%zu packet_no=%u", left, pfc->data->packet_no);
  176. MYSQLND_INC_CONN_STATISTIC_W_VALUE3(conn_stats,
  177. STAT_BYTES_SENT, count + packets_sent * MYSQLND_HEADER_SIZE,
  178. STAT_PROTOCOL_OVERHEAD_OUT, packets_sent * MYSQLND_HEADER_SIZE,
  179. STAT_PACKETS_SENT, packets_sent);
  180. if (compress_buf) {
  181. mnd_efree(compress_buf);
  182. }
  183. /* Even for zero size payload we have to send a packet */
  184. if (bytes_sent <= 0) {
  185. DBG_ERR_FMT("Can't %zu send bytes", count);
  186. SET_CLIENT_ERROR(error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  187. }
  188. DBG_RETURN(bytes_sent);
  189. }
  190. /* }}} */
  191. #ifdef MYSQLND_COMPRESSION_ENABLED
  192. /* {{{ mysqlnd_pfc::read_compressed_packet_from_stream_and_fill_read_buffer */
  193. static enum_func_status
  194. MYSQLND_METHOD(mysqlnd_pfc, read_compressed_packet_from_stream_and_fill_read_buffer)
  195. (MYSQLND_PFC * pfc, MYSQLND_VIO * vio, size_t net_payload_size, MYSQLND_STATS * conn_stats, MYSQLND_ERROR_INFO * error_info)
  196. {
  197. size_t decompressed_size;
  198. enum_func_status retval = PASS;
  199. zend_uchar * compressed_data = NULL;
  200. zend_uchar comp_header[COMPRESSED_HEADER_SIZE];
  201. DBG_ENTER("mysqlnd_pfc::read_compressed_packet_from_stream_and_fill_read_buffer");
  202. /* Read the compressed header */
  203. if (FAIL == vio->data->m.network_read(vio, comp_header, COMPRESSED_HEADER_SIZE, conn_stats, error_info)) {
  204. DBG_RETURN(FAIL);
  205. }
  206. decompressed_size = uint3korr(comp_header);
  207. /* When decompressed_size is 0, then the data is not compressed, and we have wasted 3 bytes */
  208. /* we need to decompress the data */
  209. if (decompressed_size) {
  210. compressed_data = mnd_emalloc(net_payload_size);
  211. if (FAIL == vio->data->m.network_read(vio, compressed_data, net_payload_size, conn_stats, error_info)) {
  212. retval = FAIL;
  213. goto end;
  214. }
  215. pfc->data->uncompressed_data = mysqlnd_create_read_buffer(decompressed_size);
  216. retval = pfc->data->m.decode(pfc->data->uncompressed_data->data, decompressed_size, compressed_data, net_payload_size);
  217. if (FAIL == retval) {
  218. goto end;
  219. }
  220. } else {
  221. DBG_INF_FMT("The server decided not to compress the data. Our job is easy. Copying %zu bytes", net_payload_size);
  222. pfc->data->uncompressed_data = mysqlnd_create_read_buffer(net_payload_size);
  223. if (FAIL == vio->data->m.network_read(vio, pfc->data->uncompressed_data->data, net_payload_size, conn_stats, error_info)) {
  224. retval = FAIL;
  225. goto end;
  226. }
  227. }
  228. end:
  229. if (compressed_data) {
  230. mnd_efree(compressed_data);
  231. }
  232. DBG_RETURN(retval);
  233. }
  234. /* }}} */
  235. #endif /* MYSQLND_COMPRESSION_ENABLED */
  236. /* {{{ mysqlnd_pfc::decode */
  237. static enum_func_status
  238. MYSQLND_METHOD(mysqlnd_pfc, decode)(zend_uchar * uncompressed_data, const size_t uncompressed_data_len,
  239. const zend_uchar * const compressed_data, const size_t compressed_data_len)
  240. {
  241. #ifdef MYSQLND_COMPRESSION_ENABLED
  242. int error;
  243. uLongf tmp_complen = uncompressed_data_len;
  244. DBG_ENTER("mysqlnd_pfc::decode");
  245. error = uncompress(uncompressed_data, &tmp_complen, compressed_data, compressed_data_len);
  246. DBG_INF_FMT("compressed data: decomp_len=%lu compressed_size=%zu", tmp_complen, compressed_data_len);
  247. if (error != Z_OK) {
  248. 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);
  249. }
  250. DBG_RETURN(error == Z_OK? PASS:FAIL);
  251. #else
  252. DBG_ENTER("mysqlnd_pfc::decode");
  253. DBG_RETURN(FAIL);
  254. #endif
  255. }
  256. /* }}} */
  257. /* {{{ mysqlnd_pfc::encode */
  258. static enum_func_status
  259. MYSQLND_METHOD(mysqlnd_pfc, encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len,
  260. const zend_uchar * const uncompressed_data, const size_t uncompressed_data_len)
  261. {
  262. #ifdef MYSQLND_COMPRESSION_ENABLED
  263. int error;
  264. uLongf tmp_complen = *compress_buffer_len;
  265. DBG_ENTER("mysqlnd_pfc::encode");
  266. error = compress(compress_buffer, &tmp_complen, uncompressed_data, uncompressed_data_len);
  267. if (error != Z_OK) {
  268. 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);
  269. } else {
  270. *compress_buffer_len = tmp_complen;
  271. DBG_INF_FMT("compression successful. compressed size=%lu", tmp_complen);
  272. }
  273. DBG_RETURN(error == Z_OK? PASS:FAIL);
  274. #else
  275. DBG_ENTER("mysqlnd_pfc::encode");
  276. DBG_RETURN(FAIL);
  277. #endif
  278. }
  279. /* }}} */
  280. /* {{{ mysqlnd_pfc::receive */
  281. static enum_func_status
  282. MYSQLND_METHOD(mysqlnd_pfc, receive)(MYSQLND_PFC * const pfc, MYSQLND_VIO * const vio, zend_uchar * const buffer, const size_t count,
  283. MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info)
  284. {
  285. size_t to_read = count;
  286. zend_uchar * p = buffer;
  287. DBG_ENTER("mysqlnd_pfc::receive");
  288. #ifdef MYSQLND_COMPRESSION_ENABLED
  289. if (pfc->data->compressed) {
  290. if (pfc->data->uncompressed_data) {
  291. size_t to_read_from_buffer = MIN(pfc->data->uncompressed_data->bytes_left(pfc->data->uncompressed_data), to_read);
  292. DBG_INF_FMT("reading %zu from uncompressed_data buffer", to_read_from_buffer);
  293. if (to_read_from_buffer) {
  294. pfc->data->uncompressed_data->read(pfc->data->uncompressed_data, to_read_from_buffer, (zend_uchar *) p);
  295. p += to_read_from_buffer;
  296. to_read -= to_read_from_buffer;
  297. }
  298. DBG_INF_FMT("left %zu to read", to_read);
  299. if (TRUE == pfc->data->uncompressed_data->is_empty(pfc->data->uncompressed_data)) {
  300. /* Everything was consumed. This should never happen here, but for security */
  301. pfc->data->uncompressed_data->free_buffer(&pfc->data->uncompressed_data);
  302. }
  303. }
  304. if (to_read) {
  305. zend_uchar net_header[MYSQLND_HEADER_SIZE];
  306. size_t net_payload_size;
  307. zend_uchar packet_no;
  308. if (FAIL == vio->data->m.network_read(vio, net_header, MYSQLND_HEADER_SIZE, conn_stats, error_info)) {
  309. DBG_RETURN(FAIL);
  310. }
  311. net_payload_size = uint3korr(net_header);
  312. packet_no = uint1korr(net_header + 3);
  313. if (pfc->data->compressed_envelope_packet_no != packet_no) {
  314. DBG_ERR_FMT("Transport level: packets out of order. Expected %u received %u. Packet size=%zu",
  315. pfc->data->compressed_envelope_packet_no, packet_no, net_payload_size);
  316. php_error(E_WARNING, "Packets out of order. Expected %u received %u. Packet size=%zu",
  317. pfc->data->compressed_envelope_packet_no, packet_no, net_payload_size);
  318. DBG_RETURN(FAIL);
  319. }
  320. pfc->data->compressed_envelope_packet_no++;
  321. #ifdef MYSQLND_DUMP_HEADER_N_BODY
  322. DBG_INF_FMT("HEADER: hwd_packet_no=%u size=%3u", packet_no, (zend_ulong) net_payload_size);
  323. #endif
  324. /* Now let's read from the wire, decompress it and fill the read buffer */
  325. pfc->data->m.read_compressed_packet_from_stream_and_fill_read_buffer(pfc, vio, net_payload_size, conn_stats, error_info);
  326. /*
  327. Now a bit of recursion - read from the read buffer,
  328. if the data which we have just read from the wire
  329. is not enough, then the recursive call will try to
  330. satisfy it until it is satisfied.
  331. */
  332. DBG_RETURN(pfc->data->m.receive(pfc, vio, p, to_read, conn_stats, error_info));
  333. }
  334. DBG_RETURN(PASS);
  335. }
  336. #endif /* MYSQLND_COMPRESSION_ENABLED */
  337. DBG_RETURN(vio->data->m.network_read(vio, p, to_read, conn_stats, error_info));
  338. }
  339. /* }}} */
  340. /* {{{ mysqlnd_pfc::set_client_option */
  341. static enum_func_status
  342. MYSQLND_METHOD(mysqlnd_pfc, set_client_option)(MYSQLND_PFC * const pfc, enum_mysqlnd_client_option option, const char * const value)
  343. {
  344. DBG_ENTER("mysqlnd_pfc::set_client_option");
  345. DBG_INF_FMT("option=%u", option);
  346. switch (option) {
  347. case MYSQL_OPT_COMPRESS:
  348. pfc->data->flags |= MYSQLND_PROTOCOL_FLAG_USE_COMPRESSION;
  349. break;
  350. case MYSQL_SERVER_PUBLIC_KEY: {
  351. const bool pers = pfc->persistent;
  352. if (pfc->data->sha256_server_public_key) {
  353. mnd_pefree(pfc->data->sha256_server_public_key, pers);
  354. }
  355. pfc->data->sha256_server_public_key = value? mnd_pestrdup(value, pers) : NULL;
  356. break;
  357. }
  358. case MYSQLND_OPT_NET_CMD_BUFFER_SIZE: {
  359. DBG_INF("MYSQLND_OPT_NET_CMD_BUFFER_SIZE");
  360. if (*(unsigned int*) value < MYSQLND_NET_CMD_BUFFER_MIN_SIZE) {
  361. DBG_RETURN(FAIL);
  362. }
  363. pfc->cmd_buffer.length = *(unsigned int*) value;
  364. DBG_INF_FMT("new_length=%zu", pfc->cmd_buffer.length);
  365. if (!pfc->cmd_buffer.buffer) {
  366. pfc->cmd_buffer.buffer = mnd_pemalloc(pfc->cmd_buffer.length, pfc->persistent);
  367. } else {
  368. pfc->cmd_buffer.buffer = mnd_perealloc(pfc->cmd_buffer.buffer, pfc->cmd_buffer.length, pfc->persistent);
  369. }
  370. break;
  371. }
  372. default:
  373. DBG_RETURN(FAIL);
  374. }
  375. DBG_RETURN(PASS);
  376. }
  377. /* }}} */
  378. /* {{{ mysqlnd_pfc::free_contents */
  379. static void
  380. MYSQLND_METHOD(mysqlnd_pfc, free_contents)(MYSQLND_PFC * pfc)
  381. {
  382. DBG_ENTER("mysqlnd_pfc::free_contents");
  383. #ifdef MYSQLND_COMPRESSION_ENABLED
  384. if (pfc->data->uncompressed_data) {
  385. pfc->data->uncompressed_data->free_buffer(&pfc->data->uncompressed_data);
  386. }
  387. #endif
  388. if (pfc->data->sha256_server_public_key) {
  389. mnd_pefree(pfc->data->sha256_server_public_key, pfc->persistent);
  390. pfc->data->sha256_server_public_key = NULL;
  391. }
  392. DBG_VOID_RETURN;
  393. }
  394. /* }}} */
  395. /* {{{ mysqlnd_pfc::init */
  396. static enum_func_status
  397. MYSQLND_METHOD(mysqlnd_pfc, init)(MYSQLND_PFC * const pfc, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  398. {
  399. unsigned int buf_size;
  400. DBG_ENTER("mysqlnd_pfc::init");
  401. buf_size = MYSQLND_G(net_cmd_buffer_size); /* this is long, cast to unsigned int*/
  402. pfc->data->m.set_client_option(pfc, MYSQLND_OPT_NET_CMD_BUFFER_SIZE, (char *) &buf_size);
  403. DBG_RETURN(PASS);
  404. }
  405. /* }}} */
  406. /* {{{ mysqlnd_pfc::dtor */
  407. static void
  408. MYSQLND_METHOD(mysqlnd_pfc, dtor)(MYSQLND_PFC * const pfc, MYSQLND_STATS * const stats, MYSQLND_ERROR_INFO * const error_info)
  409. {
  410. DBG_ENTER("mysqlnd_pfc::dtor");
  411. if (pfc) {
  412. pfc->data->m.free_contents(pfc);
  413. if (pfc->cmd_buffer.buffer) {
  414. DBG_INF("Freeing cmd buffer");
  415. mnd_pefree(pfc->cmd_buffer.buffer, pfc->persistent);
  416. pfc->cmd_buffer.buffer = NULL;
  417. }
  418. mnd_pefree(pfc, pfc->persistent);
  419. }
  420. DBG_VOID_RETURN;
  421. }
  422. /* }}} */
  423. MYSQLND_CLASS_METHODS_START(mysqlnd_protocol_packet_frame_codec)
  424. MYSQLND_METHOD(mysqlnd_pfc, init),
  425. MYSQLND_METHOD(mysqlnd_pfc, dtor),
  426. MYSQLND_METHOD(mysqlnd_pfc, reset),
  427. MYSQLND_METHOD(mysqlnd_pfc, set_client_option),
  428. MYSQLND_METHOD(mysqlnd_pfc, decode),
  429. MYSQLND_METHOD(mysqlnd_pfc, encode),
  430. MYSQLND_METHOD(mysqlnd_pfc, send),
  431. MYSQLND_METHOD(mysqlnd_pfc, receive),
  432. #ifdef MYSQLND_COMPRESSION_ENABLED
  433. MYSQLND_METHOD(mysqlnd_pfc, read_compressed_packet_from_stream_and_fill_read_buffer),
  434. #else
  435. NULL,
  436. #endif
  437. MYSQLND_METHOD(mysqlnd_pfc, free_contents),
  438. MYSQLND_CLASS_METHODS_END;
  439. /* {{{ mysqlnd_pfc_init */
  440. PHPAPI MYSQLND_PFC *
  441. mysqlnd_pfc_init(const bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
  442. {
  443. MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
  444. MYSQLND_PFC * pfc;
  445. DBG_ENTER("mysqlnd_pfc_init");
  446. pfc = factory->get_protocol_frame_codec(persistent, stats, error_info);
  447. DBG_RETURN(pfc);
  448. }
  449. /* }}} */
  450. /* {{{ mysqlnd_pfc_free */
  451. PHPAPI void
  452. mysqlnd_pfc_free(MYSQLND_PFC * const pfc, MYSQLND_STATS * stats, MYSQLND_ERROR_INFO * error_info)
  453. {
  454. DBG_ENTER("mysqlnd_pfc_free");
  455. if (pfc) {
  456. pfc->data->m.dtor(pfc, stats, error_info);
  457. }
  458. DBG_VOID_RETURN;
  459. }
  460. /* }}} */