async-snmp 0.18.0

Modern async-first SNMP client library for Rust
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Fixed-cardinality response-shape validation.

use crate::{Oid, Value, VarBind};
use std::ops::Range;

/// Receive-side handling for malformed fixed-cardinality responses.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum ResponseShapePolicy {
    /// Preserve all decoded bindings and describe any shape anomalies.
    #[default]
    Compatible,
    /// Reject a response containing any shape anomaly.
    Strict,
}

/// The request operation associated with a fixed-cardinality response.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FixedCardinalityOperation {
    Get,
    GetNext,
    Set,
}

/// Metadata retained from decoding one or more network responses.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ResponseMetadata {
    /// Accepted BER/value deviations in response and decode order.
    pub decode_anomalies: Vec<crate::DecodeAnomaly>,
}

impl ResponseMetadata {
    pub(crate) fn append(&mut self, mut other: Self) {
        self.decode_anomalies.append(&mut other.decode_anomalies);
    }

    pub(crate) fn from_decode_anomalies(decode_anomalies: Vec<crate::DecodeAnomaly>) -> Self {
        Self { decode_anomalies }
    }
}

/// A GETBULK response and its wire-decode metadata.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BulkResponse {
    /// Every decoded binding in received order.
    pub varbinds: Vec<VarBind>,
    /// Wire-decode metadata.
    pub metadata: ResponseMetadata,
}

/// A decoded fixed-cardinality response and its shape diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixedCardinalityResponse {
    pub operation: FixedCardinalityOperation,
    /// Every decoded response binding, in received order.
    pub varbinds: Vec<VarBind>,
    /// Shape anomalies. Empty means the response exactly satisfied the request shape.
    pub anomalies: Vec<ResponseShapeAnomaly>,
    /// Accepted BER/value deviations from the wire response, in decode order.
    pub metadata: ResponseMetadata,
}

impl FixedCardinalityResponse {
    pub(crate) fn empty(operation: FixedCardinalityOperation) -> Self {
        Self {
            operation,
            varbinds: Vec::new(),
            anomalies: Vec::new(),
            metadata: ResponseMetadata::default(),
        }
    }

    /// Returns the sole response binding when the response has exactly one
    /// binding and no shape anomalies.
    ///
    /// Compatible response-shape handling can preserve empty, excess, renamed,
    /// or otherwise anomalous responses. Callers that require a valid singleton
    /// can use this method without discarding those diagnostics.
    #[must_use]
    pub fn single(&self) -> Option<&VarBind> {
        if self.anomalies.is_empty() {
            let [varbind] = self.varbinds.as_slice() else {
                return None;
            };
            Some(varbind)
        } else {
            None
        }
    }

    /// Consumes this response and returns its sole binding when it has exactly
    /// one binding and no shape anomalies.
    ///
    /// On failure, the original response is returned so that every received
    /// binding and shape diagnostic remains available to the caller.
    pub fn into_single(mut self) -> Result<VarBind, Self> {
        if self.anomalies.is_empty() && self.varbinds.len() == 1 {
            Ok(self.varbinds.remove(0))
        } else {
            Err(self)
        }
    }
}

/// A bounded, structured diagnostic for a fixed-cardinality response.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ResponseShapeAnomaly {
    /// A response batch contained fewer bindings than requested.
    Truncated {
        request_range: Range<usize>,
        response_range: Range<usize>,
        expected: usize,
        actual: usize,
    },
    /// A response batch contained more bindings than requested.
    Excess {
        request_range: Range<usize>,
        response_range: Range<usize>,
        expected: usize,
        actual: usize,
    },
    /// An exact-count response is a uniquely provable non-identity permutation.
    Reordered {
        request_range: Range<usize>,
        response_range: Range<usize>,
    },
    /// An exact-count GET or SET response changed a positional OID.
    OidMismatch {
        request_index: usize,
        response_index: usize,
        expected: Oid,
        actual: Oid,
    },
    /// An ordinary GETNEXT result did not advance lexicographically.
    GetNextNotSuccessor {
        request_index: usize,
        response_index: usize,
        cursor: Oid,
        actual: Oid,
    },
    /// EndOfMibView was returned under a name other than the request cursor.
    GetNextEndOfMibNameMismatch {
        request_index: usize,
        response_index: usize,
        cursor: Oid,
        actual: Oid,
    },
    /// GETNEXT returned a non-EndOfMibView exception.
    GetNextUnexpectedException {
        request_index: usize,
        response_index: usize,
        value: Value,
    },
    /// An exact-count, positionally named SET echo changed a value.
    SetValueMismatch {
        request_index: usize,
        response_index: usize,
        expected: Value,
        actual: Value,
    },
}

pub(crate) enum RequestShape<'a> {
    Get(&'a [Oid]),
    GetNext(&'a [Oid]),
    Set(&'a [(Oid, Value)]),
}

impl RequestShape<'_> {
    fn operation(&self) -> FixedCardinalityOperation {
        match self {
            Self::Get(_) => FixedCardinalityOperation::Get,
            Self::GetNext(_) => FixedCardinalityOperation::GetNext,
            Self::Set(_) => FixedCardinalityOperation::Set,
        }
    }

    fn len(&self) -> usize {
        match self {
            Self::Get(oids) | Self::GetNext(oids) => oids.len(),
            Self::Set(varbinds) => varbinds.len(),
        }
    }

    fn oid(&self, index: usize) -> &Oid {
        match self {
            Self::Get(oids) | Self::GetNext(oids) => &oids[index],
            Self::Set(varbinds) => &varbinds[index].0,
        }
    }
}

/// Classify one successful response batch. Count mismatches deliberately skip
/// semantic checks because positional correspondence is then ambiguous.
pub(crate) fn classify(
    request: RequestShape<'_>,
    varbinds: Vec<VarBind>,
    request_offset: usize,
    response_offset: usize,
) -> FixedCardinalityResponse {
    let operation = request.operation();
    let expected = request.len();
    let actual = varbinds.len();
    let request_range = request_offset..request_offset + expected;
    let response_range = response_offset..response_offset + actual;
    let mut anomalies = Vec::new();

    if actual != expected {
        anomalies.push(if actual < expected {
            ResponseShapeAnomaly::Truncated {
                request_range,
                response_range,
                expected,
                actual,
            }
        } else {
            ResponseShapeAnomaly::Excess {
                request_range,
                response_range,
                expected,
                actual,
            }
        });
        return FixedCardinalityResponse {
            operation,
            varbinds,
            anomalies,
            metadata: ResponseMetadata::default(),
        };
    }

    match &request {
        RequestShape::Get(_) | RequestShape::Set(_) => {
            if unique_non_identity_permutation(&request, &varbinds) {
                anomalies.push(ResponseShapeAnomaly::Reordered {
                    request_range,
                    response_range,
                });
            } else {
                for (index, vb) in varbinds.iter().enumerate() {
                    let expected_oid = request.oid(index);
                    if vb.oid != *expected_oid {
                        anomalies.push(ResponseShapeAnomaly::OidMismatch {
                            request_index: request_offset + index,
                            response_index: response_offset + index,
                            expected: expected_oid.clone(),
                            actual: vb.oid.clone(),
                        });
                    } else if let RequestShape::Set(values) = &request
                        && vb.value != values[index].1
                    {
                        anomalies.push(ResponseShapeAnomaly::SetValueMismatch {
                            request_index: request_offset + index,
                            response_index: response_offset + index,
                            expected: values[index].1.clone(),
                            actual: vb.value.clone(),
                        });
                    }
                }
            }
        }
        RequestShape::GetNext(cursors) => {
            for (index, (cursor, vb)) in cursors.iter().zip(&varbinds).enumerate() {
                let request_index = request_offset + index;
                let response_index = response_offset + index;
                match vb.value {
                    Value::EndOfMibView if vb.oid != *cursor => {
                        anomalies.push(ResponseShapeAnomaly::GetNextEndOfMibNameMismatch {
                            request_index,
                            response_index,
                            cursor: cursor.clone(),
                            actual: vb.oid.clone(),
                        });
                    }
                    Value::EndOfMibView => {}
                    Value::NoSuchObject | Value::NoSuchInstance => {
                        anomalies.push(ResponseShapeAnomaly::GetNextUnexpectedException {
                            request_index,
                            response_index,
                            value: vb.value.clone(),
                        });
                    }
                    _ if vb.oid <= *cursor => {
                        anomalies.push(ResponseShapeAnomaly::GetNextNotSuccessor {
                            request_index,
                            response_index,
                            cursor: cursor.clone(),
                            actual: vb.oid.clone(),
                        });
                    }
                    _ => {}
                }
            }
        }
    }

    FixedCardinalityResponse {
        operation,
        varbinds,
        anomalies,
        metadata: ResponseMetadata::default(),
    }
}

fn unique_non_identity_permutation(request: &RequestShape<'_>, varbinds: &[VarBind]) -> bool {
    if (0..request.len()).any(|i| (i + 1..request.len()).any(|j| request.oid(i) == request.oid(j)))
        || (0..varbinds.len())
            .any(|i| (i + 1..varbinds.len()).any(|j| varbinds[i].oid == varbinds[j].oid))
    {
        return false;
    }

    let Some(permutation) = varbinds
        .iter()
        .map(|vb| (0..request.len()).find(|&index| request.oid(index) == &vb.oid))
        .collect::<Option<Vec<_>>>()
    else {
        return false;
    };
    permutation
        .iter()
        .enumerate()
        .any(|(index, &mapped)| index != mapped)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn oid(last: u32) -> Oid {
        Oid::from_slice(&[1, 3, 6, 1, last])
    }

    fn fixed_response(
        varbinds: Vec<VarBind>,
        anomalies: Vec<ResponseShapeAnomaly>,
    ) -> FixedCardinalityResponse {
        FixedCardinalityResponse {
            operation: FixedCardinalityOperation::Get,
            varbinds,
            anomalies,
            metadata: ResponseMetadata::default(),
        }
    }

    #[test]
    fn singleton_extractors_return_clean_single_binding() {
        let varbind = VarBind::new(oid(1), Value::Integer(42));
        let response = fixed_response(vec![varbind.clone()], Vec::new());

        assert_eq!(response.single(), Some(&varbind));
        assert_eq!(response.into_single(), Ok(varbind));
    }

    #[test]
    fn singleton_extractors_reject_other_cardinalities_and_preserve_response() {
        let responses = [
            fixed_response(Vec::new(), Vec::new()),
            fixed_response(
                vec![
                    VarBind::new(oid(1), Value::Integer(1)),
                    VarBind::new(oid(2), Value::Integer(2)),
                ],
                Vec::new(),
            ),
        ];

        for response in responses {
            assert_eq!(response.single(), None);
            assert_eq!(response.clone().into_single(), Err(response));
        }
    }

    #[test]
    fn singleton_extractors_reject_every_anomaly_and_preserve_response() {
        let anomalies = [
            ResponseShapeAnomaly::Truncated {
                request_range: 0..2,
                response_range: 0..1,
                expected: 2,
                actual: 1,
            },
            ResponseShapeAnomaly::Excess {
                request_range: 0..0,
                response_range: 0..1,
                expected: 0,
                actual: 1,
            },
            ResponseShapeAnomaly::Reordered {
                request_range: 0..1,
                response_range: 0..1,
            },
            ResponseShapeAnomaly::OidMismatch {
                request_index: 0,
                response_index: 0,
                expected: oid(1),
                actual: oid(2),
            },
            ResponseShapeAnomaly::GetNextNotSuccessor {
                request_index: 0,
                response_index: 0,
                cursor: oid(2),
                actual: oid(1),
            },
            ResponseShapeAnomaly::GetNextEndOfMibNameMismatch {
                request_index: 0,
                response_index: 0,
                cursor: oid(1),
                actual: oid(2),
            },
            ResponseShapeAnomaly::GetNextUnexpectedException {
                request_index: 0,
                response_index: 0,
                value: Value::NoSuchInstance,
            },
            ResponseShapeAnomaly::SetValueMismatch {
                request_index: 0,
                response_index: 0,
                expected: Value::Integer(1),
                actual: Value::Integer(2),
            },
        ];

        for anomaly in anomalies {
            let response = fixed_response(
                vec![VarBind::new(oid(1), Value::Integer(42))],
                vec![anomaly],
            );
            assert_eq!(response.single(), None);
            assert_eq!(response.clone().into_single(), Err(response));
        }
    }

    #[test]
    fn count_mismatch_does_not_infer_positional_semantics() {
        let response = classify(
            RequestShape::Get(&[oid(1), oid(2)]),
            vec![VarBind::null(oid(9))],
            4,
            7,
        );
        assert_eq!(response.varbinds, vec![VarBind::null(oid(9))]);
        assert!(matches!(
            response.anomalies.as_slice(),
            [ResponseShapeAnomaly::Truncated {
                request_range,
                response_range,
                expected: 2,
                actual: 1,
            }] if request_range == &(4..6) && response_range == &(7..8)
        ));
    }

    #[test]
    fn unique_reorder_is_distinguished_from_ambiguous_duplicates() {
        let reordered = classify(
            RequestShape::Get(&[oid(1), oid(2)]),
            vec![VarBind::null(oid(2)), VarBind::null(oid(1))],
            0,
            0,
        );
        assert!(matches!(
            reordered.anomalies[0],
            ResponseShapeAnomaly::Reordered { .. }
        ));

        let ambiguous = classify(
            RequestShape::Get(&[oid(1), oid(1)]),
            vec![VarBind::null(oid(1)), VarBind::null(oid(2))],
            0,
            0,
        );
        assert!(matches!(
            ambiguous.anomalies[0],
            ResponseShapeAnomaly::OidMismatch { .. }
        ));
    }

    #[test]
    fn getnext_exception_and_successor_rules_are_checked() {
        let cursors = [oid(1), oid(2), oid(3), oid(4)];
        let response = classify(
            RequestShape::GetNext(&cursors),
            vec![
                VarBind::new(oid(1), Value::Null),
                VarBind::new(oid(9), Value::EndOfMibView),
                VarBind::new(oid(3), Value::NoSuchInstance),
                VarBind::new(oid(5), Value::Integer(1)),
            ],
            0,
            0,
        );
        assert_eq!(response.anomalies.len(), 3);
        assert!(matches!(
            response.anomalies[0],
            ResponseShapeAnomaly::GetNextNotSuccessor { .. }
        ));
        assert!(matches!(
            response.anomalies[1],
            ResponseShapeAnomaly::GetNextEndOfMibNameMismatch { .. }
        ));
        assert!(matches!(
            response.anomalies[2],
            ResponseShapeAnomaly::GetNextUnexpectedException { .. }
        ));
    }

    #[test]
    fn set_changed_value_is_reported() {
        let requested = [(oid(1), Value::Integer(1))];
        let response = classify(
            RequestShape::Set(&requested),
            vec![VarBind::new(oid(1), Value::Integer(2))],
            0,
            0,
        );
        assert!(matches!(
            response.anomalies[0],
            ResponseShapeAnomaly::SetValueMismatch { .. }
        ));

        let requested = [(oid(1), Value::Integer(1)), (oid(2), Value::Integer(2))];
        let reordered = classify(
            RequestShape::Set(&requested),
            vec![
                VarBind::new(oid(2), Value::Integer(9)),
                VarBind::new(oid(1), Value::Integer(1)),
            ],
            0,
            0,
        );
        // Reordering is observable, but the classifier does not infer a
        // request-to-response mapping for value comparison.
        assert!(matches!(
            reordered.anomalies.as_slice(),
            [ResponseShapeAnomaly::Reordered { .. }]
        ));
    }
}