-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathmysqlnd_net.c
311 lines (267 loc) · 9.28 KB
/
mysqlnd_net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
+----------------------------------------------------------------------+
| PHP Version 6 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Georg Richter <[email protected]> |
| Andrey Hristov <[email protected]> |
| Ulf Wendel <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_globals.h"
#include "mysqlnd.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_statistics.h"
#include "mysqlnd_debug.h"
#include "mysqlnd_block_alloc.h"
#include "ext/standard/sha1.h"
#include "php_network.h"
#include "zend_ini.h"
#ifndef PHP_WIN32
#include <netinet/tcp.h>
#else
#include <winsock.h>
#endif
/* {{{ mysqlnd_set_sock_no_delay */
static int
mysqlnd_set_sock_no_delay(php_stream * stream)
{
int socketd = ((php_netstream_data_t*)stream->abstract)->socket;
int ret = SUCCESS;
int flag = 1;
int result = setsockopt(socketd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
TSRMLS_FETCH();
DBG_ENTER("mysqlnd_set_sock_no_delay");
if (result == -1) {
ret = FAILURE;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_net::read_from_stream */
static enum_func_status
MYSQLND_METHOD(mysqlnd_net, read_from_stream)(MYSQLND * conn, zend_uchar * buffer, size_t count TSRMLS_DC)
{
size_t to_read = count, ret;
size_t old_chunk_size = conn->net->stream->chunk_size;
DBG_ENTER("mysqlnd_net::read_from_stream");
DBG_INF_FMT("count=%u", count);
conn->net->stream->chunk_size = MIN(to_read, conn->net->options.net_read_buffer_size);
while (to_read) {
if (!(ret = php_stream_read(conn->net->stream, (char *) buffer, to_read))) {
DBG_ERR_FMT("Error while reading header from socket");
DBG_RETURN(FAIL);
}
buffer += ret;
to_read -= ret;
}
MYSQLND_INC_CONN_STATISTIC_W_VALUE(&conn->stats, STAT_BYTES_RECEIVED, count);
conn->net->stream->chunk_size = old_chunk_size;
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_net::stream_write */
static size_t
MYSQLND_METHOD(mysqlnd_net, stream_write)(MYSQLND * const conn, const zend_uchar * const buf, size_t count TSRMLS_DC)
{
size_t ret;
DBG_ENTER("mysqlnd_net::stream_write");
ret = php_stream_write(conn->net->stream, (char *)buf, count);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_net::connect */
static enum_func_status
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)
{
unsigned int streams_options = ENFORCE_SAFE_MODE;
unsigned int streams_flags = STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT;
char * hashed_details = NULL;
int hashed_details_len = 0;
struct timeval tv;
DBG_ENTER("mysqlnd_net::connect");
if (persistent) {
hashed_details_len = spprintf(&hashed_details, 0, "%p", net);
DBG_INF_FMT("hashed_details=%s", hashed_details);
}
net->packet_no = net->compressed_envelope_packet_no = 0;
if (net->options.timeout_connect) {
tv.tv_sec = net->options.timeout_connect;
tv.tv_usec = 0;
}
net->stream = php_stream_xport_create(scheme, scheme_len, streams_options, streams_flags,
hashed_details, (net->options.timeout_connect) ? &tv : NULL,
NULL /*ctx*/, errstr, errcode);
if (*errstr || !net->stream) {
if (hashed_details) {
efree(hashed_details);
}
*errcode = CR_CONNECTION_ERROR;
DBG_RETURN(FAIL);
}
if (hashed_details) {
/*
If persistent, the streams register it in EG(persistent_list).
This is unwanted. ext/mysql or ext/mysqli are responsible to clean,
whatever they have to.
*/
zend_rsrc_list_entry *le;
if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_len + 1,
(void*) &le) == SUCCESS) {
/*
in_free will let streams code skip destructing - big HACK,
but STREAMS suck big time regarding persistent streams.
Just not compatible for extensions that need persistency.
*/
net->stream->in_free = 1;
zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_len + 1);
net->stream->in_free = 0;
}
#if ZEND_DEBUG
/* Shut-up the streams, they don't know what they are doing */
net->stream->__exposed = 1;
#endif
efree(hashed_details);
}
if (!net->options.timeout_read) {
/* should always happen because read_timeout cannot be set via API */
net->options.timeout_read = (unsigned int) MYSQLND_G(net_read_timeout);
}
if (net->options.timeout_read)
{
tv.tv_sec = net->options.timeout_read;
tv.tv_usec = 0;
php_stream_set_option(net->stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
}
if (!memcmp(scheme, "tcp://", sizeof("tcp://") - 1)) {
/* TCP -> Set TCP_NODELAY */
mysqlnd_set_sock_no_delay(net->stream);
}
{
unsigned int buf_size = MYSQLND_G(net_read_buffer_size); /* this is long, cast to unsigned int*/
net->m.set_client_option(net, MYSQLND_OPT_NET_READ_BUFFER_SIZE, (char *)&buf_size TSRMLS_CC);
}
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_net::set_client_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_net, set_client_option)(MYSQLND_NET * const net, enum mysqlnd_option option, const char * const value TSRMLS_DC)
{
DBG_ENTER("mysqlnd_net::set_client_option");
DBG_INF_FMT("option=%d", option);
switch (option) {
case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
DBG_INF("MYSQLND_OPT_NET_CMD_BUFFER_SIZE");
if (*(unsigned int*) value < MYSQLND_NET_CMD_BUFFER_MIN_SIZE) {
DBG_RETURN(FAIL);
}
net->cmd_buffer.length = *(unsigned int*) value;
DBG_INF_FMT("new_length=%u", net->cmd_buffer.length);
if (!net->cmd_buffer.buffer) {
net->cmd_buffer.buffer = mnd_pemalloc(net->cmd_buffer.length, net->persistent);
} else {
net->cmd_buffer.buffer = mnd_perealloc(net->cmd_buffer.buffer, net->cmd_buffer.length, net->persistent);
}
break;
case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
DBG_INF("MYSQLND_OPT_NET_READ_BUFFER_SIZE");
net->options.net_read_buffer_size = *(unsigned int*) value;
DBG_INF_FMT("new_length=%u", net->options.net_read_buffer_size);
break;
case MYSQL_OPT_CONNECT_TIMEOUT:
DBG_INF("MYSQL_OPT_CONNECT_TIMEOUT");
net->options.timeout_connect = *(unsigned int*) value;
break;
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_OPT_READ_TIMEOUT:
DBG_INF("MYSQL_OPT_READ_TIMEOUT");
net->options.timeout_read = *(unsigned int*) value;
break;
case MYSQL_OPT_WRITE_TIMEOUT:
DBG_INF("MYSQL_OPT_WRITE_TIMEOUT");
net->options.timeout_write = *(unsigned int*) value;
break;
#endif
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_OPT_COMPRESS:
#endif
/* currently not supported. Todo!! */
break;
default:
DBG_RETURN(FAIL);
}
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_net::set_client_option */
static void
MYSQLND_METHOD(mysqlnd_net, free_contents)(MYSQLND_NET * net TSRMLS_DC)
{
DBG_ENTER("mysqlnd_net::free_contents");
#ifdef MYSQLND_COMPRESSION_ENABLED
if (net->uncompressed_data) {
net->uncompressed_data->free_buffer(&net->uncompressed_data TSRMLS_CC);
}
#endif
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_net_init */
MYSQLND_NET *
mysqlnd_net_init(zend_bool persistent TSRMLS_DC)
{
MYSQLND_NET * net = mnd_pecalloc(1, sizeof(MYSQLND_NET), persistent);
DBG_ENTER("mysqlnd_net_init");
DBG_INF_FMT("persistent=%d", persistent);
net->persistent = persistent;
net->m.connect = MYSQLND_METHOD(mysqlnd_net, connect);
net->m.stream_read = MYSQLND_METHOD(mysqlnd_net, read_from_stream);
net->m.stream_write = MYSQLND_METHOD(mysqlnd_net, stream_write);
net->m.set_client_option = MYSQLND_METHOD(mysqlnd_net, set_client_option);
net->m.free_contents = MYSQLND_METHOD(mysqlnd_net, free_contents);
{
unsigned int buf_size = MYSQLND_G(net_read_buffer_size); /* this is long, cast to unsigned int*/
net->m.set_client_option(net, MYSQLND_OPT_NET_CMD_BUFFER_SIZE, (char *) &buf_size TSRMLS_CC);
}
DBG_RETURN(net);
}
/* }}} */
/* {{{ mysqlnd_net_init */
void
mysqlnd_net_free(MYSQLND_NET * net TSRMLS_DC)
{
zend_bool pers = net->persistent;
DBG_ENTER("mysqlnd_net_free");
if (net) {
net->m.free_contents(net TSRMLS_CC);
if (net->cmd_buffer.buffer) {
DBG_INF("Freeing cmd buffer");
mnd_pefree(net->cmd_buffer.buffer, pers);
net->cmd_buffer.buffer = NULL;
}
if (net->stream) {
DBG_INF_FMT("Freeing stream. abstract=%p", net->stream->abstract);
if (pers) {
php_stream_free(net->stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
} else {
php_stream_free(net->stream, PHP_STREAM_FREE_CLOSE);
}
net->stream = NULL;
}
mnd_pefree(net, pers);
}
DBG_VOID_RETURN;
}
/* }}} */