1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
|
/*
* Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <netinet/in.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include "subagentx_internal.h"
#include "subagentx.h"
enum subagentx_index_type {
SAI_TYPE_NEW,
SAI_TYPE_ANY,
SAI_TYPE_VALUE,
SAI_TYPE_DYNAMIC
};
#define SUBAGENTX_CONTEXT_CTX(sac) (sac->sac_name_default ? NULL : \
&(sac->sac_name))
struct subagentx_agentcaps {
struct subagentx_context *saa_sac;
struct agentx_oid saa_oid;
struct agentx_ostring saa_descr;
enum subagentx_cstate saa_cstate;
enum subagentx_dstate saa_dstate;
TAILQ_ENTRY(subagentx_agentcaps) saa_sac_agentcaps;
};
struct subagentx_region {
struct subagentx_context *sar_sac;
struct agentx_oid sar_oid;
uint8_t sar_timeout;
uint8_t sar_priority;
enum subagentx_cstate sar_cstate;
enum subagentx_dstate sar_dstate;
TAILQ_HEAD(, subagentx_index) sar_indices;
TAILQ_HEAD(, subagentx_object) sar_objects;
TAILQ_ENTRY(subagentx_region) sar_sac_regions;
};
struct subagentx_index {
struct subagentx_region *sai_sar;
enum subagentx_index_type sai_type;
struct agentx_varbind sai_vb;
struct subagentx_object **sai_object;
size_t sai_objectlen;
size_t sai_objectsize;
enum subagentx_cstate sai_cstate;
enum subagentx_dstate sai_dstate;
TAILQ_ENTRY(subagentx_index) sai_sar_indices;
};
struct subagentx_object {
struct subagentx_region *sao_sar;
struct agentx_oid sao_oid;
struct subagentx_index *sao_index[SUBAGENTX_OID_INDEX_MAX_LEN];
size_t sao_indexlen;
int sao_implied;
uint8_t sao_timeout;
/* Prevent freeing object while in use by get and set requesets */
uint32_t sao_lock;
void (*sao_get)(struct subagentx_varbind *);
enum subagentx_cstate sao_cstate;
enum subagentx_dstate sao_dstate;
RB_ENTRY(subagentx_object) sao_sac_objects;
TAILQ_ENTRY(subagentx_object) sao_sar_objects;
};
struct subagentx_varbind {
struct subagentx_get *sav_sag;
struct subagentx_object *sav_sao;
struct subagentx_varbind_index {
struct subagentx_index *sav_sai;
union agentx_data sav_idata;
uint8_t sav_idatacomplete;
} sav_index[SUBAGENTX_OID_INDEX_MAX_LEN];
size_t sav_indexlen;
int sav_initialized;
int sav_include;
struct agentx_varbind sav_vb;
struct agentx_oid sav_start;
struct agentx_oid sav_end;
enum agentx_pdu_error sav_error;
};
#define SUBAGENTX_GET_CTX(sag) (sag->sag_context_default ? NULL : \
&(sag->sag_context))
struct subagentx_request {
uint32_t sar_packetid;
int (*sar_cb)(struct agentx_pdu *, void *);
void *sar_cookie;
RB_ENTRY(subagentx_request) sar_sa_requests;
};
static void subagentx_start(struct subagentx *);
static void subagentx_finalize(struct subagentx *, int);
static void subagentx_wantwritenow(struct subagentx *, int);
void (*subagentx_wantwrite)(struct subagentx *, int) =
subagentx_wantwritenow;
static void subagentx_reset(struct subagentx *);
static void subagentx_free_finalize(struct subagentx *);
static int subagentx_session_start(struct subagentx_session *);
static int subagentx_session_finalize(struct agentx_pdu *, void *);
static int subagentx_session_close(struct subagentx_session *,
enum agentx_close_reason);
static int subagentx_session_close_finalize(struct agentx_pdu *, void *);
static void subagentx_session_free_finalize(struct subagentx_session *);
static void subagentx_session_reset(struct subagentx_session *);
static void subagentx_context_start(struct subagentx_context *);
static void subagentx_context_free_finalize(struct subagentx_context *);
static void subagentx_context_reset(struct subagentx_context *);
static int subagentx_agentcaps_start(struct subagentx_agentcaps *);
static int subagentx_agentcaps_finalize(struct agentx_pdu *, void *);
static int subagentx_agentcaps_close(struct subagentx_agentcaps *);
static int subagentx_agentcaps_close_finalize(struct agentx_pdu *, void *);
static void subagentx_agentcaps_free_finalize(struct subagentx_agentcaps *);
static void subagentx_agentcaps_reset(struct subagentx_agentcaps *);
static int subagentx_region_start(struct subagentx_region *);
static int subagentx_region_finalize(struct agentx_pdu *, void *);
static int subagentx_region_close(struct subagentx_region *);
static int subagentx_region_close_finalize(struct agentx_pdu *, void *);
static void subagentx_region_free_finalize(struct subagentx_region *);
static void subagentx_region_reset(struct subagentx_region *);
static struct subagentx_index *subagentx_index(struct subagentx_region *,
struct agentx_varbind *, enum subagentx_index_type);
static int subagentx_index_start(struct subagentx_index *);
static int subagentx_index_finalize(struct agentx_pdu *, void *);
static void subagentx_index_free_finalize(struct subagentx_index *);
static void subagentx_index_reset(struct subagentx_index *);
static int subagentx_index_close(struct subagentx_index *);
static int subagentx_index_close_finalize(struct agentx_pdu *, void *);
static int subagentx_object_start(struct subagentx_object *);
static int subagentx_object_finalize(struct agentx_pdu *, void *);
static int subagentx_object_lock(struct subagentx_object *);
static void subagentx_object_unlock(struct subagentx_object *);
static int subagentx_object_close(struct subagentx_object *);
static int subagentx_object_close_finalize(struct agentx_pdu *, void *);
static void subagentx_object_free_finalize(struct subagentx_object *);
static void subagentx_object_reset(struct subagentx_object *);
static int subagentx_object_cmp(struct subagentx_object *,
struct subagentx_object *);
static void subagentx_get_start(struct subagentx_context *,
struct agentx_pdu *);
static void subagentx_get_finalize(struct subagentx_get *);
static void subagentx_get_free(struct subagentx_get *);
static void subagentx_varbind_start(struct subagentx_varbind *);
static void subagentx_varbind_finalize(struct subagentx_varbind *);
static void subagentx_varbind_nosuchobject(struct subagentx_varbind *);
static void subagentx_varbind_nosuchinstance(struct subagentx_varbind *);
static void subagentx_varbind_endofmibview(struct subagentx_varbind *);
static void subagentx_varbind_error_type(struct subagentx_varbind *,
enum agentx_pdu_error, int);
static int subagentx_request(struct subagentx *, uint32_t,
int (*)(struct agentx_pdu *, void *), void *);
static int subagentx_request_cmp(struct subagentx_request *,
struct subagentx_request *);
static int subagentx_strcat(char **, const char *);
RB_PROTOTYPE_STATIC(sa_requests, subagentx_request, sar_sa_requests,
subagentx_request_cmp)
RB_PROTOTYPE_STATIC(sac_objects, subagentx_object, sao_sac_objects,
subagentx_object_cmp)
struct subagentx *
subagentx(void (*nofd)(struct subagentx *, void *, int), void *cookie)
{
struct subagentx *sa;
if ((sa = calloc(1, sizeof(*sa))) == NULL)
return NULL;
sa->sa_nofd = nofd;
sa->sa_cookie = cookie;
sa->sa_fd = -1;
sa->sa_cstate = SA_CSTATE_CLOSE;
sa->sa_dstate = SA_DSTATE_OPEN;
TAILQ_INIT(&(sa->sa_sessions));
TAILQ_INIT(&(sa->sa_getreqs));
RB_INIT(&(sa->sa_requests));
subagentx_start(sa);
return sa;
}
/*
* subagentx_finalize is not a suitable name for a public API,
* but use it internally for consistency
*/
void
subagentx_connect(struct subagentx *sa, int fd)
{
subagentx_finalize(sa, fd);
}
static void
subagentx_start(struct subagentx *sa)
{
#ifdef AGENTX_DEBUG
if (sa->sa_cstate != SA_CSTATE_CLOSE ||
sa->sa_dstate != SA_DSTATE_OPEN)
subagentx_log_sa_fatalx(sa, "%s: unexpected connect", __func__);
#endif
sa->sa_cstate = SA_CSTATE_WAITOPEN;
sa->sa_nofd(sa, sa->sa_cookie, 0);
}
static void
subagentx_finalize(struct subagentx *sa, int fd)
{
struct subagentx_session *sas;
if (sa->sa_cstate != SA_CSTATE_WAITOPEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sa_fatalx(sa, "%s: subagentx unexpected connect",
__func__);
#else
subagentx_log_sa_warnx(sa,
"%s: subagentx unexpected connect: ignoring", __func__);
return;
#endif
}
if ((sa->sa_ax = agentx_new(fd)) == NULL) {
subagentx_log_sa_warn(sa, "failed to initialize");
close(fd);
subagentx_reset(sa);
return;
}
subagentx_log_sa_info(sa, "new connection: %d", fd);
sa->sa_fd = fd;
sa->sa_cstate = SA_CSTATE_OPEN;
TAILQ_FOREACH(sas, &(sa->sa_sessions), sas_sa_sessions) {
if (subagentx_session_start(sas) == -1)
break;
}
}
static void
subagentx_wantwritenow(struct subagentx *sa, int fd)
{
subagentx_write(sa);
}
static void
subagentx_reset(struct subagentx *sa)
{
struct subagentx_session *sas, *tsas;
struct subagentx_request *sar;
struct subagentx_get *sag;
agentx_free(sa->sa_ax);
sa->sa_ax = NULL;
sa->sa_fd = -1;
sa->sa_cstate = SA_CSTATE_CLOSE;
while ((sar = RB_MIN(sa_requests, &(sa->sa_requests))) != NULL) {
RB_REMOVE(sa_requests, &(sa->sa_requests), sar);
free(sar);
}
TAILQ_FOREACH_SAFE(sas, &(sa->sa_sessions), sas_sa_sessions, tsas)
subagentx_session_reset(sas);
while (!TAILQ_EMPTY(&(sa->sa_getreqs))) {
sag = TAILQ_FIRST(&(sa->sa_getreqs));
sag->sag_sac = NULL;
TAILQ_REMOVE(&(sa->sa_getreqs), sag, sag_sa_getreqs);
}
if (sa->sa_dstate == SA_DSTATE_CLOSE) {
subagentx_free_finalize(sa);
return;
}
subagentx_start(sa);
}
void
subagentx_free(struct subagentx *sa)
{
struct subagentx_session *sas, *tsas;
if (sa == NULL)
return;
if (sa->sa_dstate == SA_DSTATE_CLOSE) {
/* Malloc throws abort on invalid pointers as well */
subagentx_log_sa_fatalx(sa, "%s: double free", __func__);
}
sa->sa_dstate = SA_DSTATE_CLOSE;
if (!TAILQ_EMPTY(&(sa->sa_sessions))) {
TAILQ_FOREACH_SAFE(sas, &(sa->sa_sessions), sas_sa_sessions,
tsas) {
if (sas->sas_dstate != SA_DSTATE_CLOSE)
subagentx_session_free(sas);
}
} else
subagentx_free_finalize(sa);
}
static void
subagentx_free_finalize(struct subagentx *sa)
{
#ifdef AGENTX_DEBUG
if (sa->sa_dstate != SA_DSTATE_CLOSE)
subagentx_log_sa_fatalx(sa, "%s: subagentx not closing",
__func__);
if (!TAILQ_EMPTY(&(sa->sa_sessions)))
subagentx_log_sa_fatalx(sa, "%s: subagentx still has sessions",
__func__);
if (!RB_EMPTY(&(sa->sa_requests)))
subagentx_log_sa_fatalx(sa,
"%s: subagentx still has pending requests", __func__);
#endif
agentx_free(sa->sa_ax);
sa->sa_nofd(sa, sa->sa_cookie, 1);
free(sa);
}
struct subagentx_session *
subagentx_session(struct subagentx *sa, uint32_t oid[],
size_t oidlen, const char *descr, uint8_t timeout)
{
struct subagentx_session *sas;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sa_fatalx(sa, "%s: oidlen > %d", __func__,
SUBAGENTX_OID_MAX_LEN);
#else
errno = EINVAL;
return NULL;
#endif
}
if ((sas = calloc(1, sizeof(*sas))) == NULL)
return NULL;
sas->sas_sa = sa;
sas->sas_timeout = timeout;
for (i = 0; i < oidlen; i++)
sas->sas_oid.aoi_id[i] = oid[i];
sas->sas_oid.aoi_idlen = oidlen;
sas->sas_descr.aos_string = (unsigned char *)strdup(descr);
if (sas->sas_descr.aos_string == NULL) {
free(sas);
return NULL;
}
sas->sas_descr.aos_slen = strlen(descr);
sas->sas_cstate = SA_CSTATE_CLOSE;
sas->sas_dstate = SA_DSTATE_OPEN;
TAILQ_INIT(&(sas->sas_contexts));
TAILQ_INSERT_HEAD(&(sa->sa_sessions), sas, sas_sa_sessions);
if (sa->sa_cstate == SA_CSTATE_OPEN)
(void) subagentx_session_start(sas);
return sas;
}
static int
subagentx_session_start(struct subagentx_session *sas)
{
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (sa->sa_cstate != SA_CSTATE_OPEN ||
sas->sas_cstate != SA_CSTATE_CLOSE ||
sas->sas_dstate != SA_DSTATE_OPEN)
subagentx_log_sa_fatalx(sa, "%s: unexpected session open",
__func__);
#endif
packetid = agentx_open(sa->sa_ax, sas->sas_timeout, &(sas->sas_oid),
&(sas->sas_descr));
if (packetid == 0) {
subagentx_log_sa_warn(sa, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_OPEN));
subagentx_reset(sa);
return -1;
}
sas->sas_packetid = packetid;
subagentx_log_sa_info(sa, "opening session");
sas->sas_cstate = SA_CSTATE_WAITOPEN;
return subagentx_request(sa, packetid, subagentx_session_finalize, sas);
}
static int
subagentx_session_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_session *sas = cookie;
struct subagentx *sa = sas->sas_sa;
struct subagentx_context *sac;
#ifdef AGENTX_DEBUG
if (sas->sas_cstate != SA_CSTATE_WAITOPEN)
subagentx_log_sa_fatalx(sa, "%s: not expecting new session",
__func__);
#endif
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
subagentx_log_sa_warnx(sa, "failed to open session: %s",
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
subagentx_reset(sa);
return -1;
}
sas->sas_id = pdu->ap_header.aph_sessionid;
sas->sas_cstate = SA_CSTATE_OPEN;
if (sas->sas_dstate == SA_DSTATE_CLOSE) {
subagentx_session_close(sas, AGENTX_CLOSE_SHUTDOWN);
return 0;
}
subagentx_log_sas_info(sas, "open");
TAILQ_FOREACH(sac, &(sas->sas_contexts), sac_sas_contexts)
subagentx_context_start(sac);
return 0;
}
static int
subagentx_session_close(struct subagentx_session *sas,
enum agentx_close_reason reason)
{
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (sas->sas_cstate != SA_CSTATE_OPEN)
subagentx_log_sa_fatalx(sa, "%s: unexpected session close",
__func__);
#endif
if ((packetid = agentx_close(sa->sa_ax, sas->sas_id, reason)) == 0) {
subagentx_log_sas_warn(sas, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_CLOSE));
subagentx_reset(sa);
return -1;
}
subagentx_log_sas_info(sas, "closing session: %s",
agentx_closereason2string(reason));
sas->sas_cstate = SA_CSTATE_WAITCLOSE;
return subagentx_request(sa, packetid, subagentx_session_close_finalize,
sas);
}
static int
subagentx_session_close_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_session *sas = cookie;
struct subagentx *sa = sas->sas_sa;
struct subagentx_context *sac, *tsac;
#ifdef AGENTX_DEBUG
if (sas->sas_cstate != SA_CSTATE_WAITCLOSE)
subagentx_log_sas_fatalx(sas, "%s: not expecting session close",
__func__);
#endif
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
subagentx_log_sas_warnx(sas, "failed to close session: %s",
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
subagentx_reset(sa);
return -1;
}
sas->sas_cstate = SA_CSTATE_CLOSE;
subagentx_log_sas_info(sas, "closed");
TAILQ_FOREACH_SAFE(sac, &(sas->sas_contexts), sac_sas_contexts, tsac)
subagentx_context_reset(sac);
if (sas->sas_dstate == SA_DSTATE_CLOSE)
subagentx_session_free_finalize(sas);
else {
if (sa->sa_cstate == SA_CSTATE_OPEN)
if (subagentx_session_start(sas) == -1)
return -1;
}
return 0;
}
void
subagentx_session_free(struct subagentx_session *sas)
{
struct subagentx_context *sac, *tsac;
if (sas == NULL)
return;
if (sas->sas_dstate == SA_DSTATE_CLOSE)
subagentx_log_sas_fatalx(sas, "%s: double free", __func__);
sas->sas_dstate = SA_DSTATE_CLOSE;
if (sas->sas_cstate == SA_CSTATE_OPEN)
(void) subagentx_session_close(sas, AGENTX_CLOSE_SHUTDOWN);
TAILQ_FOREACH_SAFE(sac, &(sas->sas_contexts), sac_sas_contexts, tsac) {
if (sac->sac_dstate != SA_DSTATE_CLOSE)
subagentx_context_free(sac);
}
if (sas->sas_cstate == SA_CSTATE_CLOSE)
subagentx_session_free_finalize(sas);
}
static void
subagentx_session_free_finalize(struct subagentx_session *sas)
{
struct subagentx *sa = sas->sas_sa;
#ifdef AGENTX_DEBUG
if (sas->sas_cstate != SA_CSTATE_CLOSE)
subagentx_log_sas_fatalx(sas, "%s: free without closing",
__func__);
if (!TAILQ_EMPTY(&(sas->sas_contexts)))
subagentx_log_sas_fatalx(sas,
"%s: subagentx still has contexts", __func__);
#endif
TAILQ_REMOVE(&(sa->sa_sessions), sas, sas_sa_sessions);
free(sas->sas_descr.aos_string);
free(sas);
if (TAILQ_EMPTY(&(sa->sa_sessions)) && sa->sa_dstate == SA_DSTATE_CLOSE)
subagentx_free_finalize(sa);
}
static void
subagentx_session_reset(struct subagentx_session *sas)
{
struct subagentx_context *sac, *tsac;
sas->sas_cstate = SA_CSTATE_CLOSE;
TAILQ_FOREACH_SAFE(sac, &(sas->sas_contexts), sac_sas_contexts, tsac)
subagentx_context_reset(sac);
if (sas->sas_dstate == SA_DSTATE_CLOSE)
subagentx_session_free_finalize(sas);
}
struct subagentx_context *
subagentx_context(struct subagentx_session *sas, const char *name)
{
struct subagentx_context *sac;
if (sas->sas_dstate == SA_DSTATE_CLOSE)
subagentx_log_sas_fatalx(sas, "%s: use after free", __func__);
if ((sac = calloc(1, sizeof(*sac))) == NULL)
return NULL;
sac->sac_sas = sas;
sac->sac_name_default = (name == NULL);
if (name != NULL) {
sac->sac_name.aos_string = (unsigned char *)strdup(name);
if (sac->sac_name.aos_string == NULL) {
free(sac);
return NULL;
}
sac->sac_name.aos_slen = strlen(name);
}
sac->sac_cstate = sas->sas_cstate == SA_CSTATE_OPEN ?
SA_CSTATE_OPEN : SA_CSTATE_CLOSE;
sac->sac_dstate = SA_DSTATE_OPEN;
TAILQ_INIT(&(sac->sac_agentcaps));
TAILQ_INIT(&(sac->sac_regions));
TAILQ_INSERT_HEAD(&(sas->sas_contexts), sac, sac_sas_contexts);
return sac;
}
static void
subagentx_context_start(struct subagentx_context *sac)
{
struct subagentx_agentcaps *saa;
struct subagentx_region *sar;
#ifdef AGENTX_DEBUG
if (sac->sac_cstate != SA_CSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected context start",
__func__);
#endif
sac->sac_cstate = SA_CSTATE_OPEN;
TAILQ_FOREACH(saa, &(sac->sac_agentcaps), saa_sac_agentcaps) {
if (subagentx_agentcaps_start(saa) == -1)
return;
}
TAILQ_FOREACH(sar, &(sac->sac_regions), sar_sac_regions) {
if (subagentx_region_start(sar) == -1)
return;
}
}
uint32_t
subagentx_context_uptime(struct subagentx_context *sac)
{
struct timespec cur, res;
if (sac->sac_sysuptimespec.tv_sec == 0 &&
sac->sac_sysuptimespec.tv_nsec == 0)
return 0;
(void) clock_gettime(CLOCK_MONOTONIC, &cur);
timespecsub(&cur, &(sac->sac_sysuptimespec), &res);
return sac->sac_sysuptime +
(uint32_t) ((res.tv_sec * 100) + (res.tv_nsec / 10000000));
}
struct subagentx_object *
subagentx_context_object_find(struct subagentx_context *sac,
const uint32_t oid[], size_t oidlen, int active, int instance)
{
struct subagentx_object *sao, sao_search;
size_t i;
for (i = 0; i < oidlen; i++)
sao_search.sao_oid.aoi_id[i] = oid[i];
sao_search.sao_oid.aoi_idlen = oidlen;
sao = RB_FIND(sac_objects, &(sac->sac_objects), &sao_search);
while (sao == NULL && !instance && sao_search.sao_oid.aoi_idlen > 0) {
sao = RB_FIND(sac_objects, &(sac->sac_objects), &sao_search);
sao_search.sao_oid.aoi_idlen--;
}
if (active && sao != NULL && sao->sao_cstate != SA_CSTATE_OPEN)
return NULL;
return sao;
}
struct subagentx_object *
subagentx_context_object_nfind(struct subagentx_context *sac,
const uint32_t oid[], size_t oidlen, int active, int inclusive)
{
struct subagentx_object *sao, sao_search;
size_t i;
for (i = 0; i < oidlen; i++)
sao_search.sao_oid.aoi_id[i] = oid[i];
sao_search.sao_oid.aoi_idlen = oidlen;
sao = RB_NFIND(sac_objects, &(sac->sac_objects), &sao_search);
if (!inclusive && sao != NULL &&
agentx_oid_cmp(&(sao_search.sao_oid), &(sao->sao_oid)) <= 0) {
sao = RB_NEXT(sac_objects, &(sac->sac_objects), sao);
}
while (active && sao != NULL && sao->sao_cstate != SA_CSTATE_OPEN)
sao = RB_NEXT(sac_objects, &(sac->sac_objects), sao);
return sao;
}
void
subagentx_context_free(struct subagentx_context *sac)
{
struct subagentx_agentcaps *saa, *tsaa;
struct subagentx_region *sar, *tsar;
if (sac == NULL)
return;
#ifdef AGENTX_DEBUG
if (sac->sac_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: double free", __func__);
#endif
sac->sac_dstate = SA_DSTATE_CLOSE;
TAILQ_FOREACH_SAFE(saa, &(sac->sac_agentcaps), saa_sac_agentcaps,
tsaa) {
if (saa->saa_dstate != SA_DSTATE_CLOSE)
subagentx_agentcaps_free(saa);
}
TAILQ_FOREACH_SAFE(sar, &(sac->sac_regions), sar_sac_regions, tsar) {
if (sar->sar_dstate != SA_DSTATE_CLOSE)
subagentx_region_free(sar);
}
}
static void
subagentx_context_free_finalize(struct subagentx_context *sac)
{
struct subagentx_session *sas = sac->sac_sas;
#ifdef AGENTX_DEBUG
if (sac->sac_dstate != SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected context free",
__func__);
#endif
if (!TAILQ_EMPTY(&(sac->sac_regions)) ||
!TAILQ_EMPTY(&(sac->sac_agentcaps)))
return;
TAILQ_REMOVE(&(sas->sas_contexts), sac, sac_sas_contexts);
free(sac->sac_name.aos_string);
free(sac);
}
static void
subagentx_context_reset(struct subagentx_context *sac)
{
struct subagentx_agentcaps *saa, *tsaa;
struct subagentx_region *sar, *tsar;
sac->sac_cstate = SA_CSTATE_CLOSE;
sac->sac_sysuptimespec.tv_sec = 0;
sac->sac_sysuptimespec.tv_nsec = 0;
TAILQ_FOREACH_SAFE(saa, &(sac->sac_agentcaps), saa_sac_agentcaps, tsaa)
subagentx_agentcaps_reset(saa);
TAILQ_FOREACH_SAFE(sar, &(sac->sac_regions), sar_sac_regions, tsar)
subagentx_region_reset(sar);
if (sac->sac_dstate == SA_DSTATE_CLOSE)
subagentx_context_free_finalize(sac);
}
struct subagentx_agentcaps *
subagentx_agentcaps(struct subagentx_context *sac, uint32_t oid[],
size_t oidlen, const char *descr)
{
struct subagentx_agentcaps *saa;
size_t i;
if (sac->sac_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: use after free", __func__);
if ((saa = calloc(1, sizeof(*saa))) == NULL)
return NULL;
saa->saa_sac = sac;
for (i = 0; i < oidlen; i++)
saa->saa_oid.aoi_id[i] = oid[i];
saa->saa_oid.aoi_idlen = oidlen;
saa->saa_descr.aos_string = (unsigned char *)strdup(descr);
if (saa->saa_descr.aos_string == NULL) {
free(saa);
return NULL;
}
saa->saa_descr.aos_slen = strlen(descr);
saa->saa_cstate = SA_CSTATE_CLOSE;
saa->saa_dstate = SA_DSTATE_OPEN;
TAILQ_INSERT_TAIL(&(sac->sac_agentcaps), saa, saa_sac_agentcaps);
if (sac->sac_cstate == SA_CSTATE_OPEN)
subagentx_agentcaps_start(saa);
return saa;
}
static int
subagentx_agentcaps_start(struct subagentx_agentcaps *saa)
{
struct subagentx_context *sac = saa->saa_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (sac->sac_cstate != SA_CSTATE_OPEN ||
saa->saa_cstate != SA_CSTATE_CLOSE ||
saa->saa_dstate != SA_DSTATE_OPEN)
subagentx_log_sac_fatalx(sac,
"%s: unexpected region registration", __func__);
#endif
packetid = agentx_addagentcaps(sa->sa_ax, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), &(saa->saa_oid), &(saa->saa_descr));
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_ADDAGENTCAPS));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "agentcaps %s: opening",
agentx_oid2string(&(saa->saa_oid)));
saa->saa_cstate = SA_CSTATE_WAITOPEN;
return subagentx_request(sa, packetid, subagentx_agentcaps_finalize,
saa);
}
static int
subagentx_agentcaps_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_agentcaps *saa = cookie;
struct subagentx_context *sac = saa->saa_sac;
#ifdef AGENTX_DEBUG
if (saa->saa_cstate != SA_CSTATE_WAITOPEN)
subagentx_log_sac_fatalx(sac,
"%s: not expecting agentcaps open", __func__);
#endif
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
/* Agentcaps failing is nothing too serious */
subagentx_log_sac_warn(sac, "agentcaps %s: %s",
agentx_oid2string(&(saa->saa_oid)),
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
saa->saa_cstate = SA_CSTATE_CLOSE;
return 0;
}
saa->saa_cstate = SA_CSTATE_OPEN;
subagentx_log_sac_info(sac, "agentcaps %s: open",
agentx_oid2string(&(saa->saa_oid)));
if (saa->saa_dstate == SA_DSTATE_CLOSE)
subagentx_agentcaps_close(saa);
return 0;
}
static int
subagentx_agentcaps_close(struct subagentx_agentcaps *saa)
{
struct subagentx_context *sac = saa->saa_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (saa->saa_cstate != SA_CSTATE_OPEN)
subagentx_log_sac_fatalx(sac, "%s: unexpected agentcaps close",
__func__);
#endif
saa->saa_cstate = SA_CSTATE_WAITCLOSE;
if (sas->sas_cstate == SA_CSTATE_WAITCLOSE)
return 0;
packetid = agentx_removeagentcaps(sa->sa_ax, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), &(saa->saa_oid));
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_REMOVEAGENTCAPS));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "agentcaps %s: closing",
agentx_oid2string(&(saa->saa_oid)));
return subagentx_request(sa, packetid,
subagentx_agentcaps_close_finalize, saa);
}
static int
subagentx_agentcaps_close_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_agentcaps *saa = cookie;
struct subagentx_context *sac = saa->saa_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
#ifdef AGENTX_DEBUG
if (saa->saa_cstate != SA_CSTATE_WAITCLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected agentcaps close",
__func__);
#endif
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
subagentx_log_sac_warnx(sac, "agentcaps %s: %s",
agentx_oid2string(&(saa->saa_oid)),
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
subagentx_reset(sa);
return -1;
}
saa->saa_cstate = SA_CSTATE_CLOSE;
subagentx_log_sac_info(sac, "agentcaps %s: closed",
agentx_oid2string(&(saa->saa_oid)));
if (saa->saa_dstate == SA_DSTATE_CLOSE) {
subagentx_agentcaps_free_finalize(saa);
return 0;
} else {
if (sac->sac_cstate == SA_CSTATE_OPEN) {
if (subagentx_agentcaps_start(saa) == -1)
return -1;
}
}
return 0;
}
void
subagentx_agentcaps_free(struct subagentx_agentcaps *saa)
{
if (saa == NULL)
return;
if (saa->saa_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(saa->saa_sac, "%s: double free",
__func__);
saa->saa_dstate = SA_DSTATE_CLOSE;
if (saa->saa_cstate == SA_CSTATE_OPEN) {
if (subagentx_agentcaps_close(saa) == -1)
return;
}
if (saa->saa_cstate == SA_CSTATE_CLOSE)
subagentx_agentcaps_free_finalize(saa);
}
static void
subagentx_agentcaps_free_finalize(struct subagentx_agentcaps *saa)
{
struct subagentx_context *sac = saa->saa_sac;
#ifdef AGENTX_DEBUG
if (saa->saa_dstate != SA_DSTATE_CLOSE ||
saa->saa_cstate != SA_CSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected free", __func__);
#endif
TAILQ_REMOVE(&(sac->sac_agentcaps), saa, saa_sac_agentcaps);
free(saa->saa_descr.aos_string);
free(saa);
if (sac->sac_dstate == SA_DSTATE_CLOSE)
subagentx_context_free_finalize(sac);
}
static void
subagentx_agentcaps_reset(struct subagentx_agentcaps *saa)
{
saa->saa_cstate = SA_CSTATE_CLOSE;
if (saa->saa_dstate == SA_DSTATE_CLOSE)
subagentx_agentcaps_free_finalize(saa);
}
struct subagentx_region *
subagentx_region(struct subagentx_context *sac, uint32_t oid[],
size_t oidlen, uint8_t timeout)
{
struct subagentx_region *sar;
struct agentx_oid tmpoid;
size_t i;
if (sac->sac_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: use after free", __func__);
if (oidlen < 1) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sac, "%s: oidlen == 0", __func__);
#else
errno = EINVAL;
return NULL;
#endif
}
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sac, "%s: oidlen > %d", __func__,
SUBAGENTX_OID_MAX_LEN);
#else
errno = EINVAL;
return NULL;
#endif
}
for (i = 0; i < oidlen; i++)
tmpoid.aoi_id[i] = oid[i];
tmpoid.aoi_idlen = oidlen;
TAILQ_FOREACH(sar, &(sac->sac_regions), sar_sac_regions) {
if (agentx_oid_cmp(&(sar->sar_oid), &tmpoid) == 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sac,
"%s: duplicate region registration", __func__);
#else
errno = EINVAL;
return NULL;
#endif
}
}
if ((sar = calloc(1, sizeof(*sar))) == NULL)
return NULL;
sar->sar_sac = sac;
sar->sar_timeout = timeout;
sar->sar_priority = AGENTX_PRIORITY_DEFAULT;
bcopy(&tmpoid, &(sar->sar_oid), sizeof(sar->sar_oid));
sar->sar_cstate = SA_CSTATE_CLOSE;
sar->sar_dstate = SA_DSTATE_OPEN;
TAILQ_INIT(&(sar->sar_indices));
TAILQ_INIT(&(sar->sar_objects));
TAILQ_INSERT_HEAD(&(sac->sac_regions), sar, sar_sac_regions);
if (sac->sac_cstate == SA_CSTATE_OPEN)
(void) subagentx_region_start(sar);
return sar;
}
static int
subagentx_region_start(struct subagentx_region *sar)
{
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (sac->sac_cstate != SA_CSTATE_OPEN ||
sar->sar_cstate != SA_CSTATE_CLOSE ||
sar->sar_dstate != SA_DSTATE_OPEN)
subagentx_log_sac_fatalx(sac,
"%s: unexpected region registration", __func__);
#endif
packetid = agentx_register(sa->sa_ax, 0, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), sar->sar_timeout, sar->sar_priority,
0, &(sar->sar_oid), 0);
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_REGISTER));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "region %s: opening",
agentx_oid2string(&(sar->sar_oid)));
sar->sar_cstate = SA_CSTATE_WAITOPEN;
return subagentx_request(sa, packetid, subagentx_region_finalize, sar);
}
static int
subagentx_region_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_region *sar = cookie;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct subagentx_index *sai;
struct subagentx_object *sao;
#ifdef AGENTX_DEBUG
if (sar->sar_cstate != SA_CSTATE_WAITOPEN)
subagentx_log_sac_fatalx(sac, "%s: not expecting region open",
__func__);
#endif
if (pdu->ap_payload.ap_response.ap_error == AGENTX_PDU_ERROR_NOERROR) {
sar->sar_cstate = SA_CSTATE_OPEN;
subagentx_log_sac_info(sac, "region %s: open",
agentx_oid2string(&(sar->sar_oid)));
} else if (pdu->ap_payload.ap_response.ap_error ==
AGENTX_PDU_ERROR_DUPLICATEREGISTRATION) {
sar->sar_cstate = SA_CSTATE_CLOSE;
/* Try at lower priority: first come first serve */
if ((++sar->sar_priority) != 0) {
subagentx_log_sac_warnx(sac, "region %s: duplicate, "
"reducing priority",
agentx_oid2string(&(sar->sar_oid)));
return subagentx_region_start(sar);
}
subagentx_log_sac_info(sac, "region %s: duplicate, can't "
"reduce priority, ignoring",
agentx_oid2string(&(sar->sar_oid)));
} else if (pdu->ap_payload.ap_response.ap_error ==
AGENTX_PDU_ERROR_REQUESTDENIED) {
sar->sar_cstate = SA_CSTATE_CLOSE;
subagentx_log_sac_warnx(sac, "region %s: %s",
agentx_oid2string(&(sar->sar_oid)),
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
/*
* If we can't register a region, related objects are useless.
* But no need to retry.
*/
return 0;
} else {
subagentx_log_sac_info(sac, "region %s: %s",
agentx_oid2string(&(sar->sar_oid)),
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
subagentx_reset(sa);
return -1;
}
if (sar->sar_dstate == SA_DSTATE_CLOSE) {
if (subagentx_region_close(sar) == -1)
return -1;
} else {
TAILQ_FOREACH(sai, &(sar->sar_indices), sai_sar_indices) {
if (subagentx_index_start(sai) == -1)
return -1;
}
TAILQ_FOREACH(sao, &(sar->sar_objects), sao_sar_objects) {
if (subagentx_object_start(sao) == -1)
return -1;
}
}
return 0;
}
static int
subagentx_region_close(struct subagentx_region *sar)
{
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (sar->sar_cstate != SA_CSTATE_OPEN)
subagentx_log_sac_fatalx(sac, "%s: unexpected region close",
__func__);
#endif
sar->sar_cstate = SA_CSTATE_WAITCLOSE;
if (sas->sas_cstate == SA_CSTATE_WAITCLOSE)
return 0;
packetid = agentx_unregister(sa->sa_ax, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), sar->sar_priority, 0, &(sar->sar_oid),
0);
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_UNREGISTER));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "region %s: closing",
agentx_oid2string(&(sar->sar_oid)));
return subagentx_request(sa, packetid, subagentx_region_close_finalize,
sar);
}
static int
subagentx_region_close_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_region *sar = cookie;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
#ifdef AGENTX_DEBUG
if (sar->sar_cstate != SA_CSTATE_WAITCLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected region close",
__func__);
#endif
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
subagentx_log_sac_warnx(sac, "closing %s: %s",
agentx_oid2string(&(sar->sar_oid)),
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
subagentx_reset(sa);
return -1;
}
sar->sar_priority = AGENTX_PRIORITY_DEFAULT;
sar->sar_cstate = SA_CSTATE_CLOSE;
subagentx_log_sac_info(sac, "region %s: closed",
agentx_oid2string(&(sar->sar_oid)));
if (sar->sar_dstate == SA_DSTATE_CLOSE) {
subagentx_region_free_finalize(sar);
return 0;
} else {
if (sac->sac_cstate == SA_CSTATE_OPEN) {
if (subagentx_region_start(sar) == -1)
return -1;
}
}
return 0;
}
void
subagentx_region_free(struct subagentx_region *sar)
{
struct subagentx_index *sai, *tsai;
struct subagentx_object *sao, *tsao;
if (sar == NULL)
return;
if (sar->sar_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sar->sar_sac, "%s: double free",
__func__);
sar->sar_dstate = SA_DSTATE_CLOSE;
TAILQ_FOREACH_SAFE(sai, &(sar->sar_indices), sai_sar_indices, tsai) {
if (sai->sai_dstate != SA_DSTATE_CLOSE)
subagentx_index_free(sai);
}
TAILQ_FOREACH_SAFE(sao, &(sar->sar_objects), sao_sar_objects, tsao) {
if (sao->sao_dstate != SA_DSTATE_CLOSE)
subagentx_object_free(sao);
}
if (sar->sar_cstate == SA_CSTATE_OPEN) {
if (subagentx_region_close(sar) == -1)
return;
}
if (sar->sar_cstate == SA_CSTATE_CLOSE)
subagentx_region_free_finalize(sar);
}
static void
subagentx_region_free_finalize(struct subagentx_region *sar)
{
struct subagentx_context *sac = sar->sar_sac;
#ifdef AGENTX_DEBUG
if (sar->sar_dstate != SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected free", __func__);
#endif
if (!TAILQ_EMPTY(&(sar->sar_indices)) ||
!TAILQ_EMPTY(&(sar->sar_objects)))
return;
if (sar->sar_cstate != SA_CSTATE_CLOSE)
return;
TAILQ_REMOVE(&(sac->sac_regions), sar, sar_sac_regions);
free(sar);
if (sac->sac_dstate == SA_DSTATE_CLOSE)
subagentx_context_free_finalize(sac);
}
static void
subagentx_region_reset(struct subagentx_region *sar)
{
struct subagentx_index *sai, *tsai;
struct subagentx_object *sao, *tsao;
sar->sar_cstate = SA_CSTATE_CLOSE;
sar->sar_priority = AGENTX_PRIORITY_DEFAULT;
TAILQ_FOREACH_SAFE(sai, &(sar->sar_indices), sai_sar_indices, tsai)
subagentx_index_reset(sai);
TAILQ_FOREACH_SAFE(sao, &(sar->sar_objects), sao_sar_objects, tsao)
subagentx_object_reset(sao);
if (sar->sar_dstate == SA_DSTATE_CLOSE)
subagentx_region_free_finalize(sar);
}
struct subagentx_index *
subagentx_index_integer_new(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_INTEGER;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_uint32 = 0;
return subagentx_index(sar, &vb, SAI_TYPE_NEW);
}
struct subagentx_index *
subagentx_index_integer_any(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_INTEGER;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_uint32 = 0;
return subagentx_index(sar, &vb, SAI_TYPE_ANY);
}
struct subagentx_index *
subagentx_index_integer_value(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen, uint32_t value)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_INTEGER;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_uint32 = value;
return subagentx_index(sar, &vb, SAI_TYPE_VALUE);
}
struct subagentx_index *
subagentx_index_integer_dynamic(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_INTEGER;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
return subagentx_index(sar, &vb, SAI_TYPE_DYNAMIC);
}
struct subagentx_index *
subagentx_index_string_dynamic(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_OCTETSTRING;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_ostring.aos_slen = 0;
vb.avb_data.avb_ostring.aos_string = NULL;
return subagentx_index(sar, &vb, SAI_TYPE_DYNAMIC);
}
struct subagentx_index *
subagentx_index_nstring_dynamic(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen, size_t vlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
if (vlen == 0 || vlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: invalid string "
"length: %zu\n", __func__, vlen);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: invalid string "
"length: %zu\n", __func__, vlen);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_OCTETSTRING;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_ostring.aos_slen = vlen;
vb.avb_data.avb_ostring.aos_string = NULL;
return subagentx_index(sar, &vb, SAI_TYPE_DYNAMIC);
}
struct subagentx_index *
subagentx_index_oid_dynamic(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_OID;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_oid.aoi_idlen = 0;
return subagentx_index(sar, &vb, SAI_TYPE_DYNAMIC);
}
struct subagentx_index *
subagentx_index_noid_dynamic(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen, size_t vlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
if (vlen == 0 || vlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: invalid string "
"length: %zu\n", __func__, vlen);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: invalid string "
"length: %zu\n", __func__, vlen);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_OID;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_oid.aoi_idlen = oidlen;
vb.avb_data.avb_oid.aoi_idlen = vlen;
return subagentx_index(sar, &vb, SAI_TYPE_DYNAMIC);
}
struct subagentx_index *
subagentx_index_ipaddress_dynamic(struct subagentx_region *sar, uint32_t oid[],
size_t oidlen)
{
struct agentx_varbind vb;
size_t i;
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
vb.avb_type = AGENTX_DATA_TYPE_IPADDRESS;
for (i = 0; i < oidlen; i++)
vb.avb_oid.aoi_id[i] = oid[i];
vb.avb_data.avb_ostring.aos_string = NULL;
vb.avb_oid.aoi_idlen = oidlen;
return subagentx_index(sar, &vb, SAI_TYPE_DYNAMIC);
}
static struct subagentx_index *
subagentx_index(struct subagentx_region *sar, struct agentx_varbind *vb,
enum subagentx_index_type type)
{
struct subagentx_index *sai;
if (sar->sar_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sar->sar_sac, "%s: use after free",
__func__);
if (agentx_oid_cmp(&(sar->sar_oid), &(vb->avb_oid)) != -2) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oid is not child "
"of region %s", __func__,
agentx_oid2string(&(vb->avb_oid)));
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oid is not child of "
"region %s", __func__, agentx_oid2string(&(vb->avb_oid)));
errno = EINVAL;
return NULL;
#endif
}
if ((sai = calloc(1, sizeof(*sai))) == NULL)
return NULL;
sai->sai_sar = sar;
sai->sai_type = type;
bcopy(vb, &(sai->sai_vb), sizeof(*vb));
sai->sai_cstate = SA_CSTATE_CLOSE;
sai->sai_dstate = SA_DSTATE_OPEN;
TAILQ_INSERT_HEAD(&(sar->sar_indices), sai, sai_sar_indices);
if (sar->sar_cstate == SA_CSTATE_OPEN)
subagentx_index_start(sai);
return sai;
}
static int
subagentx_index_start(struct subagentx_index *sai)
{
struct subagentx_region *sar = sai->sai_sar;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
int flags = 0;
#ifdef AGENTX_DEBUG
if (sar->sar_cstate != SA_CSTATE_OPEN ||
sai->sai_cstate != SA_CSTATE_CLOSE ||
sai->sai_dstate != SA_DSTATE_OPEN)
subagentx_log_sac_fatalx(sac, "%s: unexpected index allocation",
__func__);
#endif
sai->sai_cstate = SA_CSTATE_WAITOPEN;
if (sai->sai_type == SAI_TYPE_NEW)
flags = AGENTX_PDU_FLAG_NEW_INDEX;
else if (sai->sai_type == SAI_TYPE_ANY)
flags = AGENTX_PDU_FLAG_ANY_INDEX;
else if (sai->sai_type == SAI_TYPE_DYNAMIC) {
subagentx_index_finalize(NULL, sai);
return 0;
}
/* We might be able to bundle, but if we fail we'd have to reorganise */
packetid = agentx_indexallocate(sa->sa_ax, flags, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), &(sai->sai_vb), 1);
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_INDEXDEALLOCATE));
subagentx_reset(sa);
return -1;
}
if (sai->sai_type == SAI_TYPE_VALUE)
subagentx_log_sac_info(sac, "index %s: allocating '%u'",
agentx_oid2string(&(sai->sai_vb.avb_oid)),
sai->sai_vb.avb_data.avb_uint32);
else if (sai->sai_type == SAI_TYPE_ANY)
subagentx_log_sac_info(sac, "index %s: allocating any index",
agentx_oid2string(&(sai->sai_vb.avb_oid)));
else if (sai->sai_type == SAI_TYPE_NEW)
subagentx_log_sac_info(sac, "index %s: allocating new index",
agentx_oid2string(&(sai->sai_vb.avb_oid)));
return subagentx_request(sa, packetid, subagentx_index_finalize, sai);
}
static int
subagentx_index_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_index *sai = cookie;
struct subagentx_region *sar = sai->sai_sar;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct agentx_pdu_response *resp;
size_t i;
#ifdef AGENTX_DEBUG
if (sai->sai_cstate != SA_CSTATE_WAITOPEN)
subagentx_log_sac_fatalx(sac,
"%s: not expecting index allocate", __func__);
#endif
if (sai->sai_type == SAI_TYPE_DYNAMIC) {
sai->sai_cstate = SA_CSTATE_OPEN;
return 0;
}
resp = &(pdu->ap_payload.ap_response);
if (resp->ap_error != AGENTX_PDU_ERROR_NOERROR) {
sai->sai_cstate = SA_CSTATE_CLOSE;
subagentx_log_sac_warnx(sac, "index %s: %s",
agentx_oid2string(&(sar->sar_oid)),
agentx_error2string(resp->ap_error));
return 0;
}
sai->sai_cstate = SA_CSTATE_OPEN;
if (resp->ap_nvarbind != 1) {
subagentx_log_sac_warnx(sac, "index %s: unexpected number of "
"indices", agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
if (resp->ap_varbindlist[0].avb_type != sai->sai_vb.avb_type) {
subagentx_log_sac_warnx(sac, "index %s: unexpected index type",
agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
if (agentx_oid_cmp(&(resp->ap_varbindlist[0].avb_oid),
&(sai->sai_vb.avb_oid)) != 0) {
subagentx_log_sac_warnx(sac, "index %s: unexpected oid",
agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
switch (sai->sai_vb.avb_type) {
case AGENTX_DATA_TYPE_INTEGER:
if (sai->sai_type == SAI_TYPE_NEW ||
sai->sai_type == SAI_TYPE_ANY)
sai->sai_vb.avb_data.avb_uint32 =
resp->ap_varbindlist[0].avb_data.avb_uint32;
else if (sai->sai_vb.avb_data.avb_uint32 !=
resp->ap_varbindlist[0].avb_data.avb_uint32) {
subagentx_log_sac_warnx(sac, "index %s: unexpected "
"index value", agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "index %s: allocated '%u'",
agentx_oid2string(&(sai->sai_vb.avb_oid)),
sai->sai_vb.avb_data.avb_uint32);
break;
default:
subagentx_log_sac_fatalx(sac, "%s: Unsupported index type",
__func__);
}
if (sai->sai_dstate == SA_DSTATE_CLOSE)
return subagentx_index_close(sai);
/* TODO Make use of range_subid register */
for (i = 0; i < sai->sai_objectlen; i++) {
if (sai->sai_object[i]->sao_dstate == SA_DSTATE_OPEN) {
if (subagentx_object_start(sai->sai_object[i]) == -1)
return -1;
}
}
return 0;
}
void
subagentx_index_free(struct subagentx_index *sai)
{
size_t i;
if (sai == NULL)
return;
if (sai->sai_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sai->sai_sar->sar_sac,
"%s: double free", __func__);
sai->sai_dstate = SA_DSTATE_CLOSE;
/* TODO Do a range_subid unregister before freeing */
for (i = 0; i < sai->sai_objectlen; i++) {
if (sai->sai_object[i]->sao_dstate != SA_DSTATE_CLOSE)
subagentx_object_free(sai->sai_object[i]);
}
if (sai->sai_cstate == SA_CSTATE_OPEN)
(void) subagentx_index_close(sai);
else if (sai->sai_cstate == SA_CSTATE_CLOSE)
subagentx_index_free_finalize(sai);
}
static void
subagentx_index_free_finalize(struct subagentx_index *sai)
{
struct subagentx_region *sar = sai->sai_sar;
#ifdef AGENTX_DEBUG
if (sai->sai_dstate != SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sar->sar_sac, "%s: unexpected free",
__func__);
if (sai->sai_cstate != SA_CSTATE_CLOSE)
subagentx_log_sac_fatalx(sar->sar_sac,
"%s: free without deallocating", __func__);
#endif
if (sai->sai_objectlen != 0)
return;
TAILQ_REMOVE(&(sar->sar_indices), sai, sai_sar_indices);
agentx_varbind_free(&(sai->sai_vb));
free(sai->sai_object);
free(sai);
if (sar->sar_dstate == SA_DSTATE_CLOSE)
subagentx_region_free_finalize(sar);
}
static void
subagentx_index_reset(struct subagentx_index *sai)
{
sai->sai_cstate = SA_CSTATE_CLOSE;
if (sai->sai_dstate == SA_DSTATE_CLOSE)
subagentx_index_free_finalize(sai);
}
static int
subagentx_index_close(struct subagentx_index *sai)
{
struct subagentx_region *sar = sai->sai_sar;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
uint32_t packetid;
#ifdef AGENTX_DEBUG
if (sai->sai_cstate != SA_CSTATE_OPEN)
subagentx_log_sac_fatalx(sac,
"%s: unexpected index deallocation", __func__);
#endif
sai->sai_cstate = SA_CSTATE_WAITCLOSE;
if (sas->sas_cstate == SA_CSTATE_WAITCLOSE)
return 0;
/* We might be able to bundle, but if we fail we'd have to reorganise */
packetid = agentx_indexdeallocate(sa->sa_ax, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), &(sai->sai_vb), 1);
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_INDEXDEALLOCATE));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "index %s: deallocating",
agentx_oid2string(&(sai->sai_vb.avb_oid)));
return subagentx_request(sa, packetid, subagentx_index_close_finalize,
sai);
}
static int
subagentx_index_close_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_index *sai = cookie;
struct subagentx_region *sar = sai->sai_sar;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct agentx_pdu_response *resp = &(pdu->ap_payload.ap_response);
#ifdef AGENTX_DEBUG
if (sai->sai_cstate != SA_CSTATE_WAITCLOSE)
subagentx_log_sac_fatalx(sac, "%s: unexpected indexdeallocate",
__func__);
#endif
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
subagentx_log_sac_warnx(sac,
"index %s: couldn't deallocate: %s",
agentx_oid2string(&(sai->sai_vb.avb_oid)),
agentx_error2string(resp->ap_error));
subagentx_reset(sa);
return -1;
}
if (resp->ap_nvarbind != 1) {
subagentx_log_sac_warnx(sac,
"index %s: unexpected number of indices",
agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
if (resp->ap_varbindlist[0].avb_type != sai->sai_vb.avb_type) {
subagentx_log_sac_warnx(sac, "index %s: unexpected index type",
agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
if (agentx_oid_cmp(&(resp->ap_varbindlist[0].avb_oid),
&(sai->sai_vb.avb_oid)) != 0) {
subagentx_log_sac_warnx(sac, "index %s: unexpected oid",
agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
switch (sai->sai_vb.avb_type) {
case AGENTX_DATA_TYPE_INTEGER:
if (sai->sai_vb.avb_data.avb_uint32 !=
resp->ap_varbindlist[0].avb_data.avb_uint32) {
subagentx_log_sac_warnx(sac,
"index %s: unexpected index value",
agentx_oid2string(&(sar->sar_oid)));
subagentx_reset(sa);
return -1;
}
break;
default:
subagentx_log_sac_fatalx(sac, "%s: Unsupported index type",
__func__);
}
sai->sai_cstate = SA_CSTATE_CLOSE;
subagentx_log_sac_info(sac, "index %s: deallocated",
agentx_oid2string(&(sai->sai_vb.avb_oid)));
if (sai->sai_dstate == SA_DSTATE_CLOSE) {
subagentx_index_free_finalize(sai);
} else if (sar->sar_cstate == SA_CSTATE_OPEN) {
if (subagentx_index_start(sai) == -1)
return -1;
}
return 0;
}
struct subagentx_object *
subagentx_object(struct subagentx_region *sar, uint32_t oid[], size_t oidlen,
struct subagentx_index *sai[], size_t sailen, int implied,
void (*get)(struct subagentx_varbind *))
{
struct subagentx_object *sao, **tsao, sao_search;
struct subagentx_index *lsai;
int ready = 1;
size_t i, j;
if (sar->sar_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sar->sar_sac, "%s: use after free",
__func__);
if (oidlen < 1) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen == 0",
__func__);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen == 0",
__func__);
errno = EINVAL;
return NULL;
#endif
}
if (oidlen > SUBAGENTX_OID_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: oidlen > %d",
__func__, SUBAGENTX_OID_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
if (sailen > SUBAGENTX_OID_INDEX_MAX_LEN) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: indexlen > %d",
__func__, SUBAGENTX_OID_INDEX_MAX_LEN);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: indexlen > %d",
__func__, SUBAGENTX_OID_INDEX_MAX_LEN);
errno = EINVAL;
return NULL;
#endif
}
for (i = 0; i < oidlen; i++)
sao_search.sao_oid.aoi_id[i] = oid[i];
sao_search.sao_oid.aoi_idlen = oidlen;
do {
if (RB_FIND(sac_objects, &(sar->sar_sac->sac_objects),
&sao_search) != NULL) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: invalid "
"parent child object relationship", __func__);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: invalid "
"parent child object relationship", __func__);
errno = EINVAL;
return NULL;
#endif
}
sao_search.sao_oid.aoi_idlen--;
} while (sao_search.sao_oid.aoi_idlen > 0);
sao_search.sao_oid.aoi_idlen = oidlen;
sao = RB_NFIND(sac_objects, &(sar->sar_sac->sac_objects), &sao_search);
if (sao != NULL &&
agentx_oid_cmp(&(sao->sao_oid), &(sao_search.sao_oid)) == 2) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: invalid parent "
"child object relationship", __func__);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: invalid parent "
"child object relationship", __func__);
errno = EINVAL;
return NULL;
#endif
}
if (implied == 1) {
lsai = sai[sailen - 1];
if (lsai->sai_vb.avb_type == AGENTX_DATA_TYPE_OCTETSTRING) {
if (lsai->sai_vb.avb_data.avb_ostring.aos_slen != 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac,
"%s: implied can only be used on strings "
"of dynamic length", __func__);
#else
subagentx_log_sac_warnx(sar->sar_sac,
"%s: implied can only be used on strings "
"of dynamic length", __func__);
errno = EINVAL;
return NULL;
#endif
}
} else if (lsai->sai_vb.avb_type == AGENTX_DATA_TYPE_OID) {
if (lsai->sai_vb.avb_data.avb_oid.aoi_idlen != 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac,
"%s: implied can only be used on oids of "
"dynamic length", __func__);
#else
subagentx_log_sac_warnx(sar->sar_sac,
"%s: implied can only be used on oids of "
"dynamic length", __func__);
errno = EINVAL;
return NULL;
#endif
}
} else {
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sar->sar_sac, "%s: implied "
"can only be set on oid and string indices",
__func__);
#else
subagentx_log_sac_warnx(sar->sar_sac, "%s: implied can "
"only be set on oid and string indices", __func__);
errno = EINVAL;
return NULL;
#endif
}
}
ready = sar->sar_cstate == SA_CSTATE_OPEN;
if ((sao = calloc(1, sizeof(*sao))) == NULL)
return NULL;
sao->sao_sar = sar;
bcopy(&(sao_search.sao_oid), &(sao->sao_oid), sizeof(sao->sao_oid));
for (i = 0; i < sailen; i++) {
sao->sao_index[i] = sai[i];
if (sai[i]->sai_objectlen == sai[i]->sai_objectsize) {
tsao = recallocarray(sai[i]->sai_object,
sai[i]->sai_objectlen, sai[i]->sai_objectlen + 1,
sizeof(*sai[i]->sai_object));
if (tsao == NULL) {
free(sao);
return NULL;
}
sai[i]->sai_object = tsao;
sai[i]->sai_objectsize = sai[i]->sai_objectlen + 1;
}
for (j = 0; j < sai[i]->sai_objectlen; j++) {
if (agentx_oid_cmp(&(sao->sao_oid),
&(sai[i]->sai_object[j]->sao_oid)) < 0) {
memmove(&(sai[i]->sai_object[j + 1]),
&(sai[i]->sai_object[j]),
sizeof(*(sai[i]->sai_object)) *
(sai[i]->sai_objectlen - j));
break;
}
}
sai[i]->sai_object[j] = sao;
sai[i]->sai_objectlen++;
if (sai[i]->sai_cstate != SA_CSTATE_OPEN)
ready = 0;
}
sao->sao_indexlen = sailen;
sao->sao_implied = implied;
sao->sao_timeout = 0;
sao->sao_lock = 0;
sao->sao_get = get;
sao->sao_cstate = SA_CSTATE_CLOSE;
sao->sao_dstate = SA_DSTATE_OPEN;
TAILQ_INSERT_TAIL(&(sar->sar_objects), sao, sao_sar_objects);
RB_INSERT(sac_objects, &(sar->sar_sac->sac_objects), sao);
if (ready)
subagentx_object_start(sao);
return sao;
}
static int
subagentx_object_start(struct subagentx_object *sao)
{
struct subagentx_region *sar = sao->sao_sar;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct agentx_oid oid;
char oids[1024];
size_t i;
int needregister = 0;
uint32_t packetid;
uint8_t flags = AGENTX_PDU_FLAG_INSTANCE_REGISTRATION;
#ifdef AGENTX_DEBUG
if (sar->sar_cstate != SA_CSTATE_OPEN ||
sao->sao_cstate != SA_CSTATE_CLOSE ||
sao->sao_dstate != SA_DSTATE_OPEN)
subagentx_log_sac_fatalx(sac,
"%s: unexpected object registration", __func__);
#endif
if (sao->sao_timeout != 0)
needregister = 1;
for (i = 0; i < sao->sao_indexlen; i++) {
if (sao->sao_index[i]->sai_cstate != SA_CSTATE_OPEN)
return 0;
if (sao->sao_index[i]->sai_type != SAI_TYPE_DYNAMIC)
needregister = 1;
}
if (!needregister) {
sao->sao_cstate = SA_CSTATE_WAITOPEN;
subagentx_object_finalize(NULL, sao);
return 0;
}
bcopy(&(sao->sao_oid), &(oid), sizeof(oid));
for (i = 0; i < sao->sao_indexlen; i++) {
if (sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC) {
flags = 0;
break;
}
#ifdef AGENTX_DEBUG
if (sao->sao_index[i]->sai_vb.avb_type !=
AGENTX_DATA_TYPE_INTEGER)
subagentx_log_sac_fatalx(sac,
"%s: Unsupported allocated index type", __func__);
#endif
oid.aoi_id[oid.aoi_idlen++] =
sao->sao_index[i]->sai_vb.avb_data.avb_uint32;
}
packetid = agentx_register(sa->sa_ax, flags, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), sao->sao_timeout,
AGENTX_PRIORITY_DEFAULT, 0, &oid, 0);
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_REGISTER));
subagentx_reset(sa);
return -1;
}
strlcpy(oids, agentx_oid2string(&(sao->sao_oid)), sizeof(oids));
subagentx_log_sac_info(sac, "object %s (%s %s): opening",
oids, flags ? "instance" : "region", agentx_oid2string(&(oid)));
sao->sao_cstate = SA_CSTATE_WAITOPEN;
return subagentx_request(sa, packetid, subagentx_object_finalize, sao);
}
static int
subagentx_object_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_object *sao = cookie;
struct subagentx_context *sac = sao->sao_sar->sar_sac;
struct agentx_oid oid;
char oids[1024];
size_t i;
uint8_t flags = 1;
#ifdef AGENTX_DEBUG
if (sao->sao_cstate != SA_CSTATE_WAITOPEN)
subagentx_log_sac_fatalx(sac, "%s: not expecting object open",
__func__);
#endif
if (pdu == NULL) {
sao->sao_cstate = SA_CSTATE_OPEN;
return 0;
}
bcopy(&(sao->sao_oid), &oid, sizeof(oid));
for (i = 0; i < sao->sao_indexlen; i++) {
if (sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC) {
flags = 0;
break;
}
#ifdef AGENTX_DEBUG
if (sao->sao_index[i]->sai_vb.avb_type !=
AGENTX_DATA_TYPE_INTEGER)
subagentx_log_sac_fatalx(sac,
"%s: Unsupported allocated index type", __func__);
#endif
oid.aoi_id[oid.aoi_idlen++] =
sao->sao_index[i]->sai_vb.avb_data.avb_uint32;
}
strlcpy(oids, agentx_oid2string(&(sao->sao_oid)), sizeof(oids));
/*
* We should only be here for table objects with registered indices.
* If we fail here something is misconfigured and the admin should fix
* it.
*/
if (pdu->ap_payload.ap_response.ap_error != AGENTX_PDU_ERROR_NOERROR) {
sao->sao_cstate = SA_CSTATE_CLOSE;
subagentx_log_sac_info(sac, "object %s (%s %s): %s",
oids, flags ? "instance" : "region", agentx_oid2string(&oid),
agentx_error2string(pdu->ap_payload.ap_response.ap_error));
if (sao->sao_dstate == SA_DSTATE_CLOSE)
return subagentx_object_close_finalize(NULL, sao);
return 0;
}
sao->sao_cstate = SA_CSTATE_OPEN;
subagentx_log_sac_info(sac, "object %s (%s %s): open", oids,
flags ? "instance" : "region", agentx_oid2string(&oid));
if (sao->sao_dstate == SA_DSTATE_CLOSE)
return subagentx_object_close(sao);
return 0;
}
static int
subagentx_object_lock(struct subagentx_object *sao)
{
if (sao->sao_lock == UINT32_MAX) {
subagentx_log_sac_warnx(sao->sao_sar->sar_sac,
"%s: sao_lock == %u", __func__, UINT32_MAX);
return -1;
}
sao->sao_lock++;
return 0;
}
static void
subagentx_object_unlock(struct subagentx_object *sao)
{
#ifdef AGENTX_DEBUG
if (sao->sao_lock == 0)
subagentx_log_sac_fatalx(sao->sao_sar->sar_sac,
"%s: sao_lock == 0", __func__);
#endif
sao->sao_lock--;
if (sao->sao_lock == 0 && sao->sao_dstate == SA_DSTATE_CLOSE &&
sao->sao_cstate == SA_CSTATE_CLOSE)
subagentx_object_free_finalize(sao);
}
static int
subagentx_object_close(struct subagentx_object *sao)
{
struct subagentx_context *sac = sao->sao_sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct agentx_oid oid;
char oids[1024];
size_t i;
int needclose = 0;
uint32_t packetid;
uint8_t flags = 1;
#ifdef AGENTX_DEBUG
if (sao->sao_cstate != SA_CSTATE_OPEN)
subagentx_log_sac_fatalx(sac, "%s: unexpected object close",
__func__);
#endif
for (i = 0; i < sao->sao_indexlen; i++) {
#ifdef AGENTX_DEBUG
if (sao->sao_index[i]->sai_cstate != SA_CSTATE_OPEN)
subagentx_log_sac_fatalx(sac,
"%s: Object open while index closed", __func__);
#endif
if (sao->sao_index[i]->sai_type != SAI_TYPE_DYNAMIC)
needclose = 1;
}
sao->sao_cstate = SA_CSTATE_WAITCLOSE;
if (sas->sas_cstate == SA_CSTATE_WAITCLOSE)
return 0;
if (!needclose) {
subagentx_object_close_finalize(NULL, sao);
return 0;
}
bcopy(&(sao->sao_oid), &(oid), sizeof(oid));
for (i = 0; i < sao->sao_indexlen; i++) {
if (sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC) {
flags = 0;
break;
}
#ifdef AGENTX_DEBUG
if (sao->sao_index[i]->sai_vb.avb_type !=
AGENTX_DATA_TYPE_INTEGER)
subagentx_log_sac_fatalx(sac,
"%s: Unsupported allocated index type", __func__);
#endif
oid.aoi_id[oid.aoi_idlen++] =
sao->sao_index[i]->sai_vb.avb_data.avb_uint32;
}
packetid = agentx_unregister(sa->sa_ax, sas->sas_id,
SUBAGENTX_CONTEXT_CTX(sac), AGENTX_PRIORITY_DEFAULT, 0, &oid, 0);
if (packetid == 0) {
subagentx_log_sac_warn(sac, "couldn't generate %s",
agentx_pdutype2string(AGENTX_PDU_TYPE_UNREGISTER));
subagentx_reset(sa);
return -1;
}
strlcpy(oids, agentx_oid2string(&(sao->sao_oid)), sizeof(oids));
subagentx_log_sac_info(sac, "object %s (%s %s): closing",
oids, flags ? "instance" : "region", agentx_oid2string(&(oid)));
return subagentx_request(sa, packetid, subagentx_object_close_finalize,
sao);
}
static int
subagentx_object_close_finalize(struct agentx_pdu *pdu, void *cookie)
{
struct subagentx_object *sao = cookie;
struct subagentx_region *sar = sao->sao_sar;
struct subagentx_context *sac = sar->sar_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct agentx_oid oid;
char oids[1024];
uint8_t flags = 1;
size_t i;
#ifdef AGENTX_DEBUG
if (sao->sao_cstate != SA_CSTATE_WAITCLOSE)
subagentx_log_sac_fatalx(sac,
"%s: unexpected object unregister", __func__);
#endif
if (pdu != NULL) {
bcopy(&(sao->sao_oid), &(oid), sizeof(oid));
for (i = 0; i < sao->sao_indexlen; i++) {
if (sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC) {
flags = 0;
break;
}
#ifdef AGENTX_DEBUG
if (sao->sao_index[i]->sai_vb.avb_type !=
AGENTX_DATA_TYPE_INTEGER)
subagentx_log_sac_fatalx(sac,
"%s: Unsupported allocated index type",
__func__);
#endif
oid.aoi_id[oid.aoi_idlen++] =
sao->sao_index[i]->sai_vb.avb_data.avb_uint32;
}
strlcpy(oids, agentx_oid2string(&(sao->sao_oid)), sizeof(oids));
if (pdu->ap_payload.ap_response.ap_error !=
AGENTX_PDU_ERROR_NOERROR) {
subagentx_log_sac_warnx(sac,
"closing object %s (%s %s): %s", oids,
flags ? "instance" : "region",
agentx_oid2string(&oid), agentx_error2string(
pdu->ap_payload.ap_response.ap_error));
subagentx_reset(sa);
return -1;
}
subagentx_log_sac_info(sac, "object %s (%s %s): closed", oids,
flags ? "instance" : "region", agentx_oid2string(&oid));
}
if (sao->sao_dstate == SA_DSTATE_CLOSE)
subagentx_object_free_finalize(sao);
else {
if (sar->sar_cstate == SA_CSTATE_OPEN)
if (subagentx_object_start(sao) == -1)
return -1;
}
return 0;
}
void
subagentx_object_free(struct subagentx_object *sao)
{
if (sao == NULL)
return;
if (sao->sao_dstate == SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sao->sao_sar->sar_sac,
"%s: double free", __func__);
sao->sao_dstate = SA_DSTATE_CLOSE;
if (sao->sao_cstate == SA_CSTATE_OPEN) {
if (subagentx_object_close(sao) == -1)
return;
}
if (sao->sao_cstate == SA_CSTATE_CLOSE)
subagentx_object_free_finalize(sao);
}
static void
subagentx_object_free_finalize(struct subagentx_object *sao)
{
#ifdef AGENTX_DEBUG
struct subagentx *sa = sao->sao_sar->sar_sac->sac_sas->sas_sa;
#endif
size_t i, j;
int found;
#ifdef AGENTX_DEBUG
if (sao->sao_dstate != SA_DSTATE_CLOSE)
subagentx_log_sac_fatalx(sao->sao_sar->sar_sac,
"%s: unexpected free", __func__);
#endif
if (sao->sao_lock != 0) {
#ifdef AGENTX_DEBUG
if (TAILQ_EMPTY(&(sa->sa_getreqs)))
subagentx_log_sac_fatalx(sao->sao_sar->sar_sac,
"%s: %s sao_lock == %u", __func__,
agentx_oid2string(&(sao->sao_oid)), sao->sao_lock);
#endif
return;
}
RB_REMOVE(sac_objects, &(sao->sao_sar->sar_sac->sac_objects), sao);
TAILQ_REMOVE(&(sao->sao_sar->sar_objects), sao, sao_sar_objects);
for (i = 0; i < sao->sao_indexlen; i++) {
found = 0;
for (j = 0; j < sao->sao_index[i]->sai_objectlen; j++) {
if (sao->sao_index[i]->sai_object[j] == sao)
found = 1;
if (found && j + 1 != sao->sao_index[i]->sai_objectlen)
sao->sao_index[i]->sai_object[j] =
sao->sao_index[i]->sai_object[j + 1];
}
#ifdef AGENTX_DEBUG
if (!found)
subagentx_log_sac_fatalx(sao->sao_sar->sar_sac,
"%s: object not found in index", __func__);
#endif
sao->sao_index[i]->sai_objectlen--;
if (sao->sao_index[i]->sai_dstate == SA_DSTATE_CLOSE)
subagentx_index_free_finalize(sao->sao_index[i]);
}
free(sao);
}
static void
subagentx_object_reset(struct subagentx_object *sao)
{
sao->sao_cstate = SA_CSTATE_CLOSE;
if (sao->sao_dstate == SA_DSTATE_CLOSE)
subagentx_object_free_finalize(sao);
}
static int
subagentx_object_cmp(struct subagentx_object *o1, struct subagentx_object *o2)
{
return agentx_oid_cmp(&(o1->sao_oid), &(o2->sao_oid));
}
static int
subagentx_object_implied(struct subagentx_object *sao,
struct subagentx_index *sai)
{
size_t i = 0;
for (i = 0; i < sao->sao_indexlen; i++) {
if (sao->sao_index[i] == sai) {
if (sai->sai_vb.avb_data.avb_ostring.aos_slen != 0)
return 1;
else if (i == sao->sao_indexlen - 1)
return sao->sao_implied;
return 0;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sac_fatalx(sao->sao_sar->sar_sac, "%s: unsupported index",
__func__);
#endif
return 0;
}
static void
subagentx_get_start(struct subagentx_context *sac, struct agentx_pdu *pdu)
{
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
struct subagentx_get *sag, tsag;
struct agentx_pdu_searchrangelist *srl;
char *logmsg = NULL;
size_t i, j;
int fail = 0;
if ((sag = calloc(1, sizeof(*sag))) == NULL) {
tsag.sag_sessionid = pdu->ap_header.aph_sessionid;
tsag.sag_transactionid = pdu->ap_header.aph_transactionid;
tsag.sag_packetid = pdu->ap_header.aph_packetid;
tsag.sag_context_default = sac->sac_name_default;
tsag.sag_fd = sac->sac_sas->sas_sa->sa_fd;
subagentx_log_sag_warn(&tsag, "Couldn't parse request");
subagentx_reset(sa);
return;
}
sag->sag_sessionid = pdu->ap_header.aph_sessionid;
sag->sag_transactionid = pdu->ap_header.aph_transactionid;
sag->sag_packetid = pdu->ap_header.aph_packetid;
sag->sag_context_default = sac->sac_name_default;
sag->sag_fd = sac->sac_sas->sas_sa->sa_fd;
if (!sac->sac_name_default) {
sag->sag_context.aos_string =
(unsigned char *)strdup((char *)sac->sac_name.aos_string);
if (sag->sag_context.aos_string == NULL) {
subagentx_log_sag_warn(sag, "Couldn't parse request");
free(sag);
subagentx_reset(sa);
return;
}
}
sag->sag_context.aos_slen = sac->sac_name.aos_slen;
sag->sag_type = pdu->ap_header.aph_type;
sag->sag_sac = sac;
TAILQ_INSERT_TAIL(&(sa->sa_getreqs), sag, sag_sa_getreqs);
if (sag->sag_type == AGENTX_PDU_TYPE_GET ||
sag->sag_type == AGENTX_PDU_TYPE_GETNEXT) {
srl = &(pdu->ap_payload.ap_srl);
sag->sag_nvarbind = srl->ap_nsr;
} else {
sag->sag_nonrep = pdu->ap_payload.ap_getbulk.ap_nonrep;
sag->sag_maxrep = pdu->ap_payload.ap_getbulk.ap_maxrep;
srl = &(pdu->ap_payload.ap_getbulk.ap_srl);
sag->sag_nvarbind = ((srl->ap_nsr - sag->sag_nonrep) *
sag->sag_maxrep) + sag->sag_nonrep;
}
if ((sag->sag_varbind = calloc(sag->sag_nvarbind,
sizeof(*(sag->sag_varbind)))) == NULL) {
subagentx_log_sag_warn(sag, "Couldn't parse request");
subagentx_get_free(sag);
subagentx_reset(sa);
return;
}
/* XXX net-snmp doesn't use getbulk, so untested */
/* Two loops: varbind after needs to be initialized */
for (i = 0; i < srl->ap_nsr; i++) {
if (i < sag->sag_nonrep ||
sag->sag_type != AGENTX_PDU_TYPE_GETBULK)
j = i;
else if (sag->sag_maxrep == 0)
break;
else
j = (sag->sag_maxrep * i) + sag->sag_nonrep;
bcopy(&(srl->ap_sr[i].asr_start),
&(sag->sag_varbind[j].sav_vb.avb_oid),
sizeof(srl->ap_sr[i].asr_start));
bcopy(&(srl->ap_sr[i].asr_start),
&(sag->sag_varbind[j].sav_start),
sizeof(srl->ap_sr[i].asr_start));
bcopy(&(srl->ap_sr[i].asr_stop),
&(sag->sag_varbind[j].sav_end),
sizeof(srl->ap_sr[i].asr_stop));
sag->sag_varbind[j].sav_initialized = 1;
sag->sag_varbind[j].sav_sag = sag;
sag->sag_varbind[j].sav_include =
srl->ap_sr[i].asr_start.aoi_include;
if (j == 0)
fail |= subagentx_strcat(&logmsg, " {");
else
fail |= subagentx_strcat(&logmsg, ",{");
fail |= subagentx_strcat(&logmsg,
agentx_oid2string(&(srl->ap_sr[i].asr_start)));
if (srl->ap_sr[i].asr_start.aoi_include)
fail |= subagentx_strcat(&logmsg, " (inclusive)");
if (srl->ap_sr[i].asr_stop.aoi_idlen != 0) {
fail |= subagentx_strcat(&logmsg, " - ");
fail |= subagentx_strcat(&logmsg,
agentx_oid2string(&(srl->ap_sr[i].asr_stop)));
}
fail |= subagentx_strcat(&logmsg, "}");
if (fail) {
subagentx_log_sag_warn(sag, "Couldn't parse request");
free(logmsg);
subagentx_get_free(sag);
subagentx_reset(sa);
return;
}
}
subagentx_log_sag_debug(sag, "%s:%s",
agentx_pdutype2string(sag->sag_type), logmsg);
free(logmsg);
for (i = 0; i < srl->ap_nsr; i++) {
if (i < sag->sag_nonrep ||
sag->sag_type != AGENTX_PDU_TYPE_GETBULK)
j = i;
else if (sag->sag_maxrep == 0)
break;
else
j = (sag->sag_maxrep * i) + sag->sag_nonrep;
subagentx_varbind_start(&(sag->sag_varbind[j]));
}
}
static void
subagentx_get_finalize(struct subagentx_get *sag)
{
struct subagentx_context *sac = sag->sag_sac;
struct subagentx_session *sas = sac->sac_sas;
struct subagentx *sa = sas->sas_sa;
size_t i, j, nvarbind = 0;
uint16_t error = 0, index = 0;
struct agentx_varbind *vbl;
char *logmsg = NULL;
int fail = 0;
for (i = 0; i < sag->sag_nvarbind; i++) {
if (sag->sag_varbind[i].sav_initialized) {
if (sag->sag_varbind[i].sav_vb.avb_type == 0)
return;
nvarbind++;
}
}
if (sag->sag_sac == NULL) {
subagentx_get_free(sag);
return;
}
if ((vbl = calloc(nvarbind, sizeof(*vbl))) == NULL) {
subagentx_log_sag_warn(sag, "Couldn't parse request");
subagentx_get_free(sag);
subagentx_reset(sa);
return;
}
for (i = 0, j = 0; i < sag->sag_nvarbind; i++) {
if (sag->sag_varbind[i].sav_initialized) {
memcpy(&(vbl[j]), &(sag->sag_varbind[i].sav_vb),
sizeof(*vbl));
if (error == 0 && sag->sag_varbind[i].sav_error !=
AGENTX_PDU_ERROR_NOERROR) {
error = sag->sag_varbind[i].sav_error;
index = j + 1;
}
if (j == 0)
fail |= subagentx_strcat(&logmsg, " {");
else
fail |= subagentx_strcat(&logmsg, ",{");
fail |= subagentx_strcat(&logmsg,
agentx_varbind2string(&(vbl[j])));
if (sag->sag_varbind[i].sav_error !=
AGENTX_PDU_ERROR_NOERROR) {
fail |= subagentx_strcat(&logmsg, "(");
fail |= subagentx_strcat(&logmsg,
agentx_error2string(
sag->sag_varbind[i].sav_error));
fail |= subagentx_strcat(&logmsg, ")");
}
fail |= subagentx_strcat(&logmsg, "}");
if (fail) {
subagentx_log_sag_warn(sag,
"Couldn't parse request");
free(logmsg);
subagentx_get_free(sag);
return;
}
j++;
}
}
subagentx_log_sag_debug(sag, "response:%s", logmsg);
free(logmsg);
if (agentx_response(sa->sa_ax, sas->sas_id, sag->sag_transactionid,
sag->sag_packetid, SUBAGENTX_CONTEXT_CTX(sac), 0, error, index,
vbl, nvarbind) == -1) {
subagentx_log_sag_warn(sag, "Couldn't parse request");
subagentx_reset(sa);
} else
subagentx_wantwrite(sa, sa->sa_fd);
free(vbl);
subagentx_get_free(sag);
}
void
subagentx_get_free(struct subagentx_get *sag)
{
struct subagentx_varbind *sav;
struct subagentx_object *sao;
struct subagentx *sa = sag->sag_sac->sac_sas->sas_sa;
struct subagentx_varbind_index *index;
size_t i, j;
if (sag->sag_sac != NULL)
TAILQ_REMOVE(&(sa->sa_getreqs), sag, sag_sa_getreqs);
for (i = 0; i < sag->sag_nvarbind; i++) {
sav = &(sag->sag_varbind[i]);
for (j = 0; sav->sav_sao != NULL &&
j < sav->sav_sao->sao_indexlen; j++) {
sao = sav->sav_sao;
index = &(sav->sav_index[j]);
if (sao->sao_index[j]->sai_vb.avb_type ==
AGENTX_DATA_TYPE_OCTETSTRING)
free(index->sav_idata.avb_ostring.aos_string);
}
agentx_varbind_free(&(sag->sag_varbind[i].sav_vb));
}
free(sag->sag_context.aos_string);
free(sag->sag_varbind);
free(sag);
}
static void
subagentx_varbind_start(struct subagentx_varbind *sav)
{
struct subagentx_get *sag = sav->sav_sag;
struct subagentx_context *sac = sag->sag_sac;
struct subagentx_object *sao, sao_search;
struct subagentx_varbind_index *index;
struct subagentx_index *sai;
struct agentx_oid *oid;
union agentx_data *data;
struct in_addr *ipaddress;
unsigned char *ipbytes;
size_t i, j, k;
int overflow = 0, dynamic;
#ifdef AGENTX_DEBUG
if (!sav->sav_initialized)
subagentx_log_sag_fatalx(sav->sav_sag,
"%s: sav_initialized not set", __func__);
#endif
bcopy(&(sav->sav_vb.avb_oid), &(sao_search.sao_oid),
sizeof(sao_search.sao_oid));
do {
sao = RB_FIND(sac_objects, &(sac->sac_objects), &sao_search);
if (sao_search.sao_oid.aoi_idlen > 0)
sao_search.sao_oid.aoi_idlen--;
} while (sao == NULL && sao_search.sao_oid.aoi_idlen > 0);
if (sao == NULL || sao->sao_cstate != SA_CSTATE_OPEN) {
sav->sav_include = 1;
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET) {
subagentx_varbind_nosuchobject(sav);
return;
}
bcopy(&(sav->sav_vb.avb_oid), &(sao_search.sao_oid),
sizeof(sao_search.sao_oid));
sao = RB_NFIND(sac_objects, &(sac->sac_objects), &sao_search);
getnext:
while (sao != NULL && sao->sao_cstate != SA_CSTATE_OPEN)
sao = RB_NEXT(sac_objects, &(sac->sac_objects), sao);
if (sao == NULL) {
subagentx_varbind_endofmibview(sav);
return;
}
bcopy(&(sao->sao_oid), &(sav->sav_vb.avb_oid),
sizeof(sao->sao_oid));
}
sav->sav_sao = sao;
sav->sav_indexlen = sao->sao_indexlen;
if (subagentx_object_lock(sao) == -1) {
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
oid = &(sav->sav_vb.avb_oid);
if (sao->sao_indexlen == 0) {
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET) {
if (oid->aoi_idlen != sao->sao_oid.aoi_idlen + 1 ||
oid->aoi_id[oid->aoi_idlen - 1] != 0) {
subagentx_varbind_nosuchinstance(sav);
return;
}
} else {
if (oid->aoi_idlen == sao->sao_oid.aoi_idlen) {
oid->aoi_id[oid->aoi_idlen++] = 0;
sav->sav_include = 1;
} else {
sav->sav_sao = NULL;
subagentx_object_unlock(sao);
sao = RB_NEXT(sac_objects, &(sac->sac_objects),
sao);
goto getnext;
}
}
}
j = sao->sao_oid.aoi_idlen;
/*
* We can't trust what the client gives us, so sometimes we need to map it to
* index type.
* - AGENTX_PDU_TYPE_GET: we always return AGENTX_DATA_TYPE_NOSUCHINSTANCE
* - AGENTX_PDU_TYPE_GETNEXT:
* - Missing OID digits to match indices will result in the indices to be NUL-
* initialized and the request type will be set to
* SUBAGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE
* - An overflow can happen on AGENTX_DATA_TYPE_OCTETSTRING and
* AGENTX_DATA_TYPE_IPADDRESS. This results in request type being set to
* SUBAGENTX_REQUEST_TYPE_GETNEXT and will set the index to its maximum
* value:
* - AGENTX_DATA_TYPE_INTEGER: UINT32_MAX
* - AGENTX_DATA_TYPE_OCTETSTRING: aos_slen = UINT32_MAX and
* aos_string = NULL
* - AGENTX_DATA_TYPE_OID: aoi_idlen = UINT32_MAX and aoi_id[x] = UINT32_MAX
* - AGENTX_DATA_TYPE_IPADDRESS: 255.255.255.255
*/
for (dynamic = 0, i = 0; i < sao->sao_indexlen; i++) {
index = &(sav->sav_index[i]);
index->sav_sai = sao->sao_index[i];
data = &(index->sav_idata);
if (sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC)
dynamic = 1;
if (j >= sav->sav_vb.avb_oid.aoi_idlen && !overflow &&
sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC)
continue;
switch (sao->sao_index[i]->sai_vb.avb_type) {
case AGENTX_DATA_TYPE_INTEGER:
/* Dynamic index: normal copy paste */
if (sao->sao_index[i]->sai_type == SAI_TYPE_DYNAMIC) {
data->avb_uint32 = overflow ?
UINT32_MAX : sav->sav_vb.avb_oid.aoi_id[j];
j++;
index->sav_idatacomplete = 1;
break;
}
sai = sao->sao_index[i];
/* With a GET-request we need an exact match */
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET) {
if (sai->sai_vb.avb_data.avb_uint32 !=
sav->sav_vb.avb_oid.aoi_id[j]) {
subagentx_varbind_nosuchinstance(sav);
return;
}
index->sav_idatacomplete = 1;
j++;
break;
}
/* A higher value automatically moves us to the next value */
if (overflow ||
sav->sav_vb.avb_oid.aoi_id[j] >
sai->sai_vb.avb_data.avb_uint32) {
/* If we're !dynamic up until now the rest of the oid doesn't matter */
if (!dynamic) {
subagentx_varbind_endofmibview(sav);
return;
}
/*
* Else we just pick the max value and make sure we don't return
* SUBAGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE
*/
data->avb_uint32 = UINT32_MAX;
index->sav_idatacomplete = 1;
overflow = 1;
j++;
break;
/*
* A lower value automatically moves to the set value and counts as a short oid
*/
} else if (sav->sav_vb.avb_oid.aoi_id[j] <
sai->sai_vb.avb_data.avb_uint32) {
data->avb_uint32 =
sai->sai_vb.avb_data.avb_uint32;
j = sav->sav_vb.avb_oid.aoi_idlen;
break;
}
/* Normal match, except we already matched overflow at higher value */
data->avb_uint32 = sav->sav_vb.avb_oid.aoi_id[j];
j++;
index->sav_idatacomplete = 1;
break;
case AGENTX_DATA_TYPE_OCTETSTRING:
if (!subagentx_object_implied(sao, index->sav_sai)) {
if (overflow || sav->sav_vb.avb_oid.aoi_id[j] >
SUBAGENTX_OID_MAX_LEN -
sav->sav_vb.avb_oid.aoi_idlen) {
overflow = 1;
data->avb_ostring.aos_slen = UINT32_MAX;
index->sav_idatacomplete = 1;
continue;
}
data->avb_ostring.aos_slen =
sav->sav_vb.avb_oid.aoi_id[j++];
} else {
if (overflow) {
data->avb_ostring.aos_slen = UINT32_MAX;
index->sav_idatacomplete = 1;
continue;
}
data->avb_ostring.aos_slen =
sav->sav_vb.avb_oid.aoi_idlen - j;
}
data->avb_ostring.aos_string =
calloc(data->avb_ostring.aos_slen + 1, 1);
if (data->avb_ostring.aos_string == NULL) {
subagentx_log_sag_warn(sag,
"Failed to bind string index");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
for (k = 0; k < data->avb_ostring.aos_slen; k++, j++) {
if (!overflow &&
j == sav->sav_vb.avb_oid.aoi_idlen)
break;
if (sav->sav_vb.avb_oid.aoi_id[j] > 255)
overflow = 1;
data->avb_ostring.aos_string[k] = overflow ?
0xff : sav->sav_vb.avb_oid.aoi_id[j];
}
if (k == data->avb_ostring.aos_slen)
index->sav_idatacomplete = 1;
break;
case AGENTX_DATA_TYPE_OID:
if (!subagentx_object_implied(sao, index->sav_sai)) {
if (overflow || sav->sav_vb.avb_oid.aoi_id[j] >
SUBAGENTX_OID_MAX_LEN -
sav->sav_vb.avb_oid.aoi_idlen) {
overflow = 1;
data->avb_oid.aoi_idlen = UINT32_MAX;
index->sav_idatacomplete = 1;
continue;
}
data->avb_oid.aoi_idlen =
sav->sav_vb.avb_oid.aoi_id[j++];
} else {
if (overflow) {
data->avb_oid.aoi_idlen = UINT32_MAX;
index->sav_idatacomplete = 1;
continue;
}
data->avb_oid.aoi_idlen =
sav->sav_vb.avb_oid.aoi_idlen - j;
}
for (k = 0; k < data->avb_oid.aoi_idlen; k++, j++) {
if (!overflow &&
j == sav->sav_vb.avb_oid.aoi_idlen) {
data->avb_oid.aoi_id[k] = 0;
continue;
}
data->avb_oid.aoi_id[k] = overflow ?
UINT32_MAX : sav->sav_vb.avb_oid.aoi_id[j];
}
if (j <= sav->sav_vb.avb_oid.aoi_idlen)
index->sav_idatacomplete = 1;
break;
case AGENTX_DATA_TYPE_IPADDRESS:
ipaddress = calloc(1, sizeof(ipaddress));
if (ipaddress == NULL) {
subagentx_log_sag_warn(sag,
"Failed to bind ipaddress index");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
ipbytes = (unsigned char *)ipaddress;
for (k = 0; k < 4; k++, j++) {
if (!overflow &&
j == sav->sav_vb.avb_oid.aoi_idlen)
break;
if (sav->sav_vb.avb_oid.aoi_id[j] > 255)
overflow = 1;
ipbytes[k] = overflow ? 255 :
sav->sav_vb.avb_oid.aoi_id[j];
}
if (j <= sav->sav_vb.avb_oid.aoi_idlen)
index->sav_idatacomplete = 1;
data->avb_ostring.aos_slen = sizeof(ipaddress);
data->avb_ostring.aos_string =
(unsigned char *)ipaddress;
break;
default:
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sag,
"%s: unexpected index type", __func__);
#else
subagentx_log_sag_warnx(sag,
"%s: unexpected index type", __func__);
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
#endif
}
}
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET) {
if ((sao->sao_indexlen > 0 &&
!sav->sav_index[sao->sao_indexlen - 1].sav_idatacomplete) ||
j != sav->sav_vb.avb_oid.aoi_idlen || overflow) {
subagentx_varbind_nosuchinstance(sav);
return;
}
}
if (overflow || j > sav->sav_vb.avb_oid.aoi_idlen)
sav->sav_include = 0;
/*
* SUBAGENTX_REQUEST_TYPE_GETNEXT request can !dynamic objects can just move to
* the next object
*/
if (subagentx_varbind_request(sav) == SUBAGENTX_REQUEST_TYPE_GETNEXT &&
!dynamic) {
subagentx_varbind_endofmibview(sav);
return;
}
sao->sao_get(sav);
}
void
subagentx_varbind_integer(struct subagentx_varbind *sav, uint32_t value)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_INTEGER;
sav->sav_vb.avb_data.avb_uint32 = value;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_string(struct subagentx_varbind *sav, const char *value)
{
subagentx_varbind_nstring(sav, (const unsigned char *)value,
strlen(value));
}
void
subagentx_varbind_nstring(struct subagentx_varbind *sav,
const unsigned char *value, size_t slen)
{
sav->sav_vb.avb_data.avb_ostring.aos_string = malloc(slen);
if (sav->sav_vb.avb_data.avb_ostring.aos_string == NULL) {
subagentx_log_sag_warn(sav->sav_sag, "Couldn't bind string");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_OCTETSTRING;
memcpy(sav->sav_vb.avb_data.avb_ostring.aos_string, value, slen);
sav->sav_vb.avb_data.avb_ostring.aos_slen = slen;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_printf(struct subagentx_varbind *sav, const char *fmt, ...)
{
va_list ap;
int r;
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_OCTETSTRING;
va_start(ap, fmt);
r = vasprintf((char **)&(sav->sav_vb.avb_data.avb_ostring.aos_string),
fmt, ap);
va_end(ap);
if (r == -1) {
sav->sav_vb.avb_data.avb_ostring.aos_string = NULL;
subagentx_log_sag_warn(sav->sav_sag, "Couldn't bind string");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
sav->sav_vb.avb_data.avb_ostring.aos_slen = r;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_null(struct subagentx_varbind *sav)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_NULL;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_oid(struct subagentx_varbind *sav, const uint32_t oid[],
size_t oidlen)
{
size_t i;
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_OID;
for (i = 0; i < oidlen; i++)
sav->sav_vb.avb_data.avb_oid.aoi_id[i] = oid[i];
sav->sav_vb.avb_data.avb_oid.aoi_idlen = oidlen;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_object(struct subagentx_varbind *sav,
struct subagentx_object *sao)
{
subagentx_varbind_oid(sav, sao->sao_oid.aoi_id,
sao->sao_oid.aoi_idlen);
}
void
subagentx_varbind_index(struct subagentx_varbind *sav,
struct subagentx_index *sai)
{
subagentx_varbind_oid(sav, sai->sai_vb.avb_oid.aoi_id,
sai->sai_vb.avb_oid.aoi_idlen);
}
void
subagentx_varbind_ipaddress(struct subagentx_varbind *sav,
const struct in_addr *value)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_IPADDRESS;
sav->sav_vb.avb_data.avb_ostring.aos_string = malloc(4);
if (sav->sav_vb.avb_data.avb_ostring.aos_string == NULL) {
subagentx_log_sag_warn(sav->sav_sag, "Couldn't bind ipaddress");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
memcpy(sav->sav_vb.avb_data.avb_ostring.aos_string, value, 4);
sav->sav_vb.avb_data.avb_ostring.aos_slen = 4;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_counter32(struct subagentx_varbind *sav, uint32_t value)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_COUNTER32;
sav->sav_vb.avb_data.avb_uint32 = value;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_gauge32(struct subagentx_varbind *sav, uint32_t value)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_GAUGE32;
sav->sav_vb.avb_data.avb_uint32 = value;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_timeticks(struct subagentx_varbind *sav, uint32_t value)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_TIMETICKS;
sav->sav_vb.avb_data.avb_uint32 = value;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_opaque(struct subagentx_varbind *sav, const char *string,
size_t strlen)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_OPAQUE;
sav->sav_vb.avb_data.avb_ostring.aos_string = malloc(strlen);
if (sav->sav_vb.avb_data.avb_ostring.aos_string == NULL) {
subagentx_log_sag_warn(sav->sav_sag, "Couldn't bind opaque");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 1);
return;
}
memcpy(sav->sav_vb.avb_data.avb_ostring.aos_string, string, strlen);
sav->sav_vb.avb_data.avb_ostring.aos_slen = strlen;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_counter64(struct subagentx_varbind *sav, uint64_t value)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_COUNTER64;
sav->sav_vb.avb_data.avb_uint64 = value;
subagentx_varbind_finalize(sav);
}
void
subagentx_varbind_notfound(struct subagentx_varbind *sav)
{
if (sav->sav_indexlen == 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "%s invalid call",
__func__);
#else
subagentx_log_sag_warnx(sav->sav_sag, "%s invalid call",
__func__);
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 1);
#endif
} else if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET)
subagentx_varbind_nosuchinstance(sav);
else
subagentx_varbind_endofmibview(sav);
}
void
subagentx_varbind_error(struct subagentx_varbind *sav)
{
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 1);
}
static void
subagentx_varbind_error_type(struct subagentx_varbind *sav,
enum agentx_pdu_error error, int done)
{
if (sav->sav_error == AGENTX_PDU_ERROR_NOERROR) {
sav->sav_error = error;
}
if (done) {
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_NULL;
subagentx_varbind_finalize(sav);
}
}
static void
subagentx_varbind_finalize(struct subagentx_varbind *sav)
{
struct subagentx_get *sag = sav->sav_sag;
struct agentx_oid oid;
union agentx_data *data;
size_t i, j;
int cmp;
if (sav->sav_error != AGENTX_PDU_ERROR_NOERROR) {
bcopy(&(sav->sav_start), &(sav->sav_vb.avb_oid),
sizeof(sav->sav_start));
goto done;
}
bcopy(&(sav->sav_sao->sao_oid), &oid, sizeof(oid));
if (sav->sav_indexlen == 0)
agentx_oid_add(&oid, 0);
for (i = 0; i < sav->sav_indexlen; i++) {
data = &(sav->sav_index[i].sav_idata);
switch (sav->sav_index[i].sav_sai->sai_vb.avb_type) {
case AGENTX_DATA_TYPE_INTEGER:
if (agentx_oid_add(&oid, data->avb_uint32) == -1)
goto fail;
break;
case AGENTX_DATA_TYPE_OCTETSTRING:
if (!subagentx_object_implied(sav->sav_sao,
sav->sav_index[i].sav_sai)) {
if (agentx_oid_add(&oid,
data->avb_ostring.aos_slen) == -1)
goto fail;
}
for (j = 0; j < data->avb_ostring.aos_slen; j++) {
if (agentx_oid_add(&oid,
(uint8_t)data->avb_ostring.aos_string[j]) ==
-1)
goto fail;
}
break;
case AGENTX_DATA_TYPE_OID:
if (!subagentx_object_implied(sav->sav_sao,
sav->sav_index[i].sav_sai)) {
if (agentx_oid_add(&oid,
data->avb_oid.aoi_idlen) == -1)
goto fail;
}
for (j = 0; j < data->avb_oid.aoi_idlen; j++) {
if (agentx_oid_add(&oid,
data->avb_oid.aoi_id[j]) == -1)
goto fail;
}
break;
case AGENTX_DATA_TYPE_IPADDRESS:
for (j = 0; j < 4; j++) {
if (agentx_oid_add(&oid,
data->avb_ostring.aos_string == NULL ? 0 :
(uint8_t)data->avb_ostring.aos_string[j]) ==
-1)
goto fail;
}
break;
default:
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sag,
"%s: unsupported index type", __func__);
#else
bcopy(&(sav->sav_start), &(sav->sav_vb.avb_oid),
sizeof(sav->sav_start));
sav->sav_error = AGENTX_PDU_ERROR_PROCESSINGERROR;
subagentx_object_unlock(sav->sav_sao);
subagentx_get_finalize(sav->sav_sag);
return;
#endif
}
}
cmp = agentx_oid_cmp(&(sav->sav_vb.avb_oid), &oid);
if ((subagentx_varbind_request(sav) == SUBAGENTX_REQUEST_TYPE_GETNEXT &&
cmp >= 0) || cmp > 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sag, "indices not incremented");
#else
subagentx_log_sag_warnx(sag, "indices not incremented");
bcopy(&(sav->sav_start), &(sav->sav_vb.avb_oid),
sizeof(sav->sav_start));
sav->sav_error = AGENTX_PDU_ERROR_GENERR;
#endif
} else
bcopy(&oid, &(sav->sav_vb.avb_oid), sizeof(oid));
done:
subagentx_object_unlock(sav->sav_sao);
subagentx_get_finalize(sav->sav_sag);
return;
fail:
subagentx_log_sag_warnx(sag, "oid too large");
bcopy(&(sav->sav_start), &(sav->sav_vb.avb_oid),
sizeof(sav->sav_start));
sav->sav_error = AGENTX_PDU_ERROR_GENERR;
subagentx_object_unlock(sav->sav_sao);
subagentx_get_finalize(sav->sav_sag);
}
static void
subagentx_varbind_nosuchobject(struct subagentx_varbind *sav)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_NOSUCHOBJECT;
if (sav->sav_sao != NULL)
subagentx_object_unlock(sav->sav_sao);
subagentx_get_finalize(sav->sav_sag);
}
static void
subagentx_varbind_nosuchinstance(struct subagentx_varbind *sav)
{
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_NOSUCHINSTANCE;
if (sav->sav_sao != NULL)
subagentx_object_unlock(sav->sav_sao);
subagentx_get_finalize(sav->sav_sag);
}
static void
subagentx_varbind_endofmibview(struct subagentx_varbind *sav)
{
struct subagentx_object *sao;
struct agentx_varbind *vb;
size_t i;
#ifdef AGENTX_DEBUG
if (sav->sav_sag->sag_type != AGENTX_PDU_TYPE_GETNEXT &&
sav->sav_sag->sag_type != AGENTX_PDU_TYPE_GETBULK)
subagentx_log_sag_fatalx(sav->sav_sag,
"%s: invalid request type", __func__);
#endif
if (sav->sav_sao != NULL &&
(sao = RB_NEXT(sac_objects, &(sac->sac_objects),
sav->sav_sao)) != NULL &&
agentx_oid_cmp(&(sao->sao_oid), &(sav->sav_end)) < 0) {
bcopy(&(sao->sao_oid), &(sav->sav_vb.avb_oid),
sizeof(sao->sao_oid));
sav->sav_include = 1;
for (i = 0; i < sav->sav_indexlen; i++) {
vb = &(sav->sav_index[i].sav_sai->sai_vb);
if (vb->avb_type == AGENTX_DATA_TYPE_OCTETSTRING ||
vb->avb_type == AGENTX_DATA_TYPE_IPADDRESS)
free(vb->avb_data.avb_ostring.aos_string);
}
bzero(&(sav->sav_index), sizeof(sav->sav_index));
subagentx_object_unlock(sav->sav_sao);
subagentx_varbind_start(sav);
return;
}
sav->sav_vb.avb_type = AGENTX_DATA_TYPE_ENDOFMIBVIEW;
if (sav->sav_sao != NULL)
subagentx_object_unlock(sav->sav_sao);
subagentx_get_finalize(sav->sav_sag);
}
enum subagentx_request_type
subagentx_varbind_request(struct subagentx_varbind *sav)
{
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET)
return SUBAGENTX_REQUEST_TYPE_GET;
if (sav->sav_include ||
(sav->sav_indexlen > 0 &&
!sav->sav_index[sav->sav_indexlen - 1].sav_idatacomplete))
return SUBAGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE;
return SUBAGENTX_REQUEST_TYPE_GETNEXT;
}
struct subagentx_object *
subagentx_varbind_get_object(struct subagentx_varbind *sav)
{
return sav->sav_sao;
}
uint32_t
subagentx_varbind_get_index_integer(struct subagentx_varbind *sav,
struct subagentx_index *sai)
{
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_INTEGER) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return 0;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai)
return sav->sav_index[i].sav_idata.avb_uint32;
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return 0;
#endif
}
const unsigned char *
subagentx_varbind_get_index_string(struct subagentx_varbind *sav,
struct subagentx_index *sai, size_t *slen, int *implied)
{
struct subagentx_varbind_index *index;
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_OCTETSTRING) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
*slen = 0;
*implied = 0;
return NULL;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
index = &(sav->sav_index[i]);
*slen = index->sav_idata.avb_ostring.aos_slen;
*implied = subagentx_object_implied(sav->sav_sao, sai);
return index->sav_idata.avb_ostring.aos_string;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
*slen = 0;
*implied = 0;
return NULL;
#endif
}
const uint32_t *
subagentx_varbind_get_index_oid(struct subagentx_varbind *sav,
struct subagentx_index *sai, size_t *oidlen, int *implied)
{
struct subagentx_varbind_index *index;
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_OID) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
*oidlen = 0;
*implied = 0;
return NULL;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
index = &(sav->sav_index[i]);
*oidlen = index->sav_idata.avb_oid.aoi_idlen;
*implied = subagentx_object_implied(sav->sav_sao, sai);
return index->sav_idata.avb_oid.aoi_id;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
*oidlen = 0;
*implied = 0;
return NULL;
#endif
}
const struct in_addr *
subagentx_varbind_get_index_ipaddress(struct subagentx_varbind *sav,
struct subagentx_index *sai)
{
static struct in_addr nuladdr = {0};
struct subagentx_varbind_index *index;
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_IPADDRESS) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return NULL;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
index = &(sav->sav_index[i]);
if (index->sav_idata.avb_ostring.aos_string == NULL)
return &nuladdr;
return (struct in_addr *)
index->sav_idata.avb_ostring.aos_string;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return NULL;
#endif
}
void
subagentx_varbind_set_index_integer(struct subagentx_varbind *sav,
struct subagentx_index *sai, uint32_t value)
{
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_INTEGER) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET &&
sav->sav_index[i].sav_idata.avb_uint32 != value) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag,
"can't change index on GET");
#else
subagentx_log_sag_warnx(sav->sav_sag,
"can't change index on GET");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
sav->sav_index[i].sav_idata.avb_uint32 = value;
return;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
#endif
}
void
subagentx_varbind_set_index_string(struct subagentx_varbind *sav,
struct subagentx_index *sai, const char *value)
{
subagentx_varbind_set_index_nstring(sav, sai,
(const unsigned char *)value, strlen(value));
}
void
subagentx_varbind_set_index_nstring(struct subagentx_varbind *sav,
struct subagentx_index *sai, const unsigned char *value, size_t slen)
{
struct agentx_ostring *curvalue;
unsigned char *nstring;
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_OCTETSTRING) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
if (sai->sai_vb.avb_data.avb_ostring.aos_slen != 0 &&
sai->sai_vb.avb_data.avb_ostring.aos_slen != slen) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag,
"invalid string length on explicit length "
"string");
#else
subagentx_log_sag_warnx(sav->sav_sag,
"invalid string length on explicit length "
"string");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
curvalue = &(sav->sav_index[i].sav_idata.avb_ostring);
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET &&
(curvalue->aos_slen != slen ||
memcmp(curvalue->aos_string, value, slen) != 0)) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag,
"can't change index on GET");
#else
subagentx_log_sag_warnx(sav->sav_sag,
"can't change index on GET");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
if ((nstring = recallocarray(curvalue->aos_string,
curvalue->aos_slen + 1, slen + 1, 1)) == NULL) {
subagentx_log_sag_warn(sav->sav_sag,
"Failed to bind string index");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 0);
return;
}
curvalue->aos_string = nstring;
memcpy(nstring, value, slen);
curvalue->aos_slen = slen;
return;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
#endif
}
void
subagentx_varbind_set_index_oid(struct subagentx_varbind *sav,
struct subagentx_index *sai, const uint32_t *value, size_t oidlen)
{
struct agentx_oid *curvalue, oid;
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_OID) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
if (sai->sai_vb.avb_data.avb_oid.aoi_idlen != 0 &&
sai->sai_vb.avb_data.avb_oid.aoi_idlen != oidlen) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag,
"invalid oid length on explicit length "
"oid");
#else
subagentx_log_sag_warnx(sav->sav_sag,
"invalid oid length on explicit length "
"oid");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
curvalue = &(sav->sav_index[i].sav_idata.avb_oid);
for (i = 0; i < oidlen; i++)
oid.aoi_id[i] = value[i];
oid.aoi_idlen = oidlen;
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET &&
agentx_oid_cmp(&oid, curvalue) != 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag,
"can't change index on GET");
#else
subagentx_log_sag_warnx(sav->sav_sag,
"can't change index on GET");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
for (i = 0; i < oidlen; i++)
curvalue->aoi_id[i] = value[i];
curvalue->aoi_idlen = oidlen;
return;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
#endif
}
void
subagentx_varbind_set_index_object(struct subagentx_varbind *sav,
struct subagentx_index *sai, struct subagentx_object *sao)
{
subagentx_varbind_set_index_oid(sav, sai, sao->sao_oid.aoi_id,
sao->sao_oid.aoi_idlen);
}
void
subagentx_varbind_set_index_ipaddress(struct subagentx_varbind *sav,
struct subagentx_index *sai, const struct in_addr *addr)
{
struct agentx_ostring *curvalue;
size_t i;
if (sai->sai_vb.avb_type != AGENTX_DATA_TYPE_IPADDRESS) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index type");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index type");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
for (i = 0; i < sav->sav_indexlen; i++) {
if (sav->sav_index[i].sav_sai == sai) {
curvalue = &(sav->sav_index[i].sav_idata.avb_ostring);
if (curvalue->aos_string == NULL)
curvalue->aos_string = calloc(1, sizeof(*addr));
if (curvalue->aos_string == NULL) {
subagentx_log_sag_warn(sav->sav_sag,
"Failed to bind ipaddress index");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_PROCESSINGERROR, 0);
return;
}
if (sav->sav_sag->sag_type == AGENTX_PDU_TYPE_GET &&
memcmp(addr, curvalue->aos_string,
sizeof(*addr)) != 0) {
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag,
"can't change index on GET");
#else
subagentx_log_sag_warnx(sav->sav_sag,
"can't change index on GET");
subagentx_varbind_error_type(sav,
AGENTX_PDU_ERROR_GENERR, 0);
return;
#endif
}
bcopy(addr, curvalue->aos_string, sizeof(*addr));
return;
}
}
#ifdef AGENTX_DEBUG
subagentx_log_sag_fatalx(sav->sav_sag, "invalid index");
#else
subagentx_log_sag_warnx(sav->sav_sag, "invalid index");
subagentx_varbind_error_type(sav, AGENTX_PDU_ERROR_GENERR, 0);
#endif
}
static int
subagentx_request(struct subagentx *sa, uint32_t packetid,
int (*cb)(struct agentx_pdu *, void *), void *cookie)
{
struct subagentx_request *sar;
#ifdef AGENTX_DEBUG
if (sa->sa_ax->ax_wblen == 0)
subagentx_log_sa_fatalx(sa, "%s: no data to be written",
__func__);
#endif
if ((sar = calloc(1, sizeof(*sar))) == NULL) {
subagentx_log_sa_warn(sa, "couldn't create request context");
subagentx_reset(sa);
return -1;
}
sar->sar_packetid = packetid;
sar->sar_cb = cb;
sar->sar_cookie = cookie;
if (RB_INSERT(sa_requests, &(sa->sa_requests), sar) != NULL) {
#ifdef AGENTX_DEBUG
subagentx_log_sa_fatalx(sa, "%s: duplicate packetid", __func__);
#else
subagentx_log_sa_warnx(sa, "%s: duplicate packetid", __func__);
free(sar);
subagentx_reset(sa);
return -1;
#endif
}
subagentx_wantwrite(sa, sa->sa_fd);
return 0;
}
static int
subagentx_request_cmp(struct subagentx_request *r1,
struct subagentx_request *r2)
{
return r1->sar_packetid < r2->sar_packetid ? -1 :
r1->sar_packetid > r2->sar_packetid;
}
static int
subagentx_strcat(char **dst, const char *src)
{
char *tmp;
size_t dstlen = 0, buflen = 0, srclen, nbuflen;
if (*dst != NULL) {
dstlen = strlen(*dst);
buflen = ((dstlen / 512) + 1) * 512;
}
srclen = strlen(src);
if (dst == NULL || dstlen + srclen > buflen) {
nbuflen = (((dstlen + srclen) / 512) + 1) * 512;
tmp = recallocarray(*dst, buflen, nbuflen, sizeof(*tmp));
if (tmp == NULL)
return -1;
*dst = tmp;
buflen = nbuflen;
}
(void)strlcat(*dst, src, buflen);
return 0;
}
void
subagentx_read(struct subagentx *sa)
{
struct subagentx_session *sas;
struct subagentx_context *sac;
struct subagentx_request sar_search, *sar;
struct agentx_pdu *pdu;
int error;
if ((pdu = agentx_recv(sa->sa_ax)) == NULL) {
if (errno == EAGAIN)
return;
subagentx_log_sa_warn(sa, "lost connection");
subagentx_reset(sa);
return;
}
TAILQ_FOREACH(sas, &(sa->sa_sessions), sas_sa_sessions) {
if (sas->sas_id == pdu->ap_header.aph_sessionid)
break;
if (sas->sas_cstate == SA_CSTATE_WAITOPEN &&
sas->sas_packetid == pdu->ap_header.aph_packetid)
break;
}
if (sas == NULL) {
subagentx_log_sa_warnx(sa, "received unexpected session: %d",
pdu->ap_header.aph_sessionid);
agentx_pdu_free(pdu);
subagentx_reset(sa);
return;
}
TAILQ_FOREACH(sac, &(sas->sas_contexts), sac_sas_contexts) {
if ((pdu->ap_header.aph_flags &
AGENTX_PDU_FLAG_NON_DEFAULT_CONTEXT) == 0 &&
sac->sac_name_default == 1)
break;
if (pdu->ap_header.aph_flags &
AGENTX_PDU_FLAG_NON_DEFAULT_CONTEXT &&
sac->sac_name_default == 0 &&
pdu->ap_context.aos_slen == sac->sac_name.aos_slen &&
memcmp(pdu->ap_context.aos_string,
sac->sac_name.aos_string, sac->sac_name.aos_slen) == 0)
break;
}
if (pdu->ap_header.aph_type != AGENTX_PDU_TYPE_RESPONSE) {
if (sac == NULL) {
subagentx_log_sa_warnx(sa, "%s: invalid context",
pdu->ap_context.aos_string);
agentx_pdu_free(pdu);
subagentx_reset(sa);
return;
}
}
switch (pdu->ap_header.aph_type) {
case AGENTX_PDU_TYPE_GET:
case AGENTX_PDU_TYPE_GETNEXT:
case AGENTX_PDU_TYPE_GETBULK:
subagentx_get_start(sac, pdu);
break;
/* Add stubs for set functions */
case AGENTX_PDU_TYPE_TESTSET:
case AGENTX_PDU_TYPE_COMMITSET:
case AGENTX_PDU_TYPE_UNDOSET:
if (pdu->ap_header.aph_type == AGENTX_PDU_TYPE_TESTSET)
error = AGENTX_PDU_ERROR_NOTWRITABLE;
else if (pdu->ap_header.aph_type == AGENTX_PDU_TYPE_COMMITSET)
error = AGENTX_PDU_ERROR_COMMITFAILED;
else
error = AGENTX_PDU_ERROR_UNDOFAILED;
subagentx_log_sac_debug(sac, "unsupported call: %s",
agentx_pdutype2string(pdu->ap_header.aph_type));
if (agentx_response(sa->sa_ax, sas->sas_id,
pdu->ap_header.aph_transactionid,
pdu->ap_header.aph_packetid,
sac == NULL ? NULL : SUBAGENTX_CONTEXT_CTX(sac),
0, error, 1, NULL, 0) == -1)
subagentx_log_sac_warn(sac,
"transaction: %u packetid: %u: failed to send "
"reply", pdu->ap_header.aph_transactionid,
pdu->ap_header.aph_packetid);
if (sa->sa_ax->ax_wblen > 0)
subagentx_wantwrite(sa, sa->sa_fd);
break;
case AGENTX_PDU_TYPE_CLEANUPSET:
subagentx_log_sa_debug(sa, "unsupported call: %s",
agentx_pdutype2string(pdu->ap_header.aph_type));
break;
case AGENTX_PDU_TYPE_RESPONSE:
sar_search.sar_packetid = pdu->ap_header.aph_packetid;
sar = RB_FIND(sa_requests, &(sa->sa_requests), &sar_search);
if (sar == NULL) {
if (sac == NULL)
subagentx_log_sa_warnx(sa, "received "
"response on non-request");
else
subagentx_log_sac_warnx(sac, "received "
"response on non-request");
break;
}
if (sac != NULL && pdu->ap_payload.ap_response.ap_error == 0) {
sac->sac_sysuptime =
pdu->ap_payload.ap_response.ap_uptime;
(void) clock_gettime(CLOCK_MONOTONIC,
&(sac->sac_sysuptimespec));
}
RB_REMOVE(sa_requests, &(sa->sa_requests), sar);
(void) sar->sar_cb(pdu, sar->sar_cookie);
free(sar);
break;
default:
if (sac == NULL)
subagentx_log_sa_warnx(sa, "unsupported call: %s",
agentx_pdutype2string(pdu->ap_header.aph_type));
else
subagentx_log_sac_warnx(sac, "unsupported call: %s",
agentx_pdutype2string(pdu->ap_header.aph_type));
subagentx_reset(sa);
break;
}
agentx_pdu_free(pdu);
}
void
subagentx_write(struct subagentx *sa)
{
ssize_t send;
if ((send = agentx_send(sa->sa_ax)) == -1) {
if (errno == EAGAIN) {
subagentx_wantwrite(sa, sa->sa_fd);
return;
}
subagentx_log_sa_warn(sa, "lost connection");
subagentx_reset(sa);
return;
}
if (send > 0)
subagentx_wantwrite(sa, sa->sa_fd);
}
RB_GENERATE_STATIC(sa_requests, subagentx_request, sar_sa_requests,
subagentx_request_cmp)
RB_GENERATE_STATIC(sac_objects, subagentx_object, sao_sac_objects,
subagentx_object_cmp)
|