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
|
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
#include <xpap.h>
/*
*----------------------------------------------------------------------------
*
*
* Private Routines and Data
*
*
*----------------------------------------------------------------------------
*/
/* this is the head of the global list of client xpas */
static XPA xpaclienthead=NULL;
static char errbuf[SZ_LINE]; /* holds current error message */
static int id=0; /* id of current command */
#define DATA_CONNECT 1
#define DATA_ACCEPT 2
#define DATA_DATA 4
/* use of a double fork() call is used to prevent zombies which happen
if fork is a child of xpans started by an XPA server (i.e., for some
reason, the SIGCHLD signal does not get sent to xpans parent)
See Stevens, Advanced Programming in te Unix Environment, p. 202 */
#define USE_DOUBLE_FORK 1
#ifndef USE_DOUBLE_FORK
#ifdef ANSI_FUNC
void sig_chld(int signo)
#else
#endif
{
int stat;
while(waitpid(-1, &stat, WNOHANG) > 0){
;
}
return;
}
#endif
/*
*----------------------------------------------------------------------------
*
* Routine: rdl
*
* Purpose: read characters up a new-line
*
* Returns: number of characters read
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
rdl (int fd, char *buf, size_t len)
#else
static int rdl(fd, buf, len)
int fd;
char *buf;
int len;
#endif
{
int i=0;
int got;
/* start out clean */
*buf = '\0';
/* make sure we have a valid channel */
if( fd < 0 )
return(-1);
/* grab characters up to a new-line or max len */
while( i < (len-1) ){
got = read(fd, &(buf[i]), 1);
if( got < 1 )
break;
else if( buf[i++] == '\n' )
break;
}
buf[i] = '\0';
return(i);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAProxyAccept
*
* Purpose: accept a connection from an XPA proxy server
*
* Return: fd of accepted connection or -1
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static
int XPAProxyAccept(XPA xpa, char *method, char *xclass, char *name, int ifd,
unsigned int *rip, unsigned short *rport, char *rname)
#else
static
int XPAProxyAccept(xpa, method, xclass, name, ifd, rip, rport, rname)
XPA xpa;
char *method;
char *xclass;
char *name;
int ifd;
unsigned int *rip;
unsigned short *rport;
char *rname;
#endif
{
int sock;
int got;
int oum;
int ofd;
int niter;
int swidth=FD_SETSIZE;
int keep_alive=1;
int reuse_addr=1;
unsigned int ip;
unsigned short port;
char tbuf[SZ_LINE];
char amethod[SZ_LINE];
char *tptr;
socklen_t slen;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
struct timeval tv;
struct timeval *tvp;
fd_set readfds;
/* initialize results */
if( rip ) *rip = 0;
if( rport ) *rport = 0;
if( rname ) *rname = '\0';
switch(XPAMethod(method)){
case XPA_INET:
if( !XPAParseIpPort(method, &ip, &port) ){
goto error;
}
/* open a socket for data connections */
if( (sock = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
PERROR(("xpaaccept socket"));
goto error;
}
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuse_addr, sizeof(reuse_addr));
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
sock_in.sin_port = htons(port);
/* bind to the ip:port */
if( xbind(sock, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ){
PERROR(("xpaaccept bind"));
xclose(sock);
goto error;
}
snprintf(amethod, SZ_LINE, "%x:%d", ip, port);
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
ip = 0;
port = 0;
/* get filename part, composed of class and name and unique id */
snprintf(tbuf, SZ_LINE, "%s_%s.%d", xclass, name, (int)time(NULL));
/* change "/" to "_" for filename */
for(tptr = tbuf; *tptr != '\0'; tptr++){
if( *tptr == '/' ) *tptr = '_';
}
/* create full pathname */
snprintf(amethod, SZ_LINE, "%s/%s", XPATmpdir(), tbuf);
/* delete old copy */
unlink (amethod);
/* open a socket and fill in socket information */
if( (sock = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
goto error;
}
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_un, 0, sizeof(sock_un));
sock_un.sun_family = AF_UNIX;
strcpy(sock_un.sun_path, amethod);
/* unset umask so that everyone can read and write */
oum = umask(0);
/* bind to the file */
got = xbind(sock, (struct sockaddr *)&sock_un, sizeof(sock_un));
/* reset umask to previous */
umask(oum);
/* now check for bind error */
if( got < 0 ){
xclose(sock);
goto error;
}
break;
#endif
default:
goto error;
}
/* send port to client so they can connect */
/* first listen for the connection */
if( listen(sock, XPA_MAXLISTEN) < 0 ){
PERROR(("xpaaccept listen"));
xclose(sock);
goto error;
}
/* and tell the client that we are listening */
snprintf(tbuf, SZ_LINE, "xpaaccept %s (%s:%s %s)\n",
amethod, xclass, name, method);
FPRINTF((stderr, "%sXPAProxyAccept: sending command to %d:\n%s",
_sp, ifd, tbuf));
if( XPAPuts(NULL, ifd, tbuf, XPAShortTimeout()) <= 0 ){
PERROR(("client xpaaccept write"));
xclose(sock);
goto error;
}
/* we will iterate on xselect */
if( XPAShortTimeout() > 0 )
niter = XPAShortTimeout()*100;
else
niter = XPA_SHORT_TIMEOUT*100;
again:
/* this has to be able to time out */
tv.tv_sec = 0;
tv.tv_usec = 10000;
tvp = &tv;
/* wait for this socket and XPA sockets */
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
XPAAddSelect(NULL, &readfds);
/* wait for the connection */
got = xselect(swidth, &readfds, NULL, NULL, tvp);
/* process results of select */
if( got > 0 ){
if( !FD_ISSET(sock, &readfds)){
XPAProcessSelect(&readfds, 0);
goto again;
}
switch(XPAMethod(method)){
case XPA_INET:
while( 1 ){
slen = sizeof(struct sockaddr_in);
if((ofd=xaccept(sock, (struct sockaddr *)&sock_in, &slen)) >= 0){
break;
}
else{
if( xerrno == EINTR )
continue;
else{
PERROR(("xpaaccept acccept"));
xclose(sock);
goto error;
}
}
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
while( 1 ){
slen = sizeof(struct sockaddr_un);
if((ofd=xaccept(sock, (struct sockaddr *)&sock_un, &slen)) >= 0){
break;
}
else{
if( xerrno == EINTR )
continue;
else{
PERROR(("xpaaccept acccept"));
xclose(sock);
goto error;
}
}
}
break;
#endif
default:
xclose(sock);
goto error;
}
}
/* timeout? */
else if( got == 0 ){
if( --niter > 0 ){
goto again;
}
else{
xclose(sock);
FPRINTF((stderr, "%sXPAProxyAccept: select timed out\n", _sp));
goto error;
}
}
/* error */
else{
if( xerrno == EINTR ){
PERROR(("xpaaccept select"));
goto again;
}
else{
xclose(sock);
goto error;
}
}
/* done with listening */
xclose(sock);
/* fill in return information */
if( rip ) *rip = ip;
if( rport ) *rport = port;
if( rname ){
strncpy(rname, amethod, SZ_LINE-1);
rname[SZ_LINE-1] = '\0';
}
return(ofd);
error:
return(-1);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientNewInput
*
* Purpose: allocate a new input struct for reading data from stdin
*
* Return: input struct, or NULL on error
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static XPAInput
XPAClientNewInput(XPA xpa)
#else
static XPAInput XPAClientNewInput(xpa)
XPA xpa;
#endif
{
XPAInput xnew, inp;
/* allocate a new record */
if( (xnew=(XPAInput)xcalloc(1, sizeof(XPAInputRec))) == NULL ){
return(NULL);
}
/* allocate the data buffer */
xnew->buf = (char *)xmalloc(XPA_BIOSIZE);
/* this buffer starts (and currently ends) at the current byte count */
xnew->start = xpa->inpbytes;
xnew->end = xpa->inpbytes;
xnew->bytes = 0;
/* add this input to end of list of input's */
if( xpa->inphead == NULL ){
xpa->inphead = xnew;
}
else{
for(inp=xpa->inphead; inp->next!=NULL; inp=inp->next)
;
inp->next = xnew;
}
/* return the record struct */
return(xnew);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientFreeInput
*
* Purpose: free a input buffer once its been sent to all targets
*
* Return: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientFreeInput (XPA xpa, XPAInput inp)
#else
static void XPAClientFreeInput(xpa, inp)
XPA xpa;
XPAInput inp;
#endif
{
XPAInput cur;
if( !xpa || !inp )
return;
if( inp == xpa->inphead ){
xpa->inphead = inp->next;
}
else{
for(cur=xpa->inphead; cur!=NULL; cur=cur->next){
if( cur->next == inp ){
cur->next = inp->next;
break;
}
}
}
/* free current record */
if( inp != NULL ){
if( inp->buf != NULL )
xfree(inp->buf );
xfree(inp);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientFreeAllInputs
*
* Purpose: free remaining input buffers
*
* Return: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientFreeAllInputs (XPA xpa)
#else
static void XPAClientFreeAllInputs(xpa)
XPA xpa;
#endif
{
XPAInput cur, tmp;
if( !xpa )
return;
for(cur=xpa->inphead; cur!=NULL; ){
tmp = cur->next;
XPAClientFreeInput(xpa, cur);
cur = tmp;
}
xpa->inpbytes = 0;
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientProcessInput
*
* Purpose: read input from stdin and store in an input struct
*
* Return: bytes read
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientProcessInput(XPA xpa)
#else
static int XPAClientProcessInput(xpa)
XPA xpa;
#endif
{
static XPAInput cur=NULL;
int get, got;
/* set up next buffer, if necessary */
for(cur=xpa->inphead; cur!=NULL; cur=cur->next){
if( cur->bytes < XPA_BIOSIZE )
break;
}
if( cur == NULL ){
cur = XPAClientNewInput(xpa);
}
/* read data from stdin */
get = MIN(XPA_IOSIZE, XPA_BIOSIZE - cur->bytes);
if( isatty(xpa->ifd) ){
got = rdl(xpa->ifd, &(cur->buf[cur->bytes]), get);
}
else{
got = read(xpa->ifd, &(cur->buf[cur->bytes]), get);
}
switch(got){
case -1:
if( XPAVerbosity() ){
PERROR(("XPA client read"));
}
return(0);
case 0:
xpa->ifd = -1;
FPRINTF((stderr, "%sXPAClientProcessInput: signalling EOF\n", _sp));
break;
default:
break;
}
cur->bytes += got;
cur->end += got;
xpa->inpbytes += got;
#ifdef FIXEDBYCYGWIN
#if HAVE_CYGWIN
/* on non-NT Windows machines, Cygwin select() does not work once a pipe
gets EOF. It should show the fd ready for reading (and read 0 bytes),
but does not, so we have to hack a manual check */
/* GetVersion is a Windows call */
if( GetVersion() >= 0x80000000L ){
if( got < get ){
xpa->ifd = -1;
}
}
#endif
#endif
/* verify to stdout, if necessary */
if( xpa->client_mode & XPA_CLIENT_VERIFY ){
fwrite(&(cur->buf[cur->bytes-got]), sizeof(char), got, stdout);
}
/* return the number of bytes just read */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientFree
*
* Purpose: free a client record and remove from list
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientFree (XPA xpa, XPAClient client)
#else
static void XPAClientFree(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
XPAClient cur;
/* remove from list of xpa's */
if( xpa->clienthead ){
if( xpa->clienthead == client ){
xpa->clienthead = client->next;
}
else{
for(cur=xpa->clienthead; cur!=NULL; cur=cur->next){
if( cur->next == client ){
cur->next = client->next;
break;
}
}
}
}
if( client->cmdfd >= 0 ){
#if HAVE_CYGWIN
shutdown(client->cmdfd, SHUT_RDWR);
#endif
xclose(client->cmdfd);
}
if( client->datafd >= 0 ){
#if HAVE_CYGWIN
shutdown(client->datafd, SHUT_RDWR);
#endif
xclose(client->datafd);
}
if( client->dataname ){
unlink(client->dataname);
xfree(client->dataname);
}
if( client->method )
xfree(client->method);
if( client->info )
xfree(client->info);
if( client->xtemplate )
xfree(client->xtemplate);
if( client->xclass )
xfree(client->xclass);
if( client->name )
xfree(client->name);
if( client->id )
xfree(client->id);
/* xpaget's fd mode has an alloc'ed bufptr and lenptr */
if( (client->type == 'g') && (client->mode & XPA_CLIENT_FD) ){
if( client->bufptr && *(client->bufptr) )
xfree(*(client->bufptr));
if( client->bufptr )
xfree(client->bufptr);
if( client->lenptr )
xfree(client->lenptr);
}
xfree(client);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientDataSent
*
* Purpose: data is sent, so close data channel and change status to
* signal that we are waiting for the server
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientDataSent (XPA xpa, XPAClient client)
#else
static void XPAClientDataSent(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
FPRINTF((stderr, "%sXPAClientDataSent: for cmd %d data %d\n", _sp,
client->cmdfd, client->datafd));
/* close the data channel, which should trigger a result from the server */
if( client->datafd >= 0 ){
#if HAVE_CYGWIN
shutdown(client->datafd, SHUT_RDWR);
#endif
xclose(client->datafd);
client->datafd = -1;
}
/* we are now waiting for the server to complete the calllback */
client->status = XPA_CLIENT_WAITING;
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientEnd
*
* Purpose: finish up with this client
*
* Returns: error message or null
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static char *
XPAClientEnd (XPA xpa, XPAClient client)
#else
static char *XPAClientEnd(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
char *error=NULL;
char *eptr;
FPRINTF((stderr, "%sXPAClientEnd: for cmd %d data %d\n", _sp,
client->cmdfd, client->datafd));
/* always read the status line -- if we are not ack'ing, we'll get an
OK from the server before the calllback and we can exit quickly */
/* don't do this if client is xpainfo and we're not ack'ing */
if( !((client->type == 'i') && !(client->mode & XPA_CLIENT_ACK)) ){
retry:
if( XPAGets(NULL, client->cmdfd, errbuf, SZ_LINE, XPALongTimeout()) >0 ){
FPRINTF((stderr, "%sXPAClientEnd: read %s\n", _sp, errbuf));
eptr = errbuf;
/* this should never happen */
if( *eptr == '?' ){
snprintf(errbuf, SZ_LINE,
"XPA$WARNING: protocol mismatch - missing id\n%s", eptr);
error = NULL;
}
else{
/* make sure we are dealing with a proper message */
if( strncmp(eptr, client->id, strlen(client->id)) ){
if( XPAVerbosity() > 1 ){
fprintf(stderr,
"XPA$WARNING: ignoring out of sync server message:\n");
fprintf(stderr, "%s", errbuf);
}
goto retry;
}
/* go past id */
eptr += strlen(client->id);
while( isspace((int)*eptr) ) eptr++;
if( !strncmp(eptr, "XPA$OK", 6) ){
error = NULL;
}
else{
error = eptr;
}
}
}
else{
if( XPAVerbosity() > 1 ){
fprintf(stderr,
"XPA$WARNING: no reply from server callback (assuming OK)\n");
}
error = NULL;
}
}
else
error = NULL;
/* store the error return */
if( client->errptr )
*(client->errptr) = xstrdup(error);
/* remove this client if we are not meant to persist */
if( !xpa->persist ){
XPAClientFree(xpa, client);
}
/* otherwise mark as inactive */
else{
client->status = XPA_CLIENT_IDLE;
client->bytes = 0;
}
/* return error status */
return(error);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientNew
*
* Purpose: allocate a new xpa client
*
* Returns: xpa client struct
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static XPAClient
XPAClientNew (XPA xpa, char *mode, char *xtemplate, int type,
char *xclass, char *name, char *method, char *info)
#else
static XPAClient XPAClientNew(xpa, mode, xtemplate, type,
xclass, name, method, info)
XPA xpa;
char *mode;
char *xtemplate;
int type;
char *xclass;
char *name;
char *method;
char *info;
#endif
{
XPAClient xnew, client;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
char xmode[SZ_LINE];
char tbuf[SZ_LINE];
char amethod[SZ_LINE];
char *s=NULL;
unsigned short port;
unsigned int ip=0;
int fd;
int pfd;
int tries=0;
int nsproxy=0;
int keep_alive=1;
FPRINTF((stderr, "%sXPAClientNew: entering with %s %s %s %s\n", _sp,
xclass, name, method, info));
/* no errors as yet */
*errbuf = '\0';
/* look for reuse of xpans fd (used in conjunction with the xpans proxy) */
*xmode = '\0';
if( mode ){
strncpy(xmode, mode, SZ_LINE-1);
xmode[SZ_LINE-1] = '\0';
}
if( keyword(xmode, "nsproxy", tbuf, SZ_LINE) ){
nsproxy = 1;
pfd = strtol(tbuf, &s, 0);
fd = XPAProxyAccept(xpa, XPANSMethod(NULL,2),
xclass, name, pfd, &ip, &port, amethod);
/* make sure we got a valid int fd */
if( fd < 0 ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server on proxyaccept (%s:%s%s)\n",
xclass, name, XPATimestamp());
FPRINTF((stderr, "%sXPAClientNew: %s", _sp, errbuf));
PERROR(("XPAClientNew"));
return(NULL);
}
}
/* normal usage: connect to server */
else{
switch(XPAMethod(method)){
case XPA_INET:
again1:
if( !XPAParseIpPort(method, &ip, &port) )
return(NULL);
/* use $localhost over $host (we do not trust host to be correct) */
if( (ip == gethostip("$host")) && (tries == 0) )
ip = gethostip("$localhost");
/* connect to the server before we go further */
if( (fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
return(NULL);
}
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = htonl(ip);
sock_in.sin_port = htons(port);
/* make the connection with the server */
if( connect(fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) <0 ){
xclose(fd);
/* if localhost doesn't work, make one try with the host ip */
/* we also try again just in case there was an odd error such
as "permission denied", which we have seen once or twice */
if( tries < 2 ){
tries++;
goto again1;
}
/* give up */
else{
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server on connect (%s:%s%s)\n",
xclass, name, XPATimestamp());
PERROR(("XPAClientNew"));
return(NULL);
}
}
/* make sure we close on exec */
xfcntl(fd, F_SETFD, FD_CLOEXEC);
FPRINTF((stderr, "%sXPAClientNew: inet connect returns fd %d\n",
_sp, fd));
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
again2:
/* open a socket and fill in socket information */
if( (fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
return(NULL);
}
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_un, 0, sizeof(sock_un));
sock_un.sun_family = AF_UNIX;
strcpy(sock_un.sun_path, method);
/* make the connection with the server */
if( connect(fd, (struct sockaddr *)&sock_un, sizeof(sock_un)) <0 ){
xclose(fd);
/* Unix sockets get ECONNREFUSED when the listen queue is full,
so we try a few times to give the server a chance to recover */
if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){
tries++;
XPASleep(10);
goto again2;
}
/* give up */
else{
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server on connect (%s:%s%s)\n",
xclass, name, XPATimestamp());
PERROR(("XPAClientNew"));
return(NULL);
}
}
/* make sure we close on exec */
xfcntl(fd, F_SETFD, FD_CLOEXEC);
FPRINTF((stderr, "%sXPAClientNew: unix connect returns fd %d\n",
_sp, fd));
break;
#endif
default:
return(NULL);
}
strncpy(amethod, method, SZ_LINE-1);
amethod[SZ_LINE-1] = '\0';
}
/* allocate new send record */
if( (xnew=(XPAClient)xcalloc(1, sizeof(XPAClientRec))) == NULL ){
xclose(fd);
return(NULL);
}
/* fill in the blanks */
xnew->xtemplate = xstrdup(xtemplate);
xnew->type = type;
xnew->cmdfd = fd;
xnew->datafd = -1;
xnew->xclass = xstrdup(xclass);
xnew->name = xstrdup(name);
xnew->method = xstrdup(amethod);
xnew->info = xstrdup(info);
xnew->ip = ip;
xnew->nsproxy = nsproxy;
xnew->status = XPA_CLIENT_ACTIVE;
/* now that we have a valid client, add to list */
if( xpa->clienthead == NULL ){
xpa->clienthead = xnew;
}
else{
for(client=xpa->clienthead; client->next!=NULL; client=client->next)
;
client->next = xnew;
}
FPRINTF((stderr, "%sXPAClientNew: new fd %d\n", _sp, xnew->cmdfd));
/* return the good news */
return(xnew);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientAddSelect
*
* Purpose: add one or more xpa client sockets to the select flags
*
* Return: number of clients that were added to the select flags
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAClientAddSelect (XPA xpa, fd_set *readfdsptr, fd_set *writefdsptr)
#else
int XPAClientAddSelect(xpa, readfdsptr, writefdsptr)
XPA xpa;
fd_set *readfdsptr;
fd_set *writefdsptr;
#endif
{
XPAClient client;
int got=0;
int loop=0;
/* better have some place to set the flags */
if( readfdsptr == NULL )
return(0);
/* if no xpa is specified, do them all */
if( xpa == NULL ){
if( xpaclienthead == NULL ) return(0);
xpa = xpaclienthead;
loop = 1;
}
loop:
/* set select flags for all clients */
for(client=xpa->clienthead; client!=NULL; client=client->next){
/* if this client is processing */
if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd >= 0) ){
if( client->type == 'g' ){
FPRINTF((stderr, "%sXPAClientAddSelect(get): adding fd %d\n", _sp,
client->datafd));
FD_SET(client->datafd, readfdsptr);
got++;
}
else if( client->type == 's' ){
FPRINTF((stderr, "%sXPAClientAddSelect(set): adding fd %d\n", _sp,
client->datafd));
FD_SET(client->datafd, writefdsptr);
got++;
}
}
/* if this client is waiting */
else if( (client->status == XPA_CLIENT_WAITING) && (client->cmdfd >= 0) ){
FPRINTF((stderr, "%sXPAClientAddSelect(waiting): adding fd %d\n", _sp,
client->cmdfd));
FD_SET(client->cmdfd, readfdsptr);
got++;
}
}
/* loop if necessary */
if( loop && (xpa=xpa->next) ) goto loop;
/* return the news */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientGet
*
* Purpose: process an xpaget request for a given client
*
* Return: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientGet (XPA xpa, XPAClient client)
#else
static int XPAClientGet(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
int status;
char tbuf[SZ_LINE];
/* allocate the first buffer, if necessary */
if( *(client->bufptr) == NULL ){
client->bufsize = XPA_IOSIZE;
*(client->bufptr) = (char *)xmalloc(client->bufsize);
*(client->lenptr) = 0;
}
if( (*(client->lenptr) + XPA_IOSIZE) > client->bufsize ){
client->bufsize += (XPA_IOSIZE*10);
*(client->bufptr) = (char *)xrealloc(*(client->bufptr), client->bufsize);
}
/* now retrieve the data from the server */
status = recv(client->datafd, *(client->bufptr) + *(client->lenptr),
XPA_IOSIZE, 0);
/* status < 0 means error */
switch(status){
/* error */
case -1:
/* socket would block */
if((xerrno == EINPROGRESS) || (xerrno == EWOULDBLOCK)){
return(0);
}
/* clean up after error */
if( *(client->bufptr) ){
xfree(*(client->bufptr));
*(client->bufptr) = NULL;
client->bufsize = 0;
}
*(client->lenptr) = 0;
XPAClientDataSent(xpa, client);
#ifdef OLD
(void)XPAClientEnd(xpa, client);
/* we need to flag some sort of error, if nothing came across */
if( *(client->errptr) == NULL ){
*(client->errptr) = xstrdup(
"XPA$ERROR: incomplete transmission from server\n");
}
#endif
break;
/* eof */
case 0:
/* if we have multiple clients, we now need to write this one */
if( client->mode & XPA_CLIENT_FD ){
if( xpa->nclient > 1 ){
snprintf(tbuf, SZ_LINE, "XPA$BEGIN %s:%s %s\n",
client->xclass, client->name, client->method);
write(client->fd, tbuf, strlen(tbuf));
}
write(client->fd, *(client->bufptr), *(client->lenptr));
if( xpa->nclient > 1 ){
snprintf(tbuf, SZ_LINE, "XPA$END %s:%s %s\n",
client->xclass, client->name, client->method);
write(client->fd, tbuf, strlen(tbuf));
}
/* we can free buf, since its not being passed back */
if( *(client->bufptr) ){
xfree(*(client->bufptr));
*(client->bufptr) = NULL;
client->bufsize = 0;
}
}
else{
/* set final buffer size and put a convenience null at the end */
if( *(client->bufptr) ){
client->bufsize = *(client->lenptr)+1;
*(client->bufptr) = (char *)xrealloc(*(client->bufptr),client->bufsize);
*(*(client->bufptr)+*(client->lenptr)) = '\0';
}
}
/* for all clients, we need to clean up */
XPAClientDataSent(xpa, client);
#ifdef OLD
(void)XPAClientEnd(xpa, client);
#endif
break;
/* status > 0: bytes read */
default:
*(client->lenptr) += status;
/* for single client fd mode, we write immediately -- this deals with
the important case of one client with a large amount of data */
if( (client->mode & XPA_CLIENT_FD) && (xpa->nclient == 1) ){
write(client->fd, *(client->bufptr), *(client->lenptr));
/* reset buf for next read */
if( *(client->bufptr) ) xfree(*(client->bufptr));
*(client->bufptr) = NULL;
*(client->lenptr) = 0;
}
break;
}
return(status);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientSet
*
* Purpose: process an xpaset request for a given client
*
* Return: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientSet (XPA xpa, XPAClient client)
#else
static int XPAClientSet(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
int status;
int left;
int got;
int len;
XPAInput inp;
if( client->mode & XPA_CLIENT_BUF ){
while( 1 ){
len = MIN(XPA_IOSIZE, client->len - client->bytes);
/* see if we have written it all */
if( len == 0 ){
status = 1;
goto done;
}
/* write the next chunk */
FPRINTF((stderr,
"%sXPAClientSet: fd %d sending %lu bytes (%lu - %lu)\n", _sp,
client->datafd, (unsigned long)len,
(unsigned long)client->len, (unsigned long)client->bytes));
got=send(client->datafd, &(client->buf[client->bytes]), len, 0);
if( got >= 0 ){
client->bytes += got;
if( XPALevelGet() >0 )
return(got);
}
else{
PERROR(("XPAClientSet"));
/* check for error */
if( (xerrno != EWOULDBLOCK) && (xerrno != EAGAIN) ){
status = -1;
goto done;
}
/* write would block, so we return and wait the server */
else{
return(0);
}
}
}
}
/* reading from stdin and writing to servers */
else{
/* find the input buf that contains the data we need */
for(inp=xpa->inphead; inp!=NULL; inp=inp->next){
if( (client->bytes >= inp->start) && (client->bytes < inp->end) ){
break;
}
}
/* if we can't find a buffer ... */
if( !inp ){
/* ... and we have all the input, we are done */
if( xpa->ifd < 0 ){
FPRINTF((stderr, "%sXPAClientSet: all data read\n", _sp));
status = 1;
goto done;
}
/* ... but there is more input to come, return */
else{
return(0);
}
}
/* optimization: don't write a buffer until its full (or until eof) */
if( (xpa->ifd >=0) && (inp->bytes < XPA_BIOSIZE) ){
return(0);
}
/* write bytes until we would block or until end of this buffer, etc */
while( 1 ){
len = MIN(XPA_IOSIZE, inp->end - client->bytes);
FPRINTF((stderr, "%sXPAClientSet: has %lu=min(%d,(%lu-%lu)) [%d]\n",
_sp, (unsigned long)len, XPA_IOSIZE,
(unsigned long)inp->end, (unsigned long)client->bytes,
client->status));
/* if we are done with this buffer, just return */
if( (client->status == XPA_CLIENT_PROCESSING) && (len <=0) ){
/* see if everyone else is done with this buffer as well,
in which case we can free it */
left = 0;
for(client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type != 's') || !(client->mode & XPA_CLIENT_FD) )
continue;
/* in order to be totally written out, the following must be true:
* 1. send->bytes must be past the end of this buffer
* and
* 2. this buffer must be filled or else we hit eof
*/
FPRINTF((stderr,
"%sXPAClientSet: %lu>=%lu && ((%lu>=%d) or %d<0)) .. ",
_sp, client->bytes, (unsigned long)inp->end,
(unsigned long)inp->bytes, XPA_BIOSIZE,
xpa->ifd));
if( (client->bytes >= inp->end) &&
((inp->bytes >= XPA_BIOSIZE) || (xpa->ifd < 0)) ){
/* buffer complete written */
FPRINTF((stderr, "%sEOF (%lu)\n",
_sp, (unsigned long)xpa->inpbytes));
;
}
else{
FPRINTF((stderr, "%s\n", _sp));
/* buffer not complete written */
left++;
break;
}
}
/* if nothing is left, we can free this input struct */
if( !left ){
XPAClientFreeInput(xpa, inp);
}
return(0);
}
/* write to the server */
FPRINTF((stderr,
"%sXPAClientSet: fd %d sending %lu bytes (%lu):\n", _sp,
client->datafd,
(unsigned long)len, (unsigned long)client->bytes));
got = send(client->datafd, &(inp->buf[client->bytes-inp->start]),len,0);
/* check for success */
if( got >= 0 ){
/* update the number of bytes we wrote */
client->bytes += got;
FPRINTF((stderr, "%sXPAClientSet: sent %lu bytes (total is %lu)\n",
_sp, (unsigned long)got, (unsigned long)client->bytes));
/* go back for more */
if( XPALevelGet() >0 )
return(got);
else
continue;
}
/* check for error */
else{
PERROR(("XPAClientSet"));
/* anything but a "would block" error is bad */
if( (xerrno != EWOULDBLOCK) && (xerrno != EAGAIN) ){
status = -1;
goto done;
}
/* write would block, so we return and wait the server */
else{
FPRINTF((stderr, "%sXPAClientSet: waiting for more data\n", _sp));
return(0);
}
}
}
}
done:
XPAClientDataSent(xpa, client);
#ifdef OLD
(void)XPAClientEnd(xpa, client);
#endif
return(status);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientProcessSelect
*
* Purpose: process xpas that have pending reads or writes
*
* Return: number of xpas processed
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAClientProcessSelect (XPA xpa,
fd_set *readfdsptr, fd_set *writefdsptr, int maxreq)
#else
int XPAClientProcessSelect(xpa, readfdsptr, writefdsptr, maxreq)
XPA xpa;
fd_set *readfdsptr;
fd_set *writefdsptr;
int maxreq;
#endif
{
int got=0;
int loop=0;
XPAClient client;
/* <= 0 means do all of them */
if( maxreq < 0 ){
maxreq = 0;
}
/* if no xpa is specified, do them all */
if( xpa == NULL ){
if( xpaclienthead == NULL ) return(0);
xpa = xpaclienthead;
loop = 1;
}
loop:
/* first process any new input before we write output */
if( xfd_isset_stdin(xpa->ifd, readfdsptr) ){
xfd_clr_stdin(xpa->ifd, readfdsptr);
XPAClientProcessInput(xpa);
}
/* look at all clients */
again:
for(client=xpa->clienthead; client!=NULL; client=client->next){
/* if we are processing */
if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd>=0) ){
/* then handle new requests */
if((client->type == 'g') && FD_ISSET(client->datafd, readfdsptr)){
FD_CLR(client->datafd, readfdsptr);
XPAClientGet(xpa, client);
got++;
if( maxreq && (got >= maxreq) ) return(got);
goto again;
}
else if((client->type == 's') && FD_ISSET(client->datafd, writefdsptr)){
FD_CLR(client->datafd, writefdsptr);
/* if the return is > 0, we completed the send */
if( XPAClientSet(xpa, client) > 0 )
got++;
if( maxreq && (got >= maxreq) ) return(got);
goto again;
}
}
/* if this client is waiting */
else if( (client->status == XPA_CLIENT_WAITING) && (client->cmdfd >= 0) ){
if( FD_ISSET(client->cmdfd, readfdsptr)){
FD_CLR(client->cmdfd, readfdsptr);
XPAClientEnd(xpa, client);
got++;
if( maxreq && (got >= maxreq) ) return(got);
goto again;
}
}
}
/* loop if necessary */
if( loop && (xpa=xpa->next) ) goto loop;
/* return the news */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientLoop
*
* Purpose: non-X programs event loop for handling XPA client events
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientLoop (XPA xpa, int mode)
#else
static int XPAClientLoop(xpa, mode)
XPA xpa;
int mode;
#endif
{
int got=0;
int sgot;
int doxpa=1;
int ltimeout;
char *s=NULL;
fd_set readfds;
fd_set writefds;
static int width=0;
struct timeval tv;
struct timeval *tvp;
/* set width once */
if( width == 0 ){
width = FD_SETSIZE;
}
/* allow environment to turn off xpa server processing in client loop */
if( (s=getenv("XPA_CLIENT_DOXPA")) && isfalse(s) ){
doxpa = 0;
}
ltimeout = XPALongTimeout();
FD_ZERO(&readfds);
FD_ZERO(&writefds);
while( XPAClientAddSelect(xpa, &readfds, &writefds) ){
/* add other XPA's and process them as we process the client */
if( (mode & XPA_CLIENT_SEL_XPA) && doxpa ){
FPRINTF((stderr, "%sXPAClientLoop: will handle server reqs ...\n", _sp));
XPAAddSelect(NULL, &readfds);
}
/* hopefully, a server will respond in a finite amount of time */
if( ltimeout > 0 ){
tv.tv_sec = ltimeout;
tv.tv_usec = 0;
tvp = &tv;
}
/* wait forever, if necessary */
else{
tvp = NULL;
}
/* add stdin to select, if there is one */
if( xpa->ifd >= 0 ){
xfd_set_stdin(xpa->ifd, &readfds);
#if HAVE_MINGW32
/* BUT: for windows, we can't add stdin to select and therefore we
must set a short timeout and look manually */
tv.tv_sec = 0;
tv.tv_usec = 10000;
/* this is the number of window iterations we will perform */
if( ltimeout > 0 )
ltimeout *= 100;
tvp = &tv;
#endif
}
/* wait for a server to respond */
FPRINTF((stderr, "%sXPAClientLoop: waiting on select() ...\n", _sp));
sgot = xselect(width, &readfds, &writefds, NULL, tvp);
FPRINTF((stderr, "%sXPAClientLoop: select returns: %d\n", _sp, sgot));
/* error -- what should we do? */
if( sgot < 0 ){
if( xerrno == EINTR ){
FD_ZERO(&readfds);
FD_ZERO(&writefds);
continue;
}
if( XPAVerbosity() ){
perror("XPAClientLoop() select");
}
exit(1);
}
/* timed out -- no one responded */
else if( sgot == 0 ){
#if HAVE_MINGW32
if( xpa->ifd >= 0 ){
if( ltimeout > 0 ){
if( --ltimeout <= 0 )
break;
}
}
else{
break;
}
#else
break;
#endif
}
else{
got += XPAClientProcessSelect(xpa, &readfds, &writefds, 0);
if( (mode & XPA_CLIENT_SEL_XPA) && doxpa ){
got += XPAProcessSelect(&readfds, 0);
}
}
FD_ZERO(&readfds);
FD_ZERO(&writefds);
}
return(got);
}
#if HAVE_MINGW32==0
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientLoopFork
*
* Purpose: non-X programs forked event loop for handling XPA client events
*
* Returns: number of clients "processed"
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientLoopFork (XPA xpa, int mode)
#else
static int XPAClientLoopFork(xpa, mode)
XPA xpa;
int mode;
#endif
{
XPAClient client, tclient;
pid_t pid;
int got;
int fd[2];
char active=1;
#ifndef USE_DOUBLE_FORK
struct sigaction act;
/* set up the signal handler to reap children (to avoid zombies) */
act.sa_handler = sig_chld;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART;
#endif
#ifdef SA_NOCLDWAIT
act.sa_flags |= SA_NOCLDWAIT;
#endif
sigaction(SIGCHLD, &act, NULL);
#endif
if( pipe(fd) < 0 ){
got = 0;
}
else if( (pid = fork()) < 0 ){
close(fd[0]);
close(fd[1]);
got=0;
}
else if( pid == 0 ){ /* child */
/* child write to parent that he is active */
close(fd[0]);
write(fd[1], &active, 1);
close(fd[1]);
#ifdef USE_DOUBLE_FORK
/* second fork prevents zombies:
when child/parent exits, second child is inherited
by init and thus is not a child of original parent */
if( (pid = fork()) >= 0 ){
/* child/parent exits */
if( pid > 0 )
exit(0);
/* new child goes on under init ... */
}
#endif
/* enter the main loop and process */
XPAIOCallsXPA(0);
XPAClientLoop(xpa, mode);
exit(0);
} else { /* parent */
/* parent waits for child to wake up */
close(fd[1]);
read(fd[0], &active, 1);
close(fd[0]);
#ifdef USE_DOUBLE_FORK
/* for double fork, also wait for intermediate process to exit */
waitpid(pid, NULL, 0);
#endif
/* fake end of clients */
for(got=0, client=xpa->clienthead; client!=NULL; ){
got++;
tclient = client->next;
if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd >=0) ){
#if HAVE_CYGWIN
/* In Cygwin, we call shutdown (as well as close) to avoid Windows
problems. The parent can't do this since the child is using the
sockets, so we just close the sockets explicitly here */
xclose(client->datafd);
client->datafd = -1;
if( !xpa->persist ){
xclose(client->cmdfd);
client->cmdfd = -1;
}
#endif
client->errptr = NULL;
/* remove this client if we are not meant to persist */
if( !xpa->persist ){
XPAClientFree(xpa, client);
}
/* otherwise mark as inactive */
else{
client->status = XPA_CLIENT_IDLE;
client->bytes = 0;
}
}
client = tclient;
}
}
return(got);
}
#endif
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientConnect
*
* Purpose: go to name service and get new names, merge with old,
* and connect to servers
*
* Returns: number of connections
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientConnect (XPA xpa, char *mode, char *xtemplate, int type)
#else
static int XPAClientConnect(xpa, mode, xtemplate, type)
XPA xpa;
char *mode;
char *xtemplate;
int type;
#endif
{
int i;
int n;
int got;
int lp=0;
int total=0;
char **xclasses;
char **names;
char **methods;
char **infos;
char xtype[2];
char xmode[SZ_LINE];
char tbuf[SZ_LINE];
char lbuf[SZ_LINE];
XPAClient client;
/* do some initialization */
XPAInitEnv();
/* make sure we have a target */
if( !xtemplate || !*xtemplate )
return(0);
/* make a string out of the type for the lookup */
xtype[0] = type;
xtype[1] = '\0';
/* reset the number of clients we are processing */
xpa->nclient = 0;
/* look for specific proxy info */
*xmode = '\0';
if( mode ){
strncpy(xmode, mode, SZ_LINE-1);
xmode[SZ_LINE-1] = '\0';
}
if( keyword(xmode, "ns", lbuf, SZ_LINE) ){
FPRINTF((stderr, "%sXPAClientConnect: using ns info: %s\n", _sp, lbuf));
newdtable("(),");
xclasses = (char **)xmalloc(sizeof(char *));
names = (char **)xmalloc(sizeof(char *));
methods = (char **)xmalloc(sizeof(char *));
infos = (char **)xmalloc(sizeof(char *));
if( word(lbuf, tbuf, &lp) )
xclasses[0] = xstrdup(tbuf);
if( word(lbuf, tbuf, &lp) )
names[0] = xstrdup(tbuf);
if( word(lbuf, tbuf, &lp) )
methods[0] = xstrdup(tbuf);
infos[0] = xstrdup(XPA_DEF_CLIENT_INFO);
n = 1;
freedtable();
}
/* else ask xpans for access points matching the template */
else{
n = XPANSLookup(xpa,
xtemplate, xtype, &xclasses, &names, &methods, &infos);
}
/* mark existing clients who do not match this template */
for(got=0, client=xpa->clienthead; client !=NULL; client=client->next){
for(i=0; i<n; i++){
if( (client->type == type) &&
(!strcmp(client->xclass, xclasses[i])) &&
(!strcmp(client->name, names[i])) &&
(!strcmp(client->method, methods[i])) &&
(!strcmp(client->info, infos[i])) ){
got++;
}
}
/* don't unmark if its a different type -- someone else might be active */
if( !got && (client->type == type) ){
client->status = XPA_CLIENT_IDLE;
}
}
/* add new clients for this type */
for(i=0; i<n; i++){
for(got=0, client=xpa->clienthead; client !=NULL; client=client->next){
if( (client->type == type) &&
(!strcmp(client->xclass, xclasses[i])) &&
(!strcmp(client->name, names[i])) &&
(!strcmp(client->method, methods[i])) &&
(!strcmp(client->info, infos[i])) ){
/* might have to change the template */
if( strcmp(client->xtemplate, xtemplate) ){
xfree(client->xtemplate);
client->xtemplate = xstrdup(xtemplate);
}
client->status = XPA_CLIENT_ACTIVE;
got++;
total++;
FPRINTF((stderr, "%sXPAClientConnect: existing match: %s %s %s\n",
_sp, xclasses[i], names[i], methods[i]));
break;
}
}
if( !got ){
FPRINTF((stderr, "%sXPAClientConnect: calls XPAClientNew for %s:%s %s\n",
_sp, xclasses[i], names[i], methods[i]));
if( XPAClientNew(xpa, mode, xtemplate, type,
xclasses[i], names[i], methods[i], infos[i]) )
total++;
}
/* done with these strings */
xfree(xclasses[i]);
xfree(names[i]);
xfree(methods[i]);
xfree(infos[i]);
}
/* free up arrays alloc'ed by names server */
if( n > 0 ){
xfree(xclasses);
xfree(names);
xfree(methods);
xfree(infos);
}
return(total);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientStart
*
* Purpose: send init string to server and perform other authentication
* tasks
*
* Returns: 0 if success, -1 otherwise
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientStart (XPA xpa, XPAClient client, char *paramlist, char *mode)
#else
static int XPAClientStart(xpa, client, paramlist, mode)
XPA xpa;
XPAClient client;
char *paramlist;
char *mode;
#endif
{
int fd=0;
int lp=0;
int flags;
int tries=0;
int dmode=0;
int keep_alive=1;
unsigned int ip=0;
unsigned short port;
char tbuf[SZ_LINE];
char tbuf2[SZ_LINE];
char lbuf[SZ_LINE];
char *cmd=NULL;
char *method=NULL;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
switch(client->type){
case 'a':
cmd = "xpaaccess";
break;
case 'g':
cmd = "xpaget";
break;
case 'i':
cmd = "xpainfo";
break;
case 's':
cmd = "xpaset";
break;
}
/* get mode flags */
XPAMode(mode, &(client->mode), "ack", XPA_CLIENT_ACK, 1);
if( client->type == 's' )
XPAMode(mode, &(xpa->client_mode), "verify", XPA_CLIENT_VERIFY, 0);
/* package up and send the initialization message */
strcpy(lbuf, cmd);
/* set and save the id value */
snprintf(tbuf, SZ_LINE, "%c%d", client->type, id++);
if( client->id ) xfree(client->id);
client->id = xstrdup(tbuf);
/* if we are using the xpans proxy, we will want to call
accept() (server calls connect()) for the data channel */
if( client->nsproxy )
strcat(lbuf, " -a");
/* set the value of the client big-endian-ness */
snprintf(tbuf, SZ_LINE, " -e %s", XPAEndian() ? "big" : "little");
strcat(lbuf, tbuf);
snprintf(tbuf, SZ_LINE, " -i %s", client->id);
strcat(lbuf, tbuf);
if( !(client->mode & XPA_CLIENT_ACK) )
strcat(lbuf, " -n");
if( strcmp(client->info, XPA_DEF_CLIENT_INFO) ){
snprintf(tbuf, SZ_LINE, " -p %s", client->info);
strcat(lbuf, tbuf);
}
snprintf(tbuf, SZ_LINE, " %s:%s", client->xclass, client->name);
strcat(lbuf, tbuf);
if( paramlist && *paramlist ){
strcat(lbuf, " ");
strncat(lbuf, paramlist, MAX(0,(int)(SZ_LINE-(int)strlen(lbuf)-2)));
}
strcat(lbuf, "\n");
FPRINTF((stderr, "%sXPAClientStart: fd %d sends:\n%s",
_sp, client->cmdfd, lbuf));
if( XPAPuts(NULL, client->cmdfd, lbuf, XPAShortTimeout()) <= 0 ){
goto error;
}
/* if xpainfo and no ack'ing, we are basically done */
if( (client->type == 'i') && !(client->mode & XPA_CLIENT_ACK) ){
goto done;
}
/* authentication */
retry:
lp = 0;
if( XPAGets(NULL, client->cmdfd, lbuf, SZ_LINE, XPAShortTimeout()) >0 ){
FPRINTF((stderr, "%sXPAClientStart: fd %d received cmd:\n%s", _sp,
client->cmdfd, lbuf));
/* this should never happen */
if( !word(lbuf, tbuf, &lp) || (*tbuf == '?') ){
snprintf(errbuf, SZ_LINE,
"XPA$WARNING: Protocol mismatch: id\n%s", lbuf);
goto error;
}
/* make sure we are dealing with a proper message */
if( strcmp(tbuf, client->id) ){
FPRINTF((stderr, "%sXPA$WARNING: ignoring out of sync message:\n", _sp));
FPRINTF((stderr, "%s", lbuf));
if( XPAVerbosity() > 1 ){
fprintf(stderr, "XPA$WARNING: ignoring out of sync server message:\n");
fprintf(stderr, "%s", lbuf);
}
goto retry;
}
/* this should never happen */
if( !word(lbuf, tbuf, &lp) ){
snprintf(errbuf, SZ_LINE, "XPA$WARNING: missing BUF request\n%s", lbuf);
goto error;
}
if( !strcmp(tbuf, "XPA$NODATA") || !strcmp(tbuf, "XPA$NOBUF") ){
xpa->nclient += 1;
goto started;
}
/* support 2.2 (DATA) and 2.0,2.1 (BUF) */
else if( !strcmp(tbuf, "XPA$DATA") || !strcmp(tbuf, "XPA$BUF") ){
if( !strcmp(tbuf, "XPA$DATA") ){
dmode |= DATA_DATA;
if( !word(lbuf, tbuf, &lp) ){
snprintf(errbuf, SZ_LINE,
"XPA$WARNING: missing DATA request type\n%s", lbuf);
goto error;
}
if( !strcmp(tbuf, "connect") ){
method = client->method;
dmode |= DATA_CONNECT;
}
else if( !strcmp(tbuf, "accept") ){
method = client->method;
dmode |= DATA_ACCEPT;
}
else{
snprintf(errbuf, SZ_LINE,
"XPA$WARNING: invalid data connection request: %s (%s:%s)\n",
tbuf, client->xclass, client->name);
goto error;
}
}
else if( !strcmp(tbuf, "XPA$BUF") ){
if( !word(lbuf, tbuf, &lp) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: missing data buffer method (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
goto error;
}
method = tbuf;
dmode |= DATA_CONNECT;
}
/* handle connect-type requests */
if( dmode & DATA_CONNECT ){
switch(XPAMethod(method)){
case XPA_INET:
XPAParseIpPort(method, &ip, &port);
/* connect to the server before we go further */
if( (fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: bad socket for data chan (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
if( XPAVerbosity() ){
perror("XPA client socket");
}
goto error;
}
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
/* connect using the same ip we used for the command channel
(i.e., could be localhost) */
sock_in.sin_addr.s_addr = htonl(client->ip);
sock_in.sin_port = htons(port);
FPRINTF((stderr,
"%sXPAClientStart: attempting dchan connect: %s\n",
_sp, method));
if( connect(fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ){
PERROR(("dchan connect"));
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: can't connect to data chan (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
if( XPAVerbosity() ){
perror("XPA client connect");
}
xclose(fd);
goto error;
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
again:
/* open a socket and fill in socket information */
if( (fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: bad socket for data chan (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
if( XPAVerbosity() ){
perror("XPA client socket");
}
goto error;
}
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_un, 0, sizeof(sock_un));
sock_un.sun_family = AF_UNIX;
strcpy(sock_un.sun_path, method);
FPRINTF((stderr,
"%sXPAClientStart: attempting dchan connect: %s\n",
_sp, method));
if( connect(fd, (struct sockaddr *)&sock_un, sizeof(sock_un)) < 0 ){
PERROR(("dchan connect"));
xclose(fd);
if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){
tries++;
xclose(fd);
XPASleep(10);
goto again;
}
/* give up */
else{
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: can't connect to data chan (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
if( XPAVerbosity() ){
perror("XPA client connect");
}
goto error;
}
}
break;
#endif
default:
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: unknown connection method (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
goto error;
}
}
/* handle "doaccept" requests */
else if( dmode & DATA_ACCEPT ){
fd = XPAProxyAccept(xpa, XPANSMethod(NULL,2),
client->xclass, client->name, client->cmdfd,
NULL, NULL, tbuf2);
if( fd < 0 ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: can't connect to proxy server (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
goto error;
}
else if( *tbuf2 ){
client->dataname = xstrdup(tbuf2);
}
}
/* common code for either type of data connect request */
client->datafd = fd;
/* make sure we close on exec */
xfcntl(client->datafd, F_SETFD, FD_CLOEXEC);
/* for senders, set to no block mode */
if( client->type == 's' ){
/* save state and set in non-blocking mode */
xfcntl_nonblock(client->datafd, flags);
}
xpa->nclient += 1;
goto started;
}
/* handle error message */
else if( !strcmp(tbuf, "XPA$ERROR") ){
snprintf(errbuf, SZ_LINE, "%s", &lbuf[lp]);
goto error;
}
/* everything else is an error */
else{
snprintf(errbuf, SZ_LINE, "%s", &lbuf[lp]);
goto error;
}
}
else{
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server during handshake (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
goto error;
}
error:
FPRINTF((stderr, "Error in XPAClientStart: %s", errbuf));
XPAClientFree(xpa, client);
return(-1);
started:
/* it is necessary to add this this extra hack/ack between the
authentication ack and the error return (both from the server)
to avoid getting Nagle buffered. if we already did an accept,
however, we already sent a message and need not repeat it */
if( !(dmode & DATA_ACCEPT) ){
FPRINTF((stderr, "%sXPAClientStart: %d sending nagle\n",
_sp, client->cmdfd));
XPAPuts(NULL, client->cmdfd, "xpanagle\n", XPAShortTimeout());
}
/* write a "data request" to the server on the data channel, supplying
the arguments to allow the server to find the associated command */
if( dmode & DATA_DATA ){
if( !word(lbuf, tbuf, &lp) )
strcpy(tbuf, "?");
if( !word(lbuf, tbuf2, &lp) )
strcpy(tbuf2, "?");
snprintf(lbuf, SZ_LINE, "xpadata -f %s %s\n", tbuf, tbuf2);
FPRINTF((stderr,
"%sXPAClientStart: sending data channel %d request: %s", _sp,
client->datafd, lbuf));
if( XPAPuts(NULL, client->datafd, lbuf, XPAShortTimeout()) <0 ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: unable to issue data request: %s (%s:%s%s)\n",
lbuf, client->xclass, client->name, XPATimestamp());
FPRINTF((stderr, "%sXPAClientStart: error returned is %s", _sp, errbuf));
goto error;
}
}
done:
/* mark as active and started */
client->status = XPA_CLIENT_PROCESSING;
return(0);
}
/*
*----------------------------------------------------------------------------
*
*
* Public Routines
*
*
*----------------------------------------------------------------------------
*/
/*
*---------------------------------------------------------------------------
*
* Routine: XPAClientValid
*
* Purpose: see if the xpa client struct is valid
*
* Results: 1 on success, 0 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAClientValid (XPA xpa)
#else
int XPAClientValid(xpa)
XPA xpa;
#endif
{
return(_XPAValid(xpaclienthead, xpa, "c"));
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAOpen
*
* Purpose: open a persistent XPA client connection
*
* Returns: XPA struct on success
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
XPA
XPAOpen (char *mode)
#else
XPA XPAOpen(mode)
char *mode;
#endif
{
XPA xpa;
/* allocate xpa struct */
if( (xpa = (XPA)xcalloc(1, sizeof(XPARec))) == NULL )
return(NULL);
/* add version */
xpa->version = xstrdup(XPA_VERSION);
/* mark this as a client struct */
xpa->type = xstrdup("c");
/* mark as persistent so we don't destroy at the end of the transfer */
xpa->persist = 1;
/* add this xpa to end of list of client xpas */
XPAListAdd(&xpaclienthead, xpa);
return(xpa);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClose
*
* Purpose: close a persistent XPA client connection
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
void
XPAClose (XPA xpa)
#else
void XPAClose(xpa)
XPA xpa;
#endif
{
XPAClient client, tclient;
NS ns, tns;
/* ignore struct if its not a client struct */
if( !XPAClientValid(xpa) )
return;
/* remove from list of client xpas */
XPAListDel(&xpaclienthead, xpa);
/* free each remaining client */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
XPAClientFree(xpa, client);
client = tclient;
}
/* close down the name server and all of the remotes for this xpa */
for(ns=xpa->nshead; ns!=NULL; ){
tns = ns->next;
XPANSClose(xpa, ns);
ns = tns;
}
/* free string space */
if( xpa->version )
xfree(xpa->version);
if( xpa->type )
xfree(xpa->type);
if( xpa )
xfree(xpa);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAGet
*
* Purpose: get XPA values
*
* Returns: 0 for success, -1 for failure
* len bytes of data returned in buf
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAGet (XPA xpa, char *xtemplate, char *paramlist, char *mode,
char **bufs, size_t *lens, char **names, char **messages, int n)
#else
int XPAGet(xpa, xtemplate, paramlist, mode, bufs, lens, names, messages, n)
XPA xpa;
char *xtemplate;
char *paramlist;
char *mode;
char **bufs;
size_t *lens;
char **names;
char **messages;
int n;
#endif
{
int i;
int oldmode=0;
int xmode=0;
int type='g';
int idef=1;
int got=0;
char tbuf[SZ_LINE];
XPAClient client, tclient;
FPRINTF((stderr, "%sXPAGet: starting\n", _sp));
/* if not persistent, we need a temp xpa struct;
(also ignore passed struct if its not a client struct) */
if( (xpa == NULL) || strcmp(xpa->type, "c") ){
if( (xpa = XPAOpen(NULL)) == NULL )
return(-1);
/* mark this as not persistent */
xpa->persist = 0;
}
/* save xpa mode -- this call might override */
else{
/* make sure we have a valid client handle */
if( !XPAClientValid(xpa) ){
if( XPAVerbosity() ){
fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
}
return(-1);
}
oldmode = xpa->client_mode;
}
/* these arrays are required */
if( (bufs == NULL) || (lens == NULL) ){
got = -1;
goto done;
}
/* flag that we don't read from stdin */
xpa->ifd = -1;
/* zero out the return buffers */
memset((char *)bufs, 0, ABS(n)*sizeof(char *));
memset((char *)lens, 0, ABS(n)*sizeof(size_t));
if( names != NULL )
memset((char *)names, 0, ABS(n)*sizeof(char *));
if( messages != NULL )
memset((char *)messages, 0, ABS(n)*sizeof(char *));
/* connect to clients and grab data */
if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
/* retrieve data from n active clients */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
if( names != NULL ){
snprintf(tbuf, SZ_LINE, "%s:%s %s",
client->xclass, client->name, client->method);
names[got] = xstrdup(tbuf);
}
if( XPAClientStart(xpa, client, paramlist, mode) >=0 ){
/* we fill buffers */
client->mode |= XPA_CLIENT_BUF;
client->bufptr = &(bufs[got]);
client->lenptr = &(lens[got]);
if( names != NULL )
client->nameptr = &(names[got]);
if( messages != NULL )
client->errptr = &(messages[got]);
}
else{
if( messages != NULL )
messages[got] = xstrdup(errbuf);
}
got++;
}
client = tclient;
}
/* if we have active clients */
if( got ){
#if HAVE_MINGW32==0
/* check for loop modes */
XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
/* dofork implies don't do xpa */
if( xmode & XPA_CLIENT_SEL_FORK )
idef = 0;
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
if( xmode & XPA_CLIENT_SEL_FORK ){
XPAClientLoopFork(xpa, xmode);
}
else{
/* enter the main loop and process */
XPAClientLoop(xpa, xmode);
}
#else
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
XPAClientLoop(xpa, xmode);
#endif
}
}
done:
/* look for clients who timed out */
for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(i<ABS(n)) ){
i++;
if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server callback (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
messages[i] = xstrdup(errbuf);
}
}
}
/* remove this xpa if we are not meant to persist */
if( xpa && !xpa->persist )
XPAClose(xpa);
/* restore xpa mode -- this call might override */
else
xpa->client_mode = oldmode;
/* return number of clients processes (including errors) */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAGetFd
*
* Purpose: get XPA values
*
* Returns: 0 for success, -1 for failure
* len bytes of data returned in buf
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAGetFd (XPA xpa, char *xtemplate, char *paramlist, char *mode,
int *fds, char **names, char **messages, int n)
#else
int XPAGetFd(xpa, xtemplate, paramlist, mode, fds, names, messages, n)
XPA xpa;
char *xtemplate;
char *paramlist;
int *fds;
char *mode;
char **names;
char **messages;
int n;
#endif
{
int i;
int oldmode=0;
int xmode=0;
int got=0;
int type='g';
int idef=1;
char tbuf[SZ_LINE];
XPAClient client, tclient;
FPRINTF((stderr, "%sXPAGetFd: starting\n", _sp));
/* if not persistent, we need a temp xpa struct;
(also ignore passed struct if its not a client struct) */
if( (xpa == NULL) || strcmp(xpa->type, "c") ){
if( (xpa = XPAOpen(NULL)) == NULL )
return(-1);
/* mark this as not persistent */
xpa->persist = 0;
}
/* save xpa mode -- this call might override */
else{
/* make sure we have a valid client handle */
if( !XPAClientValid(xpa) ){
if( XPAVerbosity() ){
fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
}
return(-1);
}
oldmode = xpa->client_mode;
}
/* flag that we don't read from stdin */
xpa->ifd = -1;
/* zero out the return buffers */
if( names != NULL )
memset((char *)names, 0, ABS(n)*sizeof(char *));
if( messages != NULL )
memset((char *)messages, 0, ABS(n)*sizeof(char *));
/* connect to clients and grab data */
if( XPAClientConnect(xpa, mode, xtemplate, type) > 0 ){
/* retrieve data from n active clients */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
if( names != NULL ){
snprintf(tbuf, SZ_LINE, "%s:%s %s",
client->xclass, client->name, client->method);
names[got] = xstrdup(tbuf);
}
if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
/* we write to an fd */
client->mode |= XPA_CLIENT_FD;
/* negative value => one channel for all clients */
if( n < 0 )
client->fd = fds[0];
else
client->fd = fds[got];
client->bufptr = (char **)xcalloc(1, sizeof(char *));
client->lenptr = (size_t *)xcalloc(1, sizeof(size_t));
if( names != NULL )
client->nameptr = &(names[got]);
if( messages != NULL )
client->errptr = &(messages[got]);
}
else{
if( messages != NULL )
messages[got] = xstrdup(errbuf);
}
got++;
}
client = tclient;
}
/* if we have active clients */
if( got ){
#if HAVE_MINGW32==0
/* check for loop modes */
XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
/* dofork implies don't do xpa */
if( xmode & XPA_CLIENT_SEL_FORK )
idef = 0;
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
if( xmode & XPA_CLIENT_SEL_FORK ){
XPAClientLoopFork(xpa, xmode);
}
else{
/* enter the main loop and process */
XPAClientLoop(xpa, xmode);
}
#else
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
XPAClientLoop(xpa, xmode);
#endif
}
}
/* look for clients who timed out */
for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(i<ABS(n)) ){
i++;
if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server callback (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
messages[i] = xstrdup(errbuf);
}
}
}
/* remove this xpa if we are not meant to persist */
if( xpa && !xpa->persist )
XPAClose(xpa);
/* restore xpa mode -- this call might override */
else
xpa->client_mode = oldmode;
/* return number of clients processes (including errors) */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPASet
*
* Purpose: set XPA values
*
* Returns: 0 for success, -1 for failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPASet (XPA xpa, char *xtemplate, char *paramlist, char *mode,
char *buf, size_t len, char **names, char **messages, int n)
#else
int XPASet(xpa, xtemplate, paramlist, mode, buf, len, names, messages, n)
XPA xpa;
char *xtemplate;
char *paramlist;
char *mode;
char *buf;
size_t len;
char **names;
char **messages;
int n;
#endif
{
int i;
int oldmode=0;
int xmode=0;
int got=0;
int type='s';
int idef=1;
char tbuf[SZ_LINE];
XPAClient client, tclient;
FPRINTF((stderr, "%sXPASet: starting\n", _sp));
/* if not persistent, we need a temp xpa struct;
(also ignore passed struct if its not a client struct) */
if( (xpa == NULL) || strcmp(xpa->type, "c") ){
if( (xpa = XPAOpen(NULL)) == NULL )
return(-1);
/* mark this as not persistent */
xpa->persist = 0;
}
/* save xpa mode -- this call might override */
else{
/* make sure we have a valid client handle */
if( !XPAClientValid(xpa) ){
if( XPAVerbosity() ){
fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
}
return(-1);
}
oldmode = xpa->client_mode;
}
/* flag that we don't read from stdin */
xpa->ifd = -1;
/* zero out the return buffers */
if( names != NULL )
memset((char *)names, 0, ABS(n)*sizeof(char *));
if( messages != NULL )
memset((char *)messages, 0, ABS(n)*sizeof(char *));
/* connect to clients and grab data */
if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
/* retrieve data from n active clients */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
if( names != NULL ){
snprintf(tbuf, SZ_LINE, "%s:%s %s",
client->xclass, client->name, client->method);
names[got] = xstrdup(tbuf);
}
if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
/* we fill buffers */
client->mode |= XPA_CLIENT_BUF;
client->buf = buf;
client->len = len;
if( names != NULL )
client->nameptr = &(names[got]);
if( messages != NULL )
client->errptr = &(messages[got]);
}
else{
if( messages != NULL )
messages[got] = xstrdup(errbuf);
}
got++;
}
client = tclient;
}
/* if we have active clients */
if( got ){
#if HAVE_MINGW32==0
/* check for loop modes */
XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
/* dofork implies don't do xpa */
if( xmode & XPA_CLIENT_SEL_FORK )
idef = 0;
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
if( xmode & XPA_CLIENT_SEL_FORK ){
XPAClientLoopFork(xpa, xmode);
}
else{
/* enter the main loop and process */
XPAClientLoop(xpa, xmode);
}
#else
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
XPAClientLoop(xpa, xmode);
#endif
}
}
/* look for clients who timed out */
for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(i<ABS(n)) ){
i++;
if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server callback (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
messages[i] = xstrdup(errbuf);
}
}
}
/* remove this xpa if we are not meant to persist */
if( xpa && !xpa->persist )
XPAClose(xpa);
/* restore xpa mode -- this call might override */
else
xpa->client_mode = oldmode;
/* return number of clients processes (including errors) */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPASetFd
*
* Purpose: set XPA values
*
* Returns: 0 for success, -1 for failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPASetFd (XPA xpa, char *xtemplate, char *paramlist, char *mode,
int fd, char **names, char **messages, int n)
#else
int XPASetFd(xpa, xtemplate, paramlist, mode, fd, names, messages, n)
XPA xpa;
char *xtemplate;
char *paramlist;
char *mode;
int fd;
char **names;
char **messages;
int n;
#endif
{
int i;
int oldmode=0;
int xmode=0;
int got=0;
int got2=0;
int type='s';
int idef=1;
int flags;
char *s;
char tbuf[SZ_LINE];
XPAClient client, tclient;
FPRINTF((stderr, "%sXPASetFd: starting\n", _sp));
/* if not persistent, we need a temp xpa struct;
(also ignore passed struct if its not a client struct) */
if( (xpa == NULL) || strcmp(xpa->type, "c") ){
if( (xpa = XPAOpen(NULL)) == NULL )
return(-1);
/* mark this as not persistent */
xpa->persist = 0;
}
/* save xpa mode -- this call might override */
else{
/* make sure we have a valid client handle */
if( !XPAClientValid(xpa) ){
if( XPAVerbosity() ){
fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
}
return(-1);
}
oldmode = xpa->client_mode;
}
/* Set non-blocking mode for the input fd, if its not a tty */
xpa->ifd = fd;
if( isatty(xpa->ifd) == 0 ){
/* save state and set in non-blocking mode */
xfcntl_nonblock(xpa->ifd, flags);
}
/* zero out the return buffers */
if( names != NULL )
memset((char *)names, 0, ABS(n)*sizeof(char *));
if( messages != NULL )
memset((char *)messages, 0, ABS(n)*sizeof(char *));
/* connect to clients and grab data */
if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
/* open clients all at once */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
if( names != NULL ){
snprintf(tbuf, SZ_LINE, "%s:%s %s",
client->xclass, client->name, client->method);
names[got] = xstrdup(tbuf);
}
if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
/* we fill buffers */
client->mode |= XPA_CLIENT_FD;
if( names != NULL )
client->nameptr = &(names[got]);
if( messages != NULL )
client->errptr = &(messages[got]);
}
else{
if( messages != NULL )
messages[got] = xstrdup(errbuf);
}
got++;
}
client = tclient;
}
/* if we have active clients */
if( got ){
/* if fd is null, user did not want to send data, just the paramlist */
if( fd < 0 ){
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
XPAClientDataSent(xpa, client);
s = XPAClientEnd(xpa, client);
if( (messages != NULL) && (messages[got2] == NULL) )
messages[got2] = xstrdup(s);
got2++;
}
client = tclient;
}
}
else{
#if HAVE_MINGW32==0
/* check for loop modes */
XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
/* dofork implies don't do xpa */
if( xmode & XPA_CLIENT_SEL_FORK )
idef = 0;
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
if( xmode & XPA_CLIENT_SEL_FORK ){
XPAClientLoopFork(xpa, xmode);
}
else{
/* enter the main loop and process */
XPAClientLoop(xpa, xmode);
}
#else
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
XPAClientLoop(xpa, xmode);
#endif
}
}
}
/* look for clients who timed out */
for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(i<ABS(n)) ){
i++;
if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server callback (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
messages[i] = xstrdup(errbuf);
}
}
}
/* reset flags, if necessary */
if( xpa->ifd >=0 && (isatty(xpa->ifd) ==0) ){
xfcntl(xpa->ifd, F_SETFL, flags);
}
/* free all input structs */
XPAClientFreeAllInputs(xpa);
/* remove this xpa if we are not meant to persist */
if( xpa && !xpa->persist )
XPAClose(xpa);
/* restore xpa mode -- this call might override */
else
xpa->client_mode = oldmode;
/* return number of clients processes (including errors) */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAInfo
*
* Purpose: send XPA info
*
* Returns: 0 for success, -1 for failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAInfo (XPA xpa, char *xtemplate, char *paramlist, char *mode,
char **names, char **messages, int n)
#else
int XPAInfo(xpa, xtemplate, paramlist, mode, names, messages, n)
XPA xpa;
char *xtemplate;
char *paramlist;
char *mode;
char **names;
char **messages;
int n;
#endif
{
int i;
int oldmode=0;
int got=0;
char type='i';
char *s;
char tbuf[SZ_LINE];
XPAClient client, tclient;
/* if not persistent, we need a temp xpa struct;
(also ignore passed struct if its not a client struct) */
if( (xpa == NULL) || strcmp(xpa->type, "c") ){
if( (xpa = XPAOpen(NULL)) == NULL )
return(-1);
/* mark this as not persistent */
xpa->persist = 0;
}
/* save xpa mode -- this call might override */
else{
/* make sure we have a valid client handle */
if( !XPAClientValid(xpa) ){
if( XPAVerbosity() ){
fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
}
return(-1);
}
oldmode = xpa->client_mode;
}
/* flag that we don't read from stdin */
xpa->ifd = -1;
/* zero out the return buffers */
if( names != NULL )
memset((char *)names, 0, ABS(n)*sizeof(char *));
if( messages != NULL )
memset((char *)messages, 0, ABS(n)*sizeof(char *));
/* connect to clients and grab data */
if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
/* retrieve data from n active clients */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
if( names != NULL ){
snprintf(tbuf, SZ_LINE, "%s:%s %s",
client->xclass, client->name, client->method);
names[got] = xstrdup(tbuf);
}
if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
XPAClientDataSent(xpa, client);
s = XPAClientEnd(xpa, client);
if( (messages != NULL) && (messages[got] == NULL) )
messages[got] = xstrdup(s);
}
else{
if( (messages != NULL) && (messages[got] == NULL) )
messages[got] = xstrdup(errbuf);
}
got++;
}
client = tclient;
}
}
/* look for clients who timed out */
for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(i<ABS(n)) ){
i++;
if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server callback (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
messages[i] = xstrdup(errbuf);
}
}
}
/* remove this xpa if we are not meant to persist */
if( xpa && !xpa->persist )
XPAClose(xpa);
/* restore xpa mode -- this call might override */
else
xpa->client_mode = oldmode;
/* return number of clients processes (including errors) */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAAccess
*
* Purpose: determine if XPA access point is available
*
* Returns: 0 for success, -1 for failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAAccess (XPA xpa, char *xtemplate, char *paramlist, char *mode,
char **names, char **messages, int n)
#else
int XPAAccess(xpa, xtemplate, paramlist, mode, names, messages, n)
XPA xpa;
char *xtemplate;
char *paramlist;
char *mode;
char **names;
char **messages;
int n;
#endif
{
int i;
int oldmode=0;
int xmode=0;
int got=0;
int type='a';
char *s;
char *ind1, *ind2;
char tbuf[SZ_LINE];
XPAClient client, tclient;
/* if not persistent, we need a temp xpa struct;
(also ignore passed struct if its not a client struct) */
if( (xpa == NULL) || strcmp(xpa->type, "c") ){
if( (xpa = XPAOpen(NULL)) == NULL )
return(-1);
/* mark this as not persistent */
xpa->persist = 0;
}
/* save xpa mode -- this call might override */
else{
/* make sure we have a valid client handle */
if( !XPAClientValid(xpa) ){
if( XPAVerbosity() ){
fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
}
return(-1);
}
oldmode = xpa->client_mode;
}
/* flag that we don't read from stdin */
xpa->ifd = -1;
/* zero out the return buffers */
if( names != NULL )
memset((char *)names, 0, ABS(n)*sizeof(char *));
if( messages != NULL )
memset((char *)messages, 0, ABS(n)*sizeof(char *));
/* connect to clients and grab data */
if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
/* retrieve data from n active clients */
for(client=xpa->clienthead; client!=NULL; ){
tclient = client->next;
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(got<ABS(n)) ){
if( names != NULL ){
snprintf(tbuf, SZ_LINE, "%s:%s %s",
client->xclass, client->name, client->method);
names[got] = xstrdup(tbuf);
}
if( XPAClientStart(xpa, client, paramlist, mode) >=0 ){
XPAClientDataSent(xpa, client);
s = XPAClientEnd(xpa, client);
if( (messages != NULL) && (messages[got] == NULL) )
messages[got] = xstrdup(s);
}
else{
if( (messages != NULL) && (messages[got] == NULL) )
messages[got] = xstrdup(errbuf);
}
/* might have to fix the name if was an explicit mach:port */
if( names && names[got] && *errbuf &&
!strncmp(names[got], "?:?", 3) &&
(ind1=strrchr(errbuf, '(')) && (ind2=strrchr(errbuf, ')')) ){
ind1++;
strncpy(tbuf, ind1, ind2-ind1);
tbuf[ind2-ind1] = '\0';
xfree(names[got]);
names[got] = xstrdup(tbuf);
}
got++;
}
client = tclient;
}
/* if we have active clients */
if( got ){
/* check for loop modes */
XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, 1);
/* enter the main loop and process */
XPAClientLoop(xpa, xmode);
}
}
/* look for clients who timed out */
for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
(i<ABS(n)) ){
i++;
if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server callback (%s:%s%s)\n",
client->xclass, client->name, XPATimestamp());
messages[i] = xstrdup(errbuf);
}
}
}
/* remove this xpa if we are not meant to persist */
if( xpa && !xpa->persist )
XPAClose(xpa);
/* restore xpa mode -- this call might override */
else
xpa->client_mode = oldmode;
/* return number of clients processes (including errors) */
return(got);
}
|