xref: /openbsd-src/lib/libagentx/agentx.h (revision 5a38ef86d0b61900239c7913d24a05e7b88a58f0)
1 /*	$OpenBSD: agentx.h,v 1.6 2021/10/24 18:03:27 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 
23 struct agentx;
24 struct agentx_session;
25 struct agentx_context;
26 struct agentx_agentcaps;
27 struct agentx_region;
28 struct agentx_index;
29 struct agentx_object;
30 struct agentx_varbind;
31 
32 enum 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 
47 extern void (*agentx_log_fatal)(const char *, ...)
48     __attribute__((__format__ (printf, 1, 2)));
49 extern void (*agentx_log_warn)(const char *, ...)
50     __attribute__((__format__ (printf, 1, 2)));
51 extern void (*agentx_log_info)(const char *, ...)
52     __attribute__((__format__ (printf, 1, 2)));
53 extern void (*agentx_log_debug)(const char *, ...)
54     __attribute__((__format__ (printf, 1, 2)));
55 
56 struct agentx *agentx(void (*)(struct agentx *, void *, int), void *);
57 void agentx_connect(struct agentx *, int);
58 void agentx_read(struct agentx *);
59 void agentx_write(struct agentx *);
60 extern void (*agentx_wantwrite)(struct agentx *, int);
61 void agentx_free(struct agentx *);
62 struct agentx_session *agentx_session(struct agentx *,
63     uint32_t[], size_t, const char *, uint8_t);
64 void agentx_session_free(struct agentx_session *);
65 struct agentx_context *agentx_context(struct agentx_session *,
66     const char *);
67 struct agentx_object *agentx_context_object_find(
68     struct agentx_context *, const uint32_t[], size_t, int, int);
69 struct agentx_object *agentx_context_object_nfind(
70     struct agentx_context *, const uint32_t[], size_t, int, int);
71 uint32_t agentx_context_uptime(struct agentx_context *);
72 void agentx_context_free(struct agentx_context *);
73 struct agentx_agentcaps *agentx_agentcaps(struct agentx_context *,
74     uint32_t[], size_t, const char *);
75 void agentx_agentcaps_free(struct agentx_agentcaps *);
76 struct agentx_region *agentx_region(struct agentx_context *,
77     uint32_t[], size_t, uint8_t);
78 void agentx_region_free(struct agentx_region *);
79 struct agentx_index *agentx_index_integer_new(struct agentx_region *,
80     uint32_t[], size_t);
81 struct agentx_index *agentx_index_integer_any(struct agentx_region *,
82     uint32_t[], size_t);
83 struct agentx_index *agentx_index_integer_value(struct agentx_region *,
84     uint32_t[], size_t, int32_t);
85 struct agentx_index *agentx_index_integer_dynamic(
86     struct agentx_region *, uint32_t[], size_t);
87 struct agentx_index *agentx_index_string_dynamic(
88     struct agentx_region *, uint32_t[], size_t);
89 struct agentx_index *agentx_index_nstring_dynamic(
90     struct agentx_region *, uint32_t[], size_t, size_t);
91 struct agentx_index *agentx_index_oid_dynamic(struct agentx_region *,
92     uint32_t[], size_t);
93 struct agentx_index *agentx_index_noid_dynamic(struct agentx_region *,
94     uint32_t[], size_t, size_t);
95 struct agentx_index *agentx_index_ipaddress_dynamic(
96     struct agentx_region *, uint32_t[], size_t);
97 void agentx_index_free(struct agentx_index *);
98 struct agentx_object *agentx_object(struct agentx_region *, uint32_t[],
99     size_t, struct agentx_index *[], size_t, int,
100     void (*)(struct agentx_varbind *));
101 void agentx_object_free(struct agentx_object *);
102 
103 void agentx_varbind_integer(struct agentx_varbind *, int32_t);
104 void agentx_varbind_string(struct agentx_varbind *, const char *);
105 void agentx_varbind_nstring(struct agentx_varbind *,
106     const unsigned char *, size_t);
107 void agentx_varbind_printf(struct agentx_varbind *, const char *, ...)
108     __attribute__((__format__ (printf, 2, 3)));
109 void agentx_varbind_null(struct agentx_varbind *);
110 void agentx_varbind_oid(struct agentx_varbind *, const uint32_t[],
111     size_t);
112 void agentx_varbind_object(struct agentx_varbind *,
113     struct agentx_object *);
114 void agentx_varbind_index(struct agentx_varbind *,
115     struct agentx_index *);
116 void agentx_varbind_ipaddress(struct agentx_varbind *,
117     const struct in_addr *);
118 void agentx_varbind_counter32(struct agentx_varbind *, uint32_t);
119 void agentx_varbind_gauge32(struct agentx_varbind *, uint32_t);
120 void agentx_varbind_unsigned32(struct agentx_varbind *, uint32_t);
121 void agentx_varbind_timeticks(struct agentx_varbind *, uint32_t);
122 void agentx_varbind_opaque(struct agentx_varbind *, const char *, size_t);
123 void agentx_varbind_counter64(struct agentx_varbind *, uint64_t);
124 void agentx_varbind_notfound(struct agentx_varbind *);
125 void agentx_varbind_error(struct agentx_varbind *);
126 
127 enum agentx_request_type agentx_varbind_request(
128     struct agentx_varbind *);
129 struct agentx_object *
130     agentx_varbind_get_object(struct agentx_varbind *);
131 int32_t agentx_varbind_get_index_integer(struct agentx_varbind *,
132     struct agentx_index *);
133 const unsigned char *agentx_varbind_get_index_string(
134     struct agentx_varbind *, struct agentx_index *, size_t *, int *);
135 const uint32_t *agentx_varbind_get_index_oid(struct agentx_varbind *,
136     struct agentx_index *, size_t *, int *);
137 const struct in_addr *agentx_varbind_get_index_ipaddress(
138     struct agentx_varbind *, struct agentx_index *);
139 void agentx_varbind_set_index_integer(struct agentx_varbind *,
140     struct agentx_index *, int32_t);
141 void agentx_varbind_set_index_string(struct agentx_varbind *,
142     struct agentx_index *, const char *);
143 void agentx_varbind_set_index_nstring(struct agentx_varbind *,
144     struct agentx_index *, const unsigned char *, size_t);
145 void agentx_varbind_set_index_oid(struct agentx_varbind *,
146     struct agentx_index *, const uint32_t *, size_t);
147 void agentx_varbind_set_index_object(struct agentx_varbind *,
148     struct agentx_index *, struct agentx_object *);
149 void agentx_varbind_set_index_ipaddress(struct agentx_varbind *,
150     struct agentx_index *, const struct in_addr *);
151