ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
snmp_agent.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file snmp_agent.h
6 * @brief The command responder: community-framed messages, PDU processing, and a fixed MIB.
7 *
8 * The agent answers management requests against a fixed table of managed objects. Two framings
9 * reach the same table:
10 *
11 * - **SNMPv1**, RFC 1157 sec 4: `Message ::= SEQUENCE { version version-1(0), community OCTET
12 * STRING, data ANY }`. RFC 1157 sec 3.2.5 makes the community the whole of authentication: a
13 * message is authentic when it belongs to the community it names. RFC 1157 sec 4.1 says an
14 * entity that fails authentication notes the failure and discards the datagram, so an unknown
15 * community is answered with nothing at all.
16 * - **SNMPv2c**, RFC 1901 sec 3: the same wrapper with version(1), carrying the RFC 3416 PDUs.
17 *
18 * PDU processing is RFC 3416 sec 4.2: the GetRequest-PDU (sec 4.2.1), the GetNextRequest-PDU
19 * (sec 4.2.2), the GetBulkRequest-PDU (sec 4.2.3) and the SetRequest-PDU (sec 4.2.5), each
20 * answered by a Response-PDU (sec 4.2.4). The GetBulkRequest-PDU and the per-binding exceptions
21 * noSuchObject, noSuchInstance and endOfMibView belong to v2c and v3; v1 reports through
22 * error-status and error-index instead (RFC 1157 sec 4.1.1).
23 *
24 * The MIB is a fixed table of SNMP_MAX_MIB_ENTRIES object instances, in BSS. A value is either
25 * held in the entry or fetched through a getter, as sysUpTime.0 is. String and OBJECT IDENTIFIER
26 * values are referenced by pointer and must outlive the agent.
27 *
28 * Message processing is pure: request octets in, response octets out, no socket and no heap, so
29 * it is unit-tested with no network stack under it. RFC 3417 sec 3.2 suggests command responders
30 * listen on UDP port 161, which @ref SnmpAgentNs::listen binds through the transport UDP service.
31 * SNMPv3 is the separate USM layer, reached through ::SnmpV3.
32 *
33 * @author Douglas Quigg (dstroy0)
34 * @date 2026
35 */
36
37#ifndef PROTOCORE_SNMP_AGENT_H
38#define PROTOCORE_SNMP_AGENT_H
39
40#include "protocore_config.h" // the entry point: protocore_types.h for the widths
41
42#if PROTOCORE_ENABLE_SNMP
43
44EMBED_BEGIN_DECLS
45
47/** @brief The version field of the message wrapper, as it is encoded. */
48EMBED_ENUM_BYTE(SnmpVersion,
49 SNMP_V1 = 0, ///< version-1(0), RFC 1157 sec 4
50 SNMP_V2C = 1, ///< version(1), RFC 1901 sec 3
51 SNMP_V3 = 3, ///< msgVersion, RFC 3412 sec 6
52);
53
54/** @brief error-status values (RFC 1157 sec 4.1.1 for 0 through 5, RFC 3416 sec 3 for the rest). */
55EMBED_ENUM_BYTE(SnmpErr, SNMP_ERR_NO_ERROR = 0, SNMP_ERR_TOO_BIG = 1, SNMP_ERR_NO_SUCH_NAME = 2, SNMP_ERR_BAD_VALUE = 3,
56 SNMP_ERR_READ_ONLY = 4, SNMP_ERR_GEN_ERR = 5, SNMP_ERR_NO_ACCESS = 6, SNMP_ERR_WRONG_TYPE = 7,
57 SNMP_ERR_NOT_WRITABLE = 17, );
58
59/**
60 * @brief The value half of a variable-binding (RFC 3416 sec 3).
61 *
62 * Only the field matching @ref type carries anything. String and OBJECT IDENTIFIER values are
63 * referenced, not copied, so they must stay valid.
64 */
65typedef struct
66{
67 uint8_t type; ///< the value's tag: an ASN.1 simple type, an SMIv2 application-wide type
68 ///< (RFC 2578 sec 7.1), or a VarBind exception marker
69 long ival; ///< the INTEGER value
70 uint32_t uval; ///< the TimeTicks, Counter32, Gauge32 or IpAddress value
71 const char *str; ///< the OCTET STRING octets, not owned
72 size_t str_len; ///< how many
73 const uint32_t *oid; ///< the OBJECT IDENTIFIER subidentifiers, not owned
74 size_t oid_len; ///< how many
75} SnmpValue;
76
77/** @brief Read a dynamic object's value: fill @p out and return true, or false for noSuchInstance. */
78typedef embed_bool (*SnmpGetFn)(SnmpValue *out);
79/** @brief Write a read-write object (RFC 2578 sec 7.3): true on success, false to reject the value. */
80typedef embed_bool (*SnmpSetFn)(const SnmpValue *in);
81
82/** @brief RFC 1157 sec 3.2.5: the communities a message is authenticated against. */
83typedef struct
84{
85 const char *ro; ///< the community that authorizes reads; NULL keeps the built-in default
86 const char *rw; ///< the community that authorizes a SetRequest-PDU; NULL or empty refuses every write
87} SnmpCommunityArgs;
88
89/** @brief RFC 2578 sec 7: one managed object instance and how its value is reached. */
90typedef struct
91{
92 const uint32_t *oid; ///< the instance name, as subidentifiers
93 size_t oid_len; ///< how many, at least 2
94 uint8_t type; ///< a dynamic object's value tag (RFC 2578 sec 7.1)
95 const char *text; ///< the OCTET STRING value a static registration takes, referenced not copied
96 long ival; ///< the INTEGER value a static registration takes
97 SnmpGetFn getter; ///< what a dynamic object's value is read through
98 SnmpSetFn setter; ///< what a write reaches; NULL leaves the object read-only (RFC 2578 sec 7.3)
99} SnmpObjectArgs;
100
101/** @brief RFC 3418 sec 2: the system group under 1.3.6.1.2.1.1. */
102typedef struct
103{
104 const char *descr; ///< sysDescr.0
105 const char *contact; ///< sysContact.0
106 const char *name; ///< sysName.0
107 const char *location; ///< sysLocation.0
108 long services; ///< sysServices.0, the layer bitmask
109} SnmpSystemArgs;
110
111/** @brief RFC 3417 sec 3.1: the serialized message read, and the one written back. */
112typedef struct
113{
114 const uint8_t *req; ///< the received message octets
115 size_t req_len; ///< how many
116 uint8_t *resp; ///< where the response message is written
117 size_t resp_cap; ///< how many octets that holds
118} SnmpMsgArgs;
119
120/** @brief RFC 3416 sec 4.2: the request PDU dispatched, and the Response-PDU written. */
121typedef struct
122{
123 const uint8_t *req; ///< one complete request-PDU TLV
124 size_t req_len; ///< its length in octets
125 uint8_t *out; ///< where the Response-PDU TLV is written
126 size_t out_cap; ///< how many octets that holds
127 embed_bool allow_write; ///< a SetRequest-PDU is authorized (RFC 3416 sec 4.2.5)
128 embed_bool v2c; ///< report per-binding exceptions rather than v1 error-status
129} SnmpPduArgs;
130
131/**
132 * @brief The command responder: the MIB, the PDU processing, and the message framing.
133 *
134 * A caller sets the members a call takes, invokes it through ::SnmpAgent, and reads the outcome
135 * off the same handle.
136 *
137 * @var SnmpAgentNs::port the UDP port a listen binds, 161 by convention (RFC 3417 sec 3.2)
138 * @var SnmpAgentNs::community the communities a message is authenticated against
139 * @var SnmpAgentNs::object the managed object a registration binds
140 * @var SnmpAgentNs::system the system group values (RFC 3418 sec 2)
141 * @var SnmpAgentNs::msg the message read and the one written back
142 * @var SnmpAgentNs::pdu the PDU dispatched and the Response-PDU written
143 * @var SnmpAgentNs::ok a registration's true/false outcome: the table had room
144 * @var SnmpAgentNs::n octets written, 0 to send nothing
145 * @var SnmpAgentNs::init empty the MIB and take the read-only community
146 * @var SnmpAgentNs::set_rw_community take the community that authorizes a SetRequest-PDU
147 * @var SnmpAgentNs::set_system register the system group (RFC 3418 sec 2)
148 * @var SnmpAgentNs::add_string register an object whose value is an OCTET STRING
149 * @var SnmpAgentNs::add_integer register an object whose value is an INTEGER
150 * @var SnmpAgentNs::add_dynamic register an object whose value is read through a getter
151 * @var SnmpAgentNs::dispatch_pdu run one request PDU against the MIB and write a Response-PDU
152 * @var SnmpAgentNs::process decode one message, dispatch it, and frame the response message
153 * @var SnmpAgentNs::listen answer requests arriving on @c port
154 */
155typedef struct
156{
157 uint16_t port; ///< the UDP port a listen binds
158 SnmpCommunityArgs community; ///< what authenticates a message (RFC 1157 sec 3.2.5)
159 SnmpObjectArgs object; ///< what a registration binds (RFC 2578 sec 7)
160 SnmpSystemArgs system; ///< the system group values (RFC 3418 sec 2)
161 SnmpMsgArgs msg; ///< the message pair (RFC 3417 sec 3.1)
162 SnmpPduArgs pdu; ///< the PDU pair (RFC 3416 sec 4.2)
163 embed_bool ok;
164 size_t n;
165} SnmpAgentVars;
166
167/** @brief The operands and the outcome. */
168extern SnmpAgentVars SnmpAgentV;
169
170/** @brief The entries. */
171typedef struct
172{
173 void (*const init)(uint8_t *work);
174 void (*const set_rw_community)(uint8_t *work);
175 void (*const set_system)(uint8_t *work);
176 void (*const add_string)(uint8_t *work);
177 void (*const add_integer)(uint8_t *work);
178 void (*const add_dynamic)(uint8_t *work);
179 void (*const dispatch_pdu)(uint8_t *work);
180 void (*const process)(uint8_t *work);
181 void (*const listen)(const uint8_t *work);
182} SnmpAgentNs;
183
184// What the table binds, defined once in the .c and taking one parameter each: everything
185// else an entry needs is an operand in SnmpAgentV or a region of the borrow at a fixed offset.
186void protocore_snmp_agent_init(uint8_t *work);
187void protocore_snmp_agent_set_rw_community(uint8_t *work);
188void protocore_snmp_agent_set_system(uint8_t *work);
189void protocore_snmp_agent_add_string(uint8_t *work);
190void protocore_snmp_agent_add_integer(uint8_t *work);
191void protocore_snmp_agent_add_dynamic(uint8_t *work);
192void protocore_snmp_agent_dispatch_pdu(uint8_t *work);
193void protocore_snmp_agent_process(uint8_t *work);
194void protocore_snmp_agent_listen(const uint8_t *work);
195
196// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
197// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
198// `SnmpAgent.init(work)` resolves to a named function and becomes a DIRECT call. An extern table
199// leaves the call indirect and the symbol live at every level, -O2 -flto included.
200static const SnmpAgentNs SnmpAgent __attribute__((unused)) = {
201 .init = protocore_snmp_agent_init,
202 .set_rw_community = protocore_snmp_agent_set_rw_community,
203 .set_system = protocore_snmp_agent_set_system,
204 .add_string = protocore_snmp_agent_add_string,
205 .add_integer = protocore_snmp_agent_add_integer,
206 .add_dynamic = protocore_snmp_agent_add_dynamic,
207 .dispatch_pdu = protocore_snmp_agent_dispatch_pdu,
208 .process = protocore_snmp_agent_process,
209 .listen = protocore_snmp_agent_listen,
210};
211
212/**
213 * @brief The PROTOCORE_SNMP_AGENT_BORROW bytes this module's state lives in.
214 *
215 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
216 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
217 * walks, so the state lasts the life of the program.
218 *
219 * @return the span.
220 */
221uint8_t *protocore_snmp_agent_span(void);
222
223EMBED_END_DECLS
224
225#endif // PROTOCORE_ENABLE_SNMP
226
227#endif // PROTOCORE_SNMP_AGENT_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.
The SNMP serialization: ASN.1 Basic Encoding Rules over a caller buffer.