Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
/*-
 * Copyright (c) 2005-2006 The FreeBSD Project
 * All rights reserved.
 *
 * Author: Shteryana Shopova <syrinx@FreeBSD.org>
 *
 * Redistribution of this software and documentation and use in source and
 * binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code or documentation must retain the above
 *    copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Helper functions for snmp client tools
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/uio.h>

#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include <bsnmp/asn1.h>
#include <bsnmp/snmp.h>
#include <bsnmp/snmpclient.h>
#include "bsnmptc.h"
#include "bsnmptools.h"

/* Internal varibale to turn on library debugging for testing and to
 * find bugs. It is not exported via the header file.
 * XXX should we cover it by some #ifdef BSNMPTOOLS_DEBUG? */
int _bsnmptools_debug = 0;

/* Default files to import mapping from if none explicitly provided. */
#define	bsnmpd_defs		"/usr/share/snmp/defs/tree.def"
#define	mibII_defs		"/usr/share/snmp/defs/mibII_tree.def"

/*
 * The .iso.org.dod oid that has to be prepended to every OID when requesting
 * a value.
 */
const struct asn_oid IsoOrgDod_OID = {
	3, { 1, 3, 6 }
};


#define	SNMP_ERR_UNKNOWN	0

/*
 * An array of error strings corresponding to error definitions from libbsnmp.
 */
static const struct {
	const char *str;
	int32_t error;
} error_strings[] = {
	{ "Unknown", SNMP_ERR_UNKNOWN },
	{ "Too big ", SNMP_ERR_TOOBIG },
	{ "No such Name", SNMP_ERR_NOSUCHNAME },
	{ "Bad Value", SNMP_ERR_BADVALUE },
	{ "Readonly", SNMP_ERR_READONLY },
	{ "General error", SNMP_ERR_GENERR },
	{ "No access", SNMP_ERR_NO_ACCESS },
	{ "Wrong type", SNMP_ERR_WRONG_TYPE },
	{ "Wrong length", SNMP_ERR_WRONG_LENGTH },
	{ "Wrong encoding", SNMP_ERR_WRONG_ENCODING },
	{ "Wrong value", SNMP_ERR_WRONG_VALUE },
	{ "No creation", SNMP_ERR_NO_CREATION },
	{ "Inconsistent value", SNMP_ERR_INCONS_VALUE },
	{ "Resource unavailable", SNMP_ERR_RES_UNAVAIL },
	{ "Commit failed", SNMP_ERR_COMMIT_FAILED },
	{ "Undo failed", SNMP_ERR_UNDO_FAILED },
	{ "Authorization error", SNMP_ERR_AUTH_ERR },
	{ "Not writable", SNMP_ERR_NOT_WRITEABLE },
	{ "Inconsistent name", SNMP_ERR_INCONS_NAME },
	{ NULL, 0 }
};

/* This one and any following are exceptions. */
#define	SNMP_SYNTAX_UNKNOWN	SNMP_SYNTAX_NOSUCHOBJECT

static const struct {
	const char *str;
	enum snmp_syntax stx;
} syntax_strings[] = {
	{ "Null", SNMP_SYNTAX_NULL },
	{ "Integer", SNMP_SYNTAX_INTEGER },
	{ "OctetString", SNMP_SYNTAX_OCTETSTRING },
	{ "OID", SNMP_SYNTAX_OID },
	{ "IpAddress", SNMP_SYNTAX_IPADDRESS },
	{ "Counter32", SNMP_SYNTAX_COUNTER },
	{ "Gauge", SNMP_SYNTAX_GAUGE },
	{ "TimeTicks", SNMP_SYNTAX_TIMETICKS },
	{ "Counter64", SNMP_SYNTAX_COUNTER64 },
	{ "Unknown", SNMP_SYNTAX_UNKNOWN },
};

int
snmptool_init(struct snmp_toolinfo *snmptoolctx)
{
	char *str;
	size_t slen;

	memset(snmptoolctx, 0, sizeof(struct snmp_toolinfo));
	snmptoolctx->objects = 0;
	snmptoolctx->mappings = NULL;
	snmptoolctx->flags = SNMP_PDU_GET;	/* XXX */
	SLIST_INIT(&snmptoolctx->filelist);
	snmp_client_init(&snmp_client);
	SET_MAXREP(snmptoolctx, SNMP_MAX_REPETITIONS);

	if (add_filename(snmptoolctx, bsnmpd_defs, &IsoOrgDod_OID, 0) < 0)
		warnx("Error adding file %s to list", bsnmpd_defs);

	if (add_filename(snmptoolctx, mibII_defs, &IsoOrgDod_OID, 0) < 0)
		warnx("Error adding file %s to list", mibII_defs);

	/* Read the environment */
	if ((str = getenv("SNMPAUTH")) != NULL) {
		slen = strlen(str);
		if (slen == strlen("md5") && strcasecmp(str, "md5") == 0)
			snmp_client.user.auth_proto = SNMP_AUTH_HMAC_MD5;
		else if (slen == strlen("sha")&& strcasecmp(str, "sha") == 0)
			snmp_client.user.auth_proto = SNMP_AUTH_HMAC_SHA;
		else if (slen != 0)
			warnx("Bad authentication type - %s in SNMPAUTH", str);
	}

	if ((str = getenv("SNMPPRIV")) != NULL) {
		slen = strlen(str);
		if (slen == strlen("des") && strcasecmp(str, "des") == 0)
			snmp_client.user.priv_proto = SNMP_PRIV_DES;
		else if (slen == strlen("aes")&& strcasecmp(str, "aes") == 0)
			snmp_client.user.priv_proto = SNMP_PRIV_AES;
		else if (slen != 0)
			warnx("Bad privacy type - %s in SNMPPRIV", str);
	}

	if ((str = getenv("SNMPUSER")) != NULL) {
		if ((slen = strlen(str)) > sizeof(snmp_client.user.sec_name)) {
			warnx("Username too long - %s in SNMPUSER", str);
			return (-1);
		}
		if (slen > 0) {
			strlcpy(snmp_client.user.sec_name, str,
			    sizeof(snmp_client.user.sec_name));
			snmp_client.version = SNMP_V3;
		}
	}

	if ((str = getenv("SNMPPASSWD")) != NULL) {
		if ((slen = strlen(str)) > MAXSTR)
			slen = MAXSTR - 1;
		if ((snmptoolctx->passwd = malloc(slen + 1)) == NULL) {
			warn("malloc() failed");
			return (-1);
		}
		if (slen > 0)
			strlcpy(snmptoolctx->passwd, str, slen + 1);
	}

	return (0);
}

#define	OBJECT_IDX_LIST(o)	o->info->table_idx->index_list

/*
 * Walk through the file list and import string<->oid mappings from each file.
 */
int32_t
snmp_import_all(struct snmp_toolinfo *snmptoolctx)
{
	int32_t fc;
	struct fname *tmp;

	if (snmptoolctx == NULL)
		return (-1);

	if (ISSET_NUMERIC(snmptoolctx))
		return (0);

	if ((snmptoolctx->mappings = snmp_mapping_init()) == NULL)
		return (-1);

	fc = 0;
	if (SLIST_EMPTY(&snmptoolctx->filelist)) {
		warnx("No files to read OID <-> string conversions from");
		return (-1);
	} else {
		SLIST_FOREACH(tmp, &snmptoolctx->filelist, link) {
			if (tmp->done)
				continue;
			if (snmp_import_file(snmptoolctx, tmp) < 0) {
				fc = -1;
				break;
			}
			fc++;
		}
	}

	snmp_mapping_dump(snmptoolctx);
	return (fc);
}

/*
 * Add a filename to the file list - the initial idea of keeping a list with all
 * files to read OIDs from was that an application might want to have loaded in
 * memory the OIDs from a single file only and when done with them read the OIDs
 * from another file. This is not used yet but might be a good idea at some
 * point. Size argument is number of bytes in string including trailing '\0',
 * not string length.
 */
int32_t
add_filename(struct snmp_toolinfo *snmptoolctx, const char *filename,
    const struct asn_oid *cut, int32_t done)
{
	char *fstring;
	struct fname *entry;

	if (snmptoolctx == NULL)
		return (-1);

	/* Make sure file was not in list. */
	SLIST_FOREACH(entry, &snmptoolctx->filelist, link) {
		if (strncmp(entry->name, filename, strlen(entry->name)) == 0)
			return (0);
	}

	if ((fstring = strdup(filename)) == NULL) {
		warn("strdup() failed");
		return (-1);
	}

	if ((entry = calloc(1, sizeof(struct fname))) == NULL) {
		warn("calloc() failed");
		free(fstring);
		return (-1);
	}

	if (cut != NULL)
		asn_append_oid(&(entry->cut), cut);
	entry->name = fstring;
	entry->done = done;
	SLIST_INSERT_HEAD(&snmptoolctx->filelist, entry, link);

	return (1);
}

void
free_filelist(struct snmp_toolinfo *snmptoolctx)
{
	struct fname *f;

	if (snmptoolctx == NULL)
		return; /* XXX error handling */

	while ((f = SLIST_FIRST(&snmptoolctx->filelist)) != NULL) {
		SLIST_REMOVE_HEAD(&snmptoolctx->filelist, link);
		if (f->name)
			free(f->name);
		free(f);
	}
}

static char
isvalid_fchar(char c, int pos)
{
	if (isalpha(c)|| c == '/'|| c == '_' || c == '.' || c == '~' ||
	    (pos != 0 && isdigit(c))){
		return (c);
	}

	if (c == '\0')
		return (0);

	if (!isascii(c) || !isprint(c))
		warnx("Unexpected character %#2x", (u_int) c);
	else
		warnx("Illegal character '%c'", c);

	return (-1);
}

/*
 * Re-implement getsubopt from scratch, because the second argument is broken
 * and will not compile with WARNS=5.
 * Copied from src/contrib/bsnmp/snmpd/main.c.
 */
static int
getsubopt1(char **arg, const char *const *options, char **valp, char **optp)
{
	static const char *const delim = ",\t ";
	u_int i;
	char *ptr;

	*optp = NULL;

	/* Skip leading junk. */
	for (ptr = *arg; *ptr != '\0'; ptr++)
		if (strchr(delim, *ptr) == NULL)
			break;
	if (*ptr == '\0') {
		*arg = ptr;
		return (-1);
	}
	*optp = ptr;

	/* Find the end of the option. */
	while (*++ptr != '\0')
		if (strchr(delim, *ptr) != NULL || *ptr == '=')
			break;

	if (*ptr != '\0') {
		if (*ptr == '=') {
			*ptr++ = '\0';
			*valp = ptr;
			while (*ptr != '\0' && strchr(delim, *ptr) == NULL)
				ptr++;
			if (*ptr != '\0')
				*ptr++ = '\0';
		} else
			*ptr++ = '\0';
	}

	*arg = ptr;

	for (i = 0; *options != NULL; options++, i++)
		if (strcmp(*optp, *options) == 0)
			return (i);
	return (-1);
}

static int32_t
parse_path(char *value)
{
	int32_t i, len;

	if (value == NULL)
		return (-1);

	for (len = 0; len < MAXPATHLEN; len++) {
		i = isvalid_fchar(*(value + len), len) ;

		if (i == 0)
			break;
		else if (i < 0)
			return (-1);
	}

	if (len >= MAXPATHLEN || value[len] != '\0') {
		warnx("Bad pathname - '%s'", value);
		return (-1);
	}

	return (len);
}

static int32_t
parse_flist(struct snmp_toolinfo *snmptoolctx, char *value, char *path,
    const struct asn_oid *cut)
{
	int32_t namelen;
	char filename[MAXPATHLEN + 1];

	if (value == NULL)
		return (-1);

	do {
		memset(filename, 0, MAXPATHLEN + 1);

		if (isalpha(*value) && (path == NULL || path[0] == '\0')) {
			strlcpy(filename, SNMP_DEFS_DIR, MAXPATHLEN + 1);
			namelen = strlen(SNMP_DEFS_DIR);
		} else if (path != NULL){
			strlcpy(filename, path, MAXPATHLEN + 1);
			namelen = strlen(path);
		} else
			namelen = 0;

		for ( ; namelen < MAXPATHLEN; value++) {
			if (isvalid_fchar(*value, namelen) > 0) {
				filename[namelen++] = *value;
				continue;
			}

			if (*value == ',' )
				value++;
			else if (*value == '\0')
				;
			else {
				if (!isascii(*value) || !isprint(*value))
					warnx("Unexpected character %#2x in"
					    " filename", (u_int) *value);
				else
					warnx("Illegal character '%c' in"
					    " filename", *value);
				return (-1);
			}

			filename[namelen]='\0';
			break;
		}

		if ((namelen == MAXPATHLEN) && (filename[MAXPATHLEN] != '\0')) {
			warnx("Filename %s too long", filename);
			return (-1);
		}

		if (add_filename(snmptoolctx, filename, cut, 0) < 0) {
			warnx("Error adding file %s to list", filename);
			return (-1);
		}
	} while (*value != '\0');

	return(1);
}

static int32_t
parse_ascii(char *ascii, uint8_t *binstr, size_t binlen)
{
	char dptr[3];
	size_t count;
	int32_t alen, i, saved_errno;
	uint32_t val;

	/* Filter 0x at the beginning */
	if ((alen = strlen(ascii)) > 2 && ascii[0] == '0' && ascii[1] == 'x')
		i = 2;
	else
		i = 0;

	saved_errno = errno;
	errno = 0;
	for (count = 0; i < alen; i += 2) {
		/* XXX: consider strlen(ascii) % 2 != 0 */
		dptr[0] = ascii[i];
		dptr[1] = ascii[i + 1];
		dptr[2] = '\0';
		if ((val = strtoul(dptr, NULL, 16)) > 0xFF || errno != 0) {
			errno = saved_errno;
			return (-1);
		}
		binstr[count] = (uint8_t) val;
		if (++count >= binlen) {
			warnx("Key %s too long - truncating to %zu octets",
			    ascii, binlen);
			break;
		}
	}

	return (count);
}

/*
 * Functions to parse common input options for client tools and fill in the
 * snmp_client structure.
 */
int32_t
parse_authentication(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
{
	int32_t count, subopt;
	char *val, *option;
	const char *const subopts[] = {
		"proto",
		"key",
		NULL
	};

	assert(opt_arg != NULL);
	count = 1;
	while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
		switch (subopt) {
		case 0:
			if (val == NULL) {
				warnx("Suboption 'proto' requires an argument");
				return (-1);
			}
			if (strlen(val) != 3) {
				warnx("Unknown auth protocol - %s", val);
				return (-1);
			}
			if (strncasecmp("md5", val, strlen("md5")) == 0)
				snmp_client.user.auth_proto =
				    SNMP_AUTH_HMAC_MD5;
			else if (strncasecmp("sha", val, strlen("sha")) == 0)
				snmp_client.user.auth_proto =
				    SNMP_AUTH_HMAC_SHA;
			else {
				warnx("Unknown auth protocol - %s", val);
				return (-1);
			}
			break;
		case 1:
			if (val == NULL) {
				warnx("Suboption 'key' requires an argument");
				return (-1);
			}
			if (parse_ascii(val, snmp_client.user.auth_key,
			    SNMP_AUTH_KEY_SIZ) < 0) {
				warnx("Bad authentication key- %s", val);
				return (-1);
			}
			break;
		default:
			warnx("Unknown suboption - '%s'", suboptarg);
			return (-1);
		}
		count += 1;
	}
	return (2/* count */);
}

int32_t
parse_privacy(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
{
	int32_t count, subopt;
	char *val, *option;
	const char *const subopts[] = {
		"proto",
		"key",
		NULL
	};

	assert(opt_arg != NULL);
	count = 1;
	while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
		switch (subopt) {
		case 0:
			if (val == NULL) {
				warnx("Suboption 'proto' requires an argument");
				return (-1);
			}
			if (strlen(val) != 3) {
				warnx("Unknown privacy protocol - %s", val);
				return (-1);
			}
			if (strncasecmp("aes", val, strlen("aes")) == 0)
				snmp_client.user.priv_proto = SNMP_PRIV_AES;
			else if (strncasecmp("des", val, strlen("des")) == 0)
				snmp_client.user.priv_proto = SNMP_PRIV_DES;
			else {
				warnx("Unknown privacy protocol - %s", val);
				return (-1);
			}
			break;
		case 1:
			if (val == NULL) {
				warnx("Suboption 'key' requires an argument");
				return (-1);
			}
			if (parse_ascii(val, snmp_client.user.priv_key,
			    SNMP_PRIV_KEY_SIZ) < 0) {
				warnx("Bad privacy key- %s", val);
				return (-1);
			}
			break;
		default:
			warnx("Unknown suboption - '%s'", suboptarg);
			return (-1);
		}
		count += 1;
	}
	return (2/* count */);
}

int32_t
parse_context(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
{
	int32_t count, subopt;
	char *val, *option;
	const char *const subopts[] = {
		"context",
		"context-engine",
		NULL
	};

	assert(opt_arg != NULL);
	count = 1;
	while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
		switch (subopt) {
		case 0:
			if (val == NULL) {
				warnx("Suboption 'context' - no argument");
				return (-1);
			}
			strlcpy(snmp_client.cname, val, SNMP_CONTEXT_NAME_SIZ);
			break;
		case 1:
			if (val == NULL) {
				warnx("Suboption 'context-engine' - no argument");
				return (-1);
			}
			if ((int32_t)(snmp_client.clen = parse_ascii(val,
			    snmp_client.cengine, SNMP_ENGINE_ID_SIZ)) == -1) {
				warnx("Bad EngineID - %s", val);
				return (-1);
			}
			break;
		default:
			warnx("Unknown suboption - '%s'", suboptarg);
			return (-1);
		}
		count += 1;
	}
	return (2/* count */);
}

int32_t
parse_user_security(struct snmp_toolinfo *snmptoolctx __unused, char *opt_arg)
{
	int32_t count, subopt, saved_errno;
	char *val, *option;
	const char *const subopts[] = {
		"engine",
		"engine-boots",
		"engine-time",
		"name",
		NULL
	};

	assert(opt_arg != NULL);
	count = 1;
	while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
		switch (subopt) {
		case 0:
			if (val == NULL) {
				warnx("Suboption 'engine' - no argument");
				return (-1);
			}
			snmp_client.engine.engine_len = parse_ascii(val,
			    snmp_client.engine.engine_id, SNMP_ENGINE_ID_SIZ);
			if ((int32_t)snmp_client.engine.engine_len == -1) {
				warnx("Bad EngineID - %s", val);
				return (-1);
			}
			break;
		case 1:
			if (val == NULL) {
				warnx("Suboption 'engine-boots' - no argument");
				return (-1);
			}
			saved_errno = errno;
			errno = 0;
			snmp_client.engine.engine_boots = strtoul(val, NULL, 10);
			if (errno != 0) {
				warn("Bad 'engine-boots' value %s", val);
				errno = saved_errno;
				return (-1);
			}
			errno = saved_errno;
			break;
		case 2:
			if (val == NULL) {
				warnx("Suboption 'engine-time' - no argument");
				return (-1);
			}
			saved_errno = errno;
			errno = 0;
			snmp_client.engine.engine_time = strtoul(val, NULL, 10);
			if (errno != 0) {
				warn("Bad 'engine-time' value %s", val);
				errno = saved_errno;
				return (-1);
			}
			errno = saved_errno;
			break;
		case 3:
			strlcpy(snmp_client.user.sec_name, val,
			    SNMP_ADM_STR32_SIZ);
			break;
		default:
			warnx("Unknown suboption - '%s'", suboptarg);
			return (-1);
		}
		count += 1;
	}
	return (2/* count */);
}

int32_t
parse_file(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
{
	assert(opt_arg != NULL);

	if (parse_flist(snmptoolctx, opt_arg, NULL, &IsoOrgDod_OID) < 0)
		return (-1);

	return (2);
}

int32_t
parse_include(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
{
	char path[MAXPATHLEN + 1];
	int32_t cut_dflt, len, subopt;
	struct asn_oid cut;
	char *val, *option;
	const char *const subopts[] = {
		"cut",
		"path",
		"file",
		NULL
	};

#define	INC_CUT		0
#define	INC_PATH	1
#define	INC_LIST	2

	assert(opt_arg != NULL);

	/* if (opt == 'i')
		free_filelist(snmptoolctx, ); */
	/*
	 * This function should be called only after getopt(3) - otherwise if
	 * no previous validation of opt_arg strlen() may not return what is
	 * expected.
	 */

	path[0] = '\0';
	memset(&cut, 0, sizeof(struct asn_oid));
	cut_dflt = -1;

	while ((subopt = getsubopt1(&opt_arg, subopts, &val, &option)) != EOF) {
		switch (subopt) {
		    case INC_CUT:
			if (val == NULL) {
				warnx("Suboption 'cut' requires an argument");
				return (-1);
			} else {
				if (snmp_parse_numoid(val, &cut) < 0)
					return (-1);
			}
			cut_dflt = 1;
			break;

		    case INC_PATH:
			if ((len = parse_path(val)) < 0)
				return (-1);
			strlcpy(path, val, len + 1);
			break;

		    case INC_LIST:
			if (val == NULL)
				return (-1);
			if (cut_dflt == -1)
				len = parse_flist(snmptoolctx, val, path, &IsoOrgDod_OID);
			else
				len = parse_flist(snmptoolctx, val, path, &cut);
			if (len < 0)
				return (-1);
			break;

		    default:
			warnx("Unknown suboption - '%s'", suboptarg);
			return (-1);
		}
	}

	/* XXX: Fix me - returning two is wrong here */
	return (2);
}

int32_t
parse_server(char *opt_arg)
{
	assert(opt_arg != NULL);

	if (snmp_parse_server(&snmp_client, opt_arg) < 0)
		return (-1);

	if (snmp_client.trans > SNMP_TRANS_UDP && snmp_client.chost == NULL) {
		if ((snmp_client.chost = malloc(strlen(SNMP_DEFAULT_LOCAL) + 1))
		    == NULL) {
			syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
			return (-1);
		}
		strcpy(snmp_client.chost, SNMP_DEFAULT_LOCAL);
	}

	return (2);
}

int32_t
parse_timeout(char *opt_arg)
{
	int32_t v, saved_errno;

	assert(opt_arg != NULL);

	saved_errno = errno;
	errno = 0;

	v = strtol(opt_arg, NULL, 10);
	if (errno != 0) {
		warn("Error parsing timeout value");
		errno = saved_errno;
		return (-1);
	}

	snmp_client.timeout.tv_sec = v;
	errno = saved_errno;
	return (2);
}

int32_t
parse_retry(char *opt_arg)
{
	uint32_t v;
	int32_t saved_errno;

	assert(opt_arg != NULL);

	saved_errno = errno;
	errno = 0;

	v = strtoul(opt_arg, NULL, 10);
	if (errno != 0) {
		warn("Error parsing retries count");
		errno = saved_errno;
		return (-1);
	}

	snmp_client.retries = v;
	errno = saved_errno;
	return (2);
}

int32_t
parse_version(char *opt_arg)
{
	uint32_t v;
	int32_t saved_errno;

	assert(opt_arg != NULL);

	saved_errno = errno;
	errno = 0;

	v = strtoul(opt_arg, NULL, 10);
	if (errno != 0) {
		warn("Error parsing version");
		errno = saved_errno;
		return (-1);
	}

	switch (v) {
		case 1:
			snmp_client.version = SNMP_V1;
			break;
		case 2:
			snmp_client.version = SNMP_V2c;
			break;
		case 3:
			snmp_client.version = SNMP_V3;
			break;
		default:
			warnx("Unsupported SNMP version - %u", v);
			errno = saved_errno;
			return (-1);
	}

	errno = saved_errno;
	return (2);
}

int32_t
parse_local_path(char *opt_arg)
{
	assert(opt_arg != NULL);

	if (sizeof(opt_arg) > sizeof(SNMP_LOCAL_PATH)) {
		warnx("Filename too long - %s", opt_arg);
		return (-1);
	}

	strlcpy(snmp_client.local_path, opt_arg, sizeof(SNMP_LOCAL_PATH));
	return (2);
}

int32_t
parse_buflen(char *opt_arg)
{
	uint32_t size;
	int32_t saved_errno;

	assert(opt_arg != NULL);

	saved_errno = errno;
	errno = 0;

	size = strtoul(opt_arg, NULL, 10);
	if (errno != 0) {
		warn("Error parsing buffer size");
		errno = saved_errno;
		return (-1);
	}

	if (size > MAX_BUFF_SIZE) {
		warnx("Buffer size too big - %d max allowed", MAX_BUFF_SIZE);
		errno = saved_errno;
		return (-1);
	}

	snmp_client.txbuflen = snmp_client.rxbuflen = size;
	errno = saved_errno;
	return (2);
}

int32_t
parse_debug(void)
{
	snmp_client.dump_pdus = 1;
	return (1);
}

int32_t
parse_discovery(struct snmp_toolinfo *snmptoolctx)
{
	SET_EDISCOVER(snmptoolctx);
	snmp_client.version = SNMP_V3;
	return (1);
}

int32_t
parse_local_key(struct snmp_toolinfo *snmptoolctx)
{
	SET_LOCALKEY(snmptoolctx);
	snmp_client.version = SNMP_V3;
	return (1);
}

int32_t
parse_num_oids(struct snmp_toolinfo *snmptoolctx)
{
	SET_NUMERIC(snmptoolctx);
	return (1);
}

int32_t
parse_output(struct snmp_toolinfo *snmptoolctx, char *opt_arg)
{
	assert(opt_arg != NULL);

	if (strlen(opt_arg) > strlen("verbose")) {
		warnx( "Invalid output option - %s",opt_arg);
		return (-1);
	}

	if (strncasecmp(opt_arg, "short", strlen(opt_arg)) == 0)
		SET_OUTPUT(snmptoolctx, OUTPUT_SHORT);
	else if (strncasecmp(opt_arg, "verbose", strlen(opt_arg)) == 0)
		SET_OUTPUT(snmptoolctx, OUTPUT_VERBOSE);
	else if (strncasecmp(opt_arg,"tabular", strlen(opt_arg)) == 0)
		SET_OUTPUT(snmptoolctx, OUTPUT_TABULAR);
	else if (strncasecmp(opt_arg, "quiet", strlen(opt_arg)) == 0)
		SET_OUTPUT(snmptoolctx, OUTPUT_QUIET);
	else {
		warnx( "Invalid output option - %s", opt_arg);
		return (-1);
	}

	return (2);
}

int32_t
parse_errors(struct snmp_toolinfo *snmptoolctx)
{
	SET_RETRY(snmptoolctx);
	return (1);
}

int32_t
parse_skip_access(struct snmp_toolinfo *snmptoolctx)
{
	SET_ERRIGNORE(snmptoolctx);
	return (1);
}

char *
snmp_parse_suboid(char *str, struct asn_oid *oid)
{
	char *endptr;
	asn_subid_t suboid;

	if (*str == '.')
		str++;

	if (*str < '0' || *str > '9')
		return (str);

	do {
		suboid = strtoul(str, &endptr, 10);
		if ((asn_subid_t) suboid > ASN_MAXID) {
			warnx("Suboid %u > ASN_MAXID", suboid);
			return (NULL);
		}
		if (snmp_suboid_append(oid, suboid) < 0)
			return (NULL);
		str = endptr + 1;
	} while (*endptr == '.');

	return (endptr);
}

static char *
snmp_int2asn_oid(char *str, struct asn_oid *oid)
{
	char *endptr;
	int32_t v, saved_errno;

	saved_errno = errno;
	errno = 0;

	v = strtol(str, &endptr, 10);
	if (errno != 0) {
		warn("Integer value %s not supported", str);
		errno = saved_errno;
		return (NULL);
	}
	errno = saved_errno;

	if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
		return (NULL);

	return (endptr);
}

/* It is a bit weird to have a table indexed by OID but still... */
static char *
snmp_oid2asn_oid(struct snmp_toolinfo *snmptoolctx, char *str,
    struct asn_oid *oid)
{
	int32_t i;
	char string[MAXSTR + 1], *endptr;
	struct snmp_object obj;

	for (i = 0; i < MAXSTR; i++)
		if (isalpha (*(str + i)) == 0)
			break;

	endptr = str + i;
	memset(&obj, 0, sizeof(struct snmp_object));
	if (i == 0) {
		if ((endptr = snmp_parse_suboid(str, &(obj.val.var))) == NULL)
			return (NULL);
		if (snmp_suboid_append(oid, (asn_subid_t) obj.val.var.len) < 0)
			return (NULL);
	} else {
		strlcpy(string, str, i + 1);
		if (snmp_lookup_enumoid(snmptoolctx, &obj, string) < 0) {
			warnx("Unknown string - %s", string);
			return (NULL);
		}
	}

	asn_append_oid(oid, &(obj.val.var));
	return (endptr);
}

static char *
snmp_ip2asn_oid(char *str, struct asn_oid *oid)
{
	uint32_t v;
	int32_t i;
	char *endptr, *ptr;

	ptr = str;

	for (i = 0; i < 4; i++) {
		v = strtoul(ptr, &endptr, 10);
		if (v > 0xff)
			return (NULL);
		if (*endptr != '.' && strchr("],\0", *endptr) == NULL && i != 3)
			return (NULL);
		if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
			return (NULL);
		ptr = endptr + 1;
	}

	return (endptr);
}

/* 32-bit counter, gauge, timeticks. */
static char *
snmp_uint2asn_oid(char *str, struct asn_oid *oid)
{
	char *endptr;
	uint32_t v;
	int32_t saved_errno;

	saved_errno = errno;
	errno = 0;

	v = strtoul(str, &endptr, 10);
	if (errno != 0) {
		warn("Integer value %s not supported", str);
		errno = saved_errno;
		return (NULL);
	}
	errno = saved_errno;
	if (snmp_suboid_append(oid, (asn_subid_t) v) < 0)
		return (NULL);

	return (endptr);
}

static char *
snmp_cnt64_2asn_oid(char *str, struct asn_oid *oid)
{
	char *endptr;
	uint64_t v;
	int32_t saved_errno;

	saved_errno = errno;
	errno = 0;

	v = strtoull(str, &endptr, 10);

	if (errno != 0) {
		warn("Integer value %s not supported", str);
		errno = saved_errno;
		return (NULL);
	}
	errno = saved_errno;
	if (snmp_suboid_append(oid, (asn_subid_t) (v & 0xffffffff)) < 0)
		return (NULL);

	if (snmp_suboid_append(oid, (asn_subid_t) (v >> 32)) < 0)
		return (NULL);

	return (endptr);
}

enum snmp_syntax
parse_syntax(char *str)
{
	int32_t i;

	for (i = 0; i < SNMP_SYNTAX_UNKNOWN; i++) {
		if (strncmp(syntax_strings[i].str, str,
		    strlen(syntax_strings[i].str)) == 0)
			return (syntax_strings[i].stx);
	}

	return (SNMP_SYNTAX_NULL);
}

static char *
snmp_parse_subindex(struct snmp_toolinfo *snmptoolctx, char *str,
    struct index *idx, struct snmp_object *object)
{
	char *ptr;
	int32_t i;
	enum snmp_syntax stx;
	char syntax[MAX_CMD_SYNTAX_LEN];

	ptr = str;
	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE) {
		for (i = 0; i < MAX_CMD_SYNTAX_LEN ; i++) {
			if (*(ptr + i) == ':')
				break;
		}

		if (i >= MAX_CMD_SYNTAX_LEN) {
			warnx("Unknown syntax in OID - %s", str);
			return (NULL);
		}
		/* Expect a syntax string here. */
		if ((stx = parse_syntax(str)) <= SNMP_SYNTAX_NULL) {
			warnx("Invalid  syntax - %s",syntax);
			return (NULL);
		}

		if (stx != idx->syntax && !ISSET_ERRIGNORE(snmptoolctx)) {
			warnx("Syntax mismatch - %d expected, %d given",
			    idx->syntax, stx);
			return (NULL);
		}
		/*
		 * That is where the suboid started + the syntax length + one
		 * character for ':'.
		 */
		ptr = str + i + 1;
	} else
		stx = idx->syntax;

	switch (stx) {
		case SNMP_SYNTAX_INTEGER:
			return (snmp_int2asn_oid(ptr, &(object->val.var)));
		case SNMP_SYNTAX_OID:
			return (snmp_oid2asn_oid(snmptoolctx, ptr,
			    &(object->val.var)));
		case SNMP_SYNTAX_IPADDRESS:
			return (snmp_ip2asn_oid(ptr, &(object->val.var)));
		case SNMP_SYNTAX_COUNTER:
			/* FALLTHROUGH */
		case SNMP_SYNTAX_GAUGE:
			/* FALLTHROUGH */
		case SNMP_SYNTAX_TIMETICKS:
			return (snmp_uint2asn_oid(ptr, &(object->val.var)));
		case SNMP_SYNTAX_COUNTER64:
			return (snmp_cnt64_2asn_oid(ptr, &(object->val.var)));
		case SNMP_SYNTAX_OCTETSTRING:
			return (snmp_tc2oid(idx->tc, ptr, &(object->val.var)));
		default:
			/* NOTREACHED */
			break;
	}

	return (NULL);
}

char *
snmp_parse_index(struct snmp_toolinfo *snmptoolctx, char *str,
    struct snmp_object *object)
{
	char *ptr;
	struct index *temp;

	if (object->info->table_idx == NULL)
		return (NULL);

	ptr = NULL;
	STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(object)), link) {
		if ((ptr = snmp_parse_subindex(snmptoolctx, str, temp, object))
		    == NULL)
			return (NULL);

		if (*ptr != ',' && *ptr != ']')
			return (NULL);
		str = ptr + 1;
	}

	if (ptr == NULL || *ptr != ']') {
		warnx("Mismatching index - %s", str);
		return (NULL);
	}

	return (ptr + 1);
}

/*
 * Fill in the struct asn_oid member of snmp_value with suboids from input.
 * If an error occurs - print message on stderr and return (-1).
 * If all is ok - return the length of the oid.
 */
int32_t
snmp_parse_numoid(char *argv, struct asn_oid *var)
{
	char *endptr, *str;
	asn_subid_t suboid;

	str = argv;

	if (*str == '.')
		str++;

	do {
		if (var->len == ASN_MAXOIDLEN) {
			warnx("Oid too long - %u", var->len);
			return (-1);
		}

		suboid = strtoul(str, &endptr, 10);
		if (suboid > ASN_MAXID) {
			warnx("Oid too long - %u", var->len);
			return (-1);
		}

		var->subs[var->len++] = suboid;
		str = endptr + 1;
	} while ( *endptr == '.');

	if (*endptr != '\0') {
		warnx("Invalid oid string - %s", argv);
		return (-1);
	}

	return (var->len);
}

/* Append a length 1 suboid to an asn_oid structure. */
int32_t
snmp_suboid_append(struct asn_oid *var, asn_subid_t suboid)
{
	if (var == NULL)
		return (-1);

	if (var->len >= ASN_MAXOIDLEN) {
		warnx("Oid too long - %u", var->len);
		return (-1);
	}

	var->subs[var->len++] = suboid;

	return (1);
}

/* Pop the last suboid from an asn_oid structure. */
int32_t
snmp_suboid_pop(struct asn_oid *var)
{
	asn_subid_t suboid;

	if (var == NULL)
		return (-1);

	if (var->len < 1)
		return (-1);

	suboid = var->subs[--(var->len)];
	var->subs[var->len] = 0;

	return (suboid);
}

/*
 * Parse the command-line provided string into an OID - alocate memory for a new
 * snmp object, fill in its fields and insert it in the object list. A
 * (snmp_verify_inoid_f) function must be provided to validate the input string.
 */
int32_t
snmp_object_add(struct snmp_toolinfo *snmptoolctx, snmp_verify_inoid_f func,
    char *string)
{
	struct snmp_object *obj;

	if (snmptoolctx == NULL)
		return (-1);

	/* XXX-BZ does that chack make sense? */
	if (snmptoolctx->objects >= SNMP_MAX_BINDINGS) {
		warnx("Too many bindings in PDU - %u", snmptoolctx->objects + 1);
		return (-1);
	}

	if ((obj = calloc(1, sizeof(struct snmp_object))) == NULL) {
		syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));
		return (-1);
	}

	if (func(snmptoolctx, obj, string) < 0) {
		warnx("Invalid OID - %s", string);
		free(obj);
		return (-1);
	}

	snmptoolctx->objects++;
	SLIST_INSERT_HEAD(&snmptoolctx->snmp_objectlist, obj, link);

	return (1);
}

/* Given an OID, find it in the object list and remove it. */
int32_t
snmp_object_remove(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
{
	struct snmp_object *temp;

	if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist)) {
		warnx("Object list already empty");
		return (-1);
	}


	SLIST_FOREACH(temp, &snmptoolctx->snmp_objectlist, link)
		if (asn_compare_oid(&(temp->val.var), oid) == 0)
			break;

	if (temp == NULL) {
		warnx("No such object in list");
		return (-1);
	}

	SLIST_REMOVE(&snmptoolctx->snmp_objectlist, temp, snmp_object, link);
	if (temp->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
	    temp->val.v.octetstring.octets != NULL)
		free(temp->val.v.octetstring.octets);
	free(temp);

	return (1);
}

static void
snmp_object_freeall(struct snmp_toolinfo *snmptoolctx)
{
	struct snmp_object *o;

	while ((o = SLIST_FIRST(&snmptoolctx->snmp_objectlist)) != NULL) {
		SLIST_REMOVE_HEAD(&snmptoolctx->snmp_objectlist, link);

		if (o->val.syntax == SNMP_SYNTAX_OCTETSTRING &&
		    o->val.v.octetstring.octets != NULL)
			free(o->val.v.octetstring.octets);
		free(o);
	}
}

/* Do all possible memory release before exit. */
void
snmp_tool_freeall(struct snmp_toolinfo *snmptoolctx)
{
	if (snmp_client.chost != NULL) {
		free(snmp_client.chost);
		snmp_client.chost = NULL;
	}

	if (snmp_client.cport != NULL) {
		free(snmp_client.cport);
		snmp_client.cport = NULL;
	}

	snmp_mapping_free(snmptoolctx);
	free_filelist(snmptoolctx);
	snmp_object_freeall(snmptoolctx);

	if (snmptoolctx->passwd != NULL) {
		free(snmptoolctx->passwd);
		snmptoolctx->passwd = NULL;
	}
}

/*
 * Fill all variables from the object list into a PDU. (snmp_verify_vbind_f)
 * function should check whether the variable is consistent in this PDU
 * (e.g do not add non-leaf OIDs to a GET PDU, or OIDs with read access only to
 * a SET PDU) - might be NULL though. (snmp_add_vbind_f) function is the
 * function actually adds the variable to the PDU and must not be NULL.
 */
int32_t
snmp_pdu_add_bindings(struct snmp_toolinfo *snmptoolctx,
    snmp_verify_vbind_f vfunc, snmp_add_vbind_f afunc,
    struct snmp_pdu *pdu, int32_t maxcount)
{
	int32_t nbindings, abind;
	struct snmp_object *obj;

	if (pdu == NULL || afunc == NULL)
		return (-1);

	/* Return 0 in case of no more work todo. */
	if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist))
		return (0);

	if (maxcount < 0 || maxcount > SNMP_MAX_BINDINGS) {
		warnx("maxcount out of range: <0 || >SNMP_MAX_BINDINGS");
		return (-1);
	}

	nbindings = 0;
	SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link) {
		if ((vfunc != NULL) && (vfunc(snmptoolctx, pdu, obj) < 0)) {
			nbindings = -1;
			break;
		}
		if ((abind = afunc(pdu, obj)) < 0) {
			nbindings = -1;
			break;
		}

		if (abind > 0) {
			/* Do not put more varbindings than requested. */
			if (++nbindings >= maxcount)
				break;
		}
	}

	return (nbindings);
}

/*
 * Locate an object in the object list and set a corresponding error status.
 */
int32_t
snmp_object_seterror(struct snmp_toolinfo *snmptoolctx,
    struct snmp_value *err_value, int32_t error_status)
{
	struct snmp_object *obj;

	if (SLIST_EMPTY(&snmptoolctx->snmp_objectlist) || err_value == NULL)
		return (-1);

	SLIST_FOREACH(obj, &snmptoolctx->snmp_objectlist, link)
		if (asn_compare_oid(&(err_value->var), &(obj->val.var)) == 0) {
			obj->error = error_status;
			return (1);
		}

	return (0);
}

/*
 * Check a PDU received in response to a SNMP_PDU_GET/SNMP_PDU_GETBULK request
 * but don't compare syntaxes - when sending a request PDU they must be null.
 * This is a (almost) complete copy of snmp_pdu_check() - with matching syntaxes
 * checks and some other checks skipped.
 */
int32_t
snmp_parse_get_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
{
	uint32_t i;

	for (i = 0; i < req->nbindings; i++) {
		if (asn_compare_oid(&req->bindings[i].var,
		    &resp->bindings[i].var) != 0) {
			warnx("Bad OID in response");
			return (-1);
		}

		if (snmp_client.version != SNMP_V1 && (resp->bindings[i].syntax
		    == SNMP_SYNTAX_NOSUCHOBJECT || resp->bindings[i].syntax ==
		    SNMP_SYNTAX_NOSUCHINSTANCE))
			return (0);
	}

	return (1);
}

int32_t
snmp_parse_getbulk_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
{
	int32_t N, R, M, r;

	if (req->error_status > (int32_t) resp->nbindings) {
		warnx("Bad number of bindings in response");
		return (-1);
	}

	for (N = 0; N < req->error_status; N++) {
		if (asn_is_suboid(&req->bindings[N].var,
		    &resp->bindings[N].var) == 0)
			return (0);
		if (resp->bindings[N].syntax == SNMP_SYNTAX_ENDOFMIBVIEW)
			return (0);
	}

	for (R = N , r = N; R  < (int32_t) req->nbindings; R++) {
		for (M = 0; M < req->error_index && (r + M) <
		    (int32_t) resp->nbindings; M++) {
			if (asn_is_suboid(&req->bindings[R].var,
			    &resp->bindings[r + M].var) == 0)
				return (0);

			if (resp->bindings[r + M].syntax ==
			    SNMP_SYNTAX_ENDOFMIBVIEW) {
				M++;
				break;
			}
		}
		r += M;
	}

	return (0);
}

int32_t
snmp_parse_getnext_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
{
	uint32_t i;

	for (i = 0; i < req->nbindings; i++) {
		if (asn_is_suboid(&req->bindings[i].var, &resp->bindings[i].var)
		    == 0)
			return (0);

		if (resp->version != SNMP_V1 && resp->bindings[i].syntax ==
		    SNMP_SYNTAX_ENDOFMIBVIEW)
			return (0);
	}

	return (1);
}

/*
 * Should be called to check a response to get/getnext/getbulk.
 */
int32_t
snmp_parse_resp(struct snmp_pdu *resp, struct snmp_pdu *req)
{
	if (resp == NULL || req == NULL)
		return (-2);

	if (resp->version != req->version) {
		warnx("Response has wrong version");
		return (-1);
	}

	if (resp->error_status == SNMP_ERR_NOSUCHNAME) {
		warnx("Error - No Such Name");
		return (0);
	}

	if (resp->error_status != SNMP_ERR_NOERROR) {
		warnx("Error %d in response", resp->error_status);
		return (-1);
	}

	if (resp->nbindings != req->nbindings && req->type != SNMP_PDU_GETBULK){
		warnx("Bad number of bindings in response");
		return (-1);
	}

	switch (req->type) {
		case SNMP_PDU_GET:
			return (snmp_parse_get_resp(resp,req));
		case SNMP_PDU_GETBULK:
			return (snmp_parse_getbulk_resp(resp,req));
		case SNMP_PDU_GETNEXT:
			return (snmp_parse_getnext_resp(resp,req));
		default:
			/* NOTREACHED */
			break;
	}

	return (-2);
}

static void
snmp_output_octetstring(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
    uint32_t len, uint8_t *octets)
{
	char *buf;

	if (len == 0 || octets == NULL)
		return;

	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ",
		    syntax_strings[SNMP_SYNTAX_OCTETSTRING].str);

	if ((buf = snmp_oct2tc(tc, len, (char *) octets)) != NULL) {
		fprintf(stdout, "%s", buf);
		free(buf);
	}
}

static void
snmp_output_octetindex(struct snmp_toolinfo *snmptoolctx, enum snmp_tc tc,
    struct asn_oid *oid)
{
	uint32_t i;
	uint8_t *s;

	if ((s = malloc(oid->subs[0] + 1)) == NULL)
		syslog(LOG_ERR, "malloc failed - %s", strerror(errno));
	else {
		for (i = 0; i < oid->subs[0]; i++)
			s[i] = (u_char) (oid->subs[i + 1]);

		snmp_output_octetstring(snmptoolctx, tc, oid->subs[0], s);
		free(s);
	}
}

/*
 * Check and output syntax type and value.
 */
static void
snmp_output_oid_value(struct snmp_toolinfo *snmptoolctx, struct asn_oid *oid)
{
	char oid_string[ASN_OIDSTRLEN];
	struct snmp_object obj;

	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_OID].str);

	if(!ISSET_NUMERIC(snmptoolctx)) {
		memset(&obj, 0, sizeof(struct snmp_object));
		asn_append_oid(&(obj.val.var), oid);

		if (snmp_lookup_enumstring(snmptoolctx, &obj) > 0)
			fprintf(stdout, "%s" , obj.info->string);
		else if (snmp_lookup_oidstring(snmptoolctx, &obj) > 0)
			fprintf(stdout, "%s" , obj.info->string);
		else if (snmp_lookup_nodestring(snmptoolctx, &obj) > 0)
			fprintf(stdout, "%s" , obj.info->string);
		else {
			(void) asn_oid2str_r(oid, oid_string);
			fprintf(stdout, "%s", oid_string);
		}
	} else {
		(void) asn_oid2str_r(oid, oid_string);
		fprintf(stdout, "%s", oid_string);
	}
}

static void
snmp_output_int(struct snmp_toolinfo *snmptoolctx, struct enum_pairs *enums,
    int32_t int_val)
{
	char *string;

	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ",
		    syntax_strings[SNMP_SYNTAX_INTEGER].str);

	if (enums != NULL && (string = enum_string_lookup(enums, int_val))
	    != NULL)
		fprintf(stdout, "%s", string);
	else
		fprintf(stdout, "%d", int_val);
}

static void
snmp_output_ipaddress(struct snmp_toolinfo *snmptoolctx, uint8_t *ip)
{
	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ",
		    syntax_strings[SNMP_SYNTAX_IPADDRESS].str);

	fprintf(stdout, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
}

static void
snmp_output_counter(struct snmp_toolinfo *snmptoolctx, uint32_t counter)
{
	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ",
		    syntax_strings[SNMP_SYNTAX_COUNTER].str);

	fprintf(stdout, "%u", counter);
}

static void
snmp_output_gauge(struct snmp_toolinfo *snmptoolctx, uint32_t gauge)
{
	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ", syntax_strings[SNMP_SYNTAX_GAUGE].str);

	fprintf(stdout, "%u", gauge);
}

static void
snmp_output_ticks(struct snmp_toolinfo *snmptoolctx, uint32_t ticks)
{
	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ",
		    syntax_strings[SNMP_SYNTAX_TIMETICKS].str);

	fprintf(stdout, "%u", ticks);
}

static void
snmp_output_counter64(struct snmp_toolinfo *snmptoolctx, uint64_t counter64)
{
	if (GET_OUTPUT(snmptoolctx) == OUTPUT_VERBOSE)
		fprintf(stdout, "%s : ",
		    syntax_strings[SNMP_SYNTAX_COUNTER64].str);

	fprintf(stdout,"%ju", counter64);
}

int32_t
snmp_output_numval(struct snmp_toolinfo *snmptoolctx, struct snmp_value *val,
    struct snmp_oid2str *entry)
{
	if (val == NULL)
		return (-1);

	if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
		fprintf(stdout, " = ");

	switch (val->syntax) {
	    case SNMP_SYNTAX_INTEGER:
		if (entry != NULL)
			snmp_output_int(snmptoolctx, entry->snmp_enum,
			    val->v.integer);
		else
			snmp_output_int(snmptoolctx, NULL, val->v.integer);
		break;

	    case SNMP_SYNTAX_OCTETSTRING:
		if (entry != NULL)
			snmp_output_octetstring(snmptoolctx, entry->tc,
			    val->v.octetstring.len, val->v.octetstring.octets);
		else
			snmp_output_octetstring(snmptoolctx, SNMP_STRING,
			    val->v.octetstring.len, val->v.octetstring.octets);
		break;

	    case SNMP_SYNTAX_OID:
		snmp_output_oid_value(snmptoolctx, &(val->v.oid));
		break;

	    case SNMP_SYNTAX_IPADDRESS:
		snmp_output_ipaddress(snmptoolctx, val->v.ipaddress);
		break;

	    case SNMP_SYNTAX_COUNTER:
		snmp_output_counter(snmptoolctx, val->v.uint32);
		break;

	    case SNMP_SYNTAX_GAUGE:
		snmp_output_gauge(snmptoolctx, val->v.uint32);
		break;

	    case SNMP_SYNTAX_TIMETICKS:
		snmp_output_ticks(snmptoolctx, val->v.uint32);
		break;

	    case SNMP_SYNTAX_COUNTER64:
		snmp_output_counter64(snmptoolctx, val->v.counter64);
		break;

	    case SNMP_SYNTAX_NOSUCHOBJECT:
		fprintf(stderr, "No Such Object\n");
		return (val->syntax);

	    case SNMP_SYNTAX_NOSUCHINSTANCE:
		fprintf(stderr, "No Such Instance\n");
		return (val->syntax);

	    case SNMP_SYNTAX_ENDOFMIBVIEW:
		fprintf(stdout, "End of Mib View\n");
		return (val->syntax);

	    case SNMP_SYNTAX_NULL:
		/* NOTREACHED */
		fprintf(stderr, "agent returned NULL Syntax\n");
		return (val->syntax);

	    default:
		/* NOTREACHED - If here - then all went completely wrong. */
		fprintf(stderr, "agent returned unknown syntax\n");
		return (-1);
	}

	fprintf(stdout, "\n");

	return (0);
}

static int32_t
snmp_fill_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *obj,
    struct snmp_value *val)
{
	int32_t rc;
	asn_subid_t suboid;

	if (obj == NULL || val == NULL)
		return (-1);

	if ((suboid = snmp_suboid_pop(&(val->var))) > ASN_MAXID)
		return (-1);

	memset(obj, 0, sizeof(struct snmp_object));
	asn_append_oid(&(obj->val.var), &(val->var));
	obj->val.syntax = val->syntax;

	if (obj->val.syntax > 0)
		rc = snmp_lookup_leafstring(snmptoolctx, obj);
	else
		rc = snmp_lookup_nonleaf_string(snmptoolctx, obj);

	(void) snmp_suboid_append(&(val->var), suboid);
	(void) snmp_suboid_append(&(obj->val.var), suboid);

	return (rc);
}

static int32_t
snmp_output_index(struct snmp_toolinfo *snmptoolctx, struct index *stx,
    struct asn_oid *oid)
{
	uint8_t ip[4];
	uint32_t bytes = 1;
	uint64_t cnt64;
	struct asn_oid temp, out;

	if (oid->len < bytes)
		return (-1);

	memset(&temp, 0, sizeof(struct asn_oid));
	asn_append_oid(&temp, oid);

	switch (stx->syntax) {
	    case SNMP_SYNTAX_INTEGER:
		snmp_output_int(snmptoolctx, stx->snmp_enum, temp.subs[0]);
		break;

	    case SNMP_SYNTAX_OCTETSTRING:
		if ((temp.subs[0] > temp.len -1 ) || (temp.subs[0] >
		    ASN_MAXOCTETSTRING))
			return (-1);
		snmp_output_octetindex(snmptoolctx, stx->tc, &temp);
		bytes += temp.subs[0];
		break;

	    case SNMP_SYNTAX_OID:
		if ((temp.subs[0] > temp.len -1) || (temp.subs[0] >
		    ASN_MAXOIDLEN))
			return (-1);

		bytes += temp.subs[0];
		memset(&out, 0, sizeof(struct asn_oid));
		asn_slice_oid(&out, &temp, 1, bytes);
		snmp_output_oid_value(snmptoolctx, &out);
		break;

	    case SNMP_SYNTAX_IPADDRESS:
		if (temp.len < 4)
			return (-1);
		for (bytes = 0; bytes < 4; bytes++)
			ip[bytes] = temp.subs[bytes];

		snmp_output_ipaddress(snmptoolctx, ip);
		bytes = 4;
		break;

	    case SNMP_SYNTAX_COUNTER:
		snmp_output_counter(snmptoolctx, temp.subs[0]);
		break;

	    case SNMP_SYNTAX_GAUGE:
		snmp_output_gauge(snmptoolctx, temp.subs[0]);
		break;

	    case SNMP_SYNTAX_TIMETICKS:
		snmp_output_ticks(snmptoolctx, temp.subs[0]);
		break;

	    case SNMP_SYNTAX_COUNTER64:
		if (oid->len < 2)
			return (-1);
		bytes = 2;
		memcpy(&cnt64, temp.subs, bytes);
		snmp_output_counter64(snmptoolctx, cnt64);
		break;

	    default:
		return (-1);
	}

	return (bytes);
}

static int32_t
snmp_output_object(struct snmp_toolinfo *snmptoolctx, struct snmp_object *o)
{
	int32_t i, first, len;
	struct asn_oid oid;
	struct index *temp;

	if (ISSET_NUMERIC(snmptoolctx))
		return (-1);

	if (o->info->table_idx == NULL) {
		fprintf(stdout,"%s.%d", o->info->string,
		    o->val.var.subs[o->val.var.len - 1]);
		return (1);
	}

	fprintf(stdout,"%s[", o->info->string);
	memset(&oid, 0, sizeof(struct asn_oid));

	len = 1;
	asn_slice_oid(&oid, &(o->val.var), (o->info->table_idx->var.len + len),
	    o->val.var.len);

	first = 1;
	STAILQ_FOREACH(temp, &(OBJECT_IDX_LIST(o)), link) {
		if(first)
			first = 0;
		else
			fprintf(stdout, ", ");
		if ((i = snmp_output_index(snmptoolctx, temp, &oid)) < 0)
			break;
		len += i;
		memset(&oid, 0, sizeof(struct asn_oid));
		asn_slice_oid(&oid, &(o->val.var),
		    (o->info->table_idx->var.len + len), o->val.var.len + 1);
	}

	fprintf(stdout,"]");
	return (1);
}

void
snmp_output_err_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
{
	struct snmp_object *object;
	char buf[ASN_OIDSTRLEN];

	if (pdu == NULL || (pdu->error_index > (int32_t) pdu->nbindings)) {
		fprintf(stdout, "Invalid error index in PDU\n");
		return;
	}

	if ((object = calloc(1, sizeof(struct snmp_object))) == NULL) {
		fprintf(stdout, "calloc: %s", strerror(errno));
		return;
	}

	fprintf(stdout, "Agent %s:%s returned error \n", snmp_client.chost,
	    snmp_client.cport);

	if (!ISSET_NUMERIC(snmptoolctx) && (snmp_fill_object(snmptoolctx, object,
	    &(pdu->bindings[pdu->error_index - 1])) > 0))
		snmp_output_object(snmptoolctx, object);
	else {
		asn_oid2str_r(&(pdu->bindings[pdu->error_index - 1].var), buf);
		fprintf(stdout,"%s", buf);
	}

	fprintf(stdout," caused error - ");
	if ((pdu->error_status > 0) && (pdu->error_status <=
	    SNMP_ERR_INCONS_NAME))
		fprintf(stdout, "%s\n", error_strings[pdu->error_status].str);
	else
		fprintf(stdout,"%s\n", error_strings[SNMP_ERR_UNKNOWN].str);

	free(object);
	object = NULL;
}

int32_t
snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
    struct asn_oid *root)
{
	struct snmp_object *object;
	char p[ASN_OIDSTRLEN];
	int32_t error;
	uint32_t i;

	if ((object = calloc(1, sizeof(struct snmp_object))) == NULL)
		return (-1);

	i = error = 0;
	while (i < pdu->nbindings) {
		if (root != NULL && !(asn_is_suboid(root,
		    &(pdu->bindings[i].var))))
			break;

		if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) {
			if (!ISSET_NUMERIC(snmptoolctx) &&
			    (snmp_fill_object(snmptoolctx, object,
			    &(pdu->bindings[i])) > 0))
				snmp_output_object(snmptoolctx, object);
			else {
				asn_oid2str_r(&(pdu->bindings[i].var), p);
				fprintf(stdout, "%s", p);
			}
		}
		error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]),
		    object->info);
		i++;
	}

	free(object);
	object = NULL;

	if (error)
		return (-1);

	return (i);
}

void
snmp_output_engine(void)
{
	uint32_t i;
	char *cptr, engine[2 * SNMP_ENGINE_ID_SIZ + 2];

	cptr = engine;
	for (i = 0; i < snmp_client.engine.engine_len; i++)
		cptr += sprintf(cptr, "%.2x", snmp_client.engine.engine_id[i]);
	*cptr++ = '\0';

	fprintf(stdout, "Engine ID 0x%s\n", engine);
	fprintf(stdout, "Boots : %u\t\tTime : %d\n",
	    snmp_client.engine.engine_boots,
	    snmp_client.engine.engine_time);
}

void
snmp_output_keys(void)
{
	uint32_t i, keylen = 0;
	char *cptr, extkey[2 * SNMP_AUTH_KEY_SIZ + 2];

	fprintf(stdout, "Localized keys for %s\n", snmp_client.user.sec_name);
	if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_MD5) {
		fprintf(stdout, "MD5 : 0x");
		keylen = SNMP_AUTH_HMACMD5_KEY_SIZ;
	} else if (snmp_client.user.auth_proto == SNMP_AUTH_HMAC_SHA) {
		fprintf(stdout, "SHA : 0x");
		keylen = SNMP_AUTH_HMACSHA_KEY_SIZ;
	}
	if (snmp_client.user.auth_proto != SNMP_AUTH_NOAUTH) {
		cptr = extkey;
		for (i = 0; i < keylen; i++)
			cptr += sprintf(cptr, "%.2x",
			    snmp_client.user.auth_key[i]);
		*cptr++ = '\0';
		fprintf(stdout, "%s\n", extkey);
	}

	if (snmp_client.user.priv_proto == SNMP_PRIV_DES) {
		fprintf(stdout, "DES : 0x");
		keylen = SNMP_PRIV_DES_KEY_SIZ;
	} else if (snmp_client.user.priv_proto == SNMP_PRIV_AES) {
		fprintf(stdout, "AES : 0x");
		keylen = SNMP_PRIV_AES_KEY_SIZ;
	}
	if (snmp_client.user.priv_proto != SNMP_PRIV_NOPRIV) {
		cptr = extkey;
		for (i = 0; i < keylen; i++)
			cptr += sprintf(cptr, "%.2x",
			    snmp_client.user.priv_key[i]);
		*cptr++ = '\0';
		fprintf(stdout, "%s\n", extkey);
	}
}