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
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
|
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2016 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: Andrey Hristov <andrey@mysql.com> |
| Ulf Wendel <uwendel@mysql.com> |
| Georg Richter <georg@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 "mysqlnd_debug.h"
#include "zend_smart_str.h"
/*
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;
PHPAPI const char * const mysqlnd_old_passwd = "mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. "
"Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will "
"store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords "
"flag from your my.cnf file";
PHPAPI const char * const mysqlnd_server_gone = "MySQL server has gone away";
PHPAPI const char * const mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
PHPAPI const char * const mysqlnd_out_of_memory = "Out of memory";
PHPAPI MYSQLND_STATS *mysqlnd_global_stats = NULL;
/* {{{ mysqlnd_conn_data::free_options */
static void
MYSQLND_METHOD(mysqlnd_conn_data, free_options)(MYSQLND_CONN_DATA * conn)
{
zend_bool pers = conn->persistent;
if (conn->options->charset_name) {
mnd_pefree(conn->options->charset_name, pers);
conn->options->charset_name = NULL;
}
if (conn->options->auth_protocol) {
mnd_pefree(conn->options->auth_protocol, pers);
conn->options->auth_protocol = NULL;
}
if (conn->options->num_commands) {
unsigned int i;
for (i = 0; i < conn->options->num_commands; i++) {
/* allocated with pestrdup */
mnd_pefree(conn->options->init_commands[i], pers);
}
mnd_pefree(conn->options->init_commands, pers);
conn->options->init_commands = NULL;
}
if (conn->options->cfg_file) {
mnd_pefree(conn->options->cfg_file, pers);
conn->options->cfg_file = NULL;
}
if (conn->options->cfg_section) {
mnd_pefree(conn->options->cfg_section, pers);
conn->options->cfg_section = NULL;
}
if (conn->options->connect_attr) {
zend_hash_destroy(conn->options->connect_attr);
mnd_pefree(conn->options->connect_attr, pers);
conn->options->connect_attr = NULL;
}
}
/* }}} */
/* {{{ mysqlnd_conn_data::free_contents */
static void
MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
{
zend_bool pers = conn->persistent;
DBG_ENTER("mysqlnd_conn_data::free_contents");
if (conn->current_result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
conn->current_result = NULL;
}
if (conn->net) {
conn->net->data->m.free_contents(conn->net);
}
DBG_INF("Freeing memory of members");
if (conn->host) {
mnd_pefree(conn->host, pers);
conn->host = NULL;
}
if (conn->user) {
mnd_pefree(conn->user, pers);
conn->user = NULL;
}
if (conn->passwd) {
mnd_pefree(conn->passwd, pers);
conn->passwd = NULL;
}
if (conn->connect_or_select_db) {
mnd_pefree(conn->connect_or_select_db, pers);
conn->connect_or_select_db = NULL;
}
if (conn->unix_socket) {
mnd_pefree(conn->unix_socket, pers);
conn->unix_socket = NULL;
}
DBG_INF_FMT("scheme=%s", conn->scheme);
if (conn->scheme) {
mnd_pefree(conn->scheme, pers);
conn->scheme = NULL;
}
if (conn->server_version) {
mnd_pefree(conn->server_version, pers);
conn->server_version = NULL;
}
if (conn->host_info) {
mnd_pefree(conn->host_info, pers);
conn->host_info = NULL;
}
if (conn->auth_plugin_data) {
mnd_pefree(conn->auth_plugin_data, pers);
conn->auth_plugin_data = NULL;
}
if (conn->last_message) {
mnd_pefree(conn->last_message, pers);
conn->last_message = NULL;
}
if (conn->error_info->error_list) {
zend_llist_clean(conn->error_info->error_list);
mnd_pefree(conn->error_info->error_list, pers);
conn->error_info->error_list = NULL;
}
conn->charset = NULL;
conn->greet_charset = NULL;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::dtor */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::dtor");
DBG_INF_FMT("conn=%llu", conn->thread_id);
conn->m->free_contents(conn);
conn->m->free_options(conn);
if (conn->net) {
mysqlnd_net_free(conn->net, conn->stats, conn->error_info);
conn->net = NULL;
}
if (conn->protocol) {
mysqlnd_protocol_free(conn->protocol);
conn->protocol = NULL;
}
if (conn->stats) {
mysqlnd_stats_end(conn->stats, conn->persistent);
}
mnd_pefree(conn, conn->persistent);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::simple_command_handle_response */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, simple_command_handle_response)(MYSQLND_CONN_DATA * conn, enum mysqlnd_packet_type ok_packet,
zend_bool silent, enum php_mysqlnd_server_command command,
zend_bool ignore_upsert_status)
{
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::simple_command_handle_response");
DBG_INF_FMT("silent=%u packet=%u command=%s", silent, ok_packet, mysqlnd_command_to_text[command]);
switch (ok_packet) {
case PROT_OK_PACKET:{
MYSQLND_PACKET_OK * ok_response = conn->protocol->m.get_ok_packet(conn->protocol, FALSE);
if (!ok_response) {
SET_OOM_ERROR(*conn->error_info);
break;
}
if (FAIL == (ret = PACKET_READ(ok_response, conn))) {
if (!silent) {
DBG_ERR_FMT("Error while reading %s's OK packet", mysqlnd_command_to_text[command]);
php_error_docref(NULL, E_WARNING, "Error while reading %s's OK packet. PID=%u",
mysqlnd_command_to_text[command], getpid());
}
} else {
DBG_INF_FMT("OK from server");
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);
if (!ignore_upsert_status) {
memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
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(ok_response);
break;
}
case PROT_EOF_PACKET:{
MYSQLND_PACKET_EOF * ok_response = conn->protocol->m.get_eof_packet(conn->protocol, FALSE);
if (!ok_response) {
SET_OOM_ERROR(*conn->error_info);
break;
}
if (FAIL == (ret = PACKET_READ(ok_response, conn))) {
SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE,
"Malformed packet");
if (!silent) {
DBG_ERR_FMT("Error while reading %s's EOF packet", mysqlnd_command_to_text[command]);
php_error_docref(NULL, E_WARNING, "Error while reading %s's EOF packet. PID=%d",
mysqlnd_command_to_text[command], getpid());
}
} 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) {
DBG_ERR_FMT("EOF packet expected, field count wasn't 0xFE but 0x%2X", ok_response->field_count);
php_error_docref(NULL, E_WARNING, "EOF packet expected, field count wasn't 0xFE but 0x%2X",
ok_response->field_count);
}
} else {
DBG_INF_FMT("OK from server");
}
PACKET_FREE(ok_response);
break;
}
default:
SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "Malformed packet");
php_error_docref(NULL, E_ERROR, "Wrong response packet %u passed to the function", ok_packet);
break;
}
DBG_INF(ret == PASS ? "PASS":"FAIL");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::simple_command_send_request */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, simple_command_send_request)(MYSQLND_CONN_DATA * conn, enum php_mysqlnd_server_command command,
const zend_uchar * const arg, size_t arg_len, zend_bool silent, zend_bool ignore_upsert_status)
{
enum_func_status ret = PASS;
MYSQLND_PACKET_COMMAND * cmd_packet;
DBG_ENTER("mysqlnd_conn_data::simple_command_send_request");
DBG_INF_FMT("command=%s silent=%u", mysqlnd_command_to_text[command], silent);
DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
DBG_INF_FMT("sending %u bytes", arg_len + 1); /* + 1 is for the command */
switch (CONN_GET_STATE(conn)) {
case CONN_READY:
break;
case CONN_QUIT_SENT:
SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
DBG_ERR("Server is gone");
DBG_RETURN(FAIL);
default:
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR_FMT("Command out of sync. State=%u", CONN_GET_STATE(conn));
DBG_RETURN(FAIL);
}
SET_ERROR_AFF_ROWS(conn);
SET_EMPTY_ERROR(*conn->error_info);
cmd_packet = conn->protocol->m.get_command_packet(conn->protocol, FALSE);
if (!cmd_packet) {
SET_OOM_ERROR(*conn->error_info);
DBG_RETURN(FAIL);
}
cmd_packet->command = command;
if (arg && arg_len) {
cmd_packet->argument = arg;
cmd_packet->arg_len = arg_len;
}
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_COM_QUIT + command - 1 /* because of COM_SLEEP */ );
if (! PACKET_WRITE(cmd_packet, conn)) {
if (!silent) {
DBG_ERR_FMT("Error while sending %s packet", mysqlnd_command_to_text[command]);
php_error(E_WARNING, "Error while sending %s packet. PID=%d", mysqlnd_command_to_text[command], getpid());
}
CONN_SET_STATE(conn, CONN_QUIT_SENT);
conn->m->send_close(conn);
DBG_ERR("Server is gone");
ret = FAIL;
}
PACKET_FREE(cmd_packet);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::simple_command */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, simple_command)(MYSQLND_CONN_DATA * conn, enum php_mysqlnd_server_command command,
const zend_uchar * const arg, size_t arg_len, enum mysqlnd_packet_type ok_packet, zend_bool silent,
zend_bool ignore_upsert_status)
{
enum_func_status ret;
DBG_ENTER("mysqlnd_conn_data::simple_command");
ret = conn->m->simple_command_send_request(conn, command, arg, arg_len, silent, ignore_upsert_status);
if (PASS == ret && ok_packet != PROT_LAST) {
ret = conn->m->simple_command_handle_response(conn, ok_packet, silent, command, ignore_upsert_status);
}
DBG_INF(ret == PASS ? "PASS":"FAIL");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_server_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const conn, enum_mysqlnd_server_option option)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_server_option);
zend_uchar buffer[2];
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::set_server_option");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
int2store(buffer, (unsigned int) option);
ret = conn->m->simple_command(conn, COM_SET_OPTION, buffer, sizeof(buffer), PROT_EOF_PACKET, FALSE, TRUE);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::restart_psession */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::restart_psession");
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
/* Free here what should not be seen by the next script */
if (conn->last_message) {
mnd_pefree(conn->last_message, conn->persistent);
conn->last_message = NULL;
}
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_conn_data::end_psession */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::end_psession");
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_switch_to_ssl_if_needed */
static enum_func_status
mysqlnd_switch_to_ssl_if_needed(
MYSQLND_CONN_DATA * conn,
const MYSQLND_PACKET_GREET * const greet_packet,
const MYSQLND_OPTIONS * const options,
zend_ulong mysql_flags)
{
enum_func_status ret = FAIL;
const MYSQLND_CHARSET * charset;
MYSQLND_PACKET_AUTH * auth_packet;
DBG_ENTER("mysqlnd_switch_to_ssl_if_needed");
DBG_INF_FMT("client_capability_flags=%lu", mysql_flags);
DBG_INF_FMT("CLIENT_LONG_PASSWORD= %d", mysql_flags & CLIENT_LONG_PASSWORD? 1:0);
DBG_INF_FMT("CLIENT_FOUND_ROWS= %d", mysql_flags & CLIENT_FOUND_ROWS? 1:0);
DBG_INF_FMT("CLIENT_LONG_FLAG= %d", mysql_flags & CLIENT_LONG_FLAG? 1:0);
DBG_INF_FMT("CLIENT_NO_SCHEMA= %d", mysql_flags & CLIENT_NO_SCHEMA? 1:0);
DBG_INF_FMT("CLIENT_COMPRESS= %d", mysql_flags & CLIENT_COMPRESS? 1:0);
DBG_INF_FMT("CLIENT_ODBC= %d", mysql_flags & CLIENT_ODBC? 1:0);
DBG_INF_FMT("CLIENT_LOCAL_FILES= %d", mysql_flags & CLIENT_LOCAL_FILES? 1:0);
DBG_INF_FMT("CLIENT_IGNORE_SPACE= %d", mysql_flags & CLIENT_IGNORE_SPACE? 1:0);
DBG_INF_FMT("CLIENT_PROTOCOL_41= %d", mysql_flags & CLIENT_PROTOCOL_41? 1:0);
DBG_INF_FMT("CLIENT_INTERACTIVE= %d", mysql_flags & CLIENT_INTERACTIVE? 1:0);
DBG_INF_FMT("CLIENT_SSL= %d", mysql_flags & CLIENT_SSL? 1:0);
DBG_INF_FMT("CLIENT_IGNORE_SIGPIPE= %d", mysql_flags & CLIENT_IGNORE_SIGPIPE? 1:0);
DBG_INF_FMT("CLIENT_TRANSACTIONS= %d", mysql_flags & CLIENT_TRANSACTIONS? 1:0);
DBG_INF_FMT("CLIENT_RESERVED= %d", mysql_flags & CLIENT_RESERVED? 1:0);
DBG_INF_FMT("CLIENT_SECURE_CONNECTION=%d", mysql_flags & CLIENT_SECURE_CONNECTION? 1:0);
DBG_INF_FMT("CLIENT_MULTI_STATEMENTS=%d", mysql_flags & CLIENT_MULTI_STATEMENTS? 1:0);
DBG_INF_FMT("CLIENT_MULTI_RESULTS= %d", mysql_flags & CLIENT_MULTI_RESULTS? 1:0);
DBG_INF_FMT("CLIENT_PS_MULTI_RESULTS=%d", mysql_flags & CLIENT_PS_MULTI_RESULTS? 1:0);
DBG_INF_FMT("CLIENT_CONNECT_ATTRS= %d", mysql_flags & CLIENT_PLUGIN_AUTH? 1:0);
DBG_INF_FMT("CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA= %d", mysql_flags & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA? 1:0);
DBG_INF_FMT("CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS= %d", mysql_flags & CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS? 1:0);
DBG_INF_FMT("CLIENT_SESSION_TRACK= %d", mysql_flags & CLIENT_SESSION_TRACK? 1:0);
DBG_INF_FMT("CLIENT_SSL_DONT_VERIFY_SERVER_CERT= %d", mysql_flags & CLIENT_SSL_DONT_VERIFY_SERVER_CERT? 1:0);
DBG_INF_FMT("CLIENT_SSL_VERIFY_SERVER_CERT= %d", mysql_flags & CLIENT_SSL_VERIFY_SERVER_CERT? 1:0);
DBG_INF_FMT("CLIENT_REMEMBER_OPTIONS= %d", mysql_flags & CLIENT_REMEMBER_OPTIONS? 1:0);
auth_packet = conn->protocol->m.get_auth_packet(conn->protocol, FALSE);
if (!auth_packet) {
SET_OOM_ERROR(*conn->error_info);
goto end;
}
auth_packet->client_flags = mysql_flags;
auth_packet->max_packet_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
if (options->charset_name && (charset = mysqlnd_find_charset_name(options->charset_name))) {
auth_packet->charset_no = charset->nr;
} else {
auth_packet->charset_no = greet_packet->charset_no;
}
#ifdef MYSQLND_SSL_SUPPORTED
if (mysql_flags & CLIENT_SSL) {
zend_bool server_has_ssl = (greet_packet->server_capabilities & CLIENT_SSL)? TRUE:FALSE;
if (server_has_ssl == FALSE) {
goto close_conn;
} else {
enum mysqlnd_ssl_peer verify = mysql_flags & CLIENT_SSL_VERIFY_SERVER_CERT?
MYSQLND_SSL_PEER_VERIFY:
(mysql_flags & CLIENT_SSL_DONT_VERIFY_SERVER_CERT?
MYSQLND_SSL_PEER_DONT_VERIFY:
MYSQLND_SSL_PEER_DEFAULT);
DBG_INF("Switching to SSL");
if (!PACKET_WRITE(auth_packet, conn)) {
goto close_conn;
}
conn->net->data->m.set_client_option(conn->net, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char *) &verify);
if (FAIL == conn->net->data->m.enable_ssl(conn->net)) {
goto end;
}
}
}
#else
auth_packet->client_flags &= ~CLIENT_SSL;
if (!PACKET_WRITE(auth_packet, conn)) {
goto close_conn;
}
#endif
ret = PASS;
end:
PACKET_FREE(auth_packet);
DBG_RETURN(ret);
close_conn:
CONN_SET_STATE(conn, CONN_QUIT_SENT);
conn->m->send_close(conn);
SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
PACKET_FREE(auth_packet);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::fetch_auth_plugin_by_name */
static struct st_mysqlnd_authentication_plugin *
MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name)(const char * const requested_protocol)
{
struct st_mysqlnd_authentication_plugin * auth_plugin;
char * plugin_name = NULL;
DBG_ENTER("mysqlnd_conn_data::fetch_auth_plugin_by_name");
mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
DBG_INF_FMT("looking for %s auth plugin", plugin_name);
auth_plugin = mysqlnd_plugin_find(plugin_name);
mnd_sprintf_free(plugin_name);
DBG_RETURN(auth_plugin);
}
/* }}} */
/* {{{ mysqlnd_run_authentication */
static enum_func_status
mysqlnd_run_authentication(
MYSQLND_CONN_DATA * conn,
const char * const user,
const char * const passwd,
const size_t passwd_len,
const char * const db,
const size_t db_len,
const zend_uchar * const auth_plugin_data,
const size_t auth_plugin_data_len,
const char * const auth_protocol,
unsigned int charset_no,
const MYSQLND_OPTIONS * const options,
zend_ulong mysql_flags,
zend_bool silent,
zend_bool is_change_user
)
{
enum_func_status ret = FAIL;
zend_bool first_call = TRUE;
char * switch_to_auth_protocol = NULL;
size_t switch_to_auth_protocol_len = 0;
char * requested_protocol = NULL;
zend_uchar * plugin_data;
size_t plugin_data_len;
DBG_ENTER("mysqlnd_run_authentication");
plugin_data_len = auth_plugin_data_len;
plugin_data = mnd_emalloc(plugin_data_len + 1);
if (!plugin_data) {
goto end;
}
memcpy(plugin_data, auth_plugin_data, plugin_data_len);
plugin_data[plugin_data_len] = '\0';
requested_protocol = mnd_pestrdup(auth_protocol? auth_protocol : MYSQLND_DEFAULT_AUTH_PROTOCOL, FALSE);
if (!requested_protocol) {
goto end;
}
do {
struct st_mysqlnd_authentication_plugin * auth_plugin = conn->m->fetch_auth_plugin_by_name(requested_protocol);
if (!auth_plugin) {
php_error_docref(NULL, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "The server requested authentication method unknown to the client");
goto end;
}
DBG_INF("plugin found");
{
zend_uchar * switch_to_auth_protocol_data = NULL;
size_t switch_to_auth_protocol_data_len = 0;
zend_uchar * scrambled_data = NULL;
size_t scrambled_data_len = 0;
switch_to_auth_protocol = NULL;
switch_to_auth_protocol_len = 0;
if (conn->auth_plugin_data) {
mnd_pefree(conn->auth_plugin_data, conn->persistent);
conn->auth_plugin_data = NULL;
}
conn->auth_plugin_data_len = plugin_data_len;
conn->auth_plugin_data = mnd_pemalloc(conn->auth_plugin_data_len, conn->persistent);
if (!conn->auth_plugin_data) {
SET_OOM_ERROR(*conn->error_info);
goto end;
}
memcpy(conn->auth_plugin_data, plugin_data, plugin_data_len);
DBG_INF_FMT("salt(%d)=[%.*s]", plugin_data_len, plugin_data_len, plugin_data);
/* The data should be allocated with malloc() */
scrambled_data =
auth_plugin->methods.get_auth_data(NULL, &scrambled_data_len, conn, user, passwd, passwd_len,
plugin_data, plugin_data_len, options, &conn->net->data->options, mysql_flags);
if (conn->error_info->error_no) {
goto end;
}
if (FALSE == is_change_user) {
ret = mysqlnd_auth_handshake(conn, user, passwd, passwd_len, db, db_len, options, mysql_flags,
charset_no,
first_call,
requested_protocol,
scrambled_data, scrambled_data_len,
&switch_to_auth_protocol, &switch_to_auth_protocol_len,
&switch_to_auth_protocol_data, &switch_to_auth_protocol_data_len
);
} else {
ret = mysqlnd_auth_change_user(conn, user, strlen(user), passwd, passwd_len, db, db_len, silent,
first_call,
requested_protocol,
scrambled_data, scrambled_data_len,
&switch_to_auth_protocol, &switch_to_auth_protocol_len,
&switch_to_auth_protocol_data, &switch_to_auth_protocol_data_len
);
}
first_call = FALSE;
free(scrambled_data);
DBG_INF_FMT("switch_to_auth_protocol=%s", switch_to_auth_protocol? switch_to_auth_protocol:"n/a");
if (requested_protocol && switch_to_auth_protocol) {
mnd_efree(requested_protocol);
requested_protocol = switch_to_auth_protocol;
}
if (plugin_data) {
mnd_efree(plugin_data);
}
plugin_data_len = switch_to_auth_protocol_data_len;
plugin_data = switch_to_auth_protocol_data;
}
DBG_INF_FMT("conn->error_info->error_no = %d", conn->error_info->error_no);
} while (ret == FAIL && conn->error_info->error_no == 0 && switch_to_auth_protocol != NULL);
if (ret == PASS) {
DBG_INF_FMT("saving requested_protocol=%s", requested_protocol);
conn->m->set_client_option(conn, MYSQLND_OPT_AUTH_PROTOCOL, requested_protocol);
}
end:
if (plugin_data) {
mnd_efree(plugin_data);
}
if (requested_protocol) {
mnd_efree(requested_protocol);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_connect_run_authentication */
static enum_func_status
mysqlnd_connect_run_authentication(
MYSQLND_CONN_DATA * conn,
const char * const user,
const char * const passwd,
const char * const db,
size_t db_len,
size_t passwd_len,
const MYSQLND_PACKET_GREET * const greet_packet,
const MYSQLND_OPTIONS * const options,
zend_ulong mysql_flags
)
{
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_connect_run_authentication");
ret = mysqlnd_switch_to_ssl_if_needed(conn, greet_packet, options, mysql_flags);
if (PASS == ret) {
ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, db_len,
greet_packet->auth_plugin_data, greet_packet->auth_plugin_data_len, greet_packet->auth_protocol,
greet_packet->charset_no, options, mysql_flags, FALSE /*silent*/, FALSE/*is_change*/);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::execute_init_commands */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands)(MYSQLND_CONN_DATA * conn)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::execute_init_commands");
if (conn->options->init_commands) {
unsigned int current_command = 0;
for (; current_command < conn->options->num_commands; ++current_command) {
const char * const command = conn->options->init_commands[current_command];
if (command) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
if (PASS != conn->m->query(conn, command, strlen(command))) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
ret = FAIL;
break;
}
if (conn->last_query_type == QUERY_SELECT) {
MYSQLND_RES * result = conn->m->use_result(conn, 0);
if (result) {
result->m.free_result(result, TRUE);
}
}
}
}
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_updated_connect_flags */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags)
{
MYSQLND_NET * net = conn->net;
DBG_ENTER("mysqlnd_conn_data::get_updated_connect_flags");
/* we allow load data local infile by default */
mysql_flags |= MYSQLND_CAPABILITIES;
mysql_flags |= conn->options->flags; /* use the flags from set_client_option() */
#ifndef MYSQLND_COMPRESSION_ENABLED
if (mysql_flags & CLIENT_COMPRESS) {
mysql_flags &= ~CLIENT_COMPRESS;
}
#else
if (net && net->data->options.flags & MYSQLND_NET_FLAG_USE_COMPRESSION) {
mysql_flags |= CLIENT_COMPRESS;
}
#endif
#ifndef MYSQLND_SSL_SUPPORTED
if (mysql_flags & CLIENT_SSL) {
mysql_flags &= ~CLIENT_SSL;
}
#else
if (net && (net->data->options.ssl_key || net->data->options.ssl_cert ||
net->data->options.ssl_ca || net->data->options.ssl_capath || net->data->options.ssl_cipher))
{
mysql_flags |= CLIENT_SSL;
}
#endif
DBG_RETURN(mysql_flags);
}
/* }}} */
/* {{{ mysqlnd_conn_data::connect_handshake */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
const char * const host, const char * const user,
const char * const passwd, const unsigned int passwd_len,
const char * const db, const unsigned int db_len,
const unsigned int mysql_flags)
{
MYSQLND_PACKET_GREET * greet_packet;
MYSQLND_NET * net = conn->net;
DBG_ENTER("mysqlnd_conn_data::connect_handshake");
greet_packet = conn->protocol->m.get_greet_packet(conn->protocol, FALSE);
if (!greet_packet) {
SET_OOM_ERROR(*conn->error_info);
DBG_RETURN(FAIL); /* OOM */
}
if (FAIL == net->data->m.connect_ex(conn->net, conn->scheme, conn->scheme_len, conn->persistent,
conn->stats, conn->error_info))
{
goto err;
}
DBG_INF_FMT("stream=%p", net->data->m.get_stream(net));
if (FAIL == PACKET_READ(greet_packet, conn)) {
DBG_ERR("Error while reading greeting packet");
php_error_docref(NULL, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
goto err;
} else if (greet_packet->error_no) {
DBG_ERR_FMT("errorno=%u error=%s", greet_packet->error_no, greet_packet->error);
SET_CLIENT_ERROR(*conn->error_info, greet_packet->error_no, greet_packet->sqlstate, greet_packet->error);
goto err;
} else if (greet_packet->pre41) {
DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet->server_version);
php_error_docref(NULL, 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 = mnd_pestrdup(greet_packet->server_version, conn->persistent);
conn->greet_charset = mysqlnd_find_charset_nr(greet_packet->charset_no);
if (!conn->greet_charset) {
php_error_docref(NULL, E_WARNING,
"Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet->charset_no);
SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
"Server sent charset unknown to the client. Please, report to the developers");
goto err;
}
conn->client_flag = mysql_flags;
conn->server_capabilities = greet_packet->server_capabilities;
if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len,
greet_packet, conn->options, mysql_flags))
{
goto err;
}
memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
conn->upsert_status->warning_count = 0;
conn->upsert_status->server_status = greet_packet->server_status;
conn->upsert_status->affected_rows = 0;
PACKET_FREE(greet_packet);
DBG_RETURN(PASS);
err:
conn->client_flag = 0;
conn->server_capabilities = 0;
PACKET_FREE(greet_packet);
DBG_RETURN(FAIL);
}
/* }}} */
/* {{{ mysqlnd_conn_data::connect */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
const char *host, const char *user,
const char *passwd, unsigned int passwd_len,
const char *db, unsigned int db_len,
unsigned int port,
const char *socket_or_pipe,
unsigned int mysql_flags
)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, connect);
size_t host_len;
zend_bool unix_socket = FALSE;
zend_bool named_pipe = FALSE;
zend_bool reconnect = FALSE;
zend_bool saved_compression = FALSE;
zend_bool local_tx_started = FALSE;
MYSQLND_NET * net = conn->net;
DBG_ENTER("mysqlnd_conn_data::connect");
DBG_INF_FMT("conn=%p", conn);
if (PASS != conn->m->local_tx_start(conn, this_func)) {
goto err;
}
local_tx_started = TRUE;
SET_EMPTY_ERROR(*conn->error_info);
SET_ERROR_AFF_ROWS(conn);
DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
host?host:"", user?user:"", db?db:"", port, mysql_flags,
conn? conn->persistent:0, conn? CONN_GET_STATE(conn):-1);
if (CONN_GET_STATE(conn) > CONN_ALLOCED && CONN_GET_STATE(conn) ) {
DBG_INF("Connecting on a connected handle.");
if (CONN_GET_STATE(conn) < CONN_QUIT_SENT) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
reconnect = TRUE;
conn->m->send_close(conn);
}
conn->m->free_contents(conn);
MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_CONNECTIONS);
if (conn->persistent) {
MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_PERSISTENT_CONNECTIONS);
}
/* Now reconnect using the same handle */
if (net->data->compressed) {
/*
we need to save the state. As we will re-connect, net->compressed should be off, or
we will look for a compression header as part of the greet message, but there will
be none.
*/
saved_compression = TRUE;
net->data->compressed = FALSE;
}
if (net->data->ssl) {
net->data->ssl = FALSE;
}
} else {
unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size);
}
if (!host || !host[0]) {
host = "localhost";
}
if (!user) {
DBG_INF_FMT("no user given, using empty string");
user = "";
}
if (!passwd) {
DBG_INF_FMT("no password given, using empty string");
passwd = "";
passwd_len = 0;
}
if (!db) {
DBG_INF_FMT("no db given, using empty string");
db = "";
db_len = 0;
} else {
mysql_flags |= CLIENT_CONNECT_WITH_DB;
}
host_len = strlen(host);
{
char * transport = NULL;
int transport_len;
#ifndef PHP_WIN32
if (host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
DBG_INF_FMT("socket=%s", socket_or_pipe? socket_or_pipe:"n/a");
if (!socket_or_pipe) {
socket_or_pipe = "/tmp/mysql.sock";
}
transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
unix_socket = TRUE;
#else
if (host_len == sizeof(".") - 1 && host[0] == '.') {
/* named pipe in socket */
if (!socket_or_pipe) {
socket_or_pipe = "\\\\.\\pipe\\MySQL";
}
transport_len = mnd_sprintf(&transport, 0, "pipe://%s", socket_or_pipe);
named_pipe = TRUE;
#endif
} else {
if (!port) {
port = 3306;
}
transport_len = mnd_sprintf(&transport, 0, "tcp://%s:%u", host, port);
}
if (!transport) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
DBG_INF_FMT("transport=%s conn->scheme=%s", transport, conn->scheme);
conn->scheme = mnd_pestrndup(transport, transport_len, conn->persistent);
conn->scheme_len = transport_len;
mnd_sprintf_free(transport);
transport = NULL;
if (!conn->scheme) {
goto err; /* OOM */
}
}
mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags);
if (FAIL == conn->m->connect_handshake(conn, host, user, passwd, passwd_len, db, db_len, mysql_flags)) {
goto err;
}
{
CONN_SET_STATE(conn, CONN_READY);
if (saved_compression) {
net->data->compressed = TRUE;
}
/*
If a connect on a existing handle is performed and mysql_flags is
passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
which we set based on saved_compression.
*/
net->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
conn->user_len = strlen(user);
conn->user = mnd_pestrndup(user, conn->user_len, conn->persistent);
conn->passwd = mnd_pestrndup(passwd, passwd_len, conn->persistent);
conn->passwd_len = passwd_len;
conn->port = port;
conn->connect_or_select_db = mnd_pestrndup(db, db_len, conn->persistent);
conn->connect_or_select_db_len = db_len;
if (!conn->user || !conn->passwd || !conn->connect_or_select_db) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
if (!unix_socket && !named_pipe) {
conn->host = mnd_pestrndup(host, host_len, conn->persistent);
if (!conn->host) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
conn->host_len = host_len;
{
char *p;
mnd_sprintf(&p, 0, "%s via TCP/IP", conn->host);
if (!p) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
conn->host_info = mnd_pestrdup(p, conn->persistent);
mnd_sprintf_free(p);
if (!conn->host_info) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
}
} else {
conn->unix_socket = mnd_pestrdup(socket_or_pipe, conn->persistent);
if (unix_socket) {
conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
} else if (named_pipe) {
char *p;
mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket);
if (!p) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
conn->host_info = mnd_pestrdup(p, conn->persistent);
mnd_sprintf_free(p);
if (!conn->host_info) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
} else {
php_error_docref(NULL, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
}
if (!conn->unix_socket || !conn->host_info) {
SET_OOM_ERROR(*conn->error_info);
goto err; /* OOM */
}
conn->unix_socket_len = strlen(conn->unix_socket);
}
conn->max_packet_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
/* todo: check if charset is available */
SET_EMPTY_ERROR(*conn->error_info);
mysqlnd_local_infile_default(conn);
if (FAIL == conn->m->execute_init_commands(conn)) {
goto err;
}
MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
if (reconnect) {
MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
}
if (conn->persistent) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
}
DBG_INF_FMT("connection_id=%llu", conn->thread_id);
conn->m->local_tx_end(conn, this_func, PASS);
DBG_RETURN(PASS);
}
err:
DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme);
if (!conn->error_info->error_no) {
SET_CLIENT_ERROR(*conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, conn->error_info->error? conn->error_info->error:"Unknown error");
php_error_docref(NULL, E_WARNING, "[%u] %.128s (trying to connect via %s)",
conn->error_info->error_no, conn->error_info->error, conn->scheme);
}
conn->m->free_contents(conn);
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
if (TRUE == local_tx_started) {
conn->m->local_tx_end(conn, this_func, FAIL);
}
DBG_RETURN(FAIL);
}
/* }}} */
/* {{{ mysqlnd_conn::connect */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
const char * host, const char * user,
const char * passwd, unsigned int passwd_len,
const char * db, unsigned int db_len,
unsigned int port,
const char * socket_or_pipe,
unsigned int mysql_flags
)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, connect);
enum_func_status ret = FAIL;
MYSQLND_CONN_DATA * conn = conn_handle->data;
DBG_ENTER("mysqlnd_conn::connect");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
ret = conn->m->connect(conn, host, user, passwd, passwd_len, db, db_len, port, socket_or_pipe, mysql_flags);
conn->m->local_tx_end(conn, this_func, FAIL);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_connect */
PHPAPI MYSQLND * mysqlnd_connect(MYSQLND * conn_handle,
const char * host, const char * user,
const char * passwd, unsigned int passwd_len,
const char * db, unsigned int db_len,
unsigned int port,
const char * socket_or_pipe,
unsigned int mysql_flags,
unsigned int client_api_flags
)
{
enum_func_status ret = FAIL;
zend_bool self_alloced = FALSE;
DBG_ENTER("mysqlnd_connect");
DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host?host:"", user?user:"", db?db:"", port, mysql_flags);
if (!conn_handle) {
self_alloced = TRUE;
if (!(conn_handle = mysqlnd_init(client_api_flags, FALSE))) {
/* OOM */
DBG_RETURN(NULL);
}
}
ret = conn_handle->m->connect(conn_handle, host, user, passwd, passwd_len, db, db_len, port, socket_or_pipe, mysql_flags);
if (ret == FAIL) {
if (self_alloced) {
/*
We have alloced, thus there are no references to this
object - we are free to kill it!
*/
conn_handle->m->dtor(conn_handle);
}
DBG_RETURN(NULL);
}
DBG_RETURN(conn_handle);
}
/* }}} */
/* {{{ mysqlnd_conn_data::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_data, query)(MYSQLND_CONN_DATA * conn, const char * query, unsigned int query_len)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, query);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::query");
DBG_INF_FMT("conn=%p conn=%llu query=%s", conn, conn->thread_id, query);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
if (PASS == conn->m->send_query(conn, query, query_len, MYSQLND_SEND_QUERY_IMPLICIT, NULL, NULL) &&
PASS == conn->m->reap_query(conn, MYSQLND_REAP_RESULT_IMPLICIT))
{
ret = PASS;
if (conn->last_query_type == QUERY_UPSERT && conn->upsert_status->affected_rows) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, conn->upsert_status->affected_rows);
}
}
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::send_query */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, send_query)(MYSQLND_CONN_DATA * conn, const char * query, unsigned int query_len,
enum_mysqlnd_send_query_type type, zval *read_cb, zval *err_cb)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, send_query);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::send_query");
DBG_INF_FMT("conn=%llu query=%s", conn->thread_id, query);
DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
ret = conn->m->simple_command(conn, COM_QUERY, (zend_uchar *) query, query_len,
PROT_LAST /* we will handle the OK packet*/,
FALSE, FALSE);
if (PASS == ret) {
CONN_SET_STATE(conn, CONN_QUERY_SENT);
}
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::reap_query */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn, enum_mysqlnd_reap_result_type type)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, reap_query);
enum_mysqlnd_connection_state state = CONN_GET_STATE(conn);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::reap_query");
DBG_INF_FMT("conn=%llu", conn->thread_id);
DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
if (state <= CONN_READY || state == CONN_QUIT_SENT) {
php_error_docref(NULL, E_WARNING, "Connection not opened, clear or has been closed");
DBG_ERR_FMT("Connection not opened, clear or has been closed. State=%u", state);
DBG_RETURN(ret);
}
ret = conn->m->query_read_result_set_header(conn, NULL);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
DBG_RETURN(ret);
}
/* }}} */
#include "php_network.h"
/* {{{ mysqlnd_stream_array_to_fd_set */
MYSQLND ** mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
{
int cnt = 0;
MYSQLND **p = conn_array, **p_p;
MYSQLND **ret = NULL;
while (*p) {
if (CONN_GET_STATE((*p)->data) <= CONN_READY || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT) {
cnt++;
}
p++;
}
if (cnt) {
MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
p_p = p = conn_array;
while (*p) {
if (CONN_GET_STATE((*p)->data) <= CONN_READY || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT) {
*ret_p = *p;
*p = NULL;
ret_p++;
} else {
*p_p = *p;
p_p++;
}
p++;
}
*ret_p = NULL;
}
return ret;
}
/* }}} */
/* {{{ mysqlnd_stream_array_to_fd_set */
static int mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
{
php_socket_t this_fd;
php_stream *stream = NULL;
unsigned int cnt = 0;
MYSQLND **p = conn_array;
DBG_ENTER("mysqlnd_stream_array_to_fd_set");
while (*p) {
/* get the fd.
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
* when casting. It is only used here so that the buffered data warning
* is not displayed.
* */
stream = (*p)->data->net->data->m.get_stream((*p)->data->net);
DBG_INF_FMT("conn=%llu stream=%p", (*p)->data->thread_id, stream);
if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
(void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
PHP_SAFE_FD_SET(this_fd, fds);
if (this_fd > *max_fd) {
*max_fd = this_fd;
}
cnt++;
}
p++;
}
DBG_RETURN(cnt ? 1 : 0);
}
/* }}} */
/* {{{ mysqlnd_stream_array_from_fd_set */
static int mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
{
php_socket_t this_fd;
php_stream *stream = NULL;
int ret = 0;
zend_bool disproportion = FALSE;
MYSQLND **fwd = conn_array, **bckwd = conn_array;
DBG_ENTER("mysqlnd_stream_array_from_fd_set");
while (*fwd) {
stream = (*fwd)->data->net->data->m.get_stream((*fwd)->data->net);
DBG_INF_FMT("conn=%llu stream=%p", (*fwd)->data->thread_id, stream);
if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
(void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
if (disproportion) {
*bckwd = *fwd;
}
bckwd++;
fwd++;
ret++;
continue;
}
}
disproportion = TRUE;
fwd++;
}
*bckwd = NULL;/* NULL-terminate the list */
DBG_RETURN(ret);
}
/* }}} */
#ifndef PHP_WIN32
#define php_select(m, r, w, e, t) select(m, r, w, e, t)
#else
#include "win32/select.h"
#endif
/* {{{ mysqlnd_poll */
PHPAPI enum_func_status
mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
{
struct timeval tv;
struct timeval *tv_p = NULL;
fd_set rfds, wfds, efds;
php_socket_t max_fd = 0;
int retval, sets = 0;
int set_count, max_set_count = 0;
DBG_ENTER("_mysqlnd_poll");
if (sec < 0 || usec < 0) {
php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
DBG_RETURN(FAIL);
}
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
if (r_array != NULL) {
*dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
if (set_count > max_set_count) {
max_set_count = set_count;
}
sets += set_count;
}
if (e_array != NULL) {
set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
if (set_count > max_set_count) {
max_set_count = set_count;
}
sets += set_count;
}
if (!sets) {
php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
DBG_RETURN(FAIL);
}
PHP_SAFE_MAX_FD(max_fd, max_set_count);
/* Solaris + BSD do not like microsecond values which are >= 1 sec */
if (usec > 999999) {
tv.tv_sec = sec + (usec / 1000000);
tv.tv_usec = usec % 1000000;
} else {
tv.tv_sec = sec;
tv.tv_usec = usec;
}
tv_p = &tv;
retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
if (retval == -1) {
php_error_docref(NULL, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
errno, strerror(errno), max_fd);
DBG_RETURN(FAIL);
}
if (r_array != NULL) {
mysqlnd_stream_array_from_fd_set(r_array, &rfds);
}
if (e_array != NULL) {
mysqlnd_stream_array_from_fd_set(e_array, &efds);
}
*desc_num = retval;
DBG_RETURN(PASS);
}
/* }}} */
/*
COM_FIELD_LIST is special, different from a SHOW FIELDS FROM :
- There is no result set header - status from the command, which
impacts us to allocate big chunk of memory for reading the metadata.
- The EOF packet is consumed by the metadata packet reader.
*/
/* {{{ mysqlnd_conn_data::list_fields */
MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, list_fields)(MYSQLND_CONN_DATA * conn, const char *table, const char *achtung_wild)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, list_fields);
/* db + \0 + wild + \0 (for wild) */
zend_uchar buff[MYSQLND_MAX_ALLOWED_DB_LEN * 2 + 1 + 1], *p;
size_t table_len, wild_len;
MYSQLND_RES * result = NULL;
DBG_ENTER("mysqlnd_conn_data::list_fields");
DBG_INF_FMT("conn=%llu table=%s wild=%s", conn->thread_id, table? table:"",achtung_wild? achtung_wild:"");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
p = buff;
if (table && (table_len = strlen(table))) {
size_t to_copy = MIN(table_len, MYSQLND_MAX_ALLOWED_DB_LEN);
memcpy(p, table, to_copy);
p += to_copy;
*p++ = '\0';
}
if (achtung_wild && (wild_len = strlen(achtung_wild))) {
size_t to_copy = MIN(wild_len, MYSQLND_MAX_ALLOWED_DB_LEN);
memcpy(p, achtung_wild, to_copy);
p += to_copy;
*p++ = '\0';
}
if (PASS != conn->m->simple_command(conn, COM_FIELD_LIST, buff, p - buff,
PROT_LAST /* we will handle the OK packet*/,
FALSE, TRUE)) {
conn->m->local_tx_end(conn, 0, FAIL);
break;
}
/*
Prepare for the worst case.
MyISAM goes to 2500 BIT columns, double it for safety.
*/
result = conn->m->result_init(5000, conn->persistent);
if (!result) {
break;
}
if (FAIL == result->m.read_result_metadata(result, conn)) {
DBG_ERR("Error occurred while reading metadata");
result->m.free_result(result, TRUE);
result = NULL;
break;
}
result->type = MYSQLND_RES_NORMAL;
result->unbuf = mysqlnd_result_unbuffered_init(result->field_count, FALSE, result->persistent);
if (!result->unbuf) {
/* OOM */
SET_OOM_ERROR(*conn->error_info);
result->m.free_result(result, TRUE);
result = NULL;
break;
}
result->unbuf->eof_reached = TRUE;
} while (0);
conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
}
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::list_method */
MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * query, const char *achtung_wild, char *par1)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, list_method);
char * show_query = NULL;
size_t show_query_len;
MYSQLND_RES * result = NULL;
DBG_ENTER("mysqlnd_conn_data::list_method");
DBG_INF_FMT("conn=%llu query=%s wild=%u", conn->thread_id, query, achtung_wild);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
if (par1) {
if (achtung_wild) {
show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
} else {
show_query_len = mnd_sprintf(&show_query, 0, query, par1);
}
} else {
if (achtung_wild) {
show_query_len = mnd_sprintf(&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)) {
result = conn->m->store_result(conn, MYSQLND_STORE_NO_COPY);
}
if (show_query != query) {
mnd_sprintf_free(show_query);
}
conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
}
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::errno */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, errno)(const MYSQLND_CONN_DATA * const conn)
{
return conn->error_info->error_no;
}
/* }}} */
/* {{{ mysqlnd_conn_data::error */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn)
{
return conn->error_info->error;
}
/* }}} */
/* {{{ mysqlnd_conn_data::sqlstate */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn)
{
return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
}
/* }}} */
/* {{{ mysqlnd_old_escape_string */
PHPAPI zend_ulong
mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len)
{
DBG_ENTER("mysqlnd_old_escape_string");
DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len));
}
/* }}} */
/* {{{ mysqlnd_conn_data::ssl_set */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
const char * const ca, const char * const capath, const char * const cipher)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, ssl_set);
enum_func_status ret = FAIL;
MYSQLND_NET * net = conn->net;
DBG_ENTER("mysqlnd_conn_data::ssl_set");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
ret = (PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_KEY, key) &&
PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CERT, cert) &&
PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CA, ca) &&
PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CAPATH, capath) &&
PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CIPHER, cipher)) ? PASS : FAIL;
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::escape_string */
static zend_ulong
MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, escape_string);
zend_ulong ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::escape_string");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
DBG_INF_FMT("server_status=%u", conn->upsert_status->server_status);
if (conn->upsert_status->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len);
} else {
ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len);
}
conn->m->local_tx_end(conn, this_func, PASS);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::dump_debug_info */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, server_dump_debug_information);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
ret = conn->m->simple_command(conn, COM_DEBUG, NULL, 0, PROT_EOF_PACKET, FALSE, TRUE);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::select_db */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, unsigned int db_len)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, select_db);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::select_db");
DBG_INF_FMT("conn=%llu db=%s", conn->thread_id, db);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
ret = conn->m->simple_command(conn, COM_INIT_DB, (zend_uchar*) db, db_len, PROT_OK_PACKET, FALSE, TRUE);
/*
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);
if (ret == PASS) {
if (conn->connect_or_select_db) {
mnd_pefree(conn->connect_or_select_db, conn->persistent);
}
conn->connect_or_select_db = mnd_pestrndup(db, db_len, conn->persistent);
conn->connect_or_select_db_len = db_len;
if (!conn->connect_or_select_db) {
/* OOM */
SET_OOM_ERROR(*conn->error_info);
ret = FAIL;
}
}
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::ping */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, ping);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::ping");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
ret = conn->m->simple_command(conn, COM_PING, NULL, 0, PROT_OK_PACKET, TRUE, TRUE);
/*
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);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_INF_FMT("ret=%u", ret);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::statistic */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, zend_string **message)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, get_server_statistics);
enum_func_status ret = FAIL;
MYSQLND_PACKET_STATS * stats_header;
DBG_ENTER("mysqlnd_conn_data::statistic");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
ret = conn->m->simple_command(conn, COM_STATISTICS, NULL, 0, PROT_LAST, FALSE, TRUE);
if (FAIL == ret) {
break;
}
stats_header = conn->protocol->m.get_stats_packet(conn->protocol, FALSE);
if (!stats_header) {
SET_OOM_ERROR(*conn->error_info);
break;
}
if (PASS == (ret = PACKET_READ(stats_header, conn))) {
/* will be freed by Zend, thus don't use the mnd_ allocator */
*message = zend_string_init(stats_header->message, stats_header->message_len, 0);
DBG_INF(ZSTR_VAL(*message));
}
PACKET_FREE(stats_header);
} while (0);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::kill */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, kill_connection);
enum_func_status ret = FAIL;
zend_uchar buff[4];
DBG_ENTER("mysqlnd_conn_data::kill");
DBG_INF_FMT("conn=%llu pid=%u", conn->thread_id, pid);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
int4store(buff, pid);
/* If we kill ourselves don't expect OK packet, PROT_LAST will skip it */
if (pid != conn->thread_id) {
ret = conn->m->simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_OK_PACKET, FALSE, TRUE);
/*
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 = conn->m->simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_LAST, FALSE, TRUE))) {
CONN_SET_STATE(conn, CONN_QUIT_SENT);
conn->m->send_close(conn);
}
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_charset */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_charset);
enum_func_status ret = FAIL;
const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
DBG_ENTER("mysqlnd_conn_data::set_charset");
DBG_INF_FMT("conn=%llu cs=%s", conn->thread_id, csname);
if (!charset) {
SET_CLIENT_ERROR(*conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE,
"Invalid characterset or character set not supported");
DBG_RETURN(ret);
}
if (PASS == conn->m->local_tx_start(conn, this_func)) {
char * query;
size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
if (FAIL == (ret = conn->m->query(conn, query, query_len))) {
php_error_docref(NULL, E_WARNING, "Error executing query");
} else if (conn->error_info->error_no) {
ret = FAIL;
} else {
conn->charset = charset;
}
mnd_sprintf_free(query);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::refresh */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, refresh_server);
enum_func_status ret = FAIL;
zend_uchar bits[1];
DBG_ENTER("mysqlnd_conn_data::refresh");
DBG_INF_FMT("conn=%llu options=%lu", conn->thread_id, options);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
int1store(bits, options);
ret = conn->m->simple_command(conn, COM_REFRESH, bits, 1, PROT_OK_PACKET, FALSE, TRUE);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::shutdown */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, shutdown)(MYSQLND_CONN_DATA * const conn, uint8_t level)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, shutdown_server);
enum_func_status ret = FAIL;
zend_uchar bits[1];
DBG_ENTER("mysqlnd_conn_data::shutdown");
DBG_INF_FMT("conn=%llu level=%lu", conn->thread_id, level);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
int1store(bits, level);
ret = conn->m->simple_command(conn, COM_SHUTDOWN, bits, 1, PROT_OK_PACKET, FALSE, TRUE);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_send_close */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn)
{
enum_func_status ret = PASS;
MYSQLND_NET * net = conn->net;
php_stream * net_stream = net->data->m.get_stream(net);
enum mysqlnd_connection_state state;
DBG_ENTER("mysqlnd_send_close");
DBG_INF_FMT("conn=%llu net->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
if (CONN_GET_STATE(conn) >= CONN_READY) {
MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_CONNECTIONS);
if (conn->persistent) {
MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_PERSISTENT_CONNECTIONS);
}
}
state = CONN_GET_STATE(conn);
DBG_INF_FMT("state=%u", state);
switch (state) {
case CONN_READY:
DBG_INF("Connection clean, sending COM_QUIT");
if (net_stream) {
ret = conn->m->simple_command(conn, COM_QUIT, NULL, 0, PROT_LAST, TRUE, TRUE);
net->data->m.close_stream(net, conn->stats, conn->error_info);
}
CONN_SET_STATE(conn, CONN_QUIT_SENT);
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_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme);
/*
Do nothing, the connection will be brutally closed
and the server will catch it and free close from its side.
*/
/* Fall-through */
case CONN_ALLOCED:
/*
Allocated but not connected or there was failure when trying
to connect with pre-allocated connect.
Fall-through
*/
CONN_SET_STATE(conn, CONN_QUIT_SENT);
/* Fall-through */
case CONN_QUIT_SENT:
/* The user has killed its own connection */
net->data->m.close_stream(net, conn->stats, conn->error_info);
break;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_reference */
static MYSQLND_CONN_DATA *
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::get_reference");
++conn->refcount;
DBG_INF_FMT("conn=%llu new_refcount=%u", conn->thread_id, conn->refcount);
DBG_RETURN(conn);
}
/* }}} */
/* {{{ mysqlnd_conn_data::free_reference */
static enum_func_status
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::free_reference");
DBG_INF_FMT("conn=%llu old_refcount=%u", conn->thread_id, conn->refcount);
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.
*/
ret = conn->m->send_close(conn);
conn->m->dtor(conn);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_state */
static enum mysqlnd_connection_state
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_state)(const MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::get_state");
DBG_RETURN(conn->state);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_state */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, set_state)(MYSQLND_CONN_DATA * const conn, enum mysqlnd_connection_state new_state)
{
DBG_ENTER("mysqlnd_conn_data::set_state");
DBG_INF_FMT("New state=%u", new_state);
conn->state = new_state;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::field_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn)
{
return conn->field_count;
}
/* }}} */
/* {{{ mysqlnd_conn_data::server_status */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn)
{
return conn->upsert_status->server_status;
}
/* }}} */
/* {{{ mysqlnd_conn_data::insert_id */
static uint64_t
MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn)
{
return conn->upsert_status->last_insert_id;
}
/* }}} */
/* {{{ mysqlnd_conn_data::affected_rows */
static uint64_t
MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn)
{
return conn->upsert_status->affected_rows;
}
/* }}} */
/* {{{ mysqlnd_conn_data::warning_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn)
{
return conn->upsert_status->warning_count;
}
/* }}} */
/* {{{ mysqlnd_conn_data::info */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->last_message;
}
/* }}} */
/* {{{ mysqlnd_get_client_info */
PHPAPI const char * mysqlnd_get_client_info()
{
return PHP_MYSQLND_VERSION;
}
/* }}} */
/* {{{ mysqlnd_get_client_version */
PHPAPI unsigned int mysqlnd_get_client_version()
{
return MYSQLND_VERSION_ID;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_server_info */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->server_version;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_host_info */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->host_info;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_proto_info */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->protocol_version;
}
/* }}} */
/* {{{ mysqlnd_conn_data::charset_name */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn)
{
return conn->charset->name;
}
/* }}} */
/* {{{ mysqlnd_conn_data::thread_id */
static uint64_t
MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn)
{
return conn->thread_id;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_server_version */
static zend_ulong
MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn)
{
zend_long major, minor, patch;
char *p;
if (!(p = conn->server_version)) {
return 0;
}
major = ZEND_STRTOL(p, &p, 10);
p += 1; /* consume the dot */
minor = ZEND_STRTOL(p, &p, 10);
p += 1; /* consume the dot */
patch = ZEND_STRTOL(p, &p, 10);
return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
}
/* }}} */
/* {{{ mysqlnd_conn_data::more_results */
static zend_bool
MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::more_results");
/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
DBG_RETURN(conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
}
/* }}} */
/* {{{ mysqlnd_conn_data::next_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, next_result);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::next_result");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
if (CONN_GET_STATE(conn) != CONN_NEXT_RESULT_PENDING) {
break;
}
SET_EMPTY_ERROR(*conn->error_info);
SET_ERROR_AFF_ROWS(conn);
/*
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 = conn->m->query_read_result_set_header(conn, NULL))) {
/*
There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
So there are no more results and we should just return FALSE, error_no has been set
*/
if (!conn->error_info->error_no) {
DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
php_error_docref(NULL, E_WARNING, "Serious error. PID=%d", getpid());
CONN_SET_STATE(conn, CONN_QUIT_SENT);
conn->m->send_close(conn);
} else {
DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
}
break;
}
if (conn->last_query_type == QUERY_UPSERT && conn->upsert_status->affected_rows) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, conn->upsert_status->affected_rows);
}
} while (0);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_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_JSON:
return "json";
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_data::change_user */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
const char * user,
const char * passwd,
const char * db,
zend_bool silent,
size_t passwd_len
)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, change_user);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::change_user");
DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u",
conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", (silent == TRUE)?1:0 );
if (PASS != conn->m->local_tx_start(conn, this_func)) {
goto end;
}
SET_EMPTY_ERROR(*conn->error_info);
SET_ERROR_AFF_ROWS(conn);
if (!user) {
user = "";
}
if (!passwd) {
passwd = "";
}
if (!db) {
db = "";
}
/* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
conn->auth_plugin_data, conn->auth_plugin_data_len, conn->options->auth_protocol,
0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/);
/*
Here we should close all statements. Unbuffered queries should not be a
problem as we won't allow sending COM_CHANGE_USER.
*/
conn->m->local_tx_end(conn, this_func, ret);
end:
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_client_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
enum mysqlnd_option option,
const char * const value
)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_client_option);
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::set_client_option");
DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
if (PASS != conn->m->local_tx_start(conn, this_func)) {
goto end;
}
switch (option) {
case MYSQL_OPT_COMPRESS:
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_OPT_READ_TIMEOUT:
case MYSQL_OPT_WRITE_TIMEOUT:
#endif
case MYSQLND_OPT_SSL_KEY:
case MYSQLND_OPT_SSL_CERT:
case MYSQLND_OPT_SSL_CA:
case MYSQLND_OPT_SSL_CAPATH:
case MYSQLND_OPT_SSL_CIPHER:
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
case MYSQL_OPT_CONNECT_TIMEOUT:
case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
case MYSQL_SERVER_PUBLIC_KEY:
ret = conn->net->data->m.set_client_option(conn->net, option, value);
break;
#ifdef MYSQLND_STRING_TO_INT_CONVERSION
case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
conn->options->int_and_float_native = *(unsigned int*) value;
break;
#endif
case MYSQL_OPT_LOCAL_INFILE:
if (value && (*(unsigned int*) value) ? 1 : 0) {
conn->options->flags |= CLIENT_LOCAL_FILES;
} else {
conn->options->flags &= ~CLIENT_LOCAL_FILES;
}
break;
case MYSQL_INIT_COMMAND:
{
char ** new_init_commands;
char * new_command;
/* when num_commands is 0, then realloc will be effectively a malloc call, internally */
/* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
if (!new_init_commands) {
goto oom;
}
conn->options->init_commands = new_init_commands;
new_command = mnd_pestrdup(value, conn->persistent);
if (!new_command) {
goto oom;
}
conn->options->init_commands[conn->options->num_commands] = new_command;
++conn->options->num_commands;
break;
}
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:
#endif
/* currently not supported. Todo!! */
break;
case MYSQL_SET_CHARSET_NAME:
{
char * new_charset_name;
if (!mysqlnd_find_charset_name(value)) {
SET_CLIENT_ERROR(*conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
ret = FAIL;
break;
}
new_charset_name = mnd_pestrdup(value, conn->persistent);
if (!new_charset_name) {
goto oom;
}
if (conn->options->charset_name) {
mnd_pefree(conn->options->charset_name, conn->persistent);
}
conn->options->charset_name = new_charset_name;
DBG_INF_FMT("charset=%s", conn->options->charset_name);
break;
}
case MYSQL_OPT_NAMED_PIPE:
conn->options->protocol = MYSQL_PROTOCOL_PIPE;
break;
case MYSQL_OPT_PROTOCOL:
if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
conn->options->protocol = *(unsigned int*) value;
}
break;
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_SET_CHARSET_DIR:
case MYSQL_OPT_RECONNECT:
/* 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
case MYSQLND_OPT_MAX_ALLOWED_PACKET:
if (*(unsigned int*) value > (1<<16)) {
conn->options->max_allowed_packet = *(unsigned int*) value;
}
break;
case MYSQLND_OPT_AUTH_PROTOCOL:
{
char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
if (value && !new_auth_protocol) {
goto oom;
}
if (conn->options->auth_protocol) {
mnd_pefree(conn->options->auth_protocol, conn->persistent);
}
conn->options->auth_protocol = new_auth_protocol;
DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
break;
}
case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
if (value && (*(unsigned int*) value) ? 1 : 0) {
conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
} else {
conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
}
break;
case MYSQL_OPT_CONNECT_ATTR_RESET:
if (conn->options->connect_attr) {
DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
zend_hash_clean(conn->options->connect_attr);
}
break;
case MYSQL_OPT_CONNECT_ATTR_DELETE:
if (conn->options->connect_attr && value) {
DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
}
break;
#ifdef WHEN_SUPPORTED_BY_MYSQLI
case MYSQL_SHARED_MEMORY_BASE_NAME:
case MYSQL_OPT_USE_RESULT:
case MYSQL_SECURE_AUTH:
/* not sure, todo ? */
#endif
default:
ret = FAIL;
}
conn->m->local_tx_end(conn, this_func, ret);
DBG_RETURN(ret);
oom:
SET_OOM_ERROR(*conn->error_info);
conn->m->local_tx_end(conn, this_func, FAIL);
end:
DBG_RETURN(FAIL);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_client_option_2d */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
enum mysqlnd_option option,
const char * const key,
const char * const value
)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_client_option_2d);
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
if (PASS != conn->m->local_tx_start(conn, this_func)) {
goto end;
}
switch (option) {
case MYSQL_OPT_CONNECT_ATTR_ADD:
if (!conn->options->connect_attr) {
DBG_INF("Initializing connect_attr hash");
conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
if (!conn->options->connect_attr) {
goto oom;
}
zend_hash_init(conn->options->connect_attr, 0, NULL, ZVAL_PTR_DTOR, conn->persistent);
}
DBG_INF_FMT("Adding [%s][%s]", key, value);
{
zval attrz;
ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
zend_hash_str_update(conn->options->connect_attr, key, strlen(key), &attrz);
}
break;
default:
ret = FAIL;
}
conn->m->local_tx_end(conn, this_func, ret);
DBG_RETURN(ret);
oom:
SET_OOM_ERROR(*conn->error_info);
conn->m->local_tx_end(conn, this_func, FAIL);
end:
DBG_RETURN(FAIL);
}
/* }}} */
/* {{{ mysqlnd_conn_data::use_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn, const unsigned int flags)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, use_result);
MYSQLND_RES * result = NULL;
DBG_ENTER("mysqlnd_conn_data::use_result");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
if (!conn->current_result) {
break;
}
/* Nothing to store for UPSERT/LOAD DATA */
if (conn->last_query_type != QUERY_SELECT || CONN_GET_STATE(conn) != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("Command out of sync");
break;
}
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
conn->current_result->conn = conn->m->get_reference(conn);
result = conn->current_result->m.use_result(conn->current_result, FALSE);
if (!result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
}
conn->current_result = NULL;
} while (0);
conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
}
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::store_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn, const unsigned int flags)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, store_result);
MYSQLND_RES * result = NULL;
DBG_ENTER("mysqlnd_conn_data::store_result");
DBG_INF_FMT("conn=%llu conn=%p", conn->thread_id, conn);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
unsigned int f = flags;
if (!conn->current_result) {
break;
}
/* Nothing to store for UPSERT/LOAD DATA*/
if (conn->last_query_type != QUERY_SELECT || CONN_GET_STATE(conn) != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("Command out of sync");
break;
}
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
/* overwrite */
if ((conn->m->get_client_api_capabilities(conn) & MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA)) {
if (MYSQLND_G(fetch_data_copy)) {
f &= ~MYSQLND_STORE_NO_COPY;
f |= MYSQLND_STORE_COPY;
}
} else {
/* if for some reason PDO borks something */
if (!(f & (MYSQLND_STORE_NO_COPY | MYSQLND_STORE_COPY))) {
f |= MYSQLND_STORE_COPY;
}
}
if (!(f & (MYSQLND_STORE_NO_COPY | MYSQLND_STORE_COPY))) {
SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Unknown fetch mode");
DBG_ERR("Unknown fetch mode");
break;
}
result = conn->current_result->m.store_result(conn->current_result, conn, f);
if (!result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
}
conn->current_result = NULL;
} while (0);
conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
}
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_connection_stats */
static void
MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
zval * return_value ZEND_FILE_LINE_DC)
{
DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_autocommit */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_autocommit);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::set_autocommit");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
ret = conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_commit */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
{
return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_rollback */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
{
return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
}
/* }}} */
/* {{{ mysqlnd_tx_cor_options_to_string */
static void
MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
{
if (mode & TRANS_COR_AND_CHAIN && !(mode & TRANS_COR_AND_NO_CHAIN)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
} else if (mode & TRANS_COR_AND_NO_CHAIN && !(mode & TRANS_COR_AND_CHAIN)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
}
if (mode & TRANS_COR_RELEASE && !(mode & TRANS_COR_NO_RELEASE)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
} else if (mode & TRANS_COR_NO_RELEASE && !(mode & TRANS_COR_RELEASE)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
}
smart_str_0(str);
}
/* }}} */
/* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
static char *
mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
{
char * ret = NULL;
DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
if (name) {
zend_bool warned = FALSE;
const char * p_orig = name;
char * p_copy;
p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
*p_copy++ = ' ';
*p_copy++ = '/';
*p_copy++ = '*';
while (1) {
register char v = *p_orig;
if (v == 0) {
break;
}
if ((v >= '0' && v <= '9') ||
(v >= 'a' && v <= 'z') ||
(v >= 'A' && v <= 'Z') ||
v == '-' ||
v == '_' ||
v == ' ' ||
v == '=')
{
*p_copy++ = v;
} else if (warned == FALSE) {
php_error_docref(NULL, E_WARNING, "Transaction name truncated. Must be only [0-9A-Za-z\\-_=]+");
warned = TRUE;
}
++p_orig;
}
*p_copy++ = '*';
*p_copy++ = '/';
*p_copy++ = 0;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_commit_ex */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const zend_bool commit, const unsigned int flags, const char * const name)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_commit_or_rollback);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
smart_str tmp_str = {0, 0};
conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
smart_str_0(&tmp_str);
{
char * query;
size_t query_len;
char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
smart_str_free(&tmp_str);
if (name_esc) {
mnd_efree(name_esc);
name_esc = NULL;
}
if (!query) {
SET_OOM_ERROR(*conn->error_info);
break;
}
ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
}
} while (0);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_begin */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_begin);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::tx_begin");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
smart_str tmp_str = {0, 0};
if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
if (tmp_str.s) {
smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
}
smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
}
if (mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY)) {
zend_ulong server_version = conn->m->get_server_version(conn);
if (server_version < 50605L) {
php_error_docref(NULL, E_WARNING, "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
smart_str_free(&tmp_str);
break;
} else if (mode & TRANS_START_READ_WRITE) {
if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
}
smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
} else if (mode & TRANS_START_READ_ONLY) {
if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
}
smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
}
}
smart_str_0(&tmp_str);
{
char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
char * query;
unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
smart_str_free(&tmp_str);
if (name_esc) {
mnd_efree(name_esc);
name_esc = NULL;
}
if (!query) {
SET_OOM_ERROR(*conn->error_info);
break;
}
ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
}
} while (0);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_savepoint */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_savepoint);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
char * query;
unsigned int query_len;
if (!name) {
SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
break;
}
query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
if (!query) {
SET_OOM_ERROR(*conn->error_info);
break;
}
ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
} while (0);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_savepoint_release */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_savepoint_release);
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
if (PASS == conn->m->local_tx_start(conn, this_func)) {
do {
char * query;
unsigned int query_len;
if (!name) {
SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
break;
}
query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
if (!query) {
SET_OOM_ERROR(*conn->error_info);
break;
}
ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
} while (0);
conn->m->local_tx_end(conn, this_func, ret);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const unsigned int flags)
{
unsigned int ret = 0;
DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
if (conn) {
ret = conn->client_api_capabilities;
conn->client_api_capabilities = flags;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_client_api_capabilities */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
DBG_RETURN(conn? conn->client_api_capabilities : 0);
}
/* }}} */
/* {{{ mysqlnd_conn_data::local_tx_start */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start)(MYSQLND_CONN_DATA * conn, size_t this_func)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::local_tx_start");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::local_tx_end */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end)(MYSQLND_CONN_DATA * conn, size_t this_func, enum_func_status status)
{
DBG_ENTER("mysqlnd_conn_data::local_tx_end");
DBG_RETURN(status);
}
/* }}} */
/* {{{ mysqlnd_conn_data::init */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, init)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::init");
mysqlnd_stats_init(&conn->stats, STAT_LAST, conn->persistent);
SET_ERROR_AFF_ROWS(conn);
conn->net = mysqlnd_net_init(conn->persistent, conn->stats, conn->error_info);
conn->protocol = mysqlnd_protocol_init(conn->persistent);
DBG_RETURN(conn->stats && conn->net && conn->protocol? PASS:FAIL);
}
/* }}} */
MYSQLND_STMT * _mysqlnd_stmt_init(MYSQLND_CONN_DATA * const conn);
MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
MYSQLND_METHOD(mysqlnd_conn_data, init),
MYSQLND_METHOD(mysqlnd_conn_data, connect),
MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
MYSQLND_METHOD(mysqlnd_conn_data, query),
MYSQLND_METHOD(mysqlnd_conn_data, send_query),
MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
MYSQLND_METHOD(mysqlnd_conn_data, use_result),
MYSQLND_METHOD(mysqlnd_conn_data, store_result),
MYSQLND_METHOD(mysqlnd_conn_data, next_result),
MYSQLND_METHOD(mysqlnd_conn_data, more_results),
_mysqlnd_stmt_init,
MYSQLND_METHOD(mysqlnd_conn_data, shutdown),
MYSQLND_METHOD(mysqlnd_conn_data, refresh),
MYSQLND_METHOD(mysqlnd_conn_data, ping),
MYSQLND_METHOD(mysqlnd_conn_data, kill),
MYSQLND_METHOD(mysqlnd_conn_data, select_db),
MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
MYSQLND_METHOD(mysqlnd_conn_data, change_user),
MYSQLND_METHOD(mysqlnd_conn_data, errno),
MYSQLND_METHOD(mysqlnd_conn_data, error),
MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
MYSQLND_METHOD(mysqlnd_conn_data, statistic),
MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
MYSQLND_METHOD(mysqlnd_conn_data, info),
MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
MYSQLND_METHOD(mysqlnd_conn_data, list_fields),
MYSQLND_METHOD(mysqlnd_conn_data, list_method),
MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
MYSQLND_METHOD(mysqlnd_conn_data, field_count),
MYSQLND_METHOD(mysqlnd_conn_data, server_status),
MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
MYSQLND_METHOD(mysqlnd_conn_data, free_options),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
mysqlnd_query_read_result_set_header,
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_state),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, set_state),
MYSQLND_METHOD(mysqlnd_conn_data, simple_command),
MYSQLND_METHOD(mysqlnd_conn_data, simple_command_handle_response),
MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
MYSQLND_METHOD(mysqlnd_conn_data, send_close),
MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
mysqlnd_result_init,
MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start),
MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end),
MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
MYSQLND_METHOD(mysqlnd_conn_data, simple_command_send_request),
MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_conn::get_reference */
static MYSQLND *
MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
{
MYSQLND * ret;
DBG_ENTER("mysqlnd_conn::get_reference");
ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).clone_connection_object(conn);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::dtor */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
{
DBG_ENTER("mysqlnd_conn::dtor");
DBG_INF_FMT("conn=%llu", conn->data->thread_id);
conn->data->m->free_reference(conn->data);
mnd_pefree(conn, conn->persistent);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::close */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, enum_connection_close_type close_type)
{
size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_methods, close);
MYSQLND_CONN_DATA * conn = conn_handle->data;
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn::close");
DBG_INF_FMT("conn=%llu", conn->thread_id);
if (PASS == conn->m->local_tx_start(conn, this_func)) {
if (CONN_GET_STATE(conn) >= CONN_READY) {
static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
STAT_CLOSE_EXPLICIT,
STAT_CLOSE_IMPLICIT,
STAT_CLOSE_DISCONNECT
};
MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
}
/*
Close now, free_reference will try,
if we are last, but that's not a problem.
*/
ret = conn->m->send_close(conn);
/* do it after free_reference/dtor and we might crash */
conn->m->local_tx_end(conn, this_func, ret);
conn_handle->m->dtor(conn_handle);
}
DBG_RETURN(ret);
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
MYSQLND_METHOD(mysqlnd_conn, connect),
MYSQLND_METHOD(mysqlnd_conn, clone_object),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
MYSQLND_METHOD(mysqlnd_conn, close)
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_init */
PHPAPI MYSQLND *
mysqlnd_init(unsigned int flags, zend_bool persistent)
{
MYSQLND * ret;
DBG_ENTER("mysqlnd_init");
ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).get_connection(persistent);
if (ret && ret->data) {
ret->data->m->negotiate_client_api_capabilities(ret->data, flags);
}
DBG_RETURN(ret);
}
/* }}} */
/*
* 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
*/
|