/usr/share/perl5/SNMP/MIB/Compiler.pm is in libsnmp-mib-compiler-perl 0.06-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- Mode: Perl -*-
##########################################################################
# MIB Compiler supporting SMI(v1) and SMIv2
#
# Author: Fabien Tassin <fta@oleane.net>
# Copyright 1998, 1999 Fabien Tassin <fta@oleane.net>
##########################################################################
# See Also :
# Rec. X.208: Specification of Abstract Syntax Notation (ASN.1)
# RFC 1155: Structure and Identification of Management Information
# for TCP/IP-based Internets
# RFC 1158: Management Information Base for network management of
# TCP/IP-based internets: MIB-II
# RFC 1212: Concise MIB definitions
# RFC 1215: Convention for defining traps for use with the SNMP
# RFC 1902: Structure of Management Information for Version 2 of the
# Simple Network Management Protocol (SNMPv2)
# RFC 1903: Textual Conventions for Version 2 of the Simple Network
# Management Protocol (SNMPv2)
# RFC 1904: Conformance Statements for Version 2 of the Simple Network
# Management Protocol (SNMPv2)
##########################################################################
# ABSOLUTELY NO WARRANTY WITH THIS PACKAGE. USE IT AT YOUR OWN RISKS.
##########################################################################
# TODO:
# - resolve constants (e.g. 'max-bindings' in SNMPv2-PDU)
# - check a value against a syntax
# - extend the API
# - more test scripts
package SNMP::MIB::Compiler;
use strict;
use vars qw(@ISA @EXPORT $VERSION $DEBUG);
use Exporter;
use Carp;
use Data::Dumper;
use FileHandle;
@ISA = qw(Exporter);
@EXPORT = ();
$VERSION = 0.06;
$DEBUG = 1; # no longer used
######################################################################
# ASN1 items. (See Rec. X.208 §8)
# Type references (§8.2)
my $ITEM_TYPEREFERENCE_PAT = '[A-Z](\-?[A-Za-z0-9])*';
# Reserved character sequences (See Table 3/X.208)
# and Additional keyword items (See §A.2.9)
my @RESERVED_CHAR_SEQ = ('BOOLEAN', 'INTEGER', 'BIT', 'STRING', 'OCTET',
'NULL', 'SEQUENCE', 'OF', 'SET', 'IMPLICIT', 'CHOICE',
'ANY', 'EXTERNAL', 'OBJECT', 'IDENTIFIER', 'OPTIONAL',
'DEFAULT', 'COMPONENTS', 'UNIVERSAL', 'APPLICATION',
'PRIVATE', 'TRUE', 'FALSE', 'BEGIN', 'END',
'DEFINITIONS', 'EXPLICIT', 'ENUMERATED', 'EXPORTS',
'IMPORTS', 'REAL', 'INCLUDES', 'MIN', 'MAX', 'SIZE',
'FROM', 'WITH', 'COMPONENT', 'PRESENT', 'ABSENT',
'DEFINED', 'BY', 'PLUS-INFINITY', 'MINUS-INFINITY',
'TAGS',
'MACRO', 'TYPE', 'NOTATION', 'VALUE', # Macro keywords
);
# my $ITEM_TYPEREFERENCE = '(?!' .
# (join '(?!\-?[A-Za-z0-9])|', @RESERVED_CHAR_SEQ) .
# '(?!\-?[A-Za-z0-9]))' . $ITEM_TYPEREFERENCE_PAT;
my $ITEM_TYPEREFERENCE =
'(?!(?:' . (join '|', @RESERVED_CHAR_SEQ) . ')(?!\-?[A-Za-z0-9]))' .
$ITEM_TYPEREFERENCE_PAT;
# Identifiers (§8.3)
my $ITEM_IDENTIFIER = '\b[a-z](?:\-?[A-Za-z0-9])*\b';
my $ITEM_IDENTIFIER2 = '\b[a-z](?:[\-_]?[A-Za-z0-9])*\b'; # w/allow_underscore
# Number item (§8.8)
my $ITEM_NUMBER = '\b(?:0|[1-9][0-9]*)\b';
# Binary string item (§8.9) (bstring)
my $ITEM_BINARYSTRING = '\'[01]*\'B';
# Hexadecimal string item (§8.10) (hstring)
my $ITEM_HEXADECIMALSTRING = '\'[A-F0-9]*\'H';
# Single character items (§8.13)
my $ITEM_SINGLECHARACTER = '[\{\}\<,\.\(\)\[\]\-;]';
sub MIBERROR() { 0 };
sub MIBWARN() { 1 };
sub MIBNOTICE() { 2 };
######################################################################
# Tokens
my $TOKEN = &create_tokens();
my $BSTRING = &add_token ($TOKEN, 'BSTRING');
my $HSTRING = &add_token ($TOKEN, 'HSTRING');
my $CSTRING = &add_token ($TOKEN, 'CSTRING');
my $ASSIGNMENT = &add_token ($TOKEN, 'ASSIGNMENT');
my $NUMBER = &add_token ($TOKEN, 'NUMBER');
my $IDENTIFIER = &add_token ($TOKEN, 'IDENTIFIER');
my $TYPEMODREFERENCE = &add_token ($TOKEN, 'TYPEMODREFERENCE');
my $EMPTY = &add_token ($TOKEN, 'EMPTY');
my $BOOLEAN = &add_token ($TOKEN, 'BOOLEAN');
my $INTEGER = &add_token ($TOKEN, 'INTEGER');
my $BIT = &add_token ($TOKEN, 'BIT');
my $STRING = &add_token ($TOKEN, 'STRING');
my $OCTET = &add_token ($TOKEN, 'OCTET');
my $NULL = &add_token ($TOKEN, 'NULL');
my $SEQUENCE = &add_token ($TOKEN, 'SEQUENCE');
my $OF = &add_token ($TOKEN, 'OF');
my $SET = &add_token ($TOKEN, 'SET');
my $IMPLICIT = &add_token ($TOKEN, 'IMPLICIT');
my $CHOICE = &add_token ($TOKEN, 'CHOICE');
my $ANY = &add_token ($TOKEN, 'ANY');
my $EXTERNAL = &add_token ($TOKEN, 'EXTERNAL');
my $OBJECT = &add_token ($TOKEN, 'OBJECT');
my $OPTIONAL = &add_token ($TOKEN, 'OPTIONAL');
my $DEFAULT = &add_token ($TOKEN, 'DEFAULT');
my $COMPONENTS = &add_token ($TOKEN, 'COMPONENTS');
my $UNIVERSAL = &add_token ($TOKEN, 'UNIVERSAL');
my $APPLICATION = &add_token ($TOKEN, 'APPLICATION');
my $PRIVATE = &add_token ($TOKEN, 'PRIVATE');
my $TRUE = &add_token ($TOKEN, 'TRUE');
my $FALSE = &add_token ($TOKEN, 'FALSE');
my $BEGIN = &add_token ($TOKEN, 'BEGIN');
my $END = &add_token ($TOKEN, 'END');
my $DEFINITIONS = &add_token ($TOKEN, 'DEFINITIONS');
my $EXPLICIT = &add_token ($TOKEN, 'EXPLICIT');
my $ENUMERATED = &add_token ($TOKEN, 'ENUMERATED');
my $EXPORTS = &add_token ($TOKEN, 'EXPORTS');
my $IMPORTS = &add_token ($TOKEN, 'IMPORTS');
my $REAL = &add_token ($TOKEN, 'REAL');
my $INCLUDES = &add_token ($TOKEN, 'INCLUDES');
my $MIN = &add_token ($TOKEN, 'MIN');
my $MAX = &add_token ($TOKEN, 'MAX');
my $SIZE = &add_token ($TOKEN, 'SIZE');
my $FROM = &add_token ($TOKEN, 'FROM');
my $WITH = &add_token ($TOKEN, 'WITH');
my $COMPONENT = &add_token ($TOKEN, 'COMPONENT');
my $PRESENT = &add_token ($TOKEN, 'PRESENT');
my $ABSENT = &add_token ($TOKEN, 'ABSENT');
my $DEFINED = &add_token ($TOKEN, 'DEFINED');
my $BY = &add_token ($TOKEN, 'BY');
my $PLUSINFINITY = &add_token ($TOKEN, 'PLUSINFINITY');
my $MINUSINFINITY = &add_token ($TOKEN, 'MINUSINFINITY');
my $TAGS = &add_token ($TOKEN, 'TAGS');
my $MACRO = &add_token ($TOKEN, 'MACRO');
my $TYPE = &add_token ($TOKEN, 'TYPE');
my $NOTATION = &add_token ($TOKEN, 'NOTATION');
my $VALUE = &add_token ($TOKEN, 'VALUE');
my $MACROTYPE = &add_token ($TOKEN, 'MACROTYPE');
my $MACROVALUE = &add_token ($TOKEN, 'MACROVALUE');
my $keywords = {
'BOOLEAN' => $BOOLEAN,
'INTEGER' => $INTEGER,
'BIT' => $BIT,
'STRING' => $STRING,
'OCTET' => $OCTET,
'NULL' => $NULL,
'SEQUENCE' => $SEQUENCE,
'OF' => $OF,
'SET' => $SET,
'IMPLICIT' => $IMPLICIT,
'CHOICE' => $CHOICE,
'ANY' => $ANY,
'EXTERNAL' => $EXTERNAL,
'OBJECT' => $OBJECT,
'IDENTIFIER' => $IDENTIFIER,
'OPTIONAL' => $OPTIONAL,
'DEFAULT' => $DEFAULT,
'COMPONENTS' => $COMPONENTS,
'UNIVERSAL' => $UNIVERSAL,
'APPLICATION' => $APPLICATION,
'PRIVATE' => $PRIVATE,
'TRUE' => $TRUE,
'FALSE' => $FALSE,
'BEGIN' => $BEGIN,
'END' => $END,
'DEFINITIONS' => $DEFINITIONS,
'EXPLICIT' => $EXPLICIT,
'ENUMERATED' => $ENUMERATED,
'EXPORTS' => $EXPORTS,
'IMPORTS' => $IMPORTS,
'REAL' => $REAL,
'INCLUDES' => $INCLUDES,
'MIN' => $MIN,
'MAX' => $MAX,
'SIZE' => $SIZE,
'FROM' => $FROM,
'WITH' => $WITH,
'COMPONENT' => $COMPONENT,
'PRESENT' => $PRESENT,
'ABSENT' => $ABSENT,
'DEFINED' => $DEFINED,
'BY' => $BY,
'TAGS' => $TAGS,
'MACRO' => $MACRO,
'TYPE' => $TYPE,
'NOTATION' => $NOTATION,
'VALUE' => $VALUE,
'MACROTYPE' => $MACROTYPE,
'MACROVALUE' => $MACROVALUE,
};
######################################################################
# Create the standard tokens
sub create_tokens {
my $TOKEN = [];
my $i = -1;
while ($i++ < 255) {
$$TOKEN[$i] = chr $i;
}
$TOKEN;
}
# Add a 'specialized' token to the current list of tokens
sub add_token {
my $TOKEN = shift;
my $k = shift;
push @$TOKEN, $k;
$#$TOKEN;
}
# The 'heart' of the compiler: the parser
# returns the couple token/value or the scalar 0 if something goes wrong.
sub yylex {
my $self = shift;
my $s = $self->{'stream'};
my $val;
my $c = ' '; # initialization.
CHAR: while ($c ne '' && $c !~ m/^[A-Za-z0-9:=,\{\}<.\(\)\[\]\'\">|]$/o) {
# remove useless blanks and comments
1 while ($c = $s->getc) eq ' ' || $c eq "\t" || $c eq "\n" || $c eq "\r";
return 0 if $c eq '';
if ($c eq '-') { # The first char of a "comment" (See §8.6)
$c = $s->getc;
if ($c eq '') { # a single hyphen followed by EOF
$s->ungetc; # keep EOF for the next yylex call
$c = '-';
last CHAR;
}
if ($c eq '-') { # it is a real "comment" marker
while (1) {
1 while ($c = $s->getc) ne '' && $c ne '-'
&& $c ne "\n" && $c ne "\r";
return 0 if $c eq ''; # End of file.
if ($c eq '-') {
$c = $s->getc;
return 0 if $c eq ''; # End of file.
next CHAR if $c eq "\n" || $c eq "\r" ||
$c eq '-'; # End of comment.
}
next CHAR if $c eq "\n"; # End of comment.
}
}
else { # it is NOT a comment but a single hyphen.
$s->ungetc;
$c = '-';
last CHAR;
}
}
else {
last;
}
}
# Here, the current char is a valid ASN.1 char, it can be a hyphen but
# not a double hyphen (comment start).
# Read a word and return the correspondant token.
return 0 if $c eq '';
if ($c =~ m/^$ITEM_SINGLECHARACTER$/o) {
# it is a single characters
return (ord ($c), $c);
}
if ($c =~ m/^[>|]/o) {
# it is a single extension characters
return (ord ($c), $c);
}
if ($c eq '\'') { # it can be a cstring or a hstring.
$val = $c;
# while (($c = $s->getc) ne '' && ($c =~ m/[0-9A-F]/o ||
# ($self->{'allow_lowcase_hstrings'} && $c =~ m/[a-f]/o))) {
while (($c = $s->getc) ne '' && $c =~ m/[0-9A-Fa-f]/o) {
$val .= $c;
}
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Unexpected EOF near \"$val\"") if $c eq '';
if ($c eq '\'') {
$val .= $c;
$c = $s->getc;
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Unexpected EOF near \"$val\"") if $c eq '';
$val .= $c;
# it must be 'B' or 'H'.
$c = 'B' if $c eq 'b' && $self->{'allow_lowcase_bstrings'};
if ($c =~ m/[hH]/o && $self->{'allow_lowcase_hstrings'}) {
$c = 'H';
$val = uc $val;
}
if ($c eq 'B' && $val =~ m/^$ITEM_BINARYSTRING$/o) {
return ($BSTRING, $val);
}
if ($c eq 'H' && $val =~ m/^$ITEM_HEXADECIMALSTRING$/o) {
return ($HSTRING, $val);
}
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Invalid \"$val\". See 'allow_lowcase_{b|h}strings' switches");
}
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Syntax error near \"$val\"");
}
if ($c eq '"') { # a cstring
$val = $c;
while (1) {
while (($c = $s->getc) ne '' && $c ne '"') {
$val .= $c;
}
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Unexpected EOF near \"$val\"") if $c eq '';
$val .= $c;
$c = $s->getc;
if ($c eq '' || $c ne '"') {
$s->ungetc if $c;
return ($CSTRING, $val);
}
$val .= $c;
}
}
if ($c eq ':') { # an assignment item
$val = $c;
$c = $s->getc;
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Unexpected EOF near \"$val\"") if $c eq '';
if ($c ne ':') {
$s->ungetc if $c;
}
else {
$val .= $c;
$c = $s->getc;
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Unexpected EOF near \"$val\"") if $c eq '';
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Syntax error near \"$val\"") unless $c eq '=';
$val .= $c;
return ($ASSIGNMENT, $val);
}
}
if ($c =~ m/\d/o) { # it is a number
$val = $c;
while (($c = $s->getc) ne '' && $c =~ m/\d/o) {
$val .= $c;
}
$s->ungetc if $c;
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Syntax error near \"$val\"") unless $val =~ m/^$ITEM_NUMBER$/;
return ($NUMBER, $val);
}
if ($c =~ m/[a-zA-Z]/o) {
$val = $c;
while (1) {
while (($c = $s->getc) ne '' && ($c =~ m/[A-Za-z0-9]/o ||
($self->{'allow_underscore'} && $c eq '_'))) {
$val .= $c;
}
if ($c eq '-') {
$c = $s->getc;
# a hyphen shall not be the last character
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Syntax error near \"$val\"")
if $c eq '' || $c eq "\n" || $c eq "\r";
if ($c eq '-') { # it is a comment.
COMM: while (1) {
1 while ($c = $s->getc) ne '' && $c ne '-' &&
$c ne "\n" && $c ne "\r";
last COMM if $c eq '' || # End of file...
$c eq "\n" || $c eq "\r"; # End of comment.
$c = $s->getc;
last COMM if $c eq '' || # End of file...
$c eq "\n" || $c eq "\r" || $c eq '-'; # End of comment.
$s->ungetc;
}
# TypeReference or ModuleReference
return ($TYPEMODREFERENCE, $val)
if $val =~ m/^$ITEM_TYPEREFERENCE$/o;
# Identifier or ValueReference
return ($IDENTIFIER, $val) if $val =~ m/^$ITEM_IDENTIFIER$/o;
return ($IDENTIFIER, $val) if $val =~ m/^$ITEM_IDENTIFIER2$/o &&
$self->{'allow_underscore'};
return ($PLUSINFINITY, $val) if $val eq 'PLUS-INFINITY';
return ($MINUSINFINITY, $val) if $val eq 'MINUS-INFINITY';
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"Syntax error near \"$val\"");
}
$s->ungetc if $c ne '';
$val .= "-";
}
if ($c !~ m/[A-Za-z0-9]/o) {
$s->ungetc if $c;
# Is it a known keyword ?
return ($$keywords{$val}, $val) if defined $$keywords{$val};
# TypeReference/ModuleReference/MacroReference/ProductionReference/
# LocalTypeReference
return ($TYPEMODREFERENCE, $val) if $val =~ m/^$ITEM_TYPEREFERENCE$/o;
# Identifier/ValueReference/LocalValueReference
return ($IDENTIFIER, $val) if $val =~ m/^$ITEM_IDENTIFIER$/o;
return ($IDENTIFIER, $val) if $val =~ m/^$ITEM_IDENTIFIER2$/o &&
$self->{'allow_underscore'};
return ($PLUSINFINITY, $val) if $val eq 'PLUS-INFINITY';
return ($MINUSINFINITY, $val) if $val eq 'MINUS-INFINITY';
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"'$val' unrecognized");
}
}
}
return $self->assert(MIBERROR, $self->{'filename'}, $s->{'lineno'},
"'$c' unrecognized");
}
# Constructor
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
$self->initialize();
return $self;
}
# Create the MIB tree with some special nodes.
sub initialize {
my $self = shift;
$self->{'token_list'} = [];
$self->{'srcpath'} = [];
# extension of the produced files
$self->{'dumpext'} = ".dump";
# '_' is not defined in the ASN.1 charset set but is sometimes found
# in SNMP MIBs. This flag can be used to avoid parsing errors on such
# mibs.
$self->{'allow_underscore'} = 0;
# 'abfc'h is invalid (must be 'ABFC'H) but is sometimes used in SNMP MIBs.
$self->{'allow_lowcase_hstrings'} = 0;
# '1001'b is invalid (must be '1001'B) but is sometimes used in SNMP MIBs.
$self->{'allow_lowcase_bstrings'} = 0;
$self->{'allow_keyword_any'} = 1;
# Add the 3 roots of the tree.
# These nodes cannot be specified using valid ASN.1 clauses.
$self->{'root'}{'ccitt'}{'oid'} = [ 0 ];
$self->{'root'}{'iso'}{'oid'} = [ 1 ];
$self->{'root'}{'joint-iso-ccitt'}{'oid'} = [ 2 ];
# debug flags
$self->{'debug_recursive'} = 0;
$self->{'debug_lexer'} = 0;
$self->{'make_dump'} = 1;
$self->{'use_dump'} = 1;
$self->{'accept_smiv1'} = 1;
$self->{'accept_smiv2'} = 1;
# should we import dependencies ?
$self->{'do_imports'} = 1;
}
sub assert {
my $self = shift;
my $level = shift;
my $file = shift;
my $line = shift;
my $msg = shift;
if (defined $level) {
$self->{'msg'} = [] unless defined $self->{'msg'};
my ($cpackage, $cfile, $cline) = caller;
push @{$self->{'msg'}}, { 'level' => $level,
'file' => $file,
'line' => $line,
'msg' => sprintf($msg, @_),
'cpackage' => $cpackage,
'cfile' => $cfile,
'cline' => $cline,
};
return $level;
}
else {
if (wantarray) {
return $level;
}
else {
my $s = "";
map {
$s .= sprintf "Error %d: %s at %s line %d.%s\n", $$_{'level'},
$$_{'msg'}, $$_{'file'}, $$_{'line'},
$DEBUG ? sprintf " [%s %d]", $$_{'cfile'}, $$_{'cline'}: "";
} @{$self->{'msg'}} if defined $self->{'msg'};
return $s;
}
}
}
# Get the next token from the parser
sub get_token {
my $self = shift;
my $needed = shift;
my ($res, $k);
if (@{$self->{'token_list'}}) {
my $temp = shift @{$self->{'token_list'}};
($res, $k) = ($$temp[0], $$temp[1]);
$self->{'lineno'} = $$temp[2];
}
else {
($res, $k) = $self->yylex();
$self->{'lineno'} = $self->{'stream'}->lineno;
return unless $res;
}
warn "DEBUG: token='" . ($res ? $$TOKEN[$res] : $res) . "' value='" .
(defined $k ? $k : '<EOF>') . "'\n" if $self->{'debug_lexer'};
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"'$needed' expected")
if defined $needed && $res && $$TOKEN[$res] ne $needed;
$self->{'current_token'} = $res;
$self->{'current_value'} = $k;
($res, $k);
}
# Requeue the last token in the incoming queue.
# WARNING: only one token can be requeued.
sub unget_token {
my $self = shift;
warn "DEBUG: unshift\n" if $self->{'debug_lexer'};
if (defined $self->{'current_token'}) {
push @{$self->{'token_list'}}, [ $self->{'current_token'},
$self->{'current_value'},
$self->{'lineno'} ];
$self->{'current_token'} = $self->{'current_value'} =
$self->{'lineno'} = undef;
}
else {
die "Error: can't unget more than one token. Abort.\n";
}
}
sub create_tree {
my $self = shift;
for my $node (keys %{$self->{'nodes'}}) {
my $t = $self->{'nodes'}{$node}{'oid'};
$self->{'tree'}{$$t[$#$t - 1]}{$$t[$#$t]} = $node;
}
}
# Compile a MIB file given its name
sub compile {
my $self = shift;
my $file = shift;
croak "Error: you MUST specify a file to compile\n" unless $file;
my $outdir = $self->repository;
croak "Error: you MUST specify a repository\n"
if $self->{'make_dump'} && !$outdir;
my $filename;
if ($file =~ m|/([^/]+)$|) {
croak "Error: can't find $file" unless -e $file;
$filename = $file;
$file = $1;
}
else {
my $dir = $self->{'srcpath'} ||
croak "Error: you MUST specify a path using add_path()\n";
my $ext = $self->extensions || [ '' ];
my $windir;
my $extfile;
my @dirtmp = @$dir;
while (my $d = shift @dirtmp) {
map {
my $e = $_;
# warn "testing '$d/$file$e'\n";
$windir = $d, $extfile = $e, last if -e "$d/$file$e";
} @$ext;
}
croak "Error: can't find $file" unless $windir;
$filename = "$windir/$file$extfile";
}
# push @{$self->{'filename'}}, $filename;
$self->{'filename'} = $filename;
# my $filename = $ {$self->{'filename'}}[$#{$self->{'filename'}}];
if ($self->{'use_dump'} && -e "$outdir/$file$self->{'dumpext'}") {
if (-M $filename < -M "$outdir/$file$self->{'dumpext'}") {
$self->assert(MIBNOTICE, $self->{'filename'}, $self->{'fileno'},
"$outdir/$file$self->{'dumpext'} is older than " .
"$filename. Recompiling $filename...");
}
else {
my $v;
my $fh = new FileHandle "$outdir/$file$self->{'dumpext'}";
if (defined $fh) {
local $/ = undef;
$v = eval <$fh>;
if ($v) {
map { $self->{'nodes'}{$_} = $$v{'nodes'}{$_} } keys %{$$v{'nodes'}};
map { $self->{'types'}{$_} = $$v{'types'}{$_} } keys %{$$v{'types'}};
for my $node (keys %{$$v{'tree'}}) {
for my $son (keys %{$$v{'tree'}{$node}}) {
$self->{'tree'}{$node}{$son} = $$v{'tree'}{$node}{$son};
}
}
map { $self->{'traps'}{$_} = $$v{'traps'}{$_} } keys %{$$v{'traps'}};
map { push @{$self->{'macros'}}, $_ } @{$$v{'macros'}};
}
$fh->close;
}
return $self if $v;
}
}
# open the MIB file
my $fh = new FileHandle $filename;
unless (defined $fh) {
croak "Error: can't open $filename: $!\n";
return;
}
# create a new MIB object
my $mib = new SNMP::MIB::Compiler;
$mib->{'filename'} = $filename;
$mib->repository($self->repository);
$mib->extensions($self->extensions);
$mib->{'srcpath'} = $self->{'srcpath'};
push @{$mib->{'msg'}}, @{$self->{'msg'}} if defined $self->{'msg'};
$mib->{'make_dump'} = $self->{'make_dump'};
$mib->{'use_dump'} = $self->{'use_dump'};
$mib->{'do_imports'} = $self->{'do_imports'};
$mib->{'allow_underscore'} = $self->{'allow_underscore'};
$mib->{'allow_lowcase_hstrings'} = $self->{'allow_lowcase_hstrings'};
$mib->{'allow_lowcase_bstrings'} = $self->{'allow_lowcase_bstrings'};
if ($self->{'debug_recursive'}) {
$mib->{'debug_recursive'} = $self->{'debug_recursive'};
$mib->{'debug_lexer'} = $self->{'debug_lexer'};
}
# create a stream
my $s = Stream->new($fh);
$mib->{'stream'} = $s;
# parse the MIB
my $r = $mib->parse_Module();
push @{$self->{'msg'}}, @{$mib->{'msg'}} if defined $mib->{'msg'};
# destroy the stream
delete $mib->{'stream'};
# close the file
$fh->close;
return undef unless $r;
# Create the MIB 'tree'
$mib->create_tree();
if ($self->{'make_dump'}) {
local $Data::Dumper::Purity = 1;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Terse = 1;
my $file = $mib->{'name'};
my $fh = new FileHandle "> $outdir/$file$self->{'dumpext'}";
if (defined $fh) {
print $fh "## Compiled by SNMP::MIB::Compiler version $VERSION\n" .
"## Source: $filename\n" .
"## Date: " . (scalar localtime (time)) ."\n\n";
print $fh Dumper { 'nodes' => $mib->{'nodes'},
'types' => $mib->{'types'},
'macros' => $mib->{'macros'},
'tree' => $mib->{'tree'},
'traps' => $mib->{'traps'},
'version' => $VERSION,
};
$fh->close;
}
else {
croak "Warning: can't create dump $outdir/$file$self->{'dumpext'}" .
": $!\n";
}
}
# insert this MIB into the current object
map { $self->{'nodes'}{$_} = $mib->{'nodes'}{$_} } keys %{$mib->{'nodes'}};
map { $self->{'types'}{$_} = $mib->{'types'}{$_} } keys %{$mib->{'types'}};
map { $self->{'traps'}{$_} = $mib->{'traps'}{$_} } keys %{$mib->{'traps'}};
map { push @{$self->{'macros'}}, $_ } @{$mib->{'macros'}};
for my $node (keys %{$mib->{'tree'}}) {
for my $son (keys %{$self->{'tree'}{$node}}) {
$self->{'tree'}{$node}{$son} = $mib->{'tree'}{$node}{$son};
}
}
$self->create_tree();
$self;
}
sub load {
my $self = shift;
my $file = shift;
croak "Error: you MUST specify a MIB to load\n" unless $file;
my $outdir = $self->repository;
die "Error: you MUST specify a repository\n" unless $outdir;
if ($self->{'use_dump'} && -e "$outdir/$file$self->{'dumpext'}") {
my $v;
my $fh = new FileHandle "$outdir/$file$self->{'dumpext'}";
if (defined $fh) {
local $/ = undef;
$v = eval <$fh>;
if ($v) {
map { $self->{'nodes'}{$_} = $$v{'nodes'}{$_} } keys %{$$v{'nodes'}};
map { $self->{'types'}{$_} = $$v{'types'}{$_} } keys %{$$v{'types'}};
map { $self->{'traps'}{$_} = $$v{'traps'}{$_} } keys %{$$v{'traps'}};
for my $node (keys %{$$v{'tree'}}) {
for my $son (keys %{$$v{'tree'}{$node}}) {
$self->{'tree'}{$node}{$son} = $$v{'tree'}{$node}{$son};
}
}
map { push @{$self->{'macros'}}, $_ } @{$$v{'macros'}};
}
$fh->close;
}
1;
}
else {
$self->assert(MIBWARN, $self->{'filename'}, $self->{'lineno'},
"can't find precompiled $file. Ignored\n") if $self->{'debug_lexer'};
0;
}
}
sub parse_Module {
my $self = shift;
my ($token, $mibname, $value);
# ModuleIdentifier
(($token, $mibname) = $self->get_token('TYPEMODREFERENCE')) || return;
$self->{'name'} = $mibname;
$self->get_token('DEFINITIONS') || return;
$self->get_token('ASSIGNMENT') || return;
$self->get_token('BEGIN') || return;
(($token, $value) = $self->get_token()) || return;
while ($token && $token != $END) {
if ($token == $IMPORTS) {
$self->{'imports'} = $self->parse_imports();
$self->import_modules() if $self->{'do_imports'};
}
elsif ($token == $EXPORTS) {
$self->{'exports'} = $self->parse_exports();
}
elsif ($token == $IDENTIFIER) {
my $assign = $value;
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error") unless $token;
if ($token == $OBJECT) { # probably an OBJECT IDENTIFIER
$self->get_token('IDENTIFIER') || return;
$self->get_token('ASSIGNMENT') || return;
my $oid = $self->parse_oid();
$self->{'nodes'}{$assign}{'oid'} = $oid;
$self->{'nodes'}{$assign}{'type'} = 'OBJECT IDENTIFIER';
}
elsif ($token == $INTEGER) {
$self->get_token('ASSIGNMENT') || return;
(($token, $value) = $self->get_token()) || return;
$self->{'constants'}{$assign}{'value'} = $value;
}
elsif ($value eq 'OBJECT-TYPE') {
$self->{'nodes'}{$assign} = $self->parse_objecttype() || return;
# return undef unless $self->{'nodes'}{$assign};
$self->{'nodes'}{$assign}{'type'} = 'OBJECT-TYPE';
}
elsif ($value eq 'OBJECT-IDENTITY') {
$self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'") unless $self->{'accept_smiv2'};
$self->{'nodes'}{$assign} = $self->parse_objectidentity() || return;
$self->{'nodes'}{$assign}{'type'} = 'OBJECT-IDENTITY';
}
elsif ($value eq 'MODULE-IDENTITY') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv2'};
$self->{'nodes'}{$assign} = $self->parse_moduleidentity();
$self->{'nodes'}{$assign}{'type'} = 'MODULE-IDENTITY';
}
elsif ($value eq 'MODULE-COMPLIANCE') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv2'};
$self->{'nodes'}{$assign} = $self->parse_modulecompliance();
$self->{'nodes'}{$assign}{'type'} = 'MODULE-COMPLIANCE';
}
elsif ($value eq 'OBJECT-GROUP') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv2'};
$self->{'nodes'}{$assign} = $self->parse_objectgroup();
$self->{'nodes'}{$assign}{'type'} = 'OBJECT-GROUP';
}
elsif ($value eq 'NOTIFICATION-GROUP') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv2'};
$self->{'nodes'}{$assign} = $self->parse_notificationgroup();
$self->{'nodes'}{$assign}{'type'} = 'NOTIFICATION-GROUP';
}
elsif ($value eq 'AGENT-CAPABILITIES') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv2'};
$self->{'nodes'}{$assign} = $self->parse_agentcapabilities();
$self->{'nodes'}{$assign}{'type'} = 'AGENT-CAPABILITIES';
}
elsif ($value eq 'TRAP-TYPE') {
# as defined in RFC 1215
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv1'};
$self->{'traps'}{$assign} = $self->parse_traptype();
$self->{'traps'}{$assign}{'type'} = 'TRAP-TYPE';
}
elsif ($value eq 'NOTIFICATION-TYPE') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'")
unless $self->{'accept_smiv2'};
$self->{'traps'}{$assign} = $self->parse_notificationtype();
$self->{'traps'}{$assign}{'type'} = 'NOTIFICATION-TYPE';
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'");
}
}
elsif ($token == $TYPEMODREFERENCE) {
my $label = $value;
(($token, $value) = $self->get_token()) || return;
if ($token == $ASSIGNMENT) {
my $type = $self->parse_type();
# warn "Warning: type '$label' already defined"
# if defined $self->{'types'}{$label};
$self->{'types'}{$label} = $type;
}
elsif ($token == $MACRO) {
# Skip this beast..
(($token, $value) = $self->get_token('ASSIGNMENT')) || return;
while ($token && $token != $END) {
(($token, $value) = $self->get_token()) || return;
}
push @{$self->{'macros'}}, $label;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unrecognized syntax '$value' ($$TOKEN[$token])...");
}
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"received an unknown token ($$TOKEN[$token])");
}
(($token, $value) = $self->get_token()) || return;
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"'END' expected") unless $token == $END;
}
# Given a type, this will return the corresponding SMI (v2 or v1) type
# or the corresponding ASN.1 type.
sub resolve_type {
my $self = shift;
my $type = shift;
# a basic ASN.1 type.
return $type if $type =~ m/^(SEQUENCE|CHOICE|INTEGER|OCTET\ STRING|
OBJECT\ IDENTIFIER|NULL)$/ox;
# SMIv1 type
return $type if $type =~ m/^(IpAddress|Counter|Gauge|TimeTicks|Opaque)$/o;
# SMIv2 type
return $type if $type =~ m/^(Integer32|Counter32|Gauge32|Unsigned32|
Counter64)$/ox;
defined $self->{'types'}{$type} ?
defined $self->{'types'}{$type}{'syntax'} ?
$self->{'types'}{$type}{'syntax'}{'type'} :
$self->{'types'}{$type}{'type'} : $type;
}
sub resolve_oid {
my $self = shift;
my $node = shift;
return $node unless defined $node; # no node
return $node unless defined $self->{'nodes'}{$node} &&
scalar keys %{$self->{'nodes'}{$node}} ||
defined $self->{'root'}{$node} &&
scalar keys %{$self->{'root'}{$node}}; # no such node
# copy the OID if needed
if (defined $self->{'nodes'}{$node}{'oid'} &&
!defined $self->{'nodes'}{$node}{'OID'}) {
$self->{'nodes'}{$node}{'OID'} = [];
@{$self->{'nodes'}{$node}{'OID'}} = @{$self->{'nodes'}{$node}{'oid'}};
}
my $list = $self->{'nodes'}{$node}{'OID'} ||
$self->{'root'}{$node}{'oid'};
while (defined $self->{'nodes'}{$$list[0]} ||
defined $self->{'root'}{$$list[0]}) {
# copy the OID if needed
if (defined $self->{'nodes'}{$$list[0]} &&
defined $self->{'nodes'}{$$list[0]}{'oid'} &&
!defined $self->{'nodes'}{$$list[0]}{'OID'}) {
$self->{'nodes'}{$$list[0]}{'OID'} = [];
@{$self->{'nodes'}{$$list[0]}{'OID'}} =
@{$self->{'nodes'}{$$list[0]}{'oid'}};
}
my @l = @$list;
if (defined $self->{'nodes'}{$$list[0]}) {
my $eq = 1;
if ($#{$self->{'nodes'}{$$list[0]}{'OID'}} ==
$#{$self->{'nodes'}{$$list[0]}{'oid'}}) {
my $i = -1;
for (@{$self->{'nodes'}{$$list[0]}{'OID'}}) {
$i++;
$eq = 0, last unless $ {$self->{'nodes'}{$$list[0]}{'OID'}}[$i]
eq $ {$self->{'nodes'}{$$list[0]}{'oid'}}[$i];
}
unless ($eq) {
my @a = @{$self->{'nodes'}{$$list[0]}{'oid'}};
my @l = @{$self->{'nodes'}{$$list[0]}{'OID'}};
shift @l;
my $last = pop @l;
for my $elem (@l) {
last unless $elem =~ m/^\d+$/o;
my $o = shift @a;
$self->{'tree'}{$o}{$elem} = $a[0];
}
$self->{'tree'}{$a[0]}{$last} = $node if scalar @a == 1;
}
}
}
splice @$list, 0, 1, defined $self->{'nodes'}{$$list[0]} &&
scalar keys %{$self->{'nodes'}{$$list[0]}} ?
@{$self->{'nodes'}{$$list[0]}{'OID'}} :
@{$self->{'root'}{$$list[0]}{'oid'}};
}
for my $l (@$list) {
if (defined $self->{'nodes'}{$l}) {
# copy the OID if needed
if (defined $self->{'nodes'}{$l} &&
defined $self->{'nodes'}{$l}{'oid'} &&
!defined $self->{'nodes'}{$l}{'OID'}) {
$self->{'nodes'}{$l}{'OID'} = [];
@{$self->{'nodes'}{$l}{'OID'}} = @{$self->{'nodes'}{$l}{'oid'}};
}
my @t = @{$self->{'nodes'}{$l}{'OID'}};
$l = $t[$#t];
}
if (defined $self->{'root'}{$l}) {
my @t = @{$self->{'root'}{$l}{'OID'}};
$l = $t[$#t];
}
}
join '.', @$list;
}
sub convert_oid {
my $self = shift;
my $oid = shift;
my @l = split /\./, $oid;
my @r;
my $node = $l[0];
for my $id (keys %{$self->{'root'}}) {
last unless $l[0] =~ m/^\d+$/o;
$node = $id, last if $l[0] == $self->{'root'}{$id}{'oid'}[0];
}
shift @l;
push @r, $node;
while (my $elem = shift @l) {
push (@r, $elem), last unless defined $self->{'tree'}{$node}{$elem};
push @r, $self->{'tree'}{$node}{$elem};
$node = $self->{'tree'}{$node}{$elem};
}
join '.', @r, @l;
}
sub parse_one {
my $self = shift;
warn "DEBUG: Parsing one item...\n" if $self->{'debug_lexer'};
# 1
# -1..3
# foo
# foo..bar
(my ($token, $value) = $self->get_token()) || return;
if ($value eq '-') { # a negative value ?
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"\"$value\" must be an integer") unless $token == $NUMBER;
$value = "-$value";
}
my $val = $value;
(($token, $value) = $self->get_token()) || return;
if ($value eq '.') {
$self->get_token('.') || return; # range
(($token, $value) = $self->get_token()) || return;
if ($value eq '-') { # a negative value ?
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"\"$value\" must be an integer") unless $token == $NUMBER;
$value = "-$value";
}
$val = { 'range' => { 'min' => $val, 'max' => $value } };
}
else {
$self->unget_token();
}
$val;
}
sub parse_subtype {
my $self = shift;
warn "DEBUG: Parsing a sub-type...\n" if $self->{'debug_lexer'};
(my ($token, $value) = $self->get_token()) || return;
if ($token && $value eq '(') {
(($token, $value) = $self->get_token()) || return;
if ($token == $SIZE) {
my $subtype = $self->parse_subtype();
return unless $subtype;
$self->get_token(')') || return;
return { 'size' => $subtype };
}
else {
$self->unget_token();
my $list;
while ($value ne ')') {
my $v = $self->parse_one();
return unless defined $v;
push @$list, $v;
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"\"$value\" must be ')' or '|'")
unless $value eq ')' || $value eq '|';
}
# return scalar @$list == 1 ? { 'value' => $$list[0] } :
# { 'choice' => $list };
return scalar @$list == 1 ? $$list[0] : { 'choice' => $list };
}
}
elsif ($token && $value eq '{') {
my $list = {};
while ($value ne '}') {
(($token, $value) = $self->get_token()) || return;
if ($token == $IDENTIFIER) {
my $res = $self->parse_subtype();
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"must have a subtype") unless defined $res;
$$list{$res} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"should be an identifier");
}
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"must be a '}' or a ',' instead of '$value'")
unless $value eq '}' || $value eq ',';
}
return { 'values' => $list };
}
else {
$self->unget_token();
}
}
# parse a type (SYNTAX field of an OBJECT-TYPE, TC)
sub parse_type {
my $self = shift;
warn "DEBUG: Parsing a type...\n" if $self->{'debug_lexer'};
(my ($token, $value) = $self->get_token()) || return;
if ($token == $IMPLICIT) { # implicit types
my $type = $self->parse_type();
my $ref = ref $type;
if (defined $ref && $ref eq 'HASH') {
$$type{'implicit'} = 'true';
return $type;
}
else {
return { 'implicit' => 'true',
'type' => $type };
}
}
elsif ($token == $INTEGER) { # integers
my $type = "INTEGER";
my $subtype = $self->parse_subtype(); # what if parse_subtype failed ???
my $ref = ref $subtype;
if (defined $ref && $ref eq 'HASH') {
$$subtype{'type'} = $type;
return $subtype;
}
else {
if (defined $subtype) {
return { 'values' => $subtype,
'type' => $type };
}
else {
return { 'type' => $type };
}
}
}
elsif ($token == $OCTET) { # octet strings
(($token, $value) = $self->get_token()) || return;
if ($token == $STRING) {
my $type = "OCTET STRING";
my $subtype = $self->parse_subtype();
$$subtype{'type'} = $type;
return $subtype;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"parse error");
}
}
elsif ($token == $OBJECT) { # object types
(($token, $value) = $self->get_token()) || return;
if ($token == $IDENTIFIER) {
my $type = "OBJECT IDENTIFIER";
my $subtype = $self->parse_subtype();
$$subtype{'type'} = $type;
return $subtype;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"parse error");
}
}
elsif ($token == $NULL) {
return { 'type' => "NULL" };
}
elsif ($token == $ANY && $self->{'allow_keyword_any'}) {
# ANY is only valid in ASN.1.. but nor in SMI, nor SMIv2.
# As it is used in RFC 1157, we must allow it :(
return { 'type' => "ANY" };
}
elsif ($token == $CHOICE) { # choices
# CHOICE { va ta, vb tb, vc tc }
(($token, $value) = $self->get_token('{')) || return;
my $list = {};
while ($value ne '}') {
(($token, $value) = $self->get_token()) || return;
my $res = $self->parse_type();
my $ref = ref $res;
if (defined $ref && $ref eq 'HASH') {
$$list{$value} = $res;
}
else {
$$list{$value} = { 'type' => $res };
}
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"must be a '}' or a ',' instead of '$value'")
unless $value eq '}' || $value eq ',';
}
return { 'type' => 'CHOICE',
'items' => $list };
}
elsif ($token == $SEQUENCE) { # sequence (of)
my $list = {}; # Should we keep the order of the items (and then
# use an array instead of a hash) ??
my $subtype;
(($token, $value) = $self->get_token()) || return;
if ($value eq '(') {
$self->unget_token();
$subtype = $self->parse_subtype();
(($token, $value) = $self->get_token()) || return;
}
if ($token == $OF) {
# Small hack to obtain a name for this unique (?) item
(my ($t1, $t2) = $self->get_token()) || return;
$self->unget_token();
$t2 = lc $t2;
my $res = $self->parse_type();
my $r = { 'type' => 'SEQUENCE' };
$$r{'items'} = { $t2 => $res };
if (defined $subtype) {
map { $$r{$_} = $$subtype{$_} } keys %$subtype;
}
return $r;
}
if ($value eq '{') {
my $list = {};
while ($value ne '}') {
(($token, $value) = $self->get_token()) || return;
my $res;
if ($token == $CHOICE) {
$self->unget_token();
$res = $self->parse_type();
}
else {
$res = $self->parse_type();
}
my $ref = ref $res;
if (defined $ref && $ref eq 'HASH') {
$$list{$value} = $res;
}
else {
return $self->assert(MIBERROR, $self->{'filename'},
$self->{'lineno'}, "FATAL ERROR (please report)");
$$list{$value} = { 'type' => $res };
}
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"must be a '}' or a ',' instead of '$value'")
unless $value eq '}' || $value eq ',';
}
return { 'type' => 'SEQUENCE',
'items' => $list };
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"fatal error");
}
}
elsif ($value eq '[') { # tagged types
my $list = [];
while ($value ne ']') { # read the tag
(($token, $value) = $self->get_token()) || return;
push @$list, $value unless $value eq ']';
}
my $type = $self->parse_type();
$$type{'tag'} = $list;
return $type;
}
elsif ($value eq 'TEXTUAL-CONVENTION') { # textual convention
return $self->parse_textualconvention();
}
elsif ($token == $IDENTIFIER || $token == $TYPEMODREFERENCE) {
my $type = $value;
my $subtype = $self->parse_subtype();
$$subtype{'type'} = $type;
return $subtype;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'");
}
}
sub parse_textualconvention {
my $self = shift;
my $data;
(my ($token, $value) = $self->get_token()) || return;
if ($value eq 'DISPLAY-HINT') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'display-hint'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|deprecated|obsolete)$/o) {
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for TEXTUAL-CONVENTION");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error in TC: 'STATUS' requiered");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error in TC: 'DESCRIPTION' requiered");
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'SYNTAX') {
my $type;
(($token, $value) = $self->get_token()) || return;
if ($value eq 'BITS') {
$$type{'type'} = $value;
$self->get_token('{') || return;
while ($value ne '}') {
(($token, $value) = $self->get_token()) || return;
my $identifier = $value;
$self->get_token('(') || return;
(($token, $value) = $self->get_token('NUMBER')) || return;
$$type{'values'}{$value} = $identifier;
$self->get_token(')') || return;
# should be ',' or ')'
(($token, $value) = $self->get_token()) || return;
}
}
else {
$self->unget_token();
$type = $self->parse_type;
}
$$data{'syntax'} = $type;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error in TC: 'SYNTAX' requiered");
}
$data;
}
sub parse_objectidentity {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|deprecated|obsolete)$/o) {
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for OBJECT-IDENTITY");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. '::=' needed") unless $token == $ASSIGNMENT;
$$data{'oid'} = $self->parse_oid();
$data;
}
# parse MODULE-IDENTITY macro (see RFC 1902)
sub parse_moduleidentity {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
if ($value eq 'LAST-UPDATED') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'last-updated'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'LAST-UPDATED' needed");
}
if ($value eq 'ORGANIZATION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'organization'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'ORGANIZATION' needed");
}
if ($value eq 'CONTACT-INFO') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'contact-info'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'CONTACT-INFO' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
while ($value eq 'REVISION') {
$$data{'revision'} = [] unless defined $$data{'revision'};
(($token, $value) = $self->get_token('CSTRING')) || return;
my $val = $value;
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error: found '$value', need 'DESCRIPTION'")
unless $value eq 'DESCRIPTION';
(($token, $value) = $self->get_token('CSTRING')) || return;
push @{$$data{'revision'}}, { 'revision' => $val,
'description' => $value };
(($token, $value) = $self->get_token()) || return;
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. '::=' needed") unless $token == $ASSIGNMENT;
$$data{'oid'} = $self->parse_oid();
$data;
}
# parse NOTIFICATION-TYPE macro (see RFC 1902)
sub parse_notificationtype {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
if ($value eq 'OBJECTS') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'objects'} = $list;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|deprecated|obsolete)$/o) {
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for NOTIFICATION-TYPE");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. '::=' needed") unless $token == $ASSIGNMENT;
$$data{'oid'} = $self->parse_oid();
$data;
}
# parse MODULE-COMPLIANCE macro (see RFC 1904)
sub parse_modulecompliance {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
my $name = 'this';
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|deprecated|obsolete)$/o) {
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for MODULE-COMPLIANCE");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
while ($value eq 'MODULE') {
$name = 'this';
(($token, $value) = $self->get_token()) || return;
while ($value ne 'MODULE' && $token != $ASSIGNMENT) {
if ($value eq 'MANDATORY-GROUPS') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'module'}{$name}{'mandatory-groups'} = $list;
}
elsif ($value eq 'GROUP') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
my $val = $value;
(($token, $value) = $self->get_token()) || return;
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error: found '$value', need 'DESCRIPTION'")
unless $value eq 'DESCRIPTION';
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'module'}{$name}{'group'}{$val} = $value;
}
elsif ($value eq 'OBJECT') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
my $val = $value;
(($token, $value) = $self->get_token()) || return;
if ($value eq 'SYNTAX') {
my $type = $self->parse_type();
$$data{'module'}{$name}{'object'}{$val}{'syntax'} = $type;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'WRITE-SYNTAX') {
my $type = $self->parse_type();
$$data{'module'}{$name}{'object'}{$val}{'write-syntax'} = $type;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'MIN-ACCESS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(read-(only|write|create)|not-accessible|
accessible-for-notify)$/ox) {
$$data{'module'}{$name}{'object'}{$val}{'min-access'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'},
$self->{'lineno'}, "Unknown MIN-ACCESS type ($value)");
}
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'module'}{$name}{'object'}{$val}{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
$self->unget_token();
}
elsif ($token == $TYPEMODREFERENCE) {
# Modulename
$name = $value;
(($token, $value) = $self->get_token()) || return;
if ($token == $IDENTIFIER) { # ModuleIdentifier
$$data{'module'}{$name}{'identifier'} = $value;
}
else {
$self->unget_token();
}
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at '$value'");
}
(($token, $value) = $self->get_token()) || return;
}
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. '::=' needed") unless $token == $ASSIGNMENT;
$$data{'oid'} = $self->parse_oid();
$data;
}
# parse OBJECT-GROUP macro (see RFC 1904)
sub parse_objectgroup {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value)= $self->get_token()) || return;
if ($value eq 'OBJECTS') {
my $list = [];
(($token, $value) = $self->get_token('{'));
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'objects'} = $list;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'OBJECTS' needed");
}
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|deprecated|obsolete)$/o) {
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for OBJECT-GROUP");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. '::=' needed") unless $token == $ASSIGNMENT;
$$data{'oid'} = $self->parse_oid();
$data;
}
# parse NOTIFICATION-GROUP macro (see RFC 1904)
sub parse_notificationgroup {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
if ($value eq 'NOTIFICATIONS') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'NOTIFICATIONS'} = $list;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'NOTIFICATIONS' needed");
}
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|deprecated|obsolete)$/o) {
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for NOTIFICATION-GROUP");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. '::=' needed") unless $token == $ASSIGNMENT;
$$data{'oid'} = $self->parse_oid();
$data;
}
sub parse_agentcapabilities {
my $self = shift;
my $data;
my $name = 'this';
my ($token, $value);
(($token, $value) = $self->get_token());
return unless $token;
# "PRODUCT-RELEASE" Text
if ($value eq 'PRODUCT-RELEASE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'product-release'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'PRODUCT-RELEASE' needed");
}
# "STATUS" Status
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(current|obsolete)$/o) {
$$data{'status'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"unknown status ($value) for AGENT-CAPABILITIES");
}
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
# "DESCRIPTION" Text
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'DESCRIPTION' needed");
}
# ReferPart
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
# ModulePart
while (defined $token && $token != $ASSIGNMENT) {
while (defined $token && $value ne 'SUPPORTS' && $token != $ASSIGNMENT) {
if ($value eq 'INCLUDES') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'supports'}{$name}{'includes'} = $list;
(($token, $value) = $self->get_token()) || return
}
while ($value eq 'VARIATION') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
my $val = $value; # ObjectName or NotificationName
(($token, $value) = $self->get_token()) || return;
if ($value eq 'SYNTAX') {
my $type = $self->parse_type();
$$data{'supports'}{$name}{'variation'}{$val}{'syntax'} = $type;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'WRITE-SYNTAX') {
my $type = $self->parse_type();
$$data{'supports'}{$name}{'variation'}{$val}{'write-syntax'} = $type;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'ACCESS') {
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(not-implemented|accessible-for-notify|
read-(only|write|create)|write-only)$/ox) {
$$data{'supports'}{$name}{'variation'}{$val}{'access'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'},
$self->{'lineno'}, "Unknown ACCESS type ($value)");
}
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'CREATION-REQUIRES') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'supports'}{$name}{'variation'}{$val}{'creation-requires'} =
$list;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'DEFVAL') {
(($token, $value) = $self->get_token('{')) || return;
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
$$data{'supports'}{$name}{'variation'}{$val}{'defval'} = $value;
(($token, $value) = $self->get_token('}')) || return;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'supports'}{$name}{'variation'}{$val}{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'},
$self->{'lineno'}, "Syntax error. 'DESCRIPTION' needed");
}
}
}
if ($value eq 'SUPPORTS') {
# Modulename
(($token, $value) = $self->get_token()) || return;
$name = $value;
(($token, $value) = $self->get_token()) || return;
if ($token == $IDENTIFIER) { # ModuleIdentifier
$$data{'module'}{$name}{'identifier'} = $value;
}
else {
$self->unget_token();
}
}
(($token, $value) = $self->get_token()) || return
unless $token == $ASSIGNMENT;
}
$$data{'oid'} = $self->parse_oid();
$data;
}
# Parse TRAP-TYPE macro (see RFC 1215)
sub parse_traptype {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
if ($value eq 'ENTERPRISE') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
$$data{'enterprise'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'ENTERPRISE' needed");
}
if ($value eq 'VARIABLES') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
push @$list, $value;
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'variables'} = $list;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($token == $ASSIGNMENT) {
(my $value = $self->get_token('NUMBER')) || return;
$$data{'value'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Should be '::='instead of '$value'");
}
$data;
}
# parse OBJECT-TYPE macro (see RFC 1902)
sub parse_objecttype {
my $self = shift;
my $data;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
if ($value eq 'SYNTAX') {
my $syntax = {};
my $type;
(($token, $value) = $self->get_token()) || return;
if ($self->{'accept_smiv2'} && $value eq 'BITS') {
$$type{'type'} = $value;
$self->get_token('{') || return;
while ($value ne '}') {
(($token, $value) = $self->get_token()) || return;
my $identifier = $value;
$self->get_token('(') || return;
(($token, $value) = $self->get_token('NUMBER')) || return;
$$type{'values'}{$value} = $identifier;
$self->get_token(')') || return;
# should be ',' or ')'
(($token, $value) = $self->get_token()) || return;
}
}
else {
$self->unget_token();
$type = $self->parse_type;
}
my $subtype = $self->parse_subtype();
my $ref = ref $type;
if (defined $ref && $ref eq 'HASH') {
for my $key (keys %$type) {
$$syntax{$key} = $$type{$key};
}
}
else { # should not happen
$$syntax{'type'} = $type;
}
if ($subtype) {
for my $key (keys %$subtype) {
$$syntax{$key} = $$subtype{$key};
}
}
$$data{'syntax'} = $syntax;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"'SYNTAX' needed");
}
if ($self->{'accept_smiv2'} && $value eq 'UNITS') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'units'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'ACCESS' || $value eq 'MAX-ACCESS') {
if ($value eq 'MAX-ACCESS') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at $value") unless $self->{'accept_smiv2'};
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(read-(only|write)|not-accessible|
accessible-for-notify|read-create)$/ox) {
# Valid SMIv2 acces type (rfc 1902, draft-ops-smiv2-smi-01)
$$data{'access'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Unknown acces type ($value)");
}
}
else { # 'ACCESS'
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error at $value") unless $self->{'accept_smiv1'};
(($token, $value) = $self->get_token()) || return;
if ($value =~ m/^(read-(only|write)|write-only|not-accessible)$/o) {
# Valid SMIv1 acces type (rfc 1155, rfc 1212)
$$data{'access'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Unknown acces type ($value)");
}
}
(($token, $value) = $self->get_token()) || return;
}
else {
if ($self->{'accept_smiv1'} && !$self->{'accept_smiv2'}) {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'ACCESS' needed");
}
elsif (!$self->{'accept_smiv1'} && $self->{'accept_smiv2'}) {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'MAX-ACCESS' needed");
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'ACCESS' or 'MAX-ACCESS' needed");
}
}
if ($value eq 'STATUS') {
(($token, $value) = $self->get_token()) || return;
if ($self->{'accept_smiv1'} &&
$value =~ m/^(mandatory|optional|obsolete|deprecated)$/o) {
# Valid SMIv1 status (rfc 1155)
# add 'deprecated' (rfc 1158, rfc 1212)
$$data{'status'} = $value;
}
elsif ($self->{'accept_smiv2'} &&
$value =~ m/^(current|obsolete|deprecated)$/o) {
# Valid SMIv2 status (rfc 1902, draft-ops-smiv2-smi-01)
$$data{'status'} = $value;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Unknown status ($value)");
}
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed");
}
if ($value eq 'DESCRIPTION') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'description'} = $value;
(($token, $value) = $self->get_token()) || return;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error. 'STATUS' needed") unless $self->{'accept_smiv1'};
}
if ($value eq 'REFERENCE') {
(($token, $value) = $self->get_token('CSTRING')) || return;
$$data{'reference'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'INDEX') {
my $list = [];
(($token, $value) = $self->get_token('{')) || return;
while ($value ne '}') {
(($token, $value) = $self->get_token()) || return;
my $implied = 0;
if ($value eq 'IMPLIED') {
$implied++;
(($token, $value) = $self->get_token('IDENTIFIER'));
}
push @$list, { 'value' => $value, 'implied' => $implied };
# shoud be a ',' or a '}'
(($token, $value) = $self->get_token()) || return;
}
$$data{'index'} = $list;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'AUGMENTS' && $self->{'accept_smiv2'}) {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Can't define both 'INDEX' and 'AUGMENTS'") if defined $$data{'index'};
$self->get_token('{') || return;
(($token, $value) = $self->get_token('IDENTIFIER')) || return;
$$data{'augments'} = $value;
$self->get_token('}') || return;
(($token, $value) = $self->get_token()) || return;
}
if ($value eq 'DEFVAL') {
# SMIv1: rfc 1212
# SMIv2: rfc 1902
$self->get_token('{') || return;
(($token, $value) = $self->get_token()) || return;
if ($value eq '-') {
(($token, $value) = $self->get_token()) || return;
$value = "-" . $value;
}
$self->get_token('}') || return;
$$data{'defval'} = $value;
(($token, $value) = $self->get_token()) || return;
}
if ($token == $ASSIGNMENT) {
my $oid = $self->parse_oid();
$$data{'oid'} = $oid;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"should be ::=");
}
$data;
}
# parse an OBJECT IDENTIFIER clause
# note: everything except the value has already been parsed.
sub parse_oid {
my $self = shift;
#
# internet OBJECT IDENTIFIER ::= { iso org(3) dod(6) 1 }
# mgmt OBJECT IDENTIFIER ::= { internet 2 }
# ^^^^^^^^^^^^^^^^^^^^^^^
#
my ($list, $old, $old2);
$self->get_token('{') || return;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
while (defined $token && $value ne '}') {
if ($token == $IDENTIFIER ||
$token == $NUMBER) {
push @$list, $value;
$old2 = $old;
$old = $value;
}
elsif ($value eq '(') {
if ($old2 && $old) {
(($token, $value) = $self->get_token('NUMBER')) || return;
$self->get_token(')') || return;
# Add this to the tree
$self->{'nodes'}{$old}{'oid'} = [ $old2, $value ];
}
else {
# These syntaxes are incorrect:
# { iso(1) ...}
# { (1) ... }
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error");
}
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"Syntax error");
}
(($token, $value) = $self->get_token()) || return;
}
$list;
}
# parse an IMPORTS clause.
# note: the 'IMPORTS' keyword has already been parsed.
sub parse_imports {
my $self = shift;
#
# IMPORT a, b, c FROM mib-foo
# d, e, f, g FROM mib-bar;
#
my ($list, $data);
my $elem = 0;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
while (defined $token && $value ne ';') {
if ($token == $IDENTIFIER ||
$token == $TYPEMODREFERENCE) {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"two values must be separated by a comma") if $elem;
push @$list, $value;
$elem = 1;
}
elsif ($value eq ',') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"value expected. ',' found") unless $elem;
$elem = 0;
}
elsif ($token == $FROM) {
$elem = 0;
my $oldvalue = $value;
(($token, $value) = $self->get_token()) || return;
if ($token == $IDENTIFIER ||
$token == $TYPEMODREFERENCE) {
my @l;
@l = @{$$data{$value}} if defined $$data{$value};
push @l, @$list;
$$data{$value} = \@l;
undef $list;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"identifier expected after '$oldvalue'");
}
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"syntax error while parsing IMPORTS clause");
}
(($token, $value) = $self->get_token()) || return;
}
$data;
}
# parse an EXPORTS clause.
# note: the 'EXPORTS' keyword has already been parsed.
sub parse_exports {
my $self = shift;
#
# EXPORTS a, b, c;
#
my $list;
my $elem = 0;
my ($token, $value);
(($token, $value) = $self->get_token()) || return;
while (defined $token && $value ne ';') {
if ($token == $IDENTIFIER ||
$token == $TYPEMODREFERENCE) {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"two values must be separated by a comma") if $elem;
push @$list, $value;
$elem = 1;
}
elsif ($value eq ',') {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"value expected. ',' found") unless $elem;
$elem = 0;
}
else {
return $self->assert(MIBERROR, $self->{'filename'}, $self->{'lineno'},
"syntax error while parsing EXPORTS clause");
}
(($token, $value) = $self->get_token()) || return;
}
$list;
}
sub import_modules {
my $self = shift;
for my $k (keys %{$self->{'imports'}}) {
warn "DEBUG: importing $k...\n" if $self->{'debug_lexer'};
my $mib = new SNMP::MIB::Compiler();
$mib->repository($self->repository);
$mib->extensions($self->extensions);
$mib->{'srcpath'} = $self->{'srcpath'};
$mib->{'make_dump'} = $self->{'make_dump'};
$mib->{'use_dump'} = $self->{'use_dump'};
$mib->{'do_imports'} = $self->{'do_imports'};
$mib->{'allow_underscore'} = $self->{'allow_underscore'};
$mib->{'allow_lowcase_hstrings'} = $self->{'allow_lowcase_hstrings'};
$mib->{'allow_lowcase_bstrings'} = $self->{'allow_lowcase_bstrings'};
if ($self->{'debug_recursive'}) {
$mib->{'debug_recursive'} = $self->{'debug_recursive'};
$mib->{'debug_lexer'} = $self->{'debug_lexer'};
}
$mib->load($k) || $mib->compile($k);
for my $item (@{$self->{'imports'}{$k}}) {
warn "DEBUG: importing symbol $item from $k for $self->{'name'}...\n"
if $self->{'debug_lexer'};
if (defined $mib->{'nodes'}{$item}) {
# resolve OID to break the dependencies
my @a = split /\./, $mib->convert_oid($mib->resolve_oid($item));
my @l = @{$mib->{'nodes'}{$item}{'OID'}};
$a[$#a] = $l[$#l];
@{$mib->{'nodes'}{$item}{'oid'}} = @a;
shift @l;
my $last = pop @l;
for my $elem (@l) {
last unless $elem =~ m/^\d+$/o;
my $o = shift @a;
$self->{'tree'}{$o}{$elem} = $a[0];
}
$self->{'tree'}{$a[0]}{$last} = $item if scalar @a == 1;
$self->{'nodes'}{$item} = $mib->{'nodes'}{$item};
}
elsif (defined $mib->{'types'}{$item}) {
$self->{'types'}{$item} = $mib->{'types'}{$item};
}
else {
my $found = 0;
for my $macro (@{$mib->{'macros'}}) {
$found++, push (@{$self->{'macros'}}, $item) if $macro eq $item;
}
$self->assert(MIBWARN, $self->{'filename'}, $self->{'lineno'},
"can't find '$item' in $k") unless $found;
}
}
warn "DEBUG: $k imported.\n" if $self->{'debug_lexer'};
}
}
# Where the MIBs are stored
sub repository {
my $self = shift;
my $dir = shift;
$self->{'repository'} = $dir if defined $dir;
return $self->{'repository'};
}
# Add some paths to the list of possible MIB locations
sub add_path {
my $self = shift;
croak "Usage: Compiler::addpath(path1[,path2[,path3]])" if $#_ == -1;
while (defined (my $path = shift)) {
push @{$self->{'srcpath'}}, $path;
}
@{$self->{'srcpath'}};
}
# List of possible MIB filename extensions
sub extensions {
my $self = shift;
my $ext = shift;
$self->{'extensions'} = $ext if defined $ext;
return $self->{'extensions'};
}
# Add some possible MIB filename extensions
sub add_extension {
my $self = shift;
croak "Usage: Compiler::extension(ext1[,ext2[,ext3]])" if $#_ == -1;
while (defined (my $ext = shift)) {
push @{$self->{'extensions'}}, $ext;
}
}
my $treemodes = {'read-only' => '-r-',
'read-write' => '-rw',
'read-create' => 'cr-',
'write-only' => '--w',
'not-accessible' => '---',
'accessible-for-notify' => 'n--',
'not-implemented' => 'i--',
};
my $treetypes = {'SEQUENCE' => '',
'CHOICE' => '',
'INTEGER' => 'Integer',
'OCTET STRING' => 'String',
'OBJECT IDENTIFIER' => 'ObjectID',
'NULL' => 'Null',
'IpAddress' => 'IPAddr',
'Counter' => 'Counter',
'Gauge' => 'Gauge',
'TimeTicks' => 'TimeTcks',
'Opaque' => 'Opaque',
'Integer32' => 'Int32',
'Counter32' => 'Count32',
'Gauge32' => 'Gauge32',
'Unsigned32' => 'UInt32',
'Counter64' => 'Count64',
};
# return an ASCII driagram showing the tree under the given node
sub tree {
my $self = shift;
my $node = shift;
my $level = shift;
my $inc = shift;
my $s = shift;
$level = 0 unless defined $level;
$inc = 4 unless defined $inc;
return $level ? $s : "$node\n" unless defined $self->{'tree'}{$node};
unless ($level) {
$s .= $node . "\n";
$s .= " |\n";
}
for my $n (sort { $a <=> $b } keys %{$self->{'tree'}{$node}}) {
my $new = $self->{'tree'}{$node}{$n};
$s .= " ";
$s .= " " x ($inc * $level) . "+-- ";
my $access = "";
$access = $$treemodes{$self->{'nodes'}{$new}{'access'}} || "???"
if defined $self->{'nodes'}{$new}{'access'};
$access .= " " if $access;
my $type = "";
$type = $self->{'nodes'}{$new}{'syntax'}{'type'} if
defined $self->{'nodes'}{$new}{'syntax'} &&
defined $self->{'nodes'}{$new}{'syntax'}{'type'};
if ($type) {
$type = $self->resolve_type($type);
$type = sprintf "%-8.8s ", defined $$treetypes{$type} ?
$$treetypes{$type} : $type;
$type = "" if $type =~ m/^\s+$/o;
}
$s .= $access . $type . $new . '(' . $n . ")\n";
if (defined $self->{'tree'}{$new}) {
$s .= " ";
$s .= " " x ($inc * ($level + 1)) . "|\n";
$s = $self->tree($new, $level + 1, $inc, $s);
}
}
$s;
}
###########################################################################
package Stream;
use strict;
use vars qw($VERSION);
$VERSION = 1.00;
sub new {
my $this = shift;
my $fh = shift;
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
$self->{'fh'} = $fh;
$self->{'lineno'} = 1;
$self->{'saved'} = 0;
$self;
}
sub getc {
my $self = shift;
my $char;
if ($self->{'saved'}) {
$char = $self->{'save'};
$self->{'saved'} = 0;
$self->{'lineno'}++ if $char eq "\n";
}
elsif (defined ($char = getc $self->{'fh'})) {
$self->{'save'} = $char;
$self->{'lineno'}++ if $char eq "\n";
}
else {
$char = '';
}
$char;
}
sub ungetc {
my $self = shift;
$self->{'saved'} = 1;
$self->{'lineno'}-- if $self->{'save'} eq "\n";
}
sub lineno {
my $self = shift;
$self->{'lineno'};
}
1;
=head1 NAME
SNMP::MIB::Compiler - a MIB Compiler supporting SMIv1 and SMIv2
=head1 SYNOPSIS
use SNMP::MIB::Compiler;
my $mib = new SNMP::MIB::Compiler;
# search MIBs there...
$mib->add_path('./mibs', '/foo/bar/mibs');
# possibly using these extensions...
$mib->add_extension('', '.mib', '.my');
# store the compiled MIBs there..
$mib->repository('./out');
# only accept SMIv2 MIBs
$mib->{'accept_smiv1'} = 0;
$mib->{'accept_smiv2'} = 1;
# no debug
$mib->{'debug_lexer'} = 0;
$mib->{'debug_recursive'} = 0;
# store compiled MIBs into files
$mib->{'make_dump'} = 1;
# read compiled MIBs
$mib->{'use_dump'} = 1;
# follow IMPORTS clause while compiling
$mib->{'do_imports'} = 1;
# load a precompiled MIB
$mib->load('SNMPv2-MIB');
# compile a new MIB
$mib->compile('IF-MIB');
print $mib->resolve_oid('ifInOctets'), "\n";
print $mib->convert_oid('1.3.6.1.2.1.31.1.1.1.10'), "\n";
print $mib->tree('ifMIB');
=head1 DESCRIPTION
SNMP::MIB::Compiler is a MIB compiler that fully supports
both SMI(v1) and SMIv2. This module can be use to compile
MIBs (recursively or not) or load already compiled MIBs for
later use.
Some tasks can be performed by the resulting object such as :
- resolution of object names into object identifiers (OIDs).
e.g. ifInOctets => 1.3.6.1.2.1.2.2.1.10
- convertion of OIDs.
e.g. 1.3.6.1.2.1.2.1 =>
iso.org.dod.internet.mgmt.mib-2.interfaces.ifNumber
- drawing MIB trees.
e.g. ifTestTable => ifTestTable
|
+-- --- ifTestEntry(1)
|
+-- -rw Integer ifTestId(1)
+-- -rw Integer ifTestStatus(2)
+-- -rw ObjectID ifTestType(3)
+-- -r- Integer ifTestResult(4)
+-- -r- ObjectID ifTestCode(5)
+-- -rw String ifTestOwner(6)
The MIB to be compiled requires no modification. Everything legal
according to SMIs is accepted, including MACRO definitions (which
are parsed but ignored).
This module is shipped with the basic MIBs usually needed by IMPORTS
clauses. A lot of IETF MIBs has been successfully tested as well as
some private ones.
=head1 Methods
=over 5
=item C<new>
C<SNMP::MIB::Compiler::new()> I<class method>
To create a new MIB, send a new() message to the SNMP::MIB::Compiler
class. For example:
my $mib = new SNMP::MIB::Compiler;
This will create an empty MIB ready to accept both SMIv1 and SMIv2
MIBs. A lot of attributes can be (des)activated to obtain a more
or less strict and verbose compiler.
The created object is returned.
=item C<add_path>
C<SNMP::MIB::Compiler::add_path(p1[,p2[,p3]])> I<object method>
Add one or more directories to the search path. This path is used to
locate a MIB file when the 'compile' method is invoqued.
The current list of paths is returned.
Example:
# search MIBs in the "mibs" directory (relative
# to cwd) and in "/foo/bar/mibs" (absolute path)
$mib->add_path('./mibs', '/foo/bar/mibs');
=item C<add_extension>
C<SNMP::MIB::Compiler::add_extension(e1[,e2[,e3]])> I<object method>
Add one or more extensions to the extension list. These extensions are
used to locate a MIB file when the 'compile' method is invoqued. All
extensions are tested for each directory specified by the add_path()
method until one match.
The current list of extensions is returned.
Example:
$mib->add_path('./mibs', '/foo/bar/mibs');
$mib->add_extension('', '.mib');
$mib->compile('FOO');
The order is "./mibs/FOO", "./mibs/FOO.mib", "/foo/bar/mibs/FOO"
and "/foo/bar/mibs/FOO.mib".
=item C<repository>
C<SNMP::MIB::Compiler::repository([dir])> I<object method>
If 'dir' is defined, set the directory where compiled MIBs will be
stored (using the compile() method) or loaded (using the load() method).
The repository MUST be initialized before a MIB can be compiled or loaded.
The current repository is returned.
Example:
$mib->repository('./out');
print "Current repository is ", $mib->repository, "\n";
=item C<compile>
C<SNMP::MIB::Compiler::compile(mib)> I<object method>
Compile a MIB given its name. All information contained in
this MIB is inserted into the current object and is stored
into a file in the repository (see the 'make_dump' attribute).
The choosen name is the same as the real MIB name (defined
in the MIB itself). If a precompiled MIB already exists in
the repository and is newer than the given file, it is used
instead of a real compilation (see the 'use_dump' attribute).
The compiler can be recursive if IMPORTS clauses are followed
(see the 'do_imports' attribute) and in that case, uncompiled
MIB names must be explict according to paths and extensions
critaeria (see add_path() and add_extensions() methods).
The current object is returned.
=item C<load>
C<SNMP::MIB::Compiler::load(mib)> I<object method>
Load a precompiled MIB given its name. All information contained in
this MIB is inserted into the current object. The file is searched in the
repository which MUST be initialized. In case of success, returns 1
else returns 0.
Example:
$mib->load('SNMPv2-SMI');
$mib->load('SNMPv2-MIB');
=item C<resolve_oid>
C<SNMP::MIB::Compiler::resolve_oid(node)> I<object method>
Example:
print $mib->resolve_oid('ifInOctets'), "\n";
=item C<convert_oid>
C<SNMP::MIB::Compiler::convert_oid(oid)> I<object method>
Example:
print $mib->convert_oid('1.3.6.1.2.1.31.1.1.1.10'), "\n";
=item C<tree>
C<SNMP::MIB::Compiler::tree(node)> I<object method>
Example:
print $mib->tree('ifMIB');
=back
=head1 Attributes
=over 5
=item C<do_imports>
=item C<accept_smiv1>
=item C<accept_smiv2>
=item C<allow_underscore>
=item C<allow_lowcase_hstrings>
=item C<allow_lowcase_bstrings>
=item C<make_dump>
=item C<dumpext>
=item C<use_dump>
=item C<debug_lexer>
=item C<debug_recursive>
=back
=head1 BUGS
Currently, it is more a TODO list than a bug list.
- not enough documentation
- not enough methods
- not enough test scripts
- find a better name for compiled MIBs than 'dump's.. even if they are
no more than dumps.
If your MIBs can't be compiled by this module, please, double check
their syntax. If you really think that they are correct, send them
to me including their "uncommon" dependencies.
=head1 AUTHOR
Fabien Tassin (fta@oleane.net)
=head1 COPYRIGHT
Copyright 1998, 1999, Fabien Tassin. All rights reserved.
It may be used and modified freely, but I do request that
this copyright notice remain attached to the file. You may
modify this module as you wish, but if you redistribute a
modified version, please attach a note listing the modifications
you have made.
=cut
|