1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013
|
/*
* dsock.c - Linux socket processing functions for /proc-based lsof
*/
/*
* Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#include "common.h"
#include <sys/xattr.h>
#include "hash.h"
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
/*
* UNIX endpoint definitions
*/
# include <sys/socket.h> /* for AF_NETLINK */
# include <linux/rtnetlink.h> /* for NETLINK_INET_DIAG */
# include <linux/sock_diag.h> /* for SOCK_DIAG_BY_FAMILY */
# include <linux/unix_diag.h> /* for unix_diag_req */
# include <string.h> /* memset */
# include <stdint.h> /* for unt8_t */
# include <unistd.h> /* for getpagesize */
# define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
#if defined(HASSOSTATE)
# include <linux/net.h> /* for SS_* */
#endif /* defined(HASSOSTATE) */
/*
* Local definitions
*/
#define INOBUCKS \
128 /* inode hash bucket count -- must be \
* a power of two */
#define INOHASH(ino) ((int)((ino * 31415) >> 3) & (INOBUCKS - 1))
#define TCPUDPHASH(ino) ((int)((ino * 31415) >> 3) & (TcpUdp_bucks - 1))
#define TCPUDP6HASH(ino) ((int)((ino * 31415) >> 3) & (TcpUdp6_bucks - 1))
#define IPCBUCKS \
4096 /* IPC hash bucket count -- must be \
* a power of two */
/* If a socket is used for IPC, we store both end points for the socket
* to the same hash bucket. This makes searching the counter part of
* an end point easier. See get_netpeeri(). */
#define TCPUDP_IPC_HASH(tp) \
((int)(((((tp)->faddr * 0x109 + (tp)->laddr * 0x109 + \
(tp)->fport * 0x121 + (tp)->lport * 0x121 + \
(tp)->proto * 0x181) * \
31415) >> \
3) & \
(IPCBUCKS - 1)))
#define TCPUDP6_IPC_ADDR_INT32(a, n) (((a)->s6_addr32[n]))
#define TCPUDP6_IPC_ADDR_MK_INT(a) \
((int)TCPUDP6_IPC_ADDR_INT32(a, 0x0) * 0x123 + \
(int)TCPUDP6_IPC_ADDR_INT32(a, 0x1) * 0x111 + \
(int)TCPUDP6_IPC_ADDR_INT32(a, 0x2) * 0x149 + \
(int)TCPUDP6_IPC_ADDR_INT32(a, 0x3) * 0x185)
#define TCPUDP6_IPC_HASH(tp) \
((int)((((TCPUDP6_IPC_ADDR_MK_INT(&(tp)->faddr) + \
TCPUDP6_IPC_ADDR_MK_INT(&(tp)->laddr) + (tp)->fport * 0x109 + \
(tp)->lport * 0x109 + (tp)->proto * 0x141) * \
31415) >> \
3) & \
(IPCBUCKS - 1)))
/*
* Local structures
*/
struct ax25sin { /* AX25 socket information */
char *da; /* destination address */
char *dev_ch; /* device characters */
char *sa; /* source address */
INODETYPE inode;
unsigned long sq, rq; /* send and receive queue values */
unsigned char sqs, rqs; /* send and receive queue states */
int state;
struct ax25sin *next;
};
struct icmpin {
INODETYPE inode; /* node number */
char *la; /* local address */
char *ra; /* remote address */
MALLOC_S lal; /* strlen(la) */
MALLOC_S ral; /* strlen(ra) */
struct icmpin *next;
};
struct ipxsin { /* IPX socket information */
INODETYPE inode;
char *la; /* local address */
char *ra; /* remote address */
int state;
unsigned long txq, rxq; /* transmit and receive queue values */
struct ipxsin *next;
};
struct nlksin { /* Netlink socket information */
INODETYPE inode; /* node number */
unsigned int pr; /* protocol */
struct nlksin *next;
};
struct packin { /* packet information */
INODETYPE inode;
int ty; /* socket type */
int pr; /* protocol */
struct packin *next;
};
struct rawsin { /* raw socket information */
INODETYPE inode;
char *la; /* local address */
char *ra; /* remote address */
char *sp; /* state characters */
MALLOC_S lal; /* strlen(la) */
MALLOC_S ral; /* strlen(ra) */
MALLOC_S spl; /* strlen(sp) */
struct rawsin *next;
};
struct sctpsin { /* SCTP socket information */
INODETYPE inode;
int type; /* type: 0 = assoc
* 1 = eps
* 2 assoc and eps */
char *addr; /* association or endpoint address */
char *assocID; /* association ID */
char *lport; /* local port */
char *rport; /* remote port */
char *laddrs; /* local address */
char *raddrs; /* remote address */
struct sctpsin *next;
};
struct tcp_udp { /* IPv4 TCP and UDP socket
* information */
INODETYPE inode;
unsigned long faddr, laddr; /* foreign & local IPv4 addresses */
int fport, lport; /* foreign & local ports */
unsigned long txq, rxq; /* transmit & receive queue values */
int proto; /* 0 = TCP, 1 = UDP, 2 = UDPLITE */
int state; /* protocol state */
struct tcp_udp *next; /* in TcpUdp inode hash table */
#if defined(HASEPTOPTS)
pxinfo_t *pxinfo; /* inode information */
struct tcp_udp *ipc_next; /* in TcpUdp local ipc hash table */
struct tcp_udp *ipc_peer; /* locally connected peer(s) info */
#endif /* defined(HASEPTOPTS) */
};
#if defined(HASIPv6)
struct tcp_udp6 { /* IPv6 TCP and UDP socket
* information */
INODETYPE inode;
struct in6_addr faddr, laddr; /* foreign & local IPv6 addresses */
int fport, lport; /* foreign & local ports */
unsigned long txq, rxq; /* transmit & receive queue values */
int proto; /* 0 = TCP, 1 = UDP, 2 = UDPLITE */
int state; /* protocol state */
struct tcp_udp6 *next;
# if defined(HASEPTOPTS)
pxinfo_t *pxinfo; /* inode information */
struct tcp_udp6 *ipc_next; /* in TcpUdp6 local ipc hash table */
struct tcp_udp6 *ipc_peer; /* locally connected peer(s) info */
# endif /* defined(HASEPTOPTS) */
};
#endif /* defined(HASIPv6) */
typedef struct uxsin { /* UNIX socket information */
INODETYPE inode; /* node number */
char *pcb; /* protocol control block */
char *path; /* file path */
unsigned char sb_def; /* stat(2) buffer definitions */
dev_t sb_dev; /* stat(2) buffer device */
INODETYPE sb_ino; /* stat(2) buffer node number */
dev_t sb_rdev; /* stat(2) raw device number */
uint32_t ty; /* socket type */
unsigned int opt; /* socket options */
unsigned int ss; /* socket state */
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
struct uxsin *icons; /* incoming socket connections */
unsigned int icstat; /* incoming connection status
* 0 == none */
pxinfo_t *pxinfo; /* inode information */
struct uxsin *peer; /* connected peer(s) info */
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
struct uxsin *next;
} uxsin_t;
/*
* Local static values
*/
static char *AX25path = (char *)NULL; /* path to AX25 /proc information */
/* AX25 socket info, hashed by inode */
static struct ax25sin **AX25sin = (struct ax25sin **)NULL;
static char *ax25st[] = {
"LISTENING", /* 0 */
"SABM SENT", /* 1 */
"DISC SENT", /* 2 */
"ESTABLISHED", /* 3 */
"RECOVERY" /* 4 */
};
#define NAX25ST (sizeof(ax25st) / sizeof(char *))
static char *ICMPpath = (char *)NULL; /* path to ICMP /proc information */
/* ICMP socket info, hashed by inode */
static struct icmpin **Icmpin = (struct icmpin **)NULL;
static char *Ipxpath = (char *)NULL; /* path to IPX /proc information */
/* IPX socket info, hashed by inode */
static struct ipxsin **Ipxsin = (struct ipxsin **)NULL;
static char *Nlkpath = (char *)NULL; /* path to Netlink /proc information */
/* Netlink socket info, hashed by
* inode */
static struct nlksin **Nlksin = (struct nlksin **)NULL;
/* packet info, hashed by inode */
static struct packin **Packin = (struct packin **)NULL;
static char *Packpath = (char *)NULL; /* path to packet /proc information */
static char *Rawpath = (char *)NULL; /* path to raw socket /proc
* information */
/* raw socket info, hashed by inode */
static struct rawsin **Rawsin = (struct rawsin **)NULL;
static char *SCTPPath[] = {
/* paths to /proc/net STCP info */
(char *)NULL, /* 0 = /proc/net/sctp/assocs */
(char *)NULL /* 1 = /proc/net/sctp/eps */
};
#define NSCTPPATHS sizeof(SCTPPath) / sizeof(char *)
static char *SCTPSfx[] = {
/* /proc/net suffixes */
"sctp/assocs", /* 0 = /proc/net/sctp/assocs */
"sctp/eps" /* 1 = /proc/net/sctp/eps */
};
/* SCTP info, hashed by inode */
static struct sctpsin **SCTPsin = (struct sctpsin **)NULL;
/* path to /proc/net socket status */
static char *SockStatPath = (char *)NULL;
static char *TCPpath = (char *)NULL; /* path to TCP /proc information */
/* IPv4 TCP & UDP info, hashed by
* inode */
static struct tcp_udp **TcpUdp = (struct tcp_udp **)NULL;
static int TcpUdp_bucks = 0; /* dynamically sized hash bucket
* count for TCP and UDP -- will
* be a power of two */
#if defined(HASEPTOPTS)
/* IPv4 TCP & UDP info for socket used
for IPC, hashed by (addr, port paris
and protocol */
static struct tcp_udp **TcpUdpIPC = (struct tcp_udp **)NULL;
#endif /* defined(HASEPTOPTS) */
#if defined(HASIPv6)
static char *Raw6path = (char *)NULL; /* path to raw IPv6 /proc information */
/* IPv6 raw socket info, hashed by
* inode */
static struct rawsin **Rawsin6 = (struct rawsin **)NULL;
/* path to /proc/net IPv6 socket
* status */
static char *SockStatPath6 = (char *)NULL;
static char *TCP6path = (char *)NULL; /* path to IPv6 TCP /proc information */
/* IPv6 TCP & UDP info, hashed by
* inode */
static struct tcp_udp6 **TcpUdp6 = (struct tcp_udp6 **)NULL;
static int TcpUdp6_bucks = 0; /* dynamically sized hash bucket
* count for IPv6 TCP and UDP -- will
* be a power of two */
static char *UDP6path = (char *)NULL; /* path to IPv6 UDP /proc information */
/* path to IPv6 UDPLITE /proc
* information */
static char *UDPLITE6path = (char *)NULL;
# if defined(HASEPTOPTS)
/* IPv4 TCP & UDP info for socket used
for IPC, hashed by (addr, port paris
and protocol */
static struct tcp_udp6 **TcpUdp6IPC = (struct tcp_udp6 **)NULL;
# endif /* defined(HASEPTOPTS) */
#endif /* defined(HASIPv6) */
static char *UDPpath = (char *)NULL; /* path to UDP /proc information */
/* path to UDPLITE /proc information */
static char *UDPLITEpath = (char *)NULL;
static char *UNIXpath = (char *)NULL; /* path to UNIX /proc information */
/* UNIX socket info, hashed by inode */
static uxsin_t **Uxsin = (uxsin_t **)NULL;
/*
* Local function prototypes
*/
static struct ax25sin *check_ax25(struct lsof_context *ctx, INODETYPE i);
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
static void enter_uxsinfo(struct lsof_context *ctx, uxsin_t *up);
static void fill_uxicino(struct lsof_context *ctx, INODETYPE si, INODETYPE sc);
static void fill_uxpino(struct lsof_context *ctx, INODETYPE si, INODETYPE pi);
static int get_diagmsg(int sockfd);
static void get_uxpeeri(struct lsof_context *ctx);
static void parse_diag(struct lsof_context *ctx, struct unix_diag_msg *dm,
int len);
static void prt_uxs(struct lsof_context *ctx, uxsin_t *p, int mk);
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
#if defined(HASEPTOPTS)
static void enter_netsinfo(struct lsof_context *ctx, struct tcp_udp *tp);
static void get_netpeeri(struct lsof_context *ctx);
#endif /* defined(HASEPTOPTS) */
#if defined(HASIPv6)
# if defined(HASEPTOPTS)
static void enter_nets6info(struct lsof_context *ctx, struct tcp_udp6 *tp);
static void get_net6peeri(struct lsof_context *ctx);
# endif /* defined(HASEPTOPTS) */
#endif /* defined(HASIPv6) */
static struct icmpin *check_icmp(struct lsof_context *ctx, INODETYPE i);
static struct ipxsin *check_ipx(struct lsof_context *ctx, INODETYPE i);
static struct nlksin *check_netlink(struct lsof_context *ctx, INODETYPE i);
static struct packin *check_pack(struct lsof_context *ctx, INODETYPE i);
static struct rawsin *check_raw(struct lsof_context *ctx, INODETYPE i);
static struct sctpsin *check_sctp(struct lsof_context *ctx, INODETYPE i);
static struct tcp_udp *check_tcpudp(struct lsof_context *ctx, INODETYPE i,
char **p);
static uxsin_t *check_unix(struct lsof_context *ctx, INODETYPE i);
static void get_ax25(struct lsof_context *ctx, char *p);
static void get_icmp(struct lsof_context *ctx, char *p);
static void get_ipx(struct lsof_context *ctx, char *p);
static void get_netlink(struct lsof_context *ctx, char *p);
static void get_pack(struct lsof_context *ctx, char *p);
static void get_raw(struct lsof_context *ctx, char *p);
static void get_sctp(struct lsof_context *ctx);
static char *get_sctpaddrs(char **fp, int i, int nf, int *x);
static void get_tcpudp(struct lsof_context *ctx, char *p, int pr, int clr);
static void get_unix(struct lsof_context *ctx, char *p);
static int isainb(char *a, char *b);
static void print_ax25info(struct lsof_context *ctx, struct ax25sin *ap);
static void print_ipxinfo(struct lsof_context *ctx, struct ipxsin *ip);
static char *socket_type_to_str(uint32_t ty, int *rf);
static char *netlink_proto_to_str(unsigned int pr);
#if defined(HASSOSTATE)
static char *socket_state_to_str(struct lsof_context *ctx, unsigned int ss);
#endif /* defined(HASSOSTATE) */
static char *ethernet_proto_to_str(unsigned int pr);
#if defined(HASIPv6)
static struct rawsin *check_raw6(struct lsof_context *ctx, INODETYPE i);
static struct tcp_udp6 *check_tcpudp6(struct lsof_context *ctx, INODETYPE i,
char **p);
static void get_raw6(struct lsof_context *ctx, char *p);
static void get_tcpudp6(struct lsof_context *ctx, char *p, int pr, int clr);
static int hex_ipv6_to_in6(char *as, struct in6_addr *ad);
#endif /* defined(HASIPv6) */
/*
* build_IPstates() -- build the TCP and UDP state tables
*/
void build_IPstates(struct lsof_context *ctx) {
if (!TcpSt) {
(void)enter_IPstate(ctx, "TCP", "ESTABLISHED", TCP_ESTABLISHED);
(void)enter_IPstate(ctx, "TCP", "SYN_SENT", TCP_SYN_SENT);
(void)enter_IPstate(ctx, "TCP", "SYN_RECV", TCP_SYN_RECV);
(void)enter_IPstate(ctx, "TCP", "FIN_WAIT1", TCP_FIN_WAIT1);
(void)enter_IPstate(ctx, "TCP", "FIN_WAIT2", TCP_FIN_WAIT2);
(void)enter_IPstate(ctx, "TCP", "TIME_WAIT", TCP_TIME_WAIT);
(void)enter_IPstate(ctx, "TCP", "CLOSE", TCP_CLOSE);
(void)enter_IPstate(ctx, "TCP", "CLOSE_WAIT", TCP_CLOSE_WAIT);
(void)enter_IPstate(ctx, "TCP", "LAST_ACK", TCP_LAST_ACK);
(void)enter_IPstate(ctx, "TCP", "LISTEN", TCP_LISTEN);
(void)enter_IPstate(ctx, "TCP", "CLOSING", TCP_CLOSING);
(void)enter_IPstate(ctx, "TCP", "CLOSED", 0);
(void)enter_IPstate(ctx, "TCP", (char *)NULL, 0);
}
}
/*
* check_ax25() - check for AX25 socket file
*/
static struct ax25sin *check_ax25(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(AX25sin, INOHASH, struct ax25sin, inode, i);
}
/*
* check_icmp() - check for ICMP socket
*/
static struct icmpin *check_icmp(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Icmpin, INOHASH, struct icmpin, inode, i);
}
/*
* check_ipx() - check for IPX socket file
*/
static struct ipxsin *check_ipx(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Ipxsin, INOHASH, struct ipxsin, inode, i);
}
/*
* check_netlink() - check for Netlink socket file
*/
static struct nlksin *
check_netlink(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Nlksin, INOHASH, struct nlksin, inode, i);
}
/*
* check_pack() - check for packet file
*/
static struct packin *check_pack(struct lsof_context *ctx,
INODETYPE i) /* packet file's inode number */
{
return HASH_FIND_ELEMENT(Packin, INOHASH, struct packin, inode, i);
}
/*
* check_raw() - check for raw socket file
*/
static struct rawsin *check_raw(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Rawsin, INOHASH, struct rawsin, inode, i);
}
/*
* check_sctp() - check for SCTP socket file
*/
static struct sctpsin *check_sctp(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(SCTPsin, INOHASH, struct sctpsin, inode, i);
}
/*
* check_tcpudp() - check for IPv4 TCP or UDP socket file
*/
static struct tcp_udp *
check_tcpudp(struct lsof_context *ctx,
INODETYPE i, /* socket file's inode number */
char **p) /* protocol return */
{
struct tcp_udp *tp;
tp = HASH_FIND_ELEMENT(TcpUdp, TCPUDPHASH, struct tcp_udp, inode, i);
if (tp) {
switch (tp->proto) {
case 0:
*p = "TCP";
break;
case 1:
*p = "UDP";
break;
case 2:
*p = "UDPLITE";
break;
default:
*p = "unknown";
}
}
return tp;
}
/*
* check_inet() - check for locally used INET domain socket
*/
static struct tcp_udp *check_inet(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(TcpUdp, TCPUDPHASH, struct tcp_udp, inode, i);
}
/*
* clear_netsinfo -- clear allocated INET socket info
*/
void clear_netsinfo(struct lsof_context *ctx) {
int h; /* hash index */
struct tcp_udp *ti, *tp; /* temporary pointers */
#if defined(HASEPTOPTS)
pxinfo_t *pp, *pnp;
#endif /* defined(HASEPTOPTS) */
if (TcpUdp) {
for (h = 0; h < TcpUdp_bucks; h++) {
if ((ti = TcpUdp[h])) {
do {
tp = ti->next;
#if defined(HASEPTOPTS)
for (pp = ti->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
#endif /* defined(HASEPTOPTS) */
(void)free((FREE_P *)ti);
ti = tp;
} while (ti);
TcpUdp[h] = (struct tcp_udp *)NULL;
}
}
}
if (TcpUdpIPC) {
for (h = 0; h < IPCBUCKS; h++)
TcpUdpIPC[h] = (struct tcp_udp *)NULL;
}
}
#if defined(HASIPv6)
/*
* check_raw6() - check for raw IPv6 socket file
*/
static struct rawsin *check_raw6(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Rawsin6, INOHASH, struct rawsin, inode, i);
}
/*
* check_tcpudp6() - check for IPv6 TCP or UDP socket file
*/
static struct tcp_udp6 *
check_tcpudp6(struct lsof_context *ctx,
INODETYPE i, /* socket file's inode number */
char **p) /* protocol return */
{
struct tcp_udp6 *tp6;
tp6 = HASH_FIND_ELEMENT(TcpUdp6, TCPUDP6HASH, struct tcp_udp6, inode, i);
if (tp6) {
switch (tp6->proto) {
case 0:
*p = "TCP";
break;
case 1:
*p = "UDP";
break;
case 2:
*p = "UDPLITE";
break;
default:
*p = "unknown";
}
}
return tp6;
}
/*
* check_inet6() - check for locally used INET6 domain socket
*/
static struct tcp_udp6 *
check_inet6(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(TcpUdp6, TCPUDP6HASH, struct tcp_udp6, inode, i);
}
/*
* clear_nets6info -- clear allocated INET6 socket info
*/
void clear_nets6info(struct lsof_context *ctx) {
int h; /* hash index */
struct tcp_udp6 *ti, *tp; /* temporary pointers */
# if defined(HASEPTOPTS)
pxinfo_t *pp, *pnp;
# endif /* defined(HASEPTOPTS) */
if (TcpUdp6) {
for (h = 0; h < TcpUdp6_bucks; h++) {
if ((ti = TcpUdp6[h])) {
do {
tp = ti->next;
# if defined(HASEPTOPTS)
for (pp = ti->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
# endif /* defined(HASEPTOPTS) */
(void)free((FREE_P *)ti);
ti = tp;
} while (ti);
TcpUdp6[h] = (struct tcp_udp6 *)NULL;
}
}
}
if (TcpUdp6IPC) {
for (h = 0; h < IPCBUCKS; h++)
TcpUdp6IPC[h] = (struct tcp_udp6 *)NULL;
}
}
#endif /* defined(HASIPv6) */
/*
* check_unix() - check for UNIX domain socket
*/
static uxsin_t *check_unix(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Uxsin, INOHASH, uxsin_t, inode, i);
}
/*
* clear_uxsinfo -- clear allocated UNIX socket info
*/
void clear_uxsinfo(struct lsof_context *ctx) {
int h; /* hash index */
uxsin_t *ui, *up; /* remporary pointers */
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
pxinfo_t *pp, *pnp;
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
if (!Uxsin)
return;
for (h = 0; h < INOBUCKS; h++) {
if ((ui = Uxsin[h])) {
do {
up = ui->next;
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
for (pp = ui->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
if (ui->path)
(void)free((FREE_P *)ui->path);
if (ui->pcb)
(void)free((FREE_P *)ui->pcb);
(void)free((FREE_P *)ui);
ui = up;
} while (ui);
Uxsin[h] = (uxsin_t *)NULL;
}
}
}
/*
* get_ax25() - get /proc/net/ax25 info
*/
static void get_ax25(struct lsof_context *ctx,
char *p) /* /proc/net/ax25 path */
{
struct ax25sin *ap, *np;
FILE *as;
char buf[MAXPATHLEN], *da, *dev_ch, *ep, **fp, *sa;
int h;
INODETYPE inode;
unsigned long rq, sq, state;
MALLOC_S len;
unsigned char rqs, sqs;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
/*
* Do second time cleanup or first time setup.
*/
if (AX25sin) {
for (h = 0; h < INOBUCKS; h++) {
for (ap = AX25sin[h]; ap; ap = np) {
np = ap->next;
if (ap->da)
(void)free((FREE_P *)ap->da);
if (ap->dev_ch)
(void)free((FREE_P *)ap->dev_ch);
if (ap->sa)
(void)free((FREE_P *)ap->sa);
(void)free((FREE_P *)ap);
}
AX25sin[h] = (struct ax25sin *)NULL;
}
} else {
AX25sin = (struct ax25sin **)calloc(INOBUCKS, sizeof(struct ax25sin *));
if (!AX25sin) {
(void)fprintf(stderr,
"%s: can't allocate %d AX25 hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct ax25sin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/ax25 file, assign a page size buffer to the stream,
* and read it. Store AX25 socket info in the AX25sin[] hash buckets.
*/
if (!(as = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, as)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 24)
continue;
/*
* /proc/net/ax25 has no title line, a very poor deficiency in its
* implementation.
*
* The ax25_info_show() function in kern module .../net/ax25/af_ax25.c
* says the format of the lines in the file is:
*
* magic dev src_addr dest_addr,digi1,digi2,.. st vs vr va t1 t1 \
* t2 t2 t3 t3 idle idle n2 n2 rtt window paclen Snd-Q Rcv-Q \
* inode
*
* The code in this function is forced to assume that format is in
* effect..
*/
/*
* Assemble the inode number and see if it has already been recorded.
* If it has, skip this line.
*/
ep = (char *)NULL;
if (!fp[23] || !*fp[23] ||
(inode = strtoull(fp[23], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(AX25sin, INOHASH, struct ax25sin, inode, inode))
continue;
/*
* Assemble the send and receive queue values and the state.
*/
ep = (char *)NULL;
if (!fp[21] || !*fp[21] ||
(sq = strtoul(fp[21], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
sqs = (unsigned char)1;
ep = (char *)NULL;
if (!fp[22] || !*fp[22] ||
(rq = strtoul(fp[22], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
rqs = (unsigned char)1;
ep = (char *)NULL;
if (!fp[4] || !*fp[4] ||
(state = strtoul(fp[4], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Allocate space for the destination address.
*/
if (!fp[3] || !*fp[3])
da = (char *)NULL;
else if ((len = strlen(fp[3]))) {
if (!(da = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d destination AX25 addr bytes: %s\n",
Pn, (int)(len + 1), fp[3]);
Error(ctx);
}
(void)snpf(da, len + 1, "%s", fp[3]);
} else
da = (char *)NULL;
/*
* Allocate space for the source address.
*/
if (!fp[2] || !*fp[2])
sa = (char *)NULL;
else if ((len = strlen(fp[2]))) {
if (!(sa = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d source AX25 address bytes: %s\n", Pn,
(int)(len + 1), fp[2]);
Error(ctx);
}
(void)snpf(sa, len + 1, "%s", fp[2]);
} else
sa = (char *)NULL;
/*
* Allocate space for the device characters.
*/
if (!fp[1] || !*fp[1])
dev_ch = (char *)NULL;
else if ((len = strlen(fp[1]))) {
if (!(dev_ch = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d destination AX25 dev bytes: %s\n",
Pn, (int)(len + 1), fp[1]);
Error(ctx);
}
(void)snpf(dev_ch, len + 1, "%s", fp[1]);
} else
dev_ch = (char *)NULL;
/*
* Allocate space for an ax25sin entry, fill it, and link it to its
* hash bucket.
*/
if (!(ap = (struct ax25sin *)malloc(sizeof(struct ax25sin)))) {
(void)fprintf(stderr,
"%s: can't allocate %d byte ax25sin structure\n", Pn,
(int)sizeof(struct ax25sin));
Error(ctx);
}
ap->da = da;
ap->dev_ch = dev_ch;
ap->inode = inode;
ap->rq = rq;
ap->rqs = rqs;
ap->sa = sa;
ap->sq = sq;
ap->sqs = sqs;
ap->state = (int)state;
HASH_INSERT_ELEMENT(AX25sin, INOHASH, ap, inode);
}
(void)fclose(as);
}
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
/*
* enter_uxsinfo() -- enter unix socket info
* entry Lf = local file structure pointer
* Lp = local process structure pointer
*/
static void enter_uxsinfo(struct lsof_context *ctx, uxsin_t *up) {
pxinfo_t *pi; /* pxinfo_t structure pointer */
struct lfile *lf; /* local file structure pointer */
struct lproc *lp; /* local proc structure pointer */
pxinfo_t *np; /* new pxinfo_t structure pointer */
for (pi = up->pxinfo; pi; pi = pi->next) {
lf = pi->lf;
lp = &Lproc[pi->lpx];
if (pi->ino == Lf->inode) {
if ((lp->pid == Lp->pid) && (lf->fd_type == Lf->fd_type) &&
(lf->fd_num == Lf->fd_num))
return;
}
}
if (!(np = (pxinfo_t *)malloc(sizeof(pxinfo_t)))) {
(void)fprintf(stderr, "%s: no space for pipeinfo in uxsinfo, PID %d\n",
Pn, Lp->pid);
Error(ctx);
}
np->ino = Lf->inode;
np->lf = Lf;
np->lpx = Lp - Lproc;
np->next = up->pxinfo;
up->pxinfo = np;
}
/*
* fill_uxicino() -- fill incoming connection inode number
*/
static void fill_uxicino(struct lsof_context *ctx,
INODETYPE si, /* UNIX socket inode number */
INODETYPE ic) /* incoming UNIX socket connection
* inode number */
{
uxsin_t *psi; /* pointer to socket's information */
uxsin_t *pic; /* pointer to incoming connection's
* information */
if ((psi = check_unix(ctx, si))) {
if (psi->icstat || psi->icons)
return;
if ((pic = check_unix(ctx, ic))) {
psi->icstat = 1;
psi->icons = pic;
}
}
}
/*
* fill_uxpino() -- fill in UNIX socket's peer inode number
*/
static void fill_uxpino(struct lsof_context *ctx,
INODETYPE si, /* UNIX socket inode number */
INODETYPE pi) /* UNIX socket peer's inode number */
{
uxsin_t *pp, *up;
if ((up = check_unix(ctx, si))) {
if (!up->peer) {
if ((pp = check_unix(ctx, pi)))
up->peer = pp;
}
}
}
/*
* find_uxepti(lf) -- find UNIX socket endpoint info
*/
uxsin_t *find_uxepti(struct lsof_context *ctx,
struct lfile *lf) /* pipe's lfile */
{
uxsin_t *up;
up = check_unix(ctx, lf->inode);
return (up ? up->peer : (uxsin_t *)NULL);
}
/*
* get_diagmsg() -- get UNIX socket's diag message
*/
static int get_diagmsg(int sockfd) /* socket's file descriptor */
{
struct msghdr msg; /* message header */
struct nlmsghdr nlh; /* header length */
struct unix_diag_req creq; /* connection request */
struct sockaddr_nl sa; /* netlink socket address */
struct iovec iov[2]; /* I/O vector */
/*
* Build and send message to socket's file descriptor, asking for its
* diagnostic message.
*/
zeromem((char *)&msg, sizeof(msg));
zeromem((char *)&sa, sizeof(sa));
zeromem((char *)&nlh, sizeof(nlh));
zeromem((char *)&creq, sizeof(creq));
sa.nl_family = AF_NETLINK;
creq.sdiag_family = AF_UNIX;
creq.sdiag_protocol = 0;
memset((void *)&creq.udiag_states, -1, sizeof(creq.udiag_states));
creq.udiag_ino = (INODETYPE)0;
creq.udiag_show = UDIAG_SHOW_PEER | UDIAG_SHOW_ICONS;
nlh.nlmsg_len = NLMSG_LENGTH(sizeof(creq));
nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
iov[0].iov_base = (void *)&nlh;
iov[0].iov_len = sizeof(nlh);
iov[1].iov_base = (void *)&creq;
iov[1].iov_len = sizeof(creq);
msg.msg_name = (void *)&sa;
msg.msg_namelen = sizeof(sa);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
return (sendmsg(sockfd, &msg, 0));
}
/*
* get_uxpeeri() - get UNIX socket peer inode information
*/
static void get_uxpeeri(struct lsof_context *ctx) {
struct unix_diag_msg *dm; /* pointer to diag message */
struct nlmsghdr *hp; /* netlink structure header pointer */
int nb = 0; /* number of bytes */
int ns = 0; /* netlink socket */
uint8_t rb[SOCKET_BUFFER_SIZE]; /* receive buffer */
int rl = 0; /* route info length */
/*
* Get a netlink socket.
*/
if ((ns = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_SOCK_DIAG)) == -1) {
(void)fprintf(stderr, "%s: netlink socket error: %s\n", Pn,
strerror(errno));
Error(ctx);
}
/*
* Request peer information.
*/
if (get_diagmsg(ns) < 0) {
(void)fprintf(stderr, "%s: netlink peer request error: %s\n", Pn,
strerror(errno));
goto get_uxpeeri_exit;
}
/*
* Receive peer information.
*/
while (1) {
if ((nb = recv(ns, rb, sizeof(rb), 0)) <= 0)
goto get_uxpeeri_exit;
hp = (struct nlmsghdr *)rb;
while (NLMSG_OK(hp, nb)) {
if (hp->nlmsg_type == NLMSG_DONE)
goto get_uxpeeri_exit;
if (hp->nlmsg_type == NLMSG_ERROR) {
(void)fprintf(stderr,
"%s: netlink UNIX socket msg peer info error\n",
Pn);
goto get_uxpeeri_exit;
}
dm = (struct unix_diag_msg *)NLMSG_DATA(hp);
rl = hp->nlmsg_len - NLMSG_LENGTH(sizeof(*dm));
parse_diag(ctx, dm, rl);
hp = NLMSG_NEXT(hp, nb);
}
}
get_uxpeeri_exit:
(void)close(ns);
}
/*
* parse_diag() -- parse UNIX diag message
*/
static void parse_diag(struct lsof_context *ctx,
struct unix_diag_msg *dm, /* pointer to diag message */
int len) /* message length */
{
struct rtattr *rp; /* route info pointer */
int i; /* tmporary index */
int icct; /* incoming connection count */
uint32_t *icp; /* incoming connection pointer */
uint32_t inoc, inop; /* inode numbers */
if (!dm || (dm->udiag_family != AF_UNIX) || !(inop = dm->udiag_ino) ||
(len <= 0)) {
return;
}
rp = (struct rtattr *)(dm + 1);
/*
* Process route information.
*/
while (RTA_OK(rp, len)) {
switch (rp->rta_type) {
case UNIX_DIAG_PEER:
if (len < 4) {
(void)fprintf(stderr, "%s: unix_diag: msg length (%d) < 4)\n",
Pn, len);
return;
}
if ((inoc = *(uint32_t *)RTA_DATA(rp))) {
fill_uxpino(ctx, (INODETYPE)inop, (INODETYPE)inoc);
fill_uxpino(ctx, (INODETYPE)inoc, (INODETYPE)inop);
}
break;
case UNIX_DIAG_ICONS:
icct = RTA_PAYLOAD(rp), icp = (uint32_t *)RTA_DATA(rp);
for (i = 0; i < icct; i += sizeof(uint32_t), icp++) {
fill_uxicino(ctx, (INODETYPE)inop, (INODETYPE)*icp);
}
}
rp = RTA_NEXT(rp, len);
}
}
/*
* prt_uxs() -- print UNIX socket information
*/
static void prt_uxs(struct lsof_context *ctx, uxsin_t *p, /* peer info */
int mk) /* 1 == mark for later processing */
{
struct lproc *ep; /* socket endpoint process */
struct lfile *ef; /* socket endpoint file */
int i; /* temporary index */
int len; /* string length */
char nma[1024]; /* character buffer */
pxinfo_t *pp; /* previous pipe info of socket */
char fd[FDLEN];
(void)strcpy(nma, "->INO=");
len = (int)strlen(nma);
(void)snpf(&nma[len], sizeof(nma) - len - 1, "%" INODEPSPEC "u", p->inode);
(void)add_nma(ctx, nma, strlen(nma));
for (pp = p->pxinfo; pp; pp = pp->next) {
/*
* Add a linked socket's PID, command name and FD to the name column
* addition.
*/
ep = &Lproc[pp->lpx];
ef = pp->lf;
fd_to_string(ef->fd_type, ef->fd_num, fd);
(void)snpf(nma, sizeof(nma) - 1, "%d,%.*s,%s%c", ep->pid, CmdLim,
ep->cmd, fd, access_to_char(ef->access));
(void)add_nma(ctx, nma, strlen(nma));
if (mk && FeptE == 2) {
/*
* Endpoint files have been selected, so mark this
* one for selection later.
*/
ef->chend = CHEND_UXS;
ep->ept |= EPT_UXS_END;
}
}
}
/*
* process_uxsinfo() -- process UNIX socket information, adding it to selected
* UNIX socket files and selecting UNIX socket end point
* files (if requested)
*/
void process_uxsinfo(struct lsof_context *ctx,
int f) /* function:
* 0 == process selected socket
* 1 == process socket end point
*/
{
uxsin_t *p; /* peer UNIX socket info pointer */
uxsin_t *tp; /* temporary UNIX socket info pointer */
if (!FeptE)
return;
for (Lf = Lp->file; Lf; Lf = Lf->next) {
if (Lf->type != LSOF_FILE_UNIX)
continue;
switch (f) {
case 0:
/*
* Process already selected socket.
*/
if (is_file_sel(ctx, Lp, Lf)) {
/*
* This file has been selected by some criterion other than its
* being a socket. Look up the socket's endpoints.
*/
p = find_uxepti(ctx, Lf);
if (p && p->inode)
prt_uxs(ctx, p, 1);
if ((tp = check_unix(ctx, Lf->inode))) {
if (tp->icons) {
if (tp->icstat) {
p = tp->icons;
while (p && p != tp) {
if (p->inode)
prt_uxs(ctx, p, 1);
p = p->icons;
}
} else {
for (p = tp->icons; p && !p->icstat; p = p->icons)
; /* DO NOTHING */
if (p && p->inode)
prt_uxs(ctx, p, 1);
}
}
}
}
break;
case 1:
if (!is_file_sel(ctx, Lp, Lf) && (Lf->chend & CHEND_UXS)) {
/*
* This is an unselected end point UNIX socket file. Select it
* and add its end point information to peer's name column
* addition.
*/
Lf->sf = Selflags;
Lp->pss |= PS_SEC;
p = find_uxepti(ctx, Lf);
if (p && p->inode)
prt_uxs(ctx, p, 0);
else if ((tp = check_unix(ctx, Lf->inode))) {
if (tp->icons) {
if (tp->icstat) {
p = tp->icons;
while (p && p != tp) {
if (p->inode)
prt_uxs(ctx, p, 0);
p = p->icons;
}
} else {
for (p = tp->icons; p && !p->icstat; p = p->icons)
; /* DO NOTHING */
if (p && p->inode)
prt_uxs(ctx, p, 0);
}
}
}
}
break;
}
}
}
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
#if defined(HASEPTOPTS)
/*
* enter_netsinfo_common() -- enter inet or inet6 socket info
* tp = tcp/udp on ipv4 or ipv4 socket pointer
*/
static void enter_netsinfo_common(struct lsof_context *ctx, void *tp,
pxinfo_t *(*get_pxinfo)(void *),
void (*set_pxinfo)(void *, pxinfo_t *)) {
pxinfo_t *pi; /* pxinfo_t structure pointer */
struct lfile *lf; /* local file structure pointer */
struct lproc *lp; /* local proc structure pointer */
pxinfo_t *np; /* new pxinfo_t structure pointer */
for (pi = (*get_pxinfo)(tp); pi; pi = pi->next) {
lf = pi->lf;
lp = &Lproc[pi->lpx];
if (pi->ino == Lf->inode) {
if ((lp->pid == Lp->pid) && (lf->fd_type == Lf->fd_type) &&
(lf->fd_num == Lf->fd_num))
return;
}
}
if (!(np = (pxinfo_t *)malloc(sizeof(pxinfo_t)))) {
(void)fprintf(stderr, "%s: no space for pipeinfo in netsinfo, PID %d\n",
Pn, Lp->pid);
Error(ctx);
}
np->ino = Lf->inode;
np->lf = Lf;
np->lpx = Lp - Lproc;
np->next = (*get_pxinfo)(tp);
(*set_pxinfo)(tp, np);
}
/*
* prt_nets_common() -- print locally used INET or INET6 socket information
*/
static void prt_nets_common(struct lsof_context *ctx, void *p, /* peer info */
int mk, /* 1 == mark for later
* processing */
pxinfo_t *(*get_pxinfo)(void *),
unsigned char chend, short ept_flag) {
struct lproc *ep; /* socket endpoint process */
struct lfile *ef; /* socket endpoint file */
int i; /* temporary index */
char nma[1024]; /* character buffer */
pxinfo_t *pp; /* previous pipe info of socket */
char fd[FDLEN];
for (pp = (*get_pxinfo)(p); pp; pp = pp->next) {
/*
* Add a linked socket's PID, command name and FD to the name column
* addition.
*/
ep = &Lproc[pp->lpx];
ef = pp->lf;
fd_to_string(ef->fd_type, ef->fd_num, fd);
(void)snpf(nma, sizeof(nma) - 1, "%d,%.*s,%s%c", ep->pid, CmdLim,
ep->cmd, fd, access_to_char(ef->access));
(void)add_nma(ctx, nma, strlen(nma));
if (mk && FeptE == 2) {
/*
* Endpoint files have been selected, so mark this
* one for selection later.
*/
ef->chend = chend;
ep->ept |= ept_flag;
}
}
}
/*
* enter_netsinfo() -- enter inet socket info
* tp = tcp/udp on ipv4 socket pointer
*/
static pxinfo_t *tcp_udp_get_pxinfo(void *vp) {
struct tcp_udp *tp = vp;
return tp->pxinfo;
}
static void tcp_udp_set_pxinfo(void *vp, pxinfo_t *np) {
struct tcp_udp *tp = vp;
tp->pxinfo = np;
}
static void enter_netsinfo(struct lsof_context *ctx, struct tcp_udp *tp) {
enter_netsinfo_common(ctx, tp, tcp_udp_get_pxinfo, tcp_udp_set_pxinfo);
}
/*
* find_netsepti(lf) -- find locally used INET socket endpoint info
*/
static struct tcp_udp *find_netsepti(struct lsof_context *ctx, /* context */
struct lfile *lf) /* socket's lfile */
{
struct tcp_udp *tp;
tp = check_inet(ctx, lf->inode);
return (tp ? tp->ipc_peer : (struct tcp_udp *)NULL);
}
/*
* get_netpeeri() - get INET socket peer inode information
*/
static void get_netpeeri(struct lsof_context *ctx) {
int h;
struct tcp_udp *np, *tp;
for (h = 0; h < IPCBUCKS; h++) {
for (tp = TcpUdpIPC[h]; tp; tp = tp->ipc_next) {
if (tp->ipc_peer)
continue;
for (np = TcpUdpIPC[h]; np; np = np->ipc_next) {
if (np->ipc_peer)
continue;
if (tp->faddr == np->laddr && tp->laddr == np->faddr &&
tp->fport == np->lport && tp->lport == np->fport &&
tp->proto == np->proto) {
tp->ipc_peer = np;
np->ipc_peer = tp;
break;
}
}
}
}
}
/*
* prt_nets() -- print locally used INET socket information
*/
static void prt_nets(struct lsof_context *ctx, /* context */
struct tcp_udp *p, /* peer info */
int mk) /* 1 == mark for later processing */
{
prt_nets_common(ctx, p, mk, tcp_udp_get_pxinfo, CHEND_NETS, EPT_NETS_END);
}
/*
* process_netsinfo() -- process locally used INET socket information, adding
* it to selected INET socket files and selecting INET
* socket end point files (if requested)
*/
void process_netsinfo(struct lsof_context *ctx, /* context */
int f) /* function:
* 0 == process selected socket
* 1 == process socket end point
*/
{
struct tcp_udp *p; /* peer INET socket info pointer */
if (!FeptE)
return;
for (Lf = Lp->file; Lf; Lf = Lf->next) {
# if defined(HASIPv6)
enum lsof_file_type type = LSOF_FILE_IPV4;
# else /* !defined(HASIPv6) */
enum lsof_file_type type = LSOF_FILE_INET;
# endif /* defined(HASIPv6) */
if (Lf->type != type)
continue;
switch (f) {
case 0:
/*
* Process already selected socket.
*/
if (is_file_sel(ctx, Lp, Lf)) {
/*
* This file has been selected by some criterion other than its
* being a socket. Look up the socket's endpoints.
*/
p = find_netsepti(ctx, Lf);
if (p && p->inode)
prt_nets(ctx, p, 1);
}
break;
case 1:
if (!is_file_sel(ctx, Lp, Lf) && (Lf->chend & CHEND_NETS)) {
/*
* This is an unselected end point INET socket file. Select it
* and add its end point information to peer's name column
* addition.
*/
Lf->sf = Selflags;
Lp->pss |= PS_SEC;
p = find_netsepti(ctx, Lf);
if (p && p->inode)
prt_nets(ctx, p, 0);
}
break;
}
}
}
#endif /* defined(HASEPTOPTS) */
#if defined(HASIPv6)
# if defined(HASEPTOPTS)
/*
* enter_nets6info() -- enter inet socket info
* tp = tcp/udp on ipv6 socket pointer
*/
static pxinfo_t *tcp_udp6_get_pxinfo(void *vp) {
struct tcp_udp6 *tp = vp;
return tp->pxinfo;
}
static void tcp_udp6_set_pxinfo(void *vp, pxinfo_t *np) {
struct tcp_udp6 *tp = vp;
tp->pxinfo = np;
}
static void enter_nets6info(struct lsof_context *ctx, struct tcp_udp6 *tp) {
enter_netsinfo_common(ctx, tp, tcp_udp6_get_pxinfo, tcp_udp6_set_pxinfo);
}
/*
* find_nets6epti(lf) -- find locally used INET6 socket endpoint info
*/
static struct tcp_udp6 *find_nets6epti(struct lsof_context *ctx, /* context */
struct lfile *lf) /* socket's lfile */
{
struct tcp_udp6 *tp;
tp = check_inet6(ctx, lf->inode);
return (tp ? tp->ipc_peer : (struct tcp_udp6 *)NULL);
}
/*
* get_net6peeri() - get INET6 socket peer inode information
*/
static void get_net6peeri(struct lsof_context *ctx) {
int h;
struct tcp_udp6 *np, *tp;
for (h = 0; h < IPCBUCKS; h++) {
for (tp = TcpUdp6IPC[h]; tp; tp = tp->ipc_next) {
if (tp->ipc_peer)
continue;
for (np = TcpUdp6IPC[h]; np; np = np->ipc_next) {
if (np->ipc_peer)
continue;
if (IN6_ARE_ADDR_EQUAL(&tp->faddr, &np->laddr) &&
IN6_ARE_ADDR_EQUAL(&tp->laddr, &np->faddr) &&
tp->fport == np->lport && tp->lport == np->fport &&
tp->proto == np->proto) {
tp->ipc_peer = np;
np->ipc_peer = tp;
break;
}
}
}
}
}
/*
* prt_nets6() -- print locally used INET6 socket information
*/
static void prt_nets6(struct lsof_context *ctx, /* context */
struct tcp_udp6 *p, /* peer info */
int mk) /* 1 == mark for later processing */
{
prt_nets_common(ctx, p, mk, tcp_udp6_get_pxinfo, CHEND_NETS6,
EPT_NETS6_END);
}
/*
* process_nets6info() -- process locally used INET6 socket information, adding
* it to selected INET6 socket files and selecting INET6
* socket end point files (if requested)
*/
void process_nets6info(struct lsof_context *ctx, /* context */
int f) /* function:
* 0 == process selected socket
* 1 == process socket end point
*/
{
struct tcp_udp6 *p; /* peer INET6 socket info pointer */
if (!FeptE)
return;
for (Lf = Lp->file; Lf; Lf = Lf->next) {
if (Lf->type != LSOF_FILE_IPV6)
continue;
switch (f) {
case 0:
/*
* Process already selected socket.
*/
if (is_file_sel(ctx, Lp, Lf)) {
/*
* This file has been selected by some criterion other than its
* being a socket. Look up the socket's endpoints.
*/
p = find_nets6epti(ctx, Lf);
if (p && p->inode)
prt_nets6(ctx, p, 1);
}
break;
case 1:
if (!is_file_sel(ctx, Lp, Lf) && (Lf->chend & CHEND_NETS6)) {
/*
* This is an unselected end point INET6 socket file. Select it
* and add its end point information to peer's name column
* addition.
*/
Lf->sf = Selflags;
Lp->pss |= PS_SEC;
p = find_nets6epti(ctx, Lf);
if (p && p->inode)
prt_nets6(ctx, p, 0);
}
break;
}
}
}
# endif /* defined(HASEPTOPTS) */
#endif /* defined(HASIPv6) */
/*
* get_icmp() - get ICMP net info
*/
static void get_icmp(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/icmp path */
{
char buf[MAXPATHLEN], *ep, **fp, *la, *ra;
int fl = 1;
int h;
INODETYPE inode;
struct icmpin *np, *icmpp;
MALLOC_S lal, ral;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Icmpin) {
for (h = 0; h < INOBUCKS; h++) {
for (icmpp = Icmpin[h]; icmpp; icmpp = np) {
np = icmpp->next;
(void)free((FREE_P *)icmpp);
}
Icmpin[h] = (struct icmpin *)NULL;
}
} else {
Icmpin = (struct icmpin **)calloc(INOBUCKS, sizeof(struct icmpin *));
if (!Icmpin) {
(void)fprintf(stderr,
"%s: can't allocate %d icmp hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct icmpin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/icmp file, assign a page size buffer to its stream,
* and read the file. Store icmp info in the Icmpin[] hash buckets.
*/
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 11)
continue;
if (fl) {
/*
* Check the column labels in the first line.
*
* NOTE:
* In column header, "inode" is at the 11th column.
* However, in data rows, inode appears at the 9th column.
*
* In column header, "tx_queue" and "rx_queue" are separated
* by a space. It is the same for "tr" and "tm->when"; in
* data rows they are connected with ":".
*/
if (!fp[1] || strcmp(fp[1], "local_address") || !fp[2] ||
strcmp(fp[2], "rem_address") || !fp[11] ||
strcmp(fp[11], "inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
fl = 0;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[9] || !*fp[9] ||
(inode = strtoull(fp[9], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Icmpin, INOHASH, struct icmpin, inode, inode))
continue;
/*
* Save the local address, and remote address.
*/
if (!fp[1] || !*fp[1] || (lal = strlen(fp[1])) < 1) {
la = (char *)NULL;
lal = (MALLOC_S)0;
} else {
if (!(la = (char *)malloc(lal + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d local icmp address bytes: %s\n", Pn,
(int)(lal + 1), fp[1]);
Error(ctx);
}
(void)snpf(la, lal + 1, "%s", fp[1]);
}
if (!fp[2] || !*fp[2] || (ral = strlen(fp[2])) < 1) {
ra = (char *)NULL;
ral = (MALLOC_S)0;
} else {
if (!(ra = (char *)malloc(ral + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d remote icmp address bytes: %s\n", Pn,
(int)(ral + 1), fp[2]);
Error(ctx);
}
(void)snpf(ra, ral + 1, "%s", fp[2]);
}
/*
* Allocate space for a icmpin entry, fill it, and link it to its
* hash bucket.
*/
if (!(icmpp = (struct icmpin *)malloc(sizeof(struct icmpin)))) {
(void)fprintf(stderr, "%s: can't allocate %d byte icmp structure\n",
Pn, (int)sizeof(struct icmpin));
Error(ctx);
}
icmpp->inode = inode;
icmpp->la = la;
icmpp->lal = lal;
icmpp->ra = ra;
icmpp->ral = ral;
HASH_INSERT_ELEMENT(Icmpin, INOHASH, icmpp, inode);
}
(void)fclose(xs);
}
/*
* get_ipx() - get /proc/net/ipx info
*/
static void get_ipx(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/ipx path */
{
char buf[MAXPATHLEN], *ep, **fp, *la, *ra;
int fl = 1;
int h;
INODETYPE inode;
unsigned long rxq, state, txq;
struct ipxsin *ip, *np;
MALLOC_S len;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Ipxsin) {
for (h = 0; h < INOBUCKS; h++) {
for (ip = Ipxsin[h]; ip; ip = np) {
np = ip->next;
if (ip->la)
(void)free((FREE_P *)ip->la);
if (ip->ra)
(void)free((FREE_P *)ip->ra);
(void)free((FREE_P *)ip);
}
Ipxsin[h] = (struct ipxsin *)NULL;
}
} else {
Ipxsin = (struct ipxsin **)calloc(INOBUCKS, sizeof(struct ipxsin *));
if (!Ipxsin) {
(void)fprintf(stderr,
"%s: can't allocate %d IPX hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct ipxsin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/ipx file, assign a page size buffer to the stream,
* and read it. Store IPX socket info in the Ipxsin[] hash buckets.
*/
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 7)
continue;
if (fl) {
/*
* Check the column labels in the first line.
*/
if (!fp[0] || strcmp(fp[0], "Local_Address") || !fp[1] ||
strcmp(fp[1], "Remote_Address") || !fp[2] ||
strcmp(fp[2], "Tx_Queue") || !fp[3] ||
strcmp(fp[3], "Rx_Queue") || !fp[4] || strcmp(fp[4], "State") ||
!fp[5] || strcmp(fp[5], "Uid") || !fp[6] ||
strcmp(fp[6], "Inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
fl = 0;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[6] || !*fp[6] ||
(inode = strtoull(fp[6], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Ipxsin, INOHASH, struct ipxsin, inode, inode))
continue;
/*
* Assemble the transmit and receive queue values and the state.
*/
ep = (char *)NULL;
if (!fp[2] || !*fp[2] || (txq = strtoul(fp[2], &ep, 16)) == ULONG_MAX ||
!ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[3] || !*fp[3] || (rxq = strtoul(fp[3], &ep, 16)) == ULONG_MAX ||
!ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[4] || !*fp[4] ||
(state = strtoul(fp[4], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Allocate space for the local address, unless it is "Not_Connected".
*/
if (!fp[0] || !*fp[0] || strcmp(fp[0], "Not_Connected") == 0)
la = (char *)NULL;
else if ((len = strlen(fp[0]))) {
if (!(la = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d local IPX address bytes: %s\n", Pn,
(int)(len + 1), fp[0]);
Error(ctx);
}
(void)snpf(la, len + 1, "%s", fp[0]);
} else
la = (char *)NULL;
/*
* Allocate space for the remote address, unless it is "Not_Connected".
*/
if (!fp[1] || !*fp[1] || strcmp(fp[1], "Not_Connected") == 0)
ra = (char *)NULL;
else if ((len = strlen(fp[1]))) {
if (!(ra = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d remote IPX address bytes: %s\n", Pn,
(int)(len + 1), fp[1]);
Error(ctx);
}
(void)snpf(ra, len + 1, "%s", fp[1]);
} else
ra = (char *)NULL;
/*
* Allocate space for an ipxsin entry, fill it, and link it to its
* hash bucket.
*/
if (!(ip = (struct ipxsin *)malloc(sizeof(struct ipxsin)))) {
(void)fprintf(stderr,
"%s: can't allocate %d byte ipxsin structure\n", Pn,
(int)sizeof(struct ipxsin));
Error(ctx);
}
ip->inode = inode;
ip->la = la;
ip->ra = ra;
ip->txq = txq;
ip->rxq = rxq;
ip->state = (int)state;
HASH_INSERT_ELEMENT(Ipxsin, INOHASH, ip, inode);
}
(void)fclose(xs);
}
/*
* get_netlink() - get /proc/net/netlink info
*/
static void get_netlink(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/netlink path */
{
char buf[MAXPATHLEN], *ep, **fp;
int fr = 1;
int h, pr;
INODETYPE inode;
struct nlksin *np, *lp;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Nlksin) {
for (h = 0; h < INOBUCKS; h++) {
for (lp = Nlksin[h]; lp; lp = np) {
np = lp->next;
(void)free((FREE_P *)lp);
}
Nlksin[h] = (struct nlksin *)NULL;
}
} else {
Nlksin = (struct nlksin **)calloc(INOBUCKS, sizeof(struct nlksin *));
if (!Nlksin) {
(void)fprintf(stderr,
"%s: can't allocate %d netlink hash pointer bytes\n",
Pn, (int)(INOBUCKS * sizeof(struct nlksin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/netlink file, assign a page size buffer to its stream,
* and read the file. Store Netlink info in the Nlksin[] hash buckets.
*/
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 10)
continue;
if (fr) {
/*
* Check the column labels in the first line.
*/
if (!fp[1] || strcmp(fp[1], "Eth") || !fp[9] ||
strcmp(fp[9], "Inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
fr = 0;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[9] || !*fp[9] ||
(inode = strtoull(fp[9], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Nlksin, INOHASH, struct nlksin, inode, inode))
continue;
/*
* Save the protocol from the Eth column.
*/
if (!fp[1] || !*fp[1] || (strlen(fp[1])) < 1)
continue;
pr = atoi(fp[1]);
/*
* Allocate space for a nlksin entry, fill it, and link it to its
* hash bucket.
*/
if (!(lp = (struct nlksin *)malloc(sizeof(struct nlksin)))) {
(void)fprintf(stderr,
"%s: can't allocate %d byte Netlink structure\n", Pn,
(int)sizeof(struct nlksin));
Error(ctx);
}
lp->inode = inode;
lp->pr = pr;
HASH_INSERT_ELEMENT(Nlksin, INOHASH, lp, inode);
}
(void)fclose(xs);
}
/*
* get_pack() - get /proc/net/packet info
*/
static void get_pack(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/raw path */
{
char buf[MAXPATHLEN], *ep, **fp;
int fl = 1;
int h, ty;
INODETYPE inode;
struct packin *np, *pp;
unsigned long pr;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Packin) {
for (h = 0; h < INOBUCKS; h++) {
for (pp = Packin[h]; pp; pp = np) {
np = pp->next;
(void)free((FREE_P *)pp);
}
Packin[h] = (struct packin *)NULL;
}
} else {
Packin = (struct packin **)calloc(INOBUCKS, sizeof(struct packin *));
if (!Packin) {
(void)fprintf(stderr,
"%s: can't allocate %d packet hash pointer bytes\n",
Pn, (int)(INOBUCKS * sizeof(struct packin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/packet file, assign a page size buffer to its stream,
* and read the file. Store packet info in the Packin[] hash buckets.
*/
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 9)
continue;
if (fl) {
/*
* Check the column labels in the first line.
*/
if (!fp[2] || strcmp(fp[2], "Type") || !fp[3] ||
strcmp(fp[3], "Proto") || !fp[8] || strcmp(fp[8], "Inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
fl = 0;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[8] || !*fp[8] ||
(inode = strtoull(fp[8], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Packin, INOHASH, struct packin, inode, inode))
continue;
/*
* Save the socket type and protocol.
*/
if (!fp[2] || !*fp[2] || (strlen(fp[2])) < 1)
continue;
ty = atoi(fp[2]);
ep = (char *)NULL;
if (!fp[3] || !*fp[3] || (strlen(fp[3]) < 1) ||
((pr = strtoul(fp[3], &ep, 16)) == ULONG_MAX) || !ep || *ep)
continue;
/*
* Allocate space for a packin entry, fill it, and link it to its
* hash bucket.
*/
if (!(pp = (struct packin *)malloc(sizeof(struct packin)))) {
(void)fprintf(stderr,
"%s: can't allocate %d byte packet structure\n", Pn,
(int)sizeof(struct packin));
Error(ctx);
}
pp->inode = inode;
pp->pr = (int)pr;
pp->ty = ty;
HASH_INSERT_ELEMENT(Packin, INOHASH, pp, inode);
}
(void)fclose(xs);
}
/*
* get_raw() - get /proc/net/raw info
*/
static void get_raw(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/raw path */
{
char buf[MAXPATHLEN], *ep, **fp, *la, *ra, *sp;
int h;
INODETYPE inode;
int nf = 12;
struct rawsin *np, *rp;
MALLOC_S lal, ral, spl;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Rawsin) {
for (h = 0; h < INOBUCKS; h++) {
for (rp = Rawsin[h]; rp; rp = np) {
np = rp->next;
if (rp->la)
(void)free((FREE_P *)rp->la);
if (rp->ra)
(void)free((FREE_P *)rp->ra);
(void)free((FREE_P *)rp);
}
Rawsin[h] = (struct rawsin *)NULL;
}
} else {
Rawsin = (struct rawsin **)calloc(INOBUCKS, sizeof(struct rawsin *));
if (!Rawsin) {
(void)fprintf(stderr,
"%s: can't allocate %d raw hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct rawsin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/raw file, assign a page size buffer to its stream,
* and read the file. Store raw socket info in the Rawsin[] hash buckets.
*/
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < nf)
continue;
if (nf == 12) {
/*
* Check the column labels in the first line.
*/
if (!fp[1] || strcmp(fp[1], "local_address") || !fp[2] ||
strcmp(fp[2], "rem_address") || !fp[3] || strcmp(fp[3], "st") ||
!fp[11] || strcmp(fp[11], "inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
nf = 10;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[9] || !*fp[9] ||
(inode = strtoull(fp[9], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Rawsin, INOHASH, struct rawsin, inode, inode))
continue;
/*
* Save the local address, remote address, and state.
*/
if (!fp[1] || !*fp[1] || (lal = strlen(fp[1])) < 1) {
la = (char *)NULL;
lal = (MALLOC_S)0;
} else {
if (!(la = (char *)malloc(lal + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d local raw address bytes: %s\n", Pn,
(int)(lal + 1), fp[1]);
Error(ctx);
}
(void)snpf(la, lal + 1, "%s", fp[1]);
}
if (!fp[2] || !*fp[2] || (ral = strlen(fp[2])) < 1) {
ra = (char *)NULL;
ral = (MALLOC_S)0;
} else {
if (!(ra = (char *)malloc(ral + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d remote raw address bytes: %s\n", Pn,
(int)(ral + 1), fp[2]);
Error(ctx);
}
(void)snpf(ra, ral + 1, "%s", fp[2]);
}
if (!fp[3] || !*fp[3] || (spl = strlen(fp[3])) < 1) {
sp = (char *)NULL;
spl = (MALLOC_S)0;
} else {
if (!(sp = (char *)malloc(spl + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d remote raw state bytes: %s\n", Pn,
(int)(spl + 1), fp[2]);
Error(ctx);
}
(void)snpf(sp, spl + 1, "%s", fp[3]);
}
/*
* Allocate space for an rawsin entry, fill it, and link it to its
* hash bucket.
*/
if (!(rp = (struct rawsin *)malloc(sizeof(struct rawsin)))) {
(void)fprintf(stderr,
"%s: can't allocate %d byte rawsin structure\n", Pn,
(int)sizeof(struct rawsin));
Error(ctx);
}
rp->inode = inode;
rp->la = la;
rp->lal = lal;
rp->ra = ra;
rp->ral = ral;
rp->sp = sp;
rp->spl = spl;
HASH_INSERT_ELEMENT(Rawsin, INOHASH, rp, inode);
}
(void)fclose(xs);
}
/*
* get_sctp() - get /proc/net/sctp/assocs info
*/
static void get_sctp(struct lsof_context *ctx) {
char buf[MAXPATHLEN], *a, *ep, **fp, *id, *la, *lp, *ra, *rp, *ta;
int d, err, fl, h, i, j, nf, ty, x;
INODETYPE inode;
MALLOC_S len, plen;
struct sctpsin *sp, *np;
FILE *ss;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
/*
* Do second time cleanup or first time setup.
*/
if (SCTPsin) {
for (h = 0; h < INOBUCKS; h++) {
for (sp = SCTPsin[h]; sp; sp = np) {
np = sp->next;
if (sp->addr)
(void)free((FREE_P *)sp->addr);
if (sp->assocID)
(void)free((FREE_P *)sp->assocID);
if (sp->lport)
(void)free((FREE_P *)sp->lport);
if (sp->rport)
(void)free((FREE_P *)sp->rport);
if (sp->laddrs)
(void)free((FREE_P *)sp->laddrs);
if (sp->raddrs)
(void)free((FREE_P *)sp->raddrs);
(void)free((FREE_P *)sp);
}
SCTPsin[h] = (struct sctpsin *)NULL;
}
} else {
SCTPsin = (struct sctpsin **)calloc(INOBUCKS, sizeof(struct sctpsin *));
if (!SCTPsin) {
(void)fprintf(stderr,
"%s: can't allocate %d SCTP hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct sctpsin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/sctp files, assign a page size buffer to the streams,
* and read them. Store SCTP socket info in the SCTPsin[] hash buckets.
*/
for (i = 0; i < NSCTPPATHS; i++) {
if (!(ss = open_proc_stream(ctx, SCTPPath[i], "r", &vbuf, &vsz, 0)))
continue;
fl = 1;
while (fgets(buf, sizeof(buf) - 1, ss)) {
if ((nf = get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0)) <
(i ? 9 : 16)) {
continue;
}
if (fl) {
/*
* Check the column labels in the first line.
*/
err = 0;
switch (i) {
case 0:
if (!fp[0] || strcmp(fp[0], "ASSOC") || !fp[6] ||
strcmp(fp[6], "ASSOC-ID") || !fp[10] ||
strcmp(fp[10], "INODE") || !fp[11] ||
strcmp(fp[11], "LPORT") || !fp[12] ||
strcmp(fp[12], "RPORT") || !fp[13] ||
strcmp(fp[13], "LADDRS") || !fp[14] ||
strcmp(fp[14], "<->") || !fp[15] ||
strcmp(fp[15], "RADDRS")) {
err = 1;
}
break;
case 1:
if (!fp[0] || strcmp(fp[0], "ENDPT") || !fp[5] ||
strcmp(fp[5], "LPORT") || !fp[7] ||
strcmp(fp[7], "INODE") || !fp[8] ||
strcmp(fp[8], "LADDRS")) {
err = 1;
}
}
if (err) {
if (!Fwarn)
(void)fprintf(stderr,
"%s: WARNING: unsupported format: %s\n",
Pn, SCTPPath[i]);
break;
}
fl = 0;
continue;
}
/*
* Assemble the inode number and see if it has already been
* recorded.
*/
ep = (char *)NULL;
j = i ? 7 : 10;
if (!fp[j] || !*fp[j] ||
(inode = strtoull(fp[j], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
sp = HASH_FIND_ELEMENT(SCTPsin, INOHASH, struct sctpsin, inode,
inode);
/*
* Set the entry type.
*/
if (sp)
ty = (sp->type == i) ? i : 3;
else
ty = i;
/*
* Allocate space for this line's sctpsin members.
*
* The association or endpoint address is in the first field.
*/
a = sp ? sp->addr : (char *)NULL;
if (fp[0] && *fp[0] && (len = strlen(fp[0]))) {
if (a) {
if (isainb(fp[0], a)) {
plen = strlen(a);
a = (char *)realloc((MALLOC_P *)a, plen + len + 2);
d = 0;
} else
d = 1;
} else {
plen = (MALLOC_S)0;
a = (char *)malloc(len + 1);
d = 0;
}
if (!a) {
(void)fprintf(
stderr, "%s: can't allocate %d SCTP ASSOC bytes: %s\n",
Pn, (int)(len + 1), fp[0]);
Error(ctx);
}
if (!d) {
if (plen)
(void)snpf((a + plen), len + 2, ",%s", fp[0]);
else
(void)snpf(a, len + 1, "%s", fp[0]);
}
}
/*
* The association ID is in the seventh field.
*/
id = sp ? sp->assocID : (char *)NULL;
if (!i && fp[6] && *fp[6] && (len = strlen(fp[6]))) {
if (id) {
if (isainb(fp[6], id)) {
plen = strlen(id);
id = (char *)realloc((MALLOC_P *)id, plen + len + 2);
d = 0;
} else
d = 1;
} else {
plen = (MALLOC_S)0;
id = (char *)malloc(len + 1);
d = 0;
}
if (!id) {
(void)fprintf(
stderr,
"%s: can't allocate %d SCTP ASSOC-ID bytes: %s\n", Pn,
(int)(len + 1), fp[6]);
Error(ctx);
}
if (!d) {
if (plen)
(void)snpf((id + plen), len + 2, ",%s", fp[6]);
else
(void)snpf(id, len + 1, "%s", fp[6]);
}
}
/*
* The field number for the local port depends on the entry type.
*/
j = i ? 5 : 11;
lp = sp ? sp->lport : (char *)NULL;
if (fp[j] && *fp[j] && (len = strlen(fp[j]))) {
if (lp) {
if (isainb(fp[j], lp)) {
plen = strlen(lp);
lp = (char *)realloc((MALLOC_P *)lp, plen + len + 2);
d = 0;
} else
d = 1;
} else {
plen = (MALLOC_S)0;
lp = (char *)malloc(len + 1);
d = 0;
}
if (!lp) {
(void)fprintf(
stderr, "%s: can't allocate %d SCTP LPORT bytes: %s\n",
Pn, (int)(len + 1), fp[j]);
Error(ctx);
}
if (!d) {
if (plen)
(void)snpf((lp + plen), len + 2, ",%s", fp[j]);
else
(void)snpf(lp, len + 1, "%s", fp[j]);
}
}
/*
* The field number for the remote port depends on the entry type.
*/
rp = sp ? sp->rport : (char *)NULL;
if (!i && fp[12] && *fp[12] && (len = strlen(fp[12]))) {
if (rp) {
if (isainb(fp[12], rp)) {
plen = strlen(rp);
rp = (char *)realloc((MALLOC_P *)rp, plen + len + 2);
d = 0;
} else
d = 1;
} else {
plen = (MALLOC_S)0;
rp = (char *)malloc(len + 1);
d = 0;
}
if (!rp) {
(void)fprintf(
stderr, "%s: can't allocate %d SCTP RPORT bytes: %s\n",
Pn, (int)(len + 1), fp[12]);
Error(ctx);
}
if (!d) {
if (plen)
(void)snpf((rp + plen), len + 2, ",%s", fp[12]);
else
(void)snpf(rp, len + 1, "%s", fp[12]);
}
}
/*
* The local addresses begin in a field whose number depends on
* the entry type.
*/
j = i ? 8 : 13;
la = sp ? sp->laddrs : (char *)NULL;
x = 0;
if (fp[j] && *fp[j] && (len = strlen(fp[j]))) {
if (!(ta = get_sctpaddrs(fp, j, nf, &x))) {
(void)fprintf(stderr,
"%s: can't allocate %d SCTP LADDRS bytes\n",
Pn, (int)len);
Error(ctx);
}
if (la) {
if (isainb(ta, la)) {
len = strlen(ta);
plen = strlen(la);
if (!(la = (char *)realloc((MALLOC_P *)la,
plen + len + 2))) {
(void)fprintf(
stderr,
"%s: can't reallocate %d SCTP LADDRS bytes\n",
Pn, (int)len);
Error(ctx);
}
(void)snpf(la + plen, len + 2, ",%s", ta);
(void)free((FREE_P *)ta);
}
} else
la = ta;
}
/*
* The remote addresses begin after the local addresses, but only
* for the ASSOC type.
*/
ra = sp ? sp->raddrs : (char *)NULL;
if (!i && x && fp[x + 1] && *fp[x + 1] &&
(len = strlen(fp[x + 1]))) {
if (!(ta = get_sctpaddrs(fp, x + 1, nf, &x))) {
(void)fprintf(stderr,
"%s: can't allocate %d SCTP RADDRS bytes\n",
Pn, (int)len);
Error(ctx);
}
if (ra) {
if (isainb(ta, ra)) {
len = strlen(ta);
plen = strlen(ra);
if (!(ra = (char *)realloc((MALLOC_P *)ra,
plen + len + 2))) {
(void)fprintf(
stderr,
"%s: can't reallocate %d SCTP RADDRS bytes\n",
Pn, (int)len);
Error(ctx);
}
(void)snpf(ra + plen, len + 2, ",%s", ta);
(void)free((FREE_P *)ta);
}
} else
ra = ta;
}
/*
* If no matching sctpsin entry was found for this inode, allocate
* space for a new sctpsin entry, fill it, and link it to its hash
* bucket. Update a matching entry.
*/
if (!sp) {
if (!(sp = (struct sctpsin *)malloc(sizeof(struct sctpsin)))) {
(void)fprintf(
stderr,
"%s: can't allocate %d byte sctpsin structure\n", Pn,
(int)sizeof(struct sctpsin));
Error(ctx);
}
sp->inode = inode;
HASH_INSERT_ELEMENT(SCTPsin, INOHASH, sp, inode);
}
sp->addr = a;
sp->assocID = id;
sp->lport = lp;
sp->rport = rp;
sp->laddrs = la;
sp->raddrs = ra;
sp->type = ty;
}
(void)fclose(ss);
}
}
static char *get_sctpaddrs(char **fp, /* field pointers */
int i, /* first address field index in fp */
int nf, /* number of fields */
int *x) /* index of first "<->" field entry */
{
MALLOC_S al = (MALLOC_S)0;
char *cp = (char *)NULL;
MALLOC_S tl;
*x = 0;
do {
if ((i >= nf) || !fp[i] || !*fp[i] || !(tl = strlen(fp[i])))
break;
if (!strcmp(fp[i], "<->")) {
*x = i;
break;
}
if (!strchr(fp[i], (int)'.') && !strchr(fp[i], (int)':'))
break;
if (cp)
cp = (char *)realloc((MALLOC_P *)cp, al + tl + 1);
else
cp = (char *)malloc(al + tl + 1);
if (!cp)
break;
if (al)
*(cp + al - 1) = ',';
(void)strncpy(al ? (cp + al) : cp, fp[i], tl);
al += (tl + 1);
*(cp + al - 1) = '\0';
} while (++i < nf);
return (cp);
}
/*
* get_tcpudp() - get IPv4 TCP, UDP or UDPLITE net info
*/
static void get_tcpudp(struct lsof_context *ctx, /* context */
char *p, /* /proc/net/{tcp,udp} path */
int pr, /* protocol: 0 = TCP, 1 = UDP,
* 2 = UDPLITE */
int clr) /* 1 == clear the table */
{
char buf[MAXPATHLEN], *ep, **fp;
unsigned long faddr, fport, laddr, lport, rxq, state, txq;
FILE *fs;
int h, nf;
INODETYPE inode;
struct tcp_udp *np, *tp;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
#if defined(HASEPTOPTS)
pxinfo_t *pp, *pnp;
#endif /* defined(HASEPTOPTS) */
/*
* Delete previous table contents.
*/
if (TcpUdp) {
if (clr) {
for (h = 0; h < TcpUdp_bucks; h++) {
for (tp = TcpUdp[h]; tp; tp = np) {
np = tp->next;
#if defined(HASEPTOPTS)
for (pp = tp->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
#endif /* defined(HASEPTOPTS) */
(void)free((FREE_P *)tp);
}
TcpUdp[h] = (struct tcp_udp *)NULL;
}
#if defined(HASEPTOPTS)
if (FeptE)
for (h = 0; h < IPCBUCKS; h++)
TcpUdpIPC[h] = (struct tcp_udp *)NULL;
#endif /* defined(HASEPTOPTS) */
}
/*
* If no hash buckets have been allocated, do so now.
*/
} else {
/*
* Open the /proc/net/sockstat file and establish the hash bucket
* count from its "sockets: used" line.
*/
TcpUdp_bucks = INOBUCKS;
if ((fs = fopen(SockStatPath, "r"))) {
while (fgets(buf, sizeof(buf) - 1, fs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) !=
3)
continue;
if (!fp[0] || strcmp(fp[0], "sockets:") || !fp[1] ||
strcmp(fp[1], "used") || !fp[2] || !*fp[2])
continue;
if ((h = atoi(fp[2])) < 1)
h = INOBUCKS;
while (TcpUdp_bucks < h)
TcpUdp_bucks *= 2;
break;
}
(void)fclose(fs);
}
if (!(TcpUdp = (struct tcp_udp **)calloc(TcpUdp_bucks,
sizeof(struct tcp_udp *)))) {
(void)fprintf(
stderr,
"%s: can't allocate %d bytes for TCP&UDP hash buckets\n", Pn,
(int)(TcpUdp_bucks * sizeof(struct tcp_udp *)));
Error(ctx);
}
#if defined(HASEPTOPTS)
if (FeptE && (!(TcpUdpIPC = (struct tcp_udp **)calloc(
IPCBUCKS, sizeof(struct tcp_udp *))))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for TCP&UDP local IPC "
"hash buckets\n",
Pn, (int)(IPCBUCKS * sizeof(struct tcp_udp *)));
Error(ctx);
}
#endif /* defined(HASEPTOPTS) */
}
/*
* Open the /proc/net file, assign a page size buffer to the stream, and
* read it.
*/
if (!(fs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
nf = 12;
while (fgets(buf, sizeof(buf) - 1, fs)) {
if (get_fields(ctx, buf, (nf == 12) ? (char *)NULL : ":", &fp,
(int *)NULL, 0) < nf)
continue;
if (nf == 12) {
if (!fp[1] || strcmp(fp[1], "local_address") || !fp[2] ||
strcmp(fp[2], "rem_address") || !fp[3] || strcmp(fp[3], "st") ||
!fp[4] || strcmp(fp[4], "tx_queue") || !fp[5] ||
strcmp(fp[5], "rx_queue") || !fp[11] ||
strcmp(fp[11], "inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
nf = 14;
continue;
}
/*
* Get the local and remote addresses.
*/
ep = (char *)NULL;
if (!fp[1] || !*fp[1] ||
(laddr = strtoul(fp[1], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[2] || !*fp[2] ||
(lport = strtoul(fp[2], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[3] || !*fp[3] ||
(faddr = strtoul(fp[3], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[4] || !*fp[4] ||
(fport = strtoul(fp[4], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Get the state and queue sizes.
*/
ep = (char *)NULL;
if (!fp[5] || !*fp[5] ||
(state = strtoul(fp[5], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[6] || !*fp[6] || (txq = strtoul(fp[6], &ep, 16)) == ULONG_MAX ||
!ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[7] || !*fp[7] || (rxq = strtoul(fp[7], &ep, 16)) == ULONG_MAX ||
!ep || *ep)
continue;
/*
* Get the inode and use it for hashing and searching.
*/
ep = (char *)NULL;
if (!fp[13] || !*fp[13] ||
(inode = strtoull(fp[13], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
if (HASH_FIND_ELEMENT(TcpUdp, TCPUDPHASH, struct tcp_udp, inode, inode))
continue;
/*
* Create a new entry and link it to its hash bucket.
*/
if (!(tp = (struct tcp_udp *)malloc(sizeof(struct tcp_udp)))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for tcp_udp struct\n",
Pn, (int)sizeof(struct tcp_udp));
Error(ctx);
}
tp->inode = inode;
tp->faddr = faddr;
tp->fport = (int)(fport & 0xffff);
tp->laddr = laddr;
tp->lport = (int)(lport & 0xffff);
tp->txq = txq;
tp->rxq = rxq;
tp->proto = pr;
tp->state = (int)state;
HASH_INSERT_ELEMENT(TcpUdp, TCPUDPHASH, tp, inode);
#if defined(HASEPTOPTS)
tp->pxinfo = (pxinfo_t *)NULL;
if (FeptE) {
tp->ipc_peer = (struct tcp_udp *)NULL;
if (tp->state == TCP_ESTABLISHED) {
int i = TCPUDP_IPC_HASH(tp);
tp->ipc_next = TcpUdpIPC[i];
TcpUdpIPC[i] = tp;
}
}
#endif /* defined(HASEPTOPTS) */
}
#if defined(HASEPTOPTS)
/*
* If endpoint info has been requested, link INET socket peer info.
*/
if (FeptE)
get_netpeeri(ctx);
#endif /* defined(HASEPTOPTS) */
(void)fclose(fs);
}
#if defined(HASIPv6)
/*
* get_raw6() - get /proc/net/raw6 info
*/
static void get_raw6(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/raw path */
{
char buf[MAXPATHLEN], *ep, **fp, *la, *ra, *sp;
int h;
INODETYPE inode;
int nf = 12;
struct rawsin *np, *rp;
MALLOC_S lal, ral, spl;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
FILE *xs;
/*
* Do second time cleanup or first time setup.
*/
if (Rawsin6) {
for (h = 0; h < INOBUCKS; h++) {
for (rp = Rawsin6[h]; rp; rp = np) {
np = rp->next;
if (rp->la)
(void)free((FREE_P *)rp->la);
if (rp->ra)
(void)free((FREE_P *)rp->ra);
if (rp->sp)
(void)free((FREE_P *)rp->sp);
(void)free((FREE_P *)rp);
}
Rawsin6[h] = (struct rawsin *)NULL;
}
} else {
Rawsin6 = (struct rawsin **)calloc(INOBUCKS, sizeof(struct rawsin *));
if (!Rawsin6) {
(void)fprintf(stderr,
"%s: can't allocate %d raw6 hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct rawsin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/raw6 file, assign a page size buffer to the stream,
* and read it. Store raw6 socket info in the Rawsin6[] hash buckets.
*/
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, xs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < nf)
continue;
if (nf == 12) {
/*
* Check the column labels in the first line.
*/
if (!fp[1] || strcmp(fp[1], "local_address") || !fp[2] ||
strcmp(fp[2], "remote_address") || !fp[3] ||
strcmp(fp[3], "st") || !fp[11] || strcmp(fp[11], "inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
nf = 10;
continue;
}
/*
* Assemble the inode number and see if the inode is already
* recorded.
*/
ep = (char *)NULL;
if (!fp[9] || !*fp[9] ||
(inode = strtoull(fp[9], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Rawsin6, INOHASH, struct rawsin, inode, inode))
continue;
/*
* Save the local address, remote address, and state.
*/
if (!fp[1] || !*fp[1] || (lal = strlen(fp[1])) < 1) {
la = (char *)NULL;
lal = (MALLOC_S)0;
} else {
if (!(la = (char *)malloc(lal + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d local raw6 address bytes: %s\n", Pn,
(int)(lal + 1), fp[1]);
Error(ctx);
}
(void)snpf(la, lal + 1, "%s", fp[1]);
}
if (!fp[2] || !*fp[2] || (ral = strlen(fp[2])) < 1) {
ra = (char *)NULL;
ral = (MALLOC_S)0;
} else {
if (!(ra = (char *)malloc(ral + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d remote raw6 address bytes: %s\n", Pn,
(int)(ral + 1), fp[2]);
Error(ctx);
}
(void)snpf(ra, ral + 1, "%s", fp[2]);
}
if (!fp[3] || !*fp[3] || (spl = strlen(fp[3])) < 1) {
sp = (char *)NULL;
spl = (MALLOC_S)0;
} else {
if (!(sp = (char *)malloc(spl + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d remote raw6 state bytes: %s\n", Pn,
(int)(spl + 1), fp[2]);
Error(ctx);
}
(void)snpf(sp, spl + 1, "%s", fp[3]);
}
/*
* Allocate space for an rawsin entry, fill it, and link it to its
* hash bucket.
*/
if (!(rp = (struct rawsin *)malloc(sizeof(struct rawsin)))) {
(void)fprintf(
stderr,
"%s: can't allocate %d byte rawsin structure for IPv6\n", Pn,
(int)sizeof(struct rawsin));
Error(ctx);
}
rp->inode = inode;
rp->la = la;
rp->lal = lal;
rp->ra = ra;
rp->ral = ral;
rp->sp = sp;
rp->spl = spl;
HASH_INSERT_ELEMENT(Rawsin6, INOHASH, rp, inode);
}
(void)fclose(xs);
}
/*
* get_tcpudp6() - get IPv6 TCP, UDP or UDPLITE net info
*/
static void get_tcpudp6(struct lsof_context *ctx, /* context */
char *p, /* /proc/net/{tcp,udp} path */
int pr, /* protocol: 0 = TCP, 1 = UDP */
int clr) /* 1 == clear the table */
{
char buf[MAXPATHLEN], *ep, **fp;
struct in6_addr faddr, laddr;
unsigned long fport, lport, rxq, state, txq;
FILE *fs;
int h, i, nf;
INODETYPE inode;
struct tcp_udp6 *np6, *tp6;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
# if defined(HASEPTOPTS)
pxinfo_t *pp, *pnp;
# endif /* defined(HASEPTOPTS) */
/*
* Delete previous table contents. Allocate a table for the first time.
*/
if (TcpUdp6) {
if (clr) {
for (h = 0; h < TcpUdp6_bucks; h++) {
for (tp6 = TcpUdp6[h]; tp6; tp6 = np6) {
np6 = tp6->next;
# if defined(HASEPTOPTS)
for (pp = tp6->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
# endif /* defined(HASEPTOPTS) */
(void)free((FREE_P *)tp6);
}
TcpUdp6[h] = (struct tcp_udp6 *)NULL;
}
# if defined(HASEPTOPTS)
if (FeptE)
for (h = 0; h < IPCBUCKS; h++)
TcpUdp6IPC[h] = (struct tcp_udp6 *)NULL;
# endif /* defined(HASEPTOPTS) */
}
} else {
/*
* Open the /proc/net/sockstat6 file and establish the hash bucket
* count from its "TCP6: inuse" and "UDP6: inuse" lines.
*/
TcpUdp6_bucks = INOBUCKS;
i = nf = 0;
if ((fs = fopen(SockStatPath6, "r"))) {
while (fgets(buf, sizeof(buf) - 1, fs)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) !=
3)
continue;
if (!fp[0] || !fp[1] || strcmp(fp[1], "inuse") || !fp[2] ||
!*fp[2])
continue;
if (!strcmp(fp[0], "TCP6:")) {
nf |= 1;
if ((h = atoi(fp[2])) < 1)
h = INOBUCKS;
i += h;
} else if (!strcmp(fp[0], "UDP6:")) {
nf |= 2;
if ((h = atoi(fp[2])) < 1)
h = INOBUCKS;
i += h;
} else
continue;
if (nf == 3) {
while (TcpUdp6_bucks < i)
TcpUdp6_bucks *= 2;
break;
}
}
(void)fclose(fs);
}
if (!(TcpUdp6 = (struct tcp_udp6 **)calloc(
TcpUdp6_bucks, sizeof(struct tcp_udp6 *)))) {
(void)fprintf(
stderr,
"%s: can't allocate %d bytes for TCP6&UDP6 hash buckets\n", Pn,
(int)(TcpUdp6_bucks * sizeof(struct tcp_udp6 *)));
Error(ctx);
}
# if defined(HASEPTOPTS)
if (FeptE && (!(TcpUdp6IPC = (struct tcp_udp6 **)calloc(
IPCBUCKS, sizeof(struct tcp_udp6 *))))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for TCP6&UDP6 local IPC "
"hash buckets\n",
Pn, (int)(IPCBUCKS * sizeof(struct tcp_udp6 *)));
Error(ctx);
}
# endif /* defined(HASEPTOPTS) */
}
/*
* Open the /proc/net file, assign a page size buffer to the stream,
* and read it.
*/
if (!(fs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
nf = 12;
while (fgets(buf, sizeof(buf) - 1, fs)) {
if (get_fields(ctx, buf, (nf == 12) ? (char *)NULL : ":", &fp,
(int *)NULL, 0) < nf)
continue;
if (nf == 12) {
if (!fp[1] || strcmp(fp[1], "local_address") || !fp[2] ||
strcmp(fp[2], "remote_address") || !fp[3] ||
strcmp(fp[3], "st") || !fp[4] || strcmp(fp[4], "tx_queue") ||
!fp[5] || strcmp(fp[5], "rx_queue") || !fp[11] ||
strcmp(fp[11], "inode")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
nf = 14;
continue;
}
/*
* Get the local and remote addresses.
*/
if (!fp[1] || !*fp[1] || hex_ipv6_to_in6(fp[1], &laddr))
continue;
ep = (char *)NULL;
if (!fp[2] || !*fp[2] ||
(lport = strtoul(fp[2], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
if (!fp[3] || !*fp[3] || hex_ipv6_to_in6(fp[3], &faddr))
continue;
ep = (char *)NULL;
if (!fp[4] || !*fp[4] ||
(fport = strtoul(fp[4], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Get the state and queue sizes.
*/
ep = (char *)NULL;
if (!fp[5] || !*fp[5] ||
(state = strtoul(fp[5], &ep, 16)) == ULONG_MAX || !ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[6] || !*fp[6] || (txq = strtoul(fp[6], &ep, 16)) == ULONG_MAX ||
!ep || *ep)
continue;
ep = (char *)NULL;
if (!fp[7] || !*fp[7] || (rxq = strtoul(fp[7], &ep, 16)) == ULONG_MAX ||
!ep || *ep)
continue;
/*
* Get the inode and use it for hashing and searching.
*/
ep = (char *)NULL;
if (!fp[13] || !*fp[13] ||
(inode = strtoull(fp[13], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
if (HASH_FIND_ELEMENT(TcpUdp6, TCPUDP6HASH, struct tcp_udp6, inode,
inode))
continue;
/*
* Create a new entry and link it to its hash bucket.
*/
if (!(tp6 = (struct tcp_udp6 *)malloc(sizeof(struct tcp_udp6)))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for tcp_udp6 struct\n",
Pn, (int)sizeof(struct tcp_udp6));
Error(ctx);
}
tp6->inode = inode;
tp6->faddr = faddr;
tp6->fport = (int)(fport & 0xffff);
tp6->laddr = laddr;
tp6->lport = (int)(lport & 0xffff);
tp6->txq = txq;
tp6->rxq = rxq;
tp6->proto = pr;
tp6->state = (int)state;
HASH_INSERT_ELEMENT(TcpUdp6, TCPUDP6HASH, tp6, inode);
# if defined(HASEPTOPTS)
tp6->pxinfo = (pxinfo_t *)NULL;
if (FeptE) {
tp6->ipc_peer = (struct tcp_udp6 *)NULL;
if (tp6->state == TCP_ESTABLISHED) {
int i = TCPUDP6_IPC_HASH(tp6);
tp6->ipc_next = TcpUdp6IPC[i];
TcpUdp6IPC[i] = tp6;
}
}
# endif /* defined(HASEPTOPTS) */
}
# if defined(HASEPTOPTS)
/*
* If endpoint info has been requested, link INET6 socket peer info.
*/
if (FeptE)
get_net6peeri(ctx);
# endif /* defined(HASEPTOPTS) */
(void)fclose(fs);
}
#endif /* defined(HASIPv6) */
/*
* get_unix() - get UNIX net info
*/
static void get_unix(struct lsof_context *ctx, /* context */
char *p) /* /proc/net/unix path */
{
char buf[MAXPATHLEN], *ep, **fp, *path, *pcb;
int fl = 1; /* First line */
int h, nf;
INODETYPE inode;
MALLOC_S len;
uxsin_t *np, *up;
FILE *us;
uint32_t ty;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
pxinfo_t *pp, *pnp;
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
/*
* Do second time cleanup or first time setup.
*/
if (Uxsin) {
for (h = 0; h < INOBUCKS; h++) {
for (up = Uxsin[h]; up; up = np) {
np = up->next;
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
for (pp = up->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
if (up->path)
(void)free((FREE_P *)up->path);
if (up->pcb)
(void)free((FREE_P *)up->pcb);
(void)free((FREE_P *)up);
}
Uxsin[h] = (uxsin_t *)NULL;
}
} else {
Uxsin = (uxsin_t **)calloc(INOBUCKS, sizeof(uxsin_t *));
if (!Uxsin) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for Unix socket info\n",
Pn, (int)(INOBUCKS * sizeof(uxsin_t *)));
Error(ctx);
}
}
/*
* Open the /proc/net/unix file, assign a page size buffer to the stream,
* read the file's contents, and add them to the Uxsin hash buckets.
*/
if (!(us = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, us)) {
if ((nf = get_fields(ctx, buf, ":", &fp, (int *)NULL, 0)) < 7)
continue;
if (fl) {
/*
* Check the first line for header words.
*/
if (!fp[0] || strcmp(fp[0], "Num") || !fp[1] ||
strcmp(fp[1], "RefCount") || !fp[2] ||
strcmp(fp[2], "Protocol") || !fp[3] || strcmp(fp[3], "Flags") ||
!fp[4] || strcmp(fp[4], "Type") || !fp[5] ||
strcmp(fp[5], "St") || !fp[6] || strcmp(fp[6], "Inode") ||
nf < 8 || !fp[7] || strcmp(fp[7], "Path")) {
if (!Fwarn) {
(void)fprintf(
stderr, "%s: WARNING: unsupported format: %s\n", Pn, p);
}
break;
}
fl = 0;
continue;
}
/*
* Assemble PCB address, inode number, and path name. If this
* inode is already represented in Uxsin, skip it.
*/
ep = (char *)NULL;
if (!fp[6] || !*fp[6] ||
(inode = strtoull(fp[6], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(Uxsin, INOHASH, uxsin_t, inode, inode))
continue;
if (!fp[0] || !*fp[0])
pcb = (char *)NULL;
else {
len = strlen(fp[0]) + 2;
if (!(pcb = (char *)malloc(len + 1))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for UNIX PCB: %s\n",
Pn, (int)(len + 1), fp[0]);
Error(ctx);
}
(void)snpf(pcb, len + 1, "0x%s", fp[0]);
}
if (nf >= 8 && fp[7] && *fp[7] && (len = strlen(fp[7]))) {
if (!(path = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d bytes for UNIX path \"%s\"\n", Pn,
(int)(len + 1), fp[7]);
Error(ctx);
}
(void)snpf(path, len + 1, "%s", fp[7]);
} else
path = (char *)NULL;
/*
* Assemble socket type.
*/
ep = (char *)NULL;
if (!fp[4] || !*fp[4] ||
(ty = (uint32_t)strtoul(fp[4], &ep, 16)) == (uint32_t)UINT32_MAX ||
!ep || *ep) {
ty = (uint32_t)UINT_MAX;
}
/*
* Record socket state.
*/
unsigned long proc_flags = 0UL;
ep = (char *)NULL;
if (fp[3] && *fp[3] &&
(proc_flags = strtoul(fp[3], &ep, 16)) == ULONG_MAX)
proc_flags = 0UL;
unsigned long proc_st = 0UL;
ep = (char *)NULL;
if (fp[5] && *fp[5] &&
((proc_st = strtoul(fp[5], &ep, 16)) == ULONG_MAX))
proc_st = 0UL;
/*
* Allocate and fill a Unix socket info structure; link it to its
* hash bucket.
*/
if (!(up = (uxsin_t *)malloc(sizeof(uxsin_t)))) {
(void)fprintf(stderr,
"%s: can't allocate %d bytes for uxsin struct\n", Pn,
(int)sizeof(uxsin_t));
Error(ctx);
}
up->inode = inode;
up->next = (uxsin_t *)NULL;
up->pcb = pcb;
up->sb_def = 0;
up->ty = ty;
up->opt = (unsigned int)proc_flags;
up->ss = (unsigned int)proc_st;
if ((up->path = path) && (*path == '/')) {
/*
* If an absolute path (i.e., one that begins with a '/') exists
* for the line, attempt to stat(2) it and save the device and
* node numbers reported in the stat buffer.
*/
struct stat sb;
int sr;
if (HasNFS)
sr = statsafely(ctx, path, &sb);
else
sr = stat(path, &sb);
if (sr == 0 && ((sb.st_mode & S_IFMT) == S_IFSOCK)) {
up->sb_def = 1;
up->sb_dev = sb.st_dev;
up->sb_ino = (INODETYPE)sb.st_ino;
up->sb_rdev = sb.st_rdev;
}
}
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
/*
* Clean UNIX socket endpoint values.
*/
up->icstat = 0;
up->pxinfo = (pxinfo_t *)NULL;
up->peer = up->icons = (uxsin_t *)NULL;
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
HASH_INSERT_ELEMENT(Uxsin, INOHASH, up, inode);
}
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
/*
* If endpoint info has been requested, get UNIX socket peer info.
*/
if (FeptE)
get_uxpeeri(ctx);
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
(void)fclose(us);
}
#if defined(HASIPv6)
/*
* hex_ipv6_to_in6() - convert ASCII IPv6 address in /proc/net/{tcp,udp}6 form
*to an in6_addr
*/
static int hex_ipv6_to_in6(char *as, /* address source */
struct in6_addr *ad) /* address destination */
{
char buf[9], *ep;
int i;
size_t len;
/*
* Assemble four uint32_t's from 4 X 8 hex digits into s6_addr32[].
*/
for (i = 0, len = strlen(as); (i < 4) && (len >= 8);
as += 8, i++, len -= 8) {
(void)strncpy(buf, as, 8);
buf[8] = '\0';
ep = (char *)NULL;
errno = 0;
unsigned long ul_addr = strtoul(buf, &ep, 16);
if (!ep || *ep)
break;
else if (ul_addr == ULONG_MAX && errno == ERANGE) {
/* Quoted from strtoul(3)
---------------------------------------------------
The strtoul() function returns either the result of
the conversion or, if there was a leading minus
sign, the negation of the result of the conversion
represented as an unsigned value, unless the
original (nonnegated) value would overflow; in the
latter case, strtoul() returns ULONG_MAX and sets
errno to ERANGE.
---------------------------------------------------
NOTE: even if the value doesn't overflow, a
negative is not acceptable. */
break;
} else if (ul_addr > (unsigned long)UINT32_MAX) {
/* This will never happen:
The maximum length of BUF is 8 characters.
The possible maximum value represented by BUF is
"FFFFFFFF". This is UINT32_MAX.
If you agree with what I write here, make a pull
request for removing this block. */
break;
}
ad->s6_addr32[i] = (uint32_t)ul_addr;
}
return ((*as || (i != 4) || len) ? 1 : 0);
}
#endif /* defined(HASIPv6) */
/*
* isainb(a,b) check if string a is included in b, where b is a comma-separated
* string
*/
static int isainb(char *a, /*string a */
char *b) /* string b */
{
char *cp, *pp;
MALLOC_S la, lb, lt;
if (!a || !b)
return (1);
if (!(la = strlen(a)) || !(lb = strlen(b)))
return (1);
if (!(cp = strchr(b, (int)','))) {
if (la != lb)
return (1);
return (strcmp(a, b));
}
for (pp = b; pp && *pp;) {
lt = (MALLOC_S)(cp - pp);
if ((la == lt) && !strncmp(a, pp, lt))
return (0);
if (*cp) {
pp = cp + 1;
if (!(cp = strchr(pp, (int)',')))
cp = b + lb;
} else
pp = cp;
}
return (1);
}
/*
* print_ax25info() - print AX25 socket info
*/
static void print_ax25info(struct lsof_context *ctx, /* context */
struct ax25sin *ap) /* AX25 socket info */
{
char *cp, pbuf[1024];
int ds;
MALLOC_S pl = (MALLOC_S)0;
if (Lf->nma)
return;
if (ap->sa) {
ds = (ap->da && strcmp(ap->da, "*")) ? 1 : 0;
(void)snpf(&pbuf[pl], sizeof(pbuf) - pl, "%s%s%s ", ap->sa,
ds ? "->" : "", ds ? ap->da : "");
pl = strlen(pbuf);
}
if (ap->sqs) {
(void)snpf(&pbuf[pl], sizeof(pbuf) - pl, "(Sq=%lu ", ap->sq);
pl = strlen(pbuf);
cp = "";
} else
cp = "(";
if (ap->rqs) {
(void)snpf(&pbuf[pl], sizeof(pbuf) - pl, "%sRq=%lu ", cp, ap->rq);
pl = strlen(pbuf);
cp = "";
}
(void)snpf(&pbuf[pl], sizeof(pbuf) - pl, "%sState=%d", cp, ap->state);
pl = strlen(pbuf);
if ((ap->state >= 0) && (ap->state < NAX25ST))
cp = ax25st[ap->state];
else
cp = NULL;
(void)snpf(&pbuf[pl], sizeof(pbuf) - pl, "%s%s)", cp ? ", " : "",
cp ? cp : "");
pl = strlen(pbuf);
if (!(cp = (char *)malloc(pl + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d bytes for AX25 sock state, PID: %d\n", Pn,
(int)(pl + 1), Lp->pid);
Error(ctx);
}
(void)snpf(cp, pl + 1, "%s", pbuf);
Lf->nma = cp;
}
/*
* print_ipxinfo() - print IPX socket info
*/
static void print_ipxinfo(struct lsof_context *ctx, /* context */
struct ipxsin *ip) /* IPX socket info */
{
char *cp, pbuf[256];
MALLOC_S pl;
if (Lf->nma)
return;
(void)snpf(pbuf, sizeof(pbuf), "(Tx=%lx Rx=%lx State=%02x)", ip->txq,
ip->rxq, ip->state);
pl = strlen(pbuf);
if (!(cp = (char *)malloc(pl + 1))) {
(void)fprintf(
stderr, "%s: can't allocate %d bytes for IPX sock state, PID: %d\n",
Pn, (int)(pl + 1), Lp->pid);
Error(ctx);
}
(void)snpf(cp, pl + 1, "%s", pbuf);
Lf->nma = cp;
}
/*
* process_proc_sock() - process /proc-based socket
*/
void process_proc_sock(struct lsof_context *ctx, /* context */
char *p, /* node's readlink() path */
char *pbr, /* node's path before readlink() */
struct stat *s, /* stat() result for path */
int ss, /* *s status -- i.e, SB_* values */
struct stat *l, /* lstat() result for FD (NULL for
* others) */
int lss) /* *l status -- i.e, SB_* values */
{
struct ax25sin *ap;
char *cp, *path = (char *)NULL, tbuf[64];
unsigned char *fa, *la;
struct in_addr fs, ls;
struct icmpin *icmpp;
struct ipxsin *ip;
int i, len, nl, rf;
struct nlksin *np;
struct packin *pp;
char *pr;
static char *prp = (char *)NULL;
struct rawsin *rp;
struct sctpsin *sp;
static ssize_t sz;
struct tcp_udp *tp;
uxsin_t *up;
#if defined(HASIPv6)
int af;
struct tcp_udp6 *tp6;
#endif /* defined(HASIPv6) */
/*
* Enter offset, if possible.
*/
if (l && (lss & SB_SIZE) && OffType != OFFSET_UNKNOWN) {
Lf->off = (SZOFFTYPE)l->st_size;
Lf->off_def = 1;
}
/*
* Check for socket's inode presence in the protocol info caches.
*/
if (AX25path) {
(void)get_ax25(ctx, AX25path);
(void)free((FREE_P *)AX25path);
AX25path = (char *)NULL;
}
if ((ss & SB_INO) && (ap = check_ax25(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to an AX25 /proc record.
*
* Set the type to "ax25"; save the device name; save the inode number;
* save the destination and source addresses; save the send and receive
* queue sizes; and save the connection state.
*/
Lf->type = LSOF_FILE_AX25;
if (ap->dev_ch)
(void)enter_dev_ch(ctx, ap->dev_ch);
Lf->inode = ap->inode;
Lf->inp_ty = 1;
print_ax25info(ctx, ap);
return;
}
if (Ipxpath) {
(void)get_ipx(ctx, Ipxpath);
(void)free((FREE_P *)Ipxpath);
Ipxpath = (char *)NULL;
}
if ((ss & SB_INO) && (ip = check_ipx(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to an IPX /proc record.
*
* Set the type to "ipx"; enter the inode and device numbers; store
* the addresses, queue sizes, and state in the NAME column.
*/
Lf->type = LSOF_FILE_IPX;
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
if (ss & SB_DEV) {
Lf->dev = s->st_dev;
Lf->dev_def = 1;
}
cp = Namech;
nl = Namechl;
*cp = '\0';
if (ip->la && nl) {
/*
* Store the local IPX address.
*/
len = strlen(ip->la);
if (len > nl - 1)
len = nl - 1;
(void)strncpy(cp, ip->la, len);
cp += len;
*cp = '\0';
nl -= len;
}
if (ip->ra && nl) {
/*
* Store the remote IPX address, prefixed with "->".
*/
if (nl > 2) {
(void)snpf(cp, nl, "->");
cp += 2;
nl -= 2;
}
if (nl) {
(void)snpf(cp, nl, "%s", ip->ra);
len = strlen(ip->ra);
cp += len;
nl -= len;
}
}
(void)print_ipxinfo(ctx, ip);
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
if (Rawpath) {
(void)get_raw(ctx, Rawpath);
(void)free((FREE_P *)Rawpath);
Rawpath = (char *)NULL;
}
if ((ss & SB_INO) && (rp = check_raw(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to a raw /proc record.
*
* Set the type to "raw"; enter the inode number; store the local
* address, remote address, and state in the NAME column.
*/
Lf->type = LSOF_FILE_RAW;
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
cp = Namech;
nl = Namechl - 2;
*cp = '\0';
if (rp->la && rp->lal) {
/*
* Store the local raw address.
*/
if (nl > rp->lal) {
(void)snpf(cp, nl, "%s", rp->la);
cp += rp->lal;
*cp = '\0';
nl -= rp->lal;
}
}
if (rp->ra && rp->ral) {
/*
* Store the remote raw address, prefixed with "->".
*/
if (nl > (rp->ral + 2)) {
(void)snpf(cp, nl, "->%s", rp->ra);
cp += (rp->ral + 2);
*cp = '\0';
nl -= (rp->ral + 2);
}
}
if (rp->sp && rp->spl) {
/*
* Store the state, optionally prefixed by a space, in the
* form "st=x...x".
*/
if (nl > (len = ((cp == Namech) ? 0 : 1) + 3 + rp->spl)) {
(void)snpf(cp, nl, "%sst=%s", (cp == Namech) ? "" : " ",
rp->sp);
cp += len;
*cp = '\0';
nl -= len;
}
}
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
if (Nlkpath) {
(void)get_netlink(ctx, Nlkpath);
(void)free((FREE_P *)Nlkpath);
Nlkpath = (char *)NULL;
}
if ((ss & SB_INO) && (np = check_netlink(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to a Netlink /proc record.
*
* Set the type to "netlink" and store the protocol in the NAME
* column. Save the inode number.
*/
Lf->type = LSOF_FILE_NETLINK;
cp = netlink_proto_to_str(np->pr);
if (cp)
(void)snpf(Namech, Namechl, "%s", cp);
else
(void)snpf(Namech, Namechl, "unknown protocol: %d", np->pr);
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
if (Packpath) {
(void)get_pack(ctx, Packpath);
(void)free((FREE_P *)Packpath);
Packpath = (char *)NULL;
}
if ((ss & SB_INO) && (pp = check_pack(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to a packet /proc record.
*
* Set the type to "pack" and store the socket type in the NAME
* column. Put the protocol name in the NODE column and the inode
* number in the DEVICE column.
*/
Lf->type = LSOF_FILE_PACKET;
cp = socket_type_to_str(pp->ty, &rf);
(void)snpf(Namech, Namechl, "type=%s%s", rf ? "" : "SOCK_", cp);
cp = ethernet_proto_to_str(pp->pr);
if (!cp) {
/* Unknown ethernet proto */
(void)snpf(tbuf, sizeof(tbuf) - 1, "%d", pp->pr);
tbuf[sizeof(tbuf) - 1] = '\0';
cp = tbuf;
}
(void)snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL - 1, cp);
Lf->inp_ty = 2;
if (ss & SB_INO) {
(void)snpf(tbuf, sizeof(tbuf), "%" INODEPSPEC "u",
(INODETYPE)s->st_ino);
tbuf[sizeof(tbuf) - 1] = '\0';
enter_dev_ch(ctx, tbuf);
}
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
if (UNIXpath) {
(void)get_unix(ctx, UNIXpath);
(void)free((FREE_P *)UNIXpath);
UNIXpath = (char *)NULL;
}
if ((ss & SB_INO) && (up = check_unix(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to a UNIX /proc record.
*
* Set the type to "unix"; enter the PCB address in the DEVICE column;
* enter the inode number; and save the optional path.
*/
if (Funix)
Lf->sf |= SELUNX;
Lf->type = LSOF_FILE_UNIX;
if (up->pcb)
enter_dev_ch(ctx, up->pcb);
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
Lf->lts.type = up->ty;
#if defined(HASSOOPT)
Lf->lts.opt = up->opt;
#endif /* defined(HASSOOPT) */
#if defined(HASSOSTATE)
Lf->lts.ss = up->ss;
#endif /* defined(HASSOSTATE) */
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
if (FeptE) {
(void)enter_uxsinfo(ctx, up);
Lf->sf |= SELUXSINFO;
}
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
cp = socket_type_to_str(up->ty, &rf);
(void)snpf(Namech, Namechl - 1, "%s%stype=%s", up->path ? up->path : "",
up->path ? " " : "", cp);
Namech[Namechl - 1] = '\0';
(void)enter_nm(ctx, Namech);
if (Sfile) {
/*
* See if this UNIX domain socket was specified as a search
* argument.
*
* Search first by device and node numbers, if that is possible;
* then search by name.
*/
unsigned char f = 0; /* file-found flag */
if (up->sb_def) {
/*
* If the UNIX socket information includes stat(2) results, do
* a device and node number search.
*
* Note: that requires the saving, temporary modification and
* restoration of some *Lf values.
*/
unsigned char sv_dev_def; /* saved dev_def */
unsigned char sv_inp_ty; /* saved inp_ty */
unsigned char sv_rdev_def; /* saved rdev_def */
dev_t sv_dev; /* saved dev */
INODETYPE sv_inode; /* saved inode */
dev_t sv_rdev; /* saved rdev */
sv_dev_def = Lf->dev_def;
sv_dev = Lf->dev;
sv_inode = Lf->inode;
sv_inp_ty = Lf->inp_ty;
sv_rdev_def = Lf->rdev_def;
sv_rdev = Lf->rdev;
Lf->dev_def = Lf->inp_ty = Lf->rdev_def = 1;
Lf->dev = up->sb_dev;
Lf->inode = up->sb_ino;
Lf->rdev = up->sb_rdev;
if (is_file_named(ctx, 0, path, (struct mounts *)NULL, 0)) {
f = 1;
Lf->sf |= SELNM;
}
Lf->dev_def = sv_dev_def;
Lf->dev = sv_dev;
Lf->inode = sv_inode;
Lf->inp_ty = sv_inp_ty;
Lf->rdev_def = sv_rdev_def;
Lf->rdev = sv_rdev;
}
if (!f && (ss & SB_MODE)) {
/*
* If the file has not yet been found and the stat buffer has
* st_mode, search for the file by full path.
*/
if (is_file_named(ctx, 2, up->path ? up->path : p,
(struct mounts *)NULL,
((s->st_mode & S_IFMT) == S_IFCHR))
? 1
: 0) {
Lf->sf |= SELNM;
}
}
}
return;
}
#if defined(HASIPv6)
if (Raw6path) {
if (!Fxopt)
(void)get_raw6(ctx, Raw6path);
(void)free((FREE_P *)Raw6path);
Raw6path = (char *)NULL;
}
if (!Fxopt && (ss & SB_INO) &&
(rp = check_raw6(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to a raw IPv6 /proc record.
*
* Set the type to "raw6"; enter the inode number; store the local
* address, remote address, and state in the NAME column.
*/
Lf->type = LSOF_FILE_RAW6;
if (ss & SB_INO) {
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
}
cp = Namech;
nl = MAXPATHLEN - 2;
if (rp->la && rp->lal) {
/*
* Store the local raw IPv6 address.
*/
if (nl > rp->lal) {
(void)snpf(cp, nl, "%s", rp->la);
cp += rp->lal;
*cp = '\0';
nl -= rp->lal;
}
}
if (rp->ra && rp->ral) {
/*
* Store the remote raw address, prefixed with "->".
*/
if (nl > (rp->ral + 2)) {
(void)snpf(cp, nl, "->%s", rp->ra);
cp += (rp->ral + 2);
nl -= (rp->ral + 2);
}
}
if (rp->sp && rp->spl) {
/*
* Store the state, optionally prefixed by a space, in the
* form "st=x...x".
*/
if (nl > (len = ((cp == Namech) ? 0 : 1) + 3 + rp->spl)) {
(void)snpf(cp, nl, "%sst=%s", (cp == Namech) ? "" : " ",
rp->sp);
cp += len;
*cp = '\0';
nl -= len;
}
}
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
if (TCP6path) {
if (!Fxopt)
(void)get_tcpudp6(ctx, TCP6path, 0, 1);
(void)free((FREE_P *)TCP6path);
TCP6path = (char *)NULL;
}
if (UDP6path) {
if (!Fxopt)
(void)get_tcpudp6(ctx, UDP6path, 1, 0);
(void)free((FREE_P *)UDP6path);
UDP6path = (char *)NULL;
}
if (UDPLITE6path) {
if (!Fxopt)
(void)get_tcpudp6(ctx, UDPLITE6path, 2, 0);
(void)free((FREE_P *)UDPLITE6path);
UDPLITE6path = (char *)NULL;
}
if (!Fxopt && (ss & SB_INO) &&
(tp6 = check_tcpudp6(ctx, (INODETYPE)s->st_ino, &pr))) {
/*
* The inode is connected to an IPv6 TCP or UDP /proc record.
*
* Set the type to "IPv6"; enter the protocol; put the inode number
* in the DEVICE column in lieu of the PCB address; save the local
* and foreign IPv6 addresses; save the type and protocol; and
* (optionally) save the queue sizes.
*/
i = tp6->state + TcpStOff;
if (TcpStXn) {
/*
* Check for state exclusion.
*/
if (i >= 0 && i < TcpNstates) {
if (TcpStX[i]) {
Lf->sf |= SELEXCLF;
return;
}
}
}
if (TcpStIn) {
/*
* Check for state inclusion.
*/
if (i >= 0 && i < TcpNstates) {
if (TcpStI[i])
TcpStI[i] = 2;
else {
Lf->sf |= SELEXCLF;
return;
}
}
}
if (Fnet && (FnetTy != 4))
Lf->sf |= SELNET;
Lf->type = LSOF_FILE_IPV6;
(void)snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL - 1, pr);
Lf->inp_ty = 2;
if (ss & SB_INO) {
(void)snpf(tbuf, sizeof(tbuf), "%" INODEPSPEC "u",
(INODETYPE)s->st_ino);
tbuf[sizeof(tbuf) - 1] = '\0';
enter_dev_ch(ctx, tbuf);
Lf->inode = (INODETYPE)s->st_ino;
}
af = AF_INET6;
if (!IN6_IS_ADDR_UNSPECIFIED(&tp6->faddr) || tp6->fport)
fa = (unsigned char *)&tp6->faddr;
else
fa = (unsigned char *)NULL;
if (!IN6_IS_ADDR_UNSPECIFIED(&tp6->laddr) || tp6->lport)
la = (unsigned char *)&tp6->laddr;
else
la = (unsigned char *)NULL;
if ((fa && IN6_IS_ADDR_V4MAPPED(&tp6->faddr)) ||
(la && IN6_IS_ADDR_V4MAPPED(&tp6->laddr))) {
af = AF_INET;
if (fa)
fa += 12;
if (la)
la += 12;
}
ent_inaddr(ctx, la, tp6->lport, fa, tp6->fport, af);
Lf->lts.type = tp6->proto;
Lf->lts.state.i = tp6->state;
# if defined(HASTCPTPIQ)
Lf->lts.rq = tp6->rxq;
Lf->lts.sq = tp6->txq;
Lf->lts.rqs = Lf->lts.sqs = 1;
# endif /* defined(HASTCPTPIQ) */
# if defined(HASEPTOPTS)
if (FeptE && tp6->ipc_peer) {
(void)enter_nets6info(ctx, tp6);
Lf->sf |= SELNETS6INFO;
}
# endif /* defined(HASEPTOPTS) */
return;
}
#endif /* defined(HASIPv6) */
if (TCPpath) {
if (!Fxopt)
(void)get_tcpudp(ctx, TCPpath, 0, 1);
(void)free((FREE_P *)TCPpath);
TCPpath = (char *)NULL;
}
if (UDPpath) {
if (!Fxopt)
(void)get_tcpudp(ctx, UDPpath, 1, 0);
(void)free((FREE_P *)UDPpath);
UDPpath = (char *)NULL;
}
if (UDPLITEpath) {
if (!Fxopt)
(void)get_tcpudp(ctx, UDPLITEpath, 2, 0);
(void)free((FREE_P *)UDPLITEpath);
UDPLITEpath = (char *)NULL;
}
if (!Fxopt && (ss & SB_INO) &&
(tp = check_tcpudp(ctx, (INODETYPE)s->st_ino, &pr))) {
/*
* The inode is connected to an IPv4 TCP or UDP /proc record.
*
* Set the type to "inet" or "IPv4"; enter the protocol; put the
* inode number in the DEVICE column in lieu of the PCB address;
* save the local and foreign IPv4 addresses; save the type and
* protocol; and (optionally) save the queue sizes.
*/
i = tp->state + TcpStOff;
if (TcpStXn) {
/*
* Check for state exclusion.
*/
if (i >= 0 && i < TcpNstates) {
if (TcpStX[i]) {
Lf->sf |= SELEXCLF;
return;
}
}
}
if (TcpStIn) {
/*
* Check for state inclusion.
*/
if (i >= 0 && i < TcpNstates) {
if (TcpStI[i])
TcpStI[i] = 2;
else {
Lf->sf |= SELEXCLF;
return;
}
}
}
if (Fnet && (FnetTy != 6))
Lf->sf |= SELNET;
#if defined(HASIPv6)
Lf->type = LSOF_FILE_IPV4;
#else /* !defined(HASIPv6) */
Lf->type = LSOF_FILE_INET;
#endif /* defined(HASIPv6) */
(void)snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL - 1, pr);
Lf->inp_ty = 2;
if (ss & SB_INO) {
(void)snpf(tbuf, sizeof(tbuf), "%" INODEPSPEC "u",
(INODETYPE)s->st_ino);
tbuf[sizeof(tbuf) - 1] = '\0';
enter_dev_ch(ctx, tbuf);
Lf->inode = (INODETYPE)s->st_ino;
}
if (tp->faddr || tp->fport) {
fs.s_addr = tp->faddr;
fa = (unsigned char *)&fs;
} else
fa = (unsigned char *)NULL;
if (tp->laddr || tp->lport) {
ls.s_addr = tp->laddr;
la = (unsigned char *)&ls;
} else
la = (unsigned char *)NULL;
ent_inaddr(ctx, la, tp->lport, fa, tp->fport, AF_INET);
Lf->lts.type = tp->proto;
Lf->lts.state.i = tp->state;
#if defined(HASTCPTPIQ)
Lf->lts.rq = tp->rxq;
Lf->lts.sq = tp->txq;
Lf->lts.rqs = Lf->lts.sqs = 1;
#endif /* defined(HASTCPTPIQ) */
#if defined(HASEPTOPTS)
if (FeptE && tp->ipc_peer) {
(void)enter_netsinfo(ctx, tp);
Lf->sf |= SELNETSINFO;
}
#endif /* defined(HASEPTOPTS) */
return;
}
if (SCTPPath[0]) {
(void)get_sctp(ctx);
for (i = 0; i < NSCTPPATHS; i++) {
(void)free((FREE_P *)SCTPPath[i]);
SCTPPath[i] = (char *)NULL;
}
}
if ((ss & SB_INO) && (sp = check_sctp(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to an SCTP /proc record.
*
* Set the type to "sock"; enter the inode number in the DEVICE
* column; set the protocol to SCTP; and fill in the NAME column
* with ASSOC, ASSOC-ID, ENDPT, LADDRS, LPORT, RADDRS and RPORT.
*/
Lf->type = LSOF_FILE_SOCKET;
(void)snpf(Lf->iproto, sizeof(Lf->iproto), "%.*s", IPROTOL - 1, "SCTP");
Lf->inp_ty = 2;
(void)snpf(tbuf, sizeof(tbuf), "%" INODEPSPEC "u", (INODETYPE)s->st_ino);
tbuf[sizeof(tbuf) - 1] = '\0';
enter_dev_ch(ctx, tbuf);
Namech[0] = '\0';
if (sp->type == 1) {
/*
* This is an ENDPT SCTP file.
*/
(void)snpf(Namech, Namechl, "ENDPT: %s%s%s%s%s%s",
sp->addr ? sp->addr : "",
(sp->laddrs || sp->lport) ? " " : "",
sp->laddrs ? sp->laddrs : "", sp->lport ? "[" : "",
sp->lport ? sp->lport : "", sp->lport ? "]" : "");
} else {
/*
* This is an ASSOC, or ASSOC and ENDPT socket file.
*/
(void)snpf(
Namech, Namechl, "%s: %s%s%s %s%s%s%s%s%s%s%s%s",
sp->type ? "ASSOC+ENDPT" : "ASSOC", sp->addr ? sp->addr : "",
(sp->addr && sp->assocID) ? "," : "",
sp->assocID ? sp->assocID : "", sp->laddrs ? sp->laddrs : "",
sp->lport ? "[" : "", sp->lport ? sp->lport : "",
sp->lport ? "]" : "",
((sp->laddrs || sp->lport) && (sp->raddrs || sp->rport)) ? "<->"
: "",
sp->raddrs ? sp->raddrs : "", sp->rport ? "[" : "",
sp->rport ? sp->rport : "", sp->rport ? "]" : "");
}
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
if (ICMPpath) {
(void)get_icmp(ctx, ICMPpath);
(void)free((FREE_P *)ICMPpath);
ICMPpath = (char *)NULL;
}
if ((ss & SB_INO) && (icmpp = check_icmp(ctx, (INODETYPE)s->st_ino))) {
/*
* The inode is connected to an ICMP /proc record.
*
* Set the type to "icmp" and store the type in the NAME
* column. Save the inode number.
*/
Lf->type = LSOF_FILE_ICMP;
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
cp = Namech;
nl = Namechl - 2;
*cp = '\0';
if (icmpp->la && icmpp->lal) {
/*
* Store the local raw address.
*/
if (nl > icmpp->lal) {
(void)snpf(cp, nl, "%s", icmpp->la);
cp += icmpp->lal;
*cp = '\0';
nl -= icmpp->lal;
}
}
if (icmpp->ra && icmpp->ral) {
/*
* Store the remote raw address, prefixed with "->".
*/
if (nl > (icmpp->ral + 2)) {
(void)snpf(cp, nl, "->%s", icmpp->ra);
cp += (icmpp->ral + 2);
*cp = '\0';
nl -= (icmpp->ral + 2);
}
}
if (Namech[0])
enter_nm(ctx, Namech);
return;
}
/*
* The socket's protocol can't be identified.
*/
Lf->type = LSOF_FILE_SOCKET;
if (ss & SB_INO) {
Lf->inode = (INODETYPE)s->st_ino;
Lf->inp_ty = 1;
}
if (ss & SB_DEV) {
Lf->dev = s->st_dev;
Lf->dev_def = 1;
}
if (Fxopt)
enter_nm(ctx, "can't identify protocol (-X specified)");
else {
(void)snpf(Namech, Namechl, "protocol: ");
if (!prp) {
i = (int)strlen(Namech);
prp = &Namech[i];
sz = (ssize_t)(Namechl - i - 1);
}
if ((getxattr(pbr, "system.sockprotoname", prp, sz)) < 0)
enter_nm(ctx, "can't identify protocol");
else
enter_nm(ctx, Namech);
}
}
/*
* set_net_paths() - set /proc/net paths
*/
void set_net_paths(struct lsof_context *ctx, /* context */
char *p, /* path to /proc/net/ */
int pl) /* strlen(p) */
{
int i;
int pathl;
pathl = 0;
(void)make_proc_path(ctx, p, pl, &AX25path, &pathl, "ax25");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &ICMPpath, &pathl, "icmp");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &Ipxpath, &pathl, "ipx");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &Nlkpath, &pathl, "netlink");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &Packpath, &pathl, "packet");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &Rawpath, &pathl, "raw");
for (i = 0; i < NSCTPPATHS; i++) {
pathl = 0;
(void)make_proc_path(ctx, p, pl, &SCTPPath[i], &pathl, SCTPSfx[i]);
}
pathl = 0;
(void)make_proc_path(ctx, p, pl, &SockStatPath, &pathl, "sockstat");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &TCPpath, &pathl, "tcp");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &UDPpath, &pathl, "udp");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &UDPLITEpath, &pathl, "udplite");
#if defined(HASIPv6)
pathl = 0;
(void)make_proc_path(ctx, p, pl, &Raw6path, &pathl, "raw6");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &SockStatPath6, &pathl, "sockstat6");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &TCP6path, &pathl, "tcp6");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &UDP6path, &pathl, "udp6");
pathl = 0;
(void)make_proc_path(ctx, p, pl, &UDPLITE6path, &pathl, "udplite6");
#endif /* defined(HASIPv6) */
pathl = 0;
(void)make_proc_path(ctx, p, pl, &UNIXpath, &pathl, "unix");
}
/*
* socket_type_to_str() -- convert socket type number to a string
*/
static char *socket_type_to_str(uint32_t ty, /* socket type number */
int *rf) /* result flag: 0 == known
* 1 = unknown */
{
int f = 0; /* result flag */
char *sr; /* string result */
switch (ty) {
#if defined(SOCK_STREAM)
case SOCK_STREAM:
sr = "STREAM";
break;
#endif /* defined(SOCK_STREAM) */
#if defined(SOCK_DGRAM)
case SOCK_DGRAM:
sr = "DGRAM";
break;
#endif /* defined(SOCK_DGRAM) */
#if defined(SOCK_RAW)
case SOCK_RAW:
sr = "RAW";
break;
#endif /* defined(SOCK_RAW) */
#if defined(SOCK_RDM)
case SOCK_RDM:
sr = "RDM";
break;
#endif /* defined(SOCK_RDM) */
#if defined(SOCK_SEQPACKET)
case SOCK_SEQPACKET:
sr = "SEQPACKET";
break;
#endif /* defined(SOCK_SEQPACKET) */
#if defined(SOCK_DCCP)
case SOCK_DCCP:
sr = "DCCP";
break;
#endif /* defined(SOCK_DCCP) */
#if defined(SOCK_PACKET)
case SOCK_PACKET:
sr = "PACKET";
break;
#endif /* defined(SOCK_PACKET) */
default:
f = 1;
sr = "unknown";
}
*rf = f;
return (sr);
}
/*
* netlink_proto_to_str() -- convert netlink protocol number to a string
*
* return NULL if the number is unknown.
*/
static char *netlink_proto_to_str(unsigned int pr) {
char *cp = NULL;
switch (pr) {
#if defined(NETLINK_ROUTE)
case NETLINK_ROUTE:
cp = "ROUTE";
break;
#endif /* defined(NETLINK_ROUTE) */
#if defined(NETLINK_UNUSED)
case NETLINK_UNUSED:
cp = "UNUSED";
break;
#endif /* defined(NETLINK_UNUSED) */
#if defined(NETLINK_USERSOCK)
case NETLINK_USERSOCK:
cp = "USERSOCK";
break;
#endif /* defined(NETLINK_USERSOCK) */
#if defined(NETLINK_FIREWALL)
case NETLINK_FIREWALL:
cp = "FIREWALL";
break;
#endif /* defined(NETLINK_FIREWALL) */
#if defined(NETLINK_INET_DIAG)
case NETLINK_INET_DIAG:
cp = "INET_DIAG";
break;
#endif /* defined(NETLINK_INET_DIAG) */
#if defined(NETLINK_NFLOG)
case NETLINK_NFLOG:
cp = "NFLOG";
break;
#endif /* defined(NETLINK_NFLOG) */
#if defined(NETLINK_XFRM)
case NETLINK_XFRM:
cp = "XFRM";
break;
#endif /* defined(NETLINK_XFRM) */
#if defined(NETLINK_SELINUX)
case NETLINK_SELINUX:
cp = "SELINUX";
break;
#endif /* defined(NETLINK_SELINUX) */
#if defined(NETLINK_ISCSI)
case NETLINK_ISCSI:
cp = "ISCSI";
break;
#endif /* defined(NETLINK_ISCSI) */
#if defined(NETLINK_AUDIT)
case NETLINK_AUDIT:
cp = "AUDIT";
break;
#endif /* defined(NETLINK_AUDIT) */
#if defined(NETLINK_FIB_LOOKUP)
case NETLINK_FIB_LOOKUP:
cp = "FIB_LOOKUP";
break;
#endif /* defined(NETLINK_FIB_LOOKUP) */
#if defined(NETLINK_CONNECTOR)
case NETLINK_CONNECTOR:
cp = "CONNECTOR";
break;
#endif /* defined(NETLINK_CONNECTOR) */
#if defined(NETLINK_NETFILTER)
case NETLINK_NETFILTER:
cp = "NETFILTER";
break;
#endif /* defined(NETLINK_NETFILTER) */
#if defined(NETLINK_IP6_FW)
case NETLINK_IP6_FW:
cp = "IP6_FW";
break;
#endif /* defined(NETLINK_IP6_FW) */
#if defined(NETLINK_DNRTMSG)
case NETLINK_DNRTMSG:
cp = "DNRTMSG";
break;
#endif /* defined(NETLINK_DNRTMSG) */
#if defined(NETLINK_KOBJECT_UEVENT)
case NETLINK_KOBJECT_UEVENT:
cp = "KOBJECT_UEVENT";
break;
#endif /* defined(NETLINK_KOBJECT_UEVENT) */
#if defined(NETLINK_GENERIC)
case NETLINK_GENERIC:
cp = "GENERIC";
break;
#endif /* defined(NETLINK_GENERIC) */
#if defined(NETLINK_SCSITRANSPORT)
case NETLINK_SCSITRANSPORT:
cp = "SCSITRANSPORT";
break;
#endif /* defined(NETLINK_SCSITRANSPORT) */
#if defined(NETLINK_ECRYPTFS)
case NETLINK_ECRYPTFS:
cp = "ECRYPTFS";
break;
#endif /* defined(NETLINK_ECRYPTFS) */
#if defined(NETLINK_RDMA)
case NETLINK_RDMA:
cp = "RDMA";
break;
#endif /* defined(NETLINK_RDMA) */
#if defined(NETLINK_CRYPTO)
case NETLINK_CRYPTO:
cp = "CRYPTO";
break;
#endif /* defined(NETLINK_CRYPTO) */
#if defined(NETLINK_SMC)
case NETLINK_SMC:
cp = "SMC";
break;
#endif /* defined(NETLINK_SMC) */
}
return cp;
}
/*
* ethernet_proto_to_str() -- convert ethernet protocol number to a string
*
* The string should not exceed 7 characters.
*
* return NULL if the number is unknown.
*/
static char *ethernet_proto_to_str(unsigned int pr) {
char *cp = NULL;
switch (pr) {
#if defined(ETH_P_LOOP)
case ETH_P_LOOP:
cp = "LOOP";
break;
#endif /* defined(ETH_P_LOOP) */
#if defined(ETH_P_PUP)
case ETH_P_PUP:
cp = "PUP";
break;
#endif /* defined(ETH_P_PUP) */
#if defined(ETH_P_PUPAT)
case ETH_P_PUPAT:
cp = "PUPAT";
break;
#endif /* defined(ETH_P_PUPAT) */
#if defined(ETH_P_TSN)
case ETH_P_TSN:
cp = "TSN";
break;
#endif /* defined(ETH_P_TSN) */
#if defined(ETH_P_ERSPAN2)
case ETH_P_ERSPAN2:
cp = "ERSPAN2";
break;
#endif /* defined(ETH_P_ERSPAN2) */
#if defined(ETH_P_IP)
case ETH_P_IP:
cp = "IP";
break;
#endif /* defined(ETH_P_IP) */
#if defined(ETH_P_X25)
case ETH_P_X25:
cp = "X25";
break;
#endif /* defined(ETH_P_X25) */
#if defined(ETH_P_ARP)
case ETH_P_ARP:
cp = "ARP";
break;
#endif /* defined(ETH_P_ARP) */
#if defined(ETH_P_BPQ)
case ETH_P_BPQ:
cp = "BPQ";
break;
#endif /* defined(ETH_P_BPQ) */
#if defined(ETH_P_IEEEPUP)
case ETH_P_IEEEPUP:
cp = "I3EPUP";
break;
#endif /* defined(ETH_P_IEEEPUP) */
#if defined(ETH_P_IEEEPUPAT)
case ETH_P_IEEEPUPAT:
cp = "I3EPUPA";
break;
#endif /* defined(ETH_P_IEEEPUPAT) */
#if defined(ETH_P_BATMAN)
case ETH_P_BATMAN:
cp = "BATMAN";
break;
#endif /* defined(ETH_P_BATMAN) */
#if defined(ETH_P_DEC)
case ETH_P_DEC:
cp = "DEC";
break;
#endif /* defined(ETH_P_DEC) */
#if defined(ETH_P_DNA_DL)
case ETH_P_DNA_DL:
cp = "DNA_DL";
break;
#endif /* defined(ETH_P_DNA_DL) */
#if defined(ETH_P_DNA_RC)
case ETH_P_DNA_RC:
cp = "DNA_RC";
break;
#endif /* defined(ETH_P_DNA_RC) */
#if defined(ETH_P_DNA_RT)
case ETH_P_DNA_RT:
cp = "DNA_RT";
break;
#endif /* defined(ETH_P_DNA_RT) */
#if defined(ETH_P_LAT)
case ETH_P_LAT:
cp = "LAT";
break;
#endif /* defined(ETH_P_LAT) */
#if defined(ETH_P_DIAG)
case ETH_P_DIAG:
cp = "DIAG";
break;
#endif /* defined(ETH_P_DIAG) */
#if defined(ETH_P_CUST)
case ETH_P_CUST:
cp = "CUST";
break;
#endif /* defined(ETH_P_CUST) */
#if defined(ETH_P_SCA)
case ETH_P_SCA:
cp = "SCA";
break;
#endif /* defined(ETH_P_SCA) */
#if defined(ETH_P_TEB)
case ETH_P_TEB:
cp = "TEB";
break;
#endif /* defined(ETH_P_TEB) */
#if defined(ETH_P_RARP)
case ETH_P_RARP:
cp = "RARP";
break;
#endif /* defined(ETH_P_RARP) */
#if defined(ETH_P_ATALK)
case ETH_P_ATALK:
cp = "ATALK";
break;
#endif /* defined(ETH_P_ATALK) */
#if defined(ETH_P_AARP)
case ETH_P_AARP:
cp = "AARP";
break;
#endif /* defined(ETH_P_AARP) */
#if defined(ETH_P_8021Q)
case ETH_P_8021Q:
cp = "8021Q";
break;
#endif /* defined(ETH_P_8021Q) */
#if defined(ETH_P_ERSPAN)
case ETH_P_ERSPAN:
cp = "ERSPAN";
break;
#endif /* defined(ETH_P_ERSPAN) */
#if defined(ETH_P_IPX)
case ETH_P_IPX:
cp = "IPX";
break;
#endif /* defined(ETH_P_IPX) */
#if defined(ETH_P_IPV6)
case ETH_P_IPV6:
cp = "IPV6";
break;
#endif /* defined(ETH_P_IPV6) */
#if defined(ETH_P_PAUSE)
case ETH_P_PAUSE:
cp = "PAUSE";
break;
#endif /* defined(ETH_P_PAUSE) */
#if defined(ETH_P_SLOW)
case ETH_P_SLOW:
cp = "SLOW";
break;
#endif /* defined(ETH_P_SLOW) */
#if defined(ETH_P_WCCP)
case ETH_P_WCCP:
cp = "WCCP";
break;
#endif /* defined(ETH_P_WCCP) */
#if defined(ETH_P_MPLS_UC)
case ETH_P_MPLS_UC:
cp = "MPLS_UC";
break;
#endif /* defined(ETH_P_MPLS_UC) */
#if defined(ETH_P_MPLS_MC)
case ETH_P_MPLS_MC:
cp = "MPLS_MC";
break;
#endif /* defined(ETH_P_MPLS_MC) */
#if defined(ETH_P_ATMMPOA)
case ETH_P_ATMMPOA:
cp = "ATMMPOA";
break;
#endif /* defined(ETH_P_ATMMPOA) */
#if defined(ETH_P_PPP_DISC)
case ETH_P_PPP_DISC:
cp = "PPP_DIS";
break;
#endif /* defined(ETH_P_PPP_DISC) */
#if defined(ETH_P_PPP_SES)
case ETH_P_PPP_SES:
cp = "PPP_SES";
break;
#endif /* defined(ETH_P_PPP_SES) */
#if defined(ETH_P_LINK_CTL)
case ETH_P_LINK_CTL:
cp = "LINKCTL";
break;
#endif /* defined(ETH_P_LINK_CTL) */
#if defined(ETH_P_ATMFATE)
case ETH_P_ATMFATE:
cp = "ATMFATE";
break;
#endif /* defined(ETH_P_ATMFATE) */
#if defined(ETH_P_PAE)
case ETH_P_PAE:
cp = "PAE";
break;
#endif /* defined(ETH_P_PAE) */
#if defined(ETH_P_AOE)
case ETH_P_AOE:
cp = "AOE";
break;
#endif /* defined(ETH_P_AOE) */
#if defined(ETH_P_8021AD)
case ETH_P_8021AD:
cp = "8021AD";
break;
#endif /* defined(ETH_P_8021AD) */
#if defined(ETH_P_802_EX1)
case ETH_P_802_EX1:
cp = "802_EX1";
break;
#endif /* defined(ETH_P_802_EX1) */
#if defined(ETH_P_PREAUTH)
case ETH_P_PREAUTH:
cp = "PREAUTH";
break;
#endif /* defined(ETH_P_PREAUTH) */
#if defined(ETH_P_TIPC)
case ETH_P_TIPC:
cp = "TIPC";
break;
#endif /* defined(ETH_P_TIPC) */
#if defined(ETH_P_LLDP)
case ETH_P_LLDP:
cp = "LLDP";
break;
#endif /* defined(ETH_P_LLDP) */
#if defined(ETH_P_MRP)
case ETH_P_MRP:
cp = "MRP";
break;
#endif /* defined(ETH_P_MRP) */
#if defined(ETH_P_MACSEC)
case ETH_P_MACSEC:
cp = "MACSEC";
break;
#endif /* defined(ETH_P_MACSEC) */
#if defined(ETH_P_8021AH)
case ETH_P_8021AH:
cp = "8021AH";
break;
#endif /* defined(ETH_P_8021AH) */
#if defined(ETH_P_MVRP)
case ETH_P_MVRP:
cp = "MVRP";
break;
#endif /* defined(ETH_P_MVRP) */
#if defined(ETH_P_1588)
case ETH_P_1588:
cp = "1588";
break;
#endif /* defined(ETH_P_1588) */
#if defined(ETH_P_NCSI)
case ETH_P_NCSI:
cp = "NCSI";
break;
#endif /* defined(ETH_P_NCSI) */
#if defined(ETH_P_PRP)
case ETH_P_PRP:
cp = "PRP";
break;
#endif /* defined(ETH_P_PRP) */
#if defined(ETH_P_FCOE)
case ETH_P_FCOE:
cp = "FCOE";
break;
#endif /* defined(ETH_P_FCOE) */
#if defined(ETH_P_IBOE)
case ETH_P_IBOE:
cp = "IBOE";
break;
#endif /* defined(ETH_P_IBOE) */
#if defined(ETH_P_TDLS)
case ETH_P_TDLS:
cp = "TDLS";
break;
#endif /* defined(ETH_P_TDLS) */
#if defined(ETH_P_FIP)
case ETH_P_FIP:
cp = "FIP";
break;
#endif /* defined(ETH_P_FIP) */
#if defined(ETH_P_80221)
case ETH_P_80221:
cp = "802.21";
break;
#endif /* defined(ETH_P_80221) */
#if defined(ETH_P_HSR)
case ETH_P_HSR:
cp = "HSR";
break;
#endif /* defined(ETH_P_HSR) */
#if defined(ETH_P_NSH)
case ETH_P_NSH:
cp = "NSH";
break;
#endif /* defined(ETH_P_NSH) */
#if defined(ETH_P_LOOPBACK)
case ETH_P_LOOPBACK:
cp = "LOOPBACK";
break;
#endif /* defined(ETH_P_LOOPBACK) */
#if defined(ETH_P_QINQ1)
case ETH_P_QINQ1:
cp = "QINQ1";
break;
#endif /* defined(ETH_P_QINQ1) */
#if defined(ETH_P_QINQ2)
case ETH_P_QINQ2:
cp = "QINQ2";
break;
#endif /* defined(ETH_P_QINQ2) */
#if defined(ETH_P_QINQ3)
case ETH_P_QINQ3:
cp = "QINQ3";
break;
#endif /* defined(ETH_P_QINQ3) */
#if defined(ETH_P_EDSA)
case ETH_P_EDSA:
cp = "EDSA";
break;
#endif /* defined(ETH_P_EDSA) */
#if defined(ETH_P_DSA_8021Q)
case ETH_P_DSA_8021Q:
cp = "DSAD1Q";
break;
#endif /* defined(ETH_P_DSA_8021Q) */
#if defined(ETH_P_IFE)
case ETH_P_IFE:
cp = "IFE";
break;
#endif /* defined(ETH_P_IFE) */
#if defined(ETH_P_AF_IUCV)
case ETH_P_AF_IUCV:
cp = "AF_IUCV";
break;
#endif /* defined(ETH_P_AF_IUCV) */
#if defined(ETH_P_802_3)
case ETH_P_802_3:
cp = "802.3";
break;
#endif /* defined(ETH_P_802_3) */
#if defined(ETH_P_AX25)
case ETH_P_AX25:
cp = "AX25";
break;
#endif /* defined(ETH_P_AX25) */
#if defined(ETH_P_ALL)
case ETH_P_ALL:
cp = "ALL";
break;
#endif /* defined(ETH_P_ALL) */
#if defined(ETH_P_802_2)
case ETH_P_802_2:
cp = "802.2";
break;
#endif /* defined(ETH_P_802_2) */
#if defined(ETH_P_SNAP)
case ETH_P_SNAP:
cp = "SNAP";
break;
#endif /* defined(ETH_P_SNAP) */
#if defined(ETH_P_DDCMP)
case ETH_P_DDCMP:
cp = "DDCMP";
break;
#endif /* defined(ETH_P_DDCMP) */
#if defined(ETH_P_WAN_PPP)
case ETH_P_WAN_PPP:
cp = "WAN_PPP";
break;
#endif /* defined(ETH_P_WAN_PPP) */
#if defined(ETH_P_PPP_MP)
case ETH_P_PPP_MP:
cp = "PPP MP";
break;
#endif /* defined(ETH_P_PPP_MP) */
#if defined(ETH_P_LOCALTALK)
case ETH_P_LOCALTALK:
cp = "LCLTALK";
break;
#endif /* defined(ETH_P_LOCALTALK) */
#if defined(ETH_P_CAN)
case ETH_P_CAN:
cp = "CAN";
break;
#endif /* defined(ETH_P_CAN) */
#if defined(ETH_P_CANFD)
case ETH_P_CANFD:
cp = "CANFD";
break;
#endif /* defined(ETH_P_CANFD) */
#if defined(ETH_P_PPPTALK)
case ETH_P_PPPTALK:
cp = "PPPTALK";
break;
#endif /* defined(ETH_P_PPPTALK) */
#if defined(ETH_P_TR_802_2)
case ETH_P_TR_802_2:
cp = "802.2";
break;
#endif /* defined(ETH_P_TR_802_2) */
#if defined(ETH_P_MOBITEX)
case ETH_P_MOBITEX:
cp = "MOBITEX";
break;
#endif /* defined(ETH_P_MOBITEX) */
#if defined(ETH_P_CONTROL)
case ETH_P_CONTROL:
cp = "CONTROL";
break;
#endif /* defined(ETH_P_CONTROL) */
#if defined(ETH_P_IRDA)
case ETH_P_IRDA:
cp = "IRDA";
break;
#endif /* defined(ETH_P_IRDA) */
#if defined(ETH_P_ECONET)
case ETH_P_ECONET:
cp = "ECONET";
break;
#endif /* defined(ETH_P_ECONET) */
#if defined(ETH_P_HDLC)
case ETH_P_HDLC:
cp = "HDLC";
break;
#endif /* defined(ETH_P_HDLC) */
#if defined(ETH_P_ARCNET)
case ETH_P_ARCNET:
cp = "ARCNET";
break;
#endif /* defined(ETH_P_ARCNET) */
#if defined(ETH_P_DSA)
case ETH_P_DSA:
cp = "DSA";
break;
#endif /* defined(ETH_P_DSA) */
#if defined(ETH_P_TRAILER)
case ETH_P_TRAILER:
cp = "TRAILER";
break;
#endif /* defined(ETH_P_TRAILER) */
#if defined(ETH_P_PHONET)
case ETH_P_PHONET:
cp = "PHONET";
break;
#endif /* defined(ETH_P_PHONET) */
#if defined(ETH_P_IEEE802154)
case ETH_P_IEEE802154:
cp = "802154";
break;
#endif /* defined(ETH_P_IEEE802154) */
#if defined(ETH_P_CAIF)
case ETH_P_CAIF:
cp = "CAIF";
break;
#endif /* defined(ETH_P_CAIF) */
#if defined(ETH_P_XDSA)
case ETH_P_XDSA:
cp = "XDSA";
break;
#endif /* defined(ETH_P_XDSA) */
#if defined(ETH_P_MAP)
case ETH_P_MAP:
cp = "MAP";
break;
#endif /* defined(ETH_P_MAP) */
default:
cp = NULL;
break;
}
return cp;
}
|