1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
|
/* CFSocket.c
Copyright (c) 1999-2019, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2019, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Michael LeHew
*/
#include "CFBase.h"
#if __HAS_DISPATCH__
#include "CFSocket.h"
#include <sys/types.h>
#include <math.h>
#include <limits.h>
#if TARGET_OS_MAC
#include <sys/sysctl.h>
#include <sys/un.h>
#include <libc.h>
#include <dlfcn.h>
#if TARGET_OS_CYGWIN
#include <sys/socket.h>
#endif
#endif
#if TARGET_OS_CYGWIN || TARGET_OS_BSD
#include <sys/socket.h>
#endif
#if TARGET_OS_WIN32
#include <WinSock2.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
#if !TARGET_OS_WIN32
#include <sys/ioctl.h>
#endif
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
#include <fcntl.h>
#include "CFArray.h"
#include "CFData.h"
#include "CFDictionary.h"
#include "CFRunLoop.h"
#include "CFString.h"
#include "CFPropertyList.h"
#include "CFInternal.h"
#include "CFRuntime_Internal.h"
#if TARGET_OS_WIN32
#include <process.h>
#endif
#ifndef NBBY
#define NBBY 8
#endif
#if TARGET_OS_WIN32
// redefine this to the winsock error in this file
#undef EINPROGRESS
#define EINPROGRESS WSAEINPROGRESS
// redefine this to the winsock error in this file
#undef EBADF
#define EBADF WSAENOTSOCK
#define NFDBITS (sizeof(int32_t) * NBBY)
typedef int32_t fd_mask;
typedef int socklen_t;
#define gettimeofday _NS_gettimeofday
struct timezone;
CF_PRIVATE int _NS_gettimeofday(struct timeval *tv, struct timezone *tz);
// although this is only used for debug info, we define it for compatibility
#define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0)
static void timeradd(struct timeval *a, struct timeval *b, struct timeval *res) {
res->tv_sec = a->tv_sec + b->tv_sec;
res->tv_usec = a->tv_usec + b->tv_usec;
if (res->tv_usec > 1e06) {
res->tv_sec++;
res->tv_usec -= 1e06;
}
}
#endif // TARGET_OS_WIN32
// On Mach we use a v0 RunLoopSource to make client callbacks. That source is signalled by a
// separate SocketManager thread who uses select() to watch the sockets' fds.
#undef LOG_CFSOCKET
//#define LOG_CFSOCKET 1
#define DEBUG_POLLING_SELECT 1
#if defined(LOG_CFSOCKET)
#include <sys/syslog.h>
static _CFThreadRef __cfSocketTid()
{
#if TARGET_OS_MAC
uint64_t tid = 0;
if (0 != pthread_threadid_np(NULL, &tid))
tid = pthread_mach_thread_np(pthread_self());
return (_CFThreadRef) tid;
#elif TARGET_OS_WIN32
return (_CFThreadRef) GetCurrentThreadId();
#else
return (_CFThreadRef) pthread_self();
#endif
}
static void __cfSocketLog(const char* function, int line, const char* fmt, ...)
{
#if 0
char* p = nil;
va_list args;
va_start(args, fmt);
vasprintf(&p, fmt, args);
va_end(args);
// CFLog(kCFLogLevelNotice, CFSTR("CFSocket:%d %s"), line, p);
char* s = nil;
asprintf(&s, "CFSocket:%d %s", line, p);
syslog(LOG_NOTICE, "%s", s);
free(s);
free(p);
#else
va_list args;
va_start(args, fmt);
CFStringRef fmtString = CFStringCreateWithCString(kCFAllocatorDefault, fmt, kCFStringEncodingUTF8);
CFStringRef payload = CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, NULL, fmtString, args);
if (fmtString)
CFRelease(fmtString);
if (payload == NULL)
syslog(LOG_NOTICE, "CFSocket[%p]:%s:%d - no payload?", __cfSocketTid(),function, line);
else {
CFDataRef payloadData = CFStringCreateExternalRepresentation(kCFAllocatorDefault, payload, kCFStringEncodingUTF8, '.');
CFRelease(payload);
if (payloadData == NULL)
syslog(LOG_NOTICE, "CFSocket[%p]:%s:%d - no payload?", __cfSocketTid(),function, line);
else {
syslog(LOG_NOTICE, "CFSocket[%p]:%s:%d - %.*s", __cfSocketTid(),function, line, (int) CFDataGetLength(payloadData), CFDataGetBytePtr(payloadData));
CFRelease(payloadData);
}
}
#endif
}
static void __cfSocketLogWithSocket(CFSocketRef s, const char* function, int line, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
CFStringRef fmtString = CFStringCreateWithCString(kCFAllocatorDefault, fmt, kCFStringEncodingUTF8);
CFStringRef payload = CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, NULL, fmtString, args);
if (fmtString)
CFRelease(fmtString);
if (payload == NULL)
syslog(LOG_NOTICE, "CFSocket[%p]:%s:%d (%p, fd %d) - no payload?", __cfSocketTid(), function, line, s, CFSocketGetNative(s));
else {
CFDataRef payloadData = CFStringCreateExternalRepresentation(kCFAllocatorDefault, payload, kCFStringEncodingUTF8, '.');
CFRelease(payload);
if (payloadData == NULL)
syslog(LOG_NOTICE, "CFSocket[%p]:%s:%d (%p, fd %d) - no payload?", __cfSocketTid(), function, line, s, CFSocketGetNative(s));
else {
syslog(LOG_NOTICE, "CFSocket[%p]:%s:%d (%p, fd %d) - %.*s", __cfSocketTid(), function, line, s, CFSocketGetNative(s), (int) CFDataGetLength(payloadData), CFDataGetBytePtr(payloadData));
CFRelease(payloadData);
}
}
}
#define __CFSOCKETLOG(xxx...) __cfSocketLog(__FUNCTION__, __LINE__, xxx)
#define __CFSOCKETLOG_WS(S, xxx...) __cfSocketLogWithSocket(S, __FUNCTION__, __LINE__, xxx)
#else
#define __CFSOCKETLOG(xxx...) /**/
#define __CFSOCKETLOG_WS(S, xxx...) /**/
#endif
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#define INVALID_SOCKET (CFSocketNativeHandle)(-1)
#define closesocket(a) close((a))
#define ioctlsocket(a,b,c) ioctl((a),(b),(c))
#endif
CF_INLINE int __CFSocketLastError(void) {
#if TARGET_OS_WIN32
return WSAGetLastError();
#else
return thread_errno();
#endif
}
CF_INLINE CFIndex __CFSocketFdGetSize(CFDataRef fdSet) {
#if TARGET_OS_WIN32
if (CFDataGetLength(fdSet) == 0) {
return 0;
}
return FD_SETSIZE;
#else
return NBBY * CFDataGetLength(fdSet);
#endif
}
CF_INLINE Boolean __CFSocketFdSet(CFSocketNativeHandle sock, CFMutableDataRef fdSet) {
/* returns true if a change occurred, false otherwise */
Boolean retval = false;
if (INVALID_SOCKET != sock && 0 <= sock) {
fd_set *fds;
#if TARGET_OS_WIN32
if (CFDataGetLength(fdSet) == 0) {
CFDataIncreaseLength(fdSet, sizeof(fd_set));
fds = (fd_set *)CFDataGetMutableBytePtr(fdSet);
FD_ZERO(fds);
} else {
fds = (fd_set *)CFDataGetMutableBytePtr(fdSet);
}
#else
CFIndex numFds = NBBY * CFDataGetLength(fdSet);
fd_mask *fds_bits;
if (sock >= numFds) {
CFIndex oldSize = numFds / NFDBITS, newSize = (sock + NFDBITS) / NFDBITS, changeInBytes = (newSize - oldSize) * sizeof(fd_mask);
CFDataIncreaseLength(fdSet, changeInBytes);
fds_bits = (fd_mask *)CFDataGetMutableBytePtr(fdSet);
memset(fds_bits + oldSize, 0, changeInBytes);
} else {
fds_bits = (fd_mask *)CFDataGetMutableBytePtr(fdSet);
}
fds = (fd_set *)fds_bits;
#endif
if (!FD_ISSET(sock, fds)) {
retval = true;
FD_SET(sock, fds);
}
}
return retval;
}
#define MAX_SOCKADDR_LEN 256
#define MAX_DATA_SIZE 65535
#define MAX_CONNECTION_ORIENTED_DATA_SIZE 32768
/* locks are to be acquired in the following order:
(1) __CFAllSocketsLock
(2) an individual CFSocket's lock
(3) __CFActiveSocketsLock
*/
static CFLock_t __CFAllSocketsLock = CFLockInit; /* controls __CFAllSockets */
static CFMutableDictionaryRef __CFAllSockets = NULL;
static CFLock_t __CFActiveSocketsLock = CFLockInit; /* controls __CFRead/WriteSockets, __CFRead/WriteSocketsFds, __CFSocketManagerThread, and __CFSocketManagerIteration */
static volatile UInt32 __CFSocketManagerIteration = 0;
static CFMutableArrayRef __CFWriteSockets = NULL;
static CFMutableArrayRef __CFReadSockets = NULL;
static CFMutableDataRef __CFWriteSocketsFds = NULL;
static CFMutableDataRef __CFReadSocketsFds = NULL;
static CFDataRef zeroLengthData = NULL;
static Boolean __CFReadSocketsTimeoutInvalid = true; /* rebuild the timeout value before calling select */
static CFSocketNativeHandle __CFWakeupSocketPair[2] = {INVALID_SOCKET, INVALID_SOCKET};
static void *__CFSocketManagerThread = NULL;
static void __CFSocketDoCallback(CFSocketRef s, CFDataRef data, CFDataRef address, CFSocketNativeHandle sock);
struct __CFSocket {
CFRuntimeBase _base;
struct {
unsigned client:8; // flags set by client (reenable, CloseOnInvalidate)
unsigned disabled:8; // flags marking disabled callbacks
unsigned connected:1; // Are we connected yet? (also true for connectionless sockets)
unsigned writableHint:1; // Did the polling the socket show it to be writable?
unsigned closeSignaled:1; // Have we seen FD_CLOSE? (only used on Win32)
unsigned unused:13;
} _f;
CFLock_t _lock;
CFLock_t _writeLock;
CFSocketNativeHandle _socket; /* immutable */
SInt32 _socketType;
SInt32 _errorCode;
CFDataRef _address;
CFDataRef _peerAddress;
SInt32 _socketSetCount;
CFRunLoopSourceRef _source0; // v0 RLS, messaged from SocketMgr
CFMutableArrayRef _runLoops;
CFSocketCallBack _callout; /* immutable */
CFSocketContext _context; /* immutable */
CFMutableArrayRef _dataQueue; // queues to pass data from SocketMgr thread
CFMutableArrayRef _addressQueue;
struct timeval _readBufferTimeout;
CFMutableDataRef _readBuffer;
CFIndex _bytesToBuffer; /* is length of _readBuffer */
CFIndex _bytesToBufferPos; /* where the next _CFSocketRead starts from */
CFIndex _bytesToBufferReadPos; /* Where the buffer will next be read into (always after _bytesToBufferPos, but less than _bytesToBuffer) */
Boolean _atEOF;
int _bufferedReadError;
CFMutableDataRef _leftoverBytes;
// <rdar://problem/17849895>
// If the timeout is set on the CFSocketRef but we never get select() timeout
// because we always have some network events so select never times out (e.g. while having a large download).
// We need to notify any waiting buffered read clients if there is data available without relying on select timing out.
struct timeval _readBufferTimeoutNotificationTime;
Boolean _hitTheTimeout;
};
/* Bit 6 in the base reserved bits is used for write-signalled state (mutable) */
/* Bit 5 in the base reserved bits is used for read-signalled state (mutable) */
/* Bit 4 in the base reserved bits is used for invalid state (mutable) */
/* Bits 0-3 in the base reserved bits are used for callback types (immutable) */
/* Of this, bits 0-1 are used for the read callback type. */
CF_INLINE Boolean __CFSocketIsWriteSignalled(CFSocketRef s) {
return __CFRuntimeGetFlag(s, 6);
}
CF_INLINE void __CFSocketSetWriteSignalled(CFSocketRef s) {
__CFRuntimeSetFlag(s, 6, true);
}
CF_INLINE void __CFSocketUnsetWriteSignalled(CFSocketRef s) {
__CFRuntimeSetFlag(s, 6, false);
}
CF_INLINE Boolean __CFSocketIsReadSignalled(CFSocketRef s) {
return __CFRuntimeGetFlag(s, 5);
}
CF_INLINE void __CFSocketSetReadSignalled(CFSocketRef s) {
__CFRuntimeSetFlag(s, 5, true);
}
CF_INLINE void __CFSocketUnsetReadSignalled(CFSocketRef s) {
__CFRuntimeSetFlag(s, 5, false);
}
CF_INLINE Boolean __CFSocketIsValid(CFSocketRef s) {
return __CFRuntimeGetFlag(s, 4);
}
CF_INLINE void __CFSocketSetValid(CFSocketRef s) {
__CFRuntimeSetFlag(s, 4, true);
}
CF_INLINE void __CFSocketUnsetValid(CFSocketRef s) {
__CFRuntimeSetFlag(s, 4, false);
}
CF_INLINE uint8_t __CFSocketCallBackTypes(CFSocketRef s) {
return (uint8_t)__CFRuntimeGetValue(s, 3, 0);
}
CF_INLINE uint8_t __CFSocketReadCallBackType(CFSocketRef s) {
return (uint8_t)__CFRuntimeGetValue(s, 1, 0);
}
CF_INLINE void __CFSocketSetCallBackTypes(CFSocketRef s, uint8_t types) {
__CFRuntimeSetValue(s, 3, 0, types & 0xF);
}
CF_INLINE void __CFSocketLock(CFSocketRef s) {
__CFLock(&(s->_lock));
}
CF_INLINE void __CFSocketUnlock(CFSocketRef s) {
__CFUnlock(&(s->_lock));
}
CF_INLINE Boolean __CFSocketIsConnectionOriented(CFSocketRef s) {
return (SOCK_STREAM == s->_socketType);
}
CF_INLINE Boolean __CFSocketIsScheduled(CFSocketRef s) {
return (s->_socketSetCount > 0);
}
CF_INLINE void __CFSocketEstablishAddress(CFSocketRef s) {
/* socket should already be locked */
uint8_t name[MAX_SOCKADDR_LEN];
int namelen = sizeof(name);
if (__CFSocketIsValid(s) && NULL == s->_address && INVALID_SOCKET != s->_socket && 0 == getsockname(s->_socket, (struct sockaddr *)name, (socklen_t *)&namelen) && 0 < namelen) {
s->_address = CFDataCreate(CFGetAllocator(s), name, namelen);
}
}
CF_INLINE void __CFSocketEstablishPeerAddress(CFSocketRef s) {
/* socket should already be locked */
uint8_t name[MAX_SOCKADDR_LEN];
int namelen = sizeof(name);
if (__CFSocketIsValid(s) && NULL == s->_peerAddress && INVALID_SOCKET != s->_socket && 0 == getpeername(s->_socket, (struct sockaddr *)name, (socklen_t *)&namelen) && 0 < namelen) {
s->_peerAddress = CFDataCreate(CFGetAllocator(s), name, namelen);
}
}
static Boolean __CFNativeSocketIsValid(CFSocketNativeHandle sock) {
Boolean result;
#if TARGET_OS_WIN32
SInt32 errorCode = 0;
int errorSize = sizeof(errorCode);
result = !(0 != getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&errorCode, &errorSize) && __CFSocketLastError() == WSAENOTSOCK);
#else
SInt32 flags = fcntl(sock, F_GETFL, 0);
result = !(0 > flags && EBADF == __CFSocketLastError());
#endif
__CFSOCKETLOG("socket fd %d => %d", sock, result);
return result;
}
CF_INLINE Boolean __CFSocketFdClr(CFSocketNativeHandle sock, CFMutableDataRef fdSet) {
/* returns true if a change occurred, false otherwise */
Boolean retval = false;
if (INVALID_SOCKET != sock && 0 <= sock) {
#if TARGET_OS_WIN32
if (CFDataGetLength(fdSet) > 0) {
fd_set *fds = (fd_set *)CFDataGetMutableBytePtr(fdSet);
if (FD_ISSET(sock, fds)) {
retval = true;
FD_CLR(sock, fds);
}
}
#else
CFIndex numFds = NBBY * CFDataGetLength(fdSet);
fd_mask *fds_bits;
if (sock < numFds) {
fds_bits = (fd_mask *)CFDataGetMutableBytePtr(fdSet);
if (FD_ISSET(sock, (fd_set *)fds_bits)) {
retval = true;
FD_CLR(sock, (fd_set *)fds_bits);
}
}
#endif
}
return retval;
}
static SInt32 __CFSocketCreateWakeupSocketPair(void) {
#if TARGET_OS_MAC
SInt32 error;
error = socketpair(PF_LOCAL, SOCK_DGRAM, 0, __CFWakeupSocketPair);
if (0 <= error) error = fcntl(__CFWakeupSocketPair[0], F_SETFD, FD_CLOEXEC);
if (0 <= error) error = fcntl(__CFWakeupSocketPair[1], F_SETFD, FD_CLOEXEC);
if (0 > error) {
closesocket(__CFWakeupSocketPair[0]);
closesocket(__CFWakeupSocketPair[1]);
__CFWakeupSocketPair[0] = INVALID_SOCKET;
__CFWakeupSocketPair[1] = INVALID_SOCKET;
}
#else
UInt32 i;
SInt32 error = 0;
struct sockaddr_in address[2];
int namelen = sizeof(struct sockaddr_in);
for (i = 0; i < 2; i++) {
__CFWakeupSocketPair[i] = socket(PF_INET, SOCK_DGRAM, 0);
memset(&(address[i]), 0, sizeof(struct sockaddr_in));
address[i].sin_family = AF_INET;
address[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (0 <= error) error = bind(__CFWakeupSocketPair[i], (struct sockaddr *)&(address[i]), sizeof(struct sockaddr_in));
if (0 <= error) error = getsockname(__CFWakeupSocketPair[i], (struct sockaddr *)&(address[i]), (socklen_t *)&namelen);
if (sizeof(struct sockaddr_in) != namelen) error = -1;
}
if (0 <= error) error = connect(__CFWakeupSocketPair[0], (struct sockaddr *)&(address[1]), sizeof(struct sockaddr_in));
if (0 <= error) error = connect(__CFWakeupSocketPair[1], (struct sockaddr *)&(address[0]), sizeof(struct sockaddr_in));
if (0 > error) {
closesocket(__CFWakeupSocketPair[0]);
closesocket(__CFWakeupSocketPair[1]);
__CFWakeupSocketPair[0] = INVALID_SOCKET;
__CFWakeupSocketPair[1] = INVALID_SOCKET;
}
#endif
__CFSOCKETLOG("wakeup socket pair is %d / %d\n", __CFWakeupSocketPair[0], __CFWakeupSocketPair[1]);
return error;
}
// Version 0 RunLoopSources set a mask in an FD set to control what socket activity we hear about.
// Changes to the master fs_sets occur via these 4 functions.
CF_INLINE Boolean __CFSocketSetFDForRead(CFSocketRef s) {
__CFSOCKETLOG_WS(s, "");
__CFReadSocketsTimeoutInvalid = true;
Boolean b = __CFSocketFdSet(s->_socket, __CFReadSocketsFds);
if (b && INVALID_SOCKET != __CFWakeupSocketPair[0]) {
uint8_t c = 'r';
send(__CFWakeupSocketPair[0], (const char *)&c, sizeof(c), 0);
}
return b;
}
CF_INLINE Boolean __CFSocketClearFDForRead(CFSocketRef s) {
__CFSOCKETLOG_WS(s, "");
__CFReadSocketsTimeoutInvalid = true;
Boolean b = __CFSocketFdClr(s->_socket, __CFReadSocketsFds);
if (b && INVALID_SOCKET != __CFWakeupSocketPair[0]) {
uint8_t c = 's';
send(__CFWakeupSocketPair[0], (const char *)&c, sizeof(c), 0);
}
return b;
}
CF_INLINE Boolean __CFSocketSetFDForWrite(CFSocketRef s) {
__CFSOCKETLOG_WS(s, "");
Boolean b = __CFSocketFdSet(s->_socket, __CFWriteSocketsFds);
if (b && INVALID_SOCKET != __CFWakeupSocketPair[0]) {
uint8_t c = 'w';
send(__CFWakeupSocketPair[0], (const char *)&c, sizeof(c), 0);
}
return b;
}
CF_INLINE Boolean __CFSocketClearFDForWrite(CFSocketRef s) {
__CFSOCKETLOG_WS(s, "");
Boolean b = __CFSocketFdClr(s->_socket, __CFWriteSocketsFds);
if (b && INVALID_SOCKET != __CFWakeupSocketPair[0]) {
uint8_t c = 'x';
send(__CFWakeupSocketPair[0], (const char *)&c, sizeof(c), 0);
}
return b;
}
#if TARGET_OS_WIN32
static Boolean WinSockUsed = FALSE;
static void __CFSocketInitializeWinSock_Guts(void) {
if (!WinSockUsed) {
WinSockUsed = TRUE;
WORD versionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
int errorStatus = WSAStartup(versionRequested, &wsaData);
if (errorStatus != 0 || LOBYTE(wsaData.wVersion) != LOBYTE(versionRequested) || HIBYTE(wsaData.wVersion) != HIBYTE(versionRequested)) {
WSACleanup();
CFLog(kCFLogLevelWarning, CFSTR("*** Could not initialize WinSock subsystem!!!"));
}
}
}
CF_EXPORT void __CFSocketInitializeWinSock(void) {
__CFLock(&__CFActiveSocketsLock);
__CFSocketInitializeWinSock_Guts();
__CFUnlock(&__CFActiveSocketsLock);
}
CF_PRIVATE void __CFSocketCleanup(void) {
if (INVALID_SOCKET != __CFWakeupSocketPair[0]) {
closesocket(__CFWakeupSocketPair[0]);
__CFWakeupSocketPair[0] = INVALID_SOCKET;
}
if (INVALID_SOCKET != __CFWakeupSocketPair[1]) {
closesocket(__CFWakeupSocketPair[1]);
__CFWakeupSocketPair[1] = INVALID_SOCKET;
}
if (WinSockUsed) {
// technically this is not supposed to be called here since it will be called from dllmain, but I don't know where else to put it
WSACleanup();
}
}
#endif
// CFNetwork needs to call this, especially for Win32 to get WSAStartup
static void __CFSocketInitializeSockets(void) {
__CFWriteSockets = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, NULL);
__CFReadSockets = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, NULL);
__CFWriteSocketsFds = CFDataCreateMutable(kCFAllocatorSystemDefault, 0);
__CFReadSocketsFds = CFDataCreateMutable(kCFAllocatorSystemDefault, 0);
zeroLengthData = CFDataCreateMutable(kCFAllocatorSystemDefault, 0);
#if TARGET_OS_WIN32
__CFSocketInitializeWinSock_Guts();
#endif
if (0 > __CFSocketCreateWakeupSocketPair()) {
CFLog(kCFLogLevelWarning, CFSTR("*** Could not create wakeup socket pair for CFSocket!!!"));
} else {
UInt32 yes = 1;
/* wakeup sockets must be non-blocking */
ioctlsocket(__CFWakeupSocketPair[0], FIONBIO, (u_long *)&yes);
ioctlsocket(__CFWakeupSocketPair[1], FIONBIO, (u_long *)&yes);
__CFSocketFdSet(__CFWakeupSocketPair[1], __CFReadSocketsFds);
}
}
static CFRunLoopRef __CFSocketCopyRunLoopToWakeUp(CFRunLoopSourceRef src, CFMutableArrayRef runLoops) {
if (!src) return NULL;
CFRunLoopRef rl = NULL;
SInt32 idx, cnt = CFArrayGetCount(runLoops);
if (0 < cnt) {
rl = (CFRunLoopRef)CFArrayGetValueAtIndex(runLoops, 0);
for (idx = 1; NULL != rl && idx < cnt; idx++) {
CFRunLoopRef value = (CFRunLoopRef)CFArrayGetValueAtIndex(runLoops, idx);
if (value != rl) rl = NULL;
}
if (NULL == rl) { /* more than one different rl, so we must pick one */
/* ideally, this would be a run loop which isn't also in a
* signaled state for this or another source, but that's tricky;
* we pick one that is running in an appropriate mode for this
* source, and from those if possible one that is waiting; then
* we move this run loop to the end of the list to scramble them
* a bit, and always search from the front */
Boolean foundIt = false, foundBackup = false;
SInt32 foundIdx = 0;
for (idx = 0; !foundIt && idx < cnt; idx++) {
CFRunLoopRef value = (CFRunLoopRef)CFArrayGetValueAtIndex(runLoops, idx);
CFStringRef currentMode = CFRunLoopCopyCurrentMode(value);
if (NULL != currentMode) {
if (CFRunLoopContainsSource(value, src, currentMode)) {
if (CFRunLoopIsWaiting(value)) {
foundIdx = idx;
foundIt = true;
} else if (!foundBackup) {
foundIdx = idx;
foundBackup = true;
}
}
CFRelease(currentMode);
}
}
rl = (CFRunLoopRef)CFArrayGetValueAtIndex(runLoops, foundIdx);
CFRetain(rl);
CFArrayRemoveValueAtIndex(runLoops, foundIdx);
CFArrayAppendValue(runLoops, rl);
} else {
CFRetain(rl);
}
}
return rl;
}
// If callBackNow, we immediately do client callbacks, else we have to signal a v0 RunLoopSource so the
// callbacks can happen in another thread.
static void __CFSocketHandleWrite(CFSocketRef s, Boolean callBackNow) {
SInt32 errorCode = 0;
int errorSize = sizeof(errorCode);
CFOptionFlags writeCallBacksAvailable;
if (!CFSocketIsValid(s)) return;
if (0 != (s->_f.client & kCFSocketLeaveErrors) || 0 != getsockopt(s->_socket, SOL_SOCKET, SO_ERROR, (char *)&errorCode, (socklen_t *)&errorSize)) errorCode = 0;
// cast for WinSock bad API
if (errorCode) {
__CFSOCKETLOG_WS(s, "error %ld", (long)errorCode);
}
__CFSocketLock(s);
writeCallBacksAvailable = __CFSocketCallBackTypes(s) & (kCFSocketWriteCallBack | kCFSocketConnectCallBack);
if ((s->_f.client & kCFSocketConnectCallBack) != 0) writeCallBacksAvailable &= ~kCFSocketConnectCallBack;
if (!__CFSocketIsValid(s) || ((s->_f.disabled & writeCallBacksAvailable) == writeCallBacksAvailable)) {
__CFSocketUnlock(s);
return;
}
s->_errorCode = errorCode;
__CFSocketSetWriteSignalled(s);
__CFSOCKETLOG_WS(s, "signalling write");
if (callBackNow) {
__CFSocketDoCallback(s, NULL, NULL, 0);
} else {
CFRunLoopSourceSignal(s->_source0);
CFMutableArrayRef runLoopsOrig = (CFMutableArrayRef)CFRetain(s->_runLoops);
CFMutableArrayRef runLoopsCopy = CFArrayCreateMutableCopy(kCFAllocatorSystemDefault, 0, s->_runLoops);
CFRunLoopSourceRef source0 = s->_source0;
if (NULL != source0 && !CFRunLoopSourceIsValid(source0)) {
source0 = NULL;
}
if (source0) CFRetain(source0);
__CFSocketUnlock(s);
CFRunLoopRef rl = __CFSocketCopyRunLoopToWakeUp(source0, runLoopsCopy);
if (source0) CFRelease(source0);
if (NULL != rl) {
CFRunLoopWakeUp(rl);
CFRelease(rl);
}
__CFSocketLock(s);
if (runLoopsOrig == s->_runLoops) {
s->_runLoops = runLoopsCopy;
runLoopsCopy = NULL;
CFRelease(runLoopsOrig);
}
__CFSocketUnlock(s);
CFRelease(runLoopsOrig);
if (runLoopsCopy) CFRelease(runLoopsCopy);
}
}
#if defined(LOG_CFSOCKET)
static CFStringRef someAddrToString(CFAllocatorRef alloc, int (*fun) (int, struct sockaddr*, socklen_t*), const char* name, CFSocketNativeHandle s)
{
CFStringRef resultString = NULL;
union {
struct sockaddr sa;
struct sockaddr_in sa4b;
struct sockaddr_in6 sa6b;
UInt8 static_buffer[SOCK_MAXADDRLEN];
} u;
socklen_t addrlen = sizeof(u.static_buffer);
uint16_t* pPort = NULL;
char buffer[1024];
if ((*fun) (s, &u.sa, &addrlen) != 0)
snprintf(buffer, sizeof(buffer), "error %d resolving %s address for socket %d", errno, name, s);
else {
void* pAddr = NULL;
switch (u.sa.sa_family) {
case AF_INET:
pAddr = &u.sa4b.sin_addr;
pPort = &u.sa4b.sin_port;
break;
case AF_INET6:
pAddr = &u.sa6b.sin6_addr;
pPort = &u.sa6b.sin6_port;
break;
}
if (pAddr == NULL || inet_ntop(u.sa.sa_family, pAddr, buffer, sizeof(buffer)) == NULL)
snprintf(buffer, sizeof(buffer), "[error %d converting %s address for socket %d]", pAddr != NULL? errno : EBADF, name, s);
}
if (pPort) {
resultString = CFStringCreateWithFormat(alloc, NULL, CFSTR("%s:%d"), buffer, htons(*pPort));
} else {
resultString = CFStringCreateWithFormat(alloc, NULL, CFSTR("%s"), buffer);
}
return resultString;
}
static CFStringRef copyPeerAddress(CFAllocatorRef alloc, CFSocketNativeHandle s)
{
return someAddrToString(alloc, getpeername, "peer", s);
}
static CFStringRef copyLocalAddress(CFAllocatorRef alloc, CFSocketNativeHandle s)
{
return someAddrToString(alloc, getsockname, "local", s);
}
#endif
static void __CFSocketHandleRead(CFSocketRef s, Boolean causedByTimeout)
{
CFDataRef data = NULL, address = NULL;
CFSocketNativeHandle sock = INVALID_SOCKET;
if (!CFSocketIsValid(s)) return;
if (__CFSocketReadCallBackType(s) == kCFSocketDataCallBack) {
uint8_t bufferArray[MAX_CONNECTION_ORIENTED_DATA_SIZE], *buffer;
uint8_t name[MAX_SOCKADDR_LEN];
int namelen = sizeof(name);
SInt32 recvlen = 0;
if (__CFSocketIsConnectionOriented(s)) {
buffer = bufferArray;
recvlen = recvfrom(s->_socket, (char *)buffer, MAX_CONNECTION_ORIENTED_DATA_SIZE, 0, (struct sockaddr *)name, (socklen_t *)&namelen);
} else {
buffer = (uint8_t *)malloc(MAX_DATA_SIZE);
if (buffer) recvlen = recvfrom(s->_socket, (char *)buffer, MAX_DATA_SIZE, 0, (struct sockaddr *)name, (socklen_t *)&namelen);
}
__CFSOCKETLOG_WS(s, "read %ld", (long) recvlen);
if (0 >= recvlen) {
//??? should return error if <0
/* zero-length data is the signal for perform to invalidate if socket is connection oriented */
data = (CFDataRef)CFRetain(zeroLengthData);
} else {
data = CFDataCreate(CFGetAllocator(s), buffer, recvlen);
}
if (buffer && buffer != bufferArray) free(buffer);
__CFSocketLock(s);
if (!__CFSocketIsValid(s)) {
CFRelease(data);
__CFSocketUnlock(s);
return;
}
__CFSocketSetReadSignalled(s);
if (0 < namelen) {
//??? possible optimizations: uniquing; storing last value
address = CFDataCreate(CFGetAllocator(s), name, namelen);
} else if (__CFSocketIsConnectionOriented(s)) {
if (NULL == s->_peerAddress) __CFSocketEstablishPeerAddress(s);
if (NULL != s->_peerAddress) address = (CFDataRef)CFRetain(s->_peerAddress);
}
if (NULL == address) {
address = (CFDataRef)CFRetain(zeroLengthData);
}
if (NULL == s->_dataQueue) {
s->_dataQueue = CFArrayCreateMutable(CFGetAllocator(s), 0, &kCFTypeArrayCallBacks);
}
if (NULL == s->_addressQueue) {
s->_addressQueue = CFArrayCreateMutable(CFGetAllocator(s), 0, &kCFTypeArrayCallBacks);
}
CFArrayAppendValue(s->_dataQueue, data);
CFRelease(data);
CFArrayAppendValue(s->_addressQueue, address);
CFRelease(address);
if (0 < recvlen
&& (s->_f.client & kCFSocketDataCallBack) != 0 && (s->_f.disabled & kCFSocketDataCallBack) == 0
&& __CFSocketIsScheduled(s)
) {
__CFLock(&__CFActiveSocketsLock);
/* restore socket to fds */
__CFSocketSetFDForRead(s);
__CFUnlock(&__CFActiveSocketsLock);
}
} else if (__CFSocketReadCallBackType(s) == kCFSocketAcceptCallBack) {
uint8_t name[MAX_SOCKADDR_LEN];
int namelen = sizeof(name);
sock = accept(s->_socket, (struct sockaddr *)name, (socklen_t *)&namelen);
if (INVALID_SOCKET == sock) {
//??? should return error
return;
}
if (0 < namelen) {
address = CFDataCreate(CFGetAllocator(s), name, namelen);
} else {
address = (CFDataRef)CFRetain(zeroLengthData);
}
__CFSocketLock(s);
if (!__CFSocketIsValid(s)) {
closesocket(sock);
CFRelease(address);
__CFSocketUnlock(s);
return;
}
__CFSocketSetReadSignalled(s);
if (NULL == s->_dataQueue) {
s->_dataQueue = CFArrayCreateMutable(CFGetAllocator(s), 0, NULL);
}
if (NULL == s->_addressQueue) {
s->_addressQueue = CFArrayCreateMutable(CFGetAllocator(s), 0, &kCFTypeArrayCallBacks);
}
CFArrayAppendValue(s->_dataQueue, (void *)(uintptr_t)sock);
CFArrayAppendValue(s->_addressQueue, address);
CFRelease(address);
if ((s->_f.client & kCFSocketAcceptCallBack) != 0 && (s->_f.disabled & kCFSocketAcceptCallBack) == 0
&& __CFSocketIsScheduled(s)
) {
__CFLock(&__CFActiveSocketsLock);
/* restore socket to fds */
__CFSocketSetFDForRead(s);
__CFUnlock(&__CFActiveSocketsLock);
}
} else {
__CFSocketLock(s);
if (!__CFSocketIsValid(s) || (s->_f.disabled & kCFSocketReadCallBack) != 0) {
__CFSocketUnlock(s);
return;
}
if (causedByTimeout) {
__CFSOCKETLOG_WS(s, "TIMEOUT RECEIVED - WILL SIGNAL IMMEDIATELY TO FLUSH (%ld buffered)", s->_bytesToBufferPos);
/* we've got a timeout, but no bytes read, and we don't have any bytes to send. Ignore the timeout. */
if (s->_bytesToBufferPos == 0 && s->_leftoverBytes == NULL) {
__CFSOCKETLOG_WS(s, "TIMEOUT - but no bytes, restoring to active set", s->_bytesToBufferPos);
// Clear the timeout notification time if there is no prefetched data left
timerclear(&s->_readBufferTimeoutNotificationTime);
__CFLock(&__CFActiveSocketsLock);
/* restore socket to fds */
__CFSocketSetFDForRead(s);
__CFUnlock(&__CFActiveSocketsLock);
__CFSocketUnlock(s);
return;
}
} else if (s->_bytesToBuffer != 0 && ! s->_atEOF) {
UInt8* base;
CFIndex ctRead;
CFIndex ctRemaining = s->_bytesToBuffer - s->_bytesToBufferPos;
/* if our buffer has room, we go ahead and buffer */
if (ctRemaining > 0) {
base = CFDataGetMutableBytePtr(s->_readBuffer);
ctRead = read(CFSocketGetNative(s), &base[s->_bytesToBufferPos], ctRemaining);
switch (ctRead) {
case -1:
if (errno == EAGAIN) { // no error
__CFLock(&__CFActiveSocketsLock);
/* restore socket to fds */
__CFSocketSetFDForRead(s);
__CFUnlock(&__CFActiveSocketsLock);
__CFSocketUnlock(s);
return;
} else {
s->_bufferedReadError = errno;
s->_atEOF = true;
}
__CFSOCKETLOG_WS(s, "BUFFERED READ GOT ERROR %d", errno);
break;
case 0:
__CFSOCKETLOG_WS(s, "DONE READING (EOF) - GOING TO SIGNAL");
s->_atEOF = true;
break;
default:
s->_bytesToBufferPos += ctRead;
if (s->_bytesToBuffer != s->_bytesToBufferPos) {
// Update the timeout notification time
struct timeval timeNow = { 0 };
gettimeofday(&timeNow, NULL);
timeradd(&timeNow, &s->_readBufferTimeout, &s->_readBufferTimeoutNotificationTime);
__CFSOCKETLOG_WS(s, "READ %ld - need %ld MORE - GOING BACK FOR MORE", ctRead, s->_bytesToBuffer - s->_bytesToBufferPos);
__CFLock(&__CFActiveSocketsLock);
/* restore socket to fds */
__CFSocketSetFDForRead(s);
__CFUnlock(&__CFActiveSocketsLock);
__CFSocketUnlock(s);
return;
} else {
// Clear the timeout notification time if the buffer is full
timerclear(&s->_readBufferTimeoutNotificationTime);
__CFSOCKETLOG_WS(s, "DONE READING (read %ld bytes) - GOING TO SIGNAL", ctRead);
}
}
}
}
__CFSocketSetReadSignalled(s);
}
__CFSOCKETLOG_WS(s, "read signaling source");
CFRunLoopSourceSignal(s->_source0);
CFMutableArrayRef runLoopsOrig = (CFMutableArrayRef)CFRetain(s->_runLoops);
CFMutableArrayRef runLoopsCopy = CFArrayCreateMutableCopy(kCFAllocatorSystemDefault, 0, s->_runLoops);
CFRunLoopSourceRef source0 = s->_source0;
if (NULL != source0 && !CFRunLoopSourceIsValid(source0)) {
source0 = NULL;
}
if (source0) CFRetain(source0);
__CFSocketUnlock(s);
CFRunLoopRef rl = __CFSocketCopyRunLoopToWakeUp(source0, runLoopsCopy);
if (source0) CFRelease(source0);
if (NULL != rl) {
CFRunLoopWakeUp(rl);
CFRelease(rl);
}
__CFSocketLock(s);
if (runLoopsOrig == s->_runLoops) {
s->_runLoops = runLoopsCopy;
runLoopsCopy = NULL;
CFRelease(runLoopsOrig);
}
__CFSocketUnlock(s);
CFRelease(runLoopsOrig);
if (runLoopsCopy) CFRelease(runLoopsCopy);
}
static struct timeval* intervalToTimeval(CFTimeInterval timeout, struct timeval* tv)
{
if (timeout == 0.0)
timerclear(tv);
else {
tv->tv_sec = (0 >= timeout || INT_MAX <= timeout) ? INT_MAX : (int)(float)floor(timeout);
tv->tv_usec = (int)((timeout - floor(timeout)) * 1.0E6);
}
return tv;
}
/* note that this returns a pointer to the min value, which won't have changed during
the dictionary apply, since we've got the active sockets lock held */
static void _calcMinTimeout_locked(const void* val, void* ctxt)
{
CFSocketRef s = (CFSocketRef) val;
struct timeval** minTime = (struct timeval**) ctxt;
if (timerisset(&s->_readBufferTimeout) && (*minTime == NULL || timercmp(&s->_readBufferTimeout, *minTime, <)))
*minTime = &s->_readBufferTimeout;
else if (s->_leftoverBytes) {
/* If there's anyone with leftover bytes, they'll need to be awoken immediately */
static struct timeval sKickerTime = { 0, 0 };
*minTime = &sKickerTime;
}
}
void __CFSocketSetSocketReadBufferAttrs(CFSocketRef s, CFTimeInterval timeout, CFIndex length)
{
struct timeval timeoutVal;
intervalToTimeval(timeout, &timeoutVal);
/* lock ordering is socket lock, activesocketslock */
/* activesocketslock protects our timeout calculation */
__CFSocketLock(s);
__CFLock(&__CFActiveSocketsLock);
if (s->_bytesToBuffer != length) {
CFIndex ctBuffer = s->_bytesToBufferPos - s->_bytesToBufferReadPos;
if (ctBuffer) {
/* As originally envisaged, you were supposed to be sure to drain the buffer before
* issuing another request on the socket. In practice, there seem to be times when we want to re-use
* the stream (or perhaps, are on our way to closing it out) and this policy doesn't work so well.
* So, if someone changes the buffer size while we have bytes already buffered, we put them
* aside and use them to satisfy any subsequent reads.
*/
__CFSOCKETLOG_WS(s, "WARNING: shouldn't set read buffer length while data (%ld bytes) is still in the read buffer (leftover total %ld)", ctBuffer, s->_leftoverBytes? CFDataGetLength(s->_leftoverBytes) : 0);
if (s->_leftoverBytes == NULL)
s->_leftoverBytes = CFDataCreateMutable(CFGetAllocator(s), 0);
/* append the current buffered bytes over. We'll keep draining _leftoverBytes while we have them... */
CFDataAppendBytes(s->_leftoverBytes, CFDataGetBytePtr(s->_readBuffer) + s->_bytesToBufferReadPos, ctBuffer);
CFRelease(s->_readBuffer);
s->_readBuffer = NULL;
s->_bytesToBuffer = 0;
s->_bytesToBufferPos = 0;
s->_bytesToBufferReadPos = 0;
}
if (length == 0) {
s->_bytesToBuffer = 0;
s->_bytesToBufferPos = 0;
s->_bytesToBufferReadPos = 0;
if (s->_readBuffer) {
CFRelease(s->_readBuffer);
s->_readBuffer = NULL;
}
// Zero length buffer, smash the timeout
timeoutVal.tv_sec = 0;
timeoutVal.tv_usec = 0;
} else {
/* if the buffer shrank, we can re-use the old one */
if (length > s->_bytesToBuffer) {
if (s->_readBuffer) {
CFRelease(s->_readBuffer);
s->_readBuffer = NULL;
}
}
s->_bytesToBuffer = length;
s->_bytesToBufferPos = 0;
s->_bytesToBufferReadPos = 0;
if (s->_readBuffer == NULL) {
s->_readBuffer = CFDataCreateMutable(kCFAllocatorSystemDefault, length);
CFDataSetLength(s->_readBuffer, length);
}
}
}
if (timercmp(&s->_readBufferTimeout, &timeoutVal, !=)) {
s->_readBufferTimeout = timeoutVal;
__CFReadSocketsTimeoutInvalid = true;
}
__CFUnlock(&__CFActiveSocketsLock);
__CFSocketUnlock(s);
}
CFIndex __CFSocketRead(CFSocketRef s, UInt8* buffer, CFIndex length, int* error)
{
__CFSOCKETLOG_WS(s, "READING BYTES (%ld buffered, out of %ld desired, eof = %d, err = %d)", s->_bytesToBufferPos, s->_bytesToBuffer, s->_atEOF, s->_bufferedReadError);
CFIndex result = -1;
__CFSocketLock(s);
*error = 0;
/* Any leftover buffered bytes? */
if (s->_leftoverBytes) {
CFIndex ctBuffer = CFDataGetLength(s->_leftoverBytes);
#if defined(DEBUG)
fprintf(stderr, "%s(%ld): WARNING: Draining %ld leftover bytes first\n\n", __FUNCTION__, (long)__LINE__, (long)ctBuffer);
#endif
if (ctBuffer > length)
ctBuffer = length;
memcpy(buffer, CFDataGetBytePtr(s->_leftoverBytes), ctBuffer);
if (ctBuffer < CFDataGetLength(s->_leftoverBytes))
CFDataReplaceBytes(s->_leftoverBytes, CFRangeMake(0, ctBuffer), NULL, 0);
else {
CFRelease(s->_leftoverBytes);
s->_leftoverBytes = NULL;
}
result = ctBuffer;
goto unlock;
}
/* return whatever we've buffered */
if (s->_bytesToBuffer != 0) {
CFIndex ctBuffer = s->_bytesToBufferPos - s->_bytesToBufferReadPos;
if (ctBuffer > 0) {
/* drain our buffer first */
if (ctBuffer > length)
ctBuffer = length;
memcpy(buffer, CFDataGetBytePtr(s->_readBuffer) + s->_bytesToBufferReadPos, ctBuffer);
s->_bytesToBufferReadPos += ctBuffer;
if (s->_bytesToBufferReadPos == s->_bytesToBufferPos) {
__CFSOCKETLOG_WS(s, "DRAINED BUFFER - SHOULD START BUFFERING AGAIN");
s->_bytesToBufferPos = 0;
s->_bytesToBufferReadPos = 0;
}
__CFSOCKETLOG_WS(s, "SLURPED %ld BYTES FROM BUFFER %ld LEFT TO READ", ctBuffer, length);
result = ctBuffer;
goto unlock;
}
}
/* nothing buffered, or no buffer selected */
/* Did we get an error on a previous read (or buffered read)? */
if (s->_bufferedReadError != 0) {
__CFSOCKETLOG_WS(s, "RETURNING ERROR %d", s->_bufferedReadError);
*error = s->_bufferedReadError;
result = -1;
goto unlock;
}
/* nothing buffered, if we've hit eof, don't bother reading any more */
if (s->_atEOF) {
__CFSOCKETLOG_WS(s, "RETURNING EOF");
result = 0;
goto unlock;
}
/* normal read */
result = read(CFSocketGetNative(s), buffer, length);
__CFSOCKETLOG_WS(s, "READ %ld bytes", result);
if (result == 0) {
/* note that we hit EOF */
s->_atEOF = true;
} else if (result < 0) {
*error = errno;
/* if it wasn't EAGAIN, record it (although we shouldn't get called again) */
if (*error != EAGAIN) {
s->_bufferedReadError = *error;
}
}
unlock:
__CFSocketUnlock(s);
return result;
}
Boolean __CFSocketGetBytesAvailable(CFSocketRef s, CFIndex* ctBytesAvailable)
{
CFIndex ctBuffer = s->_bytesToBufferPos - s->_bytesToBufferReadPos;
if (ctBuffer != 0) {
*ctBytesAvailable = ctBuffer;
return true;
} else {
int result;
unsigned long bytesAvailable;
result = ioctlsocket(CFSocketGetNative(s), FIONREAD, &bytesAvailable);
if (result < 0)
return false;
*ctBytesAvailable = (CFIndex) bytesAvailable;
return true;
}
}
#if defined(LOG_CFSOCKET)
static void __CFSocketWriteSocketList(CFArrayRef sockets, CFDataRef fdSet, char* dst, CFIndex dstCount, Boolean onlyIfSet) {
int len = snprintf(dst, dstCount, "{");
dst += len;
dstCount -= len;
fd_set *tempfds = (fd_set *)CFDataGetBytePtr(fdSet);
SInt32 idx, cnt;
for (idx = 0, cnt = CFArrayGetCount(sockets); idx < cnt; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(sockets, idx);
len = 0;
if (FD_ISSET(s->_socket, tempfds)) {
len = snprintf(dst, dstCount, " %d ", s->_socket);
} else if (!onlyIfSet) {
len = snprintf(dst, dstCount, " (%d) ", s->_socket);
}
dst += len;
dstCount -= len;
}
snprintf(dst, dstCount, "}");
}
#endif
static void
clearInvalidFileDescriptors(CFMutableDataRef d)
{
if (d) {
#if TARGET_OS_WIN32
if (CFDataGetLength(d) == 0) {
return;
}
fd_set *fds = (fd_set *)CFDataGetMutableBytePtr(d);
fd_set invalidFds;
FD_ZERO(&invalidFds);
// Gather all invalid sockets into invalidFds set
for (u_int idx = 0; idx < fds->fd_count; idx++) {
SOCKET socket = fds->fd_array[idx];
if (! __CFNativeSocketIsValid(socket)) {
FD_SET(socket, &invalidFds);
}
}
// Remove invalid sockets from source set
for (u_int idx = 0; idx < invalidFds.fd_count; idx++) {
SOCKET socket = invalidFds.fd_array[idx];
FD_CLR(socket, fds);
}
#else
SInt32 count = __CFSocketFdGetSize(d);
fd_set* s = (fd_set*) CFDataGetMutableBytePtr(d);
for (SInt32 idx = 0; idx < count; idx++) {
if (FD_ISSET(idx, s))
if (! __CFNativeSocketIsValid(idx)) {
FD_CLR(idx, s);
}
}
#endif
}
}
static void
manageSelectError(SInt32 selectError)
{
__CFSOCKETLOG("socket manager received error %ld from select", (long)selectError);
if (EBADF == selectError) {
CFMutableArrayRef invalidSockets = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
__CFLock(&__CFActiveSocketsLock);
CFIndex cnt = CFArrayGetCount(__CFWriteSockets);
CFIndex idx;
for (idx = 0; idx < cnt; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(__CFWriteSockets, idx);
if (!__CFNativeSocketIsValid(s->_socket)) {
__CFSOCKETLOG_WS(s, "socket manager found write socket invalid");
CFArrayAppendValue(invalidSockets, s);
}
}
cnt = CFArrayGetCount(__CFReadSockets);
for (idx = 0; idx < cnt; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(__CFReadSockets, idx);
if (!__CFNativeSocketIsValid(s->_socket)) {
__CFSOCKETLOG_WS(s, "socket manager found read socket invalid");
CFArrayAppendValue(invalidSockets, s);
}
}
cnt = CFArrayGetCount(invalidSockets);
/* Note that we're doing this only when we got EBADF but otherwise
* don't have an explicit bad descriptor. Note that the lock is held now.
* Finally, note that cnt == 0 doesn't necessarily mean
* that this loop will do anything, since fd's may have been invalidated
* while we were in select.
*/
if (cnt == 0) {
__CFSOCKETLOG("socket manager received EBADF(1): No sockets were marked as invalid, cleaning out fdsets");
clearInvalidFileDescriptors(__CFReadSocketsFds);
clearInvalidFileDescriptors(__CFWriteSocketsFds);
}
__CFUnlock(&__CFActiveSocketsLock);
for (idx = 0; idx < cnt; idx++) {
CFSocketInvalidate(((CFSocketRef)CFArrayGetValueAtIndex(invalidSockets, idx)));
}
CFRelease(invalidSockets);
}
}
static void *__CFSocketManager(void * arg)
{
#if TARGET_OS_LINUX && !TARGET_OS_CYGWIN
pthread_setname_np(pthread_self(), "com.apple.CFSocket.private");
#elif !TARGET_OS_CYGWIN && !TARGET_OS_BSD
pthread_setname_np("com.apple.CFSocket.private");
#endif
SInt32 nrfds, maxnrfds, fdentries = 1;
SInt32 rfds, wfds;
fd_set *exceptfds = NULL;
#if TARGET_OS_WIN32
fd_set *writefds = (fd_set *)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(fd_set), 0);
fd_set *readfds = (fd_set *)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(fd_set), 0);
FD_ZERO(writefds);
FD_ZERO(readfds);
#else
fd_set *writefds = (fd_set *)CFAllocatorAllocate(kCFAllocatorSystemDefault, fdentries * sizeof(fd_mask), 0);
fd_set *readfds = (fd_set *)CFAllocatorAllocate(kCFAllocatorSystemDefault, fdentries * sizeof(fd_mask), 0);
#endif
fd_set *tempfds;
SInt32 idx, cnt;
uint8_t buffer[256];
CFMutableArrayRef selectedWriteSockets = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
CFMutableArrayRef selectedReadSockets = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
CFIndex selectedWriteSocketsIndex = 0, selectedReadSocketsIndex = 0;
struct timeval tv;
struct timeval* pTimeout = NULL;
struct timeval timeBeforeSelect = {0, 0};
for (;;) {
__CFLock(&__CFActiveSocketsLock);
__CFSocketManagerIteration++;
#if defined(LOG_CFSOCKET)
char* readBuffer = (char*) malloc(16384);
__CFSocketWriteSocketList(__CFReadSockets, __CFReadSocketsFds, readBuffer, 16384, FALSE);
char* writeBuffer = (char*) malloc(16384);
__CFSocketWriteSocketList(__CFWriteSockets, __CFWriteSocketsFds, writeBuffer, 16384, FALSE);
__CFSOCKETLOG("socket manager iteration %lu looking at: read sockets %s, write sockets %s", (unsigned long)__CFSocketManagerIteration, readBuffer, writeBuffer);
free(readBuffer);
free(writeBuffer);
#endif
#if TARGET_OS_WIN32
// This parameter is ignored by `select` from Winsock2 API
maxnrfds = INT_MAX;
#else
rfds = __CFSocketFdGetSize(__CFReadSocketsFds);
wfds = __CFSocketFdGetSize(__CFWriteSocketsFds);
maxnrfds = __CFMax(rfds, wfds);
if (maxnrfds > fdentries * (int)NFDBITS) {
fdentries = (maxnrfds + NFDBITS - 1) / NFDBITS;
writefds = __CFSafelyReallocateWithAllocator(kCFAllocatorSystemDefault, writefds, fdentries * sizeof(fd_mask), 0, NULL);
readfds = __CFSafelyReallocateWithAllocator(kCFAllocatorSystemDefault, readfds, fdentries * sizeof(fd_mask), 0, NULL);
}
memset(writefds, 0, fdentries * sizeof(fd_mask));
memset(readfds, 0, fdentries * sizeof(fd_mask));
#endif
CFDataGetBytes(__CFWriteSocketsFds, CFRangeMake(0, CFDataGetLength(__CFWriteSocketsFds)), (UInt8 *)writefds);
CFDataGetBytes(__CFReadSocketsFds, CFRangeMake(0, CFDataGetLength(__CFReadSocketsFds)), (UInt8 *)readfds);
if (__CFReadSocketsTimeoutInvalid) {
struct timeval* minTimeout = NULL;
__CFReadSocketsTimeoutInvalid = false;
__CFSOCKETLOG("Figuring out which sockets have timeouts...");
CFArrayApplyFunction(__CFReadSockets, CFRangeMake(0, CFArrayGetCount(__CFReadSockets)), _calcMinTimeout_locked, (void*) &minTimeout);
if (minTimeout == NULL) {
__CFSOCKETLOG("No one wants a timeout!");
pTimeout = NULL;
} else {
__CFSOCKETLOG("timeout will be %ld, %d!", minTimeout->tv_sec, minTimeout->tv_usec);
tv = *minTimeout;
pTimeout = &tv;
}
}
if (pTimeout) {
__CFSOCKETLOG("select will have a %ld, %d timeout", pTimeout->tv_sec, pTimeout->tv_usec);
gettimeofday(&timeBeforeSelect, NULL);
}
__CFUnlock(&__CFActiveSocketsLock);
#if TARGET_OS_WIN32
// On Windows, select checks connection failed sockets via the exceptfds parameter. connection succeeded is checked via writefds. We need both.
exceptfds = writefds;
#elif defined(LOG_CFSOCKET) && defined(DEBUG_POLLING_SELECT)
if (pTimeout == NULL) {
/* If there's anyone with leftover bytes, they'll need to be awoken immediately */
static struct timeval sKickerTime = { 5, 0 };
pTimeout = &sKickerTime;
__CFSOCKETLOG("Setting 5s select timeout as debug measure");
}
if (exceptfds == NULL) {
exceptfds = (fd_set*) malloc(maxnrfds * NFDBITS);
bzero(exceptfds, maxnrfds * NFDBITS);
}
#endif
SInt32 error = 0;
nrfds = select(maxnrfds, readfds, writefds, exceptfds, pTimeout);
if (nrfds < 0) {
// Store error as early as possible, as the code below could
// reset it and make late check unreliable.
error = __CFSocketLastError();
}
#if defined(LOG_CFSOCKET) && defined(DEBUG_POLLING_SELECT)
__CFSOCKETLOG("socket manager woke from select, ret=%ld", (long)nrfds);
if (nrfds < 0 && exceptfds && exceptfds != writefds) {
CFMutableStringRef s = NULL;
for (int i = 0; i < nrfds; i++) {
if (FD_ISSET(i, exceptfds)) {
if (s == NULL) {
s = CFStringCreateMutable(kCFAllocatorDefault, 0);
CFStringAppendCString(s, "Error set { ", kCFStringEncodingUTF8);
}
CFStringAppendFormat(s, NULL, CFSTR("%d "), i);
}
}
if (s == NULL)
__CFSOCKETLOG("Error from select errno %d, but no fds specified", errno);
else {
CFStringAppendFormat(s, NULL, CFSTR("}"));
__CFSOCKETLOG("Error from select errno %d, %@", errno, s);
CFRelease(s);
}
free(exceptfds);
exceptfds = nil;
}
#endif
/*
* select returned a timeout
*/
if (0 == nrfds) {
Boolean didFindOne = false;
struct timeval timeAfterSelect;
struct timeval deltaTime;
gettimeofday(&timeAfterSelect, NULL);
/* timeBeforeSelect becomes the delta */
timersub(&timeAfterSelect, &timeBeforeSelect, &deltaTime);
__CFSOCKETLOG("Socket manager received timeout - kicking off expired reads (expired delta %ld, %d)", deltaTime.tv_sec, deltaTime.tv_usec);
__CFLock(&__CFActiveSocketsLock);
tempfds = NULL;
cnt = CFArrayGetCount(__CFReadSockets);
for (idx = 0; idx < cnt; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(__CFReadSockets, idx);
if (timerisset(&s->_readBufferTimeout) || s->_leftoverBytes) {
didFindOne = true;
CFSocketNativeHandle sock = s->_socket;
// We might have an new element in __CFReadSockets that we weren't listening to,
// in which case we must be sure not to test a bit in the fdset that is
// outside our mask size.
Boolean sockInBounds = (0 <= sock && sock < maxnrfds);
/* if this sockets timeout is less than or equal elapsed time, then signal it */
if (INVALID_SOCKET != sock && sockInBounds) {
__CFSOCKETLOG_WS(s, "Expiring socket (delta %ld, %d)", s->_readBufferTimeout.tv_sec, s->_readBufferTimeout.tv_usec);
CFArraySetValueAtIndex(selectedReadSockets, selectedReadSocketsIndex, s);
selectedReadSocketsIndex++;
/* socket is removed from fds here, will be restored in read handling or in perform function */
if (!tempfds) tempfds = (fd_set *)CFDataGetMutableBytePtr(__CFReadSocketsFds);
FD_CLR(sock, tempfds);
}
}
}
__CFUnlock(&__CFActiveSocketsLock);
/* and below, we dispatch through the normal read dispatch mechanism */
if (! didFindOne) {
#if defined(LOG_CFSOCKET) && defined(DEBUG_POLLING_SELECT)
__CFSOCKETLOG("select() timeout - but no sockets actually timed out. Iteration %lu", (unsigned long) __CFSocketManagerIteration);
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent() + 3;
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopDefaultMode, ^{
CFTimeInterval dt = CFAbsoluteTimeGetCurrent() - endTime;
if (dt > 0) {
__CFSOCKETLOG("select() timeout %lu - took %.05f for the main runloop (TOO LONG!)", __CFSocketManagerIteration, dt);
} else {
__CFSOCKETLOG("select() timeout %lu - took %.05f for the main runloop", __CFSocketManagerIteration, dt < 0? -dt : dt);
}
});
CFRunLoopWakeUp(CFRunLoopGetMain());
#endif
}
}
if (0 > nrfds) {
manageSelectError(error);
continue;
}
if (FD_ISSET(__CFWakeupSocketPair[1], readfds)) {
recv(__CFWakeupSocketPair[1], (char *)buffer, sizeof(buffer), 0);
__CFSOCKETLOG("socket manager received %c on wakeup socket\n", buffer[0]);
}
__CFLock(&__CFActiveSocketsLock);
tempfds = NULL;
cnt = CFArrayGetCount(__CFWriteSockets);
for (idx = 0; idx < cnt; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(__CFWriteSockets, idx);
CFSocketNativeHandle sock = s->_socket;
// We might have an new element in __CFWriteSockets that we weren't listening to,
// in which case we must be sure not to test a bit in the fdset that is
// outside our mask size.
Boolean sockInBounds = (0 <= sock && sock < maxnrfds);
if (INVALID_SOCKET != sock && sockInBounds) {
if (FD_ISSET(sock, writefds)) {
CFArraySetValueAtIndex(selectedWriteSockets, selectedWriteSocketsIndex, s);
selectedWriteSocketsIndex++;
/* socket is removed from fds here, restored by CFSocketReschedule */
if (!tempfds) tempfds = (fd_set *)CFDataGetMutableBytePtr(__CFWriteSocketsFds);
FD_CLR(sock, tempfds);
__CFSOCKETLOG_WS(s, "Manager: cleared socket from write fds");
}
}
}
tempfds = NULL;
cnt = CFArrayGetCount(__CFReadSockets);
struct timeval timeNow = { 0 };
if (pTimeout) {
gettimeofday(&timeNow, NULL);
}
for (idx = 0; idx < cnt; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(__CFReadSockets, idx);
CFSocketNativeHandle sock = s->_socket;
// We might have an new element in __CFReadSockets that we weren't listening to,
// in which case we must be sure not to test a bit in the fdset that is
// outside our mask size.
Boolean sockInBounds = (0 <= sock && sock < maxnrfds);
// Check if we hit the timeout
s->_hitTheTimeout = false;
if (pTimeout && sockInBounds && 0 != nrfds && !FD_ISSET(sock, readfds) &&
timerisset(&s->_readBufferTimeoutNotificationTime) &&
timercmp(&timeNow, &s->_readBufferTimeoutNotificationTime, >))
{
s->_hitTheTimeout = true;
}
if (INVALID_SOCKET != sock && sockInBounds && (FD_ISSET(sock, readfds) || s->_hitTheTimeout)) {
CFArraySetValueAtIndex(selectedReadSockets, selectedReadSocketsIndex, s);
selectedReadSocketsIndex++;
/* socket is removed from fds here, will be restored in read handling or in perform function */
if (!tempfds) tempfds = (fd_set *)CFDataGetMutableBytePtr(__CFReadSocketsFds);
FD_CLR(sock, tempfds);
}
}
__CFUnlock(&__CFActiveSocketsLock);
for (idx = 0; idx < selectedWriteSocketsIndex; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(selectedWriteSockets, idx);
if (kCFNull == (CFNullRef)s) continue;
__CFSOCKETLOG_WS(s, "socket manager signaling for write", s, s->_socket);
__CFSocketHandleWrite(s, FALSE);
CFArraySetValueAtIndex(selectedWriteSockets, idx, kCFNull);
}
selectedWriteSocketsIndex = 0;
for (idx = 0; idx < selectedReadSocketsIndex; idx++) {
CFSocketRef s = (CFSocketRef)CFArrayGetValueAtIndex(selectedReadSockets, idx);
if (kCFNull == (CFNullRef)s) continue;
__CFSOCKETLOG_WS(s, "socket manager signaling for read", s, s->_socket);
__CFSocketHandleRead(s, nrfds == 0 || s->_hitTheTimeout);
CFArraySetValueAtIndex(selectedReadSockets, idx, kCFNull);
}
selectedReadSocketsIndex = 0;
}
return NULL;
}
static CFStringRef __CFSocketCopyDescription(CFTypeRef cf) {
CFSocketRef s = (CFSocketRef)cf;
CFMutableStringRef result;
CFStringRef contextDesc = NULL;
void *contextInfo = NULL;
CFStringRef (*contextCopyDescription)(const void *info) = NULL;
result = CFStringCreateMutable(CFGetAllocator(s), 0);
__CFSocketLock(s);
void *addr = s->_callout;
#if TARGET_OS_MAC
Dl_info info;
const char *name = (dladdr(addr, &info) && info.dli_saddr == addr && info.dli_sname) ? info.dli_sname : "???";
#else
// don't bother trying to figure out callout names
const char *name = "<unknown>";
#endif
CFStringAppendFormat(result, NULL, CFSTR("<CFSocket %p [%p]>{valid = %s, type = %d, socket = %d, socket set count = %ld,\n callback types = 0x%x, callout = %s (%p), source = %p,\n run loops = %@,\n context = "), cf, CFGetAllocator(s), (__CFSocketIsValid(s) ? "Yes" : "No"), (int)(s->_socketType), s->_socket, (long)s->_socketSetCount, __CFSocketCallBackTypes(s), name, addr, s->_source0, s->_runLoops);
contextInfo = s->_context.info;
contextCopyDescription = s->_context.copyDescription;
__CFSocketUnlock(s);
if (NULL != contextInfo && NULL != contextCopyDescription) {
contextDesc = (CFStringRef)contextCopyDescription(contextInfo);
}
if (NULL == contextDesc) {
contextDesc = CFStringCreateWithFormat(CFGetAllocator(s), NULL, CFSTR("<CFSocket context %p>"), contextInfo);
}
CFStringAppend(result, contextDesc);
CFStringAppend(result, CFSTR("}"));
CFRelease(contextDesc);
return result;
}
static void __CFSocketDeallocate(CFTypeRef cf) {
/* Since CFSockets are cached, we can only get here sometime after being invalidated */
CFSocketRef s = (CFSocketRef)cf;
if (NULL != s->_address) {
CFRelease(s->_address);
s->_address = NULL;
}
if (NULL != s->_readBuffer) {
CFRelease(s->_readBuffer);
s->_readBuffer = NULL;
}
if (NULL != s->_leftoverBytes) {
CFRelease(s->_leftoverBytes);
s->_leftoverBytes = NULL;
}
timerclear(&s->_readBufferTimeout);
s->_bytesToBuffer = 0;
s->_bytesToBufferPos = 0;
s->_bytesToBufferReadPos = 0;
s->_atEOF = true;
s->_bufferedReadError = 0;
}
const CFRuntimeClass __CFSocketClass = {
0,
"CFSocket",
NULL, // init
NULL, // copy
__CFSocketDeallocate,
NULL, // equal
NULL, // hash
NULL, //
__CFSocketCopyDescription
};
CFTypeID CFSocketGetTypeID(void) {
static dispatch_once_t initOnce;
dispatch_once(&initOnce, ^{
#if TARGET_OS_MAC
struct rlimit lim1;
int ret1 = getrlimit(RLIMIT_NOFILE, &lim1);
int mib[] = {CTL_KERN, KERN_MAXFILESPERPROC};
int maxfd = 0;
size_t len = sizeof(int);
int ret0 = sysctl(mib, 2, &maxfd, &len, NULL, 0);
if (0 == ret0 && 0 == ret1 && lim1.rlim_max < maxfd) maxfd = lim1.rlim_max;
if (0 == ret1 && lim1.rlim_cur < maxfd) {
struct rlimit lim2 = lim1;
lim2.rlim_cur += 2304;
if (maxfd < lim2.rlim_cur) lim2.rlim_cur = maxfd;
setrlimit(RLIMIT_NOFILE, &lim2);
// we try, but do not go to extraordinary measures
}
#endif
});
return _kCFRuntimeIDCFSocket;
}
#if TARGET_OS_WIN32
struct _args {
void *func;
void *arg;
HANDLE handle;
};
static unsigned __stdcall __CFWinThreadFunc(void *arg) {
struct _args *args = (struct _args*)arg;
((void (*)(void *))args->func)(args->arg);
CloseHandle(args->handle);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, arg);
_endthreadex(0);
return 0;
}
#endif
static CFSocketRef _CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketNativeHandle sock, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context, Boolean useExistingInstance) {
CHECK_FOR_FORK();
CFSocketRef memory;
int typeSize = sizeof(memory->_socketType);
__CFLock(&__CFActiveSocketsLock);
if (NULL == __CFReadSockets) __CFSocketInitializeSockets();
__CFUnlock(&__CFActiveSocketsLock);
__CFLock(&__CFAllSocketsLock);
if (NULL == __CFAllSockets) {
__CFAllSockets = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
}
if (INVALID_SOCKET != sock && CFDictionaryGetValueIfPresent(__CFAllSockets, (void *)(uintptr_t)sock, (const void **)&memory)) {
if (useExistingInstance) {
__CFUnlock(&__CFAllSocketsLock);
CFRetain(memory);
return memory;
} else {
__CFSOCKETLOG("useExistingInstance is FALSE, removing existing instance %p from __CFAllSockets\n", memory);
__CFUnlock(&__CFAllSocketsLock);
CFSocketInvalidate(memory);
__CFLock(&__CFAllSocketsLock);
}
}
memory = (CFSocketRef)_CFRuntimeCreateInstance(allocator, CFSocketGetTypeID(), sizeof(struct __CFSocket) - sizeof(CFRuntimeBase), NULL);
if (NULL == memory) {
__CFUnlock(&__CFAllSocketsLock);
return NULL;
}
__CFSocketSetCallBackTypes(memory, callBackTypes);
if (INVALID_SOCKET != sock) __CFSocketSetValid(memory);
__CFSocketUnsetWriteSignalled(memory);
__CFSocketUnsetReadSignalled(memory);
memory->_f.client = ((callBackTypes & (~kCFSocketConnectCallBack)) & (~kCFSocketWriteCallBack)) | kCFSocketCloseOnInvalidate;
memory->_lock = CFLockInit;
memory->_writeLock = CFLockInit;
memory->_socket = sock;
if (INVALID_SOCKET == sock || 0 != getsockopt(sock, SOL_SOCKET, SO_TYPE, (char *)&(memory->_socketType), (socklen_t *)&typeSize)) memory->_socketType = 0; // cast for WinSock bad API
if (INVALID_SOCKET != sock) {
CFArrayCallBacks retainingCallbacks = kCFTypeArrayCallBacks;
retainingCallbacks.copyDescription = NULL;
memory->_runLoops = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &retainingCallbacks);
}
memory->_callout = callout;
timerclear(&memory->_readBufferTimeout);
timerclear(&memory->_readBufferTimeoutNotificationTime);
if (INVALID_SOCKET != sock) CFDictionaryAddValue(__CFAllSockets, (void *)(uintptr_t)sock, memory);
if (NULL == __CFSocketManagerThread) {
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
_CFThreadRef tid = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
#if TARGET_OS_MAC
pthread_attr_set_qos_class_np(&attr, qos_class_main(), 0);
#endif
pthread_create(&tid, &attr, __CFSocketManager, 0);
pthread_attr_destroy(&attr);
_Static_assert(sizeof(_CFThreadRef) == sizeof(void *), "_CFThreadRef is not pointer sized");
__CFSocketManagerThread = (void *)tid;
#elif TARGET_OS_WIN32
unsigned tid;
struct _args *args = (struct _args*)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(struct _args), 0);
if (__CFOASafe) __CFSetLastAllocationEventName(args, "CFUtilities (thread-args)");
HANDLE handle;
args->func = __CFSocketManager;
args->arg = 0;
/* The thread is created suspended, because otherwise there would be a race between the assignment below of the handle field, and it's possible use in the thread func above. */
args->handle = (HANDLE)_beginthreadex(NULL, 0, __CFWinThreadFunc, args, CREATE_SUSPENDED, &tid);
handle = args->handle;
ResumeThread(handle);
__CFSocketManagerThread = handle;
#endif
}
__CFUnlock(&__CFAllSocketsLock);
if (NULL != context) {
void *contextInfo = context->retain ? (void *)context->retain(context->info) : context->info;
__CFSocketLock(memory);
memory->_context.retain = context->retain;
memory->_context.release = context->release;
memory->_context.copyDescription = context->copyDescription;
memory->_context.info = contextInfo;
__CFSocketUnlock(memory);
}
__CFSOCKETLOG("created socket %p (%d) with callbacks 0x%x, callout %p", memory, memory->_socket, callBackTypes, callout);
return memory;
}
CFSocketRef CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketNativeHandle sock, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context) {
return _CFSocketCreateWithNative(allocator, sock, callBackTypes, callout, context, TRUE);
}
void CFSocketInvalidate(CFSocketRef s) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
UInt32 previousSocketManagerIteration;
__CFGenericValidateType(s, CFSocketGetTypeID());
__CFSOCKETLOG_WS(s, "flags 0x%x disabled 0x%x connected 0x%x\n", s->_f.client, s->_f.disabled, s->_f.connected);
CFRetain(s);
__CFLock(&__CFAllSocketsLock);
__CFSocketLock(s);
if (__CFSocketIsValid(s)) {
SInt32 idx;
CFRunLoopSourceRef source0;
void *contextInfo = NULL;
void (*contextRelease)(const void *info) = NULL;
__CFSocketUnsetValid(s);
__CFSocketUnsetWriteSignalled(s);
__CFSocketUnsetReadSignalled(s);
__CFLock(&__CFActiveSocketsLock);
idx = CFArrayGetFirstIndexOfValue(__CFWriteSockets, CFRangeMake(0, CFArrayGetCount(__CFWriteSockets)), s);
if (0 <= idx) {
CFArrayRemoveValueAtIndex(__CFWriteSockets, idx);
__CFSocketClearFDForWrite(s);
}
// No need to clear FD's for V1 sources, since we'll just throw the whole event away
idx = CFArrayGetFirstIndexOfValue(__CFReadSockets, CFRangeMake(0, CFArrayGetCount(__CFReadSockets)), s);
if (0 <= idx) {
CFArrayRemoveValueAtIndex(__CFReadSockets, idx);
__CFSocketClearFDForRead(s);
}
previousSocketManagerIteration = __CFSocketManagerIteration;
__CFUnlock(&__CFActiveSocketsLock);
CFDictionaryRemoveValue(__CFAllSockets, (void *)(uintptr_t)(s->_socket));
if ((s->_f.client & kCFSocketCloseOnInvalidate) != 0) closesocket(s->_socket);
s->_socket = INVALID_SOCKET;
if (NULL != s->_peerAddress) {
CFRelease(s->_peerAddress);
s->_peerAddress = NULL;
}
if (NULL != s->_dataQueue) {
CFRelease(s->_dataQueue);
s->_dataQueue = NULL;
}
if (NULL != s->_addressQueue) {
CFRelease(s->_addressQueue);
s->_addressQueue = NULL;
}
s->_socketSetCount = 0;
// we'll need this later
CFArrayRef runLoops = (CFArrayRef)CFRetain(s->_runLoops);
CFRelease(s->_runLoops);
s->_runLoops = NULL;
source0 = s->_source0;
s->_source0 = NULL;
contextInfo = s->_context.info;
contextRelease = s->_context.release;
s->_context.info = 0;
s->_context.retain = 0;
s->_context.release = 0;
s->_context.copyDescription = 0;
__CFSocketUnlock(s);
// Do this after the socket unlock to avoid deadlock (10462525)
for (idx = CFArrayGetCount(runLoops); idx--;) {
CFRunLoopWakeUp((CFRunLoopRef)CFArrayGetValueAtIndex(runLoops, idx));
}
CFRelease(runLoops);
if (NULL != contextRelease) {
contextRelease(contextInfo);
}
if (NULL != source0) {
CFRunLoopSourceInvalidate(source0);
CFRelease(source0);
}
} else {
__CFSocketUnlock(s);
}
__CFUnlock(&__CFAllSocketsLock);
__CFSOCKETLOG("done for %p", s);
CFRelease(s);
}
Boolean CFSocketIsValid(CFSocketRef s) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
return __CFSocketIsValid(s);
}
CFSocketNativeHandle CFSocketGetNative(CFSocketRef s) {
CF_ASSERT_TYPE_OR_NULL(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
return s == NULL? -1 : s->_socket;
}
CFDataRef CFSocketCopyAddress(CFSocketRef s) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
CFDataRef result = NULL;
__CFSocketLock(s);
__CFSocketEstablishAddress(s);
if (NULL != s->_address) {
result = (CFDataRef)CFRetain(s->_address);
}
__CFSocketUnlock(s);
#if defined(LOG_CFSOCKET)
CFStringRef local = copyLocalAddress(kCFAllocatorDefault, s->_socket);
CFStringRef peer = copyPeerAddress(kCFAllocatorDefault, s->_socket);
__CFSOCKETLOG_WS(s, "addresses local %@ peer %@", local, peer);
if (local)
CFRelease(local);
if (peer)
CFRelease(peer);
#endif
return result;
}
CFDataRef CFSocketCopyPeerAddress(CFSocketRef s) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
CFDataRef result = NULL;
__CFSocketLock(s);
__CFSocketEstablishPeerAddress(s);
if (NULL != s->_peerAddress) {
result = (CFDataRef)CFRetain(s->_peerAddress);
}
__CFSocketUnlock(s);
#if defined(LOG_CFSOCKET)
CFStringRef local = copyLocalAddress(kCFAllocatorDefault, s->_socket);
CFStringRef peer = copyPeerAddress(kCFAllocatorDefault, s->_socket);
__CFSOCKETLOG_WS(s, "addresses local %@ peer %@", local, peer);
if (local)
CFRelease(local);
if (peer)
CFRelease(peer);
#endif
return result;
}
void CFSocketGetContext(CFSocketRef s, CFSocketContext *context) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
CFAssert1(0 == context->version, __kCFLogAssertion, "%s(): context version not initialized to 0", __PRETTY_FUNCTION__);
*context = s->_context;
}
CFOptionFlags CFSocketGetSocketFlags(CFSocketRef s) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
return s->_f.client;
}
void CFSocketSetSocketFlags(CFSocketRef s, CFOptionFlags flags) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
__CFSocketLock(s);
#if LOG_CFSOCKET
CFOptionFlags oldFlags = s->_f.client;
#endif
s->_f.client = flags;
__CFSocketUnlock(s);
__CFSOCKETLOG_WS(s, "set flags 0x%x (was 0x%x)", flags, oldFlags);
}
void CFSocketDisableCallBacks(CFSocketRef s, CFOptionFlags callBackTypes) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
Boolean wakeup = false;
uint8_t readCallBackType;
__CFSocketLock(s);
if (__CFSocketIsValid(s) && __CFSocketIsScheduled(s)) {
callBackTypes &= __CFSocketCallBackTypes(s);
readCallBackType = __CFSocketReadCallBackType(s);
s->_f.disabled |= callBackTypes;
__CFSOCKETLOG_WS(s, "unscheduling with flags 0x%x disabled 0x%x connected 0x%x for types 0x%lx\n", s->_f.client, s->_f.disabled, s->_f.connected, callBackTypes);
__CFLock(&__CFActiveSocketsLock);
if ((readCallBackType == kCFSocketAcceptCallBack) || !__CFSocketIsConnectionOriented(s)) s->_f.connected = TRUE;
if (((callBackTypes & kCFSocketWriteCallBack) != 0) || (((callBackTypes & kCFSocketConnectCallBack) != 0) && !s->_f.connected)) {
if (__CFSocketClearFDForWrite(s)) {
// do not wake up the socket manager thread if all relevant write callbacks are disabled
CFOptionFlags writeCallBacksAvailable = __CFSocketCallBackTypes(s) & (kCFSocketWriteCallBack | kCFSocketConnectCallBack);
if (s->_f.connected) writeCallBacksAvailable &= ~kCFSocketConnectCallBack;
if ((s->_f.disabled & writeCallBacksAvailable) != writeCallBacksAvailable) wakeup = true;
}
}
if (readCallBackType != kCFSocketNoCallBack && (callBackTypes & readCallBackType) != 0) {
if (__CFSocketClearFDForRead(s)) {
// do not wake up the socket manager thread if callback type is read
if (readCallBackType != kCFSocketReadCallBack) wakeup = true;
}
}
__CFUnlock(&__CFActiveSocketsLock);
}
__CFSocketUnlock(s);
}
// "force" means to clear the disabled bits set by DisableCallBacks and always reenable.
// if (!force) we respect those bits, meaning they may stop us from enabling.
// In addition, if !force we assume that the sockets have already been added to the
// __CFReadSockets and __CFWriteSockets arrays. This is true because the callbacks start
// enabled when the CFSocket is created (at which time we enable with force).
// Called with SocketLock held, returns with it released!
void __CFSocketEnableCallBacks(CFSocketRef s, CFOptionFlags callBackTypes, Boolean force, uint8_t wakeupChar) {
CHECK_FOR_FORK();
Boolean wakeup = FALSE;
if (!callBackTypes) {
__CFSocketUnlock(s);
return;
}
if (__CFSocketIsValid(s) && __CFSocketIsScheduled(s)) {
Boolean turnOnWrite = FALSE, turnOnConnect = FALSE, turnOnRead = FALSE;
uint8_t readCallBackType = __CFSocketReadCallBackType(s);
callBackTypes &= __CFSocketCallBackTypes(s);
if (force) s->_f.disabled &= ~callBackTypes;
__CFSOCKETLOG_WS(s, "rescheduling with flags 0x%x disabled 0x%x connected 0x%x for types 0x%lx\n", s->_f.client, s->_f.disabled, s->_f.connected, callBackTypes);
/* We will wait for connection only for connection-oriented, non-rendezvous sockets that are not already connected. Mark others as already connected. */
if ((readCallBackType == kCFSocketAcceptCallBack) || !__CFSocketIsConnectionOriented(s)) s->_f.connected = TRUE;
// First figure out what to turn on
if (s->_f.connected || (callBackTypes & kCFSocketConnectCallBack) == 0) {
// if we want write callbacks and they're not disabled...
if ((callBackTypes & kCFSocketWriteCallBack) != 0 && (s->_f.disabled & kCFSocketWriteCallBack) == 0) turnOnWrite = TRUE;
} else {
// if we want connect callbacks and they're not disabled...
if ((callBackTypes & kCFSocketConnectCallBack) != 0 && (s->_f.disabled & kCFSocketConnectCallBack) == 0) turnOnConnect = TRUE;
}
// if we want read callbacks and they're not disabled...
if (readCallBackType != kCFSocketNoCallBack && (callBackTypes & readCallBackType) != 0 && (s->_f.disabled & kCFSocketReadCallBack) == 0) turnOnRead = TRUE;
// Now turn on the callbacks we've determined that we want on
if (turnOnRead || turnOnWrite || turnOnConnect) {
__CFLock(&__CFActiveSocketsLock);
if (turnOnWrite || turnOnConnect) {
if (force) {
SInt32 idx = CFArrayGetFirstIndexOfValue(__CFWriteSockets, CFRangeMake(0, CFArrayGetCount(__CFWriteSockets)), s);
if (kCFNotFound == idx)
CFArrayAppendValue(__CFWriteSockets, s);
if (kCFNotFound == idx)
__CFSOCKETLOG_WS(s, "put %p __CFWriteSockets list due to force and non-presence");
}
if (__CFSocketSetFDForWrite(s)) wakeup = true;
}
if (turnOnRead) {
if (force) {
SInt32 idx = CFArrayGetFirstIndexOfValue(__CFReadSockets, CFRangeMake(0, CFArrayGetCount(__CFReadSockets)), s);
if (kCFNotFound == idx) CFArrayAppendValue(__CFReadSockets, s);
}
if (__CFSocketSetFDForRead(s)) wakeup = true;
}
__CFUnlock(&__CFActiveSocketsLock);
}
}
__CFSocketUnlock(s);
}
void CFSocketEnableCallBacks(CFSocketRef s, CFOptionFlags callBackTypes) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
__CFSocketLock(s);
__CFSocketEnableCallBacks(s, callBackTypes, TRUE, 'r');
__CFSOCKETLOG_WS(s, "done for callbackTypes %x", callBackTypes);
}
static void __CFSocketSchedule(void *info, CFRunLoopRef rl, CFStringRef mode) {
CFSocketRef s = (CFSocketRef)info;
__CFSocketLock(s);
//??? also need to arrange delivery of all pending data
if (__CFSocketIsValid(s)) {
CFMutableArrayRef runLoopsOrig = s->_runLoops;
CFMutableArrayRef runLoopsCopy = CFArrayCreateMutableCopy(kCFAllocatorSystemDefault, 0, s->_runLoops);
CFArrayAppendValue(runLoopsCopy, rl);
s->_runLoops = runLoopsCopy;
CFRelease(runLoopsOrig);
s->_socketSetCount++;
// Since the v0 source is listened to on the SocketMgr thread, no matter how many modes it
// is added to we just need to enable it there once (and _socketSetCount gives us a refCount
// to know when we can finally disable it).
if (1 == s->_socketSetCount) {
__CFSOCKETLOG_WS(s, "rl %p (%s), mode %@", rl, (CFRunLoopGetCurrent() == rl? "current" : CFRunLoopGetMain() == rl? "main" : ""), mode);
__CFSocketEnableCallBacks(s, __CFSocketCallBackTypes(s), TRUE, 's'); // unlocks s
} else
__CFSocketUnlock(s);
} else
__CFSocketUnlock(s);
}
static void __CFSocketCancel(void *info, CFRunLoopRef rl, CFStringRef mode) {
CFSocketRef s = (CFSocketRef)info;
SInt32 idx;
__CFSocketLock(s);
s->_socketSetCount--;
if (0 == s->_socketSetCount) {
__CFLock(&__CFActiveSocketsLock);
idx = CFArrayGetFirstIndexOfValue(__CFWriteSockets, CFRangeMake(0, CFArrayGetCount(__CFWriteSockets)), s);
if (0 <= idx) {
__CFSOCKETLOG_WS(s, "removing from __CFWriteSockets list");
CFArrayRemoveValueAtIndex(__CFWriteSockets, idx);
__CFSocketClearFDForWrite(s);
}
idx = CFArrayGetFirstIndexOfValue(__CFReadSockets, CFRangeMake(0, CFArrayGetCount(__CFReadSockets)), s);
if (0 <= idx) {
CFArrayRemoveValueAtIndex(__CFReadSockets, idx);
__CFSocketClearFDForRead(s);
}
__CFUnlock(&__CFActiveSocketsLock);
}
if (NULL != s->_runLoops) {
CFMutableArrayRef runLoopsOrig = s->_runLoops;
CFMutableArrayRef runLoopsCopy = CFArrayCreateMutableCopy(kCFAllocatorSystemDefault, 0, s->_runLoops);
idx = CFArrayGetFirstIndexOfValue(runLoopsCopy, CFRangeMake(0, CFArrayGetCount(runLoopsCopy)), rl);
if (0 <= idx) CFArrayRemoveValueAtIndex(runLoopsCopy, idx);
s->_runLoops = runLoopsCopy;
CFRelease(runLoopsOrig);
}
__CFSocketUnlock(s);
}
// Note: must be called with socket lock held, then returns with it released
// Used by both the v0 and v1 RunLoopSource perform routines
static void __CFSocketDoCallback(CFSocketRef s, CFDataRef data, CFDataRef address, CFSocketNativeHandle sock) {
CFSocketCallBack callout = NULL;
void *contextInfo = NULL;
SInt32 errorCode = 0;
Boolean readSignalled = false, writeSignalled = false, connectSignalled = false, calledOut = false;
uint8_t readCallBackType, callBackTypes;
callBackTypes = __CFSocketCallBackTypes(s);
readCallBackType = __CFSocketReadCallBackType(s);
readSignalled = __CFSocketIsReadSignalled(s);
writeSignalled = __CFSocketIsWriteSignalled(s);
connectSignalled = writeSignalled && !s->_f.connected;
__CFSocketUnsetReadSignalled(s);
__CFSocketUnsetWriteSignalled(s);
callout = s->_callout;
contextInfo = s->_context.info;
__CFSOCKETLOG_WS(s, "entering perform with read signalled %d write signalled %d connect signalled %d callback types %d", readSignalled, writeSignalled, connectSignalled, callBackTypes);
if (writeSignalled) {
errorCode = s->_errorCode;
s->_f.connected = TRUE;
}
__CFSocketUnlock(s);
if ((callBackTypes & kCFSocketConnectCallBack) != 0) {
if (connectSignalled && (!calledOut || CFSocketIsValid(s))) {
__CFSOCKETLOG_WS(s, "doing connect callback (%p), error: %d", callout, errorCode);
if (errorCode) {
if (callout) callout(s, kCFSocketConnectCallBack, NULL, &errorCode, contextInfo);
calledOut = true;
} else {
if (callout) callout(s, kCFSocketConnectCallBack, NULL, NULL, contextInfo);
calledOut = true;
}
}
}
if (kCFSocketDataCallBack == readCallBackType) {
if (NULL != data && (!calledOut || CFSocketIsValid(s))) {
SInt32 datalen = CFDataGetLength(data);
__CFSOCKETLOG_WS(s, "perform calling out data of length %ld", (long)datalen);
if (callout) callout(s, kCFSocketDataCallBack, address, data, contextInfo);
calledOut = true;
if (0 == datalen && __CFSocketIsConnectionOriented(s)) CFSocketInvalidate(s);
}
} else if (kCFSocketAcceptCallBack == readCallBackType) {
if (INVALID_SOCKET != sock && (!calledOut || CFSocketIsValid(s))) {
__CFSOCKETLOG_WS(s, "perform calling out accept");
if (callout) callout(s, kCFSocketAcceptCallBack, address, &sock, contextInfo);
calledOut = true;
}
} else if (kCFSocketReadCallBack == readCallBackType) {
if (readSignalled && (!calledOut || CFSocketIsValid(s))) {
__CFSOCKETLOG_WS(s, "doing read callback");
__CFSOCKETLOG("__CFSocketPerformV0(%p) for socket %d", s, s->_socket);
if (callout) callout(s, kCFSocketReadCallBack, NULL, NULL, contextInfo);
calledOut = true;
}
}
if ((callBackTypes & kCFSocketWriteCallBack) != 0) {
if (writeSignalled && !errorCode && (!calledOut || CFSocketIsValid(s))) {
__CFSOCKETLOG_WS(s, "doing write callback");
if (callout) callout(s, kCFSocketWriteCallBack, NULL, NULL, contextInfo);
calledOut = true;
}
}
}
static void __CFSocketPerformV0(void *info) {
CFSocketRef s = (CFSocketRef)info;
CFDataRef data = NULL;
CFDataRef address = NULL;
CFSocketNativeHandle sock = INVALID_SOCKET;
uint8_t readCallBackType, callBackTypes;
CFRunLoopRef rl = NULL;
void *contextInfo = NULL;
void (*contextRelease)(const void *) = NULL;
__CFSOCKETLOG_WS(s, "Starting");
__CFSocketLock(s);
if (!__CFSocketIsValid(s)) {
__CFSocketUnlock(s);
return;
}
callBackTypes = __CFSocketCallBackTypes(s);
readCallBackType = __CFSocketReadCallBackType(s);
CFOptionFlags callBacksSignalled = 0;
if (__CFSocketIsReadSignalled(s)) callBacksSignalled |= readCallBackType;
if (__CFSocketIsWriteSignalled(s)) callBacksSignalled |= kCFSocketWriteCallBack;
if (kCFSocketDataCallBack == readCallBackType) {
if (NULL != s->_dataQueue && 0 < CFArrayGetCount(s->_dataQueue)) {
data = (CFDataRef)CFArrayGetValueAtIndex(s->_dataQueue, 0);
CFRetain(data);
CFArrayRemoveValueAtIndex(s->_dataQueue, 0);
address = (CFDataRef)CFArrayGetValueAtIndex(s->_addressQueue, 0);
CFRetain(address);
CFArrayRemoveValueAtIndex(s->_addressQueue, 0);
}
} else if (kCFSocketAcceptCallBack == readCallBackType) {
if (NULL != s->_dataQueue && 0 < CFArrayGetCount(s->_dataQueue)) {
sock = (CFSocketNativeHandle)(uintptr_t)CFArrayGetValueAtIndex(s->_dataQueue, 0);
CFArrayRemoveValueAtIndex(s->_dataQueue, 0);
address = (CFDataRef)CFArrayGetValueAtIndex(s->_addressQueue, 0);
CFRetain(address);
CFArrayRemoveValueAtIndex(s->_addressQueue, 0);
}
}
if (NULL != s->_context.retain) {
contextInfo = s->_context.info;
contextRelease = s->_context.release;
s->_context.retain(contextInfo);
}
__CFSocketDoCallback(s, data, address, sock); // does __CFSocketUnlock(s)
if (NULL != contextRelease) {
contextRelease(contextInfo);
}
if (NULL != data) CFRelease(data);
if (NULL != address) CFRelease(address);
__CFSocketLock(s);
if (__CFSocketIsValid(s) && kCFSocketNoCallBack != readCallBackType) {
// if there's still more data, we want to wake back up right away
if ((kCFSocketDataCallBack == readCallBackType || kCFSocketAcceptCallBack == readCallBackType) && NULL != s->_dataQueue && 0 < CFArrayGetCount(s->_dataQueue)) {
__CFSOCKETLOG_WS(s, "perform short-circuit signaling source with flags 0x%x disabled 0x%x connected 0x%x\n", s->_f.client, s->_f.disabled, s->_f.connected);
CFRunLoopSourceSignal(s->_source0);
CFMutableArrayRef runLoopsOrig = (CFMutableArrayRef)CFRetain(s->_runLoops);
CFMutableArrayRef runLoopsCopy = CFArrayCreateMutableCopy(kCFAllocatorSystemDefault, 0, s->_runLoops);
CFRunLoopSourceRef source0 = s->_source0;
if (NULL != source0 && !CFRunLoopSourceIsValid(source0)) {
source0 = NULL;
}
if (source0) CFRetain(source0);
__CFSocketUnlock(s);
rl = __CFSocketCopyRunLoopToWakeUp(source0, runLoopsCopy);
if (source0) CFRelease(source0);
__CFSocketLock(s);
if (runLoopsOrig == s->_runLoops) {
s->_runLoops = runLoopsCopy;
runLoopsCopy = NULL;
CFRelease(runLoopsOrig);
}
CFRelease(runLoopsOrig);
if (runLoopsCopy) CFRelease(runLoopsCopy);
}
}
// Only reenable callbacks that are auto-reenabled
__CFSocketEnableCallBacks(s, callBacksSignalled & s->_f.client, FALSE, 'p'); // unlocks s
if (NULL != rl) {
CFRunLoopWakeUp(rl);
CFRelease(rl);
}
__CFSOCKETLOG_WS(s, "Done");
}
CFRunLoopSourceRef CFSocketCreateRunLoopSource(CFAllocatorRef allocator, CFSocketRef s, CFIndex order) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
CFRunLoopSourceRef result = NULL;
__CFSocketLock(s);
if (__CFSocketIsValid(s)) {
if (NULL != s->_source0 && !CFRunLoopSourceIsValid(s->_source0)) {
CFRelease(s->_source0);
s->_source0 = NULL;
}
if (NULL == s->_source0) {
CFRunLoopSourceContext context;
context.version = 0;
context.info = s;
context.retain = CFRetain;
context.release = CFRelease;
context.copyDescription = CFCopyDescription;
context.equal = CFEqual;
context.hash = CFHash;
context.schedule = __CFSocketSchedule;
context.cancel = __CFSocketCancel;
context.perform = __CFSocketPerformV0;
s->_source0 = CFRunLoopSourceCreate(allocator, order, &context);
}
CFRetain(s->_source0); /* This retain is for the receiver */
result = s->_source0;
}
__CFSocketUnlock(s);
return result;
}
static uint16_t __CFSocketDefaultNameRegistryPortNumber = 2454;
CONST_STRING_DECL(kCFSocketCommandKey, "Command")
CONST_STRING_DECL(kCFSocketNameKey, "Name")
CONST_STRING_DECL(kCFSocketValueKey, "Value")
CONST_STRING_DECL(kCFSocketResultKey, "Result")
CONST_STRING_DECL(kCFSocketErrorKey, "Error")
CONST_STRING_DECL(kCFSocketRegisterCommand, "Register")
CONST_STRING_DECL(kCFSocketRetrieveCommand, "Retrieve")
CONST_STRING_DECL(__kCFSocketRegistryRequestRunLoopMode, "CFSocketRegistryRequest")
static os_unfair_lock __CFSocketWriteLock_ = OS_UNFAIR_LOCK_INIT;
//#warning can only send on one socket at a time now
CF_INLINE void __CFSocketWriteLock(CFSocketRef s) {
os_unfair_lock_lock(& __CFSocketWriteLock_);
}
CF_INLINE void __CFSocketWriteUnlock(CFSocketRef s) {
os_unfair_lock_unlock(& __CFSocketWriteLock_);
}
//??? need timeout, error handling, retries
CFSocketError CFSocketSendData(CFSocketRef s, CFDataRef address, CFDataRef data, CFTimeInterval timeout) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
const uint8_t *dataptr, *addrptr = NULL;
SInt32 datalen, addrlen = 0, size = 0;
CFSocketNativeHandle sock = INVALID_SOCKET;
struct timeval tv;
if (address) {
addrptr = CFDataGetBytePtr(address);
addrlen = CFDataGetLength(address);
}
dataptr = CFDataGetBytePtr(data);
datalen = CFDataGetLength(data);
if (CFSocketIsValid(s)) sock = CFSocketGetNative(s);
if (INVALID_SOCKET != sock) {
CFRetain(s);
__CFSocketWriteLock(s);
tv.tv_sec = (timeout <= 0.0 || (CFTimeInterval)INT_MAX <= timeout) ? INT_MAX : (int)floor(timeout);
tv.tv_usec = (int)floor(1.0e+6 * (timeout - floor(timeout)));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); // cast for WinSock bad API
if (NULL != addrptr && 0 < addrlen) {
size = sendto(sock, (char *)dataptr, datalen, 0, (struct sockaddr *)addrptr, addrlen);
} else {
size = send(sock, (char *)dataptr, datalen, 0);
}
__CFSOCKETLOG_WS(s, "wrote %ld bytes", (long)size);
__CFSocketWriteUnlock(s);
CFRelease(s);
}
return (size > 0) ? kCFSocketSuccess : kCFSocketError;
}
CFSocketError CFSocketSetAddress(CFSocketRef s, CFDataRef address) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
struct sockaddr *name;
socklen_t namelen;
__CFGenericValidateType(s, CFSocketGetTypeID());
if (NULL == address) return kCFSocketError;
if (!CFSocketIsValid(s)) return kCFSocketError;
name = (struct sockaddr *)CFDataGetBytePtr(address);
namelen = (socklen_t)CFDataGetLength(address);
if (!name || namelen <= 0) return kCFSocketError;
CFSocketNativeHandle sock = CFSocketGetNative(s);
#if TARGET_OS_MAC
// Verify that the namelen is correct. If not, we have to fix it up. Developers will often incorrectly use 0 or strlen(path). See 9217961 and the second half of 9098274.
// Max size is a size byte, plus family byte, plus path of 255, plus a null byte.
char newName[255];
if (namelen > 2 && name->sa_family == AF_UNIX) {
// Don't use the SUN_LEN macro, because strnlen is safer and we know the max length of the string (from CFData, minus 2 bytes for len and addr)
socklen_t realLength = (sizeof(*((struct sockaddr_un *)name)) - sizeof(((struct sockaddr_un *)name)->sun_path) + strnlen(((struct sockaddr_un *)name)->sun_path, namelen - 2));
if (realLength > 255) return kCFSocketError;
// For a UNIX domain socket, we must pass the value of name.sun_len to bind in order for getsockname() to return a result that makes sense.
namelen = (socklen_t)(((struct sockaddr_un *)name)->sun_len);
if (realLength != namelen) {
// We got a different answer for length than was supplied by the caller. Fix it up so we don't end up truncating the path.
CFLog(kCFLogLevelWarning, CFSTR("WARNING: The sun_len field of a sockaddr_un structure passed to CFSocketSetAddress was not set correctly using the SUN_LEN macro."));
memcpy(newName, name, realLength);
namelen = realLength;
((struct sockaddr_un *)newName)->sun_len = realLength;
name = (struct sockaddr *)newName;
}
}
#endif
const int bindResult = bind(sock, name, namelen);
if (0 == bindResult) {
const int listenResult = listen(sock, 256);
if (listenResult != 0) {
CFLog(kCFLogLevelDebug, CFSTR("CFSocketSetAddress listen failure: %d"), errno);
}
}
else {
CFLog(kCFLogLevelDebug, CFSTR("CFSocketSetAddress bind failure: %d"), errno);
}
//??? should return errno; historically this never looked at the listenResult
return (CFIndex)bindResult;
}
CFSocketError CFSocketConnectToAddress(CFSocketRef s, CFDataRef address, CFTimeInterval timeout) {
CF_ASSERT_TYPE(CFSocketGetTypeID(), s);
CHECK_FOR_FORK();
//??? need error handling, retries
const uint8_t *name;
SInt32 namelen, result = -1, connect_err = 0, select_err = 0;
UInt32 yes = 1, no = 0;
Boolean wasBlocking = true;
__CFGenericValidateType(s, CFSocketGetTypeID());
if (!CFSocketIsValid(s)) return kCFSocketError;
name = CFDataGetBytePtr(address);
namelen = CFDataGetLength(address);
if (!name || namelen <= 0) return kCFSocketError;
CFSocketNativeHandle sock = CFSocketGetNative(s);
{
#if TARGET_OS_MAC
SInt32 flags = fcntl(sock, F_GETFL, 0);
if (flags >= 0) wasBlocking = ((flags & O_NONBLOCK) == 0);
if (wasBlocking && (timeout > 0.0 || timeout < 0.0)) ioctlsocket(sock, FIONBIO, (u_long *)&yes);
#else
// You can set but not get this flag in WIN32, so assume it was in non-blocking mode.
// The downside is that when we leave this routine we'll leave it non-blocking,
// whether it started that way or not.
SInt32 flags = 0;
if (timeout > 0.0 || timeout < 0.0) ioctlsocket(sock, FIONBIO, (u_long *)&yes);
wasBlocking = false;
#endif
result = connect(sock, (struct sockaddr *)name, namelen);
if (result != 0) {
connect_err = __CFSocketLastError();
#if TARGET_OS_WIN32
if (connect_err == WSAEWOULDBLOCK) connect_err = EINPROGRESS;
#endif
}
__CFSOCKETLOG_WS(s, "connection attempt returns %d error %d on socket %d (flags 0x%x blocking %d)", (int) result, (int) connect_err, sock, (int) flags, wasBlocking);
if (EINPROGRESS == connect_err && timeout >= 0.0) {
/* select on socket */
SInt32 nrfds;
int error_size = sizeof(select_err);
struct timeval tv;
CFMutableDataRef fds = CFDataCreateMutable(kCFAllocatorSystemDefault, 0);
__CFSocketFdSet(sock, fds);
tv.tv_sec = (timeout <= 0.0 || (CFTimeInterval)INT_MAX <= timeout) ? INT_MAX : (int)floor(timeout);
tv.tv_usec = (int)floor(1.0e+6 * (timeout - floor(timeout)));
nrfds = select(__CFSocketFdGetSize(fds), NULL, (fd_set *)CFDataGetMutableBytePtr(fds), NULL, &tv);
if (nrfds < 0) {
select_err = __CFSocketLastError();
result = -1;
} else if (nrfds == 0) {
result = -2;
} else {
if (0 != getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&select_err, (socklen_t *)&error_size)) select_err = 0;
result = (select_err == 0) ? 0 : -1;
}
CFRelease(fds);
__CFSOCKETLOG_WS(s, "timed connection attempt %s result %d, select returns %d error %d\n", (result == 0) ? "succeeds" : "fails", (int) result, (int) nrfds, (int) select_err);
}
if (wasBlocking && (timeout > 0.0 || timeout < 0.0)) ioctlsocket(sock, FIONBIO, (u_long *)&no);
if (EINPROGRESS == connect_err && timeout < 0.0) {
result = 0;
__CFSOCKETLOG_WS(s, "connection attempt continues in background\n");
}
}
//??? should return errno
return result;
}
CFSocketRef CFSocketCreate(CFAllocatorRef allocator, SInt32 protocolFamily, SInt32 socketType, SInt32 protocol, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context) {
CHECK_FOR_FORK();
CFSocketNativeHandle sock = INVALID_SOCKET;
CFSocketRef s = NULL;
if (0 >= protocolFamily) protocolFamily = PF_INET;
if (PF_INET == protocolFamily) {
if (0 >= socketType) socketType = SOCK_STREAM;
if (0 >= protocol && SOCK_STREAM == socketType) protocol = IPPROTO_TCP;
if (0 >= protocol && SOCK_DGRAM == socketType) protocol = IPPROTO_UDP;
}
#if TARGET_OS_MAC
if (PF_LOCAL == protocolFamily && 0 >= socketType) socketType = SOCK_STREAM;
#endif
#if TARGET_OS_WIN32
// make sure we've called proper Win32 startup facilities before socket()
__CFSocketInitializeWinSock();
#endif
sock = socket(protocolFamily, socketType, protocol);
if (INVALID_SOCKET != sock) {
s = CFSocketCreateWithNative(allocator, sock, callBackTypes, callout, context);
}
return s;
}
CFSocketRef CFSocketCreateWithSocketSignature(CFAllocatorRef allocator, const CFSocketSignature *signature, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context) {
CHECK_FOR_FORK();
CFSocketRef s = CFSocketCreate(allocator, signature->protocolFamily, signature->socketType, signature->protocol, callBackTypes, callout, context);
if (NULL != s && (!CFSocketIsValid(s) || kCFSocketSuccess != CFSocketSetAddress(s, signature->address))) {
CFSocketInvalidate(s);
CFRelease(s);
s = NULL;
}
return s;
}
CFSocketRef CFSocketCreateConnectedToSocketSignature(CFAllocatorRef allocator, const CFSocketSignature *signature, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context, CFTimeInterval timeout) {
CHECK_FOR_FORK();
CFSocketRef s = CFSocketCreate(allocator, signature->protocolFamily, signature->socketType, signature->protocol, callBackTypes, callout, context);
if (NULL != s && (!CFSocketIsValid(s) || kCFSocketSuccess != CFSocketConnectToAddress(s, signature->address, timeout))) {
CFSocketInvalidate(s);
CFRelease(s);
s = NULL;
}
return s;
}
typedef struct {
CFSocketError *error;
CFPropertyListRef *value;
CFDataRef *address;
} __CFSocketNameRegistryResponse;
static void __CFSocketHandleNameRegistryReply(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
CFDataRef replyData = (CFDataRef)data;
__CFSocketNameRegistryResponse *response = (__CFSocketNameRegistryResponse *)info;
CFDictionaryRef replyDictionary = NULL;
CFPropertyListRef value;
replyDictionary = (CFDictionaryRef)CFPropertyListCreateWithData(kCFAllocatorSystemDefault, replyData, kCFPropertyListImmutable, NULL, NULL);
if (NULL != response->error) *(response->error) = kCFSocketError;
if (NULL != replyDictionary) {
if (CFGetTypeID((CFTypeRef)replyDictionary) == CFDictionaryGetTypeID() && NULL != (value = CFDictionaryGetValue(replyDictionary, kCFSocketResultKey))) {
if (NULL != response->error) *(response->error) = kCFSocketSuccess;
if (NULL != response->value) *(response->value) = CFRetain(value);
if (NULL != response->address) *(response->address) = address ? CFDataCreateCopy(kCFAllocatorSystemDefault, address) : NULL;
}
CFRelease(replyDictionary);
}
CFSocketInvalidate(s);
}
static void __CFSocketSendNameRegistryRequest(CFSocketSignature *signature, CFDictionaryRef requestDictionary, __CFSocketNameRegistryResponse *response, CFTimeInterval timeout) {
CFDataRef requestData = NULL;
CFSocketContext context = {0, response, NULL, NULL, NULL};
CFSocketRef s = NULL;
CFRunLoopSourceRef source = NULL;
if (NULL != response->error) *(response->error) = kCFSocketError;
requestData = CFPropertyListCreateData(kCFAllocatorSystemDefault, requestDictionary, kCFPropertyListXMLFormat_v1_0, 0, NULL);
if (NULL != requestData) {
if (NULL != response->error) *(response->error) = kCFSocketTimeout;
s = CFSocketCreateConnectedToSocketSignature(kCFAllocatorSystemDefault, signature, kCFSocketDataCallBack, __CFSocketHandleNameRegistryReply, &context, timeout);
if (NULL != s) {
if (kCFSocketSuccess == CFSocketSendData(s, NULL, requestData, timeout)) {
source = CFSocketCreateRunLoopSource(kCFAllocatorSystemDefault, s, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, __kCFSocketRegistryRequestRunLoopMode);
CFRunLoopRunInMode(__kCFSocketRegistryRequestRunLoopMode, timeout, false);
CFRelease(source);
}
CFSocketInvalidate(s);
CFRelease(s);
}
CFRelease(requestData);
}
}
static void __CFSocketValidateSignature(const CFSocketSignature *providedSignature, CFSocketSignature *signature, uint16_t defaultPortNumber) {
struct sockaddr_in sain, *sainp;
memset(&sain, 0, sizeof(sain));
#if TARGET_OS_MAC
sain.sin_len = sizeof(sain);
#endif
sain.sin_family = AF_INET;
sain.sin_port = htons(__CFSocketDefaultNameRegistryPortNumber);
sain.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (NULL == providedSignature) {
signature->protocolFamily = PF_INET;
signature->socketType = SOCK_STREAM;
signature->protocol = IPPROTO_TCP;
signature->address = CFDataCreate(kCFAllocatorSystemDefault, (uint8_t *)&sain, sizeof(sain));
} else {
signature->protocolFamily = providedSignature->protocolFamily;
signature->socketType = providedSignature->socketType;
signature->protocol = providedSignature->protocol;
if (0 >= signature->protocolFamily) signature->protocolFamily = PF_INET;
if (PF_INET == signature->protocolFamily) {
if (0 >= signature->socketType) signature->socketType = SOCK_STREAM;
if (0 >= signature->protocol && SOCK_STREAM == signature->socketType) signature->protocol = IPPROTO_TCP;
if (0 >= signature->protocol && SOCK_DGRAM == signature->socketType) signature->protocol = IPPROTO_UDP;
}
if (NULL == providedSignature->address) {
signature->address = CFDataCreate(kCFAllocatorSystemDefault, (uint8_t *)&sain, sizeof(sain));
} else {
sainp = (struct sockaddr_in *)CFDataGetBytePtr(providedSignature->address);
if ((int)sizeof(struct sockaddr_in) <= CFDataGetLength(providedSignature->address) && (AF_INET == sainp->sin_family || 0 == sainp->sin_family)) {
#if TARGET_OS_MAC
sain.sin_len = sizeof(sain);
#endif
sain.sin_family = AF_INET;
sain.sin_port = sainp->sin_port;
if (0 == sain.sin_port) sain.sin_port = htons(defaultPortNumber);
sain.sin_addr.s_addr = sainp->sin_addr.s_addr;
if (htonl(INADDR_ANY) == sain.sin_addr.s_addr) sain.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
signature->address = CFDataCreate(kCFAllocatorSystemDefault, (uint8_t *)&sain, sizeof(sain));
} else {
signature->address = (CFDataRef)CFRetain(providedSignature->address);
}
}
}
}
CFSocketError CFSocketRegisterValue(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, CFPropertyListRef value) {
CFSocketSignature signature;
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFSocketError retval = kCFSocketError;
__CFSocketNameRegistryResponse response = {&retval, NULL, NULL};
CFDictionaryAddValue(dictionary, kCFSocketCommandKey, kCFSocketRegisterCommand);
CFDictionaryAddValue(dictionary, kCFSocketNameKey, name);
if (NULL != value) CFDictionaryAddValue(dictionary, kCFSocketValueKey, value);
__CFSocketValidateSignature(nameServerSignature, &signature, __CFSocketDefaultNameRegistryPortNumber);
__CFSocketSendNameRegistryRequest(&signature, dictionary, &response, timeout);
CFRelease(dictionary);
CFRelease(signature.address);
return retval;
}
CFSocketError CFSocketCopyRegisteredValue(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, CFPropertyListRef *value, CFDataRef *serverAddress) {
CFSocketSignature signature;
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFSocketError retval = kCFSocketError;
__CFSocketNameRegistryResponse response = {&retval, value, serverAddress};
CFDictionaryAddValue(dictionary, kCFSocketCommandKey, kCFSocketRetrieveCommand);
CFDictionaryAddValue(dictionary, kCFSocketNameKey, name);
__CFSocketValidateSignature(nameServerSignature, &signature, __CFSocketDefaultNameRegistryPortNumber);
__CFSocketSendNameRegistryRequest(&signature, dictionary, &response, timeout);
CFRelease(dictionary);
CFRelease(signature.address);
return retval;
}
CFSocketError CFSocketRegisterSocketSignature(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, const CFSocketSignature *signature) {
CFSocketSignature validatedSignature;
CFMutableDataRef data = NULL;
CFSocketError retval;
CFIndex length;
uint8_t bytes[4];
if (NULL == signature) {
retval = CFSocketUnregister(nameServerSignature, timeout, name);
} else {
__CFSocketValidateSignature(signature, &validatedSignature, 0);
if (NULL == validatedSignature.address || 0 > validatedSignature.protocolFamily || 255 < validatedSignature.protocolFamily || 0 > validatedSignature.socketType || 255 < validatedSignature.socketType || 0 > validatedSignature.protocol || 255 < validatedSignature.protocol || 0 >= (length = CFDataGetLength(validatedSignature.address)) || 255 < length) {
retval = kCFSocketError;
} else {
data = CFDataCreateMutable(kCFAllocatorSystemDefault, sizeof(bytes) + length);
bytes[0] = validatedSignature.protocolFamily;
bytes[1] = validatedSignature.socketType;
bytes[2] = validatedSignature.protocol;
bytes[3] = length;
CFDataAppendBytes(data, bytes, sizeof(bytes));
CFDataAppendBytes(data, CFDataGetBytePtr(validatedSignature.address), length);
retval = CFSocketRegisterValue(nameServerSignature, timeout, name, data);
CFRelease(data);
}
if (validatedSignature.address) CFRelease(validatedSignature.address);
}
return retval;
}
CFSocketError CFSocketCopyRegisteredSocketSignature(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, CFSocketSignature *signature, CFDataRef *nameServerAddress) {
CFDataRef data = NULL;
CFSocketSignature returnedSignature;
const uint8_t *ptr = NULL, *aptr = NULL;
uint8_t *mptr;
CFIndex length = 0;
CFDataRef serverAddress = NULL;
CFSocketError retval = CFSocketCopyRegisteredValue(nameServerSignature, timeout, name, (CFPropertyListRef *)&data, &serverAddress);
if (NULL == data || CFGetTypeID(data) != CFDataGetTypeID() || NULL == (ptr = CFDataGetBytePtr(data)) || (length = CFDataGetLength(data)) < 4) retval = kCFSocketError;
if (kCFSocketSuccess == retval && NULL != signature) {
returnedSignature.protocolFamily = (SInt32)*ptr++;
returnedSignature.socketType = (SInt32)*ptr++;
returnedSignature.protocol = (SInt32)*ptr++;
ptr++;
returnedSignature.address = CFDataCreate(kCFAllocatorSystemDefault, ptr, length - 4);
__CFSocketValidateSignature(&returnedSignature, signature, 0);
CFRelease(returnedSignature.address);
ptr = CFDataGetBytePtr(signature->address);
if (CFDataGetLength(signature->address) >= (int)sizeof(struct sockaddr_in) && AF_INET == ((struct sockaddr *)ptr)->sa_family && NULL != serverAddress && CFDataGetLength(serverAddress) >= (int)sizeof(struct sockaddr_in) && NULL != (aptr = CFDataGetBytePtr(serverAddress)) && AF_INET == ((struct sockaddr *)aptr)->sa_family) {
CFMutableDataRef address = CFDataCreateMutableCopy(kCFAllocatorSystemDefault, CFDataGetLength(signature->address), signature->address);
mptr = CFDataGetMutableBytePtr(address);
((struct sockaddr_in *)mptr)->sin_addr = ((struct sockaddr_in *)aptr)->sin_addr;
CFRelease(signature->address);
signature->address = address;
}
if (NULL != nameServerAddress) *nameServerAddress = serverAddress ? (CFDataRef)CFRetain(serverAddress) : NULL;
}
if (NULL != data) CFRelease(data);
if (NULL != serverAddress) CFRelease(serverAddress);
return retval;
}
CFSocketError CFSocketUnregister(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name) {
return CFSocketRegisterValue(nameServerSignature, timeout, name, NULL);
}
CF_EXPORT void CFSocketSetDefaultNameRegistryPortNumber(uint16_t port) {
__CFSocketDefaultNameRegistryPortNumber = port;
}
CF_EXPORT uint16_t CFSocketGetDefaultNameRegistryPortNumber(void) {
return __CFSocketDefaultNameRegistryPortNumber;
}
#endif /* __HAS_DISPATCH__ */
|