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
|
/* net.cc: network-related routines.
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2005 Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
/* #define DEBUG_NEST_ON 1 */
#define __INSIDE_CYGWIN_NET__
#include "winsup.h"
#include <ctype.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <iphlpapi.h>
#include <stdlib.h>
#define gethostname cygwin_gethostname
#include <unistd.h>
#undef gethostname
#include <netdb.h>
#define USE_SYS_TYPES_FD_SET
#include <winsock2.h>
#include <assert.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "sigproc.h"
#include "pinfo.h"
#include "registry.h"
#include "cygtls.h"
extern "C"
{
int h_errno;
int __stdcall rcmd (char **ahost, unsigned short inport, char *locuser,
char *remuser, char *cmd, SOCKET * fd2p);
int __stdcall rexec (char **ahost, unsigned short inport, char *locuser,
char *password, char *cmd, SOCKET * fd2p);
int __stdcall rresvport (int *);
int sscanf (const char *, const char *, ...);
} /* End of "C" section */
WSADATA wsadata;
static fhandler_socket *
get (const int fd)
{
cygheap_fdget cfd (fd);
if (cfd < 0)
return 0;
fhandler_socket *const fh = cfd->is_socket ();
if (!fh)
set_errno (ENOTSOCK);
return fh;
}
static SOCKET __stdcall
set_socket_inheritance (SOCKET sock)
{
SOCKET osock = sock;
if (!DuplicateHandle (hMainProc, (HANDLE) sock, hMainProc, (HANDLE *) &sock,
0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
system_printf ("DuplicateHandle failed %E");
else
debug_printf ("DuplicateHandle succeeded osock %p, sock %p", osock, sock);
VerifyHandle ((HANDLE) sock);
return sock;
}
/* htonl: standards? */
extern "C" unsigned long int
htonl (unsigned long int x)
{
return ((((x & 0x000000ffU) << 24) |
((x & 0x0000ff00U) << 8) |
((x & 0x00ff0000U) >> 8) |
((x & 0xff000000U) >> 24)));
}
/* ntohl: standards? */
extern "C" unsigned long int
ntohl (unsigned long int x)
{
return htonl (x);
}
/* htons: standards? */
extern "C" unsigned short
htons (unsigned short x)
{
return ((((x & 0x000000ffU) << 8) |
((x & 0x0000ff00U) >> 8)));
}
/* ntohs: standards? */
extern "C" unsigned short
ntohs (unsigned short x)
{
return htons (x);
}
/* exported as inet_ntoa: BSD 4.3 */
extern "C" char *
cygwin_inet_ntoa (struct in_addr in)
{
char *res = inet_ntoa (in);
if (_my_tls.locals.ntoa_buf)
{
free (_my_tls.locals.ntoa_buf);
_my_tls.locals.ntoa_buf = NULL;
}
if (res)
_my_tls.locals.ntoa_buf = strdup (res);
return _my_tls.locals.ntoa_buf;
}
/* exported as inet_addr: BSD 4.3 */
extern "C" unsigned long
cygwin_inet_addr (const char *cp)
{
myfault efault;
if (efault.faulted (EFAULT))
return INADDR_NONE;
unsigned long res = inet_addr (cp);
return res;
}
/* exported as inet_aton: BSD 4.3
inet_aton is not exported by wsock32 and ws2_32,
so it has to be implemented here. */
extern "C" int
cygwin_inet_aton (const char *cp, struct in_addr *inp)
{
myfault efault;
if (efault.faulted (EFAULT))
return 0;
unsigned long res = inet_addr (cp);
if (res == INADDR_NONE && strcmp (cp, "255.255.255.255"))
return 0;
if (inp)
inp->s_addr = res;
return 1;
}
/* undocumented in wsock32.dll */
extern "C" unsigned int WINAPI inet_network (const char *);
extern "C" unsigned int
cygwin_inet_network (const char *cp)
{
myfault efault;
if (efault.faulted (EFAULT))
return INADDR_NONE;
unsigned int res = inet_network (cp);
return res;
}
/* inet_netof is in the standard BSD sockets library. It is useless
for modern networks, since it assumes network values which are no
longer meaningful, but some existing code calls it. */
extern "C" unsigned long
inet_netof (struct in_addr in)
{
unsigned long i, res;
i = ntohl (in.s_addr);
if (IN_CLASSA (i))
res = (i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT;
else if (IN_CLASSB (i))
res = (i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT;
else
res = (i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT;
return res;
}
/* inet_makeaddr is in the standard BSD sockets library. It is
useless for modern networks, since it assumes network values which
are no longer meaningful, but some existing code calls it. */
extern "C" struct in_addr
inet_makeaddr (int net, int lna)
{
unsigned long i;
struct in_addr in;
if (net < IN_CLASSA_MAX)
i = (net << IN_CLASSA_NSHIFT) | (lna & IN_CLASSA_HOST);
else if (net < IN_CLASSB_MAX)
i = (net << IN_CLASSB_NSHIFT) | (lna & IN_CLASSB_HOST);
else if (net < 0x1000000)
i = (net << IN_CLASSC_NSHIFT) | (lna & IN_CLASSC_HOST);
else
i = net | lna;
in.s_addr = htonl (i);
return in;
}
struct tl
{
int w;
const char *s;
int e;
};
static NO_COPY struct tl errmap[] = {
{WSAEINTR, "WSAEINTR", EINTR},
{WSAEWOULDBLOCK, "WSAEWOULDBLOCK", EWOULDBLOCK},
{WSAEINPROGRESS, "WSAEINPROGRESS", EINPROGRESS},
{WSAEALREADY, "WSAEALREADY", EALREADY},
{WSAENOTSOCK, "WSAENOTSOCK", ENOTSOCK},
{WSAEDESTADDRREQ, "WSAEDESTADDRREQ", EDESTADDRREQ},
{WSAEMSGSIZE, "WSAEMSGSIZE", EMSGSIZE},
{WSAEPROTOTYPE, "WSAEPROTOTYPE", EPROTOTYPE},
{WSAENOPROTOOPT, "WSAENOPROTOOPT", ENOPROTOOPT},
{WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT", EPROTONOSUPPORT},
{WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT", ESOCKTNOSUPPORT},
{WSAEOPNOTSUPP, "WSAEOPNOTSUPP", EOPNOTSUPP},
{WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT", EPFNOSUPPORT},
{WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT", EAFNOSUPPORT},
{WSAEADDRINUSE, "WSAEADDRINUSE", EADDRINUSE},
{WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL", EADDRNOTAVAIL},
{WSAENETDOWN, "WSAENETDOWN", ENETDOWN},
{WSAENETUNREACH, "WSAENETUNREACH", ENETUNREACH},
{WSAENETRESET, "WSAENETRESET", ENETRESET},
{WSAECONNABORTED, "WSAECONNABORTED", ECONNABORTED},
{WSAECONNRESET, "WSAECONNRESET", ECONNRESET},
{WSAENOBUFS, "WSAENOBUFS", ENOBUFS},
{WSAEISCONN, "WSAEISCONN", EISCONN},
{WSAENOTCONN, "WSAENOTCONN", ENOTCONN},
{WSAESHUTDOWN, "WSAESHUTDOWN", ESHUTDOWN},
{WSAETOOMANYREFS, "WSAETOOMANYREFS", ETOOMANYREFS},
{WSAETIMEDOUT, "WSAETIMEDOUT", ETIMEDOUT},
{WSAECONNREFUSED, "WSAECONNREFUSED", ECONNREFUSED},
{WSAELOOP, "WSAELOOP", ELOOP},
{WSAENAMETOOLONG, "WSAENAMETOOLONG", ENAMETOOLONG},
{WSAEHOSTDOWN, "WSAEHOSTDOWN", EHOSTDOWN},
{WSAEHOSTUNREACH, "WSAEHOSTUNREACH", EHOSTUNREACH},
{WSAENOTEMPTY, "WSAENOTEMPTY", ENOTEMPTY},
{WSAEPROCLIM, "WSAEPROCLIM", EPROCLIM},
{WSAEUSERS, "WSAEUSERS", EUSERS},
{WSAEDQUOT, "WSAEDQUOT", EDQUOT},
{WSAESTALE, "WSAESTALE", ESTALE},
{WSAEREMOTE, "WSAEREMOTE", EREMOTE},
{WSAEINVAL, "WSAEINVAL", EINVAL},
{WSAEFAULT, "WSAEFAULT", EFAULT},
{0, "NOERROR", 0},
{0, NULL, 0}
};
static int
find_winsock_errno (int why)
{
for (int i = 0; errmap[i].s != NULL; ++i)
if (why == errmap[i].w)
return errmap[i].e;
return EPERM;
}
void
__set_winsock_errno (const char *fn, int ln)
{
DWORD werr = WSAGetLastError ();
int err = find_winsock_errno (werr);
set_errno (err);
syscall_printf ("%s:%d - winsock error %d -> errno %d", fn, ln, werr, err);
}
/*
* Since the member `s' isn't used for debug output we can use it
* for the error text returned by herror and hstrerror.
*/
static NO_COPY struct tl host_errmap[] = {
{WSAHOST_NOT_FOUND, "Unknown host", HOST_NOT_FOUND},
{WSATRY_AGAIN, "Host name lookup failure", TRY_AGAIN},
{WSANO_RECOVERY, "Unknown server error", NO_RECOVERY},
{WSANO_DATA, "No address associated with name", NO_DATA},
{0, NULL, 0}
};
static void
set_host_errno ()
{
int i;
int why = WSAGetLastError ();
for (i = 0; host_errmap[i].w != 0; ++i)
if (why == host_errmap[i].w)
break;
if (host_errmap[i].w != 0)
h_errno = host_errmap[i].e;
else
h_errno = NETDB_INTERNAL;
}
inline int
DWORD_round (int n)
{
return sizeof (DWORD) * (((n + sizeof (DWORD) - 1)) / sizeof (DWORD));
}
inline int
strlen_round (const char *s)
{
if (!s)
return 0;
return DWORD_round (strlen (s) + 1);
}
#pragma pack(push,2)
struct pservent
{
char *s_name;
char **s_aliases;
short s_port;
char *s_proto;
};
#pragma pack(pop)
struct unionent
{
char *name;
char **list;
short port_proto_addrtype;
short h_len;
union
{
char *s_proto;
char **h_addr_list;
};
};
enum struct_type
{
t_hostent, t_protoent, t_servent
};
static const char *entnames[] = {"host", "proto", "serv"};
/* Generic "dup a {host,proto,serv}ent structure" function.
This is complicated because we need to be able to free the
structure at any point and we can't rely on the pointer contents
being untouched by callers. So, we allocate a chunk of memory
large enough to hold the structure and all of the stuff it points
to then we copy the source into this new block of memory.
The 'unionent' struct is a union of all of the currently used
*ent structure. */
#define dup_ent(old, src, type) __dup_ent ((unionent *&) (_my_tls.locals.old), (unionent *) (src), type)
#ifdef DEBUGGING
static void *
#else
static inline void *
#endif
__dup_ent (unionent *&dst, unionent *src, struct_type type)
{
if (dst)
debug_printf ("old %sent structure \"%s\" %p\n", entnames[type],
((unionent *) dst)->name, dst);
if (!src)
{
set_winsock_errno ();
return NULL;
}
debug_printf ("duping %sent \"%s\", %p", entnames[type], src->name, src);
/* Find the size of the raw structure minus any character strings, etc. */
int sz, struct_sz;
switch (type)
{
case t_protoent:
struct_sz = sizeof (protoent);
break;
case t_servent:
struct_sz = sizeof (servent);
break;
case t_hostent:
struct_sz = sizeof (hostent);
break;
default:
api_fatal ("called with invalid value %d", type);
break;
}
/* Every *ent begins with a name. Calculate it's length. */
int namelen = strlen_round (src->name);
sz = struct_sz + namelen;
char **av;
/* The next field in every *ent is an argv list of "something".
Calculate the number of components and how much space the
character strings will take. */
int list_len = 0;
for (av = src->list; av && *av; av++)
{
list_len++;
sz += sizeof (char **) + strlen_round (*av);
}
/* NULL terminate if there actually was a list */
if (av)
{
sz += sizeof (char **);
list_len++;
}
/* Do servent/hostent specific processing */
int protolen = 0;
int addr_list_len = 0;
char *s_proto = NULL;
if (type == t_servent)
{
if (src->s_proto)
{
/* Windows 95 idiocy. Structure is misaligned on Windows 95.
Kludge around this by trying a different pointer alignment. */
if (!IsBadStringPtr (src->s_proto, INT32_MAX))
s_proto = src->s_proto;
else if (!IsBadStringPtr (((pservent *) src)->s_proto, INT32_MAX))
s_proto = ((pservent *) src)->s_proto;
sz += (protolen = strlen_round (s_proto));
}
}
else if (type == t_hostent)
{
/* Calculate the length and storage used for h_addr_list */
for (av = src->h_addr_list; av && *av; av++)
{
addr_list_len++;
sz += sizeof (char **) + DWORD_round (src->h_len);
}
if (av)
{
sz += sizeof (char **);
addr_list_len++;
}
}
/* Allocate the storage needed. Allocate a rounded size to attempt to force
reuse of this buffer so that a poorly-written caller will not be using
a freed buffer. */
unsigned rsz = 256 * ((sz + 255) / 256);
dst = (unionent *) realloc (dst, rsz);
/* Hopefully, this worked. */
if (dst)
{
memset (dst, 0, sz);
/* This field is common to all *ent structures but named differently
in each, of course. */
dst->port_proto_addrtype = src->port_proto_addrtype;
char *dp = ((char *) dst) + struct_sz;
if (namelen)
{
/* Copy the name field to dst, using space just beyond the end of
the dst structure. */
strcpy (dst->name = dp, src->name);
dp += namelen;
}
/* Copy the 'list' type to dst, using space beyond end of structure
+ storage for name. */
if (src->list)
{
char **dav = dst->list = (char **) dp;
dp += sizeof (char **) * list_len;
for (av = src->list; av && *av; av++)
{
int len = strlen (*av) + 1;
memcpy (*dav++ = dp, *av, len);
dp += DWORD_round (len);
}
}
/* Do servent/protoent/hostent specific processing. */
if (type == t_protoent)
debug_printf ("protoent %s %x %x", dst->name, dst->list, dst->port_proto_addrtype);
else if (type == t_servent)
{
if (s_proto)
{
strcpy (dst->s_proto = dp, s_proto);
dp += protolen;
}
}
else if (type == t_hostent)
{
/* Transfer h_len and duplicate contents of h_addr_list, using
memory after 'list' allocation. */
dst->h_len = src->h_len;
char **dav = dst->h_addr_list = (char **) dp;
dp += sizeof (char **) * addr_list_len;
for (av = src->h_addr_list; av && *av; av++)
{
memcpy (*dav++ = dp, *av, src->h_len);
dp += DWORD_round (src->h_len);
}
}
/* Sanity check that we did our bookkeeping correctly. */
assert ((dp - (char *) dst) == sz);
}
debug_printf ("duped %sent \"%s\", %p", entnames[type], dst ? dst->name : "<null!>", dst);
return dst;
}
/* exported as getprotobyname: standards? */
extern "C" struct protoent *
cygwin_getprotobyname (const char *p)
{
myfault efault;
if (efault.faulted (EFAULT))
return NULL;
return (protoent *) dup_ent (protoent_buf, getprotobyname (p), t_protoent);
}
/* exported as getprotobynumber: standards? */
extern "C" struct protoent *
cygwin_getprotobynumber (int number)
{
return (protoent *) dup_ent (protoent_buf, getprotobynumber (number), t_protoent);
}
bool
fdsock (cygheap_fdmanip& fd, const device *dev, SOCKET soc)
{
if (!winsock2_active)
soc = set_socket_inheritance (soc);
else if (wincap.has_set_handle_information ())
{
/* NT systems apparently set sockets to inheritable by default */
SetHandleInformation ((HANDLE) soc, HANDLE_FLAG_INHERIT, 0);
debug_printf ("reset socket inheritance since winsock2_active %d",
winsock2_active);
}
else
debug_printf ("not setting socket inheritance since winsock2_active %d",
winsock2_active);
fd = build_fh_dev (*dev);
if (!fd.isopen ())
return false;
fd->set_io_handle ((HANDLE) soc);
fd->set_flags (O_RDWR | O_BINARY);
fd->uninterruptible_io (winsock2_active);
cygheap->fdtab.inc_need_fixup_before ();
debug_printf ("fd %d, name '%s', soc %p", (int) fd, dev->name, soc);
return true;
}
/* exported as socket: standards? */
extern "C" int
cygwin_socket (int af, int type, int protocol)
{
int res = -1;
SOCKET soc = 0;
debug_printf ("socket (%d, %d, %d)", af, type, protocol);
soc = socket (AF_INET, type, af == AF_LOCAL ? 0 : protocol);
if (soc == INVALID_SOCKET)
{
set_winsock_errno ();
goto done;
}
const device *dev;
if (af == AF_INET)
dev = type == SOCK_STREAM ? tcp_dev : udp_dev;
else
dev = type == SOCK_STREAM ? stream_dev : dgram_dev;
{
cygheap_fdnew fd;
if (fd < 0 || !fdsock (fd, dev, soc))
closesocket (soc);
else
{
((fhandler_socket *) fd)->set_addr_family (af);
((fhandler_socket *) fd)->set_socket_type (type);
res = fd;
}
}
done:
syscall_printf ("%d = socket (%d, %d, %d)", res, af, type, protocol);
return res;
}
/* exported as sendto: standards? */
extern "C" int
cygwin_sendto (int fd, const void *buf, int len, int flags,
const struct sockaddr *to, int tolen)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else if ((res = len) != 0)
res = fh->sendto (buf, len, flags, to, tolen);
syscall_printf ("%d = sendto (%d, %p, %d, %x, %p, %d)",
res, fd, buf, len, flags, to, tolen);
return res;
}
/* exported as recvfrom: standards? */
extern "C" int
cygwin_recvfrom (int fd, void *buf, int len, int flags,
struct sockaddr *from, int *fromlen)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else if ((res = len) != 0)
res = fh->recvfrom (buf, len, flags, from, fromlen);
syscall_printf ("%d = recvfrom (%d, %p, %d, %x, %p, %p)",
res, fd, buf, len, flags, from, fromlen);
return res;
}
/* exported as setsockopt: standards? */
extern "C" int
cygwin_setsockopt (int fd, int level, int optname, const void *optval,
int optlen)
{
int res;
fhandler_socket *fh = get (fd);
const char *name = "error";
/* For the following debug_printf */
switch (optname)
{
case SO_DEBUG:
name = "SO_DEBUG";
break;
case SO_ACCEPTCONN:
name = "SO_ACCEPTCONN";
break;
case SO_REUSEADDR:
name = "SO_REUSEADDR";
break;
case SO_KEEPALIVE:
name = "SO_KEEPALIVE";
break;
case SO_DONTROUTE:
name = "SO_DONTROUTE";
break;
case SO_BROADCAST:
name = "SO_BROADCAST";
break;
case SO_USELOOPBACK:
name = "SO_USELOOPBACK";
break;
case SO_LINGER:
name = "SO_LINGER";
break;
case SO_OOBINLINE:
name = "SO_OOBINLINE";
break;
case SO_ERROR:
name = "SO_ERROR";
break;
}
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
{
res = setsockopt (fh->get_socket (), level, optname,
(const char *) optval, optlen);
if (optlen == 4)
syscall_printf ("setsockopt optval=%x", *(long *) optval);
if (res)
set_winsock_errno ();
}
syscall_printf ("%d = setsockopt (%d, %d, %x (%s), %p, %d)",
res, fd, level, optname, name, optval, optlen);
return res;
}
/* exported as getsockopt: standards? */
extern "C" int
cygwin_getsockopt (int fd, int level, int optname, void *optval, int *optlen)
{
int res;
fhandler_socket *fh = get (fd);
const char *name = "error";
/* For the following debug_printf */
switch (optname)
{
case SO_DEBUG:
name = "SO_DEBUG";
break;
case SO_ACCEPTCONN:
name = "SO_ACCEPTCONN";
break;
case SO_REUSEADDR:
name = "SO_REUSEADDR";
break;
case SO_KEEPALIVE:
name = "SO_KEEPALIVE";
break;
case SO_DONTROUTE:
name = "SO_DONTROUTE";
break;
case SO_BROADCAST:
name = "SO_BROADCAST";
break;
case SO_USELOOPBACK:
name = "SO_USELOOPBACK";
break;
case SO_LINGER:
name = "SO_LINGER";
break;
case SO_OOBINLINE:
name = "SO_OOBINLINE";
break;
case SO_ERROR:
name = "SO_ERROR";
break;
case SO_PEERCRED:
name = "SO_PEERCRED";
}
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else if (optname == SO_PEERCRED)
{
struct ucred *cred = (struct ucred *) optval;
res = fh->getpeereid (&cred->pid, &cred->uid, &cred->gid);
}
else
{
res = getsockopt (fh->get_socket (), level, optname, (char *) optval,
(int *) optlen);
if (optname == SO_ERROR)
{
int *e = (int *) optval;
debug_printf ("WinSock SO_ERROR = %d", *e);
*e = find_winsock_errno (*e);
}
if (res)
set_winsock_errno ();
}
syscall_printf ("%d = getsockopt (%d, %d, %x (%s), %p, %p)",
res, fd, level, optname, name, optval, optlen);
return res;
}
extern "C" int
getpeereid (int fd, __uid32_t *euid, __gid32_t *egid)
{
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
if (fh)
return fh->getpeereid (NULL, euid, egid);
return -1;
}
/* exported as connect: standards? */
extern "C" int
cygwin_connect (int fd, const struct sockaddr *name, int namelen)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
{
bool was_blocking = false;
if (!fh->is_nonblocking ())
{
int nonblocking = 1;
fh->ioctl (FIONBIO, &nonblocking);
was_blocking = true;
}
res = fh->connect (name, namelen);
if (was_blocking)
{
if (res == -1 && get_errno () == EINPROGRESS)
{
size_t fds_size = howmany (fd + 1, NFDBITS) * sizeof (fd_mask);
fd_set *write_fds = (fd_set *) alloca (fds_size);
fd_set *except_fds = (fd_set *) alloca (fds_size);
memset (write_fds, 0, fds_size);
memset (except_fds, 0, fds_size);
FD_SET (fd, write_fds);
FD_SET (fd, except_fds);
res = cygwin_select (fd + 1, NULL, write_fds, except_fds, NULL);
if (res > 0 && FD_ISSET (fd, except_fds))
{
res = -1;
for (;;)
{
int err;
int len = sizeof err;
cygwin_getsockopt (fd, SOL_SOCKET, SO_ERROR,
(void *) &err, &len);
if (err)
{
set_errno (err);
break;
}
low_priority_sleep (0);
}
}
else if (res > 0)
res = 0;
else
{
WSASetLastError (WSAEINPROGRESS);
set_winsock_errno ();
}
}
int nonblocking = 0;
fh->ioctl (FIONBIO, &nonblocking);
}
}
syscall_printf ("%d = connect (%d, %p, %d)", res, fd, name, namelen);
return res;
}
/* exported as getservbyname: standards? */
extern "C" struct servent *
cygwin_getservbyname (const char *name, const char *proto)
{
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return NULL;
servent *res = (servent *) dup_ent (servent_buf, getservbyname (name, proto), t_servent);
syscall_printf ("%p = getservbyname (%s, %s)", res, name, proto);
return res;
}
/* exported as getservbyport: standards? */
extern "C" struct servent *
cygwin_getservbyport (int port, const char *proto)
{
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return NULL;
servent *res = (servent *) dup_ent (servent_buf, getservbyport (port, proto), t_servent);
syscall_printf ("%p = getservbyport (%d, %s)", _my_tls.locals.servent_buf, port, proto);
return res;
}
extern "C" int
cygwin_gethostname (char *name, size_t len)
{
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return -1;
if (gethostname (name, len))
{
DWORD local_len = len;
if (!GetComputerNameA (name, &local_len))
{
set_winsock_errno ();
return -1;
}
}
debug_printf ("name %s", name);
return 0;
}
/* exported as gethostbyname: standards? */
extern "C" struct hostent *
cygwin_gethostbyname (const char *name)
{
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
{ console_printf ("OUCH!\n");
return NULL;
}
unsigned char tmp_addr[4];
struct hostent tmp, *h;
char *tmp_aliases[1] = {0};
char *tmp_addr_list[2] = {0,0};
unsigned int a, b, c, d;
char dummy;
if (sscanf (name, "%u.%u.%u.%u%c", &a, &b, &c, &d, &dummy) != 4
|| a >= 256 || b >= 256 || c >= 256 || d >= 256)
h = gethostbyname (name);
else
{
/* In case you don't have DNS, at least x.x.x.x still works */
memset (&tmp, 0, sizeof (tmp));
tmp_addr[0] = a;
tmp_addr[1] = b;
tmp_addr[2] = c;
tmp_addr[3] = d;
tmp_addr_list[0] = (char *) tmp_addr;
tmp.h_name = name;
tmp.h_aliases = tmp_aliases;
tmp.h_addrtype = 2;
tmp.h_length = 4;
tmp.h_addr_list = tmp_addr_list;
h = &tmp;
}
hostent *res = (hostent *) dup_ent (hostent_buf, h, t_hostent);
if (res)
{
debug_printf ("h_name %s", res->h_name);
debug_printf ("HERE");
}
else
{
debug_printf ("dup_ent returned NULL for name %s, h %p", name, h);
set_host_errno ();
}
return res;
}
/* exported as gethostbyaddr: standards? */
extern "C" struct hostent *
cygwin_gethostbyaddr (const char *addr, int len, int type)
{
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return NULL;
hostent *res = (hostent *) dup_ent (hostent_buf, gethostbyaddr (addr, len, type), t_hostent);
if (res)
debug_printf ("h_name %s", _my_tls.locals.hostent_buf->h_name);
else
set_host_errno ();
return res;
}
/* exported as accept: standards? */
extern "C" int
cygwin_accept (int fd, struct sockaddr *peer, int *len)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
{
if (!fh->is_nonblocking ())
{
size_t fds_size = howmany (fd + 1, NFDBITS) * sizeof (fd_mask);
fd_set *read_fds = (fd_set *) alloca (fds_size);
memset (read_fds, 0, fds_size);
FD_SET (fd, read_fds);
res = cygwin_select (fd + 1, read_fds, NULL, NULL, NULL);
if (res == -1)
return -1;
}
res = fh->accept (peer, len);
}
syscall_printf ("%d = accept (%d, %p, %p)", res, fd, peer, len);
return res;
}
/* exported as bind: standards? */
extern "C" int
cygwin_bind (int fd, const struct sockaddr *my_addr, int addrlen)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
res = fh->bind (my_addr, addrlen);
syscall_printf ("%d = bind (%d, %p, %d)", res, fd, my_addr, addrlen);
return res;
}
/* exported as getsockname: standards? */
extern "C" int
cygwin_getsockname (int fd, struct sockaddr *addr, int *namelen)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
res = fh->getsockname (addr, namelen);
syscall_printf ("%d = getsockname (%d, %p, %p)", res, fd, addr, namelen);
return res;
}
/* exported as listen: standards? */
extern "C" int
cygwin_listen (int fd, int backlog)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
if (!fh)
res = -1;
else
res = fh->listen (backlog);
syscall_printf ("%d = listen (%d, %d)", res, fd, backlog);
return res;
}
/* exported as shutdown: standards? */
extern "C" int
cygwin_shutdown (int fd, int how)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
if (!fh)
res = -1;
else
res = fh->shutdown (how);
syscall_printf ("%d = shutdown (%d, %d)", res, fd, how);
return res;
}
/* exported as hstrerror: BSD 4.3 */
extern "C" const char *
cygwin_hstrerror (int err)
{
int i;
for (i = 0; host_errmap[i].e != 0; ++i)
if (err == host_errmap[i].e)
break;
return host_errmap[i].s;
}
/* exported as herror: BSD 4.3 */
extern "C" void
cygwin_herror (const char *s)
{
myfault efault;
if (efault.faulted ())
return;
if (cygheap->fdtab.not_open (2))
return;
if (s)
{
write (2, s, strlen (s));
write (2, ": ", 2);
}
const char *h_errstr = cygwin_hstrerror (h_errno);
if (!h_errstr)
switch (h_errno)
{
case NETDB_INTERNAL:
h_errstr = "Resolver internal error";
break;
case NETDB_SUCCESS:
h_errstr = "Resolver error 0 (no error)";
break;
default:
h_errstr = "Unknown resolver error";
break;
}
write (2, h_errstr, strlen (h_errstr));
write (2, "\n", 1);
}
/* exported as getpeername: standards? */
extern "C" int
cygwin_getpeername (int fd, struct sockaddr *name, int *len)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
res = fh->getpeername (name, len);
syscall_printf ("%d = getpeername (%d) %d", res, fd, (fh ? fh->get_socket () : -1));
return res;
}
/* exported as recv: standards? */
extern "C" int
cygwin_recv (int fd, void *buf, int len, int flags)
{
return cygwin_recvfrom (fd, buf, len, flags, NULL, NULL);
}
/* exported as send: standards? */
extern "C" int
cygwin_send (int fd, const void *buf, int len, int flags)
{
return cygwin_sendto (fd, buf, len, flags, NULL, 0);
}
/* getdomainname: standards? */
extern "C" int
getdomainname (char *domain, size_t len)
{
/*
* This works for Win95 only if the machine is configured to use MS-TCP.
* If a third-party TCP is being used this will fail.
* FIXME: On Win95, is there a way to portably check the TCP stack
* in use and include paths for the Domain name in each ?
* Punt for now and assume MS-TCP on Win95.
*/
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return -1;
PFIXED_INFO info = NULL;
ULONG size = 0;
if (GetNetworkParams(info, &size) == ERROR_BUFFER_OVERFLOW
&& (info = (PFIXED_INFO) alloca(size))
&& GetNetworkParams(info, &size) == ERROR_SUCCESS)
{
strncpy(domain, info->DomainName, len);
return 0;
}
/* This is only used by Win95 and NT <= 4.0.
The registry names are language independent.
FIXME: Handle DHCP on Win95. The DhcpDomain(s) may be available
in ..VxD\DHCP\DhcpInfoXX\OptionInfo, RFC 1533 format */
reg_key r (HKEY_LOCAL_MACHINE, KEY_READ,
(!wincap.is_winnt ()) ? "System" : "SYSTEM",
"CurrentControlSet", "Services",
(!wincap.is_winnt ()) ? "VxD" : "Tcpip",
(!wincap.is_winnt ()) ? "MSTCP" : "Parameters", NULL);
if (!r.error ())
{
int res1, res2 = 0; /* Suppress compiler warning */
res1 = r.get_string ("Domain", domain, len, "");
if (res1 != ERROR_SUCCESS || !domain[0])
res2 = r.get_string ("DhcpDomain", domain, len, "");
if (res1 == ERROR_SUCCESS || res2 == ERROR_SUCCESS)
return 0;
}
__seterrno ();
return -1;
}
/* Fill out an ifconf struct. */
/*
* IFCONF 98/ME, NTSP4, W2K:
* Use IP Helper Library
*/
static void
get_2k_ifconf (struct ifconf *ifc, int what)
{
int cnt = 0;
int ethId = 0, pppId = 0, slpId = 0, tokId = 0;
/* Union maps buffer to correct struct */
struct ifreq *ifr = ifc->ifc_req;
DWORD ip_cnt, lip, lnp;
DWORD siz_ip_table = 0;
PMIB_IPADDRTABLE ipt;
PMIB_IFROW ifrow;
struct sockaddr_in *sa = NULL;
struct sockaddr *so = NULL;
typedef struct ifcount_t
{
DWORD ifIndex;
size_t count;
unsigned int enumerated; // for eth0:1
unsigned int classId; // for eth0, tok0 ...
};
ifcount_t *iflist, *ifEntry;
if (GetIpAddrTable (NULL, &siz_ip_table, TRUE) == ERROR_INSUFFICIENT_BUFFER
&& (ifrow = (PMIB_IFROW) alloca (sizeof (MIB_IFROW)))
&& (ipt = (PMIB_IPADDRTABLE) alloca (siz_ip_table))
&& !GetIpAddrTable (ipt, &siz_ip_table, TRUE))
{
iflist =
(ifcount_t *) alloca (sizeof (ifcount_t) * (ipt->dwNumEntries + 1));
memset (iflist, 0, sizeof (ifcount_t) * (ipt->dwNumEntries + 1));
for (ip_cnt = 0; ip_cnt < ipt->dwNumEntries; ++ip_cnt)
{
ifEntry = iflist;
/* search for matching entry (and stop at first free entry) */
while (ifEntry->count != 0)
{
if (ifEntry->ifIndex == ipt->table[ip_cnt].dwIndex)
break;
ifEntry++;
}
if (ifEntry->count == 0)
{
ifEntry->count = 1;
ifEntry->ifIndex = ipt->table[ip_cnt].dwIndex;
}
else
{
ifEntry->count++;
}
}
// reset the last element. This is just the stopper for the loop.
iflist[ipt->dwNumEntries].count = 0;
/* Iterate over all configured IP-addresses */
for (ip_cnt = 0; ip_cnt < ipt->dwNumEntries; ++ip_cnt)
{
memset (ifrow, 0, sizeof (MIB_IFROW));
ifrow->dwIndex = ipt->table[ip_cnt].dwIndex;
if (GetIfEntry (ifrow) != NO_ERROR)
continue;
ifcount_t *ifEntry = iflist;
/* search for matching entry (and stop at first free entry) */
while (ifEntry->count != 0)
{
if (ifEntry->ifIndex == ipt->table[ip_cnt].dwIndex)
break;
ifEntry++;
}
/* Setup the interface name */
switch (ifrow->dwType)
{
case MIB_IF_TYPE_TOKENRING:
if (ifEntry->enumerated == 0)
{
ifEntry->classId = tokId++;
__small_sprintf (ifr->ifr_name, "tok%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "tok%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_ETHERNET:
if (ifEntry->enumerated == 0)
{
ifEntry->classId = ethId++;
__small_sprintf (ifr->ifr_name, "eth%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "eth%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_PPP:
if (ifEntry->enumerated == 0)
{
ifEntry->classId = pppId++;
__small_sprintf (ifr->ifr_name, "ppp%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "ppp%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_SLIP:
if (ifEntry->enumerated == 0)
{
ifEntry->classId = slpId++;
__small_sprintf (ifr->ifr_name, "slp%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "slp%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_LOOPBACK:
strcpy (ifr->ifr_name, "lo");
break;
default:
continue;
}
/* setup sockaddr struct */
switch (what)
{
case SIOCGIFCONF:
case SIOCGIFADDR:
sa = (struct sockaddr_in *) &ifr->ifr_addr;
sa->sin_addr.s_addr = ipt->table[ip_cnt].dwAddr;
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFBRDADDR:
sa = (struct sockaddr_in *) &ifr->ifr_broadaddr;
#if 0
/* Unfortunately, the field returns only crap. */
sa->sin_addr.s_addr = ipt->table[ip_cnt].dwBCastAddr;
#else
lip = ipt->table[ip_cnt].dwAddr;
lnp = ipt->table[ip_cnt].dwMask;
sa->sin_addr.s_addr = lip & lnp | ~lnp;
sa->sin_family = AF_INET;
sa->sin_port = 0;
#endif
break;
case SIOCGIFNETMASK:
sa = (struct sockaddr_in *) &ifr->ifr_netmask;
sa->sin_addr.s_addr = ipt->table[ip_cnt].dwMask;
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFHWADDR:
so = &ifr->ifr_hwaddr;
for (UINT i = 0; i < IFHWADDRLEN; ++i)
if (i >= ifrow->dwPhysAddrLen)
so->sa_data[i] = '\0';
else
so->sa_data[i] = ifrow->bPhysAddr[i];
so->sa_family = AF_INET;
break;
case SIOCGIFMETRIC:
ifr->ifr_metric = 1;
break;
case SIOCGIFMTU:
ifr->ifr_mtu = ifrow->dwMtu;
break;
}
++cnt;
if ((caddr_t)++ ifr >
ifc->ifc_buf + ifc->ifc_len - sizeof (struct ifreq))
goto done;
}
}
done:
/* Set the correct length */
ifc->ifc_len = cnt * sizeof (struct ifreq);
}
/*
* IFCONF Windows NT < SP4:
* Look at the Bind value in
* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage\
* This is a REG_MULTI_SZ with strings of the form:
* \Device\<Netcard>, where netcard is the name of the net device.
* Then look under:
* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<NetCard>\
* Parameters\Tcpip
* at the IPAddress, Subnetmask and DefaultGateway values for the
* required values.
*/
static void
get_nt_ifconf (struct ifconf *ifc, int what)
{
HKEY key;
unsigned long lip, lnp;
struct sockaddr_in *sa = NULL;
struct sockaddr *so = NULL;
DWORD size;
int cnt = 1;
char *binding = (char *) 0;
/* Union maps buffer to correct struct */
struct ifreq *ifr = ifc->ifc_req;
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"SYSTEM\\"
"CurrentControlSet\\"
"Services\\"
"Tcpip\\" "Linkage",
0, KEY_READ, &key) == ERROR_SUCCESS)
{
if (RegQueryValueEx (key, "Bind",
NULL, NULL,
NULL, &size) == ERROR_SUCCESS)
{
binding = (char *) alloca (size);
if (RegQueryValueEx (key, "Bind",
NULL, NULL,
(unsigned char *) binding,
&size) != ERROR_SUCCESS)
{
binding = NULL;
}
}
RegCloseKey (key);
}
if (binding)
{
char *bp, eth[2] = "/";
char cardkey[256], ipaddress[256], netmask[256];
for (bp = binding; *bp; bp += strlen (bp) + 1)
{
bp += strlen ("\\Device\\");
strcpy (cardkey, "SYSTEM\\CurrentControlSet\\Services\\");
strcat (cardkey, bp);
strcat (cardkey, "\\Parameters\\Tcpip");
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, cardkey,
0, KEY_READ, &key) != ERROR_SUCCESS)
continue;
if (RegQueryValueEx (key, "IPAddress",
NULL, NULL,
(unsigned char *) ipaddress,
(size = 256, &size)) == ERROR_SUCCESS
&& RegQueryValueEx (key, "SubnetMask",
NULL, NULL,
(unsigned char *) netmask,
(size = 256, &size)) == ERROR_SUCCESS)
{
char *ip, *np;
char dhcpaddress[256], dhcpnetmask[256];
for (ip = ipaddress, np = netmask;
*ip && *np;
ip += strlen (ip) + 1, np += strlen (np) + 1)
{
if ((caddr_t)++ ifr > ifc->ifc_buf
+ ifc->ifc_len - sizeof (struct ifreq))
break;
if (!strncmp (bp, "NdisWan", 7))
{
strcpy (ifr->ifr_name, "ppp");
strcat (ifr->ifr_name, bp + 7);
}
else
{
++*eth;
strcpy (ifr->ifr_name, "eth");
strcat (ifr->ifr_name, eth);
}
memset (&ifr->ifr_addr, '\0', sizeof ifr->ifr_addr);
if (cygwin_inet_addr (ip) == 0L
&& RegQueryValueEx (key, "DhcpIPAddress",
NULL, NULL,
(unsigned char *) dhcpaddress,
(size = 256, &size))
== ERROR_SUCCESS
&& RegQueryValueEx (key, "DhcpSubnetMask",
NULL, NULL,
(unsigned char *) dhcpnetmask,
(size = 256, &size))
== ERROR_SUCCESS)
{
switch (what)
{
case SIOCGIFCONF:
case SIOCGIFADDR:
sa = (struct sockaddr_in *) &ifr->ifr_addr;
sa->sin_addr.s_addr =
cygwin_inet_addr (dhcpaddress);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFBRDADDR:
lip = cygwin_inet_addr (dhcpaddress);
lnp = cygwin_inet_addr (dhcpnetmask);
sa = (struct sockaddr_in *) &ifr->ifr_broadaddr;
sa->sin_addr.s_addr = lip & lnp | ~lnp;
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFNETMASK:
sa = (struct sockaddr_in *) &ifr->ifr_netmask;
sa->sin_addr.s_addr =
cygwin_inet_addr (dhcpnetmask);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFHWADDR:
so = &ifr->ifr_hwaddr;
memset (so->sa_data, 0, IFHWADDRLEN);
so->sa_family = AF_INET;
break;
case SIOCGIFMETRIC:
ifr->ifr_metric = 1;
break;
case SIOCGIFMTU:
ifr->ifr_mtu = 1500;
break;
}
}
else
{
switch (what)
{
case SIOCGIFCONF:
case SIOCGIFADDR:
sa = (struct sockaddr_in *) &ifr->ifr_addr;
sa->sin_addr.s_addr = cygwin_inet_addr (ip);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFBRDADDR:
lip = cygwin_inet_addr (ip);
lnp = cygwin_inet_addr (np);
sa = (struct sockaddr_in *) &ifr->ifr_broadaddr;
sa->sin_addr.s_addr = lip & lnp | ~lnp;
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFNETMASK:
sa = (struct sockaddr_in *) &ifr->ifr_netmask;
sa->sin_addr.s_addr = cygwin_inet_addr (np);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFHWADDR:
so = &ifr->ifr_hwaddr;
memset (so->sa_data, 0, IFHWADDRLEN);
so->sa_family = AF_INET;
break;
case SIOCGIFMETRIC:
ifr->ifr_metric = 1;
break;
case SIOCGIFMTU:
ifr->ifr_mtu = 1500;
break;
}
}
++cnt;
}
}
RegCloseKey (key);
}
}
/* Set the correct length */
ifc->ifc_len = cnt * sizeof (struct ifreq);
}
/*
* IFCONF Windows 95:
* HKLM/Enum/Network/MSTCP/"*"
* -> Value "Driver" enthält Subkey relativ zu
* HKLM/System/CurrentControlSet/Class/
* -> In Subkey "Bindings" die Values aufzählen
* -> Enthält Subkeys der Form "VREDIR\*"
* Das * ist ein Subkey relativ zu
* HKLM/System/CurrentControlSet/Class/Net/
* HKLM/System/CurrentControlSet/Class/"Driver"
* -> Value "IPAddress"
* -> Value "IPMask"
* HKLM/System/CurrentControlSet/Class/Net/"*"(aus "VREDIR\*")
* -> Wenn Value "AdapterName" == "MS$PPP" -> ppp interface
* -> Value "DriverDesc" enthält den Namen
*
*/
static void
get_95_ifconf (struct ifconf *ifc, int what)
{
HKEY key;
unsigned long lip, lnp;
struct sockaddr_in *sa = NULL;
struct sockaddr *so = NULL;
FILETIME update;
LONG res;
DWORD size;
int cnt = 1;
char ifname[256];
char eth[2] = "/";
char ppp[2] = "/";
/* Union maps buffer to correct struct */
struct ifreq *ifr = ifc->ifc_req;
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Enum\\Network\\MSTCP",
0, KEY_READ, &key) != ERROR_SUCCESS)
{
/* Set the correct length */
ifc->ifc_len = cnt * sizeof (struct ifreq);
return;
}
for (int i = 0;
(res = RegEnumKeyEx (key, i, ifname,
(size = sizeof ifname, &size),
0, 0, 0, &update)) != ERROR_NO_MORE_ITEMS;
++i)
{
HKEY ifkey, subkey;
char driver[256], classname[256], netname[256];
char adapter[256], ip[256], np[256];
if (res != ERROR_SUCCESS
|| RegOpenKeyEx (key, ifname, 0, KEY_READ, &ifkey) != ERROR_SUCCESS)
continue;
if (RegQueryValueEx (ifkey, "Driver", 0,
NULL, (unsigned char *) driver,
(size = sizeof driver, &size)) != ERROR_SUCCESS)
{
RegCloseKey (ifkey);
continue;
}
strcpy (classname, "System\\CurrentControlSet\\Services\\Class\\");
strcat (classname, driver);
if ((res = RegOpenKeyEx (HKEY_LOCAL_MACHINE, classname,
0, KEY_READ, &subkey)) != ERROR_SUCCESS)
{
RegCloseKey (ifkey);
continue;
}
if (RegQueryValueEx (subkey, "IPAddress", 0,
NULL, (unsigned char *) ip,
(size = sizeof ip, &size)) == ERROR_SUCCESS
&& RegQueryValueEx (subkey, "IPMask", 0,
NULL, (unsigned char *) np,
(size = sizeof np, &size)) == ERROR_SUCCESS)
{
if ((caddr_t)++ ifr > ifc->ifc_buf
+ ifc->ifc_len - sizeof (struct ifreq))
goto out;
switch (what)
{
case SIOCGIFCONF:
case SIOCGIFADDR:
sa = (struct sockaddr_in *) &ifr->ifr_addr;
sa->sin_addr.s_addr = cygwin_inet_addr (ip);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFBRDADDR:
lip = cygwin_inet_addr (ip);
lnp = cygwin_inet_addr (np);
sa = (struct sockaddr_in *) &ifr->ifr_broadaddr;
sa->sin_addr.s_addr = lip & lnp | ~lnp;
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFNETMASK:
sa = (struct sockaddr_in *) &ifr->ifr_netmask;
sa->sin_addr.s_addr = cygwin_inet_addr (np);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFHWADDR:
so = &ifr->ifr_hwaddr;
memset (so->sa_data, 0, IFHWADDRLEN);
so->sa_family = AF_INET;
break;
case SIOCGIFMETRIC:
ifr->ifr_metric = 1;
break;
case SIOCGIFMTU:
ifr->ifr_mtu = 1500;
break;
}
}
RegCloseKey (subkey);
strcpy (netname, "System\\CurrentControlSet\\Services\\Class\\Net\\");
strcat (netname, ifname);
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, netname,
0, KEY_READ, &subkey) != ERROR_SUCCESS)
{
RegCloseKey (ifkey);
--ifr;
continue;
}
if (RegQueryValueEx (subkey, "AdapterName", 0,
NULL, (unsigned char *) adapter,
(size = sizeof adapter, &size)) == ERROR_SUCCESS
&& strcasematch (adapter, "MS$PPP"))
{
++*ppp;
strcpy (ifr->ifr_name, "ppp");
strcat (ifr->ifr_name, ppp);
}
else
{
++*eth;
strcpy (ifr->ifr_name, "eth");
strcat (ifr->ifr_name, eth);
}
RegCloseKey (subkey);
RegCloseKey (ifkey);
++cnt;
}
out:
RegCloseKey (key);
/* Set the correct length */
ifc->ifc_len = cnt * sizeof (struct ifreq);
}
int
get_ifconf (struct ifconf *ifc, int what)
{
unsigned long lip, lnp;
struct sockaddr_in *sa;
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return -1;
/* Union maps buffer to correct struct */
struct ifreq *ifr = ifc->ifc_req;
/* Ensure we have space for two struct ifreqs, fail if not. */
if (ifc->ifc_len < (int) (2 * sizeof (struct ifreq)))
{
set_errno (EFAULT);
return -1;
}
/* Set up interface lo0 first */
strcpy (ifr->ifr_name, "lo");
memset (&ifr->ifr_addr, '\0', sizeof (ifr->ifr_addr));
switch (what)
{
case SIOCGIFCONF:
case SIOCGIFADDR:
sa = (struct sockaddr_in *) &ifr->ifr_addr;
sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFBRDADDR:
lip = htonl (INADDR_LOOPBACK);
lnp = cygwin_inet_addr ("255.0.0.0");
sa = (struct sockaddr_in *) &ifr->ifr_broadaddr;
sa->sin_addr.s_addr = lip & lnp | ~lnp;
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFNETMASK:
sa = (struct sockaddr_in *) &ifr->ifr_netmask;
sa->sin_addr.s_addr = cygwin_inet_addr ("255.0.0.0");
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
case SIOCGIFHWADDR:
ifr->ifr_hwaddr.sa_family = AF_INET;
memset (ifr->ifr_hwaddr.sa_data, 0, IFHWADDRLEN);
break;
case SIOCGIFMETRIC:
ifr->ifr_metric = 1;
break;
case SIOCGIFMTU:
/* This funny value is returned by `ifconfig lo' on Linux 2.2 kernel. */
ifr->ifr_mtu = 3924;
break;
default:
set_errno (EINVAL);
return -1;
}
OSVERSIONINFO os_version_info;
memset (&os_version_info, 0, sizeof os_version_info);
os_version_info.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&os_version_info);
if (wincap.has_ip_helper_lib ())
get_2k_ifconf (ifc, what);
else if (wincap.is_winnt ())
get_nt_ifconf (ifc, what);
else
get_95_ifconf (ifc, what);
return 0;
}
/* exported as rcmd: standards? */
extern "C" int
cygwin_rcmd (char **ahost, unsigned short inport, char *locuser,
char *remuser, char *cmd, int *fd2p)
{
int res = -1;
SOCKET fd2s;
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return (int) INVALID_SOCKET;
if (!*locuser)
{
set_errno (EINVAL);
return (int) INVALID_SOCKET;
}
res = rcmd (ahost, inport, locuser, remuser, cmd, fd2p ? &fd2s : NULL);
if (res != (int) INVALID_SOCKET)
{
cygheap_fdnew res_fd;
if (res_fd >= 0 && fdsock (res_fd, tcp_dev, res))
{
((fhandler_socket *) res_fd)->connect_state (connected);
res = res_fd;
}
else
{
closesocket (res);
res = -1;
}
if (res >= 0 && fd2p)
{
cygheap_fdnew newfd (res_fd, false);
cygheap_fdget fd (*fd2p);
if (newfd >= 0 && fdsock (newfd, tcp_dev, fd2s))
{
*fd2p = newfd;
((fhandler_socket *) fd2p)->connect_state (connected);
}
else
{
closesocket (res);
closesocket (fd2s);
res = -1;
}
}
}
syscall_printf ("%d = rcmd (...)", res);
return res;
}
/* exported as rresvport: standards? */
extern "C" int
cygwin_rresvport (int *port)
{
int res;
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return -1;
res = rresvport (port);
if (res != (int) INVALID_SOCKET)
{
cygheap_fdnew res_fd;
if (res_fd >= 0 && fdsock (res_fd, tcp_dev, res))
res = res_fd;
else
res = -1;
}
syscall_printf ("%d = rresvport (%d)", res, port ? *port : 0);
return res;
}
/* exported as rexec: standards? */
extern "C" int
cygwin_rexec (char **ahost, unsigned short inport, char *locuser,
char *password, char *cmd, int *fd2p)
{
int res = -1;
SOCKET fd2s;
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return (int) INVALID_SOCKET;
res = rexec (ahost, inport, locuser, password, cmd, fd2p ? &fd2s : NULL);
if (res != (int) INVALID_SOCKET)
{
cygheap_fdnew res_fd;
if (res_fd >= 0 && fdsock (res_fd, tcp_dev, res))
{
((fhandler_socket *) res_fd)->connect_state (connected);
res = res_fd;
}
else
{
closesocket (res);
res = -1;
}
if (res >= 0 && fd2p)
{
cygheap_fdnew newfd (res_fd, false);
cygheap_fdget fd (*fd2p);
if (newfd >= 0 && fdsock (newfd, tcp_dev, fd2s))
{
((fhandler_socket *) fd2p)->connect_state (connected);
*fd2p = newfd;
}
else
{
closesocket (res);
closesocket (fd2s);
res = -1;
}
}
}
syscall_printf ("%d = rexec (...)", res);
return res;
}
/* socketpair: standards? */
/* Win32 supports AF_INET only, so ignore domain and protocol arguments */
extern "C" int
socketpair (int family, int type, int protocol, int *sb)
{
int res = -1;
SOCKET insock, outsock, newsock;
struct sockaddr_in sock_in, sock_out;
int len;
sig_dispatch_pending ();
myfault efault;
if (efault.faulted (EFAULT))
return -1;
if (family != AF_LOCAL && family != AF_INET)
{
set_errno (EAFNOSUPPORT);
goto done;
}
if (type != SOCK_STREAM && type != SOCK_DGRAM)
{
set_errno (EPROTOTYPE);
goto done;
}
if ((family == AF_LOCAL && protocol != PF_UNSPEC && protocol != PF_LOCAL)
|| (family == AF_INET && protocol != PF_UNSPEC && protocol != PF_INET))
{
set_errno (EPROTONOSUPPORT);
goto done;
}
/* create the first socket */
newsock = socket (AF_INET, type, 0);
if (newsock == INVALID_SOCKET)
{
debug_printf ("first socket call failed");
set_winsock_errno ();
goto done;
}
/* bind the socket to any unused port */
sock_in.sin_family = AF_INET;
sock_in.sin_port = 0;
sock_in.sin_addr.s_addr = INADDR_ANY;
if (bind (newsock, (struct sockaddr *) &sock_in, sizeof (sock_in)) < 0)
{
debug_printf ("bind failed");
set_winsock_errno ();
closesocket (newsock);
goto done;
}
len = sizeof (sock_in);
if (getsockname (newsock, (struct sockaddr *) &sock_in, &len) < 0)
{
debug_printf ("getsockname error");
set_winsock_errno ();
closesocket (newsock);
goto done;
}
/* For stream sockets, create a listener */
if (type == SOCK_STREAM)
listen (newsock, 2);
/* create a connecting socket */
outsock = socket (AF_INET, type, 0);
if (outsock == INVALID_SOCKET)
{
debug_printf ("second socket call failed");
set_winsock_errno ();
closesocket (newsock);
goto done;
}
/* For datagram sockets, bind the 2nd socket to an unused address, too */
if (type == SOCK_DGRAM)
{
sock_out.sin_family = AF_INET;
sock_out.sin_port = 0;
sock_out.sin_addr.s_addr = INADDR_ANY;
if (bind (outsock, (struct sockaddr *) &sock_out, sizeof (sock_out)) < 0)
{
debug_printf ("bind failed");
set_winsock_errno ();
closesocket (newsock);
closesocket (outsock);
goto done;
}
len = sizeof (sock_out);
if (getsockname (outsock, (struct sockaddr *) &sock_out, &len) < 0)
{
debug_printf ("getsockname error");
set_winsock_errno ();
closesocket (newsock);
closesocket (outsock);
goto done;
}
}
/* Force IP address to loopback */
sock_in.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
if (type == SOCK_DGRAM)
sock_out.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
/* Do a connect */
if (connect (outsock, (struct sockaddr *) &sock_in, sizeof (sock_in)) < 0)
{
debug_printf ("connect error");
set_winsock_errno ();
closesocket (newsock);
closesocket (outsock);
goto done;
}
if (type == SOCK_STREAM)
{
/* For stream sockets, accept the connection and close the listener */
len = sizeof (sock_in);
insock = accept (newsock, (struct sockaddr *) &sock_in, &len);
if (insock == INVALID_SOCKET)
{
debug_printf ("accept error");
set_winsock_errno ();
closesocket (newsock);
closesocket (outsock);
goto done;
}
closesocket (newsock);
}
else
{
/* For datagram sockets, connect the 2nd socket */
if (connect (newsock, (struct sockaddr *) &sock_out,
sizeof (sock_out)) < 0)
{
debug_printf ("connect error");
set_winsock_errno ();
closesocket (newsock);
closesocket (outsock);
goto done;
}
insock = newsock;
}
{
cygheap_fdnew sb0;
const device *dev;
if (family == AF_INET)
dev = (type == SOCK_STREAM ? tcp_dev : udp_dev);
else
dev = (type == SOCK_STREAM ? stream_dev : dgram_dev);
if (sb0 >= 0 && fdsock (sb0, dev, insock))
{
((fhandler_socket *) sb0)->set_sun_path ("");
((fhandler_socket *) sb0)->set_addr_family (family);
((fhandler_socket *) sb0)->set_socket_type (type);
((fhandler_socket *) sb0)->connect_state (connected);
if (family == AF_LOCAL && type == SOCK_STREAM)
((fhandler_socket *) sb0)->af_local_set_sockpair_cred ();
cygheap_fdnew sb1 (sb0, false);
if (sb1 >= 0 && fdsock (sb1, dev, outsock))
{
((fhandler_socket *) sb1)->set_sun_path ("");
((fhandler_socket *) sb1)->set_addr_family (family);
((fhandler_socket *) sb1)->set_socket_type (type);
((fhandler_socket *) sb1)->connect_state (connected);
if (family == AF_LOCAL && type == SOCK_STREAM)
((fhandler_socket *) sb1)->af_local_set_sockpair_cred ();
sb[0] = sb0;
sb[1] = sb1;
res = 0;
}
}
if (res == -1)
{
closesocket (insock);
closesocket (outsock);
}
}
done:
syscall_printf ("%d = socketpair (...)", res);
return res;
}
/* sethostent: standards? */
extern "C" void
sethostent (int)
{
}
/* endhostent: standards? */
extern "C" void
endhostent (void)
{
}
/* exported as recvmsg: standards? */
extern "C" int
cygwin_recvmsg (int fd, struct msghdr *msg, int flags)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
{
res = check_iovec_for_read (msg->msg_iov, msg->msg_iovlen);
if (res > 0)
res = fh->recvmsg (msg, flags, res); // res == iovec tot
}
syscall_printf ("%d = recvmsg (%d, %p, %x)", res, fd, msg, flags);
return res;
}
/* exported as sendmsg: standards? */
extern "C" int
cygwin_sendmsg (int fd, const struct msghdr *msg, int flags)
{
int res;
sig_dispatch_pending ();
fhandler_socket *fh = get (fd);
myfault efault;
if (efault.faulted (EFAULT) || !fh)
res = -1;
else
{
res = check_iovec_for_write (msg->msg_iov, msg->msg_iovlen);
if (res > 0)
res = fh->sendmsg (msg, flags, res); // res == iovec tot
}
syscall_printf ("%d = sendmsg (%d, %p, %x)", res, fd, msg, flags);
return res;
}
/* See "UNIX Network Programming, Networing APIs: Sockets and XTI",
W. Richard Stevens, Prentice Hall PTR, 1998. */
extern "C" int
cygwin_inet_pton (int family, const char *strptr, void *addrptr)
{
if (family == AF_INET)
{
struct in_addr in_val;
if (cygwin_inet_aton (strptr, &in_val))
{
memcpy (addrptr, &in_val, sizeof (struct in_addr));
return 1;
}
return 0;
}
set_errno (EAFNOSUPPORT);
return -1;
}
/* See "UNIX Network Programming, Networing APIs: Sockets and XTI",
W. Richard Stevens, Prentice Hall PTR, 1998. */
extern "C" const char *
cygwin_inet_ntop (int family, const void *addrptr, char *strptr, size_t len)
{
const u_char *p = (const u_char *) addrptr;
myfault efault;
if (efault.faulted (EFAULT))
return NULL;
if (family == AF_INET)
{
char temp[64]; /* Big enough for 4 ints ... */
__small_sprintf (temp, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
if (strlen (temp) >= len)
{
set_errno (ENOSPC);
return NULL;
}
strcpy (strptr, temp);
return strptr;
}
set_errno (EAFNOSUPPORT);
return NULL;
}
|