ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
snmp_notify.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_notify.h
6 * @brief Notifications: the SNMPv2-Trap-PDU and the InformRequest-PDU (PROTOCORE_ENABLE_SNMP_TRAP).
7 *
8 * The notification originator side of the agent: an event is pushed to a notification receiver
9 * instead of waiting to be polled.
10 *
11 * RFC 3416 sec 4.2.6 defines the SNMPv2-Trap-PDU and sec 4.2.7 the InformRequest-PDU. Both carry
12 * the same two mandatory first variable-bindings: sysUpTime.0 and snmpTrapOID.0 (RFC 3418 sec 2),
13 * followed by whatever bindings the caller adds. The two differ in confirmation, not in shape: a
14 * trap is unacknowledged, while an InformRequest-PDU is answered by a Response-PDU that echoes its
15 * request-id, so its sender owns the retransmission.
16 *
17 * RFC 3417 sec 3.2 suggests notification receivers listen on UDP port 162.
18 *
19 * The build calls are pure: message octets in a caller buffer, no socket and no clock, so they are
20 * unit-tested with no network stack under them. The send calls add the address parse and the
21 * datagram. SNMPv3 USM notifications are the v3 layer's, reached through ::SnmpV3.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef PROTOCORE_SNMP_NOTIFY_H
28#define PROTOCORE_SNMP_NOTIFY_H
29
30#include "protocore_config.h" // the entry point: protocore_types.h for the widths
31
32#if PROTOCORE_ENABLE_SNMP_TRAP
33
34EMBED_BEGIN_DECLS
35
36#include "services/net/snmp/snmp_ber/snmp_ber.h" // BerEnc: the open encoder a PDU append writes into
37/** @brief Which SMIv2 type a caller variable-binding carries (RFC 2578 sec 7.1). */
38EMBED_ENUM_BYTE(SnmpVbType,
39 SNMP_VB_INT = 0, ///< INTEGER, in ival
40 SNMP_VB_STRING = 1, ///< OCTET STRING, in bytes/blen
41 SNMP_VB_OID = 2, ///< OBJECT IDENTIFIER, in oid_val/oid_val_len
42 SNMP_VB_COUNTER32 = 3, ///< Counter32, in ival (RFC 2578 sec 7.1.6)
43 SNMP_VB_GAUGE32 = 4, ///< Gauge32, in ival (RFC 2578 sec 7.1.7)
44 SNMP_VB_TIMETICKS = 5, ///< TimeTicks, in ival (RFC 2578 sec 7.1.8)
45 SNMP_VB_IPADDR = 6, ///< IpAddress, 4 octets in bytes (RFC 2578 sec 7.1.5)
46);
47
48/** @brief One variable-binding of the VarBindList: a name and its typed value (RFC 3416 sec 3). */
49typedef struct
50{
51 const uint32_t *oid; ///< the binding's name, as subidentifiers
52 size_t oid_len; ///< how many
53 uint8_t type; ///< which ::SnmpVbType the value is
54 long ival; ///< INTEGER, Counter32, Gauge32 or TimeTicks value
55 const uint8_t *bytes; ///< OCTET STRING or IpAddress octets
56 size_t blen; ///< how many
57 const uint32_t *oid_val; ///< an OBJECT IDENTIFIER value's subidentifiers
58 size_t oid_val_len; ///< how many
59} SnmpVarbind;
60
61/** @brief RFC 3416 sec 4.2.6 and sec 4.2.7: what a notification PDU carries. */
62typedef struct
63{
64 uint8_t pdu_tag; ///< ::SNMP_TAG_SNMP_PDU_TRAPV2 or ::SNMP_TAG_SNMP_PDU_INFORM
65 uint32_t request_id; ///< the PDU's request-id, echoed by an inform's Response-PDU
66 const uint32_t *trap_oid; ///< the snmpTrapOID.0 value (RFC 3418 sec 2)
67 size_t trap_oid_len; ///< how many subidentifiers
68 uint32_t uptime_ticks; ///< the sysUpTime.0 value, TimeTicks (RFC 2578 sec 7.1.8)
69 const SnmpVarbind *vbs; ///< the caller bindings that follow the mandatory two
70 size_t vb_count; ///< how many
71} SnmpNotifyPduArgs;
72
73/** @brief RFC 3417 sec 3.2: the notification receiver a send addresses. */
74typedef struct
75{
76 const char *dst_ip; ///< its address, as text
77 uint16_t port; ///< its port, 162 by convention
78 const char *community; ///< the community the message carries (RFC 1157 sec 3.2.5)
79} SnmpNotifyDstArgs;
80
81/** @brief Where a notification is built: an open encoder, or a bare buffer. */
82typedef struct
83{
84 BerEnc *enc; ///< the open encoder a PDU append writes into
85 uint8_t *out; ///< where a complete message is built
86 size_t cap; ///< how many octets that holds
87} SnmpNotifyBufArgs;
88
89/**
90 * @brief The notification originator (RFC 3416 sec 4.2.6, sec 4.2.7).
91 *
92 * A caller sets the members a call takes, invokes it through ::SnmpNotify, and reads the outcome
93 * off the same handle.
94 *
95 * @var SnmpNotifyNs::pdu what the notification PDU carries
96 * @var SnmpNotifyNs::dst the notification receiver a send addresses
97 * @var SnmpNotifyNs::buf where the message is built
98 * @var SnmpNotifyNs::ok a send's true/false outcome: the stack took the datagram
99 * @var SnmpNotifyNs::n octets a build wrote, 0 when the buffer could not hold them
100 * @var SnmpNotifyNs::build_pdu append the notification PDU to @c buf.enc, mandatory bindings first
101 * @var SnmpNotifyNs::build_v2c build a complete SNMPv2c notification message into @c buf.out
102 * @var SnmpNotifyNs::trap_v2c build and send an SNMPv2-Trap-PDU, sysUpTime.0 from the clock
103 * @var SnmpNotifyNs::inform_v2c build and send an InformRequest-PDU under the caller's request-id
104 */
105typedef struct
106{
107 SnmpNotifyPduArgs pdu; ///< what the notification PDU carries
108 SnmpNotifyDstArgs dst; ///< where a send goes
109 SnmpNotifyBufArgs buf; ///< where the message is built
110 embed_bool ok;
111 size_t n;
112} SnmpNotifyVars;
113
114/** @brief The operands and the outcome. */
115extern SnmpNotifyVars SnmpNotifyV;
116
117/** @brief The entries. */
118typedef struct
119{
120 void (*const build_pdu)(uint8_t *work);
121 void (*const build_v2c)(uint8_t *work);
122 void (*const trap_v2c)(uint8_t *work);
123 void (*const inform_v2c)(uint8_t *work);
124} SnmpNotifyNs;
125
126// What the table binds, defined once in the .c and taking one parameter each: everything
127// else an entry needs is an operand in SnmpNotifyV or a region of the borrow at a fixed offset.
128void protocore_snmp_notify_build_pdu(uint8_t *work);
129void protocore_snmp_notify_build_v2c(uint8_t *work);
130void protocore_snmp_notify_trap_v2c(uint8_t *work);
131void protocore_snmp_notify_inform_v2c(uint8_t *work);
132
133// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
134// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
135// `SnmpNotify.build_pdu(work)` resolves to a named function and becomes a DIRECT call. An extern table
136// leaves the call indirect and the symbol live at every level, -O2 -flto included.
137static const SnmpNotifyNs SnmpNotify __attribute__((unused)) = {
138 .build_pdu = protocore_snmp_notify_build_pdu,
139 .build_v2c = protocore_snmp_notify_build_v2c,
140 .trap_v2c = protocore_snmp_notify_trap_v2c,
141 .inform_v2c = protocore_snmp_notify_inform_v2c,
142};
143
144/**
145 * @brief The PROTOCORE_SNMP_NOTIFY_BORROW bytes this module's state lives in.
146 *
147 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
148 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
149 * walks, so the state lasts the life of the program.
150 *
151 * @return the span.
152 */
153uint8_t *protocore_snmp_notify_span(void);
154
155EMBED_END_DECLS
156
157#endif // PROTOCORE_ENABLE_SNMP_TRAP
158
159#endif // PROTOCORE_SNMP_NOTIFY_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.