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 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
|
/* -*- c -*-
*
* Author: James Brister <brister@vix.com> -- berkeley-unix --
* Start Date: Thu Dec 28 13:34:47 1995
* Project: INN (innfeed)
* File: connection.c
* RCSId: $Id: connection.c,v 1.11 1997/08/25 05:32:31 scrappy Exp $
* Copyright: Copyright (c) 1996 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this
* software for any purpose with or without fee is hereby
* granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE
* CONSORTIUM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET
* SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Description: The implementation of the Connection class.
*
* The Connection object is what manages the NNTP
* protocol. If the remote doesn't do streaming, then the
* standard IHAVE lock-step protcol is performed. In the
* streaming situation we have two cases. One where we must
* send CHECK commands, and the other where we can directly
* send TAKETHIS commands without a prior CHECK.
*
* The Connection object maintains four article queues. The first
* one is where new articles are put if they need to have an
* IHAVE or CHECK command sent for them. The second queue is
* where the articles move from the first after their IHAVE/CHECK
* command is sent, but the reply has not yet been seen. The
* third queue is where articles go after the IHAVE/CHECK reply
* has been seen (and the reply says to send the article). It is
* articles in the third queue that have the TAKETHIS command
* sent, or the body of an IHAVE. The third queue is also where
* new articles go if the connection is running in no-CHECK
* mode. The fourth queue is where the articles move to from the
* third queue after their IHAVE-body or TAKETHIS command has
* been sent. When the response to the IHAVE-body or TAKETHIS is
* received the articles are removed from the fourth queue and
* the Host object controlling this Connection is notified of
* the success or failure of the transfer.
*
* The whole system is event-driven by the EndPoint class and the
* Host via calls to prepareRead() and prepareWrite() and
* prepareSleep().
*
*/
/*
We should probably store the results of gethostbyname in the connection so
we can rotate through the address when one fails for connecting. Perhaps
the gethostbyname should be done in the Host and the connection should just
be given the address to use.
Should we worry about articles being stuck on a queue for ever if the
remote forgets to send a response to a CHECK?
Perhaps instead of killing the connection on some of the more simple
errors, we should perhaps try to flush the input and keep going.
Worry about counter overflow.
Worry about stats gathering when switch to no-check mode.
XXX if issueQUIT() has a problem and the state goes to cxnDeadS this is not
handled properly everywhere yet.
*/
#if ! defined (lint)
static const char *rcsid = "$Id: connection.c,v 1.11 1997/08/25 05:32:31 scrappy Exp $" ;
static void use_rcsid (const char *rid) { /* Never called */
use_rcsid (rcsid) ; use_rcsid (rid) ;
}
#endif
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#if defined (DO_HAVE_UNISTD)
#include <unistd.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#if defined (DO_NEED_TIME)
#include <time.h>
#endif
#include <sys/time.h>
#include <syslog.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#include <sys/file.h>
#include <fcntl.h>
#if defined (__FreeBSD__)
#include <sys/ioctl.h>
#endif
#ifdef XXX_RAWHACK
/* From: Sang-yong Suh <sysuh@kigam.re.kr> */
#if defined (linux) && ! defined (_SYS_IOCTL_H)
#include <sys/ioctl.h>
#endif
#endif /* XXX_RAWHACK */
#include "buffer.h"
#include "connection.h"
#include "endpoint.h"
#include "host.h"
#include "article.h"
#include "msgs.h"
#include "configfile.h"
#if defined (NDEBUG)
#define VALIDATE_CONNECTION(x) ((void) 0)
#else
#define VALIDATE_CONNECTION(x) validateConnection (x)
#endif
extern char **PointersFreedOnExit ;
extern const char *pidFile ;
/*
* Private types.
*/
/* We keep a linked list of articles the connection is trying to transmit */
typedef struct art_holder_s
{
Article article ;
struct art_holder_s *next ;
} *ArtHolder ;
typedef enum {
cxnStartingS, /* the connection's start state. */
cxnWaitingS, /* not connected. Waiting for an article. */
cxnConnectingS, /* in the middle of connecting */
cxnIdleS, /* open and ready to feed, has empty queues */
cxnIdleTimeoutS, /* timed out in the idle state */
cxnFeedingS, /* in the processes of feeding articles */
cxnSleepingS, /* blocked on reestablishment timer */
cxnFlushingS, /* am waiting for queues to drain to bounce connection. */
cxnClosingS, /* have been told to close down permanently when queues drained */
cxnDeadS /* connection is dead. */
} CxnState ;
/* The Connection class */
struct connection_s
{
Host myHost ; /* the host who owns the connection */
EndPoint myEp ; /* the endpoint the connection talks through */
u_int ident ; /* an identifier for syslogging. */
CxnState state ; /* the state the connection is in */
/*
* The Connection maintains 4 queue of articles.
*/
ArtHolder checkHead ; /* head of article list to do CHECK/IHAVE */
ArtHolder checkRespHead ; /* head of list waiting on CHECK/IHAVE
response */
ArtHolder takeHead ; /* head of list of articles to send
TAKETHIS/IHAVE-body */
ArtHolder takeRespHead ; /* list of articles waiting on
TAKETHIS/IHAVE-body response */
u_int articleQTotal ; /* number of articles in all four queues */
ArtHolder missing ; /* head of missing list */
Buffer respBuffer ; /* buffer all responses are read into */
char *ipName ; /* the ip name (possibly quad) of the remote */
u_int maxCheck ; /* the max number of CHECKs to send */
u_short port ; /* the port number to use */
/*
* Timeout values and their callback IDs
*/
/* Timer for max amount of time between receiving articles from the
Host */
u_int articleReceiptTimeout ;
TimeoutId artReceiptTimerId ;
/* Timer for the max amount of time to wait for a response from the
remote */
u_int readTimeout ;
TimeoutId readBlockedTimerId ;
/* Timer for the max amount of time to wait for a any amount of data
to be written to the remote */
u_int writeTimeout ;
TimeoutId writeBlockedTimerId ;
/* Timer for the max number of seconds to keep the network connection
up (long lasting connections give older nntp servers problems). */
u_int flushTimeout ;
TimeoutId flushTimerId ;
/* Timer for the number of seconds to sleep before attempting a
reconnect. */
u_int sleepTimeout ;
TimeoutId sleepTimerId ;
bool loggedNoCr ; /* true if we logged the NOCR_MSG */
bool immedRecon ; /* true if we recon immediately after flushing. */
bool doesStreaming ; /* true if remote will handle streaming */
bool quitWasIssued ; /* true if QUIT command was sent. */
bool needsChecks ; /* true if we issue CHECK commands in
streaming mode (rather than just sending
TAKETHIS commands) */
time_t timeCon ; /* the time the connect happened (including
the MODE STREAM command). */
/*
* STATISTICS
*/
u_int artsTaken ; /* the number of articles INN gave this cxn */
u_int checksIssued ; /* the number of CHECKS/IHAVES we
sent. Note that if we're running in
no-CHECK mode, then we add in the
TAKETHIS commands too */
u_int checksRefused ; /* the number of response 435/438 */
u_int takesRejected ; /* the number of response 437/439 recevied */
u_int takesOkayed ; /* the number of response 235/239 received */
double onThreshold ; /* for no-CHECK mode */
double offThreshold ; /* for no-CHECK mode */
double filterValue ; /* current value of IIR filter */
double lowPassFilter ; /* time constant for IIR filter */
Connection next ; /* for global list. */
};
static Connection gCxnList = NULL ;
static u_int gCxnCount = 0 ;
static u_int max_reconnect_period = MAX_RECON_PER ;
static u_int init_reconnect_period = INIT_RECON_PER ;
#if 0
static bool inited = false ;
#endif
static Buffer dotFirstBuffer ;
static Buffer dotBuffer ;
static Buffer crlfBuffer ;
/***************************************************
*
* Private function declarations.
*
***************************************************/
/* I/O Callbacks */
static void connectionDone (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void getBanner (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void getModeResponse (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void responseIsRead (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void quitWritten (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void ihaveBodyDone (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void commandWriteDone (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void modeCmdIssued (EndPoint e, IoStatus i, Buffer *b, void *d) ;
static void writeProgress (EndPoint e, IoStatus i, Buffer *b, void *d) ;
/* Timer callbacks */
static void responseTimeoutCbk (TimeoutId id, void *data) ;
static void writeTimeoutCbk (TimeoutId id, void *data) ;
static void reopenTimeoutCbk (TimeoutId id, void *data) ;
static void flushCxnCbk (TimeoutId, void *data) ;
static void articleTimeoutCbk (TimeoutId id, void *data) ;
/* Work callbacks */
static void cxnWorkProc (EndPoint ep, void *data) ;
static void cxnSleepOrDie (Connection cxn) ;
/* Response processing. */
static void processResponse205 (Connection cxn, char *response) ;
static void processResponse238 (Connection cxn, char *response) ;
static void processResponse431 (Connection cxn, char *response) ;
static void processResponse438 (Connection cxn, char *response) ;
static void processResponse239 (Connection cxn, char *response) ;
static void processResponse439 (Connection cxn, char *response) ;
static void processResponse235 (Connection cxn, char *response) ;
static void processResponse335 (Connection cxn, char *response) ;
static void processResponse400 (Connection cxn, char *response) ;
static void processResponse435 (Connection cxn, char *response) ;
static void processResponse436 (Connection cxn, char *response) ;
static void processResponse437 (Connection cxn, char *response) ;
static void processResponse480 (Connection cxn, char *response) ;
/* Misc functions */
static void cxnSleep (Connection cxn) ;
static void cxnDead (Connection cxn) ;
static void cxnIdle (Connection cxn) ;
static void noSuchMessageId (Connection cxn, u_int responseCode,
const char *msgid, const char *response) ;
static void abortConnection (Connection cxn) ;
static void resetConnection (Connection cxn) ;
static void deferAllArticles (Connection cxn) ;
static void deferQueuedArticles (Connection cxn) ;
static void doSomeWrites (Connection cxn) ;
static bool issueIHAVE (Connection cxn) ;
static void issueIHAVEBody (Connection cxn) ;
static bool issueStreamingCommands (Connection cxn) ;
static Buffer buildCheckBuffer (Connection cxn) ;
static Buffer *buildTakethisBuffers (Connection cxn, Buffer checkBuffer) ;
static void issueQUIT (Connection cxn) ;
static void initReadBlockedTimeout (Connection cxn) ;
static int prepareWriteWithTimeout (EndPoint endp, Buffer *buffers,
EndpRWCB done, Connection cxn) ;
static void delConnection (Connection cxn) ;
static void incrFilter (Connection cxn) ;
static void decrFilter (Connection cxn) ;
static bool writesNeeded (Connection cxn) ;
static void validateConnection (Connection cxn) ;
static const char *stateToString (CxnState state) ;
static void prepareReopenCbk (Connection cxn) ;
/* Article queue management routines. */
static ArtHolder newArtHolder (Article art) ;
static void delArtHolder (ArtHolder artH) ;
static bool remArtHolder (ArtHolder art, ArtHolder *head, u_int *count) ;
static void appendArtHolder (ArtHolder artH, ArtHolder *head, u_int *count) ;
static ArtHolder artHolderByMsgId (const char *msgid, ArtHolder head) ;
static int fudgeFactor (int initVal) ;
/***************************************************
*
* Public functions implementation.
*
***************************************************/
int cxnConfigLoadCbk (void *data)
{
long iv ;
int rval = 1 ;
FILE *fp = (FILE *) data ;
(void) data ; /* keep lint happy */
if (getInteger (topScope,"max-reconnect-time",&iv,NO_INHERIT))
{
if (iv < 1)
{
rval = 0 ;
logOrPrint (LOG_ERR,fp,LESS_THAN_ONE,"max-reconnect-time",
iv,"global scope",(long) MAX_RECON_PER);
iv = MAX_RECON_PER ;
}
}
else
iv = MAX_RECON_PER ;
max_reconnect_period = (u_int) iv ;
if (getInteger (topScope,"initial-reconnect-time",&iv,NO_INHERIT))
{
if (iv < 1)
{
rval = 0 ;
logOrPrint (LOG_ERR,fp,LESS_THAN_ONE,"initial-reconnect-time",
iv,"global scope",(long)INIT_RECON_PER);
iv = INIT_RECON_PER ;
}
}
else
iv = INIT_RECON_PER ;
init_reconnect_period = (u_int) iv ;
return rval ;
}
/*
* Create a new Connection object and return it. All fields are
* initialized to reasonable values.
*/
Connection newConnection (Host host,
u_int id,
const char *ipname,
u_int articleReceiptTimeout,
u_int portNum,
u_int respTimeout,
u_int flushTimeout,
double lowPassLow,
double lowPassHigh,
double lowPassFilter)
{
Connection cxn ;
bool croak = false ;
if (ipname == NULL)
{
dprintf (1,"NULL ipname in newConnection\n") ;
croak = true ;
}
if (ipname && strlen (ipname) == 0)
{
dprintf (1,"Empty ipname in newConnection\n") ;
croak = true ;
}
if (croak)
return NULL ;
cxn = CALLOC (struct connection_s, 1) ;
ASSERT (cxn != NULL) ;
cxn->myHost = host ;
cxn->myEp = NULL ;
cxn->ident = id ;
cxn->checkHead = NULL ;
cxn->checkRespHead = NULL ;
cxn->takeHead = NULL ;
cxn->takeRespHead = NULL ;
cxn->articleQTotal = 0 ;
cxn->missing = NULL ;
cxn->respBuffer = newBuffer (BUFFER_SIZE) ;
ASSERT (cxn->respBuffer != NULL) ;
cxn->ipName = strdup (ipname) ;
cxn->port = portNum ;
/* Time out the higher numbered connections faster */
cxn->articleReceiptTimeout = articleReceiptTimeout * 10.0 / (10.0 + id) ;
cxn->artReceiptTimerId = 0 ;
cxn->readTimeout = respTimeout ;
cxn->readBlockedTimerId = 0 ;
cxn->writeTimeout = respTimeout ; /* XXX should be a separate value */
cxn->writeBlockedTimerId = 0 ;
cxn->flushTimeout = fudgeFactor (flushTimeout) ;
cxn->flushTimerId = 0 ;
cxn->onThreshold = lowPassHigh * lowPassFilter / 100.0 ;
cxn->offThreshold = lowPassLow * lowPassFilter / 100.0 ;
cxn->lowPassFilter = lowPassFilter;
cxn->sleepTimerId = 0 ;
cxn->sleepTimeout = init_reconnect_period ;
resetConnection (cxn) ;
cxn->next = gCxnList ;
gCxnList = cxn ;
gCxnCount++ ;
cxn->state = cxnStartingS ;
return cxn ;
}
/* Create a new endpoint hooked to a non-blocking socket that is trying to
* connect to the host info stored in the Connection. On fast machines
* connecting locally the connect() may have already succeeded when this
* returns, but typically the connect will still be running and when it
* completes. The Connection will be notified via a write callback setup by
* prepareWrite below. If nothing goes wrong then this will return true
* (even if the connect() has not yet completed). If something fails
* (hostname lookup etc.) then it returns false (and the Connection is left
* in the sleeping state)..
*
* Pre-state Reason cxnConnect called
* --------- ------------------------
* cxnStartingS Connection owner issued call.
* cxnWaitingS side effect of cxnTakeArticle() call
* cxnConnecting side effect of cxnFlush() call
* cxnSleepingS side effect of reopenTimeoutCbk() call.
*/
bool cxnConnect (Connection cxn)
{
struct sockaddr_in cxnAddr ;
int fd, rval ;
struct in_addr *addr = NULL;
const char *peerName = hostPeerName (cxn->myHost) ;
ASSERT (cxn->myEp == NULL) ;
if (!(cxn->state == cxnStartingS ||
cxn->state == cxnWaitingS ||
cxn->state == cxnFlushingS ||
cxn->state == cxnSleepingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return false;
}
if (cxn->state == cxnWaitingS)
ASSERT (cxn->articleQTotal == 1) ;
else
ASSERT (cxn->articleQTotal == 0) ;
cxn->state = cxnConnectingS ;
addr = hostIpAddr (cxn->myHost) ;
if (addr == NULL)
{
cxnSleepOrDie (cxn) ;
return false ;
}
memset (&cxnAddr, 0, sizeof (cxnAddr)) ;
cxnAddr.sin_family = AF_INET ;
cxnAddr.sin_port = htons(cxn->port) ;
cxnAddr.sin_addr.s_addr = addr->s_addr ;
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
syslog (LOG_ERR, SOCKET_CREATE_ERROR, peerName, cxn->ident) ;
dprintf (1,"Can't get a socket: %m\n") ;
cxnSleepOrDie (cxn) ;
return false ;
}
/* set our file descriptor to non-blocking */
#if defined (NBIO_FCNTL)
#if ! defined (FNDELAY)
#define FNDELAY O_NDELAY
#endif
rval = fcntl (fd, F_SETFL, FNDELAY) ;
#else
{
int state = 1 ;
rval = ioctl (fd, FIONBIO, (char *) &state) ;
}
#endif
if (rval < 0)
{
syslog (LOG_ERR, FCNTL_ERROR, peerName, cxn->ident) ;
close (fd) ;
cxnSleepOrDie (cxn) ;
return false ;
}
rval = connect (fd, (struct sockaddr *) &cxnAddr, sizeof (cxnAddr)) ;
if (rval < 0 && errno != EINPROGRESS)
{
syslog (LOG_ERR, CONNECT_ERROR, peerName, cxn->ident) ;
close (fd) ;
cxnSleepOrDie (cxn) ;
return false ;
}
if ((cxn->myEp = newEndPoint (fd)) == NULL)
{
/* If this happens, then fd was bigger than what select could handle,
so endpoint.c refused to create the new object. */
close (fd) ;
cxnSleepOrDie (cxn) ;
return false ;
}
if (rval < 0)
/* when the write callback gets done the connection went through */
prepareWrite (cxn->myEp, NULL, NULL, connectionDone, cxn) ;
else
connectionDone (cxn->myEp, IoDone, NULL, cxn) ;
/* connectionDone() could set state to sleeping */
return (cxn->state == cxnConnectingS ? true : false) ;
}
/* Put the Connection into the wait state.
*
* Pre-state Reason cxnWait called
* --------- ------------------------
* cxnStartingS - Connection owner called cxnWait()
* cxnSleepingS - side effect of cxnFlush() call.
* cxnConnectingS - side effect of cxnFlush() call.
* cxnFlushingS - side effect of receiving response 205
* and Connection had no articles when
* cxnFlush() was issued.
* - prepareRead failed.
* - I/O failed.
*
*/
void cxnWait (Connection cxn)
{
ASSERT (cxn->state == cxnStartingS ||
cxn->state == cxnSleepingS ||
cxn->state == cxnConnectingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnFlushingS) ;
VALIDATE_CONNECTION (cxn) ;
abortConnection (cxn) ;
cxn->state = cxnWaitingS ;
hostCxnWaiting (cxn->myHost,cxn) ; /* tell our Host we're waiting */
}
/* Tells the Connection to flush itself (i.e. push out all articles,
* issue a QUIT and drop the network connection. If necessary a
* reconnect will be done immediately after. Called by the Host, or
* by the timer callback.
*
* Pre-state Reason cxnFlush called
* --------- ------------------------
* ALL (except cxnDeadS - Connection owner called cxnFlush()
* and cxnStartingS)
* cxnFeedingS - side effect of flushCxnCbk() call.
*/
void cxnFlush (Connection cxn)
{
ASSERT (cxn != NULL) ;
ASSERT (cxn->state != cxnStartingS) ;
ASSERT (cxn->state != cxnDeadS) ;
VALIDATE_CONNECTION (cxn) ;
switch (cxn->state)
{
case cxnSleepingS:
cxnWait (cxn) ;
break ;
case cxnConnectingS:
cxnWait (cxn) ;
cxnConnect (cxn) ;
break ;
case cxnIdleTimeoutS:
case cxnIdleS:
ASSERT (cxn->articleQTotal == 0) ;
if (cxn->state != cxnIdleTimeoutS)
clearTimer (cxn->artReceiptTimerId) ;
clearTimer (cxn->flushTimerId) ;
cxn->state = cxnFlushingS ;
issueQUIT (cxn) ;
break ;
case cxnClosingS:
case cxnFlushingS:
case cxnWaitingS:
if (cxn->articleQTotal == 0 && !writeIsPending (cxn->myEp))
issueQUIT (cxn) ;
break ;
case cxnFeedingS:
/* we only reconnect immediately if we're not idle when cxnFlush()
is called. */
if (!cxn->immedRecon)
{
cxn->immedRecon = (cxn->articleQTotal > 0 ? true : false) ;
dprintf (1,"%s:%d immediate reconnect for a cxnFlush()\n",
hostPeerName (cxn->myHost), cxn->ident) ;
}
clearTimer (cxn->flushTimerId) ;
cxn->state = cxnFlushingS ;
if (cxn->articleQTotal == 0 && !writeIsPending (cxn->myEp))
issueQUIT (cxn) ;
break ;
default:
die ("Bad connection state: %s\n",stateToString (cxn->state)) ;
}
}
/*
* Tells the Connection to dump all articles that are queued and to issue a
* QUIT as quickly as possible. Much like cxnClose, except queued articles
* are not sent, but are given back to the Host.
*/
void cxnTerminate (Connection cxn)
{
ASSERT (cxn != NULL) ;
ASSERT (cxn->state != cxnDeadS) ;
ASSERT (cxn->state != cxnStartingS) ;
VALIDATE_CONNECTION (cxn) ;
switch (cxn->state)
{
case cxnFeedingS:
dprintf (1,"%s:%d Issuing terminate\n",
hostPeerName (cxn->myHost), cxn->ident) ;
clearTimer (cxn->flushTimerId) ;
cxn->state = cxnClosingS ;
deferQueuedArticles (cxn) ;
if (cxn->articleQTotal == 0)
issueQUIT (cxn) ; /* send out the QUIT if we can */
break ;
case cxnIdleTimeoutS:
case cxnIdleS:
ASSERT (cxn->articleQTotal == 0) ;
if (cxn->state != cxnIdleTimeoutS)
clearTimer (cxn->artReceiptTimerId) ;
clearTimer (cxn->flushTimerId) ;
cxn->state = cxnClosingS ;
issueQUIT (cxn) ;
break ;
case cxnFlushingS: /* we are in the middle of a periodic close. */
dprintf (1,"%s:%d Connection already being flushed\n",
hostPeerName (cxn->myHost),cxn->ident);
cxn->state = cxnClosingS ;
if (cxn->articleQTotal == 0)
issueQUIT (cxn) ; /* send out the QUIT if we can */
break ;
case cxnClosingS:
dprintf (1,"%s:%d Connection already closing\n",
hostPeerName (cxn->myHost),cxn->ident) ;
break ;
case cxnWaitingS:
case cxnConnectingS:
case cxnSleepingS:
cxnDead (cxn) ;
break ;
default:
die ("Bad connection state: %s\n",stateToString (cxn->state)) ;
}
VALIDATE_CONNECTION (cxn) ;
if (cxn->state == cxnDeadS)
{
dprintf (1,"%s:%d Deleting connection\n",hostPeerName (cxn->myHost),
cxn->ident) ;
delConnection (cxn) ;
}
}
/* Tells the Connection to do a disconnect and then when it is
* disconnected to delete itself.
*
* Pre-state Reason cxnClose called
* --------- ------------------------
* ALL (except cxnDeadS - Connecton owner called directly.
* and cxnStartingS).
*/
void cxnClose (Connection cxn)
{
ASSERT (cxn != NULL) ;
ASSERT (cxn->state != cxnDeadS) ;
ASSERT (cxn->state != cxnStartingS) ;
VALIDATE_CONNECTION (cxn) ;
switch (cxn->state)
{
case cxnFeedingS:
dprintf (1,"%s:%d Issuing disconnect\n",
hostPeerName (cxn->myHost), cxn->ident) ;
clearTimer (cxn->flushTimerId) ;
cxn->state = cxnClosingS ;
if (cxn->articleQTotal == 0)
issueQUIT (cxn) ; /* send out the QUIT if we can */
break ;
case cxnIdleS:
case cxnIdleTimeoutS:
ASSERT (cxn->articleQTotal == 0) ;
if (cxn->state != cxnIdleTimeoutS)
clearTimer (cxn->artReceiptTimerId) ;
clearTimer (cxn->flushTimerId) ;
cxn->state = cxnClosingS ;
issueQUIT (cxn) ;
break ;
case cxnFlushingS: /* we are in the middle of a periodic close. */
dprintf (1,"%s:%d Connection already being flushed\n",
hostPeerName (cxn->myHost),cxn->ident);
cxn->state = cxnClosingS ;
if (cxn->articleQTotal == 0)
issueQUIT (cxn) ; /* send out the QUIT if we can */
break ;
case cxnClosingS:
dprintf (1,"%s:%d Connection already closing\n",
hostPeerName (cxn->myHost),cxn->ident) ;
break ;
case cxnWaitingS:
case cxnConnectingS:
case cxnSleepingS:
cxnDead (cxn) ;
break ;
default:
die ("Bad connection state: %s\n",stateToString (cxn->state)) ;
}
VALIDATE_CONNECTION (cxn) ;
if (cxn->state == cxnDeadS)
{
dprintf (1,"%s:%d Deleting connection\n",hostPeerName (cxn->myHost),
cxn->ident) ;
delConnection (cxn) ;
}
}
/* This is what the Host calls to get us to tranfer an article. If
* we're running the IHAVE sequence, then we can't take it if we've
* got an article already. If we're running the CHECK/TAKETHIS
* sequence, then we'll take as many as we can (up to our MAXCHECK
* limit).
*/
bool cxnTakeArticle (Connection cxn, Article art)
{
bool rval = true ;
ASSERT (cxn != NULL) ;
VALIDATE_CONNECTION (cxn) ;
if ( !cxnQueueArticle (cxn,art) ) /* might change cxnIdleS to cxnFeedingS */
return false ;
if (!(cxn->state == cxnConnectingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnWaitingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return false ;
}
if (cxn->state != cxnWaitingS) /* because articleQTotal == 1 */
VALIDATE_CONNECTION (cxn) ;
else
ASSERT (cxn->articleQTotal == 1) ;
switch (cxn->state)
{
case cxnWaitingS:
cxnConnect (cxn) ;
break ;
case cxnFeedingS:
doSomeWrites (cxn) ;
break ;
case cxnConnectingS:
break ;
default:
die ("Bad connection state: %s\n",stateToString (cxn->state)) ;
}
return rval ;
}
/* if there's room in the Connection then stick the article on the
* queue, otherwise return false.
*/
bool cxnQueueArticle (Connection cxn, Article art)
{
ArtHolder newArt ;
bool rval = false ;
ASSERT (cxn != NULL) ;
ASSERT (cxn->state != cxnStartingS) ;
ASSERT (cxn->state != cxnDeadS) ;
VALIDATE_CONNECTION (cxn) ;
switch (cxn->state)
{
case cxnClosingS:
dprintf (5,"%s:%d Refusing article due to closing\n",
hostPeerName (cxn->myHost),cxn->ident) ;
break ;
case cxnFlushingS:
dprintf (5,"%s:%d Refusing article due to flushing\n",
hostPeerName (cxn->myHost),cxn->ident) ;
break ;
case cxnSleepingS:
dprintf (5,"%s:%d Refusing article due to sleeping\n",
hostPeerName (cxn->myHost),cxn->ident) ;
break ;
case cxnWaitingS:
rval = true ;
newArt = newArtHolder (art) ;
appendArtHolder (newArt, &cxn->checkHead, &cxn->articleQTotal) ;
break ;
case cxnConnectingS:
if (cxn->articleQTotal != 0)
break ;
rval = true ;
newArt = newArtHolder (art) ;
appendArtHolder (newArt, &cxn->checkHead, &cxn->articleQTotal) ;
break ;
case cxnIdleS:
case cxnFeedingS:
if (cxn->articleQTotal >= cxn->maxCheck)
dprintf (5, "%s:%d Refusing article due to articleQTotal >= maxCheck (%d > %d)\n",
hostPeerName (cxn->myHost), cxn->ident,
cxn->articleQTotal, cxn->maxCheck) ;
else
{
rval = true ;
newArt = newArtHolder (art) ;
if (cxn->needsChecks)
appendArtHolder (newArt, &cxn->checkHead, &cxn->articleQTotal) ;
else
appendArtHolder (newArt, &cxn->takeHead, &cxn->articleQTotal) ;
if (cxn->state == cxnIdleS)
{
cxn->state = cxnFeedingS ;
clearTimer (cxn->artReceiptTimerId) ;
}
}
break ;
default:
die ("Invalid state: %s\n", stateToString (cxn->state)) ;
}
if (rval)
{
dprintf (5,"%s:%d accepting article %s\n",hostPeerName (cxn->myHost),
cxn->ident,artMsgId (art)) ;
cxn->artsTaken++ ;
}
return rval ;
}
/*
* generate a log message for activity. Usually called by the Connection's
* owner
*/
void cxnLogStats (Connection cxn, bool final)
{
const char *peerName ;
time_t now = theTime() ;
ASSERT (cxn != NULL) ;
/* only log stats when in one of these three states. */
switch (cxn->state)
{
case cxnFeedingS:
case cxnFlushingS:
case cxnClosingS:
break ;
default:
return ;
}
peerName = hostPeerName (cxn->myHost) ;
syslog (LOG_NOTICE,STATS_MSG, peerName, cxn->ident,
(final ? "final" : "checkpoint"), (long) (now - cxn->timeCon),
cxn->checksIssued, cxn->takesOkayed, cxn->checksRefused,
cxn->takesRejected) ;
if (final)
{
cxn->artsTaken = 0 ;
cxn->checksIssued = 0 ;
cxn->checksRefused = 0 ;
cxn->takesRejected = 0 ;
cxn->takesOkayed = 0 ;
if (cxn->timeCon > 0)
cxn->timeCon = theTime() ;
}
}
/*
* return the number of articles the connection will accept.
*/
size_t cxnQueueSpace (Connection cxn)
{
int rval = 0 ;
ASSERT (cxn != NULL) ;
if (cxn->state == cxnFeedingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnConnectingS ||
cxn->state == cxnWaitingS)
rval = cxn->maxCheck - cxn->articleQTotal ;
return rval ;
}
/*
* Print info on all the connections that currently exist.
*/
void gPrintCxnInfo (FILE *fp, u_int indentAmt)
{
char indent [INDENT_BUFFER_SIZE] ;
u_int i ;
Connection cxn ;
for (i = 0 ; i < MIN(INDENT_BUFFER_SIZE - 1,indentAmt) ; i++)
indent [i] = ' ' ;
indent [i] = '\0' ;
fprintf (fp,"%sGlobal Connection list : (count %d) {\n",
indent,gCxnCount) ;
for (cxn = gCxnList ; cxn != NULL ; cxn = cxn->next)
printCxnInfo (cxn,fp,indentAmt + INDENT_INCR) ;
fprintf (fp,"%s}\n",indent) ;
}
/*
* Print the info about the given connection.
*/
void printCxnInfo (Connection cxn, FILE *fp, u_int indentAmt)
{
char indent [INDENT_BUFFER_SIZE] ;
u_int i ;
ArtHolder artH ;
for (i = 0 ; i < MIN(INDENT_BUFFER_SIZE - 1,indentAmt) ; i++)
indent [i] = ' ' ;
indent [i] = '\0' ;
fprintf (fp,"%sConnection : %p {\n",indent,cxn) ;
fprintf (fp,"%s host : %p\n",indent, (void *) cxn->myHost) ;
fprintf (fp,"%s endpoint : %p\n",indent,cxn->myEp) ;
fprintf (fp,"%s state : %s\n",indent, stateToString (cxn->state)) ;
fprintf (fp,"%s ident : %d\n",indent,cxn->ident) ;
fprintf (fp,"%s ip-name : %s\n", indent, cxn->ipName) ;
fprintf (fp,"%s port-number : %d\n",indent,cxn->port) ;
fprintf (fp,"%s max-checks : %d\n",indent,cxn->maxCheck) ;
fprintf (fp,"%s does-streaming : %s\n",indent,
boolToString (cxn->doesStreaming)) ;
fprintf (fp,"%s quitWasIssued : %s\n",indent,
boolToString (cxn->quitWasIssued)) ;
fprintf (fp,"%s needs-checks : %s\n",indent,
boolToString (cxn->needsChecks)) ;
fprintf (fp,"%s time-connected : %ld\n",indent,(long) cxn->timeCon) ;
fprintf (fp,"%s articles from INN : %d\n",indent,cxn->artsTaken) ;
fprintf (fp,"%s articles offered : %d\n",indent,
cxn->checksIssued) ;
fprintf (fp,"%s articles refused : %d\n",indent,
cxn->checksRefused) ;
fprintf (fp,"%s articles rejected : %d\n",indent,
cxn->takesRejected) ;
fprintf (fp,"%s articles accepted : %d\n",indent,
cxn->takesOkayed) ;
fprintf (fp,"%s low-pass upper limit : %0.6f\n", indent,
cxn->onThreshold) ;
fprintf (fp,"%s low-pass lower limit : %0.6f\n", indent,
cxn->offThreshold) ;
fprintf (fp,"%s low-pass filter tc : %0.6f\n", indent,
cxn->lowPassFilter) ;
fprintf (fp,"%s low-pass filter : %0.6f\n", indent,
cxn->filterValue) ;
fprintf (fp,"%s article-timeout : %d\n",indent,cxn->articleReceiptTimeout) ;
fprintf (fp,"%s article-callback : %d\n",indent,cxn->artReceiptTimerId) ;
fprintf (fp,"%s response-timeout : %d\n",indent,cxn->readTimeout) ;
fprintf (fp,"%s response-callback : %d\n",indent,cxn->readBlockedTimerId) ;
fprintf (fp,"%s write-timeout : %d\n",indent,cxn->writeTimeout) ;
fprintf (fp,"%s write-callback : %d\n",indent,cxn->writeBlockedTimerId) ;
fprintf (fp,"%s flushTimeout : %d\n",indent,cxn->flushTimeout) ;
fprintf (fp,"%s flushTimerId : %d\n",indent,cxn->flushTimerId) ;
fprintf (fp,"%s reopen wait : %d\n",indent,cxn->sleepTimeout) ;
fprintf (fp,"%s reopen id : %d\n",indent,cxn->sleepTimerId) ;
fprintf (fp,"%s CHECK queue {\n",indent) ;
for (artH = cxn->checkHead ; artH != NULL ; artH = artH->next)
printArticleInfo (artH->article,fp,indentAmt + INDENT_INCR) ;
fprintf (fp,"%s }\n",indent) ;
fprintf (fp,"%s CHECK Response queue {\n",indent) ;
for (artH = cxn->checkRespHead ; artH != NULL ; artH = artH->next)
printArticleInfo (artH->article,fp,indentAmt + INDENT_INCR) ;
fprintf (fp,"%s }\n",indent) ;
fprintf (fp,"%s TAKE queue {\n",indent) ;
for (artH = cxn->takeHead ; artH != NULL ; artH = artH->next)
printArticleInfo (artH->article,fp,indentAmt + INDENT_INCR) ;
fprintf (fp,"%s }\n",indent) ;
fprintf (fp,"%s TAKE response queue {\n",indent) ;
for (artH = cxn->takeRespHead ; artH != NULL ; artH = artH->next)
printArticleInfo (artH->article,fp,indentAmt + INDENT_INCR) ;
fprintf (fp,"%s }\n",indent) ;
fprintf (fp,"%s response buffer {\n",indent) ;
printBufferInfo (cxn->respBuffer,fp,indentAmt + INDENT_INCR) ;
fprintf (fp,"%s }\n",indent) ;
fprintf (fp,"%s}\n",indent) ;
}
/*
* return the number of articles the connection will accept.
*/
bool cxnCheckstate (Connection cxn)
{
bool rval = false ;
ASSERT (cxn != NULL) ;
if (cxn->state == cxnFeedingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnConnectingS)
rval = true ;
return rval ;
}
/**********************************************************************/
/** STATIC PRIVATE FUNCTIONS **/
/**********************************************************************/
/*
* ENDPOINT CALLBACK AREA.
*
* All the functions in this next section are callbacks fired by the
* EndPoint objects/class (either timers or i/o completion callbacks)..
*/
/*
* this is the first stage of the NNTP FSM. This function is called
* when the tcp/ip network connection is setup and we should get
* ready to read the banner message. When this function returns the
* state of the Connection will still be cxnConnectingS unless
* something broken, in which case it probably went into the
* cxnSleepingS state.
*/
static void connectionDone (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Buffer *readBuffers ;
Connection cxn = (Connection) d ;
const char *peerName ;
int optval, size ;
ASSERT (b == NULL) ;
ASSERT (cxn->state == cxnConnectingS) ;
ASSERT (!writeIsPending (cxn->myEp)) ;
size = sizeof (optval) ;
peerName = hostPeerName (cxn->myHost) ;
if (i != IoDone)
{
errno = endPointErrno (e) ;
syslog (LOG_ERR,IO_FAILED,peerName,cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else if (getsockopt (endPointFd (e), SOL_SOCKET, SO_ERROR,
(GETSOCKOPT_ARG) &optval, &size) != 0)
{
/* This is bad. Can't even get the SO_ERROR value out of the socket */
syslog (LOG_ERR,GETSOCKOPT_FAILED, peerName, cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else if (optval != 0)
{
/* if the connect failed then the only way to know is by getting
the SO_ERROR value out of the socket. */
errno = optval ;
syslog (LOG_NOTICE,CONNECTION_FAILURE,peerName,cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else
{
readBuffers = makeBufferArray (bufferTakeRef (cxn->respBuffer), NULL) ;
if ( !prepareRead (e, readBuffers, getBanner, cxn, 1) )
{
syslog (LOG_ERR, PREPARE_READ_FAILED, peerName, cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else
{
initReadBlockedTimeout (cxn) ;
/* set up the callback for closing down the connection at regular
intervals (due to problems with long running nntpd). */
if (cxn->flushTimeout > 0)
cxn->flushTimerId = prepareSleep (flushCxnCbk,
cxn->flushTimeout,cxn) ;
/* The state doesn't change yet until we've read the banner and
tried the MODE STREAM command. */
}
}
VALIDATE_CONNECTION (cxn) ;
}
/*
* Called when the banner message has been read off the wire and is
* in the buffer(s). When this function returns the state of the
* Connection will still be cxnConnectingS unless something broken,
* in which case it probably went into the cxnSleepiongS state.
*/
static void getBanner (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Buffer *modeCmdBuffers, *readBuffers ;
Connection cxn = (Connection) d ;
char *p = bufferBase (b[0]) ;
int code ;
bool isOk = false ;
const char *peerName ;
char *rest ;
ASSERT (e == cxn->myEp) ;
ASSERT (b[0] == cxn->respBuffer) ;
ASSERT (b[1] == NULL) ;
ASSERT (cxn->state == cxnConnectingS) ;
ASSERT (!writeIsPending (cxn->myEp));
peerName = hostPeerName (cxn->myHost) ;
bufferAddNullByte (b[0]) ;
if (i != IoDone)
{
errno = endPointErrno (cxn->myEp) ;
syslog (LOG_ERR, BANNER_READ_FAILED, peerName, cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else if (strchr (p, '\n') == NULL)
{ /* partial read. expand buffer and retry */
expandBuffer (b[0], BUFFER_EXPAND_AMOUNT) ;
readBuffers = makeBufferArray (bufferTakeRef (b[0]), NULL) ;
if ( !prepareRead (e, readBuffers, getBanner, cxn, 1) )
{
syslog (LOG_ERR, PREPARE_READ_FAILED, peerName, cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
}
else if ( !getNntpResponse (p, &code, &rest) )
{
trim_ws (p) ;
syslog (LOG_ERR, INVALID_RESP_FORMAT, peerName, cxn->ident, p) ;
cxnSleepOrDie (cxn) ;
}
else
{
trim_ws (p) ;
switch (code)
{
case 200: /* normal */
case 201: /* can transfer but not post -- old nntpd */
isOk = true ;
break ;
case 400:
cxnSleepOrDie (cxn) ;
hostCxnBlocked (cxn->myHost, cxn, rest) ;
break ;
case 502:
syslog (LOG_NOTICE,NO_TALK_NNRPD,peerName,cxn->ident,p) ;
cxnSleepOrDie (cxn) ;
hostCxnBlocked (cxn->myHost, cxn, rest) ;
break ;
default:
syslog (LOG_NOTICE,UNKNOWN_BANNER, peerName, cxn->ident, code, p) ;
dprintf (1,"%s:%d Unknown response code: %d: %s\n",
hostPeerName (cxn->myHost),cxn->ident, code, p) ;
cxnSleepOrDie (cxn) ;
hostCxnBlocked (cxn->myHost, cxn, rest) ;
break ;
}
if ( isOk )
{
Buffer modeBuffer ;
#define MODE_CMD "MODE STREAM\r\n"
modeBuffer = newBuffer (strlen (MODE_CMD) + 1) ;
p = bufferBase (modeBuffer) ;
/* now issue the MODE STREAM command */
dprintf (1,"%s:%d Issuing the streaming command: %s",
hostPeerName (cxn->myHost),cxn->ident,MODE_CMD) ;
strcpy (p, MODE_CMD) ;
bufferSetDataSize (modeBuffer, strlen (p)) ;
modeCmdBuffers = makeBufferArray (modeBuffer, NULL) ;
if ( !prepareWriteWithTimeout (e, modeCmdBuffers, modeCmdIssued,
cxn) )
{
syslog (LOG_ERR, PREPARE_WRITE_FAILED, peerName, cxn->ident) ;
die ("Prepare write for mode stream failed") ;
}
bufferSetDataSize (cxn->respBuffer, 0) ;
readBuffers = makeBufferArray (bufferTakeRef(cxn->respBuffer),NULL);
if ( !prepareRead (e, readBuffers, getModeResponse, cxn, 1) )
{
syslog (LOG_ERR, PREPARE_READ_FAILED, peerName, cxn->ident) ;
freeBufferArray (readBuffers) ;
cxnSleepOrDie (cxn) ;
}
}
}
freeBufferArray (b) ;
}
/*
* Process the remote's response to our MODE STREAM command. This is where
* the Connection moves into the cxnFeedingS state. If the remote has given
* us a good welcome banner, but then immediately dropped the connection,
* we'll arrive here with the MODE STREAM command still queued up.
*/
static void getModeResponse (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
int code ;
char *p = bufferBase (b[0]) ;
Buffer *buffers ;
const char *peerName ;
ASSERT (e == cxn->myEp) ;
ASSERT (b [0] == cxn->respBuffer) ;
ASSERT (b [1] == NULL) ; /* only ever one buffer on this read */
ASSERT (cxn->state == cxnConnectingS) ;
VALIDATE_CONNECTION (cxn) ;
peerName = hostPeerName (cxn->myHost) ;
bufferAddNullByte (b[0]) ;
dprintf (1,"%s:%d Processing mode response: %s", /* no NL */
hostPeerName (cxn->myHost), cxn->ident, p) ;
if (i == IoDone && writeIsPending (cxn->myEp))
{ /* badness. should never happen */
syslog (LOG_ERR, MODE_WRITE_PENDING, peerName, cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else if (i != IoDone)
{
errno = endPointErrno (e) ;
syslog (LOG_ERR, RESPONSE_READ_FAILED, peerName, cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
else if (strchr (p, '\n') == NULL)
{ /* partial read */
expandBuffer (b [0], BUFFER_EXPAND_AMOUNT) ;
buffers = makeBufferArray (bufferTakeRef (b [0]), NULL) ;
if ( !prepareRead (e, buffers, getModeResponse, cxn, 1) )
{
syslog (LOG_ERR, PREPARE_READ_FAILED, peerName, cxn->ident) ;
freeBufferArray (buffers) ;
cxnSleepOrDie (cxn) ;
}
}
else
{
clearTimer (cxn->readBlockedTimerId) ;
if ( !getNntpResponse (p, &code, NULL) )
{
syslog (LOG_ERR, BAD_MODE_RESPONSE, peerName, cxn->ident, p) ;
cxnSleepOrDie (cxn) ;
}
else
{
syslog (LOG_NOTICE,CONNECTED,peerName, cxn->ident) ;
switch (code)
{
case 203: /* will do streaming */
hostRemoteStreams (cxn->myHost, cxn, true) ;
if (hostWantsStreaming (cxn->myHost))
{
cxn->doesStreaming = true ;
cxn->maxCheck = hostMaxChecks (cxn->myHost) ;
}
else
cxn->maxCheck = 1 ;
break ;
default: /* won't do it */
hostRemoteStreams (cxn->myHost, cxn, false) ;
cxn->maxCheck = 1 ;
break ;
}
/* now we consider ourselves completly connected. */
cxn->timeCon = theTime () ;
if (cxn->articleQTotal == 0)
cxnIdle (cxn) ;
else
cxn->state = cxnFeedingS ;
/* one for the connection and one for the buffer array */
ASSERT (bufferRefCount (cxn->respBuffer) == 2) ;
/* there was only one line in there, right? */
bufferSetDataSize (cxn->respBuffer, 0) ;
buffers = makeBufferArray (bufferTakeRef (cxn->respBuffer), NULL) ;
/* sleepTimeout get changed at each failed attempt, so reset. */
cxn->sleepTimeout = init_reconnect_period ;
if ( !prepareRead (cxn->myEp, buffers, responseIsRead, cxn, 1) )
{
freeBufferArray (buffers) ;
cxnSleepOrDie (cxn) ;
}
else
{
/* now we wait for articles from our Host, or we have some
articles already. On infrequently used connections, the
network link is torn down and rebuilt as needed. So we may
be rebuilding the connection here in which case we have an
article to send. */
if (writesNeeded (cxn) || hostGimmeArticle (cxn->myHost,cxn))
doSomeWrites (cxn) ;
}
}
}
freeBufferArray (b) ;
}
/*
* called when a response has been read from the socket. This is
* where the bulk of the processing starts.
*/
static void responseIsRead (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
char *response ;
char *endr ;
char *bufBase ;
u_int respSize ;
int code ;
char *rest = NULL ;
Buffer buf ;
Buffer *bArr ;
const char *peerName ;
ASSERT (e == cxn->myEp) ;
ASSERT (b != NULL) ;
ASSERT (b [1] == NULL) ;
ASSERT (b [0] == cxn->respBuffer) ;
ASSERT (cxn->state == cxnFeedingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnClosingS ||
cxn->state == cxnFlushingS) ;
VALIDATE_CONNECTION (cxn) ;
bufferAddNullByte (b [0]) ;
peerName = hostPeerName (cxn->myHost) ;
if (i != IoDone)
{ /* uh oh. */
errno = endPointErrno (e) ;
syslog (LOG_ERR, RESPONSE_READ_FAILED, peerName, cxn->ident) ;
freeBufferArray (b) ;
cxnLogStats (cxn,true) ;
if (cxn->state == cxnClosingS)
{
cxnDead (cxn) ;
delConnection (cxn) ;
}
else
cxnSleep (cxn) ;
return ;
}
buf = b [0] ;
bufBase = bufferBase (buf) ;
/* check that we have (at least) a full line response. If not expand
the buffer and resubmit the read. */
if (strchr (bufBase, '\n') == 0)
{
if (!expandBuffer (buf, BUFFER_EXPAND_AMOUNT))
{
syslog (LOG_ERR, CXN_BUFFER_EXPAND_ERROR, peerName, cxn->ident) ;
freeBufferArray (b) ;
cxnSleepOrDie (cxn) ;
}
else if ( !prepareRead (cxn->myEp, b, responseIsRead, cxn, 1))
{
syslog (LOG_ERR, PREPARE_READ_FAILED, peerName, cxn->ident) ;
freeBufferArray (b) ;
cxnSleepOrDie (cxn) ;
}
return ;
}
freeBufferArray (b) ; /* connection still has reference to buffer */
/*
* Now process all the full responses that we have.
*/
response = bufBase ;
respSize = bufferDataSize (cxn->respBuffer) ;
while ((endr = strchr (response, '\n')) != NULL)
{
char *next = endr + 1 ;
if (*next == '\r')
next++ ;
endr-- ;
if (*endr != '\r')
endr++ ;
if (next - endr != 2 && !cxn->loggedNoCr)
{
/* only a newline there. we'll live with it */
syslog (LOG_ERR, NOCR_MSG, peerName, cxn->ident) ;
cxn->loggedNoCr = true ;
}
*endr = '\0' ;
if ( !getNntpResponse (response, &code, &rest) )
{
syslog (LOG_ERR, INVALID_RESP_FORMAT, peerName,
cxn->ident, response) ;
cxnSleepOrDie (cxn) ;
return ;
}
dprintf (5,"%s:%d Response %d: %s\n", peerName, cxn->ident, code, response) ;
/* now handle the response code. I'm not using symbolic names on
purpose--the numbers are all you see in the RFC's. */
switch (code)
{
case 205: /* OK response to QUIT. */
processResponse205 (cxn, response) ;
break ;
/* These three are from the CHECK command */
case 238: /* no such article found */
/* Do not incrFilter (cxn) now, wait till after
subsequent TAKETHIS */
processResponse238 (cxn, response) ;
break ;
case 431: /* try again later (also for TAKETHIS) */
decrFilter (cxn) ;
processResponse431 (cxn, response) ;
break ;
case 438: /* already have it */
decrFilter (cxn) ;
processResponse438 (cxn, response) ;
break ;
/* These are from the TAKETHIS command */
case 239: /* article transferred OK */
incrFilter (cxn) ;
processResponse239 (cxn, response) ;
break ;
case 439: /* article rejected */
decrFilter (cxn) ;
processResponse439 (cxn, response) ;
break ;
/* These are from the IHAVE command */
case 335: /* send article */
processResponse335 (cxn, response) ;
break ;
case 435: /* article not wanted */
processResponse435 (cxn, response) ;
break ;
case 436: /* transfer failed try again later */
processResponse436 (cxn, response) ;
break ;
case 437: /* article rejected */
processResponse437 (cxn, response) ;
break ;
case 400: /* has stopped accepting articles */
processResponse400 (cxn, response) ;
break ;
case 235: /* article transfered OK (IHAVE-body) */
processResponse235 (cxn, response) ;
break ;
case 480: /* Transfer permission denied. */
processResponse480 (cxn,response) ;
break ;
default:
syslog (LOG_ERR, UNKNOWN_RESPONSE, peerName, cxn->ident,
code, response) ;
cxnSleepOrDie (cxn) ;
break ;
}
VALIDATE_CONNECTION (cxn) ;
if (cxn->state != cxnFeedingS && cxn->state != cxnClosingS &&
cxn->state != cxnFlushingS && cxn->state != cxnIdleS /* XXX */)
break ; /* connection is terminated */
response = next ;
}
dprintf (5,"%s:%d done with responses\n",hostPeerName (cxn->myHost),
cxn->ident) ;
switch (cxn->state)
{
case cxnIdleS:
case cxnFeedingS:
case cxnClosingS:
case cxnFlushingS:
/* see if we need to drop in to or out of no-CHECK mode */
if (cxn->state == cxnFeedingS && cxn->doesStreaming)
{
if ((cxn->filterValue > cxn->onThreshold) && cxn->needsChecks)
hostLogNoCheckMode (cxn->myHost, !(cxn->needsChecks = false),
cxn->offThreshold/cxn->lowPassFilter,
cxn->filterValue/cxn->lowPassFilter,
cxn->onThreshold/cxn->lowPassFilter) ;
/* on and log */
else if ((cxn->filterValue < cxn->offThreshold) &&
!cxn->needsChecks)
hostLogNoCheckMode (cxn->myHost, !(cxn->needsChecks = true),
cxn->offThreshold/cxn->lowPassFilter,
cxn->filterValue/cxn->lowPassFilter,
cxn->onThreshold/cxn->lowPassFilter) ;
/* off and log */
}
/* Now handle possible remaining partial reponse and set up for
next read. */
if (*response != '\0')
{ /* partial response */
u_int leftAmt = respSize - (response - bufBase) ;
dprintf (2,"%s:%d handling a partial response\n",
hostPeerName (cxn->myHost),cxn->ident) ;
/* first we shift what's left in the buffer down to the
bottom, if needed, or just expand the buffer */
if (response != bufBase)
{
/* so next read appends */
memcpy (bufBase, response, leftAmt) ;
bufferSetDataSize (cxn->respBuffer, leftAmt) ;
}
else if (!expandBuffer (cxn->respBuffer, BUFFER_EXPAND_AMOUNT))
die (CXN_BUFFER_EXPAND_ERROR,peerName,cxn->ident);
}
else
bufferSetDataSize (cxn->respBuffer, 0) ;
bArr = makeBufferArray (bufferTakeRef (cxn->respBuffer), NULL) ;
if ( !prepareRead (e, bArr, responseIsRead, cxn, 1) )
{
syslog (LOG_ERR, PREPARE_READ_FAILED, peerName, cxn->ident) ;
freeBufferArray (bArr) ;
cxnWait (cxn) ;
return ;
}
else
{
/* only setup the timer if we're still waiting for a response
to something. There's not necessarily a 1-to-1 mapping
between reads and writes in streaming mode. May have been
set already above (that would be unlikely I think). */
VALIDATE_CONNECTION (cxn) ;
dprintf (5,"%s:%d about to do some writes\n",
hostPeerName (cxn->myHost),cxn->ident) ;
doSomeWrites (cxn) ;
/* If the read timer is (still) running, update it to give
those terminally slow hosts that take forever to drain
the network buffers and just dribble out responses the
benefit of the doubt. XXX - maybe should just increase
timeout for these! */
if (cxn->readBlockedTimerId)
cxn->readBlockedTimerId = updateSleep (cxn->readBlockedTimerId,
responseTimeoutCbk,
cxn->readTimeout,
cxn) ;
}
VALIDATE_CONNECTION (cxn) ;
break ;
case cxnWaitingS: /* presumably after a code 205 or 400 */
case cxnConnectingS: /* presumably after a code 205 or 400 */
case cxnSleepingS: /* probably after a 480 */
break ;
case cxnDeadS:
delConnection (cxn) ;
break ;
case cxnStartingS:
default:
die ("Bad connection state: %s\n",stateToString (cxn->state)) ;
}
}
/*
* called when the write of the QUIT command has completed.
*/
static void quitWritten (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
const char *peerName ;
peerName = hostPeerName (cxn->myHost) ;
clearTimer (cxn->writeBlockedTimerId) ;
ASSERT (cxn->myEp == e) ;
VALIDATE_CONNECTION (cxn) ;
if (i != IoDone)
{
errno = endPointErrno (e) ;
syslog (LOG_ERR, QUIT_WRITE_FAILED, peerName, cxn->ident) ;
if (cxn->state == cxnClosingS)
{
cxnDead (cxn) ;
delConnection (cxn) ;
}
else
cxnWait (cxn) ;
}
else
/* The QUIT command has been sent, so start the response timer. */
initReadBlockedTimeout (cxn) ;
freeBufferArray (b) ;
}
/*
* called when the write of the IHAVE-body data is finished
*/
static void ihaveBodyDone (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
ASSERT (e == cxn->myEp) ;
clearTimer (cxn->writeBlockedTimerId) ;
if (i != IoDone)
{
errno = endPointErrno (e) ;
syslog (LOG_ERR, IHAVE_WRITE_FAILED, hostPeerName (cxn->myHost),
cxn->ident) ;
cxnLogStats (cxn,true) ;
if (cxn->state == cxnClosingS)
{
cxnDead (cxn) ;
delConnection (cxn) ;
}
else
cxnSleep (cxn) ;
}
else
/* The article has been sent, so start the response timer. */
initReadBlockedTimeout (cxn) ;
freeBufferArray (b) ;
return ;
}
/*
* Called when a command set (IHAVE, CHECK, TAKETHIS) has been
* written to the remote.
*/
static void commandWriteDone (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
const char *peerName ;
ASSERT (e == cxn->myEp) ;
peerName = hostPeerName (cxn->myHost) ;
freeBufferArray (b) ;
clearTimer (cxn->writeBlockedTimerId) ;
if (i != IoDone)
{
errno = endPointErrno (e) ;
syslog (LOG_ERR, COMMAND_WRITE_FAILED, peerName, cxn->ident) ;
cxnLogStats (cxn,true) ;
if (cxn->state == cxnClosingS)
{
cxnDead (cxn) ;
delConnection (cxn) ;
}
else
{
/* XXX - so cxnSleep() doesn't die in VALIDATE_CONNECTION () */
deferAllArticles (cxn) ;
cxnIdle (cxn) ;
cxnSleep (cxn) ;
}
}
else
{
/* Some(?) hosts return the 439 response even before we're done
sending, so don't go idle until here */
if (cxn->state == cxnFeedingS && cxn->articleQTotal == 0)
cxnIdle (cxn) ;
else
/* The command set has been sent, so start the response timer.
XXX - we'd like finer grained control */
initReadBlockedTimeout (cxn) ;
if ( cxn->doesStreaming )
doSomeWrites (cxn) ; /* pump data as fast as possible */
/* XXX - will clear the read timeout */
}
}
/*
* Called when the MODE STREAM command has been written down the pipe.
*/
static void modeCmdIssued (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
ASSERT (e == cxn->myEp) ;
clearTimer (cxn->writeBlockedTimerId) ;
/* The mode command has been sent, so start the response timer */
initReadBlockedTimeout (cxn) ;
if (i != IoDone)
{
dprintf (1,"%s:%d MODE STREAM command failed to write\n",
hostPeerName (cxn->myHost), cxn->ident) ;
syslog (LOG_ERR,MODE_STREAM_FAILED,hostPeerName (cxn->myHost),
cxn->ident) ;
cxnSleepOrDie (cxn) ;
}
freeBufferArray (b) ;
}
/*
* Called whenever some amount of data has been written to the pipe but
* more data remains to be written
*/
static void writeProgress (EndPoint e, IoStatus i, Buffer *b, void *d)
{
Connection cxn = (Connection) d ;
ASSERT (i == IoProgress) ;
if (cxn->writeTimeout > 0)
cxn->writeBlockedTimerId = updateSleep (cxn->writeBlockedTimerId,
writeTimeoutCbk, cxn->writeTimeout,
cxn) ;
}
/*
* Timers.
*/
/*
* This is called when the timeout for the reponse from the remote
* goes off. We tear down the connection and notify our host.
*/
static void responseTimeoutCbk (TimeoutId id, void *data)
{
Connection cxn = (Connection) data ;
const char *peerName ;
ASSERT (id == cxn->readBlockedTimerId) ;
ASSERT (cxn->state == cxnConnectingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnFlushingS ||
cxn->state == cxnClosingS) ;
VALIDATE_CONNECTION (cxn) ;
/* XXX - let abortConnection clear readBlockedTimerId, otherwise
VALIDATE_CONNECTION() will croak */
peerName = hostPeerName (cxn->myHost) ;
syslog (LOG_WARNING, RESPONSE_TIMEOUT, peerName, cxn->ident) ;
dprintf (1,"%s:%d shutting down non-repsonsive connection\n",
hostPeerName (cxn->myHost), cxn->ident) ;
cxnLogStats (cxn,true) ;
if (cxn->state == cxnClosingS)
{
abortConnection (cxn) ;
delConnection (cxn) ;
}
else
cxnSleep (cxn) ; /* will notify the Host */
}
/*
* This is called when the data write timeout for the remote
* goes off. We tear down the connection and notify our host.
*/
static void writeTimeoutCbk (TimeoutId id, void *data)
{
Connection cxn = (Connection) data ;
const char *peerName ;
ASSERT (id == cxn->writeBlockedTimerId) ;
ASSERT (cxn->state == cxnConnectingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnFlushingS ||
cxn->state == cxnClosingS) ;
VALIDATE_CONNECTION (cxn) ;
/* XXX - let abortConnection clear writeBlockedTimerId, otherwise
VALIDATE_CONNECTION() will croak */
peerName = hostPeerName (cxn->myHost) ;
syslog (LOG_WARNING, WRITE_TIMEOUT, peerName, cxn->ident) ;
dprintf (1,"%s:%d shutting down non-responsive connection\n",
hostPeerName (cxn->myHost), cxn->ident) ;
cxnLogStats (cxn,true) ;
if (cxn->state == cxnClosingS)
{
abortConnection (cxn) ;
delConnection (cxn) ;
}
else
cxnSleep (cxn) ; /* will notify the Host */
}
/*
* Called by the EndPoint class when the timer goes off
*/
void reopenTimeoutCbk (TimeoutId id, void *data)
{
Connection cxn = (Connection) data ;
ASSERT (id == cxn->sleepTimerId) ;
cxn->sleepTimerId = 0 ;
if (cxn->state != cxnSleepingS)
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
}
else
(void) cxnConnect (cxn) ;
}
/*
* timeout callback to close down long running connection.
*/
static void flushCxnCbk (TimeoutId id, void *data)
{
Connection cxn = (Connection) data ;
ASSERT (id == cxn->flushTimerId) ;
VALIDATE_CONNECTION (cxn) ;
cxn->flushTimerId = 0 ;
if (!(cxn->state == cxnFeedingS || cxn->state == cxnConnectingS ||
cxn->state == cxnIdleS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
}
else
{
dprintf (1,"%s:%d Handling periodic connection close.\n",
hostPeerName (cxn->myHost), cxn->ident) ;
syslog (LOG_NOTICE, CXN_PERIODIC_CLOSE,
hostPeerName (cxn->myHost), cxn->ident) ;
cxnFlush (cxn) ;
}
}
/*
* Timer callback for when the connection has not received an
* article from INN. When that happens we tear down the network
* connection to help recycle fds
*/
static void articleTimeoutCbk (TimeoutId id, void *data)
{
Connection cxn = (Connection) data ;
const char *peerName = hostPeerName (cxn->myHost) ;
(void) id ; /* keep lint happy */
ASSERT (cxn->artReceiptTimerId == id) ;
VALIDATE_CONNECTION (cxn) ;
cxn->artReceiptTimerId = 0 ;
if (cxn->state != cxnIdleS)
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
/* it's doubtful (right?) that this timer could go off and there'd
still be articles in the queue. */
if (cxn->articleQTotal > 0)
{
syslog (LOG_WARNING, ARTICLE_TIMEOUT_W_Q_MSG, peerName, cxn->ident) ;
}
else
{
syslog (LOG_NOTICE, ARTICLE_TIMEOUT_MSG, peerName, cxn->ident) ;
cxn->state = cxnIdleTimeoutS ;
cxnFlush (cxn) ;
}
}
/*
* function to be called when the fd is not ready for reading, but there is
* an article on tape or in the queue to be done. Things are done this way
* so that a Connection doesn't hog time trying to find the next good
* article for writing. With a large backlog of expired articles that would
* take a long time. Instead the Connection just tries its next article on
* tape or queue, and if that's no good then it registers this callback so
* that other Connections have a chance of being serviced.
*/
static void cxnWorkProc (EndPoint ep, void *data)
{
Connection cxn = (Connection) data ;
(void) ep ; /* keep lint happy */
dprintf (2,"%s:%d calling work proc\n",
hostPeerName (cxn->myHost),cxn->ident) ;
if (writesNeeded (cxn))
doSomeWrites (cxn) ; /* may re-register the work proc... */
else if (cxn->state == cxnFlushingS || cxn->state == cxnClosingS)
{
if (cxn->articleQTotal == 0)
issueQUIT (cxn) ;
}
else
dprintf (2,"%s:%d no writes were needed....\n",
hostPeerName (cxn->myHost), cxn->ident) ;
}
/****************************************************************************
*
* END EndPoint callback area.
*
****************************************************************************/
/****************************************************************************
*
* REPONSE CODE PROCESSING.
*
***************************************************************************/
/*
* A connection needs to sleep, but if it's closing it needs to die instead.
*/
static void cxnSleepOrDie (Connection cxn)
{
if (cxn->state == cxnClosingS)
cxnDead (cxn) ;
else
cxnSleep (cxn) ;
}
/*
* Handle the response 205 to our QUIT command, which means the
* remote is going away and we can happily cleanup
*/
static void processResponse205 (Connection cxn, char *response)
{
bool immedRecon ;
VALIDATE_CONNECTION (cxn) ;
(void) response ; /* keep lint happy */
if (!(cxn->state == cxnFeedingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnFlushingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
switch (cxn->state)
{
case cxnFlushingS:
case cxnClosingS:
ASSERT (cxn->articleQTotal == 0) ;
cxnLogStats (cxn,true) ;
immedRecon = cxn->immedRecon ;
hostCxnDead (cxn->myHost,cxn) ;
if (cxn->state == cxnFlushingS && immedRecon)
{
abortConnection (cxn) ;
if (!cxnConnect (cxn))
syslog (LOG_NOTICE,CXN_REOPEN_FAILED,hostPeerName (cxn->myHost),
cxn->ident) ;
}
else if (cxn->state == cxnFlushingS)
cxnWait (cxn) ;
else
cxnDead (cxn) ;
break ;
case cxnIdleS:
case cxnFeedingS:
/* this shouldn't ever happen... */
syslog (LOG_NOTICE,BAD_RESPONSE,hostPeerName (cxn->myHost),
cxn->ident, 205) ;
cxnSleepOrDie (cxn) ;
break ;
default:
die ("Bad connection state: %s\n",stateToString (cxn->state)) ;
}
}
/*
* Handle a response code of 238 which is the "no such article"
* reply to the CHECK command (i.e. remote wants it).
*/
static void processResponse238 (Connection cxn, char *response)
{
char *msgid ;
ArtHolder artHolder ;
if (!cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_STREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
msgid = getMsgId (response) ;
if (cxn->checkRespHead == NULL) /* peer is confused */
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,238) ;
cxnSleepOrDie (cxn) ;
}
else if (msgid == NULL || strlen (msgid) == 0 ||
(artHolder = artHolderByMsgId (msgid, cxn->checkRespHead)) == NULL)
noSuchMessageId (cxn,238,msgid,response) ;
else
{
/* now remove the article from the check queue and move it onto the
transmit queue. Another function wil take care of transmitting */
remArtHolder (artHolder, &cxn->checkRespHead, &cxn->articleQTotal) ;
if (cxn->state != cxnClosingS)
appendArtHolder (artHolder, &cxn->takeHead, &cxn->articleQTotal) ;
else
{
hostTakeBackArticle (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
}
if (msgid != NULL)
FREE (msgid) ;
}
/*
* process the response "try again later" to the CHECK command If this
* returns true then the connection is still usable.
*/
static void processResponse431 (Connection cxn, char *response)
{
char *msgid ;
ArtHolder artHolder ;
if (!cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_STREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
msgid = getMsgId (response) ;
if (cxn->checkRespHead == NULL) /* peer is confused */
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,431) ;
cxnSleepOrDie (cxn) ;
}
else if (msgid == NULL || strlen (msgid) == 0 ||
(artHolder = artHolderByMsgId (msgid, cxn->checkRespHead)) == NULL)
noSuchMessageId (cxn,431,msgid,response) ;
else
{
remArtHolder (artHolder, &cxn->checkRespHead, &cxn->articleQTotal) ;
if (cxn->articleQTotal == 0)
cxnIdle (cxn) ;
hostArticleDeferred (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
if (msgid != NULL)
FREE (msgid) ;
}
/*
* process the "already have it" response to the CHECK command. If this
* returns true then the connection is still usable.
*/
static void processResponse438 (Connection cxn, char *response)
{
char *msgid ;
ArtHolder artHolder ;
if (!cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_STREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
msgid = getMsgId (response) ;
if (cxn->checkRespHead == NULL) /* peer is confused */
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,438) ;
cxnSleepOrDie (cxn) ;
}
else if (msgid == NULL || strlen (msgid) == 0 ||
(artHolder = artHolderByMsgId (msgid, cxn->checkRespHead)) == NULL)
noSuchMessageId (cxn,438,msgid,response) ;
else
{
cxn->checksRefused++ ;
remArtHolder (artHolder, &cxn->checkRespHead, &cxn->articleQTotal) ;
if (cxn->articleQTotal == 0)
cxnIdle (cxn) ;
hostArticleNotWanted (cxn->myHost, cxn, artHolder->article);
delArtHolder (artHolder) ;
}
if (msgid != NULL)
FREE (msgid) ;
}
/*
* process the "article transferred ok" response to the TAKETHIS.
*/
static void processResponse239 (Connection cxn, char *response)
{
char *msgid ;
ArtHolder artHolder ;
if (!cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_STREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
msgid = getMsgId (response) ;
if (cxn->takeRespHead == NULL) /* peer is confused */
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,239) ;
cxnSleepOrDie (cxn) ;
}
else if (msgid == NULL || strlen (msgid) == 0 ||
(artHolder = artHolderByMsgId (msgid, cxn->takeRespHead)) == NULL)
noSuchMessageId (cxn,239,msgid,response) ;
else
{
cxn->takesOkayed++ ;
remArtHolder (artHolder, &cxn->takeRespHead, &cxn->articleQTotal) ;
if (cxn->articleQTotal == 0)
cxnIdle (cxn) ;
hostArticleAccepted (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
if (msgid != NULL)
FREE (msgid) ;
}
/*
* Set the thresholds for no-CHECK mode; negative means leave existing value
*/
void cxnSetCheckThresholds (Connection cxn,
double lowFilter, double highFilter,
double lowPassFilter)
{
/* Adjust current value for new scaling */
if (cxn->lowPassFilter > 0.0)
cxn->filterValue = cxn->filterValue / cxn->lowPassFilter * lowPassFilter;
/* Stick in new values */
if (highFilter >= 0)
cxn->onThreshold = highFilter * lowPassFilter / 100.0;
if (lowFilter >= 0)
cxn->offThreshold = lowFilter * lowPassFilter / 100.0;
cxn->lowPassFilter = lowPassFilter;
}
/*
* Blow away the connection gracelessly and immedately clean up
*/
void cxnNuke (Connection cxn)
{
abortConnection (cxn) ;
hostCxnDead (cxn->myHost,cxn) ;
delConnection(cxn) ;
}
/*
* process a "article rejected do not try again" response to the
* TAKETHIS.
*/
static void processResponse439 (Connection cxn, char *response)
{
char *msgid ;
ArtHolder artHolder ;
if (!cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_STREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
msgid = getMsgId (response) ;
if (cxn->takeRespHead == NULL) /* peer is confused */
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,239) ;
cxnSleepOrDie (cxn) ;
}
else if (msgid == NULL || strlen (msgid) == 0 ||
(artHolder = artHolderByMsgId (msgid, cxn->takeRespHead)) == NULL)
noSuchMessageId (cxn,439,msgid,response) ;
else
{
cxn->takesRejected++ ;
remArtHolder (artHolder, &cxn->takeRespHead, &cxn->articleQTotal) ;
/* Some(?) hosts return the 439 response even before we're done
sending */
if (cxn->articleQTotal == 0 && !writeIsPending(cxn->myEp))
cxnIdle (cxn) ;
hostArticleRejected (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
if (msgid != NULL)
FREE (msgid) ;
}
/*
* process the "article transferred ok" response to the IHAVE-body.
*/
static void processResponse235 (Connection cxn, char *response)
{
ArtHolder artHolder ;
(void) response ; /* keep lint happy */
if (cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_NONSTREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
ASSERT (cxn->articleQTotal == 1) ;
ASSERT (cxn->takeRespHead != NULL) ;
VALIDATE_CONNECTION (cxn) ;
if (cxn->takeRespHead == NULL) /* peer is confused */
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,235) ;
cxnSleepOrDie (cxn) ;
}
else
{
/* now remove the article from the queue and tell the Host to forget
about it. */
artHolder = cxn->takeRespHead ;
cxn->takeRespHead = NULL ;
cxn->articleQTotal = 0 ;
cxn->takesOkayed++ ;
if (cxn->articleQTotal == 0)
cxnIdle (cxn) ;
hostArticleAccepted (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
}
/*
* process the "send article to be transfered" reponse to the IHAVE.
*/
static void processResponse335 (Connection cxn, char *response)
{
(void) response ; /* keep lint happy */
if (cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_NONSTREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (cxn->checkRespHead == NULL)
{
syslog (LOG_NOTICE,BAD_RESPONSE,
hostPeerName (cxn->myHost),cxn->ident,335) ;
cxnSleepOrDie (cxn) ;
}
else
{
VALIDATE_CONNECTION (cxn) ;
/* now move the article into the third queue */
cxn->takeHead = cxn->checkRespHead ;
cxn->checkRespHead = NULL ;
issueIHAVEBody (cxn) ;
}
}
/*
* process the "not accepting articles" response. This could be to any of
* the IHAVE/CHECK/TAKETHIS command, but not the banner--that's handled
* elsewhere.
*/
static void processResponse400 (Connection cxn, char *response)
{
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
/* We may get a response 400 multiple times when in streaming mode. */
syslog (LOG_NOTICE,CXN_BLOCKED,hostPeerName(cxn->myHost),cxn->ident,
response) ;
/* right here there may still be data queued to write and so we'll fail
trying to issue the quit ('cause a write will be pending). Furthermore,
the data pending may be half way through an command, and so just
tossing the buffer is nt sufficient. But figuring out where we are and
doing a tidy job is hard */
if (writeIsPending (cxn->myEp))
cxnSleepOrDie (cxn) ;
else
{
if (cxn->articleQTotal > 0)
{
/* Defer the articles here so that cxnFlush() doesn't set up an
immediate reconnect. */
deferAllArticles (cxn) ;
clearTimer (cxn->readBlockedTimerId) ;
/* XXX - so cxnSleep() doesn't die when it validates the connection */
cxnIdle (cxn) ;
}
/* XXX - it would be nice if we QUIT first, but we'd have to go
into a state where we just search for the 205 response, and
only go into the sleep state at that point */
cxnSleepOrDie (cxn) ;
}
}
/*
* process the "not wanted" reponse to the IHAVE.
*/
static void processResponse435 (Connection cxn, char *response)
{
ArtHolder artHolder ;
(void) response ; /* keep lint happy */
if (cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_NONSTREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
ASSERT (cxn->articleQTotal == 1) ;
ASSERT (cxn->checkRespHead != NULL) ;
VALIDATE_CONNECTION (cxn) ;
cxn->articleQTotal-- ;
cxn->checksRefused++ ;
artHolder = cxn->checkRespHead ;
cxn->checkRespHead = NULL ;
if (cxn->articleQTotal == 0)
cxnIdle (cxn) ;
hostArticleNotWanted (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
#if 0
dprintf (1,"%s:%d On exiting 435 article queue total is %d (%d %d %d %d)\n",
hostPeerName (cxn->myHost), cxn->ident,
cxn->articleQTotal,
(int) (cxn->checkHead != NULL),
(int) (cxn->checkRespHead != NULL),
(int) (cxn->takeHead != NULL),
(int) (cxn->takeRespHead != NULL));
#endif
}
/*
* process the "transfer failed" response to the IHAVE-body, (seems this
* can come from the IHAVE too).
*/
static void processResponse436 (Connection cxn, char *response)
{
ArtHolder artHolder ;
(void) response ; /* keep lint happy */
if (cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_NONSTREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
ASSERT (cxn->articleQTotal == 1) ;
ASSERT (cxn->takeRespHead != NULL || cxn->checkRespHead != NULL) ;
VALIDATE_CONNECTION (cxn) ;
cxn->articleQTotal-- ;
if (cxn->takeRespHead != NULL) /* IHAVE-body command barfed */
{
artHolder = cxn->takeRespHead ;
cxn->takeRespHead = NULL ;
}
else /* IHAVE command barfed */
{
artHolder = cxn->checkRespHead ;
cxn->checkRespHead = NULL ;
}
/* Some(?) hosts return the 436 response even before we're done
sending */
if (cxn->articleQTotal == 0 && !writeIsPending(cxn->myEp))
cxnIdle (cxn) ;
hostArticleDeferred (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
/*
* Process the "article rejected do not try again" response to the
* IHAVE-body.
*/
static void processResponse437 (Connection cxn, char *response)
{
ArtHolder artHolder ;
(void) response ; /* keep lint happy */
if (cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_NONSTREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
ASSERT (cxn->articleQTotal == 1) ;
ASSERT (cxn->takeRespHead != NULL) ;
VALIDATE_CONNECTION (cxn) ;
cxn->articleQTotal-- ;
cxn->takesRejected++ ;
artHolder = cxn->takeRespHead ;
cxn->takeRespHead = NULL ;
/* Some(?) hosts return the 437 response even before we're done
sending */
if (cxn->articleQTotal == 0 && !writeIsPending(cxn->myEp))
cxnIdle (cxn) ;
hostArticleRejected (cxn->myHost, cxn, artHolder->article) ;
delArtHolder (artHolder) ;
}
/* Process the response 480 Transfer permission defined. We're probably
talking to a remote nnrpd on a system that forgot to put us in
the hosts.nntp */
static void processResponse480 (Connection cxn, char *response)
{
(void) response ; /* keep lint happy */
if (cxn->doesStreaming)
{
syslog (LOG_ERR,CXN_NONSTREAM_RESP,hostPeerName (cxn->myHost),
cxn->ident,response) ;
cxnSleepOrDie (cxn) ;
return ;
}
if (!(cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
VALIDATE_CONNECTION (cxn) ;
syslog (LOG_NOTICE,NO_TRANSFER_NNRPD,hostPeerName (cxn->myHost),
cxn->ident) ;
if (cxn->state == cxnClosingS)
cxnDead (cxn) ;
else
cxnSleep (cxn) ;
}
/****************************************************************************
*
* END REPONSE CODE PROCESSING.
*
***************************************************************************/
/*
* puts the Connection into the sleep state.
*/
static void cxnSleep (Connection cxn)
{
ASSERT (cxn != NULL) ;
ASSERT (cxn->state == cxnFlushingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnConnectingS) ;
VALIDATE_CONNECTION (cxn) ;
abortConnection (cxn) ;
prepareReopenCbk (cxn) ; /* XXX - we don't want to reopen if idle */
cxn->state = cxnSleepingS ;
/* tell our Host we're asleep so it doesn't try to give us articles */
hostCxnSleeping (cxn->myHost,cxn) ;
}
static void cxnDead (Connection cxn)
{
ASSERT (cxn != NULL) ;
VALIDATE_CONNECTION (cxn) ;
abortConnection (cxn) ;
cxn->state = cxnDeadS ;
}
/*
* Sets the idle timer. If no articles arrive before the timer expires, the
* connection will be closed.
*/
static void cxnIdle (Connection cxn)
{
ASSERT (cxn != NULL) ;
ASSERT (cxn->state == cxnFeedingS || cxn->state == cxnConnectingS ||
cxn->state == cxnFlushingS || cxn->state == cxnClosingS) ;
ASSERT (cxn->articleQTotal == 0) ;
ASSERT (cxn->writeBlockedTimerId == 0) ;
ASSERT (!writeIsPending (cxn->myEp)) ;
ASSERT (cxn->sleepTimerId == 0) ;
if (cxn->state == cxnFeedingS || cxn->state == cxnConnectingS)
{
if (cxn->articleReceiptTimeout > 0)
{
clearTimer (cxn->artReceiptTimerId) ;
cxn->artReceiptTimerId = prepareSleep (articleTimeoutCbk,
cxn->articleReceiptTimeout,
cxn) ;
}
if (cxn->readTimeout > 0 && cxn->state == cxnFeedingS)
clearTimer (cxn->readBlockedTimerId) ;
cxn->state = cxnIdleS ;
ASSERT (cxn->readBlockedTimerId == 0) ;
}
}
/*
* Called when a response from the remote refers to a non-existant
* message-id. The network connection is aborted and the Connection
* object goes into sleep mode.
*/
static void noSuchMessageId (Connection cxn, u_int responseCode,
const char *msgid, const char *response)
{
const char *peerName = hostPeerName (cxn->myHost) ;
if (msgid == NULL || strlen (msgid) == 0)
syslog (LOG_ERR, NOMSGID, peerName, cxn->ident, responseCode, response) ;
else
syslog (LOG_ERR, INVALID_MSGID, peerName, cxn->ident, responseCode, msgid) ;
cxnLogStats (cxn,true) ;
if (cxn->state != cxnClosingS)
cxnSleep (cxn) ;
else
cxnDead (cxn) ;
}
/*
* a processing error has occured (for example in parsing a response), or
* we're at the end of the FSM and we're cleaning up.
*/
static void abortConnection (Connection cxn)
{
ASSERT (cxn != NULL) ;
VALIDATE_CONNECTION (cxn) ;
/* cxn->myEp could be NULL if we get here during cxnConnect (via
cxnWait()) */
if (cxn->myEp != NULL)
{
delEndPoint (cxn->myEp) ;
cxn->myEp = NULL ;
}
clearTimer (cxn->sleepTimerId) ;
clearTimer (cxn->artReceiptTimerId) ;
clearTimer (cxn->readBlockedTimerId) ;
clearTimer (cxn->writeBlockedTimerId) ;
clearTimer (cxn->flushTimerId) ;
deferAllArticles (cxn) ; /* give any articles back to Host */
bufferSetDataSize (cxn->respBuffer,0) ;
resetConnection (cxn) ;
if (cxn->state == cxnFeedingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnFlushingS ||
cxn->state == cxnClosingS)
hostCxnDead (cxn->myHost,cxn) ;
}
/*
* Set up the callback used when the Connection is sleeping (i.e. will try
* to reopen the connection).
*/
static void prepareReopenCbk (Connection cxn)
{
ASSERT (cxn->sleepTimerId == 0) ;
if (!(cxn->state == cxnConnectingS ||
cxn->state == cxnIdleS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnFlushingS ||
cxn->state == cxnStartingS))
{
syslog (LOG_ERR,CXN_BAD_STATE,hostPeerName (cxn->myHost),
cxn->ident,stateToString (cxn->state)) ;
cxnSleepOrDie (cxn) ;
return ;
}
dprintf (1,"%s:%d Setting up a reopen callback\n",
hostPeerName (cxn->myHost), cxn->ident) ;
cxn->sleepTimerId = prepareSleep (reopenTimeoutCbk, cxn->sleepTimeout, cxn) ;
/* bump the sleep timer amount each time to wait longer and longer. Gets
reset in resetConnection() */
cxn->sleepTimeout *= 2 ;
if (cxn->sleepTimeout > max_reconnect_period)
cxn->sleepTimeout = max_reconnect_period ;
}
/*
* (re)set all state variables to inital condition.
*/
static void resetConnection (Connection cxn)
{
ASSERT (cxn != NULL) ;
bufferSetDataSize (cxn->respBuffer,0) ;
cxn->loggedNoCr = false ;
cxn->maxCheck = 1 ;
cxn->immedRecon = false ;
cxn->doesStreaming = false ; /* who knows, next time around maybe... */
cxn->quitWasIssued = false ;
cxn->needsChecks = true ;
cxn->timeCon = 0 ;
cxn->artsTaken = 0 ;
cxn->checksIssued = 0 ;
cxn->checksRefused = 0 ;
cxn->takesRejected = 0 ;
cxn->takesOkayed = 0 ;
cxn->filterValue = 0.0 ;
}
/*
* Give back all articles that are queued, but not actually in progress.
* XXX merge come of this with deferAllArticles
*/
static void deferQueuedArticles (Connection cxn)
{
ArtHolder p, q ;
for (q = NULL, p = cxn->checkHead ; p != NULL ; p = q)
{
q = p->next ;
hostTakeBackArticle (cxn->myHost, cxn, p->article) ;
delArtHolder (p) ;
cxn->articleQTotal-- ;
}
cxn->checkHead = NULL ;
for (q = NULL, p = cxn->takeHead ; cxn->doesStreaming && p != NULL ; p = q)
{
q = p->next ;
hostTakeBackArticle (cxn->myHost, cxn, p->article) ;
delArtHolder (p) ;
cxn->articleQTotal-- ;
}
cxn->takeHead = NULL ;
}
/*
* Give back any articles we have to the Host for later retrys.
*/
static void deferAllArticles (Connection cxn)
{
ArtHolder p, q ;
for (q = NULL, p = cxn->checkHead ; p != NULL ; p = q)
{
q = p->next ;
hostTakeBackArticle (cxn->myHost, cxn, p->article) ;
delArtHolder (p) ;
cxn->articleQTotal-- ;
}
cxn->checkHead = NULL ;
for (q = NULL, p = cxn->checkRespHead ; p != NULL ; p = q)
{
q = p->next ;
hostTakeBackArticle (cxn->myHost, cxn, p->article) ;
delArtHolder (p) ;
cxn->articleQTotal-- ;
}
cxn->checkRespHead = NULL ;
for (q = NULL, p = cxn->takeHead ; p != NULL ; p = q)
{
q = p->next ;
hostTakeBackArticle (cxn->myHost, cxn, p->article) ;
delArtHolder (p) ;
cxn->articleQTotal-- ;
}
cxn->takeHead = NULL ;
for (q = NULL, p = cxn->takeRespHead ; p != NULL ; p = q)
{
q = p->next ;
hostTakeBackArticle (cxn->myHost, cxn, p->article) ;
delArtHolder (p) ;
cxn->articleQTotal-- ;
}
cxn->takeRespHead = NULL ;
ASSERT (cxn->articleQTotal == 0) ;
}
/*
* Called when there's an article to be pushed out to the remote. Even if
* the Connection has an article it's possible that nothing will be written
* (e.g. if the article on the queue doesn't exist any more)
*/
static void doSomeWrites (Connection cxn)
{
bool doneSome = false ;
/* If there's a write pending we can't do anything now. */
if ( writeIsPending (cxn->myEp) )
return ;
else if ( writesNeeded (cxn) ) /* something on a queue. */
{
if (cxn->doesStreaming)
doneSome = issueStreamingCommands (cxn) ;
else
doneSome = issueIHAVE (cxn) ;
/* doneSome will be false if article(s) were gone, but if the Host
has something available, then it would have been put on the queue
for next time around. */
if (!doneSome)
{
if (writesNeeded (cxn)) /* Host gave us something */
addWorkCallback (cxn->myEp,cxnWorkProc,cxn) ; /* for next time. */
else if (cxn->articleQTotal == 0)
{
/* if we were in cxnFeedingS, then issueStreamingCommands
already called cxnIdle(). */
if (cxn->state == cxnClosingS || cxn->state == cxnFlushingS)
issueQUIT (cxn) ; /* and nothing to wait for... */
}
}
}
else if (cxn->state == cxnClosingS || cxn->state == cxnFlushingS)
{ /* nothing to do... */
if (cxn->articleQTotal == 0)
issueQUIT (cxn) ; /* and nothing to wait for before closing */
}
}
/* Queue up a buffer with the IHAVE command in it for the article at
* the head of the transmisson queue.
*
* If the article is missing, then the Host will be notified and
* another article may be put on the Connections queue. This new
* article is ignored for now, but a work callback is registered so
* that it can be looked at later.
*/
static bool issueIHAVE (Connection cxn)
{
Buffer ihaveBuff, *writeArr ;
ArtHolder artH ;
Article article ;
const char *msgid ;
char *p ;
u_int tmp ;
size_t bufLen = 256 ;
bool rval = false ;
ASSERT (!cxn->doesStreaming) ;
ASSERT (cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS) ;
ASSERT (cxn->articleQTotal == 1) ;
ASSERT (cxn->checkHead != NULL) ;
ASSERT (writeIsPending (cxn->myEp) == false) ;
VALIDATE_CONNECTION (cxn) ;
artH = cxn->checkHead ;
article = cxn->checkHead->article ;
msgid = artMsgId (artH->article) ;
ASSERT (msgid != NULL) ;
ASSERT (article != NULL) ;
#if 0
if ( !artFileIsValid (article) )
{
cxn->checkHead = NULL ;
cxn->articleQTotal-- ;
hostArticleIsMissing (cxn->myHost, cxn, article) ;
delArtHolder (artH) ;
}
else
#endif
{
if ((tmp = (strlen (msgid) + 10)) > bufLen)
bufLen = tmp ;
ihaveBuff = newBuffer (bufLen) ;
ASSERT (ihaveBuff != NULL) ;
p = bufferBase (ihaveBuff) ;
sprintf (p, "IHAVE %s\r\n", msgid) ;
bufferSetDataSize (ihaveBuff, strlen (p)) ;
dprintf (5,"%s:%d Command IHAVE %s\n",
hostPeerName (cxn->myHost),cxn->ident,msgid) ;
writeArr = makeBufferArray (ihaveBuff, NULL) ;
if ( !prepareWriteWithTimeout (cxn->myEp, writeArr, commandWriteDone,
cxn) )
{
syslog (LOG_ERR, PREPARE_WRITE_FAILED,
hostPeerName (cxn->myHost), cxn->ident) ;
die ("Prepare write for IHAVE failed") ;
}
/* now move the article to the second queue */
cxn->checkRespHead = cxn->checkHead ;
cxn->checkHead = NULL ;
cxn->checksIssued++ ;
hostArticleOffered (cxn->myHost, cxn) ;
rval = true ;
}
return rval ;
}
/*
* Do a prepare write with the article body as the body portion of the
* IHAVE command
*/
static void issueIHAVEBody (Connection cxn)
{
Buffer *writeArray ;
Article article ;
#ifdef XXX_RAWHACK
static int XXXboguscount = 0;
time_t now = theTime();
#endif /* XXX_RAWHACK */
ASSERT (cxn != NULL) ;
ASSERT (!cxn->doesStreaming) ;
ASSERT (cxn->state == cxnFlushingS ||
cxn->state == cxnFeedingS ||
cxn->state == cxnClosingS) ;
ASSERT (cxn->articleQTotal == 1) ;
ASSERT (cxn->takeHead != NULL) ;
VALIDATE_CONNECTION (cxn) ;
article = cxn->takeHead->article ;
ASSERT (article != NULL) ;
if (cxn->state != cxnClosingS)
writeArray = artGetNntpBuffers (article) ;
else
writeArray = NULL ;
if (writeArray == NULL)
{
/* missing article (expired for example) will get us here. */
if (dotBuffer == NULL)
{
dotBuffer = newBufferByCharP (".\r\n",3,3) ;
dotFirstBuffer = newBufferByCharP ("\r\n.",3,3) ;
crlfBuffer = newBufferByCharP ("\r\n",2,2) ;
}
/* we'll just write the empty buffer and the remote will complain
with 437 */
writeArray = makeBufferArray (bufferTakeRef (dotBuffer),NULL) ;
}
if ( !prepareWriteWithTimeout (cxn->myEp, writeArray, ihaveBodyDone, cxn) )
{
syslog (LOG_ERR, PREPARE_WRITE_FAILED,
hostPeerName (cxn->myHost), cxn->ident) ;
die ("Preparewrite failed in issueIHAVEBody") ;
}
else
{
dprintf (5,"%s:%d prepared write for IHAVE body.\n",
hostPeerName (cxn->myHost),cxn->ident) ;
}
/* now move the article to the last queue */
cxn->takeRespHead = cxn->takeHead ;
cxn->takeHead = NULL ;
return ;
}
/* Process the two command queues. Slaps all the CHECKs together and
* then does the TAKETHIS commands.
*
* If no articles on the queue(s) are valid, then the Host is
* notified. It may queue up new articles on the Connection, but
* these are ignored for now. A work proc is registered so the
* articles can be processed later.
*/
static bool issueStreamingCommands (Connection cxn)
{
Buffer checkBuffer = NULL ; /* the buffer with the CHECK commands in it. */
Buffer *writeArray = NULL ;
ArtHolder p, q ;
bool rval = false ;
ASSERT (cxn != NULL) ;
ASSERT (cxn->myEp != NULL) ;
ASSERT (cxn->doesStreaming) ;
VALIDATE_CONNECTION (cxn) ;
checkBuffer = buildCheckBuffer (cxn) ; /* may be null if none to issue */
if (checkBuffer != NULL)
{
/* Now shift the articles to their new queue. */
for (p = cxn->checkRespHead ; p != NULL && p->next != NULL ; p = p->next)
/* nada--finding end of queue*/ ;
if (p == NULL)
cxn->checkRespHead = cxn->checkHead ;
else
p->next = cxn->checkHead ;
cxn->checkHead = NULL ;
}
writeArray = buildTakethisBuffers (cxn,checkBuffer) ; /* may be null */
/* If not null, then writeArray will have checkBuffer (if it wasn't NULL)
in the first spot and the takethis buffers after that. */
if (writeArray)
{
if ( !prepareWriteWithTimeout (cxn->myEp, writeArray,
commandWriteDone, cxn) )
{
syslog (LOG_ERR, PREPARE_WRITE_FAILED,
hostPeerName (cxn->myHost), cxn->ident) ;
die ("Prepare write for STREAMING commands failed") ;
}
rval = true ;
/* now shift articles over to their new queue. */
for (p = cxn->takeRespHead ; p != NULL && p->next != NULL ; p = p->next)
/* nada--finding end of queue */ ;
if (p == NULL)
cxn->takeRespHead = cxn->takeHead ;
else
p->next = cxn->takeHead ;
cxn->takeHead = NULL ;
}
/* we defer the missing article notification to here because if there
was a big backlog of missing articles *and* we're running in
no-CHECK mode, then the Host would be putting bad articles on the
queue we're taking them off of. */
if (cxn->missing && cxn->articleQTotal == 0)
cxnIdle (cxn) ;
for (p = cxn->missing ; p != NULL ; p = q)
{
hostArticleIsMissing (cxn->myHost, cxn, p->article) ;
q = p->next ;
delArtHolder (p) ;
}
cxn->missing = NULL ;
return rval ;
}
/*
* build up the buffer of all the CHECK commands.
*/
static Buffer buildCheckBuffer (Connection cxn)
{
ArtHolder p ;
size_t lenBuff = 0 ;
Buffer checkBuffer = NULL ;
const char *peerName = hostPeerName (cxn->myHost) ;
p = cxn->checkHead ;
while (p != NULL)
{
Article article = p->article ;
const char *msgid ;
msgid = artMsgId (article) ;
lenBuff += (8 + strlen (msgid)) ; /* 8 == strlen("CHECK \r\n") */
p = p->next ;
}
if (lenBuff > 0)
lenBuff++ ; /* for the null byte */
/* now build up the single buffer that contains all the CHECK commands */
if (lenBuff > 0)
{
char *t ;
size_t tlen = 0 ;
checkBuffer = newBuffer (lenBuff) ;
t = bufferBase (checkBuffer) ;
p = cxn->checkHead ;
while (p != NULL)
{
const char *msgid = artMsgId (p->article) ;
sprintf (t,"CHECK %s\r\n", msgid) ;
dprintf (5,"%s:%d Command %s", peerName, cxn->ident, t) ;
tlen += strlen (t) ;
while ( *t ) t++ ;
cxn->checksIssued++ ;
hostArticleOffered (cxn->myHost,cxn) ;
p = p->next ;
}
ASSERT (tlen + 1 == lenBuff) ;
bufferSetDataSize (checkBuffer, tlen) ;
}
return checkBuffer ;
}
/*
* Construct and array of TAKETHIS commands and the command bodies. Any
* articles on the queue that are missing will be removed and the Host will
* be informed.
*/
static Buffer *buildTakethisBuffers (Connection cxn, Buffer checkBuffer)
{
size_t lenArray = 0 ;
ArtHolder p, q ;
Buffer *rval = NULL ;
const char *peerName = hostPeerName (cxn->myHost) ;
if (checkBuffer != NULL)
lenArray++ ;
if (cxn->takeHead != NULL) /* some TAKETHIS commands to be done. */
{
Buffer takeBuffer ;
u_int takeBuffLen ;
u_int writeIdx = 0 ;
/* count up all the buffers we'll be writing. One extra each time for
the TAKETHIS command buffer*/
for (p = cxn->takeHead ; p != NULL ; p = p->next)
if (artContentsOk (p->article))
lenArray += (1 + artNntpBufferCount (p->article)) ;
/* now allocate the array for the buffers and put them all in it */
rval = ALLOC (Buffer, lenArray + 1) ; /* 1 for the terminator */
ASSERT (rval != NULL) ;
if (checkBuffer != NULL)
rval [writeIdx++] = checkBuffer ;
q = NULL ;
p = cxn->takeHead ;
while (p != NULL)
{
char *t ;
const char *msgid ;
Article article ;
Buffer *articleBuffers ;
int i, nntpLen ;
article = p->article ;
nntpLen = artNntpBufferCount (article) ;
msgid = artMsgId (article) ;
if (nntpLen == 0)
{ /* file no longer valid so drop from queue */
ArtHolder ta = p ;
if (q == NULL) /* it's the first in the queue */
cxn->takeHead = p->next ;
else
q->next = p->next ;
p = p->next ;
ASSERT (cxn->articleQTotal > 0) ;
cxn->articleQTotal-- ;
ta->next = cxn->missing ;
cxn->missing = ta ;
}
else
{
articleBuffers = artGetNntpBuffers (article) ;
/* set up the buffer with the TAKETHIS command in it.
12 == strlen ("TAKETHIS \n\r") */
takeBuffLen = 12 + strlen (msgid) ;
takeBuffer = newBuffer (takeBuffLen) ;
t = bufferBase (takeBuffer) ;
sprintf (t, "TAKETHIS %s\r\n", msgid) ;
bufferSetDataSize (takeBuffer, strlen (t)) ;
dprintf (5,"%s:%d Command %s", peerName, cxn->ident, t) ;
ASSERT (writeIdx <= lenArray) ;
rval [writeIdx++] = takeBuffer ;
/* now add all the buffers that make up the body of the TAKETHIS
command */
for (i = 0 ; i < nntpLen ; i++)
{
ASSERT (writeIdx <= lenArray) ;
rval [writeIdx++] = bufferTakeRef (articleBuffers [i]) ;
}
freeBufferArray (articleBuffers) ;
if ( !cxn->needsChecks )
{
/* this isn't quite right. An article may be counted
twice if we switch to no-CHECK mode after its
CHECK was issued, but before its TAKETHIS was done
just now. I'm not going to worry unless someone
complains. */
cxn->checksIssued++ ;
hostArticleOffered (cxn->myHost,cxn) ;
}
q = p ;
p = p->next ;
}
}
if (writeIdx > 0)
rval [writeIdx] = NULL ;
else
{ /* all articles were missing and no CHECKS */
FREE (rval) ;
rval = NULL ;
}
}
else if (checkBuffer != NULL) /* no TAKETHIS to do, but some CHECKS */
rval = makeBufferArray (checkBuffer, NULL) ;
return rval ;
}
/*
* for one reason or another we need to disconnect gracefully. We send a
* QUIT command.
*/
static void issueQUIT (Connection cxn)
{
Buffer quitBuffer, *writeArray ;
const char *peerName = hostPeerName (cxn->myHost) ;
ASSERT (cxn->takeHead == NULL) ;
ASSERT (cxn->checkHead == NULL) ;
VALIDATE_CONNECTION (cxn) ;
if (cxn->quitWasIssued)
return ;
if (writeIsPending (cxn->myEp))
{
syslog (LOG_ERR, QUIT_WHILE_WRITING, peerName, cxn->ident) ;
if (cxn->state == cxnClosingS)
cxnDead (cxn) ;
else
cxnWait (cxn) ;
}
else
{
quitBuffer = newBuffer (7) ;
strcpy (bufferBase (quitBuffer), "QUIT\r\n") ;
bufferSetDataSize (quitBuffer, 6) ;
writeArray = makeBufferArray (quitBuffer, NULL) ;
dprintf (1,"%s:%d Sending a quit command\n",
hostPeerName (cxn->myHost),cxn->ident) ;
cxn->quitWasIssued = true ; /* not exactly true, but good enough */
if ( !prepareWriteWithTimeout (cxn->myEp, writeArray, quitWritten,
cxn) )
{
syslog (LOG_ERR, PREPARE_WRITE_FAILED, peerName, cxn->ident) ;
die ("Prepare write for QUIT command failed") ;
}
}
}
/*
* Set up the timer for the blocked reads
*/
static void initReadBlockedTimeout (Connection cxn)
{
ASSERT (cxn != NULL) ;
ASSERT (cxn->state != cxnIdleS ) ;
/* set up the response timer. */
clearTimer (cxn->readBlockedTimerId) ;
if (cxn->readTimeout > 0)
cxn->readBlockedTimerId = prepareSleep (responseTimeoutCbk, cxn->readTimeout, cxn) ;
}
/*
* Set up the timer for the blocked reads
*/
static int prepareWriteWithTimeout (EndPoint endp,
Buffer *buffers,
EndpRWCB done,
Connection cxn)
{
/* Clear the read timer, since we can't expect a response until everything
is sent.
XXX - would be nice to have a timeout for reponses if we're sending a
string of commands. */
clearTimer (cxn->readBlockedTimerId) ;
/* set up the write timer. */
clearTimer (cxn->writeBlockedTimerId) ;
if (cxn->writeTimeout > 0)
cxn->writeBlockedTimerId = prepareSleep (writeTimeoutCbk, cxn->writeTimeout,
cxn) ;
/* set up the write. */
return prepareWrite (endp, buffers, writeProgress, done, cxn) ;
}
/*
* Does the actual deletion of a connection and all its private data.
*/
static void delConnection (Connection cxn)
{
bool shutDown;
Connection c, q;
if (cxn == NULL)
return ;
dprintf (1,"Deleting connection: %s:%d\n",
hostPeerName (cxn->myHost),cxn->ident) ;
for (c = gCxnList, q = NULL ; c != NULL ; q = c, c = c->next)
if (c == cxn)
{
if (gCxnList == c)
gCxnList = gCxnList->next ;
else
q->next = c->next ;
break ;
}
ASSERT (c != NULL) ;
if (cxn->myEp != NULL)
delEndPoint (cxn->myEp) ;
ASSERT (cxn->checkHead == NULL) ;
ASSERT (cxn->checkRespHead == NULL) ;
ASSERT (cxn->takeHead == NULL) ;
ASSERT (cxn->takeRespHead == NULL) ;
delBuffer (cxn->respBuffer) ;
/* tell the Host we're outta here. */
shutDown = hostCxnGone (cxn->myHost, cxn) ;
cxn->ident = 0 ;
cxn->timeCon = 0 ;
FREE (cxn->ipName) ;
clearTimer (cxn->artReceiptTimerId) ;
clearTimer (cxn->readBlockedTimerId) ;
clearTimer (cxn->writeBlockedTimerId) ;
clearTimer (cxn->flushTimerId) ;
FREE (cxn) ;
if (shutDown)
{
/* exit program if that was the last connexion for the last host */
/* XXX what about if there are ever multiple listeners?
XXX this will be executed if all hosts on only one of the
XXX listeners have gone */
time_t now = theTime () ;
char dateString [30] ;
register char **p = PointersFreedOnExit ;
/* finish out all outstanding memory */
while (*p)
FREE (*p++) ;
FREE (PointersFreedOnExit) ;
freeTimeoutQueue () ;
strcpy (dateString,ctime (&now)) ;
dateString [24] = '\0' ;
syslog (LOG_NOTICE,STOPPING_PROGRAM,dateString) ;
exit (0) ;
}
}
/*
* Bump up the value of the low pass filter on the connection.
*/
static void incrFilter (Connection cxn)
{
cxn->filterValue *= (1.0 - (1.0 / cxn->lowPassFilter)) ;
cxn->filterValue += 1.0 ;
}
/*
* decrement the value of the low pass filter on the connection.
*/
static void decrFilter (Connection cxn)
{
cxn->filterValue *= (1.0 - (1.0 / cxn->lowPassFilter)) ;
}
/*
* return true if we have articles we need to issue commands for.
*/
static bool writesNeeded (Connection cxn)
{
return (cxn->checkHead != NULL || cxn->takeHead != NULL ? true : false) ;
}
/*
* do some simple tests to make sure it's OK.
*/
static void validateConnection (Connection cxn)
{
u_int i ;
u_int old ;
ArtHolder p ;
i = 0 ;
/* count up the articles the Connection has and make sure that matches. */
for (p = cxn->takeHead ; p != NULL ; p = p->next)
i++ ;
dprintf (4,"TAKE queue: %d\n",i) ;
old = i ;
for (p = cxn->takeRespHead ; p != NULL ; p = p->next)
i++ ;
dprintf (4,"TAKE response queue: %d\n",i - old) ;
old = i ;
for (p = cxn->checkHead ; p != NULL ; p = p->next)
i++ ;
dprintf (4,"CHECK queue: %d\n",i - old) ;
old = i ;
for (p = cxn->checkRespHead ; p != NULL ; p = p->next)
i++ ;
dprintf (4,"CHECK response queue: %d\n",i - old) ;
ASSERT (i == cxn->articleQTotal) ;
switch (cxn->state)
{
case cxnConnectingS:
ASSERT (cxn->doesStreaming == false) ;
ASSERT (cxn->articleQTotal <= 1) ;
ASSERT (cxn->artReceiptTimerId == 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
/* ASSERT (cxn->timeCon == 0) ; */
break ;
case cxnWaitingS:
ASSERT (cxn->articleQTotal == 0) ;
ASSERT (cxn->myEp == NULL) ;
ASSERT (cxn->artReceiptTimerId == 0) ;
ASSERT (cxn->readBlockedTimerId == 0) ;
ASSERT (cxn->writeBlockedTimerId == 0) ;
ASSERT (cxn->flushTimerId == 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
ASSERT (cxn->timeCon == 0) ;
break ;
case cxnFlushingS:
case cxnClosingS:
if (!cxn->doesStreaming)
ASSERT (cxn->articleQTotal <= 1) ;
ASSERT (cxn->artReceiptTimerId == 0) ;
ASSERT (cxn->flushTimerId == 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
ASSERT (cxn->timeCon != 0) ;
ASSERT (cxn->doesStreaming || cxn->maxCheck == 1) ;
break ;
case cxnFeedingS:
if (cxn->doesStreaming)
/* Some(?) hosts return the 439 response even before we're done
sending, so don't go idle until here */
ASSERT (cxn->articleQTotal > 0 || writeIsPending (cxn->myEp)) ;
else
ASSERT (cxn->articleQTotal == 1) ;
if (cxn->readTimeout > 0 && !writeIsPending (cxn->myEp) &&
cxn->checkRespHead != NULL && cxn->takeRespHead != NULL)
ASSERT (cxn->readBlockedTimerId != 0) ;
if (cxn->writeTimeout > 0 && writeIsPending (cxn->myEp))
ASSERT (cxn->writeBlockedTimerId != 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
ASSERT (cxn->timeCon != 0) ;
ASSERT (cxn->doesStreaming || cxn->maxCheck == 1) ;
break;
case cxnIdleS:
ASSERT (cxn->articleQTotal == 0) ;
if (cxn->articleReceiptTimeout > 0)
ASSERT (cxn->artReceiptTimerId != 0) ;
ASSERT (cxn->readBlockedTimerId == 0) ;
ASSERT (cxn->writeBlockedTimerId == 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
ASSERT (cxn->timeCon != 0) ;
ASSERT (!writeIsPending (cxn->myEp)) ;
break ;
case cxnIdleTimeoutS:
ASSERT (cxn->articleQTotal == 0) ;
ASSERT (cxn->artReceiptTimerId == 0) ;
ASSERT (cxn->writeBlockedTimerId == 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
ASSERT (cxn->timeCon != 0) ;
ASSERT (!writeIsPending (cxn->myEp)) ;
break ;
case cxnSleepingS:
ASSERT (cxn->articleQTotal == 0) ;
ASSERT (cxn->myEp == NULL) ;
ASSERT (cxn->artReceiptTimerId == 0) ;
ASSERT (cxn->readBlockedTimerId == 0) ;
ASSERT (cxn->writeBlockedTimerId == 0) ;
ASSERT (cxn->flushTimerId == 0) ;
ASSERT (cxn->timeCon == 0) ;
break ;
case cxnStartingS:
ASSERT (cxn->articleQTotal == 0) ;
ASSERT (cxn->myEp == NULL) ;
ASSERT (cxn->artReceiptTimerId == 0) ;
ASSERT (cxn->readBlockedTimerId == 0) ;
ASSERT (cxn->writeBlockedTimerId == 0) ;
ASSERT (cxn->flushTimerId == 0) ;
ASSERT (cxn->sleepTimerId == 0) ;
ASSERT (cxn->timeCon == 0) ;
break ;
case cxnDeadS:
break ;
}
}
/*
* Generate a printable string of the parameter.
*/
static const char *stateToString (CxnState state)
{
static char rval [64] ;
switch (state)
{
case cxnStartingS:
strcpy (rval,"cxnStartingS") ;
break ;
case cxnWaitingS:
strcpy (rval,"cxnWaitingS") ;
break ;
case cxnConnectingS:
strcpy (rval,"cxnConnectingS") ;
break ;
case cxnIdleS:
strcpy (rval,"cxnIdleS") ;
break ;
case cxnIdleTimeoutS:
strcpy (rval,"cxnIdleTimeoutS") ;
break ;
case cxnFeedingS:
strcpy (rval,"cxnFeedingS") ;
break ;
case cxnSleepingS:
strcpy (rval,"cxnSleepingS") ;
break ;
case cxnFlushingS:
strcpy (rval,"cxnFlushingS") ;
break ;
case cxnClosingS:
strcpy (rval,"cxnClosingS") ;
break ;
case cxnDeadS:
strcpy (rval,"cxnDeadS") ;
break ;
default:
sprintf (rval,"UNKNOWN STATE: %d",state) ;
break ;
}
return rval ;
}
/****************************************************************************
*
* Functions for managing the internal queue of Articles on each Connection.
*
****************************************************************************/
static ArtHolder newArtHolder (Article article)
{
ArtHolder a = ALLOC (struct art_holder_s, 1) ;
ASSERT (a != NULL) ;
a->article = article ;
a->next = NULL ;
return a ;
}
/*
* Deletes the article holder
*/
static void delArtHolder (ArtHolder artH)
{
if (artH != NULL)
FREE (artH) ;
}
/*
* remove the article holder from the queue. Adjust the count and if nxtPtr
* points at the element then adjust that too.
*/
static bool remArtHolder (ArtHolder artH, ArtHolder *head, u_int *count)
{
ArtHolder h, i ;
ASSERT (head != NULL) ;
ASSERT (count != NULL) ;
h = *head ;
i = NULL ;
while (h != NULL && h != artH)
{
i = h ;
h = h->next ;
}
if (h == NULL)
return false ;
if (i == NULL)
*head = (*head)->next ;
else
i->next = artH->next ;
(*count)-- ;
return true ;
}
/*
* append the ArticleHolder to the queue
*/
static void appendArtHolder (ArtHolder artH, ArtHolder *head, u_int *count)
{
ArtHolder p ;
ASSERT (head != NULL) ;
ASSERT (count != NULL) ;
for (p = *head ; p != NULL && p->next != NULL ; p = p->next)
/* nada */ ;
if (p == NULL)
*head = artH ;
else
p->next = artH ;
artH->next = NULL ;
(*count)++ ;
}
/*
* find the article holder on the queue by comparing the message-id.
*/
static ArtHolder artHolderByMsgId (const char *msgid, ArtHolder head)
{
while (head != NULL)
{
if (strcmp (msgid, artMsgId (head->article)) == 0)
return head ;
head = head->next ;
}
return NULL ;
}
/*
* Randomize a numeber by the given percentage
*/
static int fudgeFactor (int initVal)
{
int newValue ;
static bool seeded ;
if ( !seeded )
{
time_t t = theTime () ;
/* this may have been done already in endpoint.c. Is that a problem??? */
srand (t) ;
seeded = true ;
}
newValue = initVal + (initVal / 10 - (rand() % (initVal / 5)));
return newValue ;
}
|