summaryrefslogtreecommitdiff
path: root/regress/lib/libagentx/main.c
blob: 92c04afca71932926d314500236907abc02216e2 (plain)
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
/*	$OpenBSD: main.c,v 1.7 2021/05/01 16:55:14 martijn Exp $	*/

/*
 * 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 <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include <arpa/inet.h>

#include <event.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "log.h"
#include <agentx.h>

#define LINKDOWN 1, 3, 6, 1, 6, 3, 1, 1, 5, 3
#define IFINDEX 1, 3, 6, 1, 2, 1, 2, 2, 1, 1
#define IFADMINSTATUS 1, 3, 6, 1, 2, 1, 2, 2, 1, 7
#define IFOPERSTATUS 1, 3, 6, 1, 2, 1, 2, 2, 1, 8

void regress_fd(struct agentx *, void *, int);
void regress_tryconnect(int, short, void *);
void regress_read(int, short, void *);
void regress_usr1(int, short, void *);
void regress_usr2(int, short, void *);
void regress_scalarinteger(struct agentx_varbind *);
void regress_scalarstring(struct agentx_varbind *);
void regress_scalarnull(struct agentx_varbind *);
void regress_scalaroid(struct agentx_varbind *);
void regress_scalaripaddress(struct agentx_varbind *);
void regress_scalarcounter32(struct agentx_varbind *);
void regress_scalargauge32(struct agentx_varbind *);
void regress_scalartimeticks(struct agentx_varbind *);
void regress_scalaropaque(struct agentx_varbind *);
void regress_scalarcounter64(struct agentx_varbind *);
void regress_scalarerror(struct agentx_varbind *);

void regress_intindex(struct agentx_varbind *);
void regress_intindex2(struct agentx_varbind *);

void regress_strindex(struct agentx_varbind *);
void regress_implstrindex(struct agentx_varbind *);
void regress_strindex2(struct agentx_varbind *);

void regress_oidimplindex(struct agentx_varbind *);

void regress_ipaddressindex(struct agentx_varbind *);

void regress_intindexstaticvalueint(struct agentx_varbind *);
void regress_intindexstaticvaluestring(struct agentx_varbind *);
void regress_intindexstaticanyint(struct agentx_varbind *);
void regress_intindexstaticanystring(struct agentx_varbind *);
void regress_intindexstaticnewint(struct agentx_varbind *);
void regress_intindexstaticnewstring(struct agentx_varbind *);

struct agentx *sa;
struct agentx_session *sas;
struct agentx_context *sac;
struct agentx_agentcaps *saa;
struct agentx_region *regress;

struct agentx_index *regressidx_int;
struct agentx_index *regressidx_int2;

struct agentx_index *regressidx_str;
struct agentx_index *regressidx_str2;

struct agentx_index *regressidx_oid;

struct agentx_index *regressidx_ipaddress;

struct agentx_index *regressidx_new;
struct agentx_index *regressidx_any;
struct agentx_index *regressidx_value;

struct agentx_object *regressobj_scalarinteger;
struct agentx_object *regressobj_scalarstring;
struct agentx_object *regressobj_scalarnull;
struct agentx_object *regressobj_scalaroid;
struct agentx_object *regressobj_scalaripaddress;
struct agentx_object *regressobj_scalarcounter32;
struct agentx_object *regressobj_scalargauge32;
struct agentx_object *regressobj_scalartimeticks;
struct agentx_object *regressobj_scalaropaque;
struct agentx_object *regressobj_scalarcounter64;

struct agentx_object *regressobj_intindexint;
struct agentx_object *regressobj_intindexint2;

struct agentx_object *regressobj_strindexstr;
struct agentx_object *regressobj_implstrindexstr;
struct agentx_object *regressobj_strindexstr2;

struct agentx_object *regressobj_oidimplindexoid;

struct agentx_object *regressobj_ipaddressindexipaddress;

struct agentx_object *regressobj_intindexstaticvalueint;
struct agentx_object *regressobj_intindexstaticvaluestring;
struct agentx_object *regressobj_intindexstaticanyint;
struct agentx_object *regressobj_intindexstaticanystring;
struct agentx_object *regressobj_intindexstaticnewint;
struct agentx_object *regressobj_intindexstaticnewstring;

struct agentx_object *regressobj_scalarerror;

char *path = AGENTX_MASTER_PATH;
int fd = -1;
struct event rev;
struct event intev, usr1ev, usr2ev, connev;

int
main(int argc, char *argv[])
{
	struct agentx_index *idx[AGENTX_OID_INDEX_MAX_LEN];
	int ch;
	int dflag = 0;

	log_init(2, 1);

	while ((ch = getopt(argc, argv, "d")) != -1) {
		switch (ch) {
		case 'd':
			dflag = 1;
			break;
		default:
			exit(1);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc >= 1)
		path = argv[0];

	bzero(&rev, sizeof(rev));

	agentx_log_fatal = fatalx;
	agentx_log_warn = log_warnx;
	agentx_log_info = log_info;
	agentx_log_debug = log_debug;

	if ((sa = agentx(regress_fd, (void *)path)) == NULL)
		fatal("agentx");
	sas = agentx_session(sa, NULL, 0, "OpenAgentX regress", 0);
	if (sas == NULL)
		fatal("agentx_session");
	if ((sac = agentx_context(sas, NULL)) == NULL)
		fatal("agentx_context");
	if ((saa = agentx_agentcaps(sac,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 50, 1),
	    "OpenBSD AgentX regress")) == NULL)
		fatal("agentx_agentcaps");
	if ((regress = agentx_region(sac,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100), 0)) == NULL)
		fatal("agentx_region application");
	if ((regressobj_scalarinteger = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 1), NULL, 0,
	    0, regress_scalarinteger)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalarstring = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 2), NULL, 0,
	    0, regress_scalarstring)) == NULL)
		fatal("agentx_object");
/* netsnmp doesn't return NULL-objects */
/*
	if ((regressobj_scalarnull = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 3), NULL, 0,
	    0, regress_scalarnull)) == NULL)
		fatal("agentx_object");
*/
	if ((regressobj_scalaroid = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 4), NULL, 0,
	    0, regress_scalaroid)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalaripaddress = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 5), NULL, 0,
	    0, regress_scalaripaddress)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalarcounter32 = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 6), NULL, 0,
	    0, regress_scalarcounter32)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalargauge32 = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 7), NULL, 0,
	    0, regress_scalargauge32)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalartimeticks = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 8), NULL, 0,
	    0, regress_scalartimeticks)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalaropaque = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 9), NULL, 0,
	    0, regress_scalaropaque)) == NULL)
		fatal("agentx_object");
	if ((regressobj_scalarcounter64 = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 10), NULL, 0,
	    0, regress_scalarcounter64)) == NULL)
		fatal("agentx_object");

	if ((regressidx_int = agentx_index_integer_dynamic(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 11, 1, 1))) == NULL)
		fatal("agentx_index_integer_dynamic");
	if ((regressobj_intindexint = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 11, 1, 1),
	    &regressidx_int, 1, 0, regress_intindex)) == NULL)
		fatal("agentx_object");

	if ((regressidx_int2 = agentx_index_integer_dynamic(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 12, 1, 1))) == NULL)
		fatal("agentx_index_integer_dynamic");
	idx[0] = regressidx_int;
	idx[1] = regressidx_int2;
	if ((regressobj_intindexint2 = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 12, 1, 1),
	    idx, 2, 0, regress_intindex2)) == NULL)
		fatal("agentx_object");

	if ((regressidx_str = agentx_index_string_dynamic(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 13, 1, 1))) == NULL)
		fatal("agentx_index_string_dynamic");
	if ((regressobj_strindexstr = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 13, 1, 1),
	    &regressidx_str, 1, 0, regress_strindex)) == NULL)
		fatal("agentx_object");
	if ((regressobj_implstrindexstr = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 14, 1, 1),
	    &regressidx_str, 1, 1, regress_implstrindex)) == NULL)
		fatal("agentx_object");
	if ((regressidx_str2 = agentx_index_string_dynamic(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 15, 1, 1))) == NULL)
		fatal("agentx_index_string_dynamic");
	idx[0] = regressidx_str;
	idx[1] = regressidx_str2;
	if ((regressobj_strindexstr = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 15, 1, 1),
	    idx, 2, 0, regress_strindex2)) == NULL)
		fatal("agentx_object");

	if ((regressidx_oid = agentx_index_oid_dynamic(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 16, 1, 1))) == NULL)
		fatal("agentx_index_oid_dynamic");
	if ((regressobj_oidimplindexoid = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 16, 1, 1),
	    &regressidx_oid, 1, 1, regress_oidimplindex)) == NULL)
		fatal("agentx_object");

	if ((regressidx_ipaddress = agentx_index_ipaddress_dynamic(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 17, 1, 1))) == NULL)
		fatal("agentx_index_oid_dynamic");
	if ((regressobj_oidimplindexoid = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 17, 1, 1),
	    &regressidx_ipaddress, 1, 0, regress_ipaddressindex)) == NULL)
		fatal("agentx_object");

	if ((regressidx_value = agentx_index_integer_value(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 18, 1, 1), 5)) == NULL)
		fatal("agentx_index_oid_dynamic");
	if ((regressobj_intindexstaticvalueint = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 18, 1, 1),
	    &regressidx_value, 1, 0, regress_intindexstaticvalueint)) == NULL)
		fatal("agentx_object");
	if ((regressobj_intindexstaticvaluestring = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 18, 1, 2),
	    &regressidx_value, 1, 0, regress_intindexstaticvaluestring)) == NULL)
		fatal("agentx_object");

	if ((regressidx_any = agentx_index_integer_any(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 19, 1, 1))) == NULL)
		fatal("agentx_index_integer_any");
	if ((regressobj_intindexstaticanyint = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 19, 1, 1),
	    &regressidx_any, 1, 0, regress_intindexstaticanyint)) == NULL)
		fatal("agentx_object");
	if ((regressobj_intindexstaticanystring = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 19, 1, 2),
	    &regressidx_any, 1, 0, regress_intindexstaticanystring)) == NULL)
		fatal("agentx_object");

	if ((regressidx_new = agentx_index_integer_new(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 20, 1, 1))) == NULL)
		fatal("agentx_index_integer_new");
	if ((regressobj_intindexstaticnewint = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 20, 1, 1),
	    &regressidx_new, 1, 0, regress_intindexstaticnewint)) == NULL)
		fatal("agentx_object");
	if ((regressobj_intindexstaticnewstring = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, 20, 1, 2),
	    &regressidx_new, 1, 0, regress_intindexstaticnewstring)) == NULL)
		fatal("agentx_object");

	if ((regressobj_scalarerror = agentx_object(regress,
	    AGENTX_OID(AGENTX_ENTERPRISES, 30155, 100, UINT32_MAX), NULL,
	    0, 0, regress_scalarerror)) == NULL)
		fatal("agentx_object");

	if (!dflag) {
		if (daemon(0, 1) == -1)
			fatalx("daemon");
	}

	event_init();

	event_set(&rev, fd, EV_READ|EV_PERSIST, regress_read, NULL);
	if (event_add(&rev, NULL) == -1)
		fatal("event_add");

	event_dispatch();
	return 0;
}

void
regress_fd(struct agentx *sa2, void *cookie, int close)
{
	static int init = 0;
	struct sockaddr_un sun;

	/* For ease of cleanup we take the single run approach */
	if (init) {
		if (!close)
			agentx_free(sa);
		else
			evtimer_del(&rev);
	} else {
		sun.sun_family = AF_UNIX;
		strlcpy(sun.sun_path, path, sizeof(sun.sun_path));

		if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ||
		    connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
			fatal("connect");
		}
		agentx_connect(sa2, fd);
		init = 1;
	}
}

void
regress_read(int fd, short event, void *cookie)
{
	agentx_read(sa);
}

#ifdef notyet

void
regress_usr1(int fd, short event, void *cookie)
{
	struct agentx_notify *san;

	if ((san = agentx_notify(sac, AGENTX_OID(LINKDOWN))) == NULL)
		fatal("agentx_notify");

	agentx_notify_integer(san, AGENTX_OID(IFINDEX), 1);
	agentx_notify_integer(san, AGENTX_OID(IFADMINSTATUS), 3);
	agentx_notify_integer(san, AGENTX_OID(IFOPERSTATUS), 6);
	agentx_notify_send(san);
}

#endif

void
regress_usr2(int fd, short event, void *cookie)
{
}

void
regress_scalarstring(struct agentx_varbind *vb)
{
	agentx_varbind_string(vb, "scalar-string");
}

void
regress_scalarinteger(struct agentx_varbind *vb)
{
	agentx_varbind_integer(vb, 1);
}

void
regress_scalarnull(struct agentx_varbind *vb)
{
	agentx_varbind_null(vb);
}

void
regress_scalaroid(struct agentx_varbind *vb)
{
	agentx_varbind_oid(vb, AGENTX_OID(AGENTX_ENTERPRISES, 30155));
}

void
regress_scalaripaddress(struct agentx_varbind *vb)
{
	struct in_addr addr;

	inet_aton("127.0.0.1", &addr);

	agentx_varbind_ipaddress(vb, &addr);
}

void
regress_scalarcounter32(struct agentx_varbind *vb)
{
	agentx_varbind_counter32(vb, 1);
}

void
regress_scalargauge32(struct agentx_varbind *vb)
{
	agentx_varbind_gauge32(vb, 1);
}

void
regress_scalartimeticks(struct agentx_varbind *vb)
{
	agentx_varbind_timeticks(vb, 1);
}

void
regress_scalaropaque(struct agentx_varbind *vb)
{
	agentx_varbind_opaque(vb, "abc", 3);
}

void
regress_scalarcounter64(struct agentx_varbind *vb)
{
	agentx_varbind_counter64(vb, 1);
}

void
regress_intindex(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_int);
	switch (agentx_varbind_request(vb)) {
	case AGENTX_REQUEST_TYPE_GET:
		if (idx == 0) {
			agentx_varbind_notfound(vb);
			return;
		}
		break;
	case AGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE:
		if (idx == 0)
			idx++;
		break;
	case AGENTX_REQUEST_TYPE_GETNEXT:
		idx++;
		break;
	}
	if (idx > 0xf)
		agentx_varbind_notfound(vb);
	else {
		agentx_varbind_set_index_integer(vb, regressidx_int, idx);
		agentx_varbind_integer(vb, idx);
	}
}

void
regress_intindex2(struct agentx_varbind *vb)
{
	uint32_t idx1, idx2;
	enum agentx_request_type type;

	idx1 = agentx_varbind_get_index_integer(vb, regressidx_int);
	idx2 = agentx_varbind_get_index_integer(vb, regressidx_int2);
	type = agentx_varbind_request(vb);
	if (type == AGENTX_REQUEST_TYPE_GETNEXT)
		idx2++;
	if (type == AGENTX_REQUEST_TYPE_GETNEXT ||
	    type == AGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE) {
		if (idx2 > 1) {
			idx1++;
			idx2 = 0;
		}
	}
	if (idx2 > 1 || idx1 > 8)
		agentx_varbind_notfound(vb);
	else {
		agentx_varbind_set_index_integer(vb, regressidx_int, idx1);
		agentx_varbind_set_index_integer(vb, regressidx_int2, idx2);
		agentx_varbind_integer(vb, (idx1 << 1) + idx2);
	}
}

void
regress_strindex(struct agentx_varbind *vb)
{
	const unsigned char *idx;
	size_t slen;
	int implied;
	enum agentx_request_type request = agentx_varbind_request(vb);

	idx = agentx_varbind_get_index_string(vb, regressidx_str, &slen,
	    &implied);

	if (implied)
		fatalx("%s: string length should not be implied", __func__);

	if (slen == 0) {
		if (request == AGENTX_REQUEST_TYPE_GET) {
			log_warnx("%s: 0 index should be handled in agentx.c",
			    __func__);
			agentx_varbind_error(vb);
			return;
		}
	}
	/* !implied first needs a length check before content check */
	if (slen > 1) {
		agentx_varbind_notfound(vb);
		return;
	}
	if (request == AGENTX_REQUEST_TYPE_GETNEXT) {
		if (idx[0] == 'a')
			idx = (unsigned char *)"b";
		else if (idx[0] == 'b')
			idx = (unsigned char *)"c";
		else if (idx[0] >= 'c') {
			agentx_varbind_notfound(vb);
			return;
		}
	}
	if (idx == NULL || idx[0] < 'a') {
		if (request == AGENTX_REQUEST_TYPE_GET) {
			agentx_varbind_notfound(vb);
			return;
		}
		idx = (unsigned char *)"a";
	}

	agentx_varbind_set_index_string(vb, regressidx_str,
	    (const char *)idx);
	agentx_varbind_string(vb, (const char *)idx);
}

void
regress_implstrindex(struct agentx_varbind *vb)
{
	const unsigned char *idx;
	size_t slen;
	int implied;
	enum agentx_request_type request = agentx_varbind_request(vb);

	idx = agentx_varbind_get_index_string(vb, regressidx_str, &slen,
	    &implied);

	if (!implied)
		fatalx("%s: string length should be implied", __func__);

	if (slen == 0) {
		if (request == AGENTX_REQUEST_TYPE_GET)
			fatalx("%s: 0 index should be handled in agentx.c",
			    __func__);
	}
	if (request == AGENTX_REQUEST_TYPE_GET && (slen != 1 ||
	    (idx[0] != 'a' && idx[0] != 'b' && idx[0] != 'c'))) {
		agentx_varbind_notfound(vb);
		return;
	}
	/* implied doesn't needs a length check before content check */
	if (request == AGENTX_REQUEST_TYPE_GETNEXT) {
		if (idx[0] == 'a')
			idx = (const unsigned char *)"b";
		else if (idx[0] == 'b')
			idx = (const unsigned char *)"c";
		else if (idx[0] >= 'c') {
			agentx_varbind_notfound(vb);
			return;
		}
	}
	if (idx == NULL || idx[0] < 'a')
		idx = (const unsigned char *)"a";

	agentx_varbind_set_index_string(vb, regressidx_str,
	    (const char *)idx);
	agentx_varbind_string(vb, (const char *)idx);
}

void
regress_strindex2(struct agentx_varbind *vb)
{
	/* Opt is !implied sorted */
	const char *opt1[] = {"a", "b", "c"};
	const char *opt2[] = {"b", "aa", "bb"};
	size_t opt1len, opt2len;
	size_t opt1i, opt2i;
	const unsigned char *idx1, *idx2;
	size_t slen1, slen2;
	int implied1, implied2;
	enum agentx_request_type request = agentx_varbind_request(vb);
	int match;

	idx1 = agentx_varbind_get_index_string(vb, regressidx_str, &slen1,
	    &implied1);
	idx2 = agentx_varbind_get_index_string(vb, regressidx_str2, &slen2,
	    &implied2);

	/* agentx.c debugging checks */
	if (implied1 || implied2)
		fatalx("%s: string length should not be implied", __func__);
	if (slen1 == 0 || slen2 == 0) {
		if (request == AGENTX_REQUEST_TYPE_GET)
			fatalx("%s: 0 index should be handled in agentx.c",
			    __func__);
	}

	opt1len = sizeof(opt1) / sizeof(*opt1);
	match = 0;
	for (opt1i = 0; opt1i < opt1len; opt1i++) {
		if (strlen(opt1[opt1i]) < slen1 ||
		    (strlen(opt1[opt1i]) == slen1 &&
		    memcmp(opt1[opt1i], idx1, slen1) < 0)) {
			continue;
		}
		if (strlen(opt1[opt1i]) == slen1 &&
		    memcmp(opt1[opt1i], idx1, slen1) == 0)
			match = 1;
		break;
	}
	if (opt1i == opt1len) {
		agentx_varbind_notfound(vb);
		return;
	}
	opt2len = sizeof(opt2) / sizeof(*opt2);
	for (opt2i = 0; opt2i < opt2len; opt2i++) {
		if (!match)
			break;
		if (strlen(opt2[opt2i]) < slen2 ||
		    (strlen(opt2[opt2i]) == slen2 &&
		    memcmp(opt2[opt2i], idx2, slen2) < 0)) {
			continue;
		}
		if (strlen(opt2[opt2i]) != slen2 ||
		    memcmp(opt2[opt2i], idx2, slen2) > 0)
			match = 0;
		break;
	}
	if (opt2i == opt2len)
		match = 0;
	if (request == AGENTX_REQUEST_TYPE_GET) {
		if (!match) {
			agentx_varbind_notfound(vb);
			return;
		}
	} else {
		if (AGENTX_REQUEST_TYPE_GETNEXT && match)
			opt2i++;
		if (opt2i >= opt2len) {
			if (++opt1i == opt1len) {
				agentx_varbind_notfound(vb);
				return;
			}
			opt2i = 0;
		}
	}

	agentx_varbind_set_index_string(vb, regressidx_str, opt1[opt1i]);
	agentx_varbind_set_index_string(vb, regressidx_str2, opt2[opt2i]);
	agentx_varbind_printf(vb, "%s - %s", opt1[opt1i], opt2[opt2i]);
}

void
regress_oidimplindex(struct agentx_varbind *vb)
{
	struct agentx_object *obj;
	const uint32_t *idx;
	size_t oidlen;
	int implied;
	enum agentx_request_type request = agentx_varbind_request(vb);

	idx = agentx_varbind_get_index_oid(vb, regressidx_oid, &oidlen,
	    &implied);

	if (!implied)
		fatalx("%s: string length should be implied", __func__);

	if (request == AGENTX_REQUEST_TYPE_GET)
		obj = agentx_context_object_find(sac, idx, oidlen, 1, 1);
	else
		obj = agentx_context_object_nfind(sac, idx, oidlen, 1,
		    request == AGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE);

	if (obj == NULL) {
		agentx_varbind_notfound(vb);
		return;
	}

	agentx_varbind_set_index_object(vb, regressidx_oid, obj);
	agentx_varbind_object(vb, obj);
}

void
regress_ipaddressindex(struct agentx_varbind *vb)
{
	const struct in_addr *addr;
	struct in_addr addrlist[4];
	enum agentx_request_type request = agentx_varbind_request(vb);
	size_t i;
	int cmp;

	inet_pton(AF_INET, "10.0.0.0", &(addrlist[0]));
	inet_pton(AF_INET, "127.0.0.1", &(addrlist[1]));
	inet_pton(AF_INET, "172.16.0.0", &(addrlist[2]));
	inet_pton(AF_INET, "192.168.0.0", &(addrlist[3]));

	addr = agentx_varbind_get_index_ipaddress(vb, regressidx_ipaddress);

	for (i = 0; i < 4; i++) {
		if ((cmp = memcmp(&(addrlist[i]), addr, sizeof(*addr))) == 0) {
			if (request == AGENTX_REQUEST_TYPE_GET ||
			    request == AGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE)
				break;
		} else if (cmp > 0) {
			if (request == AGENTX_REQUEST_TYPE_GET) {
				agentx_varbind_notfound(vb);
				return;
			}
			break;
		}
	}
	if (i == 4) {
		agentx_varbind_notfound(vb);
		return;
	}

	agentx_varbind_set_index_ipaddress(vb, regressidx_ipaddress,
	    &(addrlist[i]));
	agentx_varbind_ipaddress(vb, &(addrlist[i]));
}

void
regress_intindexstaticvalueint(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_value);
	agentx_varbind_integer(vb, idx);
}

void
regress_intindexstaticvaluestring(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_value);
	agentx_varbind_printf(vb, "%u", idx);
}

void
regress_intindexstaticanyint(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_any);
	agentx_varbind_integer(vb, idx);
}

void
regress_intindexstaticanystring(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_any);
	agentx_varbind_printf(vb, "%u", idx);
}

void
regress_intindexstaticnewint(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_new);
	agentx_varbind_integer(vb, idx);
}

void
regress_intindexstaticnewstring(struct agentx_varbind *vb)
{
	uint32_t idx;

	idx = agentx_varbind_get_index_integer(vb, regressidx_new);
	agentx_varbind_printf(vb, "%u", idx);
}

void
regress_scalarerror(struct agentx_varbind *vb)
{
	agentx_varbind_error(vb);
}