ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
snmp_ber.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_ber.h
6 * @brief The SNMP serialization: ASN.1 Basic Encoding Rules over a caller buffer.
7 *
8 * RFC 3417 sec 3.1: "Each instance of a message is serialized (i.e., encoded according to the
9 * convention of [BER]) onto a single UDP over IPv4 datagram, using the algorithm specified in
10 * Section 8." The encoding rules themselves are not an IETF RFC: they are ITU-T Recommendation
11 * X.690 (ISO/IEC 8825-1), which RFC 3417 cites as [BER].
12 *
13 * RFC 3417 sec 8 states the two limits this codec is built to. Item (1): "When encoding the
14 * length field, only the definite form is used; use of the indefinite form encoding is
15 * prohibited. Note that when using the definite-long form, it is permissible to use more than the
16 * minimum number of length octets necessary to encode the length field." A constructed type
17 * therefore opens with a reserved 3-octet definite-long length that is back-patched at close, so
18 * no value is buffered and no octet is shifted. Item (2): simple types are encoded primitive, and
19 * the constructed form is used only for SEQUENCE.
20 *
21 * The tags cover exactly what SNMP puts on the wire: the ASN.1 simple types, the SMIv2
22 * application-wide types (RFC 2578 sec 7.1.5 through 7.1.10), the variable-binding exception
23 * markers and the context-specific PDU tags of the RFC 3416 sec 3 ASN.1.
24 *
25 * Encoder and decoder both run over caller-provided fixed buffers, so the module holds nothing
26 * between calls and is unit-testable with no network stack under it.
27 *
28 * @author Douglas Quigg (dstroy0)
29 * @date 2026
30 */
31
32#ifndef PROTOCORE_SNMP_BER_H
33#define PROTOCORE_SNMP_BER_H
34
35#include "protocore_config.h" // the entry point: protocore_types.h for the widths
36
37#if PROTOCORE_ENABLE_SNMP
38
39EMBED_BEGIN_DECLS
40
41/** @brief The identifier octets SNMP puts on the wire. */
42EMBED_ENUM_BYTE(SnmpTag,
43 // ASN.1 simple types, encoded primitive (RFC 3417 sec 8 item 2).
44 SNMP_TAG_BER_INTEGER = 0x02, SNMP_TAG_BER_OCTET_STRING = 0x04, SNMP_TAG_BER_NULL = 0x05,
45 SNMP_TAG_BER_OID = 0x06, SNMP_TAG_BER_SEQUENCE = 0x30,
46 // SMIv2 application-wide types (RFC 2578 sec 7.1.5 through 7.1.10).
47 SNMP_TAG_SNMP_IPADDRESS = 0x40, SNMP_TAG_SNMP_COUNTER32 = 0x41, SNMP_TAG_SNMP_GAUGE32 = 0x42,
48 SNMP_TAG_SNMP_TIMETICKS = 0x43, SNMP_TAG_SNMP_OPAQUE = 0x44, SNMP_TAG_SNMP_COUNTER64 = 0x46,
49 // VarBind CHOICE exception markers (RFC 3416 sec 3; used per sec 4.2.1 and sec 4.2.2).
50 SNMP_TAG_SNMP_NO_SUCH_OBJECT = 0x80, SNMP_TAG_SNMP_NO_SUCH_INSTANCE = 0x81,
51 SNMP_TAG_SNMP_END_OF_MIB_VIEW = 0x82,
52 // PDU tags, context-specific constructed (RFC 3416 sec 3).
53 SNMP_TAG_SNMP_PDU_GET = 0xA0, ///< GetRequest-PDU ::= [0] IMPLICIT PDU
54 SNMP_TAG_SNMP_PDU_GETNEXT = 0xA1, ///< GetNextRequest-PDU ::= [1] IMPLICIT PDU
55 SNMP_TAG_SNMP_PDU_RESPONSE = 0xA2, ///< Response-PDU ::= [2] IMPLICIT PDU
56 SNMP_TAG_SNMP_PDU_SET = 0xA3, ///< SetRequest-PDU ::= [3] IMPLICIT PDU
57 SNMP_TAG_SNMP_PDU_GETBULK = 0xA5, ///< GetBulkRequest-PDU ::= [5] IMPLICIT BulkPDU
58 SNMP_TAG_SNMP_PDU_INFORM = 0xA6, ///< InformRequest-PDU ::= [6] IMPLICIT PDU
59 SNMP_TAG_SNMP_PDU_TRAPV2 = 0xA7, ///< SNMPv2-Trap-PDU ::= [7] IMPLICIT PDU
60 SNMP_TAG_SNMP_PDU_REPORT = 0xA8, ///< Report-PDU ::= [8] IMPLICIT PDU
61);
62
63/**
64 * @brief An encoder's cursor: the caller buffer, how far it is written, and whether it still fits.
65 *
66 * The cursor is the caller's, so several encodings can be open at once (a PDU into one buffer
67 * while the message that will carry it is framed in another).
68 */
69typedef struct BerEnc
70{
71 uint8_t *buf; ///< the buffer octets are written into
72 size_t cap; ///< how many it holds
73 size_t len; ///< how many are written
74 embed_bool ok; ///< no write has run past cap
75} BerEnc;
76
77/** @brief A decoder's cursor: the octets being read, and how far the read walked. */
78typedef struct
79{
80 const uint8_t *buf; ///< the octets being read
81 size_t len; ///< how many
82 size_t pos; ///< the next octet a read takes
83 embed_bool ok; ///< no read has run past len
84} BerDec;
85
86/** @brief The caller buffer a codec runs over. */
87typedef struct
88{
89 uint8_t *out; ///< where an encoder writes its octets
90 const uint8_t *in; ///< the octets a decoder reads
91 size_t cap; ///< octets available at @c out, or held by @c in
92} SnmpBerBufArgs;
93
94/** @brief The TLV a write carries: its identifier octet and the value under it. */
95typedef struct
96{
97 uint8_t tag; ///< the identifier octet the write emits
98 long ival; ///< the INTEGER value
99 uint32_t uval; ///< the non-negative application-type value (RFC 2578 sec 7.1.6 through 7.1.8)
100 const uint8_t *bytes; ///< OCTET STRING octets, or pre-encoded octets a raw append copies
101 size_t len; ///< how many
102 const uint32_t *arcs; ///< the OBJECT IDENTIFIER subidentifiers
103 size_t arc_count; ///< how many, at least 2
104 size_t token; ///< in: the constructed type a close back-patches; out: the one an open reserved
105} SnmpBerTlvArgs;
106
107/** @brief Where a read lands what it took. */
108typedef struct
109{
110 uint32_t *arc_out; ///< where an OBJECT IDENTIFIER read lands its subidentifiers
111 size_t arc_cap; ///< how many that holds, at least 2
112 size_t skip; ///< value octets a skip steps over
113} SnmpBerReadArgs;
114
115/**
116 * @brief The SNMP serialization (RFC 3417 sec 8, over ITU-T X.690).
117 *
118 * A caller binds a cursor with an init, sets the members a call takes, invokes it through
119 * ::SnmpBer, and reads the outcome off the same handle. Nesting is explicit: an open reports the
120 * token its close needs, so the caller holds it while the inner types are written.
121 *
122 * No storage member: both cursors are the caller's and every call reads or writes only through
123 * them, so the module keeps nothing between calls.
124 *
125 * @var SnmpBerNs::enc the encoder cursor a write acts on
126 * @var SnmpBerNs::dec the decoder cursor a read acts on
127 * @var SnmpBerNs::buf the caller buffer an init binds a cursor to
128 * @var SnmpBerNs::tlv the identifier octet and value a write carries
129 * @var SnmpBerNs::read_args where a read lands what it took
130 * @var SnmpBerNs::ok a call's true/false outcome: the cursor still fits its buffer
131 * @var SnmpBerNs::tag the identifier octet a header read took
132 * @var SnmpBerNs::vlen the value length that header states, in octets
133 * @var SnmpBerNs::ival the value an INTEGER read took
134 * @var SnmpBerNs::n subidentifiers an OBJECT IDENTIFIER read landed
135 * @var SnmpBerNs::enc_init bind an encoder cursor to @c buf.out for @c buf.cap octets
136 * @var SnmpBerNs::put_integer write an INTEGER, two's complement, minimal
137 * @var SnmpBerNs::put_uint write a non-negative value under @c tlv.tag, big-endian, minimal
138 * @var SnmpBerNs::put_octet_string write @c tlv.bytes under @c tlv.tag
139 * @var SnmpBerNs::put_null write NULL, the value a GetRequest-PDU binding carries
140 * @var SnmpBerNs::put_oid write an OBJECT IDENTIFIER from @c tlv.arcs
141 * @var SnmpBerNs::put_tlv write one primitive TLV verbatim
142 * @var SnmpBerNs::put_raw append already-encoded octets, no header of their own
143 * @var SnmpBerNs::seq_begin open a constructed type, reserving its definite-long length
144 * @var SnmpBerNs::seq_end close it, back-patching that length (RFC 3417 sec 8 item 1)
145 * @var SnmpBerNs::dec_init bind a decoder cursor to @c buf.in for @c buf.cap octets
146 * @var SnmpBerNs::read_header take a tag and length, leaving the cursor at the value
147 * @var SnmpBerNs::read_integer take an INTEGER, sign-extended from its first octet
148 * @var SnmpBerNs::read_oid take an OBJECT IDENTIFIER into @c read.arc_out
149 * @var SnmpBerNs::skip step the cursor past @c read.skip value octets
150 */
151typedef struct
152{
153 BerEnc *enc; ///< the encoder cursor every write names
154 BerDec *dec; ///< the decoder cursor every read names
155 SnmpBerBufArgs buf; ///< the caller buffer an init binds
156 SnmpBerTlvArgs tlv; ///< what a write carries
157 SnmpBerReadArgs read_args; ///< where a read lands
158 embed_bool ok;
159 uint8_t tag;
160 size_t vlen;
161 long ival;
162 size_t n;
163} SnmpBerVars;
164
165/** @brief The operands and the outcome. */
166extern SnmpBerVars SnmpBerV;
167
168/** @brief The entries. */
169typedef struct
170{
171 void (*const enc_init)(const uint8_t *work);
172 void (*const put_integer)(const uint8_t *work);
173 void (*const put_uint)(const uint8_t *work);
174 void (*const put_octet_string)(const uint8_t *work);
175 void (*const put_null)(const uint8_t *work);
176 void (*const put_oid)(const uint8_t *work);
177 void (*const put_tlv)(const uint8_t *work);
178 void (*const put_raw)(const uint8_t *work);
179 void (*const seq_begin)(const uint8_t *work);
180 void (*const seq_end)(const uint8_t *work);
181 void (*const dec_init)(const uint8_t *work);
182 void (*const read_header)(const uint8_t *work);
183 void (*const read_integer)(const uint8_t *work);
184 void (*const read_oid)(const uint8_t *work);
185 void (*const skip)(const uint8_t *work);
186} SnmpBerNs;
187
188// What the table binds, defined once in the .c and taking one parameter each: everything
189// else an entry needs is an operand in SnmpBerV or a region of the borrow at a fixed offset.
190void protocore_snmp_ber_enc_init(const uint8_t *work);
191void protocore_snmp_ber_put_integer(const uint8_t *work);
192void protocore_snmp_ber_put_uint(const uint8_t *work);
193void protocore_snmp_ber_put_octet_string(const uint8_t *work);
194void protocore_snmp_ber_put_null(const uint8_t *work);
195void protocore_snmp_ber_put_oid(const uint8_t *work);
196void protocore_snmp_ber_put_raw(const uint8_t *work);
197void protocore_snmp_ber_seq_begin(const uint8_t *work);
198void protocore_snmp_ber_seq_end(const uint8_t *work);
199void protocore_snmp_ber_dec_init(const uint8_t *work);
200void protocore_snmp_ber_read_header(const uint8_t *work);
201void protocore_snmp_ber_read_integer(const uint8_t *work);
202void protocore_snmp_ber_read_oid(const uint8_t *work);
203void protocore_snmp_ber_skip(const uint8_t *work);
204
205// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
206// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
207// `SnmpBer.enc_init(work)` resolves to a named function and becomes a DIRECT call. An extern table
208// leaves the call indirect and the symbol live at every level, -O2 -flto included.
209static const SnmpBerNs SnmpBer __attribute__((unused)) = {
210 .enc_init = protocore_snmp_ber_enc_init,
211 .put_integer = protocore_snmp_ber_put_integer,
212 .put_uint = protocore_snmp_ber_put_uint,
213 .put_octet_string = protocore_snmp_ber_put_octet_string,
214 .put_null = protocore_snmp_ber_put_null,
215 .put_oid = protocore_snmp_ber_put_oid,
216 .put_tlv = protocore_snmp_ber_put_octet_string,
217 .put_raw = protocore_snmp_ber_put_raw,
218 .seq_begin = protocore_snmp_ber_seq_begin,
219 .seq_end = protocore_snmp_ber_seq_end,
220 .dec_init = protocore_snmp_ber_dec_init,
221 .read_header = protocore_snmp_ber_read_header,
222 .read_integer = protocore_snmp_ber_read_integer,
223 .read_oid = protocore_snmp_ber_read_oid,
224 .skip = protocore_snmp_ber_skip,
225};
226
227EMBED_END_DECLS
228
229#endif // PROTOCORE_ENABLE_SNMP
230
231#endif // PROTOCORE_SNMP_BER_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.