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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
|
/*
+----------------------------------------------------------------------+
| PHP Version 6 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2007 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Georg Richter <georg@mysql.com> |
| Andrey Hristov <andrey@mysql.com> |
| Ulf Wendel <uwendel@mysql.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_result.h"
#include "mysqlnd_statistics.h"
#include "mysqlnd_charset.h"
#include "php_ini.h"
#include "ext/standard/basic_functions.h"
#include "ext/standard/php_lcg.h"
#include "ext/standard/info.h"
#define MYSQLND_SILENT
/* the server doesn't support 4byte utf8, but let's make it forward compatible */
#define MYSQLND_MAX_ALLOWED_USER_LEN 256 /* 64 char * 4byte */
#define MYSQLND_MAX_ALLOWED_DB_LEN 256 /* 64 char * 4byte */
/*
TODO :
- Don't bind so tightly the metadata with the result set. This means
that the metadata reading should not expect a MYSQLND_RES pointer, it
does not need it, but return a pointer to the metadata (MYSQLND_FIELD *).
For normal statements we will then just assign it to a member of
MYSQLND_RES. For PS statements, it will stay as part of the statement
(MYSQLND_STMT) between prepare and execute. At execute the new metadata
will be sent by the server, so we will discard the old one and then
finally attach it to the result set. This will make the code more clean,
as a prepared statement won't have anymore stmt->result != NULL, as it
is now, just to have where to store the metadata.
- Change mysqlnd_simple_command to accept a heap dynamic array of MYSQLND_STRING
terminated by a string with ptr being NULL. Thus, multi-part messages can be
sent to the network like writev() and this can save at least for
mysqlnd_stmt_send_long_data() new malloc. This change will probably make the
code in few other places cleaner.
*/
extern MYSQLND_CHARSET *mysqlnd_charsets;
static const char * mysqlnd_server_gone = "MySQL server has gone away";
const char * mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
MYSQLND_STATS *mysqlnd_global_stats = NULL;
static zend_bool mysqlnd_library_initted = FALSE;
/* {{{ mysqlnd_library_init */
static
void mysqlnd_library_init(zend_bool collect_statistics)
{
if (mysqlnd_library_initted == FALSE) {
mysqlnd_library_initted = TRUE;
_mysqlnd_init_ps_subsystem();
if (collect_statistics) {
mysqlnd_global_stats = calloc(1, sizeof(MYSQLND_STATS));
#ifdef ZTS
mysqlnd_global_stats->LOCK_access = tsrm_mutex_alloc();
#endif
}
}
}
/* }}} */
/* {{{ mysqlnd_library_end */
static
void mysqlnd_library_end()
{
if (mysqlnd_library_initted == TRUE) {
if (mysqlnd_global_stats) {
#ifdef ZTS
tsrm_mutex_free(mysqlnd_global_stats->LOCK_access);
#endif
free(mysqlnd_global_stats);
mysqlnd_global_stats = NULL;
}
mysqlnd_library_initted = FALSE;
}
}
/* }}} */
/* {{{ mysqlnd_conn::free_contents */
static void
MYSQLND_METHOD(mysqlnd_conn, free_contents)(MYSQLND *conn TSRMLS_DC)
{
zend_bool pers = conn->persistent;
mysqlnd_local_infile_default(conn, TRUE);
if (conn->current_result) {
conn->current_result->m.free_result_contents(conn->current_result TSRMLS_CC);
efree(conn->current_result);
conn->current_result = NULL;
}
if (conn->net.stream) {
php_stream_free(conn->net.stream, (pers) ? PHP_STREAM_FREE_RSRC_DTOR :
PHP_STREAM_FREE_CLOSE);
conn->net.stream = NULL;
}
if (conn->host) {
pefree(conn->host, pers);
conn->host = NULL;
}
if (conn->user) {
pefree(conn->user, pers);
conn->user = NULL;
}
if (conn->passwd) {
pefree(conn->passwd, pers);
conn->passwd = NULL;
}
if (conn->unix_socket) {
pefree(conn->unix_socket, pers);
conn->unix_socket = NULL;
}
if (conn->scheme) {
pefree(conn->scheme, pers);
conn->scheme = NULL;
}
if (conn->server_version) {
pefree(conn->server_version, pers);
conn->server_version = NULL;
}
if (conn->host_info) {
pefree(conn->host_info, pers);
conn->host_info = NULL;
}
if (conn->scramble) {
pefree(conn->scramble, pers);
conn->scramble = NULL;
}
if (conn->last_message) {
pefree(conn->last_message, pers);
conn->last_message = NULL;
}
if (conn->options.charset_name) {
pefree(conn->options.charset_name, pers);
conn->options.charset_name = NULL;
}
if (conn->options.num_commands) {
unsigned int i;
for (i=0; i < conn->options.num_commands; i++) {
pefree(conn->options.init_commands[i], pers);
}
pefree(conn->options.init_commands, pers);
conn->options.init_commands = NULL;
}
if (conn->options.cfg_file) {
pefree(conn->options.cfg_file, pers);
conn->options.cfg_file = NULL;
}
if (conn->options.cfg_section) {
pefree(conn->options.cfg_section, pers);
conn->options.cfg_section = NULL;
}
if (conn->options.ssl_key) {
pefree(conn->options.ssl_key, pers);
conn->options.ssl_key = NULL;
}
if (conn->options.ssl_cert) {
pefree(conn->options.ssl_cert, pers);
conn->options.ssl_cert = NULL;
}
if (conn->options.ssl_ca) {
pefree(conn->options.ssl_ca, pers);
conn->options.ssl_ca = NULL;
}
if (conn->options.ssl_capath) {
pefree(conn->options.ssl_capath, pers);
conn->options.ssl_capath = NULL;
}
if (conn->options.ssl_cipher) {
pefree(conn->options.ssl_cipher, pers);
conn->options.ssl_cipher = NULL;
}
if (conn->zval_cache) {
mysqlnd_palloc_free_thd_cache_reference(&conn->zval_cache);
conn->zval_cache = NULL;
}
if (conn->qcache) {
mysqlnd_qcache_free_cache_reference(&conn->qcache);
conn->qcache = NULL;
}
if (conn->net.cmd_buffer.buffer) {
pefree(conn->net.cmd_buffer.buffer, pers);
conn->net.cmd_buffer.buffer = NULL;
}
}
/* }}} */
/* {{{ mysqlnd_conn::dtor */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND *conn TSRMLS_DC)
{
conn->m->free_contents(conn TSRMLS_CC);
pefree(conn, conn->persistent);
}
/* }}} */
/* {{{ mysqlnd_simple_command_handle_response */
enum_func_status
mysqlnd_simple_command_handle_response(MYSQLND *conn, enum php_mysql_packet_type ok_packet,
zend_bool silent, enum php_mysqlnd_server_command command
TSRMLS_DC)
{
enum_func_status ret;
switch (ok_packet) {
case PROT_OK_PACKET:{
php_mysql_packet_ok ok_response;
PACKET_INIT_ALLOCA(ok_response, PROT_OK_PACKET);
if (FAIL == (ret = PACKET_READ_ALLOCA(ok_response, conn))) {
if (!silent) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading %s's OK packet",
mysqlnd_command_to_text[command]);
}
} else {
#ifndef MYSQLND_SILENT
php_printf("\tOK from server\n");
#endif
if (0xFF == ok_response.field_count) {
/* The server signalled error. Set the error */
SET_CLIENT_ERROR(conn->error_info, ok_response.error_no,
ok_response.sqlstate, ok_response.error);
ret = FAIL;
/*
Cover a protocol design error: error packet does not
contain the server status. Therefore, the client has no way
to find out whether there are more result sets of
a multiple-result-set statement pending. Luckily, in 5.0 an
error always aborts execution of a statement, wherever it is
a multi-statement or a stored procedure, so it should be
safe to unconditionally turn off the flag here.
*/
conn->upsert_status.server_status &= ~SERVER_MORE_RESULTS_EXISTS;
SET_ERROR_AFF_ROWS(conn);
} else {
SET_NEW_MESSAGE(conn->last_message, conn->last_message_len,
ok_response.message, ok_response.message_len,
conn->persistent);
conn->upsert_status.warning_count = ok_response.warning_count;
conn->upsert_status.server_status = ok_response.server_status;
conn->upsert_status.affected_rows = ok_response.affected_rows;
conn->upsert_status.last_insert_id = ok_response.last_insert_id;
}
}
PACKET_FREE_ALLOCA(ok_response);
break;
}
case PROT_EOF_PACKET:{
php_mysql_packet_eof ok_response;
PACKET_INIT_ALLOCA(ok_response, PROT_EOF_PACKET);
if (FAIL == (ret = PACKET_READ_ALLOCA(ok_response, conn))) {
SET_CLIENT_ERROR(conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE,
"Malformed packet");
if (!silent) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading %s's EOF packet",
mysqlnd_command_to_text[command]);
}
} else if (0xFF == ok_response.field_count) {
/* The server signalled error. Set the error */
SET_CLIENT_ERROR(conn->error_info, ok_response.error_no,
ok_response.sqlstate, ok_response.error);
SET_ERROR_AFF_ROWS(conn);
} else if (0xFE != ok_response.field_count) {
SET_CLIENT_ERROR(conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE,
"Malformed packet");
if (!silent) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"EOF packet expected, field count wasn't 0xFE but 0x%2X",
ok_response.field_count);
}
} else {
#ifndef MYSQLND_SILENT
php_printf("\tOK from server\n");
#endif
}
PACKET_FREE_ALLOCA(ok_response);
break;
}
default:
ret = FAIL;
SET_CLIENT_ERROR(conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE,
"Malformed packet");
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Wrong response packet %d passed to the function",
ok_packet);
break;
}
return ret;
}
/* }}} */
/* {{{ mysqlnd_simple_command */
enum_func_status
mysqlnd_simple_command(MYSQLND *conn, enum php_mysqlnd_server_command command,
const char * const arg, size_t arg_len,
enum php_mysql_packet_type ok_packet, zend_bool silent TSRMLS_DC)
{
enum_func_status ret = PASS;
php_mysql_packet_command cmd_packet;
switch (conn->state) {
case CONN_READY:
break;
case CONN_QUIT_SENT:
SET_CLIENT_ERROR(conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
return FAIL;
default:
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
mysqlnd_out_of_sync);
return FAIL;
}
/* clean UPSERT info */
memset(&conn->upsert_status, 0, sizeof(conn->upsert_status));
SET_ERROR_AFF_ROWS(conn);
SET_EMPTY_ERROR(conn->error_info);
PACKET_INIT_ALLOCA(cmd_packet, PROT_CMD_PACKET);
cmd_packet.command = command;
if (arg && arg_len) {
cmd_packet.argument = arg;
cmd_packet.arg_len = arg_len;
}
if (! PACKET_WRITE_ALLOCA(cmd_packet, conn)) {
if (!silent) {
php_error(E_WARNING, "Error while sending %s packet", mysqlnd_command_to_text[command]);
}
SET_CLIENT_ERROR(conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
ret = FAIL;
} else if (ok_packet != PROT_LAST) {
ret = mysqlnd_simple_command_handle_response(conn, ok_packet, silent, command TSRMLS_CC);
}
/*
There is no need to call FREE_ALLOCA on cmd_packet as the
only allocated string is cmd_packet.argument and it was passed
to us. We should not free it.
*/
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::set_server_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, set_server_option)(MYSQLND * const conn,
enum_mysqlnd_server_option option TSRMLS_DC)
{
char buffer[2];
int2store(buffer, (uint) option);
return mysqlnd_simple_command(conn, COM_SET_OPTION, buffer, sizeof(buffer),
PROT_EOF_PACKET, FALSE TSRMLS_CC);
}
/* }}} */
/* {{{ mysqlnd_start_psession */
PHPAPI void mysqlnd_restart_psession(MYSQLND *conn)
{
MYSQLND_INC_CONN_STATISTIC(&conn->stats, STAT_CONNECT_REUSED);
/* Free here what should not be seen by the next script */
if (conn->last_message) {
pefree(conn->last_message, conn->persistent);
conn->last_message = NULL;
}
}
/* }}} */
/* {{{ mysqlnd_end_psession */
PHPAPI void mysqlnd_end_psession(MYSQLND *conn)
{
}
/* }}} */
/* {{{ mysqlnd_connect */
PHPAPI MYSQLND *mysqlnd_connect(MYSQLND *conn,
char *host, char *user,
char *passwd, unsigned int passwd_len,
char *db, unsigned int db_len,
unsigned int port,
char *socket,
unsigned int mysql_flags,
MYSQLND_THD_ZVAL_PCACHE *zval_cache
TSRMLS_DC)
{
char *transport = NULL, *errstr = NULL;
char *hashed_details = NULL;
int transport_len, errcode = 0;
unsigned int streams_options = ENFORCE_SAFE_MODE;
unsigned int streams_flags = STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT;
zend_bool self_alloced = FALSE;
struct timeval tv;
zend_bool unix_socket = FALSE;
const MYSQLND_CHARSET * charset;
php_mysql_packet_greet greet_packet;
php_mysql_packet_auth *auth_packet;
php_mysql_packet_ok ok_packet;
if (conn && conn->state != CONN_ALLOCED) {
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
mysqlnd_out_of_sync);
return NULL;
}
if (!host || !host[0]) {
host = "localhost";
}
if (!user || !user[0]) {
user = php_get_current_user();
}
if (!passwd) {
passwd = "";
passwd_len = 0;
}
if (!db) {
db = "";
db_len = 0;
}
if (!port && !socket) {
port = 3306;
}
#ifndef PHP_WIN32
if (!strncasecmp(host, "localhost", sizeof("localhost") - 1)) {
if (!socket) {
socket = "/tmp/mysql.sock";
}
transport_len = spprintf(&transport, 0, "unix://%s", socket);
unix_socket = TRUE;
} else
#endif
{
transport_len = spprintf(&transport, 0, "tcp://%s:%d", host, port);
}
if (conn->persistent) {
struct timeval tv;
gettimeofday(&tv, NULL);
/* We should generate something unique */
spprintf(&hashed_details, 0, "%s@%s@%s@%ld@%ld@%0.8F",
transport, user, db, tv.tv_sec, (long int)tv.tv_usec,
php_combined_lcg(TSRMLS_C) * 10);
}
PACKET_INIT_ALLOCA(greet_packet, PROT_GREET_PACKET);
PACKET_INIT(auth_packet, PROT_AUTH_PACKET, php_mysql_packet_auth *);
PACKET_INIT_ALLOCA(ok_packet, PROT_OK_PACKET);
if (!conn) {
conn = mysqlnd_init(FALSE);
self_alloced = TRUE;
}
conn->state = CONN_ALLOCED;
conn->net.packet_no = 0;
if (conn->options.timeout_connect) {
tv.tv_sec = conn->options.timeout_connect;
tv.tv_usec = 0;
}
if (conn->persistent) {
conn->scheme = pestrndup(transport, transport_len, 1);
efree(transport);
} else {
conn->scheme = transport;
}
conn->net.stream = php_stream_xport_create(conn->scheme, transport_len, streams_options, streams_flags,
hashed_details,
(conn->options.timeout_connect) ? &tv : NULL,
NULL /*ctx*/, &errstr, &errcode);
if (hashed_details) {
efree(hashed_details);
}
if (errstr || !conn->net.stream) {
goto err;
}
if (conn->options.timeout_read)
{
tv.tv_sec = conn->options.timeout_read;
tv.tv_usec = 0;
php_stream_set_option(conn->net.stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
}
if (!unix_socket) {
/* Set TCP_NODELAY */
mysqlnd_set_sock_no_delay(conn->net.stream);
}
if (FAIL == PACKET_READ_ALLOCA(greet_packet, conn)) {
#ifndef MYSQLND_SILENT
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading greeting packet");
#endif
goto err;
} else if (greet_packet.error_no) {
SET_CLIENT_ERROR(conn->error_info, greet_packet.error_no,
greet_packet.sqlstate, greet_packet.error);
goto err;
} else if (greet_packet.pre41) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 "
" is not supported. Server is %-.32s", greet_packet.server_version);
SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
"Connecting to 3.22, 3.23 & 4.0 servers is not supported");
goto err;
}
conn->thread_id = greet_packet.thread_id;
conn->protocol_version = greet_packet.protocol_version;
conn->server_version = greet_packet.server_version;
greet_packet.server_version = NULL; /* The string will be freed otherwise */
/* we allow load data local infile by default */
mysql_flags |= CLIENT_LOCAL_FILES;
auth_packet->user = user;
auth_packet->password = passwd;
if (conn->options.charset_name &&
(charset = mysqlnd_find_charset_name(conn->options.charset_name)))
{
auth_packet->charset_no = charset->nr;
#if PHP_MAJOR_VERSION >= 6
} else if (UG(unicode)) {
auth_packet->charset_no = 200;/* utf8 - swedish collation, check mysqlnd_charset.c */
#endif
} else {
auth_packet->charset_no = greet_packet.charset_no;
}
auth_packet->db = db;
auth_packet->db_len = db_len;
auth_packet->max_packet_size= 3UL*1024UL*1024UL*1024UL;
auth_packet->client_flags= mysql_flags;
conn->scramble = auth_packet->server_scramble_buf = pemalloc(SCRAMBLE_LENGTH, conn->persistent);
memcpy(auth_packet->server_scramble_buf, greet_packet.scramble_buf, SCRAMBLE_LENGTH);
PACKET_WRITE(auth_packet, conn);
if (FAIL == PACKET_READ_ALLOCA(ok_packet, conn) || ok_packet.field_count >= 0xFE) {
if (ok_packet.field_count == 0xFE) {
/* old authentication with new server !*/
php_error_docref(NULL TSRMLS_CC, E_WARNING, "mysqlnd cannot connect to MySQL 4.1+ using old authentication");
} else if (ok_packet.field_count == 0xFF) {
if (ok_packet.sqlstate[0]) {
if (!self_alloced) {
strncpy(conn->error_info.sqlstate, ok_packet.sqlstate, sizeof(conn->error_info.sqlstate));
}
#ifndef MYSQLND_SILENT
php_error_docref(NULL TSRMLS_CC, E_WARNING, "ERROR:%d [SQLSTATE:%s] %s",
ok_packet.error_no, ok_packet.sqlstate, ok_packet.error);
#endif
}
if (!self_alloced) {
conn->error_info.error_no = ok_packet.error_no;
strncpy(conn->error_info.error, ok_packet.error, sizeof(conn->error_info.error));
}
}
} else {
conn->state = CONN_READY;
conn->user = pestrdup(user, conn->persistent);
conn->passwd = pestrndup(passwd, passwd_len, conn->persistent);
conn->port = port;
if (host && !socket) {
char *p;
conn->host = pestrdup(host, conn->persistent);
spprintf(&p, 0, "MySQL host info: %s via TCP/IP", conn->host);
if (conn->persistent) {
conn->host_info = pestrdup(p, 1);
efree(p);
} else {
conn->host_info = p;
}
} else {
conn->unix_socket = pestrdup(socket, conn->persistent);
conn->host_info = pestrdup("MySQL host info: Localhost via UNIX socket", conn->persistent);
}
conn->client_flag = auth_packet->client_flags;
conn->max_packet_size = auth_packet->max_packet_size;
/* todo: check if charset is available */
conn->charset = mysqlnd_find_charset_nr(auth_packet->charset_no);
conn->server_capabilities = greet_packet.server_capabilities;
conn->upsert_status.warning_count = 0;
conn->upsert_status.server_status = greet_packet.server_status;
conn->upsert_status.affected_rows = 0;
SET_NEW_MESSAGE(conn->last_message, conn->last_message_len,
ok_packet.message, ok_packet.message_len,
conn->persistent);
SET_EMPTY_ERROR(conn->error_info);
PACKET_FREE_ALLOCA(greet_packet);
PACKET_FREE(auth_packet);
PACKET_FREE_ALLOCA(ok_packet);
conn->zval_cache = mysqlnd_palloc_get_thd_cache_reference(zval_cache);
conn->net.cmd_buffer.length = 128L*1024L;
conn->net.cmd_buffer.buffer = pemalloc(conn->net.cmd_buffer.length, conn->persistent);
MYSQLND_INC_CONN_STATISTIC(&conn->stats, STAT_CONNECT_SUCCESS);
{
uint as_unicode = 1;
conn->m->set_client_option(conn, MYSQLND_OPT_NUMERIC_AND_DATETIME_AS_UNICODE,
(char *)&as_unicode);
}
return conn;
}
err:
PACKET_FREE_ALLOCA(greet_packet);
PACKET_FREE(auth_packet);
PACKET_FREE_ALLOCA(ok_packet);
if (errstr) {
SET_CLIENT_ERROR(conn->error_info, errcode, UNKNOWN_SQLSTATE, errstr);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "[%d] %.64s (trying to connect via %s)", errcode, errstr, conn->scheme);
efree(errstr);
}
if (conn->scheme) {
pefree(conn->scheme, conn->persistent);
conn->scheme = NULL;
}
/* This will also close conn->net.stream if it has been opened */
conn->m->free_contents(conn TSRMLS_CC);
if (self_alloced) {
/*
We have alloced, thus there are no references to this
object - we are free to kill it!
*/
conn->m->dtor(conn TSRMLS_CC);
} else {
MYSQLND_INC_CONN_STATISTIC(&conn->stats, STAT_CONNECT_FAILURE);
}
return NULL;
}
/* }}} */
/* {{{ mysqlnd_conn::query */
/*
If conn->error_info.error_no is not zero, then we had an error.
Still the result from the query is PASS
*/
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, query)(MYSQLND *conn, const char *query, unsigned int query_len TSRMLS_DC)
{
enum_func_status ret;
if (PASS != mysqlnd_simple_command(conn, COM_QUERY, query, query_len,
PROT_LAST /* we will handle the OK packet*/,
FALSE TSRMLS_CC)) {
return FAIL;
}
/*
Here read the result set. We don't do it in simple_command because it need
information from the ok packet. We will fetch it ourselves.
*/
ret = mysqlnd_query_read_result_set_header(conn, NULL TSRMLS_CC);
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::list_method */
MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn, list_method)(MYSQLND *conn, const char *query, char *achtung_wild, char *par1 TSRMLS_DC)
{
char *show_query = NULL;
size_t show_query_len;
MYSQLND_RES *result = NULL;
if (par1) {
if (achtung_wild) {
show_query_len = spprintf(&show_query, 0, query, par1, achtung_wild);
} else {
show_query_len = spprintf(&show_query, 0, query, par1);
}
} else {
if (achtung_wild) {
show_query_len = spprintf(&show_query, 0, query, achtung_wild);
} else {
show_query_len = strlen(show_query = (char *)query);
}
}
if (PASS == conn->m->query(conn, show_query, show_query_len TSRMLS_CC)) {
result = conn->m->store_result(conn TSRMLS_CC);
}
if (show_query != query) {
efree(show_query);
}
return result;
}
/* }}} */
/* {{{ mysqlnd_conn::errno */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn, errno)(const MYSQLND * const conn)
{
return conn->error_info.error_no;
}
/* }}} */
/* {{{ mysqlnd_conn::error */
static const char *
MYSQLND_METHOD(mysqlnd_conn, error)(const MYSQLND * const conn)
{
return conn->error_info.error;
}
/* }}} */
/* {{{ mysqlnd_conn::sqlstate */
static const char *
MYSQLND_METHOD(mysqlnd_conn, sqlstate)(const MYSQLND * const conn)
{
return conn->error_info.sqlstate[0] ? conn->error_info.sqlstate:MYSQLND_SQLSTATE_NULL;
}
/* }}} */
/* {{{ mysqlnd_old_escape_string */
PHPAPI ulong mysqlnd_old_escape_string(char *newstr, const char *escapestr, int escapestr_len)
{
return mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"),
newstr, escapestr, escapestr_len);
}
/* }}} */
/* {{{ mysqlnd_conn::escape_string */
static ulong
MYSQLND_METHOD(mysqlnd_conn, escape_string)(const MYSQLND * const conn, char *newstr, const char *escapestr, int escapestr_len)
{
if (conn->upsert_status.server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
return mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len);
}
return mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len);
}
/* }}} */
/* {{{ mysqlnd_conn::dump_debug_info */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, dump_debug_info)(MYSQLND * const conn TSRMLS_DC)
{
return mysqlnd_simple_command(conn, COM_DEBUG, NULL, 0, PROT_EOF_PACKET, FALSE TSRMLS_CC);
}
/* }}} */
/* {{{ mysqlnd_conn::select_db */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, select_db)(MYSQLND * const conn,
const char * const db,
unsigned int db_len TSRMLS_DC)
{
enum_func_status ret;
ret = mysqlnd_simple_command(conn, COM_INIT_DB, db, db_len, PROT_OK_PACKET, FALSE TSRMLS_CC);
/*
The server sends 0 but libmysql doesn't read it and has established
a protocol of giving back -1. Thus we have to follow it :(
*/
SET_ERROR_AFF_ROWS(conn);
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::ping */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, ping)(MYSQLND * const conn TSRMLS_DC)
{
enum_func_status ret;
ret = mysqlnd_simple_command(conn, COM_PING, NULL, 0, PROT_OK_PACKET, FALSE TSRMLS_CC);
/*
The server sends 0 but libmysql doesn't read it and has established
a protocol of giving back -1. Thus we have to follow it :(
*/
SET_ERROR_AFF_ROWS(conn);
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::stat */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, stat)(MYSQLND *conn, char **message, unsigned int * message_len TSRMLS_DC)
{
enum_func_status ret;
php_mysql_packet_stats stats_header;
ret = mysqlnd_simple_command(conn, COM_STATISTICS, NULL, 0, PROT_LAST, FALSE TSRMLS_CC);
if (FAIL == ret) {
return FAIL;
}
PACKET_INIT_ALLOCA(stats_header, PROT_STATS_PACKET);
if (FAIL == (ret = PACKET_READ_ALLOCA(stats_header, conn))) {
return FAIL;
}
*message = stats_header.message;
*message_len = stats_header.message_len;
/* Ownership transfer */
stats_header.message = NULL;
PACKET_FREE_ALLOCA(stats_header);
return PASS;
}
/* }}} */
/* {{{ mysqlnd_conn::kill */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, kill)(MYSQLND *conn, unsigned long pid TSRMLS_DC)
{
enum_func_status ret;
char buff[4];
int4store(buff, pid);
/* If we kill ourselves don't expect OK packet, PROT_LAST will skip it */
if (pid != conn->thread_id) {
ret = mysqlnd_simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_OK_PACKET, FALSE TSRMLS_CC);
/*
The server sends 0 but libmysql doesn't read it and has established
a protocol of giving back -1. Thus we have to follow it :(
*/
SET_ERROR_AFF_ROWS(conn);
} else if (PASS == (ret = mysqlnd_simple_command(conn, COM_PROCESS_KILL, buff,
4, PROT_LAST, FALSE TSRMLS_CC))) {
conn->state = CONN_QUIT_SENT;
}
return ret;
}
/* }}} */
/* {{{ _mysqlnd_conn::set_charset */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, set_charset)(MYSQLND * const conn, const char * const csname TSRMLS_DC)
{
enum_func_status ret = PASS;
char *query;
size_t query_len;
const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
if (!charset) {
SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE,
"Invalid characterset or character set not supported");
return FAIL;
}
query_len = spprintf(&query, 0, "SET NAMES %s", csname);
if (FAIL == conn->m->query(conn, query, query_len TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error executing query");
} else if (conn->error_info.error_no) {
ret = FAIL;
} else {
conn->charset = charset;
}
efree(query);
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::refresh */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, refresh)(MYSQLND * const conn, unsigned long options TSRMLS_DC)
{
zend_uchar bits[1];
int1store(bits, options);
return mysqlnd_simple_command(conn, COM_REFRESH, (char *)bits, 1, PROT_OK_PACKET, FALSE TSRMLS_CC);
}
/* }}} */
/* {{{ mysqlnd_conn::shutdown */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, shutdown)(MYSQLND * const conn, unsigned long level TSRMLS_DC)
{
zend_uchar bits[1];
int1store(bits, level);
return mysqlnd_simple_command(conn, COM_SHUTDOWN, (char *)bits, 1, PROT_OK_PACKET, FALSE TSRMLS_CC);
}
/* }}} */
/* {{{ mysqlnd_send_close */
static enum_func_status
mysqlnd_send_close(MYSQLND * conn TSRMLS_DC)
{
enum_func_status ret = PASS;
switch (conn->state) {
case CONN_READY:
ret = mysqlnd_simple_command(conn, COM_QUIT, NULL, 0, PROT_LAST,
conn->tmp_int? TRUE : FALSE TSRMLS_CC);
/* Do nothing */
break;
case CONN_SENDING_LOAD_DATA:
/*
Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
will crash (assert) a debug server.
*/
case CONN_NEXT_RESULT_PENDING:
case CONN_QUERY_SENT:
case CONN_FETCHING_DATA:
MYSQLND_INC_CONN_STATISTIC(NULL, STAT_CLOSE_IN_MIDDLE);
#ifndef MYSQLND_SILENT
php_printf("Brutally closing connection [%p][%s]\n", conn, conn->scheme);
#endif
/*
Do nothing, the connection will be brutally closed
and the server will catch it and free close from its side.
*/
case CONN_ALLOCED:
/*
Allocated but not connected or there was failure when trying
to connect with pre-allocated connect.
Fall-through
*/
case CONN_QUIT_SENT:
/* The user has killed its own connection */
break;
}
/*
We hold one reference, and every other object which needs the
connection does increase it by 1.
*/
conn->state = CONN_QUIT_SENT;
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::close */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn, enum_connection_close_type close_type TSRMLS_DC)
{
enum_func_status ret = PASS;
static enum_mysqlnd_collected_stats
close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
STAT_CLOSE_EXPLICIT,
STAT_CLOSE_IMPLICIT,
STAT_CLOSE_DISCONNECT
};
enum_mysqlnd_collected_stats stat = close_type_to_stat_map[close_type];
MYSQLND_INC_CONN_STATISTIC(NULL, stat);
mysqlnd_send_close(conn TSRMLS_CC);
conn->m->free_reference(conn TSRMLS_CC);
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::get_reference */
static MYSQLND *
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, get_reference)(MYSQLND * const conn)
{
++conn->refcount;
return conn;
}
/* }}} */
/* {{{ mysqlnd_conn::free_reference */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, free_reference)(MYSQLND * const conn TSRMLS_DC)
{
if (!(--conn->refcount)) {
/*
No multithreading issues as we don't share the connection :)
This will free the object too, of course because references has
reached zero.
*/
mysqlnd_send_close(conn TSRMLS_CC);
conn->m->dtor(conn TSRMLS_CC);
}
}
/* }}} */
/* {{{ mysqlnd_conn::field_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn, field_count)(const MYSQLND * const conn)
{
return conn->field_count;
}
/* }}} */
/* {{{ mysqlnd_conn::insert_id */
static mynd_ulonglong
MYSQLND_METHOD(mysqlnd_conn, insert_id)(const MYSQLND * const conn)
{
return conn->upsert_status.last_insert_id;
}
/* }}} */
/* {{{ mysqlnd_conn::affected_rows */
static mynd_ulonglong
MYSQLND_METHOD(mysqlnd_conn, affected_rows)(const MYSQLND * const conn)
{
return conn->upsert_status.affected_rows;
}
/* }}} */
/* {{{ mysqlnd_conn::warning_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn, warning_count)(const MYSQLND * const conn)
{
return conn->upsert_status.warning_count;
}
/* }}} */
/* {{{ mysqlnd_conn::info */
static const char *
MYSQLND_METHOD(mysqlnd_conn, info)(const MYSQLND * const conn)
{
return conn->last_message;
}
/* }}} */
/* {{{ mysqlnd_get_client_info */
PHPAPI const char * mysqlnd_get_client_info()
{
return MYSQLND_VERSION;
}
/* }}} */
/* {{{ mysqlnd_get_client_version */
PHPAPI unsigned int mysqlnd_get_client_version()
{
return MYSQLND_VERSION_ID;
}
/* }}} */
/* {{{ mysqlnd_conn::get_server_info */
static const char *
MYSQLND_METHOD(mysqlnd_conn, get_server_info)(const MYSQLND * const conn)
{
return conn->server_version;
}
/* }}} */
/* {{{ mysqlnd_conn::get_host_info */
static const char *
MYSQLND_METHOD(mysqlnd_conn, get_host_info)(const MYSQLND * const conn)
{
return conn->host_info;
}
/* }}} */
/* {{{ mysqlnd_conn::get_proto_info */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn, get_proto_info)(const MYSQLND *const conn)
{
return conn->protocol_version;
}
/* }}} */
/* {{{ mysqlnd_conn::charset_name */
static const char *
MYSQLND_METHOD(mysqlnd_conn, charset_name)(const MYSQLND * const conn)
{
return conn->charset->name;
}
/* }}} */
/* {{{ mysqlnd_conn::thread_id */
static mynd_ulonglong
MYSQLND_METHOD(mysqlnd_conn, thread_id)(const MYSQLND * const conn)
{
return conn->thread_id;
}
/* }}} */
/* {{{ mysqlnd_conn::get_server_version */
static unsigned long
MYSQLND_METHOD(mysqlnd_conn, get_server_version)(const MYSQLND * const conn)
{
long major, minor, patch;
char *p;
if (!(p = conn->server_version)) {
return 0;
}
major = strtol(p, &p, 10);
p += 1; /* consume the dot */
minor = strtol(p, &p, 10);
p += 1; /* consume the dot */
patch = strtol(p, &p, 10);
return (unsigned long)(major * 10000L + (unsigned long)(minor * 100L + patch));
}
/* }}} */
/* {{{ mysqlnd_conn::more_results */
static zend_bool
MYSQLND_METHOD(mysqlnd_conn,more_results)(const MYSQLND * const conn)
{
/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
return conn->upsert_status.server_status & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE;
}
/* }}} */
/* {{{ mysqlnd_conn::next_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, next_result)(MYSQLND * const conn TSRMLS_DC)
{
enum_func_status ret;
if (conn->state != CONN_NEXT_RESULT_PENDING) {
return FAIL;
}
SET_EMPTY_ERROR(conn->error_info);
conn->upsert_status.affected_rows= ~(mynd_ulonglong) 0;
/*
We are sure that there is a result set, since conn->state is set accordingly
in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
*/
if (FAIL == (ret = mysqlnd_query_read_result_set_header(conn, NULL TSRMLS_CC))) {
#ifndef MYSQLND_SILENT
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Serious error");
#endif
conn->state = CONN_QUIT_SENT;
}
return ret;
}
/* }}} */
/* {{{ mysqlnd_field_type_name */
PHPAPI const char *mysqlnd_field_type_name(enum mysqlnd_field_types field_type)
{
switch(field_type) {
case FIELD_TYPE_STRING:
case FIELD_TYPE_VAR_STRING:
return "string";
case FIELD_TYPE_TINY:
case FIELD_TYPE_SHORT:
case FIELD_TYPE_LONG:
case FIELD_TYPE_LONGLONG:
case FIELD_TYPE_INT24:
return "int";
case FIELD_TYPE_FLOAT:
case FIELD_TYPE_DOUBLE:
case FIELD_TYPE_DECIMAL:
case FIELD_TYPE_NEWDECIMAL:
return "real";
case FIELD_TYPE_TIMESTAMP:
return "timestamp";
case FIELD_TYPE_YEAR:
return "year";
case FIELD_TYPE_DATE:
case FIELD_TYPE_NEWDATE:
return "date";
case FIELD_TYPE_TIME:
return "time";
case FIELD_TYPE_SET:
return "set";
case FIELD_TYPE_ENUM:
return "enum";
case FIELD_TYPE_GEOMETRY:
return "geometry";
case FIELD_TYPE_DATETIME:
return "datetime";
case FIELD_TYPE_TINY_BLOB:
case FIELD_TYPE_MEDIUM_BLOB:
case FIELD_TYPE_LONG_BLOB:
case FIELD_TYPE_BLOB:
return "blob";
case FIELD_TYPE_NULL:
return "null";
case FIELD_TYPE_BIT:
return "bit";
default:
return "unknown";
}
}
/* }}} */
/* {{{ mysqlnd_conn::change_user */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, change_user)(MYSQLND * const conn,
const char *user,
const char *passwd,
const char *db TSRMLS_DC)
{
/*
User could be max 16 * 3 (utf8), pass is 20 usually, db is up to 64*3
Stack space is not that expensive, so use a bit more to be protected against
stack overrungs.
*/
size_t user_len;
enum_func_status ret;
php_mysql_packet_chg_user_resp chg_user_resp;
char buffer[MYSQLND_MAX_ALLOWED_USER_LEN + 1 + SCRAMBLE_LENGTH + MYSQLND_MAX_ALLOWED_DB_LEN + 1];
char *p = buffer;
if (!user) {
user = "";
}
if (!passwd) {
passwd = "";
}
if (!db) {
db = "";
}
/* 1. user ASCIIZ */
user_len = strlen(user);
memcpy(p, user, MIN(user_len, MYSQLND_MAX_ALLOWED_DB_LEN));
p += user_len;
*p++ = '\0';
/* 2. password SCRAMBLE_LENGTH followed by the scramble or \0 */
if (passwd[0]) {
*p++ = SCRAMBLE_LENGTH;
php_mysqlnd_scramble((unsigned char *)p, conn->scramble, (unsigned char *)passwd);
p += SCRAMBLE_LENGTH;
} else {
*p++ = '\0';
}
/* 3. db ASCIIZ */
if (db[0]) {
size_t db_len = strlen(db);
memcpy(p, db, MIN(db_len, MYSQLND_MAX_ALLOWED_DB_LEN));
p += db_len;
}
*p++ = '\0';
if (PASS != mysqlnd_simple_command(conn, COM_CHANGE_USER, buffer, p - buffer,
PROT_LAST /* we will handle the OK packet*/,
FALSE TSRMLS_CC)) {
return FAIL;
}
PACKET_INIT_ALLOCA(chg_user_resp, PROT_CHG_USER_PACKET);
ret = PACKET_READ_ALLOCA(chg_user_resp, conn);
conn->error_info = chg_user_resp.error_info;
PACKET_FREE_ALLOCA(chg_user_resp);
if (conn->error_info.error_no) {
ret = FAIL;
/*
COM_CHANGE_USER is broken in 5.1. At least in 5.1.15 and 5.1.14, 5.1.11 is immune.
bug#25371 mysql_change_user() triggers "packets out of sync"
When it gets fixed, there should be one more check here
*/
if (mysqlnd_get_server_version(conn) > 50113L &&
mysqlnd_get_server_version(conn) < 50118L)
{
php_mysql_packet_ok redundant_error_packet;
PACKET_INIT_ALLOCA(redundant_error_packet, PROT_OK_PACKET);
PACKET_READ_ALLOCA(redundant_error_packet, conn);
PACKET_FREE_ALLOCA(redundant_error_packet);
}
}
/*
Here we should close all statements. Unbuffered queries should not be a
problem as we won't allow sending COM_CHANGE_USER.
*/
return ret;
}
/* }}} */
/* {{{ mysqlnd_conn::set_client_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, set_client_option)(MYSQLND * const conn,
enum mysqlnd_option option,
const char * const value)
{
switch (option) {
case MYSQLND_OPT_NUMERIC_AND_DATETIME_AS_UNICODE:
conn->options.numeric_and_datetime_as_unicode = *(uint*) value;
break;
#ifdef MYSQLND_STRING_TO_INT_CONVERSION
case MYSQLND_OPT_INT_AND_YEAR_AS_INT:
conn->options.int_and_year_as_int = *(uint*) value;
break;
#endif
case MYSQL_OPT_CONNECT_TIMEOUT:
conn->options.timeout_connect = *(uint*) value;
break;
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_OPT_READ_TIMEOUT:
conn->options.timeout_read = *(uint*) value;
break;
case MYSQL_OPT_WRITE_TIMEOUT:
conn->options.timeout_write = *(uint*) value;
break;
#endif
case MYSQL_OPT_LOCAL_INFILE:
if (!value || (*(uint*) value) ? 1 : 0) {
conn->options.flags |= CLIENT_LOCAL_FILES;
} else {
conn->options.flags &= ~CLIENT_LOCAL_FILES;
}
break;
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_OPT_COMPRESS:
#endif
case MYSQL_INIT_COMMAND:
case MYSQL_READ_DEFAULT_FILE:
case MYSQL_READ_DEFAULT_GROUP:
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_SET_CLIENT_IP:
case MYSQL_REPORT_DATA_TRUNCATION:
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
#endif
/* currently not supported. Todo!! */
break;
case MYSQL_SET_CHARSET_NAME:
conn->options.charset_name = pestrdup(value, conn->persistent);
break;
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_SET_CHARSET_DIR:
case MYSQL_OPT_RECONNECT:
case MYSQL_OPT_PROTOCOL:
/* we don't need external character sets, all character sets are
compiled in. For compatibility we just ignore this setting.
Same for protocol, we don't support old protocol */
case MYSQL_OPT_USE_REMOTE_CONNECTION:
case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
case MYSQL_OPT_GUESS_CONNECTION:
/* todo: throw an error, we don't support embedded */
break;
#endif
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_OPT_NAMED_PIPE:
case MYSQL_SHARED_MEMORY_BASE_NAME:
case MYSQL_OPT_USE_RESULT:
case MYSQL_SECURE_AUTH:
/* not sure, todo ? */
#endif
default:
return FAIL;
}
return PASS;
}
/* }}} */
/* {{{ _mysqlnd_conn::use_result */
MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn, use_result)(MYSQLND * const conn TSRMLS_DC)
{
MYSQLND_RES *result;
if (!conn->current_result) {
return NULL;
}
/* Nothing to store for UPSERT/LOAD DATA */
if (conn->last_query_type != QUERY_SELECT || conn->state != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
mysqlnd_out_of_sync);
return NULL;
}
MYSQLND_INC_CONN_STATISTIC(&conn->stats, STAT_UNBUFFERED_SETS);
result = conn->current_result;
conn->current_result = NULL;
result->conn = conn->m->get_reference(conn);
return result->m.use_result(result, FALSE TSRMLS_CC);
}
/* }}} */
/* {{{ _mysqlnd_conn::store_result */
MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn, store_result)(MYSQLND * const conn TSRMLS_DC)
{
MYSQLND_RES *result;
if (!conn->current_result) {
return NULL;
}
/* Nothing to store for UPSERT/LOAD DATA*/
if (conn->last_query_type != QUERY_SELECT || conn->state != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
mysqlnd_out_of_sync);
return NULL;
}
MYSQLND_INC_CONN_STATISTIC(&conn->stats, STAT_BUFFERED_SETS);
result = conn->current_result;
conn->current_result = NULL;
return result->m.store_result(result, conn, FALSE TSRMLS_CC);
}
/* }}} */
/* {{{ mysqlnd_conn::get_connection_stats */
void
MYSQLND_METHOD(mysqlnd_conn, get_connection_stats)(const MYSQLND * const conn,
zval *return_value
TSRMLS_DC ZEND_FILE_LINE_DC)
{
mysqlnd_fill_stats_hash(&(conn->stats), return_value TSRMLS_CC ZEND_FILE_LINE_CC);
}
/* }}} */
MYSQLND_STMT * _mysqlnd_stmt_init(MYSQLND * const conn);
MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
MYSQLND_METHOD(mysqlnd_conn, escape_string),
MYSQLND_METHOD(mysqlnd_conn, set_charset),
MYSQLND_METHOD(mysqlnd_conn, query),
MYSQLND_METHOD(mysqlnd_conn, use_result),
MYSQLND_METHOD(mysqlnd_conn, store_result),
MYSQLND_METHOD(mysqlnd_conn, next_result),
MYSQLND_METHOD(mysqlnd_conn, more_results),
_mysqlnd_stmt_init,
MYSQLND_METHOD(mysqlnd_conn, shutdown),
MYSQLND_METHOD(mysqlnd_conn, refresh),
MYSQLND_METHOD(mysqlnd_conn, ping),
MYSQLND_METHOD(mysqlnd_conn, kill),
MYSQLND_METHOD(mysqlnd_conn, select_db),
MYSQLND_METHOD(mysqlnd_conn, dump_debug_info),
MYSQLND_METHOD(mysqlnd_conn, change_user),
MYSQLND_METHOD(mysqlnd_conn, errno),
MYSQLND_METHOD(mysqlnd_conn, error),
MYSQLND_METHOD(mysqlnd_conn, sqlstate),
MYSQLND_METHOD(mysqlnd_conn, thread_id),
MYSQLND_METHOD(mysqlnd_conn, get_connection_stats),
MYSQLND_METHOD(mysqlnd_conn, get_server_version),
MYSQLND_METHOD(mysqlnd_conn, get_server_info),
MYSQLND_METHOD(mysqlnd_conn, stat),
MYSQLND_METHOD(mysqlnd_conn, get_host_info),
MYSQLND_METHOD(mysqlnd_conn, get_proto_info),
MYSQLND_METHOD(mysqlnd_conn, info),
MYSQLND_METHOD(mysqlnd_conn, charset_name),
MYSQLND_METHOD(mysqlnd_conn, list_method),
MYSQLND_METHOD(mysqlnd_conn, insert_id),
MYSQLND_METHOD(mysqlnd_conn, affected_rows),
MYSQLND_METHOD(mysqlnd_conn, warning_count),
MYSQLND_METHOD(mysqlnd_conn, field_count),
MYSQLND_METHOD(mysqlnd_conn, set_server_option),
MYSQLND_METHOD(mysqlnd_conn, set_client_option),
MYSQLND_METHOD(mysqlnd_conn, free_contents),
MYSQLND_METHOD(mysqlnd_conn, close),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, get_reference),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, free_reference),
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_init */
PHPAPI MYSQLND *mysqlnd_init(zend_bool persistent)
{
MYSQLND *ret = pecalloc(1, sizeof(MYSQLND), persistent);
SET_ERROR_AFF_ROWS(ret);
ret->persistent = persistent;
ret->m = & mysqlnd_mysqlnd_conn_methods;
ret->m->get_reference(ret);
return ret;
}
/* }}} */
/* {{{ mysqlnd_functions[]
*
* Every user visible function must have an entry in mysqlnd_functions[].
*/
static const zend_function_entry mysqlnd_functions[] = {
{NULL, NULL, NULL} /* Must be the last line in mysqlnd_functions[] */
};
/* }}} */
/* {{{ mysqlnd_minfo_print_hash */
#if PHP_MAJOR_VERSION >= 6
PHPAPI void mysqlnd_minfo_print_hash(zval *values)
{
zval **values_entry;
HashPosition pos_values;
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos_values);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(values),
(void **)&values_entry, &pos_values) == SUCCESS) {
TSRMLS_FETCH();
zstr string_key;
uint string_key_len;
ulong num_key;
char *s = NULL;
zend_hash_get_current_key_ex(Z_ARRVAL_P(values), &string_key, &string_key_len, &num_key, 0, &pos_values);
convert_to_string(*values_entry);
if (UG(unicode)) {
int s_len;
if (zend_unicode_to_string(ZEND_U_CONVERTER(UG(runtime_encoding_conv)),
&s, &s_len, string_key.u, string_key_len TSRMLS_CC) == SUCCESS) {
php_info_print_table_row(2, s, Z_STRVAL_PP(values_entry));
}
if (s) {
efree(s);
}
} else {
php_info_print_table_row(2, string_key.s, Z_STRVAL_PP(values_entry));
}
zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos_values);
}
}
#else
void mysqlnd_minfo_print_hash(zval *values)
{
zval **values_entry;
HashPosition pos_values;
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos_values);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&values_entry, &pos_values) == SUCCESS) {
char *string_key;
uint string_key_len;
ulong num_key;
zend_hash_get_current_key_ex(Z_ARRVAL_P(values), &string_key, &string_key_len, &num_key, 0, &pos_values);
convert_to_string(*values_entry);
php_info_print_table_row(2, string_key, Z_STRVAL_PP(values_entry));
zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos_values);
}
}
#endif
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mysqlnd)
{
zval values;
php_info_print_table_start();
php_info_print_table_header(2, "mysqlnd", "enabled");
php_info_print_table_row(2, "Version", mysqlnd_get_client_info());
/* Print client stats */
php_info_print_table_header(2, "Client statistics", "");
mysqlnd_get_client_stats(&values);
mysqlnd_minfo_print_hash(&values);
zval_dtor(&values);
php_info_print_table_end();
}
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(mysqlnd)
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(mysqlnd)
{
mysqlnd_globals->collect_statistics = FALSE;
}
/* }}} */
/* {{{ PHP_INI_BEGIN
*/
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("mysqlnd.collect_statistics", "1", PHP_INI_SYSTEM, OnUpdateBool, collect_statistics, zend_mysqlnd_globals, mysqlnd_globals)
PHP_INI_END()
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
static PHP_MINIT_FUNCTION(mysqlnd)
{
REGISTER_INI_ENTRIES();
mysqlnd_library_init(MYSQLND_G(collect_statistics));
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
static PHP_MSHUTDOWN_FUNCTION(mysqlnd)
{
mysqlnd_library_end();
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ mysqlnd_module_entry
*/
zend_module_entry mysqlnd_module_entry = {
STANDARD_MODULE_HEADER,
"mysqlnd",
mysqlnd_functions,
PHP_MINIT(mysqlnd),
PHP_MSHUTDOWN(mysqlnd),
NULL,
NULL,
PHP_MINFO(mysqlnd),
MYSQLND_VERSION,
PHP_MODULE_GLOBALS(mysqlnd),
PHP_GINIT(mysqlnd),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
|