mysqlnd_net.c 41 KB

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