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
|
/*
* Copyright (C) 2003,2006 Juan Lang
* Copyright (C) 2007 TransGaming Technologies Inc.
* Copyright (C) 2009 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_ALIAS_H
#include <alias.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SOCKETVAR_H
#include <sys/socketvar.h>
#endif
#ifdef HAVE_SYS_TIMEOUT_H
#include <sys/timeout.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_NET_IF_DL_H
#include <net/if_dl.h>
#endif
#ifdef HAVE_NET_IF_TYPES_H
#include <net/if_types.h>
#endif
#ifdef HAVE_NET_ROUTE_H
#include <net/route.h>
#endif
#ifdef HAVE_NET_IF_ARP_H
#include <net/if_arp.h>
#endif
#ifdef HAVE_NETINET_IF_ETHER_H
#include <netinet/if_ether.h>
#endif
#ifdef HAVE_NETINET_IF_INARP_H
#include <netinet/if_inarp.h>
#endif
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#ifdef HAVE_NETINET_IP_VAR_H
#include <netinet/ip_var.h>
#endif
#ifdef HAVE_NETINET_TCP_FSM_H
#include <netinet/tcp_fsm.h>
#endif
#ifdef HAVE_NETINET_IN_PCB_H
#include <netinet/in_pcb.h>
#endif
#ifdef HAVE_NETINET_TCP_TIMER_H
#include <netinet/tcp_timer.h>
#endif
#ifdef HAVE_NETINET_TCP_VAR_H
#include <netinet/tcp_var.h>
#endif
#ifdef HAVE_NETINET_IP_ICMP_H
#include <netinet/ip_icmp.h>
#endif
#ifdef HAVE_NETINET_ICMP_VAR_H
#include <netinet/icmp_var.h>
#endif
#ifdef HAVE_NETINET_UDP_H
#include <netinet/udp.h>
#endif
#ifdef HAVE_NETINET_UDP_VAR_H
#include <netinet/udp_var.h>
#endif
#ifdef HAVE_SYS_PROTOSW_H
#include <sys/protosw.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HAVE_KSTAT_H
#include <kstat.h>
#endif
#ifdef HAVE_INET_MIB2_H
#include <inet/mib2.h>
#endif
#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif
#ifdef HAVE_SYS_TIHDR_H
#include <sys/tihdr.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_QUEUE_H
#include <sys/queue.h>
#endif
#ifdef HAVE_SYS_USER_H
/* Make sure the definitions of struct kinfo_proc are the same. */
#include <sys/user.h>
#endif
#ifdef HAVE_LIBPROCSTAT_H
#include <libprocstat.h>
#endif
#ifdef HAVE_LIBPROC_H
#include <libproc.h>
#endif
#ifndef ROUNDUP
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#endif
#ifndef ADVANCE
#define ADVANCE(x, n) (x += ROUNDUP(((struct sockaddr *)n)->sa_len))
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#define NONAMELESSUNION
#define USE_WS_PREFIX
#include "winsock2.h"
#include "ws2ipdef.h"
#include "ifenum.h"
#include "ipstats.h"
#include "iphlpapi.h"
#include "wine/debug.h"
#include "wine/server.h"
#include "wine/unicode.h"
#ifndef HAVE_NETINET_TCP_FSM_H
#define TCPS_ESTABLISHED 1
#define TCPS_SYN_SENT 2
#define TCPS_SYN_RECEIVED 3
#define TCPS_FIN_WAIT_1 4
#define TCPS_FIN_WAIT_2 5
#define TCPS_TIME_WAIT 6
#define TCPS_CLOSED 7
#define TCPS_CLOSE_WAIT 8
#define TCPS_LAST_ACK 9
#define TCPS_LISTEN 10
#define TCPS_CLOSING 11
#endif
#ifndef RTF_MULTICAST
#define RTF_MULTICAST 0 /* Not available on NetBSD/OpenBSD */
#endif
#ifndef RTF_LLINFO
#define RTF_LLINFO 0 /* Not available on FreeBSD 8 and above */
#endif
WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
#ifdef HAVE_LIBKSTAT
static DWORD kstat_get_ui32( kstat_t *ksp, const char *name )
{
unsigned int i;
kstat_named_t *data = ksp->ks_data;
for (i = 0; i < ksp->ks_ndata; i++)
if (!strcmp( data[i].name, name )) return data[i].value.ui32;
return 0;
}
static ULONGLONG kstat_get_ui64( kstat_t *ksp, const char *name )
{
unsigned int i;
kstat_named_t *data = ksp->ks_data;
for (i = 0; i < ksp->ks_ndata; i++)
if (!strcmp( data[i].name, name )) return data[i].value.ui64;
return 0;
}
#endif
#if defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
static int open_streams_mib( const char *proto )
{
int fd;
struct strbuf buf;
struct request
{
struct T_optmgmt_req req_header;
struct opthdr opt_header;
} request;
if ((fd = open( "/dev/arp", O_RDWR )) == -1)
{
WARN( "could not open /dev/arp: %s\n", strerror(errno) );
return -1;
}
if (proto) ioctl( fd, I_PUSH, proto );
request.req_header.PRIM_type = T_SVR4_OPTMGMT_REQ;
request.req_header.OPT_length = sizeof(request.opt_header);
request.req_header.OPT_offset = FIELD_OFFSET( struct request, opt_header );
request.req_header.MGMT_flags = T_CURRENT;
request.opt_header.level = MIB2_IP;
request.opt_header.name = 0;
request.opt_header.len = 0;
buf.len = sizeof(request);
buf.buf = (caddr_t)&request;
if (putmsg( fd, &buf, NULL, 0 ) == -1)
{
WARN( "putmsg: %s\n", strerror(errno) );
close( fd );
fd = -1;
}
return fd;
}
static void *read_mib_entry( int fd, int level, int name, int *len )
{
struct strbuf buf;
void *data;
int ret, flags = 0;
struct reply
{
struct T_optmgmt_ack ack_header;
struct opthdr opt_header;
} reply;
for (;;)
{
buf.maxlen = sizeof(reply);
buf.buf = (caddr_t)&reply;
if ((ret = getmsg( fd, &buf, NULL, &flags )) < 0) return NULL;
if (!(ret & MOREDATA)) return NULL;
if (reply.ack_header.PRIM_type != T_OPTMGMT_ACK) return NULL;
if (buf.len < sizeof(reply.ack_header)) return NULL;
if (reply.ack_header.OPT_length < sizeof(reply.opt_header)) return NULL;
if (!(data = HeapAlloc( GetProcessHeap(), 0, reply.opt_header.len ))) return NULL;
buf.maxlen = reply.opt_header.len;
buf.buf = (caddr_t)data;
flags = 0;
if (getmsg( fd, NULL, &buf, &flags ) >= 0 &&
reply.opt_header.level == level &&
reply.opt_header.name == name)
{
*len = buf.len;
return data;
}
HeapFree( GetProcessHeap(), 0, data );
}
}
#endif /* HAVE_SYS_TIHDR_H && T_OPTMGMT_ACK */
DWORD getInterfaceStatsByName(const char *name, PMIB_IFROW entry)
{
DWORD ret = ERROR_NOT_SUPPORTED;
if (!name || !entry) return ERROR_INVALID_PARAMETER;
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/dev", "r")))
{
DWORD skip;
char buf[512], *ptr;
int nameLen = strlen(name);
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
while (*ptr && isspace(*ptr)) ptr++;
if (strncasecmp(ptr, name, nameLen) == 0 && *(ptr + nameLen) == ':')
{
ptr += nameLen + 1;
sscanf( ptr, "%u %u %u %u %u %u %u %u %u %u %u %u",
&entry->dwInOctets, &entry->dwInUcastPkts,
&entry->dwInErrors, &entry->dwInDiscards,
&skip, &skip, &skip,
&entry->dwInNUcastPkts, &entry->dwOutOctets,
&entry->dwOutUcastPkts, &entry->dwOutErrors,
&entry->dwOutDiscards );
break;
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#elif defined(HAVE_LIBKSTAT)
{
kstat_ctl_t *kc;
kstat_t *ksp;
if ((kc = kstat_open()) &&
(ksp = kstat_lookup( kc, NULL, -1, (char *)name )) &&
kstat_read( kc, ksp, NULL ) != -1 &&
ksp->ks_type == KSTAT_TYPE_NAMED)
{
entry->dwMtu = 1500; /* FIXME */
entry->dwSpeed = min( kstat_get_ui64( ksp, "ifspeed" ), ~0u );
entry->dwInOctets = kstat_get_ui32( ksp, "rbytes" );
entry->dwInNUcastPkts = kstat_get_ui32( ksp, "multircv" );
entry->dwInNUcastPkts += kstat_get_ui32( ksp, "brdcstrcv" );
entry->dwInUcastPkts = kstat_get_ui32( ksp, "ipackets" ) - entry->dwInNUcastPkts;
entry->dwInDiscards = kstat_get_ui32( ksp, "norcvbuf" );
entry->dwInErrors = kstat_get_ui32( ksp, "ierrors" );
entry->dwInUnknownProtos = kstat_get_ui32( ksp, "unknowns" );
entry->dwOutOctets = kstat_get_ui32( ksp, "obytes" );
entry->dwOutNUcastPkts = kstat_get_ui32( ksp, "multixmt" );
entry->dwOutNUcastPkts += kstat_get_ui32( ksp, "brdcstxmt" );
entry->dwOutUcastPkts = kstat_get_ui32( ksp, "opackets" ) - entry->dwOutNUcastPkts;
entry->dwOutDiscards = 0; /* FIXME */
entry->dwOutErrors = kstat_get_ui32( ksp, "oerrors" );
entry->dwOutQLen = kstat_get_ui32( ksp, "noxmtbuf" );
ret = NO_ERROR;
}
if (kc) kstat_close( kc );
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_IFLIST)
{
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_IFLIST, if_nametoindex(name)};
size_t needed;
char *buf = NULL, *end;
struct if_msghdr *ifm;
struct if_data ifdata;
if(sysctl(mib, ARRAY_SIZE(mib), NULL, &needed, NULL, 0) == -1)
{
ERR ("failed to get size of iflist\n");
goto done;
}
buf = HeapAlloc (GetProcessHeap (), 0, needed);
if (!buf)
{
ret = ERROR_OUTOFMEMORY;
goto done;
}
if(sysctl(mib, ARRAY_SIZE(mib), buf, &needed, NULL, 0) == -1)
{
ERR ("failed to get iflist\n");
goto done;
}
for ( end = buf + needed; buf < end; buf += ifm->ifm_msglen)
{
ifm = (struct if_msghdr *) buf;
if(ifm->ifm_type == RTM_IFINFO)
{
ifdata = ifm->ifm_data;
entry->dwMtu = ifdata.ifi_mtu;
entry->dwSpeed = ifdata.ifi_baudrate;
entry->dwInOctets = ifdata.ifi_ibytes;
entry->dwInErrors = ifdata.ifi_ierrors;
entry->dwInDiscards = ifdata.ifi_iqdrops;
entry->dwInUcastPkts = ifdata.ifi_ipackets;
entry->dwInNUcastPkts = ifdata.ifi_imcasts;
entry->dwOutOctets = ifdata.ifi_obytes;
entry->dwOutUcastPkts = ifdata.ifi_opackets;
entry->dwOutErrors = ifdata.ifi_oerrors;
ret = NO_ERROR;
break;
}
}
done:
HeapFree (GetProcessHeap (), 0, buf);
}
#else
FIXME( "unimplemented\n" );
#endif
return ret;
}
/******************************************************************
* GetIcmpStatistics (IPHLPAPI.@)
*
* Get the ICMP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for ICMP statistics
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetIcmpStatistics(PMIB_ICMP stats)
{
DWORD ret = ERROR_NOT_SUPPORTED;
if (!stats) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(MIB_ICMP) );
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp", "r")))
{
static const char hdr[] = "Icmp:";
char buf[512], *ptr;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (strncasecmp(buf, hdr, sizeof(hdr) - 1)) continue;
/* last line was a header, get another */
if (!(ptr = fgets(buf, sizeof(buf), fp))) break;
if (!strncasecmp(buf, hdr, sizeof(hdr) - 1))
{
ptr += sizeof(hdr);
sscanf( ptr, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
&stats->stats.icmpInStats.dwMsgs,
&stats->stats.icmpInStats.dwErrors,
&stats->stats.icmpInStats.dwDestUnreachs,
&stats->stats.icmpInStats.dwTimeExcds,
&stats->stats.icmpInStats.dwParmProbs,
&stats->stats.icmpInStats.dwSrcQuenchs,
&stats->stats.icmpInStats.dwRedirects,
&stats->stats.icmpInStats.dwEchoReps,
&stats->stats.icmpInStats.dwTimestamps,
&stats->stats.icmpInStats.dwTimestampReps,
&stats->stats.icmpInStats.dwAddrMasks,
&stats->stats.icmpInStats.dwAddrMaskReps,
&stats->stats.icmpOutStats.dwMsgs,
&stats->stats.icmpOutStats.dwErrors,
&stats->stats.icmpOutStats.dwDestUnreachs,
&stats->stats.icmpOutStats.dwTimeExcds,
&stats->stats.icmpOutStats.dwParmProbs,
&stats->stats.icmpOutStats.dwSrcQuenchs,
&stats->stats.icmpOutStats.dwRedirects,
&stats->stats.icmpOutStats.dwEchoReps,
&stats->stats.icmpOutStats.dwTimestamps,
&stats->stats.icmpOutStats.dwTimestampReps,
&stats->stats.icmpOutStats.dwAddrMasks,
&stats->stats.icmpOutStats.dwAddrMaskReps );
break;
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#elif defined(HAVE_LIBKSTAT)
{
static char ip[] = "ip", icmp[] = "icmp";
kstat_ctl_t *kc;
kstat_t *ksp;
if ((kc = kstat_open()) &&
(ksp = kstat_lookup( kc, ip, 0, icmp )) &&
kstat_read( kc, ksp, NULL ) != -1 &&
ksp->ks_type == KSTAT_TYPE_NAMED)
{
stats->stats.icmpInStats.dwMsgs = kstat_get_ui32( ksp, "inMsgs" );
stats->stats.icmpInStats.dwErrors = kstat_get_ui32( ksp, "inErrors" );
stats->stats.icmpInStats.dwDestUnreachs = kstat_get_ui32( ksp, "inDestUnreachs" );
stats->stats.icmpInStats.dwTimeExcds = kstat_get_ui32( ksp, "inTimeExcds" );
stats->stats.icmpInStats.dwParmProbs = kstat_get_ui32( ksp, "inParmProbs" );
stats->stats.icmpInStats.dwSrcQuenchs = kstat_get_ui32( ksp, "inSrcQuenchs" );
stats->stats.icmpInStats.dwRedirects = kstat_get_ui32( ksp, "inRedirects" );
stats->stats.icmpInStats.dwEchos = kstat_get_ui32( ksp, "inEchos" );
stats->stats.icmpInStats.dwEchoReps = kstat_get_ui32( ksp, "inEchoReps" );
stats->stats.icmpInStats.dwTimestamps = kstat_get_ui32( ksp, "inTimestamps" );
stats->stats.icmpInStats.dwTimestampReps = kstat_get_ui32( ksp, "inTimestampReps" );
stats->stats.icmpInStats.dwAddrMasks = kstat_get_ui32( ksp, "inAddrMasks" );
stats->stats.icmpInStats.dwAddrMaskReps = kstat_get_ui32( ksp, "inAddrMaskReps" );
stats->stats.icmpOutStats.dwMsgs = kstat_get_ui32( ksp, "outMsgs" );
stats->stats.icmpOutStats.dwErrors = kstat_get_ui32( ksp, "outErrors" );
stats->stats.icmpOutStats.dwDestUnreachs = kstat_get_ui32( ksp, "outDestUnreachs" );
stats->stats.icmpOutStats.dwTimeExcds = kstat_get_ui32( ksp, "outTimeExcds" );
stats->stats.icmpOutStats.dwParmProbs = kstat_get_ui32( ksp, "outParmProbs" );
stats->stats.icmpOutStats.dwSrcQuenchs = kstat_get_ui32( ksp, "outSrcQuenchs" );
stats->stats.icmpOutStats.dwRedirects = kstat_get_ui32( ksp, "outRedirects" );
stats->stats.icmpOutStats.dwEchos = kstat_get_ui32( ksp, "outEchos" );
stats->stats.icmpOutStats.dwEchoReps = kstat_get_ui32( ksp, "outEchoReps" );
stats->stats.icmpOutStats.dwTimestamps = kstat_get_ui32( ksp, "outTimestamps" );
stats->stats.icmpOutStats.dwTimestampReps = kstat_get_ui32( ksp, "outTimestampReps" );
stats->stats.icmpOutStats.dwAddrMasks = kstat_get_ui32( ksp, "outAddrMasks" );
stats->stats.icmpOutStats.dwAddrMaskReps = kstat_get_ui32( ksp, "outAddrMaskReps" );
ret = NO_ERROR;
}
if (kc) kstat_close( kc );
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(ICMPCTL_STATS) && (defined(HAVE_STRUCT_ICMPSTAT_ICPS_INHIST) || defined(HAVE_STRUCT_ICMPSTAT_ICPS_OUTHIST))
{
int mib[] = {CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS};
struct icmpstat icmp_stat;
size_t needed = sizeof(icmp_stat);
int i;
if(sysctl(mib, ARRAY_SIZE(mib), &icmp_stat, &needed, NULL, 0) != -1)
{
#ifdef HAVE_STRUCT_ICMPSTAT_ICPS_INHIST
/*in stats */
stats->stats.icmpInStats.dwMsgs = icmp_stat.icps_badcode + icmp_stat.icps_checksum + icmp_stat.icps_tooshort + icmp_stat.icps_badlen;
for(i = 0; i <= ICMP_MAXTYPE; i++)
stats->stats.icmpInStats.dwMsgs += icmp_stat.icps_inhist[i];
stats->stats.icmpInStats.dwErrors = icmp_stat.icps_badcode + icmp_stat.icps_tooshort + icmp_stat.icps_checksum + icmp_stat.icps_badlen;
stats->stats.icmpInStats.dwDestUnreachs = icmp_stat.icps_inhist[ICMP_UNREACH];
stats->stats.icmpInStats.dwTimeExcds = icmp_stat.icps_inhist[ICMP_TIMXCEED];
stats->stats.icmpInStats.dwParmProbs = icmp_stat.icps_inhist[ICMP_PARAMPROB];
stats->stats.icmpInStats.dwSrcQuenchs = icmp_stat.icps_inhist[ICMP_SOURCEQUENCH];
stats->stats.icmpInStats.dwRedirects = icmp_stat.icps_inhist[ICMP_REDIRECT];
stats->stats.icmpInStats.dwEchos = icmp_stat.icps_inhist[ICMP_ECHO];
stats->stats.icmpInStats.dwEchoReps = icmp_stat.icps_inhist[ICMP_ECHOREPLY];
stats->stats.icmpInStats.dwTimestamps = icmp_stat.icps_inhist[ICMP_TSTAMP];
stats->stats.icmpInStats.dwTimestampReps = icmp_stat.icps_inhist[ICMP_TSTAMPREPLY];
stats->stats.icmpInStats.dwAddrMasks = icmp_stat.icps_inhist[ICMP_MASKREQ];
stats->stats.icmpInStats.dwAddrMaskReps = icmp_stat.icps_inhist[ICMP_MASKREPLY];
#endif
#ifdef HAVE_STRUCT_ICMPSTAT_ICPS_OUTHIST
/* out stats */
stats->stats.icmpOutStats.dwMsgs = icmp_stat.icps_oldshort + icmp_stat.icps_oldicmp;
for(i = 0; i <= ICMP_MAXTYPE; i++)
stats->stats.icmpOutStats.dwMsgs += icmp_stat.icps_outhist[i];
stats->stats.icmpOutStats.dwErrors = icmp_stat.icps_oldshort + icmp_stat.icps_oldicmp;
stats->stats.icmpOutStats.dwDestUnreachs = icmp_stat.icps_outhist[ICMP_UNREACH];
stats->stats.icmpOutStats.dwTimeExcds = icmp_stat.icps_outhist[ICMP_TIMXCEED];
stats->stats.icmpOutStats.dwParmProbs = icmp_stat.icps_outhist[ICMP_PARAMPROB];
stats->stats.icmpOutStats.dwSrcQuenchs = icmp_stat.icps_outhist[ICMP_SOURCEQUENCH];
stats->stats.icmpOutStats.dwRedirects = icmp_stat.icps_outhist[ICMP_REDIRECT];
stats->stats.icmpOutStats.dwEchos = icmp_stat.icps_outhist[ICMP_ECHO];
stats->stats.icmpOutStats.dwEchoReps = icmp_stat.icps_outhist[ICMP_ECHOREPLY];
stats->stats.icmpOutStats.dwTimestamps = icmp_stat.icps_outhist[ICMP_TSTAMP];
stats->stats.icmpOutStats.dwTimestampReps = icmp_stat.icps_outhist[ICMP_TSTAMPREPLY];
stats->stats.icmpOutStats.dwAddrMasks = icmp_stat.icps_outhist[ICMP_MASKREQ];
stats->stats.icmpOutStats.dwAddrMaskReps = icmp_stat.icps_outhist[ICMP_MASKREPLY];
#endif /* HAVE_STRUCT_ICMPSTAT_ICPS_OUTHIST */
ret = NO_ERROR;
}
}
#else /* ICMPCTL_STATS */
FIXME( "unimplemented\n" );
#endif
return ret;
}
/******************************************************************
* GetIcmpStatisticsEx (IPHLPAPI.@)
*
* Get the IPv4 and IPv6 ICMP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for ICMP statistics
* family [In] specifies whether IPv4 or IPv6 statistics are returned
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetIcmpStatisticsEx(PMIB_ICMP_EX stats, DWORD family)
{
DWORD ret = ERROR_NOT_SUPPORTED;
MIB_ICMP ipv4stats;
if (!stats) return ERROR_INVALID_PARAMETER;
if (family != WS_AF_INET && family != WS_AF_INET6) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(MIB_ICMP_EX) );
if (family == WS_AF_INET6)
{
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp6", "r")))
{
struct icmpstatstruct{
const char *name;
DWORD pos;
};
static const struct icmpstatstruct icmpinstatlist[] = {
{ "Icmp6InDestUnreachs", ICMP6_DST_UNREACH },
{ "Icmp6InPktTooBigs", ICMP6_PACKET_TOO_BIG },
{ "Icmp6InTimeExcds", ICMP6_TIME_EXCEEDED },
{ "Icmp6InParmProblems", ICMP6_PARAM_PROB },
{ "Icmp6InEchos", ICMP6_ECHO_REQUEST },
{ "Icmp6InEchoReplies", ICMP6_ECHO_REPLY },
{ "Icmp6InGroupMembQueries", ICMP6_MEMBERSHIP_QUERY },
{ "Icmp6InGroupMembResponses", ICMP6_MEMBERSHIP_REPORT },
{ "Icmp6InGroupMembReductions", ICMP6_MEMBERSHIP_REDUCTION },
{ "Icmp6InRouterSolicits", ND_ROUTER_SOLICIT },
{ "Icmp6InRouterAdvertisements", ND_ROUTER_ADVERT },
{ "Icmp6InNeighborSolicits", ND_NEIGHBOR_SOLICIT },
{ "Icmp6InNeighborAdvertisements", ND_NEIGHBOR_ADVERT },
{ "Icmp6InRedirects", ND_REDIRECT },
{ "Icmp6InMLDv2Reports", ICMP6_V2_MEMBERSHIP_REPORT },
};
static const struct icmpstatstruct icmpoutstatlist[] = {
{ "Icmp6OutDestUnreachs", ICMP6_DST_UNREACH },
{ "Icmp6OutPktTooBigs", ICMP6_PACKET_TOO_BIG },
{ "Icmp6OutTimeExcds", ICMP6_TIME_EXCEEDED },
{ "Icmp6OutParmProblems", ICMP6_PARAM_PROB },
{ "Icmp6OutEchos", ICMP6_ECHO_REQUEST },
{ "Icmp6OutEchoReplies", ICMP6_ECHO_REPLY },
{ "Icmp6OutGroupMembQueries", ICMP6_MEMBERSHIP_QUERY },
{ "Icmp6OutGroupMembResponses", ICMP6_MEMBERSHIP_REPORT },
{ "Icmp6OutGroupMembReductions", ICMP6_MEMBERSHIP_REDUCTION },
{ "Icmp6OutRouterSolicits", ND_ROUTER_SOLICIT },
{ "Icmp6OutRouterAdvertisements", ND_ROUTER_ADVERT },
{ "Icmp6OutNeighborSolicits", ND_NEIGHBOR_SOLICIT },
{ "Icmp6OutNeighborAdvertisements", ND_NEIGHBOR_ADVERT },
{ "Icmp6OutRedirects", ND_REDIRECT },
{ "Icmp6OutMLDv2Reports", ICMP6_V2_MEMBERSHIP_REPORT },
};
char buf[512], *ptr, *value;
DWORD res, i;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (!(value = strchr(buf, ' ')))
continue;
/* terminate the valuename */
ptr = value - 1;
*(ptr + 1) = '\0';
/* and strip leading spaces from value */
value += 1;
while (*value==' ') value++;
if ((ptr = strchr(value, '\n')))
*ptr='\0';
if (!strcasecmp(buf, "Icmp6InMsgs"))
{
if (sscanf(value, "%d", &res)) stats->icmpInStats.dwMsgs = res;
continue;
}
if (!strcasecmp(buf, "Icmp6InErrors"))
{
if (sscanf(value, "%d", &res)) stats->icmpInStats.dwErrors = res;
continue;
}
for (i = 0; i < ARRAY_SIZE(icmpinstatlist); i++)
{
if (!strcasecmp(buf, icmpinstatlist[i].name))
{
if (sscanf(value, "%d", &res))
stats->icmpInStats.rgdwTypeCount[icmpinstatlist[i].pos] = res;
break;
}
}
if (!strcasecmp(buf, "Icmp6OutMsgs"))
{
if (sscanf(value, "%d", &res)) stats->icmpOutStats.dwMsgs = res;
continue;
}
if (!strcasecmp(buf, "Icmp6OutErrors"))
{
if (sscanf(value, "%d", &res)) stats->icmpOutStats.dwErrors = res;
continue;
}
for (i = 0; i < ARRAY_SIZE(icmpoutstatlist); i++)
{
if (!strcasecmp(buf, icmpoutstatlist[i].name))
{
if (sscanf(value, "%d", &res))
stats->icmpOutStats.rgdwTypeCount[icmpoutstatlist[i].pos] = res;
break;
}
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#else
FIXME( "unimplemented for IPv6\n" );
#endif
return ret;
}
ret = GetIcmpStatistics(&ipv4stats);
if (!ret)
{
stats->icmpInStats.dwMsgs = ipv4stats.stats.icmpInStats.dwMsgs;
stats->icmpInStats.dwErrors = ipv4stats.stats.icmpInStats.dwErrors;
stats->icmpInStats.rgdwTypeCount[ICMP4_DST_UNREACH] = ipv4stats.stats.icmpInStats.dwDestUnreachs;
stats->icmpInStats.rgdwTypeCount[ICMP4_SOURCE_QUENCH] = ipv4stats.stats.icmpInStats.dwSrcQuenchs;
stats->icmpInStats.rgdwTypeCount[ICMP4_REDIRECT] = ipv4stats.stats.icmpInStats.dwRedirects;
stats->icmpInStats.rgdwTypeCount[ICMP4_ECHO_REQUEST] = ipv4stats.stats.icmpInStats.dwEchos;
stats->icmpInStats.rgdwTypeCount[ICMP4_TIME_EXCEEDED] = ipv4stats.stats.icmpInStats.dwTimeExcds;
stats->icmpInStats.rgdwTypeCount[ICMP4_PARAM_PROB] = ipv4stats.stats.icmpInStats.dwParmProbs;
stats->icmpInStats.rgdwTypeCount[ICMP4_TIMESTAMP_REQUEST] = ipv4stats.stats.icmpInStats.dwTimestamps;
stats->icmpInStats.rgdwTypeCount[ICMP4_TIMESTAMP_REPLY] = ipv4stats.stats.icmpInStats.dwTimestampReps;
stats->icmpInStats.rgdwTypeCount[ICMP4_MASK_REQUEST] = ipv4stats.stats.icmpInStats.dwAddrMasks;
stats->icmpInStats.rgdwTypeCount[ICMP4_MASK_REPLY] = ipv4stats.stats.icmpInStats.dwAddrMaskReps;
stats->icmpOutStats.dwMsgs = ipv4stats.stats.icmpOutStats.dwMsgs;
stats->icmpOutStats.dwErrors = ipv4stats.stats.icmpOutStats.dwErrors;
stats->icmpOutStats.rgdwTypeCount[ICMP4_DST_UNREACH] = ipv4stats.stats.icmpOutStats.dwDestUnreachs;
stats->icmpOutStats.rgdwTypeCount[ICMP4_SOURCE_QUENCH] = ipv4stats.stats.icmpOutStats.dwSrcQuenchs;
stats->icmpOutStats.rgdwTypeCount[ICMP4_REDIRECT] = ipv4stats.stats.icmpOutStats.dwRedirects;
stats->icmpOutStats.rgdwTypeCount[ICMP4_ECHO_REQUEST] = ipv4stats.stats.icmpOutStats.dwEchos;
stats->icmpOutStats.rgdwTypeCount[ICMP4_TIME_EXCEEDED] = ipv4stats.stats.icmpOutStats.dwTimeExcds;
stats->icmpOutStats.rgdwTypeCount[ICMP4_PARAM_PROB] = ipv4stats.stats.icmpOutStats.dwParmProbs;
stats->icmpOutStats.rgdwTypeCount[ICMP4_TIMESTAMP_REQUEST] = ipv4stats.stats.icmpOutStats.dwTimestamps;
stats->icmpOutStats.rgdwTypeCount[ICMP4_TIMESTAMP_REPLY] = ipv4stats.stats.icmpOutStats.dwTimestampReps;
stats->icmpOutStats.rgdwTypeCount[ICMP4_MASK_REQUEST] = ipv4stats.stats.icmpOutStats.dwAddrMasks;
stats->icmpOutStats.rgdwTypeCount[ICMP4_MASK_REPLY] = ipv4stats.stats.icmpOutStats.dwAddrMaskReps;
}
return ret;
}
/******************************************************************
* GetIpStatisticsEx (IPHLPAPI.@)
*
* Get the IPv4 and IPv6 statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for IP statistics
* family [In] specifies whether IPv4 or IPv6 statistics are returned
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetIpStatisticsEx(PMIB_IPSTATS stats, DWORD family)
{
DWORD ret = ERROR_NOT_SUPPORTED;
MIB_IPFORWARDTABLE *fwd_table;
if (!stats) return ERROR_INVALID_PARAMETER;
if (family != WS_AF_INET && family != WS_AF_INET6) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(*stats) );
stats->dwNumIf = stats->dwNumAddr = get_interface_indices( FALSE, NULL );
if (!AllocateAndGetIpForwardTableFromStack( &fwd_table, FALSE, GetProcessHeap(), 0 ))
{
stats->dwNumRoutes = fwd_table->dwNumEntries;
HeapFree( GetProcessHeap(), 0, fwd_table );
}
if (family == WS_AF_INET6)
{
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp6", "r")))
{
struct {
const char *name;
DWORD *elem;
} ipstatlist[] = {
{ "Ip6InReceives", &stats->dwInReceives },
{ "Ip6InHdrErrors", &stats->dwInHdrErrors },
{ "Ip6InAddrErrors", &stats->dwInAddrErrors },
{ "Ip6OutForwDatagrams", &stats->dwForwDatagrams },
{ "Ip6InUnknownProtos", &stats->dwInUnknownProtos },
{ "Ip6InDiscards", &stats->dwInDiscards },
{ "Ip6InDelivers", &stats->dwInDelivers },
{ "Ip6OutRequests", &stats->dwOutRequests },
{ "Ip6OutDiscards", &stats->dwOutDiscards },
{ "Ip6OutNoRoutes", &stats->dwOutNoRoutes },
{ "Ip6ReasmTimeout", &stats->dwReasmTimeout },
{ "Ip6ReasmReqds", &stats->dwReasmReqds },
{ "Ip6ReasmOKs", &stats->dwReasmOks },
{ "Ip6ReasmFails", &stats->dwReasmFails },
{ "Ip6FragOKs", &stats->dwFragOks },
{ "Ip6FragFails", &stats->dwFragFails },
{ "Ip6FragCreates", &stats->dwFragCreates },
/* hmm, no routingDiscards, defaultTTL and forwarding? */
};
char buf[512], *ptr, *value;
DWORD res, i;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (!(value = strchr(buf, ' ')))
continue;
/* terminate the valuename */
ptr = value - 1;
*(ptr + 1) = '\0';
/* and strip leading spaces from value */
value += 1;
while (*value==' ') value++;
if ((ptr = strchr(value, '\n')))
*ptr='\0';
for (i = 0; i < ARRAY_SIZE(ipstatlist); i++)
if (!strcasecmp(buf, ipstatlist[i].name))
{
if (sscanf(value, "%d", &res)) *ipstatlist[i].elem = res;
continue;
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#else
FIXME( "unimplemented for IPv6\n" );
#endif
return ret;
}
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp", "r")))
{
static const char hdr[] = "Ip:";
char buf[512], *ptr;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (strncasecmp(buf, hdr, sizeof(hdr) - 1)) continue;
/* last line was a header, get another */
if (!(ptr = fgets(buf, sizeof(buf), fp))) break;
if (!strncasecmp(buf, hdr, sizeof(hdr) - 1))
{
ptr += sizeof(hdr);
sscanf( ptr, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
&stats->u.dwForwarding,
&stats->dwDefaultTTL,
&stats->dwInReceives,
&stats->dwInHdrErrors,
&stats->dwInAddrErrors,
&stats->dwForwDatagrams,
&stats->dwInUnknownProtos,
&stats->dwInDiscards,
&stats->dwInDelivers,
&stats->dwOutRequests,
&stats->dwOutDiscards,
&stats->dwOutNoRoutes,
&stats->dwReasmTimeout,
&stats->dwReasmReqds,
&stats->dwReasmOks,
&stats->dwReasmFails,
&stats->dwFragOks,
&stats->dwFragFails,
&stats->dwFragCreates );
/* hmm, no routingDiscards */
break;
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#elif defined(HAVE_LIBKSTAT)
{
static char ip[] = "ip";
kstat_ctl_t *kc;
kstat_t *ksp;
if ((kc = kstat_open()) &&
(ksp = kstat_lookup( kc, ip, 0, ip )) &&
kstat_read( kc, ksp, NULL ) != -1 &&
ksp->ks_type == KSTAT_TYPE_NAMED)
{
stats->u.dwForwarding = kstat_get_ui32( ksp, "forwarding" );
stats->dwDefaultTTL = kstat_get_ui32( ksp, "defaultTTL" );
stats->dwInReceives = kstat_get_ui32( ksp, "inReceives" );
stats->dwInHdrErrors = kstat_get_ui32( ksp, "inHdrErrors" );
stats->dwInAddrErrors = kstat_get_ui32( ksp, "inAddrErrors" );
stats->dwForwDatagrams = kstat_get_ui32( ksp, "forwDatagrams" );
stats->dwInUnknownProtos = kstat_get_ui32( ksp, "inUnknownProtos" );
stats->dwInDiscards = kstat_get_ui32( ksp, "inDiscards" );
stats->dwInDelivers = kstat_get_ui32( ksp, "inDelivers" );
stats->dwOutRequests = kstat_get_ui32( ksp, "outRequests" );
stats->dwRoutingDiscards = kstat_get_ui32( ksp, "routingDiscards" );
stats->dwOutDiscards = kstat_get_ui32( ksp, "outDiscards" );
stats->dwOutNoRoutes = kstat_get_ui32( ksp, "outNoRoutes" );
stats->dwReasmTimeout = kstat_get_ui32( ksp, "reasmTimeout" );
stats->dwReasmReqds = kstat_get_ui32( ksp, "reasmReqds" );
stats->dwReasmOks = kstat_get_ui32( ksp, "reasmOKs" );
stats->dwReasmFails = kstat_get_ui32( ksp, "reasmFails" );
stats->dwFragOks = kstat_get_ui32( ksp, "fragOKs" );
stats->dwFragFails = kstat_get_ui32( ksp, "fragFails" );
stats->dwFragCreates = kstat_get_ui32( ksp, "fragCreates" );
ret = NO_ERROR;
}
if (kc) kstat_close( kc );
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(IPCTL_STATS) && (defined(HAVE_STRUCT_IPSTAT_IPS_TOTAL) || defined(HAVE_STRUCT_IP_STATS_IPS_TOTAL))
{
int mib[] = {CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS};
int ip_ttl, ip_forwarding;
#if defined(HAVE_STRUCT_IPSTAT_IPS_TOTAL)
struct ipstat ip_stat;
#elif defined(HAVE_STRUCT_IP_STATS_IPS_TOTAL)
struct ip_stats ip_stat;
#endif
size_t needed;
needed = sizeof(ip_stat);
if(sysctl(mib, ARRAY_SIZE(mib), &ip_stat, &needed, NULL, 0) == -1)
{
ERR ("failed to get ipstat\n");
return ERROR_NOT_SUPPORTED;
}
needed = sizeof(ip_ttl);
if (sysctlbyname ("net.inet.ip.ttl", &ip_ttl, &needed, NULL, 0) == -1)
{
ERR ("failed to get ip Default TTL\n");
return ERROR_NOT_SUPPORTED;
}
needed = sizeof(ip_forwarding);
if (sysctlbyname ("net.inet.ip.forwarding", &ip_forwarding, &needed, NULL, 0) == -1)
{
ERR ("failed to get ip forwarding\n");
return ERROR_NOT_SUPPORTED;
}
stats->u.dwForwarding = ip_forwarding;
stats->dwDefaultTTL = ip_ttl;
stats->dwInDelivers = ip_stat.ips_delivered;
stats->dwInHdrErrors = ip_stat.ips_badhlen + ip_stat.ips_badsum + ip_stat.ips_tooshort + ip_stat.ips_badlen;
stats->dwInAddrErrors = ip_stat.ips_cantforward;
stats->dwInReceives = ip_stat.ips_total;
stats->dwForwDatagrams = ip_stat.ips_forward;
stats->dwInUnknownProtos = ip_stat.ips_noproto;
stats->dwInDiscards = ip_stat.ips_fragdropped;
stats->dwOutDiscards = ip_stat.ips_odropped;
stats->dwReasmOks = ip_stat.ips_reassembled;
stats->dwFragOks = ip_stat.ips_fragmented;
stats->dwFragFails = ip_stat.ips_cantfrag;
stats->dwReasmTimeout = ip_stat.ips_fragtimeout;
stats->dwOutNoRoutes = ip_stat.ips_noroute;
stats->dwOutRequests = ip_stat.ips_localout;
stats->dwReasmReqds = ip_stat.ips_fragments;
ret = NO_ERROR;
}
#else
FIXME( "unimplemented for IPv4\n" );
#endif
return ret;
}
/******************************************************************
* GetIpStatistics (IPHLPAPI.@)
*
* Get the IP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for IP statistics
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetIpStatistics(PMIB_IPSTATS stats)
{
return GetIpStatisticsEx(stats, WS_AF_INET);
}
/******************************************************************
* GetTcpStatisticsEx (IPHLPAPI.@)
*
* Get the IPv4 and IPv6 TCP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for TCP statistics
* family [In] specifies whether IPv4 or IPv6 statistics are returned
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetTcpStatisticsEx(PMIB_TCPSTATS stats, DWORD family)
{
DWORD ret = ERROR_NOT_SUPPORTED;
if (!stats) return ERROR_INVALID_PARAMETER;
if (family != WS_AF_INET && family != WS_AF_INET6) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(*stats) );
if (family == WS_AF_INET6)
{
FIXME( "unimplemented for IPv6\n" );
return ret;
}
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp", "r")))
{
static const char hdr[] = "Tcp:";
MIB_TCPTABLE *tcp_table;
char buf[512], *ptr;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (strncasecmp(buf, hdr, sizeof(hdr) - 1)) continue;
/* last line was a header, get another */
if (!(ptr = fgets(buf, sizeof(buf), fp))) break;
if (!strncasecmp(buf, hdr, sizeof(hdr) - 1))
{
ptr += sizeof(hdr);
sscanf( ptr, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u",
&stats->u.dwRtoAlgorithm,
&stats->dwRtoMin,
&stats->dwRtoMax,
&stats->dwMaxConn,
&stats->dwActiveOpens,
&stats->dwPassiveOpens,
&stats->dwAttemptFails,
&stats->dwEstabResets,
&stats->dwCurrEstab,
&stats->dwInSegs,
&stats->dwOutSegs,
&stats->dwRetransSegs,
&stats->dwInErrs,
&stats->dwOutRsts );
break;
}
}
if (!AllocateAndGetTcpTableFromStack( &tcp_table, FALSE, GetProcessHeap(), 0 ))
{
stats->dwNumConns = tcp_table->dwNumEntries;
HeapFree( GetProcessHeap(), 0, tcp_table );
}
fclose(fp);
ret = NO_ERROR;
}
}
#elif defined(HAVE_LIBKSTAT)
{
static char tcp[] = "tcp";
kstat_ctl_t *kc;
kstat_t *ksp;
if ((kc = kstat_open()) &&
(ksp = kstat_lookup( kc, tcp, 0, tcp )) &&
kstat_read( kc, ksp, NULL ) != -1 &&
ksp->ks_type == KSTAT_TYPE_NAMED)
{
stats->u.dwRtoAlgorithm = kstat_get_ui32( ksp, "rtoAlgorithm" );
stats->dwRtoMin = kstat_get_ui32( ksp, "rtoMin" );
stats->dwRtoMax = kstat_get_ui32( ksp, "rtoMax" );
stats->dwMaxConn = kstat_get_ui32( ksp, "maxConn" );
stats->dwActiveOpens = kstat_get_ui32( ksp, "activeOpens" );
stats->dwPassiveOpens = kstat_get_ui32( ksp, "passiveOpens" );
stats->dwAttemptFails = kstat_get_ui32( ksp, "attemptFails" );
stats->dwEstabResets = kstat_get_ui32( ksp, "estabResets" );
stats->dwCurrEstab = kstat_get_ui32( ksp, "currEstab" );
stats->dwInSegs = kstat_get_ui32( ksp, "inSegs" );
stats->dwOutSegs = kstat_get_ui32( ksp, "outSegs" );
stats->dwRetransSegs = kstat_get_ui32( ksp, "retransSegs" );
stats->dwInErrs = kstat_get_ui32( ksp, "inErrs" );
stats->dwOutRsts = kstat_get_ui32( ksp, "outRsts" );
stats->dwNumConns = kstat_get_ui32( ksp, "connTableSize" );
ret = NO_ERROR;
}
if (kc) kstat_close( kc );
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(TCPCTL_STATS) && (defined(HAVE_STRUCT_TCPSTAT_TCPS_CONNATTEMPT) || defined(HAVE_STRUCT_TCP_STATS_TCPS_CONNATTEMPT))
{
#ifndef TCPTV_MIN /* got removed in Mac OS X for some reason */
#define TCPTV_MIN 2
#define TCPTV_REXMTMAX 128
#endif
int mib[] = {CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_STATS};
#define hz 1000
#if defined(HAVE_STRUCT_TCPSTAT_TCPS_CONNATTEMPT)
struct tcpstat tcp_stat;
#elif defined(HAVE_STRUCT_TCP_STATS_TCPS_CONNATTEMPT)
struct tcp_stats tcp_stat;
#endif
size_t needed = sizeof(tcp_stat);
if(sysctl(mib, ARRAY_SIZE(mib), &tcp_stat, &needed, NULL, 0) != -1)
{
stats->u.RtoAlgorithm = MIB_TCP_RTO_VANJ;
stats->dwRtoMin = TCPTV_MIN;
stats->dwRtoMax = TCPTV_REXMTMAX;
stats->dwMaxConn = -1;
stats->dwActiveOpens = tcp_stat.tcps_connattempt;
stats->dwPassiveOpens = tcp_stat.tcps_accepts;
stats->dwAttemptFails = tcp_stat.tcps_conndrops;
stats->dwEstabResets = tcp_stat.tcps_drops;
stats->dwCurrEstab = 0;
stats->dwInSegs = tcp_stat.tcps_rcvtotal;
stats->dwOutSegs = tcp_stat.tcps_sndtotal - tcp_stat.tcps_sndrexmitpack;
stats->dwRetransSegs = tcp_stat.tcps_sndrexmitpack;
stats->dwInErrs = tcp_stat.tcps_rcvbadsum + tcp_stat.tcps_rcvbadoff + tcp_stat.tcps_rcvmemdrop + tcp_stat.tcps_rcvshort;
stats->dwOutRsts = tcp_stat.tcps_sndctrl - tcp_stat.tcps_closed;
stats->dwNumConns = tcp_stat.tcps_connects;
ret = NO_ERROR;
}
else ERR ("failed to get tcpstat\n");
}
#else
FIXME( "unimplemented\n" );
#endif
return ret;
}
/******************************************************************
* GetTcpStatistics (IPHLPAPI.@)
*
* Get the TCP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for TCP statistics
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS stats)
{
return GetTcpStatisticsEx(stats, WS_AF_INET);
}
/******************************************************************
* GetUdpStatistics (IPHLPAPI.@)
*
* Get the IPv4 and IPv6 UDP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for UDP statistics
* family [In] specifies whether IPv4 or IPv6 statistics are returned
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetUdpStatisticsEx(PMIB_UDPSTATS stats, DWORD family)
{
DWORD ret = ERROR_NOT_SUPPORTED;
if (!stats) return ERROR_INVALID_PARAMETER;
if (family != WS_AF_INET && family != WS_AF_INET6) return ERROR_INVALID_PARAMETER;
memset( stats, 0, sizeof(*stats) );
stats->dwNumAddrs = get_interface_indices( FALSE, NULL );
if (family == WS_AF_INET6)
{
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp6", "r")))
{
struct {
const char *name;
DWORD *elem;
} udpstatlist[] = {
{ "Udp6InDatagrams", &stats->dwInDatagrams },
{ "Udp6NoPorts", &stats->dwNoPorts },
{ "Udp6InErrors", &stats->dwInErrors },
{ "Udp6OutDatagrams", &stats->dwOutDatagrams },
};
char buf[512], *ptr, *value;
DWORD res, i;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (!(value = strchr(buf, ' ')))
continue;
/* terminate the valuename */
ptr = value - 1;
*(ptr + 1) = '\0';
/* and strip leading spaces from value */
value += 1;
while (*value==' ') value++;
if ((ptr = strchr(value, '\n')))
*ptr='\0';
for (i = 0; i < ARRAY_SIZE(udpstatlist); i++)
if (!strcasecmp(buf, udpstatlist[i].name))
{
if (sscanf(value, "%d", &res)) *udpstatlist[i].elem = res;
continue;
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#else
FIXME( "unimplemented for IPv6\n" );
#endif
return ret;
}
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/snmp", "r")))
{
static const char hdr[] = "Udp:";
char buf[512], *ptr;
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (strncasecmp(buf, hdr, sizeof(hdr) - 1)) continue;
/* last line was a header, get another */
if (!(ptr = fgets(buf, sizeof(buf), fp))) break;
if (!strncasecmp(buf, hdr, sizeof(hdr) - 1))
{
ptr += sizeof(hdr);
sscanf( ptr, "%u %u %u %u %u",
&stats->dwInDatagrams, &stats->dwNoPorts,
&stats->dwInErrors, &stats->dwOutDatagrams, &stats->dwNumAddrs );
break;
}
}
fclose(fp);
ret = NO_ERROR;
}
}
#elif defined(HAVE_LIBKSTAT)
{
static char udp[] = "udp";
kstat_ctl_t *kc;
kstat_t *ksp;
MIB_UDPTABLE *udp_table;
if ((kc = kstat_open()) &&
(ksp = kstat_lookup( kc, udp, 0, udp )) &&
kstat_read( kc, ksp, NULL ) != -1 &&
ksp->ks_type == KSTAT_TYPE_NAMED)
{
stats->dwInDatagrams = kstat_get_ui32( ksp, "inDatagrams" );
stats->dwNoPorts = 0; /* FIXME */
stats->dwInErrors = kstat_get_ui32( ksp, "inErrors" );
stats->dwOutDatagrams = kstat_get_ui32( ksp, "outDatagrams" );
if (!AllocateAndGetUdpTableFromStack( &udp_table, FALSE, GetProcessHeap(), 0 ))
{
stats->dwNumAddrs = udp_table->dwNumEntries;
HeapFree( GetProcessHeap(), 0, udp_table );
}
ret = NO_ERROR;
}
if (kc) kstat_close( kc );
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(UDPCTL_STATS) && defined(HAVE_STRUCT_UDPSTAT_UDPS_IPACKETS)
{
int mib[] = {CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS};
struct udpstat udp_stat;
MIB_UDPTABLE *udp_table;
size_t needed = sizeof(udp_stat);
if(sysctl(mib, ARRAY_SIZE(mib), &udp_stat, &needed, NULL, 0) != -1)
{
stats->dwInDatagrams = udp_stat.udps_ipackets;
stats->dwOutDatagrams = udp_stat.udps_opackets;
stats->dwNoPorts = udp_stat.udps_noport;
stats->dwInErrors = udp_stat.udps_hdrops + udp_stat.udps_badsum + udp_stat.udps_fullsock + udp_stat.udps_badlen;
if (!AllocateAndGetUdpTableFromStack( &udp_table, FALSE, GetProcessHeap(), 0 ))
{
stats->dwNumAddrs = udp_table->dwNumEntries;
HeapFree( GetProcessHeap(), 0, udp_table );
}
ret = NO_ERROR;
}
else ERR ("failed to get udpstat\n");
}
#else
FIXME( "unimplemented for IPv4\n" );
#endif
return ret;
}
/******************************************************************
* GetUdpStatistics (IPHLPAPI.@)
*
* Get the UDP statistics for the local computer.
*
* PARAMS
* stats [Out] buffer for UDP statistics
*
* RETURNS
* Success: NO_ERROR
* Failure: error code from winerror.h
*/
DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS stats)
{
return GetUdpStatisticsEx(stats, WS_AF_INET);
}
static MIB_IPFORWARDTABLE *append_ipforward_row( HANDLE heap, DWORD flags, MIB_IPFORWARDTABLE *table,
DWORD *count, const MIB_IPFORWARDROW *row )
{
if (table->dwNumEntries >= *count)
{
MIB_IPFORWARDTABLE *new_table;
DWORD new_count = table->dwNumEntries * 2;
if (!(new_table = HeapReAlloc( heap, flags, table,
FIELD_OFFSET(MIB_IPFORWARDTABLE, table[new_count] ))))
{
HeapFree( heap, 0, table );
return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( &table->table[table->dwNumEntries++], row, sizeof(*row) );
return table;
}
static int compare_ipforward_rows(const void *a, const void *b)
{
const MIB_IPFORWARDROW *rowA = a;
const MIB_IPFORWARDROW *rowB = b;
int ret;
if ((ret = rowA->dwForwardDest - rowB->dwForwardDest) != 0) return ret;
if ((ret = rowA->u2.dwForwardProto - rowB->u2.dwForwardProto) != 0) return ret;
if ((ret = rowA->dwForwardPolicy - rowB->dwForwardPolicy) != 0) return ret;
return rowA->dwForwardNextHop - rowB->dwForwardNextHop;
}
/******************************************************************
* AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
*
* Get the route table.
* Like GetIpForwardTable(), but allocate the returned table from heap.
*
* PARAMS
* ppIpForwardTable [Out] pointer into which the MIB_IPFORWARDTABLE is
* allocated and returned.
* bOrder [In] whether to sort the table
* heap [In] heap from which the table is allocated
* flags [In] flags to HeapAlloc
*
* RETURNS
* ERROR_INVALID_PARAMETER if ppIfTable is NULL, other error codes
* on failure, NO_ERROR on success.
*/
DWORD WINAPI AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE *ppIpForwardTable, BOOL bOrder,
HANDLE heap, DWORD flags)
{
MIB_IPFORWARDTABLE *table;
MIB_IPFORWARDROW row;
DWORD ret = NO_ERROR, count = 16;
TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppIpForwardTable, bOrder, heap, flags);
if (!ppIpForwardTable) return ERROR_INVALID_PARAMETER;
if (!(table = HeapAlloc( heap, flags, FIELD_OFFSET(MIB_IPFORWARDTABLE, table[count] ))))
return ERROR_OUTOFMEMORY;
table->dwNumEntries = 0;
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/route", "r")))
{
char buf[512], *ptr;
DWORD flags;
/* skip header line */
ptr = fgets(buf, sizeof(buf), fp);
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
memset( &row, 0, sizeof(row) );
while (!isspace(*ptr)) ptr++;
*ptr++ = 0;
if (getInterfaceIndexByName(buf, &row.dwForwardIfIndex) != NO_ERROR)
continue;
row.dwForwardDest = strtoul(ptr, &ptr, 16);
row.dwForwardNextHop = strtoul(ptr + 1, &ptr, 16);
flags = strtoul(ptr + 1, &ptr, 16);
if (!(flags & RTF_UP)) row.u1.ForwardType = MIB_IPROUTE_TYPE_INVALID;
else if (flags & RTF_GATEWAY) row.u1.ForwardType = MIB_IPROUTE_TYPE_INDIRECT;
else row.u1.ForwardType = MIB_IPROUTE_TYPE_DIRECT;
strtoul(ptr + 1, &ptr, 16); /* refcount, skip */
strtoul(ptr + 1, &ptr, 16); /* use, skip */
row.dwForwardMetric1 = strtoul(ptr + 1, &ptr, 16);
row.dwForwardMask = strtoul(ptr + 1, &ptr, 16);
/* FIXME: other protos might be appropriate, e.g. the default
* route is typically set with MIB_IPPROTO_NETMGMT instead */
row.u2.ForwardProto = MIB_IPPROTO_LOCAL;
if (!(table = append_ipforward_row( heap, flags, table, &count, &row )))
break;
}
fclose(fp);
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
{
void *data;
int fd, len, namelen;
mib2_ipRouteEntry_t *entry;
char name[64];
if ((fd = open_streams_mib( NULL )) != -1)
{
if ((data = read_mib_entry( fd, MIB2_IP, MIB2_IP_ROUTE, &len )))
{
for (entry = data; (char *)(entry + 1) <= (char *)data + len; entry++)
{
row.dwForwardDest = entry->ipRouteDest;
row.dwForwardMask = entry->ipRouteMask;
row.dwForwardPolicy = 0;
row.dwForwardNextHop = entry->ipRouteNextHop;
row.u1.dwForwardType = entry->ipRouteType;
row.u2.dwForwardProto = entry->ipRouteProto;
row.dwForwardAge = entry->ipRouteAge;
row.dwForwardNextHopAS = 0;
row.dwForwardMetric1 = entry->ipRouteMetric1;
row.dwForwardMetric2 = entry->ipRouteMetric2;
row.dwForwardMetric3 = entry->ipRouteMetric3;
row.dwForwardMetric4 = entry->ipRouteMetric4;
row.dwForwardMetric5 = entry->ipRouteMetric5;
namelen = min( sizeof(name) - 1, entry->ipRouteIfIndex.o_length );
memcpy( name, entry->ipRouteIfIndex.o_bytes, namelen );
name[namelen] = 0;
getInterfaceIndexByName( name, &row.dwForwardIfIndex );
if (!(table = append_ipforward_row( heap, flags, table, &count, &row ))) break;
}
HeapFree( GetProcessHeap(), 0, data );
}
close( fd );
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP)
{
int mib[6] = {CTL_NET, PF_ROUTE, 0, PF_INET, NET_RT_DUMP, 0};
size_t needed;
char *buf = NULL, *lim, *next, *addrPtr;
struct rt_msghdr *rtm;
if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
{
ERR ("sysctl 1 failed!\n");
ret = ERROR_NOT_SUPPORTED;
goto done;
}
buf = HeapAlloc (GetProcessHeap (), 0, needed);
if (!buf)
{
ret = ERROR_OUTOFMEMORY;
goto done;
}
if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0)
{
ret = ERROR_NOT_SUPPORTED;
goto done;
}
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen)
{
int i;
rtm = (struct rt_msghdr *)next;
if (rtm->rtm_type != RTM_GET)
{
WARN ("Got unexpected message type 0x%x!\n",
rtm->rtm_type);
continue;
}
/* Ignore gateway routes which are multicast */
if ((rtm->rtm_flags & RTF_GATEWAY) && (rtm->rtm_flags & RTF_MULTICAST))
continue;
memset( &row, 0, sizeof(row) );
row.dwForwardIfIndex = rtm->rtm_index;
row.u1.ForwardType = (rtm->rtm_flags & RTF_GATEWAY) ? MIB_IPROUTE_TYPE_INDIRECT : MIB_IPROUTE_TYPE_DIRECT;
row.dwForwardMetric1 = rtm->rtm_rmx.rmx_hopcount;
row.u2.ForwardProto = MIB_IPPROTO_LOCAL;
addrPtr = (char *)(rtm + 1);
for (i = 1; i; i <<= 1)
{
struct sockaddr *sa;
DWORD addr;
if (!(i & rtm->rtm_addrs))
continue;
sa = (struct sockaddr *)addrPtr;
ADVANCE (addrPtr, sa);
/* default routes are encoded by length-zero sockaddr */
if (sa->sa_len == 0) {
addr = 0;
}else {
switch(sa->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
addr = sin->sin_addr.s_addr;
break;
}
#ifdef AF_LINK
case AF_LINK:
if(i == RTA_GATEWAY && row.u1.ForwardType == MIB_IPROUTE_TYPE_DIRECT) {
/* For direct route we may simply use dest addr as next hop */
C_ASSERT(RTA_DST < RTA_GATEWAY);
addr = row.dwForwardDest;
break;
}
/* fallthrough */
#endif
default:
WARN ("Received unsupported sockaddr family 0x%x\n", sa->sa_family);
addr = 0;
}
}
switch (i)
{
case RTA_DST: row.dwForwardDest = addr; break;
case RTA_GATEWAY: row.dwForwardNextHop = addr; break;
case RTA_NETMASK: row.dwForwardMask = addr; break;
default:
WARN ("Unexpected address type 0x%x\n", i);
}
}
if (!(table = append_ipforward_row( heap, flags, table, &count, &row )))
break;
}
done:
HeapFree( GetProcessHeap (), 0, buf );
}
#else
FIXME( "not implemented\n" );
ret = ERROR_NOT_SUPPORTED;
#endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret)
{
if (bOrder && table->dwNumEntries)
qsort( table->table, table->dwNumEntries, sizeof(row), compare_ipforward_rows );
*ppIpForwardTable = table;
}
else HeapFree( heap, flags, table );
TRACE( "returning ret %u table %p\n", ret, table );
return ret;
}
static MIB_IPNETTABLE *append_ipnet_row( HANDLE heap, DWORD flags, MIB_IPNETTABLE *table,
DWORD *count, const MIB_IPNETROW *row )
{
if (table->dwNumEntries >= *count)
{
MIB_IPNETTABLE *new_table;
DWORD new_count = table->dwNumEntries * 2;
if (!(new_table = HeapReAlloc( heap, flags, table,
FIELD_OFFSET(MIB_IPNETTABLE, table[new_count] ))))
{
HeapFree( heap, 0, table );
return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( &table->table[table->dwNumEntries++], row, sizeof(*row) );
return table;
}
static int compare_ipnet_rows(const void *a, const void *b)
{
const MIB_IPNETROW *rowA = a;
const MIB_IPNETROW *rowB = b;
return ntohl(rowA->dwAddr) - ntohl(rowB->dwAddr);
}
/******************************************************************
* AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
*
* Get the IP-to-physical address mapping table.
* Like GetIpNetTable(), but allocate the returned table from heap.
*
* PARAMS
* ppIpNetTable [Out] pointer into which the MIB_IPNETTABLE is
* allocated and returned.
* bOrder [In] whether to sort the table
* heap [In] heap from which the table is allocated
* flags [In] flags to HeapAlloc
*
* RETURNS
* ERROR_INVALID_PARAMETER if ppIpNetTable is NULL, other error codes
* on failure, NO_ERROR on success.
*/
DWORD WINAPI AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE *ppIpNetTable, BOOL bOrder,
HANDLE heap, DWORD flags)
{
MIB_IPNETTABLE *table;
MIB_IPNETROW row;
DWORD ret = NO_ERROR, count = 16;
TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppIpNetTable, bOrder, heap, flags);
if (!ppIpNetTable) return ERROR_INVALID_PARAMETER;
if (!(table = HeapAlloc( heap, flags, FIELD_OFFSET(MIB_IPNETTABLE, table[count] ))))
return ERROR_OUTOFMEMORY;
table->dwNumEntries = 0;
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/arp", "r")))
{
char buf[512], *ptr;
DWORD flags;
/* skip header line */
ptr = fgets(buf, sizeof(buf), fp);
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
memset( &row, 0, sizeof(row) );
row.dwAddr = inet_addr(ptr);
while (*ptr && !isspace(*ptr)) ptr++;
strtoul(ptr + 1, &ptr, 16); /* hw type (skip) */
flags = strtoul(ptr + 1, &ptr, 16);
#ifdef ATF_COM
if (flags & ATF_COM) row.u.Type = MIB_IPNET_TYPE_DYNAMIC;
else
#endif
#ifdef ATF_PERM
if (flags & ATF_PERM) row.u.Type = MIB_IPNET_TYPE_STATIC;
else
#endif
row.u.Type = MIB_IPNET_TYPE_OTHER;
while (*ptr && isspace(*ptr)) ptr++;
while (*ptr && !isspace(*ptr))
{
row.bPhysAddr[row.dwPhysAddrLen++] = strtoul(ptr, &ptr, 16);
if (*ptr) ptr++;
}
while (*ptr && isspace(*ptr)) ptr++;
while (*ptr && !isspace(*ptr)) ptr++; /* mask (skip) */
while (*ptr && isspace(*ptr)) ptr++;
getInterfaceIndexByName(ptr, &row.dwIndex);
if (!(table = append_ipnet_row( heap, flags, table, &count, &row )))
break;
}
fclose(fp);
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
{
void *data;
int fd, len, namelen;
mib2_ipNetToMediaEntry_t *entry;
char name[64];
if ((fd = open_streams_mib( NULL )) != -1)
{
if ((data = read_mib_entry( fd, MIB2_IP, MIB2_IP_MEDIA, &len )))
{
for (entry = data; (char *)(entry + 1) <= (char *)data + len; entry++)
{
row.dwPhysAddrLen = min( entry->ipNetToMediaPhysAddress.o_length, MAXLEN_PHYSADDR );
memcpy( row.bPhysAddr, entry->ipNetToMediaPhysAddress.o_bytes, row.dwPhysAddrLen );
row.dwAddr = entry->ipNetToMediaNetAddress;
row.u.Type = entry->ipNetToMediaType;
namelen = min( sizeof(name) - 1, entry->ipNetToMediaIfIndex.o_length );
memcpy( name, entry->ipNetToMediaIfIndex.o_bytes, namelen );
name[namelen] = 0;
getInterfaceIndexByName( name, &row.dwIndex );
if (!(table = append_ipnet_row( heap, flags, table, &count, &row ))) break;
}
HeapFree( GetProcessHeap(), 0, data );
}
close( fd );
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP)
{
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_LLINFO};
size_t needed;
char *buf = NULL, *lim, *next;
struct rt_msghdr *rtm;
struct sockaddr_inarp *sinarp;
struct sockaddr_dl *sdl;
if (sysctl (mib, ARRAY_SIZE(mib), NULL, &needed, NULL, 0) == -1)
{
ERR ("failed to get arp table\n");
ret = ERROR_NOT_SUPPORTED;
goto done;
}
buf = HeapAlloc (GetProcessHeap (), 0, needed);
if (!buf)
{
ret = ERROR_OUTOFMEMORY;
goto done;
}
if (sysctl (mib, ARRAY_SIZE(mib), buf, &needed, NULL, 0) == -1)
{
ret = ERROR_NOT_SUPPORTED;
goto done;
}
lim = buf + needed;
next = buf;
while(next < lim)
{
rtm = (struct rt_msghdr *)next;
sinarp=(struct sockaddr_inarp *)(rtm + 1);
sdl = (struct sockaddr_dl *)((char *)sinarp + ROUNDUP(sinarp->sin_len));
if(sdl->sdl_alen) /* arp entry */
{
memset( &row, 0, sizeof(row) );
row.dwAddr = sinarp->sin_addr.s_addr;
row.dwIndex = sdl->sdl_index;
row.dwPhysAddrLen = min( 8, sdl->sdl_alen );
memcpy( row.bPhysAddr, &sdl->sdl_data[sdl->sdl_nlen], row.dwPhysAddrLen );
if(rtm->rtm_rmx.rmx_expire == 0) row.u.Type = MIB_IPNET_TYPE_STATIC;
else if(sinarp->sin_other & SIN_PROXY) row.u.Type = MIB_IPNET_TYPE_OTHER;
else if(rtm->rtm_rmx.rmx_expire != 0) row.u.Type = MIB_IPNET_TYPE_DYNAMIC;
else row.u.Type = MIB_IPNET_TYPE_INVALID;
if (!(table = append_ipnet_row( heap, flags, table, &count, &row )))
break;
}
next += rtm->rtm_msglen;
}
done:
HeapFree( GetProcessHeap (), 0, buf );
}
#else
FIXME( "not implemented\n" );
ret = ERROR_NOT_SUPPORTED;
#endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret)
{
if (bOrder && table->dwNumEntries)
qsort( table->table, table->dwNumEntries, sizeof(row), compare_ipnet_rows );
*ppIpNetTable = table;
}
else HeapFree( heap, flags, table );
TRACE( "returning ret %u table %p\n", ret, table );
return ret;
}
static DWORD get_tcp_table_sizes( TCP_TABLE_CLASS class, DWORD row_count, DWORD *row_size )
{
DWORD table_size;
switch (class)
{
case TCP_TABLE_BASIC_LISTENER:
case TCP_TABLE_BASIC_CONNECTIONS:
case TCP_TABLE_BASIC_ALL:
{
table_size = FIELD_OFFSET(MIB_TCPTABLE, table[row_count]);
if (row_size) *row_size = sizeof(MIB_TCPROW);
break;
}
case TCP_TABLE_OWNER_PID_LISTENER:
case TCP_TABLE_OWNER_PID_CONNECTIONS:
case TCP_TABLE_OWNER_PID_ALL:
{
table_size = FIELD_OFFSET(MIB_TCPTABLE_OWNER_PID, table[row_count]);
if (row_size) *row_size = sizeof(MIB_TCPROW_OWNER_PID);
break;
}
case TCP_TABLE_OWNER_MODULE_LISTENER:
case TCP_TABLE_OWNER_MODULE_CONNECTIONS:
case TCP_TABLE_OWNER_MODULE_ALL:
{
table_size = FIELD_OFFSET(MIB_TCPTABLE_OWNER_MODULE, table[row_count]);
if (row_size) *row_size = sizeof(MIB_TCPROW_OWNER_MODULE);
break;
}
default:
ERR("unhandled class %u\n", class);
return 0;
}
return table_size;
}
static MIB_TCPTABLE *append_tcp_row( TCP_TABLE_CLASS class, HANDLE heap, DWORD flags,
MIB_TCPTABLE *table, DWORD *count,
const MIB_TCPROW_OWNER_MODULE *row, DWORD row_size )
{
if (table->dwNumEntries >= *count)
{
MIB_TCPTABLE *new_table;
DWORD new_count = table->dwNumEntries * 2, new_table_size;
new_table_size = get_tcp_table_sizes( class, new_count, NULL );
if (!(new_table = HeapReAlloc( heap, flags, table, new_table_size )))
{
HeapFree( heap, 0, table );
return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( (char *)table->table + (table->dwNumEntries * row_size), row, row_size );
table->dwNumEntries++;
return table;
}
/* Why not a lookup table? Because the TCPS_* constants are different
on different platforms */
static inline MIB_TCP_STATE TCPStateToMIBState (int state)
{
switch (state)
{
case TCPS_ESTABLISHED: return MIB_TCP_STATE_ESTAB;
case TCPS_SYN_SENT: return MIB_TCP_STATE_SYN_SENT;
case TCPS_SYN_RECEIVED: return MIB_TCP_STATE_SYN_RCVD;
case TCPS_FIN_WAIT_1: return MIB_TCP_STATE_FIN_WAIT1;
case TCPS_FIN_WAIT_2: return MIB_TCP_STATE_FIN_WAIT2;
case TCPS_TIME_WAIT: return MIB_TCP_STATE_TIME_WAIT;
case TCPS_CLOSE_WAIT: return MIB_TCP_STATE_CLOSE_WAIT;
case TCPS_LAST_ACK: return MIB_TCP_STATE_LAST_ACK;
case TCPS_LISTEN: return MIB_TCP_STATE_LISTEN;
case TCPS_CLOSING: return MIB_TCP_STATE_CLOSING;
default:
case TCPS_CLOSED: return MIB_TCP_STATE_CLOSED;
}
}
static int compare_tcp_rows(const void *a, const void *b)
{
const MIB_TCPROW *rowA = a;
const MIB_TCPROW *rowB = b;
int ret;
if ((ret = ntohl (rowA->dwLocalAddr) - ntohl (rowB->dwLocalAddr)) != 0) return ret;
if ((ret = ntohs ((unsigned short)rowA->dwLocalPort) -
ntohs ((unsigned short)rowB->dwLocalPort)) != 0) return ret;
if ((ret = ntohl (rowA->dwRemoteAddr) - ntohl (rowB->dwRemoteAddr)) != 0) return ret;
return ntohs ((unsigned short)rowA->dwRemotePort) - ntohs ((unsigned short)rowB->dwRemotePort);
}
struct pid_map
{
unsigned int pid;
unsigned int unix_pid;
};
static struct pid_map *get_pid_map( unsigned int *num_entries )
{
HANDLE snapshot = NULL;
struct pid_map *map;
unsigned int i = 0, count = 16, size = count * sizeof(*map);
NTSTATUS ret;
if (!(map = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
SERVER_START_REQ( create_snapshot )
{
req->flags = SNAP_PROCESS;
req->attributes = 0;
if (!(ret = wine_server_call( req )))
snapshot = wine_server_ptr_handle( reply->handle );
}
SERVER_END_REQ;
*num_entries = 0;
while (ret == STATUS_SUCCESS)
{
SERVER_START_REQ( next_process )
{
req->handle = wine_server_obj_handle( snapshot );
req->reset = (i == 0);
if (!(ret = wine_server_call( req )))
{
if (i >= count)
{
struct pid_map *new_map;
count *= 2;
size = count * sizeof(*new_map);
if (!(new_map = HeapReAlloc( GetProcessHeap(), 0, map, size )))
{
HeapFree( GetProcessHeap(), 0, map );
map = NULL;
goto done;
}
map = new_map;
}
map[i].pid = reply->pid;
map[i].unix_pid = reply->unix_pid;
(*num_entries)++;
i++;
}
}
SERVER_END_REQ;
}
done:
NtClose( snapshot );
return map;
}
static unsigned int find_owning_pid( struct pid_map *map, unsigned int num_entries, UINT_PTR inode )
{
#ifdef __linux__
unsigned int i, len_socket;
char socket[32];
sprintf( socket, "socket:[%lu]", inode );
len_socket = strlen( socket );
for (i = 0; i < num_entries; i++)
{
char dir[32];
struct dirent *dirent;
DIR *dirfd;
sprintf( dir, "/proc/%u/fd", map[i].unix_pid );
if ((dirfd = opendir( dir )))
{
while ((dirent = readdir( dirfd )))
{
char link[sizeof(dirent->d_name) + 32], name[32];
int len;
sprintf( link, "/proc/%u/fd/%s", map[i].unix_pid, dirent->d_name );
if ((len = readlink( link, name, sizeof(name) - 1 )) > 0) name[len] = 0;
if (len == len_socket && !strcmp( socket, name ))
{
closedir( dirfd );
return map[i].pid;
}
}
closedir( dirfd );
}
}
return 0;
#elif defined(HAVE_LIBPROCSTAT)
struct procstat *pstat;
struct kinfo_proc *proc;
struct filestat_list *fds;
struct filestat *fd;
struct sockstat sock;
unsigned int i, proc_count;
pstat = procstat_open_sysctl();
if (!pstat) return 0;
for (i = 0; i < num_entries; i++)
{
proc = procstat_getprocs( pstat, KERN_PROC_PID, map[i].unix_pid, &proc_count );
if (!proc || proc_count < 1) continue;
fds = procstat_getfiles( pstat, proc, 0 );
if (!fds)
{
procstat_freeprocs( pstat, proc );
continue;
}
STAILQ_FOREACH( fd, fds, next )
{
char errbuf[_POSIX2_LINE_MAX];
if (fd->fs_type != PS_FST_TYPE_SOCKET) continue;
procstat_get_socket_info( pstat, fd, &sock, errbuf );
if (sock.so_pcb == inode)
{
procstat_freefiles( pstat, fds );
procstat_freeprocs( pstat, proc );
procstat_close( pstat );
return map[i].pid;
}
}
procstat_freefiles( pstat, fds );
procstat_freeprocs( pstat, proc );
}
procstat_close( pstat );
return 0;
#elif defined(HAVE_PROC_PIDINFO)
struct proc_fdinfo *fds;
struct socket_fdinfo sock;
unsigned int i, j, n;
for (i = 0; i < num_entries; i++)
{
int fd_len = proc_pidinfo( map[i].unix_pid, PROC_PIDLISTFDS, 0, NULL, 0 );
if (fd_len <= 0) continue;
fds = HeapAlloc( GetProcessHeap(), 0, fd_len );
if (!fds) continue;
proc_pidinfo( map[i].unix_pid, PROC_PIDLISTFDS, 0, fds, fd_len );
n = fd_len / sizeof(struct proc_fdinfo);
for (j = 0; j < n; j++)
{
if (fds[j].proc_fdtype != PROX_FDTYPE_SOCKET) continue;
proc_pidfdinfo( map[i].unix_pid, fds[j].proc_fd, PROC_PIDFDSOCKETINFO, &sock, sizeof(sock) );
if (sock.psi.soi_pcb == inode)
{
HeapFree( GetProcessHeap(), 0, fds );
return map[i].pid;
}
}
HeapFree( GetProcessHeap(), 0, fds );
}
return 0;
#else
FIXME( "not implemented\n" );
return 0;
#endif
}
static BOOL match_class( TCP_TABLE_CLASS class, MIB_TCP_STATE state )
{
switch (class)
{
case TCP_TABLE_BASIC_ALL:
case TCP_TABLE_OWNER_PID_ALL:
case TCP_TABLE_OWNER_MODULE_ALL:
return TRUE;
case TCP_TABLE_BASIC_LISTENER:
case TCP_TABLE_OWNER_PID_LISTENER:
case TCP_TABLE_OWNER_MODULE_LISTENER:
if (state == MIB_TCP_STATE_LISTEN) return TRUE;
return FALSE;
case TCP_TABLE_BASIC_CONNECTIONS:
case TCP_TABLE_OWNER_PID_CONNECTIONS:
case TCP_TABLE_OWNER_MODULE_CONNECTIONS:
if (state == MIB_TCP_STATE_ESTAB) return TRUE;
return FALSE;
default:
ERR( "unhandled class %u\n", class );
return FALSE;
}
}
DWORD build_tcp_table( TCP_TABLE_CLASS class, void **tablep, BOOL order, HANDLE heap, DWORD flags,
DWORD *size )
{
MIB_TCPTABLE *table;
MIB_TCPROW_OWNER_MODULE row;
DWORD ret = NO_ERROR, count = 16, table_size, row_size;
if (!(table_size = get_tcp_table_sizes( class, count, &row_size )))
return ERROR_INVALID_PARAMETER;
if (!(table = HeapAlloc( heap, flags, table_size )))
return ERROR_OUTOFMEMORY;
table->dwNumEntries = 0;
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/tcp", "r")))
{
char buf[512], *ptr;
struct pid_map *map = NULL;
unsigned int dummy, num_entries = 0;
int inode;
if (class >= TCP_TABLE_OWNER_PID_LISTENER) map = get_pid_map( &num_entries );
/* skip header line */
ptr = fgets(buf, sizeof(buf), fp);
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
if (sscanf( ptr, "%x: %x:%x %x:%x %x %*s %*s %*s %*s %*s %d", &dummy,
&row.dwLocalAddr, &row.dwLocalPort, &row.dwRemoteAddr,
&row.dwRemotePort, &row.dwState, &inode ) != 7)
continue;
row.dwLocalPort = htons( row.dwLocalPort );
row.dwRemotePort = htons( row.dwRemotePort );
row.dwState = TCPStateToMIBState( row.dwState );
if (!match_class( class, row.dwState )) continue;
if (class >= TCP_TABLE_OWNER_PID_LISTENER)
row.dwOwningPid = find_owning_pid( map, num_entries, inode );
if (class >= TCP_TABLE_OWNER_MODULE_LISTENER)
{
row.liCreateTimestamp.QuadPart = 0; /* FIXME */
memset( &row.OwningModuleInfo, 0, sizeof(row.OwningModuleInfo) );
}
if (!(table = append_tcp_row( class, heap, flags, table, &count, &row, row_size )))
break;
}
HeapFree( GetProcessHeap(), 0, map );
fclose( fp );
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
{
void *data;
int fd, len;
mib2_tcpConnEntry_t *entry;
if ((fd = open_streams_mib( "tcp" )) != -1)
{
if ((data = read_mib_entry( fd, MIB2_TCP, MIB2_TCP_CONN, &len )))
{
for (entry = data; (char *)(entry + 1) <= (char *)data + len; entry++)
{
row.dwLocalAddr = entry->tcpConnLocalAddress;
row.dwLocalPort = htons( entry->tcpConnLocalPort );
row.dwRemoteAddr = entry->tcpConnRemAddress;
row.dwRemotePort = htons( entry->tcpConnRemPort );
row.dwState = entry->tcpConnState;
if (!match_class( class, row.dwState )) continue;
if (!(table = append_tcp_row( class, heap, flags, table, &count, &row, row_size )))
break;
}
HeapFree( GetProcessHeap(), 0, data );
}
close( fd );
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_STRUCT_XINPGEN)
{
size_t Len = 0;
char *Buf = NULL;
struct xinpgen *pXIG, *pOrigXIG;
struct pid_map *pMap = NULL;
unsigned NumEntries;
if (sysctlbyname ("net.inet.tcp.pcblist", NULL, &Len, NULL, 0) < 0)
{
ERR ("Failure to read net.inet.tcp.pcblist via sysctlbyname!\n");
ret = ERROR_NOT_SUPPORTED;
goto done;
}
Buf = HeapAlloc (GetProcessHeap (), 0, Len);
if (!Buf)
{
ret = ERROR_OUTOFMEMORY;
goto done;
}
if (sysctlbyname ("net.inet.tcp.pcblist", Buf, &Len, NULL, 0) < 0)
{
ERR ("Failure to read net.inet.tcp.pcblist via sysctlbyname!\n");
ret = ERROR_NOT_SUPPORTED;
goto done;
}
if (class >= TCP_TABLE_OWNER_PID_LISTENER) pMap = get_pid_map( &NumEntries );
/* Might be nothing here; first entry is just a header it seems */
if (Len <= sizeof (struct xinpgen)) goto done;
pOrigXIG = (struct xinpgen *)Buf;
pXIG = pOrigXIG;
for (pXIG = (struct xinpgen *)((char *)pXIG + pXIG->xig_len);
pXIG->xig_len > sizeof (struct xinpgen);
pXIG = (struct xinpgen *)((char *)pXIG + pXIG->xig_len))
{
struct tcpcb *pTCPData = NULL;
struct inpcb *pINData;
struct xsocket *pSockData;
pTCPData = &((struct xtcpcb *)pXIG)->xt_tp;
pINData = &((struct xtcpcb *)pXIG)->xt_inp;
pSockData = &((struct xtcpcb *)pXIG)->xt_socket;
/* Ignore sockets for other protocols */
if (pSockData->xso_protocol != IPPROTO_TCP)
continue;
/* Ignore PCBs that were freed while generating the data */
if (pINData->inp_gencnt > pOrigXIG->xig_gen)
continue;
/* we're only interested in IPv4 addresses */
if (!(pINData->inp_vflag & INP_IPV4) ||
(pINData->inp_vflag & INP_IPV6))
continue;
/* If all 0's, skip it */
if (!pINData->inp_laddr.s_addr &&
!pINData->inp_lport &&
!pINData->inp_faddr.s_addr &&
!pINData->inp_fport)
continue;
/* Fill in structure details */
row.dwLocalAddr = pINData->inp_laddr.s_addr;
row.dwLocalPort = pINData->inp_lport;
row.dwRemoteAddr = pINData->inp_faddr.s_addr;
row.dwRemotePort = pINData->inp_fport;
row.dwState = TCPStateToMIBState (pTCPData->t_state);
if (!match_class( class, row.dwState )) continue;
if (class >= TCP_TABLE_OWNER_PID_LISTENER)
row.dwOwningPid = find_owning_pid( pMap, NumEntries, (UINT_PTR)pSockData->so_pcb );
if (class >= TCP_TABLE_OWNER_MODULE_LISTENER)
{
row.liCreateTimestamp.QuadPart = 0; /* FIXME */
memset( &row.OwningModuleInfo, 0, sizeof(row.OwningModuleInfo) );
}
if (!(table = append_tcp_row( class, heap, flags, table, &count, &row, row_size )))
break;
}
done:
HeapFree( GetProcessHeap(), 0, pMap );
HeapFree (GetProcessHeap (), 0, Buf);
}
#else
FIXME( "not implemented\n" );
ret = ERROR_NOT_SUPPORTED;
#endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret)
{
if (order && table->dwNumEntries)
qsort( table->table, table->dwNumEntries, row_size, compare_tcp_rows );
*tablep = table;
}
else HeapFree( heap, flags, table );
if (size) *size = get_tcp_table_sizes( class, count, NULL );
TRACE( "returning ret %u table %p\n", ret, table );
return ret;
}
/******************************************************************
* AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
*
* Get the TCP connection table.
* Like GetTcpTable(), but allocate the returned table from heap.
*
* PARAMS
* ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
* allocated and returned.
* bOrder [In] whether to sort the table
* heap [In] heap from which the table is allocated
* flags [In] flags to HeapAlloc
*
* RETURNS
* ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
* returns otherwise.
*/
DWORD WINAPI AllocateAndGetTcpTableFromStack( PMIB_TCPTABLE *ppTcpTable, BOOL bOrder,
HANDLE heap, DWORD flags )
{
TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppTcpTable, bOrder, heap, flags);
if (!ppTcpTable) return ERROR_INVALID_PARAMETER;
return build_tcp_table( TCP_TABLE_BASIC_ALL, (void **)ppTcpTable, bOrder, heap, flags, NULL );
}
static DWORD get_udp_table_sizes( UDP_TABLE_CLASS class, DWORD row_count, DWORD *row_size )
{
DWORD table_size;
switch (class)
{
case UDP_TABLE_BASIC:
{
table_size = FIELD_OFFSET(MIB_UDPTABLE, table[row_count]);
if (row_size) *row_size = sizeof(MIB_UDPROW);
break;
}
case UDP_TABLE_OWNER_PID:
{
table_size = FIELD_OFFSET(MIB_UDPTABLE_OWNER_PID, table[row_count]);
if (row_size) *row_size = sizeof(MIB_UDPROW_OWNER_PID);
break;
}
case UDP_TABLE_OWNER_MODULE:
{
table_size = FIELD_OFFSET(MIB_UDPTABLE_OWNER_MODULE, table[row_count]);
if (row_size) *row_size = sizeof(MIB_UDPROW_OWNER_MODULE);
break;
}
default:
ERR("unhandled class %u\n", class);
return 0;
}
return table_size;
}
static MIB_UDPTABLE *append_udp_row( UDP_TABLE_CLASS class, HANDLE heap, DWORD flags,
MIB_UDPTABLE *table, DWORD *count,
const MIB_UDPROW_OWNER_MODULE *row, DWORD row_size )
{
if (table->dwNumEntries >= *count)
{
MIB_UDPTABLE *new_table;
DWORD new_count = table->dwNumEntries * 2, new_table_size;
new_table_size = get_udp_table_sizes( class, new_count, NULL );
if (!(new_table = HeapReAlloc( heap, flags, table, new_table_size )))
{
HeapFree( heap, 0, table );
return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( (char *)table->table + (table->dwNumEntries * row_size), row, row_size );
table->dwNumEntries++;
return table;
}
static int compare_udp_rows(const void *a, const void *b)
{
const MIB_UDPROW *rowA = a;
const MIB_UDPROW *rowB = b;
int ret;
if ((ret = rowA->dwLocalAddr - rowB->dwLocalAddr) != 0) return ret;
return rowA->dwLocalPort - rowB->dwLocalPort;
}
DWORD build_udp_table( UDP_TABLE_CLASS class, void **tablep, BOOL order, HANDLE heap, DWORD flags,
DWORD *size )
{
MIB_UDPTABLE *table;
MIB_UDPROW_OWNER_MODULE row;
DWORD ret = NO_ERROR, count = 16, table_size, row_size;
if (!(table_size = get_udp_table_sizes( class, count, &row_size )))
return ERROR_INVALID_PARAMETER;
if (!(table = HeapAlloc( heap, flags, table_size )))
return ERROR_OUTOFMEMORY;
table->dwNumEntries = 0;
memset( &row, 0, sizeof(row) );
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen( "/proc/net/udp", "r" )))
{
char buf[512], *ptr;
struct pid_map *map = NULL;
unsigned int dummy, num_entries = 0;
int inode;
if (class >= UDP_TABLE_OWNER_PID) map = get_pid_map( &num_entries );
/* skip header line */
ptr = fgets( buf, sizeof(buf), fp );
while ((ptr = fgets( buf, sizeof(buf), fp )))
{
if (sscanf( ptr, "%u: %x:%x %*s %*s %*s %*s %*s %*s %*s %d", &dummy,
&row.dwLocalAddr, &row.dwLocalPort, &inode ) != 4)
continue;
row.dwLocalPort = htons( row.dwLocalPort );
if (class >= UDP_TABLE_OWNER_PID)
row.dwOwningPid = find_owning_pid( map, num_entries, inode );
if (class >= UDP_TABLE_OWNER_MODULE)
{
row.liCreateTimestamp.QuadPart = 0; /* FIXME */
row.u.dwFlags = 0;
memset( &row.OwningModuleInfo, 0, sizeof(row.OwningModuleInfo) );
}
if (!(table = append_udp_row( class, heap, flags, table, &count, &row, row_size )))
break;
}
HeapFree( GetProcessHeap(), 0, map );
fclose( fp );
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
{
void *data;
int fd, len;
mib2_udpEntry_t *entry;
if ((fd = open_streams_mib( "udp" )) != -1)
{
if ((data = read_mib_entry( fd, MIB2_UDP, MIB2_UDP_ENTRY, &len )))
{
for (entry = data; (char *)(entry + 1) <= (char *)data + len; entry++)
{
row.dwLocalAddr = entry->udpLocalAddress;
row.dwLocalPort = htons( entry->udpLocalPort );
if (!(table = append_udp_row( class, heap, flags, table, &count, &row, row_size ))) break;
}
HeapFree( GetProcessHeap(), 0, data );
}
close( fd );
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_STRUCT_XINPGEN)
{
size_t Len = 0;
char *Buf = NULL;
struct xinpgen *pXIG, *pOrigXIG;
struct pid_map *pMap = NULL;
unsigned NumEntries;
if (sysctlbyname ("net.inet.udp.pcblist", NULL, &Len, NULL, 0) < 0)
{
ERR ("Failure to read net.inet.udp.pcblist via sysctlbyname!\n");
ret = ERROR_NOT_SUPPORTED;
goto done;
}
Buf = HeapAlloc (GetProcessHeap (), 0, Len);
if (!Buf)
{
ret = ERROR_OUTOFMEMORY;
goto done;
}
if (sysctlbyname ("net.inet.udp.pcblist", Buf, &Len, NULL, 0) < 0)
{
ERR ("Failure to read net.inet.udp.pcblist via sysctlbyname!\n");
ret = ERROR_NOT_SUPPORTED;
goto done;
}
if (class >= UDP_TABLE_OWNER_PID)
pMap = get_pid_map( &NumEntries );
/* Might be nothing here; first entry is just a header it seems */
if (Len <= sizeof (struct xinpgen)) goto done;
pOrigXIG = (struct xinpgen *)Buf;
pXIG = pOrigXIG;
for (pXIG = (struct xinpgen *)((char *)pXIG + pXIG->xig_len);
pXIG->xig_len > sizeof (struct xinpgen);
pXIG = (struct xinpgen *)((char *)pXIG + pXIG->xig_len))
{
struct inpcb *pINData;
struct xsocket *pSockData;
pINData = &((struct xinpcb *)pXIG)->xi_inp;
pSockData = &((struct xinpcb *)pXIG)->xi_socket;
/* Ignore sockets for other protocols */
if (pSockData->xso_protocol != IPPROTO_UDP)
continue;
/* Ignore PCBs that were freed while generating the data */
if (pINData->inp_gencnt > pOrigXIG->xig_gen)
continue;
/* we're only interested in IPv4 addresses */
if (!(pINData->inp_vflag & INP_IPV4) ||
(pINData->inp_vflag & INP_IPV6))
continue;
/* If all 0's, skip it */
if (!pINData->inp_laddr.s_addr &&
!pINData->inp_lport)
continue;
/* Fill in structure details */
row.dwLocalAddr = pINData->inp_laddr.s_addr;
row.dwLocalPort = pINData->inp_lport;
if (class >= UDP_TABLE_OWNER_PID)
row.dwOwningPid = find_owning_pid( pMap, NumEntries, (UINT_PTR)pSockData->so_pcb );
if (class >= UDP_TABLE_OWNER_MODULE)
{
row.liCreateTimestamp.QuadPart = 0; /* FIXME */
row.u.dwFlags = 0;
memset( &row.OwningModuleInfo, 0, sizeof(row.OwningModuleInfo) );
}
if (!(table = append_udp_row( class, heap, flags, table, &count, &row, row_size ))) break;
}
done:
HeapFree( GetProcessHeap(), 0, pMap );
HeapFree (GetProcessHeap (), 0, Buf);
}
#else
FIXME( "not implemented\n" );
ret = ERROR_NOT_SUPPORTED;
#endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret)
{
if (order && table->dwNumEntries)
qsort( table->table, table->dwNumEntries, row_size, compare_udp_rows );
*tablep = table;
}
else HeapFree( heap, flags, table );
if (size) *size = get_udp_table_sizes( class, count, NULL );
TRACE( "returning ret %u table %p\n", ret, table );
return ret;
}
static DWORD get_udp6_table_sizes( UDP_TABLE_CLASS class, DWORD row_count, DWORD *row_size )
{
DWORD table_size;
switch (class)
{
case UDP_TABLE_BASIC:
{
table_size = FIELD_OFFSET(MIB_UDP6TABLE, table[row_count]);
if (row_size) *row_size = sizeof(MIB_UDP6ROW);
break;
}
case UDP_TABLE_OWNER_PID:
{
table_size = FIELD_OFFSET(MIB_UDP6TABLE_OWNER_PID, table[row_count]);
if (row_size) *row_size = sizeof(MIB_UDP6ROW_OWNER_PID);
break;
}
case UDP_TABLE_OWNER_MODULE:
{
table_size = FIELD_OFFSET(MIB_UDP6TABLE_OWNER_MODULE, table[row_count]);
if (row_size) *row_size = sizeof(MIB_UDP6ROW_OWNER_MODULE);
break;
}
default:
ERR("unhandled class %u\n", class);
return 0;
}
return table_size;
}
static MIB_UDP6TABLE *append_udp6_row( UDP_TABLE_CLASS class, HANDLE heap, DWORD flags,
MIB_UDP6TABLE *table, DWORD *count,
const MIB_UDP6ROW_OWNER_MODULE *row, DWORD row_size )
{
if (table->dwNumEntries >= *count)
{
MIB_UDP6TABLE *new_table;
DWORD new_count = table->dwNumEntries * 2, new_table_size;
new_table_size = get_udp6_table_sizes( class, new_count, NULL );
if (!(new_table = HeapReAlloc( heap, flags, table, new_table_size )))
{
HeapFree( heap, 0, table );
return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( (char *)table->table + (table->dwNumEntries * row_size), row, row_size );
table->dwNumEntries++;
return table;
}
static int compare_udp6_rows(const void *a, const void *b)
{
const MIB_UDP6ROW *rowA = a;
const MIB_UDP6ROW *rowB = b;
int ret;
if ((ret = memcmp(&rowA->dwLocalAddr, &rowB->dwLocalAddr, sizeof(rowA->dwLocalAddr)) != 0)) return ret;
if ((ret = rowA->dwLocalScopeId - rowB->dwLocalScopeId) != 0) return ret;
return rowA->dwLocalPort - rowB->dwLocalPort;
}
struct ipv6_addr_scope
{
IN6_ADDR addr;
DWORD scope;
};
static struct ipv6_addr_scope *get_ipv6_addr_scope_table(unsigned int *size)
{
struct ipv6_addr_scope *table = NULL;
unsigned int table_size = 0;
if (!(table = HeapAlloc( GetProcessHeap(), 0, sizeof(table[0]) )))
return NULL;
#ifdef __linux__
{
FILE *fp;
char buf[512], *ptr;
if (!(fp = fopen( "/proc/net/if_inet6", "r" )))
goto failed;
while ((ptr = fgets( buf, sizeof(buf), fp )))
{
WORD a[8];
DWORD scope;
struct ipv6_addr_scope *new_table;
struct ipv6_addr_scope *entry;
unsigned int i;
if (sscanf( ptr, "%4hx%4hx%4hx%4hx%4hx%4hx%4hx%4hx %*s %*s %x",
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &scope ) != 9)
continue;
table_size++;
if (!(new_table = HeapReAlloc( GetProcessHeap(), 0, table, table_size * sizeof(table[0]) )))
{
fclose(fp);
goto failed;
}
table = new_table;
entry = &table[table_size - 1];
i = 0;
while (i < 8)
{
entry->addr.u.Word[i] = htons(a[i]);
i++;
}
entry->scope = htons(scope);
}
fclose(fp);
}
#else
FIXME( "not implemented\n" );
goto failed;
#endif
*size = table_size;
return table;
failed:
HeapFree( GetProcessHeap(), 0, table );
return NULL;
}
static DWORD find_ipv6_addr_scope(const IN6_ADDR *addr, const struct ipv6_addr_scope *table, unsigned int size)
{
const BYTE multicast_scope_mask = 0x0F;
const BYTE multicast_scope_shift = 0;
unsigned int i = 0;
if (WS_IN6_IS_ADDR_UNSPECIFIED(addr))
return 0;
if (WS_IN6_IS_ADDR_MULTICAST(addr))
return htons((addr->u.Byte[1] & multicast_scope_mask) >> multicast_scope_shift);
if (!table)
return -1;
while (i < size)
{
if (memcmp(&table[i].addr, addr, sizeof(table[i].addr)) == 0)
return table[i].scope;
i++;
}
return -1;
}
DWORD build_udp6_table( UDP_TABLE_CLASS class, void **tablep, BOOL order, HANDLE heap, DWORD flags,
DWORD *size )
{
MIB_UDP6TABLE *table;
MIB_UDP6ROW_OWNER_MODULE row;
DWORD ret = NO_ERROR, count = 16, table_size, row_size;
if (!(table_size = get_udp6_table_sizes( class, count, &row_size )))
return ERROR_INVALID_PARAMETER;
if (!(table = HeapAlloc( heap, flags, table_size )))
return ERROR_OUTOFMEMORY;
table->dwNumEntries = 0;
memset( &row, 0, sizeof(row) );
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen( "/proc/net/udp6", "r" )))
{
char buf[512], *ptr;
struct pid_map *map = NULL;
unsigned int num_entries = 0;
struct ipv6_addr_scope *addr_scopes;
unsigned int addr_scopes_size = 0;
unsigned int dummy;
int inode;
addr_scopes = get_ipv6_addr_scope_table(&addr_scopes_size);
if (class >= UDP_TABLE_OWNER_PID) map = get_pid_map( &num_entries );
/* skip header line */
ptr = fgets( buf, sizeof(buf), fp );
while ((ptr = fgets( buf, sizeof(buf), fp )))
{
DWORD in6_addr32[4];
if (sscanf( ptr, "%u: %8x%8x%8x%8x:%x %*s %*s %*s %*s %*s %*s %*s %d", &dummy,
&in6_addr32[0], &in6_addr32[1], &in6_addr32[2], &in6_addr32[3],
&row.dwLocalPort, &inode ) != 7)
continue;
memcpy(&row.ucLocalAddr, in6_addr32, sizeof(row.ucLocalAddr));
row.dwLocalScopeId = find_ipv6_addr_scope((const IN6_ADDR *)&row.ucLocalAddr, addr_scopes, addr_scopes_size);
row.dwLocalPort = htons( row.dwLocalPort );
if (class >= UDP_TABLE_OWNER_PID)
row.dwOwningPid = find_owning_pid( map, num_entries, inode );
if (class >= UDP_TABLE_OWNER_MODULE)
{
row.liCreateTimestamp.QuadPart = 0; /* FIXME */
row.u.dwFlags = 0;
memset( &row.OwningModuleInfo, 0, sizeof(row.OwningModuleInfo) );
}
if (!(table = append_udp6_row( class, heap, flags, table, &count, &row, row_size )))
break;
}
HeapFree( GetProcessHeap(), 0, map );
HeapFree( GetProcessHeap(), 0, addr_scopes );
fclose( fp );
}
else ret = ERROR_NOT_SUPPORTED;
}
#else
FIXME( "not implemented\n" );
ret = ERROR_NOT_SUPPORTED;
#endif
if (!table) return ERROR_OUTOFMEMORY;
if (!ret)
{
if (order && table->dwNumEntries)
qsort( table->table, table->dwNumEntries, row_size, compare_udp6_rows );
*tablep = table;
}
else HeapFree( heap, flags, table );
if (size) *size = get_udp6_table_sizes( class, count, NULL );
TRACE( "returning ret %u table %p\n", ret, table );
return ret;
}
/******************************************************************
* AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
*
* Get the UDP listener table.
* Like GetUdpTable(), but allocate the returned table from heap.
*
* PARAMS
* ppUdpTable [Out] pointer into which the MIB_UDPTABLE is
* allocated and returned.
* bOrder [In] whether to sort the table
* heap [In] heap from which the table is allocated
* flags [In] flags to HeapAlloc
*
* RETURNS
* ERROR_INVALID_PARAMETER if ppUdpTable is NULL, whatever GetUdpTable()
* returns otherwise.
*/
DWORD WINAPI AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE *ppUdpTable, BOOL bOrder,
HANDLE heap, DWORD flags)
{
TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppUdpTable, bOrder, heap, flags);
if (!ppUdpTable) return ERROR_INVALID_PARAMETER;
return build_udp_table( UDP_TABLE_BASIC, (void **)ppUdpTable, bOrder, heap, flags, NULL );
}
|