about summary refs log tree commit diff stats
path: root/WWW/Library/Implementation/HTTCP.c
blob: f99397a6d2fff9a66adf4617dd0146e64a9d4fa3 (plain) (blame)
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
/*			Generic Communication Code		HTTCP.c
 *			==========================
 *
 *	This code is in common between client and server sides.
 *
 *	16 Jan 92  TBL	Fix strtol() undefined on CMU Mach.
 *	25 Jun 92  JFG	Added DECNET option through TCP socket emulation.
 *	13 Sep 93  MD	Added correct return of vmserrorno for HTInetStatus.
 *			Added decoding of vms error message for MULTINET.
 *	7-DEC-1993 Bjorn S. Nilsson, ALEPH, CERN, VMS UCX ioctl() changes
 *			(done of Mosaic)
 *	19 Feb 94  Danny Mayer	Added Bjorn Fixes to Lynx version
 *	 7 Mar 94  Danny Mayer	Added Fix UCX version for full domain name
 *	20 May 94  Andy Harper	Added support for CMU TCP/IP transport
 *	17 Nov 94  Andy Harper	Added support for SOCKETSHR transport
 *	16 Jul 95  S. Bjorndahl added kluge to deal with LIBCMU bug
 */

#include <HTUtils.h>
#include <HTParse.h>
#include <HTAlert.h>
#include <HTTCP.h>
#include <LYGlobalDefs.h>	/* added for no_suspend */
#include <LYUtils.h>

#ifdef NSL_FORK
#include <signal.h>
#include <www_wait.h>
#endif /* NSL_FORK */

#ifdef HAVE_RESOLV_H
#include <resolv.h>
#endif

#ifdef __DJGPP__
#include <netdb.h>
#endif /* __DJGPP__ */

#define LYNX_ADDRINFO	struct addrinfo
#define LYNX_HOSTENT	struct hostent

#define OK_HOST(p) ((p) != 0 && ((p)->h_length) != 0)

#ifdef SVR4_BSDSELECT
int BSDselect(int nfds,
	      fd_set * readfds,
	      fd_set * writefds,
	      fd_set * exceptfds,
	      struct timeval *select_timeout);

#ifdef select
#undef select
#endif /* select */
#define select BSDselect
#ifdef SOCKS
#ifdef Rselect
#undef Rselect
#endif /* Rselect */
#define Rselect BSDselect
#endif /* SOCKS */
#endif /* SVR4_BSDSELECT */

#include <LYLeaks.h>

/*
 *  Module-Wide variables
 */
static char *hostname = NULL;	/* The name of this host */

/*
 *  PUBLIC VARIABLES
 */
#ifdef SOCKS
unsigned long socks_bind_remoteAddr;	/* for long Rbind */
#endif /* SOCKS */

/* PUBLIC SockA HTHostAddress; *//* The internet address of the host */
					/* Valid after call to HTHostName() */

/*	Encode INET status (as in sys/errno.h)			  inet_status()
 *	------------------
 *
 *  On entry,
 *	where		gives a description of what caused the error
 *	global errno	gives the error number in the Unix way.
 *
 *  On return,
 *	returns		a negative status in the Unix way.
 */

#ifdef DECL_SYS_ERRLIST
extern char *sys_errlist[];	/* see man perror on cernvax */
extern int sys_nerr;
#endif /* DECL_SYS_ERRLIST */

#ifdef __DJGPP__
static int ResolveYield(void)
{
    return HTCheckForInterrupt()? 0 : 1;
}
#endif

#if defined(VMS) && defined(UCX)
/*
 *  A routine to mimic the ioctl function for UCX.
 *  Bjorn S. Nilsson, 25-Nov-1993. Based on an example in the UCX manual.
 */
#include <HTioctl.h>

int HTioctl(int d,
	    int request,
	    int *argp)
{
    int sdc, status;
    unsigned short fun, iosb[4];
    char *p5, *p6;
    struct comm {
	int command;
	char *addr;
    } ioctl_comm;
    struct it2 {
	unsigned short len;
	unsigned short opt;
	struct comm *addr;
    } ioctl_desc;

    if ((sdc = vaxc$get_sdc(d)) == 0) {
	set_errno(EBADF);
	return -1;
    }
    ioctl_desc.opt = UCX$C_IOCTL;
    ioctl_desc.len = sizeof(struct comm);

    ioctl_desc.addr = &ioctl_comm;
    if (request & IOC_OUT) {
	fun = IO$_SENSEMODE;
	p5 = 0;
	p6 = (char *) &ioctl_desc;
    } else {
	fun = IO$_SETMODE;
	p5 = (char *) &ioctl_desc;
	p6 = 0;
    }
    ioctl_comm.command = request;
    ioctl_comm.addr = (char *) argp;
    status = sys$qiow(0, sdc, fun, iosb, 0, 0, 0, 0, 0, 0, p5, p6);
    if (!(status & 01)) {
	set_errno(status);
	return -1;
    }
    if (!(iosb[0] & 01)) {
	set_errno(iosb[0]);
	return -1;
    }
    return 0;
}
#endif /* VMS && UCX */

#define MY_FORMAT "TCP: Error %d in `SOCKET_ERRNO' after call to %s() failed.\n\t%s\n"
	   /* third arg is transport/platform specific */

/*	Report Internet Error
 *	---------------------
 */
int HTInetStatus(const char *where)
{
    int status;
    int saved_errno = errno;

#ifdef VMS
#ifdef MULTINET
    SOCKET_ERRNO = vmserrno;
#endif /* MULTINET */
#endif /* VMS */

#ifdef VM
    CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where,
	    "(Error number not translated)"));	/* What Is the VM equiv? */
#define ER_NO_TRANS_DONE
#endif /* VM */

#ifdef VMS
#ifdef MULTINET
    CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where,
	    vms_errno_string()));
#else
    CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where,
	    ((SOCKET_ERRNO > 0 && SOCKET_ERRNO <= 65) ?
	     strerror(SOCKET_ERRNO) : "(Error number not translated)")));
#endif /* MULTINET */
#define ER_NO_TRANS_DONE
#endif /* VMS */

#ifdef HAVE_STRERROR
    CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where,
	    strerror(SOCKET_ERRNO)));
#define ER_NO_TRANS_DONE
#endif /* HAVE_STRERROR */

#ifndef ER_NO_TRANS_DONE
    CTRACE((tfp, MY_FORMAT, SOCKET_ERRNO, where,
	    (SOCKET_ERRNO < sys_nerr ?
	     sys_errlist[SOCKET_ERRNO] : "Unknown error")));
#endif /* !ER_NO_TRANS_DONE */

#ifdef VMS
#ifndef MULTINET
    CTRACE((tfp,
	    "         Unix error number (SOCKET_ERRNO) = %ld dec\n",
	    SOCKET_ERRNO));
    CTRACE((tfp,
	    "         VMS error (vaxc$errno)    = %lx hex\n",
	    vaxc$errno));
#endif /* MULTINET */
#endif /* VMS */

    set_errno(saved_errno);

#ifdef VMS
    /*
     * uerrno and errno happen to be zero if vmserrno <> 0
     */
#ifdef MULTINET
    status = -vmserrno;
#else
    status = -vaxc$errno;
#endif /* MULTINET */
#else
    status = -SOCKET_ERRNO;
#endif /* VMS */
    return status;
}

/*	Parse a cardinal value				       parse_cardinal()
 *	----------------------
 *
 * On entry,
 *	*pp	    points to first character to be interpreted, terminated by
 *		    non 0:9 character.
 *	*pstatus    points to status already valid
 *	maxvalue    gives the largest allowable value.
 *
 * On exit,
 *	*pp	    points to first unread character
 *	*pstatus    points to status updated iff bad
 */
unsigned int HTCardinal(int *pstatus,
			char **pp,
			unsigned int max_value)
{
    unsigned int n;

    if ((**pp < '0') || (**pp > '9')) {		/* Null string is error */
	*pstatus = -3;		/* No number where one expected */
	return 0;
    }

    n = 0;
    while ((**pp >= '0') && (**pp <= '9'))
	n = n * 10 + *((*pp)++) - '0';

    if (n > max_value) {
	*pstatus = -4;		/* Cardinal outside range */
	return 0;
    }

    return n;
}

#ifndef DECNET			/* Function only used below for a trace message */
/*	Produce a string for an Internet address
 *	----------------------------------------
 *
 *  On exit,
 *	returns a pointer to a static string which must be copied if
 *		it is to be kept.
 */
const char *HTInetString(SockA * soc_in)
{
#ifdef INET6
    static char hostbuf[MAXHOSTNAMELEN];

    getnameinfo((struct sockaddr *) soc_in,
		SOCKADDR_LEN(soc_in),
		hostbuf, sizeof(hostbuf), NULL, 0, NI_NUMERICHOST);
    return hostbuf;
#else
    static char string[20];

    sprintf(string, "%d.%d.%d.%d",
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 0),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 1),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 2),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 3));
    return string;
#endif /* INET6 */
}
#endif /* !DECNET */

/*	Check whether string is a valid Internet hostname - kw
 *	-------------------------------------------------
 *
 *  Checks whether
 *  - contains only valid chars for domain names (actually, the
 *    restrictions are somewhat relaxed),
 *  - no leading dots or empty segments,
 *  - no segment starts with '-' or '+' [this protects telnet command],
 *  - max. length of dot-separated segment <= 63 (RFC 1034,1035),
 *  - total length <= 254 (if it ends with dot) or 253 (otherwise)
 *     [an interpretation of RFC 1034,1035, although RFC 1123
 *      suggests 255 as limit - kw].
 *
 *  Note: user (before '@') and port (after ':') components from
 *      host part of URL should be already stripped (if appropriate)
 *      from the input string.
 *
 *  On exit,
 *	returns 1 if valid, otherwise 0.
 */
BOOL valid_hostname(char *name)
{
    int i = 1, iseg = 0;
    char *cp = name;

    if (!(name && *name))
	return NO;
    for (; (*cp && i <= 253); cp++, i++) {
	if (*cp == '.') {
	    if (iseg == 0) {
		return NO;
	    } else {
		iseg = 0;
		continue;
	    }
	} else if (iseg == 0 && (*cp == '-' || *cp == '+')) {
	    return NO;
	} else if (++iseg > 63) {
	    return NO;
	}
	if (!isalnum(UCH(*cp)) &&
	    *cp != '-' && *cp != '_' &&
	    *cp != '$' && *cp != '+') {
	    return NO;
	}
    }
    return (BOOL) (*cp == '\0' || (*cp == '.' && iseg != 0 && cp[1] == '\0'));
}

#ifdef NSL_FORK
/*
 *  Function to allow us to be killed with a normal signal (not
 *  SIGKILL), but don't go through normal libc exit() processing, which
 *  would screw up parent's stdio.  -BL
 */
static void quench(int sig GCC_UNUSED)
{
    _exit(2);
}
#endif /* NSL_FORK */

int lynx_nsl_status = HT_OK;

#define DEBUG_HOSTENT		/* disable in case of problems */
#define DEBUG_HOSTENT_CHILD	/* for NSL_FORK, may screw up trace file */

/*
 *  Two auxiliary functions for name lookup and LYNX_HOSTENT.
 *
 *  dump_hostent - dumps the contents of a LYNX_HOSTENT to the
 *  trace log or stderr, including all pointer values, strings, and
 *  addresses, in a format inspired by gdb's print format. - kw
 */
static void dump_hostent(const char *msgprefix,
			 const LYNX_HOSTENT *phost)
{
    if (TRACE) {
	int i;
	char **pcnt;

	CTRACE((tfp, "%s: %p ", msgprefix, phost));
	if (phost) {
	    CTRACE((tfp, "{ h_name = %p", phost->h_name));
	    if (phost->h_name) {
		CTRACE((tfp, " \"%s\",", phost->h_name));
	    } else {
		CTRACE((tfp, ","));
	    }
	    CTRACE((tfp, "\n\t h_aliases = %p", phost->h_aliases));
	    if (phost->h_aliases) {
		CTRACE((tfp, " {"));
		for (pcnt = phost->h_aliases; *pcnt; pcnt++) {
		    CTRACE((tfp, "%s %p \"%s\"",
			    (pcnt == phost->h_aliases ? " " : ", "),
			    *pcnt, *pcnt));
		}
		CTRACE((tfp, "%s0x0 },\n\t",
			(*phost->h_aliases ? ", " : " ")));
	    } else {
		CTRACE((tfp, ",\n\t"));
	    }
	    CTRACE((tfp, " h_addrtype = %d,", phost->h_addrtype));
	    CTRACE((tfp, " h_length = %d,\n\t", phost->h_length));
	    CTRACE((tfp, " h_addr_list = %p", phost->h_addr_list));
	    if (phost->h_addr_list) {
		CTRACE((tfp, " {"));
		for (pcnt = phost->h_addr_list; *pcnt; pcnt++) {
		    CTRACE((tfp, "%s %p",
			    (pcnt == phost->h_addr_list ? "" : ","),
			    *pcnt));
		    for (i = 0; i < phost->h_length; i++) {
			CTRACE((tfp, "%s%d%s", (i == 0 ? " \"" : "."),
				(int) *((unsigned char *) (*pcnt) + i),
				(i + 1 == phost->h_length ? "\"" : "")));
		    }
		}
		if (*phost->h_addr_list) {
		    CTRACE((tfp, ", 0x0 } }"));
		} else {
		    CTRACE((tfp, " 0x0 } }"));
		}
	    } else {
		CTRACE((tfp, "}"));
	    }
	}
	CTRACE((tfp, "\n"));
	fflush(tfp);
    }
}

/*
 *  fill_rehostent - copies as much as possible relevant content from
 *  the LYNX_HOSTENT pointed to by phost to the char buffer given
 *  by rehostent, subject to maximum output length rehostentsize,
 *  following pointers and building self-contained output which can be
 *  cast to a LYNX_HOSTENT. - kw
 *  See also description of LYGetHostByName.
 */
#if defined(NSL_FORK) || defined(_WINDOWS_NSL)

#define REHOSTENT_SIZE 128	/* not bigger than pipe buffer! */

typedef struct {
    LYNX_HOSTENT h;
    char rest[REHOSTENT_SIZE];
} AlignedHOSTENT;

static size_t fill_rehostent(char *rehostent,
			     size_t rehostentsize,
			     const LYNX_HOSTENT *phost)
{
    AlignedHOSTENT *data = (AlignedHOSTENT *) rehostent;
    int num_addrs = 0;
    int num_aliases = 0;
    char **pcnt;
    char *p_next_char;
    char **p_next_charptr;
    size_t name_len = 0;
    size_t required_per_addr;
    size_t curlen = sizeof(LYNX_HOSTENT);
    size_t available = rehostentsize - curlen;
    size_t chk_available, mem_this_alias, required_this_alias;
    int i_addr, i_alias;

    if (!phost)
	return 0;
    required_per_addr = phost->h_length + sizeof(char *);

    if (phost->h_addr_list)
	available -= sizeof(phost->h_addr_list[0]);
    if (phost->h_aliases)
	available -= sizeof(phost->h_aliases[0]);
    if (phost->h_name)
	available--;
    if (phost->h_addr_list) {
	if (phost->h_addr_list[0]) {
	    if (available >= required_per_addr) {
		num_addrs++;
		available -= required_per_addr;
	    }
	}
    }
    if (phost->h_name) {
	name_len = strlen(phost->h_name);
	if (available >= name_len) {
	    available -= name_len;
	} else {
	    name_len = 0;
	}
    }
    if (num_addrs) {
	for (pcnt = phost->h_addr_list + 1; *pcnt; pcnt++) {
	    if (available >= required_per_addr) {
		num_addrs++;
		available -= required_per_addr;
	    } else {
		break;
	    }
	}
    }
    chk_available = available;
    if (phost->h_aliases) {
	for (pcnt = phost->h_aliases; *pcnt; pcnt++) {
	    required_this_alias = sizeof(phost->h_aliases[0]) +
		strlen(*pcnt) + 1;
	    if (chk_available >= required_this_alias) {
		num_aliases++;
		chk_available -= required_this_alias;
	    }
	}
    }

    data->h.h_addrtype = phost->h_addrtype;
    data->h.h_length = phost->h_length;
    p_next_charptr = (char **) (rehostent + curlen);
    p_next_char = rehostent + curlen;
    if (phost->h_addr_list)
	p_next_char += (num_addrs + 1) * sizeof(phost->h_addr_list[0]);
    if (phost->h_aliases)
	p_next_char += (num_aliases + 1) * sizeof(phost->h_aliases[0]);

    if (phost->h_addr_list) {
	data->h.h_addr_list = p_next_charptr;
	for (pcnt = phost->h_addr_list, i_addr = 0;
	     i_addr < num_addrs;
	     pcnt++, i_addr++) {
	    memcpy(p_next_char, *pcnt, sizeof(phost->h_addr_list[0]));
	    *p_next_charptr++ = p_next_char;
	    p_next_char += sizeof(phost->h_addr_list[0]);
	}
	*p_next_charptr++ = NULL;
    } else {
	data->h.h_addr_list = NULL;
    }

    if (phost->h_name) {
	data->h.h_name = p_next_char;
	if (name_len) {
	    strcpy(p_next_char, phost->h_name);
	    p_next_char += name_len + 1;
	} else {
	    *p_next_char++ = '\0';
	}
    } else {
	data->h.h_name = NULL;
    }

    if (phost->h_aliases) {
	data->h.h_aliases = p_next_charptr;
	for (pcnt = phost->h_aliases, i_alias = 0;
	     (*pcnt && i_alias < num_addrs);
	     pcnt++, i_alias++) {
	    mem_this_alias = strlen(*pcnt) + 1;
	    required_this_alias = sizeof(phost->h_aliases[0]) +
		mem_this_alias;
	    if (available >= required_this_alias) {
		i_alias++;
		available -= required_this_alias;
		strcpy(p_next_char, *pcnt);
		*p_next_charptr++ = p_next_char;
		p_next_char += mem_this_alias;
	    }
	    p_next_char += sizeof(phost->h_aliases[0]);
	}
	*p_next_charptr++ = NULL;
    } else {
	data->h.h_aliases = NULL;
    }
    curlen = p_next_char - (char *) rehostent;
    return curlen;
}

/*
 * This chunk of code is used in both win32 and cygwin.
 */
#if defined(_WINDOWS_NSL)
static LYNX_HOSTENT *gbl_phost;	/* Pointer to host - See netdb.h */

#if !(defined(__CYGWIN__) && defined(NSL_FORK))
static int donelookup;

static unsigned long __stdcall _fork_func(void *arg)
{
    const char *host = (const char *) arg;
    static AlignedHOSTENT aligned_full_rehostent;
    char *rehostent = (char *) &aligned_full_rehostent;
    size_t rehostentlen = 0;

#ifdef SH_EX
    unsigned long addr;

    addr = (unsigned long) inet_addr(host);
    if (addr != INADDR_NONE)
	gbl_phost = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
    else
	gbl_phost = gethostbyname(host);
#else
    gbl_phost = gethostbyname(host);
#endif

    if (gbl_phost) {
	rehostentlen = fill_rehostent(rehostent, REHOSTENT_SIZE, gbl_phost);
	if (rehostentlen == 0) {
	    gbl_phost = (LYNX_HOSTENT *) NULL;
	} else {
	    gbl_phost = (LYNX_HOSTENT *) rehostent;
	}
    }

    donelookup = TRUE;
    return (unsigned long) (gbl_phost);
}
#endif /* __CYGWIN__ */
#endif /* _WINDOWS_NSL */
#endif /* NSL_FORK */

#ifndef HAVE_H_ERRNO
#undef  h_errno
#define h_errno my_errno
static int my_errno;

#else /* we do HAVE_H_ERRNO: */
#ifndef h_errno			/* there may be a macro as well as the extern data */
extern int h_errno;
#endif
#endif

/*
 * Even though it is a small amount, we cannot count on reading the whole
 * struct via a pipe in one read -TD
 */
#ifdef NSL_FORK
static unsigned readit(int fd, char *buffer, unsigned length)
{
    unsigned result = 0;

    while (length != 0) {
	unsigned got = read(fd, buffer, length);

	if (got != 0) {
	    result += got;
	    buffer += got;
	    length -= got;
	} else {
	    break;
	}
    }
    return result;
}
#endif /* NSL_FORK */

/*	Resolve an internet hostname, like gethostbyname
 *	------------------------------------------------
 *
 *  On entry,
 *	str	points to the given host name, not numeric address,
 *		without colon or port number.
 *
 *  On exit,
 *	returns a pointer to a LYNX_HOSTENT in static storage,
 *	or NULL in case of error or user interruption.
 *
 *  The interface is intended to be exactly the same as for (Unix)
 *  gethostbyname(), except for the following:
 *
 *  If NSL_FORK is not used, the result of gethostbyname is returned
 *  directly.  Otherwise:
 *  All lists, addresses, and strings referred to by pointers in the
 *  returned struct are located, together with the returned struct
 *  itself, in a buffer of size REHOSTENT_SIZE.  If not everything fits,
 *  some info is omitted, but the function is careful to still return
 *  a valid structure, without truncating strings; it tries to return,
 *  in order of decreasing priority, the first address (h_addr_list[0]), the
 *  official name (h_name), the additional addresses, then alias names.
 *
 *  If NULL is returned, the reason is made available in the global
 *  variable lynx_nsl_status, with one of the following values:
 *	HT_INTERRUPTED		Interrupted by user
 *	HT_NOT_ACCEPTABLE	Hostname detected as invalid
 *				(also sets h_errno)
 *	HT_H_ERRNO_VALID	An error occurred, and h_errno holds
 *				an appropriate value
 *	HT_ERROR		Resolver error, reason not known
 *	HT_INTERNAL		Internal error
 */
LYNX_HOSTENT *LYGetHostByName(char *str)
{
    char *host = str;

#ifdef NSL_FORK
    /* for transfer of result between from child to parent: */
    static AlignedHOSTENT aligned_full_rehostent;

    /*
     * We could define rehosten directly as a static char
     * rehostent[REHOSTENT_SIZE], but the indirect approach via the above
     * struct should automatically take care of alignment requirements.
     * Note that, in addition,
     * - this must be static, as we shall return a pointer to it which must
     *   remain valid, and
     * - we have to use the same rehostent in the child process as in the
     *   parent (its address in the parent's address space must be the same as
     *   in the child's, otherwise the internal pointers built by the child's
     *   call to fill_rehostent would be invalid when seen by the parent).  -kw
     */
    char *rehostent = (char *) &aligned_full_rehostent;

    /* for transfer of status from child to parent: */
    struct _statuses {
	size_t rehostentlen;
	int h_length;
	int child_errno;	/* sometimes useful to pass this on */
	int child_h_errno;
	BOOL h_errno_valid;
    } statuses;

    size_t rehostentlen = 0;
#endif /* NSL_FORK */

    LYNX_HOSTENT *result_phost = NULL;

#ifdef __DJGPP__
    _resolve_hook = ResolveYield;
#endif

    if (!str) {
	CTRACE((tfp, "LYGetHostByName: Can't parse `NULL'.\n"));
	lynx_nsl_status = HT_INTERNAL;
	return NULL;
    }
    CTRACE((tfp, "LYGetHostByName: parsing `%s'.\n", str));

    /*  Could disable this if all our callers already check - kw */
    if (HTCheckForInterrupt()) {
	CTRACE((tfp, "LYGetHostByName: INTERRUPTED for '%s'.\n", str));
	lynx_nsl_status = HT_INTERRUPTED;
	return NULL;
    }

    if (!valid_hostname(host)) {
	lynx_nsl_status = HT_NOT_ACCEPTABLE;
#ifdef NO_RECOVERY
#ifdef _WINDOWS
	WSASetLastError(NO_RECOVERY);
#else
	h_errno = NO_RECOVERY;
#endif
#endif
	return NULL;
    }
#ifdef MVS			/* Outstanding problem with crash in MVS gethostbyname */
    CTRACE((tfp, "LYGetHostByName: Calling gethostbyname(%s)\n", host));
#endif /* MVS */

    CTRACE_FLUSH(tfp);		/* so child messages will not mess up parent log */

    lynx_nsl_status = HT_INTERNAL;	/* should be set to something else below */

#ifdef NSL_FORK
    statuses.h_errno_valid = NO;
    /*
     * Start block for fork-based gethostbyname() with checks for interrupts.
     * - Tom Zerucha (tz@execpc.com) & FM
     */
    {
	int got_rehostent = 0;

#if HAVE_SIGACTION
	sigset_t old_sigset;
	sigset_t new_sigset;
#endif
	/*
	 * Pipe, child pid, status buffers, start time, select() control
	 * variables.
	 */
	int fpid, waitret;
	int pfd[2], selret, readret;

#ifdef HAVE_TYPE_UNIONWAIT
	union wait waitstat;

#else
	int waitstat = 0;
#endif
	time_t start_time = time((time_t *) 0);
	fd_set readfds;
	struct timeval one_second;
	long dns_patience = 30;	/* how many seconds will we wait for DNS? */
	int child_exited = 0;

	/*
	 * Reap any children that have terminated since last time through.
	 * This might include children that we killed, then waited with WNOHANG
	 * before they were actually ready to be reaped.  (Should be max of 1
	 * in this state, but the loop is safe if waitpid() is implemented
	 * correctly:  returns 0 when children exist but none have exited; -1
	 * with errno == ECHILD when no children.) -BL
	 */
	do {
	    waitret = waitpid(-1, 0, WNOHANG);
	} while (waitret > 0 || (waitret == -1 && errno == EINTR));
	waitret = 0;

	pipe(pfd);

#if HAVE_SIGACTION
	/*
	 * Attempt to prevent a rare situation where the child could execute
	 * the Lynx signal handlers because it gets killed before it even has a
	 * chance to reset its handlers, resulting in bogus 'Exiting via
	 * interrupt' message and screen corruption or worse.
	 * Should that continue to be reported, for systems without
	 * sigprocmask(), we need to find a different solutions for those.  -
	 * kw 19990430
	 */
	sigemptyset(&new_sigset);
	sigaddset(&new_sigset, SIGTERM);
	sigaddset(&new_sigset, SIGINT);
#ifndef NOSIGHUP
	sigaddset(&new_sigset, SIGHUP);
#endif /* NOSIGHUP */
#ifdef SIGTSTP
	sigaddset(&new_sigset, SIGTSTP);
#endif /* SIGTSTP */
#ifdef SIGWINCH
	sigaddset(&new_sigset, SIGWINCH);
#endif /* SIGWINCH */
	sigprocmask(SIG_BLOCK, &new_sigset, &old_sigset);
#endif /* HAVE_SIGACTION */

	if ((fpid = fork()) == 0) {
	    LYNX_HOSTENT *phost;	/* Pointer to host - See netdb.h */

	    /*
	     * Child - for the long call.
	     *
	     * Make sure parent can kill us at will.  -BL
	     */
	    (void) signal(SIGTERM, quench);

	    /*
	     * Also make sure the child does not run one of the signal handlers
	     * that may have been installed by Lynx if one of those signals
	     * occurs.  For example we don't want the child to remove temp
	     * files on ^C, let the parent deal with that.  - kw
	     */
	    (void) signal(SIGINT, quench);
#ifndef NOSIGHUP
	    (void) signal(SIGHUP, quench);
#endif /* NOSIGHUP */
#ifdef SIGTSTP
	    if (no_suspend)
		(void) signal(SIGTSTP, SIG_IGN);
	    else
		(void) signal(SIGTSTP, SIG_DFL);
#endif /* SIGTSTP */
#ifdef SIGWINCH
	    (void) signal(SIGWINCH, SIG_IGN);
#endif /* SIGWINCH */
#ifndef __linux__
#ifndef DOSPATH
	    signal(SIGBUS, SIG_DFL);
#endif /* DOSPATH */
#endif /* !__linux__ */
	    signal(SIGSEGV, SIG_DFL);
	    signal(SIGILL, SIG_DFL);

#if HAVE_SIGACTION
	    /* Restore signal mask to whatever it was before the fork. -kw */
	    sigprocmask(SIG_SETMASK, &old_sigset, NULL);
#endif /* HAVE_SIGACTION */

	    /*
	     * Child won't use read side.  -BL
	     */
	    close(pfd[0]);
#ifdef HAVE_H_ERRNO
	    /* to detect cases when it doesn't get set although it should */
	    h_errno = -2;
#endif
	    set_errno(0);
	    phost = gethostbyname(host);
	    statuses.child_errno = errno;
	    statuses.child_h_errno = h_errno;
#ifdef HAVE_H_ERRNO
	    statuses.h_errno_valid = YES;
#endif
#ifdef MVS
	    CTRACE((tfp, "LYGetHostByName: gethostbyname() returned %d\n", phost));
#endif /* MVS */

#ifdef DEBUG_HOSTENT_CHILD
	    dump_hostent("CHILD gethostbyname", phost);
#endif
	    if (OK_HOST(phost)) {
		rehostentlen = fill_rehostent(rehostent, REHOSTENT_SIZE, phost);
#ifdef DEBUG_HOSTENT_CHILD
		dump_hostent("CHILD fill_rehostent", (LYNX_HOSTENT *) rehostent);
#endif
	    }
	    if (rehostentlen <= sizeof(LYNX_HOSTENT) ||
		!OK_HOST((LYNX_HOSTENT *) rehostent)) {
		rehostentlen = 0;
		statuses.h_length = 0;
	    } else {
		statuses.h_length = ((LYNX_HOSTENT *) rehostent)->h_length;
#ifdef HAVE_H_ERRNO
		if (h_errno == -2)	/* success, but h_errno unchanged? */
		    statuses.h_errno_valid = NO;
#endif
	    }
	    /*
	     * Send variables indicating status of lookup to parent.  That
	     * includes rehostentlen, which the parent will use as the size for
	     * the second read (if > 0).
	     */
	    if (!statuses.child_errno)
		statuses.child_errno = errno;
	    statuses.rehostentlen = rehostentlen;
	    write(pfd[1], &statuses, sizeof(statuses));

	    if (rehostentlen) {
		/*
		 * Return our resulting rehostent through pipe...
		 */
		write(pfd[1], rehostent, rehostentlen);
		close(pfd[1]);
		_exit(0);
	    } else {
		/*
		 * ...  or return error as exit code.
		 */
		_exit(1);
	    }
	}
#if HAVE_SIGACTION
	/*
	 * (parent) Restore signal mask to whatever it was before the fork.  -
	 * kw
	 */
	sigprocmask(SIG_SETMASK, &old_sigset, NULL);
#endif /* HAVE_SIGACTION */

	/*
	 * (parent) Wait until lookup finishes, or interrupt, or cycled too
	 * many times (just in case) -BL
	 */

	close(pfd[1]);		/* parent won't use write side -BL */

	if (fpid < 0) {		/* fork failed */
	    close(pfd[0]);
	    goto failed;
	}

	while (child_exited || (long) (time((time_t *) 0) - start_time) < dns_patience) {

	    FD_ZERO(&readfds);
	    /*
	     * This allows us to abort immediately, not after 1-second timeout,
	     * when user hits abort key.  Can't do this when using SLANG (or at
	     * least I don't know how), so SLANG users must live with up-to-1s
	     * timeout.  -BL
	     *
	     * Whoops -- we need to make sure stdin is actually selectable!
	     * /dev/null isn't, on some systems, which makes some useful Lynx
	     * invocations fail.  -BL
	     */
	    {
		int kbd_fd = LYConsoleInputFD(TRUE);

		if (kbd_fd != INVSOC) {
		    FD_SET(kbd_fd, &readfds);
		}
	    }

	    one_second.tv_sec = 1;
	    one_second.tv_usec = 0;
	    FD_SET(pfd[0], &readfds);

	    /*
	     * Return when data received, interrupted, or failed.  If nothing
	     * is waiting, we sleep for 1 second in select(), to be nice to the
	     * system.  -BL
	     */
#ifdef SOCKS
	    if (socks_flag)
		selret = Rselect(pfd[0] + 1, &readfds, NULL, NULL, &one_second);
	    else
#endif /* SOCKS */
		selret = select(pfd[0] + 1, &readfds, NULL, NULL, &one_second);

	    if ((selret > 0) && FD_ISSET(pfd[0], &readfds)) {
		/*
		 * First get status, including length of address.  -BL, kw
		 */
		readret = readit(pfd[0], (char *) &statuses, sizeof(statuses));
		if (readret == sizeof(statuses)) {
		    h_errno = statuses.child_h_errno;
		    set_errno(statuses.child_errno);
#ifdef HAVE_H_ERRNO
		    if (statuses.h_errno_valid) {
			lynx_nsl_status = HT_H_ERRNO_VALID;
			/*
			 * If something went wrong in the child process other
			 * than normal lookup errors, and it appears that we
			 * have enough info to know what went wrong, generate
			 * diagnostic output.  ENOMEM observed on linux in
			 * processes constrained with ulimit.  It would be too
			 * unkind to abort the session, access to local files
			 * or through a proxy may still work.  - kw
			 */
			if (
#ifdef NETDB_INTERNAL		/* linux glibc: defined in netdb.h */
			       (errno && h_errno == NETDB_INTERNAL) ||
#endif
			       (errno == ENOMEM &&
				statuses.rehostentlen == 0 &&
			/* should probably be NETDB_INTERNAL if child
			   memory exhausted, but we may find that
			   h_errno remains unchanged. - kw */
				h_errno == -2)) {
#ifndef MULTINET
			    HTInetStatus("CHILD gethostbyname");
#endif
			    HTAlert(LYStrerror(statuses.child_errno));
			    if (errno == ENOMEM) {
				/*
				 * Not much point in continuing, right?  Fake a
				 * 'z', should shorten pointless guessing
				 * cycle.  - kw
				 */
				LYFakeZap(YES);
			    }
			}
		    }
#endif /* HAVE_H_ERRNO */
		    if (statuses.rehostentlen > sizeof(LYNX_HOSTENT)) {
			/*
			 * Then get the full reorganized hostent.  -BL, kw
			 */
			readret = readit(pfd[0], rehostent, statuses.rehostentlen);
#ifdef DEBUG_HOSTENT
			dump_hostent("Read from pipe", (LYNX_HOSTENT *) rehostent);
#endif
			if (readret == (int) statuses.rehostentlen) {
			    got_rehostent = 1;
			    result_phost = (LYNX_HOSTENT *) rehostent;
			    lynx_nsl_status = HT_OK;
			} else if (!statuses.h_errno_valid) {
			    lynx_nsl_status = HT_INTERNAL;
			}
		    }
		} else {
		    lynx_nsl_status = HT_ERROR;
		}
		/*
		 * Make sure child is cleaned up.  -BL
		 */
		if (!child_exited)
		    waitret = waitpid(fpid, &waitstat, WNOHANG);
		if (!WIFEXITED(waitstat) && !WIFSIGNALED(waitstat)) {
		    kill(fpid, SIGTERM);
		    waitret = waitpid(fpid, &waitstat, WNOHANG);
		}
		break;
	    }

	    /*
	     * Clean up if child exited before & no data received.  -BL
	     */
	    if (child_exited) {
		waitret = waitpid(fpid, &waitstat, WNOHANG);
		break;
	    }
	    /*
	     * If child exited, loop once more looking for data.  -BL
	     */
	    if ((waitret = waitpid(fpid, &waitstat, WNOHANG)) > 0) {
		/*
		 * Data will be arriving right now, so make sure we don't
		 * short-circuit out for too many loops, and skip the interrupt
		 * check.  -BL
		 */
		child_exited = 1;
		continue;
	    }

	    /*
	     * Abort if interrupt key pressed.
	     */
	    if (HTCheckForInterrupt()) {
		CTRACE((tfp, "LYGetHostByName: INTERRUPTED gethostbyname.\n"));
		kill(fpid, SIGTERM);
		waitpid(fpid, NULL, WNOHANG);
		close(pfd[0]);
		lynx_nsl_status = HT_INTERRUPTED;
		return NULL;
	    }
	}
	close(pfd[0]);
	if (waitret <= 0) {
	    kill(fpid, SIGTERM);
	    waitret = waitpid(fpid, &waitstat, WNOHANG);
	}
	if (waitret > 0) {
	    if (WIFEXITED(waitstat)) {
		CTRACE((tfp,
			"LYGetHostByName: NSL_FORK child %d exited, status 0x%x.\n",
			(int) waitret, WEXITSTATUS(waitstat)));
	    } else if (WIFSIGNALED(waitstat)) {
		CTRACE((tfp,
			"LYGetHostByName: NSL_FORK child %d got signal, status 0x%x!\n",
			(int) waitret, WTERMSIG(waitstat)));
#ifdef WCOREDUMP
		if (WCOREDUMP(waitstat)) {
		    CTRACE((tfp,
			    "LYGetHostByName: NSL_FORK child %d dumped core!\n",
			    (int) waitret));
		}
#endif /* WCOREDUMP */
	    } else if (WIFSTOPPED(waitstat)) {
		CTRACE((tfp,
			"LYGetHostByName: NSL_FORK child %d is stopped, status 0x%x!\n",
			(int) waitret, WSTOPSIG(waitstat)));
	    }
	}
	if (!got_rehostent) {
	    goto failed;
	}
    }
#else /* Not NSL_FORK: */

#ifdef _WINDOWS_NSL
    {
	HANDLE hThread;
	DWORD dwThreadID;

#ifndef __CYGWIN__
	if (!system_is_NT) {	/* for Windows9x */
	    unsigned long t;

	    t = (unsigned long) inet_addr(host);
	    if (t != INADDR_NONE)
		gbl_phost = gethostbyaddr((char *) &t, sizeof(t), AF_INET);
	    else
		gbl_phost = gethostbyname(host);
	} else {		/* for Windows NT */
#endif /* !__CYGWIN__ */
	    gbl_phost = (LYNX_HOSTENT *) NULL;
	    donelookup = FALSE;

#if defined(__CYGWIN__) || defined(USE_WINSOCK2_H)
	    SetLastError(WSAHOST_NOT_FOUND);
#else
	    WSASetLastError(WSAHOST_NOT_FOUND);
#endif

	    hThread = CreateThread(NULL, 4096UL, _fork_func, host, 0UL,
				   &dwThreadID);
	    if (!hThread)
		MessageBox(NULL, "CreateThread",
			   "CreateThread Failed", 0L);

	    while (!donelookup) {
		if (HTCheckForInterrupt()) {
		    /* Note that host is a character array and is not freed */
		    /* to avoid possible subthread problems: */
		    if (!CloseHandle(hThread)) {
			MessageBox((void *) NULL,
				   "CloseHandle", "CloseHandle Failed", 0L);
		    }
		    lynx_nsl_status = HT_INTERRUPTED;
		    return NULL;
		}
	    }
#ifndef __CYGWIN__
	}
#endif /* !__CYGWIN__ */
	if (gbl_phost) {
	    lynx_nsl_status = HT_OK;
	    result_phost = gbl_phost;
	} else {
	    lynx_nsl_status = HT_ERROR;
	    goto failed;
	}
    }

#else /* !NSL_FORK, !_WINDOWS_NSL: */
    {
	LYNX_HOSTENT *phost;

	phost = gethostbyname(host);	/* See netdb.h */
#ifdef MVS
	CTRACE((tfp, "LYGetHostByName: gethostbyname() returned %d\n", phost));
#endif /* MVS */
	if (phost) {
	    lynx_nsl_status = HT_OK;
	    result_phost = phost;
	} else {
	    lynx_nsl_status = HT_H_ERRNO_VALID;
	    goto failed;
	}
    }
#endif /* !NSL_FORK, !_WINDOWS_NSL */
#endif /* !NSL_FORK */

#ifdef DEBUG_HOSTENT
    dump_hostent("End of LYGetHostByName", result_phost);
    CTRACE((tfp, "LYGetHostByName: Resolved name to a hostent.\n"));
#endif

    return result_phost;	/* OK */

  failed:
    CTRACE((tfp, "LYGetHostByName: Can't find internet node name `%s'.\n",
	    host));
    return NULL;
}

/*	Parse a network node address and port
 *	-------------------------------------
 *
 *  On entry,
 *	str	points to a string with a node name or number,
 *		with optional trailing colon and port number.
 *	soc_in	points to the binary internet or decnet address field.
 *
 *  On exit,
 *	*soc_in is filled in.  If no port is specified in str, that
 *		field is left unchanged in *soc_in.
 */
#ifndef INET6
static int HTParseInet(SockA * soc_in, const char *str)
{
    char *port;
    int dotcount_ip = 0;	/* for dotted decimal IP addr */
    char *strptr;
    char *host = NULL;

    if (!str) {
	CTRACE((tfp, "HTParseInet: Can't parse `NULL'.\n"));
	return -1;
    }
    CTRACE((tfp, "HTParseInet: parsing `%s'.\n", str));
    if (HTCheckForInterrupt()) {
	CTRACE((tfp, "HTParseInet: INTERRUPTED for '%s'.\n", str));
	return -1;
    }
    StrAllocCopy(host, str);	/* Make a copy we can mutilate */
    /*
     * Parse port number if present.
     */
    if ((port = strchr(host, ':')) != NULL) {
	*port++ = 0;		/* Chop off port */
	strptr = port;
	if (port[0] >= '0' && port[0] <= '9') {
#ifdef UNIX
	    soc_in->sin_port = (PortNumber) htons(strtol(port, &strptr, 10));
#else /* VMS: */
#ifdef DECNET
	    soc_in->sdn_objnum = (unsigned char) (strtol(port, &strptr, 10));
#else
	    soc_in->sin_port = htons((PortNumber) strtol(port, &strptr, 10));
#endif /* Decnet */
#endif /* Unix vs. VMS */
#ifdef SUPPRESS			/* 1. crashes!?!.  2. URL syntax has number not name */
	} else {
	    struct servent *serv = getservbyname(port, (char *) 0);

	    if (serv) {
		soc_in->sin_port = serv->s_port;
	    } else {
		CTRACE((tfp, "TCP: Unknown service %s\n", port));
	    }
#endif /* SUPPRESS */
	}
	if (strptr && *strptr != '\0') {
	    FREE(host);
	    HTAlwaysAlert(NULL, gettext("Address has invalid port"));
	    return -1;
	}
    }
#ifdef DECNET
    /*
     * Read Decnet node name.  @@ Should know about DECnet addresses, but it's
     * probably worth waiting until the Phase transition from IV to V.
     */
    soc_in->sdn_nam.n_len = min(DN_MAXNAML, strlen(host));	/* <=6 in phase 4 */
    strncpy(soc_in->sdn_nam.n_name, host, soc_in->sdn_nam.n_len + 1);
    CTRACE((tfp,
	    "DECnet: Parsed address as object number %d on host %.6s...\n",
	    soc_in->sdn_objnum, host));
#else /* parse Internet host: */

    if (*host >= '0' && *host <= '9') {		/* Test for numeric node address: */
	strptr = host;
	while (*strptr) {
	    if (*strptr == '.') {
		dotcount_ip++;
	    } else if (!isdigit(UCH(*strptr))) {
		break;
	    }
	    strptr++;
	}
	if (*strptr) {		/* found non-numeric, assume domain name */
	    dotcount_ip = 0;
	}
    }

    /*
     * Parse host number if present.
     */
    if (dotcount_ip == 3)	/* Numeric node address: */
    {
#ifdef DGUX_OLD
	soc_in->sin_addr.s_addr = inet_addr(host).s_addr;	/* See arpa/inet.h */
#else
#ifdef GUSI
	soc_in->sin_addr = inet_addr(host);	/* See netinet/in.h */
#else
#ifdef HAVE_INET_ATON
	if (!inet_aton(host, &(soc_in->sin_addr))) {
	    CTRACE((tfp, "inet_aton(%s) returns error\n", host));
	    FREE(host);
	    return -1;
	}
#else
	soc_in->sin_addr.s_addr = inet_addr(host);	/* See arpa/inet.h */
#endif /* HAVE_INET_ATON */
#endif /* GUSI */
#endif /* DGUX_OLD */
	FREE(host);
    } else {			/* Alphanumeric node name: */

#ifdef MVS			/* Outstanding problem with crash in MVS gethostbyname */
	CTRACE((tfp, "HTParseInet: Calling LYGetHostByName(%s)\n", host));
#endif /* MVS */

#ifdef _WINDOWS_NSL
	gbl_phost = LYGetHostByName(host);	/* See above */
	if (!gbl_phost)
	    goto failed;
	memcpy((void *) &soc_in->sin_addr, gbl_phost->h_addr_list[0], gbl_phost->h_length);
#else /* !_WINDOWS_NSL */
	{
	    LYNX_HOSTENT *phost;

	    phost = LYGetHostByName(host);	/* See above */

	    if (!phost)
		goto failed;
	    if (!phost)
		goto failed;
	    if (phost->h_length != sizeof soc_in->sin_addr) {
		HTAlwaysAlert(host, gettext("Address length looks invalid"));
	    }
	    memcpy((void *) &soc_in->sin_addr, phost->h_addr_list[0], phost->h_length);
	}
#endif /* _WINDOWS_NSL */

	FREE(host);
    }				/* Alphanumeric node name */

    CTRACE((tfp,
	    "HTParseInet: Parsed address as port %d, IP address %d.%d.%d.%d\n",
	    (int) ntohs(soc_in->sin_port),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 0),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 1),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 2),
	    (int) *((unsigned char *) (&soc_in->sin_addr) + 3)));
#endif /* Internet vs. Decnet */

    return 0;			/* OK */

  failed:
    CTRACE((tfp, "HTParseInet: Can't find internet node name `%s'.\n",
	    host));
    FREE(host);
    switch (lynx_nsl_status) {
    case HT_NOT_ACCEPTABLE:
    case HT_INTERRUPTED:
	return lynx_nsl_status;
    default:
	return -1;
    }
}
#endif /* !INET6 */

#ifdef INET6
static LYNX_ADDRINFO *HTGetAddrInfo(const char *str,
				    const int defport)
{
    LYNX_ADDRINFO hints, *res;
    int error;
    char *p;
    char *s = NULL;
    char *host, *port;
    char pbuf[80];

    StrAllocCopy(s, str);

    if (s[0] == '[' && (p = strchr(s, ']')) != NULL) {
	*p++ = '\0';
	host = s + 1;
    } else {
	p = s;
	host = &s[0];
    }
    port = strrchr(p, ':');
    if (port) {
	*port++ = '\0';
    } else {
	sprintf(pbuf, "%d", defport);
	port = pbuf;
    }

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    error = getaddrinfo(host, port, &hints, &res);
    if (error || !res) {
	CTRACE((tfp, "HTGetAddrInfo: getaddrinfo(%s, %s): %s\n", host, port,
		gai_strerror(error)));
	res = NULL;
    }

    free(s);
    return res;
}
#endif /* INET6 */

#ifdef LY_FIND_LEAKS
/*	Free our name for the host on which we are - FM
 *	-------------------------------------------
 *
 */
static void free_HTTCP_hostname(void)
{
    FREE(hostname);
}
#endif /* LY_FIND_LEAKS */

/*	Derive the name of the host on which we are
 *	-------------------------------------------
 *
 */
static void get_host_details(void)
{
    char name[MAXHOSTNAMELEN + 1];	/* The name of this host */

#ifdef UCX
    char *domain_name;		/* The name of this host domain */
#endif /* UCX */
#ifdef NEED_HOST_ADDRESS	/* no -- needs name server! */
#ifdef INET6
    LYNX_ADDRINFO hints, *res;
    int error;

#else
    LYNX_HOSTENT *phost;	/* Pointer to host -- See netdb.h */
#endif /* INET6 */
#endif /* NEED_HOST_ADDRESS */
    int namelength = sizeof(name);

    if (hostname)
	return;			/* Already done */
    gethostname(name, namelength);	/* Without domain */
    StrAllocCopy(hostname, name);
#ifdef LY_FIND_LEAKS
    atexit(free_HTTCP_hostname);
#endif
#ifdef UCX
    /*
     * UCX doesn't give the complete domain name.  Get rest from UCX$BIND_DOM
     * logical.
     */
    if (strchr(hostname, '.') == NULL) {	/* Not full address */
	domain_name = LYGetEnv("UCX$BIND_DOMAIN");
	if (domain_name == NULL)
	    domain_name = LYGetEnv("TCPIP$BIND_DOMAIN");
	if (domain_name != NULL) {
	    StrAllocCat(hostname, ".");
	    StrAllocCat(hostname, domain_name);
	}
    }
#endif /* UCX */
    CTRACE((tfp, "TCP: Local host name is %s\n", hostname));

#ifndef DECNET			/* Decnet ain't got no damn name server 8#OO */
#ifdef NEED_HOST_ADDRESS	/* no -- needs name server! */
#ifdef INET6
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_CANONNAME;
    error = getaddrinfo(name, NULL, &hints, &res);
    if (error || !res || !res->ai_canonname) {
	CTRACE((tfp, "TCP: %s: `%s'\n", gai_strerror(error), name));
	if (res)
	    freeaddrinfo(res);
	return;			/* Fail! */
    }
    StrAllocCopy(hostname, res->ai_canonname);
    memcpy(&HTHostAddress, res->ai_addr, res->ai_addrlen);
    freeaddrinfo(res);
#else
    phost = gethostbyname(name);	/* See netdb.h */
    if (!OK_HOST(phost)) {
	CTRACE((tfp,
		"TCP: Can't find my own internet node address for `%s'!!\n",
		name));
	return;			/* Fail! */
    }
    StrAllocCopy(hostname, phost->h_name);
    memcpy(&HTHostAddress, &phost->h_addr_list[0], phost->h_length);
#endif /* INET6 */
    CTRACE((tfp, "     Name server says that I am `%s' = %s\n",
	    hostname, HTInetString(&HTHostAddress)));
#endif /* NEED_HOST_ADDRESS */

#endif /* !DECNET */
}

const char *HTHostName(void)
{
    get_host_details();
    return hostname;
}

#ifndef MULTINET		/* SOCKET_ERRNO != errno ? */
#if !defined(UCX) || !defined(VAXC)	/* errno not modifiable ? */
#define SOCKET_DEBUG_TRACE	/* show errno status after some system calls */
#endif /* UCX && VAXC */
#endif /* MULTINET */
/*
 *  Interruptible connect as implemented for Mosaic by Marc Andreesen
 *  and hacked in for Lynx years ago by Lou Montulli, and further
 *  modified over the years by numerous Lynx lovers. - FM
 */
int HTDoConnect(const char *url,
		const char *protocol,
		int default_port,
		int *s)
{
    int status = 0;
    char *line = NULL;
    char *p1 = NULL;
    char *at_sign = NULL;
    char *host = NULL;

#ifdef INET6
    LYNX_ADDRINFO *res = 0, *res0 = 0;

#else
    struct sockaddr_in soc_address;
    struct sockaddr_in *soc_in = &soc_address;

    /*
     * Set up defaults.
     */
    memset(soc_in, 0, sizeof(*soc_in));
    soc_in->sin_family = AF_INET;
    soc_in->sin_port = htons((PortNumber) default_port);
#endif /* INET6 */

    /*
     * Get node name and optional port number.
     */
    p1 = HTParse(url, "", PARSE_HOST);
    if ((at_sign = strchr(p1, '@')) != NULL) {
	/*
	 * If there's an @ then use the stuff after it as a hostname.
	 */
	StrAllocCopy(host, (at_sign + 1));
    } else {
	StrAllocCopy(host, p1);
    }
    FREE(p1);

    HTSprintf0(&line, "%s%s", WWW_FIND_MESSAGE, host);
    _HTProgress(line);
#ifdef INET6
    /* HTParseInet() is useless! */
    res0 = HTGetAddrInfo(host, default_port);
    if (res0 == NULL) {
	HTSprintf0(&line, gettext("Unable to locate remote host %s."), host);
	_HTProgress(line);
	FREE(host);
	FREE(line);
	return HT_NO_DATA;
    }
#else
    status = HTParseInet(soc_in, host);
    if (status) {
	if (status != HT_INTERRUPTED) {
	    if (status == HT_NOT_ACCEPTABLE) {
		/* Not HTProgress, so warning won't be overwritten immediately;
		 * but not HTAlert, because typically there will be other
		 * alerts from the callers.  - kw
		 */
		HTUserMsg2(gettext("Invalid hostname %s"), host);
	    } else {
		HTSprintf0(&line,
			   gettext("Unable to locate remote host %s."), host);
		_HTProgress(line);
	    }
	    status = HT_NO_DATA;
	}
	FREE(host);
	FREE(line);
	return status;
    }
#endif /* INET6 */

    HTSprintf0(&line, gettext("Making %s connection to %s"), protocol, host);
    _HTProgress(line);
    FREE(host);
    FREE(line);

    /*
     * Now, let's get a socket set up from the server for the data.
     */
#ifndef INET6
    *s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (*s == -1) {
	HTAlert(gettext("socket failed."));
	return HT_NO_DATA;
    }
#else
    for (res = res0; res; res = res->ai_next) {
	*s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	if (*s == -1) {
	    char hostbuf[1024], portbuf[1024];

	    getnameinfo(res->ai_addr, res->ai_addrlen,
			hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
			NI_NUMERICHOST | NI_NUMERICSERV);
	    HTSprintf0(&line,
		       gettext("socket failed: family %d addr %s port %s."),
		       res->ai_family, hostbuf, portbuf);
	    _HTProgress(line);
	    FREE(line);
	    continue;
	}
#endif /* INET6 */

#if !defined(DOSPATH) || defined(__DJGPP__)
#if !defined(NO_IOCTL) || defined(USE_FCNTL)
	/*
	 * Make the socket non-blocking, so the connect can be canceled.  This
	 * means that when we issue the connect we should NOT have to wait for
	 * the accept on the other end.
	 */
	{
#ifdef USE_FCNTL
	    int ret = fcntl(*s, F_SETFL, O_NONBLOCK);

#else
	    int val = 1;
	    int ret = IOCTL(*s, FIONBIO, &val);
#endif /* USE_FCNTL */
	    if (ret == -1)
		_HTProgress(gettext("Could not make connection non-blocking."));
	}
#endif /* !NO_IOCTL || USE_FCNTL */
#endif /* !DOSPATH || __DJGPP__ */

	/*
	 * Issue the connect.  Since the server can't do an instantaneous
	 * accept and we are non-blocking, this will almost certainly return a
	 * negative status.
	 */
#ifdef SOCKS
	if (socks_flag) {
#ifdef INET6
	    status = Rconnect(*s, res->ai_addr, res->ai_addrlen);
#else
	    status = Rconnect(*s, (struct sockaddr *) &soc_address,
			      sizeof(soc_address));
#ifndef SHORTENED_RBIND
	    socks_bind_remoteAddr = soc_address.sin_addr.s_addr;
#endif
#endif /* INET6 */
	} else
#endif /* SOCKS */
#ifdef INET6
	    status = connect(*s, res->ai_addr, res->ai_addrlen);
#else
	    status = connect(*s, (struct sockaddr *) &soc_address, sizeof(soc_address));
#endif /* INET6 */

	/*
	 * According to the Sun man page for connect:
	 *  EINPROGRESS         The socket is non-blocking and the  con-
	 *                      nection cannot be completed immediately.
	 *                      It is possible to select(2) for  comple-
	 *                      tion  by  selecting the socket for writ-
	 *                      ing.
	 * According to the Motorola SVR4 man page for connect:
	 *  EAGAIN              The socket is non-blocking and the  con-
	 *                      nection cannot be completed immediately.
	 *                      It is possible to select for  completion
	 *                      by  selecting  the  socket  for writing.
	 *                      However, this is only  possible  if  the
	 *                      socket  STREAMS  module  is  the topmost
	 *                      module on  the  protocol  stack  with  a
	 *                      write  service  procedure.  This will be
	 *                      the normal case.
	 */
	if ((status < 0) &&
	    (SOCKET_ERRNO == EINPROGRESS
#ifdef EAGAIN
	     || SOCKET_ERRNO == EAGAIN
#endif
	    )) {
	    struct timeval select_timeout;
	    int ret;
	    int tries = 0;

#ifdef SOCKET_DEBUG_TRACE
	    HTInetStatus("this socket's first connect");
#endif /* SOCKET_DEBUG_TRACE */
	    ret = 0;
	    while (ret <= 0) {
		fd_set writefds;

		/*
		 * Protect against an infinite loop.
		 */
		if ((tries++ / 10) >= connect_timeout) {
		    HTAlert(gettext("Connection failed (too many retries)."));
#ifdef INET6
		    FREE(line);
		    if (res0)
			freeaddrinfo(res0);
#endif /* INET6 */
		    return HT_NO_DATA;
		}
#ifdef _WINDOWS_NSL
		select_timeout.tv_sec = connect_timeout;
		select_timeout.tv_usec = 0;
#else
		select_timeout.tv_sec = 0;
		select_timeout.tv_usec = 100000;
#endif /* _WINDOWS_NSL */
		FD_ZERO(&writefds);
		FD_SET((unsigned) *s, &writefds);
#ifdef SOCKS
		if (socks_flag)
		    ret = Rselect((unsigned) *s + 1, NULL,
				  &writefds, NULL, &select_timeout);
		else
#endif /* SOCKS */
		    ret = select((unsigned) *s + 1,
				 NULL,
				 &writefds,
				 NULL,
				 &select_timeout);

#ifdef SOCKET_DEBUG_TRACE
		if (tries == 1) {
		    HTInetStatus("this socket's first select");
		}
#endif /* SOCKET_DEBUG_TRACE */
		/*
		 * If we suspend, then it is possible that select will be
		 * interrupted.  Allow for this possibility.  - JED
		 */
		if ((ret == -1) && (errno == EINTR))
		    continue;

#ifdef SOCKET_DEBUG_TRACE
		if (ret < 0) {
		    HTInetStatus("failed select");
		}
#endif /* SOCKET_DEBUG_TRACE */
		/*
		 * Again according to the Sun and Motorola man pages for
		 * connect:
		 *  EALREADY    The socket is non-blocking and a  previ-
		 *              ous  connection attempt has not yet been
		 *              completed.
		 * Thus if the SOCKET_ERRNO is NOT EALREADY we have a real
		 * error, and should break out here and return that error.
		 * Otherwise if it is EALREADY keep on trying to complete the
		 * connection.
		 */
		if ((ret < 0) && (SOCKET_ERRNO != EALREADY)) {
		    status = ret;
		    break;
		} else if (ret > 0) {
		    /*
		     * Extra check here for connection success, if we try to
		     * connect again, and get EISCONN, it means we have a
		     * successful connection.  But don't check with SOCKS.
		     */
#ifdef SOCKS
		    if (socks_flag) {
			status = 0;
		    } else {
#endif /* SOCKS */
#ifdef INET6
			status = connect(*s, res->ai_addr, res->ai_addrlen);
#else
			status = connect(*s, (struct sockaddr *) &soc_address,
					 sizeof(soc_address));
#endif /* INET6 */
#ifdef UCX
			/*
			 * A UCX feature:  Instead of returning EISCONN UCX
			 * returns EADDRINUSE.  Test for this status also.
			 */
			if ((status < 0) && ((SOCKET_ERRNO == EISCONN) ||
					     (SOCKET_ERRNO == EADDRINUSE)))
#else
			if ((status < 0) && (SOCKET_ERRNO == EISCONN))
#endif /* UCX */
			{
			    status = 0;
			}

			if (status && (SOCKET_ERRNO == EALREADY))	/* new stuff LJM */
			    ret = 0;	/* keep going */
			else {
#ifdef SOCKET_DEBUG_TRACE
			    if (status < 0) {
				HTInetStatus("confirm-ready connect");
			    }
#endif /* SOCKET_DEBUG_TRACE */
			    break;
			}
#ifdef SOCKS
		    }
#endif /* SOCKS */
		}
#ifdef SOCKS
		else if (!socks_flag)
#else
		else
#endif /* SOCKS */
		{
		    /*
		     * The select says we aren't ready yet.  Try to connect
		     * again to make sure.  If we don't get EALREADY or
		     * EISCONN, something has gone wrong.  Break out and report
		     * it.
		     *
		     * For some reason, SVR4 returns EAGAIN here instead of
		     * EALREADY, even though the man page says it should be
		     * EALREADY.
		     *
		     * For some reason, UCX pre 3 apparently returns errno =
		     * 18242 instead of EALREADY or EISCONN.
		     */
#ifdef INET6
		    status = connect(*s, res->ai_addr, res->ai_addrlen);
#else
		    status = connect(*s, (struct sockaddr *) &soc_address,
				     sizeof(soc_address));
#endif /* INET6 */
		    if ((status < 0) &&
			(SOCKET_ERRNO != EALREADY
#ifdef EAGAIN
			 && SOCKET_ERRNO != EAGAIN
#endif
			) &&
#ifdef UCX
			(SOCKET_ERRNO != 18242) &&
#endif /* UCX */
			(SOCKET_ERRNO != EISCONN)) {
#ifdef SOCKET_DEBUG_TRACE
			HTInetStatus("confirm-not-ready connect");
#endif /* SOCKET_DEBUG_TRACE */
			break;
		    }
		}
		if (HTCheckForInterrupt()) {
		    CTRACE((tfp, "*** INTERRUPTED in middle of connect.\n"));
		    status = HT_INTERRUPTED;
#ifdef _WINDOWS
		    WSASetLastError(EINTR);
#else
		    SOCKET_ERRNO = EINTR;
#endif
		    break;
		}
	    }
	}
#ifdef SOCKET_DEBUG_TRACE
	else if (status < 0) {
	    HTInetStatus("this socket's first and only connect");
	}
#endif /* SOCKET_DEBUG_TRACE */
#ifdef INET6
	if (status < 0) {
	    NETCLOSE(*s);
	    *s = -1;
	    continue;
	}
	break;
    }
#endif /* INET6 */

#ifdef INET6
    if (*s < 0)
#else
    if (status < 0)
#endif /* INET6 */
    {
	/*
	 * The connect attempt failed or was interrupted, so close up the
	 * socket.
	 */
	NETCLOSE(*s);
    }
#if !defined(DOSPATH) || defined(__DJGPP__)
#if !defined(NO_IOCTL) || defined(USE_FCNTL)
    else {
	/*
	 * Make the socket blocking again on good connect.
	 */
#ifdef USE_FCNTL
	int ret = fcntl(*s, F_SETFL, 0);

#else
	int val = 0;
	int ret = IOCTL(*s, FIONBIO, &val);
#endif /* USE_FCNTL */
	if (ret == -1)
	    _HTProgress(gettext("Could not restore socket to blocking."));
    }
#endif /* !NO_IOCTL || USE_FCNTL */
#endif /* !DOSPATH || __DJGPP__ */

#ifdef INET6
    FREE(line);
    if (res0)
	freeaddrinfo(res0);
#endif /* INET6 */
    return status;
}

/*
 *  This is so interruptible reads can be implemented cleanly.
 */
int HTDoRead(int fildes,
	     void *buf,
	     unsigned nbyte)
{
    int ready, ret;
    fd_set readfds;
    struct timeval select_timeout;
    int tries = 0;

#ifdef USE_READPROGRESS
    int otries = 0;
    time_t otime = time((time_t *) 0);
#endif
#if defined(UNIX) || defined(UCX)
    int nb;
#endif /* UCX, BSN */

#if defined(UNIX) && !defined(__BEOS__)
    if (fildes == 0) {
	/*
	 * 0 can be a valid socket fd, but if it's a tty something must have
	 * gone wrong.  - kw
	 */
	if (isatty(fildes)) {
	    CTRACE((tfp, "HTDoRead - refusing to read fd 0 which is a tty!\n"));
	    return -1;
	}
    } else
#endif
    if (fildes <= 0)
	return -1;

    if (HTCheckForInterrupt()) {
#ifdef _WINDOWS
	WSASetLastError(EINTR);
#else
	SOCKET_ERRNO = EINTR;
#endif
	return (HT_INTERRUPTED);
    }
#if !defined(NO_IOCTL)
    ready = 0;
#else
    ready = 1;
#endif /* bypass for NO_IOCTL */
    while (!ready) {
	/*
	 * Protect against an infinite loop.
	 */
	if (tries++ >= 180000) {
	    HTAlert(gettext("Socket read failed for 180,000 tries."));
#ifdef _WINDOWS
	    WSASetLastError(EINTR);
#else
	    SOCKET_ERRNO = EINTR;
#endif
	    return HT_INTERRUPTED;
	}
#ifdef USE_READPROGRESS
	if (tries - otries > 10) {
	    time_t t = time((time_t *) 0);

	    otries = tries;
	    if (t - otime >= 5) {
		otime = t;
		HTReadProgress(-1, 0);	/* Put "stalled" message */
	    }
	}
#endif

	/*
	 * If we suspend, then it is possible that select will be interrupted.
	 * Allow for this possibility.  - JED
	 */
	do {
	    select_timeout.tv_sec = 0;
	    select_timeout.tv_usec = 100000;
	    FD_ZERO(&readfds);
	    FD_SET((unsigned) fildes, &readfds);
#ifdef SOCKS
	    if (socks_flag)
		ret = Rselect((unsigned) fildes + 1,
			      &readfds, NULL, NULL, &select_timeout);
	    else
#endif /* SOCKS */
		ret = select((unsigned) fildes + 1,
			     &readfds, NULL, NULL, &select_timeout);
	} while ((ret == -1) && (errno == EINTR));

	if (ret < 0) {
	    return -1;
	} else if (ret > 0) {
	    ready = 1;
	} else if (HTCheckForInterrupt()) {
#ifdef _WINDOWS
	    WSASetLastError(EINTR);
#else
	    SOCKET_ERRNO = EINTR;
#endif
	    return HT_INTERRUPTED;
	}
    }

#if !defined(UCX) || !defined(VAXC)
#ifdef UNIX
    while ((nb = SOCKET_READ(fildes, buf, nbyte)) == -1) {
	if (errno == EINTR)
	    continue;
#ifdef ERESTARTSYS
	if (errno == ERESTARTSYS)
	    continue;
#endif /* ERESTARTSYS */
	HTInetStatus("read");
	break;
    }
    return nb;
#else /* UNIX */
    return SOCKET_READ(fildes, buf, nbyte);
#endif /* !UNIX */

#else /* UCX && VAXC */
    /*
     * VAXC and UCX problem only.
     */
    errno = vaxc$errno = 0;
    nb = SOCKET_READ(fildes, buf, nbyte);
    CTRACE((tfp,
	    "Read - nb,errno,vaxc$errno: %d %d %d\n", nb, errno, vaxc$errno));
    if ((nb <= 0) && TRACE)
	perror("HTTCP.C:HTDoRead:read");	/* RJF */
    /*
     * An errno value of EPIPE and nb < 0 indicates end-of-file on VAXC.
     */
    if ((nb <= 0) && (errno == EPIPE)) {
	nb = 0;
	set_errno(0);
    }
    return nb;
#endif /* UCX, BSN */
}

#ifdef SVR4_BSDSELECT
/*
 *  This is a fix for the difference between BSD's select() and
 *  SVR4's select().  SVR4's select() can never return a value larger
 *  than the total number of file descriptors being checked.  So, if
 *  you select for read and write on one file descriptor, and both
 *  are true, SVR4 select() will only return 1.  BSD select in the
 *  same situation will return 2.
 *
 *	Additionally, BSD select() on timing out, will zero the masks,
 *	while SVR4 does not.  This is fixed here as well.
 *
 *	Set your tabstops to 4 characters to have this code nicely formatted.
 *
 *	Jerry Whelan, guru@bradley.edu, June 12th, 1993
 */
#ifdef select
#undef select
#endif /* select */

#ifdef SOCKS
#ifdef Rselect
#undef Rselect
#endif /* Rselect */
#endif /* SOCKS */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>

int BSDselect(int nfds,
	      fd_set * readfds,
	      fd_set * writefds,
	      fd_set * exceptfds,
	      struct timeval *select_timeout)
{
    int rval, i;

#ifdef SOCKS
    if (socks_flag)
	rval = Rselect(nfds, readfds, writefds, exceptfds, select_timeout);
    else
#endif /* SOCKS */
	rval = select(nfds, readfds, writefds, exceptfds, select_timeout);

    switch (rval) {
    case -1:
	return (rval);

    case 0:
	if (readfds != NULL)
	    FD_ZERO(readfds);
	if (writefds != NULL)
	    FD_ZERO(writefds);
	if (exceptfds != NULL)
	    FD_ZERO(exceptfds);
	return (rval);

    default:
	for (i = 0, rval = 0; i < nfds; i++) {
	    if ((readfds != NULL) && FD_ISSET(i, readfds))
		rval++;
	    if ((writefds != NULL) && FD_ISSET(i, writefds))
		rval++;
	    if ((exceptfds != NULL) && FD_ISSET(i, exceptfds))
		rval++;

	}
	return (rval);
    }
/* Should never get here */
}
#endif /* SVR4_BSDSELECT */