1/*	$OpenBSD: agentx.h,v 1.7 2022/10/14 15:26:58 martijn 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 <netinet/in.h>
19
20#include <stdint.h>
21#include <stddef.h>
22
23struct agentx;
24struct agentx_session;
25struct agentx_context;
26struct agentx_agentcaps;
27struct agentx_region;
28struct agentx_index;
29struct agentx_object;
30struct agentx_varbind;
31
32enum agentx_request_type {
33	AGENTX_REQUEST_TYPE_GET,
34	AGENTX_REQUEST_TYPE_GETNEXT,
35	AGENTX_REQUEST_TYPE_GETNEXTINCLUSIVE
36};
37
38#define AGENTX_MASTER_PATH "/var/agentx/master"
39#define AGENTX_OID_MIN_LEN 2
40#define AGENTX_OID_MAX_LEN 128
41#define AGENTX_OID_INDEX_MAX_LEN 10
42#define AGENTX_MIB2 1, 3, 6, 1, 2, 1
43#define AGENTX_ENTERPRISES 1, 3, 6, 1, 4, 1
44#define AGENTX_OID(...) (uint32_t []) { __VA_ARGS__ }, \
45    (sizeof((uint32_t []) { __VA_ARGS__ }) / sizeof(uint32_t))
46
47extern void (*agentx_log_fatal)(const char *, ...)
48    __attribute__((__format__ (printf, 1, 2)));
49extern void (*agentx_log_warn)(const char *, ...)
50    __attribute__((__format__ (printf, 1, 2)));
51extern void (*agentx_log_info)(const char *, ...)
52    __attribute__((__format__ (printf, 1, 2)));
53extern void (*agentx_log_debug)(const char *, ...)
54    __attribute__((__format__ (printf, 1, 2)));
55
56struct agentx *agentx(void (*)(struct agentx *, void *, int), void *);
57void agentx_connect(struct agentx *, int);
58void agentx_retry(struct agentx *);
59void agentx_read(struct agentx *);
60void agentx_write(struct agentx *);
61extern void (*agentx_wantwrite)(struct agentx *, int);
62void agentx_free(struct agentx *);
63struct agentx_session *agentx_session(struct agentx *,
64    uint32_t[], size_t, const char *, uint8_t);
65void agentx_session_free(struct agentx_session *);
66struct agentx_context *agentx_context(struct agentx_session *,
67    const char *);
68struct agentx_object *agentx_context_object_find(
69    struct agentx_context *, const uint32_t[], size_t, int, int);
70struct agentx_object *agentx_context_object_nfind(
71    struct agentx_context *, const uint32_t[], size_t, int, int);
72uint32_t agentx_context_uptime(struct agentx_context *);
73void agentx_context_free(struct agentx_context *);
74struct agentx_agentcaps *agentx_agentcaps(struct agentx_context *,
75    uint32_t[], size_t, const char *);
76void agentx_agentcaps_free(struct agentx_agentcaps *);
77struct agentx_region *agentx_region(struct agentx_context *,
78    uint32_t[], size_t, uint8_t);
79void agentx_region_free(struct agentx_region *);
80struct agentx_index *agentx_index_integer_new(struct agentx_region *,
81    uint32_t[], size_t);
82struct agentx_index *agentx_index_integer_any(struct agentx_region *,
83    uint32_t[], size_t);
84struct agentx_index *agentx_index_integer_value(struct agentx_region *,
85    uint32_t[], size_t, int32_t);
86struct agentx_index *agentx_index_integer_dynamic(
87    struct agentx_region *, uint32_t[], size_t);
88struct agentx_index *agentx_index_string_dynamic(
89    struct agentx_region *, uint32_t[], size_t);
90struct agentx_index *agentx_index_nstring_dynamic(
91    struct agentx_region *, uint32_t[], size_t, size_t);
92struct agentx_index *agentx_index_oid_dynamic(struct agentx_region *,
93    uint32_t[], size_t);
94struct agentx_index *agentx_index_noid_dynamic(struct agentx_region *,
95    uint32_t[], size_t, size_t);
96struct agentx_index *agentx_index_ipaddress_dynamic(
97    struct agentx_region *, uint32_t[], size_t);
98void agentx_index_free(struct agentx_index *);
99struct agentx_object *agentx_object(struct agentx_region *, uint32_t[],
100    size_t, struct agentx_index *[], size_t, int,
101    void (*)(struct agentx_varbind *));
102void agentx_object_free(struct agentx_object *);
103
104void agentx_varbind_integer(struct agentx_varbind *, int32_t);
105void agentx_varbind_string(struct agentx_varbind *, const char *);
106void agentx_varbind_nstring(struct agentx_varbind *,
107    const unsigned char *, size_t);
108void agentx_varbind_printf(struct agentx_varbind *, const char *, ...)
109    __attribute__((__format__ (printf, 2, 3)));
110void agentx_varbind_null(struct agentx_varbind *);
111void agentx_varbind_oid(struct agentx_varbind *, const uint32_t[],
112    size_t);
113void agentx_varbind_object(struct agentx_varbind *,
114    struct agentx_object *);
115void agentx_varbind_index(struct agentx_varbind *,
116    struct agentx_index *);
117void agentx_varbind_ipaddress(struct agentx_varbind *,
118    const struct in_addr *);
119void agentx_varbind_counter32(struct agentx_varbind *, uint32_t);
120void agentx_varbind_gauge32(struct agentx_varbind *, uint32_t);
121void agentx_varbind_unsigned32(struct agentx_varbind *, uint32_t);
122void agentx_varbind_timeticks(struct agentx_varbind *, uint32_t);
123void agentx_varbind_opaque(struct agentx_varbind *, const char *, size_t);
124void agentx_varbind_counter64(struct agentx_varbind *, uint64_t);
125void agentx_varbind_notfound(struct agentx_varbind *);
126void agentx_varbind_error(struct agentx_varbind *);
127
128enum agentx_request_type agentx_varbind_request(
129    struct agentx_varbind *);
130struct agentx_object *
131    agentx_varbind_get_object(struct agentx_varbind *);
132int32_t agentx_varbind_get_index_integer(struct agentx_varbind *,
133    struct agentx_index *);
134const unsigned char *agentx_varbind_get_index_string(
135    struct agentx_varbind *, struct agentx_index *, size_t *, int *);
136const uint32_t *agentx_varbind_get_index_oid(struct agentx_varbind *,
137    struct agentx_index *, size_t *, int *);
138const struct in_addr *agentx_varbind_get_index_ipaddress(
139    struct agentx_varbind *, struct agentx_index *);
140void agentx_varbind_set_index_integer(struct agentx_varbind *,
141    struct agentx_index *, int32_t);
142void agentx_varbind_set_index_string(struct agentx_varbind *,
143    struct agentx_index *, const char *);
144void agentx_varbind_set_index_nstring(struct agentx_varbind *,
145    struct agentx_index *, const unsigned char *, size_t);
146void agentx_varbind_set_index_oid(struct agentx_varbind *,
147    struct agentx_index *, const uint32_t *, size_t);
148void agentx_varbind_set_index_object(struct agentx_varbind *,
149    struct agentx_index *, struct agentx_object *);
150void agentx_varbind_set_index_ipaddress(struct agentx_varbind *,
151    struct agentx_index *, const struct in_addr *);
152