1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! MibHandler trait and related types.
use Future;
use Pin;
use crateOid;
use crateValue;
use ;
/// Type alias for boxed async return type (dyn-compatible).
///
/// This type is required because async trait methods cannot be object-safe.
/// All handler methods return `BoxFuture` to allow handlers to be stored
/// as trait objects in the agent.
///
/// # Example
///
/// ```rust
/// use async_snmp::handler::{BoxFuture, GetResult};
///
/// fn example_async_fn<'a>(value: &'a i32) -> BoxFuture<'a, GetResult> {
/// Box::pin(async move {
/// // Async work here
/// GetResult::Value(async_snmp::Value::Integer(*value))
/// })
/// }
/// ```
pub type BoxFuture<'a, T> = ;
/// Handler for SNMP MIB operations.
///
/// Implement this trait to provide values for a subtree of OIDs.
/// Register handlers with [`AgentBuilder::handler()`](crate::agent::AgentBuilder::handler)
/// using a prefix OID.
///
/// # Required Methods
///
/// - [`get`](MibHandler::get): Handle GET requests for specific OIDs
/// - [`get_next`](MibHandler::get_next): Handle GETNEXT/GETBULK requests
///
/// # Optional Methods
///
/// - [`test_set`](MibHandler::test_set): Validate SET operations (default: read-only)
/// - [`commit_set`](MibHandler::commit_set): Apply SET operations (default: read-only)
/// - [`undo_set`](MibHandler::undo_set): Rollback failed SET operations
/// - [`free_set`](MibHandler::free_set): Cleanup resources on test failure
/// - [`handles`](MibHandler::handles): Custom OID matching logic
///
/// # GET Implementation
///
/// The [`get`](MibHandler::get) method should return:
/// - [`GetResult::Value`] if the OID exists and has a value
/// - [`GetResult::NoSuchObject`] if the object type is not implemented
/// - [`GetResult::NoSuchInstance`] if the object exists but this instance doesn't
///
/// # GETNEXT and Lexicographic Ordering
///
/// The [`get_next`](MibHandler::get_next) method must return the lexicographically
/// next OID after the requested one. OIDs are compared arc-by-arc as unsigned integers.
/// For example: `1.3.6.1.2` < `1.3.6.1.2.1` < `1.3.6.1.3`.
///
/// Key considerations:
/// - The returned OID must be strictly greater than the input OID
/// - GETBULK uses GETNEXT repeatedly, so efficient implementation matters
/// - Use [`OidTable`](super::OidTable) to simplify sorted OID management
///
/// # SET Two-Phase Commit (RFC 3416)
///
/// SET operations use a multi-phase protocol modeled after net-snmp's
/// RESERVE1/RESERVE2/ACTION/COMMIT/FREE/UNDO phases:
///
/// 1. **Test phase**: [`test_set`](MibHandler::test_set) is called for ALL varbinds
/// before any commits. If any test fails, [`free_set`](MibHandler::free_set)
/// is called for all previously successful varbinds (in reverse order) to
/// release resources allocated during the test phase.
///
/// 2. **Commit phase**: [`commit_set`](MibHandler::commit_set) is called for each
/// varbind in order. If a commit fails, [`undo_set`](MibHandler::undo_set) is
/// called for all previously committed varbinds (in reverse order).
///
/// By default, handlers are read-only and return [`SetResult::NotWritable`].
///
/// # Bounds
///
/// The `'static` bound is required because handlers are stored as
/// `Arc<dyn MibHandler>` within the agent. This allows the agent to
/// hold handlers for its entire lifetime without lifetime annotations.
/// In practice, most handlers naturally satisfy this bound.
///
/// # Thread Safety
///
/// Handlers must be `Send + Sync` because the agent may process
/// requests concurrently from multiple tasks.
///
/// # Example: Read-Only Handler
///
/// ```rust
/// use async_snmp::handler::{MibHandler, RequestContext, GetResult, GetNextResult, BoxFuture};
/// use async_snmp::{Oid, Value, VarBind, oid};
///
/// struct SystemInfoHandler {
/// sys_descr: String,
/// sys_uptime: u32,
/// }
///
/// impl MibHandler for SystemInfoHandler {
/// fn get<'a>(&'a self, _ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, GetResult> {
/// Box::pin(async move {
/// // sysDescr.0
/// if oid == &oid!(1, 3, 6, 1, 2, 1, 1, 1, 0) {
/// return GetResult::Value(Value::OctetString(self.sys_descr.clone().into()));
/// }
/// // sysUpTime.0
/// if oid == &oid!(1, 3, 6, 1, 2, 1, 1, 3, 0) {
/// return GetResult::Value(Value::TimeTicks(self.sys_uptime));
/// }
/// GetResult::NoSuchObject
/// })
/// }
///
/// fn get_next<'a>(&'a self, _ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, GetNextResult> {
/// Box::pin(async move {
/// let sys_descr = oid!(1, 3, 6, 1, 2, 1, 1, 1, 0);
/// let sys_uptime = oid!(1, 3, 6, 1, 2, 1, 1, 3, 0);
///
/// // Return the next OID in lexicographic order
/// if oid < &sys_descr {
/// return GetNextResult::Value(VarBind::new(
/// sys_descr,
/// Value::OctetString("My System".into())
/// ));
/// }
/// if oid < &sys_uptime {
/// return GetNextResult::Value(VarBind::new(
/// sys_uptime,
/// Value::TimeTicks(12345)
/// ));
/// }
/// GetNextResult::EndOfMibView
/// })
/// }
/// }
/// ```
///
/// # Example: Writable Handler
///
/// ```rust
/// use async_snmp::handler::{
/// MibHandler, RequestContext, GetResult, GetNextResult, SetResult, BoxFuture
/// };
/// use async_snmp::{Oid, Value, VarBind, oid};
/// use std::sync::atomic::{AtomicI32, Ordering};
///
/// struct WritableHandler {
/// counter: AtomicI32,
/// }
///
/// impl MibHandler for WritableHandler {
/// fn get<'a>(&'a self, _ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, GetResult> {
/// Box::pin(async move {
/// if oid == &oid!(1, 3, 6, 1, 4, 1, 99999, 1, 0) {
/// return GetResult::Value(Value::Integer(
/// self.counter.load(Ordering::Relaxed)
/// ));
/// }
/// GetResult::NoSuchObject
/// })
/// }
///
/// fn get_next<'a>(&'a self, _ctx: &'a RequestContext, oid: &'a Oid) -> BoxFuture<'a, GetNextResult> {
/// Box::pin(async move {
/// let my_oid = oid!(1, 3, 6, 1, 4, 1, 99999, 1, 0);
/// if oid < &my_oid {
/// return GetNextResult::Value(VarBind::new(
/// my_oid,
/// Value::Integer(self.counter.load(Ordering::Relaxed))
/// ));
/// }
/// GetNextResult::EndOfMibView
/// })
/// }
///
/// fn test_set<'a>(
/// &'a self,
/// _ctx: &'a RequestContext,
/// oid: &'a Oid,
/// value: &'a Value,
/// ) -> BoxFuture<'a, SetResult> {
/// Box::pin(async move {
/// if oid != &oid!(1, 3, 6, 1, 4, 1, 99999, 1, 0) {
/// return SetResult::NotWritable;
/// }
/// // Validate the value type
/// match value {
/// Value::Integer(_) => SetResult::Ok,
/// _ => SetResult::WrongType,
/// }
/// })
/// }
///
/// fn commit_set<'a>(
/// &'a self,
/// _ctx: &'a RequestContext,
/// _oid: &'a Oid,
/// value: &'a Value,
/// ) -> BoxFuture<'a, SetResult> {
/// Box::pin(async move {
/// if let Value::Integer(v) = value {
/// self.counter.store(*v, Ordering::Relaxed);
/// SetResult::Ok
/// } else {
/// SetResult::CommitFailed
/// }
/// })
/// }
/// }
/// ```