summaryrefslogtreecommitdiffstats
path: root/usr.sbin/relayd/agentx.c
blob: 33708f0a4b9058f5b01de1ecc6eda2bc67953b92 (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
/*      $OpenBSD: agentx.c,v 1.15 2020/06/05 19:50:59 denis Exp $    */
/*
 * Copyright (c) 2013,2014 Bret Stephen Lambert <blambert@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/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/un.h>

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "snmp.h"

int	snmp_agentx_octetstring(struct agentx_pdu *, char *, int);
int	snmp_agentx_buffercheck(struct agentx_pdu *, size_t);
int	snmp_agentx_oid(struct agentx_pdu *, struct snmp_oid *);
int	snmp_agentx_buffer_consume(struct agentx_pdu *, u_int);
int	snmp_agentx_int(struct agentx_pdu *, uint32_t *);
int	snmp_agentx_int64(struct agentx_pdu *, uint64_t *);
int	snmp_agentx_do_read_raw(struct agentx_pdu *, void *, int, int);
void	snmp_agentx_update_ids(struct agentx_handle *, struct agentx_pdu *);
struct agentx_pdu *
	agentx_find_inflight(struct agentx_handle *, uint32_t, uint32_t);
int	snmp_agentx_do_read_oid(struct agentx_pdu *, struct snmp_oid *, int *);

#ifdef DEBUG
static void	snmp_agentx_dump_hdr(struct agentx_hdr *);
#endif

#define PDU_BUFLEN 256

/* snmpTrapOid.0 */
struct snmp_oid trapoid_0 = {
	.o_id =	{ 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 },
	.o_n = 11
};

/*
 * AgentX handle allocation and management routines.
 */

struct agentx_handle *
snmp_agentx_alloc(int s)
{
	struct agentx_handle *h;

	if ((h = calloc(1, sizeof(*h))) == NULL)
		return (NULL);

	h->fd = s;
	h->timeout = AGENTX_DEFAULT_TIMEOUT;

	TAILQ_INIT(&h->w);
	TAILQ_INIT(&h->inflight);

	return (h);
}

/*
 * Synchronous open of unix socket path.
 */
struct agentx_handle *
snmp_agentx_open(const char *path, char *descr, struct snmp_oid *oid)
{
	struct sockaddr_un	 sun;
	struct agentx_handle	*h;
	int s;

	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
		return (NULL);

	bzero(&sun, sizeof(sun));
	sun.sun_family = AF_UNIX;
	if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
	    sizeof(sun.sun_path))
		goto fail;

	if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) == -1)
		goto fail;

	if ((h = snmp_agentx_fdopen(s, descr, oid)) == NULL)
		goto fail;

	return (h);
 fail:
	close(s);
	return (NULL);
}

/*
 * Synchronous AgentX open operation over previously-opened socket.
 */
struct agentx_handle *
snmp_agentx_fdopen(int s, char *descr, struct snmp_oid *oid)
{
	struct agentx_handle	*h;
	struct agentx_pdu	*pdu = NULL;

	if ((h = snmp_agentx_alloc(s)) == NULL)
		return (NULL);

	if ((pdu = snmp_agentx_open_pdu(h, descr, oid)) == NULL ||
	    (pdu = snmp_agentx_request(h, pdu)) == NULL ||
	    snmp_agentx_open_response(h, pdu) == -1) {
		if (pdu)
			snmp_agentx_pdu_free(pdu);
		snmp_agentx_free(h);
		return (NULL);
	}

	return (h);
}

/*
 * Synchronous close of agentx handle.
 */
int
snmp_agentx_close(struct agentx_handle *h, uint8_t reason)
{
	struct agentx_pdu			*pdu;
	int					 error = 0;

	if ((pdu = snmp_agentx_close_pdu(h, reason)) == NULL)
		return (-1);
	if ((pdu = snmp_agentx_request(h, pdu)) == NULL)
		return (-1);
	if (snmp_agentx_response(h, pdu) == -1)
		error = -1;

	snmp_agentx_pdu_free(pdu);

	return (error);
}

void
snmp_agentx_free(struct agentx_handle *h)
{
	struct agentx_pdu *pdu;

	if (h->fd != -1)
		close(h->fd);

	while ((pdu = TAILQ_FIRST(&h->w))) {
		TAILQ_REMOVE(&h->w, pdu, entry);
		snmp_agentx_pdu_free(pdu);
	}
	while ((pdu = TAILQ_FIRST(&h->inflight))) {
		TAILQ_REMOVE(&h->w, pdu, entry);
		snmp_agentx_pdu_free(pdu);
	}
	if (h->r)
		snmp_agentx_pdu_free(h->r);

	free(h);
}

/*
 * AgentX pdu allocation routines.
 */

/*
 * Allocate an AgentX PDU.
 */
struct agentx_pdu *
snmp_agentx_pdu_alloc(void)
{
	struct agentx_pdu	*pdu;

	if ((pdu = calloc(1, sizeof(*pdu))) == NULL)
		return (NULL);
	if ((pdu->buffer = calloc(PDU_BUFLEN, sizeof(uint8_t))) == NULL) {
		free(pdu);
		return (NULL);
	}

	pdu->buflen = PDU_BUFLEN;

	bzero(pdu->buffer, pdu->buflen);
	pdu->ptr = pdu->buffer + sizeof(struct agentx_hdr);
	pdu->ioptr = pdu->buffer;
	pdu->hdr = (struct agentx_hdr *)pdu->buffer;
	pdu->hdr->version = AGENTX_VERSION;
	pdu->hdr->flags = AGENTX_LOCAL_BYTE_ORDER_FLAG;
	pdu->hdr->reserved = 0;
	pdu->hdr->length = 0;
	pdu->datalen = sizeof(struct agentx_hdr);

	return (pdu);
}

/*
 * Read the response PDU for a generic operation.
 */
int
snmp_agentx_response(struct agentx_handle *h, struct agentx_pdu *pdu)
{
	struct agentx_response_data resp;

	if (snmp_agentx_read_raw(pdu, &resp, sizeof(resp)) == -1)
		return (-1);

	if (!snmp_agentx_byteorder_native(pdu->hdr)) {
		resp.error = snmp_agentx_int16_byteswap(resp.error);
		resp.index = snmp_agentx_int16_byteswap(resp.index);
	}

	h->error = resp.error;
	if (resp.error != AGENTX_ERR_NONE)
		return (-1);

	return (0);
}

/*
 * Read the response PDU for an open operation.
 */
int
snmp_agentx_open_response(struct agentx_handle *h, struct agentx_pdu *pdu)
{

	if (snmp_agentx_response(h, pdu) == -1)
		return (-1);

	h->sessionid = pdu->hdr->sessionid;
	return (0);
}

void
snmp_agentx_pdu_free(struct agentx_pdu *pdu)
{
	free(pdu->buffer);
	free(pdu->request);
	free(pdu);
}

int
snmp_agentx_buffer_consume(struct agentx_pdu *b, u_int len)
{
	int padding;

	padding = ((len + 3) & ~0x03) - len;

	if (b->datalen < (len + padding))
		return (-1);

	b->datalen -= len + padding;
	b->ptr += len + padding;

	return (0);
}

/*
 * Send an AgentX PDU. Flushes any already-enqueued PDUs.
 */
int
snmp_agentx_send(struct agentx_handle *h, struct agentx_pdu *pdu)
{
	ssize_t n;

	/* set the appropriate IDs in the protocol header */
	if (pdu != NULL &&
	    (pdu->datalen == pdu->hdr->length + sizeof(struct agentx_hdr))) {
		pdu->hdr->sessionid = h->sessionid;

		if (pdu->hdr->type != AGENTX_RESPONSE) {
			++h->transactid;
			++h->packetid;
		}

		pdu->hdr->transactid = h->transactid;
		pdu->hdr->packetid = h->packetid;
		TAILQ_INSERT_TAIL(&h->w, pdu, entry);
	}

 again:
	if ((pdu = TAILQ_FIRST(&h->w)) == NULL)
		return (0);

	if ((n = send(h->fd, pdu->ioptr, pdu->datalen, 0)) == -1)
		return (-1);

	pdu->ioptr += n;
	pdu->datalen -= n;

	if (pdu->datalen > 0) {
		errno = EAGAIN;
		return (-1);
	}

#ifdef DEBUG
	snmp_agentx_dump_hdr(pdu->hdr);
#endif

	TAILQ_REMOVE(&h->w, pdu, entry);
	TAILQ_INSERT_TAIL(&h->inflight, pdu, entry);

	goto again;
}

/*
 * Attempt to read a single AgentX PDU.
 */
struct agentx_pdu *
snmp_agentx_recv(struct agentx_handle *h)
{
	struct agentx_pdu *pdu, *match;
	ssize_t n;

	h->error = AGENTX_ERR_NONE;

	if (h->r == NULL) {
		if ((h->r = snmp_agentx_pdu_alloc()) == NULL)
			return (NULL);
		h->r->datalen = 0;	/* XXX force this for receive buffers */
	}
	pdu = h->r;

	if (snmp_agentx_buffercheck(pdu, sizeof(struct agentx_hdr)) == -1)
		return (NULL);

	/* read header */
	if (pdu->datalen < sizeof(struct agentx_hdr)) {
		n = recv(h->fd, pdu->ioptr, sizeof(struct agentx_hdr), 0);

		if (n == 0 || n == -1)
			return (NULL);

		pdu->datalen += n;
		pdu->ioptr += n;

		if (pdu->datalen < sizeof(struct agentx_hdr)) {
			errno = EAGAIN;
			return (NULL);
		}

		if (pdu->hdr->version != AGENTX_VERSION) {
			h->error = AGENTX_ERR_PARSE_ERROR;
			return (NULL);
		}

		if (snmp_agentx_buffercheck(pdu, pdu->hdr->length) == -1)
			return (NULL);
	}

	/* read body */
	if (pdu->hdr->length > 0) {
		n = recv(h->fd, pdu->ioptr, pdu->hdr->length, 0);

		if (n == 0 || n == -1)
			return (NULL);

		pdu->datalen += n;
		pdu->ioptr += n;
	}

	if (pdu->datalen < pdu->hdr->length + sizeof(struct agentx_hdr)) {
		errno = EAGAIN;
		return (NULL);
	}

	if (pdu->hdr->version != AGENTX_VERSION) {
		h->error = AGENTX_ERR_PARSE_ERROR;
		goto fail;
	}

	/* If this is an open on a new connection, fix it up */
	if (pdu->hdr->type == AGENTX_OPEN && h->sessionid == 0) {
		pdu->hdr->sessionid = 0;		/* ignored, per RFC */
		h->transactid = pdu->hdr->transactid;
		h->packetid = pdu->hdr->packetid;
	}

	if (pdu->hdr->type == AGENTX_RESPONSE) {

		match = agentx_find_inflight(h, pdu->hdr->transactid,
		    pdu->hdr->packetid);
		if (match == NULL) {
			errno = ESRCH;		/* XXX */
			goto fail;
		}

		TAILQ_REMOVE(&h->inflight, match, entry);
		pdu->request = match;
		h->r = NULL;

	} else {
		if (pdu->hdr->sessionid != h->sessionid) {
			h->error = AGENTX_ERR_NOT_OPEN;
			goto fail;
		}

		if (pdu->hdr->flags & AGENTX_NON_DEFAULT_CONTEXT) {
			h->error = AGENTX_ERR_UNSUPPORTED_CONTEXT;
			goto fail;
		}

		snmp_agentx_update_ids(h, pdu);              /* XXX */

		if (pdu->datalen != pdu->hdr->length + sizeof(*pdu->hdr)) {
			h->error = AGENTX_ERR_PARSE_ERROR;
			goto fail;
		}
	}

#ifdef DEBUG
	snmp_agentx_dump_hdr(pdu->hdr);
#endif
	h->r = NULL;

	return (pdu);

 fail:
#ifdef DEBUG
	snmp_agentx_dump_hdr(pdu->hdr);
#endif
	snmp_agentx_pdu_free(pdu);
	h->r = NULL;

	return (NULL);
}

/*
 * Synchonous request and receipt of response.
 */
struct agentx_pdu *
snmp_agentx_request(struct agentx_handle *h, struct agentx_pdu *pdu)
{

	if (snmp_agentx_send(h, pdu) == -1) {
		if (errno != EAGAIN)
			return (NULL);
	}
	while (snmp_agentx_send(h, NULL) == -1) {
		if (errno != EAGAIN)
			return (NULL);
	}
	while ((pdu = snmp_agentx_recv(h)) == NULL) {
		if (errno != EAGAIN)
			return (NULL);
	}
	h->error = AGENTX_ERR_NONE;

	return (pdu);
}

struct agentx_pdu *
agentx_find_inflight(struct agentx_handle *h, uint32_t tid, uint32_t pid)
{
	struct agentx_pdu *pdu;

	TAILQ_FOREACH(pdu, &h->inflight, entry)
		if (pdu->hdr->transactid == tid && pdu->hdr->packetid == pid)
			break;
	return (pdu);
}

int
snmp_agentx_buffercheck(struct agentx_pdu *pdu, size_t len)
{
	uint8_t *newptr;
	size_t newlen;

	if (pdu->buflen - pdu->datalen >= len)
		return (0);

	newlen = pdu->buflen + len;
	if (newlen < pdu->buflen || newlen < len)
		return (-1);

	if ((newptr = realloc(pdu->buffer, newlen)) == NULL)
		return (-1);

	pdu->buflen = newlen;
	pdu->ioptr = &newptr[pdu->ioptr - pdu->buffer];
	pdu->buffer = newptr;
	pdu->hdr = (struct agentx_hdr *)pdu->buffer;
	pdu->ptr = &pdu->buffer[pdu->datalen];

	return (0);
}

/*
 * Utility routines for initializing common AgentX PDUs.
 */

struct agentx_pdu *
snmp_agentx_open_pdu(struct agentx_handle *h, char *descr,
    struct snmp_oid *oid)
{
	struct agentx_open_timeout to;
	struct snmp_oid nulloid;
	struct agentx_pdu *pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);

	pdu->hdr->type = AGENTX_OPEN;

	if (oid == NULL) {
		bzero(&nulloid, sizeof(nulloid));
		oid = &nulloid;
	}

	bzero(&to, sizeof(to));
	to.timeout = AGENTX_DEFAULT_TIMEOUT;

	if (snmp_agentx_raw(pdu, &to, sizeof(to)) == -1 ||
	    snmp_agentx_oid(pdu, oid) == -1 ||
	    snmp_agentx_octetstring(pdu, descr, strlen(descr)) == -1)
		goto fail;

	return (pdu);
 fail:
	snmp_agentx_pdu_free(pdu);
	return (NULL);
}

struct agentx_pdu *
snmp_agentx_close_pdu(struct agentx_handle *h, uint8_t reason)
{
	struct agentx_close_request_data	 req;
	struct agentx_pdu			*pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);
	pdu->hdr->type = AGENTX_CLOSE;

	bzero(&req, sizeof(req));
	req.reason = reason;

	if (snmp_agentx_raw(pdu, &req, sizeof(req)) == -1) {
		snmp_agentx_pdu_free(pdu);
		return (NULL);
	}

	return (pdu);
}

struct agentx_pdu *
snmp_agentx_notify_pdu(struct snmp_oid *oid)
{
	struct agentx_pdu	*pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);
	pdu->hdr->type = AGENTX_NOTIFY;

	if (snmp_agentx_varbind(pdu, &trapoid_0,
	    AGENTX_OBJECT_IDENTIFIER, oid, sizeof(*oid)) == -1) {
		snmp_agentx_pdu_free(pdu);
		return (NULL);
	}

	return (pdu);
}

struct agentx_pdu *
snmp_agentx_response_pdu(int uptime, int error, int idx)
{
	struct agentx_response_data	 resp;
	struct agentx_pdu		*pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);
	pdu->hdr->type = AGENTX_RESPONSE;

	resp.sysuptime = uptime;
	resp.error = error;
	resp.index = idx;

	if (snmp_agentx_raw(pdu, &resp, sizeof(resp)) == -1) {
		snmp_agentx_pdu_free(pdu);
		return (NULL);
	}

	return (pdu);
}

struct agentx_pdu *
snmp_agentx_ping_pdu(void)
{
	struct agentx_pdu *pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);
	pdu->hdr->version = AGENTX_VERSION;
	pdu->hdr->type = AGENTX_PING;

	return (pdu);
}

struct agentx_pdu *
snmp_agentx_register_pdu(struct snmp_oid *oid, int timeout, int range_index,
    int range_bound)
{
	struct agentx_register_hdr	 rhdr;
	struct agentx_pdu		*pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);

	pdu->hdr->version = AGENTX_VERSION;
	pdu->hdr->type = AGENTX_REGISTER;

	rhdr.timeout = timeout;
	rhdr.priority = AGENTX_REGISTER_PRIO_DEFAULT;
	rhdr.subrange = range_index;
	rhdr.reserved = 0;

	if (snmp_agentx_raw(pdu, &rhdr, sizeof(rhdr)) == -1 ||
	    snmp_agentx_oid(pdu, oid) == -1 ||
	    (range_index && snmp_agentx_int(pdu, &range_bound) == -1)) {
		snmp_agentx_pdu_free(pdu);
		return (NULL);
	}

	return (pdu);
}

struct agentx_pdu *
snmp_agentx_unregister_pdu(struct snmp_oid *oid, int range_index,
    int range_bound)
{
	struct agentx_unregister_hdr	 uhdr;
	struct agentx_pdu		*pdu;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);

	pdu->hdr->version = AGENTX_VERSION;
	pdu->hdr->type = AGENTX_UNREGISTER;

	uhdr.reserved1 = 0;
	uhdr.priority = AGENTX_REGISTER_PRIO_DEFAULT;
	uhdr.subrange = range_index;
	uhdr.reserved2 = 0;

	if (snmp_agentx_raw(pdu, &uhdr, sizeof(uhdr)) == -1 ||
	    snmp_agentx_oid(pdu, oid) == -1 ||
	    (range_index && snmp_agentx_int(pdu, &range_bound) == -1)) {
		snmp_agentx_pdu_free(pdu);
		return (NULL);
	}

	return (pdu);
}

struct agentx_pdu *
snmp_agentx_get_pdu(struct snmp_oid oid[], int noid)
{
	struct snmp_oid		 nulloid;
	struct agentx_pdu	*pdu;
	int			 i;

	if ((pdu = snmp_agentx_pdu_alloc()) == NULL)
		return (NULL);

	pdu->hdr->version = AGENTX_VERSION;
	pdu->hdr->type = AGENTX_GET;

	bzero(&nulloid, sizeof(nulloid));

	for (i = 0; i < noid; i++) {
		if (snmp_agentx_oid(pdu, &oid[i]) == -1 ||
		    snmp_agentx_oid(pdu, &nulloid) == -1) {
			snmp_agentx_pdu_free(pdu);
			return (NULL);
		}
	}

	return (pdu);
}

/*
 * AgentX PDU write routines.
 */

int
snmp_agentx_raw(struct agentx_pdu *pdu, void *data, int len)
{

	if (snmp_agentx_buffercheck(pdu, len) == -1)
		return (-1);

	memcpy(pdu->ptr, data, len);

	pdu->hdr->length += len;
	pdu->ptr += len;
	pdu->datalen += len;

	return (0);
}

int
snmp_agentx_int(struct agentx_pdu *pdu, uint32_t *i)
{
	return (snmp_agentx_raw(pdu, i, sizeof(*i)));
}

int
snmp_agentx_int64(struct agentx_pdu *pdu, uint64_t *i)
{
	return (snmp_agentx_raw(pdu, i, sizeof(*i)));
}

int
snmp_agentx_octetstring(struct agentx_pdu *pdu, char *str, int len)
{
	static uint8_t pad[4] = { 0, 0, 0, 0 };
	int padding;
	uint32_t l;

	padding = ((len + 3) & ~0x03) - len;

	l = len;
	if (snmp_agentx_int(pdu, &l) == -1 ||
	    snmp_agentx_raw(pdu, str, len) == -1 ||
	    snmp_agentx_raw(pdu, pad, padding) == -1)
		return (-1);

	return (0);
}

int
snmp_agentx_oid(struct agentx_pdu *pdu, struct snmp_oid *oid)
{
	struct agentx_oid_hdr ohdr;
	u_int i, prefix;

	i = prefix = 0;

	if (oid->o_id[0] == 1 && oid->o_id[1] == 3 &&
	    oid->o_id[2] == 6 && oid->o_id[3] == 1 &&
	    oid->o_id[4] < 256) {
		prefix = oid->o_id[4];
		i = 5;
	}

	if (prefix)
		ohdr.n_subid = oid->o_n - 5;
	else
		ohdr.n_subid = oid->o_n;
	ohdr.prefix = prefix;
	ohdr.include = 0;
	ohdr.reserved = 0;

	if (snmp_agentx_raw(pdu, &ohdr, sizeof(ohdr)) == -1)
		return (-1);

	for (; i < oid->o_n; i++)
		if (snmp_agentx_int(pdu, &oid->o_id[i]) == -1)
			return (-1);

	return (0);
}

int
snmp_agentx_varbind(struct agentx_pdu *pdu, struct snmp_oid *oid, int type,
    void *data, int len)
{
	struct agentx_varbind_hdr vbhdr;

	vbhdr.type = type;
	vbhdr.reserved = 0;
	if (snmp_agentx_raw(pdu, &vbhdr, sizeof(vbhdr)) == -1)
		return (-1);

	if (snmp_agentx_oid(pdu, oid) == -1)
		return (-1);

	switch (type) {

	case AGENTX_NO_SUCH_OBJECT:
	case AGENTX_NO_SUCH_INSTANCE:
	case AGENTX_END_OF_MIB_VIEW:
	case AGENTX_NULL:
		/* no data follows the OID */
		return (0);

	case AGENTX_IP_ADDRESS:
	case AGENTX_OPAQUE:
	case AGENTX_OCTET_STRING:
		return (snmp_agentx_octetstring(pdu, data, len));

	case AGENTX_OBJECT_IDENTIFIER:
		return (snmp_agentx_oid(pdu, (struct snmp_oid *)data));

	case AGENTX_INTEGER:
	case AGENTX_COUNTER32:
	case AGENTX_GAUGE32:
	case AGENTX_TIME_TICKS:
		return (snmp_agentx_int(pdu, (uint32_t *)data));

	case AGENTX_COUNTER64:
		return (snmp_agentx_int64(pdu, (uint64_t *)data));

	default:
		return (-1);
	}
	/* NOTREACHED */
}

/*
 * AgentX PDU read routines.
 */

int
snmp_agentx_read_vbhdr(struct agentx_pdu *pdu,
    struct agentx_varbind_hdr *vbhdr)
{
	if (snmp_agentx_read_raw(pdu, vbhdr, sizeof(*vbhdr)) == -1)
		return (-1);
	if (!snmp_agentx_byteorder_native(pdu->hdr))
		vbhdr->type = snmp_agentx_int16_byteswap(vbhdr->type);
	return (0);
}

int
snmp_agentx_copy_raw(struct agentx_pdu *pdu, void *v, int len)
{
	return (snmp_agentx_do_read_raw(pdu, v, len, 0));
}

int
snmp_agentx_read_raw(struct agentx_pdu *pdu, void *v, int len)
{
	return (snmp_agentx_do_read_raw(pdu, v, len, 1));
}

int
snmp_agentx_do_read_raw(struct agentx_pdu *pdu, void *v, int len, int consume)
{
	void *ptr = pdu->ptr;

	if (consume)
		if (snmp_agentx_buffer_consume(pdu, len) == -1)
			return (-1);

	memcpy(v, ptr, len);

	return (0);
}

int
snmp_agentx_read_int(struct agentx_pdu *pdu, uint32_t *i)
{
	if (snmp_agentx_read_raw(pdu, i, sizeof(*i)) == -1)
		return (-1);
	if (!snmp_agentx_byteorder_native(pdu->hdr))
		*i = snmp_agentx_int_byteswap(*i);
	return (0);
}

int
snmp_agentx_read_int64(struct agentx_pdu *pdu, uint64_t *i)
{
	if (snmp_agentx_read_raw(pdu, i, sizeof(*i)) == -1)
		return (-1);
	if (!snmp_agentx_byteorder_native(pdu->hdr))
		*i = snmp_agentx_int64_byteswap(*i);
	return (0);
}

int
snmp_agentx_read_oid(struct agentx_pdu *pdu, struct snmp_oid *oid)
{
	int dummy;

	return (snmp_agentx_do_read_oid(pdu, oid, &dummy));
}

int
snmp_agentx_do_read_oid(struct agentx_pdu *pdu, struct snmp_oid *oid,
    int *include)
{
	struct agentx_oid_hdr ohdr;
	int i = 0;

	if (snmp_agentx_read_raw(pdu, &ohdr, sizeof(ohdr)) == -1)
		return (-1);

	bzero(oid, sizeof(*oid));

	if (ohdr.prefix != 0) {
		oid->o_id[0] = 1;
		oid->o_id[1] = 3;
		oid->o_id[2] = 6;
		oid->o_id[3] = 1;
		oid->o_id[4] = ohdr.prefix;
		i = 5;
	}

	while (ohdr.n_subid--)
		if (snmp_agentx_read_int(pdu, &oid->o_id[i++]) == -1)
			return (-1);

	oid->o_n = i;
	*include = ohdr.include;

	return (0);
}

int
snmp_agentx_read_searchrange(struct agentx_pdu *pdu,
    struct agentx_search_range *sr)
{
	if (snmp_agentx_do_read_oid(pdu, &sr->start, &sr->include) == -1 ||
	    snmp_agentx_read_oid(pdu, &sr->end) == -1)
		return (-1);

	return (0);
}

char *
snmp_agentx_read_octetstr(struct agentx_pdu *pdu, int *len)
{
	char *str;
	uint32_t l;

	if (snmp_agentx_read_int(pdu, &l) == -1)
		return (NULL);

	if ((str = malloc(l)) == NULL)
		return (NULL);

	if (snmp_agentx_read_raw(pdu, str, l) == -1) {
		free(str);
		return (NULL);
	}
	*len = l;

	return (str);
}

/*
 * Synchronous AgentX calls.
 */

int
snmp_agentx_ping(struct agentx_handle *h)
{
	struct agentx_pdu	*pdu;
	int			 error = 0;

	if ((pdu = snmp_agentx_ping_pdu()) == NULL)
		return (-1);

	/* Attaches the pdu to the handle; will be released later */
	if ((pdu = snmp_agentx_request(h, pdu)) == NULL)
		return (-1);

	if (snmp_agentx_response(h, pdu) == -1)
		error = -1;
	snmp_agentx_pdu_free(pdu);

	return (error);
}

/*
 * Internal utility functions.
 */

void
snmp_agentx_update_ids(struct agentx_handle *h, struct agentx_pdu *pdu)
{
	/* XXX -- update to reflect the new queueing semantics */
	h->transactid = pdu->hdr->transactid;
	h->packetid = pdu->hdr->packetid;
}

char *
snmp_oid2string(struct snmp_oid *o, char *buf, size_t len)
{
	char		 str[256];
	size_t		 i;

	bzero(buf, len);

	for (i = 0; i < o->o_n; i++) {
		snprintf(str, sizeof(str), "%u", o->o_id[i]);
		strlcat(buf, str, len);
		if (i < (o->o_n - 1))
			strlcat(buf, ".", len);
	}

	return (buf);
}

int
snmp_oid_cmp(struct snmp_oid *a, struct snmp_oid *b)
{
	size_t		i;

	for (i = 0; i < SNMP_MAX_OID_LEN; i++) {
		if (a->o_id[i] != 0) {
			if (a->o_id[i] == b->o_id[i])
				continue;
			else if (a->o_id[i] < b->o_id[i]) {
				/* b is a successor of a */
				return (1);
			} else {
				/* b is a predecessor of a */
				return (-1);
			}
		} else if (b->o_id[i] != 0) {
			/* b is larger, but a child of a */
			return (2);
		} else
			break;
	}

	/* b and a are identical */
	return (0);
}

void
snmp_oid_increment(struct snmp_oid *o)
{
	u_int		i;

	for (i = o->o_n; i > 0; i--) {
		o->o_id[i - 1]++;
		if (o->o_id[i - 1] != 0)
			break;
	}
}

char *
snmp_agentx_type2name(int type)
{
	static char *names[] = {
		"AGENTX_OPEN",
		"AGENTX_CLOSE",
		"AGENTX_REGISTER",
		"AGENTX_UNREGISTER",
		"AGENTX_GET",
		"AGENTX_GET_NEXT",
		"AGENTX_GET_BULK",
		"AGENTX_TEST_SET",
		"AGENTX_COMMIT_SET",
		"AGENTX_UNDO_SET",
		"AGENTX_CLEANUP_SET",
		"AGENTX_NOTIFY",
		"AGENTX_PING",
		"AGENTX_INDEX_ALLOCATE",
		"AGENTX_INDEX_DEALLOCATE",
		"AGENTX_ADD_AGENT_CAPS",
		"AGENTX_REMOVE_AGENT_CAPS",
		"AGENTX_RESPONSE"
	};

	if (type > 18)
		return ("unknown");

	return (names[type - 1]);
}

#ifdef DEBUG
static void
snmp_agentx_dump_hdr(struct agentx_hdr *hdr)
{
	if (hdr == NULL) {
		printf("NULL\n");
		return;
	}

	fprintf(stderr,
	    "agentx: version %d type %s flags %d reserved %d"
	    " sessionid %d transactid %d packetid %d length %d",
	    hdr->version, snmp_agentx_type2name(hdr->type), hdr->flags,
	    hdr->reserved, hdr->sessionid, hdr->transactid,
	    hdr->packetid, hdr->length);

	if (hdr->type == AGENTX_RESPONSE) {
		struct agentx_response *r = (struct agentx_response *)hdr;

		fprintf(stderr, " sysuptime %d error %d index %d",
		    r->data.sysuptime, r->data.error, r->data.index);
	}

	fprintf(stderr, "\n");
}
#endif