aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/bsnmpd/modules/snmp_netgraph/snmp_netgraph.c
blob: 4eccc2363431d6c3615416b1d6dfa6f0034db4b2 (plain) (blame)
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
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 *	All rights reserved.
 *
 * Author: Harti Brandt <harti@freebsd.org>
 *
 * Redistribution of this software and documentation and use in source and
 * binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code or documentation must retain the above
 *    copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
 * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS  BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 *
 * Netgraph interface for SNMPd.
 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/linker.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <netgraph.h>
#include "snmpmod.h"
#include "snmp_netgraph.h"
#include "netgraph_tree.h"
#include "netgraph_oid.h"

/* maximum message size */
#define RESBUFSIZ	20000

/* default node name */
#define NODENAME	"NgSnmpd"

/* my node Id */
ng_ID_t snmp_node;
u_char *snmp_nodename;

/* the Object Resource registration index */
static u_int reg_index;
static const struct asn_oid oid_begemotNg = OIDX_begemotNg;

/* configuration */
/* this must be smaller than int32_t because the functions in libnetgraph
 * falsely return an int */
static size_t resbufsiz = RESBUFSIZ;
static u_int timeout = 1000;
static u_int debug_level;

/* number of microseconds per clock tick */
static struct clockinfo clockinfo;

/* Csock buffers. Communication on the csock is asynchronuous. This means
 * if we wait for a specific response, we may get other messages. Put these
 * into a queue and execute them when we are idle. */
struct csock_buf {
	STAILQ_ENTRY(csock_buf) link;
	struct ng_mesg *mesg;
	char path[NG_PATHSIZ];
};
static STAILQ_HEAD(, csock_buf) csock_bufs =
	STAILQ_HEAD_INITIALIZER(csock_bufs);

/*
 * We dispatch unsolicieted messages by node cookies and ids.
 * So we must keep a list of hook names and dispatch functions.
 */
struct msgreg {
	u_int32_t 	cookie;
	ng_ID_t		id;
	ng_cookie_f	*func;
	void		*arg;
	const struct lmodule *mod;
	SLIST_ENTRY(msgreg) link;
};
static SLIST_HEAD(, msgreg) msgreg_list =
	SLIST_HEAD_INITIALIZER(msgreg_list);

/*
 * Data messages are dispatched by hook names.
 */
struct datareg {
	char		hook[NG_HOOKSIZ];
	ng_hook_f	*func;
	void		*arg;
	const struct lmodule *mod;
	SLIST_ENTRY(datareg) link;
};
static SLIST_HEAD(, datareg) datareg_list =
	SLIST_HEAD_INITIALIZER(datareg_list);

/* the netgraph sockets */
static int csock, dsock;
static void *csock_fd, *dsock_fd;

/* our module handle */
static struct lmodule *module;

/* statistics */
static u_int32_t stats[LEAF_begemotNgTooLargeDatas+1];

/* netgraph type list */
struct ngtype {
	char		name[NG_TYPESIZ];
	struct asn_oid	index;
	TAILQ_ENTRY(ngtype) link;
};
TAILQ_HEAD(ngtype_list, ngtype);

static struct ngtype_list ngtype_list;
static uint64_t ngtype_tick;


/*
 * Register a function to receive unsolicited messages
 */
void *
ng_register_cookie(const struct lmodule *mod, u_int32_t cookie, ng_ID_t id,
    ng_cookie_f *func, void *arg)
{
	struct msgreg *d;

	if ((d = malloc(sizeof(*d))) == NULL)
		return (NULL);

	d->cookie = cookie;
	d->id = id;
	d->func = func;
	d->arg = arg;
	d->mod = mod;

	SLIST_INSERT_HEAD(&msgreg_list, d, link);

	return (d);
}

/*
 * Remove a registration.
 */
void
ng_unregister_cookie(void *dd)
{
	struct msgreg *d = dd;

	SLIST_REMOVE(&msgreg_list, d, msgreg, link);
	free(d);
}

/*
 * Register a function for hook data.
 */
void *
ng_register_hook(const struct lmodule *mod, const char *hook, 
    ng_hook_f *func, void *arg)
{
	struct datareg *d;

	if ((d = malloc(sizeof(*d))) == NULL)
		return (NULL);

	strcpy(d->hook, hook);
	d->func = func;
	d->arg = arg;
	d->mod = mod;

	SLIST_INSERT_HEAD(&datareg_list, d, link);

	return (d);
}

/*
 * Unregister a hook function
 */
void
ng_unregister_hook(void *dd)
{
	struct datareg *d = dd;

	SLIST_REMOVE(&datareg_list, d, datareg, link);
	free(d);
}

/*
 * Unregister all hooks and cookies for that module. Note: doesn't disconnect
 * any hooks!
 */
void
ng_unregister_module(const struct lmodule *mod)
{
	struct msgreg *m, *m1;
	struct datareg *d, *d1;

	m = SLIST_FIRST(&msgreg_list);
	while (m != NULL) {
		m1 = SLIST_NEXT(m, link);
		if (m->mod == mod) {
			SLIST_REMOVE(&msgreg_list, m, msgreg, link);
			free(m);
		}
		m = m1;
	}

	d = SLIST_FIRST(&datareg_list);
	while (d != NULL) {
		d1 = SLIST_NEXT(d, link);
		if (d->mod == mod) {
			SLIST_REMOVE(&datareg_list, d, datareg, link);
			free(d);
		}
		d = d1;
	}
}

/*
 * Dispatch a message to the correct module and delete it. More than one
 * module can get a message.
 */
static void
csock_handle(struct ng_mesg *mesg, const char *path)
{
	struct msgreg *d, *d1;
	u_int id;
	int len;

	if (sscanf(path, "[%x]:%n", &id, &len) != 1 ||
	    (u_int)len != strlen(path)) {
		syslog(LOG_ERR, "cannot parse message path '%s'", path);
		id = 0;
	}

	d = SLIST_FIRST(&msgreg_list);
	while (d != NULL) {
		d1 = SLIST_NEXT(d, link);
		if (d->cookie == mesg->header.typecookie &&
		    (d->id == 0 || d->id == id || id == 0))
			(*d->func)(mesg, path, id, d->arg);
		d = d1;
	}
	free(mesg);
}

/*
 * Input from the control socket.
 */
static struct ng_mesg *
csock_read(char *path)
{
	struct ng_mesg *mesg;
	int ret, err;

	if ((mesg = malloc(resbufsiz + 1)) == NULL) {
		stats[LEAF_begemotNgNoMems]++;
		syslog(LOG_CRIT, "out of memory");
		errno = ENOMEM;
		return (NULL);
	}
	if ((ret = NgRecvMsg(csock, mesg, resbufsiz + 1, path)) < 0) {
		err = errno;
		free(mesg);
		if (errno == EWOULDBLOCK) {
			errno = err;
			return (NULL);
		}
		stats[LEAF_begemotNgMsgReadErrs]++;
		syslog(LOG_WARNING, "read from csock: %m");
		errno = err;
		return (NULL);
	}
	if (ret == 0) {
		syslog(LOG_DEBUG, "node closed -- exiting");
		exit(0);
	}
	if ((size_t)ret > resbufsiz) {
		stats[LEAF_begemotNgTooLargeMsgs]++;
		syslog(LOG_WARNING, "ng message too large");
		free(mesg);
		errno = EFBIG;
		return (NULL);
	}
	return (mesg);
}

static void
csock_input(int fd __unused, void *udata __unused)
{
	struct ng_mesg *mesg;
	char path[NG_PATHSIZ];

	if ((mesg = csock_read(path)) == NULL)
		return;

	csock_handle(mesg, path);
}

/*
 * Write a message to a node.
 */
int
ng_output(const char *path, u_int cookie, u_int opcode,
    const void *arg, size_t arglen)
{
	return (NgSendMsg(csock, path, (int)cookie, (int)opcode, arg, arglen));
}
int
ng_output_node(const char *node, u_int cookie, u_int opcode,
    const void *arg, size_t arglen)
{
	char path[NG_PATHSIZ];

	sprintf(path, "%s:", node);
	return (ng_output(path, cookie, opcode, arg, arglen));
}
int
ng_output_id(ng_ID_t node, u_int cookie, u_int opcode,
    const void *arg, size_t arglen)
{
	char path[NG_PATHSIZ];

	sprintf(path, "[%x]:", node);
	return (ng_output(path, cookie, opcode, arg, arglen));
}



/*
 * Execute a synchronuous dialog with the csock. All message we receive, that
 * do not match our request, are queue until the next call to the IDLE function.
 */
struct ng_mesg *
ng_dialog(const char *path, u_int cookie, u_int opcode,
    const void *arg, size_t arglen)
{
	int token, err;
	struct ng_mesg *mesg;
	char rpath[NG_PATHSIZ];
	struct csock_buf *b;
	struct timeval end, tv;

	if ((token = ng_output(path, cookie, opcode, arg, arglen)) < 0)
		return (NULL);

	if (csock_fd)
		fd_suspend(csock_fd);

	gettimeofday(&end, NULL);
	tv.tv_sec = timeout / 1000;
	tv.tv_usec = (timeout % 1000) * 1000;
	timeradd(&end, &tv, &end);
	for (;;) {
		mesg = NULL;
		gettimeofday(&tv, NULL);
		if (timercmp(&tv, &end, >=)) {
  block:
			syslog(LOG_WARNING, "no response for request %u/%u",
			    cookie, opcode);
			errno = EWOULDBLOCK;
			break;
		}
		timersub(&end, &tv, &tv);
		if (tv.tv_sec == 0 && tv.tv_usec < clockinfo.tick)
			goto block;

		if (setsockopt(csock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
			syslog(LOG_WARNING, "setsockopt(SO_RCVTIMEO): %m");
		if ((mesg = csock_read(rpath)) == NULL) {
			if (errno == EWOULDBLOCK)
				continue;
			break;
		}
		if (mesg->header.token == (u_int)token)
			break;
		if ((b = malloc(sizeof(*b))) == NULL) {
			stats[LEAF_begemotNgNoMems]++;
			syslog(LOG_ERR, "out of memory");
			free(mesg);
			continue;
		}
		b->mesg = mesg;
		strcpy(b->path, rpath);
		STAILQ_INSERT_TAIL(&csock_bufs, b, link);
	}

	tv.tv_sec = 0;
	tv.tv_usec = 0;
	if (setsockopt(csock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
		syslog(LOG_WARNING, "setsockopt(SO_RCVTIMEO,0): %m");

	if (csock_fd) {
		err = errno;
		fd_resume(csock_fd);
		errno = err;
	}

	return (mesg);
}
struct ng_mesg *
ng_dialog_node(const char *node, u_int cookie, u_int opcode,
    const void *arg, size_t arglen)
{
	char path[NG_PATHSIZ];

	sprintf(path, "%s:", node);
	return (ng_dialog(path, cookie, opcode, arg, arglen));
}
struct ng_mesg *
ng_dialog_id(ng_ID_t id, u_int cookie, u_int opcode,
    const void *arg, size_t arglen)
{
	char path[NG_PATHSIZ];

	sprintf(path, "[%x]:", id);
	return (ng_dialog(path, cookie, opcode, arg, arglen));
}


/*
 * Send a data message to a given hook.
 */
int
ng_send_data(const char *hook, const void *sndbuf, size_t sndlen)
{
	return (NgSendData(dsock, hook, sndbuf, sndlen));
}

/*
 * Input from a data socket. Dispatch to the function for that hook.
 */
static void
dsock_input(int fd __unused, void *udata __unused)
{
	u_char *resbuf, embuf[100];
	ssize_t len;
	char hook[NG_HOOKSIZ];
	struct datareg *d, *d1;

	if ((resbuf = malloc(resbufsiz + 1)) == NULL) {
		stats[LEAF_begemotNgNoMems]++;
		syslog(LOG_CRIT, "out of memory");
		(void)NgRecvData(fd, embuf, sizeof(embuf), hook);
		errno = ENOMEM;
		return;
	}
	if ((len = NgRecvData(fd, resbuf, resbufsiz + 1, hook)) == -1) {
		stats[LEAF_begemotNgDataReadErrs]++;
		syslog(LOG_ERR, "reading message: %m");
		free(resbuf);
		return;
	}
	if (len == 0) {
		free(resbuf);
		return;
	}
	if ((size_t)len == resbufsiz + 1) {
		stats[LEAF_begemotNgTooLargeDatas]++;
		syslog(LOG_WARNING, "message too long");
		free(resbuf);
		return;
	}

	/*
	 * Dispatch message. Maybe dispatched to more than one function.
	 */
	d = SLIST_FIRST(&datareg_list);
	while (d != NULL) {
		d1 = SLIST_NEXT(d, link);
		if (strcmp(hook, d->hook) == 0)
			(*d->func)(hook, resbuf, len, d->arg);
		d = d1;
	}

	free(resbuf);
}

/*
 * The SNMP daemon is about to wait for an event. Look whether we have
 * netgraph messages waiting. If yes, drain the queue.
 */
static void
ng_idle(void)
{
	struct csock_buf *b;

	/* execute waiting csock_bufs */
	while ((b = STAILQ_FIRST(&csock_bufs)) != NULL) {
		STAILQ_REMOVE_HEAD(&csock_bufs, link);
		csock_handle(b->mesg, b->path);
		free(b);
	}
}

/*
 * Called when the module is loaded. Returning a non-zero value means,
 * rejecting the initialisation.
 *
 * We make the netgraph socket.
 */
static int
ng_init(struct lmodule *mod, int argc, char *argv[])
{
	int name[2];
	size_t len;

	module = mod;

	if (argc == 0) {
		if ((snmp_nodename = malloc(strlen(NODENAME) + 1)) == NULL)
			return (ENOMEM);
		strcpy(snmp_nodename, NODENAME);
	} else {
		if ((snmp_nodename = malloc(NG_NODESIZ)) == NULL)
			return (ENOMEM);
		strlcpy(snmp_nodename, argv[0], NG_NODESIZ);
	}

	/* fetch clockinfo (for the number of microseconds per tick) */
	name[0] = CTL_KERN;
	name[1] = KERN_CLOCKRATE;
	len = sizeof(clockinfo);
	if (sysctl(name, 2, &clockinfo, &len, NULL, 0) == -1)
		return (errno);

	TAILQ_INIT(&ngtype_list);

	return (0);
}

/*
 * Get the node Id/name/type of a node.
 */
ng_ID_t
ng_node_id(const char *path)
{
	struct ng_mesg *resp;
	ng_ID_t id;

	if ((resp = ng_dialog(path, NGM_GENERIC_COOKIE, NGM_NODEINFO,
	    NULL, 0)) == NULL)
		return (0);
	id = ((struct nodeinfo *)(void *)resp->data)->id;
	free(resp);
	return (id);
}
ng_ID_t
ng_node_id_node(const char *node)
{
	struct ng_mesg *resp;
	ng_ID_t id;

	if ((resp = ng_dialog_node(node, NGM_GENERIC_COOKIE, NGM_NODEINFO,
	    NULL, 0)) == NULL)
		return (0);
	id = ((struct nodeinfo *)(void *)resp->data)->id;
	free(resp);
	return (id);
}
ng_ID_t
ng_node_name(ng_ID_t id, char *name)
{
	struct ng_mesg *resp;

	if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE, NGM_NODEINFO,
	    NULL, 0)) == NULL)
		return (0);
	strcpy(name, ((struct nodeinfo *)(void *)resp->data)->name);
	free(resp);
	return (id);

}
ng_ID_t
ng_node_type(ng_ID_t id, char *type)
{
	struct ng_mesg *resp;

	if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE, NGM_NODEINFO,
	    NULL, 0)) == NULL)
		return (0);
	strcpy(type, ((struct nodeinfo *)(void *)resp->data)->type);
	free(resp);
	return (id);
}

/*
 * Connect our node to some other node
 */
int
ng_connect_node(const char *node, const char *ourhook, const char *peerhook)
{
	struct ngm_connect conn;

	snprintf(conn.path, NG_PATHSIZ, "%s:", node);
	strlcpy(conn.ourhook, ourhook, NG_HOOKSIZ);
	strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
	return (NgSendMsg(csock, ".:",
	    NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
}
int
ng_connect_id(ng_ID_t id, const char *ourhook, const char *peerhook)
{
	struct ngm_connect conn;

	snprintf(conn.path, NG_PATHSIZ, "[%x]:", id);
	strlcpy(conn.ourhook, ourhook, NG_HOOKSIZ);
	strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
	return (NgSendMsg(csock, ".:",
	    NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
}

int
ng_connect2_id(ng_ID_t id, ng_ID_t peer, const char *ourhook,
    const char *peerhook)
{
	struct ngm_connect conn;
	char path[NG_PATHSIZ];

	snprintf(path, NG_PATHSIZ, "[%x]:", id);

	snprintf(conn.path, NG_PATHSIZ, "[%x]:", peer);
	strlcpy(conn.ourhook, ourhook, NG_HOOKSIZ);
	strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
	return (NgSendMsg(csock, path,
	    NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
}

int
ng_connect2_tee_id(ng_ID_t id, ng_ID_t peer, const char *ourhook,
    const char *peerhook)
{
	struct ngm_connect conn;
	char path[NG_PATHSIZ];
	ng_ID_t tee;

	if ((tee = ng_mkpeer_id(id, NULL, "tee", ourhook, "left")) == 0)
		return (-1);

	snprintf(path, NG_PATHSIZ, "[%x]:", tee);

	snprintf(conn.path, NG_PATHSIZ, "[%x]:", peer);
	strlcpy(conn.ourhook, "right", NG_HOOKSIZ);
	strlcpy(conn.peerhook, peerhook, NG_HOOKSIZ);
	return (NgSendMsg(csock, path,
	    NGM_GENERIC_COOKIE, NGM_CONNECT, &conn, sizeof(conn)));
}

/*
 * Ensure that a node of type 'type' is connected to 'hook' of 'node'
 * and return its node id. tee nodes between node and the target node
 * are skipped. If the type is wrong, or the hook is a dead-end return 0.
 * If type is NULL, it is not checked.
 */
static ng_ID_t
ng_next_node_id_internal(ng_ID_t node, const char *type, const char *hook,
    int skip_tee)
{
	struct ng_mesg *resp;
	struct hooklist *hooklist;
	u_int i;

	if ((resp = ng_dialog_id(node, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
	    NULL, 0)) == NULL) {
		syslog(LOG_ERR, "get hook list: %m");
		exit(1);
	}
	hooklist = (struct hooklist *)(void *)resp->data;

	for (i = 0; i < hooklist->nodeinfo.hooks; i++)
		if (strcmp(hooklist->link[i].ourhook, hook) == 0)
			break;

	if (i == hooklist->nodeinfo.hooks) {
		free(resp);
		return (0);
	}

	node = hooklist->link[i].nodeinfo.id;

	if (skip_tee && strcmp(hooklist->link[i].nodeinfo.type, "tee") == 0) {
		if (strcmp(hooklist->link[i].peerhook, "left") == 0)
			node = ng_next_node_id(node, type, "right");
		else if (strcmp(hooklist->link[i].peerhook, "right") == 0)
			node = ng_next_node_id(node, type, "left");
		else if (type != NULL &&
		    strcmp(hooklist->link[i].nodeinfo.type, type) != 0)
			node = 0;

	} else if (type != NULL &&
	    strcmp(hooklist->link[i].nodeinfo.type, type) != 0)
		node = 0;

	free(resp);

	return (node);
}

/*
 * Ensure that a node of type 'type' is connected to 'hook' of 'node'
 * and return its node id. tee nodes between node and the target node
 * are skipped. If the type is wrong, or the hook is a dead-end return 0.
 * If type is NULL, it is not checked.
 */
ng_ID_t
ng_next_node_id(ng_ID_t node, const char *type, const char *hook)
{
	return (ng_next_node_id_internal(node, type, hook, 1));
}

ng_ID_t
ng_mkpeer_id(ng_ID_t id, const char *nodename, const char *type,
    const char *hook, const char *peerhook)
{
	char path[NG_PATHSIZ];
	struct ngm_mkpeer mkpeer;
	struct ngm_name name;

	strlcpy(mkpeer.type, type, NG_TYPESIZ);
	strlcpy(mkpeer.ourhook, hook, NG_HOOKSIZ);
	strlcpy(mkpeer.peerhook, peerhook, NG_HOOKSIZ);

	sprintf(path, "[%x]:", id);
	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_MKPEER,
	    &mkpeer, sizeof(mkpeer)) == -1)
		return (0);

	if ((id = ng_next_node_id_internal(id, NULL, hook, 0)) == 0)
		return (0);

	if (nodename != NULL) {
		strcpy(name.name, nodename);
		sprintf(path, "[%x]:", id);
		if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_NAME,
		    &name, sizeof(name)) == -1)
			return (0);
	}
	return (id);
}

/*
 * SHutdown node
 */
int
ng_shutdown_id(ng_ID_t id)
{
	char path[NG_PATHSIZ];

	snprintf(path, NG_PATHSIZ, "[%x]:", id);
	return (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
	    NGM_SHUTDOWN, NULL, 0));
}

/*
 * Disconnect one of our hooks
 */
int
ng_rmhook(const char *ourhook)
{
	struct ngm_rmhook rmhook;

	strlcpy(rmhook.ourhook, ourhook, NG_HOOKSIZ);
	return (NgSendMsg(csock, ".:",
	    NGM_GENERIC_COOKIE, NGM_RMHOOK, &rmhook, sizeof(rmhook)));
}

/*
 * Disconnect a hook of a node
 */
int
ng_rmhook_id(ng_ID_t id, const char *hook)
{
	struct ngm_rmhook rmhook;
	char path[NG_PATHSIZ];

	strlcpy(rmhook.ourhook, hook, NG_HOOKSIZ);
	snprintf(path, NG_PATHSIZ, "[%x]:", id);
	return (NgSendMsg(csock, path,
	    NGM_GENERIC_COOKIE, NGM_RMHOOK, &rmhook, sizeof(rmhook)));
}

/*
 * Disconnect a hook and shutdown all tee nodes that were connected to that
 * hook.
 */
int
ng_rmhook_tee_id(ng_ID_t node, const char *hook)
{
	struct ng_mesg *resp;
	struct hooklist *hooklist;
	u_int i;
	int first = 1;
	ng_ID_t next_node;
	const char *next_hook;

  again:
	/* if we have just shutdown a tee node, which had no other hooks
	 * connected, the node id may already be wrong here. */
	if ((resp = ng_dialog_id(node, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
	    NULL, 0)) == NULL)
		return (0);

	hooklist = (struct hooklist *)(void *)resp->data;

	for (i = 0; i < hooklist->nodeinfo.hooks; i++)
		if (strcmp(hooklist->link[i].ourhook, hook) == 0)
			break;

	if (i == hooklist->nodeinfo.hooks) {
		free(resp);
		return (0);
	}

	next_node = 0;
	next_hook = NULL;
	if (strcmp(hooklist->link[i].nodeinfo.type, "tee") == 0) {
		if (strcmp(hooklist->link[i].peerhook, "left") == 0) {
			next_node = hooklist->link[i].nodeinfo.id;
			next_hook = "right";
		} else if (strcmp(hooklist->link[i].peerhook, "right") == 0) {
			next_node = hooklist->link[i].nodeinfo.id;
			next_hook = "left";
		}
	}
	free(resp);

	if (first) {
		ng_rmhook_id(node, hook);
		first = 0;
	} else {
		ng_shutdown_id(node);
	}
	if ((node = next_node) == 0)
		return (0);
	hook = next_hook;

	goto again;
}

/*
 * Get the peer hook of a hook on a given node. Skip any tee nodes in between
 */
int
ng_peer_hook_id(ng_ID_t node, const char *hook, char *peerhook)
{
	struct ng_mesg *resp;
	struct hooklist *hooklist;
	u_int i;
	int ret;

	if ((resp = ng_dialog_id(node, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
	    NULL, 0)) == NULL) {
		syslog(LOG_ERR, "get hook list: %m");
		exit(1);
	}
	hooklist = (struct hooklist *)(void *)resp->data;

	for (i = 0; i < hooklist->nodeinfo.hooks; i++)
		if (strcmp(hooklist->link[i].ourhook, hook) == 0)
			break;

	if (i == hooklist->nodeinfo.hooks) {
		free(resp);
		return (-1);
	}

	node = hooklist->link[i].nodeinfo.id;

	ret = 0;
	if (strcmp(hooklist->link[i].nodeinfo.type, "tee") == 0) {
		if (strcmp(hooklist->link[i].peerhook, "left") == 0)
			ret = ng_peer_hook_id(node, "right", peerhook);
		else if (strcmp(hooklist->link[i].peerhook, "right") == 0)
			ret = ng_peer_hook_id(node, "left", peerhook);
		else
			strcpy(peerhook, hooklist->link[i].peerhook);

	} else 
		strcpy(peerhook, hooklist->link[i].peerhook);

	free(resp);

	return (ret);
}


/*
 * Now the module is started. Select on the sockets, so that we can get 
 * unsolicited input.
 */
static void
ng_start(void)
{
	if (snmp_node == 0) {
		if (NgMkSockNode(snmp_nodename, &csock, &dsock) < 0) {
			syslog(LOG_ERR, "NgMkSockNode: %m");
			exit(1);
		}
		snmp_node = ng_node_id(".:");
	}

	if ((csock_fd = fd_select(csock, csock_input, NULL, module)) == NULL) {
		syslog(LOG_ERR, "fd_select failed on csock: %m");
		return;
	}
	if ((dsock_fd = fd_select(dsock, dsock_input, NULL, module)) == NULL) {
		syslog(LOG_ERR, "fd_select failed on dsock: %m");
		return;
	}

	reg_index = or_register(&oid_begemotNg, 
	    "The MIB for the NetGraph access module for SNMP.", module);
}

/*
 * Called, when the module is to be unloaded after it was successfully loaded
 */
static int
ng_fini(void)
{
	struct ngtype *t;

	while ((t = TAILQ_FIRST(&ngtype_list)) != NULL) {
		TAILQ_REMOVE(&ngtype_list, t, link);
		free(t);
	}

	if (csock_fd != NULL)
		fd_deselect(csock_fd);
	(void)close(csock);

	if (dsock_fd != NULL)
		fd_deselect(dsock_fd);
	(void)close(dsock);

	free(snmp_nodename);

	or_unregister(reg_index);

	return (0);
}

const struct snmp_module config = {
	"This module implements access to the netgraph sub-system",
	ng_init,
	ng_fini,
	ng_idle,
	NULL,
	NULL,
	ng_start,
	NULL,
	netgraph_ctree,
	netgraph_CTREE_SIZE,
	NULL
};

int
op_ng_config(struct snmp_context *ctx, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];
	int ret;

	switch (op) {

	  case SNMP_OP_GETNEXT:
		abort();

	  case SNMP_OP_GET:
		/*
		 * Come here for GET, GETNEXT and COMMIT
		 */
		switch (which) {

		  case LEAF_begemotNgControlNodeName:
			return (string_get(value, snmp_nodename, -1));

		  case LEAF_begemotNgResBufSiz:
			value->v.integer = resbufsiz;
			break;

		  case LEAF_begemotNgTimeout:
			value->v.integer = timeout;
			break;

		  case LEAF_begemotNgDebugLevel:
			value->v.uint32 = debug_level;
			break;

		  default:
			abort();
		}
		return (SNMP_ERR_NOERROR);

	  case SNMP_OP_SET:
		switch (which) {

		  case LEAF_begemotNgControlNodeName:
			/* only at initialisation */
			if (community != COMM_INITIALIZE)
				return (SNMP_ERR_NOT_WRITEABLE);

			if (snmp_node != 0)
				return (SNMP_ERR_NOT_WRITEABLE);

			if ((ret = string_save(value, ctx, -1, &snmp_nodename))
			    != SNMP_ERR_NOERROR)
				return (ret);

			if (NgMkSockNode(snmp_nodename, &csock, &dsock) < 0) {
				syslog(LOG_ERR, "NgMkSockNode: %m");
				string_rollback(ctx, &snmp_nodename);
				return (SNMP_ERR_GENERR);
			}
			snmp_node = ng_node_id(".:");

			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgResBufSiz:
			ctx->scratch->int1 = resbufsiz;
			if (value->v.integer < 1024 ||
			    value->v.integer > 0x10000)
				return (SNMP_ERR_WRONG_VALUE);
			resbufsiz = value->v.integer;
			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgTimeout:
			ctx->scratch->int1 = timeout;
			if (value->v.integer < 10 ||
			    value->v.integer > 10000)
				return (SNMP_ERR_WRONG_VALUE);
			timeout = value->v.integer;
			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgDebugLevel:
			ctx->scratch->int1 = debug_level;
			debug_level = value->v.uint32;
			NgSetDebug(debug_level);
			return (SNMP_ERR_NOERROR);
		}
		abort();

	  case SNMP_OP_ROLLBACK:
		switch (which) {

		  case LEAF_begemotNgControlNodeName:
			string_rollback(ctx, &snmp_nodename);
			close(csock);
			close(dsock);
			snmp_node = 0;
			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgResBufSiz:
			resbufsiz = ctx->scratch->int1;
			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgTimeout:
			timeout = ctx->scratch->int1;
			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgDebugLevel:
			debug_level = ctx->scratch->int1;
			NgSetDebug(debug_level);
			return (SNMP_ERR_NOERROR);
		}
		abort();

	  case SNMP_OP_COMMIT:
		switch (which) {

		  case LEAF_begemotNgControlNodeName:
			string_commit(ctx);
			return (SNMP_ERR_NOERROR);

		  case LEAF_begemotNgResBufSiz:
		  case LEAF_begemotNgTimeout:
		  case LEAF_begemotNgDebugLevel:
			return (SNMP_ERR_NOERROR);
		}
		abort();
	}
	abort();
}

int
op_ng_stats(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op op)
{
	switch (op) {

	  case SNMP_OP_GETNEXT:
		abort();

	  case SNMP_OP_GET:
		value->v.uint32 = stats[value->var.subs[sub - 1] - 1];
		return (SNMP_ERR_NOERROR);

	  case SNMP_OP_SET:
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
		abort();
	}
	abort();
}

/*
 * Netgraph type table
 */
static int
fetch_types(void)
{
	struct ngtype *t;
	struct typelist *typelist;
	struct ng_mesg *resp;
	u_int u, i;

	if (this_tick <= ngtype_tick)
		return (0);

	while ((t = TAILQ_FIRST(&ngtype_list)) != NULL) {
		TAILQ_REMOVE(&ngtype_list, t, link);
		free(t);
	}

	if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE,
	    NGM_LISTTYPES, NULL, 0)) == NULL)
		return (SNMP_ERR_GENERR);
	typelist = (struct typelist *)(void *)resp->data;

	for (u = 0; u < typelist->numtypes; u++) {
		if ((t = malloc(sizeof(*t))) == NULL) {
			free(resp);
			return (SNMP_ERR_GENERR);
		}
		strcpy(t->name, typelist->typeinfo[u].type_name);
		t->index.subs[0] = strlen(t->name);
		t->index.len = t->index.subs[0] + 1;
		for (i = 0; i < t->index.subs[0]; i++)
			t->index.subs[i + 1] = t->name[i];

		INSERT_OBJECT_OID(t, &ngtype_list);
	}

	ngtype_tick = this_tick;

	free(resp);
	return (0);
}

/*
 * Try to load the netgraph type with the given name. We assume, that
 * type 'type' is implemented in the kernel module 'ng_type'.
 */
static int
ngtype_load(const u_char *name, size_t namelen)
{
	char *mod;
	int ret;

	if ((mod = malloc(namelen + 4)) == NULL)
		return (-1);
	strcpy(mod, "ng_");
	strncpy(mod + 3, name, namelen);
	mod[namelen + 3] = '\0';

	ret = kldload(mod);
	free(mod);
	return (ret);
}

/*
 * Unload a netgraph type.
 */
static int
ngtype_unload(const u_char *name, size_t namelen)
{
	char *mod;
	int id;

	if ((mod = malloc(namelen + 4)) == NULL)
		return (-1);
	strcpy(mod, "ng_");
	strncpy(mod + 3, name, namelen);
	mod[namelen + 3] = '\0';

	if ((id = kldfind(mod)) == -1) {
		free(mod);
		return (-1);
	}
	free(mod);
	return (kldunload(id));
}

int
op_ng_type(struct snmp_context *ctx, struct snmp_value *value,
    u_int sub, u_int iidx, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];
	struct ngtype *t;
	u_char *name;
	size_t namelen;
	int status = 1;
	int ret;

	switch (op) {

	  case SNMP_OP_GETNEXT:
		if ((ret = fetch_types()) != 0)
			return (ret);
		if ((t = NEXT_OBJECT_OID(&ngtype_list, &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		index_append(&value->var, sub, &t->index);
		break;

	  case SNMP_OP_GET:
		if ((ret = fetch_types()) != 0)
			return (ret);
		if ((t = FIND_OBJECT_OID(&ngtype_list, &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		break;

	  case SNMP_OP_SET:
		if (index_decode(&value->var, sub, iidx, &name, &namelen))
			return (SNMP_ERR_NO_CREATION);
		if (namelen == 0 || namelen >= NG_TYPESIZ) {
			free(name);
			return (SNMP_ERR_NO_CREATION);
		}
		if ((ret = fetch_types()) != 0) {
			free(name);
			return (ret);
		}
		t = FIND_OBJECT_OID(&ngtype_list, &value->var, sub);

		if (which != LEAF_begemotNgTypeStatus) {
			free(name);
			if (t != NULL)
				return (SNMP_ERR_NOT_WRITEABLE);
			return (SNMP_ERR_NO_CREATION);
		}
		if (!TRUTH_OK(value->v.integer)) {
			free(name);
			return (SNMP_ERR_WRONG_VALUE);
		}
		ctx->scratch->int1 = TRUTH_GET(value->v.integer);
		ctx->scratch->int1 |= (t != NULL) << 1;
		ctx->scratch->ptr2 = name;
		ctx->scratch->int2 = namelen;

		if (t == NULL) {
			/* type not loaded */
			if (ctx->scratch->int1 & 1) {
				/* request to load */
				if (ngtype_load(name, namelen) == -1) {
					free(name);
					if (errno == ENOENT)
						return (SNMP_ERR_INCONS_NAME);
					else
						return (SNMP_ERR_GENERR);
				}
			}
		} else {
			/* is type loaded */
			if (!(ctx->scratch->int1 & 1)) {
				/* request to unload */
				if (ngtype_unload(name, namelen) == -1) {
					free(name);
					return (SNMP_ERR_GENERR);
				}
			}
		}
		return (SNMP_ERR_NOERROR);

	  case SNMP_OP_ROLLBACK:
		ret = SNMP_ERR_NOERROR;
		if (!(ctx->scratch->int1 & 2)) {
			/* did not exist */
			if (ctx->scratch->int1 & 1) {
				/* request to load - unload */
				if (ngtype_unload(ctx->scratch->ptr2,
				    ctx->scratch->int2) == -1)
					ret = SNMP_ERR_UNDO_FAILED;
			}
		} else {
			/* did exist */
			if (!(ctx->scratch->int1 & 1)) {
				/* request to unload - reload */
				if (ngtype_load(ctx->scratch->ptr2,
				    ctx->scratch->int2) == -1)
					ret = SNMP_ERR_UNDO_FAILED;
			}
		}
		free(ctx->scratch->ptr2);
		return (ret);

	  case SNMP_OP_COMMIT:
		free(ctx->scratch->ptr2);
		return (SNMP_ERR_NOERROR);

	  default:
		abort();
	}

	/*
	 * Come here for GET and COMMIT
	 */
	switch (which) {

	  case LEAF_begemotNgTypeStatus:
		value->v.integer = status;
		break;

	  default:
		abort();
	}
	return (SNMP_ERR_NOERROR);
}

/*
 * Implement the node table
 */
static int
find_node(const struct asn_oid *oid, u_int sub, struct nodeinfo *info)
{
	ng_ID_t id = oid->subs[sub];
	struct ng_mesg *resp;

	if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE, NGM_NODEINFO,
	    NULL, 0)) == NULL)
		return (-1);

	*info = *(struct nodeinfo *)(void *)resp->data;
	free(resp);
	return (0);
}

static int
ncmp(const void *p1, const void *p2)
{
	const struct nodeinfo *i1 = p1;
	const struct nodeinfo *i2 = p2;

	if (i1->id < i2->id)
		return (-1);
	if (i1->id > i2->id)
		return (+1);
	return (0);
}

static int
find_node_next(const struct asn_oid *oid, u_int sub, struct nodeinfo *info)
{
	u_int idxlen = oid->len - sub;
	struct ng_mesg *resp;
	struct namelist *list;
	ng_ID_t id;
	u_int i;

	if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE, NGM_LISTNODES,
	    NULL, 0)) == NULL)
		return (-1);
	list = (struct namelist *)(void *)resp->data;

	qsort(list->nodeinfo, list->numnames, sizeof(list->nodeinfo[0]), ncmp);

	if (idxlen == 0) {
		if (list->numnames == 0) {
			free(resp);
			return (-1);
		}
		*info = list->nodeinfo[0];
		free(resp);
		return (0);
	}
	id = oid->subs[sub];

	for (i = 0; i < list->numnames; i++)
		if (list->nodeinfo[i].id > id) {
			*info = list->nodeinfo[i];
			free(resp);
			return (0);
		}

	free(resp);
	return (-1);
}

int
op_ng_node(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];
	u_int idxlen = value->var.len - sub;
	struct nodeinfo nodeinfo;

	switch (op) {

	  case SNMP_OP_GETNEXT:
		if (find_node_next(&value->var, sub, &nodeinfo) == -1)
			return (SNMP_ERR_NOSUCHNAME);
		value->var.len = sub + 1;
		value->var.subs[sub] = nodeinfo.id;
		break;

	  case SNMP_OP_GET:
		if (idxlen != 1)
			return (SNMP_ERR_NOSUCHNAME);
		if (find_node(&value->var, sub, &nodeinfo) == -1)
			return (SNMP_ERR_NOSUCHNAME);
		break;

	  case SNMP_OP_SET:
		if (idxlen != 1)
			return (SNMP_ERR_NO_CREATION);
		if (find_node(&value->var, sub, &nodeinfo) == -1)
			return (SNMP_ERR_NO_CREATION);
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
	  default:
		abort();
	}

	/*
	 * Come here for GET and COMMIT
	 */
	switch (which) {

	  case LEAF_begemotNgNodeStatus:
		value->v.integer = 1;
		break;
	  case LEAF_begemotNgNodeName:
		return (string_get(value, nodeinfo.name, -1));
	  case LEAF_begemotNgNodeType:
		return (string_get(value, nodeinfo.type, -1));
	  case LEAF_begemotNgNodeHooks:
		value->v.uint32 = nodeinfo.hooks;
		break;

	  default:
		abort();
	}
	return (SNMP_ERR_NOERROR);
}

/*
 * Implement the hook table
 */
static int
find_hook(int32_t id, const u_char *hook, size_t hooklen, struct linkinfo *info)
{
	struct ng_mesg *resp;
	struct hooklist *list;
	u_int i;

	if ((resp = ng_dialog_id(id, NGM_GENERIC_COOKIE,
	    NGM_LISTHOOKS, NULL, 0)) == NULL)
		return (-1);

	list = (struct hooklist *)(void *)resp->data;

	for (i = 0; i < list->nodeinfo.hooks; i++) {
		if (strlen(list->link[i].ourhook) == hooklen &&
		    strncmp(list->link[i].ourhook, hook, hooklen) == 0) {
			*info = list->link[i];
			free(resp);
			return (0);
		}
	}
	free(resp);
	return (-1);
}

static int
hook_cmp(const void *p1, const void *p2)
{
	const struct linkinfo *i1 = p1;
	const struct linkinfo *i2 = p2;

	if (strlen(i1->ourhook) < strlen(i2->ourhook))
		return (-1);
	if (strlen(i1->ourhook) > strlen(i2->ourhook))
		return (+1);
	return (strcmp(i1->ourhook, i2->ourhook));
}

static int
find_hook_next(const struct asn_oid *oid, u_int sub, struct nodeinfo *nodeinfo,
    struct linkinfo *linkinfo)
{
	u_int idxlen = oid->len - sub;
	struct namelist *list;
	struct ng_mesg *resp;
	struct hooklist *hooks;
	struct ng_mesg *resp1;
	u_int node_index;
	struct asn_oid idx;
	u_int i, j;

	/*
	 * Get and sort Node list
	 */
	if ((resp = ng_dialog_id(snmp_node, NGM_GENERIC_COOKIE, NGM_LISTNODES,
	    NULL, 0)) == NULL)
		return (-1);
	list = (struct namelist *)(void *)resp->data;

	qsort(list->nodeinfo, list->numnames, sizeof(list->nodeinfo[0]), ncmp);

	/*
	 * If we have no index, take the first node and return the
	 * first hook.
	 */
	if (idxlen == 0) {
		node_index = 0;
		goto return_first_hook;
	}

	/*
	 * Locate node
	 */
	for (node_index = 0; node_index < list->numnames; node_index++)
		if (list->nodeinfo[node_index].id >= oid->subs[sub])
			break;

	/*
	 * If we have only the node part of the index take, or
	 * there is no node with that Id, take the first hook of that node.
	 */
	if (idxlen == 1 || node_index >= list->numnames ||
	    list->nodeinfo[node_index].id > oid->subs[sub])
		goto return_first_hook;

	/*
	 * We had an exact match on the node id and have (at last part)
	 * of the hook name index. Loop through the hooks of the node
	 * and find the next one.
	 */
	if ((resp1 = ng_dialog_id(list->nodeinfo[node_index].id,
	    NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0)) == NULL) {
		free(resp);
		return (-1);
	}
	hooks = (struct hooklist *)(void *)resp1->data;
	if (hooks->nodeinfo.hooks > 0) {
		qsort(hooks->link, hooks->nodeinfo.hooks,
		    sizeof(hooks->link[0]), hook_cmp);
		for (i = 0; i < hooks->nodeinfo.hooks; i++) {
			idx.len = strlen(hooks->link[i].ourhook) + 1;
			idx.subs[0] = idx.len - 1;
			for (j = 0; j < idx.len; j++)
				idx.subs[j + 1] = hooks->link[i].ourhook[j];
			if (index_compare(oid, sub + 1, &idx) < 0)
				break;
		}
		if (i < hooks->nodeinfo.hooks) {
			*nodeinfo = hooks->nodeinfo;
			*linkinfo = hooks->link[i];

			free(resp);
			free(resp1);
			return (0);
		}
	}

	/* no hook found larger than the index on the index node - take
	 * first hook of next node */
	free(resp1);
	node_index++;

  return_first_hook:
	while (node_index < list->numnames) {
		if ((resp1 = ng_dialog_id(list->nodeinfo[node_index].id,
		    NGM_GENERIC_COOKIE, NGM_LISTHOOKS, NULL, 0)) == NULL)
			break;
		hooks = (struct hooklist *)(void *)resp1->data;
		if (hooks->nodeinfo.hooks > 0) {
			qsort(hooks->link, hooks->nodeinfo.hooks,
			    sizeof(hooks->link[0]), hook_cmp);

			*nodeinfo = hooks->nodeinfo;
			*linkinfo = hooks->link[0];

			free(resp);
			free(resp1);
			return (0);
		}

		/* if we don't have hooks, try next node */
		free(resp1);
		node_index++;
	}

	free(resp);
	return (-1);
}

int
op_ng_hook(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];
	struct linkinfo linkinfo;
	struct nodeinfo nodeinfo;
	u_int32_t lid;
	u_char *hook;
	size_t hooklen;
	u_int i;

	switch (op) {

	  case SNMP_OP_GETNEXT:
		if (find_hook_next(&value->var, sub, &nodeinfo, &linkinfo) == -1)
			return (SNMP_ERR_NOSUCHNAME);

		value->var.len = sub + 1 + 1 + strlen(linkinfo.ourhook);
		value->var.subs[sub] = nodeinfo.id;
		value->var.subs[sub + 1] = strlen(linkinfo.ourhook);
		for (i = 0; i < strlen(linkinfo.ourhook); i++)
			value->var.subs[sub + i + 2] =
			    linkinfo.ourhook[i];
		break;

	  case SNMP_OP_GET:
		if (index_decode(&value->var, sub, iidx, &lid,
		    &hook, &hooklen))
			return (SNMP_ERR_NOSUCHNAME);
		if (find_hook(lid, hook, hooklen, &linkinfo) == -1) {
			free(hook);
			return (SNMP_ERR_NOSUCHNAME);
		}
		free(hook);
		break;

	  case SNMP_OP_SET:
		if (index_decode(&value->var, sub, iidx, &lid,
		    &hook, &hooklen))
			return (SNMP_ERR_NO_CREATION);
		if (find_hook(lid, hook, hooklen, &linkinfo) == -1) {
			free(hook);
			return (SNMP_ERR_NO_CREATION);
		}
		free(hook);
		return (SNMP_ERR_NOT_WRITEABLE);

	  case SNMP_OP_ROLLBACK:
	  case SNMP_OP_COMMIT:
	  default:
		abort();

	}

	switch (which) {

	  case LEAF_begemotNgHookStatus:
		value->v.integer = 1;
		break;
	  case LEAF_begemotNgHookPeerNodeId:
		value->v.uint32 = linkinfo.nodeinfo.id;
		break;
	  case LEAF_begemotNgHookPeerHook:
		return (string_get(value, linkinfo.peerhook, -1));
	  case LEAF_begemotNgHookPeerType:
		return (string_get(value, linkinfo.nodeinfo.type, -1));
	  default:
		abort();
	}
	return (SNMP_ERR_NOERROR);
}