File: HTFTP.c

package info (click to toggle)
cern-httpd 3.0A-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 5,392 kB
  • ctags: 6,554
  • sloc: ansic: 37,902; makefile: 1,746; perl: 535; csh: 167; sh: 143
file content (3105 lines) | stat: -rw-r--r-- 89,928 bytes parent folder | download | duplicates (6)
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
/*			File Transfer Protocol (FTP) Client
**			for a WorldWideWeb browser
**			===================================
**
**	A cache of control connections is kept.
**
** Note: Port allocation
**
**	It is essential that the port is allocated by the system, rather
**	than chosen in rotation by us (POLL_PORTS), or the following
**	problem occurs.
**
**	It seems that an attempt by the server to connect to a port which has
**	been used recently by a listen on the same socket, or by another
**	socket this or another process causes a hangup of (almost exactly)
**	one minute. Therefore, we have to use a rotating port number.
**	The problem remains that if the application is run twice in quick
**	succession, it will hang for what remains of a minute.
**
** Authors
**	TBL	Tim Berners-lee <timbl@info.cern.ch>
**	DD	Denis DeLaRoca 310 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
**      LM      Lou Montulli <montulli@ukanaix.cc.ukans.edu>
**      FM      Foteos Macrides <macrides@sci.wfeb.edu>
**	HF	Henrik Frystyk <frystyk@dxcern.cern.ch>
**	AL	Ari Luotonen <luotonen@www.cern.ch>
**
** History:
**	 2 May 91	Written TBL, as a part of the WorldWideWeb project.
**	15 Jan 92	Bug fix: close() was used for NETCLOSE for control soc
**	10 Feb 92	Retry if cached connection times out or breaks
**	 8 Dec 92	Bug fix 921208 TBL after DD
**	17 Dec 92	Anon FTP password now just WWWuser@ suggested by DD
**			fails on princeton.edu!
**	27 Dec 93 (FM)  Fixed up so FTP now works with VMS hosts.  Path
**			must be Unix-style and cannot include the device
**			or top directory.
**      ?? ??? ?? (LM)  Added code to prompt and send passwords for non
**			anonymous FTP
**      25 Mar 94 (LM)  Added code to recognize different ftp server types
**                      and code to parse dates and sizes on most hosts.
**	27 Mar 93 (FM)  Added code for getting dates and sizes on VMS hosts.
**	27 Apr 94 (HF)  The module is basically rewritten to conform with
**			rfc 959, 1123 and 1579 and turned into a state 
**			machine. New semantics of ftp URLs are supported.
**	 2 May 94 (AL)	Fixed possible security hole when the URL contains
**			a newline, that could cause multiple commands to be
**			sent to an FTP server.
**
** Options:
**	LISTEN		The default way to open a dats connection is by using
**			PASV, but if that fails, we try PORT. If the PORT part
**			is unwanted, it can be disabled by undefine LISTEN.
**
** Notes:
**     			Portions Copyright 1994 Trustees of Dartmouth College
** 			Code for recognizing different FTP servers and
**			parsing "ls -l" output taken from Macintosh Fetch
**			program with permission from Jim Matthews,
**			Dartmouth Software Development Team.
**
**	BUGS:	@@@  	Use configuration file for user names
**
*/

/* Implementation dependent include files */
#include "tcp.h"

/* Library include files */
#include "HTParse.h"
#include "HTUtils.h"
#include "HTTCP.h"
#include "HTAnchor.h"
#include "HTFile.h"
#include "HTBTree.h"
#include "HTChunk.h"
#include "HTAlert.h"
#include "HTDirBrw.h"
#include "HTError.h"
#include "HTTCP.h"
#include "HTFTP.h"					 /* Implemented here */

#ifdef VMS
#include "HTVMSUtils.h"
#endif /* VMS */

/* Macros and other defines */
/* If LISTEN is defined, then first 'PASV' then 'PORT' (if error) is tried,
   else ONLY 'PASV' is used in order to establish a data connection. */
#define LISTEN
#ifdef LISTEN
/* #define REPEAT_LISTEN */    		  /* Reuse the portnumber once found */
/* #define POLL_PORTS */      /* If allocation does not work, poll ourselves.*/
#endif

#define FTP_DEFAULT_TIMEOUT	1000L			    /* x/100 seconds */

#ifndef IPPORT_FTP
#define IPPORT_FTP		21
#endif

#define WWW_FTP_CLIENT "WWWuser"         /* If can't get user-info, use this */

/* Globals */
PUBLIC BOOL HTFTPUserInfo = YES;
PUBLIC long HTFTPTimeOut = FTP_DEFAULT_TIMEOUT;

/* Type definitions and global variables etc. local to this module */ 
#ifdef POLL_PORTS
#define FIRST_TCP_PORT	 1024	       /* Region to try for a listening port */
#define LAST_TCP_PORT	 5999
PRIVATE	unsigned short 	 port_number = FIRST_TCP_PORT;
#endif

#ifdef LISTEN
#ifdef REPEAT_LISTEN
PRIVATE int     master_socket = -1;	       /* Listening socket = invalid */
#endif
PRIVATE char *	this_addr;				    /* Local address */
#endif

/* This is the local definition of HTRequest->net_info. In the special
   case of FTP we need two structures: one for the data connection and one
   for the control connection */
typedef enum _HTFTPServerType {
    UNKNOWN = -1,
    GENERIC_SERVER = 0,
    MACHTEN_SERVER,
    UNIX_SERVER,
    VMS_SERVER,
    CMS_SERVER,
    DCTS_SERVER,
    TCPC_SERVER,
    PETER_LEWIS_SERVER,
    NCSA_SERVER,
    WINDOWS_NT
} HTFTPServerType;

typedef enum _HTFTPMainState {
    FTP_ERROR = -2,
    FTP_FAILURE = -1,
    FTP_IDLE = 0,
    FTP_BEGIN,
    FTP_LOGGED_IN,
    FTP_GOT_DATA_CON,
    FTP_GOT_SERVER_INFO,
    FTP_GOT_DATA
} HTFTPState;

typedef struct _user_info {
    char *		     	domain;
    char *			id;
    char *			passwd;
} user_info;

/* The ftp_ctrl_info and ftp_data_info are both subclasses of the HTNetInfo
   structure defined in HTAccess.html */
typedef struct _ftp_ctrl_info {
    int				sockfd;	  /* Socket number for communication */
    HTInputSocket *             isoc;			     /* Input buffer */
    int 			addressCount;	  /* Attempts if multi-homed */
    BOOL			CRLFdotCRLF;   /* Transmission end like this */
    HTRequest *			request;	      /* Link to the request */

    u_long			serv_node;           /* IP address of server */
    u_short			serv_port;	    /* Port number on server */
    char *			location;        /* Current escaped position */
    user_info *			user;           /* Userid, passwd and domain */
    HTChunk *			welcome;	      /* The welcome message */
    HTChunk *			reply;	           /* Last reply from server */
    HTFTPServerType		server;		           /* Type of server */
    BOOL			unsure_type;         /* Sure about the type? */
    BOOL			use_list;	  	  /* Can we use LIST */
    HTFTPState     		state;		  /* State of the connection */
    HTList *			data_cons;           /* The data connections */
} ftp_ctrl_info;

/* We assume that the data connection is established between the same hosts
   as the control connection */
typedef struct _ftp_data_info {
    int				sockfd;	  /* Socket number for communication */
    HTInputSocket *             isoc;			     /* Input buffer */
    int 			addressCount;	  /* Attempts if multi-homed */
    BOOL			CRLFdotCRLF;   /* Transmission end like this */
    HTRequest *			request;	          /* Link to request */

    char *			host;		 /* host to contact for data */
    HTFormat			fileformat;   /* File format of current file */
    char			passive; 	 /* Have we opened passively */
    BOOL 			directory;	         /* Yes if directory */
    char *			datatype;  /* See rfc959 p.48, but NO SPACE! */
    ftp_ctrl_info *		ctrl;		   /* Controlling connection */
} ftp_data_info;

PRIVATE user_info *old_user;	    /* Only used if HT_REUSE_USER_INFO is on */
PRIVATE HTList *session;	 /* List of control connections in a session */

/* Comments to the Data Structures

    location:	The current location on the FTP-site. This is kept up to date
                using CWD and CDUP, but as there is no garanty for a
		hierarchical file structure in FTP, it should be handled
		carefully.
    passive:	0 means we are active, 1 means we are passive but haven't done
                any accept, 2 means we are passive AND have done the accept. */

/* ------------------------------------------------------------------------- */
/*			   Directory Specific Functions			     */
/* ------------------------------------------------------------------------- */

/*							      HTFTPParseError
**
**	This function parses an (multiple line) error message and takes out
**	the error codes.
**
*/
PRIVATE void HTFTPParseError ARGS1(HTChunk **, error)
{
    HTChunk *oldtext;
    if (!error || !*error || !(*error)->data) {
	if (PROT_TRACE) fprintf(stderr, "FTP......... No error message?\n");
	return;
    }
    oldtext = *error;
    {
	int result;		       /* The first return code in the chunk */
	char *oldp = oldtext->data;
	HTChunk *newtext = HTChunkCreate(128);
	if (oldtext->size > 4 && sscanf(oldp, "%d", &result) == 1) {
	    oldp += 4;
	    while (*oldp) {
		if (*oldp == '\n') {
		    int tmpres;
		    if (sscanf(++oldp, "%d", &tmpres) == 1) {
			if (tmpres == result) {
			    HTChunkPutc(newtext, ' ');
			    oldp += 3;			   /* Skip this code */
			}
		    }
		} else
		    HTChunkPutc(newtext, *oldp);
		oldp++;
	    }
	}
	HTChunkFree(oldtext);
	HTChunkTerminate(newtext);
	*error = newtext;
    }
    return;
}


/*							      HTFTPParseWelcome
**
**	This function parses the welcome message stored in ctrl->welcome.
**	Only multi-line messages are considered interesting, and the starting
**	return code is removed.
**
*/
PRIVATE void HTFTPParseWelcome ARGS1(HTChunk **, welcome)
{
    HTChunk *oldtext;
    if (!welcome || !*welcome || !(*welcome)->data) {
	if (PROT_TRACE) fprintf(stderr, "FTP......... No welcome message?\n");
	return;
    }
    oldtext = *welcome;
    {
	int result;		       /* The first return code in the chunk */
	char cont;					/* Either ' ' or '-' */
	char *oldp = oldtext->data;
	HTChunk *newtext = HTChunkCreate(128);
	if (oldtext->size > 4 && sscanf(oldp, "%d%c", &result, &cont) == 2) {
	    oldp += 4;
	    while (cont == '-') {
		HTChunkPutc(newtext, *oldp);
		if (*oldp == '\n') {
		    int tmpres;
		    if (isdigit(*++oldp) &&
			sscanf(oldp, "%d%c", &tmpres, &cont) == 2) {
			if (tmpres == result && cont == ' ')
			    break;
			else
			    oldp +=3;			   /* Skip this code */
		    }
		}
		oldp++;
	    }
	}
	HTChunkTerminate(newtext);
	HTChunkFree(oldtext);
	*welcome = newtext;
    }
}


/*							      HTFTPAddWelcome
**
**	This function accumulates every welcome messages from the various
**	states in the login procedure.
**
*/
PRIVATE void HTFTPAddWelcome ARGS1(ftp_ctrl_info *, ctrl)
{
    if (!ctrl->welcome)				            /* If first time */
	ctrl->welcome = HTChunkCreate(128);

    HTFTPParseWelcome(&ctrl->reply);
    if (ctrl->reply->size > 1) {
	HTChunkPutc(ctrl->welcome, '\n');
	HTChunkPuts(ctrl->welcome, ctrl->reply->data);
    }
}


#if 0 /* NOT NEEDED FOR THE MOMENT */
/*
 * is_ls_date() --
 *      Return TRUE if s points to a string of the form:
 *              "Sep  1  1990 " or
 *              "Sep 11 11:59 " or
 *              "Dec 12 1989  " or
 *              "FCv 23 1990  " ...
 *
 * Thanks to James.W.Matthews@Dartmouth.EDU (James W. Matthews)
 */
PRIVATE BOOL is_ls_date ARGS1(char *, s)
{
    /* must start with three alpha characget_striters */
    if (!isalpha(*s++) || !isalpha(*s++) || !isalpha(*s++))
	return FALSE;
    
    /* space */
    if (*s++ != ' ')
	return FALSE;
    
    /* space or digit */
    if ((*s != ' ') && !isdigit(*s))
	return FALSE;
    s++;
    
    /* digit */
    if (!isdigit(*s++))
	return FALSE;
    
    /* space */
    if (*s++ != ' ')
	return FALSE;
    
    /* space or digit */
    if ((*s != ' ') && !isdigit(*s))
	return FALSE;
    s++;
    
    /* digit */
    if (!isdigit(*s++))
	return FALSE;
    
    /* colon or digit */
    if ((*s != ':') && !isdigit(*s))
	return FALSE;
    s++;
    
    /* digit */
    if (!isdigit(*s++))
	return FALSE;
    
    /* space or digit */
    if ((*s != ' ') && !isdigit(*s))
	return FALSE;
    s++;
    
    /* space */
    if (*s++ != ' ')
	return FALSE;
    
    return TRUE;
} /* is_ls_date() */
#endif

/*								HTStrpMonth()
**
**	Returns the number of the month given or -1 on error.
**
**	BUG: Handles US dates only!!!
*/
PRIVATE int HTStrpMonth ARGS1(char *, month)
{
    int ret;
    if (!strncmp(month, "JAN", 3))
	ret = 0;
    else if (!strncmp(month, "FEB", 3))
	ret = 1;
    else if (!strncmp(month, "MAR", 3))
	ret = 2;
    else if (!strncmp(month, "APR", 3))
	ret = 3;
    else if (!strncmp(month, "MAY", 3))
	ret = 4;
    else if (!strncmp(month, "JUN", 3))
	ret = 5;
    else if (!strncmp(month, "JUL", 3))
	ret = 6;
    else if (!strncmp(month, "AUG", 3))
	ret = 7;
    else if (!strncmp(month, "SEP", 3))
	ret = 8;
    else if (!strncmp(month, "OCT", 3))
	ret = 9;
    else if (!strncmp(month, "NOV", 3))
	ret = 10;
    else if (!strncmp(month, "DEC", 3))
	ret = 11;
    else {
	ret = -1;
	if (PROT_TRACE)
	    fprintf(stderr, "HTStrpMonth. Couldn't resolve date.\n");
    }
    return ret;
}


/*								HTStrpTime()
**
**	Converts a date string from 'ls -l' to a time_t number
**	This is needed in order to put out the date using the same format
**	for all directory listings.
**
**	Returns 0 on error.
*/
PRIVATE time_t HTStrpTime ARGS1(char *, datestr)
{
    struct tm *time_info;		    /* Points to static tm structure */
    char *bcol = datestr;			             /* Column begin */
    char *ecol;						       /* Column end */
    long tval;
    int cnt;
    time_t curtime = time(NULL);
    if ((time_info = gmtime(&curtime)) == NULL) {
	if (PROT_TRACE)
	    fprintf(stderr, "HTStrpTime.. Can't get current time.\n");
	return (time_t) 0;
    }
    time_info->tm_isdst = -1;			      /* Disable summer time */
    for (cnt=0; cnt<3; cnt++)
    {					    /* Month */
	*bcol = toupper(*bcol);
        bcol++;
    }
    if ((time_info->tm_mon = HTStrpMonth(datestr)) < 0)
	return (time_t) 0;
    ecol = bcol;		   			       	      /* Day */
    while (*ecol++ == ' ');		       /* Spool to other side of day */
    while (*ecol++ != ' ');
    *--ecol = '\0';
    time_info->tm_mday = atoi(bcol);
    time_info->tm_wday = 0;
    time_info->tm_yday = 0;
    bcol = ++ecol;				             	     /* Year */
    if ((ecol = strchr(bcol, ':')) == NULL) {
	time_info->tm_year = atoi(bcol)-1900;
	time_info->tm_sec = 0;
	time_info->tm_min = 0;
	time_info->tm_hour = 0;
    } else {							     /* Time */
	/* If the time is given as hh:mm, then the file is less than 1 year
	   old, but we might shift calandar year. This is avoided by checking 
	   if the date parsed is future or not. */
	*ecol = '\0';
	time_info->tm_sec = 0;
	time_info->tm_min = atoi(++ecol);	       	/* Right side of ':' */
	time_info->tm_hour = atoi(bcol);		 /* Left side of ':' */
	if (mktime(time_info) > curtime)
	    --time_info->tm_year;
    }
    return ((tval = mktime(time_info)) == -1 ? (time_t) 0 : tval); 
}


/*								HTVMSStrpTime()
**
**	Converts a date string from vms to a time_t number
**	This is needed in order to put out the date using the same format
**	for all directory listings.
**
**	Returns 0 on error
*/
PRIVATE time_t HTVMSStrpTime ARGS1(char *, datestr)
{
    struct tm *time_info;		    /* Points to static tm structure */
    char *col;
    long tval;
    time_t curtime = time(NULL);
    if ((time_info = gmtime(&curtime)) == NULL)
	return (time_t) 0;
    time_info->tm_isdst = -1;			      /* Disable summer time */
    if ((col = strtok(datestr, "-")) == NULL)
	return (time_t) 0;
    time_info->tm_mday = atoi(col);				      /* Day */
    time_info->tm_wday = 0;
    time_info->tm_yday = 0;
    if ((col = strtok(NULL, "-")) == NULL ||
	(time_info->tm_mon = HTStrpMonth(col)) < 0)
	return (time_t) 0;
    if ((col = strtok(NULL, " ")) == NULL)			     /* Year */
	return (time_t) 0;
    time_info->tm_year = atoi(col)-1900;
    if ((col = strtok(NULL, ":")) == NULL)			     /* Hour */
	return (time_t) 0;
    time_info->tm_hour = atoi(col);
    if ((col = strtok(NULL, " ")) == NULL)			     /* Mins */
	return (time_t) 0;
    time_info->tm_min = atoi(col);
    time_info->tm_sec = 0;
    return ((tval = mktime(time_info)) < 0 ? (time_t) 0 : tval);
}


/*								HTFTPFilePerm()
**
**	Converts the file type from 'ls -l' into a long. The reason for
**	doing this is to be able to handle the file type and icon selection
**	similar to the Unix way used in HTBrowseDirectory().
**
*/
PRIVATE long HTFTPFilePerm ARGS1(char *, permission)
{
    char *strptr = permission;
    long mode = 0L;
    
    /* Special files etc. are all handled like regular files */
    switch (*strptr++) {					/* File type */
      case 'd':	mode = S_IFMT & S_IFDIR; break;
      case 'l': mode = S_IFMT & S_IFLNK; break;
      default:  mode = S_IFMT & S_IFREG; break;
    }
    if (*strptr++ == 'r') mode |= S_IRUSR;			     /* User */
    if (*strptr++ == 'w') mode |= S_IWUSR;
    if (*strptr == 'x')
	mode |= S_IXUSR;
    else if (*strptr == 's')
	mode |= (S_IXUSR | S_ISUID);
    else if (*strptr == 'S')
	mode |= S_ISUID;
    strptr++;
    if (*strptr++ == 'r') mode |= S_IRGRP;		            /* Group */
    if (*strptr++ == 'w') mode |= S_IWGRP;
    if (*strptr == 'x')
	mode |= S_IXGRP;
    else if (*strptr == 's')
	mode |= (S_IXGRP | S_ISGID);
    else if (*strptr == 'S')
	mode |= S_ISGID;
    strptr++;
    if (*strptr++ == 'r') mode |= S_IROTH;			    /* Other */
    if (*strptr++ == 'w') mode |= S_IWOTH;
    if (*strptr == 'x')
	mode |= S_IXOTH;
    else if (*strptr == 't')
	mode |= (S_IXOTH | S_ISVTX);
    else if (*strptr == 'T')
	mode |= S_ISVTX;
    strptr++;
    return mode;
}


/*							     parse_unix_line()
**
**	Extract the name, size, and date from an 'ls'. The function expects
**	the following format of the ls-line:
**
**	<permission> <nlink> <owner> [<group>] <size> <date> <filename>
**
**	The group is not always present and is therefore optional. Both owner
**	and group can be numbers.
**
**	Returns YES if OK, NO on error
*/
PRIVATE BOOL parse_unix_line ARGS2(char *,line, dir_file_info *,f_info)
{
    char *column;
    char *strptr;
    int ival;
    unsigned long lval;
    if (!line || !*line || !f_info)
	return NO;

    if ((column = strtok(line, " ")) == NULL)      	      /* Permissions */
	return NO;
    f_info->f_mode = HTFTPFilePerm(column);
    if ((column = strtok(NULL, " ")) == NULL)		            /* Links */
	return NO;
    if (sscanf(column, "%d", &ival) == 1)
	f_info->f_nlink = ival;
    if ((column = strtok(NULL, " ")) == NULL)		            /* Owner */
	return NO;
    StrAllocCopy(f_info->f_uid, column);
    if ((column = strtok(NULL, " ")) == NULL)           /* Group and/or Size */
	return NO;
    if (sscanf(column, "%lu", &lval) != 1) {
	StrAllocCopy(f_info->f_gid, column);
	if ((column = strtok(NULL, " ")) == NULL)
	    return NO;
	if (sscanf(column, "%lu", &lval) == 1)
	    f_info->f_size = lval;
    } else {				           /* Group can be a number! */
	strptr = column+strlen(column)+1;
	while (*strptr++ == ' ');
	if (isdigit(*--strptr)) {      	   /* Month can't start with a digit */
	    StrAllocCopy(f_info->f_gid, column);
	    if ((column = strtok(NULL, " ")) == NULL)
		return NO;
	    if (sscanf(column, "%lu", &lval) == 1)
		f_info->f_size = lval;
	} else {
	    StrAllocCopy(f_info->f_gid, "");
	    f_info->f_size = lval;
	}
    }
    column = column+strlen(column)+1;
    while (*column++ == ' ');
    strptr = --column+12;		      	     /* Find the date column */
    *strptr++ = '\0';
    if ((f_info->f_mtime = HTStrpTime(column)) == (time_t) 0)
	return NO;
    while (*strptr++ == ' ');			        /* Spool to filename */
    if ((f_info->f_mode & S_IFMT) == S_IFLNK) {        /* Strip any '->' */
	char *link = strstr(strptr-1, " -> ");
	if (link)
	    *link = '\0';
    }
    StrAllocCopy(f_info->f_name, --strptr);
    return YES;				        /* We have a full structure! */
}


/*						   	      parse_vms_line()
**
**      Format the name, date, and size from a VMS LIST line
**      into the dir_file_info structure
**
**	BUGS: Group, user and permissions are not parsed!
**
**	Returns YES if OK, NO on error
*/
PRIVATE BOOL parse_vms_line ARGS2(char *, line, dir_file_info *, f_info)
{
    int i, j, ialloc;
    char *cp, *cpd, *cps, *cdir, *sp = " ";
    
    /**  Get rid of information lines by making them blank too **/
    /**  Valid lines have the semi-colon version number token  **/
    if (!line || !*line || !f_info || (cp = strchr(line, ';')) == NULL) {
	return NO;
    }

    /** Cut out file or directory name at VMS version number **/
    *cp++ ='\0';
    StrAllocCopy(f_info->f_name,line);
    
    /** Cast VMS file and directory names to lowercase **/
    for (i=0; f_info->f_name[i]; i++)
	f_info->f_name[i] = tolower(f_info->f_name[i]);
    
    /** Uppercase terminal .z's or _z's **/
    if ((--i > 2) && f_info->f_name[i] == 'z' &&
	(f_info->f_name[i-1] == '.' || f_info->f_name[i-1] == '_'))
	f_info->f_name[i] = 'Z';
    
    /* Trim off VMS directory extensions */
    if ((cdir = strstr(f_info->f_name, ".dir")) != NULL) { /* Strip any .dir */
	f_info->f_mode = S_IFMT & S_IFDIR;
	*cdir = '\0';
    } else
	f_info->f_mode = S_IFMT & S_IFREG;

    /** Convert any tabs in rest of line to spaces **/
    cps = cp-1;
    while ((cps=strchr(cps+1, '\t')) != NULL)
	*cps = ' ';
    
    /** Collapse serial spaces **/
    i = 0; j = 1;
    cps = cp;
    while (cps[j] != '\0') {
	if (cps[i] == ' ' && cps[j] == ' ')
	    j++;
	else
	    cps[++i] = cps[j++];
    }
    cps[++i] = '\0';
    
    /** Track down the date **/
    if ((cpd=strchr(cp, '-')) != NULL) {
	if ((int)strlen(cpd) > 9 && isdigit(*(cpd-1)) &&
	    isalpha(*(cpd+1)) && *(cpd+4) == '-') {
	    if ((f_info->f_mtime = HTVMSStrpTime(cpd-2)) == (time_t) 0)
		return NO;
	}
    }
    
    /** Track down the size **/
    if ((cpd = strchr(cp, '/')) != NULL) {
	/* Appears be in used/allocated format */
	cps = cpd;
	while (isdigit(*(cps-1)))
	    cps--;
	if (cps < cpd)
	    *cpd = '\0';
	f_info->f_size = atoi(cps);
	cps = cpd+1;
	while (isdigit(*cps))
	    cps++;
	*cps = '\0';
	ialloc = atoi(cpd+1);
	/* Check if used is in blocks or bytes */
	if (f_info->f_size <= ialloc)
	    f_info->f_size *= 512;
    }
    else if ((cps=strtok(cp, sp)) != NULL) {
	/* We just initialized on the version number */
	/* Now let's hunt for a lone, size number    */
	while ((cps=strtok(NULL, sp)) != NULL) {
	    cpd = cps;
	    while (isdigit(*cpd))
		cpd++;
	    if (*cpd == '\0') {
		/* Assume it's blocks */
		f_info->f_size = atoi(cps) * 512;
		break;
	    }
	}
    }
    return YES;				        /* We have a full structure! */
}


/*						 	     parse_dir_entry() 
**
**      Given a line of LIST/NLST output in entry, return results 
**      and a file/dir name in f_info struct
**
**      If first_entry is true, this it must be the first name in a directory.
**	Returns YES if OK, NO on error
*/
PRIVATE BOOL parse_dir_entry ARGS4(ftp_data_info *, data, char *, entry,
				   BOOL, first_entry, dir_file_info *, f_info)
{
    BOOL status = YES;
    switch (data->ctrl->server) {
      case WINDOWS_NT:		   /* Might not send the UNIX first line :-( */
	if (first_entry) {
	    if (strncmp(entry, "total ", 6) && !strstr(entry, "not available"))
		status = parse_unix_line(entry, f_info);
	    else
		status = NO;
	} else
	    status = parse_unix_line(entry, f_info);
	break;

      case UNIX_SERVER:
      case PETER_LEWIS_SERVER:
      case MACHTEN_SERVER:
	/* Interpret and edit LIST output from Unix server */
	if (first_entry) {
	    if (data->ctrl->unsure_type == YES &&
		strncmp(entry, "total ", 6) &&
		(strstr(entry, "not available") != NULL)) {
		/* this isn't really a unix server! */
		if (PROT_TRACE)
		    fprintf(stderr, "FTP......... No, this isn't a UNIX server anyway :-(\n");
		data->ctrl->server = GENERIC_SERVER;
	    }
	    /* We might as well say that it is not unsure any more, as we
	       can't (or don't) do anything about it, */
	    data->ctrl->unsure_type = NO;
	    status = NO;
	} else
	    status = parse_unix_line(entry, f_info);
	break;
	
      case VMS_SERVER:
	/* Interpret and edit LIST output from VMS server */
	/* and convert information lines to zero length.  */
	status = parse_vms_line(entry, f_info);
	break;
	
      case CMS_SERVER:
	/* Can't be directory... "entry" already equals the correct f_name */
	StrAllocCopy(f_info->f_name, entry);
	f_info->f_mode = S_IFMT & S_IFREG;
	break;
	
      case NCSA_SERVER:
      case TCPC_SERVER:
	/* Directories identified by trailing "/" characters */
	StrAllocCopy(f_info->f_name, entry);
	{
	    int len = strlen(entry);
	    if (*(entry+len-1) == '/') {
		*(entry+len-1) = '\0';
		f_info->f_mode = S_IFMT & S_IFDIR;
	    } else {
		f_info->f_mode = S_IFMT & S_IFREG;
	    }
	}
	break;
	
      default:
	/* We cant tell if it is a directory since we only did an NLST :-( */
	StrAllocCopy(f_info->f_name, entry);
	f_info->f_mode = S_IFMT & S_IFREG;
	break;
    }
    return status;
}


PRIVATE int HTFTP_get_dir_string ARGS2(HTNetInfo *, data,
				       dir_file_info *, f_info)
{
    int status = 1;
    int ch;			      /* Must be int in order to contain EOF */
    BOOL got_line = NO;
    static BOOL first = YES;		    /* Is it the first time through? */
    HTChunk *chunk = HTChunkCreate(128);

    do {			                /* Until we have a nice line */
	while ((ch = HTInputSocket_getCharacter(data->isoc)) >= 0) {
	    if (ch == CR || ch == LF) {			      /* Terminator? */
		if (chunk->size != 0) {			    /* got some text */
		    if (((ftp_data_info *) data)->ctrl->server == VMS_SERVER) {
			/* Deal with MultiNet's wrapping of long lines - F.M.*/
			if (isdigit(*(chunk->data+chunk->size-1)))
			    continue;
		    }
		    HTChunkTerminate(chunk);
		    if (parse_dir_entry((ftp_data_info *) data, chunk->data,
					first, f_info)) {
			first = NO;
			got_line = YES;
			break;
		    } else {
			first = NO;
			HTChunkClear(chunk);
		    }
		}
	    } else
		HTChunkPutc(chunk, ch);
	}
    } while (got_line == NO && ch >= 0);
    if (ch < 0) {
	first = YES;
	status = (ch == EOF) ? 0 : ch;
    }
    HTChunkFree(chunk);
    return status;
}

/* ------------------------------------------------------------------------- */
/*	  FTP Client Functions for managing control and data connections     */
/* ------------------------------------------------------------------------- */

/*								HTFTP_send_cmd
**
**	This function sends a command through the control connection 
**	specified. The Telnet terminating end of line code <CRLF> is
**	appended automaticly. The parameter argument is ignored if NULL.
**
**	Returns 0 on OK, else -1 but does NOT close the connection
*/
PRIVATE int HTFTP_send_cmd ARGS3(ftp_ctrl_info *, ctrl, char *, cmd,
				 char *, pars)
{
    char *command;
    if (!ctrl && ctrl->sockfd < 0) {
	if (PROT_TRACE)
	    fprintf(stderr, "HTFTP_send_cmd: Invalid socket\n");
	return -1;
    }
    if ((command = (char *) malloc(strlen(cmd) + (pars ? strlen(pars)+1 : 0) +
				   2 + 1)) == NULL)
	outofmem(__FILE__, "HTFTP_send_cmd");
    if (pars && *pars)
	sprintf(command, "%s %s%c%c", cmd, pars, CR, LF);
    else
	sprintf(command, "%s%c%c", cmd, CR, LF);
    if (PROT_TRACE) {
	if (!strcasecomp(cmd, "pass"))
	    fprintf(stderr, "FTP Tx...... PASS ********\n");
	else
	    fprintf(stderr, "FTP Tx...... %s", command);
    }
#ifdef NOT_ASCII
    {
	char *sp;
	for (sp = command; *sp; sp++) {
	    *sp = TOASCII(*sp);
	}
    }
#endif
    {
	int status = NETWRITE(ctrl->sockfd, command,
			      (int) strlen(command));
	if (status < 0) {
	    if (PROT_TRACE)
		fprintf(stderr, "FTP......... Error sending command\n");
	    HTErrorSysAdd(ctrl->request, ERR_FATAL, NO, "NETWRITE");
	    free(command);
	    return -1;
	}
	free(command);
	return 0;
    }
}


/*							   HTFTP_get_response
**
**	This function gets the response from the net to the control connection
**	specified. If text is not NULL, the response text is put into this 
**	chunk. In case of OK, the freeing is then left to the callee. The
**	response is read until a <LF> is encountered, since not all servers
**	use a full telnet <CRLF> code.
**
**	Returns the 3 digit return code on OK, else -1 but does NOT close
**	the control connection.
*/
PRIVATE int HTFTP_get_response ARGS2(ftp_ctrl_info *, ctrl_info,
				     HTChunk **, text)
{
    int result;				         /* Three-digit decimal code */
    int offset = 0;		      /* Offset for each newline in response */
    BOOL first_line = YES;
    int ch;
    HTChunk *chunk = HTChunkCreate(128);

    if (!ctrl_info && ctrl_info->sockfd < 0) {
	if (PROT_TRACE)
	    fprintf(stderr, "HTFTP_get_response: Invalid socket\n");
	return -1;
    }

    /* Read response */
    while ((ch = HTInputSocket_getCharacter(ctrl_info->isoc)) >= 0) {
	if (ch == LF) {
	    int tmpres;
	    char cont;
	    if (first_line == YES) {
		if (sscanf(chunk->data, "%d%c", &result, &cont) < 2) {
		    if (PROT_TRACE)
			fprintf(stderr,
				"FTP Rx...... `%s\' - no code found?\n",
				chunk->data);
		    HTChunkFree(chunk);
		    return -1;
		}
		if (cont == '-') {
		    HTChunkPutc(chunk, '\n');
		    offset = chunk->size;		  /* Remember offset */
		    first_line = NO;
		} else {
		    HTChunkTerminate(chunk);
		    break;
		}
	    } else {
		if (isdigit(*(chunk->data+offset)) &&
		    sscanf(chunk->data+offset, "%d%c", &tmpres, &cont) == 2 &&
		    tmpres == result && cont == ' ') {
		    HTChunkTerminate(chunk);
		    break;
		} else {
		    HTChunkPutc(chunk, '\n');
		    offset = chunk->size;		    /* Update offset */
		}
	    }
	} else
	    HTChunkPutc(chunk, ch);
    }
    if (!chunk->size || ch < 0) {		        /* No response read? */
	if (PROT_TRACE) fprintf(stderr, "FTP Rx...... No response?\n");
	HTChunkFree(chunk);
	return -1;
    }
    if (PROT_TRACE) fprintf(stderr, "FTP Rx...... %s\n", chunk->data);
    if (!text)		    /* Response text not wanted so we free the chunk */
	HTChunkFree(chunk);
    else {
	if (*text)				   /* Free old value, if any */
	    HTChunkFree(*text);
	*text = chunk;
    }
    return result;
}



/*							HTFTP_close_data_con
**	Closes the data connection and frees memory
**	Returns 0 if OK, -1 on error
*/
PRIVATE int HTFTP_close_data_con ARGS1(ftp_data_info *, data)
{
    int status = 0;
    if (data) {
	if (data->sockfd >= 0) {
	    if (PROT_TRACE)
		fprintf(stderr, "FTP......... Closing data socket %d\n",
			data->sockfd);
	    if ((status = NETCLOSE(data->sockfd)) < 0)
		HTErrorSysAdd(data->request, ERR_FATAL, NO, "NETCLOSE");
#ifdef REPEAT_LISTEN
	    if (master_socket == data->sockfd)
		master_socket = -1;
#endif
	}
	FREE(data->host);
	FREE(data->datatype);
	free(data);
    } else {
	if (PROT_TRACE) fprintf(stderr, "HTFTP_close_data_con: bad argument!");
	status = -1;
    }
    return status;
}


/*							HTFTP_close_ctrl_con
**	Only if the control connection has no data connection(s) pending
**	then it can be closed and the memory freed.
**	Returns 0 if OK, -1 on error
*/
PRIVATE int HTFTP_close_ctrl_con ARGS1(ftp_ctrl_info *, ctrl)
{
    int status = 0;
    if (ctrl && (!ctrl->data_cons ||
		 (ctrl->data_cons && !HTList_count(ctrl->data_cons)))) {
	if (PROT_TRACE)
	    fprintf(stderr,
		    "FTP......... Closing control socket %d\n", ctrl->sockfd);
	if (ctrl->sockfd >= 0) {
	    if ((status = NETCLOSE(ctrl->sockfd)) < 0)
		HTErrorSysAdd(ctrl->request, ERR_FATAL, NO, "NETCLOSE");
	}
	if (ctrl->isoc)
	    HTInputSocket_free(ctrl->isoc);
	FREE(ctrl->location);
	if (ctrl->user) {
	    FREE(ctrl->user->domain);
	    FREE(ctrl->user->id);
	    FREE(ctrl->user->passwd);
	    free(ctrl->user);
	}
	if (ctrl->welcome)
	    HTChunkFree(ctrl->welcome);
	if (ctrl->reply)
	    HTChunkFree(ctrl->reply);
	HTList_delete(ctrl->data_cons);
	free(ctrl);
    }
    return status;
}


/*							HTFTP_abort_ctrl_con
**	Closes the control connection without looking if any data connections
**	are pending => they are all removed, so be careful!
**	Returns 0 if OK, -1 on error
*/
PRIVATE int HTFTP_abort_ctrl_con ARGS1(ftp_ctrl_info *, ctrl)
{
    int status = 0;
    if (!ctrl) {
	if (PROT_TRACE)
	    fprintf(stderr, "HTFTP_abort_ctrl_con called with bad argument\n");
	return -1;
    }
    if (PROT_TRACE) fprintf(stderr, "FTP......... Aborting ctrl socket %d\n",
			    ctrl->sockfd);

    /* Close any pending data connections */
    if (ctrl->data_cons && HTList_count(ctrl->data_cons)) {
	HTList *cur = ctrl->data_cons;
	ftp_data_info *pres;
	while ((pres = (ftp_data_info *) HTList_nextObject(cur))) {
	    HTFTP_close_data_con(pres);
	}
	HTList_delete(ctrl->data_cons);
	ctrl->data_cons = NULL;
    }

    /* If a session is going on, the control connections are closed later */
    if (!session) {
	if (ctrl->sockfd >= 0) {
	    if ((status = NETCLOSE(ctrl->sockfd)) < 0)
		HTErrorSysAdd(ctrl->request, ERR_FATAL, NO, "NETCLOSE");
	}
	if (ctrl->isoc)
	    HTInputSocket_free(ctrl->isoc);
	FREE(ctrl->location);
	if (ctrl->user) {
	    FREE(ctrl->user->domain);
	    FREE(ctrl->user->id);
	    FREE(ctrl->user->passwd);
	    free(ctrl->user);
	}
	if (ctrl->welcome)
	    HTChunkFree(ctrl->welcome);
	if (ctrl->reply)
	    HTChunkFree(ctrl->reply);
	free(ctrl);
    }
    return status;
}


/*							   HTFTP_parse_login
**
**    	Scan 'login' part of URL for portnumber, uid and passwd. The
**	expected format is [user[:password]@]host[:port]. The 'domain' field
**	in the user structure is always filled out together with the
**	serv_port. The rest is optional.
**
**	Returns YES if anything BUT the domain and serv_port is specified,
**	else NO 
*/
PRIVATE BOOL HTFTP_parse_login ARGS3(char *, url, user_info *, user,
				     u_short *, serv_port)
{
    BOOL status = NO;
    char *login = HTParse(url, "", PARSE_HOST);
    char *host = strrchr(login, '@');
    
    if (host) {				      /* Uid and/or passwd specified */
	char *uid = login;
	char *passwd;
	FREE(user->id);					  /* Skip old values */
	FREE(user->passwd);
	*host++ = '\0';
	if ((passwd = strrchr(uid, ':')) != NULL) {	 /* Passwd specified */
	    *passwd++ = '\0';    
	    if (passwd-1 > uid) {	         /* Passwd AND uid specified */
		HTUnEscape(passwd);
		StrAllocCopy(user->passwd, passwd);
	    }
	}
	HTUnEscape(uid);
	StrAllocCopy(user->id, uid);
	status = YES;
    } else {
	host = login;
    }
    {
	char *portstr;
	if ((portstr = strrchr(host, ':')) != NULL) {	   /* Port specified */
	    char *endp = NULL;
	    *portstr++ = '\0';
	    *serv_port = (u_short) strtol(portstr, &endp, 10);
	    if (endp && *endp)	 /* If portstr is not good, use default port */
		*serv_port = (u_short) IPPORT_FTP;
	}
    }
    StrAllocCopy(user->domain, host);	              /* This is what's left */
    free(login);
    return status;
}


/*							   HTFTP_parse_datatype
**
**    	Scan 'ftptype' part of URL for the data type with parameters and
**	returns the result in accordance with the FTP standard. If nothing is
**	found then datatype = NULL.
**
**	Returns YES if type is found, else NO 
*/
PRIVATE BOOL HTFTP_parse_datatype ARGS2(char *, url, char **, datatype)
{
    BOOL retour = NO;
    char *path = HTParse(url, "", PARSE_PATH);
    char *type = strrchr(path, ';');
    char dtype[6];
    char *tptr = dtype;
    
    if (type && !strncasecomp(++type, "type=", 5)) {   	   /* type specified */
	*tptr++ = toupper(*(type+5));		    /* Look at the type-code */
	if (*dtype == 'L') {		     /* We must look for a byte_size */
	    int cnt;
	    *tptr++ = ' ';
	    for (cnt=0; cnt<3 && *(type+6+cnt); cnt++)       /* Max 3 digits */
		*tptr++ = *(type+6+cnt);
	} else if (*dtype == 'A' || *dtype == 'E') {
	    *tptr++ = ' ';
	    *tptr++ = toupper(*(type+6));     		    /* Get form-code */
	}
	*tptr = '\0';
	StrAllocCopy(*datatype, dtype);
	if (PROT_TRACE)
	    fprintf(stderr, "FTP......... Datatype found: `%s\'\n", *datatype);
	retour = YES;
    }
    free(path);
    return retour;
}


/*							   HTFTP_init_con
**
**	This function returns a control connection structure linked with a
**	data connection structure. The control connection might already be
**	open if HTFTPReuseCtrlCon == YES, but that is indicated in the state
**	variable. ctrl->user->domain is always filled out but id and passwd
**	are optional.
**	If error, NULL is returned.
*/
PRIVATE ftp_ctrl_info *HTFTP_init_con ARGS2(HTRequest *, req, char *, url)
{
    int status;
    BOOL use_url = NO;	   /* YES if uid and passwd are specified in the URL */
    u_short serv_port = IPPORT_FTP;		/* Might be changed from URL */
    
    ftp_ctrl_info *ctrl;
    ftp_data_info *data;
    user_info user;		    /* Contains userid, passwd etc. from URL */

    if (!url || !*url) {
	if (PROT_TRACE)
	    fprintf(stderr, "HTFTP_get_connection: Bad server address!\n");
	return NULL;
    }

    /* Initiate new data connection structure and bin to request */
    if ((data = (ftp_data_info *) calloc(1, sizeof(ftp_data_info))) == NULL)
	outofmem(__FILE__, "HTFTP_get_ctrl_con");
    data->sockfd = -1;			            /* Illigal socket number */
    data->passive = 0;			 /* We do the active open pr default */
    data->request = req;
    req->net_info = (HTNetInfo *) data;
    
    /* Scan URL for uid, pw and portnumber */
    memset((void *) &user, '\0', sizeof(user_info));
    use_url = HTFTP_parse_login(url, &user, &serv_port);

    {
	char *filename = HTParse(url, "", PARSE_PATH+PARSE_PUNCTUATION);
	char *strptr;

	/* Check if file name is a directory */
	if (!*(strptr = filename) || 
	    *(strptr = filename+strlen(filename)-1) == '/') {
	    data->directory = YES;
	} else {
	    /* If data type is not specified in URL let's find it ourselves. */
	    HTUnEscape(filename);
	    data->fileformat = HTFileFormat(filename,
					    &req->content_encoding,
					    &req->content_language);
	    if (HTFTP_parse_datatype(filename, &data->datatype) != YES) {
		if ((req->content_encoding != HTAtom_for("8bit") &&
		     req->content_encoding != HTAtom_for("7bit"))) {
		    if (PROT_TRACE)
			fprintf(stderr, "FTP......... Binary data mode\n");
		    StrAllocCopy(data->datatype, "I");
		}
	    } else {
		/* Chop off data type */
		strptr = strrchr(filename, ';');
		*strptr = '\0';
	    }
	}
	FREE(filename);
    }

    /* Look if control connection already exists else generate new one */
    if (session) {
	BOOL found = NO;
	HTList *cur = session;
	SockA sock_addr;			/* SockA is defined in tcp.h */
	char *host;
	    
	/* if theres an @ then use the stuff after it as a hostname */
	{
	    char *fullhost = HTParse(url, "", PARSE_HOST);
	    char *at_sign;
	    if((at_sign = strchr(fullhost, '@')) != NULL)
		host = at_sign+1;
	    else
		host = fullhost;
	}
	
	/* Set up defaults: */
	memset((void *) &sock_addr, '\0', sizeof(sock_addr));
	sock_addr.sin_family = AF_INET;
	sock_addr.sin_port = htons(serv_port);
	
	/* Get node name */
	if (HTParseInet(&sock_addr, host, NO) < 0) {
	    if (PROT_TRACE) fprintf(stderr,
			       "FTP......... Can't locate remote host `%s\'\n", host);
	    FREE(user.domain);
	    FREE(user.id);
	    FREE(user.passwd);
	    free(host);
	    return NULL;
	}
	{
	    /* Check if host, port and user info is the same */
	    u_long new_node = ntohl(sock_addr.sin_addr.s_addr);
	    while ((ctrl = (ftp_ctrl_info *) HTList_nextObject(cur))) {
		if (new_node==ctrl->serv_node && serv_port==ctrl->serv_port) {
		    if ((user.id && strcmp(user.id, ctrl->user->id)) ||
			(user.passwd && strcmp(user.id, ctrl->user->passwd))) {
			found = NO;
		    } else {
			found = YES;
			break;
		    }
		}
	    }
	}
	FREE(host);
	if (found) {
	    if (PROT_TRACE) fprintf(stderr,
			       "FTP......... Already have connection for %d.%d.%d.%d. on port %d, socket %d at location `%s\'\n",
			       (int)*((unsigned char *)(&ctrl->serv_node)+0),
			       (int)*((unsigned char *)(&ctrl->serv_node)+1),
			       (int)*((unsigned char *)(&ctrl->serv_node)+2),
			       (int)*((unsigned char *)(&ctrl->serv_node)+3),
			       ctrl->serv_port,
			       ctrl->sockfd,
			       ctrl->location);
	    data->ctrl = ctrl;			       /* Link them together */
	    HTList_addObject(ctrl->data_cons, (void *) data); /* Add to list */
	    FREE(user.domain);
	    FREE(user.id);
	    FREE(user.passwd);
	    return ctrl;           /* This should return the found structure */
	} else {
	    if (PROT_TRACE)
		fprintf(stderr, "FTP......... No existing connection found, so I build a new\n");
	}
    }
    
    /* Set up data structure for control connection and bind to request */
    if ((ctrl = (ftp_ctrl_info *) calloc(1, sizeof(ftp_ctrl_info))) == NULL ||
	(ctrl->user = (user_info *) calloc(1, sizeof(user_info))) == NULL)
	outofmem(__FILE__, "HTFTP_init_con");
    ctrl->serv_port = serv_port;
    ctrl->sockfd = -1;			            /* Illigal socket number */
    ctrl->request = req;
    StrAllocCopy(ctrl->location, "");			  /* Default is root */
    ctrl->server = UNKNOWN;
    ctrl->unsure_type = YES;
    ctrl->use_list = NO;
    ctrl->state = FTP_IDLE;
    if (session)
	HTList_addObject(session, (void *) ctrl);
    data->ctrl = ctrl;				       /* Link them together */
    ctrl->data_cons = HTList_new();
    HTList_addObject(ctrl->data_cons, (void *) data);	    /* First element */

    /* Initialize user info */
    if (HTFTPUserInfo && !old_user) {   /* If first time */
	if ((old_user = (user_info *) calloc(1, sizeof(user_info))) == NULL)
	    outofmem(__FILE__, "HTFTP_init_con");
	StrAllocCopy(old_user->domain, "");/* Can't strcmp with NULL, can I? */
    }
    if (use_url) {
	StrAllocCopy(ctrl->user->domain, user.domain);
	StrAllocCopy(ctrl->user->id, user.id);
	if (user.passwd) {
	    StrAllocCopy(ctrl->user->passwd, user.passwd);
	}
    } else if (HTFTPUserInfo && !strcmp(old_user->domain, user.domain)) {
	StrAllocCopy(ctrl->user->domain, user.domain);
	if (old_user->id) {
	    StrAllocCopy(ctrl->user->id, old_user->id);
	    if (old_user->passwd)
		StrAllocCopy(ctrl->user->passwd, old_user->passwd);
	}
    } else {
	char *uid = getenv("USER");
	StrAllocCopy(ctrl->user->domain, user.domain);
	StrAllocCopy(ctrl->user->id, "anonymous");
	if (HTGetMailAddress())
	    StrAllocCopy(ctrl->user->passwd, HTGetMailAddress());
	else if (uid) {
	    StrAllocCopy(ctrl->user->passwd, uid);
	    StrAllocCat(ctrl->user->passwd, "@");
	} else {
	    StrAllocCopy(ctrl->user->passwd, WWW_FTP_CLIENT);
	    StrAllocCat(ctrl->user->passwd, "@");
	}
    }
    FREE(user.domain);
    FREE(user.id);
    FREE(user.passwd);

    /* Now get ready for a connect */
    if ((status = HTDoConnect((HTNetInfo *) ctrl, url, serv_port,
			      &ctrl->serv_node, NO)) < 0)
    {
	if (PROT_TRACE)
	    fprintf(stderr, "HTFTP_init_con: Connection not established!\n");
	HTFTP_abort_ctrl_con(ctrl);
	return NULL;
    }
    ctrl->isoc = HTInputSocket_new(ctrl->sockfd);               /* buffering */
    
    if (PROT_TRACE) 
 	fprintf(stderr, "FTP......... Control connected, socket %d\n",
		ctrl->sockfd);
    return ctrl;
}


#ifdef LISTEN
/*	Open a master socket for listening on
**	-------------------------------------
**
**	When data is transferred, we open a port, and wait for the server to
**	connect with the data.
**
** On entry,
**	master_socket	Must be negative if not set up already.
** On exit,
**	Returns		The listening data socket if OK
**			Less than zero if error.
**	master_socket	is socket number if good, else negative.
**	port_number	is valid if good.
*/
PRIVATE BOOL get_listen_socket ARGS1(ftp_data_info *, data)
{
    SockA local_addr;				   /* Binary network address */
    int status = -1;

#ifdef REPEAT_LISTEN
    if (master_socket >= 0) {				     /* Done already */
	data->sockfd = master_socket;
	if (PROT_TRACE)
	    fprintf(stderr, "FTP......... Reusing passive data socket: %d\n",
		    data->sockfd);
	return data->sockfd;
    }
#endif /* REPEAT_LISTEN */

    /* Create internet socket */
    if ((data->sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
	return HTErrorSysAdd(data->request, ERR_FATAL, NO, "socket");
    if (PROT_TRACE)
	fprintf(stderr, "HTListen.... Created socket number %d\n",
		data->sockfd);
    
    /* Search for a free port. */
    memset((void *) &local_addr, '\0', sizeof(local_addr));
    local_addr.sin_family = AF_INET;	    /* Family = internet, host order */
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* For multi homed hosts */

    /* Inherit the local address information from the control socket */
    {
	int addr_size = sizeof(local_addr);
	if (getsockname(data->ctrl->sockfd, (struct sockaddr *)
			&local_addr, &addr_size) < 0) {
	    status = HTErrorSysAdd(data->request, ERR_FATAL, NO,"getsockname");
	    goto errorfin;
	}
    }

#ifdef POLL_PORTS
    {
        unsigned short old_port_number = port_number;
	for (port_number=old_port_number+1;;port_number++) {
	    if (port_number > LAST_TCP_PORT)
		port_number = FIRST_TCP_PORT;
	    if (port_number == old_port_number) {
		if (PROT_TRACE)
		    fprintf(stderr, "FTP......... No data port available.\n");
		goto errorfin;
	    }
	    local_addr.sin_port = htons(port_number);

	    /* The socket address is casted to generic sockaddr */
	    if (bind(data->sockfd, (struct sockaddr *) &local_addr,
		     sizeof(local_addr)) == 0)
		break;				     /* We have found a port */
	    status = HTErrorSysAdd(data->request, ERR_FATAL, NO, "bind");
	}
    }
#else
    {
	local_addr.sin_port = 0;	     /* Unspecified: please allocate */

	/* The socket address is casted to a generic address */
	if (bind(data->sockfd, (struct sockaddr *) &local_addr,
		 sizeof(local_addr)) < 0) {
	    status = HTErrorSysAdd(data->request, ERR_FATAL, NO, "bind");
	    goto errorfin;
	}
    }
#endif /* POLL_PORTS */
    /* Now we must find out who we are to tell the other guy. */
    {
	int addr_size = sizeof(local_addr);
	if (getsockname(data->sockfd, (struct sockaddr *) &local_addr,
			&addr_size) < 0) {
	    status = HTErrorSysAdd(data->request, ERR_FATAL, NO,"getsockname");
	    goto errorfin;
	}
	if (PROT_TRACE) fprintf(stderr, "FTP......... This host is `%s\'\n",
			   HTInetString(&local_addr));
    }
    if (PROT_TRACE) fprintf(stderr, "FTP......... Bound to port %d on %s\n",
		       (int) ntohs(local_addr.sin_port),
		       HTInetString(&local_addr));

    /* this_addr is a static global, we can refer to later */
    if (!this_addr && (this_addr = (char *) malloc(24)) == NULL)
	outofmem(__FILE__, "get_listen_socket");
    {
	u_long addr = local_addr.sin_addr.s_addr;
	u_short port = local_addr.sin_port;
	sprintf(this_addr, "%d,%d,%d,%d,%d,%d",
		(int)*((unsigned char *)(&addr)+0),
		(int)*((unsigned char *)(&addr)+1),
		(int)*((unsigned char *)(&addr)+2),
		(int)*((unsigned char *)(&addr)+3),
		(int)*((unsigned char *)(&port)+0),
		(int)*((unsigned char *)(&port)+1));
    }

    /* Inform TCP that we will accept connections. Backlog is 1 as we only
       want (and expect) one connection. If a 3rd host makes a connect
       to this port, we have problems! */
    if (listen(data->sockfd, 1) < 0) {
        status = HTErrorSysAdd(data->request, ERR_FATAL, NO, "listen");
	goto errorfin;
    }
    if (PROT_TRACE) fprintf(stderr,
		       "FTP......... Data socket number %d listening\n",
		       data->sockfd);

#ifdef REPEAT_LISTEN
    master_socket = data->sockfd;		     /* Update master_socket */
#endif /* REPEAT_LISTEN */
    return data->sockfd;			       		     /* Good */

  errorfin:
    NETCLOSE(data->sockfd);
    data->sockfd = -1;
    return -1;
}
#endif /* LISTEN */


/*						   		HTFTP_login
**
**    	This function makes a login to a ftp-server. It takes the user name
**	and passwd specified in ctrl->user and if that fails or an additional
**	account is needed, the user is prompted. As it is difficult, when
**	the server sends it's welcome message, we receive them all and choose
**	the longest.
**
**	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
*/
PRIVATE int HTFTP_login ARGS1(ftp_ctrl_info *, ctrl)
{
    enum _state {
	ERROR  = -2,
	FAILURE = -1,
	SUCCESS = 0,
	BEGIN,
	SENT_UID,
	SENT_PASSWD,
	NEED_USER_INFO,
	NEED_PASSWD,
	NEED_ACCOUNT,
	SENT_ACCOUNT
    } state = BEGIN;
    BOOL asked = YES;		    /* Have we already asked for uid/passwd? */
    int status = HTFTP_get_response(ctrl, &ctrl->reply);     /* Get greeting */
    if (status < 0) {
        if (PROT_TRACE) fprintf (stderr, "FTP......... Interrupted at beginning of login.\n");
	return ERROR;
    } else
	HTFTPAddWelcome(ctrl);

    /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
    while (state > 0) {
	switch (state) {
	  case BEGIN:
	    if (!HTFTP_send_cmd(ctrl, "USER", ctrl->user->id))
		state = SENT_UID;
	    else
		state = ERROR;
	    break;

	  case SENT_UID:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2) {		    /* Logged in w/o passwd! */
		HTFTPAddWelcome(ctrl);
		state = SUCCESS;
	    } else if (status/100 == 3) {      		/* Password demanded */
		HTFTPAddWelcome(ctrl);
		state = NEED_PASSWD;
	    } else if (status == 530 && asked == YES)
		state = NEED_USER_INFO;			     /* User unknown */
	    else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;

	  case NEED_PASSWD:
	    if (!ctrl->user->passwd) {			/* Got to ask for it */
		char *prompt = NULL;
		StrAllocCopy(prompt, "Enter password for user: ");
		StrAllocCat(prompt, ctrl->user->id);
		StrAllocCat(prompt, "@");
		StrAllocCat(prompt, ctrl->user->domain);
		StrAllocCat(prompt, ": ");
		if ((ctrl->user->passwd = HTPromptPassword(prompt)) == NULL) {
		    state = ERROR;
		    free(prompt);
		    break;
		}
		free(prompt);
	    }
	    /* If userid = "anonymous" then make sure that there is a '@' at
	       the end of the passwd if it is without domain name */
	    if (!strcasecomp(ctrl->user->id, "anonymous")) {
		char *astr;
		if ((astr = strchr(ctrl->user->passwd, '@')) == NULL)
		    StrAllocCat(ctrl->user->passwd, "@");
	    }
	    if (!HTFTP_send_cmd(ctrl, "PASS", ctrl->user->passwd))
		state = SENT_PASSWD;
	    else
		state = ERROR;
	    break;

	  case SENT_PASSWD:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2) {		    /* Logged in with passwd */
		HTFTPAddWelcome(ctrl);
		state = SUCCESS;
	    } else if (status/100 == 3) {      		 /* Account demanded */
		HTFTPAddWelcome(ctrl);
		state = NEED_ACCOUNT;
	    } else if (status == 530 && asked == YES)
		state = NEED_USER_INFO;			     /* User unknown */
	    else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;
	    
	  case NEED_ACCOUNT:
	    {
		char *prompt = NULL;
		char *account = NULL;
		StrAllocCopy(prompt, "Enter account for user: ");
		StrAllocCat(prompt, ctrl->user->domain);
		StrAllocCat(prompt, "@");
		StrAllocCat(prompt, ctrl->user->id);
		if ((account = HTPrompt(prompt, NULL)) != NULL &&
		    !HTFTP_send_cmd(ctrl, "ACCT", account)) {
		    state = SENT_ACCOUNT;
		} else {
		    state = ERROR;
		}
		free(prompt);
		free(account);
	    }
	    break;

	  case SENT_ACCOUNT:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2) {
		HTFTPAddWelcome(ctrl);
		state = SUCCESS;
	    } else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;
	    
	  case NEED_USER_INFO:
	    {
		char *prompt = NULL;
		StrAllocCopy(prompt, "Enter username and password for: ");
		StrAllocCat(prompt, ctrl->user->domain);
		FREE(ctrl->user->id);
		FREE(ctrl->user->passwd);
		HTPromptUsernameAndPassword(prompt, &ctrl->user->id,
					     &ctrl->user->passwd);
		if (ctrl->user->id && ctrl->user->passwd &&
		    !HTFTP_send_cmd(ctrl, "USER", ctrl->user->id))
		    state = SENT_UID;
		else
		    state = ERROR;
		free(prompt);
	    }
	    asked = NO;
	    break;

	  case FAILURE:			      /* Otherwise gcc complains :-( */
	  case ERROR:
	  case SUCCESS:
	    break;
	} /* end of switch */
    }
    if (state == SUCCESS) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP......... Logged in at `%s\' as `%s\'\n",
		    ctrl->user->domain, ctrl->user->id);
    }

    /* This is a real pain this reuse user stuff :-( */
    if (HTFTPUserInfo) {
	StrAllocCopy(old_user->domain, ctrl->user->domain);
	StrAllocCopy(old_user->id, ctrl->user->id);
	StrAllocCopy(old_user->passwd, ctrl->user->passwd);
    }
    return state;
}

/*						   		HTFTP_logout
**
**    	This function logs out from a ftp-server.
**
**	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
*/
PRIVATE int HTFTP_logout ARGS1(ftp_ctrl_info *, ctrl)
{
    enum _state {
	ERROR  = -2,
	FAILURE = -1,
	SUCCESS = 0,
	BEGIN,
	SENT_QUIT
    } state = BEGIN;
    int status;

    /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
    while (state > 0) {
	switch (state) {
	  case BEGIN:
	    if (!HTFTP_send_cmd(ctrl, "QUIT", NULL))
		state = SENT_QUIT;
	    else
		state = ERROR;
	    break;

	  case SENT_QUIT:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2)
		state = SUCCESS;
	    else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;

	  case FAILURE:			      /* Otherwise gcc complains :-( */
	  case ERROR:
	  case SUCCESS:
	    break;
	}
    }
    return state;
}


/*						   	    HTFTP_get_data_con
 **
 **    	Gets a valid data connection to the server and initializes the
 **	transfer mode.
 **
 **	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
 */
PRIVATE int HTFTP_get_data_con ARGS3(HTRequest *, request,
				     ftp_data_info *, data, char *, url)
{
    enum _state {
	ERROR = -2,
	FAILURE = -1,
	SUCCESS = 0,
	BEGIN,
	SENT_TYPE,
	SENT_PASV,
	SENT_PORT,
	NEED_ACTIVE,		 /* We are the active ones in the connection */
	NEED_PASSIVE					   /* We are passive */
    } state = BEGIN;
    int serv_port;
    int status;
    ftp_ctrl_info *ctrl = data->ctrl;
    
    /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
    while (state > 0) {
	switch (state) {
	  case BEGIN:

	    /* First check if it is necessary to send TYPE, else send PASV */
	    if (data->datatype) {
		if (!HTFTP_send_cmd(ctrl, "TYPE", data->datatype))
		    state = SENT_TYPE;
		else
		    state = ERROR;
	    } else {
		if (!HTFTP_send_cmd(ctrl, "PASV", NULL))
		    state = SENT_PASV;
		else
		    state = ERROR;
	    }
	    break;
	    
	  case SENT_PASV:
	    {
		status = HTFTP_get_response(ctrl, &ctrl->reply);
		if (status == 227) {		    
		    /* If succes, we have to scan for the returned port number.
		       However, the format for the response is not standard, so
		       the best thing to do is to scan for the first digit
		       after the status code, see RFC1123 */
		    char *portstr;
		    int h0, h1, h2, h3, p0, p1;
		    portstr = ctrl->reply->data+3;
		    while (*portstr && !isdigit(*portstr++));
		    if (!*portstr || sscanf(--portstr, "%d,%d,%d,%d,%d,%d",
					    &h0, &h1, &h2, &h3, &p0, &p1)<6) {
			if (PROT_TRACE) fprintf(stderr,
			    "FTP......... PASV reply has no IP-address or port number\n");
			state = ERROR;
		    } else {
			if ((data->host = (char *) malloc(30)) == NULL)
			    outofmem(__FILE__, "HTFTP_get_data_con");

			/* Dummy URL containing the host returned by PASV */
			sprintf(data->host, "ftp://%d.%d.%d.%d/",
				h0, h1, h2, h3);
			serv_port = (p0<<8)+p1;
			state = NEED_ACTIVE;
		    }
		} else if (status/100 == 4)
		    state = FAILURE;
		else if (status == 530)			 /* Not logged in??? */
		    state = ERROR;
		else
		    state = NEED_PASSIVE;      /* If error, try PORT instead */
	    }
	    break;

	  case NEED_ACTIVE:
	    /* Now get ready for a connect */
	    if (PROT_TRACE) fprintf(stderr,
			       "FTP......... Server is listening on port %d\n",
			       serv_port);

#ifdef OLD_CODE
	    /* We can't generally expect that the data connection is going to
	       be on the same host as the control connection. Use therefore
	       not the URL but the host port returned by the PASV call */

	    /* Got to strip any host port indication as this is for the control
	       connection. Thanks to ramey@jello.csc.ti.com (Joe Ramey) */
	    {
		char *host, *port;
		char *fullhost = HTParse(url, "",
				 PARSE_ACCESS+PARSE_HOST+PARSE_PUNCTUATION);
		/* Must spool by a UserID and PW if any (J.A.Stubbs) */
		if ((host = strchr(fullhost, '@')) == NULL)
		    host = fullhost;
		else
		    host++;
		port = strrchr(host, ':');
		if (port && strncmp(port, "://", 3))
		    *port = '\0';
		status = HTDoConnect((HTNetInfo *) data, fullhost, serv_port,
				     (u_long *) NULL, YES);
		free(fullhost);
	    }
#endif /* OLD_CODE */
	    status = HTDoConnect((HTNetInfo *) data, data->host, serv_port,
				 (u_long *) NULL, YES);
	    if (status < 0) {
		if (PROT_TRACE) fprintf(stderr,
				   "FTP......... Data connection failed using PASV, let's try PORT instead\n");
		HTErrorFree(request);	     /* Don't generate error message */
		state = NEED_PASSIVE;
	    } else if (status >= 0) {
		if (PROT_TRACE) fprintf(stderr, "FTP......... Data connected using PASV, socket %d\n", data->sockfd);
		state = SUCCESS;
	    } else {
		state = ERROR;     	       		      /* Interrupted */
	    }
	    break;

	  case NEED_PASSIVE:
#ifdef LISTEN
	    /* The server didn't accept our PASV so now we try ourselves to be
	       passive using PORT */
	    if (get_listen_socket(data) < 0 ||
		HTFTP_send_cmd(ctrl, "PORT", this_addr))
		state = ERROR;
	    else
		state = SENT_PORT;
#else
	    /* If PORT is not compiled, then there is nothing we can do! */
	    if (PROT_TRACE) fprintf(stderr, "FTP......... PORT is not possible!\n");
	    state = ERROR;
#endif
	    break;

	  case SENT_PORT:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2) {
		data->passive = 1;
		state = SUCCESS;
	    } else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;

	  case SENT_TYPE:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    /* If OK, then tell the server to be passive */
	    if (status/100 == 2) {
		if (!HTFTP_send_cmd(ctrl, "PASV", NULL))
		    state = SENT_PASV;
		else
		    state = ERROR;
	    } else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;
	    		
	  case ERROR:			      /* Otherwise gcc complains :-( */
	  case FAILURE:
	  case SUCCESS:
	    break;
	}
    }
    return state;
}


#ifdef LISTEN
/*						   	  HTFTP_switch_to_port
**
**    	This function changes the current data connection from being PASV to
**	PORT. This is to handle servers that doesn't tell if they can handle
**	a PASV data connection before very late in the state diagram.
**
**	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
*/
PRIVATE int HTFTP_switch_to_port ARGS2(ftp_data_info *, data,
				       HTRequest *, 	req)
{
    enum _state {
	ERROR = -2,
	SUCCESS = 0
    } state = ERROR;
    int status;
    ftp_ctrl_info *ctrl = data->ctrl;

    if (data->passive) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP Switch.. We are already passive, so PORT won't help :-(\n");
	return state;
    }

    if (PROT_TRACE)
	fprintf(stderr, "FTP Switch.. Closing PASV data connection number %d, and try to reopen it on the fly using PORT\n",
		data->sockfd);
    if ((status = NETCLOSE(data->sockfd)) < 0) {
        HTErrorSysAdd(req, ERR_FATAL, NO, "NETCLOSE");
    } else
	data->sockfd = -1;	   /* Invalid socket */
    
    /* Now get new data connection using PORT */
    if (status >= 0 && get_listen_socket(data) >= 0 &&
	!HTFTP_send_cmd(ctrl, "PORT", this_addr)) {
	status = HTFTP_get_response(ctrl,&ctrl->reply);
	if (status/100 == 2) {
	    data->passive = 1;
	    state = SUCCESS;
	}
    }
    return state;
}
#endif /* LISTEN */


/*						   	  HTFTP_look_for_data
**
**    	This function puts up a select on the data connetcion and the control
**	connection in order to find out on which one data is transmitted.
**
**	Returns -1 on ERROR, 0 if data-connection, 1 if control connection.
*/
PRIVATE int HTFTP_look_for_data ARGS2(HTRequest *, 	request,
				      ftp_data_info *, 	data)
{
    int status = -1;
    fd_set read_socks;
    struct timeval max_wait;
    ftp_ctrl_info *ctrl = data->ctrl;
    int maxfdpl = HTMAX(data->sockfd, ctrl->sockfd) + 1;
    if (data->sockfd < 0 || ctrl->sockfd < 0) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP Select.. Invalid socket\n");
	return -1;
    }

    /* Initialize the set of sockets */
    FD_ZERO(&read_socks);
    FD_SET(data->sockfd, &read_socks);	      /* Turn on bit for data socket */
    FD_SET(ctrl->sockfd, &read_socks);	   /* Turn on bit for control socket */

    /* Set up timer */
    if (HTFTPTimeOut <= 0)
	HTFTPTimeOut = FTP_DEFAULT_TIMEOUT;
    max_wait.tv_sec = HTFTPTimeOut/100;
    max_wait.tv_usec = (HTFTPTimeOut%100)*10000;

    /* This sleep is necessary as data is usually arriving more slow on control
       connection than on data connection. Even on an error, the data socket 
       might indicate that data is ready, even though they are not :-( */
    sleep(1);

    /* Now make the select */
#ifdef __hpux
    if ((status = select(maxfdpl, (int *) &read_socks, (int *) NULL,
			 (int *) NULL, &max_wait)) < 0)
#else
    if ((status = select(maxfdpl, &read_socks, (fd_set *) NULL,
			 (fd_set *) NULL, &max_wait)) < 0)
#endif /* __hpux */
        HTErrorSysAdd(request, ERR_FATAL, NO, "select");
    else if (!status) {
	HTErrorAdd(request, ERR_FATAL, NO,
		   HTERR_FTP_NO_RESPONSE, NULL, 0, "HTFTP_look_for_data");
	status = -1;
    } else if (status > 1) {
	if (PROT_TRACE) fprintf(stderr, "FTP Select.. Both data connetion and control data connection has data, let's grab the control\n");
	status = 1;
    } else if (FD_ISSET(data->sockfd, &read_socks)) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP Select.. Data connection %d ready\n",
		    data->sockfd);
	status = 0;
    } else if (FD_ISSET(ctrl->sockfd, &read_socks)) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP Select.. Control connection %d ready\n",
		    ctrl->sockfd);
	status = 1;
    } else {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP Select.. Unknown socket returned\n");
	status = -1;
    }
    return status;
}


/*							     HTFTPServerInfo()
**
**	This function finds out what server we are talking to.
**	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
**
**	Thanks to James.W.Matthews@Dartmouth.EDU (James W. Matthews) for making
**	his code available.
*/
PRIVATE int HTFTPServerInfo ARGS1(ftp_ctrl_info *, ctrl)
{
    enum _state {
	ERROR  = -2,
	FAILURE = -1,
	SUCCESS = 0,
	BEGIN,
	SENT_SYST,
	NEED_PWD,
	SENT_PWD
    } state = BEGIN;
    int status;

    /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
    while (state > 0) {
	switch (state) {
	  case BEGIN:
	    if (!HTFTP_send_cmd(ctrl, "SYST", NULL))
		state = SENT_SYST;
	    else
		state = ERROR;
	    break;
	    
	  case SENT_SYST:
	    {
		char *info;
		status = HTFTP_get_response(ctrl, &ctrl->reply);
		if (status/100 != 2) {
		    state = NEED_PWD;
		    break;
		}

		/* Got a line - what kind of server are we talking to? */
		info = ctrl->reply->data+3;	       /* Skip response code */
		while (*info && *info++ == ' ');
		if (!*info) {
		    if (PROT_TRACE)
			fprintf(stderr, "FTP......... No server info?\n");
		    state = NEED_PWD;
		    break;
		}
		--info;
		if (strncmp(info, "UNIX Type: L8MAC-OSMachTen", 28) == 0) {
		    ctrl->server = MACHTEN_SERVER;
		    ctrl->use_list = YES;
		    ctrl->unsure_type = NO;
		} else if (strstr(info, "UNIX") != NULL) {
		    ctrl->server = UNIX_SERVER;
		    ctrl->use_list = YES;
		    ctrl->unsure_type = NO;
		} else if (strncmp(info, "VMS", 3) == 0) {
		    ctrl->server = VMS_SERVER;
		    ctrl->use_list = YES;
		    ctrl->unsure_type = NO;
		} else if ((strncmp(info, "VM/CMS", 6) == 0) ||
			   (strncmp(info, "VM", 2) == 0)) {
		    ctrl->server = CMS_SERVER;
		    ctrl->unsure_type = NO;
		} else if (strncmp(info, "DCTS", 4) == 0) {
		    ctrl->server = DCTS_SERVER;
		    ctrl->unsure_type = NO;
		} else if (strstr(info, "MAC-OS TCP/ConnectII") != NULL) {
		    ctrl->server = TCPC_SERVER;
		    /* Check old versions of TCP/C using / in pathnames */
		    ctrl->unsure_type = YES;
		} else if (strncmp(info, "MACOS Peter's Server", 20) == 0) {
		    ctrl->server = PETER_LEWIS_SERVER;
		    ctrl->use_list = YES;
		    ctrl->unsure_type = NO;
		} else if (strncmp(info, "Windows_NT", 10) == 0) {
		    ctrl->server = WINDOWS_NT;
		    ctrl->use_list = YES;
		    ctrl->unsure_type = NO;
		}
		
		/* If we are unsure, try PWD to get more information */
		if (ctrl->server == UNKNOWN || ctrl->unsure_type == YES)
		    state = NEED_PWD;
		else
		    state = SUCCESS;
	    }
	    break;

	  case NEED_PWD:
	    /* get the working directory (to see what it looks like) */
	    if (!HTFTP_send_cmd(ctrl, "PWD", NULL))
		state = SENT_PWD;
	    else
		state = ERROR;
	    break;

	  case SENT_PWD:
	    {
		status = HTFTP_get_response(ctrl, &ctrl->reply);
		if (status/100 != 2)
		    state = ERROR;
		else {
		    char *start, *end;

		    /* Now analyze response information between "'s */
		    if ((start = strchr(ctrl->reply->data, '"')) == NULL ||
			(end = strchr(++start, '"')) == NULL) {
			if (PROT_TRACE)
			    fprintf(stderr,
				    "FTP......... No current directory?\n");
			ctrl->server = GENERIC_SERVER;
		    } else {
			*end = '\0';
			if (ctrl->server == TCPC_SERVER) {
			    ctrl->server = *start == '/' ?
				NCSA_SERVER : TCPC_SERVER;
			    ctrl->unsure_type = NO;
			} else if (*start == '/') {
			    /* path names starting with / imply Unix, right? */
			    ctrl->server = UNIX_SERVER;
			    ctrl->use_list = YES;
			} else if (*(end-1) == ']') {
			    /* path names ending with ] imply VMS, right? */
			    ctrl->server = VMS_SERVER;
			} else
			    ctrl->server = GENERIC_SERVER;
		    }
		    state = SUCCESS;
		}
	    }		    
	    break;
	    
	  case FAILURE:			      /* Otherwise gcc complains :-( */
	  case ERROR:
	  case SUCCESS:
	    break;
	}
    }
    if (PROT_TRACE) {
	static char *servers[] = {
	    "UNKNOWN",
	    "GENERIC",
	    "MACHTEN",
	    "UNIX",
	    "VMS",
	    "CMS",
	    "DCTS",
	    "TCPC",
	    "PETER LEWIS",
	    "NCSA",
	    "WINDOWS NT"
	    };
	if (ctrl->unsure_type == YES)
	    fprintf(stderr, "FTP......... This might be a %s server\n",
		    *(servers+ctrl->server+1));
	else
	    fprintf(stderr, "FTP......... We are talking to a %s server\n",
		    *(servers+ctrl->server+1));
    }
    return state;
}


/*							     HTFTPLocation()
**
**	This function compares the current directory location in the
**	ftp-session to the new URL and returns the relative position. If
**	the new URL is at a higher location, the function performs CDUP's
**	until this location is reached so that the relative position is '.'
**
**	Returns relative path name if OK, else current location
**
*/
PRIVATE char *HTFTPLocation ARGS2(ftp_ctrl_info *, ctrl, char *, url)
{
    unsigned char getup = 0;
    char *current;
    char *new;
    char *relative;
    char *result = NULL;
    char *strptr;

    /* Make a full URL out of current location */ 
    current = HTParse(url, "", PARSE_ACCESS+PARSE_HOST+PARSE_PUNCTUATION);
    StrAllocCat(current, "/");
    StrAllocCat(current, ctrl->location);
    if (*(current+strlen(current)-1) != '/')
	StrAllocCat(current, "/");

    /* Make a temporary URL without any type indication */
    new = HTParse(url, "", PARSE_ALL);
    if ((strptr = strrchr(new, ';')) != NULL)
	*strptr = '\0';

    /* Compare those two URLs */
    relative = HTRelative(new, current);
    {
	/* Now send any CDUP if necessary */
	char *tail = relative;
	int status;
	while (tail && (strptr = strstr(tail, "../")) != NULL) {
	    if (HTFTP_send_cmd(ctrl, "CDUP", NULL)) {
		break;
	    }
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 != 2) {
		break;
	    }
	    ++getup;
	    tail += 3;
	}
    }

    /* Now update current location if we have used CDUP and make relative 
       return value. */
    if (getup) {
	char *location = HTParse(new, "", PARSE_PATH);
	free(ctrl->location);
	if (*location == '/')
	    ctrl->location = ++location;
	else
	    ctrl->location = location;
	if (*ctrl->location &&
	    *(ctrl->location+strlen(ctrl->location)-1) == '/')
	    *(ctrl->location+strlen(ctrl->location)-1) = '\0';
	StrAllocCopy(result, "");
	free(relative);
    } else {
	if (*relative == '/') {
	    StrAllocCopy(result, relative+1);
	    free(relative);
	} else
	    result = relative;
	if (*relative && *(relative+strlen(relative)-1) == '/')
	    *(relative+strlen(relative)-1) = '\0';
    }
    if (PROT_TRACE)
	fprintf(stderr, "FTP......... current location on server: `%s\'\n",
		ctrl->location);
    free(current);
    free(new);
    return result;
}


/* ------------------------------------------------------------------------- */
/*	              FTP Client Functions for receiving data     	     */
/* ------------------------------------------------------------------------- */

/*						   		HTFTP_get_dir
**
**    	This function asks for the directory specified and calls
**	HTFTPBrowseDirectory. First we try in one go, but if that doesn't
**	work, then we use CWD for each segment. The directory is searched
**	relative to the login directory, that is without a starting '/'.
**
**	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
*/
PRIVATE int HTFTP_get_dir ARGS3(ftp_ctrl_info *, ctrl, HTRequest *, req,
				char *, relative)
{
    enum _state {
	ERROR  = -2,
	FAILURE = -1,
	SUCCESS = 0,
	BEGIN,
	NEED_LIST,
	SENT_LIST,
	SENT_CWD,
        MULTIPLE_CWD,
	READY_FOR_DATA,
	SENT_ABOR,
	WAIT_FOR_CONNECTION,
	HANDLE_CTRL,
	GOT_DATA
    } state = BEGIN;
    BOOL handle_ctrl = NO;
    ftp_data_info *data = (ftp_data_info *) ctrl->data_cons->next->object; 
    int status;
    char *unescaped = NULL;
    StrAllocCopy(unescaped, relative);
    HTUnEscape(unescaped);
    HTCleanTelnetString(unescaped);	/* Prevent security holes */

    /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
    while (state > 0) {
	switch (state) {
	  case BEGIN:
	    /* Only if the root directory is requested, we can use LIST right
	       away, else we must first use CWD */
	    if (!*unescaped || !strcmp(unescaped, "/"))
		state = NEED_LIST;
	    else {
		/* We first try to CWD to the location in one go. */
		if (!HTFTP_send_cmd(ctrl, "CWD", unescaped))
		    state = SENT_CWD;
		else
		    state = ERROR;
	    }
	    break;

	  case NEED_LIST:
	    if (ctrl->use_list == YES) {
		if (!HTFTP_send_cmd(ctrl, "LIST", NULL))
		    state = SENT_LIST;
		else
		    state = ERROR;
	    } else {
		if (!HTFTP_send_cmd(ctrl, "NLST", NULL))
		    state = SENT_LIST;
		else
		    state = ERROR;
	    }
	    break;

	  case SENT_LIST:
#ifdef SEQUENT
	    
	    /* If we are listening, do a non-blocking accept now, as the
	       accept on some systems (DYNIX) completes the connection. On
	       BSD systems, the completion is done independently of the
	       accept. (thanks to Bill Rushka, wcr@aps.org) */
	    if (data->passive == 1) {
		int newfd;
		if ((newfd = HTDoAccept(req, data->sockfd)) >= 0) {
#ifdef REPEAT_LISTEN
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->sockfd);
#else
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->sockfd);
		    if (NETCLOSE(data->sockfd) < 0) {
			HTErrorSysAdd(data->request, ERR_FATAL, NO,"NETCLOSE");
			state = ERROR;
			break;
		    }
#endif
		    data->sockfd = newfd;        /* Switch to new socket */
		    data->passive = 2;
		    if (PROT_TRACE)
			fprintf(stderr, "FTP......... New data socket: %d\n",
				data->sockfd);
		} else {
		    HTChunkClear(ctrl->reply);
		    ctrl->reply = NULL;
		    state = ERROR;
		    break;
		}
	    }
#endif /* SEQUENT */

	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status == 150)			 /* About to open connection */
		state = WAIT_FOR_CONNECTION;
	    else if (status == 125)			/* Transfer starting */
		state = READY_FOR_DATA;
	    else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;
	    
	  case SENT_CWD:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2) {
		/* Update current location */
		if (*ctrl->location)
		    StrAllocCat(ctrl->location, "/");
		StrAllocCat(ctrl->location, relative);
		HTChunkClear(ctrl->welcome);
		HTFTPAddWelcome(ctrl);
		state = NEED_LIST;
	    } else if (status/100 == 4)
		state = FAILURE;
	    else
		state = MULTIPLE_CWD;
	    break;

	  case MULTIPLE_CWD:
	    /* We must use the escaped version when looking for '/' as
	       delimiter between segments, and then unescape each segment */
	    if (PROT_TRACE) fprintf(stderr, "FTP......... Can't jump directly to location, try multiple CD's instead\n");
	    state = NEED_LIST;		     /* This is overwritten if error */
	    {
		char *path = NULL;
		char *segment;
		StrAllocCopy(path, relative);
		segment = strtok(path, "/");
		while (segment && *segment) {
		    HTUnEscape(segment);
		    HTCleanTelnetString(segment);  /* Prevent security holes */
		    if (HTFTP_send_cmd(ctrl, "CWD", segment)) {
			state = ERROR;
			break;
		    }
		    status = HTFTP_get_response(ctrl, &ctrl->reply);
		    if (status/100 != 2) {
			if (status/100 == 4)
			    state = FAILURE;
			else
			    state = ERROR;
			break;
		    } else {			  /* Update current location */
			char *new_seg = HTEscape(segment, URL_XPALPHAS);
			if (*ctrl->location)
			    StrAllocCat(ctrl->location, "/");
			StrAllocCat(ctrl->location, new_seg);
			free(new_seg);
			HTChunkClear(ctrl->welcome);
			HTFTPAddWelcome(ctrl);
		    }
		    segment = strtok(NULL, "/");	   /* Get next token */
		}
		free(path);
	    }
	    break;

	  case READY_FOR_DATA:
	    if (data->passive == 1) {
		int newfd;
		if ((newfd = HTDoAccept((HTNetInfo *) data)) >= 0) {
#ifdef REPEAT_LISTEN
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->sockfd);
#else
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->sockfd);
		    if (NETCLOSE(data->sockfd) < 0) {
			HTErrorSysAdd(data->request, ERR_FATAL, NO,"NETCLOSE");
			state = ERROR;
			break;
		    }
#endif
		    data->sockfd = newfd;            /* Switch to new socket */
		    data->passive = 2;
		    if (PROT_TRACE)
			fprintf(stderr, "FTP......... New data socket: %d\n",
				data->sockfd);
		} else {
		    HTChunkClear(ctrl->reply);
		    ctrl->reply = NULL;
		    state = ERROR;
		    break;
		}
	    }

	    /* Now, the browsing module can be called */
	    {
		char *url = HTAnchor_physical(req->anchor);
		char *path = HTParse(url, "", PARSE_PATH+PARSE_PUNCTUATION);
		HTUnEscape(path);
		if (PROT_TRACE)
		    fprintf(stderr, "FTP......... Receiving directory `%s\'\n",
			    path);
		data->isoc = HTInputSocket_new(data->sockfd);
		status = HTFTPBrowseDirectory(req, path, HTFTP_get_dir_string);
		HTInputSocket_free(data->isoc);
		if (status == -1)
		    state = ERROR;
		else if (status == HT_INTERRUPTED) {
		    if (!HTFTP_send_cmd(ctrl, "ABOR", NULL))
			state = SENT_ABOR;
		    else
			state = ERROR;
		} else
		    state = GOT_DATA;
		free(path);
	    }
	    break;

	  case GOT_DATA:
	    if (!handle_ctrl) {
		status = HTFTP_get_response(ctrl, &ctrl->reply);
		if (status/100 == 2)
		    state = SUCCESS;       	       	   /* Directory read */
		else if (status/100 == 4)
		    state = FAILURE;
		else
		    state = ERROR;
	    } else
		state = SUCCESS;
	    break;

	  case SENT_ABOR:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2)
		state = SUCCESS;
	    else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;

	  case WAIT_FOR_CONNECTION:
	    /* Now we have to wait to see whether the FTP-server sends on the
	       data port or the control port */
	    status = HTFTP_look_for_data(req, data);
	    if (!status)
		state = READY_FOR_DATA;
	    else if (status == 1)
		state = HANDLE_CTRL;
	    else
		state = ERROR;
	    break;

	  case HANDLE_CTRL:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2) {
		state = READY_FOR_DATA;
#ifdef LISTEN
	    } else if (status == 425) {	   /* Connection could not be opened */
		if (HTFTP_switch_to_port(data, req))
		    state = ERROR;
		else
		    state = NEED_LIST;
#endif /* LISTEN */
	    } else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    handle_ctrl = YES;
	    break;

	  case FAILURE:			      /* Otherwise gcc complains :-( */
	  case ERROR:
	  case SUCCESS:
	    break;
	}
    }
    FREE(unescaped);
    return state;
}


/*						   		HTFTP_get_file
**
**    	This function asks for the file specified. First we try in one go,
**	but if that doesn't work, then we use CWD for each segment and then
**	try to retrieve it. If that also fails, then we try if it is a
**	directory. This procedure causes that directory links generated in
**	HTFTPBrowseDirectory should have a '/' at the end in order to go
**	directly to HTFTP_get_dir. The relative is searched relative to
**	the login directory, that is without a starting '/'.
**
**	Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
*/
PRIVATE int HTFTP_get_file ARGS3(ftp_ctrl_info *, ctrl, HTRequest *, req,
				 char *, relative)
{
    enum _state {
	ERROR  = -2,
	FAILURE = -1,
	SUCCESS = 0,
	BEGIN,
	SENT_RETR,
        MULTIPLE_CWD,
	READY_FOR_DATA,
	SENT_ABOR,
	WAIT_FOR_CONNECTION,
	HANDLE_CTRL,
	GOT_DATA
    } state = BEGIN;
    BOOL handle_ctrl = NO;
    BOOL multiple = NO;		      /* Have we already tried multiple CWD? */
    ftp_data_info *data = (ftp_data_info *) ctrl->data_cons->next->object; 
    int status;
    char *unescaped = NULL;
    StrAllocCopy(unescaped, relative);
    HTUnEscape(unescaped);
    HTCleanTelnetString(unescaped);  /* Prevent security holes */

    /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
    while (state > 0) {
	switch (state) {
	  case BEGIN:
	    /* First we try to retrieve the file in one go. */
	    if (!HTFTP_send_cmd(ctrl, "RETR", unescaped))
		state = SENT_RETR;
	    else
		state = ERROR;
	    break;

	  case SENT_RETR:
#ifdef SEQUENT	    
	    /* If we are listening, do a non-blocking accept now, as the
	       accept on some systems (DYNIX) completes the connection. On
	       BSD systems, the completion is done independently of the
	       accept. (thanks to Bill Rushka, wcr@aps.org) */
	    if (data->passive == 1) {
		int newfd;
		if ((newfd = HTDoAccept(req, data->sockfd)) >= 0) {
#ifdef REPEAT_LISTEN
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->sockfd);
#else
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->sockfd);
		    if (NETCLOSE(data->sockfd) < 0) {
			HTErrorSysAdd(data->request, ERR_FATAL, NO,"NETCLOSE");
			state = ERROR;
			break;
		    }
#endif
		    data->sockfd = newfd;            /* Switch to new socket */
		    data->passive = 2;
		    if (PROT_TRACE)
			fprintf(stderr, "FTP......... New data socket: %d\n",
				data->sockfd);
		} else {
		    HTChunkClear(ctrl->reply);
		    ctrl->reply = NULL;
		    state = ERROR;
		    break;
		}
	    }
#endif /* SEQUENT */

	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status == 150)			 /* About to open connection */
		state = WAIT_FOR_CONNECTION;
	    else if (status == 125)
		state = READY_FOR_DATA;			/* Transfer starting */
	    else if (status/100 == 4)
		state = FAILURE;

	    /* If there is no '/' in unescaped, it won't help to try
	       multiple CWD's, as it either doesn't exist or is a directory */
	    else if (multiple == NO && strchr(unescaped, '/') != NULL)
		state = MULTIPLE_CWD;
	    else {
		data->directory = YES;
		state = FAILURE;
	    }
	    break;
	    
	  case MULTIPLE_CWD:
	    /* We must use the escaped version when looking for '/' as
	       delimiter between segments, and then unescape each segment */
	    if (PROT_TRACE) fprintf(stderr, "FTP......... Can't jump directly to location, try multiple CD's instead\n");
	    multiple = YES;
	    {
		char *path = NULL;
		char *segment;
		char *last_slash;	/* Used to identify the last segment */
		StrAllocCopy(path, relative);
		if ((last_slash = strrchr(path, '/')) == NULL)
		    last_slash = path;
		else
		    last_slash++;
		segment = strtok(path, "/");
		while (segment && *segment && segment != last_slash) {
		    HTUnEscape(segment);
		    HTCleanTelnetString(segment);  /* Prevent security holes */
		    if (HTFTP_send_cmd(ctrl, "CWD", segment)) {
			state = ERROR;
			break;
		    }
		    status = HTFTP_get_response(ctrl, &ctrl->reply);
		    if (status/100 != 2) {
			if (status/100 == 4)
			    state = FAILURE;
			else
			    state = ERROR;
			break;
		    } else {			  /* Update current location */
			char *new_seg = HTEscape(segment, URL_XPALPHAS);
			if (*ctrl->location)
			    StrAllocCat(ctrl->location, "/");
			StrAllocCat(ctrl->location, new_seg);
			free(new_seg);
		    }
		    segment = strtok(NULL, "/");	   /* Get next token */
		}
		/* Now try to retrieve the last segment */
		if (segment == last_slash) {
		    HTUnEscape(segment);
		    HTCleanTelnetString(segment);  /* Prevent security holes */
		    if (!HTFTP_send_cmd(ctrl, "RETR", segment)) {
			StrAllocCopy(unescaped, segment);
			state = SENT_RETR;
		    } else
			state = ERROR;
		} else {
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Strange error, filename not found?\n");
		    state = ERROR;
		}
		free(path);
	    }
	    break;

	  case READY_FOR_DATA:
	    if (data->passive == 1) {
		int newfd;
		if ((newfd = HTDoAccept((HTNetInfo *) data)) >= 0) {
#ifdef REPEAT_LISTEN
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->sockfd);
#else
		    if (PROT_TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->sockfd);
		    if (NETCLOSE(data->sockfd) < 0) {
			HTErrorSysAdd(data->request, ERR_FATAL, NO,"NETCLOSE");
			state = ERROR;
			break;
		    }
#endif
		    data->sockfd = newfd;        /* Switch to new socket */
		    data->passive = 2;
		    if (PROT_TRACE)
			fprintf(stderr, "FTP......... New data socket: %d\n",
				data->sockfd);
		} else {
		    HTChunkClear(ctrl->reply);
		    ctrl->reply = NULL;
		    state = ERROR;
		    break;
		}
	    }

	    /* Now, the net parse module can be called */
	    if (PROT_TRACE) fprintf(stderr, "FTP......... Receiving file `%s\'\n",
			       unescaped);
	    status = HTParseSocket(data->fileformat, data->sockfd, req);
	    if (status != HT_LOADED) {
		if (status == HT_INTERRUPTED) {
		    if (!HTFTP_send_cmd(ctrl, "ABOR", NULL))
			state = SENT_ABOR;
		    else
			state = ERROR;
		} else
		    state = ERROR;
	    } else
		state = GOT_DATA;
	    break;

	  case GOT_DATA:
	    if (!handle_ctrl) {
		status = HTFTP_get_response(ctrl, &ctrl->reply);
		if (status/100 == 2)
		    state = SUCCESS;   			   	/* File read */
		else if (status/100 == 4)
		    state = FAILURE;
		else
		    state = ERROR;
	    } else
		state = SUCCESS;
	    break;

	  case SENT_ABOR:
	    status = HTFTP_get_response(ctrl, &ctrl->reply);
	    if (status/100 == 2)
		state = SUCCESS;
	    else if (status/100 == 4)
		state = FAILURE;
	    else
		state = ERROR;
	    break;

          case WAIT_FOR_CONNECTION:
            /* Now we have to wait to see whether the FTP-server sends on the
               data port or the control port */
            status = HTFTP_look_for_data(req, data);
            if (!status)
                state = READY_FOR_DATA;
            else if (status == 1)
                state = HANDLE_CTRL;
	    else
		state = ERROR;
            break;

          case HANDLE_CTRL:
            status = HTFTP_get_response(ctrl, &ctrl->reply);
            if (status/100 == 2) {
                state = READY_FOR_DATA;
#ifdef LISTEN
            } else if (status == 425) {    /* Connection could not be opened */
                if (HTFTP_switch_to_port(data, req))
                    state = ERROR;
                else {
		    if (!HTFTP_send_cmd(ctrl, "RETR", unescaped))
			state = SENT_RETR;
		    else
			state = ERROR;
		}
#endif /* LISTEN */
            } else if (status/100 == 4)
                state = FAILURE;
            else
                state = ERROR;
            handle_ctrl = YES;
            break;

	  case FAILURE:			      /* Otherwise gcc complains :-( */
	  case ERROR:
	  case SUCCESS:
	    break;
	}
    }
    FREE(unescaped);
    return state;
}


/* ------------------------------------------------------------------------- */
/*			       PUBLIC FTP functions			     */
/* ------------------------------------------------------------------------- */

/*						   	   HTFTPWelcomeMsg
**
**    	Returns the welcome message from the login sequence
*/
PUBLIC HTChunk *HTFTPWelcomeMsg ARGS1(HTNetInfo *, data)
{
    return ((ftp_data_info *) data)->ctrl->welcome;
}


/*						   	   HTFTPUseList
**
**    	Can we use long listings in HTDirBrw.c?
*/
PUBLIC BOOL HTFTUseList ARGS1(HTNetInfo *, data)
{
    return ((ftp_data_info *) data)->ctrl->use_list;
}


/*						   	   HTFTP_enable_session
**
**    	This function makes it possible to reuse the same control connections
**	until they are either timed out by the server, or that the session
**	is closed by HTFTP_end_session. Note that HTLoadFTP can run
**	independently of start and end session, and then each load runs like
**	an atomic action.
*/
PUBLIC void HTFTP_enable_session NOARGS
{
    if (session) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP......... Session is already enabled?\n");
	return;
    }
    session = HTList_new();
}


/*						   	  HTFTP_disable_session
**
**    	This function is the closing function for HTFTP_enable_session.
**
**	Returns YES if OK, else NO
*/
PUBLIC BOOL HTFTP_disable_session NOARGS
{
    BOOL status = YES;
    if (!session) {
	if (PROT_TRACE)
	    fprintf(stderr, "FTP......... No session to disable?\n");
	return NO;
    }
    {
	HTList *cur = session;
	ftp_ctrl_info *pres;
	while ((pres = (ftp_ctrl_info *) HTList_nextObject(cur))) {
	    if (HTFTP_close_ctrl_con(pres))
		status = NO;
	}
	HTList_delete(session);
    }
    session = NULL;
    return status;
}


/*	Retrieve File from Server as an atomic action. 
**	-----------------------------------------------
**
** On entry,
**	request		This is the request structure
** On exit,
**	returns		<0		Error has occured
**			HT_LOADED	OK
*/
PUBLIC int HTLoadFTP ARGS1(HTRequest *, request)
{
    char *url;
    int status = -1;
    int retry;		       			    /* How many times tried? */
    ftp_ctrl_info *ctrl;

    if (!request || !request->anchor) {
	if (PROT_TRACE) fprintf(stderr, "HTLoadFTP... Bad argument\n");
	return -1;
    }
    url = HTAnchor_physical(request->anchor);
    if (PROT_TRACE) fprintf(stderr, "FTP......... Looking for `%s\'\n", url);

    /* Initiate a (possibly already exsisting) control connection and a
       corresponding data connection */
    if((ctrl = HTFTP_init_con(request, url)) == NULL) {
	goto endfunc;
    }

    /* Only if the control connection is in IDLE state, a new
       transfer can be started. The control connection can be in another
       mode if (session), and then the request is getting queued in
       ctrl->data_cons. */
    if (ctrl->state == FTP_IDLE || (session && ctrl->state == FTP_LOGGED_IN)) {
	ftp_data_info *data = ctrl->data_cons->next->object;
	if (ctrl->state == FTP_IDLE)
	    ctrl->state = FTP_BEGIN;
	while (ctrl->state != FTP_IDLE) {	       	/* Do until finished */
	    switch (ctrl->state) {
	      case FTP_BEGIN:
		if (!HTFTP_login(ctrl))
		    ctrl->state = FTP_LOGGED_IN;
		else
		    ctrl->state = FTP_ERROR;
		break;

	      case FTP_LOGGED_IN:
		if (!HTFTP_get_data_con(request, data, url))
		    ctrl->state = FTP_GOT_DATA_CON;
		else
		    ctrl->state = FTP_ERROR;
		break;

	      case FTP_GOT_DATA_CON:
		{
		    /* Now we must ask for the URL requested. If FAILURE, then
		       we try twice to see, if it helps */
		    char *rel = NULL;
		    for (retry=0; retry<2; retry++) {
			if ((rel = HTFTPLocation(ctrl, url)) == NULL) {
			    ctrl->state = FTP_ERROR;
			    break;
			}
			if (retry == 1 && TRACE)
			    fprintf(stderr,
				    "FTP......... First attempt to get URL failed, let's try again\n");

			if (data->directory == YES) {
			    /* If we haven't already got server-info */
			    if (ctrl->server == UNKNOWN) {
				if (HTFTPServerInfo(ctrl)) {
				    ctrl->state = FTP_ERROR;
				    break;
				}
			    }
			    status = HTFTP_get_dir(ctrl, request, rel);
			}
			else
			    status = HTFTP_get_file(ctrl, request, rel);
			if (!status) {
			    ctrl->state = FTP_GOT_DATA;
			    break;
			} else if (status == -2) {		    /* Error */
			    ctrl->state = FTP_ERROR;
			    break;
			} else {
			    FREE(rel);
			    ctrl->state = FTP_FAILURE;		/* Try twice */
			}
		    }
		    FREE(rel);
		}
		if (retry == 2 && ctrl->state == FTP_FAILURE)
		    ctrl->state = FTP_ERROR;
		break;

	      case FTP_GOT_DATA:
		if (HTFTP_close_data_con(data))
		    ctrl->state = FTP_ERROR;
		else {
		    HTList_removeLastObject(ctrl->data_cons);
		    if (!session) {
			if (!HTFTP_logout(ctrl)) {
			    ctrl->state = FTP_IDLE;
			    status = HT_LOADED;
			} else
			    ctrl->state = FTP_ERROR;
		    } else {
			ctrl->state = FTP_LOGGED_IN; /*Ready for next request*/
			return HT_LOADED;
		    }
		    break;
		}
		break;

	      case FTP_ERROR:
		{
		    if (ctrl->reply && ctrl->reply->data) {
			HTFTPParseError(&ctrl->reply);
			HTErrorAdd(request, ERR_FATAL, NO, HTERR_FTP_SERVER,
				   (void *) ctrl->reply->data,
				   ctrl->reply->size-1, "HTLoadFTP");
		    } else {
			char *hoststr = HTParse(url, "", PARSE_HOST);
			HTUnEscape(hoststr);
			HTErrorAdd(request, ERR_FATAL, NO,
				   HTERR_FTP_NO_RESPONSE,
				   (void *) hoststr, strlen(hoststr),
				   "HTLoadFTP");
			free(hoststr);
		    }
		    HTFTP_abort_ctrl_con(ctrl);
		    status = -1;
		    goto endfunc;
		}
		break;

	      default:
		if (PROT_TRACE)
		    fprintf(stderr, "FTP......... Unknown state, what happened?\n");
		break;
	    }
	}

	/* The control connection is only closed if the load is atomic */
	if (!session && HTFTP_close_ctrl_con(ctrl))
	    status = -1;
    }

  endfunc:
    if (status < 0 && status != HT_INTERRUPTED) {
	char *unescaped = NULL;
	StrAllocCopy(unescaped, url);
	HTUnEscape(unescaped);
	HTErrorAdd(request, ERR_FATAL, NO, HTERR_INTERNAL, (void *) unescaped,
		   (int) strlen(unescaped), "HTLoadFTP");
	free(unescaped);
    }
    return status;
}

/* Protocol descriptors */
GLOBALDEF PUBLIC HTProtocol HTFTP  = { "ftp", HTLoadFTP, 0 , 0 };

/* END OF MODULE */