1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/host_resolver_manager.h"
#include <cmath>
#include <cstdint>
#include <iterator>
#include <limits>
#include <memory>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/containers/circular_deque.h"
#include "base/containers/contains.h"
#include "base/containers/cxx20_erase.h"
#include "base/containers/flat_set.h"
#include "base/containers/linked_list.h"
#include "base/debug/debugger.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/functional/identity.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/safe_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/observer_list.h"
#include "base/ranges/algorithm.h"
#include "base/sequence_checker.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "base/types/optional_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/address_family.h"
#include "net/base/address_list.h"
#include "net/base/completion_once_callback.h"
#include "net/base/features.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_interfaces.h"
#include "net/base/prioritized_dispatcher.h"
#include "net/base/request_priority.h"
#include "net/base/trace_constants.h"
#include "net/base/tracing.h"
#include "net/base/url_util.h"
#include "net/dns/address_sorter.h"
#include "net/dns/dns_alias_utility.h"
#include "net/dns/dns_client.h"
#include "net/dns/dns_names_util.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_response_result_extractor.h"
#include "net/dns/dns_transaction.h"
#include "net/dns/dns_util.h"
#include "net/dns/host_cache.h"
#include "net/dns/host_resolver_internal_result.h"
#include "net/dns/host_resolver_mdns_listener_impl.h"
#include "net/dns/host_resolver_mdns_task.h"
#include "net/dns/host_resolver_nat64_task.h"
#include "net/dns/host_resolver_proc.h"
#include "net/dns/host_resolver_system_task.h"
#include "net/dns/httpssvc_metrics.h"
#include "net/dns/loopback_only.h"
#include "net/dns/mdns_client.h"
#include "net/dns/public/dns_protocol.h"
#include "net/dns/public/dns_query_type.h"
#include "net/dns/public/host_resolver_results.h"
#include "net/dns/public/resolve_error_info.h"
#include "net/dns/public/secure_dns_mode.h"
#include "net/dns/public/secure_dns_policy.h"
#include "net/dns/public/util.h"
#include "net/dns/record_parsed.h"
#include "net/dns/resolve_context.h"
#include "net/dns/test_dns_config_service.h"
#include "net/http/http_network_session.h"
#include "net/log/net_log.h"
#include "net/log/net_log_capture_mode.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source.h"
#include "net/log/net_log_source_type.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/client_socket_factory.h"
#include "net/url_request/url_request_context.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "url/scheme_host_port.h"
#include "url/url_constants.h"
#if BUILDFLAG(ENABLE_MDNS)
#include "net/dns/mdns_client_impl.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <Winsock2.h>
#include "net/base/winsock_init.h"
#endif
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <net/if.h>
#include "net/base/sys_addrinfo.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#else // !BUILDFLAG(IS_ANDROID)
#include <ifaddrs.h>
#endif // BUILDFLAG(IS_ANDROID)
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
namespace net {
namespace {
// Limit the size of hostnames that will be resolved to combat issues in
// some platform's resolvers.
const size_t kMaxHostLength = 4096;
// Default TTL for successful resolutions with HostResolverSystemTask.
const unsigned kCacheEntryTTLSeconds = 60;
// Default TTL for unsuccessful resolutions with HostResolverSystemTask.
const unsigned kNegativeCacheEntryTTLSeconds = 0;
// Minimum TTL for successful resolutions with DnsTask.
const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
// Time between IPv6 probes, i.e. for how long results of each IPv6 probe are
// cached.
const int kIPv6ProbePeriodMs = 1000;
// Google DNS address used for IPv6 probes.
const uint8_t kIPv6ProbeAddress[] = {0x20, 0x01, 0x48, 0x60, 0x48, 0x60,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x88, 0x88};
// ICANN uses this localhost address to indicate a name collision.
//
// The policy in Chromium is to fail host resolving if it resolves to
// this special address.
//
// Not however that IP literals are exempt from this policy, so it is still
// possible to navigate to http://127.0.53.53/ directly.
//
// For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
const uint8_t kIcanNameCollisionIp[] = {127, 0, 53, 53};
bool ContainsIcannNameCollisionIp(const std::vector<IPEndPoint>& endpoints) {
for (const auto& endpoint : endpoints) {
const IPAddress& addr = endpoint.address();
if (addr.IsIPv4() && IPAddressStartsWith(addr, kIcanNameCollisionIp)) {
return true;
}
}
return false;
}
// True if |hostname| ends with either ".local" or ".local.".
bool ResemblesMulticastDNSName(base::StringPiece hostname) {
return base::EndsWith(hostname, ".local") ||
base::EndsWith(hostname, ".local.");
}
bool ConfigureAsyncDnsNoFallbackFieldTrial() {
const bool kDefault = false;
// Configure the AsyncDns field trial as follows:
// groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
// groups AsyncDnsA and AsyncDnsB: return false,
// groups SystemDnsA and SystemDnsB: return false,
// otherwise (trial absent): return default.
std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
if (!group_name.empty()) {
return base::StartsWith(group_name, "AsyncDnsNoFallback",
base::CompareCase::INSENSITIVE_ASCII);
}
return kDefault;
}
// Returns true if NAT64 can be used in place of an IPv4 address during host
// resolution.
bool MayUseNAT64ForIPv4Literal(HostResolverFlags flags,
HostResolverSource source,
const IPAddress& ip_address) {
return !(flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
ip_address.IsValid() && ip_address.IsIPv4() &&
base::FeatureList::IsEnabled(features::kUseNAT64ForIPv4Literal) &&
(source != HostResolverSource::LOCAL_ONLY);
}
//-----------------------------------------------------------------------------
// Creates NetLog parameters when the DnsTask failed.
base::Value::Dict NetLogDnsTaskFailedParams(
int net_error,
absl::optional<DnsQueryType> failed_transaction_type,
absl::optional<base::TimeDelta> ttl,
const HostCache::Entry* saved_results) {
base::Value::Dict dict;
if (failed_transaction_type) {
dict.Set("dns_query_type", kDnsQueryTypes.at(*failed_transaction_type));
}
if (ttl)
dict.Set("error_ttl_sec",
base::saturated_cast<int>(ttl.value().InSeconds()));
dict.Set("net_error", net_error);
if (saved_results)
dict.Set("saved_results", saved_results->NetLogParams());
return dict;
}
base::Value::Dict NetLogDnsTaskExtractionFailureParams(
DnsResponseResultExtractor::ExtractionError extraction_error,
DnsQueryType dns_query_type) {
base::Value::Dict dict;
dict.Set("extraction_error", base::strict_cast<int>(extraction_error));
dict.Set("dns_query_type", kDnsQueryTypes.at(dns_query_type));
return dict;
}
// Creates NetLog parameters for HOST_RESOLVER_MANAGER_JOB_ATTACH/DETACH events.
base::Value::Dict NetLogJobAttachParams(const NetLogSource& source,
RequestPriority priority) {
base::Value::Dict dict;
source.AddToEventParameters(dict);
dict.Set("priority", RequestPriorityToString(priority));
return dict;
}
base::Value::Dict NetLogIPv6AvailableParams(bool ipv6_available, bool cached) {
base::Value::Dict dict;
dict.Set("ipv6_available", ipv6_available);
dict.Set("cached", cached);
return dict;
}
// The logging routines are defined here because some requests are resolved
// without a Request object.
//-----------------------------------------------------------------------------
// Maximum of 64 concurrent resolver calls (excluding retries).
// Between 2010 and 2020, the limit was set to 6 because of a report of a broken
// home router that would fail in the presence of more simultaneous queries.
// In 2020, we conducted an experiment to see if this kind of router was still
// present on the Internet, and found no evidence of any remaining issues, so
// we increased the limit to 64 at that time.
const size_t kDefaultMaxSystemTasks = 64u;
PrioritizedDispatcher::Limits GetDispatcherLimits(
const HostResolver::ManagerOptions& options) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES,
options.max_concurrent_resolves);
// If not using default, do not use the field trial.
if (limits.total_jobs != HostResolver::ManagerOptions::kDefaultParallelism)
return limits;
// Default, without trial is no reserved slots.
limits.total_jobs = kDefaultMaxSystemTasks;
// Parallelism is determined by the field trial.
std::string group =
base::FieldTrialList::FindFullName("HostResolverDispatch");
if (group.empty())
return limits;
// The format of the group name is a list of non-negative integers separated
// by ':'. Each of the elements in the list corresponds to an element in
// |reserved_slots|, except the last one which is the |total_jobs|.
std::vector<base::StringPiece> group_parts = base::SplitStringPiece(
group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (group_parts.size() != NUM_PRIORITIES + 1) {
NOTREACHED();
return limits;
}
std::vector<size_t> parsed(group_parts.size());
for (size_t i = 0; i < group_parts.size(); ++i) {
if (!base::StringToSizeT(group_parts[i], &parsed[i])) {
NOTREACHED();
return limits;
}
}
const size_t total_jobs = parsed.back();
parsed.pop_back();
const size_t total_reserved_slots =
std::accumulate(parsed.begin(), parsed.end(), 0u);
// There must be some unreserved slots available for the all priorities.
if (total_reserved_slots > total_jobs ||
(total_reserved_slots == total_jobs && parsed[MINIMUM_PRIORITY] == 0)) {
NOTREACHED();
return limits;
}
limits.total_jobs = total_jobs;
limits.reserved_slots = parsed;
return limits;
}
// Keeps track of the highest priority.
class PriorityTracker {
public:
explicit PriorityTracker(RequestPriority initial_priority)
: highest_priority_(initial_priority) {}
RequestPriority highest_priority() const { return highest_priority_; }
size_t total_count() const { return total_count_; }
void Add(RequestPriority req_priority) {
++total_count_;
++counts_[req_priority];
if (highest_priority_ < req_priority)
highest_priority_ = req_priority;
}
void Remove(RequestPriority req_priority) {
DCHECK_GT(total_count_, 0u);
DCHECK_GT(counts_[req_priority], 0u);
--total_count_;
--counts_[req_priority];
size_t i;
for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i) {
}
highest_priority_ = static_cast<RequestPriority>(i);
// In absence of requests, default to MINIMUM_PRIORITY.
if (total_count_ == 0)
DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
}
private:
RequestPriority highest_priority_;
size_t total_count_ = 0;
size_t counts_[NUM_PRIORITIES] = {};
};
base::Value::Dict NetLogResults(const HostCache::Entry& results) {
base::Value::Dict dict;
dict.Set("results", results.NetLogParams());
return dict;
}
base::Value ToLogStringValue(
const absl::variant<url::SchemeHostPort, std::string>& host) {
if (absl::holds_alternative<url::SchemeHostPort>(host)) {
return base::Value(absl::get<url::SchemeHostPort>(host).Serialize());
}
return base::Value(absl::get<std::string>(host));
}
// Returns empty string if `host` has no known scheme.
base::StringPiece GetScheme(
const absl::variant<url::SchemeHostPort, std::string>& host) {
if (absl::holds_alternative<url::SchemeHostPort>(host))
return absl::get<url::SchemeHostPort>(host).scheme();
return base::StringPiece();
}
base::StringPiece GetHostname(
const absl::variant<url::SchemeHostPort, std::string>& host) {
if (absl::holds_alternative<url::SchemeHostPort>(host)) {
base::StringPiece hostname = absl::get<url::SchemeHostPort>(host).host();
if (hostname.size() >= 2 && hostname.front() == '[' &&
hostname.back() == ']') {
hostname = hostname.substr(1, hostname.size() - 2);
}
return hostname;
}
return absl::get<std::string>(host);
}
// Only use scheme/port in JobKey if `https_svcb_options_enabled` is true
// (or the query is explicitly for HTTPS). Otherwise DNS will not give different
// results for the same hostname.
absl::variant<url::SchemeHostPort, std::string> CreateHostForJobKey(
const HostResolver::Host& input,
DnsQueryType query_type,
bool https_svcb_options_enabled) {
if ((https_svcb_options_enabled || query_type == DnsQueryType::HTTPS) &&
input.HasScheme()) {
return input.AsSchemeHostPort();
}
return std::string(input.GetHostnameWithoutBrackets());
}
DnsResponse CreateFakeEmptyResponse(base::StringPiece hostname,
DnsQueryType query_type) {
absl::optional<std::vector<uint8_t>> qname =
dns_names_util::DottedNameToNetwork(
hostname, /*require_valid_internet_hostname=*/true);
CHECK(qname.has_value());
return DnsResponse::CreateEmptyNoDataResponse(
/*id=*/0u, /*is_authoritative=*/true, qname.value(),
DnsQueryTypeToQtype(query_type));
}
std::vector<IPEndPoint> FilterAddresses(std::vector<IPEndPoint> addresses,
DnsQueryTypeSet query_types) {
DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
DCHECK(!query_types.Empty());
const AddressFamily want_family =
HostResolver::DnsQueryTypeSetToAddressFamily(query_types);
if (want_family == ADDRESS_FAMILY_UNSPECIFIED)
return addresses;
// Keep only the endpoints that match `want_family`.
addresses.erase(
base::ranges::remove_if(
addresses,
[want_family](AddressFamily family) { return family != want_family; },
&IPEndPoint::GetFamily),
addresses.end());
return addresses;
}
void RecordResolveTimeDiffForBucket(const char* histogram_variant,
const char* histogram_bucket,
base::TimeDelta diff) {
base::UmaHistogramTimes(
base::StrCat({"Net.Dns.ResolveTimeDiff.", histogram_variant,
".FirstRecord", histogram_bucket}),
diff);
}
void RecordResolveTimeDiff(const char* histogram_variant,
base::TimeTicks start_time,
base::TimeTicks first_record_end_time,
base::TimeTicks second_record_end_time) {
CHECK_LE(start_time, first_record_end_time);
CHECK_LE(first_record_end_time, second_record_end_time);
base::TimeDelta first_elapsed = first_record_end_time - start_time;
base::TimeDelta diff = second_record_end_time - first_record_end_time;
if (first_elapsed < base::Milliseconds(10)) {
RecordResolveTimeDiffForBucket(histogram_variant, "FasterThan10ms", diff);
} else if (first_elapsed < base::Milliseconds(25)) {
RecordResolveTimeDiffForBucket(histogram_variant, "10msTo25ms", diff);
} else if (first_elapsed < base::Milliseconds(50)) {
RecordResolveTimeDiffForBucket(histogram_variant, "25msTo50ms", diff);
} else if (first_elapsed < base::Milliseconds(100)) {
RecordResolveTimeDiffForBucket(histogram_variant, "50msTo100ms", diff);
} else if (first_elapsed < base::Milliseconds(250)) {
RecordResolveTimeDiffForBucket(histogram_variant, "100msTo250ms", diff);
} else if (first_elapsed < base::Milliseconds(500)) {
RecordResolveTimeDiffForBucket(histogram_variant, "250msTo500ms", diff);
} else if (first_elapsed < base::Seconds(1)) {
RecordResolveTimeDiffForBucket(histogram_variant, "500msTo1s", diff);
} else {
RecordResolveTimeDiffForBucket(histogram_variant, "SlowerThan1s", diff);
}
}
int GetPortForGloballyReachableCheck() {
if (!base::FeatureList::IsEnabled(
features::kUseAlternativePortForGloballyReachableCheck)) {
return 443;
}
return features::kAlternativePortForGloballyReachableCheck.Get();
}
} // namespace
//-----------------------------------------------------------------------------
bool ResolveLocalHostname(base::StringPiece host,
std::vector<IPEndPoint>* address_list) {
address_list->clear();
if (!IsLocalHostname(host))
return false;
address_list->emplace_back(IPAddress::IPv6Localhost(), 0);
address_list->emplace_back(IPAddress::IPv4Localhost(), 0);
return true;
}
struct HostResolverManager::JobKey {
explicit JobKey(ResolveContext* resolve_context)
: resolve_context(resolve_context->AsSafeRef()) {}
bool operator<(const JobKey& other) const {
return std::forward_as_tuple(query_types.ToEnumBitmask(), flags, source,
secure_dns_mode, &*resolve_context, host,
network_anonymization_key) <
std::forward_as_tuple(other.query_types.ToEnumBitmask(), other.flags,
other.source, other.secure_dns_mode,
&*other.resolve_context, other.host,
other.network_anonymization_key);
}
bool operator==(const JobKey& other) const {
return !(*this < other || other < *this);
}
absl::variant<url::SchemeHostPort, std::string> host;
NetworkAnonymizationKey network_anonymization_key;
DnsQueryTypeSet query_types;
HostResolverFlags flags;
HostResolverSource source;
SecureDnsMode secure_dns_mode;
base::SafeRef<ResolveContext, base::SafeRefDanglingUntriaged> resolve_context;
HostCache::Key ToCacheKey(bool secure) const {
if (query_types.Size() != 1) {
// This function will produce identical cache keys for `JobKey` structs
// that differ only in their (non-singleton) `query_types` fields. When we
// enable new query types, this behavior could lead to subtle bugs. That
// is why the following DCHECK restricts the allowable query types.
DCHECK(Difference(query_types, {DnsQueryType::A, DnsQueryType::AAAA,
DnsQueryType::HTTPS})
.Empty());
}
const DnsQueryType query_type_for_key = query_types.Size() == 1
? *query_types.begin()
: DnsQueryType::UNSPECIFIED;
HostCache::Key key(host, query_type_for_key, flags, source,
network_anonymization_key);
key.secure = secure;
return key;
}
handles::NetworkHandle GetTargetNetwork() const {
return resolve_context->GetTargetNetwork();
}
};
// Holds the callback and request parameters for an outstanding request.
//
// The RequestImpl is owned by the end user of host resolution. Deletion prior
// to the request having completed means the request was cancelled by the
// caller.
//
// Both the RequestImpl and its associated Job hold non-owning pointers to each
// other. Care must be taken to clear the corresponding pointer when
// cancellation is initiated by the Job (OnJobCancelled) vs by the end user
// (~RequestImpl).
class HostResolverManager::RequestImpl
: public HostResolver::ResolveHostRequest,
public base::LinkNode<HostResolverManager::RequestImpl> {
public:
RequestImpl(NetLogWithSource source_net_log,
HostResolver::Host request_host,
NetworkAnonymizationKey network_anonymization_key,
absl::optional<ResolveHostParameters> optional_parameters,
base::WeakPtr<ResolveContext> resolve_context,
HostCache* host_cache,
base::WeakPtr<HostResolverManager> resolver,
const base::TickClock* tick_clock)
: source_net_log_(std::move(source_net_log)),
request_host_(std::move(request_host)),
network_anonymization_key_(
NetworkAnonymizationKey::IsPartitioningEnabled()
? std::move(network_anonymization_key)
: NetworkAnonymizationKey()),
parameters_(optional_parameters ? std::move(optional_parameters).value()
: ResolveHostParameters()),
resolve_context_(std::move(resolve_context)),
host_cache_(host_cache),
host_resolver_flags_(
HostResolver::ParametersToHostResolverFlags(parameters_)),
priority_(parameters_.initial_priority),
job_key_(JobKey(resolve_context_.get())),
resolver_(std::move(resolver)),
tick_clock_(tick_clock) {}
RequestImpl(const RequestImpl&) = delete;
RequestImpl& operator=(const RequestImpl&) = delete;
~RequestImpl() override;
int Start(CompletionOnceCallback callback) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(callback);
// Start() may only be called once per request.
CHECK(!job_.has_value());
DCHECK(!complete_);
DCHECK(!callback_);
// Parent HostResolver must still be alive to call Start().
DCHECK(resolver_);
if (!resolve_context_) {
complete_ = true;
resolver_.reset();
set_error_info(ERR_CONTEXT_SHUT_DOWN, false);
return ERR_NAME_NOT_RESOLVED;
}
LogStartRequest();
next_state_ = STATE_IPV6_REACHABILITY;
callback_ = std::move(callback);
int rv = OK;
rv = DoLoop(rv);
return rv;
}
int DoLoop(int rv) {
do {
ResolveState state = next_state_;
next_state_ = STATE_NONE;
switch (state) {
case STATE_IPV6_REACHABILITY:
rv = DoIPv6Reachability();
break;
case STATE_GET_PARAMETERS:
DCHECK_EQ(OK, rv);
rv = DoGetParameters();
break;
case STATE_GET_PARAMETERS_COMPLETE:
rv = DoGetParametersComplete(rv);
break;
case STATE_RESOLVE_LOCALLY:
rv = DoResolveLocally();
break;
case STATE_START_JOB:
rv = DoStartJob();
break;
case STATE_FINISH_REQUEST:
rv = DoFinishRequest(rv);
break;
default:
NOTREACHED() << "next_state_: " << next_state_;
break;
}
} while (next_state_ != STATE_NONE && rv != ERR_IO_PENDING);
return rv;
}
void OnIOComplete(int rv) {
rv = DoLoop(rv);
if (rv != ERR_IO_PENDING && !callback_.is_null()) {
std::move(callback_).Run(rv);
}
}
int DoIPv6Reachability() {
next_state_ = STATE_GET_PARAMETERS;
// If a single reachability probe has not been completed, and the latest
// probe will return asynchronously, return ERR_NAME_NOT_RESOLVED when the
// request source is LOCAL_ONLY. This is due to LOCAL_ONLY requiring a
// synchronous response, so it cannot wait on an async probe result and
// cannot make assumptions about reachability.
if (parameters_.source == HostResolverSource::LOCAL_ONLY) {
int rv = resolver_->StartIPv6ReachabilityCheck(
source_net_log_, GetClientSocketFactory(),
base::DoNothingAs<void(int)>());
if (rv == ERR_IO_PENDING) {
next_state_ = STATE_FINISH_REQUEST;
return ERR_NAME_NOT_RESOLVED;
}
return OK;
}
return resolver_->StartIPv6ReachabilityCheck(
source_net_log_, GetClientSocketFactory(),
base::BindOnce(&RequestImpl::OnIOComplete,
weak_ptr_factory_.GetWeakPtr()));
}
int DoGetParameters() {
job_key_.host =
CreateHostForJobKey(request_host_, parameters_.dns_query_type,
resolver_->https_svcb_options_.enable);
job_key_.network_anonymization_key = network_anonymization_key_;
job_key_.source = parameters_.source;
bool is_ip = ip_address_.AssignFromIPLiteral(GetHostname(job_key_.host));
resolver_->GetEffectiveParametersForRequest(
job_key_.host, parameters_.dns_query_type, host_resolver_flags_,
parameters_.secure_dns_policy, is_ip, source_net_log_,
&job_key_.query_types, &job_key_.flags, &job_key_.secure_dns_mode);
// A reachability probe to determine if the network is only reachable on
// IPv6 will be scheduled if the parameters are met for using NAT64 in place
// of an IPv4 address.
if (MayUseNAT64ForIPv4Literal(job_key_.flags, parameters_.source,
ip_address_) &&
resolver_->last_ipv6_probe_result_) {
next_state_ = STATE_GET_PARAMETERS_COMPLETE;
return resolver_->StartGloballyReachableCheck(
ip_address_, source_net_log_, GetClientSocketFactory(),
base::BindOnce(&RequestImpl::OnIOComplete,
weak_ptr_factory_.GetWeakPtr()));
}
next_state_ = STATE_RESOLVE_LOCALLY;
return OK;
}
int DoGetParametersComplete(int rv) {
next_state_ = STATE_RESOLVE_LOCALLY;
only_ipv6_reachable_ = (rv == ERR_FAILED) ? true : false;
return OK;
}
int DoResolveLocally() {
absl::optional<HostCache::EntryStaleness> stale_info;
HostCache::Entry results = resolver_->ResolveLocally(
only_ipv6_reachable_, job_key_, ip_address_, parameters_.cache_usage,
parameters_.secure_dns_policy, parameters_.source, source_net_log_,
host_cache_, &tasks_, &stale_info);
if (results.error() != ERR_DNS_CACHE_MISS ||
parameters_.source == HostResolverSource::LOCAL_ONLY ||
tasks_.empty()) {
if (results.error() == OK && !parameters_.is_speculative) {
set_results(results.CopyWithDefaultPort(request_host_.GetPort()));
}
if (stale_info && !parameters_.is_speculative) {
set_stale_info(std::move(stale_info).value());
}
next_state_ = STATE_FINISH_REQUEST;
return results.error();
}
next_state_ = STATE_START_JOB;
return OK;
}
int DoStartJob() {
resolver_->CreateAndStartJob(std::move(job_key_), std::move(tasks_), this);
DCHECK(!complete_);
resolver_.reset();
return ERR_IO_PENDING;
}
int DoFinishRequest(int rv) {
CHECK(!job_.has_value());
complete_ = true;
set_error_info(rv, /*is_secure_network_error=*/false);
rv = HostResolver::SquashErrorCode(rv);
LogFinishRequest(rv, /*async_completion=*/false);
return rv;
}
const AddressList* GetAddressResults() const override {
DCHECK(complete_);
return base::OptionalToPtr(legacy_address_results_);
}
const std::vector<HostResolverEndpointResult>* GetEndpointResults()
const override {
DCHECK(complete_);
return base::OptionalToPtr(endpoint_results_);
}
const std::vector<std::string>* GetTextResults() const override {
DCHECK(complete_);
return results_ ? &results_.value().text_records() : nullptr;
}
const std::vector<HostPortPair>* GetHostnameResults() const override {
DCHECK(complete_);
return results_ ? &results_.value().hostnames() : nullptr;
}
const std::set<std::string>* GetDnsAliasResults() const override {
DCHECK(complete_);
// If `include_canonical_name` param was true, should only ever have at most
// a single alias, representing the expected "canonical name".
#if DCHECK_IS_ON()
if (parameters().include_canonical_name && fixed_up_dns_alias_results_) {
DCHECK_LE(fixed_up_dns_alias_results_->size(), 1u);
if (GetAddressResults()) {
std::set<std::string> address_list_aliases_set(
GetAddressResults()->dns_aliases().begin(),
GetAddressResults()->dns_aliases().end());
DCHECK(address_list_aliases_set == fixed_up_dns_alias_results_.value());
}
}
#endif // DCHECK_IS_ON()
return base::OptionalToPtr(fixed_up_dns_alias_results_);
}
const std::vector<bool>* GetExperimentalResultsForTesting() const override {
DCHECK(complete_);
return results_ ? &results_.value().https_record_compatibility() : nullptr;
}
net::ResolveErrorInfo GetResolveErrorInfo() const override {
DCHECK(complete_);
return error_info_;
}
const absl::optional<HostCache::EntryStaleness>& GetStaleInfo()
const override {
DCHECK(complete_);
return stale_info_;
}
void ChangeRequestPriority(RequestPriority priority) override;
void set_results(HostCache::Entry results) {
// Should only be called at most once and before request is marked
// completed.
DCHECK(!complete_);
DCHECK(!results_);
DCHECK(!parameters_.is_speculative);
results_ = std::move(results);
FixUpEndpointAndAliasResults();
}
void set_error_info(int error, bool is_secure_network_error) {
error_info_ = ResolveErrorInfo(error, is_secure_network_error);
}
void set_stale_info(HostCache::EntryStaleness stale_info) {
// Should only be called at most once and before request is marked
// completed.
DCHECK(!complete_);
DCHECK(!stale_info_);
DCHECK(!parameters_.is_speculative);
stale_info_ = std::move(stale_info);
}
void AssignJob(base::SafeRef<Job> job) {
CHECK(!job_.has_value());
job_ = std::move(job);
}
bool HasJob() const { return job_.has_value(); }
// Gets the Job's key. Crashes if no Job has been assigned.
const JobKey& GetJobKey() const;
// Unassigns the Job without calling completion callback.
void OnJobCancelled(const JobKey& key);
// Cleans up Job assignment, marks request completed, and calls the completion
// callback. |is_secure_network_error| indicates whether |error| came from a
// secure DNS lookup.
void OnJobCompleted(const JobKey& job_key,
int error,
bool is_secure_network_error);
// NetLog for the source, passed in HostResolver::Resolve.
const NetLogWithSource& source_net_log() { return source_net_log_; }
const HostResolver::Host& request_host() const { return request_host_; }
const NetworkAnonymizationKey& network_anonymization_key() const {
return network_anonymization_key_;
}
const ResolveHostParameters& parameters() const { return parameters_; }
ResolveContext* resolve_context() const { return resolve_context_.get(); }
HostCache* host_cache() const { return host_cache_; }
HostResolverFlags host_resolver_flags() const { return host_resolver_flags_; }
RequestPriority priority() const { return priority_; }
void set_priority(RequestPriority priority) { priority_ = priority; }
bool complete() const { return complete_; }
private:
enum ResolveState {
STATE_IPV6_REACHABILITY,
STATE_GET_PARAMETERS,
STATE_GET_PARAMETERS_COMPLETE,
STATE_RESOLVE_LOCALLY,
STATE_START_JOB,
STATE_FINISH_REQUEST,
STATE_NONE,
};
void FixUpEndpointAndAliasResults() {
DCHECK(results_.has_value());
DCHECK(!legacy_address_results_.has_value());
DCHECK(!endpoint_results_.has_value());
DCHECK(!fixed_up_dns_alias_results_.has_value());
endpoint_results_ = results_.value().GetEndpoints();
if (endpoint_results_.has_value()) {
fixed_up_dns_alias_results_ = results_.value().aliases();
// Skip fixups for `include_canonical_name` requests. Just use the
// canonical name exactly as it was received from the system resolver.
if (parameters().include_canonical_name) {
DCHECK_LE(fixed_up_dns_alias_results_.value().size(), 1u);
} else {
fixed_up_dns_alias_results_ = dns_alias_utility::FixUpDnsAliases(
fixed_up_dns_alias_results_.value());
}
legacy_address_results_ = HostResolver::EndpointResultToAddressList(
endpoint_results_.value(), fixed_up_dns_alias_results_.value());
}
}
// Logging and metrics for when a request has just been started.
void LogStartRequest() {
DCHECK(request_time_.is_null());
request_time_ = tick_clock_->NowTicks();
source_net_log_.BeginEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_REQUEST, [this] {
base::Value::Dict dict;
dict.Set("host", request_host_.ToString());
dict.Set("dns_query_type",
kDnsQueryTypes.at(parameters_.dns_query_type));
dict.Set("allow_cached_response",
parameters_.cache_usage !=
ResolveHostParameters::CacheUsage::DISALLOWED);
dict.Set("is_speculative", parameters_.is_speculative);
dict.Set("network_anonymization_key",
network_anonymization_key_.ToDebugString());
dict.Set("secure_dns_policy",
base::strict_cast<int>(parameters_.secure_dns_policy));
return dict;
});
}
// Logging and metrics for when a request has just completed (before its
// callback is run).
void LogFinishRequest(int net_error, bool async_completion) {
source_net_log_.EndEventWithNetErrorCode(
NetLogEventType::HOST_RESOLVER_MANAGER_REQUEST, net_error);
if (!parameters_.is_speculative) {
DCHECK(!request_time_.is_null());
base::TimeDelta duration = tick_clock_->NowTicks() - request_time_;
UMA_HISTOGRAM_MEDIUM_TIMES("Net.DNS.Request.TotalTime", duration);
if (async_completion)
UMA_HISTOGRAM_MEDIUM_TIMES("Net.DNS.Request.TotalTimeAsync", duration);
}
}
// Logs when a request has been cancelled.
void LogCancelRequest() {
source_net_log_.AddEvent(NetLogEventType::CANCELLED);
source_net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_REQUEST);
}
ClientSocketFactory* GetClientSocketFactory() {
if (resolve_context_->url_request_context()) {
return resolve_context_->url_request_context()
->GetNetworkSessionContext()
->client_socket_factory;
} else {
return ClientSocketFactory::GetDefaultFactory();
}
}
const NetLogWithSource source_net_log_;
const HostResolver::Host request_host_;
const NetworkAnonymizationKey network_anonymization_key_;
ResolveHostParameters parameters_;
base::WeakPtr<ResolveContext> resolve_context_;
const raw_ptr<HostCache, DanglingUntriaged> host_cache_;
const HostResolverFlags host_resolver_flags_;
RequestPriority priority_;
ResolveState next_state_;
JobKey job_key_;
IPAddress ip_address_;
std::deque<TaskType> tasks_;
// The resolve job that this request is dependent on.
absl::optional<base::SafeRef<Job>> job_;
base::WeakPtr<HostResolverManager> resolver_ = nullptr;
// The user's callback to invoke when the request completes.
CompletionOnceCallback callback_;
bool complete_ = false;
bool only_ipv6_reachable_ = false;
absl::optional<HostCache::Entry> results_;
absl::optional<HostCache::EntryStaleness> stale_info_;
absl::optional<AddressList> legacy_address_results_;
absl::optional<std::vector<HostResolverEndpointResult>> endpoint_results_;
absl::optional<std::set<std::string>> fixed_up_dns_alias_results_;
ResolveErrorInfo error_info_;
const raw_ptr<const base::TickClock> tick_clock_;
base::TimeTicks request_time_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<RequestImpl> weak_ptr_factory_{this};
};
class HostResolverManager::ProbeRequestImpl
: public HostResolver::ProbeRequest,
public ResolveContext::DohStatusObserver {
public:
ProbeRequestImpl(base::WeakPtr<ResolveContext> context,
base::WeakPtr<HostResolverManager> resolver)
: context_(std::move(context)), resolver_(std::move(resolver)) {}
ProbeRequestImpl(const ProbeRequestImpl&) = delete;
ProbeRequestImpl& operator=(const ProbeRequestImpl&) = delete;
~ProbeRequestImpl() override {
// Ensure that observers are deregistered to avoid wasting memory.
if (context_)
context_->UnregisterDohStatusObserver(this);
}
int Start() override {
DCHECK(resolver_);
DCHECK(!runner_);
if (!context_)
return ERR_CONTEXT_SHUT_DOWN;
context_->RegisterDohStatusObserver(this);
StartRunner(false /* network_change */);
return ERR_IO_PENDING;
}
// ResolveContext::DohStatusObserver
void OnSessionChanged() override { CancelRunner(); }
void OnDohServerUnavailable(bool network_change) override {
// Start the runner asynchronously, as this may trigger reentrant calls into
// HostResolverManager, which are not allowed during notification handling.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&ProbeRequestImpl::StartRunner,
weak_ptr_factory_.GetWeakPtr(), network_change));
}
private:
void StartRunner(bool network_change) {
DCHECK(resolver_);
DCHECK(!resolver_->invalidation_in_progress_);
if (!context_)
return; // Reachable if the context ends before a posted task runs.
if (!runner_)
runner_ = resolver_->CreateDohProbeRunner(context_.get());
if (runner_)
runner_->Start(network_change);
}
void CancelRunner() {
runner_.reset();
// Cancel any asynchronous StartRunner() calls.
weak_ptr_factory_.InvalidateWeakPtrs();
}
base::WeakPtr<ResolveContext> context_;
std::unique_ptr<DnsProbeRunner> runner_;
base::WeakPtr<HostResolverManager> resolver_;
base::WeakPtrFactory<ProbeRequestImpl> weak_ptr_factory_{this};
};
//-----------------------------------------------------------------------------
// Resolves the hostname using DnsTransaction, which is a full implementation of
// a DNS stub resolver. One DnsTransaction is created for each resolution
// needed, which for AF_UNSPEC resolutions includes both A and AAAA. The
// transactions are scheduled separately and started separately.
//
// TODO(szym): This could be moved to separate source file as well.
class HostResolverManager::DnsTask : public base::SupportsWeakPtr<DnsTask> {
public:
class Delegate {
public:
virtual void OnDnsTaskComplete(base::TimeTicks start_time,
bool allow_fallback,
HostCache::Entry results,
bool secure) = 0;
// Called when one or more transactions complete or get cancelled, but only
// if more transactions are needed. If no more transactions are needed,
// expect `OnDnsTaskComplete()` to be called instead.
virtual void OnIntermediateTransactionsComplete() = 0;
virtual RequestPriority priority() const = 0;
virtual void AddTransactionTimeQueued(base::TimeDelta time_queued) = 0;
protected:
Delegate() = default;
virtual ~Delegate() = default;
};
DnsTask(DnsClient* client,
absl::variant<url::SchemeHostPort, std::string> host,
DnsQueryTypeSet query_types,
ResolveContext* resolve_context,
bool secure,
SecureDnsMode secure_dns_mode,
Delegate* delegate,
const NetLogWithSource& job_net_log,
const base::TickClock* tick_clock,
bool fallback_available,
const HostResolver::HttpsSvcbOptions& https_svcb_options)
: client_(client),
host_(std::move(host)),
resolve_context_(resolve_context->AsSafeRef()),
secure_(secure),
secure_dns_mode_(secure_dns_mode),
delegate_(delegate),
net_log_(job_net_log),
tick_clock_(tick_clock),
task_start_time_(tick_clock_->NowTicks()),
fallback_available_(fallback_available),
https_svcb_options_(https_svcb_options) {
DCHECK(client_);
DCHECK(delegate_);
if (!secure_) {
DCHECK(client_->CanUseInsecureDnsTransactions());
}
PushTransactionsNeeded(MaybeDisableAdditionalQueries(query_types));
}
DnsTask(const DnsTask&) = delete;
DnsTask& operator=(const DnsTask&) = delete;
int num_additional_transactions_needed() const {
return base::checked_cast<int>(transactions_needed_.size());
}
int num_transactions_in_progress() const {
return base::checked_cast<int>(transactions_in_progress_.size());
}
bool secure() const { return secure_; }
void StartNextTransaction() {
DCHECK_GE(num_additional_transactions_needed(), 1);
if (!any_transaction_started_) {
net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK,
[&] { return NetLogDnsTaskCreationParams(); });
}
any_transaction_started_ = true;
TransactionInfo transaction_info = std::move(transactions_needed_.front());
transactions_needed_.pop_front();
DCHECK(IsAddressType(transaction_info.type) || secure_ ||
client_->CanQueryAdditionalTypesViaInsecureDns());
// Record how long this transaction has been waiting to be created.
base::TimeDelta time_queued = tick_clock_->NowTicks() - task_start_time_;
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.JobQueueTime.PerTransaction",
time_queued);
delegate_->AddTransactionTimeQueued(time_queued);
CreateAndStartTransaction(std::move(transaction_info));
}
private:
using Results = std::set<std::unique_ptr<HostResolverInternalResult>>;
enum class TransactionErrorBehavior {
// Errors lead to task fallback (immediately unless another pending/started
// transaction has the `kFatalOrEmpty` behavior).
kFallback,
// Transaction errors are treated as if a NOERROR response were received,
// allowing task success if other transactions complete successfully.
kSynthesizeEmpty,
// Transaction errors are potentially fatal (determined by
// `OnTransactionComplete` and often its helper
// `IsFatalTransactionFailure()`) for the entire Job and may disallow
// fallback. Otherwise, same as `kSynthesizeEmpty`.
// TODO(crbug.com/1264933): Implement the fatality behavior.
kFatalOrEmpty,
};
struct TransactionInfo {
explicit TransactionInfo(DnsQueryType type,
TransactionErrorBehavior error_behavior =
TransactionErrorBehavior::kFallback)
: type(type), error_behavior(error_behavior) {}
TransactionInfo(TransactionInfo&&) = default;
TransactionInfo& operator=(TransactionInfo&&) = default;
bool operator<(const TransactionInfo& other) const {
return std::tie(type, error_behavior, transaction) <
std::tie(other.type, other.error_behavior, other.transaction);
}
DnsQueryType type;
TransactionErrorBehavior error_behavior;
std::unique_ptr<DnsTransaction> transaction;
};
base::Value::Dict NetLogDnsTaskCreationParams() {
base::Value::Dict dict;
dict.Set("secure", secure());
base::Value::List transactions_needed_value;
for (const TransactionInfo& info : transactions_needed_) {
base::Value::Dict transaction_dict;
transaction_dict.Set("dns_query_type", kDnsQueryTypes.at(info.type));
transactions_needed_value.Append(std::move(transaction_dict));
}
dict.Set("transactions_needed", std::move(transactions_needed_value));
return dict;
}
base::Value::Dict NetLogDnsTaskTimeoutParams() {
base::Value::Dict dict;
if (!transactions_in_progress_.empty()) {
base::Value::List list;
for (const TransactionInfo& info : transactions_in_progress_) {
base::Value::Dict transaction_dict;
transaction_dict.Set("dns_query_type", kDnsQueryTypes.at(info.type));
list.Append(std::move(transaction_dict));
}
dict.Set("started_transactions", std::move(list));
}
if (!transactions_needed_.empty()) {
base::Value::List list;
for (const TransactionInfo& info : transactions_needed_) {
base::Value::Dict transaction_dict;
transaction_dict.Set("dns_query_type", kDnsQueryTypes.at(info.type));
list.Append(std::move(transaction_dict));
}
dict.Set("queued_transactions", std::move(list));
}
return dict;
}
DnsQueryTypeSet MaybeDisableAdditionalQueries(DnsQueryTypeSet types) {
DCHECK(!types.Empty());
DCHECK(!types.Has(DnsQueryType::UNSPECIFIED));
// No-op if the caller explicitly requested this one query type.
if (types.Size() == 1)
return types;
if (types.Has(DnsQueryType::HTTPS)) {
if (!secure_ && !client_->CanQueryAdditionalTypesViaInsecureDns()) {
types.Remove(DnsQueryType::HTTPS);
} else {
DCHECK(!httpssvc_metrics_);
httpssvc_metrics_.emplace(secure_);
}
}
DCHECK(!types.Empty());
return types;
}
void PushTransactionsNeeded(DnsQueryTypeSet query_types) {
DCHECK(transactions_needed_.empty());
if (query_types.Has(DnsQueryType::HTTPS) &&
features::kUseDnsHttpsSvcbEnforceSecureResponse.Get() && secure_) {
query_types.Remove(DnsQueryType::HTTPS);
transactions_needed_.emplace_back(
DnsQueryType::HTTPS, TransactionErrorBehavior::kFatalOrEmpty);
}
// Give AAAA/A queries a head start by pushing them to the queue first.
constexpr DnsQueryType kHighPriorityQueries[] = {DnsQueryType::AAAA,
DnsQueryType::A};
for (DnsQueryType high_priority_query : kHighPriorityQueries) {
if (query_types.Has(high_priority_query)) {
query_types.Remove(high_priority_query);
transactions_needed_.emplace_back(high_priority_query);
}
}
for (DnsQueryType remaining_query : query_types) {
if (remaining_query == DnsQueryType::HTTPS) {
// Ignore errors for these types. In most cases treating them normally
// would only result in fallback to resolution without querying the
// type. Instead, synthesize empty results.
transactions_needed_.emplace_back(
remaining_query, TransactionErrorBehavior::kSynthesizeEmpty);
} else {
transactions_needed_.emplace_back(remaining_query);
}
}
}
void CreateAndStartTransaction(TransactionInfo transaction_info) {
DCHECK(!transaction_info.transaction);
DCHECK_NE(DnsQueryType::UNSPECIFIED, transaction_info.type);
std::string transaction_hostname(GetHostname(host_));
// For HTTPS, prepend "_<port>._https." for any non-default port.
uint16_t request_port = 0;
if (transaction_info.type == DnsQueryType::HTTPS &&
absl::holds_alternative<url::SchemeHostPort>(host_)) {
const auto& scheme_host_port = absl::get<url::SchemeHostPort>(host_);
transaction_hostname =
dns_util::GetNameForHttpsQuery(scheme_host_port, &request_port);
}
transaction_info.transaction =
client_->GetTransactionFactory()->CreateTransaction(
std::move(transaction_hostname),
DnsQueryTypeToQtype(transaction_info.type), net_log_, secure_,
secure_dns_mode_, &*resolve_context_,
fallback_available_ /* fast_timeout */);
transaction_info.transaction->SetRequestPriority(delegate_->priority());
auto transaction_info_it =
transactions_in_progress_.insert(std::move(transaction_info)).first;
// Safe to pass `transaction_info_it` because it is only modified/removed
// after async completion of this call or by destruction (which cancels the
// transaction and prevents callback because it owns the `DnsTransaction`
// object).
transaction_info_it->transaction->Start(base::BindOnce(
&DnsTask::OnDnsTransactionComplete, base::Unretained(this),
transaction_info_it, request_port));
}
void OnTimeout() {
net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK_TIMEOUT,
[&] { return NetLogDnsTaskTimeoutParams(); });
for (const TransactionInfo& transaction : transactions_in_progress_) {
base::TimeDelta elapsed_time = tick_clock_->NowTicks() - task_start_time_;
switch (transaction.type) {
case DnsQueryType::HTTPS:
DCHECK(!secure_ ||
!features::kUseDnsHttpsSvcbEnforceSecureResponse.Get());
if (httpssvc_metrics_) {
// Don't record provider ID for timeouts. It is not precisely known
// at this level which provider is actually to blame for the
// timeout, and breaking metrics out by provider is no longer
// important for current experimentation goals.
httpssvc_metrics_->SaveForHttps(HttpssvcDnsRcode::kTimedOut,
/*condensed_records=*/{},
elapsed_time);
}
break;
default:
// The timeout timer is only started when all other transactions have
// completed.
NOTREACHED();
}
}
transactions_needed_.clear();
transactions_in_progress_.clear();
OnTransactionsFinished();
}
// Called on completion of a `DnsTransaction`, but not necessarily completion
// of all work for the individual transaction in this task (see
// `OnTransactionsFinished()`).
void OnDnsTransactionComplete(
std::set<TransactionInfo>::iterator transaction_info_it,
uint16_t request_port,
int net_error,
const DnsResponse* response) {
DCHECK(transaction_info_it != transactions_in_progress_.end());
DCHECK(transactions_in_progress_.find(*transaction_info_it) !=
transactions_in_progress_.end());
// Pull the TransactionInfo out of `transactions_in_progress_` now, so it
// and its underlying DnsTransaction will be deleted on completion of
// OnTransactionComplete. Note: Once control leaves OnTransactionComplete,
// there's no further need for the transaction object. On the other hand,
// since it owns `*response`, it should stay around while
// OnTransactionComplete executes.
TransactionInfo transaction_info = std::move(
transactions_in_progress_.extract(transaction_info_it).value());
const base::TimeTicks now = tick_clock_->NowTicks();
base::TimeDelta elapsed_time = now - task_start_time_;
enum HttpssvcDnsRcode rcode_for_httpssvc = HttpssvcDnsRcode::kNoError;
if (httpssvc_metrics_) {
if (net_error == ERR_DNS_TIMED_OUT) {
rcode_for_httpssvc = HttpssvcDnsRcode::kTimedOut;
} else if (net_error == ERR_NAME_NOT_RESOLVED) {
rcode_for_httpssvc = HttpssvcDnsRcode::kNoError;
} else if (response == nullptr) {
rcode_for_httpssvc = HttpssvcDnsRcode::kMissingDnsResponse;
} else {
rcode_for_httpssvc =
TranslateDnsRcodeForHttpssvcExperiment(response->rcode());
}
}
// Handle network errors. Note that for NXDOMAIN, DnsTransaction returns
// ERR_NAME_NOT_RESOLVED, so that is not a network error if received with a
// valid response.
bool fatal_error =
IsFatalTransactionFailure(net_error, transaction_info, response);
absl::optional<DnsResponse> fake_response;
if (net_error != OK && !(net_error == ERR_NAME_NOT_RESOLVED && response &&
response->IsValid())) {
if (transaction_info.error_behavior ==
TransactionErrorBehavior::kFallback ||
fatal_error) {
// Fail task (or maybe Job) completely on network failure.
OnFailure(net_error, /*allow_fallback=*/!fatal_error,
/*ttl=*/absl::nullopt, transaction_info.type);
return;
} else {
DCHECK((transaction_info.error_behavior ==
TransactionErrorBehavior::kFatalOrEmpty &&
!fatal_error) ||
transaction_info.error_behavior ==
TransactionErrorBehavior::kSynthesizeEmpty);
// For non-fatal failures, synthesize an empty response.
fake_response =
CreateFakeEmptyResponse(GetHostname(host_), transaction_info.type);
response = &fake_response.value();
}
}
DCHECK(response);
DnsResponseResultExtractor extractor(*response);
DnsResponseResultExtractor::ResultsOrError results =
extractor.ExtractDnsResults(transaction_info.type,
/*original_domain_name=*/GetHostname(host_),
request_port);
DCHECK_NE(
results.error_or(DnsResponseResultExtractor::ExtractionError::kOk),
DnsResponseResultExtractor::ExtractionError::kUnexpected);
if (!results.has_value()) {
net_log_.AddEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK_EXTRACTION_FAILURE,
[&] {
return NetLogDnsTaskExtractionFailureParams(results.error(),
transaction_info.type);
});
if (transaction_info.error_behavior ==
TransactionErrorBehavior::kFatalOrEmpty ||
transaction_info.error_behavior ==
TransactionErrorBehavior::kSynthesizeEmpty) {
// No extraction errors are currently considered fatal, otherwise, there
// would need to be a call to some sort of
// IsFatalTransactionExtractionError() function.
DCHECK(!fatal_error);
DCHECK_EQ(transaction_info.type, DnsQueryType::HTTPS);
results = Results();
} else {
OnFailure(ERR_DNS_MALFORMED_RESPONSE, /*allow_fallback=*/true,
/*ttl=*/absl::nullopt, transaction_info.type);
return;
}
}
CHECK(results.has_value());
if (httpssvc_metrics_) {
if (transaction_info.type == DnsQueryType::HTTPS) {
bool has_compatible_https = base::ranges::any_of(
results.value(),
[](const std::unique_ptr<HostResolverInternalResult>& result) {
return result->type() ==
HostResolverInternalResult::Type::kMetadata;
});
if (has_compatible_https) {
httpssvc_metrics_->SaveForHttps(
rcode_for_httpssvc, std::vector<bool>{true}, elapsed_time);
} else {
httpssvc_metrics_->SaveForHttps(rcode_for_httpssvc,
std::vector<bool>(), elapsed_time);
}
} else {
httpssvc_metrics_->SaveForAddressQuery(elapsed_time,
rcode_for_httpssvc);
}
}
// Trigger HTTP->HTTPS upgrade if an HTTPS record is received for an "http"
// or "ws" request.
if (transaction_info.type == DnsQueryType::HTTPS &&
ShouldTriggerHttpToHttpsUpgrade(results.value())) {
// Disallow fallback. Otherwise DNS could be reattempted without HTTPS
// queries, and that would hide this error instead of triggering upgrade.
OnFailure(
ERR_DNS_NAME_HTTPS_ONLY, /*allow_fallback=*/false,
HostCache::Entry::TtlFromInternalResults(
results.value(), base::Time::Now(), tick_clock_->NowTicks()),
transaction_info.type);
return;
}
switch (transaction_info.type) {
case DnsQueryType::A:
a_record_end_time_ = now;
if (!aaaa_record_end_time_.is_null()) {
RecordResolveTimeDiff("AAAABeforeA", task_start_time_,
aaaa_record_end_time_, a_record_end_time_);
}
break;
case DnsQueryType::AAAA:
aaaa_record_end_time_ = now;
if (!a_record_end_time_.is_null()) {
RecordResolveTimeDiff("ABeforeAAAA", task_start_time_,
a_record_end_time_, aaaa_record_end_time_);
}
break;
case DnsQueryType::HTTPS: {
base::TimeTicks first_address_end_time =
std::min(a_record_end_time_, aaaa_record_end_time_);
if (!first_address_end_time.is_null()) {
RecordResolveTimeDiff("AddressRecordBeforeHTTPS", task_start_time_,
first_address_end_time, now);
}
break;
}
default:
break;
}
// TODO(crbug.com/1381506): Use new results type directly instead of
// converting to HostCache::Entry.
HostCache::Entry legacy_results(std::move(results).value(),
base::Time::Now(), tick_clock_->NowTicks(),
HostCache::Entry::SOURCE_DNS);
// Merge results with saved results from previous transactions.
if (saved_results_) {
// If saved result is a deferred failure, try again to complete with that
// failure.
if (saved_results_is_failure_) {
OnFailure(saved_results_.value().error(), /*allow_fallback=*/true,
saved_results_.value().GetOptionalTtl());
return;
}
switch (transaction_info.type) {
case DnsQueryType::A:
// Canonical names from A results have lower priority than those
// from AAAA results, so merge to the back.
legacy_results = HostCache::Entry::MergeEntries(
std::move(saved_results_).value(), std::move(legacy_results));
break;
case DnsQueryType::AAAA:
// Canonical names from AAAA results take priority over those
// from A results, so merge to the front.
legacy_results = HostCache::Entry::MergeEntries(
std::move(legacy_results), std::move(saved_results_).value());
break;
case DnsQueryType::HTTPS:
// No particular importance to order.
legacy_results = HostCache::Entry::MergeEntries(
std::move(legacy_results), std::move(saved_results_).value());
break;
default:
// Only expect address query types with multiple transactions.
NOTREACHED();
}
}
saved_results_ = std::move(legacy_results);
OnTransactionsFinished();
}
bool IsFatalTransactionFailure(int transaction_error,
const TransactionInfo& transaction_info,
const DnsResponse* response) {
if (transaction_info.type != DnsQueryType::HTTPS) {
DCHECK(transaction_info.error_behavior !=
TransactionErrorBehavior::kFatalOrEmpty);
return false;
}
// These values are logged to UMA. Entries should not be renumbered and
// numeric values should never be reused. Please keep in sync with
// "DNS.SvcbHttpsTransactionError" in
// src/tools/metrics/histograms/enums.xml.
enum class HttpsTransactionError {
kNoError = 0,
kInsecureError = 1,
kNonFatalError = 2,
kFatalErrorDisabled = 3,
kFatalErrorEnabled = 4,
kMaxValue = kFatalErrorEnabled
} error;
if (transaction_error == OK ||
(transaction_error == ERR_NAME_NOT_RESOLVED && response &&
response->IsValid())) {
error = HttpsTransactionError::kNoError;
} else if (!secure_) {
// HTTPS failures are never fatal via insecure DNS.
DCHECK(transaction_info.error_behavior !=
TransactionErrorBehavior::kFatalOrEmpty);
error = HttpsTransactionError::kInsecureError;
} else if (transaction_error == ERR_DNS_SERVER_FAILED && response &&
response->rcode() != dns_protocol::kRcodeSERVFAIL) {
// For server failures, only SERVFAIL is fatal.
error = HttpsTransactionError::kNonFatalError;
} else if (features::kUseDnsHttpsSvcbEnforceSecureResponse.Get()) {
DCHECK(transaction_info.error_behavior ==
TransactionErrorBehavior::kFatalOrEmpty);
error = HttpsTransactionError::kFatalErrorEnabled;
} else {
DCHECK(transaction_info.error_behavior !=
TransactionErrorBehavior::kFatalOrEmpty);
error = HttpsTransactionError::kFatalErrorDisabled;
}
UMA_HISTOGRAM_ENUMERATION("Net.DNS.DnsTask.SvcbHttpsTransactionError",
error);
return error == HttpsTransactionError::kFatalErrorEnabled;
}
// Called on processing for one or more individual transaction being
// completed/cancelled. Processes overall results if all transactions are
// finished.
void OnTransactionsFinished() {
if (!transactions_in_progress_.empty() || !transactions_needed_.empty()) {
delegate_->OnIntermediateTransactionsComplete();
MaybeStartTimeoutTimer();
return;
}
DCHECK(saved_results_.has_value());
HostCache::Entry results = std::move(*saved_results_);
timeout_timer_.Stop();
std::vector<IPEndPoint> ip_endpoints = results.ip_endpoints();
// If there are multiple addresses, and at least one is IPv6, need to
// sort them.
bool at_least_one_ipv6_address = base::ranges::any_of(
ip_endpoints,
[](auto& e) { return e.GetFamily() == ADDRESS_FAMILY_IPV6; });
if (at_least_one_ipv6_address) {
// Sort addresses if needed. Sort could complete synchronously.
client_->GetAddressSorter()->Sort(
ip_endpoints,
base::BindOnce(&DnsTask::OnSortComplete, AsWeakPtr(),
tick_clock_->NowTicks(), std::move(results), secure_));
return;
}
OnSuccess(std::move(results));
}
void OnSortComplete(base::TimeTicks sort_start_time,
HostCache::Entry results,
bool secure,
bool success,
std::vector<IPEndPoint> sorted) {
results.set_ip_endpoints(std::move(sorted));
if (!success) {
OnFailure(ERR_DNS_SORT_ERROR, /*allow_fallback=*/true,
results.GetOptionalTtl());
return;
}
// AddressSorter prunes unusable destinations.
if (results.ip_endpoints().empty() && results.text_records().empty() &&
results.hostnames().empty()) {
LOG(WARNING) << "Address list empty after RFC3484 sort";
OnFailure(ERR_NAME_NOT_RESOLVED, /*allow_fallback=*/true,
results.GetOptionalTtl());
return;
}
OnSuccess(std::move(results));
}
bool AnyPotentiallyFatalTransactionsRemain() {
auto is_fatal_or_empty_error = [](TransactionErrorBehavior behavior) {
return behavior == TransactionErrorBehavior::kFatalOrEmpty;
};
return base::ranges::any_of(transactions_needed_, is_fatal_or_empty_error,
&TransactionInfo::error_behavior) ||
base::ranges::any_of(transactions_in_progress_,
is_fatal_or_empty_error,
&TransactionInfo::error_behavior);
}
void CancelNonFatalTransactions() {
auto has_non_fatal_or_empty_error = [](const TransactionInfo& info) {
return info.error_behavior != TransactionErrorBehavior::kFatalOrEmpty;
};
base::EraseIf(transactions_needed_, has_non_fatal_or_empty_error);
base::EraseIf(transactions_in_progress_, has_non_fatal_or_empty_error);
}
void OnFailure(
int net_error,
bool allow_fallback,
absl::optional<base::TimeDelta> ttl = absl::nullopt,
absl::optional<DnsQueryType> failed_transaction_type = absl::nullopt) {
if (httpssvc_metrics_ && failed_transaction_type.has_value() &&
IsAddressType(failed_transaction_type.value())) {
httpssvc_metrics_->SaveAddressQueryFailure();
}
DCHECK_NE(OK, net_error);
HostCache::Entry results(net_error, HostCache::Entry::SOURCE_UNKNOWN, ttl);
// On non-fatal errors, if any potentially fatal transactions remain, need
// to defer ending the task in case any of those remaining transactions end
// with a fatal failure.
if (allow_fallback && AnyPotentiallyFatalTransactionsRemain()) {
saved_results_ = std::move(results);
saved_results_is_failure_ = true;
CancelNonFatalTransactions();
OnTransactionsFinished();
return;
}
net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK, [&] {
return NetLogDnsTaskFailedParams(net_error, failed_transaction_type, ttl,
base::OptionalToPtr(saved_results_));
});
// Expect this to result in destroying `this` and thus cancelling any
// remaining transactions.
delegate_->OnDnsTaskComplete(task_start_time_, allow_fallback,
std::move(results), secure_);
}
void OnSuccess(HostCache::Entry results) {
net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK,
[&] { return NetLogResults(results); });
delegate_->OnDnsTaskComplete(task_start_time_, /*allow_fallback=*/true,
std::move(results), secure_);
}
// Returns whether any transactions left to finish are of a transaction type
// in `types`. Used for logging and starting the timeout timer (see
// MaybeStartTimeoutTimer()).
bool AnyOfTypeTransactionsRemain(
std::initializer_list<DnsQueryType> types) const {
// Should only be called if some transactions are still running or waiting
// to run.
DCHECK(!transactions_needed_.empty() || !transactions_in_progress_.empty());
// Check running transactions.
if (base::ranges::find_first_of(transactions_in_progress_, types,
/*pred=*/{},
/*proj1=*/&TransactionInfo::type) !=
transactions_in_progress_.end()) {
return true;
}
// Check queued transactions, in case it ever becomes possible to get here
// without the transactions being started first.
return base::ranges::find_first_of(transactions_needed_, types, /*pred=*/{},
/*proj1=*/&TransactionInfo::type) !=
transactions_needed_.end();
}
void MaybeStartTimeoutTimer() {
// Should only be called if some transactions are still running or waiting
// to run.
DCHECK(!transactions_in_progress_.empty() || !transactions_needed_.empty());
// Timer already running.
if (timeout_timer_.IsRunning())
return;
// Always wait for address transactions.
if (AnyOfTypeTransactionsRemain({DnsQueryType::A, DnsQueryType::AAAA}))
return;
base::TimeDelta timeout_max;
int extra_time_percent = 0;
base::TimeDelta timeout_min;
if (AnyOfTypeTransactionsRemain({DnsQueryType::HTTPS})) {
DCHECK(https_svcb_options_.enable);
if (secure_) {
timeout_max = https_svcb_options_.secure_extra_time_max;
extra_time_percent = https_svcb_options_.secure_extra_time_percent;
timeout_min = https_svcb_options_.secure_extra_time_min;
} else {
timeout_max = https_svcb_options_.insecure_extra_time_max;
extra_time_percent = https_svcb_options_.insecure_extra_time_percent;
timeout_min = https_svcb_options_.insecure_extra_time_min;
}
// Skip timeout for secure requests if the timeout would be a fatal
// failure.
if (secure_ && features::kUseDnsHttpsSvcbEnforceSecureResponse.Get()) {
timeout_max = base::TimeDelta();
extra_time_percent = 0;
timeout_min = base::TimeDelta();
}
} else {
// Unhandled supplemental type.
NOTREACHED();
}
base::TimeDelta timeout;
if (extra_time_percent > 0) {
base::TimeDelta total_time_for_other_transactions =
tick_clock_->NowTicks() - task_start_time_;
timeout = total_time_for_other_transactions * extra_time_percent / 100;
// Use at least 1ms to ensure timeout doesn't occur immediately in tests.
timeout = std::max(timeout, base::Milliseconds(1));
if (!timeout_max.is_zero())
timeout = std::min(timeout, timeout_max);
if (!timeout_min.is_zero())
timeout = std::max(timeout, timeout_min);
} else {
// If no relative timeout, use a non-zero min/max as timeout. If both are
// non-zero, that's not very sensible, but arbitrarily take the higher
// timeout.
timeout = std::max(timeout_min, timeout_max);
}
if (!timeout.is_zero())
timeout_timer_.Start(
FROM_HERE, timeout,
base::BindOnce(&DnsTask::OnTimeout, base::Unretained(this)));
}
bool ShouldTriggerHttpToHttpsUpgrade(const Results& results) {
// Upgrade if at least one HTTPS record was compatible, and the host uses an
// upgradable scheme.
return base::ranges::any_of(
results,
[](const std::unique_ptr<HostResolverInternalResult>& result) {
return result->type() ==
HostResolverInternalResult::Type::kMetadata;
}) &&
(GetScheme(host_) == url::kHttpScheme ||
GetScheme(host_) == url::kWsScheme);
}
const raw_ptr<DnsClient> client_;
absl::variant<url::SchemeHostPort, std::string> host_;
base::SafeRef<ResolveContext> resolve_context_;
// Whether lookups in this DnsTask should occur using DoH or plaintext.
const bool secure_;
const SecureDnsMode secure_dns_mode_;
// The listener to the results of this DnsTask.
const raw_ptr<Delegate> delegate_;
const NetLogWithSource net_log_;
bool any_transaction_started_ = false;
base::circular_deque<TransactionInfo> transactions_needed_;
// Active transactions have iterators pointing to their entry in this set, so
// individual entries should not be modified or removed until completion or
// cancellation of the transaction.
std::set<TransactionInfo> transactions_in_progress_;
// For histograms.
base::TimeTicks a_record_end_time_;
base::TimeTicks aaaa_record_end_time_;
absl::optional<HostCache::Entry> saved_results_;
bool saved_results_is_failure_ = false;
const raw_ptr<const base::TickClock> tick_clock_;
base::TimeTicks task_start_time_;
absl::optional<HttpssvcMetrics> httpssvc_metrics_;
// Timer for task timeout. Generally started after completion of address
// transactions to allow aborting experimental or supplemental transactions.
base::OneShotTimer timeout_timer_;
// If true, there are still significant fallback options available if this
// task completes unsuccessfully. Used as a signal that underlying
// transactions should timeout more quickly.
bool fallback_available_;
const HostResolver::HttpsSvcbOptions https_svcb_options_;
};
//-----------------------------------------------------------------------------
// Aggregates all Requests for the same Key. Dispatched via
// PrioritizedDispatcher.
class HostResolverManager::Job : public PrioritizedDispatcher::Job,
public HostResolverManager::DnsTask::Delegate {
public:
// Creates new job for |key| where |request_net_log| is bound to the
// request that spawned it.
Job(const base::WeakPtr<HostResolverManager>& resolver,
JobKey key,
ResolveHostParameters::CacheUsage cache_usage,
HostCache* host_cache,
std::deque<TaskType> tasks,
RequestPriority priority,
const NetLogWithSource& source_net_log,
const base::TickClock* tick_clock,
const HostResolver::HttpsSvcbOptions& https_svcb_options)
: resolver_(resolver),
key_(std::move(key)),
cache_usage_(cache_usage),
host_cache_(host_cache),
tasks_(tasks),
priority_tracker_(priority),
tick_clock_(tick_clock),
https_svcb_options_(https_svcb_options),
net_log_(
NetLogWithSource::Make(source_net_log.net_log(),
NetLogSourceType::HOST_RESOLVER_IMPL_JOB)) {
source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_CREATE_JOB);
net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB, [&] {
return NetLogJobCreationParams(source_net_log.source());
});
}
~Job() override {
bool was_queued = is_queued();
bool was_running = is_running();
// Clean up now for nice NetLog.
Finish();
if (was_running) {
// This Job was destroyed while still in flight.
net_log_.EndEventWithNetErrorCode(
NetLogEventType::HOST_RESOLVER_MANAGER_JOB, ERR_ABORTED);
} else if (was_queued) {
// Job was cancelled before it could run.
// TODO(szym): is there any benefit in having this distinction?
net_log_.AddEvent(NetLogEventType::CANCELLED);
net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB);
}
// else CompleteRequests logged EndEvent.
while (!requests_.empty()) {
// Log any remaining Requests as cancelled.
RequestImpl* req = requests_.head()->value();
req->RemoveFromList();
CHECK(key_ == req->GetJobKey());
req->OnJobCancelled(key_);
}
}
// Add this job to the dispatcher. If "at_head" is true, adds at the front
// of the queue.
void Schedule(bool at_head) {
DCHECK(!is_queued());
PrioritizedDispatcher::Handle handle;
DCHECK(dispatched_);
if (!at_head) {
handle = resolver_->dispatcher_->Add(this, priority());
} else {
handle = resolver_->dispatcher_->AddAtHead(this, priority());
}
// The dispatcher could have started |this| in the above call to Add, which
// could have called Schedule again. In that case |handle| will be null,
// but |handle_| may have been set by the other nested call to Schedule.
if (!handle.is_null()) {
DCHECK(handle_.is_null());
handle_ = handle;
}
}
void AddRequest(RequestImpl* request) {
// Job currently assumes a 1:1 correspondence between ResolveContext and
// HostCache. Since the ResolveContext is part of the JobKey, any request
// added to any existing Job should share the same HostCache.
DCHECK_EQ(host_cache_, request->host_cache());
// TODO(crbug.com/1206799): Check equality of whole host once Jobs are
// separated by scheme/port.
DCHECK_EQ(GetHostname(key_.host),
request->request_host().GetHostnameWithoutBrackets());
request->AssignJob(weak_ptr_factory_.GetSafeRef());
priority_tracker_.Add(request->priority());
request->source_net_log().AddEventReferencingSource(
NetLogEventType::HOST_RESOLVER_MANAGER_JOB_ATTACH, net_log_.source());
net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_REQUEST_ATTACH,
[&] {
return NetLogJobAttachParams(
request->source_net_log().source(), priority());
});
if (!request->parameters().is_speculative)
had_non_speculative_request_ = true;
requests_.Append(request);
UpdatePriority();
}
void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) {
// TODO(crbug.com/1206799): Check equality of whole host once Jobs are
// separated by scheme/port.
DCHECK_EQ(GetHostname(key_.host),
req->request_host().GetHostnameWithoutBrackets());
priority_tracker_.Remove(req->priority());
req->set_priority(priority);
priority_tracker_.Add(req->priority());
UpdatePriority();
}
// Detach cancelled request. If it was the last active Request, also finishes
// this Job.
void CancelRequest(RequestImpl* request) {
// TODO(crbug.com/1206799): Check equality of whole host once Jobs are
// separated by scheme/port.
DCHECK_EQ(GetHostname(key_.host),
request->request_host().GetHostnameWithoutBrackets());
DCHECK(!requests_.empty());
priority_tracker_.Remove(request->priority());
net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_REQUEST_DETACH,
[&] {
return NetLogJobAttachParams(
request->source_net_log().source(), priority());
});
if (num_active_requests() > 0) {
UpdatePriority();
request->RemoveFromList();
} else {
// If we were called from a Request's callback within CompleteRequests,
// that Request could not have been cancelled, so num_active_requests()
// could not be 0. Therefore, we are not in CompleteRequests().
CompleteRequestsWithError(ERR_DNS_REQUEST_CANCELLED,
/*task_type=*/absl::nullopt);
}
}
// Called from AbortJobsWithoutTargetNetwork(). Completes all requests and
// destroys the job. This currently assumes the abort is due to a network
// change.
// TODO This should not delete |this|.
void Abort() {
CompleteRequestsWithError(ERR_NETWORK_CHANGED, /*task_type=*/absl::nullopt);
}
// Gets a closure that will abort an insecure DnsTask (see
// AbortInsecureDnsTask()) iff |this| is still valid. Useful if aborting a
// list of Jobs as some may be cancelled while aborting others.
base::OnceClosure GetAbortInsecureDnsTaskClosure(int error,
bool fallback_only) {
return base::BindOnce(&Job::AbortInsecureDnsTask,
weak_ptr_factory_.GetWeakPtr(), error, fallback_only);
}
// Aborts or removes any current/future insecure DnsTasks if a
// HostResolverSystemTask is available for fallback. If no fallback is
// available and |fallback_only| is false, a job that is currently running an
// insecure DnsTask will be completed with |error|.
void AbortInsecureDnsTask(int error, bool fallback_only) {
bool has_system_fallback = base::Contains(tasks_, TaskType::SYSTEM);
if (has_system_fallback) {
for (auto it = tasks_.begin(); it != tasks_.end();) {
if (*it == TaskType::DNS)
it = tasks_.erase(it);
else
++it;
}
}
if (dns_task_ && !dns_task_->secure()) {
if (has_system_fallback) {
KillDnsTask();
dns_task_error_ = OK;
RunNextTask();
} else if (!fallback_only) {
CompleteRequestsWithError(error, /*task_type=*/absl::nullopt);
}
}
}
// Called by HostResolverManager when this job is evicted due to queue
// overflow. Completes all requests and destroys the job. The job could have
// waiting requests that will receive completion callbacks, so cleanup
// asynchronously to avoid reentrancy.
void OnEvicted() {
DCHECK(!is_running());
DCHECK(is_queued());
handle_.Reset();
net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_EVICTED);
// This signals to CompleteRequests that parts of this job never ran.
// Job must be saved in |resolver_| to be completed asynchronously.
// Otherwise the job will be destroyed with requests silently cancelled
// before completion runs.
DCHECK(self_iterator_);
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&Job::CompleteRequestsWithError,
weak_ptr_factory_.GetWeakPtr(),
ERR_HOST_RESOLVER_QUEUE_TOO_LARGE,
/*task_type=*/absl::nullopt));
}
// Attempts to serve the job from HOSTS. Returns true if succeeded and
// this Job was destroyed.
bool ServeFromHosts() {
DCHECK_GT(num_active_requests(), 0u);
absl::optional<HostCache::Entry> results = resolver_->ServeFromHosts(
GetHostname(key_.host), key_.query_types,
key_.flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6, tasks_);
if (results) {
// This will destroy the Job.
CompleteRequests(results.value(), base::TimeDelta(),
true /* allow_cache */, true /* secure */,
TaskType::HOSTS);
return true;
}
return false;
}
void OnAddedToJobMap(JobMap::iterator iterator) {
DCHECK(!self_iterator_);
DCHECK(iterator != resolver_->jobs_.end());
self_iterator_ = iterator;
}
void OnRemovedFromJobMap() {
DCHECK(self_iterator_);
self_iterator_ = absl::nullopt;
}
void RunNextTask() {
// If there are no tasks left to try, cache any stored results and complete
// the request with the last stored result. All stored results should be
// errors.
if (tasks_.empty()) {
// If there are no stored results, complete with an error.
if (completion_results_.size() == 0) {
CompleteRequestsWithError(ERR_NAME_NOT_RESOLVED,
/*task_type=*/absl::nullopt);
return;
}
// Cache all but the last result here. The last result will be cached
// as part of CompleteRequests.
for (size_t i = 0; i < completion_results_.size() - 1; ++i) {
const auto& result = completion_results_[i];
DCHECK_NE(OK, result.entry.error());
MaybeCacheResult(result.entry, result.ttl, result.secure);
}
const auto& last_result = completion_results_.back();
DCHECK_NE(OK, last_result.entry.error());
CompleteRequests(
last_result.entry, last_result.ttl, true /* allow_cache */,
last_result.secure,
last_result.secure ? TaskType::SECURE_DNS : TaskType::DNS);
return;
}
TaskType next_task = tasks_.front();
// Schedule insecure DnsTasks and HostResolverSystemTasks with the
// dispatcher.
if (!dispatched_ &&
(next_task == TaskType::DNS || next_task == TaskType::SYSTEM ||
next_task == TaskType::MDNS)) {
dispatched_ = true;
job_running_ = false;
Schedule(false);
DCHECK(is_running() || is_queued());
// Check for queue overflow.
PrioritizedDispatcher& dispatcher = *resolver_->dispatcher_;
if (dispatcher.num_queued_jobs() > resolver_->max_queued_jobs_) {
Job* evicted = static_cast<Job*>(dispatcher.EvictOldestLowest());
DCHECK(evicted);
evicted->OnEvicted();
}
return;
}
if (start_time_ == base::TimeTicks()) {
net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_STARTED);
start_time_ = tick_clock_->NowTicks();
}
tasks_.pop_front();
job_running_ = true;
switch (next_task) {
case TaskType::SYSTEM:
StartSystemTask();
break;
case TaskType::DNS:
StartDnsTask(false /* secure */);
break;
case TaskType::SECURE_DNS:
StartDnsTask(true /* secure */);
break;
case TaskType::MDNS:
StartMdnsTask();
break;
case TaskType::INSECURE_CACHE_LOOKUP:
InsecureCacheLookup();
break;
case TaskType::NAT64:
StartNat64Task();
break;
case TaskType::SECURE_CACHE_LOOKUP:
case TaskType::CACHE_LOOKUP:
case TaskType::CONFIG_PRESET:
case TaskType::HOSTS:
// These task types should have been handled synchronously in
// ResolveLocally() prior to Job creation.
NOTREACHED();
break;
}
}
const JobKey& key() const { return key_; }
bool is_queued() const { return !handle_.is_null(); }
bool is_running() const { return job_running_; }
bool HasTargetNetwork() const {
return key_.GetTargetNetwork() != handles::kInvalidNetworkHandle;
}
private:
base::Value::Dict NetLogJobCreationParams(const NetLogSource& source) {
base::Value::Dict dict;
source.AddToEventParameters(dict);
dict.Set("host", ToLogStringValue(key_.host));
base::Value::List query_types_list;
for (DnsQueryType query_type : key_.query_types)
query_types_list.Append(kDnsQueryTypes.at(query_type));
dict.Set("dns_query_types", std::move(query_types_list));
dict.Set("secure_dns_mode", base::strict_cast<int>(key_.secure_dns_mode));
dict.Set("network_anonymization_key",
key_.network_anonymization_key.ToDebugString());
return dict;
}
void Finish() {
if (is_running()) {
// Clean up but don't run any callbacks.
system_task_ = nullptr;
KillDnsTask();
mdns_task_ = nullptr;
job_running_ = false;
if (dispatched_) {
// Job should only ever occupy one slot after any tasks that may have
// required additional slots, e.g. DnsTask, have been killed, and
// additional slots are expected to be vacated as part of killing the
// task.
DCHECK_EQ(1, num_occupied_job_slots_);
if (resolver_)
resolver_->dispatcher_->OnJobFinished();
num_occupied_job_slots_ = 0;
}
} else if (is_queued()) {
DCHECK(dispatched_);
if (resolver_)
resolver_->dispatcher_->Cancel(handle_);
handle_.Reset();
}
}
void KillDnsTask() {
if (dns_task_) {
if (dispatched_) {
while (num_occupied_job_slots_ > 1 || is_queued()) {
ReduceByOneJobSlot();
}
}
dns_task_.reset();
}
}
// Reduce the number of job slots occupied and queued in the dispatcher by
// one. If the next Job slot is queued in the dispatcher, cancels the queued
// job. Otherwise, the next Job has been started by the PrioritizedDispatcher,
// so signals it is complete.
void ReduceByOneJobSlot() {
DCHECK_GE(num_occupied_job_slots_, 1);
DCHECK(dispatched_);
if (is_queued()) {
if (resolver_)
resolver_->dispatcher_->Cancel(handle_);
handle_.Reset();
} else if (num_occupied_job_slots_ > 1) {
if (resolver_)
resolver_->dispatcher_->OnJobFinished();
--num_occupied_job_slots_;
} else {
NOTREACHED();
}
}
void UpdatePriority() {
if (is_queued())
handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
}
// PrioritizedDispatcher::Job:
void Start() override {
handle_.Reset();
++num_occupied_job_slots_;
if (num_occupied_job_slots_ >= 2) {
if (!dns_task_) {
resolver_->dispatcher_->OnJobFinished();
return;
}
StartNextDnsTransaction();
DCHECK_EQ(num_occupied_job_slots_,
dns_task_->num_transactions_in_progress());
if (dns_task_->num_additional_transactions_needed() >= 1) {
Schedule(true);
}
return;
}
DCHECK(!is_running());
DCHECK(!tasks_.empty());
RunNextTask();
// Caution: Job::Start must not complete synchronously.
}
// TODO(szym): Since DnsTransaction does not consume threads, we can increase
// the limits on |dispatcher_|. But in order to keep the number of
// ThreadPool threads low, we will need to use an "inner"
// PrioritizedDispatcher with tighter limits.
void StartSystemTask() {
DCHECK(dispatched_);
DCHECK_EQ(1, num_occupied_job_slots_);
DCHECK(HasAddressType(key_.query_types));
system_task_ = HostResolverSystemTask::Create(
std::string(GetHostname(key_.host)),
HostResolver::DnsQueryTypeSetToAddressFamily(key_.query_types),
key_.flags, resolver_->host_resolver_system_params_, net_log_,
key_.GetTargetNetwork());
// Start() could be called from within Resolve(), hence it must NOT directly
// call OnSystemTaskComplete, for example, on synchronous failure.
system_task_->Start(base::BindOnce(&Job::OnSystemTaskComplete,
base::Unretained(this),
tick_clock_->NowTicks()));
}
// Called by HostResolverSystemTask when it completes.
void OnSystemTaskComplete(base::TimeTicks start_time,
const AddressList& addr_list,
int /*os_error*/,
int net_error) {
DCHECK(system_task_);
base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
if (net_error == OK)
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.SystemTask.SuccessTime", duration);
else
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.SystemTask.FailureTime", duration);
if (dns_task_error_ != OK && net_error == OK) {
// This HostResolverSystemTask was a fallback resolution after a failed
// insecure DnsTask.
resolver_->OnFallbackResolve(dns_task_error_);
}
if (ContainsIcannNameCollisionIp(addr_list.endpoints()))
net_error = ERR_ICANN_NAME_COLLISION;
base::TimeDelta ttl = base::Seconds(kNegativeCacheEntryTTLSeconds);
if (net_error == OK)
ttl = base::Seconds(kCacheEntryTTLSeconds);
auto aliases = std::set<std::string>(addr_list.dns_aliases().begin(),
addr_list.dns_aliases().end());
// Source unknown because the system resolver could have gotten it from a
// hosts file, its own cache, a DNS lookup or somewhere else.
// Don't store the |ttl| in cache since it's not obtained from the server.
CompleteRequests(
HostCache::Entry(
net_error,
net_error == OK ? addr_list.endpoints() : std::vector<IPEndPoint>(),
std::move(aliases), HostCache::Entry::SOURCE_UNKNOWN),
ttl, /*allow_cache=*/true, /*secure=*/false, TaskType::SYSTEM);
}
void InsecureCacheLookup() {
// Insecure cache lookups for requests allowing stale results should have
// occurred prior to Job creation.
DCHECK(cache_usage_ != ResolveHostParameters::CacheUsage::STALE_ALLOWED);
absl::optional<HostCache::EntryStaleness> stale_info;
absl::optional<HostCache::Entry> resolved = resolver_->MaybeServeFromCache(
host_cache_, key_.ToCacheKey(/*secure=*/false), cache_usage_,
false /* ignore_secure */, net_log_, &stale_info);
if (resolved) {
DCHECK(stale_info);
DCHECK(!stale_info.value().is_stale());
CompleteRequestsWithoutCache(resolved.value(), std::move(stale_info),
TaskType::INSECURE_CACHE_LOOKUP);
} else {
RunNextTask();
}
}
void StartDnsTask(bool secure) {
DCHECK_EQ(secure, !dispatched_);
DCHECK_EQ(dispatched_ ? 1 : 0, num_occupied_job_slots_);
DCHECK(!resolver_->ShouldForceSystemResolverDueToTestOverride());
// Need to create the task even if we're going to post a failure instead of
// running it, as a "started" job needs a task to be properly cleaned up.
dns_task_ = std::make_unique<DnsTask>(
resolver_->dns_client_.get(), key_.host, key_.query_types,
&*key_.resolve_context, secure, key_.secure_dns_mode, this, net_log_,
tick_clock_, !tasks_.empty() /* fallback_available */,
https_svcb_options_);
dns_task_->StartNextTransaction();
// Schedule a second transaction, if needed. DoH queries can bypass the
// dispatcher and start all of their transactions immediately.
if (secure) {
while (dns_task_->num_additional_transactions_needed() >= 1)
dns_task_->StartNextTransaction();
DCHECK_EQ(dns_task_->num_additional_transactions_needed(), 0);
} else if (dns_task_->num_additional_transactions_needed() >= 1) {
Schedule(true);
}
}
void StartNextDnsTransaction() {
DCHECK(dns_task_);
DCHECK_EQ(dns_task_->secure(), !dispatched_);
DCHECK(!dispatched_ || num_occupied_job_slots_ ==
dns_task_->num_transactions_in_progress() + 1);
DCHECK_GE(dns_task_->num_additional_transactions_needed(), 1);
dns_task_->StartNextTransaction();
}
// Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
// deleted before this callback. In this case dns_task is deleted as well,
// so we use it as indicator whether Job is still valid.
void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
base::TimeDelta duration,
bool allow_fallback,
const HostCache::Entry& failure_results,
bool secure) {
DCHECK_NE(OK, failure_results.error());
if (!secure) {
DCHECK_NE(key_.secure_dns_mode, SecureDnsMode::kSecure);
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.InsecureDnsTask.FailureTime",
duration);
}
if (!dns_task)
return;
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.JobQueueTime.Failure",
total_transaction_time_queued_);
// If one of the fallback tasks doesn't complete the request, store a result
// to use during request completion.
base::TimeDelta ttl =
failure_results.has_ttl() ? failure_results.ttl() : base::Seconds(0);
completion_results_.push_back({failure_results, ttl, secure});
dns_task_error_ = failure_results.error();
KillDnsTask();
if (!allow_fallback)
tasks_.clear();
RunNextTask();
}
// HostResolverManager::DnsTask::Delegate implementation:
void OnDnsTaskComplete(base::TimeTicks start_time,
bool allow_fallback,
HostCache::Entry results,
bool secure) override {
DCHECK(dns_task_);
// Tasks containing address queries are only considered successful overall
// if they find address results. However, DnsTask may claim success if any
// transaction, e.g. a supplemental HTTPS transaction, finds results.
DCHECK(!key_.query_types.Has(DnsQueryType::UNSPECIFIED));
if (HasAddressType(key_.query_types) && results.error() == OK &&
results.ip_endpoints().empty()) {
results.set_error(ERR_NAME_NOT_RESOLVED);
}
base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
if (results.error() != OK) {
OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, allow_fallback,
results, secure);
return;
}
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.SuccessTime", duration);
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.JobQueueTime.Success",
total_transaction_time_queued_);
// Reset the insecure DNS failure counter if an insecure DnsTask completed
// successfully.
if (!secure)
resolver_->dns_client_->ClearInsecureFallbackFailures();
base::TimeDelta bounded_ttl =
std::max(results.ttl(), base::Seconds(kMinimumTTLSeconds));
if (ContainsIcannNameCollisionIp(results.ip_endpoints())) {
CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION,
secure ? TaskType::SECURE_DNS : TaskType::DNS);
return;
}
CompleteRequests(results, bounded_ttl, true /* allow_cache */, secure,
secure ? TaskType::SECURE_DNS : TaskType::DNS);
}
void OnIntermediateTransactionsComplete() override {
if (dispatched_) {
DCHECK_GE(num_occupied_job_slots_,
dns_task_->num_transactions_in_progress());
int unused_slots =
num_occupied_job_slots_ - dns_task_->num_transactions_in_progress();
// Reuse vacated slots for any remaining transactions.
while (unused_slots > 0 &&
dns_task_->num_additional_transactions_needed() > 0) {
dns_task_->StartNextTransaction();
--unused_slots;
}
// If all remaining transactions found a slot, no more needed from the
// dispatcher.
if (is_queued() && dns_task_->num_additional_transactions_needed() == 0) {
resolver_->dispatcher_->Cancel(handle_);
handle_.Reset();
}
// Relinquish any remaining extra slots.
while (unused_slots > 0) {
ReduceByOneJobSlot();
--unused_slots;
}
} else if (dns_task_->num_additional_transactions_needed() >= 1) {
dns_task_->StartNextTransaction();
}
}
void AddTransactionTimeQueued(base::TimeDelta time_queued) override {
total_transaction_time_queued_ += time_queued;
}
void StartMdnsTask() {
// No flags are supported for MDNS except
// HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6 (which is not actually an
// input flag).
DCHECK_EQ(0, key_.flags & ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6);
MDnsClient* client = nullptr;
int rv = resolver_->GetOrCreateMdnsClient(&client);
mdns_task_ = std::make_unique<HostResolverMdnsTask>(
client, std::string{GetHostname(key_.host)}, key_.query_types);
if (rv == OK) {
mdns_task_->Start(
base::BindOnce(&Job::OnMdnsTaskComplete, base::Unretained(this)));
} else {
// Could not create an mDNS client. Since we cannot complete synchronously
// from here, post a failure without starting the task.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&Job::OnMdnsImmediateFailure,
weak_ptr_factory_.GetWeakPtr(), rv));
}
}
void OnMdnsTaskComplete() {
DCHECK(mdns_task_);
// TODO(crbug.com/846423): Consider adding MDNS-specific logging.
HostCache::Entry results = mdns_task_->GetResults();
if (ContainsIcannNameCollisionIp(results.ip_endpoints())) {
CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION, TaskType::MDNS);
return;
}
// MDNS uses a separate cache, so skip saving result to cache.
// TODO(crbug.com/926300): Consider merging caches.
CompleteRequestsWithoutCache(results, absl::nullopt /* stale_info */,
TaskType::MDNS);
}
void OnMdnsImmediateFailure(int rv) {
DCHECK(mdns_task_);
DCHECK_NE(OK, rv);
CompleteRequestsWithError(rv, TaskType::MDNS);
}
void StartNat64Task() {
DCHECK(!nat64_task_);
RequestImpl* req = requests_.head()->value();
nat64_task_ = std::make_unique<HostResolverNat64Task>(
std::string{GetHostname(key_.host)}, req->network_anonymization_key(),
req->source_net_log(), req->resolve_context(), req->host_cache(),
resolver_);
nat64_task_->Start(base::BindOnce(&Job::OnNat64TaskComplete,
weak_ptr_factory_.GetWeakPtr()));
}
void OnNat64TaskComplete() {
DCHECK(nat64_task_);
HostCache::Entry results = nat64_task_->GetResults();
CompleteRequestsWithoutCache(results, absl::nullopt /* stale_info */,
TaskType::NAT64);
}
void RecordJobHistograms(const HostCache::Entry& results,
absl::optional<TaskType> task_type) {
int error = results.error();
// Used in UMA_HISTOGRAM_ENUMERATION. Do not renumber entries or reuse
// deprecated values.
enum Category {
RESOLVE_SUCCESS = 0,
RESOLVE_FAIL = 1,
RESOLVE_SPECULATIVE_SUCCESS = 2,
RESOLVE_SPECULATIVE_FAIL = 3,
RESOLVE_ABORT = 4,
RESOLVE_SPECULATIVE_ABORT = 5,
RESOLVE_MAX, // Bounding value.
};
Category category = RESOLVE_MAX; // Illegal value for later DCHECK only.
base::TimeDelta duration = tick_clock_->NowTicks() - start_time_;
if (error == OK) {
if (had_non_speculative_request_) {
category = RESOLVE_SUCCESS;
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime", duration);
} else {
category = RESOLVE_SPECULATIVE_SUCCESS;
}
} else if (error == ERR_NETWORK_CHANGED ||
error == ERR_HOST_RESOLVER_QUEUE_TOO_LARGE) {
category = had_non_speculative_request_ ? RESOLVE_ABORT
: RESOLVE_SPECULATIVE_ABORT;
} else {
if (had_non_speculative_request_) {
category = RESOLVE_FAIL;
UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime", duration);
} else {
category = RESOLVE_SPECULATIVE_FAIL;
}
}
DCHECK_LT(static_cast<int>(category),
static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
UMA_HISTOGRAM_ENUMERATION("Net.DNS.ResolveCategory", category, RESOLVE_MAX);
if (category == RESOLVE_FAIL ||
(start_time_ != base::TimeTicks() && category == RESOLVE_ABORT)) {
if (duration < base::Milliseconds(10))
base::UmaHistogramSparse("Net.DNS.ResolveError.Fast", std::abs(error));
else
base::UmaHistogramSparse("Net.DNS.ResolveError.Slow", std::abs(error));
}
if (error == OK) {
DCHECK(task_type.has_value());
// Record, for HTTPS-capable queries to a host known to serve HTTPS
// records, whether the HTTPS record was successfully received.
if (key_.query_types.Has(DnsQueryType::HTTPS) &&
// Skip http- and ws-schemed hosts. Although they query HTTPS records,
// successful queries are reported as errors, which would skew the
// metrics.
(GetScheme(key_.host) == url::kHttpsScheme ||
GetScheme(key_.host) == url::kWssScheme) &&
IsGoogleHostWithAlpnH3(GetHostname(key_.host))) {
bool has_metadata = !results.GetMetadatas().empty();
base::UmaHistogramExactLinear(
"Net.DNS.H3SupportedGoogleHost.TaskTypeMetadataAvailability2",
static_cast<int>(task_type.value()) * 2 + (has_metadata ? 1 : 0),
(static_cast<int>(TaskType::kMaxValue) + 1) * 2);
}
}
}
void MaybeCacheResult(const HostCache::Entry& results,
base::TimeDelta ttl,
bool secure) {
// If the request did not complete, don't cache it.
if (!results.did_complete())
return;
resolver_->CacheResult(host_cache_, key_.ToCacheKey(secure), results, ttl);
}
// Performs Job's last rites. Completes all Requests. Deletes this.
//
// If not |allow_cache|, result will not be stored in the host cache, even if
// result would otherwise allow doing so. Update the key to reflect |secure|,
// which indicates whether or not the result was obtained securely.
void CompleteRequests(const HostCache::Entry& results,
base::TimeDelta ttl,
bool allow_cache,
bool secure,
absl::optional<TaskType> task_type) {
CHECK(resolver_.get());
// This job must be removed from resolver's |jobs_| now to make room for a
// new job with the same key in case one of the OnComplete callbacks decides
// to spawn one. Consequently, if the job was owned by |jobs_|, the job
// deletes itself when CompleteRequests is done.
std::unique_ptr<Job> self_deleter;
if (self_iterator_)
self_deleter = resolver_->RemoveJob(self_iterator_.value());
Finish();
if (results.error() == ERR_DNS_REQUEST_CANCELLED) {
net_log_.AddEvent(NetLogEventType::CANCELLED);
net_log_.EndEventWithNetErrorCode(
NetLogEventType::HOST_RESOLVER_MANAGER_JOB, OK);
return;
}
net_log_.EndEventWithNetErrorCode(
NetLogEventType::HOST_RESOLVER_MANAGER_JOB, results.error());
// Handle all caching before completing requests as completing requests may
// start new requests that rely on cached results.
if (allow_cache)
MaybeCacheResult(results, ttl, secure);
RecordJobHistograms(results, task_type);
// Complete all of the requests that were attached to the job and
// detach them.
while (!requests_.empty()) {
RequestImpl* req = requests_.head()->value();
req->RemoveFromList();
CHECK(key_ == req->GetJobKey());
if (results.error() == OK && !req->parameters().is_speculative) {
req->set_results(
results.CopyWithDefaultPort(req->request_host().GetPort()));
}
req->OnJobCompleted(
key_, results.error(),
/*is_secure_network_error=*/secure && results.error() != OK);
// Check if the resolver was destroyed as a result of running the
// callback. If it was, we could continue, but we choose to bail.
if (!resolver_.get())
return;
}
// TODO(crbug.com/1200908): Call StartBootstrapFollowup() if any of the
// requests have the Bootstrap policy. Note: A naive implementation could
// cause an infinite loop if the bootstrap result has TTL=0.
}
void CompleteRequestsWithoutCache(
const HostCache::Entry& results,
absl::optional<HostCache::EntryStaleness> stale_info,
TaskType task_type) {
// Record the stale_info for all non-speculative requests, if it exists.
if (stale_info) {
for (auto* node = requests_.head(); node != requests_.end();
node = node->next()) {
if (!node->value()->parameters().is_speculative)
node->value()->set_stale_info(stale_info.value());
}
}
CompleteRequests(results, base::TimeDelta(), false /* allow_cache */,
false /* secure */, task_type);
}
// Convenience wrapper for CompleteRequests in case of failure.
void CompleteRequestsWithError(int net_error,
absl::optional<TaskType> task_type) {
DCHECK_NE(OK, net_error);
CompleteRequests(
HostCache::Entry(net_error, HostCache::Entry::SOURCE_UNKNOWN),
base::TimeDelta(), true /* allow_cache */, false /* secure */,
task_type);
}
RequestPriority priority() const override {
return priority_tracker_.highest_priority();
}
// Number of non-canceled requests in |requests_|.
size_t num_active_requests() const { return priority_tracker_.total_count(); }
base::WeakPtr<HostResolverManager> resolver_;
const JobKey key_;
const ResolveHostParameters::CacheUsage cache_usage_;
// TODO(crbug.com/969847): Consider allowing requests within a single Job to
// have different HostCaches.
const raw_ptr<HostCache> host_cache_;
struct CompletionResult {
const HostCache::Entry entry;
base::TimeDelta ttl;
bool secure;
};
// Results to use in last-ditch attempt to complete request.
std::vector<CompletionResult> completion_results_;
// The sequence of tasks to run in this Job. Tasks may be aborted and removed
// from the sequence, but otherwise the tasks will run in order until a
// successful result is found.
std::deque<TaskType> tasks_;
// Whether the job is running.
bool job_running_ = false;
// Tracks the highest priority across |requests_|.
PriorityTracker priority_tracker_;
bool had_non_speculative_request_ = false;
// Number of slots occupied by this Job in |dispatcher_|. Should be 0 when
// the job is not registered with any dispatcher.
int num_occupied_job_slots_ = 0;
// True once this Job has been sent to `resolver_->dispatcher_`.
bool dispatched_ = false;
// Result of DnsTask.
int dns_task_error_ = OK;
raw_ptr<const base::TickClock> tick_clock_;
base::TimeTicks start_time_;
HostResolver::HttpsSvcbOptions https_svcb_options_;
NetLogWithSource net_log_;
// Resolves the host using the system DNS resolver, which can be overridden
// for tests.
std::unique_ptr<HostResolverSystemTask> system_task_;
// Resolves the host using a DnsTransaction.
std::unique_ptr<DnsTask> dns_task_;
// Resolves the host using MDnsClient.
std::unique_ptr<HostResolverMdnsTask> mdns_task_;
// Perform NAT64 address synthesis to a given IPv4 literal.
std::unique_ptr<HostResolverNat64Task> nat64_task_;
// All Requests waiting for the result of this Job. Some can be canceled.
base::LinkedList<RequestImpl> requests_;
// A handle used for |dispatcher_|.
PrioritizedDispatcher::Handle handle_;
// Iterator to |this| in the JobMap. |nullopt| if not owned by the JobMap.
absl::optional<JobMap::iterator> self_iterator_;
base::TimeDelta total_transaction_time_queued_;
base::WeakPtrFactory<Job> weak_ptr_factory_{this};
};
//-----------------------------------------------------------------------------
HostResolverManager::HostResolverManager(
const HostResolver::ManagerOptions& options,
SystemDnsConfigChangeNotifier* system_dns_config_notifier,
NetLog* net_log)
: HostResolverManager(PassKey(),
options,
system_dns_config_notifier,
handles::kInvalidNetworkHandle,
net_log) {}
HostResolverManager::HostResolverManager(
base::PassKey<HostResolverManager>,
const HostResolver::ManagerOptions& options,
SystemDnsConfigChangeNotifier* system_dns_config_notifier,
handles::NetworkHandle target_network,
NetLog* net_log)
: host_resolver_system_params_(nullptr, options.max_system_retry_attempts),
net_log_(net_log),
system_dns_config_notifier_(system_dns_config_notifier),
target_network_(target_network),
check_ipv6_on_wifi_(options.check_ipv6_on_wifi),
ipv6_reachability_override_(base::FeatureList::IsEnabled(
features::kEnableIPv6ReachabilityOverride)),
tick_clock_(base::DefaultTickClock::GetInstance()),
https_svcb_options_(
options.https_svcb_options
? *options.https_svcb_options
: HostResolver::HttpsSvcbOptions::FromFeatures()) {
PrioritizedDispatcher::Limits job_limits = GetDispatcherLimits(options);
dispatcher_ = std::make_unique<PrioritizedDispatcher>(job_limits);
max_queued_jobs_ = job_limits.total_jobs * 100u;
DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
#if BUILDFLAG(IS_WIN)
EnsureWinsockInit();
#endif
#if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)) || \
BUILDFLAG(IS_FUCHSIA)
RunLoopbackProbeJob();
#endif
// Network-bound HostResolverManagers don't need to act on network changes.
if (!IsBoundToNetwork()) {
NetworkChangeNotifier::AddIPAddressObserver(this);
NetworkChangeNotifier::AddConnectionTypeObserver(this);
}
if (system_dns_config_notifier_)
system_dns_config_notifier_->AddObserver(this);
EnsureSystemHostResolverCallReady();
auto connection_type =
IsBoundToNetwork()
? NetworkChangeNotifier::GetNetworkConnectionType(target_network)
: NetworkChangeNotifier::GetConnectionType();
UpdateConnectionType(connection_type);
#if defined(ENABLE_BUILT_IN_DNS)
dns_client_ = DnsClient::CreateClient(net_log_);
dns_client_->SetInsecureEnabled(
options.insecure_dns_client_enabled,
options.additional_types_via_insecure_dns_enabled);
dns_client_->SetConfigOverrides(options.dns_config_overrides);
#else
DCHECK(options.dns_config_overrides == DnsConfigOverrides());
#endif
allow_fallback_to_systemtask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
}
HostResolverManager::~HostResolverManager() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// Prevent the dispatcher from starting new jobs.
dispatcher_->SetLimitsToZero();
// It's now safe for Jobs to call KillDnsTask on destruction, because
// OnJobComplete will not start any new jobs.
jobs_.clear();
if (target_network_ == handles::kInvalidNetworkHandle) {
NetworkChangeNotifier::RemoveIPAddressObserver(this);
NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
}
if (system_dns_config_notifier_)
system_dns_config_notifier_->RemoveObserver(this);
}
// static
std::unique_ptr<HostResolverManager>
HostResolverManager::CreateNetworkBoundHostResolverManager(
const HostResolver::ManagerOptions& options,
handles::NetworkHandle target_network,
NetLog* net_log) {
#if BUILDFLAG(IS_ANDROID)
DCHECK(NetworkChangeNotifier::AreNetworkHandlesSupported());
return std::make_unique<HostResolverManager>(
PassKey(), options, nullptr /* system_dns_config_notifier */,
target_network, net_log);
#else // !BUILDFLAG(IS_ANDROID)
NOTIMPLEMENTED();
return nullptr;
#endif // BUILDFLAG(IS_ANDROID)
}
std::unique_ptr<HostResolver::ResolveHostRequest>
HostResolverManager::CreateRequest(
absl::variant<url::SchemeHostPort, HostPortPair> host,
NetworkAnonymizationKey network_anonymization_key,
NetLogWithSource net_log,
absl::optional<ResolveHostParameters> optional_parameters,
ResolveContext* resolve_context,
HostCache* host_cache) {
return CreateRequest(HostResolver::Host(std::move(host)),
std::move(network_anonymization_key), std::move(net_log),
std::move(optional_parameters), resolve_context,
host_cache);
}
std::unique_ptr<HostResolver::ResolveHostRequest>
HostResolverManager::CreateRequest(
HostResolver::Host host,
NetworkAnonymizationKey network_anonymization_key,
NetLogWithSource net_log,
absl::optional<ResolveHostParameters> optional_parameters,
ResolveContext* resolve_context,
HostCache* host_cache) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!invalidation_in_progress_);
DCHECK_EQ(resolve_context->GetTargetNetwork(), target_network_);
// ResolveContexts must register (via RegisterResolveContext()) before use to
// ensure cached data is invalidated on network and configuration changes.
DCHECK(registered_contexts_.HasObserver(resolve_context));
return std::make_unique<RequestImpl>(
std::move(net_log), std::move(host), std::move(network_anonymization_key),
std::move(optional_parameters), resolve_context->GetWeakPtr(), host_cache,
weak_ptr_factory_.GetWeakPtr(), tick_clock_);
}
std::unique_ptr<HostResolver::ProbeRequest>
HostResolverManager::CreateDohProbeRequest(ResolveContext* context) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return std::make_unique<ProbeRequestImpl>(context->GetWeakPtr(),
weak_ptr_factory_.GetWeakPtr());
}
std::unique_ptr<HostResolver::MdnsListener>
HostResolverManager::CreateMdnsListener(const HostPortPair& host,
DnsQueryType query_type) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK_NE(DnsQueryType::UNSPECIFIED, query_type);
auto listener =
std::make_unique<HostResolverMdnsListenerImpl>(host, query_type);
MDnsClient* client;
int rv = GetOrCreateMdnsClient(&client);
if (rv == OK) {
std::unique_ptr<net::MDnsListener> inner_listener = client->CreateListener(
DnsQueryTypeToQtype(query_type), host.host(), listener.get());
listener->set_inner_listener(std::move(inner_listener));
} else {
listener->set_initialization_error(rv);
}
return listener;
}
void HostResolverManager::SetInsecureDnsClientEnabled(
bool enabled,
bool additional_dns_types_enabled) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!dns_client_)
return;
bool enabled_before = dns_client_->CanUseInsecureDnsTransactions();
bool additional_types_before =
enabled_before && dns_client_->CanQueryAdditionalTypesViaInsecureDns();
dns_client_->SetInsecureEnabled(enabled, additional_dns_types_enabled);
// Abort current tasks if `CanUseInsecureDnsTransactions()` changes or if
// insecure transactions are enabled and
// `CanQueryAdditionalTypesViaInsecureDns()` changes. Changes to allowing
// additional types don't matter if insecure transactions are completely
// disabled.
if (dns_client_->CanUseInsecureDnsTransactions() != enabled_before ||
(dns_client_->CanUseInsecureDnsTransactions() &&
dns_client_->CanQueryAdditionalTypesViaInsecureDns() !=
additional_types_before)) {
AbortInsecureDnsTasks(ERR_NETWORK_CHANGED, false /* fallback_only */);
}
}
base::Value::Dict HostResolverManager::GetDnsConfigAsValue() const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return dns_client_ ? dns_client_->GetDnsConfigAsValueForNetLog()
: base::Value::Dict();
}
void HostResolverManager::SetDnsConfigOverrides(DnsConfigOverrides overrides) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!dns_client_ && overrides == DnsConfigOverrides())
return;
// Not allowed to set overrides if compiled without DnsClient.
DCHECK(dns_client_);
bool transactions_allowed_before =
dns_client_->CanUseSecureDnsTransactions() ||
dns_client_->CanUseInsecureDnsTransactions();
bool changed = dns_client_->SetConfigOverrides(std::move(overrides));
if (changed) {
NetworkChangeNotifier::TriggerNonSystemDnsChange();
// Only invalidate cache if new overrides have resulted in a config change.
InvalidateCaches();
// Need to update jobs iff transactions were previously allowed because
// in-progress jobs may be running using a now-invalid configuration.
if (transactions_allowed_before) {
UpdateJobsForChangedConfig();
}
}
}
void HostResolverManager::RegisterResolveContext(ResolveContext* context) {
registered_contexts_.AddObserver(context);
context->InvalidateCachesAndPerSessionData(
dns_client_ ? dns_client_->GetCurrentSession() : nullptr,
false /* network_change */);
}
void HostResolverManager::DeregisterResolveContext(
const ResolveContext* context) {
registered_contexts_.RemoveObserver(context);
// Destroy Jobs when their context is closed.
RemoveAllJobs(context);
}
void HostResolverManager::SetTickClockForTesting(
const base::TickClock* tick_clock) {
tick_clock_ = tick_clock;
}
void HostResolverManager::SetIPv6ReachabilityOverride(
bool reachability_override) {
ipv6_reachability_override_ = reachability_override;
}
void HostResolverManager::SetMaxQueuedJobsForTesting(size_t value) {
DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
DCHECK_GE(value, 0u);
max_queued_jobs_ = value;
}
void HostResolverManager::SetHaveOnlyLoopbackAddresses(bool result) {
if (result) {
additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
} else {
additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
}
}
void HostResolverManager::SetMdnsSocketFactoryForTesting(
std::unique_ptr<MDnsSocketFactory> socket_factory) {
DCHECK(!mdns_client_);
mdns_socket_factory_ = std::move(socket_factory);
}
void HostResolverManager::SetMdnsClientForTesting(
std::unique_ptr<MDnsClient> client) {
mdns_client_ = std::move(client);
}
void HostResolverManager::SetDnsClientForTesting(
std::unique_ptr<DnsClient> dns_client) {
DCHECK(dns_client);
if (dns_client_) {
if (!dns_client->GetSystemConfigForTesting())
dns_client->SetSystemConfig(dns_client_->GetSystemConfigForTesting());
dns_client->SetConfigOverrides(dns_client_->GetConfigOverridesForTesting());
}
dns_client_ = std::move(dns_client);
// Inform `registered_contexts_` of the new `DnsClient`.
InvalidateCaches();
}
void HostResolverManager::SetLastIPv6ProbeResultForTesting(
bool last_ipv6_probe_result) {
SetLastIPv6ProbeResult(last_ipv6_probe_result);
}
// static
bool HostResolverManager::IsLocalTask(TaskType task) {
switch (task) {
case TaskType::SECURE_CACHE_LOOKUP:
case TaskType::INSECURE_CACHE_LOOKUP:
case TaskType::CACHE_LOOKUP:
case TaskType::CONFIG_PRESET:
case TaskType::HOSTS:
return true;
default:
return false;
}
}
HostCache::Entry HostResolverManager::ResolveLocally(
bool only_ipv6_reachable,
const JobKey& job_key,
const IPAddress& ip_address,
ResolveHostParameters::CacheUsage cache_usage,
SecureDnsPolicy secure_dns_policy,
HostResolverSource source,
const NetLogWithSource& source_net_log,
HostCache* cache,
std::deque<TaskType>* out_tasks,
absl::optional<HostCache::EntryStaleness>* out_stale_info) {
DCHECK(out_stale_info);
*out_stale_info = absl::nullopt;
CreateTaskSequence(job_key, cache_usage, secure_dns_policy, out_tasks);
if (!ip_address.IsValid()) {
// Check that the caller supplied a valid hostname to resolve. For
// MULTICAST_DNS, we are less restrictive.
// TODO(ericorth): Control validation based on an explicit flag rather
// than implicitly based on |source|.
const bool is_valid_hostname =
job_key.source == HostResolverSource::MULTICAST_DNS
? dns_names_util::IsValidDnsName(GetHostname(job_key.host))
: IsCanonicalizedHostCompliant(GetHostname(job_key.host));
if (!is_valid_hostname) {
return HostCache::Entry(ERR_NAME_NOT_RESOLVED,
HostCache::Entry::SOURCE_UNKNOWN);
}
}
bool resolve_canonname = job_key.flags & HOST_RESOLVER_CANONNAME;
bool default_family_due_to_no_ipv6 =
job_key.flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
// The result of |getaddrinfo| for empty hosts is inconsistent across systems.
// On Windows it gives the default interface's address, whereas on Linux it
// gives an error. We will make it fail on all platforms for consistency.
if (GetHostname(job_key.host).empty() ||
GetHostname(job_key.host).size() > kMaxHostLength) {
return HostCache::Entry(ERR_NAME_NOT_RESOLVED,
HostCache::Entry::SOURCE_UNKNOWN);
}
if (ip_address.IsValid()) {
// Use NAT64Task for IPv4 literal when the network is IPv6 only.
if (MayUseNAT64ForIPv4Literal(job_key.flags, source, ip_address) &&
only_ipv6_reachable) {
out_tasks->push_front(TaskType::NAT64);
return HostCache::Entry(ERR_DNS_CACHE_MISS,
HostCache::Entry::SOURCE_UNKNOWN);
}
return ResolveAsIP(job_key.query_types, resolve_canonname, ip_address);
}
// Special-case localhost names, as per the recommendations in
// https://tools.ietf.org/html/draft-west-let-localhost-be-localhost.
absl::optional<HostCache::Entry> resolved =
ServeLocalhost(GetHostname(job_key.host), job_key.query_types,
default_family_due_to_no_ipv6);
if (resolved)
return resolved.value();
// Do initial cache lookups.
while (!out_tasks->empty() && IsLocalTask(out_tasks->front())) {
TaskType task = out_tasks->front();
out_tasks->pop_front();
if (task == TaskType::SECURE_CACHE_LOOKUP ||
task == TaskType::INSECURE_CACHE_LOOKUP ||
task == TaskType::CACHE_LOOKUP) {
bool secure = task == TaskType::SECURE_CACHE_LOOKUP;
HostCache::Key key = job_key.ToCacheKey(secure);
bool ignore_secure = task == TaskType::CACHE_LOOKUP;
resolved = MaybeServeFromCache(cache, key, cache_usage, ignore_secure,
source_net_log, out_stale_info);
if (resolved) {
// |MaybeServeFromCache()| will update |*out_stale_info| as needed.
DCHECK(out_stale_info->has_value());
source_net_log.AddEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_CACHE_HIT,
[&] { return NetLogResults(resolved.value()); });
// TODO(crbug.com/1200908): Call StartBootstrapFollowup() if the Secure
// DNS Policy is kBootstrap and the result is not secure. Note: A naive
// implementation could cause an infinite loop if |resolved| always
// expires or is evicted before the followup runs.
return resolved.value();
}
DCHECK(!out_stale_info->has_value());
} else if (task == TaskType::CONFIG_PRESET) {
resolved = MaybeReadFromConfig(job_key);
if (resolved) {
source_net_log.AddEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_CONFIG_PRESET_MATCH,
[&] { return NetLogResults(resolved.value()); });
StartBootstrapFollowup(job_key, cache, source_net_log);
return resolved.value();
}
} else if (task == TaskType::HOSTS) {
resolved = ServeFromHosts(GetHostname(job_key.host), job_key.query_types,
default_family_due_to_no_ipv6, *out_tasks);
if (resolved) {
source_net_log.AddEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_HOSTS_HIT,
[&] { return NetLogResults(resolved.value()); });
return resolved.value();
}
} else {
NOTREACHED();
}
}
return HostCache::Entry(ERR_DNS_CACHE_MISS, HostCache::Entry::SOURCE_UNKNOWN);
}
void HostResolverManager::CreateAndStartJob(JobKey key,
std::deque<TaskType> tasks,
RequestImpl* request) {
DCHECK(!tasks.empty());
auto jobit = jobs_.find(key);
Job* job;
if (jobit == jobs_.end()) {
job = AddJobWithoutRequest(key, request->parameters().cache_usage,
request->host_cache(), std::move(tasks),
request->priority(), request->source_net_log());
job->AddRequest(request);
job->RunNextTask();
} else {
job = jobit->second.get();
job->AddRequest(request);
}
}
HostResolverManager::Job* HostResolverManager::AddJobWithoutRequest(
JobKey key,
ResolveHostParameters::CacheUsage cache_usage,
HostCache* host_cache,
std::deque<TaskType> tasks,
RequestPriority priority,
const NetLogWithSource& source_net_log) {
auto new_job =
std::make_unique<Job>(weak_ptr_factory_.GetWeakPtr(), key, cache_usage,
host_cache, std::move(tasks), priority,
source_net_log, tick_clock_, https_svcb_options_);
auto insert_result = jobs_.emplace(std::move(key), std::move(new_job));
auto& iterator = insert_result.first;
bool is_new = insert_result.second;
DCHECK(is_new);
auto& job = iterator->second;
job->OnAddedToJobMap(iterator);
return job.get();
}
HostCache::Entry HostResolverManager::ResolveAsIP(DnsQueryTypeSet query_types,
bool resolve_canonname,
const IPAddress& ip_address) {
DCHECK(ip_address.IsValid());
DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
// IP literals cannot resolve unless the query type is an address query that
// allows addresses with the same address family as the literal. E.g., don't
// return IPv6 addresses for IPv4 queries or anything for a non-address query.
AddressFamily family = GetAddressFamily(ip_address);
if (!query_types.Has(AddressFamilyToDnsQueryType(family))) {
return HostCache::Entry(ERR_NAME_NOT_RESOLVED,
HostCache::Entry::SOURCE_UNKNOWN);
}
std::set<std::string> aliases;
if (resolve_canonname) {
aliases = {ip_address.ToString()};
}
return HostCache::Entry(OK, {IPEndPoint(ip_address, 0)}, std::move(aliases),
HostCache::Entry::SOURCE_UNKNOWN);
}
absl::optional<HostCache::Entry> HostResolverManager::MaybeServeFromCache(
HostCache* cache,
const HostCache::Key& key,
ResolveHostParameters::CacheUsage cache_usage,
bool ignore_secure,
const NetLogWithSource& source_net_log,
absl::optional<HostCache::EntryStaleness>* out_stale_info) {
DCHECK(out_stale_info);
*out_stale_info = absl::nullopt;
if (!cache)
return absl::nullopt;
if (cache_usage == ResolveHostParameters::CacheUsage::DISALLOWED)
return absl::nullopt;
// Local-only requests search the cache for non-local-only results.
HostCache::Key effective_key = key;
if (effective_key.host_resolver_source == HostResolverSource::LOCAL_ONLY)
effective_key.host_resolver_source = HostResolverSource::ANY;
const std::pair<const HostCache::Key, HostCache::Entry>* cache_result;
HostCache::EntryStaleness staleness;
if (cache_usage == ResolveHostParameters::CacheUsage::STALE_ALLOWED) {
cache_result = cache->LookupStale(effective_key, tick_clock_->NowTicks(),
&staleness, ignore_secure);
} else {
DCHECK(cache_usage == ResolveHostParameters::CacheUsage::ALLOWED);
cache_result =
cache->Lookup(effective_key, tick_clock_->NowTicks(), ignore_secure);
staleness = HostCache::kNotStale;
}
if (cache_result) {
*out_stale_info = std::move(staleness);
source_net_log.AddEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_CACHE_HIT,
[&] { return NetLogResults(cache_result->second); });
return cache_result->second;
}
return absl::nullopt;
}
absl::optional<HostCache::Entry> HostResolverManager::MaybeReadFromConfig(
const JobKey& key) {
DCHECK(HasAddressType(key.query_types));
if (!absl::holds_alternative<url::SchemeHostPort>(key.host))
return absl::nullopt;
absl::optional<std::vector<IPEndPoint>> preset_addrs =
dns_client_->GetPresetAddrs(absl::get<url::SchemeHostPort>(key.host));
if (!preset_addrs)
return absl::nullopt;
std::vector<IPEndPoint> filtered_addresses =
FilterAddresses(std::move(*preset_addrs), key.query_types);
if (filtered_addresses.empty())
return absl::nullopt;
return HostCache::Entry(OK, std::move(filtered_addresses), /*aliases=*/{},
HostCache::Entry::SOURCE_CONFIG);
}
void HostResolverManager::StartBootstrapFollowup(
JobKey key,
HostCache* host_cache,
const NetLogWithSource& source_net_log) {
DCHECK_EQ(SecureDnsMode::kOff, key.secure_dns_mode);
DCHECK(host_cache);
key.secure_dns_mode = SecureDnsMode::kSecure;
if (jobs_.count(key) != 0)
return;
Job* job = AddJobWithoutRequest(
key, ResolveHostParameters::CacheUsage::ALLOWED, host_cache,
{TaskType::SECURE_DNS}, RequestPriority::LOW, source_net_log);
job->RunNextTask();
}
absl::optional<HostCache::Entry> HostResolverManager::ServeFromHosts(
base::StringPiece hostname,
DnsQueryTypeSet query_types,
bool default_family_due_to_no_ipv6,
const std::deque<TaskType>& tasks) {
DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
// Don't attempt a HOSTS lookup if there is no DnsConfig or the HOSTS lookup
// is going to be done next as part of a system lookup.
if (!dns_client_ || !HasAddressType(query_types) ||
(!tasks.empty() && tasks.front() == TaskType::SYSTEM))
return absl::nullopt;
const DnsHosts* hosts = dns_client_->GetHosts();
if (!hosts || hosts->empty())
return absl::nullopt;
// HOSTS lookups are case-insensitive.
std::string effective_hostname = base::ToLowerASCII(hostname);
// If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
// (glibc and c-ares) return the first matching line. We have more
// flexibility, but lose implicit ordering.
// We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
// necessary.
std::vector<IPEndPoint> addresses;
if (query_types.Has(DnsQueryType::AAAA)) {
auto it = hosts->find(DnsHostsKey(effective_hostname, ADDRESS_FAMILY_IPV6));
if (it != hosts->end()) {
addresses.emplace_back(it->second, 0);
}
}
if (query_types.Has(DnsQueryType::A)) {
auto it = hosts->find(DnsHostsKey(effective_hostname, ADDRESS_FAMILY_IPV4));
if (it != hosts->end()) {
addresses.emplace_back(it->second, 0);
}
}
// If got only loopback addresses and the family was restricted, resolve
// again, without restrictions. See SystemHostResolverCall for rationale.
if (default_family_due_to_no_ipv6 &&
base::ranges::all_of(addresses, &IPAddress::IsIPv4,
&IPEndPoint::address) &&
base::ranges::all_of(addresses, &IPAddress::IsLoopback,
&IPEndPoint::address)) {
query_types.Put(DnsQueryType::AAAA);
return ServeFromHosts(hostname, query_types, false, tasks);
}
if (addresses.empty())
return absl::nullopt;
return HostCache::Entry(OK, std::move(addresses),
/*aliases=*/{}, HostCache::Entry::SOURCE_HOSTS);
}
absl::optional<HostCache::Entry> HostResolverManager::ServeLocalhost(
base::StringPiece hostname,
DnsQueryTypeSet query_types,
bool default_family_due_to_no_ipv6) {
DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
std::vector<IPEndPoint> resolved_addresses;
if (!HasAddressType(query_types) ||
!ResolveLocalHostname(hostname, &resolved_addresses)) {
return absl::nullopt;
}
if (default_family_due_to_no_ipv6 && query_types.Has(DnsQueryType::A) &&
!query_types.Has(DnsQueryType::AAAA)) {
// The caller disabled the AAAA query due to lack of detected IPv6 support.
// (See SystemHostResolverCall for rationale).
query_types.Put(DnsQueryType::AAAA);
}
std::vector<IPEndPoint> filtered_addresses =
FilterAddresses(std::move(resolved_addresses), query_types);
return HostCache::Entry(OK, std::move(filtered_addresses), /*aliases=*/{},
HostCache::Entry::SOURCE_UNKNOWN);
}
void HostResolverManager::CacheResult(HostCache* cache,
const HostCache::Key& key,
const HostCache::Entry& entry,
base::TimeDelta ttl) {
// Don't cache an error unless it has a positive TTL.
if (cache && (entry.error() == OK || ttl.is_positive()))
cache->Set(key, entry, tick_clock_->NowTicks(), ttl);
}
std::unique_ptr<HostResolverManager::Job> HostResolverManager::RemoveJob(
JobMap::iterator job_it) {
DCHECK(job_it != jobs_.end());
DCHECK(job_it->second);
DCHECK_EQ(1u, jobs_.count(job_it->first));
std::unique_ptr<Job> job;
job_it->second.swap(job);
jobs_.erase(job_it);
job->OnRemovedFromJobMap();
return job;
}
SecureDnsMode HostResolverManager::GetEffectiveSecureDnsMode(
SecureDnsPolicy secure_dns_policy) {
// Use switch() instead of if() to ensure that all policies are handled.
switch (secure_dns_policy) {
case SecureDnsPolicy::kDisable:
case SecureDnsPolicy::kBootstrap:
return SecureDnsMode::kOff;
case SecureDnsPolicy::kAllow:
break;
}
const DnsConfig* config =
dns_client_ ? dns_client_->GetEffectiveConfig() : nullptr;
SecureDnsMode secure_dns_mode = SecureDnsMode::kOff;
if (config) {
secure_dns_mode = config->secure_dns_mode;
}
return secure_dns_mode;
}
bool HostResolverManager::ShouldForceSystemResolverDueToTestOverride() const {
// If tests have provided a catch-all DNS block and then disabled it, check
// that we are not at risk of sending queries beyond the local network.
if (HostResolverProc::GetDefault() && system_resolver_disabled_for_testing_) {
DCHECK(dns_client_);
DCHECK(dns_client_->GetEffectiveConfig());
DCHECK(base::ranges::none_of(dns_client_->GetEffectiveConfig()->nameservers,
&IPAddress::IsPubliclyRoutable,
&IPEndPoint::address))
<< "Test could query a publicly-routable address.";
}
return !host_resolver_system_params_.resolver_proc &&
HostResolverProc::GetDefault() &&
!system_resolver_disabled_for_testing_;
}
void HostResolverManager::PushDnsTasks(bool system_task_allowed,
SecureDnsMode secure_dns_mode,
bool insecure_tasks_allowed,
bool allow_cache,
bool prioritize_local_lookups,
ResolveContext* resolve_context,
std::deque<TaskType>* out_tasks) {
DCHECK(dns_client_);
DCHECK(dns_client_->GetEffectiveConfig());
// If a catch-all DNS block has been set for unit tests, we shouldn't send
// DnsTasks. It is still necessary to call this method, however, so that the
// correct cache tasks for the secure dns mode are added.
const bool dns_tasks_allowed = !ShouldForceSystemResolverDueToTestOverride();
// Upgrade the insecure DnsTask depending on the secure dns mode.
switch (secure_dns_mode) {
case SecureDnsMode::kSecure:
DCHECK(!allow_cache ||
out_tasks->front() == TaskType::SECURE_CACHE_LOOKUP);
// Policy misconfiguration can put us in secure DNS mode without any DoH
// servers to query. See https://crbug.com/1326526.
if (dns_tasks_allowed && dns_client_->CanUseSecureDnsTransactions())
out_tasks->push_back(TaskType::SECURE_DNS);
break;
case SecureDnsMode::kAutomatic:
DCHECK(!allow_cache || out_tasks->front() == TaskType::CACHE_LOOKUP);
if (dns_client_->FallbackFromSecureTransactionPreferred(
resolve_context)) {
// Don't run a secure DnsTask if there are no available DoH servers.
if (dns_tasks_allowed && insecure_tasks_allowed)
out_tasks->push_back(TaskType::DNS);
} else if (prioritize_local_lookups) {
// If local lookups are prioritized, the cache should be checked for
// both secure and insecure results prior to running a secure DnsTask.
// The task sequence should already contain the appropriate cache task.
if (dns_tasks_allowed) {
out_tasks->push_back(TaskType::SECURE_DNS);
if (insecure_tasks_allowed)
out_tasks->push_back(TaskType::DNS);
}
} else {
if (allow_cache) {
// Remove the initial cache lookup task so that the secure and
// insecure lookups can be separated.
out_tasks->pop_front();
out_tasks->push_back(TaskType::SECURE_CACHE_LOOKUP);
}
if (dns_tasks_allowed)
out_tasks->push_back(TaskType::SECURE_DNS);
if (allow_cache)
out_tasks->push_back(TaskType::INSECURE_CACHE_LOOKUP);
if (dns_tasks_allowed && insecure_tasks_allowed)
out_tasks->push_back(TaskType::DNS);
}
break;
case SecureDnsMode::kOff:
DCHECK(!allow_cache || IsLocalTask(out_tasks->front()));
if (dns_tasks_allowed && insecure_tasks_allowed)
out_tasks->push_back(TaskType::DNS);
break;
default:
NOTREACHED();
break;
}
constexpr TaskType kWantTasks[] = {TaskType::DNS, TaskType::SECURE_DNS};
const bool no_dns_or_secure_tasks =
base::ranges::find_first_of(*out_tasks, kWantTasks) == out_tasks->end();
// The system resolver can be used as a fallback for a non-existent or
// failing DnsTask if allowed by the request parameters.
if (system_task_allowed &&
(no_dns_or_secure_tasks || allow_fallback_to_systemtask_))
out_tasks->push_back(TaskType::SYSTEM);
}
void HostResolverManager::CreateTaskSequence(
const JobKey& job_key,
ResolveHostParameters::CacheUsage cache_usage,
SecureDnsPolicy secure_dns_policy,
std::deque<TaskType>* out_tasks) {
DCHECK(out_tasks->empty());
// A cache lookup should generally be performed first. For jobs involving a
// DnsTask, this task may be replaced.
bool allow_cache =
cache_usage != ResolveHostParameters::CacheUsage::DISALLOWED;
if (secure_dns_policy == SecureDnsPolicy::kBootstrap) {
DCHECK_EQ(SecureDnsMode::kOff, job_key.secure_dns_mode);
if (allow_cache)
out_tasks->push_front(TaskType::INSECURE_CACHE_LOOKUP);
out_tasks->push_front(TaskType::CONFIG_PRESET);
if (allow_cache)
out_tasks->push_front(TaskType::SECURE_CACHE_LOOKUP);
} else if (allow_cache) {
if (job_key.secure_dns_mode == SecureDnsMode::kSecure) {
out_tasks->push_front(TaskType::SECURE_CACHE_LOOKUP);
} else {
out_tasks->push_front(TaskType::CACHE_LOOKUP);
}
}
out_tasks->push_back(TaskType::HOSTS);
// Determine what type of task a future Job should start.
bool prioritize_local_lookups =
cache_usage ==
HostResolver::ResolveHostParameters::CacheUsage::STALE_ALLOWED;
const bool has_address_type = HasAddressType(job_key.query_types);
switch (job_key.source) {
case HostResolverSource::ANY:
// Force address queries with canonname to use HostResolverSystemTask to
// counter poor CNAME support in DnsTask. See https://crbug.com/872665
//
// Otherwise, default to DnsTask (with allowed fallback to
// HostResolverSystemTask for address queries). But if hostname appears to
// be an MDNS name (ends in *.local), go with HostResolverSystemTask for
// address queries and MdnsTask for non- address queries.
if ((job_key.flags & HOST_RESOLVER_CANONNAME) && has_address_type) {
out_tasks->push_back(TaskType::SYSTEM);
} else if (!ResemblesMulticastDNSName(GetHostname(job_key.host))) {
bool system_task_allowed =
has_address_type &&
job_key.secure_dns_mode != SecureDnsMode::kSecure;
if (dns_client_ && dns_client_->GetEffectiveConfig()) {
bool insecure_allowed =
dns_client_->CanUseInsecureDnsTransactions() &&
!dns_client_->FallbackFromInsecureTransactionPreferred() &&
(has_address_type ||
dns_client_->CanQueryAdditionalTypesViaInsecureDns());
PushDnsTasks(system_task_allowed, job_key.secure_dns_mode,
insecure_allowed, allow_cache, prioritize_local_lookups,
&*job_key.resolve_context, out_tasks);
} else if (system_task_allowed) {
out_tasks->push_back(TaskType::SYSTEM);
}
} else if (has_address_type) {
// For *.local address queries, try the system resolver even if the
// secure dns mode is SECURE. Public recursive resolvers aren't expected
// to handle these queries.
out_tasks->push_back(TaskType::SYSTEM);
} else {
out_tasks->push_back(TaskType::MDNS);
}
break;
case HostResolverSource::SYSTEM:
out_tasks->push_back(TaskType::SYSTEM);
break;
case HostResolverSource::DNS:
if (dns_client_ && dns_client_->GetEffectiveConfig()) {
bool insecure_allowed =
dns_client_->CanUseInsecureDnsTransactions() &&
(has_address_type ||
dns_client_->CanQueryAdditionalTypesViaInsecureDns());
PushDnsTasks(false /* system_task_allowed */, job_key.secure_dns_mode,
insecure_allowed, allow_cache, prioritize_local_lookups,
&*job_key.resolve_context, out_tasks);
}
break;
case HostResolverSource::MULTICAST_DNS:
out_tasks->push_back(TaskType::MDNS);
break;
case HostResolverSource::LOCAL_ONLY:
// If no external source allowed, a job should not be created or started
break;
}
// `HOST_RESOLVER_CANONNAME` is only supported through system resolution.
if (job_key.flags & HOST_RESOLVER_CANONNAME) {
DCHECK(base::ranges::find(*out_tasks, TaskType::DNS) == out_tasks->end());
DCHECK(base::ranges::find(*out_tasks, TaskType::MDNS) == out_tasks->end());
}
}
void HostResolverManager::GetEffectiveParametersForRequest(
const absl::variant<url::SchemeHostPort, std::string>& host,
DnsQueryType dns_query_type,
HostResolverFlags flags,
SecureDnsPolicy secure_dns_policy,
bool is_ip,
const NetLogWithSource& net_log,
DnsQueryTypeSet* out_effective_types,
HostResolverFlags* out_effective_flags,
SecureDnsMode* out_effective_secure_dns_mode) {
const SecureDnsMode secure_dns_mode =
GetEffectiveSecureDnsMode(secure_dns_policy);
*out_effective_secure_dns_mode = secure_dns_mode;
*out_effective_flags = flags | additional_resolver_flags_;
if (dns_query_type != DnsQueryType::UNSPECIFIED) {
*out_effective_types = {dns_query_type};
return;
}
DnsQueryTypeSet effective_types = {DnsQueryType::A, DnsQueryType::AAAA};
// Disable AAAA queries when we cannot do anything with the results.
bool use_local_ipv6 = true;
if (dns_client_) {
const DnsConfig* config = dns_client_->GetEffectiveConfig();
if (config)
use_local_ipv6 = config->use_local_ipv6;
}
// When resolving IPv4 literals, there's no need to probe for IPv6. When
// resolving IPv6 literals, there's no benefit to artificially limiting our
// resolution based on a probe. Prior logic ensures that this is an automatic
// query, so the code requesting the resolution should be amenable to
// receiving an IPv6 resolution.
if (!use_local_ipv6 && !is_ip && !last_ipv6_probe_result_ &&
!ipv6_reachability_override_) {
*out_effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
effective_types.Remove(DnsQueryType::AAAA);
}
// Optimistically enable feature-controlled queries. These queries may be
// skipped at a later point.
// `https_svcb_options_.enable` has precedence, so if enabled, ignore any
// other related features.
if (https_svcb_options_.enable) {
static const char* const kSchemesForHttpsQuery[] = {
url::kHttpScheme, url::kHttpsScheme, url::kWsScheme, url::kWssScheme};
if (base::Contains(kSchemesForHttpsQuery, GetScheme(host)))
effective_types.Put(DnsQueryType::HTTPS);
}
*out_effective_types = effective_types;
}
namespace {
bool RequestWillUseWiFi(handles::NetworkHandle network) {
NetworkChangeNotifier::ConnectionType connection_type;
if (network == handles::kInvalidNetworkHandle)
connection_type = NetworkChangeNotifier::GetConnectionType();
else
connection_type = NetworkChangeNotifier::GetNetworkConnectionType(network);
return connection_type == NetworkChangeNotifier::CONNECTION_WIFI;
}
} // namespace
void HostResolverManager::FinishIPv6ReachabilityCheck(
CompletionOnceCallback callback,
int rv) {
SetLastIPv6ProbeResult((rv == OK) ? true : false);
std::move(callback).Run(OK);
if (!ipv6_request_callbacks_.empty()) {
std::vector<CompletionOnceCallback> tmp_request_callbacks;
ipv6_request_callbacks_.swap(tmp_request_callbacks);
for (auto& request_callback : tmp_request_callbacks) {
std::move(request_callback).Run(OK);
}
}
}
int HostResolverManager::StartIPv6ReachabilityCheck(
const NetLogWithSource& net_log,
ClientSocketFactory* client_socket_factory,
CompletionOnceCallback callback) {
// Don't bother checking if the request will use WiFi and IPv6 is assumed to
// not work on WiFi.
if (!check_ipv6_on_wifi_ && RequestWillUseWiFi(target_network_)) {
probing_ipv6_ = false;
last_ipv6_probe_result_ = false;
last_ipv6_probe_time_ = base::TimeTicks();
return OK;
}
if (probing_ipv6_) {
ipv6_request_callbacks_.push_back(std::move(callback));
return ERR_IO_PENDING;
}
// Cache the result for kIPv6ProbePeriodMs (measured from after
// StartGloballyReachableCheck() completes).
int rv = OK;
bool cached = true;
if (last_ipv6_probe_time_.is_null() ||
(tick_clock_->NowTicks() - last_ipv6_probe_time_).InMilliseconds() >
kIPv6ProbePeriodMs) {
probing_ipv6_ = true;
rv = StartGloballyReachableCheck(
IPAddress(kIPv6ProbeAddress), net_log, client_socket_factory,
base::BindOnce(&HostResolverManager::FinishIPv6ReachabilityCheck,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
if (rv != ERR_IO_PENDING) {
SetLastIPv6ProbeResult((rv == OK) ? true : false);
rv = OK;
}
cached = false;
}
net_log.AddEvent(
NetLogEventType::HOST_RESOLVER_MANAGER_IPV6_REACHABILITY_CHECK, [&] {
return NetLogIPv6AvailableParams(last_ipv6_probe_result_, cached);
});
return rv;
}
void HostResolverManager::SetLastIPv6ProbeResult(bool last_ipv6_probe_result) {
probing_ipv6_ = false;
last_ipv6_probe_result_ = last_ipv6_probe_result;
last_ipv6_probe_time_ = tick_clock_->NowTicks();
}
int HostResolverManager::StartGloballyReachableCheck(
const IPAddress& dest,
const NetLogWithSource& net_log,
ClientSocketFactory* client_socket_factory,
CompletionOnceCallback callback) {
std::unique_ptr<DatagramClientSocket> probing_socket =
client_socket_factory->CreateDatagramClientSocket(
DatagramSocket::DEFAULT_BIND, net_log.net_log(), net_log.source());
DatagramClientSocket* probing_socket_ptr = probing_socket.get();
auto refcounted_socket = base::MakeRefCounted<
base::RefCountedData<std::unique_ptr<DatagramClientSocket>>>(
std::move(probing_socket));
int rv = probing_socket_ptr->ConnectAsync(
IPEndPoint(dest, GetPortForGloballyReachableCheck()),
base::BindOnce(&HostResolverManager::RunFinishGloballyReachableCheck,
weak_ptr_factory_.GetWeakPtr(), refcounted_socket,
std::move(callback)));
if (rv != ERR_IO_PENDING) {
rv = FinishGloballyReachableCheck(probing_socket_ptr, rv) ? OK : ERR_FAILED;
}
return rv;
}
bool HostResolverManager::FinishGloballyReachableCheck(
DatagramClientSocket* socket,
int rv) {
if (rv != OK) {
return false;
}
IPEndPoint endpoint;
rv = socket->GetLocalAddress(&endpoint);
if (rv != OK) {
return false;
}
const IPAddress& address = endpoint.address();
if (address.IsLinkLocal()) {
return false;
}
if (address.IsIPv6()) {
const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0};
if (IPAddressStartsWith(address, kTeredoPrefix)) {
return false;
}
}
return true;
}
void HostResolverManager::RunFinishGloballyReachableCheck(
scoped_refptr<base::RefCountedData<std::unique_ptr<DatagramClientSocket>>>
socket,
CompletionOnceCallback callback,
int rv) {
bool is_reachable = FinishGloballyReachableCheck(socket->data.get(), rv);
std::move(callback).Run(is_reachable ? OK : ERR_FAILED);
}
void HostResolverManager::RunLoopbackProbeJob() {
RunHaveOnlyLoopbackAddressesJob(
base::BindOnce(&HostResolverManager::SetHaveOnlyLoopbackAddresses,
weak_ptr_factory_.GetWeakPtr()));
}
void HostResolverManager::RemoveAllJobs(const ResolveContext* context) {
for (auto it = jobs_.begin(); it != jobs_.end();) {
const JobKey& key = it->first;
if (&*key.resolve_context == context) {
RemoveJob(it++);
} else {
++it;
}
}
}
void HostResolverManager::AbortJobsWithoutTargetNetwork(bool in_progress_only) {
// In Abort, a Request callback could spawn new Jobs with matching keys, so
// first collect and remove all running jobs from `jobs_`.
std::vector<std::unique_ptr<Job>> jobs_to_abort;
for (auto it = jobs_.begin(); it != jobs_.end();) {
Job* job = it->second.get();
if (!job->HasTargetNetwork() && (!in_progress_only || job->is_running())) {
jobs_to_abort.push_back(RemoveJob(it++));
} else {
++it;
}
}
// Pause the dispatcher so it won't start any new dispatcher jobs while
// aborting the old ones. This is needed so that it won't start the second
// DnsTransaction for a job in `jobs_to_abort` if the DnsConfig just became
// invalid.
PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
dispatcher_->SetLimits(
PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
// Life check to bail once `this` is deleted.
base::WeakPtr<HostResolverManager> self = weak_ptr_factory_.GetWeakPtr();
// Then Abort them.
for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
jobs_to_abort[i]->Abort();
}
if (self)
dispatcher_->SetLimits(limits);
}
void HostResolverManager::AbortInsecureDnsTasks(int error, bool fallback_only) {
// Aborting jobs potentially modifies |jobs_| and may even delete some jobs.
// Create safe closures of all current jobs.
std::vector<base::OnceClosure> job_abort_closures;
for (auto& job : jobs_) {
job_abort_closures.push_back(
job.second->GetAbortInsecureDnsTaskClosure(error, fallback_only));
}
// Pause the dispatcher so it won't start any new dispatcher jobs while
// aborting the old ones. This is needed so that it won't start the second
// DnsTransaction for a job if the DnsConfig just changed.
PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
dispatcher_->SetLimits(
PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
for (base::OnceClosure& closure : job_abort_closures)
std::move(closure).Run();
dispatcher_->SetLimits(limits);
}
// TODO(crbug.com/995984): Consider removing this and its usage.
void HostResolverManager::TryServingAllJobsFromHosts() {
if (!dns_client_ || !dns_client_->GetEffectiveConfig())
return;
// TODO(szym): Do not do this if nsswitch.conf instructs not to.
// http://crbug.com/117655
// Life check to bail once |this| is deleted.
base::WeakPtr<HostResolverManager> self = weak_ptr_factory_.GetWeakPtr();
for (auto it = jobs_.begin(); self.get() && it != jobs_.end();) {
Job* job = it->second.get();
++it;
// This could remove |job| from |jobs_|, but iterator will remain valid.
job->ServeFromHosts();
}
}
void HostResolverManager::OnIPAddressChanged() {
DCHECK(!IsBoundToNetwork());
last_ipv6_probe_time_ = base::TimeTicks();
// Abandon all ProbeJobs.
probe_weak_ptr_factory_.InvalidateWeakPtrs();
InvalidateCaches();
#if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)) || \
BUILDFLAG(IS_FUCHSIA)
RunLoopbackProbeJob();
#endif
AbortJobsWithoutTargetNetwork(true /* in_progress_only */);
// `this` may be deleted inside AbortJobsWithoutTargetNetwork().
}
void HostResolverManager::OnConnectionTypeChanged(
NetworkChangeNotifier::ConnectionType type) {
DCHECK(!IsBoundToNetwork());
UpdateConnectionType(type);
}
void HostResolverManager::OnSystemDnsConfigChanged(
absl::optional<DnsConfig> config) {
DCHECK(!IsBoundToNetwork());
// If tests have provided a catch-all DNS block and then disabled it, check
// that we are not at risk of sending queries beyond the local network.
if (HostResolverProc::GetDefault() && system_resolver_disabled_for_testing_ &&
config.has_value()) {
DCHECK(base::ranges::none_of(config->nameservers,
&IPAddress::IsPubliclyRoutable,
&IPEndPoint::address))
<< "Test could query a publicly-routable address.";
}
bool changed = false;
bool transactions_allowed_before = false;
if (dns_client_) {
transactions_allowed_before = dns_client_->CanUseSecureDnsTransactions() ||
dns_client_->CanUseInsecureDnsTransactions();
changed = dns_client_->SetSystemConfig(std::move(config));
}
// Always invalidate cache, even if no change is seen.
InvalidateCaches();
if (changed) {
// Need to update jobs iff transactions were previously allowed because
// in-progress jobs may be running using a now-invalid configuration.
if (transactions_allowed_before)
UpdateJobsForChangedConfig();
}
}
void HostResolverManager::UpdateJobsForChangedConfig() {
// Life check to bail once `this` is deleted.
base::WeakPtr<HostResolverManager> self = weak_ptr_factory_.GetWeakPtr();
// Existing jobs that were set up using the nameservers and secure dns mode
// from the original config need to be aborted (does not apply to jobs
// targeting a specific network).
AbortJobsWithoutTargetNetwork(false /* in_progress_only */);
// `this` may be deleted inside AbortJobsWithoutTargetNetwork().
if (self.get())
TryServingAllJobsFromHosts();
}
void HostResolverManager::OnFallbackResolve(int dns_task_error) {
DCHECK(dns_client_);
DCHECK_NE(OK, dns_task_error);
// Nothing to do if DnsTask is already not preferred.
if (dns_client_->FallbackFromInsecureTransactionPreferred())
return;
dns_client_->IncrementInsecureFallbackFailures();
// If DnsClient became not preferred, fallback all fallback-allowed insecure
// DnsTasks to HostResolverSystemTasks.
if (dns_client_->FallbackFromInsecureTransactionPreferred())
AbortInsecureDnsTasks(ERR_FAILED, true /* fallback_only */);
}
int HostResolverManager::GetOrCreateMdnsClient(MDnsClient** out_client) {
#if BUILDFLAG(ENABLE_MDNS)
if (!mdns_client_) {
if (!mdns_socket_factory_)
mdns_socket_factory_ = std::make_unique<MDnsSocketFactoryImpl>(net_log_);
mdns_client_ = MDnsClient::CreateDefault();
}
int rv = OK;
if (!mdns_client_->IsListening())
rv = mdns_client_->StartListening(mdns_socket_factory_.get());
DCHECK_NE(ERR_IO_PENDING, rv);
DCHECK(rv != OK || mdns_client_->IsListening());
if (rv == OK)
*out_client = mdns_client_.get();
return rv;
#else
// Should not request MDNS resoltuion unless MDNS is enabled.
NOTREACHED();
return ERR_UNEXPECTED;
#endif
}
void HostResolverManager::InvalidateCaches(bool network_change) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!invalidation_in_progress_);
#if DCHECK_IS_ON()
base::WeakPtr<HostResolverManager> self_ptr = weak_ptr_factory_.GetWeakPtr();
size_t num_jobs = jobs_.size();
#endif
invalidation_in_progress_ = true;
for (auto& context : registered_contexts_) {
context.InvalidateCachesAndPerSessionData(
dns_client_ ? dns_client_->GetCurrentSession() : nullptr,
network_change);
}
invalidation_in_progress_ = false;
#if DCHECK_IS_ON()
// Sanity checks that invalidation does not have reentrancy issues.
DCHECK(self_ptr);
DCHECK_EQ(num_jobs, jobs_.size());
#endif
}
void HostResolverManager::UpdateConnectionType(
NetworkChangeNotifier::ConnectionType type) {
host_resolver_system_params_.unresponsive_delay =
GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
"DnsUnresponsiveDelayMsByConnectionType",
HostResolverSystemTask::Params::kDnsDefaultUnresponsiveDelay, type);
// Note that NetworkChangeNotifier always sends a CONNECTION_NONE notification
// before non-NONE notifications. This check therefore just ensures each
// connection change notification is handled once and has nothing to do with
// whether the change is to offline or online.
if (type == NetworkChangeNotifier::CONNECTION_NONE && dns_client_) {
dns_client_->ReplaceCurrentSession();
InvalidateCaches(true /* network_change */);
}
}
std::unique_ptr<DnsProbeRunner> HostResolverManager::CreateDohProbeRunner(
ResolveContext* resolve_context) {
DCHECK(resolve_context);
DCHECK(registered_contexts_.HasObserver(resolve_context));
if (!dns_client_ || !dns_client_->CanUseSecureDnsTransactions()) {
return nullptr;
}
return dns_client_->GetTransactionFactory()->CreateDohProbeRunner(
resolve_context);
}
HostResolverManager::RequestImpl::~RequestImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!job_.has_value())
return;
job_.value()->CancelRequest(this);
LogCancelRequest();
}
void HostResolverManager::RequestImpl::ChangeRequestPriority(
RequestPriority priority) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!job_.has_value()) {
priority_ = priority;
return;
}
job_.value()->ChangeRequestPriority(this, priority);
}
const HostResolverManager::JobKey& HostResolverManager::RequestImpl::GetJobKey()
const {
CHECK(job_.has_value());
return job_.value()->key();
}
void HostResolverManager::RequestImpl::OnJobCancelled(const JobKey& job_key) {
CHECK(job_.has_value());
CHECK(job_key == job_.value()->key());
job_.reset();
DCHECK(!complete_);
DCHECK(callback_);
callback_.Reset();
// No results should be set.
DCHECK(!results_);
LogCancelRequest();
}
void HostResolverManager::RequestImpl::OnJobCompleted(
const JobKey& job_key,
int error,
bool is_secure_network_error) {
set_error_info(error, is_secure_network_error);
CHECK(job_.has_value());
CHECK(job_key == job_.value()->key());
job_.reset();
DCHECK(!complete_);
complete_ = true;
LogFinishRequest(error, true /* async_completion */);
DCHECK(callback_);
std::move(callback_).Run(HostResolver::SquashErrorCode(error));
}
} // namespace net
|