summaryrefslogtreecommitdiff
path: root/lib/libagentx/agentx.h
blob: 2300c79117a64a22356171a451f46ce47613db40 (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
/*
 * 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 <stdint.h>

#define AGENTX_PDU_FLAG_INSTANCE_REGISTRATION (1 << 0)
#define AGENTX_PDU_FLAG_NEW_INDEX (1 << 1)
#define AGENTX_PDU_FLAG_ANY_INDEX (1 << 2)
#define AGENTX_PDU_FLAG_NON_DEFAULT_CONTEXT (1 << 3)
#define AGENTX_PDU_FLAG_NETWORK_BYTE_ORDER (1 << 4)

#define AGENTX_PRIORITY_DEFAULT 127

enum agentx_byte_order {
	AGENTX_BYTE_ORDER_BE,
	AGENTX_BYTE_ORDER_LE
};

#if BYTE_ORDER == BIG_ENDIAN
#define AGENTX_BYTE_ORDER_NATIVE AGENTX_BYTE_ORDER_BE
#else
#define AGENTX_BYTE_ORDER_NATIVE AGENTX_BYTE_ORDER_LE
#endif

enum agentx_pdu_type {
	AGENTX_PDU_TYPE_OPEN		= 1,
	AGENTX_PDU_TYPE_CLOSE		= 2,
	AGENTX_PDU_TYPE_REGISTER	= 3,
	AGENTX_PDU_TYPE_UNREGISTER	= 4,
	AGENTX_PDU_TYPE_GET		= 5,
	AGENTX_PDU_TYPE_GETNEXT		= 6,
	AGENTX_PDU_TYPE_GETBULK		= 7,
	AGENTX_PDU_TYPE_TESTSET		= 8,
	AGENTX_PDU_TYPE_COMMITSET	= 9,
	AGENTX_PDU_TYPE_UNDOSET		= 10,
	AGENTX_PDU_TYPE_CLEANUPSET	= 11,
	AGENTX_PDU_TYPE_NOTIFY		= 12,
	AGENTX_PDU_TYPE_PING		= 13,
	AGENTX_PDU_TYPE_INDEXALLOCATE	= 14,
	AGENTX_PDU_TYPE_INDEXDEALLOCATE	= 15,
	AGENTX_PDU_TYPE_ADDAGENTCAPS	= 16,
	AGENTX_PDU_TYPE_REMOVEAGENTCAPS	= 17,
	AGENTX_PDU_TYPE_RESPONSE	= 18
};

enum agentx_pdu_error {
	AGENTX_PDU_ERROR_NOERROR		= 0,
	AGENTX_PDU_ERROR_GENERR			= 5,
	AGENTX_PDU_ERROR_NOACCESS		= 6,
	AGENTX_PDU_ERROR_WRONGTYPE		= 7,
	AGENTX_PDU_ERROR_WRONGLENGTH		= 8,
	AGENTX_PDU_ERROR_WRONGENCODING		= 9,
	AGENTX_PDU_ERROR_WRONGVALUE		= 10,
	AGENTX_PDU_ERROR_NOCREATION		= 11,
	AGENTX_PDU_ERROR_INCONSISTENTVALUE	= 12,
	AGENTX_PDU_ERROR_RESOURCEUNAVAILABLE	= 13,
	AGENTX_PDU_ERROR_COMMITFAILED		= 14,
	AGENTX_PDU_ERROR_UNDOFAILED		= 15,
	AGENTX_PDU_ERROR_NOTWRITABLE		= 17,
	AGENTX_PDU_ERROR_INCONSISTENTNAME	= 18,
	AGENTX_PDU_ERROR_OPENFAILED		= 256,
	AGENTX_PDU_ERROR_NOTOPEN		= 257,
	AGENTX_PDU_ERROR_INDEXWRONGTYPE		= 258,
	AGENTX_PDU_ERROR_INDEXALREADYALLOCATED	= 259,
	AGENTX_PDU_ERROR_INDEXNONEAVAILABLE	= 260,
	AGENTX_PDU_ERROR_INDEXNOTALLOCATED	= 261,
	AGENTX_PDU_ERROR_UNSUPPORTEDCONETXT	= 262,
	AGENTX_PDU_ERROR_DUPLICATEREGISTRATION	= 263,
	AGENTX_PDU_ERROR_UNKNOWNREGISTRATION	= 264,
	AGENTX_PDU_ERROR_UNKNOWNAGENTCAPS	= 265,
	AGENTX_PDU_ERROR_PARSEERROR		= 266,
	AGENTX_PDU_ERROR_REQUESTDENIED		= 267,
	AGENTX_PDU_ERROR_PROCESSINGERROR	= 268
};

enum agentx_data_type {
	AGENTX_DATA_TYPE_INTEGER	= 2,
	AGENTX_DATA_TYPE_OCTETSTRING	= 4,
	AGENTX_DATA_TYPE_NULL		= 5,
	AGENTX_DATA_TYPE_OID		= 6,
	AGENTX_DATA_TYPE_IPADDRESS	= 64,
	AGENTX_DATA_TYPE_COUNTER32	= 65,
	AGENTX_DATA_TYPE_GAUGE32	= 66,
	AGENTX_DATA_TYPE_TIMETICKS	= 67,
	AGENTX_DATA_TYPE_OPAQUE		= 68,
	AGENTX_DATA_TYPE_COUNTER64	= 70,
	AGENTX_DATA_TYPE_NOSUCHOBJECT	= 128,
	AGENTX_DATA_TYPE_NOSUCHINSTANCE	= 129,
	AGENTX_DATA_TYPE_ENDOFMIBVIEW	= 130
};

enum agentx_close_reason {
	AGENTX_CLOSE_OTHER		= 1,
	AGENTX_CLOSEN_PARSEERROR	= 2,
	AGENTX_CLOSE_PROTOCOLERROR	= 3,
	AGENTX_CLOSE_TIMEOUTS		= 4,
	AGENTX_CLOSE_SHUTDOWN		= 5,
	AGENTX_CLOSE_BYMANAGER		= 6
};

struct agentx {
	int ax_fd;
	enum agentx_byte_order ax_byteorder;
	uint8_t *ax_rbuf;
	size_t ax_rblen;
	size_t ax_rbsize;
	uint8_t *ax_wbuf;
	size_t ax_wblen;
	size_t ax_wbtlen;
	size_t ax_wbsize;
	uint32_t *ax_packetids;
	size_t ax_packetidsize;
};

#ifndef AGENTX_PRIMITIVE
#define AGENTX_PRIMITIVE

#define AGENTX_OID_MAX_LEN 128

struct agentx_oid {
	uint8_t aoi_include;
	uint32_t aoi_id[AGENTX_OID_MAX_LEN];
	size_t aoi_idlen;
};

struct agentx_ostring {
	unsigned char *aos_string;
	uint32_t aos_slen;
};
#endif

struct agentx_searchrange {
	struct agentx_oid asr_start;
	struct agentx_oid asr_stop;
};

struct agentx_pdu_header {
	uint8_t	aph_version;
	uint8_t	aph_type;
	uint8_t	aph_flags;
	uint8_t	aph_reserved;
	uint32_t aph_sessionid;
	uint32_t aph_transactionid;
	uint32_t aph_packetid;
	uint32_t aph_plength;
};

struct agentx_varbind {
	enum agentx_data_type avb_type;
	struct agentx_oid avb_oid;
	union agentx_data {
		uint32_t avb_uint32;
		uint64_t avb_uint64;
		struct agentx_ostring avb_ostring;
		struct agentx_oid avb_oid;
	} avb_data;
};

struct agentx_pdu {
	struct agentx_pdu_header ap_header;
	struct agentx_ostring ap_context;
	union {
		struct agentx_pdu_searchrangelist {
			size_t ap_nsr;
			struct agentx_searchrange *ap_sr;
		} ap_srl;
		struct agentx_pdu_getbulk {
			uint16_t ap_nonrep;
			uint16_t ap_maxrep;
			struct agentx_pdu_searchrangelist ap_srl;
		} ap_getbulk;
		struct agentx_pdu_varbindlist {
			struct agentx_varbind *ap_varbind;
			size_t ap_nvarbind;
		} ap_vbl;
		struct agentx_pdu_response {
			uint32_t ap_uptime;
			enum agentx_pdu_error ap_error;
			uint16_t ap_index;
			struct agentx_varbind *ap_varbindlist;
			size_t ap_nvarbind;
		} ap_response;
		void *ap_raw;
	} ap_payload;
};

struct agentx *agentx_new(int);
void agentx_free(struct agentx *);
struct agentx_pdu *agentx_recv(struct agentx *);
ssize_t agentx_send(struct agentx *);
uint32_t agentx_open(struct agentx *, uint8_t, struct agentx_oid *,
    struct agentx_ostring *);
uint32_t agentx_close(struct agentx *, uint32_t, enum agentx_close_reason);
uint32_t agentx_indexallocate(struct agentx *, uint8_t, uint32_t,
    struct agentx_ostring *, struct agentx_varbind *, size_t);
uint32_t agentx_indexdeallocate(struct agentx *, uint32_t,
    struct agentx_ostring *, struct agentx_varbind *, size_t);
uint32_t agentx_addagentcaps(struct agentx *, uint32_t, struct agentx_ostring *,
    struct agentx_oid *, struct agentx_ostring *);
uint32_t agentx_removeagentcaps(struct agentx *, uint32_t,
    struct agentx_ostring *, struct agentx_oid *);
uint32_t agentx_register(struct agentx *, uint8_t, uint32_t,
    struct agentx_ostring *, uint8_t, uint8_t, uint8_t, struct agentx_oid *,
    uint32_t);
uint32_t agentx_unregister(struct agentx *, uint32_t, struct agentx_ostring *,
    uint8_t, uint8_t, struct agentx_oid *, uint32_t);
int agentx_response(struct agentx *, uint32_t, uint32_t, uint32_t,
    struct agentx_ostring *, uint32_t, uint16_t, uint16_t,
    struct agentx_varbind *, size_t);
void agentx_pdu_free(struct agentx_pdu *);
void agentx_varbind_free(struct agentx_varbind *);
const char *agentx_error2string(enum agentx_pdu_error);
const char *agentx_pdutype2string(enum agentx_pdu_type);
const char *agentx_oid2string(struct agentx_oid *);
const char *agentx_oidrange2string(struct agentx_oid *, uint8_t, uint32_t);
const char *agentx_varbind2string(struct agentx_varbind *);
const char *agentx_closereason2string(enum agentx_close_reason);
int agentx_oid_cmp(struct agentx_oid *, struct agentx_oid *);
int agentx_oid_add(struct agentx_oid *, uint32_t);