summaryrefslogtreecommitdiffstats
path: root/rpc/rpc-transport/socket/src/socket.c
blob: 5cee869fdfccf83184bf54ad930eb0d15c567225 (plain)
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
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include "socket.h"
#include "name.h"
#include "dict.h"
#include "rpc-transport.h"
#include "logging.h"
#include "xlator.h"
#include "syscall.h"
#include "byte-order.h"
#include "common-utils.h"
#include "compat-errno.h"
#include "socket-mem-types.h"
#include "timer.h"

/* ugly #includes below */
#include "protocol-common.h"
#include "glusterfs3-xdr.h"
#include "xdr-nfs3.h"
#include "rpcsvc.h"

/* for TCP_USER_TIMEOUT */
#if !defined(TCP_USER_TIMEOUT) && defined(GF_LINUX_HOST_OS)
#include <linux/tcp.h>
#else
#include <netinet/tcp.h>
#endif

#include <fcntl.h>
#include <errno.h>
#include <rpc/xdr.h>
#include <sys/ioctl.h>
#define GF_LOG_ERRNO(errno) ((errno == ENOTCONN) ? GF_LOG_DEBUG : GF_LOG_ERROR)
#define SA(ptr) ((struct sockaddr *)ptr)

#define SSL_ENABLED_OPT     "transport.socket.ssl-enabled"
#define SSL_OWN_CERT_OPT    "transport.socket.ssl-own-cert"
#define SSL_PRIVATE_KEY_OPT "transport.socket.ssl-private-key"
#define SSL_CA_LIST_OPT     "transport.socket.ssl-ca-list"
#define SSL_CERT_DEPTH_OPT  "transport.socket.ssl-cert-depth"
#define SSL_CIPHER_LIST_OPT "transport.socket.ssl-cipher-list"
#define SSL_DH_PARAM_OPT    "transport.socket.ssl-dh-param"
#define SSL_EC_CURVE_OPT    "transport.socket.ssl-ec-curve"
#define SSL_CRL_PATH_OPT    "transport.socket.ssl-crl-path"
#define OWN_THREAD_OPT      "transport.socket.own-thread"

/* TBD: do automake substitutions etc. (ick) to set these. */
#if !defined(DEFAULT_ETC_SSL)
#  ifdef GF_LINUX_HOST_OS
#    define DEFAULT_ETC_SSL "/etc/ssl"
#  endif
#  ifdef GF_BSD_HOST_OS
#    define DEFAULT_ETC_SSL "/etc/openssl"
#  endif
#  ifdef GF_DARWIN_HOST_OS
#    define DEFAULT_ETC_SSL "/usr/local/etc/openssl"
#  endif
#  if !defined(DEFAULT_ETC_SSL)
#    define DEFAULT_ETC_SSL "/etc/ssl"
#  endif
#endif

#if !defined(DEFAULT_CERT_PATH)
#define DEFAULT_CERT_PATH   DEFAULT_ETC_SSL "/glusterfs.pem"
#endif
#if !defined(DEFAULT_KEY_PATH)
#define DEFAULT_KEY_PATH    DEFAULT_ETC_SSL "/glusterfs.key"
#endif
#if !defined(DEFAULT_CA_PATH)
#define DEFAULT_CA_PATH     DEFAULT_ETC_SSL "/glusterfs.ca"
#endif
#if !defined(DEFAULT_VERIFY_DEPTH)
#define DEFAULT_VERIFY_DEPTH 1
#endif
#define DEFAULT_CIPHER_LIST "EECDH:EDH:HIGH:!3DES:!RC4:!DES:!MD5:!aNULL:!eNULL"
#define DEFAULT_DH_PARAM   DEFAULT_ETC_SSL "/dhparam.pem"
#define DEFAULT_EC_CURVE   "prime256v1"

#define POLL_MASK_INPUT  (POLLIN | POLLPRI)
#define POLL_MASK_OUTPUT (POLLOUT)
#define POLL_MASK_ERROR  (POLLERR | POLLHUP | POLLNVAL)

typedef int SSL_unary_func (SSL *);
typedef int SSL_trinary_func (SSL *, void *, int);

#define __socket_proto_reset_pending(priv) do {                         \
                struct gf_sock_incoming_frag *frag;                     \
                frag = &priv->incoming.frag;                            \
                                                                        \
                memset (&frag->vector, 0, sizeof (frag->vector));       \
                frag->pending_vector = &frag->vector;                   \
                frag->pending_vector->iov_base = frag->fragcurrent;     \
                priv->incoming.pending_vector =  frag->pending_vector;  \
        } while (0)


#define __socket_proto_update_pending(priv)                             \
        do {                                                            \
                uint32_t remaining;                                     \
                struct gf_sock_incoming_frag *frag;                     \
                frag = &priv->incoming.frag;                            \
                if (frag->pending_vector->iov_len == 0) {               \
                        remaining = (RPC_FRAGSIZE (priv->incoming.fraghdr) \
                                     - frag->bytes_read);               \
                                                                        \
                        frag->pending_vector->iov_len =                 \
                                (remaining > frag->remaining_size)      \
                                ? frag->remaining_size : remaining;     \
                                                                        \
                        frag->remaining_size -=                         \
                                frag->pending_vector->iov_len;          \
                }                                                       \
        } while (0)

#define __socket_proto_update_priv_after_read(priv, ret, bytes_read)    \
        {                                                               \
                struct gf_sock_incoming_frag *frag;                     \
                frag = &priv->incoming.frag;                            \
                                                                        \
                frag->fragcurrent += bytes_read;                        \
                frag->bytes_read += bytes_read;                         \
                                                                        \
                if ((ret > 0) || (frag->remaining_size != 0)) {         \
                        if (frag->remaining_size != 0 && ret == 0) {    \
                                __socket_proto_reset_pending (priv);    \
                        }                                               \
                                                                        \
                        gf_log (this->name, GF_LOG_TRACE,               \
                                "partial read on non-blocking socket"); \
                                                                        \
                        break;                                          \
                }                                                       \
        }

#define __socket_proto_init_pending(priv,size)                          \
        do {                                                            \
            uint32_t remaining = 0;                                     \
            struct gf_sock_incoming_frag *frag;                         \
            frag = &priv->incoming.frag;                                \
                                                                        \
            remaining = (RPC_FRAGSIZE (priv->incoming.fraghdr)          \
                         - frag->bytes_read);                           \
                                                                        \
            __socket_proto_reset_pending (priv);                        \
                                                                        \
            frag->pending_vector->iov_len =                             \
                    (remaining > size) ? size : remaining;              \
                                                                        \
            frag->remaining_size = (size - frag->pending_vector->iov_len); \
                                                                        \
            } while(0)


/* This will be used in a switch case and breaks from the switch case if all
 * the pending data is not read.
 */
#define __socket_proto_read(priv, ret)                                  \
                {                                                       \
                size_t bytes_read = 0;                                  \
                struct gf_sock_incoming *in;                            \
                in = &priv->incoming;                                   \
                                                                        \
                __socket_proto_update_pending (priv);                   \
                                                                        \
                ret = __socket_readv (this,                             \
                                      in->pending_vector, 1,            \
                                      &in->pending_vector,              \
                                      &in->pending_count,               \
                                      &bytes_read);                     \
                if (ret == -1)                                          \
                        break;                                          \
                __socket_proto_update_priv_after_read (priv, ret, bytes_read); \
        }

struct socket_connect_error_state_ {
        xlator_t            *this;
        rpc_transport_t     *trans;
        gf_boolean_t         refd;
};
typedef struct socket_connect_error_state_ socket_connect_error_state_t;

static int socket_init (rpc_transport_t *this);

static void
ssl_dump_error_stack (const char *caller)
{
	unsigned long  errnum = 0;
	char           errbuf[120] = {0,};

	/* OpenSSL docs explicitly give 120 as the error-string length. */

	while ((errnum = ERR_get_error())) {
		ERR_error_string(errnum,errbuf);
		gf_log(caller,GF_LOG_ERROR,"  %s",errbuf);
	}
}

static int
ssl_do (rpc_transport_t *this, void *buf, size_t len, SSL_trinary_func *func)
{
	int               r = (-1);
	struct pollfd     pfd = {-1,};
	socket_private_t *priv = NULL;

	GF_VALIDATE_OR_GOTO(this->name,this->private,out);
	priv = this->private;

	for (;;) {
		if (buf) {
                        if (priv->connected == -1) {
                                /*
                                 * Fields in the SSL structure (especially
                                 * the BIO pointers) are not valid at this
                                 * point, so we'll segfault if we pass them
                                 * to SSL_read/SSL_write.
                                 */
                                gf_log(this->name,GF_LOG_INFO,
                                       "lost connection in %s", __func__);
                                break;
                        }
			r = func(priv->ssl_ssl,buf,len);
		}
		else {
                        /*
                         * We actually need these functions to get to
                         * priv->connected == 1.
                         */
			r = ((SSL_unary_func *)func)(priv->ssl_ssl);
		}
		switch (SSL_get_error(priv->ssl_ssl,r)) {
		case SSL_ERROR_NONE:
			return r;
		case SSL_ERROR_WANT_READ:
                        /* If we are attempting to connect/accept then we
                         * should wait here on the poll, for the SSL
                         * (re)negotiation to complete, else we would error out
                         * on the accept/connect.
                         * If we are here when attempting to read/write
                         * then we return r (or -1) as the socket is always
                         * primed for the read event, and it would eventually
                         * call one of the SSL routines */
                        /* NOTE: Only way to determine this is a accept/connect
                         * is to examine buf or func, which is not very
                         * clean */
                        if ((func == (SSL_trinary_func *)SSL_read)
                            || (func == (SSL_trinary_func *) SSL_write)) {
                                return r;
                        }

			pfd.fd = priv->sock;
			pfd.events = POLLIN;
			if (poll(&pfd,1,-1) < 0) {
				gf_log(this->name,GF_LOG_ERROR,"poll error %d",
				       errno);
			}
			break;
		case SSL_ERROR_WANT_WRITE:
                        if ((func == (SSL_trinary_func *)SSL_read)
                            || (func == (SSL_trinary_func *) SSL_write)) {
                                errno = EAGAIN;
                                return r;
                        }
			pfd.fd = priv->sock;
			pfd.events = POLLOUT;
			if (poll(&pfd,1,-1) < 0) {
				gf_log(this->name,GF_LOG_ERROR,"poll error %d",
				       errno);
			}
			break;
		case SSL_ERROR_SYSCALL:
			/* This is what we get when remote disconnects. */
			gf_log(this->name,GF_LOG_DEBUG,
			       "syscall error (probably remote disconnect)");
			errno = ENODATA;
			goto out;
		default:
			errno = EIO;
			goto out;	/* "break" would just loop again */
		}
	}
out:
	return -1;
}

#define ssl_connect_one(t)   ssl_do((t),NULL,0,(SSL_trinary_func *)SSL_connect)
#define ssl_accept_one(t)    ssl_do((t),NULL,0,(SSL_trinary_func *)SSL_accept)
#define ssl_read_one(t,b,l)  ssl_do((t),(b),(l),(SSL_trinary_func *)SSL_read)
#define ssl_write_one(t,b,l) ssl_do((t),(b),(l),(SSL_trinary_func *)SSL_write)

static char *
ssl_setup_connection (rpc_transport_t *this, int server)
{
	X509             *peer = NULL;
	char              peer_CN[256] = "";
	int               ret = -1;
	socket_private_t *priv = NULL;

	GF_VALIDATE_OR_GOTO(this->name,this->private,done);
	priv = this->private;

	priv->ssl_ssl = SSL_new(priv->ssl_ctx);
	if (!priv->ssl_ssl) {
		gf_log(this->name,GF_LOG_ERROR,"SSL_new failed");
		ssl_dump_error_stack(this->name);
		goto done;
	}
	priv->ssl_sbio = BIO_new_socket(priv->sock,BIO_NOCLOSE);
	if (!priv->ssl_sbio) {
		gf_log(this->name,GF_LOG_ERROR,"BIO_new_socket failed");
		ssl_dump_error_stack(this->name);
		goto free_ssl;
	}
	SSL_set_bio(priv->ssl_ssl,priv->ssl_sbio,priv->ssl_sbio);

	if (server) {
		ret = ssl_accept_one(this);
	}
	else {
		ret = ssl_connect_one(this);
	}

	/* Make sure _the call_ succeeded. */
	if (ret < 0) {
		goto ssl_error;
	}

	/* Make sure _SSL verification_ succeeded, yielding an identity. */
	if (SSL_get_verify_result(priv->ssl_ssl) != X509_V_OK) {
		goto ssl_error;
	}
	peer = SSL_get_peer_certificate(priv->ssl_ssl);
	if (!peer) {
		goto ssl_error;
	}

	/* Finally, everything seems OK. */
	X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
		NID_commonName, peer_CN, sizeof(peer_CN)-1);
	peer_CN[sizeof(peer_CN)-1] = '\0';
	gf_log(this->name, GF_LOG_DEBUG, "peer CN = %s", peer_CN);
        gf_log (this->name, GF_LOG_DEBUG,
                "SSL verification succeeded (client: %s) (server: %s)",
                this->peerinfo.identifier, this->myinfo.identifier);
        return gf_strdup(peer_CN);

	/* Error paths. */
ssl_error:
	gf_log (this->name, GF_LOG_ERROR,
                "SSL connect error (client: %s) (server: %s)",
                this->peerinfo.identifier, this->myinfo.identifier);
	ssl_dump_error_stack(this->name);
free_ssl:
	SSL_free(priv->ssl_ssl);
        priv->ssl_ssl = NULL;
done:
	return NULL;
}


static void
ssl_teardown_connection (socket_private_t *priv)
{
        if (priv->ssl_ssl) {
                SSL_shutdown(priv->ssl_ssl);
                SSL_clear(priv->ssl_ssl);
                SSL_free(priv->ssl_ssl);
                priv->ssl_ssl = NULL;
        }
        priv->use_ssl = _gf_false;
}


static ssize_t
__socket_ssl_readv (rpc_transport_t *this, struct iovec *opvector, int opcount)
{
	socket_private_t    *priv = NULL;
	int                  sock = -1;
	int                  ret = -1;

	priv = this->private;
	sock = priv->sock;

	if (priv->use_ssl) {
		ret = ssl_read_one (this, opvector->iov_base, opvector->iov_len);
	} else {
		ret = sys_readv (sock, opvector, IOV_MIN(opcount));
	}

	return ret;
}


static ssize_t
__socket_ssl_read (rpc_transport_t *this, void *buf, size_t count)
{
	struct iovec iov = {0, };
	int          ret = -1;

	iov.iov_base = buf;
	iov.iov_len = count;

	ret = __socket_ssl_readv (this, &iov, 1);

	return ret;
}


static int
__socket_cached_read (rpc_transport_t *this, struct iovec *opvector, int opcount)
{
	socket_private_t   *priv = NULL;
	struct gf_sock_incoming *in = NULL;
	int                 req_len = -1;
	int                 ret = -1;

	priv = this->private;
	in = &priv->incoming;
	req_len = iov_length (opvector, opcount);

	if (in->record_state == SP_STATE_READING_FRAGHDR) {
		in->ra_read = 0;
		in->ra_served = 0;
		in->ra_max = 0;
		in->ra_buf = NULL;
		goto uncached;
	}

	if (!in->ra_max) {
		/* first call after passing SP_STATE_READING_FRAGHDR */
		in->ra_max = min (RPC_FRAGSIZE (in->fraghdr), GF_SOCKET_RA_MAX);
		/* Note that the in->iobuf is the primary iobuf into which
		   headers are read into, and in->frag.fragcurrent points to
 		   some position in the buffer. By using this itself as our
		   read-ahead cache, we can avoid memory copies in iov_load
		*/
		in->ra_buf = in->frag.fragcurrent;
	}

	/* fill read-ahead */
	if (in->ra_read < in->ra_max) {
		ret = __socket_ssl_read (this, &in->ra_buf[in->ra_read],
					 (in->ra_max - in->ra_read));
		if (ret > 0)
			in->ra_read += ret;

		/* we proceed to test if there is still cached data to
		   be served even if readahead could not progress */
	}

	/* serve cached */
	if (in->ra_served < in->ra_read) {
		ret = iov_load (opvector, opcount, &in->ra_buf[in->ra_served],
				min (req_len, (in->ra_read - in->ra_served)));

		in->ra_served += ret;
		/* Do not read uncached and cached in the same call */
		goto out;
	}

	if (in->ra_read < in->ra_max)
		/* If there was no cached data to be served, (and we are
		   guaranteed to have already performed an attempt to progress
		   readahead above), and we have not yet read out the full
		   readahead capacity, then bail out for now without doing
		   the uncached read below (as that will overtake future cached
		   read)
		*/
		goto out;
uncached:
	ret = __socket_ssl_readv (this, opvector, opcount);
out:
	return ret;
}

static gf_boolean_t
__does_socket_rwv_error_need_logging (socket_private_t *priv, int write)
{
        int read = !write;

        if (priv->connected == -1) /* Didn't even connect, of course it fails */
                return _gf_false;

        if (read && (priv->read_fail_log == _gf_false))
                return _gf_false;

        return _gf_true;
}

/*
 * return value:
 *   0 = success (completed)
 *  -1 = error
 * > 0 = incomplete
 */

static int
__socket_rwv (rpc_transport_t *this, struct iovec *vector, int count,
              struct iovec **pending_vector, int *pending_count, size_t *bytes,
              int write)
{
        socket_private_t *priv = NULL;
        int               sock = -1;
        int               ret = -1;
        struct iovec     *opvector = NULL;
        int               opcount = 0;
        int               moved = 0;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        sock = priv->sock;

        opvector = vector;
        opcount  = count;

        if (bytes != NULL) {
                *bytes = 0;
        }

        while (opcount > 0) {
                if (opvector->iov_len == 0) {
                        gf_log(this->name,GF_LOG_DEBUG,
                               "would have passed zero length to read/write");
                        ++opvector;
                        --opcount;
                        continue;
                }
                if (priv->use_ssl && !priv->ssl_ssl) {
                        /*
                         * We could end up here with priv->ssl_ssl still NULL
                         * if (a) the connection failed and (b) some fool
                         * called other socket functions anyway.  Demoting to
                         * non-SSL might be insecure, so just fail it outright.
                         */
                        ret = -1;
                } else if (write) {
			if (priv->use_ssl) {
                                ret = ssl_write_one (this, opvector->iov_base,
                                                     opvector->iov_len);
			} else {
				ret = sys_writev (sock, opvector, IOV_MIN(opcount));
			}

                        if (ret == 0 || (ret == -1 && errno == EAGAIN)) {
                                /* done for now */
                                break;
                        }
                        this->total_bytes_write += ret;
                } else {
			ret = __socket_cached_read (this, opvector, opcount);

			if (ret == 0) {
				gf_log(this->name,GF_LOG_DEBUG,"EOF on socket");
				errno = ENODATA;
				ret = -1;
			}
                        if (ret == -1 && errno == EAGAIN) {
                                /* done for now */
                                break;
                        }
                        this->total_bytes_read += ret;
                }

                if (ret == 0) {
                        /* Mostly due to 'umount' in client */

                        gf_log (this->name, GF_LOG_DEBUG,
                                "EOF from peer %s", this->peerinfo.identifier);
                        opcount = -1;
                        errno = ENOTCONN;
                        break;
                }
                if (ret == -1) {
                        if (errno == EINTR)
                                continue;

                        if (__does_socket_rwv_error_need_logging (priv,
                                                                  write)) {
                                GF_LOG_OCCASIONALLY(priv->log_ctr, this->name,
                                                    GF_LOG_WARNING,
                                                    "%s on %s failed (%s)",
                                                    write ? "writev":"readv",
                                                    this->peerinfo.identifier,
                                                    strerror (errno));
                        }

			if (priv->use_ssl && priv->ssl_ssl) {
				ssl_dump_error_stack(this->name);
			}
                        opcount = -1;
                        break;
                }

                if (bytes != NULL) {
                        *bytes += ret;
                }

                moved = 0;

                while (moved < ret) {
                        if (!opcount) {
                                gf_log(this->name,GF_LOG_DEBUG,
                                       "ran out of iov, moved %d/%d",
                                       moved, ret);
                                goto ran_out;
                        }
                        if (!opvector[0].iov_len) {
                                opvector++;
                                opcount--;
                                continue;
                        }
                        if ((ret - moved) >= opvector[0].iov_len) {
                                moved += opvector[0].iov_len;
                                opvector++;
                                opcount--;
                        } else {
                                opvector[0].iov_len -= (ret - moved);
                                opvector[0].iov_base += (ret - moved);
                                moved += (ret - moved);
                        }
                }
        }

ran_out:

        if (pending_vector)
                *pending_vector = opvector;

        if (pending_count)
                *pending_count = opcount;

out:
        return opcount;
}


static int
__socket_readv (rpc_transport_t *this, struct iovec *vector, int count,
                struct iovec **pending_vector, int *pending_count,
                size_t *bytes)
{
        int ret = -1;

        ret = __socket_rwv (this, vector, count,
                            pending_vector, pending_count, bytes, 0);

        return ret;
}


static int
__socket_writev (rpc_transport_t *this, struct iovec *vector, int count,
                 struct iovec **pending_vector, int *pending_count)
{
        int ret = -1;

        ret = __socket_rwv (this, vector, count,
                            pending_vector, pending_count, NULL, 1);

        return ret;
}


static int
__socket_shutdown (rpc_transport_t *this)
{
        int               ret = -1;
        socket_private_t *priv = this->private;

        priv->connected = -1;
        ret = shutdown (priv->sock, SHUT_RDWR);
        if (ret) {
                /* its already disconnected.. no need to understand
                   why it failed to shutdown in normal cases */
                gf_log (this->name, GF_LOG_DEBUG,
                        "shutdown() returned %d. %s",
                        ret, strerror (errno));
        }

        return ret;
}

static int
__socket_teardown_connection (rpc_transport_t *this)
{
        int               ret = -1;
        socket_private_t *priv = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        if (priv->use_ssl)
                ssl_teardown_connection(priv);

        ret = __socket_shutdown(this);
out:
        return ret;
}

static int
__socket_disconnect (rpc_transport_t *this)
{
        int               ret = -1;
        socket_private_t *priv = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        gf_log (this->name, GF_LOG_TRACE,
                "disconnecting %p, state=%u gen=%u sock=%d", this,
                priv->ot_state, priv->ot_gen, priv->sock);

        if (priv->sock != -1) {
                gf_log_callingfn (this->name, GF_LOG_TRACE,
                                  "tearing down socket connection");
                ret = __socket_teardown_connection (this);
                if (ret) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "__socket_teardown_connection () failed: %s",
                                strerror (errno));
                }

		if (priv->own_thread) {
                        /*
                         * Without this, reconnect (= disconnect + connect)
                         * won't work except by accident.
                         */
                        gf_log (this->name, GF_LOG_TRACE,
                                "OT_PLEASE_DIE on %p", this);
                        priv->ot_state = OT_PLEASE_DIE;
                }
        }

out:
        return ret;
}


static int
__socket_server_bind (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;
        int               ret = -1;
        int               opt = 1;
        int               reuse_check_sock = -1;
        struct sockaddr_storage   unix_addr = {0};

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        ret = setsockopt (priv->sock, SOL_SOCKET, SO_REUSEADDR,
                          &opt, sizeof (opt));

        if (ret == -1) {
                gf_log (this->name, GF_LOG_ERROR,
                        "setsockopt() for SO_REUSEADDR failed (%s)",
                        strerror (errno));
        }

        /* reuse-address doesn't work for unix type sockets */
        if (AF_UNIX == SA (&this->myinfo.sockaddr)->sa_family) {
                memcpy (&unix_addr, SA (&this->myinfo.sockaddr),
                        this->myinfo.sockaddr_len);
                reuse_check_sock = socket (AF_UNIX, SOCK_STREAM, 0);
                if (reuse_check_sock >= 0) {
                        ret = connect (reuse_check_sock, SA (&unix_addr),
                                       this->myinfo.sockaddr_len);
                        if ((ret == -1) && (ECONNREFUSED == errno)) {
                                sys_unlink (((struct sockaddr_un *)&unix_addr)->sun_path);
                        }
                        sys_close (reuse_check_sock);
                }
        }

        ret = bind (priv->sock, (struct sockaddr *)&this->myinfo.sockaddr,
                    this->myinfo.sockaddr_len);

        if (ret == -1) {
                gf_log (this->name, GF_LOG_ERROR,
                        "binding to %s failed: %s",
                        this->myinfo.identifier, strerror (errno));
                if (errno == EADDRINUSE) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "Port is already in use");

                        ret = -EADDRINUSE;
                }
        }

out:
        return ret;
}


static int
__socket_nonblock (int fd)
{
        int flags = 0;
        int ret = -1;

        flags = fcntl (fd, F_GETFL);

        if (flags != -1)
                ret = fcntl (fd, F_SETFL, flags | O_NONBLOCK);

        return ret;
}

static int
__socket_nodelay (int fd)
{
        int     on = 1;
        int     ret = -1;

        ret = setsockopt (fd, IPPROTO_TCP, TCP_NODELAY,
                          &on, sizeof (on));
        if (!ret)
                gf_log (THIS->name, GF_LOG_TRACE,
                        "NODELAY enabled for socket %d", fd);

        return ret;
}


static int
__socket_keepalive (int fd, int family, int keepaliveintvl,
                    int keepaliveidle, int keepalivecnt, int timeout)
{
        int     on = 1;
        int     ret = -1;
        int     timeout_ms = timeout * 1000;

        ret = setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on));
        if (ret == -1) {
                gf_log ("socket", GF_LOG_WARNING,
                        "failed to set keep alive option on socket %d", fd);
                goto err;
        }

        if (keepaliveintvl == GF_USE_DEFAULT_KEEPALIVE)
                goto done;

#if !defined(GF_LINUX_HOST_OS) && !defined(__NetBSD__)
#if defined(GF_SOLARIS_HOST_OS) || defined(__FreeBSD__)
        ret = setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &keepaliveintvl,
                          sizeof (keepaliveintvl));
#else
        ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPALIVE, &keepaliveintvl,
                          sizeof (keepaliveintvl));
#endif
        if (ret == -1) {
                gf_log ("socket", GF_LOG_WARNING,
                        "failed to set keep alive interval on socket %d", fd);
                goto err;
        }
#else
        if (family != AF_INET && family != AF_INET6)
                goto done;

        ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepaliveidle,
                          sizeof (keepaliveidle));
        if (ret == -1) {
                gf_log ("socket", GF_LOG_WARNING,
                        "failed to set keep idle %d on socket %d, %s",
                        keepaliveidle, fd, strerror(errno));
                goto err;
        }
        ret = setsockopt (fd, IPPROTO_TCP , TCP_KEEPINTVL, &keepaliveintvl,
                          sizeof (keepaliveintvl));
        if (ret == -1) {
                gf_log ("socket", GF_LOG_WARNING,
                        "failed to set keep interval %d on socket %d, %s",
                        keepaliveintvl, fd, strerror(errno));
                goto err;
        }

#if defined(TCP_USER_TIMEOUT)
        if (timeout_ms < 0)
                goto done;
        ret = setsockopt (fd, IPPROTO_TCP , TCP_USER_TIMEOUT, &timeout_ms,
                          sizeof (timeout_ms));
        if (ret == -1) {
                gf_log ("socket", GF_LOG_WARNING, "failed to set "
                        "TCP_USER_TIMEOUT %d on socket %d, %s", timeout_ms, fd,
                        strerror(errno));
                goto err;
        }
#endif
#if defined(TCP_KEEPCNT)
        ret = setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &keepalivecnt,
                          sizeof (keepalivecnt));
        if (ret == -1) {
                gf_log ("socket", GF_LOG_WARNING, "failed to set "
                        "TCP_KEEPCNT %d on socket %d, %s", keepalivecnt, fd,
                        strerror(errno));
                goto err;
        }
#endif
#endif

done:
        gf_log (THIS->name, GF_LOG_TRACE, "Keep-alive enabled for socket: %d, "
                "(idle: %d, interval: %d, max-probes: %d, timeout: %d)",
                fd, keepaliveidle, keepaliveintvl, keepalivecnt,
                timeout);

err:
        return ret;
}


static int
__socket_connect_finish (int fd)
{
        int       ret = -1;
        int       optval = 0;
        socklen_t optlen = sizeof (int);

        ret = getsockopt (fd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen);

        if (ret == 0 && optval) {
                errno = optval;
                ret = -1;
        }

        return ret;
}


static void
__socket_reset (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        /* TODO: use mem-pool on incoming data */

        if (priv->incoming.iobref) {
                iobref_unref (priv->incoming.iobref);
                priv->incoming.iobref = NULL;
        }

        if (priv->incoming.iobuf) {
                iobuf_unref (priv->incoming.iobuf);
                priv->incoming.iobuf = NULL;
        }

        GF_FREE (priv->incoming.request_info);

        memset (&priv->incoming, 0, sizeof (priv->incoming));

        event_unregister_close (this->ctx->event_pool, priv->sock, priv->idx);

        priv->sock = -1;
        priv->idx = -1;
        priv->connected = -1;

out:
        return;
}


static void
socket_set_lastfrag (uint32_t *fragsize) {
        (*fragsize) |= 0x80000000U;
}


static void
socket_set_frag_header_size (uint32_t size, char *haddr)
{
        size = htonl (size);
        memcpy (haddr, &size, sizeof (size));
}


static void
socket_set_last_frag_header_size (uint32_t size, char *haddr)
{
        socket_set_lastfrag (&size);
        socket_set_frag_header_size (size, haddr);
}

static struct ioq *
__socket_ioq_new (rpc_transport_t *this, rpc_transport_msg_t *msg)
{
        struct ioq       *entry = NULL;
        int               count = 0;
        uint32_t          size  = 0;

        GF_VALIDATE_OR_GOTO ("socket", this, out);

        /* TODO: use mem-pool */
        entry = GF_CALLOC (1, sizeof (*entry), gf_common_mt_ioq);
        if (!entry)
                return NULL;

        count = msg->rpchdrcount + msg->proghdrcount + msg->progpayloadcount;

        GF_ASSERT (count <= (MAX_IOVEC - 1));

        size = iov_length (msg->rpchdr, msg->rpchdrcount)
                + iov_length (msg->proghdr, msg->proghdrcount)
                + iov_length (msg->progpayload, msg->progpayloadcount);

        if (size > RPC_MAX_FRAGMENT_SIZE) {
                gf_log (this->name, GF_LOG_ERROR,
                        "msg size (%u) bigger than the maximum allowed size on "
                        "sockets (%u)", size, RPC_MAX_FRAGMENT_SIZE);
                GF_FREE (entry);
                return NULL;
        }

        socket_set_last_frag_header_size (size, (char *)&entry->fraghdr);

        entry->vector[0].iov_base = (char *)&entry->fraghdr;
        entry->vector[0].iov_len = sizeof (entry->fraghdr);
        entry->count = 1;

        if (msg->rpchdr != NULL) {
                memcpy (&entry->vector[1], msg->rpchdr,
                        sizeof (struct iovec) * msg->rpchdrcount);
                entry->count += msg->rpchdrcount;
        }

        if (msg->proghdr != NULL) {
                memcpy (&entry->vector[entry->count], msg->proghdr,
                        sizeof (struct iovec) * msg->proghdrcount);
                entry->count += msg->proghdrcount;
        }

        if (msg->progpayload != NULL) {
                memcpy (&entry->vector[entry->count], msg->progpayload,
                        sizeof (struct iovec) * msg->progpayloadcount);
                entry->count += msg->progpayloadcount;
        }

        entry->pending_vector = entry->vector;
        entry->pending_count  = entry->count;

        if (msg->iobref != NULL)
                entry->iobref = iobref_ref (msg->iobref);

        INIT_LIST_HEAD (&entry->list);

out:
        return entry;
}


static void
__socket_ioq_entry_free (struct ioq *entry)
{
        GF_VALIDATE_OR_GOTO ("socket", entry, out);

        list_del_init (&entry->list);
        if (entry->iobref)
                iobref_unref (entry->iobref);

        /* TODO: use mem-pool */
        GF_FREE (entry);

out:
        return;
}


static void
__socket_ioq_flush (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;
        struct ioq       *entry = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        while (!list_empty (&priv->ioq)) {
                entry = priv->ioq_next;
                __socket_ioq_entry_free (entry);
        }

out:
        return;
}


static int
__socket_ioq_churn_entry (rpc_transport_t *this, struct ioq *entry, int direct)
{
        int               ret = -1;
	socket_private_t *priv = NULL;
	char              a_byte = 0;

        ret = __socket_writev (this, entry->pending_vector,
                               entry->pending_count,
                               &entry->pending_vector,
                               &entry->pending_count);

        if (ret == 0) {
                /* current entry was completely written */
                GF_ASSERT (entry->pending_count == 0);
                __socket_ioq_entry_free (entry);
		priv = this->private;
		if (priv->own_thread) {
			/*
			 * The pipe should only remain readable if there are
			 * more entries after this, so drain the byte
			 * representing this entry.
			 */
			if (!direct && sys_read (priv->pipe[0], &a_byte, 1) < 1) {
				gf_log(this->name,GF_LOG_WARNING,
				       "read error on pipe");
			}
		}
        }

        return ret;
}


static int
__socket_ioq_churn (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;
        int               ret = 0;
        struct ioq       *entry = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        while (!list_empty (&priv->ioq)) {
                /* pick next entry */
                entry = priv->ioq_next;

                ret = __socket_ioq_churn_entry (this, entry, 0);

                if (ret != 0)
                        break;
        }

        if (!priv->own_thread && list_empty (&priv->ioq)) {
                /* all pending writes done, not interested in POLLOUT */
                priv->idx = event_select_on (this->ctx->event_pool,
                                             priv->sock, priv->idx, -1, 0);
        }

out:
        return ret;
}


static gf_boolean_t
socket_event_poll_err (rpc_transport_t *this, int gen, int idx)
{
        socket_private_t *priv          = NULL;
        gf_boolean_t      socket_closed = _gf_false;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        pthread_mutex_lock (&priv->lock);
        {
                if ((priv->gen == gen) && (priv->idx == idx)
                    && (priv->sock != -1)) {
                        __socket_ioq_flush (this);
                        __socket_reset (this);
                        socket_closed = _gf_true;
                }
        }
        pthread_mutex_unlock (&priv->lock);

        if (socket_closed) {
                pthread_mutex_lock (&priv->notify.lock);
                {
                        while (priv->notify.in_progress)
                                pthread_cond_wait (&priv->notify.cond,
                                                   &priv->notify.lock);
                }
                pthread_mutex_unlock (&priv->notify.lock);

                rpc_transport_notify (this, RPC_TRANSPORT_DISCONNECT, this);
        }

out:
        return socket_closed;
}


static int
socket_event_poll_out (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;
        int               ret = -1;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        pthread_mutex_lock (&priv->lock);
        {
                if (priv->connected == 1) {
                        ret = __socket_ioq_churn (this);

                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_TRACE,
                                        "__socket_ioq_churn returned -1; "
                                        "disconnecting socket");
                                __socket_disconnect (this);
                        }
                }
        }
        pthread_mutex_unlock (&priv->lock);

        if (ret == 0)
                ret = rpc_transport_notify (this, RPC_TRANSPORT_MSG_SENT, NULL);

out:
        return ret;
}


static int
__socket_read_simple_msg (rpc_transport_t *this)
{
        int                           ret            = 0;
        uint32_t                      remaining_size = 0;
        size_t                        bytes_read     = 0;
        socket_private_t             *priv           = NULL;
        struct gf_sock_incoming      *in             = NULL;
        struct gf_sock_incoming_frag *frag           = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        in = &priv->incoming;
        frag = &in->frag;

        switch (frag->simple_state) {

        case SP_STATE_SIMPLE_MSG_INIT:
                remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;

                __socket_proto_init_pending (priv, remaining_size);

                frag->simple_state = SP_STATE_READING_SIMPLE_MSG;

                /* fall through */

        case SP_STATE_READING_SIMPLE_MSG:
                ret = 0;

                remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;

                if (remaining_size > 0) {
                        ret = __socket_readv (this,
                                              in->pending_vector, 1,
                                              &in->pending_vector,
                                              &in->pending_count,
                                              &bytes_read);
                }

                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "reading from socket failed. Error (%s), "
                                "peer (%s)", strerror (errno),
                                this->peerinfo.identifier);
                        break;
                }

                frag->bytes_read += bytes_read;
                frag->fragcurrent += bytes_read;

                if (ret > 0) {
                        gf_log (this->name, GF_LOG_TRACE,
                                "partial read on non-blocking socket.");
                        break;
                }

                if (ret == 0) {
                        frag->simple_state =  SP_STATE_SIMPLE_MSG_INIT;
                }
        }

out:
        return ret;
}


static int
__socket_read_simple_request (rpc_transport_t *this)
{
        return __socket_read_simple_msg (this);
}


#define rpc_cred_addr(buf) (buf + RPC_MSGTYPE_SIZE + RPC_CALL_BODY_SIZE - 4)

#define rpc_verf_addr(fragcurrent) (fragcurrent - 4)

#define rpc_msgtype_addr(buf) (buf + 4)

#define rpc_prognum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 4)
#define rpc_progver_addr(buf) (buf + RPC_MSGTYPE_SIZE + 8)
#define rpc_procnum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 12)

static int
__socket_read_vectored_request (rpc_transport_t *this, rpcsvc_vector_sizer vector_sizer)
{
        socket_private_t *priv                   = NULL;
        int               ret                    = 0;
        uint32_t          credlen                = 0, verflen = 0;
        char             *addr                   = NULL;
        struct iobuf     *iobuf                  = NULL;
        uint32_t          remaining_size         = 0;
        ssize_t           readsize               = 0;
        size_t            size                   = 0;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;
        sp_rpcfrag_request_state_t   *request    = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        /* used to reduce the indirection */
        in = &priv->incoming;
        frag = &in->frag;
        request = &frag->call_body.request;

        switch (request->vector_state) {
        case SP_STATE_VECTORED_REQUEST_INIT:
                request->vector_sizer_state = 0;

                addr = rpc_cred_addr (iobuf_ptr (in->iobuf));

                /* also read verf flavour and verflen */
                credlen = ntoh32 (*((uint32_t *)addr))
                        +  RPC_AUTH_FLAVOUR_N_LENGTH_SIZE;

                __socket_proto_init_pending (priv, credlen);

                request->vector_state = SP_STATE_READING_CREDBYTES;

                /* fall through */

        case SP_STATE_READING_CREDBYTES:
                __socket_proto_read (priv, ret);

                request->vector_state = SP_STATE_READ_CREDBYTES;

                /* fall through */

        case SP_STATE_READ_CREDBYTES:
                addr = rpc_verf_addr (frag->fragcurrent);
                verflen = ntoh32 (*((uint32_t *)addr));

                if (verflen == 0) {
                        request->vector_state = SP_STATE_READ_VERFBYTES;
                        goto sp_state_read_verfbytes;
                }
                __socket_proto_init_pending (priv, verflen);

                request->vector_state = SP_STATE_READING_VERFBYTES;

                /* fall through */

        case SP_STATE_READING_VERFBYTES:
                __socket_proto_read (priv, ret);

                request->vector_state = SP_STATE_READ_VERFBYTES;

                /* fall through */

        case SP_STATE_READ_VERFBYTES:
sp_state_read_verfbytes:
		/* set the base_addr 'persistently' across multiple calls
		   into the state machine */
                in->proghdr_base_addr = frag->fragcurrent;

                request->vector_sizer_state =
                        vector_sizer (request->vector_sizer_state,
                                      &readsize, in->proghdr_base_addr,
                                      frag->fragcurrent);
                __socket_proto_init_pending (priv, readsize);

                request->vector_state = SP_STATE_READING_PROGHDR;

                /* fall through */

        case SP_STATE_READING_PROGHDR:
                __socket_proto_read (priv, ret);

		request->vector_state =	SP_STATE_READ_PROGHDR;

		/* fall through */

	case SP_STATE_READ_PROGHDR:
sp_state_read_proghdr:
                request->vector_sizer_state =
                        vector_sizer (request->vector_sizer_state,
                                      &readsize, in->proghdr_base_addr,
                                      frag->fragcurrent);
                if (readsize == 0) {
                        request->vector_state = SP_STATE_READ_PROGHDR_XDATA;
			goto sp_state_read_proghdr_xdata;
                }

		__socket_proto_init_pending (priv, readsize);

                request->vector_state =	SP_STATE_READING_PROGHDR_XDATA;

		/* fall through */

	case SP_STATE_READING_PROGHDR_XDATA:
		__socket_proto_read (priv, ret);

		request->vector_state =	SP_STATE_READ_PROGHDR;
		/* check if the vector_sizer() has more to say */
		goto sp_state_read_proghdr;

        case SP_STATE_READ_PROGHDR_XDATA:
sp_state_read_proghdr_xdata:
                if (in->payload_vector.iov_base == NULL) {

                        size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;
                        iobuf = iobuf_get2 (this->ctx->iobuf_pool, size);
                        if (!iobuf) {
                                ret = -1;
                                break;
                        }

                        if (in->iobref == NULL) {
                                in->iobref = iobref_new ();
                                if (in->iobref == NULL) {
                                        ret = -1;
                                        iobuf_unref (iobuf);
                                        break;
                                }
                        }

                        iobref_add (in->iobref, iobuf);

                        in->payload_vector.iov_base = iobuf_ptr (iobuf);
                        frag->fragcurrent = iobuf_ptr (iobuf);

                        iobuf_unref (iobuf);
                }

                request->vector_state = SP_STATE_READING_PROG;

                /* fall through */

        case SP_STATE_READING_PROG:
                /* now read the remaining rpc msg into buffer pointed by
                 * fragcurrent
                 */

                ret = __socket_read_simple_msg (this);

                remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;

                if ((ret == -1) ||
                    ((ret == 0) && (remaining_size == 0)
                     && RPC_LASTFRAG (in->fraghdr))) {
                        request->vector_state = SP_STATE_VECTORED_REQUEST_INIT;
                        in->payload_vector.iov_len
                                = ((unsigned long)frag->fragcurrent
                                   - (unsigned long)in->payload_vector.iov_base);
                }
                break;
        }

out:
        return ret;
}

static int
__socket_read_request (rpc_transport_t *this)
{
        socket_private_t *priv               = NULL;
        uint32_t          prognum            = 0, procnum = 0, progver = 0;
        uint32_t          remaining_size     = 0;
        int               ret                = -1;
        char             *buf                = NULL;
        rpcsvc_vector_sizer     vector_sizer = NULL;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;
        sp_rpcfrag_request_state_t   *request    = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        /* used to reduce the indirection */
        in = &priv->incoming;
        frag = &in->frag;
        request = &frag->call_body.request;

        switch (request->header_state) {

        case SP_STATE_REQUEST_HEADER_INIT:

                __socket_proto_init_pending (priv, RPC_CALL_BODY_SIZE);

                request->header_state = SP_STATE_READING_RPCHDR1;

                /* fall through */

        case SP_STATE_READING_RPCHDR1:
                __socket_proto_read (priv, ret);

                request->header_state = SP_STATE_READ_RPCHDR1;

                /* fall through */

        case SP_STATE_READ_RPCHDR1:
                buf = rpc_prognum_addr (iobuf_ptr (in->iobuf));
                prognum = ntoh32 (*((uint32_t *)buf));

                buf = rpc_progver_addr (iobuf_ptr (in->iobuf));
                progver = ntoh32 (*((uint32_t *)buf));

                buf = rpc_procnum_addr (iobuf_ptr (in->iobuf));
                procnum = ntoh32 (*((uint32_t *)buf));

                if (priv->is_server) {
                        /* this check is needed as rpcsvc and rpc-clnt
                         * actor structures are not same */
                        vector_sizer =
                                rpcsvc_get_program_vector_sizer ((rpcsvc_t *)this->mydata,
                                                                 prognum, progver, procnum);
                }

                if (vector_sizer) {
                        ret = __socket_read_vectored_request (this, vector_sizer);
                } else {
                        ret = __socket_read_simple_request (this);
                }

                remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;

                if ((ret == -1)
                    || ((ret == 0)
                        && (remaining_size == 0)
                        && (RPC_LASTFRAG (in->fraghdr)))) {
                        request->header_state = SP_STATE_REQUEST_HEADER_INIT;
                }

                break;
        }

out:
        return ret;
}


static int
__socket_read_accepted_successful_reply (rpc_transport_t *this)
{
        socket_private_t *priv              = NULL;
        int               ret               = 0;
        struct iobuf     *iobuf             = NULL;
        gfs3_read_rsp     read_rsp          = {0, };
        ssize_t           size              = 0;
        ssize_t           default_read_size = 0;
        XDR               xdr;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        /* used to reduce the indirection */
        in = &priv->incoming;
        frag = &in->frag;

        switch (frag->call_body.reply.accepted_success_state) {

        case SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT:
                default_read_size = xdr_sizeof ((xdrproc_t) xdr_gfs3_read_rsp,
                                                &read_rsp);

                /* We need to store the current base address because we will
                 * need it after a partial read. */
                in->proghdr_base_addr = frag->fragcurrent;

                __socket_proto_init_pending (priv, default_read_size);

                frag->call_body.reply.accepted_success_state
                        = SP_STATE_READING_PROC_HEADER;

                /* fall through */

        case SP_STATE_READING_PROC_HEADER:
                __socket_proto_read (priv, ret);

                /* there can be 'xdata' in read response, figure it out */
                default_read_size = frag->fragcurrent - in->proghdr_base_addr;
                xdrmem_create (&xdr, in->proghdr_base_addr, default_read_size,
                               XDR_DECODE);

                /* This will fail if there is xdata sent from server, if not,
                   well and good, we don't need to worry about  */
                xdr_gfs3_read_rsp (&xdr, &read_rsp);

                free (read_rsp.xdata.xdata_val);

                /* need to round off to proper roof (%4), as XDR packing pads
                   the end of opaque object with '0' */
                size = roof (read_rsp.xdata.xdata_len, 4);

                if (!size) {
                        frag->call_body.reply.accepted_success_state
                                = SP_STATE_READ_PROC_OPAQUE;
                        goto read_proc_opaque;
                }

                __socket_proto_init_pending (priv, size);

                frag->call_body.reply.accepted_success_state
                        = SP_STATE_READING_PROC_OPAQUE;

        case SP_STATE_READING_PROC_OPAQUE:
                __socket_proto_read (priv, ret);

                frag->call_body.reply.accepted_success_state
                        = SP_STATE_READ_PROC_OPAQUE;

        case SP_STATE_READ_PROC_OPAQUE:
        read_proc_opaque:
                if (in->payload_vector.iov_base == NULL) {

                        size = (RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read);

                        iobuf = iobuf_get2 (this->ctx->iobuf_pool, size);
                        if (iobuf == NULL) {
                                ret = -1;
                                goto out;
                        }

                        if (in->iobref == NULL) {
                                in->iobref = iobref_new ();
                                if (in->iobref == NULL) {
                                        ret = -1;
                                        iobuf_unref (iobuf);
                                        goto out;
                                }
                        }

                        iobref_add (in->iobref, iobuf);
                        iobuf_unref (iobuf);

                        in->payload_vector.iov_base = iobuf_ptr (iobuf);

                        in->payload_vector.iov_len = size;
                }

                frag->fragcurrent = in->payload_vector.iov_base;

                frag->call_body.reply.accepted_success_state
                        = SP_STATE_READ_PROC_HEADER;

                /* fall through */

        case SP_STATE_READ_PROC_HEADER:
                /* now read the entire remaining msg into new iobuf */
                ret = __socket_read_simple_msg (this);
                if ((ret == -1)
                    || ((ret == 0) && RPC_LASTFRAG (in->fraghdr))) {
                        frag->call_body.reply.accepted_success_state
                                = SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT;
                }

                break;
        }

out:
        return ret;
}

#define rpc_reply_verflen_addr(fragcurrent) ((char *)fragcurrent - 4)
#define rpc_reply_accept_status_addr(fragcurrent) ((char *)fragcurrent - 4)

static int
__socket_read_accepted_reply (rpc_transport_t *this)
{
        socket_private_t *priv           = NULL;
        int               ret            = -1;
        char             *buf            = NULL;
        uint32_t          verflen        = 0, len = 0;
        uint32_t          remaining_size = 0;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        /* used to reduce the indirection */
        in = &priv->incoming;
        frag = &in->frag;

        switch (frag->call_body.reply.accepted_state) {

        case SP_STATE_ACCEPTED_REPLY_INIT:
                __socket_proto_init_pending (priv,
                                             RPC_AUTH_FLAVOUR_N_LENGTH_SIZE);

                frag->call_body.reply.accepted_state
                        = SP_STATE_READING_REPLY_VERFLEN;

                /* fall through */

        case SP_STATE_READING_REPLY_VERFLEN:
                __socket_proto_read (priv, ret);

                frag->call_body.reply.accepted_state
                        = SP_STATE_READ_REPLY_VERFLEN;

                /* fall through */

        case SP_STATE_READ_REPLY_VERFLEN:
                buf = rpc_reply_verflen_addr (frag->fragcurrent);

                verflen = ntoh32 (*((uint32_t *) buf));

                /* also read accept status along with verf data */
                len = verflen + RPC_ACCEPT_STATUS_LEN;

                __socket_proto_init_pending (priv, len);

                frag->call_body.reply.accepted_state
                        = SP_STATE_READING_REPLY_VERFBYTES;

                /* fall through */

        case SP_STATE_READING_REPLY_VERFBYTES:
                __socket_proto_read (priv, ret);

                frag->call_body.reply.accepted_state
                        = SP_STATE_READ_REPLY_VERFBYTES;

                buf = rpc_reply_accept_status_addr (frag->fragcurrent);

                frag->call_body.reply.accept_status
                        = ntoh32 (*(uint32_t *) buf);

                /* fall through */

        case SP_STATE_READ_REPLY_VERFBYTES:

                if (frag->call_body.reply.accept_status
                    == SUCCESS) {
                        ret = __socket_read_accepted_successful_reply (this);
                } else {
                        /* read entire remaining msg into buffer pointed to by
                         * fragcurrent
                         */
                        ret = __socket_read_simple_msg (this);
                }

                remaining_size = RPC_FRAGSIZE (in->fraghdr)
                        - frag->bytes_read;

                if ((ret == -1)
                    || ((ret == 0) && (remaining_size == 0)
                        && (RPC_LASTFRAG (in->fraghdr)))) {
                        frag->call_body.reply.accepted_state
                                = SP_STATE_ACCEPTED_REPLY_INIT;
                }

                break;
        }

out:
        return ret;
}


static int
__socket_read_denied_reply (rpc_transport_t *this)
{
        return __socket_read_simple_msg (this);
}


#define rpc_reply_status_addr(fragcurrent) ((char *)fragcurrent - 4)


static int
__socket_read_vectored_reply (rpc_transport_t *this)
{
        socket_private_t *priv           = NULL;
        int               ret            = 0;
        char             *buf            = NULL;
        uint32_t          remaining_size = 0;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        in = &priv->incoming;
        frag = &in->frag;

        switch (frag->call_body.reply.status_state) {

        case SP_STATE_ACCEPTED_REPLY_INIT:
                __socket_proto_init_pending (priv, RPC_REPLY_STATUS_SIZE);

                frag->call_body.reply.status_state
                        = SP_STATE_READING_REPLY_STATUS;

                /* fall through */

        case SP_STATE_READING_REPLY_STATUS:
                __socket_proto_read (priv, ret);

                buf = rpc_reply_status_addr (frag->fragcurrent);

                frag->call_body.reply.accept_status
                        = ntoh32 (*((uint32_t *) buf));

                frag->call_body.reply.status_state
                        = SP_STATE_READ_REPLY_STATUS;

                /* fall through */

        case SP_STATE_READ_REPLY_STATUS:
                if (frag->call_body.reply.accept_status == MSG_ACCEPTED) {
                        ret = __socket_read_accepted_reply (this);
                } else {
                        ret = __socket_read_denied_reply (this);
                }

                remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;

                if ((ret == -1)
                    || ((ret == 0) && (remaining_size == 0)
                        && (RPC_LASTFRAG (in->fraghdr)))) {
                        frag->call_body.reply.status_state
                                = SP_STATE_VECTORED_REPLY_STATUS_INIT;
                        in->payload_vector.iov_len
                                = (unsigned long)frag->fragcurrent
                                - (unsigned long)in->payload_vector.iov_base;
                }
                break;
        }

out:
        return ret;
}


static int
__socket_read_simple_reply (rpc_transport_t *this)
{
        return __socket_read_simple_msg (this);
}

#define rpc_xid_addr(buf) (buf)

static int
__socket_read_reply (rpc_transport_t *this)
{
        socket_private_t   *priv         = NULL;
        char               *buf          = NULL;
        int32_t             ret          = -1;
        rpc_request_info_t *request_info = NULL;
        char                map_xid      = 0;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        in = &priv->incoming;
        frag = &in->frag;

        buf = rpc_xid_addr (iobuf_ptr (in->iobuf));

        if (in->request_info == NULL) {
                in->request_info = GF_CALLOC (1, sizeof (*request_info),
                                              gf_common_mt_rpc_trans_reqinfo_t);
                if (in->request_info == NULL) {
                        goto out;
                }

                map_xid = 1;
        }

        request_info = in->request_info;

        if (map_xid) {
                request_info->xid = ntoh32 (*((uint32_t *) buf));

                /* release priv->lock, so as to avoid deadlock b/w conn->lock
                 * and priv->lock, since we are doing an upcall here.
                 */
                frag->state = SP_STATE_NOTIFYING_XID;
                pthread_mutex_unlock (&priv->lock);
                {
                        ret = rpc_transport_notify (this,
                                                    RPC_TRANSPORT_MAP_XID_REQUEST,
                                                    in->request_info);
                }
                pthread_mutex_lock (&priv->lock);

                /* Transition back to externally visible state. */
                frag->state = SP_STATE_READ_MSGTYPE;

                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "notify for event MAP_XID failed for %s",
                                this->peerinfo.identifier);
                        goto out;
                }
        }

        if ((request_info->prognum == GLUSTER_FOP_PROGRAM)
            && (request_info->procnum == GF_FOP_READ)) {
                if (map_xid && request_info->rsp.rsp_payload_count != 0) {
                        in->iobref = iobref_ref (request_info->rsp.rsp_iobref);
                        in->payload_vector = *request_info->rsp.rsp_payload;
                }

                ret = __socket_read_vectored_reply (this);
        } else {
                ret = __socket_read_simple_reply (this);
        }
out:
        return ret;
}


/* returns the number of bytes yet to be read in a fragment */
static int
__socket_read_frag (rpc_transport_t *this)
{
        socket_private_t *priv           = NULL;
        int32_t           ret            = 0;
        char             *buf            = NULL;
        uint32_t          remaining_size = 0;
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        /* used to reduce the indirection */
        in = &priv->incoming;
        frag = &in->frag;

        switch (frag->state) {
        case SP_STATE_NADA:
                __socket_proto_init_pending (priv, RPC_MSGTYPE_SIZE);

                frag->state = SP_STATE_READING_MSGTYPE;

                /* fall through */

        case SP_STATE_READING_MSGTYPE:
                __socket_proto_read (priv, ret);

                frag->state = SP_STATE_READ_MSGTYPE;
                /* fall through */

        case SP_STATE_READ_MSGTYPE:
                buf = rpc_msgtype_addr (iobuf_ptr (in->iobuf));
                in->msg_type = ntoh32 (*((uint32_t *)buf));

                if (in->msg_type == CALL) {
                        ret = __socket_read_request (this);
                } else if (in->msg_type == REPLY) {
                        ret = __socket_read_reply (this);
                } else if (in->msg_type == (msg_type_t) GF_UNIVERSAL_ANSWER) {
                        gf_log ("rpc", GF_LOG_ERROR,
                                "older version of protocol/process trying to "
                                "connect from %s. use newer version on that node",
                                this->peerinfo.identifier);
                } else {
                        gf_log ("rpc", GF_LOG_ERROR,
                                "wrong MSG-TYPE (%d) received from %s",
                                in->msg_type,
                                this->peerinfo.identifier);
                        ret = -1;
                }

                remaining_size = RPC_FRAGSIZE (in->fraghdr) - frag->bytes_read;

                if ((ret == -1)
                    || ((ret == 0) && (remaining_size == 0)
                        && (RPC_LASTFRAG (in->fraghdr)))) {
                     /* frag->state = SP_STATE_NADA; */
                        frag->state = SP_STATE_RPCFRAG_INIT;
                }

                break;

        case SP_STATE_NOTIFYING_XID:
                /* Another epoll thread is notifying higher layers
                 *of reply's xid. */
                errno = EAGAIN;
                return -1;
                break;

        }

out:
        return ret;
}


static void
__socket_reset_priv (socket_private_t *priv)
{
        struct gf_sock_incoming   *in    = NULL;

        /* used to reduce the indirection */
        in = &priv->incoming;

        if (in->iobref) {
                iobref_unref (in->iobref);
                in->iobref = NULL;
        }

        if (in->iobuf) {
                iobuf_unref (in->iobuf);
                in->iobuf = NULL;
        }

        if (in->request_info != NULL) {
                GF_FREE (in->request_info);
                in->request_info = NULL;
        }

        memset (&in->payload_vector, 0,
                sizeof (in->payload_vector));

}


static int
__socket_proto_state_machine (rpc_transport_t *this,
                              rpc_transport_pollin_t **pollin)
{
        int               ret    = -1;
        socket_private_t *priv   = NULL;
        struct iobuf     *iobuf  = NULL;
        struct iobref    *iobref = NULL;
        struct iovec      vector[2];
        struct gf_sock_incoming      *in         = NULL;
        struct gf_sock_incoming_frag *frag       = NULL;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        /* used to reduce the indirection */
        in = &priv->incoming;
        frag = &in->frag;

        while (in->record_state != SP_STATE_COMPLETE) {
                switch (in->record_state) {

                case SP_STATE_NADA:
                        in->total_bytes_read = 0;
                        in->payload_vector.iov_len = 0;

                        in->pending_vector = in->vector;
                        in->pending_vector->iov_base =  &in->fraghdr;

                        in->pending_vector->iov_len  = sizeof (in->fraghdr);

                        in->record_state = SP_STATE_READING_FRAGHDR;

                        /* fall through */

                case SP_STATE_READING_FRAGHDR:
                        ret = __socket_readv (this, in->pending_vector, 1,
                                              &in->pending_vector,
                                              &in->pending_count,
                                              NULL);
                        if (ret == -1)
                                goto out;

                        if (ret > 0) {
                                gf_log (this->name, GF_LOG_TRACE, "partial "
                                        "fragment header read");
                                goto out;
                        }

                        if (ret == 0) {
                                in->record_state = SP_STATE_READ_FRAGHDR;
                        }
                        /* fall through */

                case SP_STATE_READ_FRAGHDR:

                        in->fraghdr = ntoh32 (in->fraghdr);
                        in->total_bytes_read += RPC_FRAGSIZE(in->fraghdr);

                        if (in->total_bytes_read >= GF_UNIT_GB) {
                                ret = -ENOMEM;
                                goto out;
                        }

                        iobuf = iobuf_get2 (this->ctx->iobuf_pool,
                                            (in->total_bytes_read +
                                             sizeof (in->fraghdr)));
                        if (!iobuf) {
                                ret = -ENOMEM;
                                goto out;
                        }

                        if (in->iobuf == NULL) {
                            /* first fragment */
                            frag->fragcurrent = iobuf_ptr (iobuf);
                        } else {
                            /* second or further fragment */
                            memcpy(iobuf_ptr (iobuf), iobuf_ptr (in->iobuf),
                               in->total_bytes_read - RPC_FRAGSIZE(in->fraghdr));
                            iobuf_unref (in->iobuf);
                            frag->fragcurrent = (char *) iobuf_ptr (iobuf) +
                                in->total_bytes_read - RPC_FRAGSIZE(in->fraghdr);
                            frag->pending_vector->iov_base = frag->fragcurrent;
                            in->pending_vector = frag->pending_vector;
                        }

                        in->iobuf = iobuf;
                        in->iobuf_size = 0;
                        in->record_state = SP_STATE_READING_FRAG;
                        /* fall through */

                case SP_STATE_READING_FRAG:
                        ret = __socket_read_frag (this);

                        if ((ret == -1) ||
                            (frag->bytes_read != RPC_FRAGSIZE (in->fraghdr))) {
                                goto out;
                        }

                        frag->bytes_read = 0;

                        if (!RPC_LASTFRAG (in->fraghdr)) {
                                in->pending_vector = in->vector;
                                in->pending_vector->iov_base = &in->fraghdr;
                                in->pending_vector->iov_len = sizeof(in->fraghdr);
                                in->record_state = SP_STATE_READING_FRAGHDR;
                                break;
                        }

                        /* we've read the entire rpc record, notify the
                         * upper layers.
                         */
                        if (pollin != NULL) {
                                int count = 0;
                                in->iobuf_size = (in->total_bytes_read -
                                                  in->payload_vector.iov_len);

                                memset (vector, 0, sizeof (vector));

                                if (in->iobref == NULL) {
                                        in->iobref = iobref_new ();
                                        if (in->iobref == NULL) {
                                                ret = -1;
                                                goto out;
                                        }
                                }

                                vector[count].iov_base = iobuf_ptr (in->iobuf);
                                vector[count].iov_len = in->iobuf_size;

                                iobref = in->iobref;

                                count++;

                                if (in->payload_vector.iov_base != NULL) {
                                        vector[count] = in->payload_vector;
                                        count++;
                                }

                                *pollin = rpc_transport_pollin_alloc (this,
                                                                      vector,
                                                                      count,
                                                                      in->iobuf,
                                                                      iobref,
                                                                      in->request_info);
                                iobuf_unref (in->iobuf);
                                in->iobuf = NULL;

                                if (*pollin == NULL) {
                                        gf_log (this->name, GF_LOG_WARNING,
                                                "transport pollin allocation failed");
                                        ret = -1;
                                        goto out;
                                }
                                if (in->msg_type == REPLY)
                                        (*pollin)->is_reply = 1;

                                in->request_info = NULL;
                        }
                        in->record_state = SP_STATE_COMPLETE;
                        break;

                case SP_STATE_COMPLETE:
                        /* control should not reach here */
                        gf_log (this->name, GF_LOG_WARNING, "control reached to "
                                "SP_STATE_COMPLETE, which should not have "
                                "happened");
                        break;
                }
        }

        if (in->record_state == SP_STATE_COMPLETE) {
                in->record_state = SP_STATE_NADA;
                __socket_reset_priv (priv);
        }

out:
        if ((ret == -1) && (errno == EAGAIN)) {
                ret = 0;
        }

        return ret;
}


static int
socket_proto_state_machine (rpc_transport_t *this,
                            rpc_transport_pollin_t **pollin)
{
        socket_private_t *priv = NULL;
        int               ret = 0;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        pthread_mutex_lock (&priv->lock);
        {
                ret = __socket_proto_state_machine (this, pollin);
        }
        pthread_mutex_unlock (&priv->lock);

out:
        return ret;
}


static int
socket_event_poll_in (rpc_transport_t *this, gf_boolean_t notify_handled)
{
        int                     ret    = -1;
        rpc_transport_pollin_t *pollin = NULL;
        socket_private_t       *priv = this->private;
        glusterfs_ctx_t        *ctx  = NULL;

        ctx = this->ctx;

	ret = socket_proto_state_machine (this, &pollin);

        if (pollin) {
                pthread_mutex_lock (&priv->notify.lock);
                {
                        priv->notify.in_progress++;
                }
                pthread_mutex_unlock (&priv->notify.lock);
        }


        if (notify_handled && (ret != -1))
                event_handled (ctx->event_pool, priv->sock, priv->idx,
                               priv->gen);

	if (pollin) {
                priv->ot_state = OT_CALLBACK;

                ret = rpc_transport_notify (this, RPC_TRANSPORT_MSG_RECEIVED,
                                            pollin);

                if (priv->ot_state == OT_CALLBACK) {
                        priv->ot_state = OT_RUNNING;
                }

                rpc_transport_pollin_destroy (pollin);

                pthread_mutex_lock (&priv->notify.lock);
                {
                        --priv->notify.in_progress;

                        if (!priv->notify.in_progress)
                                pthread_cond_signal (&priv->notify.cond);
                }
                pthread_mutex_unlock (&priv->notify.lock);
        }

        return ret;
}


static int
socket_connect_finish (rpc_transport_t *this)
{
        int                   ret        = -1;
        socket_private_t     *priv       = NULL;
        rpc_transport_event_t event      = 0;
        char                  notify_rpc = 0;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        pthread_mutex_lock (&priv->lock);
        {
                if (priv->connected != 0)
                        goto unlock;

                get_transport_identifiers (this);

                ret = __socket_connect_finish (priv->sock);

                if (ret == -1 && errno == EINPROGRESS)
                        ret = 1;

                if (ret == -1 && errno != EINPROGRESS) {
                        if (!priv->connect_finish_log) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "connection to %s failed (%s); "
                                        "disconnecting socket",
                                        this->peerinfo.identifier,
                                        strerror (errno));
                                priv->connect_finish_log = 1;
                        }
                        __socket_disconnect (this);
                        goto unlock;
                }

                if (ret == 0) {
                        notify_rpc = 1;

                        this->myinfo.sockaddr_len =
                                sizeof (this->myinfo.sockaddr);

                        ret = getsockname (priv->sock,
                                           SA (&this->myinfo.sockaddr),
                                           &this->myinfo.sockaddr_len);
                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_WARNING,
                                        "getsockname on (%d) failed (%s) - "
                                        "disconnecting socket",
                                        priv->sock, strerror (errno));
                                __socket_disconnect (this);
                                event = RPC_TRANSPORT_DISCONNECT;
                                goto unlock;
                        }

                        priv->connected = 1;
                        priv->connect_finish_log = 0;
                        event = RPC_TRANSPORT_CONNECT;
                }
        }
unlock:
        pthread_mutex_unlock (&priv->lock);

        if (notify_rpc) {
                rpc_transport_notify (this, event, this);
        }
out:
        return ret;
}

static int socket_disconnect (rpc_transport_t *this, gf_boolean_t wait);

/* reads rpc_requests during pollin */
static int
socket_event_handler (int fd, int idx, int gen, void *data,
                      int poll_in, int poll_out, int poll_err)
{
        rpc_transport_t  *this          = NULL;
        socket_private_t *priv          = NULL;
	int               ret           = -1;
        glusterfs_ctx_t  *ctx           = NULL;
        gf_boolean_t      socket_closed = _gf_false, notify_handled = _gf_false;

        this = data;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);
        GF_VALIDATE_OR_GOTO ("socket", this->xl, out);

        THIS = this->xl;
        priv = this->private;
        ctx = this->ctx;

        pthread_mutex_lock (&priv->lock);
        {
                priv->idx = idx;
                priv->gen = gen;
        }
        pthread_mutex_unlock (&priv->lock);

        if (priv->connected != 1) {
                if (priv->connect_failed) {
                        /* connect failed with some other error than
                        EINPROGRESS or ENOENT, so nothing more to do, fail
                        reading/writing anything even if poll_in or poll_out
                        is set */
                        gf_log ("transport", GF_LOG_DEBUG,
                                "connect failed with some other error than "
                                "EINPROGRESS or ENOENT, so nothing more to "
                                "do; disconnecting socket");
                        ret = socket_disconnect (this, _gf_false);

                        /* Force ret to be -1, as we are officially done with
                        this socket */
                        ret = -1;
                } else {
                        ret = socket_connect_finish (this);
                }
        } else {
                ret = 0;
        }

        if (!ret && poll_out) {
                ret = socket_event_poll_out (this);
        }

        if (!ret && poll_in) {
                ret = socket_event_poll_in (this, !poll_err);
                notify_handled = _gf_true;
        }

        if ((ret < 0) || poll_err) {
                /* Logging has happened already in earlier cases */
                gf_log ("transport", ((ret >= 0) ? GF_LOG_INFO : GF_LOG_DEBUG),
                        "EPOLLERR - disconnecting now");

                socket_closed = socket_event_poll_err (this, gen, idx);

                if (socket_closed)
                        rpc_transport_unref (this);

	} else if (!notify_handled) {
                event_handled (ctx->event_pool, fd, idx, gen);
        }

out:
	return ret;
}

static int poll_err_cnt;
static void *
socket_poller (void *ctx)
{
        rpc_transport_t  *this = ctx;
        socket_private_t *priv = this->private;
	struct pollfd     pfd[2] = {{0,},};
	gf_boolean_t      to_write = _gf_false;
	int               ret = 0;
        uint32_t          gen = 0;
        char             *cname = NULL;

        GF_ASSERT (this);
        /* Set THIS early on in the life of this thread, instead of setting it
         * conditionally
         */
        THIS = this->xl;

        if (priv->ot_state == OT_PLEASE_DIE) {
                gf_log (this->name, GF_LOG_DEBUG, "socket_poller is exiting "
                        "because socket state is OT_PLEASE_DIE");
                goto err;
        }

        priv->ot_state = OT_RUNNING;

        if (priv->use_ssl) {
                cname = ssl_setup_connection(this,priv->connected);
                if (!cname) {
                        gf_log (this->name,GF_LOG_ERROR, "%s setup failed",
                                priv->connected ? "server" : "client");
                        goto err;
                }
                if (priv->connected) {
                        this->ssl_name = cname;
                }
                else {
                        GF_FREE(cname);
                }
        }

        if (!priv->bio) {
                ret = __socket_nonblock (priv->sock);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "NBIO on %d failed (%s)",
                                priv->sock, strerror (errno));
                        goto err;
                }
        }

        if (priv->connected == 0) {
                ret = socket_connect_finish (this);
                if (ret != 0) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "asynchronous socket_connect_finish failed");
                }
        }

        ret = rpc_transport_notify (this->listener,
                                    RPC_TRANSPORT_ACCEPT, this);
        if (ret != 0) {
                gf_log (this->name, GF_LOG_WARNING,
                        "asynchronous rpc_transport_notify failed");
        }

        gen = priv->ot_gen;
	for (;;) {
		pthread_mutex_lock(&priv->lock);
		to_write = !list_empty(&priv->ioq);
		pthread_mutex_unlock(&priv->lock);
		pfd[0].fd = priv->pipe[0];
		pfd[0].events = POLL_MASK_ERROR;
		pfd[0].revents = 0;
		pfd[1].fd = priv->sock;
		pfd[1].events = POLL_MASK_INPUT | POLL_MASK_ERROR;
		pfd[1].revents = 0;
		if (to_write) {
			pfd[1].events |= POLL_MASK_OUTPUT;
		}
		else {
			pfd[0].events |= POLL_MASK_INPUT;
		}
		if (poll(pfd,2,-1) < 0) {
			gf_log(this->name,GF_LOG_ERROR,"poll failed");
			break;
		}
		if (pfd[0].revents & POLL_MASK_ERROR) {
			gf_log(this->name,GF_LOG_ERROR,
			       "poll error on pipe");
			break;
		}

                if (priv->ot_state == OT_PLEASE_DIE) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "OT_PLEASE_DIE on %p (exiting socket_poller)",
                                this);
                        break;
                }

		if (pfd[1].revents & POLL_MASK_INPUT) {
			ret = socket_event_poll_in(this, 0);
			if (ret >= 0) {
				/* Suppress errors while making progress. */
				pfd[1].revents &= ~POLL_MASK_ERROR;
			}
			else if (errno == ENOTCONN) {
				ret = 0;
			}
                        if (priv->ot_state == OT_PLEASE_DIE) {
                                gf_log (this->name, GF_LOG_TRACE,
                                        "OT_IDLE on %p (input request)",
                                        this);
                                break;
                        }
		}
		else if (pfd[1].revents & POLL_MASK_OUTPUT) {
			ret = socket_event_poll_out(this);
			if (ret >= 0) {
				/* Suppress errors while making progress. */
				pfd[1].revents &= ~POLL_MASK_ERROR;
			}
			else if (errno == ENOTCONN) {
				ret = 0;
			}
                        if (priv->ot_state == OT_PLEASE_DIE) {
                                gf_log (this->name, GF_LOG_TRACE,
                                        "OT_IDLE on %p (output request)",
                                        this);
                                break;
                        }
		}
		else {
			/*
			 * This usually means that we left poll() because
			 * somebody pushed a byte onto our pipe.  That wakeup
			 * is why the pipe is there, but once awake we can do
			 * all the checking we need on the next iteration.
			 */
			ret = 0;
		}
		if (pfd[1].revents & POLL_MASK_ERROR) {
			gf_log(this->name,GF_LOG_ERROR,
			       "poll error on socket");
			break;
		}
		if (ret < 0) {
                        GF_LOG_OCCASIONALLY (poll_err_cnt, this->name,
                                             GF_LOG_ERROR,
                                             "socket_poller %s failed (%s)",
                                             this->peerinfo.identifier,
                                             strerror (errno));
			break;
		}
                if (priv->ot_gen != gen) {
                        gf_log (this->name, GF_LOG_TRACE,
                                "generation mismatch, my %u != %u",
                                gen, priv->ot_gen);
                        return NULL;
                }
	}

err:
	/* All (and only) I/O errors should come here. */
        pthread_mutex_lock(&priv->lock);
        {
                gf_log (this->name, GF_LOG_TRACE, "disconnecting socket");
                __socket_teardown_connection (this);
                sys_close (priv->sock);
                priv->sock = -1;

                sys_close (priv->pipe[0]);
                sys_close (priv->pipe[1]);
                priv->pipe[0] = -1;
                priv->pipe[1] = -1;

                priv->ot_state = OT_IDLE;
        }
        pthread_mutex_unlock(&priv->lock);

        rpc_transport_notify (this, RPC_TRANSPORT_DISCONNECT, this);

        GF_REF_PUT (priv);

        rpc_transport_unref (this);

	return NULL;
}


static int
socket_spawn (rpc_transport_t *this)
{
        socket_private_t        *priv   = this->private;
        int ret = -1;
        switch (priv->ot_state) {
        case OT_IDLE:
        case OT_PLEASE_DIE:
                break;
        default:
                gf_log (this->name, GF_LOG_WARNING,
                        "refusing to start redundant poller");
                return ret;
        }

        priv->ot_gen += 7;
        priv->ot_state = OT_SPAWNING;
        gf_log (this->name, GF_LOG_TRACE,
                "spawning %p with gen %u", this, priv->ot_gen);

        GF_REF_GET (priv);

        /* Create thread after enable detach flag */

        ret = gf_thread_create_detached (&priv->thread, socket_poller, this,
                                         "spoller");
        if (ret) {
                gf_log (this->name, GF_LOG_ERROR,
                        "could not create poll thread");
                ret = -1;
        }

        return ret;
}

static int
socket_server_event_handler (int fd, int idx, int gen, void *data,
                             int poll_in, int poll_out, int poll_err)
{
        rpc_transport_t             *this = NULL;
        socket_private_t        *priv = NULL;
        int                      ret = 0;
        int                      new_sock = -1;
        rpc_transport_t             *new_trans = NULL;
        struct sockaddr_storage  new_sockaddr = {0, };
        socklen_t                addrlen = sizeof (new_sockaddr);
        socket_private_t        *new_priv = NULL;
        glusterfs_ctx_t         *ctx = NULL;
        char                    *cname = NULL;

        this = data;
        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);
        GF_VALIDATE_OR_GOTO ("socket", this->xl, out);

        THIS = this->xl;
        priv = this->private;
        ctx  = this->ctx;

        /* NOTE:
         * We have done away with the critical section in this function. since
         * there's little that it helps with. There's no other code that
         * attempts to unref the listener socket/transport from any other
         * thread context while we are using it here.
         */
        priv->idx = idx;

        if (poll_in) {
                new_sock = accept (priv->sock, SA (&new_sockaddr), &addrlen);

                if (new_sock == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "accept on %d failed (%s)",
                                priv->sock, strerror (errno));
                        goto out;
                }

                if (priv->nodelay && (new_sockaddr.ss_family != AF_UNIX)) {
                        ret = __socket_nodelay (new_sock);
                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_WARNING,
                                        "setsockopt() failed for "
                                        "NODELAY (%s)",
                                        strerror (errno));
                        }
                }

                if (priv->keepalive &&
                                new_sockaddr.ss_family != AF_UNIX) {
                        ret = __socket_keepalive (new_sock,
                                                  new_sockaddr.ss_family,
                                                  priv->keepaliveintvl,
                                                  priv->keepaliveidle,
                                                  priv->keepalivecnt,
                                                  priv->timeout);
                        if (ret == -1)
                                gf_log (this->name, GF_LOG_WARNING,
                                        "Failed to set keep-alive: %s",
                                        strerror (errno));
                }

                new_trans = GF_CALLOC (1, sizeof (*new_trans),
                                gf_common_mt_rpc_trans_t);
                if (!new_trans) {
                        sys_close (new_sock);
                        goto out;
                }

                ret = pthread_mutex_init(&new_trans->lock, NULL);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "pthread_mutex_init() failed: %s",
                                strerror (errno));
                        sys_close (new_sock);
                        GF_FREE (new_trans);
                        goto out;
                }
                INIT_LIST_HEAD (&new_trans->list);

                new_trans->name = gf_strdup (this->name);

                memcpy (&new_trans->peerinfo.sockaddr, &new_sockaddr, addrlen);
                new_trans->peerinfo.sockaddr_len = addrlen;

                new_trans->myinfo.sockaddr_len = sizeof (new_trans->myinfo.sockaddr);

                ret = getsockname (new_sock, SA (&new_trans->myinfo.sockaddr),
                                   &new_trans->myinfo.sockaddr_len);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "getsockname on %d failed (%s)",
                                new_sock, strerror (errno));
                        sys_close (new_sock);
                        GF_FREE (new_trans->name);
                        GF_FREE (new_trans);
                        goto out;
                }

                get_transport_identifiers (new_trans);
                ret = socket_init(new_trans);
                if (ret != 0) {
                        sys_close (new_sock);
                        GF_FREE (new_trans->name);
                        GF_FREE (new_trans);
                        goto out;
                }
                new_trans->ops = this->ops;
                new_trans->init = this->init;
                new_trans->fini = this->fini;
                new_trans->ctx  = ctx;
                new_trans->xl   = this->xl;
                new_trans->mydata = this->mydata;
                new_trans->notify = this->notify;
                new_trans->listener = this;
                new_priv = new_trans->private;

                if (new_sockaddr.ss_family == AF_UNIX) {
                        new_priv->use_ssl = _gf_false;
                } else {
                        switch (priv->srvr_ssl) {
                                case MGMT_SSL_ALWAYS:
                                        /* Glusterd with secure_mgmt. */
                                        new_priv->use_ssl = _gf_true;
                                        break;
                                case MGMT_SSL_COPY_IO:
                                        /* Glusterfsd. */
                                        new_priv->use_ssl = priv->ssl_enabled;
                                        break;
                                default:
                                        new_priv->use_ssl = _gf_false;
                        }
                }

                new_priv->sock = new_sock;
                new_priv->own_thread = priv->own_thread;

                new_priv->ssl_ctx = priv->ssl_ctx;
                if (new_priv->use_ssl && !new_priv->own_thread) {
                        cname = ssl_setup_connection(new_trans, 1);
                        if (!cname) {
                                gf_log(this->name, GF_LOG_ERROR,
                                       "server setup failed");
                                sys_close (new_sock);
                                GF_FREE (new_trans->name);
                                GF_FREE (new_trans);
                                goto out;
                        }
                        this->ssl_name = cname;
                }

                if (!priv->bio && !priv->own_thread) {
                        ret = __socket_nonblock (new_sock);

                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_WARNING,
                                        "NBIO on %d failed (%s)",
                                        new_sock, strerror (errno));

                                sys_close (new_sock);
                                GF_FREE (new_trans->name);
                                GF_FREE (new_trans);
                                goto out;
                        }
                }

                /*
                 * In the own_thread case, this is used to
                 * indicate that we're initializing a server
                 * connection.
                 */
                new_priv->connected = 1;
                new_priv->is_server = _gf_true;
                rpc_transport_ref (new_trans);

                if (new_priv->own_thread) {
                        if (pipe(new_priv->pipe) < 0) {
                                gf_log(this->name, GF_LOG_ERROR,
                                       "could not create pipe");
                        }
                        ret = socket_spawn(new_trans);
                        if (ret) {
                                gf_log(this->name, GF_LOG_ERROR,
                                       "could not spawn thread");
                                sys_close (new_priv->pipe[0]);
                                sys_close (new_priv->pipe[1]);
                        }
                }  else {
                        /* Take a ref on the new_trans to avoid
                         * getting deleted when event_register()
                         * causes socket_event_handler() to race
                         * ahead of this path to eventually find
                         * a disconnect and unref the transport
                         */
                        rpc_transport_ref (new_trans);

                        /* Send a notification to RPCSVC layer
                         * to save the new_trans in its service
                         * list before we register the new_sock
                         * with epoll to begin receiving notifications
                         * for data handling.
                         */
                        ret = rpc_transport_notify (this, RPC_TRANSPORT_ACCEPT, new_trans);

                        if (ret != -1) {
                                new_priv->idx =
                                        event_register (ctx->event_pool,
                                                        new_sock,
                                                        socket_event_handler,
                                                        new_trans,
                                                        1, 0);
                                if (new_priv->idx == -1) {
                                        ret = -1;
                                        gf_log(this->name, GF_LOG_ERROR,
                                               "failed to register the socket "
                                               "with event");

                                        /* event_register() could have failed for some
                                         * reason, implying that the new_sock cannot be
                                         * added to the epoll set. If we wont get any
                                         * more notifications for new_sock from epoll,
                                         * then we better remove the corresponding
                                         * new_trans object from the RPCSVC service list.
                                         * Since we've notified RPC service of new_trans
                                         * before we attempted event_register(), we better
                                         * unlink the new_trans from the RPCSVC service list
                                         * to cleanup the stateby sending out a DISCONNECT
                                         * notification.
                                         */
                                        rpc_transport_notify (this, RPC_TRANSPORT_DISCONNECT, new_trans);
                                }
                        }

                        /* this rpc_transport_unref() is for managing race between
                         * 1. socket_server_event_handler and
                         * 2. socket_event_handler
                         * trying to add and remove new_trans from the rpcsvc
                         * service list
                         * now that we are done with the notifications, lets
                         * reduce the reference
                         */
                        rpc_transport_unref (new_trans);
                }

                if (ret == -1) {
                        sys_close (new_sock);
                        /* this unref is to actually cause the destruction of
                         * the new_trans since we've failed at everything so far
                         */
                        rpc_transport_unref (new_trans);
                }
        }
out:
        event_handled (ctx->event_pool, fd, idx, gen);

        if (cname && (cname != this->ssl_name)) {
                GF_FREE(cname);
        }
        return ret;
}


static int
socket_disconnect (rpc_transport_t *this, gf_boolean_t wait)
{
        socket_private_t *priv   = NULL;
        int               ret    = -1;
        char              a_byte = 'r';

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;

        if (wait && priv->own_thread) {
                GF_REF_PUT (priv);

                pthread_mutex_lock (&priv->cond_lock);
                {
                        /* Change the state to OT_PLEASE_DIE so that
                         * socket_poller can exit. */
                        priv->ot_state = OT_PLEASE_DIE;
                        /* Write something into the pipe so that poller
                         * thread can wake up.*/
                        if (sys_write (priv->pipe[1], &a_byte, 1) < 1) {
                                gf_log (this->name, GF_LOG_WARNING,
                                                "write error on pipe");
                        }

                        /* Wait for socket_poller to exit */
                        if (!priv->own_thread_done)
                                pthread_cond_wait (&priv->cond,
                                                   &priv->cond_lock);
                }
                pthread_mutex_unlock (&priv->cond_lock);
        }

        pthread_mutex_lock (&priv->lock);
        {
                ret = __socket_disconnect (this);
        }
        pthread_mutex_unlock (&priv->lock);

out:
        return ret;
}

void*
socket_connect_error_cbk (void *opaque)
{
        socket_connect_error_state_t *arg;

        GF_ASSERT (opaque);

        arg = opaque;
        THIS = arg->this;

        rpc_transport_notify (arg->trans, RPC_TRANSPORT_DISCONNECT, arg->trans);

        if (arg->refd)
                rpc_transport_unref (arg->trans);

        GF_FREE (opaque);
        return NULL;
}

static void
socket_fix_ssl_opts (rpc_transport_t *this, socket_private_t *priv,
                     uint16_t port)
{
        if (port == GF_DEFAULT_SOCKET_LISTEN_PORT) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "%s SSL for portmapper connection",
                        priv->mgmt_ssl ? "enabling" : "disabling");
                priv->use_ssl = priv->mgmt_ssl;
        }
        else if (priv->ssl_enabled && !priv->use_ssl) {
                gf_log(this->name,GF_LOG_DEBUG,
                       "re-enabling SSL for I/O connection");
                priv->use_ssl = _gf_true;
        }
}

/*
 * If we might just be trying to connect prematurely, e.g. to a brick that's
 * slow coming up, all we need is a simple retry.  Don't worry about sleeping
 * in some arbitrary thread.  The connect(2) could already have the exact same
 * effect, and we deal with it in that case so we can deal with it for sleep(2)
 * as well.
 */
static int
connect_loop (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
        int     ret;
        int     connect_fails   = 0;

        for (;;) {
                ret = connect (sockfd, addr, addrlen);
                if (ret >= 0) {
                        break;
                }
                if ((errno != ENOENT) || (++connect_fails >= 5)) {
                        break;
                }
                sleep (1);
        }

        return ret;
}

static int
socket_connect (rpc_transport_t *this, int port)
{
        int                            ret             = -1;
        int                            th_ret          = -1;
        int                            sock            = -1;
        socket_private_t              *priv            = NULL;
        socklen_t                      sockaddr_len    = 0;
        glusterfs_ctx_t               *ctx             = NULL;
        sa_family_t                    sa_family       = {0, };
        char                          *local_addr      = NULL;
        union gf_sock_union            sock_union;
        struct sockaddr_in            *addr            = NULL;
        gf_boolean_t                   refd      = _gf_false;
        socket_connect_error_state_t  *arg             = NULL;
        pthread_t                      th_id           = {0, };
        char                          *cname           = NULL;
        gf_boolean_t                   ign_enoent      = _gf_false;

        GF_VALIDATE_OR_GOTO ("socket", this, err);
        GF_VALIDATE_OR_GOTO ("socket", this->private, err);

        priv = this->private;
        ctx = this->ctx;

        if (!priv) {
                gf_log_callingfn (this->name, GF_LOG_WARNING,
                        "connect() called on uninitialized transport");
                goto err;
        }

        pthread_mutex_lock (&priv->lock);
        {
                priv->own_thread_done = _gf_false;
                if (priv->sock != -1) {
                        gf_log_callingfn (this->name, GF_LOG_TRACE,
                                          "connect () called on transport "
                                          "already connected");
                        errno = EINPROGRESS;
                        ret = -1;
                        goto unlock;
                }

                gf_log (this->name, GF_LOG_TRACE,
                        "connecting %p, state=%u gen=%u sock=%d", this,
                        priv->ot_state, priv->ot_gen, priv->sock);

                ret = socket_client_get_remote_sockaddr (this, &sock_union.sa,
                                                     &sockaddr_len, &sa_family);
                if (ret == -1) {
                        /* logged inside client_get_remote_sockaddr */
                        goto unlock;
                }

                if (sa_family == AF_UNIX) {
                        priv->ssl_enabled = _gf_false;
                        priv->mgmt_ssl = _gf_false;
                }
                else {
                        if (port > 0) {
                                sock_union.sin.sin_port = htons (port);
                        }
                        socket_fix_ssl_opts (this, priv,
                                             ntohs(sock_union.sin.sin_port));
                }

                memcpy (&this->peerinfo.sockaddr, &sock_union.storage,
                        sockaddr_len);
                this->peerinfo.sockaddr_len = sockaddr_len;

                priv->sock = socket (sa_family, SOCK_STREAM, 0);
                if (priv->sock == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "socket creation failed (%s)",
                                strerror (errno));
                        ret = -1;
                        goto unlock;
                }

                /* Cant help if setting socket options fails. We can continue
                 * working nonetheless.
                 */
                if (priv->windowsize != 0) {
                        if (setsockopt (priv->sock, SOL_SOCKET, SO_RCVBUF,
                                        &priv->windowsize,
                                        sizeof (priv->windowsize)) < 0) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "setting receive window "
                                        "size failed: %d: %d: %s",
                                        priv->sock, priv->windowsize,
                                        strerror (errno));
                        }

                        if (setsockopt (priv->sock, SOL_SOCKET, SO_SNDBUF,
                                        &priv->windowsize,
                                        sizeof (priv->windowsize)) < 0) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "setting send window size "
                                        "failed: %d: %d: %s",
                                        priv->sock, priv->windowsize,
                                        strerror (errno));
                        }
                }


                if (priv->nodelay && (sa_family != AF_UNIX)) {
                        ret = __socket_nodelay (priv->sock);

                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "NODELAY on %d failed (%s)",
                                        priv->sock, strerror (errno));
                        }
                }

                if (priv->keepalive && sa_family != AF_UNIX) {
                        ret = __socket_keepalive (priv->sock,
                                                  sa_family,
                                                  priv->keepaliveintvl,
                                                  priv->keepaliveidle,
                                                  priv->keepalivecnt,
                                                  priv->timeout);
                        if (ret == -1)
                                gf_log (this->name, GF_LOG_ERROR,
                                        "Failed to set keep-alive: %s",
                                        strerror (errno));
                }

                SA (&this->myinfo.sockaddr)->sa_family =
                        SA (&this->peerinfo.sockaddr)->sa_family;

                /* If a source addr is explicitly specified, use it */
                ret = dict_get_str (this->options,
                                    "transport.socket.source-addr",
                                    &local_addr);
                if (!ret && SA (&this->myinfo.sockaddr)->sa_family == AF_INET) {
                        addr = (struct sockaddr_in *)(&this->myinfo.sockaddr);
                        ret = inet_pton (AF_INET, local_addr,
                                         &(addr->sin_addr.s_addr));
                }

                /* If client wants ENOENT to be ignored */
               ign_enoent = dict_get_str_boolean (this->options,
                   "transport.socket.ignore-enoent", _gf_false);

                ret = client_bind (this, SA (&this->myinfo.sockaddr),
                                   &this->myinfo.sockaddr_len, priv->sock);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "client bind failed: %s", strerror (errno));
                        goto handler;
                }

                if (!priv->use_ssl && !priv->bio && !priv->own_thread) {
                        ret = __socket_nonblock (priv->sock);
                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "NBIO on %d failed (%s)",
                                        priv->sock, strerror (errno));
                                goto handler;
                        }
                }

                this->connect_failed = _gf_false;
                if (ign_enoent) {
                        ret = connect_loop (priv->sock,
                                            SA (&this->peerinfo.sockaddr),
                                            this->peerinfo.sockaddr_len);
                } else {
                        ret = connect (priv->sock,
                                       SA (&this->peerinfo.sockaddr),
                                       this->peerinfo.sockaddr_len);
                }

                if (ret == -1 && errno == ENOENT && ign_enoent) {
                        gf_log (this->name, GF_LOG_WARNING,
                               "Ignore failed connection attempt on %s, (%s) ",
                                this->peerinfo.identifier, strerror (errno));

                        /* connect failed with some other error than EINPROGRESS
                        so, getsockopt (... SO_ERROR ...), will not catch any
                        errors and return them to us, we need to remember this
                        state, and take actions in socket_event_handler
                        appropriately */
                        /* TBD: What about ENOENT, we will do getsockopt there
                        as well, so how is that exempt from such a problem? */
                        priv->connect_failed = 1;
                        this->connect_failed = _gf_true;

                        goto handler;
                }

                if (ret == -1 && ((errno != EINPROGRESS) && (errno != ENOENT))) {
                        /* For unix path based sockets, the socket path is
                         * cryptic (md5sum of path) and may not be useful for
                         * the user in debugging so log it in DEBUG
                         */
                        gf_log (this->name, ((sa_family == AF_UNIX) ?
                                GF_LOG_DEBUG : GF_LOG_ERROR),
                                "connection attempt on %s failed, (%s)",
                                this->peerinfo.identifier, strerror (errno));

                        /* connect failed with some other error than EINPROGRESS
                        so, getsockopt (... SO_ERROR ...), will not catch any
                        errors and return them to us, we need to remember this
                        state, and take actions in socket_event_handler
                        appropriately */
                        /* TBD: What about ENOENT, we will do getsockopt there
                        as well, so how is that exempt from such a problem? */
                        priv->connect_failed = 1;

                        goto handler;
                }
                else {
                        /* reset connect_failed so that any previous attempts
                        state is not carried forward */
                        priv->connect_failed = 0;
                        ret = 0;
                }

                if (priv->use_ssl && !priv->own_thread) {
                        cname = ssl_setup_connection(this,0);
                        if (!cname) {
                                errno = ENOTCONN;
                                ret = -1;
                                gf_log(this->name,GF_LOG_ERROR,
                                       "client setup failed");
                                goto handler;
                        }
                        if (priv->connected) {
                                this->ssl_name = cname;
                        }
                        else {
                                GF_FREE(cname);
                        }
                }

                if (!priv->bio && !priv->own_thread) {
                        ret = __socket_nonblock (priv->sock);

                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "NBIO on %d failed (%s)",
                                        priv->sock, strerror (errno));
                                goto handler;
                        }
                }

handler:
                if (ret < 0) {
                        /* Ignore error from connect. epoll events
                           should be handled in the socket handler.  shutdown(2)
                           will result in EPOLLERR, so cleanup is done in
                           socket_event_handler or socket_poller */
                        shutdown (priv->sock, SHUT_RDWR);
                }

                /*
                 * In the own_thread case, this is used to indicate that we're
                 * initializing a client connection.
                 */
                priv->connected = 0;
                priv->is_server = _gf_false;
                rpc_transport_ref (this);
                refd = _gf_true;

                if (priv->own_thread) {
                        if (priv->connect_failed) {
                                gf_msg_debug (this->name, 0,
                                              "socket connect is failed so close it");
                                sys_close (priv->sock);
                                priv->sock = -1;
                                ret = -1;
                                goto unlock;
                        }

                        if (pipe(priv->pipe) < 0) {
                                gf_log(this->name,GF_LOG_ERROR,
                                "could not create pipe");
                        }

                        this->listener = this;
                        ret =  socket_spawn(this);
                        if (ret) {
                                gf_log(this->name, GF_LOG_ERROR,
                                       "could not spawn thread");
                                sys_close (priv->pipe[0]);
                                sys_close (priv->pipe[1]);
                                sys_close (priv->sock);
                                priv->sock = -1;
                        }
                }
                else {
                        priv->idx = event_register (ctx->event_pool, priv->sock,
                                                    socket_event_handler,
                                                    this, 1, 1);
                        if (priv->idx == -1) {
                                gf_log ("", GF_LOG_WARNING,
                                        "failed to register the event");
                                sys_close (priv->sock);
                                priv->sock = -1;
                                ret = -1;
                        }
                }

unlock:
                sock = priv->sock;
        }
        pthread_mutex_unlock (&priv->lock);

err:
        /* if sock != -1, then cleanup is done from the event handler */
        if (ret == -1 && sock == -1) {
                /* Cleaup requires to send notification to upper layer which
                   intern holds the big_lock. There can be dead-lock situation
                   if big_lock is already held by the current thread.
                   So transfer the ownership to seperate thread for cleanup.
                */
                arg = GF_CALLOC (1, sizeof (*arg),
                                 gf_sock_connect_error_state_t);
                arg->this = THIS;
                arg->trans = this;
                arg->refd = refd;
                th_ret = gf_thread_create_detached (&th_id,
                                                    socket_connect_error_cbk,
                                                    arg, "scleanup");
                if (th_ret) {
                        /* Error will be logged by gf_thread_create_attached */
                        gf_log (this->name, GF_LOG_ERROR, "Thread creation "
                               "failed");
                        GF_FREE (arg);
                        GF_ASSERT (0);
                }
        }

        return ret;
}


static int
socket_listen (rpc_transport_t *this)
{
        socket_private_t *       priv = NULL;
        int                      ret = -1;
        int                      sock = -1;
        struct sockaddr_storage  sockaddr;
        socklen_t                sockaddr_len = 0;
        peer_info_t             *myinfo = NULL;
        glusterfs_ctx_t         *ctx = NULL;
        sa_family_t              sa_family = {0, };

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv   = this->private;
        myinfo = &this->myinfo;
        ctx    = this->ctx;

        pthread_mutex_lock (&priv->lock);
        {
                sock = priv->sock;
        }
        pthread_mutex_unlock (&priv->lock);

        if (sock != -1)  {
                gf_log_callingfn (this->name, GF_LOG_DEBUG,
                                  "already listening");
                return ret;
        }

        ret = socket_server_get_local_sockaddr (this, SA (&sockaddr),
                                                &sockaddr_len, &sa_family);
        if (ret == -1) {
                return ret;
        }

        pthread_mutex_lock (&priv->lock);
        {
                if (priv->sock != -1) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "already listening");
                        goto unlock;
                }

                memcpy (&myinfo->sockaddr, &sockaddr, sockaddr_len);
                myinfo->sockaddr_len = sockaddr_len;

                priv->sock = socket (sa_family, SOCK_STREAM, 0);

                if (priv->sock == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "socket creation failed (%s)",
                                strerror (errno));
                        goto unlock;
                }

                /* Cant help if setting socket options fails. We can continue
                 * working nonetheless.
                 */
                if (priv->windowsize != 0) {
                        if (setsockopt (priv->sock, SOL_SOCKET, SO_RCVBUF,
                                        &priv->windowsize,
                                        sizeof (priv->windowsize)) < 0) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "setting receive window size "
                                        "failed: %d: %d: %s", priv->sock,
                                        priv->windowsize,
                                        strerror (errno));
                        }

                        if (setsockopt (priv->sock, SOL_SOCKET, SO_SNDBUF,
                                        &priv->windowsize,
                                        sizeof (priv->windowsize)) < 0) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "setting send window size failed:"
                                        " %d: %d: %s", priv->sock,
                                        priv->windowsize,
                                        strerror (errno));
                        }
                }

                if (priv->nodelay && (sa_family != AF_UNIX)) {
                        ret = __socket_nodelay (priv->sock);
                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "setsockopt() failed for NODELAY (%s)",
                                        strerror (errno));
                        }
                }

                if (!priv->bio) {
                        ret = __socket_nonblock (priv->sock);

                        if (ret == -1) {
                                gf_log (this->name, GF_LOG_ERROR,
                                        "NBIO on %d failed (%s)",
                                        priv->sock, strerror (errno));
                                sys_close (priv->sock);
                                priv->sock = -1;
                                goto unlock;
                        }
                }

                ret = __socket_server_bind (this);

                if ((ret == -EADDRINUSE) || (ret == -1)) {
                        /* logged inside __socket_server_bind() */
                        sys_close (priv->sock);
                        priv->sock = -1;
                        goto unlock;
                }

                ret = listen (priv->sock, priv->backlog);

                if (ret == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "could not set socket %d to listen mode (%s)",
                                priv->sock, strerror (errno));
                        sys_close (priv->sock);
                        priv->sock = -1;
                        goto unlock;
                }

                rpc_transport_ref (this);

                priv->idx = event_register (ctx->event_pool, priv->sock,
                                            socket_server_event_handler,
                                            this, 1, 0);

                if (priv->idx == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "could not register socket %d with events",
                                priv->sock);
                        ret = -1;
                        sys_close (priv->sock);
                        priv->sock = -1;
                        goto unlock;
                }
        }
unlock:
        pthread_mutex_unlock (&priv->lock);

out:
        return ret;
}


static int32_t
socket_submit_request (rpc_transport_t *this, rpc_transport_req_t *req)
{
        socket_private_t *priv = NULL;
        int               ret = -1;
        char              need_poll_out = 0;
        char              need_append = 1;
        struct ioq       *entry = NULL;
        glusterfs_ctx_t  *ctx = NULL;
	char              a_byte = 'j';

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        ctx  = this->ctx;

        pthread_mutex_lock (&priv->lock);
        {
                if (priv->connected != 1) {
                        if (!priv->submit_log && !priv->connect_finish_log) {
                                gf_log (this->name, GF_LOG_INFO,
                                        "not connected (priv->connected = %d)",
                                        priv->connected);
                                priv->submit_log = 1;
                        }
                        goto unlock;
                }

                priv->submit_log = 0;
                entry = __socket_ioq_new (this, &req->msg);
                if (!entry)
                        goto unlock;

                if (list_empty (&priv->ioq)) {
                        ret = __socket_ioq_churn_entry (this, entry, 1);

                        if (ret == 0) {
                                need_append = 0;
			}
                        if (ret > 0) {
                                need_poll_out = 1;
			}
                }

                if (need_append) {
                        list_add_tail (&entry->list, &priv->ioq);
			if (priv->own_thread) {
				/*
				 * Make sure the polling thread wakes up, by
				 * writing a byte to represent this entry.
				 */
				if (sys_write (priv->pipe[1], &a_byte, 1) < 1) {
					gf_log(this->name,GF_LOG_WARNING,
					       "write error on pipe");
				}
			}
                        ret = 0;
                }
                if (!priv->own_thread && need_poll_out) {
                        /* first entry to wait. continue writing on POLLOUT */
                        priv->idx = event_select_on (ctx->event_pool,
                                                     priv->sock,
                                                     priv->idx, -1, 1);
                }
        }
unlock:
        pthread_mutex_unlock (&priv->lock);

out:
        return ret;
}


static int32_t
socket_submit_reply (rpc_transport_t *this, rpc_transport_reply_t *reply)
{
        socket_private_t *priv = NULL;
        int               ret = -1;
        char              need_poll_out = 0;
        char              need_append = 1;
        struct ioq       *entry = NULL;
        glusterfs_ctx_t  *ctx = NULL;
	char              a_byte = 'd';

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        priv = this->private;
        ctx  = this->ctx;

        pthread_mutex_lock (&priv->lock);
        {
                if (priv->connected != 1) {
                        if (!priv->submit_log && !priv->connect_finish_log) {
                                gf_log (this->name, GF_LOG_INFO,
                                        "not connected (priv->connected = %d)",
                                        priv->connected);
                                priv->submit_log = 1;
                        }
                        goto unlock;
                }

                priv->submit_log = 0;
                entry = __socket_ioq_new (this, &reply->msg);
                if (!entry)
                        goto unlock;

                if (list_empty (&priv->ioq)) {
                        ret = __socket_ioq_churn_entry (this, entry, 1);

                        if (ret == 0) {
                                need_append = 0;
			}
                        if (ret > 0) {
                                need_poll_out = 1;
			}
                }

                if (need_append) {
                        list_add_tail (&entry->list, &priv->ioq);
			if (priv->own_thread) {
				/*
				 * Make sure the polling thread wakes up, by
				 * writing a byte to represent this entry.
				 */
				if (sys_write (priv->pipe[1], &a_byte, 1) < 1) {
					gf_log(this->name,GF_LOG_WARNING,
					       "write error on pipe");
				}
			}
                        ret = 0;
                }
                if (!priv->own_thread && need_poll_out) {
                        /* first entry to wait. continue writing on POLLOUT */
                        priv->idx = event_select_on (ctx->event_pool,
                                                     priv->sock,
                                                     priv->idx, -1, 1);
                }
        }
unlock:
        pthread_mutex_unlock (&priv->lock);

out:
        return ret;
}


static int32_t
socket_getpeername (rpc_transport_t *this, char *hostname, int hostlen)
{
        int32_t ret = -1;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", hostname, out);

        if (hostlen < (strlen (this->peerinfo.identifier) + 1)) {
                goto out;
        }

        strcpy (hostname, this->peerinfo.identifier);
        ret = 0;
out:
        return ret;
}


static int32_t
socket_getpeeraddr (rpc_transport_t *this, char *peeraddr, int addrlen,
                    struct sockaddr_storage *sa, socklen_t salen)
{
        int32_t ret = -1;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", sa, out);

        *sa = this->peerinfo.sockaddr;

        if (peeraddr != NULL) {
                ret = socket_getpeername (this, peeraddr, addrlen);
        }
        ret = 0;

out:
        return ret;
}


static int32_t
socket_getmyname (rpc_transport_t *this, char *hostname, int hostlen)
{
        int32_t ret = -1;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", hostname, out);

        if (hostlen < (strlen (this->myinfo.identifier) + 1)) {
                goto out;
        }

        strcpy (hostname, this->myinfo.identifier);
        ret = 0;
out:
        return ret;
}


static int32_t
socket_getmyaddr (rpc_transport_t *this, char *myaddr, int addrlen,
                  struct sockaddr_storage *sa, socklen_t salen)
{
        int32_t ret = 0;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", sa, out);

        *sa =  this->myinfo.sockaddr;

        if (myaddr != NULL) {
                ret = socket_getmyname (this, myaddr, addrlen);
        }

out:
        return ret;
}


static int
socket_throttle (rpc_transport_t *this, gf_boolean_t onoff)
{
        socket_private_t *priv = NULL;

        priv = this->private;

        /* The way we implement throttling is by taking off
           POLLIN event from the polled flags. This way we
           never get called with the POLLIN event and therefore
           will never read() any more data until throttling
           is turned off.
        */
        pthread_mutex_lock (&priv->lock);
        {

                /* Throttling is useless on a disconnected transport. In fact,
                 * it's dangerous since priv->idx and priv->sock are set to -1
                 * on a disconnected transport, which breaks epoll's event to
                 * registered fd mapping. */

                if (priv->connected == 1)
                        priv->idx = event_select_on (this->ctx->event_pool,
                                                     priv->sock,
                                                     priv->idx, (int) !onoff,
                                                     -1);
        }
        pthread_mutex_unlock (&priv->lock);
        return 0;
}


struct rpc_transport_ops tops = {
        .listen             = socket_listen,
        .connect            = socket_connect,
        .disconnect         = socket_disconnect,
        .submit_request     = socket_submit_request,
        .submit_reply       = socket_submit_reply,
        .get_peername       = socket_getpeername,
        .get_peeraddr       = socket_getpeeraddr,
        .get_myname         = socket_getmyname,
        .get_myaddr         = socket_getmyaddr,
	.throttle           = socket_throttle,
};

int
reconfigure (rpc_transport_t *this, dict_t *options)
{
        socket_private_t *priv          = NULL;
        gf_boolean_t      tmp_bool      = _gf_false;
        char             *optstr        = NULL;
        int               ret           = 0;
        uint32_t          backlog       = 0;
        uint64_t          windowsize    = 0;
        uint32_t          timeout       = 0;
        int               keepaliveidle  = GF_KEEPALIVE_TIME;
        int               keepaliveintvl = GF_KEEPALIVE_INTERVAL;
        int               keepalivecnt   = GF_KEEPALIVE_COUNT;

        GF_VALIDATE_OR_GOTO ("socket", this, out);
        GF_VALIDATE_OR_GOTO ("socket", this->private, out);

        if (!this || !this->private) {
                ret =-1;
                goto out;
        }

        priv = this->private;

        if (dict_get_str (options, "transport.socket.keepalive",
                          &optstr) == 0) {
                if (gf_string2boolean (optstr, &tmp_bool) == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "'transport.socket.keepalive' takes only "
                                "boolean options, not taking any action");
                        priv->keepalive = 1;
                        ret = -1;
                        goto out;
                }
                gf_log (this->name, GF_LOG_DEBUG, "Reconfigured transport.socket.keepalive");

                priv->keepalive = tmp_bool;
        }
        else
                priv->keepalive = 1;

        if (dict_get_int32 (options, "transport.tcp-user-timeout",
                            &(priv->timeout)) != 0)
                priv->timeout = timeout;
        gf_log (this->name, GF_LOG_DEBUG, "Reconfigued "
                "transport.tcp-user-timeout=%d", priv->timeout);

        if (dict_get_uint32 (options, "transport.listen-backlog",
                             &backlog) == 0) {
                priv->backlog = backlog;
                gf_log (this->name, GF_LOG_DEBUG, "Reconfigued "
                        "transport.listen-backlog=%d", priv->backlog);
        }

        if (dict_get_int32 (options, "transport.socket.keepalive-time",
                            &(priv->keepaliveidle)) != 0)
                priv->keepaliveidle = keepaliveidle;
        gf_log (this->name, GF_LOG_DEBUG, "Reconfigued "
                "transport.socket.keepalive-time=%d", priv->keepaliveidle);

        if (dict_get_int32 (options,
                            "transport.socket.keepalive-interval",
                            &(priv->keepaliveintvl)) != 0)
                priv->keepaliveintvl = keepaliveintvl;
        gf_log (this->name, GF_LOG_DEBUG, "Reconfigued "
                "transport.socket.keepalive-interval=%d", priv->keepaliveintvl);

        if (dict_get_int32 (options, "transport.socket.keepalive-count",
                            &(priv->keepalivecnt)) != 0)
                priv->keepalivecnt = keepalivecnt;
        gf_log (this->name, GF_LOG_DEBUG, "Reconfigued "
                "transport.socket.keepalive-count=%d", priv->keepalivecnt);

        optstr = NULL;
        if (dict_get_str (options, "tcp-window-size",
                          &optstr) == 0) {
                if (gf_string2uint64 (optstr, &windowsize) != 0) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "invalid number format: %s", optstr);
                        goto out;
                }
        }

        priv->windowsize = (int)windowsize;

        if (dict_get (options, "non-blocking-io")) {
                optstr = data_to_str (dict_get (options,
                                                "non-blocking-io"));

                if (gf_string2boolean (optstr, &tmp_bool) == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "'non-blocking-io' takes only boolean options,"
                                " not taking any action");
                        tmp_bool = 1;
                }

                if (!tmp_bool) {
                        priv->bio = 1;
                        gf_log (this->name, GF_LOG_WARNING,
                                "disabling non-blocking IO");
                }
        }

        if (!priv->bio) {
                ret = __socket_nonblock (priv->sock);
                if (ret == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                "NBIO on %d failed (%s)",
                                priv->sock, strerror (errno));
                        goto out;
                }
        }

        ret = 0;
out:
        return ret;

}

static pthread_mutex_t  *lock_array     = NULL;

static void
locking_func (int mode, int type, const char *file, int line)
{
        if (mode & CRYPTO_UNLOCK) {
                pthread_mutex_unlock (&lock_array[type]);
        } else {
                pthread_mutex_lock (&lock_array[type]);
        }
}

#if HAVE_CRYPTO_THREADID
static void
threadid_func (CRYPTO_THREADID *id)
{
        /*
         * We're not supposed to know whether a pthread_t is a number or a
         * pointer, but we definitely need an unsigned long.  Even though it
         * happens to be an unsigned long already on Linux, do the cast just in
         * case that's not so on another platform.  Note that this can still
         * break if any platforms are left where a pointer is larger than an
         * unsigned long.  In that case there's not much we can do; hopefully
         * anyone porting to such a platform will be aware enough to notice the
         * compile warnings about truncating the pointer value.
         */
        CRYPTO_THREADID_set_numeric (id, (unsigned long)pthread_self());
}
#else /* older openssl */
static unsigned long
legacy_threadid_func (void)
{
	/* See comments above, it applies here too. */
	return (unsigned long)pthread_self();
}
#endif

static void
init_openssl_mt (void)
{
        int     num_locks       = CRYPTO_num_locks();
        int     i;

        if (lock_array) {
                /* this only needs to be initialized once GLOBALLY no
                   matter how many translators/sockets we end up with. */
                return;
        }

        SSL_library_init();
        SSL_load_error_strings();

        lock_array = GF_CALLOC (num_locks, sizeof(pthread_mutex_t),
                                gf_sock_mt_lock_array);
        if (lock_array) {
                for (i = 0; i < num_locks; ++i) {
                        pthread_mutex_init (&lock_array[i], NULL);
                }
#if HAVE_CRYPTO_THREADID
                CRYPTO_THREADID_set_callback (threadid_func);
#else /* older openssl */
                CRYPTO_set_id_callback (legacy_threadid_func);
#endif
                CRYPTO_set_locking_callback (locking_func);
        }

}

static void __attribute__((destructor))
fini_openssl_mt (void)
{
        int i;

        if (!lock_array) {
                return;
        }

        CRYPTO_set_locking_callback(NULL);
#if HAVE_CRYPTO_THREADID
        CRYPTO_THREADID_set_callback (NULL);
#else /* older openssl */
        CRYPTO_set_id_callback (NULL);
#endif

        for (i = 0; i < CRYPTO_num_locks(); ++i) {
                pthread_mutex_destroy (&lock_array[i]);
        }

        GF_FREE (lock_array);
        lock_array = NULL;

        ERR_free_strings();
}

static void
socket_poller_mayday (socket_private_t *priv)
{
        if (priv == NULL)
                return;

        pthread_mutex_lock (&priv->cond_lock);
        {
                /* Signal waiting threads before exiting from socket_poller */
                if (!priv->own_thread_done) {
                        gf_log ("socket", GF_LOG_TRACE, "priv->cond SIGNALED");
                        pthread_cond_signal (&priv->cond);
                        priv->own_thread_done = _gf_true;
                }
        }
        pthread_mutex_unlock (&priv->cond_lock);
}

static int
socket_init (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;
        gf_boolean_t      tmp_bool = 0;
        uint64_t          windowsize = GF_DEFAULT_SOCKET_WINDOW_SIZE;
        char             *optstr = NULL;
        uint32_t          timeout = 0;
        int               keepaliveidle  = GF_KEEPALIVE_TIME;
        int               keepaliveintvl = GF_KEEPALIVE_INTERVAL;
        int               keepalivecnt   = GF_KEEPALIVE_COUNT;
        uint32_t          backlog = 0;
	int               session_id = 0;
        int32_t           cert_depth = DEFAULT_VERIFY_DEPTH;
        char             *cipher_list = DEFAULT_CIPHER_LIST;
        char             *dh_param = DEFAULT_DH_PARAM;
        char             *ec_curve = DEFAULT_EC_CURVE;
        char             *crl_path = NULL;

        if (this->private) {
                gf_log_callingfn (this->name, GF_LOG_ERROR,
                                  "double init attempted");
                return -1;
        }

        priv = GF_CALLOC (1, sizeof (*priv), gf_common_mt_socket_private_t);
        if (!priv) {
                return -1;
        }
        memset(priv,0,sizeof(*priv));

        pthread_mutex_init (&priv->lock, NULL);
        pthread_mutex_init (&priv->cond_lock, NULL);
        pthread_cond_init (&priv->cond, NULL);

        GF_REF_INIT (priv, socket_poller_mayday);

        priv->sock = -1;
        priv->idx = -1;
        priv->connected = -1;
        priv->nodelay = 1;
        priv->bio = 0;
        priv->windowsize = GF_DEFAULT_SOCKET_WINDOW_SIZE;
        INIT_LIST_HEAD (&priv->ioq);
        pthread_mutex_init (&priv->notify.lock, NULL);
        pthread_cond_init (&priv->notify.cond, NULL);

        /* All the below section needs 'this->options' to be present */
        if (!this->options)
                goto out;

        if (dict_get (this->options, "non-blocking-io")) {
                optstr = data_to_str (dict_get (this->options,
                                                "non-blocking-io"));

                if (gf_string2boolean (optstr, &tmp_bool) == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "'non-blocking-io' takes only boolean options,"
                                " not taking any action");
                        tmp_bool = 1;
                }

                if (!tmp_bool) {
                        priv->bio = 1;
                        gf_log (this->name, GF_LOG_WARNING,
                                "disabling non-blocking IO");
                }
        }

        optstr = NULL;

        // By default, we enable NODELAY
        if (dict_get (this->options, "transport.socket.nodelay")) {
                optstr = data_to_str (dict_get (this->options,
                                                "transport.socket.nodelay"));

                if (gf_string2boolean (optstr, &tmp_bool) == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "'transport.socket.nodelay' takes only "
                                "boolean options, not taking any action");
                        tmp_bool = 1;
                }
                if (!tmp_bool) {
                        priv->nodelay = 0;
                        gf_log (this->name, GF_LOG_DEBUG,
                                "disabling nodelay");
                }
        }

        optstr = NULL;
        if (dict_get_str (this->options, "tcp-window-size",
                          &optstr) == 0) {
                if (gf_string2uint64 (optstr, &windowsize) != 0) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "invalid number format: %s", optstr);
                        return -1;
                }
        }

        priv->windowsize = (int)windowsize;

        optstr = NULL;
        /* Enable Keep-alive by default. */
        priv->keepalive = 1;
        priv->keepaliveintvl = GF_KEEPALIVE_INTERVAL;
        priv->keepaliveidle = GF_KEEPALIVE_TIME;
        priv->keepalivecnt = GF_KEEPALIVE_COUNT;
        if (dict_get_str (this->options, "transport.socket.keepalive",
                          &optstr) == 0) {
                if (gf_string2boolean (optstr, &tmp_bool) == -1) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "'transport.socket.keepalive' takes only "
                                "boolean options, not taking any action");
                        tmp_bool = 1;
                }

                if (!tmp_bool)
                        priv->keepalive = 0;
        }

        if (dict_get_int32 (this->options, "transport.tcp-user-timeout",
                            &(priv->timeout)) != 0)
                priv->timeout = timeout;
        gf_log (this->name, GF_LOG_DEBUG, "Configued "
                "transport.tcp-user-timeout=%d", priv->timeout);

        if (dict_get_int32 (this->options,
                            "transport.socket.keepalive-time",
                            &(priv->keepaliveidle)) != 0) {
                priv->keepaliveidle = keepaliveidle;
        }

        if (dict_get_int32 (this->options,
                            "transport.socket.keepalive-interval",
                            &(priv->keepaliveintvl)) != 0) {
                priv->keepaliveintvl = keepaliveintvl;
        }

        if (dict_get_int32 (this->options, "transport.socket.keepalive-count",
                            &(priv->keepalivecnt)) != 0)
                priv->keepalivecnt = keepalivecnt;
        gf_log (this->name, GF_LOG_DEBUG, "Reconfigued "
                "transport.keepalivecnt=%d", keepalivecnt);

        if (dict_get_uint32 (this->options,
                             "transport.listen-backlog",
                             &backlog) != 0) {

                backlog = GLUSTERFS_SOCKET_LISTEN_BACKLOG;
        }
        priv->backlog = backlog;

        optstr = NULL;

         /* Check if socket read failures are to be logged */
        priv->read_fail_log = 1;
        if (dict_get (this->options, "transport.socket.read-fail-log")) {
                optstr = data_to_str (dict_get (this->options, "transport.socket.read-fail-log"));
                if (gf_string2boolean (optstr, &tmp_bool) == -1) {
                        gf_log (this->name, GF_LOG_WARNING,
                                   "'transport.socket.read-fail-log' takes only "
                                   "boolean options; logging socket read fails");
                }
                else if (tmp_bool == _gf_false) {
                        priv->read_fail_log = 0;
                }
        }

        priv->windowsize = (int)windowsize;

        priv->ssl_enabled = _gf_false;
	if (dict_get_str(this->options,SSL_ENABLED_OPT,&optstr) == 0) {
                if (gf_string2boolean (optstr, &priv->ssl_enabled) != 0) {
                        gf_log (this->name, GF_LOG_ERROR,
				"invalid value given for ssl-enabled boolean");
		}
	}
        priv->mgmt_ssl = this->ctx->secure_mgmt;
        priv->srvr_ssl = this->ctx->secure_srvr;

        priv->ssl_own_cert = DEFAULT_CERT_PATH;
	if (dict_get_str(this->options,SSL_OWN_CERT_OPT,&optstr) == 0) {
                if (!priv->ssl_enabled) {
                        gf_log(this->name,GF_LOG_WARNING,
                               "%s specified without %s (ignored)",
                               SSL_OWN_CERT_OPT, SSL_ENABLED_OPT);
                }
                priv->ssl_own_cert = optstr;
	}
        priv->ssl_own_cert = gf_strdup(priv->ssl_own_cert);

        priv->ssl_private_key = DEFAULT_KEY_PATH;
	if (dict_get_str(this->options,SSL_PRIVATE_KEY_OPT,&optstr) == 0) {
                if (!priv->ssl_enabled) {
                        gf_log(this->name,GF_LOG_WARNING,
                               "%s specified without %s (ignored)",
                               SSL_PRIVATE_KEY_OPT, SSL_ENABLED_OPT);
                }
                priv->ssl_private_key = optstr;
	}
        priv->ssl_private_key = gf_strdup(priv->ssl_private_key);

        priv->ssl_ca_list = DEFAULT_CA_PATH;
	if (dict_get_str(this->options,SSL_CA_LIST_OPT,&optstr) == 0) {
                if (!priv->ssl_enabled) {
                        gf_log(this->name,GF_LOG_WARNING,
                               "%s specified without %s (ignored)",
                               SSL_CA_LIST_OPT, SSL_ENABLED_OPT);
                }
                priv->ssl_ca_list = optstr;
	}
        priv->ssl_ca_list = gf_strdup(priv->ssl_ca_list);

	if (dict_get_str(this->options,SSL_CRL_PATH_OPT,&optstr) == 0) {
                if (!priv->ssl_enabled) {
                        gf_log(this->name,GF_LOG_WARNING,
                               "%s specified without %s (ignored)",
                               SSL_CRL_PATH_OPT, SSL_ENABLED_OPT);
		}
		if (strcasecmp(optstr, "NULL") == 0)
			crl_path = NULL;
		else
			crl_path = optstr;
	}

        gf_log(this->name, priv->ssl_enabled ? GF_LOG_INFO: GF_LOG_DEBUG,
               "SSL support on the I/O path is %s",
               priv->ssl_enabled ? "ENABLED" : "NOT enabled");
        gf_log(this->name, priv->mgmt_ssl ? GF_LOG_INFO: GF_LOG_DEBUG,
               "SSL support for glusterd is %s",
               priv->mgmt_ssl ? "ENABLED" : "NOT enabled");
        /*
         * This might get overridden temporarily in socket_connect (q.v.)
         * if we're using the glusterd portmapper.
         */
        priv->use_ssl = priv->ssl_enabled;

	priv->own_thread = priv->use_ssl;
	if (dict_get_str(this->options,OWN_THREAD_OPT,&optstr) == 0) {
                gf_log (this->name, GF_LOG_INFO, "OWN_THREAD_OPT found");
                if (gf_string2boolean (optstr, &priv->own_thread) != 0) {
                        gf_log (this->name, GF_LOG_WARNING,
				"invalid value given for own-thread boolean");
		}
	}
	gf_log(this->name, priv->own_thread ? GF_LOG_INFO: GF_LOG_DEBUG,
               "using %s polling thread",
	       priv->own_thread ? "private" : "system");

        if (!dict_get_int32 (this->options, SSL_CERT_DEPTH_OPT, &cert_depth)) {
                gf_log (this->name, GF_LOG_INFO,
                        "using certificate depth %d", cert_depth);
        }
        if (!dict_get_str (this->options, SSL_CIPHER_LIST_OPT, &cipher_list)) {
                gf_log (this->name, GF_LOG_INFO,
                        "using cipher list %s", cipher_list);
        }
        if (!dict_get_str (this->options, SSL_DH_PARAM_OPT, &dh_param)) {
                gf_log (this->name, GF_LOG_INFO,
                        "using DH parameters %s", dh_param);
        }
        if (!dict_get_str (this->options, SSL_EC_CURVE_OPT, &ec_curve)) {
                gf_log (this->name, GF_LOG_INFO,
                        "using EC curve %s", ec_curve);
        }

	if (priv->ssl_enabled || priv->mgmt_ssl) {
                BIO *bio = NULL;

#if HAVE_TLSV1_2_METHOD
		priv->ssl_meth = (SSL_METHOD *)TLSv1_2_method();
#else
/*
 * Nobody should use an OpenSSL so old it does not support TLS 1.2.
 * If that is really required, build with -DUSE_INSECURE_OPENSSL
 */
#ifndef USE_INSECURE_OPENSSL
#error Old and insecure OpenSSL, use -DUSE_INSECURE_OPENSSL to use it anyway
#endif
		/* SSLv23_method uses highest available protocol */
		priv->ssl_meth = (SSL_METHOD *)SSLv23_method();
#endif
		priv->ssl_ctx = SSL_CTX_new(priv->ssl_meth);

                SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_SSLv2);
                SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_SSLv3);
#ifdef SSL_OP_NO_TICKET
                SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_TICKET);
#endif
#ifdef SSL_OP_NO_COMPRESSION
                SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_COMPRESSION);
#endif

		if ((bio = BIO_new_file(dh_param, "r")) == NULL) {
			gf_log(this->name,GF_LOG_ERROR,
			       "failed to open %s, "
			       "DH ciphers are disabled", dh_param);
		}

		if (bio != NULL) {
#ifdef HAVE_OPENSSL_DH_H
                        DH *dh;
                        unsigned long err;

                        dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
                        BIO_free(bio);
                        if (dh != NULL) {
				SSL_CTX_set_options(priv->ssl_ctx,
						    SSL_OP_SINGLE_DH_USE);
				SSL_CTX_set_tmp_dh(priv->ssl_ctx, dh);
				DH_free(dh);
                        } else {
                                err = ERR_get_error();
                                gf_log(this->name,GF_LOG_ERROR,
                                       "failed to read DH param from %s: %s "
                                       "DH ciphers are disabled.",
                                       dh_param, ERR_error_string(err, NULL));
                        }
#else /* HAVE_OPENSSL_DH_H */
                        BIO_free(bio);
                        gf_log(this->name, GF_LOG_ERROR,
                               "OpenSSL has no DH support");
#endif /* HAVE_OPENSSL_DH_H */
                }

                if (ec_curve != NULL) {
#ifdef HAVE_OPENSSL_ECDH_H
                        EC_KEY *ecdh = NULL;
                        int nid;
                        unsigned long err;

                        nid = OBJ_sn2nid(ec_curve);
                        if (nid != 0)
                                ecdh = EC_KEY_new_by_curve_name(nid);

                        if (ecdh != NULL) {
				SSL_CTX_set_options(priv->ssl_ctx,
						    SSL_OP_SINGLE_ECDH_USE);
				SSL_CTX_set_tmp_ecdh(priv->ssl_ctx, ecdh);
				EC_KEY_free(ecdh);
                        } else {
                                err = ERR_get_error();
                                gf_log(this->name, GF_LOG_ERROR,
                                       "failed to load EC curve %s: %s. "
				       "ECDH ciphers are disabled.",
                                       ec_curve, ERR_error_string(err, NULL));
			}
#else /* HAVE_OPENSSL_ECDH_H */
                        gf_log(this->name, GF_LOG_ERROR,
                               "OpenSSL has no ECDH support");
#endif /* HAVE_OPENSSL_ECDH_H */
                }

		/* This must be done after DH and ECDH setups */
                if (SSL_CTX_set_cipher_list(priv->ssl_ctx, cipher_list) == 0) {
                        gf_log(this->name,GF_LOG_ERROR,
                               "failed to find any valid ciphers");
                        goto err;
                }

		SSL_CTX_set_options(priv->ssl_ctx,
                                    SSL_OP_CIPHER_SERVER_PREFERENCE);

		if (!SSL_CTX_use_certificate_chain_file(priv->ssl_ctx,
							priv->ssl_own_cert)) {
			gf_log(this->name,GF_LOG_ERROR,
			       "could not load our cert");
			goto err;
		}

		if (!SSL_CTX_use_PrivateKey_file(priv->ssl_ctx,
						 priv->ssl_private_key,
						 SSL_FILETYPE_PEM)) {
			gf_log(this->name,GF_LOG_ERROR,
			       "could not load private key");
			goto err;
		}

		if (!SSL_CTX_load_verify_locations(priv->ssl_ctx,
						   priv->ssl_ca_list,
						   crl_path)) {
			gf_log(this->name,GF_LOG_ERROR,
			       "could not load CA list");
			goto err;
		}

#if (OPENSSL_VERSION_NUMBER < 0x00905100L)
		SSL_CTX_set_verify_depth(ctx,cert_depth);
#endif

		if (crl_path) {
#ifdef X509_V_FLAG_CRL_CHECK_ALL
			X509_STORE *x509store;

			x509store  = SSL_CTX_get_cert_store(priv->ssl_ctx);
			X509_STORE_set_flags(x509store,
			    X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
#else
			gf_log(this->name,GF_LOG_ERROR,
			       "OpenSSL version does not support CRL");
#endif
		}

		priv->ssl_session_id = ++session_id;
		SSL_CTX_set_session_id_context(priv->ssl_ctx,
					       (void *)&priv->ssl_session_id,
					       sizeof(priv->ssl_session_id));

		SSL_CTX_set_verify(priv->ssl_ctx,SSL_VERIFY_PEER,0);

                /*
                 * Since glusterfs shares the same settings for client-side
                 * and server-side of SSL, we need to ignore any certificate
                 * usage specification (SSL client vs SSL server), otherwise
                 * SSL connexions will fail with 'unsupported cerritifcate"
                 */
                SSL_CTX_set_purpose(priv->ssl_ctx, X509_PURPOSE_ANY);
	}

        if (priv->own_thread) {
                priv->ot_state = OT_IDLE;
        }

out:
        this->private = priv;
        return 0;

err:
        if (priv->ssl_own_cert) {
                GF_FREE(priv->ssl_own_cert);
        }
        if (priv->ssl_private_key) {
                GF_FREE(priv->ssl_private_key);
        }
        if (priv->ssl_ca_list) {
                GF_FREE(priv->ssl_ca_list);
        }
        GF_FREE(priv);
        return -1;
}


void
fini (rpc_transport_t *this)
{
        socket_private_t *priv = NULL;

        if (!this)
                return;

        priv = this->private;
        if (priv) {
                if (priv->sock != -1) {
                        pthread_mutex_lock (&priv->lock);
                        {
                                __socket_ioq_flush (this);
                                __socket_reset (this);
                        }
                        pthread_mutex_unlock (&priv->lock);
                }
                gf_log (this->name, GF_LOG_TRACE,
                        "transport %p destroyed", this);

                pthread_mutex_destroy (&priv->lock);
                pthread_mutex_destroy (&priv->cond_lock);
                pthread_cond_destroy (&priv->cond);
		if (priv->ssl_private_key) {
			GF_FREE(priv->ssl_private_key);
		}
		if (priv->ssl_own_cert) {
			GF_FREE(priv->ssl_own_cert);
		}
		if (priv->ssl_ca_list) {
			GF_FREE(priv->ssl_ca_list);
		}
                GF_FREE (priv);
        }

        this->private = NULL;
}

int32_t
init (rpc_transport_t *this)
{
        int ret = -1;

        init_openssl_mt();

        ret = socket_init (this);

        if (ret == -1) {
                gf_log (this->name, GF_LOG_DEBUG, "socket_init() failed");
        }

        return ret;
}

struct volume_options options[] = {
        { .key   = {"remote-port",
                    "transport.remote-port",
                    "transport.socket.remote-port"},
          .type  = GF_OPTION_TYPE_INT
        },
        { .key   = {"transport.socket.listen-port", "listen-port"},
          .type  = GF_OPTION_TYPE_INT
        },
        { .key   = {"transport.socket.bind-address", "bind-address" },
          .type  = GF_OPTION_TYPE_INTERNET_ADDRESS
        },
        { .key   = {"transport.socket.connect-path", "connect-path"},
          .type  = GF_OPTION_TYPE_ANY
        },
        { .key   = {"transport.socket.bind-path", "bind-path"},
          .type  = GF_OPTION_TYPE_ANY
        },
        { .key   = {"transport.socket.listen-path", "listen-path"},
          .type  = GF_OPTION_TYPE_ANY
        },
        { .key   = { "transport.address-family",
                     "address-family" },
          .value = {"inet", "inet6", "unix", "inet-sdp" },
          .type  = GF_OPTION_TYPE_STR
        },

        { .key   = {"non-blocking-io"},
          .type  = GF_OPTION_TYPE_BOOL
        },
        { .key   = {"tcp-window-size"},
          .type  = GF_OPTION_TYPE_SIZET,
          .min   = GF_MIN_SOCKET_WINDOW_SIZE,
          .max   = GF_MAX_SOCKET_WINDOW_SIZE
        },
        { .key   = {"transport.tcp-user-timeout"},
          .type  = GF_OPTION_TYPE_INT,
          .default_value = "0"
        },
        { .key   = {"transport.socket.nodelay"},
          .type  = GF_OPTION_TYPE_BOOL,
          .default_value = "1"
        },
        { .key   = {"transport.socket.lowlat"},
          .type  = GF_OPTION_TYPE_BOOL
        },
        { .key   = {"transport.socket.keepalive"},
          .type  = GF_OPTION_TYPE_BOOL,
          .default_value = "1"
        },
        { .key   = {"transport.socket.keepalive-interval"},
          .type  = GF_OPTION_TYPE_INT,
          .default_value = "2"
        },
        { .key   = {"transport.socket.keepalive-time"},
          .type  = GF_OPTION_TYPE_INT,
          .default_value = "20"
        },
        { .key   = {"transport.socket.keepalive-count"},
          .type  = GF_OPTION_TYPE_INT,
          .default_value = "9"
        },
        { .key   = {"transport.socket.read-fail-log"},
          .type  = GF_OPTION_TYPE_BOOL
        },
        { .key   = {SSL_ENABLED_OPT},
          .type  = GF_OPTION_TYPE_BOOL
        },
	{ .key   = {SSL_OWN_CERT_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_PRIVATE_KEY_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_CA_LIST_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_CERT_DEPTH_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_CIPHER_LIST_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_DH_PARAM_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_EC_CURVE_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {SSL_CRL_PATH_OPT},
	  .type  = GF_OPTION_TYPE_STR
	},
	{ .key   = {OWN_THREAD_OPT},
	  .type  = GF_OPTION_TYPE_BOOL
	},
        { .key   = {"ssl-own-cert"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "SSL certificate. Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-private-key"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "SSL private key. Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-ca-list"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "SSL CA list. Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-cert-depth"},
          .type  = GF_OPTION_TYPE_INT,
          .description = "Maximum certificate-chain depth.  If zero, the "
                         "peer's certificate itself must be in the local "
                         "certificate list.  Otherwise, there may be up to N "
                         "signing certificates between the peer's and the "
                         "local list.  Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-cipher-list"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "Allowed SSL ciphers. Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-dh-param"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "DH parameters file. Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-ec-curve"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "ECDH curve name. Ignored if SSL is not enabled."
        },
        { .key   = {"ssl-crl-path"},
          .type  = GF_OPTION_TYPE_STR,
          .description = "Path to directory containing CRL. "
                         "Ignored if SSL is not enabled."
        },
        { .key = {NULL} }
};