1*b26e5074Smartijn /* $OpenBSD: ax.h,v 1.4 2024/02/20 12:25:43 martijn Exp $ */ 24100cc5fSmartijn /* 34100cc5fSmartijn * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org> 44100cc5fSmartijn * 54100cc5fSmartijn * Permission to use, copy, modify, and distribute this software for any 64100cc5fSmartijn * purpose with or without fee is hereby granted, provided that the above 74100cc5fSmartijn * copyright notice and this permission notice appear in all copies. 84100cc5fSmartijn * 94100cc5fSmartijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 104100cc5fSmartijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 114100cc5fSmartijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 124100cc5fSmartijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 134100cc5fSmartijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 144100cc5fSmartijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 154100cc5fSmartijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 164100cc5fSmartijn */ 174100cc5fSmartijn 18a9292d2aSmartijn #include <sys/types.h> 19a9292d2aSmartijn 20a9292d2aSmartijn #include <endian.h> 214100cc5fSmartijn #include <stdint.h> 224100cc5fSmartijn 234100cc5fSmartijn #define AX_PDU_FLAG_INSTANCE_REGISTRATION (1 << 0) 244100cc5fSmartijn #define AX_PDU_FLAG_NEW_INDEX (1 << 1) 254100cc5fSmartijn #define AX_PDU_FLAG_ANY_INDEX (1 << 2) 264100cc5fSmartijn #define AX_PDU_FLAG_NON_DEFAULT_CONTEXT (1 << 3) 274100cc5fSmartijn #define AX_PDU_FLAG_NETWORK_BYTE_ORDER (1 << 4) 284100cc5fSmartijn 294100cc5fSmartijn #define AX_PRIORITY_DEFAULT 127 304100cc5fSmartijn 314100cc5fSmartijn enum ax_byte_order { 324100cc5fSmartijn AX_BYTE_ORDER_BE, 334100cc5fSmartijn AX_BYTE_ORDER_LE 344100cc5fSmartijn }; 354100cc5fSmartijn 364100cc5fSmartijn #if BYTE_ORDER == BIG_ENDIAN 374100cc5fSmartijn #define AX_BYTE_ORDER_NATIVE AX_BYTE_ORDER_BE 384100cc5fSmartijn #else 394100cc5fSmartijn #define AX_BYTE_ORDER_NATIVE AX_BYTE_ORDER_LE 404100cc5fSmartijn #endif 414100cc5fSmartijn 424100cc5fSmartijn enum ax_pdu_type { 434100cc5fSmartijn AX_PDU_TYPE_OPEN = 1, 444100cc5fSmartijn AX_PDU_TYPE_CLOSE = 2, 454100cc5fSmartijn AX_PDU_TYPE_REGISTER = 3, 464100cc5fSmartijn AX_PDU_TYPE_UNREGISTER = 4, 474100cc5fSmartijn AX_PDU_TYPE_GET = 5, 484100cc5fSmartijn AX_PDU_TYPE_GETNEXT = 6, 494100cc5fSmartijn AX_PDU_TYPE_GETBULK = 7, 504100cc5fSmartijn AX_PDU_TYPE_TESTSET = 8, 514100cc5fSmartijn AX_PDU_TYPE_COMMITSET = 9, 524100cc5fSmartijn AX_PDU_TYPE_UNDOSET = 10, 534100cc5fSmartijn AX_PDU_TYPE_CLEANUPSET = 11, 544100cc5fSmartijn AX_PDU_TYPE_NOTIFY = 12, 554100cc5fSmartijn AX_PDU_TYPE_PING = 13, 564100cc5fSmartijn AX_PDU_TYPE_INDEXALLOCATE = 14, 574100cc5fSmartijn AX_PDU_TYPE_INDEXDEALLOCATE = 15, 584100cc5fSmartijn AX_PDU_TYPE_ADDAGENTCAPS = 16, 594100cc5fSmartijn AX_PDU_TYPE_REMOVEAGENTCAPS = 17, 604100cc5fSmartijn AX_PDU_TYPE_RESPONSE = 18 614100cc5fSmartijn }; 624100cc5fSmartijn 634100cc5fSmartijn enum ax_pdu_error { 644100cc5fSmartijn AX_PDU_ERROR_NOERROR = 0, 654100cc5fSmartijn AX_PDU_ERROR_GENERR = 5, 664100cc5fSmartijn AX_PDU_ERROR_NOACCESS = 6, 674100cc5fSmartijn AX_PDU_ERROR_WRONGTYPE = 7, 684100cc5fSmartijn AX_PDU_ERROR_WRONGLENGTH = 8, 694100cc5fSmartijn AX_PDU_ERROR_WRONGENCODING = 9, 704100cc5fSmartijn AX_PDU_ERROR_WRONGVALUE = 10, 714100cc5fSmartijn AX_PDU_ERROR_NOCREATION = 11, 724100cc5fSmartijn AX_PDU_ERROR_INCONSISTENTVALUE = 12, 734100cc5fSmartijn AX_PDU_ERROR_RESOURCEUNAVAILABLE = 13, 744100cc5fSmartijn AX_PDU_ERROR_COMMITFAILED = 14, 754100cc5fSmartijn AX_PDU_ERROR_UNDOFAILED = 15, 764100cc5fSmartijn AX_PDU_ERROR_NOTWRITABLE = 17, 774100cc5fSmartijn AX_PDU_ERROR_INCONSISTENTNAME = 18, 784100cc5fSmartijn AX_PDU_ERROR_OPENFAILED = 256, 794100cc5fSmartijn AX_PDU_ERROR_NOTOPEN = 257, 804100cc5fSmartijn AX_PDU_ERROR_INDEXWRONGTYPE = 258, 814100cc5fSmartijn AX_PDU_ERROR_INDEXALREADYALLOCATED = 259, 824100cc5fSmartijn AX_PDU_ERROR_INDEXNONEAVAILABLE = 260, 834100cc5fSmartijn AX_PDU_ERROR_INDEXNOTALLOCATED = 261, 844100cc5fSmartijn AX_PDU_ERROR_UNSUPPORTEDCONETXT = 262, 854100cc5fSmartijn AX_PDU_ERROR_DUPLICATEREGISTRATION = 263, 864100cc5fSmartijn AX_PDU_ERROR_UNKNOWNREGISTRATION = 264, 874100cc5fSmartijn AX_PDU_ERROR_UNKNOWNAGENTCAPS = 265, 884100cc5fSmartijn AX_PDU_ERROR_PARSEERROR = 266, 894100cc5fSmartijn AX_PDU_ERROR_REQUESTDENIED = 267, 904100cc5fSmartijn AX_PDU_ERROR_PROCESSINGERROR = 268 914100cc5fSmartijn }; 924100cc5fSmartijn 934100cc5fSmartijn enum ax_data_type { 944100cc5fSmartijn AX_DATA_TYPE_INTEGER = 2, 954100cc5fSmartijn AX_DATA_TYPE_OCTETSTRING = 4, 964100cc5fSmartijn AX_DATA_TYPE_NULL = 5, 974100cc5fSmartijn AX_DATA_TYPE_OID = 6, 984100cc5fSmartijn AX_DATA_TYPE_IPADDRESS = 64, 994100cc5fSmartijn AX_DATA_TYPE_COUNTER32 = 65, 1004100cc5fSmartijn AX_DATA_TYPE_GAUGE32 = 66, 1014100cc5fSmartijn AX_DATA_TYPE_TIMETICKS = 67, 1024100cc5fSmartijn AX_DATA_TYPE_OPAQUE = 68, 1034100cc5fSmartijn AX_DATA_TYPE_COUNTER64 = 70, 1044100cc5fSmartijn AX_DATA_TYPE_NOSUCHOBJECT = 128, 1054100cc5fSmartijn AX_DATA_TYPE_NOSUCHINSTANCE = 129, 1064100cc5fSmartijn AX_DATA_TYPE_ENDOFMIBVIEW = 130 1074100cc5fSmartijn }; 1084100cc5fSmartijn 1094100cc5fSmartijn enum ax_close_reason { 1104100cc5fSmartijn AX_CLOSE_OTHER = 1, 1114100cc5fSmartijn AX_CLOSEN_PARSEERROR = 2, 1124100cc5fSmartijn AX_CLOSE_PROTOCOLERROR = 3, 1134100cc5fSmartijn AX_CLOSE_TIMEOUTS = 4, 1144100cc5fSmartijn AX_CLOSE_SHUTDOWN = 5, 1154100cc5fSmartijn AX_CLOSE_BYMANAGER = 6 1164100cc5fSmartijn }; 1174100cc5fSmartijn 1184100cc5fSmartijn struct ax { 1194100cc5fSmartijn int ax_fd; 1204100cc5fSmartijn enum ax_byte_order ax_byteorder; 1214100cc5fSmartijn uint8_t *ax_rbuf; 1224100cc5fSmartijn size_t ax_rblen; 1234100cc5fSmartijn size_t ax_rbsize; 1244100cc5fSmartijn uint8_t *ax_wbuf; 1254100cc5fSmartijn size_t ax_wblen; 1264100cc5fSmartijn size_t ax_wbtlen; 1274100cc5fSmartijn size_t ax_wbsize; 1284100cc5fSmartijn }; 1294100cc5fSmartijn 1304100cc5fSmartijn #ifndef AX_PRIMITIVE 1314100cc5fSmartijn #define AX_PRIMITIVE 1324100cc5fSmartijn 1334100cc5fSmartijn #define AX_OID_MAX_LEN 128 1344100cc5fSmartijn 1354100cc5fSmartijn struct ax_oid { 1364100cc5fSmartijn uint8_t aoi_include; 1374100cc5fSmartijn uint32_t aoi_id[AX_OID_MAX_LEN]; 1384100cc5fSmartijn size_t aoi_idlen; 1394100cc5fSmartijn }; 1404100cc5fSmartijn 1414100cc5fSmartijn struct ax_ostring { 1424100cc5fSmartijn unsigned char *aos_string; 1434100cc5fSmartijn uint32_t aos_slen; 1444100cc5fSmartijn }; 1454100cc5fSmartijn #endif 1464100cc5fSmartijn 1474100cc5fSmartijn struct ax_searchrange { 1484100cc5fSmartijn struct ax_oid asr_start; 1494100cc5fSmartijn struct ax_oid asr_stop; 1504100cc5fSmartijn }; 1514100cc5fSmartijn 1524100cc5fSmartijn struct ax_pdu_header { 1534100cc5fSmartijn uint8_t aph_version; 1544100cc5fSmartijn uint8_t aph_type; 1554100cc5fSmartijn uint8_t aph_flags; 1564100cc5fSmartijn uint8_t aph_reserved; 1574100cc5fSmartijn uint32_t aph_sessionid; 1584100cc5fSmartijn uint32_t aph_transactionid; 1594100cc5fSmartijn uint32_t aph_packetid; 1604100cc5fSmartijn uint32_t aph_plength; 1614100cc5fSmartijn }; 1624100cc5fSmartijn 1634100cc5fSmartijn struct ax_varbind { 1644100cc5fSmartijn enum ax_data_type avb_type; 1654100cc5fSmartijn struct ax_oid avb_oid; 1664100cc5fSmartijn union ax_data { 1674100cc5fSmartijn int32_t avb_int32; 1684100cc5fSmartijn uint32_t avb_uint32; 1694100cc5fSmartijn uint64_t avb_uint64; 1704100cc5fSmartijn struct ax_ostring avb_ostring; 1714100cc5fSmartijn struct ax_oid avb_oid; 1724100cc5fSmartijn } avb_data; 1734100cc5fSmartijn }; 1744100cc5fSmartijn 1754100cc5fSmartijn struct ax_pdu { 1764100cc5fSmartijn struct ax_pdu_header ap_header; 1774100cc5fSmartijn struct ax_ostring ap_context; 1784100cc5fSmartijn union { 1794100cc5fSmartijn struct ax_pdu_open { 1804100cc5fSmartijn uint8_t ap_timeout; 1814100cc5fSmartijn struct ax_oid ap_oid; 1824100cc5fSmartijn struct ax_ostring ap_descr; 1834100cc5fSmartijn } ap_open; 1844100cc5fSmartijn struct ax_pdu_close { 1854100cc5fSmartijn enum ax_close_reason ap_reason; 1864100cc5fSmartijn } ap_close; 1874100cc5fSmartijn struct ax_pdu_register { 1884100cc5fSmartijn uint8_t ap_timeout; 1894100cc5fSmartijn uint8_t ap_priority; 1904100cc5fSmartijn uint8_t ap_range_subid; 1914100cc5fSmartijn struct ax_oid ap_subtree; 1924100cc5fSmartijn uint32_t ap_upper_bound; 1934100cc5fSmartijn } ap_register; 1944100cc5fSmartijn struct ax_pdu_unregister { 1954100cc5fSmartijn uint8_t ap_priority; 1964100cc5fSmartijn uint8_t ap_range_subid; 1974100cc5fSmartijn struct ax_oid ap_subtree; 1984100cc5fSmartijn uint32_t ap_upper_bound; 1994100cc5fSmartijn } ap_unregister; 2004100cc5fSmartijn struct ax_pdu_searchrangelist { 2014100cc5fSmartijn size_t ap_nsr; 2024100cc5fSmartijn struct ax_searchrange *ap_sr; 2034100cc5fSmartijn } ap_srl; 2044100cc5fSmartijn struct ax_pdu_getbulk { 2054100cc5fSmartijn uint16_t ap_nonrep; 2064100cc5fSmartijn uint16_t ap_maxrep; 2074100cc5fSmartijn struct ax_pdu_searchrangelist ap_srl; 2084100cc5fSmartijn } ap_getbulk; 2094100cc5fSmartijn struct ax_pdu_varbindlist { 2104100cc5fSmartijn struct ax_varbind *ap_varbind; 2114100cc5fSmartijn size_t ap_nvarbind; 2124100cc5fSmartijn } ap_vbl; 2134100cc5fSmartijn struct ax_pdu_addagentcaps { 2144100cc5fSmartijn struct ax_oid ap_oid; 2154100cc5fSmartijn struct ax_ostring ap_descr; 2164100cc5fSmartijn } ap_addagentcaps; 2174100cc5fSmartijn struct ax_pdu_removeagentcaps { 2184100cc5fSmartijn struct ax_oid ap_oid; 2194100cc5fSmartijn } ap_removeagentcaps; 2204100cc5fSmartijn struct ax_pdu_response { 2214100cc5fSmartijn uint32_t ap_uptime; 2224100cc5fSmartijn enum ax_pdu_error ap_error; 2234100cc5fSmartijn uint16_t ap_index; 2244100cc5fSmartijn struct ax_varbind *ap_varbindlist; 2254100cc5fSmartijn size_t ap_nvarbind; 2264100cc5fSmartijn } ap_response; 2274100cc5fSmartijn } ap_payload; 2284100cc5fSmartijn }; 2294100cc5fSmartijn 2304100cc5fSmartijn struct ax *ax_new(int); 2314100cc5fSmartijn void ax_free(struct ax *); 2324100cc5fSmartijn struct ax_pdu *ax_recv(struct ax *); 2334100cc5fSmartijn ssize_t ax_send(struct ax *); 2344100cc5fSmartijn uint32_t ax_open(struct ax *, uint8_t, struct ax_oid *, 2354100cc5fSmartijn struct ax_ostring *); 2364100cc5fSmartijn uint32_t ax_close(struct ax *, uint32_t, enum ax_close_reason); 2374100cc5fSmartijn int ax_get(struct ax *, uint32_t, uint32_t, uint32_t, struct ax_ostring *, 2384100cc5fSmartijn struct ax_searchrange *, size_t); 2394100cc5fSmartijn int ax_getnext(struct ax *, uint32_t, uint32_t, uint32_t, struct ax_ostring *, 2404100cc5fSmartijn struct ax_searchrange *, size_t); 2414100cc5fSmartijn uint32_t ax_indexallocate(struct ax *, uint8_t, uint32_t, 2424100cc5fSmartijn struct ax_ostring *, struct ax_varbind *, size_t); 2434100cc5fSmartijn uint32_t ax_indexdeallocate(struct ax *, uint32_t, 2444100cc5fSmartijn struct ax_ostring *, struct ax_varbind *, size_t); 2454100cc5fSmartijn uint32_t ax_addagentcaps(struct ax *, uint32_t, struct ax_ostring *, 2464100cc5fSmartijn struct ax_oid *, struct ax_ostring *); 2474100cc5fSmartijn uint32_t ax_removeagentcaps(struct ax *, uint32_t, 2484100cc5fSmartijn struct ax_ostring *, struct ax_oid *); 2494100cc5fSmartijn uint32_t ax_register(struct ax *, uint8_t, uint32_t, 2504100cc5fSmartijn struct ax_ostring *, uint8_t, uint8_t, uint8_t, struct ax_oid *, 2514100cc5fSmartijn uint32_t); 2524100cc5fSmartijn uint32_t ax_unregister(struct ax *, uint32_t, struct ax_ostring *, 2534100cc5fSmartijn uint8_t, uint8_t, struct ax_oid *, uint32_t); 2544100cc5fSmartijn int ax_response(struct ax *, uint32_t, uint32_t, uint32_t, 2556f5d9364Smartijn uint32_t, uint16_t, uint16_t, struct ax_varbind *, size_t); 2564100cc5fSmartijn void ax_pdu_free(struct ax_pdu *); 2574100cc5fSmartijn void ax_varbind_free(struct ax_varbind *); 2584100cc5fSmartijn const char *ax_error2string(enum ax_pdu_error); 2594100cc5fSmartijn const char *ax_pdutype2string(enum ax_pdu_type); 2604100cc5fSmartijn const char *ax_oid2string(struct ax_oid *); 2614100cc5fSmartijn const char *ax_oidrange2string(struct ax_oid *, uint8_t, uint32_t); 2624100cc5fSmartijn const char *ax_varbind2string(struct ax_varbind *); 2634100cc5fSmartijn const char *ax_closereason2string(enum ax_close_reason); 2644100cc5fSmartijn int ax_oid_cmp(struct ax_oid *, struct ax_oid *); 2654100cc5fSmartijn int ax_oid_add(struct ax_oid *, uint32_t); 266