xref: /openbsd/lib/libagentx/ax.h (revision 097a140d)
1 /*	$OpenBSD: ax.h,v 1.4 2021/01/02 01:06:31 rob Exp $ */
2 /*
3  * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <stdint.h>
19 
20 #define AX_PDU_FLAG_INSTANCE_REGISTRATION (1 << 0)
21 #define AX_PDU_FLAG_NEW_INDEX (1 << 1)
22 #define AX_PDU_FLAG_ANY_INDEX (1 << 2)
23 #define AX_PDU_FLAG_NON_DEFAULT_CONTEXT (1 << 3)
24 #define AX_PDU_FLAG_NETWORK_BYTE_ORDER (1 << 4)
25 
26 #define AX_PRIORITY_DEFAULT 127
27 
28 enum ax_byte_order {
29 	AX_BYTE_ORDER_BE,
30 	AX_BYTE_ORDER_LE
31 };
32 
33 #if BYTE_ORDER == BIG_ENDIAN
34 #define AX_BYTE_ORDER_NATIVE AX_BYTE_ORDER_BE
35 #else
36 #define AX_BYTE_ORDER_NATIVE AX_BYTE_ORDER_LE
37 #endif
38 
39 enum ax_pdu_type {
40 	AX_PDU_TYPE_OPEN		= 1,
41 	AX_PDU_TYPE_CLOSE		= 2,
42 	AX_PDU_TYPE_REGISTER		= 3,
43 	AX_PDU_TYPE_UNREGISTER		= 4,
44 	AX_PDU_TYPE_GET			= 5,
45 	AX_PDU_TYPE_GETNEXT		= 6,
46 	AX_PDU_TYPE_GETBULK		= 7,
47 	AX_PDU_TYPE_TESTSET		= 8,
48 	AX_PDU_TYPE_COMMITSET		= 9,
49 	AX_PDU_TYPE_UNDOSET		= 10,
50 	AX_PDU_TYPE_CLEANUPSET		= 11,
51 	AX_PDU_TYPE_NOTIFY		= 12,
52 	AX_PDU_TYPE_PING		= 13,
53 	AX_PDU_TYPE_INDEXALLOCATE	= 14,
54 	AX_PDU_TYPE_INDEXDEALLOCATE	= 15,
55 	AX_PDU_TYPE_ADDAGENTCAPS	= 16,
56 	AX_PDU_TYPE_REMOVEAGENTCAPS	= 17,
57 	AX_PDU_TYPE_RESPONSE		= 18
58 };
59 
60 enum ax_pdu_error {
61 	AX_PDU_ERROR_NOERROR			= 0,
62 	AX_PDU_ERROR_GENERR			= 5,
63 	AX_PDU_ERROR_NOACCESS			= 6,
64 	AX_PDU_ERROR_WRONGTYPE			= 7,
65 	AX_PDU_ERROR_WRONGLENGTH		= 8,
66 	AX_PDU_ERROR_WRONGENCODING		= 9,
67 	AX_PDU_ERROR_WRONGVALUE			= 10,
68 	AX_PDU_ERROR_NOCREATION			= 11,
69 	AX_PDU_ERROR_INCONSISTENTVALUE		= 12,
70 	AX_PDU_ERROR_RESOURCEUNAVAILABLE	= 13,
71 	AX_PDU_ERROR_COMMITFAILED		= 14,
72 	AX_PDU_ERROR_UNDOFAILED			= 15,
73 	AX_PDU_ERROR_NOTWRITABLE		= 17,
74 	AX_PDU_ERROR_INCONSISTENTNAME		= 18,
75 	AX_PDU_ERROR_OPENFAILED			= 256,
76 	AX_PDU_ERROR_NOTOPEN			= 257,
77 	AX_PDU_ERROR_INDEXWRONGTYPE		= 258,
78 	AX_PDU_ERROR_INDEXALREADYALLOCATED	= 259,
79 	AX_PDU_ERROR_INDEXNONEAVAILABLE		= 260,
80 	AX_PDU_ERROR_INDEXNOTALLOCATED		= 261,
81 	AX_PDU_ERROR_UNSUPPORTEDCONETXT		= 262,
82 	AX_PDU_ERROR_DUPLICATEREGISTRATION	= 263,
83 	AX_PDU_ERROR_UNKNOWNREGISTRATION	= 264,
84 	AX_PDU_ERROR_UNKNOWNAGENTCAPS		= 265,
85 	AX_PDU_ERROR_PARSEERROR			= 266,
86 	AX_PDU_ERROR_REQUESTDENIED		= 267,
87 	AX_PDU_ERROR_PROCESSINGERROR		= 268
88 };
89 
90 enum ax_data_type {
91 	AX_DATA_TYPE_INTEGER		= 2,
92 	AX_DATA_TYPE_OCTETSTRING	= 4,
93 	AX_DATA_TYPE_NULL		= 5,
94 	AX_DATA_TYPE_OID		= 6,
95 	AX_DATA_TYPE_IPADDRESS		= 64,
96 	AX_DATA_TYPE_COUNTER32		= 65,
97 	AX_DATA_TYPE_GAUGE32		= 66,
98 	AX_DATA_TYPE_TIMETICKS		= 67,
99 	AX_DATA_TYPE_OPAQUE		= 68,
100 	AX_DATA_TYPE_COUNTER64		= 70,
101 	AX_DATA_TYPE_NOSUCHOBJECT	= 128,
102 	AX_DATA_TYPE_NOSUCHINSTANCE	= 129,
103 	AX_DATA_TYPE_ENDOFMIBVIEW	= 130
104 };
105 
106 enum ax_close_reason {
107 	AX_CLOSE_OTHER			= 1,
108 	AX_CLOSEN_PARSEERROR		= 2,
109 	AX_CLOSE_PROTOCOLERROR		= 3,
110 	AX_CLOSE_TIMEOUTS		= 4,
111 	AX_CLOSE_SHUTDOWN		= 5,
112 	AX_CLOSE_BYMANAGER		= 6
113 };
114 
115 struct ax {
116 	int ax_fd;
117 	enum ax_byte_order ax_byteorder;
118 	uint8_t *ax_rbuf;
119 	size_t ax_rblen;
120 	size_t ax_rbsize;
121 	uint8_t *ax_wbuf;
122 	size_t ax_wblen;
123 	size_t ax_wbtlen;
124 	size_t ax_wbsize;
125 	uint32_t *ax_packetids;
126 	size_t ax_packetidsize;
127 };
128 
129 #ifndef AX_PRIMITIVE
130 #define AX_PRIMITIVE
131 
132 #define AX_OID_MAX_LEN 128
133 
134 struct ax_oid {
135 	uint8_t aoi_include;
136 	uint32_t aoi_id[AX_OID_MAX_LEN];
137 	size_t aoi_idlen;
138 };
139 
140 struct ax_ostring {
141 	unsigned char *aos_string;
142 	uint32_t aos_slen;
143 };
144 #endif
145 
146 struct ax_searchrange {
147 	struct ax_oid asr_start;
148 	struct ax_oid asr_stop;
149 };
150 
151 struct ax_pdu_header {
152 	uint8_t	aph_version;
153 	uint8_t	aph_type;
154 	uint8_t	aph_flags;
155 	uint8_t	aph_reserved;
156 	uint32_t aph_sessionid;
157 	uint32_t aph_transactionid;
158 	uint32_t aph_packetid;
159 	uint32_t aph_plength;
160 };
161 
162 struct ax_varbind {
163 	enum ax_data_type avb_type;
164 	struct ax_oid avb_oid;
165 	union ax_data {
166 		int32_t avb_int32;
167 		uint32_t avb_uint32;
168 		uint64_t avb_uint64;
169 		struct ax_ostring avb_ostring;
170 		struct ax_oid avb_oid;
171 	} avb_data;
172 };
173 
174 struct ax_pdu {
175 	struct ax_pdu_header ap_header;
176 	struct ax_ostring ap_context;
177 	union {
178 		struct ax_pdu_searchrangelist {
179 			size_t ap_nsr;
180 			struct ax_searchrange *ap_sr;
181 		} ap_srl;
182 		struct ax_pdu_getbulk {
183 			uint16_t ap_nonrep;
184 			uint16_t ap_maxrep;
185 			struct ax_pdu_searchrangelist ap_srl;
186 		} ap_getbulk;
187 		struct ax_pdu_varbindlist {
188 			struct ax_varbind *ap_varbind;
189 			size_t ap_nvarbind;
190 		} ap_vbl;
191 		struct ax_pdu_response {
192 			uint32_t ap_uptime;
193 			enum ax_pdu_error ap_error;
194 			uint16_t ap_index;
195 			struct ax_varbind *ap_varbindlist;
196 			size_t ap_nvarbind;
197 		} ap_response;
198 		void *ap_raw;
199 	} ap_payload;
200 };
201 
202 struct ax *ax_new(int);
203 void ax_free(struct ax *);
204 struct ax_pdu *ax_recv(struct ax *);
205 ssize_t ax_send(struct ax *);
206 uint32_t ax_open(struct ax *, uint8_t, struct ax_oid *,
207     struct ax_ostring *);
208 uint32_t ax_close(struct ax *, uint32_t, enum ax_close_reason);
209 uint32_t ax_indexallocate(struct ax *, uint8_t, uint32_t,
210     struct ax_ostring *, struct ax_varbind *, size_t);
211 uint32_t ax_indexdeallocate(struct ax *, uint32_t,
212     struct ax_ostring *, struct ax_varbind *, size_t);
213 uint32_t ax_addagentcaps(struct ax *, uint32_t, struct ax_ostring *,
214     struct ax_oid *, struct ax_ostring *);
215 uint32_t ax_removeagentcaps(struct ax *, uint32_t,
216     struct ax_ostring *, struct ax_oid *);
217 uint32_t ax_register(struct ax *, uint8_t, uint32_t,
218     struct ax_ostring *, uint8_t, uint8_t, uint8_t, struct ax_oid *,
219     uint32_t);
220 uint32_t ax_unregister(struct ax *, uint32_t, struct ax_ostring *,
221     uint8_t, uint8_t, struct ax_oid *, uint32_t);
222 int ax_response(struct ax *, uint32_t, uint32_t, uint32_t,
223     struct ax_ostring *, uint32_t, uint16_t, uint16_t,
224     struct ax_varbind *, size_t);
225 void ax_pdu_free(struct ax_pdu *);
226 void ax_varbind_free(struct ax_varbind *);
227 const char *ax_error2string(enum ax_pdu_error);
228 const char *ax_pdutype2string(enum ax_pdu_type);
229 const char *ax_oid2string(struct ax_oid *);
230 const char *ax_oidrange2string(struct ax_oid *, uint8_t, uint32_t);
231 const char *ax_varbind2string(struct ax_varbind *);
232 const char *ax_closereason2string(enum ax_close_reason);
233 int ax_oid_cmp(struct ax_oid *, struct ax_oid *);
234 int ax_oid_add(struct ax_oid *, uint32_t);
235