File: ncbi_ftp_connector.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (2280 lines) | stat: -rw-r--r-- 77,253 bytes parent folder | download | duplicates (2)
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
/* $Id: ncbi_ftp_connector.c,v 1.73 2012/05/07 15:39:33 kazimird Exp $
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 * Author:  Anton Lavrentiev
 *
 * File Description:
 *   FTP CONNECTOR
 *   See also:  RFCs 959 (STD 9), 1123 (4.1), 1635 (FYI 24), 2428,
 *   3659 ("Extensions to FTP"), 5797 (FTP Command Registry).
 *
 *   Minimum FTP implementation: RFC 1123 (4.1.2.13)
 *
 *   See <connect/ncbi_connector.h> for the detailed specification of
 *   the connector's methods and structures.
 *
 *   Note:  We do not implement transfers of files whose names include
 *          CR or LF characters:  for those to work, all FTP commands will
 *          have be required to terminate with CRLF at the user level
 *          (currently, LF alone acts as the command terminator), and all
 *          solitary CRs to be recoded as 'CR\0' (per the RFC), yet all
 *          solitary LFs to be passed through.  Nonetheless, we escape all
 *          IACs for the sake of safety of the control connection.
 *
 */

#include "ncbi_ansi_ext.h"
#include "ncbi_priv.h"
#include <connect/ncbi_ftp_connector.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

#if !defined(NCBI_OS_MSWIN)  &&  !defined(__CYGWIN__)
#  define _timezone timezone
#  define _daylight daylight
#endif /*!NCBI_OS_MSWIN && !__CYGWIN__*/

#define NCBI_USE_ERRCODE_X   Connect_FTP


/***********************************************************************
 *  INTERNAL -- Auxiliary types and static functions
 ***********************************************************************/

enum EFTP_Feature {                /* NB: values must occupy 12 bits at most */
    fFtpFeature_NOOP =  0x001,     /* all implementations MUST support       */
    fFtpFeature_SYST =  0x002,
    fFtpFeature_SITE =  0x004,
    fFtpFeature_FEAT =  0x008,
    fFtpFeature_MDTM =  0x010,
    fFtpFeature_REST =  0x020,
    fFtpFeature_SIZE =  0x040,
    fFtpFeature_EPRT =  0x080,
    fFtpFeature_MLSx =  0x100,
    fFtpFeature_EPSV = 0x1000,
    fFtpFeature_APSV = 0x3000      /* EPSV ALL -- a la "APSV" from RFC 1579  */
};
typedef unsigned short TFTP_Features; /* bitwise OR of EFtpFeature */


/* All internal data necessary to perform I/O
 */
typedef struct {
    SConnNetInfo*         info;    /* connection parameters                  */
    unsigned              sync:1;  /* true when last cmd acked (cntl synced) */
    unsigned              send:1;  /* true when in send mode (STOR/APPE)     */
    unsigned              open:1;  /* true when data open ok in send mode    */
    unsigned              rclr:1;  /* true when "rest" to clear by next cmd  */
    unsigned              soft:12; /* learned server features (future ext)   */
    TFTP_Features         feat;    /* FTP server features as discovered      */
    TFTP_Flags            flag;    /* connector flags per constructor        */
    SFTP_Callback         cmcb;    /* user-provided command callback         */
    const char*           what;    /* goes to description                    */
    SOCK                  cntl;    /* control connection                     */
    SOCK                  data;    /* data    connection                     */
    BUF                   wbuf;    /* write buffer (command)                 */
    BUF                   rbuf;    /* read  buffer (response)                */
    TNCBI_BigCount        size;    /* size of data                           */
    TNCBI_BigCount        rest;    /* restart position                       */
    EIO_Status        r_status;
    EIO_Status        w_status;
} SFTPConnector;


static const STimeout kFailsafeTimeout = { 10, 0 };
static const STimeout kZeroTimeout     = {  0, 0 };
static const char     kDigits[] = "0123456789";


typedef EIO_Status (*FFTPReplyParser)(SFTPConnector* xxx, int code,
                                      size_t lineno, const char* line);


static EIO_Status x_FTPParseReply(SFTPConnector* xxx, int* code,
                                  char* line, size_t maxlinelen,
                                  FFTPReplyParser parser)
{
    EIO_Status status = eIO_Success;
    size_t     lineno;
    size_t     len;

    assert(xxx->cntl);

    for (lineno = 0; ; lineno++) {
        EIO_Status rdstat;
        const char* msg;
        char buf[1024];
        int c, m;

        /* all FTP replies are at least '\n'-terminated, no ending with EOF */
        rdstat = SOCK_ReadLine(xxx->cntl, buf, sizeof(buf), &len);
        if (rdstat != eIO_Success) {
            status  = rdstat;
            break;
        }
        if (len == sizeof(buf)) {
            status = eIO_Unknown/*line too long*/;
            break;
        }
        msg = buf;
        if (!lineno  ||  isdigit((unsigned char)(*buf))) {
            if (sscanf(buf, "%d%n", &c, &m) < 1  ||  m != 3  ||  !c
                ||  (buf[m]  &&  buf[m] != ' '  &&  buf[m] != '-')
                ||  (lineno  &&  c != *code)) {
                status = eIO_Unknown;
                break;
            }
            msg += m + 1;
            if (buf[m] == '-')
                m = 0;
        } else {
            c = *code;
            m = 0;
        }
        msg += strspn(msg, " \t");
        if (status == eIO_Success  &&  parser)
            status  = parser(xxx, lineno  &&  m ? 0 : c, lineno, msg);
        if (!lineno) {
            *code = c;
            if (line)
                strncpy0(line, msg, maxlinelen);
        }
        if (m)
            break;
    }
    return status;
}


/* Close data connection w/error messages
 * how == eIO_Open                        -- unexpected closure, log error
 * how == eIO_Read or eIO_Write (or both) -- normal close in the direction
 *                                           no size check with eIO_ReadWrite
 * how == eIO_Close                       -- pre-approved close, w/o log errs
 * Notes:
 * 1. eIO_Open and eIO_Close suppress log message if xxx->cntl is closed;
 * 2. timeout is ignored for both eIO_Open and eIO_Close;
 * 3. post-condition: !xxx->data.
 */
static EIO_Status x_FTPCloseData(SFTPConnector* xxx,
                                 EIO_Event how, const STimeout* timeout)
{
    EIO_Status status;

    assert(xxx->data);
    if (xxx->flag & fFTP_LogControl)
        SOCK_SetDataLogging(xxx->data, eOn);

    if (how & eIO_ReadWrite) {
        TNCBI_BigCount size = xxx->size  &&  how != eIO_ReadWrite
            ? SOCK_GetCount(xxx->data, how) : xxx->size;
        assert(!xxx->sync); /* still expecting close ack */
        SOCK_SetTimeout(xxx->data, eIO_Close, timeout);
        status = SOCK_Close(xxx->data);
        if (status != eIO_Success) {
            CORE_LOGF_X(7, eLOG_Error,
                        ("[FTP; %s]  Error closing data connection: %s",
                         xxx->what, IO_StatusStr(status)));
        } else if (xxx->size != size) {
            if (how == eIO_Write) {
                CORE_LOGF_X(9, eLOG_Error,
                            ("[FTP; %s]  Incomplete data transfer: "
                             "%" NCBI_BIGCOUNT_FORMAT_SPEC " out of "
                             "%" NCBI_BIGCOUNT_FORMAT_SPEC " byte%s uploaded",
                             xxx->what, size,
                             xxx->size, &"s"[xxx->size == 1]));
                status = eIO_Unknown;
            } else if (xxx->rest == (TNCBI_BigCount)(-1L)  ||
                       xxx->rest + size == xxx->size) {
                static const char* kWarningFmt[] =
                    { "[FTP; %s]  Server reports restarted download"
                      " size incorrectly: %" NCBI_BIGCOUNT_FORMAT_SPEC,
                      "[FTP; %s]  Restart parse error prevents download"
                      " size verification: %" NCBI_BIGCOUNT_FORMAT_SPEC };
                CORE_LOGF_X(11, eLOG_Warning,
                            (kWarningFmt[xxx->rest != (TNCBI_BigCount)(-1L)],
                             xxx->what, xxx->size));
            } else {
                CORE_LOGF_X(8, eLOG_Error,
                            ("[FTP; %s]  Premature EOF in data: "
                             "%" NCBI_BIGCOUNT_FORMAT_SPEC " byte%s expected, "
                             "%" NCBI_BIGCOUNT_FORMAT_SPEC " byte%s received",
                             xxx->what, xxx->size, &"s"[xxx->size == 1],
                             size, &"s"[size == 1]));
                status = eIO_Unknown;
            }
        } else if (size  &&  how != eIO_ReadWrite)
            CORE_TRACEF(("[FTP; %s]  Transfer size verified", xxx->what));
    } else {
        if (!xxx->cntl) {
            how = eIO_Open;
        } else if (xxx->what  &&  how != eIO_Close) {
            CORE_LOGF_X(1, xxx->send ? eLOG_Error : eLOG_Warning,
                        ("[FTP; %s]  Data connection transfer aborted",
                         xxx->what));
        }
        if (how != eIO_Close) {
            status = SOCK_Abort(xxx->data);
            SOCK_Close(xxx->data);
        } else {
            SOCK_SetTimeout(xxx->data, eIO_Close, &kZeroTimeout); 
            status = SOCK_Close(xxx->data);
        }
        xxx->open = 0/*false*/;
    }

    xxx->data = 0;
    return status;
}


static EIO_Status s_FTPReply(SFTPConnector* xxx, int* code,
                             char* line, size_t maxlinelen,
                             FFTPReplyParser parser)
{
    EIO_Status status;
    int        c = 0;

    if (xxx->cntl) {
        status = x_FTPParseReply(xxx, &c, line, maxlinelen, parser);
        if (status != eIO_Timeout)
            xxx->sync = 1/*true*/;
        if (status == eIO_Success) {
            if (c == 421)
                status = eIO_Closed;
            else if (c == 502)
                status = eIO_NotSupported;
            else if (c == 332  ||  c == 532)
                status = eIO_NotSupported/*account*/;
            else if (c == 110  &&  (xxx->data  ||  xxx->send))
                status = eIO_NotSupported/*restart mark*/;
        }
        if (status == eIO_Closed   ||  c == 221) {
            SOCK cntl = xxx->cntl;
            xxx->cntl = 0;
            if (status == eIO_Closed) {
                CORE_LOGF_X(10, eLOG_Error,
                            ("[FTP%s%s]  Lost connection to server @ %s:%hu",
                             xxx->what ? "; " : "", xxx->what ? xxx->what : "",
                             xxx->info->host, xxx->info->port));
            }
            if (xxx->data)
                x_FTPCloseData(xxx, eIO_Close/*silent close*/, 0);
            if (status == eIO_Closed)
                SOCK_Abort(cntl);
            else
                SOCK_SetTimeout(cntl, eIO_Close, &kZeroTimeout);
            SOCK_Close(cntl);
        }
        if (status == eIO_Success  &&  c == 530/*not logged in*/)
            status  = eIO_Closed;
    } else
        status = eIO_Closed;
    if (code)
        *code = c;
    return status;
}


static EIO_Status s_FTPDrainReply(SFTPConnector* xxx, int* code, int cXX)
{
    int        c;
    EIO_Status status;
    int        quit = *code;
    *code = 0;
    while ((status = s_FTPReply(xxx, &c, 0, 0, 0)) == eIO_Success) {
        *code = c;
        if ((quit  &&  quit == c)  ||  (cXX  &&  c / 100 == cXX))
            break;
    }
    return status;
}


#define s_FTPCommand(x, c, a)  s_FTPCommandEx(x, c, a, 0/*false*/)

static EIO_Status s_FTPCommandEx(SFTPConnector* xxx,
                                 const char*    cmd,
                                 const char*    arg,
                                 int/*bool*/    off)
{
    char*      line;
    EIO_Status status;
    char       x_buf[128];
    size_t     cmdlen, arglen, linelen;

    if (!xxx->cntl)
        return eIO_Closed;

    cmdlen  = strlen(cmd);
    arglen  = arg ? strlen(arg) : 0;
    linelen = cmdlen + 2;
    if (arg)
        linelen += 1 + arglen;
    line    = linelen < sizeof(x_buf) ? x_buf : (char*) malloc(linelen + 1);

    if (line) {
        ESwitch log = eDefault;
        memcpy(line, cmd, cmdlen);
        if (arg) {
            line[cmdlen++] = ' ';
            memcpy(line + cmdlen, arg, arglen);
            cmdlen += arglen;
        }
        line[cmdlen++] = '\r';
        line[cmdlen++] = '\n';
        line[cmdlen]   = '\0';
        log = off ? SOCK_SetDataLogging(xxx->cntl, eOff) : eOff;
        status = SOCK_Write(xxx->cntl, line, cmdlen, 0, eIO_WritePersist);
        if (off  &&  log != eOff) {
            SOCK_SetDataLogging(xxx->cntl, log);
            if (log == eOn  ||  SOCK_SetDataLoggingAPI(eDefault) == eOn)
                CORE_LOGF_X(4, eLOG_Trace,
                            ("Sending FTP %.*s command (%s)",
                             (int) strcspn(line, " \t"), line,
                             IO_StatusStr(status)));
        }
        if (line != x_buf)
            free(line);
        xxx->sync = 0/*false*/;
    } else
        status = eIO_Unknown;
    return status;
}


static const char* x_4Word(const char* line, const char word[4+1])
{
    const char* s = strstr(line, word);
    return !s  ||  ((s == line  ||  isspace((unsigned char) s[-1]))
                    &&  !isalpha((unsigned char) s[4])) ? s : 0;
}


static EIO_Status x_FTPParseHelp(SFTPConnector* xxx, int code,
                                 size_t lineno, const char* line)
{
    if (!lineno)
        return code == 211  ||  code == 214 ? eIO_Success : eIO_NotSupported;
    if (code) {
        const char* s;
        assert(code == 211  ||  code == 214);
        if ((s = x_4Word(line, "NOOP")) != 0) {  /* RFC 959 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_NOOP;
            else
                xxx->feat &= ~fFtpFeature_NOOP;
        }
        if ((s = x_4Word(line, "SYST")) != 0) {  /* RFC 959 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_SYST;
            else
                xxx->feat &= ~fFtpFeature_SYST;
        }
        if ((s = x_4Word(line, "SITE")) != 0) {  /* RFC 959, 1123 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_SITE;
            else
                xxx->feat &= ~fFtpFeature_SITE;
        }
        if ((s = x_4Word(line, "FEAT")) != 0) {  /* RFC 3659 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_FEAT;
            else
                xxx->feat &= ~fFtpFeature_FEAT;
        }
        if ((s = x_4Word(line, "MDTM")) != 0) {  /* RFC 3659 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_MDTM;
            else
                xxx->feat &= ~fFtpFeature_MDTM;
        }
        if ((s = x_4Word(line, "REST")) != 0) {  /* RFC 3659, NB: FEAT */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_REST;
            else
                xxx->feat &= ~fFtpFeature_REST;
        }
        if ((s = x_4Word(line, "SIZE")) != 0) {  /* RFC 3659 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_SIZE;
            else
                xxx->feat &= ~fFtpFeature_SIZE;
        }
        if ((s = x_4Word(line, "EPRT")) != 0) {  /* RFC 2428 */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_EPRT;
            else
                xxx->feat &= ~fFtpFeature_EPRT;
        }
        if ((s = x_4Word(line, "EPSV")) != 0) {  /* RFC 2428 (cf 1579) */
            if (s[4 + strspn(s + 4, " \t")] != '*')
                xxx->feat |=  fFtpFeature_EPSV;
            else
                xxx->feat &= ~fFtpFeature_EPSV;
        }
    } /* else last line */
    return eIO_Success;
}


static EIO_Status x_FTPHelp(SFTPConnector* xxx)
{
    int code;
    TFTP_Features feat;
    EIO_Status status = s_FTPCommand(xxx, "HELP", 0);
    if (status != eIO_Success)
        return status;
    feat = xxx->feat;
    status = s_FTPReply(xxx, &code, 0, 0, x_FTPParseHelp);
    if (status != eIO_Success  ||  (code != 211  &&  code != 214)) {
        xxx->feat = feat;
        return status != eIO_Success ? status : eIO_NotSupported;
    }
    return eIO_Success;
}


static EIO_Status x_FTPParseFeat(SFTPConnector* xxx, int code,
                                 size_t lineno, const char* line)
{
    if (!lineno)
        return code == 211 ? eIO_Success : eIO_NotSupported;
    if (code  &&  strlen(line) >= 4  &&  line[4] == ' ') {
        assert(code == 211);
        if      (strncasecmp(line, "MDTM", 4) == 0)
            xxx->feat |= fFtpFeature_MDTM;
        else if (strncasecmp(line, "SIZE", 4) == 0)
            xxx->feat |= fFtpFeature_SIZE;
        else if (strncasecmp(line, "EPSV", 4) == 0)
            xxx->feat |= fFtpFeature_EPSV;
        else if (strncasecmp(line, "REST", 4) == 0)
            xxx->feat |= fFtpFeature_REST;  /* NB: "STREAM" must also follow */
        else if (strncasecmp(line, "MLST", 4) == 0)
            xxx->feat |= fFtpFeature_MLSx;
    }
    return eIO_Success;
}


static EIO_Status x_FTPFeat(SFTPConnector* xxx)
{
    int code;
    EIO_Status status;
    TFTP_Features feat;
    if (xxx->feat  &&  !(xxx->feat & fFtpFeature_FEAT))
        return eIO_NotSupported;
    status = s_FTPCommand(xxx, "FEAT", 0);
    if (status != eIO_Success)
        return status;
    feat = xxx->feat;
    status = s_FTPReply(xxx, &code, 0, 0, x_FTPParseFeat);
    if (status != eIO_Success  ||  code != 211) {
        xxx->feat = feat;
        return status != eIO_Success ? status : eIO_NotSupported;
    }
    return eIO_Success;
}


/* all implementations MUST support NOOP */
static EIO_Status x_FTPNoop(SFTPConnector* xxx)
{
    int code;
    EIO_Status status = s_FTPCommand(xxx, "NOOP", 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code != 200  &&  (code / 100 != 5  ||  (xxx->feat & fFtpFeature_NOOP)))
        return eIO_Unknown;
    return eIO_Success;
}


static EIO_Status x_FTPFeatures(SFTPConnector* xxx)
{
    xxx->soft = 0;
    if (xxx->flag & fFTP_UseFeatures) {
        /* try to setup features */
        if (x_FTPHelp(xxx) == eIO_Closed)
            return eIO_Closed;
        if (x_FTPFeat(xxx) == eIO_Closed)
            return eIO_Closed;
        /* make sure the connection is still good */
        return x_FTPNoop(xxx);
    }
    return eIO_Success;
}


static EIO_Status x_FTPLogin(SFTPConnector* xxx)
{
    int code;
    EIO_Status status;

    xxx->feat = 0;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code == 120)
        return eIO_Timeout;
    if (code != 220  ||  !*xxx->info->user)
        return eIO_Unknown;
    status = s_FTPCommand(xxx, "USER", xxx->info->user);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code != 230) {
        if (code != 331)
            return code == 332 ? eIO_NotSupported : eIO_Unknown;
        status = s_FTPCommandEx(xxx, "PASS", xxx->info->pass, 1);
        if (status != eIO_Success)
            return status;
        status = s_FTPReply(xxx, &code, 0, 0, 0);
        if (status != eIO_Success)
            return status;
        if (code == 503)
            return eIO_Closed;
        if (code != 230  &&  code != 202)
            return code == 332 ? eIO_NotSupported : eIO_Unknown;
    }
    status = x_FTPFeatures(xxx);
    if (status != eIO_Success)
        return status;
    if (xxx->flag & fFTP_LogControl) {
        CORE_LOGF_X(3, eLOG_Trace,
                    ("[FTP]  Server ready @ %s:%hu, features = 0x%02X",
                     xxx->info->host, xxx->info->port,
                     (unsigned int) xxx->feat));
    }
    if (xxx->feat &  fFtpFeature_EPSV)
        xxx->feat |= fFtpFeature_APSV;
    assert(xxx->sync);
    return eIO_Success;
}


/* all implementations MUST support TYPE (I and/or L 8, RFC1123 4.1.5) */
/* Note that other transfer defaults are:  STRU F, MODE S (RFC959 5.1) */
static EIO_Status x_FTPBinary(SFTPConnector* xxx)
{
    int code;
    EIO_Status status;
    const char* type = xxx->flag & fFTP_UseTypeL8 ? "L8" : "I";
    status = s_FTPCommand(xxx, "TYPE", type);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    return code == 200 ? eIO_Success : eIO_Unknown;
}


static EIO_Status x_FTPRest(SFTPConnector* xxx,
                            const char*    arg,
                            int/*bool*/    out)
{
    int code;
    EIO_Status status = s_FTPCommand(xxx, "REST", arg);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code == 350) {
        return out  &&  !BUF_Write(&xxx->rbuf, "350", 3)
            ? eIO_Unknown : eIO_Success;
    }
    if (code == 501  ||  /* RFC1123 4.1.3.4: */ code == 554  ||  code == 555)
        return eIO_NotSupported;
    return xxx->feat & fFtpFeature_REST ? eIO_Unknown : eIO_NotSupported;
}


static char* x_FTPUnquote(char* str, size_t* len)
{
    char* s = ++str;
    assert(str[-1] == '"');
    for (;;) {
        size_t l = strcspn(s, "\"");
        if (!*(s += l))
            break;
        if (*++s != '"') {
            *--s  = '\0';
            *len  = (size_t)(s - str);
            return str;
        }
        memmove(s, s + 1, strlen(s + 1) + 1);
    }
    *len = 0;
    return 0;
}


/* (null), MKD, RMD, PWD, CWD, CDUP, XMKD, XRMD, XPWD, XCWD, XCUP */
static EIO_Status x_FTPDir(SFTPConnector* xxx,
                           const char*    cmd,
                           const char*    arg)
{
    static const char kCwd[] = "CWD";
    EIO_Status status;
    char buf[256];
    int code;

    assert(!arg  ||  *arg);
    assert(!cmd  ||  strlen(cmd) >= 3);
    assert( cmd  ||  (arg  &&  arg == xxx->info->path));
    status = s_FTPCommand(xxx, cmd ? cmd : kCwd, cmd ? 0 : arg);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
    if (status != eIO_Success  &&  (status != eIO_NotSupported || code != 502))
        return status;
    if (code == 500  ||  code == 502) {
        /* RFC1123 4.1.3.1 requires reissue the command using an X-form */
        char verb[5];
        if (!cmd)
            cmd = kCwd;
        else if (toupper((unsigned char)(*cmd))  == 'X')
            return code == 500 ? eIO_Unknown : eIO_NotSupported;
        else if (toupper((unsigned char) cmd[2]) == 'U') /* CDUP */
            cmd = "CUP";
        verb[0] = 'X';
        strupr(strncpy0(verb + 1, cmd, 3));
        status = s_FTPCommand(xxx, verb, arg);
        if (status != eIO_Success)
            return status;
        status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
        if (status != eIO_Success)
            return status;
        if (code == 502)
            return eIO_NotSupported;
    } else if (!cmd) {
        cmd = kCwd;
    } else if (toupper((unsigned char)(*cmd)) == 'X')
        cmd++;
    if (toupper((unsigned char)(*cmd)) == 'R') { /* [X]RMD */
        if (code != 250)
            return eIO_Unknown;
    }else if (toupper((unsigned char)(*cmd)) == 'C') { /* [X]CWD, CDUP/XCUP */
        if (code != 200  &&  code != 250)
            return eIO_Unknown;
        /* fixup codes w/accordance to RFC959 */
        if (toupper((unsigned char) cmd[1]) != 'W') {
            /* CDUP, XCUP */
            if (code != 200)
                code  = 200;
        } else
            if (code != 250)
                code  = 250;
    } else { /* [X]MKD & [X]PWD */
        char*  dir;
        size_t len;
        if (code != 257) {
            /* [X]MKD: 521 "directory exists, no action taken" */
            return toupper((unsigned char)(*cmd)) != 'M'  ||  code != 521
                ? eIO_Unknown : eIO_Success;
        }
        dir = buf + strspn(buf, " ");
        return *dir != '"'
            ||  !(dir = x_FTPUnquote(dir, &len))
            ||  !BUF_Write(&xxx->rbuf, dir, len)
            ? eIO_Unknown : eIO_Success;
    }

    if (arg == xxx->info->path)
        return eIO_Success;
    code = sprintf(buf, "%d", code);
    assert((size_t) code < sizeof(buf));
    return !BUF_Write(&xxx->rbuf, buf, (size_t) code)
        ? eIO_Unknown : eIO_Success;
}


static EIO_Status x_FTPTelnetSynch(SFTPConnector* xxx)
{
    EIO_Status status;
    size_t     n;

    /* Send TELNET IAC/IP (Interrupt Process) command */
    status = SOCK_Write(xxx->cntl, "\377\364", 2, &n, eIO_WritePersist);
    if (status != eIO_Success)
        return status;
    assert(n == 2);
    /* Send TELNET IAC/DM (Data Mark) command to complete SYNCH, RFC 854 */
    status = SOCK_Write(xxx->cntl, "\377\362", 2, &n, eIO_WriteOutOfBand);
    if (status != eIO_Success)
        return status;
    return n == 2 ? eIO_Success : eIO_Unknown;
}


/*
 * how = 0 -- ABOR sequence only if data connection is open;
 * how = 1 -- just abort data connection, if any open;
 * how = 2 -- force full ABOR sequence;
 * how = 3 -- abort current command.
 *
 * Post-condition: !xxx->data
 */
static EIO_Status x_FTPAbort(SFTPConnector*  xxx,
                             int             how,
                             const STimeout* timeout)
{
    EIO_Status  status;

    if (!xxx->data  &&  how != 2)
        return eIO_Success;
    if (!xxx->cntl  ||  how == 1)
        return x_FTPCloseData(xxx, eIO_Close/*silent close*/, 0);
    if (!timeout)
        timeout = &kFailsafeTimeout;
    SOCK_SetTimeout(xxx->cntl, eIO_ReadWrite, timeout);
    status = x_FTPTelnetSynch(xxx);
    if (status == eIO_Success)
        status  = s_FTPCommand(xxx, "ABOR", 0);
    if (xxx->data) {
        if (status == eIO_Success  &&  !xxx->send) {
            /* this is not "data" per se, so go silent */
            if (xxx->flag & fFTP_LogData)
                SOCK_SetDataLogging(xxx->data, eDefault);
            SOCK_SetTimeout(xxx->data, eIO_ReadWrite, timeout);
            /* drain up data connection by discarding 1MB blocks repeatedly */
            while (SOCK_Read(xxx->data, 0, 1<<20, 0, eIO_ReadPlain)
                   == eIO_Success) {
                continue;
            }
        }
        x_FTPCloseData(xxx, how == 3
                       ||  SOCK_Status(xxx->data, eIO_Read) != eIO_Closed
                       ? eIO_Open/*warning*/ : eIO_Close/*silent*/, 0);
    }
    assert(!xxx->data);
    if (status == eIO_Success) {
        int         code = 426;
        int/*bool*/ sync = xxx->sync;
        status = s_FTPDrainReply(xxx, &code, 2/*2xx*/);
        if (status == eIO_Success) {
            /* Microsoft FTP is known to return 225 instead of 226 */
            if (code != 225  &&  code != 226  &&  code != 426)
                status = eIO_Unknown;
        } else if (status == eIO_Timeout  &&  !code)
            sync = 0/*false*/;
        xxx->sync = sync;
    }
    return status;
}


static EIO_Status x_FTPEpsv(SFTPConnector*  xxx,
                            unsigned int*   host,
                            unsigned short* port)
{
    EIO_Status status;
    char buf[128], d;
    unsigned int p;
    const char* s;
    int n;

    assert(port  ||  (xxx->feat & fFtpFeature_APSV) == fFtpFeature_APSV);
    if (xxx->flag & fFTP_NoExtensions)
        return eIO_NotSupported;
    status = s_FTPCommand(xxx, "EPSV", port ? 0 : "ALL");
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &n, buf, sizeof(buf) - 1, 0);
    if (status != eIO_Success)
        return status;
    if (!port)
        return n == 200 ? eIO_Success : eIO_NotSupported;
    if (n != 229)
        return xxx->feat & fFtpFeature_APSV ? eIO_Unknown : eIO_NotSupported;
    buf[sizeof(buf) - 1] = '\0';
    if (!(s = strchr(buf, '('))  ||  !(d = *++s)  ||  *++s != d  ||  *++s != d
        ||  sscanf(++s, "%u%c%n", &p, buf, &n) < 2  ||  p > 0xFFFF
        ||  *buf != d  ||  s[n] != ')') {
        return eIO_Unknown;
    }
    *host = 0;
    *port = (unsigned short) p;
    return eIO_Success;
}


/* all implementations MUST support PASV */
static EIO_Status x_FTPPasv(SFTPConnector*  xxx,
                            unsigned int*   host,
                            unsigned short* port)
{
    EIO_Status status;
    int  code, o[6];
    unsigned int i;
    char buf[128];

    status = s_FTPCommand(xxx, "PASV", 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
    if (status != eIO_Success  ||  code != 227)
        return eIO_Unknown;
    buf[sizeof(buf) - 1] = '\0';
    for (;;) {
        char* c;
        size_t len;
        /* RFC 1123 4.1.2.6 says that ()'s in PASV reply MUST NOT be assumed */
        for (c = buf;  *c;  ++c) {
            if (isdigit((unsigned char)(*c)))
                break;
        }
        if (!*c)
            return eIO_Unknown;
        len = 0;
        for (i = 0;  i < (unsigned int)(sizeof(o) / sizeof(o[0]));  i++) {
            if (sscanf(c + len, ",%d%n" + !i, &o[i], &code) < 1)
                break;
            len += code;
        }
        if (i >= (unsigned int)(sizeof(o) / sizeof(o[0])))
            break;
        if (!len) {
            len = strspn(c, kDigits);
            assert(len > 0);
        }
        memmove(buf, c + len, strlen(c + len) + 1);
    }
    for (i = 0;  i < (unsigned int)(sizeof(o) / sizeof(o[0]));  i++) {
        if (o[i] < 0  ||  o[i] > 255)
            return eIO_Unknown;
    }
    if (!(i = (((((o[0] << 8) | o[1]) << 8) | o[2]) << 8) | o[3]))
        return eIO_Unknown;
    *host = SOCK_HostToNetLong(i);
    if (!(i = (o[4] << 8) | o[5]))
        return eIO_Unknown;
    *port = (unsigned short) i;
    return eIO_Success;
}


static EIO_Status x_FTPPassive(SFTPConnector*  xxx,
                               const STimeout* timeout)
{
    EIO_Status   status;
    unsigned int   host;
    unsigned short port;
    char           addr[40];

    if ((xxx->feat & fFtpFeature_APSV) == fFtpFeature_APSV) {
        /* first time here, try to set EPSV ALL */
        if (x_FTPEpsv(xxx, 0, 0) == eIO_Success)
            xxx->feat &= ~fFtpFeature_EPSV;                    /* APSV mode */
        else
            xxx->feat &= ~fFtpFeature_APSV | fFtpFeature_EPSV; /* EPSV mode */
    }
    if (xxx->feat & fFtpFeature_APSV) {
        status = x_FTPEpsv(xxx, &host, &port);
        switch (status) {
        case eIO_NotSupported:
            xxx->feat &= ~fFtpFeature_EPSV;
            port = 0;
            break;
        case eIO_Success:
            assert(port);
            break;
        default:
            return status;
        }
    } else
        port = 0;
    if (!port  &&  (status = x_FTPPasv(xxx, &host, &port)) != eIO_Success)
        return status;
    assert(port);
    if (( host  &&
          SOCK_ntoa(host, addr, sizeof(addr)) != 0)  ||
        (!host  &&
         !SOCK_GetPeerAddressStringEx(xxx->cntl, addr,sizeof(addr), eSAF_IP))){
        return eIO_Unknown;
    }
    status = SOCK_CreateEx(addr, port, timeout, &xxx->data, 0, 0,
                           xxx->flag & fFTP_LogControl
                           ? fSOCK_LogOn : fSOCK_LogDefault);
    if (status != eIO_Success) {
        assert(!xxx->data);
        CORE_LOGF_X(2, eLOG_Error,
                    ("[FTP; %s]  Cannot open data connection to %s:%hu (%s)",
                     xxx->what, addr, port, IO_StatusStr(status)));
        return status;
    }
    assert(xxx->data);
    SOCK_SetDataLogging(xxx->data, xxx->flag & fFTP_LogData ? eOn : eDefault);
    return eIO_Success;
}


static EIO_Status x_FTPEprt(SFTPConnector* xxx,
                            unsigned int   host,
                            unsigned short port)
{
    char       buf[80];
    EIO_Status status;
    int        code;

    if (xxx->flag & fFTP_NoExtensions)
        return eIO_NotSupported;
    memcpy(buf, "|1|", 3); /*IPv4*/
    SOCK_ntoa(host, buf + 3, sizeof(buf) - 3);
    sprintf(buf + 3 + strlen(buf + 3), "|%hu|", port);
    status = s_FTPCommand(xxx, "EPRT", buf);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code == 500  ||  code == 501)
        return xxx->feat & fFtpFeature_EPRT ? eIO_Unknown : eIO_NotSupported;
    if (code == 522)
        return eIO_NotSupported;
    return code == 200 ? eIO_Success : eIO_Unknown;
}


/* all implementations MUST support PORT */
static EIO_Status x_FTPPort(SFTPConnector* xxx,
                            unsigned int   host,
                            unsigned short port)
{
    unsigned char octet[sizeof(host) + sizeof(port)];
    char          buf[80], *s = buf;
    EIO_Status    status;
    int           code;
    size_t        n;

    port = SOCK_HostToNetShort(port);
    memcpy(octet,                &host, sizeof(host));
    memcpy(octet + sizeof(host), &port, sizeof(port));
    for (n = 0;  n < sizeof(octet);  n++)
        s += sprintf(s, "%s%u", &","[!n], octet[n]);
    assert(s < buf + sizeof(buf));
    status = s_FTPCommand(xxx, "PORT", buf);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    return code == 200 ? eIO_Success : eIO_Unknown;
}


static EIO_Status x_FTPActive(SFTPConnector*  xxx,
                              LSOCK*          lsock,
                              const STimeout* timeout)
{
    EIO_Status   status;
    unsigned int   host;
    unsigned short port;

    /* NB: Apache FTP proxy re-uses SOCK_LocalPort(xxx->cntl);
     * which is the default port for user-end data port per RFC959,
     * other implementations don't do that leaving OS to decide,
     * since the PORT command will be issued, anyways... */
    status = LSOCK_CreateEx(0, 1, lsock, xxx->flag & fFTP_LogControl
                            ? fSOCK_LogOn : fSOCK_LogDefault);
    if (status != eIO_Success)
        return status;
    if (!(host = SOCK_GetLocalHostAddress(eDefault))  ||
        !(port = LSOCK_GetPort(*lsock, eNH_HostByteOrder))) {
        return eIO_Unknown;
    }
    if (xxx->feat & fFtpFeature_EPRT) {
        status = x_FTPEprt(xxx, host, port);
        if (status != eIO_NotSupported)
            return status;
        xxx->feat &= ~fFtpFeature_EPRT;
    }
    return x_FTPPort(xxx, host, port);
}


static EIO_Status x_FTPOpenData(SFTPConnector*  xxx,
                                LSOCK*          lsock,
                                const STimeout* timeout)
{
    EIO_Status status;

    *lsock = 0;
    if ((xxx->flag & fFTP_UsePassive)  ||  !(xxx->flag & fFTP_UseActive)) {
        status = x_FTPPassive(xxx, timeout);
        if (status == eIO_Success  ||
            (xxx->flag & (fFTP_UseActive|fFTP_UsePassive)) == fFTP_UsePassive){
            return status;
        }
        if ((xxx->feat & fFtpFeature_APSV) != (xxx->feat & fFtpFeature_EPSV)) {
            /* seems like an impossible case (EPSV ALL accepted but no EPSV);
             * still, better safe than sorry */
            return status;
        }
    }
    status = x_FTPActive(xxx, lsock, timeout);
    if (status != eIO_Success) {
        if (*lsock) {
            LSOCK_Close(*lsock);
            *lsock = 0;
        }
    } else
        assert(*lsock);
    return status;
}


/* LIST, NLST, RETR, STOR, APPE, MLSD */
static EIO_Status x_FTPXfer(SFTPConnector*  xxx,
                            const char*     cmd,
                            const STimeout* timeout,
                            FFTPReplyParser parser)
{
    int code;
    LSOCK lsock;
    EIO_Status status = x_FTPOpenData(xxx, &lsock, timeout);
    if (status != eIO_Success) {
        assert(!lsock  &&  !xxx->data);
        xxx->open = 0/*false*/;
        return status;
    }
    if (xxx->rest  &&  (xxx->flag & fFTP_DelayRestart)) {
        char buf[80];
        assert(xxx->rest != (TNCBI_BigCount)(-1L));
        sprintf(buf, "%" NCBI_BIGCOUNT_FORMAT_SPEC, xxx->rest);
        status = x_FTPRest(xxx, buf, 0/*false*/);
    }
    xxx->r_status = status;
    if (status == eIO_Success)
        status  = s_FTPCommand(xxx, cmd, 0);
    if (status == eIO_Success)
        status  = s_FTPReply(xxx, &code, 0, 0, parser);
    if (status == eIO_Success) {
        if (code == 125  ||  code == 150) {
            if (lsock) {
                assert(!xxx->data);
                status = LSOCK_AcceptEx(lsock, timeout, &xxx->data,
                                        xxx->flag & fFTP_LogControl
                                        ? fSOCK_LogOn : fSOCK_LogDefault);
                if (status != eIO_Success) {
                    assert(!xxx->data);
                    CORE_LOGF_X(5, eLOG_Error,
                                ("[FTP; %s]  Cannot accept data connection"
                                 " @ :%hu (%s)", xxx->what,
                                 LSOCK_GetPort(lsock, eNH_HostByteOrder),
                                 IO_StatusStr(status)));
                    /* NB: data conn may have started at the server end */
                    code = 2/*full abort*/;
                } else {
                    SOCK_SetDataLogging(xxx->data, xxx->flag & fFTP_LogData
                                        ? eOn : eDefault);
                }
                LSOCK_Close(lsock);
                lsock = 0;
            }
            if (status == eIO_Success) {
                assert(xxx->data);
                if (xxx->send) {
                    if (!(xxx->flag & fFTP_UncorkUpload))
                        SOCK_SetCork(xxx->data, 1);
                    assert(xxx->open);
                    xxx->size = 0;
                }
                xxx->sync = 0/*false*/;
                return eIO_Success;
            }
        } else if (code == 450  ||  code == 550) {
            /* file processing errors: not a file, not a dir, etc */
            status = eIO_Closed;
            code = 1/*quick*/;
        } else {
            status = eIO_Unknown;
            code = 1/*quick*/;
        }
    } else
        code = 0/*regular*/;
    if (lsock)
        LSOCK_Close(lsock);
    x_FTPAbort(xxx, code, timeout);
    assert(status != eIO_Success);
    xxx->open = 0/*false*/;
    assert(!xxx->data);
    return status;
}


static EIO_Status x_FTPRename(SFTPConnector* xxx,
                              const char*    src,
                              const char*    dst)
{
    int code;
    EIO_Status status = s_FTPCommand(xxx, "RNFR", src);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code != 350)
        return code == 450  ||  code == 550 ? eIO_Closed : eIO_Unknown;
    status = s_FTPCommand(xxx, "RNTO", dst);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code != 250)
        return code == 553 ? eIO_Closed  : eIO_Unknown;
    return BUF_Write(&xxx->rbuf, "250", 3) ? eIO_Success : eIO_Unknown;
}


/* REN */
static EIO_Status s_FTPRename(SFTPConnector* xxx,
                              const char*    arg)
{
    char *buf, *tmp;
    EIO_Status status;
    const char *src, *dst;
    size_t len = strcspn(arg, " \t");

    if (!arg[len]  ||  !(buf = strdup(arg)))
        return eIO_Unknown;

    tmp = buf;
    if (*tmp != '"') {
        src = tmp;
        tmp[len] = '\0';
    } else {
        src = x_FTPUnquote(tmp, &len);
        ++len;
    }
    tmp += ++len;
    tmp += strspn(tmp, " \t");
    if (*tmp != '"') {
        len = strcspn(tmp, " \t");
        dst = tmp;
        if (tmp[len])
            tmp[len++] = '\0';
    } else {
        dst = x_FTPUnquote(tmp, &len);
        len += 2;
    }
    tmp += len;

    status =
        src  &&  *src  &&  dst  &&  *dst  &&  !tmp[strspn(tmp, " \t")]
        ? x_FTPRename(xxx, src, dst)
        : eIO_Unknown;

    free(buf);
    return status;
}


static EIO_Status s_FTPDir(SFTPConnector* xxx,
                           const char*    cmd,
                           const char*    arg)
{
    assert(cmd  &&  arg  &&  arg != xxx->info->path  &&  !BUF_Size(xxx->rbuf));
    return x_FTPDir(xxx, cmd, *arg ? arg : 0);
}


/* SYST */
static EIO_Status s_FTPSyst(SFTPConnector* xxx,
                            const char*    cmd)
{
    int code;
    char buf[128];
    EIO_Status status = s_FTPCommand(xxx, cmd, 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
    if (status != eIO_Success)
        return status;
    if (code != 215  ||  !BUF_Write(&xxx->rbuf, buf, strlen(buf)))
        return eIO_Unknown;
    return eIO_Success;
}


static EIO_Status x_FTPParseStat(SFTPConnector* xxx, int code,
                                 size_t lineno, const char* line)
{
    if (!lineno  &&  code != 211  &&  code != 212  &&  code != 213)
        return code == 450 ? eIO_Closed : eIO_NotSupported;
    /* NB: STAT uses ASA (FORTRAN) vertical format control in 1st char */
    if (!BUF_Write(&xxx->rbuf, line, strlen(line))  ||
        !BUF_Write(&xxx->rbuf, "\n", 1)) {
        /* NB: leaving partial buffer, it's just an info */
        return eIO_Unknown;
    }
    return eIO_Success;
}


/* STAT */
static EIO_Status s_FTPStat(SFTPConnector* xxx,
                            const char*    cmd)
{
    EIO_Status status = s_FTPCommand(xxx, cmd, 0);
    if (status != eIO_Success)
        return status;
    return s_FTPReply(xxx, 0, 0, 0, x_FTPParseStat);
}


static EIO_Status x_FTPParseSize(SFTPConnector* xxx,
                                 const char*    size)
{
    size_t n = strspn(size, kDigits);
    if (!n  ||  n != strlen(size))
        return eIO_Unknown;
    if (xxx->cmcb.func  &&  (xxx->flag & fFTP_NotifySize))
        return xxx->cmcb.func(xxx->cmcb.data, xxx->what, size);
    return BUF_Write(&xxx->rbuf, size, n) ? eIO_Success : eIO_Unknown;
}


/* SIZE */
static EIO_Status s_FTPSize(SFTPConnector* xxx,
                            const char*    cmd)
{
    int code;
    char buf[128];
    EIO_Status status = s_FTPCommand(xxx, cmd, 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
    if (status == eIO_Success) {
        switch (code) {
        case 213:
            status = x_FTPParseSize(xxx, buf);
            break;
        case 550:
            /* file not plain or does not exist, EOF */
            break;
        default:
            status =
                xxx->feat & fFtpFeature_SIZE ? eIO_Unknown : eIO_NotSupported;
            break;
        }
    }
    return status;
}


static EIO_Status x_FTPParseMdtm(SFTPConnector* xxx, char* timestamp)
{
    static const int kDay[12] = {31,  0, 31, 30, 31, 30,
                                 31, 31, 30, 31, 30, 31};
    char* frac = strchr(timestamp, '.');
    int field[6], n;
    struct tm tm;
    char buf[80];
    size_t len;
    time_t t;

    if (frac) {
        *frac++ = '\0';
        if (strlen(frac) != strspn(frac, kDigits))
            return eIO_Unknown;
    }
    len = strlen(timestamp);
    if (len == 15  &&  strncmp(timestamp++, "191", 3) == 0) {
        /* Y2K problem with ftpd: 191xx as a "year" gets replaced with 20xx */
        timestamp[0] = '2';
        timestamp[1] = '0';
    } else if (len != 14)
        return eIO_Unknown;
    /* Can't use strptime() here, per the following reasons:
     * 1. Only GNU implementation is documented not to require spaces
     *    between input format specifiers in the format string (%-based);
     * 2. None to any spaces are allowed to match a space in the format,
     *    whilst an MDTM response must not contain even a single space. */
    for (n = 0;  n < 6;  n++) {
        size_t len = n ? 2 : 4/*year*/;
        if (len != strlen(strncpy0(buf, timestamp, len))  ||
            len != strspn(buf, kDigits)) {
            return eIO_Unknown;
        }
        field[n] = atoi(buf);
        timestamp += len;
    }
    memset(&tm, 0, sizeof(tm));
    if (field[0] < 1970)
        return eIO_Unknown;
    tm.tm_year  = field[0] - 1900;
    if (field[1] < 1  ||  field[1] > 12)
        return eIO_Unknown;
    tm.tm_mon   = field[1] - 1;
    if (field[2] < 1  ||  field[2] > (field[1] != 2
                                      ? kDay[tm.tm_mon]
                                      : 28 +
                                      (!(field[0] % 4)  &&
                                       (field[0] % 100
                                        ||  !(field[0] % 400))))) {
        return eIO_Unknown;
    }
    tm.tm_mday  = field[2];
    if (field[3] < 1  ||  field[3] > 23)
        return eIO_Unknown;
    tm.tm_hour  = field[3];
    if (field[4] < 1  ||  field[4] > 59)
        return eIO_Unknown;
    tm.tm_min   = field[4];
    if (field[5] < 1  ||  field[5] > 60) /* allow one leap second */
        return eIO_Unknown;
    tm.tm_sec   = field[5];
#ifdef HAVE_TIMEGM
    tm.tm_isdst = -1;
    if ((t = timegm(&tm)) == (time_t)(-1))
        return eIO_Unknown;
#else
    tm.tm_isdst =  0;
    if ((t = mktime(&tm)) == (time_t)(-1))
        return eIO_Unknown;
#  if !defined(NCBI_OS_DARWIN)  &&  !defined(NCBI_OS_BSD)
    /* NB: timezone information is unavailable on Darwin or BSD :-/ */
    if (t >= _timezone)
        t -= _timezone;
    if (t >= _daylight  &&  tm.tm_isdst > 0)
        t -= _daylight;
#  endif /*!NCBI_OS_DARWIN && !NCBI_OS_BSD*/
#endif /*HAVE_TIMEGM*/
    n = sprintf(buf, "%lu%s%-.9s", (unsigned long) t,
                frac  &&  *frac ? "." : "", frac ? frac : "");
    if (n <= 0  ||  !BUF_Write(&xxx->rbuf, buf, (size_t) n))
        return eIO_Unknown;
    return eIO_Success;
}


/* MDTM */
static EIO_Status s_FTPMdtm(SFTPConnector* xxx,
                            const char*    cmd)
{
    int code;
    char buf[128];
    EIO_Status status = s_FTPCommand(xxx, cmd, 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
    if (status == eIO_Success) {
        switch (code) {
        case 213:
            status = x_FTPParseMdtm(xxx, buf);
            break;
        case 550:
            /* file not plain or does not exist */
            break;
        default:
            status =
                xxx->feat & fFtpFeature_MDTM ? eIO_Unknown : eIO_NotSupported;
            break;
        }
    }
    return status;
}


/* REST */
static EIO_Status s_FTPRestart(SFTPConnector* xxx,
                               const char*    arg)
{
    TNCBI_BigCount rest;
    int n;

    if (sscanf(arg, "%" NCBI_BIGCOUNT_FORMAT_SPEC "%n", &rest, &n) < 1
        ||  arg[n]) {
        if (xxx->flag & fFTP_DelayRestart) {
            return eIO_Unknown;
        }
        xxx->rest = (TNCBI_BigCount)(-1L);
        xxx->rclr = 0/*false*/;
    } else {
        xxx->rclr = 0/*false*/;
        xxx->rest = rest;
        if (xxx->flag & fFTP_DelayRestart) {
            if (rest)
                return eIO_Success;
            /* "REST 0" goes through right away */
        }
    }
    return x_FTPRest(xxx, arg, 1/*true*/);
}


static EIO_Status x_FTPSzcb(SFTPConnector* xxx, int code,
                            size_t lineno, const char* line)
{
    EIO_Status status = eIO_Success;
    if (!lineno  &&  (code == 125  ||  code == 150)) {
        const char* comment = strrchr(line,  '(');
        size_t n, m;
        if (comment  &&  strchr(++comment, ')')
            &&  (n = strspn(comment, kDigits)) > 0
            &&  (m = strspn(comment + n, " \t")) > 0
            &&  strncasecmp(comment + n + m, "byte", 4) == 0) {
            TNCBI_BigCount size;
            int            k;
            if (sscanf(comment, "%" NCBI_BIGCOUNT_FORMAT_SPEC "%n",
                       &size, &k) < 1  &&  k != (int) n) {
                CORE_TRACEF(("[FTP; %s]  Error reading size from \"%.*s\"",
                             xxx->what, (int) n, comment));
            } else
                xxx->size = size;
            if (xxx->cmcb.func) {
                char* text = (char*) malloc(n + 1);
                if (text) {
                    status = xxx->cmcb.func(xxx->cmcb.data, xxx->what,
                                            strncpy0(text, comment, n));
                    free(text);
                } else
                    status = eIO_Unknown;
            }
        }
    }
    return status;
}


/* RETR, LIST, NLST */
/* all implementations MUST support RETR */
static EIO_Status s_FTPRetrieve(SFTPConnector*  xxx,
                                const char*     cmd,
                                const STimeout* timeout)
{
    xxx->size = 0;
    return x_FTPXfer(xxx, cmd, timeout, x_FTPSzcb);
}


/* STOR, APPE */
/* all implementations MUST support STOR */
static EIO_Status s_FTPStore(SFTPConnector*  xxx,
                             const char*     cmd,
                             const STimeout* timeout)
{
    xxx->send = xxx->open = 1/*true*/;
    return x_FTPXfer(xxx, cmd, timeout, 0);
}


/* DELE */
static EIO_Status s_FTPDele(SFTPConnector* xxx,
                            const char*    cmd)
{
    int code;
    EIO_Status status = s_FTPCommand(xxx, cmd, 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Success)
        return status;
    if (code != 250  ||  !BUF_Write(&xxx->rbuf, "250", 3))
        return eIO_Unknown;
    return eIO_Success;
}


static EIO_Status x_FTPMlsd(SFTPConnector* xxx, int code,
                            size_t lineno, const char* line)
{
    if (!lineno  ||  code != 501)
        return eIO_Success;
    return xxx->feat & fFtpFeature_MLSx ? eIO_Closed : eIO_NotSupported;
}


static EIO_Status x_FTPMlst(SFTPConnector* xxx, int code,
                            size_t lineno, const char* line)
{
    if (!lineno) {
        return code == 250 ? eIO_Success :
            xxx->feat & fFtpFeature_MLSx ? eIO_Closed : eIO_NotSupported;
    }
    if (code) {
        if (*line++ != ' ' /*RFC3659 7.2*/  ||  !*line  ||
            !BUF_Write(&xxx->rbuf, line, strlen(line))  ||
            !BUF_Write(&xxx->rbuf, "\n", 1)) {
            /* NB: must reset partial rbuf */
            return eIO_Unknown;
        }
    } /* else last line */
    return eIO_Success;
}


/* MLST, MLSD */
static EIO_Status s_FTPMlsx(SFTPConnector*  xxx,
                            const char*     cmd,
                            const STimeout* timeout)
{
    if (cmd[4] == 'T') { /*MLST*/
        EIO_Status status = s_FTPCommand(xxx, cmd, 0);
        if (status != eIO_Success)
            return status;
        status = s_FTPReply(xxx, 0/*code checked by cb*/, 0, 0, x_FTPMlst);
        if (status != eIO_Success)
            BUF_Erase(xxx->rbuf);
        return status;
    }
    xxx->size = 0;  /* cf. s_FTPRetrieve() */
    return x_FTPXfer(xxx, cmd, timeout, x_FTPMlsd);
}


static EIO_Status x_FTPNgcb(SFTPConnector* xxx, int code,
                            size_t lineno, const char* line)
{
    if (lineno  &&  code / 100 == 2) {
        if (*line++ != ' ' /*RFC2389 3.2 & 4*/ ||  !*line  ||
            !BUF_Write(&xxx->rbuf, line, strlen(line))     ||
            !BUF_Write(&xxx->rbuf, "\n", 1)) {
            /* NB: must reset partial rbuf */
            return eIO_Unknown;
        }
    }
    return eIO_Success;
}


/* FEAT, OPTS */
static EIO_Status s_FTPNegotiate(SFTPConnector* xxx,
                                 const char*    cmd)
{
    int code;
    EIO_Status status = s_FTPCommand(xxx, cmd, 0);
    if (status != eIO_Success)
        return status;
    status = s_FTPReply(xxx, &code, 0, 0, x_FTPNgcb);
    if (status == eIO_Success) {
        if (*cmd == 'F') {
            if (code != 200)
                status = eIO_Closed;
        } else {
            if (code == 451)
                status = eIO_Unknown;
            else if (code != 211)
                status = eIO_Closed;
        }
    }
    if (status != eIO_Success)
        BUF_Erase(xxx->rbuf);
    return status;
}


/* NB: data connection (upload only) may end up closed */
static EIO_Status s_FTPPollCntl(SFTPConnector* xxx, const STimeout* timeout)
{
    EIO_Status status = eIO_Success;
    while (SOCK_Wait(xxx->cntl, eIO_Read, &kZeroTimeout) == eIO_Success) {
        char buf[80];
        int  code;
        if (timeout != &kZeroTimeout) {
            SOCK_SetTimeout(xxx->cntl, eIO_Read,
                            timeout ? timeout : &kFailsafeTimeout);
            timeout  = &kZeroTimeout;
        }
        status = s_FTPReply(xxx, &code, buf, sizeof(buf) - 1, 0);
        if (status == eIO_Success) {
            assert(!xxx->data  ||  xxx->send);
            CORE_LOGF_X(12, eLOG_Error,
                        ("[FTP%s%s]  %spurious response %d from server%s%s",
                         xxx->what ? "; " : "", xxx->what ? xxx->what : "",
                         xxx->data ? "Aborting upload due to a s" : "S", code,
                         *buf ? ": " : "", buf));
            if (xxx->data) {
                x_FTPCloseData(xxx, eIO_Close/*silent*/, 0);
                xxx->sync = 1/*true*/;
                status = eIO_Closed;
            }
        }
        if (status == eIO_Closed)
            break;
    }
    return status;
}


static EIO_Status s_FTPSyncCntl(SFTPConnector* xxx, const STimeout* timeout)
{
    if (!xxx->sync) {
        EIO_Status status;
        SOCK_SetTimeout(xxx->cntl, eIO_Read, timeout);
        status = s_FTPReply(xxx, 0, 0, 0, 0);
        if (status != eIO_Success)
            return status;
        timeout = &kZeroTimeout;
        assert(xxx->sync);
    }
    return s_FTPPollCntl(xxx, timeout);
}


/* post-condition: empties xxx->wbuf, updates xxx->w_status */
static EIO_Status s_FTPExecute(SFTPConnector* xxx, const STimeout* timeout)
{
    EIO_Status status;
    size_t     size;
    char*      s;

    BUF_Erase(xxx->rbuf);
    status = x_FTPAbort(xxx, 3, timeout);
    if (xxx->what) {
        free((void*) xxx->what);
        xxx->what = 0;
    }
    if (status == eIO_Success)
        status  = s_FTPSyncCntl(xxx, timeout);
    if (status != eIO_Success)
        goto out;
    if (xxx->rest) {
        if (xxx->rclr) {
            xxx->rest = 0;
            xxx->rclr = 0/*false*/;
        } else
            xxx->rclr = 1/*true*/;
    }
    assert(xxx->cntl);
    verify((size = BUF_Size(xxx->wbuf)) != 0);
    if ((s = (char*) malloc(size + 1)) != 0
        &&  BUF_Read(xxx->wbuf, s, size) == size) {
        const char* c;
        assert(!memchr(s, '\n', size));
        if (s[size - 1] == '\r')
            --size;
        s[size] = '\0';
        if (!(c = (const char*) memchr(s, ' ', size)))
            c = s + size;
        else
            size = (size_t)(c - s);
        assert(*c == ' '  ||  !*c);
        if (*s)
            xxx->what = s;
        if (size == 3  ||  size == 4) {
            SOCK_SetTimeout(xxx->cntl, eIO_ReadWrite, timeout);
            if         (size == 3  &&   strncasecmp(s, "REN",  3) == 0) {
                /* a special-case, non-standard command */
                status = s_FTPRename(xxx, c + strspn(c, " \t"));
            } else if ((size == 3  ||  toupper((unsigned char) c[-4]) == 'X')
                       &&          (strncasecmp(c - 3, "CWD",  3) == 0  ||
                                    strncasecmp(c - 3, "PWD",  3) == 0  ||
                                    strncasecmp(c - 3, "MKD",  3) == 0  ||
                                    strncasecmp(c - 3, "RMD",  3) == 0)) {
                status = s_FTPDir(xxx, s, *c ? c + 1 : c);
            } else if  (size == 4  &&  (strncasecmp(s, "CDUP", 4) == 0  ||
                                        strncasecmp(s, "XCUP", 4) == 0)) {
                status = s_FTPDir(xxx, s, *c ? c + 1 : c);
            } else if  (size == 4  &&   strncasecmp(s, "SYST", 4) == 0) {
                status = s_FTPSyst(xxx, s);
            } else if  (size == 4  &&   strncasecmp(s, "STAT", 4) == 0) {
                status = s_FTPStat(xxx, s);
            } else if  (size == 4  &&   strncasecmp(s, "SIZE", 4) == 0) {
                status = s_FTPSize(xxx, s);
            } else if  (size == 4  &&   strncasecmp(s, "MDTM", 4) == 0) {
                status = s_FTPMdtm(xxx, s);
            } else if  (size == 4  &&   strncasecmp(s, "DELE", 4) == 0) {
                status = s_FTPDele(xxx, s);
            } else if  (size == 4  &&   strncasecmp(s, "REST", 4) == 0) {
                status = s_FTPRestart (xxx, *c ? c + 1 : c);
            } else if  (size == 4  &&  (strncasecmp(s, "RETR", 4) == 0  ||
                                        strncasecmp(s, "LIST", 4) == 0  ||
                                        strncasecmp(s, "NLST", 4) == 0)) {
                status = s_FTPRetrieve(xxx, s, timeout);
            } else if  (size == 4  &&  (strncasecmp(s, "STOR", 4) == 0  ||
                                        strncasecmp(s, "APPE", 4) == 0)) {
                status = s_FTPStore   (xxx, s, timeout);
            } else if  (size == 4  &&  (strncasecmp(s, "MLSD", 4) == 0  ||
                                        strncasecmp(s, "MLST", 4) == 0)) {
                status = s_FTPMlsx    (xxx, s, timeout);
            } else if  (size == 4  &&  (strncasecmp(s, "FEAT", 4) == 0  ||
                                        strncasecmp(s, "OPTS", 4) == 0)) {
                status = s_FTPNegotiate(xxx, s);
            } else if  (size == 4  &&   strncasecmp(s, "NOOP", 4) == 0 && !*c){
                /* Special, means to stop the current command and reach EOF */
                *s = '\0';
                xxx->what = 0;
                status = x_FTPNoop(xxx);
            } else
                status = eIO_NotSupported;
        } else
            status = eIO_NotSupported;
        if (*s)
            s = 0;
    } else
        status = eIO_Unknown;
    if (s)
        free(s);
 out:
    xxx->w_status = status;
    BUF_Erase(xxx->wbuf);
    return status;
}


static EIO_Status s_FTPCompleteUpload(SFTPConnector*  xxx,
                                      const STimeout* timeout)
{
    EIO_Status status;
    int code;

    assert(!BUF_Size(xxx->rbuf));
    assert(xxx->cntl  &&  xxx->send  &&  xxx->open);
    if (xxx->data) {
        status = x_FTPCloseData(xxx, xxx->flag & fFTP_NoSizeChecks
                                ? eIO_ReadWrite : eIO_Write, timeout);
        xxx->w_status = status;
        if (status != eIO_Success)
            return status;
        assert(!xxx->data);
    }
    SOCK_SetTimeout(xxx->cntl, eIO_Read, timeout);
    status = s_FTPReply(xxx, &code, 0, 0, 0);
    if (status != eIO_Timeout) {
        xxx->send = 0/*false*/;
        if (status == eIO_Success) {
            if (code == 225/*Microsoft*/  ||  code == 226) {
                char buf[80];
                int n = sprintf(buf, "%" NCBI_BIGCOUNT_FORMAT_SPEC, xxx->size);
                assert(!BUF_Size(xxx->rbuf)  &&  n);
                if (!BUF_Write(&xxx->rbuf, buf, (size_t) n))
                    status = eIO_Unknown;
                xxx->rest = 0;
            } else
                status = eIO_Unknown;
        }
    }
    xxx->r_status = status;
    return status;
}


/***********************************************************************
 *  INTERNAL -- "s_VT_*" functions for the "virt. table" of connector methods
 ***********************************************************************/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
    static const char* s_VT_GetType (CONNECTOR       connector);
    static char*       s_VT_Descr   (CONNECTOR       connector);
    static EIO_Status  s_VT_Open    (CONNECTOR       connector,
                                     const STimeout* timeout);
    static EIO_Status  s_VT_Wait    (CONNECTOR       connector,
                                     EIO_Event       event,
                                     const STimeout* timeout);
    static EIO_Status  s_VT_Write   (CONNECTOR       connector,
                                     const void*     buf,
                                     size_t          size,
                                     size_t*         n_written,
                                     const STimeout* timeout);
    static EIO_Status  s_VT_Read    (CONNECTOR       connector,
                                     void*           buf,
                                     size_t          size,
                                     size_t*         n_read,
                                     const STimeout* timeout);
    static EIO_Status  s_VT_Flush   (CONNECTOR       connector,
                                     const STimeout* timeout);
    static EIO_Status  s_VT_Status  (CONNECTOR       connector,
                                     EIO_Event       direction);
    static EIO_Status  s_VT_Close   (CONNECTOR       connector,
                                     const STimeout* timeout);
    static void        s_Setup      (CONNECTOR       connector);
    static void        s_Destroy    (CONNECTOR       connector);
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */


/*ARGSUSED*/
static const char* s_VT_GetType
(CONNECTOR connector)
{
    return "FTP";
}


static char* s_VT_Descr
(CONNECTOR connector)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    return xxx->what ? strdup(xxx->what) : 0;
}


static EIO_Status s_VT_Open
(CONNECTOR       connector,
 const STimeout* timeout)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    EIO_Status status;

    assert(!xxx->what  &&  !xxx->data  &&  !xxx->cntl);
    assert(!BUF_Size(xxx->wbuf)  &&  !BUF_Size(xxx->rbuf));
    status = SOCK_CreateEx(xxx->info->host, xxx->info->port, timeout,
                           &xxx->cntl, 0, 0, fSOCK_KeepAlive
                           | (xxx->flag & fFTP_LogControl
                              ? fSOCK_LogOn : fSOCK_LogDefault));
    if (status == eIO_Success) {
        SOCK_DisableOSSendDelay(xxx->cntl, 1/*yes,disable*/);
        SOCK_SetTimeout(xxx->cntl, eIO_ReadWrite, timeout);
        status  = x_FTPLogin(xxx);
    } else
        assert(!xxx->cntl);
    if (status == eIO_Success)
        status  = x_FTPBinary(xxx);
    if (status == eIO_Success  &&  *xxx->info->path)
        status  = x_FTPDir(xxx, 0,  xxx->info->path);
    if (status == eIO_Success) {
        xxx->send = xxx->open = xxx->rclr = 0/*false*/;
        assert(xxx->sync);
        xxx->rest = 0;
    } else if (xxx->cntl) {
        SOCK_Abort(xxx->cntl);
        SOCK_Close(xxx->cntl);
        xxx->cntl = 0;
    }
    assert(!xxx->what  &&  !xxx->data);
    xxx->r_status = status;
    xxx->w_status = status;
    return status;
}


static EIO_Status s_VT_Wait
(CONNECTOR       connector,
 EIO_Event       event,
 const STimeout* timeout)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    assert(event == eIO_Read  ||  event == eIO_Write);

    if (!xxx->cntl)
        return eIO_Closed;

    if (xxx->send) {
        if (xxx->data) {
            assert(xxx->open);
            if (event == eIO_Read)
                return s_FTPCompleteUpload(xxx, timeout);
            return SOCK_Wait(xxx->data, eIO_Write, timeout);
        }
        if (event == eIO_Write  ||  !xxx->open)
            return eIO_Closed;
        return SOCK_Wait(xxx->cntl, eIO_Read, timeout);
    }
    if (event == eIO_Write)
        return eIO_Success;
    /* event == eIO_Read */
    if (!xxx->data) {
        EIO_Status status;
        if (!BUF_Size(xxx->wbuf))
            return BUF_Size(xxx->rbuf) ? eIO_Success : eIO_Closed;
        status = SOCK_Wait(xxx->cntl, eIO_Write, timeout);
        if (status != eIO_Success)
            return status;
        status = s_FTPExecute(xxx, timeout);
        if (status != eIO_Success)
            return status;
        if (BUF_Size(xxx->rbuf))
            return eIO_Success;
    }
    return xxx->data ? SOCK_Wait(xxx->data, eIO_Read, timeout) : eIO_Closed;
}


static EIO_Status s_VT_Write
(CONNECTOR       connector,
 const void*     buf,
 size_t          size,
 size_t*         n_written,
 const STimeout* timeout)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    EIO_Status status;

    if (!xxx->cntl)
        return eIO_Closed;

    if (xxx->send) {
        if (!xxx->data)
            return eIO_Closed;
        status = s_FTPPollCntl(xxx, timeout);
        if (status == eIO_Success) {
            SOCK_SetTimeout(xxx->data, eIO_Write, timeout);
            status = SOCK_Write(xxx->data, buf, size,n_written,eIO_WritePlain);
            xxx->size += *n_written;
            if (status == eIO_Closed) {
                CORE_LOGF_X(6, eLOG_Error,
                            ("[FTP; %s]  Data connection lost", xxx->what));
                x_FTPCloseData(xxx, eIO_Close/*silent close*/, 0);
            }
        }
    } else if (size) {
        size_t count;
        const char* run = (const char*) memchr((const char*) buf, '\n', size);
        *n_written = size; /* by default report the entire command consumed */
        if (run  &&  run < (const char*) buf + --size) {
            /* reject multiple commands */
            BUF_Erase(xxx->wbuf);
            return eIO_Unknown;
        }
        count = 0;
        if (xxx->flag & fFTP_UncleanIAC) {
            if (BUF_Write(&xxx->wbuf, buf, size))
                count = size;
        } else {
            static const char kIAC[] = { '\377'/*IAC*/, '\377' };
            const char* s = (const char*) buf;
            while (count < size) {
                /* Escaped IAC (Interpret As Command) character, per RFC854 */
                const char* p;
                size_t part;
                if (count) {
                    if (!BUF_Write(&xxx->wbuf, kIAC, sizeof(kIAC)))
                        break;
                    ++count;
                    ++s;
                }
                if (!(p = (const char*) memchr(s, kIAC[0], size - count)))
                    part = size - count;
                else
                    part = (size_t)(p - s);
                if (!BUF_Write(&xxx->wbuf, s, part))
                    break;
                count += part;
                s     += part;
            }
        }
        if (count < size) {
            /* short write */
            *n_written = count;
            status = eIO_Unknown;
        } else if (!run) {
            status = eIO_Success;
        } else
            return s_FTPExecute(xxx, timeout);
    } else
        status = eIO_Success;
    xxx->w_status = status;
    return status;
}


static EIO_Status s_VT_Flush
(CONNECTOR       connector,
 const STimeout* timeout)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    if (!xxx->cntl)
        return eIO_Closed;

    if (xxx->send)
        return xxx->open ? eIO_Success : eIO_Closed;
    if (!BUF_Size(xxx->wbuf))
        return eIO_Success;
    return s_FTPExecute(xxx, timeout);
}


static EIO_Status s_VT_Read
(CONNECTOR       connector,
 void*           buf,
 size_t          size,
 size_t*         n_read,
 const STimeout* timeout)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    EIO_Status status;
    int code;

    if (!xxx->cntl)
        return eIO_Closed;

    if (xxx->send) {
        if (!xxx->open) {
            assert(!xxx->data);
            xxx->send = 0/*false*/;
            assert(!BUF_Size(xxx->wbuf));
            assert(!BUF_Size(xxx->rbuf));
            return eIO_Closed;
        }
        status = s_FTPCompleteUpload(xxx, timeout);
        if (status != eIO_Success)
            return status;
        assert(!xxx->data  &&  !xxx->send);
    } else if (BUF_Size(xxx->wbuf)) {
        status = s_FTPExecute(xxx, timeout);
        if (status != eIO_Success)
            return status;
    } else
        status = eIO_Success;
    if (xxx->data) {
        assert(!xxx->send  &&  !BUF_Size(xxx->rbuf));
        /* NB:  Cannot use s_FTPPollCntl() here because a response about data
         * connection closure may be seen before the actual EOF in the
         * (heavily loaded) data connection. */
        SOCK_SetTimeout(xxx->data, eIO_Read, timeout);
        status = SOCK_Read(xxx->data, buf, size, n_read, eIO_ReadPlain);
        if (status == eIO_Closed) {
            status  = x_FTPCloseData(xxx, xxx->flag & fFTP_NoSizeChecks
                                     ? eIO_ReadWrite : eIO_Read, timeout);
            if (status == eIO_Success) {
                status  = s_FTPReply(xxx, &code, 0, 0, 0);
                if (status == eIO_Success) {
                    if (code == 225/*Microsoft*/  ||  code == 226) {
                        status = eIO_Closed;
                        xxx->rest = 0;
                    } else
                        status = eIO_Unknown;
                }
            }
        }
        xxx->r_status = status;
        return status;
    }
    if (size  &&  !(*n_read = BUF_Read(xxx->rbuf, buf, size)))
        status = eIO_Closed;
    return status;
}


static EIO_Status s_VT_Status
(CONNECTOR connector,
 EIO_Event direction)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;

    switch (direction) {
    case eIO_Read:
        return !xxx->cntl ? eIO_Closed : xxx->r_status;
    case eIO_Write:
        return !xxx->cntl ? eIO_Closed : xxx->w_status;
    default:
        assert(0); /* should never happen (verified by connection) */
        break;
    }
    return eIO_InvalidArg;
}


static EIO_Status s_VT_Close
(CONNECTOR       connector,
 const STimeout* timeout)
{
    SFTPConnector* xxx  = (SFTPConnector*) connector->handle;
    SOCK           data = xxx->data;
    EIO_Status     status;

    BUF_Erase(xxx->wbuf);
    BUF_Erase(xxx->rbuf);
    if (data) {
        EIO_Event how;
        assert(!xxx->send  ||  xxx->open);
        if (xxx->cntl  &&  !(xxx->r_status | xxx->w_status)  &&  xxx->send)
            how = eIO_Open/*warning close*/;
        else
            how = eIO_Close/*silent close*/;
        status = x_FTPCloseData(xxx, how, 0);
        if (status == eIO_Success  &&  how == eIO_Open)
            status  = eIO_Unknown;
    } else
        status = eIO_Success;
    assert(!xxx->data);
    if (xxx->what) {
        free((void*) xxx->what);
        xxx->what = 0;
    }
    if (xxx->cntl) {
        if (!data  &&  status == eIO_Success) {
            int code = 0/*any*/;
            if (!timeout)
                timeout = &kFailsafeTimeout;
            SOCK_SetTimeout(xxx->cntl, eIO_ReadWrite, timeout);
            /* all implementations MUST support QUIT */
            status = s_FTPCommand(xxx, "QUIT", 0);
            if (status == eIO_Success) {
                status  = s_FTPDrainReply(xxx, &code, code);
                if (status == eIO_Success)
                    status  = eIO_Unknown;
                if (status == eIO_Closed  &&  code == 221)
                    status  = eIO_Success;
            }
        }
    } else
        status = eIO_Closed;
    if (xxx->cntl) {
        SOCK_Abort(xxx->cntl);
        SOCK_Close(xxx->cntl);
        xxx->cntl = 0;
    }
    return status;
}


static void s_Setup
(CONNECTOR connector)
{
    SMetaConnector* meta = connector->meta;

    /* initialize virtual table */
    CONN_SET_METHOD(meta, get_type, s_VT_GetType, connector);
    CONN_SET_METHOD(meta, descr,    s_VT_Descr,   connector);
    CONN_SET_METHOD(meta, open,     s_VT_Open,    connector);
    CONN_SET_METHOD(meta, wait,     s_VT_Wait,    connector);
    CONN_SET_METHOD(meta, write,    s_VT_Write,   connector);
    CONN_SET_METHOD(meta, flush,    s_VT_Flush,   connector);
    CONN_SET_METHOD(meta, read,     s_VT_Read,    connector);
    CONN_SET_METHOD(meta, status,   s_VT_Status,  connector);
    CONN_SET_METHOD(meta, close,    s_VT_Close,   connector);
    meta->default_timeout = kInfiniteTimeout;
}


static void s_Destroy
(CONNECTOR connector)
{
    SFTPConnector* xxx = (SFTPConnector*) connector->handle;
    connector->handle = 0;

    ConnNetInfo_Destroy(xxx->info);
    assert(!xxx->what  &&  !xxx->cntl  &&  !xxx->data);
    assert(!BUF_Size(xxx->wbuf)  &&  !BUF_Size(xxx->rbuf));
    BUF_Destroy(xxx->wbuf);
    xxx->wbuf = 0;
    BUF_Destroy(xxx->rbuf);
    xxx->rbuf = 0;
    free(xxx);
    free(connector);
}


extern CONNECTOR s_CreateConnector(const SConnNetInfo*  info,
                                   const char*          host,
                                   unsigned short       port,
                                   const char*          user,
                                   const char*          pass,
                                   const char*          path,
                                   TFTP_Flags           flag,
                                   const SFTP_Callback* cmcb)
{
    static const SFTP_Callback kNoCmcb = { 0 };
    CONNECTOR      ccc;
    SFTPConnector* xxx;

    if ((host  &&  strlen(host) >= sizeof(xxx->info->host))  ||
        (user  &&  strlen(user) >= sizeof(xxx->info->user))  ||
        (pass  &&  strlen(pass) >= sizeof(xxx->info->pass))  ||
        (path  &&  strlen(path) >= sizeof(xxx->info->path))  ||
        (info  &&  info->scheme != eURL_Unspec  &&  info->scheme != eURL_Ftp)){
        return 0;
    }
    if (!(ccc = (SConnector*)    malloc(sizeof(SConnector))))
        return 0;
    if (!(xxx = (SFTPConnector*) malloc(sizeof(*xxx)))) {
        free(ccc);
        return 0;
    }
    xxx->info = info ? ConnNetInfo_Clone(info) : ConnNetInfo_Create("_FTP");
    if (!xxx->info) {
        free(ccc);
        free(xxx);
        return 0;
    }
    if (xxx->info->scheme == eURL_Unspec)
        xxx->info->scheme  = eURL_Ftp;
    if (host  &&  *host)
        strcpy(xxx->info->host, host);
    xxx->info->port = port ? port : CONN_PORT_FTP;
    strcpy(xxx->info->user, user  &&  *user ? user : "ftp");
    strcpy(xxx->info->pass, pass            ? pass : "-none");
    strcpy(xxx->info->path, path            ? path : "");
    *xxx->info->args = '\0';

    /* some uninited fields are taken care of in s_VT_Open */
    xxx->cmcb    = cmcb ? *cmcb : kNoCmcb;
    xxx->flag    = flag;
    xxx->what    = 0;
    xxx->cntl    = 0;
    xxx->data    = 0;
    xxx->wbuf    = 0;
    xxx->rbuf    = 0;

    /* initialize connector data */
    ccc->handle  = xxx;
    ccc->next    = 0;
    ccc->meta    = 0;
    ccc->setup   = s_Setup;
    ccc->destroy = s_Destroy;

    return ccc;
}


/***********************************************************************
 *  EXTERNAL -- the connector's "constructors"
 ***********************************************************************/

extern CONNECTOR FTP_CreateConnectorSimple(const char*          host,
                                           unsigned short       port,
                                           const char*          user,
                                           const char*          pass,
                                           const char*          path,
                                           TFTP_Flags           flag,
                                           const SFTP_Callback* cmcb)
{
    return s_CreateConnector(0,    host, port, user, pass, path, flag, cmcb);
}


extern CONNECTOR FTP_CreateConnector(const SConnNetInfo*  info,
                                     TFTP_Flags           flag,
                                     const SFTP_Callback* cmcb)
{
    return s_CreateConnector(info, 0,    0,    0,    0,    0,    flag, cmcb);
}


/*DEPRECATED*/
extern CONNECTOR FTP_CreateDownloadConnector(const char*    host,
                                             unsigned short port,
                                             const char*    user,
                                             const char*    pass,
                                             const char*    path,
                                             TFTP_Flags     flag)
{
    return s_CreateConnector(0,    host, port, user, pass, path, flag, 0);
}