async-snmp 0.18.1

Modern async-first SNMP client library for Rust
Documentation
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
//! BER encoding.
//!
//! Uses a reverse buffer approach: writes from end backwards to avoid
//! needing to pre-calculate lengths.

use super::length::encode_length;
use super::tag;
use crate::error::Result;
use bytes::Bytes;

/// Buffer for BER encoding that writes backwards.
///
/// This approach avoids needing to pre-calculate content lengths:
/// we write the content first, then prepend the length and tag.
pub struct EncodeBuf {
    buf: Vec<u8>,
}

impl EncodeBuf {
    /// Create an encoding buffer with the default capacity.
    #[must_use]
    pub fn new() -> Self {
        Self::with_capacity(512)
    }

    /// Create an encoding buffer with the specified capacity.
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            buf: Vec::with_capacity(capacity),
        }
    }

    /// Push a single byte (prepends to front).
    pub fn push_byte(&mut self, byte: u8) {
        self.buf.push(byte);
    }

    /// Push multiple bytes (prepends to front, reversed).
    pub fn push_bytes(&mut self, bytes: &[u8]) {
        self.buf.extend(bytes.iter().rev());
    }

    /// Push a BER length encoding.
    ///
    /// The buffer is not modified when `len` is outside the supported BER
    /// length domain.
    pub fn push_length(&mut self, len: usize) -> Result<()> {
        let (bytes, count) = encode_length(len)?;
        self.push_encoded_length(&bytes, count);
        Ok(())
    }

    fn push_encoded_length(&mut self, bytes: &[u8; 5], count: usize) {
        // `encode_length` returns bytes in reverse order for prepending.
        self.buf.extend_from_slice(&bytes[..count]);
    }

    /// Push a BER tag.
    pub fn push_tag(&mut self, tag: u8) {
        self.buf.push(tag);
    }

    /// Returns the current length of the encoded data.
    #[must_use]
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// Check if buffer is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// Encode a constructed type (SEQUENCE, PDU, etc).
    ///
    /// Calls the closure to encode contents, then wraps them with a length and
    /// tag. If nested encoding or length representation fails, the buffer is
    /// restored to its entry length.
    pub fn push_constructed<F>(&mut self, tag: u8, f: F) -> Result<()>
    where
        F: FnOnce(&mut Self) -> Result<()>,
    {
        let start_len = self.len();
        if let Err(error) = f(self) {
            self.buf.truncate(start_len);
            return Err(error);
        }
        let content_len = self.len() - start_len;
        self.finish_constructed(tag, start_len, content_len)
    }

    fn finish_constructed(&mut self, tag: u8, start_len: usize, content_len: usize) -> Result<()> {
        if let Err(error) = self.push_length(content_len) {
            self.buf.truncate(start_len);
            return Err(error);
        }
        self.push_tag(tag);
        Ok(())
    }

    /// Encode a SEQUENCE, rolling back if nested encoding fails.
    pub fn push_sequence<F>(&mut self, f: F) -> Result<()>
    where
        F: FnOnce(&mut Self) -> Result<()>,
    {
        self.push_constructed(tag::universal::SEQUENCE, f)
    }

    /// Encode an INTEGER.
    pub fn push_integer(&mut self, value: i32) {
        let (arr, len) = encode_integer_stack(value);
        // Valid bytes are at the end of the array
        self.push_bytes(&arr[4 - len..]);
        self.push_length(len)
            .expect("i32 BER content length is representable");
        self.push_tag(tag::universal::INTEGER);
    }

    /// Encode a 64-bit integer (for Counter64).
    pub fn push_integer64(&mut self, value: u64) {
        let (arr, len) = encode_integer64_stack(value);
        // Valid bytes are at the end of the array
        self.push_bytes(&arr[9 - len..]);
        self.push_length(len)
            .expect("u64 BER content length is representable");
        self.push_tag(tag::application::COUNTER64);
    }

    /// Encode an unsigned 32-bit integer with a specific tag.
    pub fn push_unsigned32(&mut self, tag: u8, value: u32) {
        let (arr, len) = encode_unsigned32_stack(value);
        // Valid bytes are at the end of the array
        self.push_bytes(&arr[5 - len..]);
        self.push_length(len)
            .expect("u32 BER content length is representable");
        self.push_tag(tag);
    }

    /// Encode an OCTET STRING after checking its length.
    ///
    /// Length representability is checked before the buffer is modified.
    pub fn push_octet_string(&mut self, data: &[u8]) -> Result<()> {
        self.push_octet_string_with_len(data, data.len())
    }

    fn push_octet_string_with_len(&mut self, data: &[u8], len: usize) -> Result<()> {
        let (bytes, count) = encode_length(len)?;
        debug_assert_eq!(data.len(), len);
        self.push_bytes(data);
        self.push_encoded_length(&bytes, count);
        self.push_tag(tag::universal::OCTET_STRING);
        Ok(())
    }

    /// Encode a NULL.
    pub fn push_null(&mut self) {
        self.push_length(0)
            .expect("zero BER content length is representable");
        self.push_tag(tag::universal::NULL);
    }

    /// Encode a wire-valid OBJECT IDENTIFIER.
    ///
    /// Validation occurs before the buffer is modified.
    pub fn push_oid(&mut self, oid: &crate::oid::Oid) -> Result<()> {
        oid.validate_for_wire()?;
        let ber = oid.to_ber_smallvec();
        self.push_bytes(&ber);
        self.push_length(ber.len())?;
        self.push_tag(tag::universal::OBJECT_IDENTIFIER);
        Ok(())
    }

    /// Encode an IP address.
    pub fn push_ip_address(&mut self, addr: [u8; 4]) {
        self.push_bytes(&addr);
        self.push_length(4)
            .expect("IPv4 BER content length is representable");
        self.push_tag(tag::application::IP_ADDRESS);
    }

    /// Finalize and return the encoded bytes.
    ///
    /// The buffer is reversed to produce the correct order.
    #[must_use]
    pub fn finish(mut self) -> Bytes {
        self.buf.reverse();
        Bytes::from(self.buf)
    }

    /// Finalize and return as `Vec<u8>`.
    #[must_use]
    pub fn finish_vec(mut self) -> Vec<u8> {
        self.buf.reverse();
        self.buf
    }
}

impl Default for EncodeBuf {
    fn default() -> Self {
        Self::new()
    }
}

/// Encode a signed 32-bit integer in minimal BER form.
///
/// Returns a stack-allocated array and the number of valid bytes.
/// The valid bytes are at the END of the array (for reverse-buffer compatibility).
#[inline]
pub(super) fn encode_integer_stack(value: i32) -> ([u8; 4], usize) {
    let bytes = value.to_be_bytes();

    // Find first significant byte
    let mut start = 0;
    if value >= 0 {
        // For positive/zero, skip leading 0x00 bytes (but keep one if needed for sign)
        while start < 3 && bytes[start] == 0 && bytes[start + 1] & 0x80 == 0 {
            start += 1;
        }
    } else {
        // For negative, skip leading 0xFF bytes (but keep one if needed for sign)
        while start < 3 && bytes[start] == 0xFF && bytes[start + 1] & 0x80 != 0 {
            start += 1;
        }
    }

    (bytes, 4 - start)
}

/// Encode an unsigned 32-bit integer.
///
/// Returns a stack-allocated array and the number of valid bytes.
/// The valid bytes are at the END of the array (for reverse-buffer compatibility).
#[inline]
fn encode_unsigned32_stack(value: u32) -> ([u8; 5], usize) {
    if value == 0 {
        return ([0, 0, 0, 0, 0], 1);
    }

    let bytes = value.to_be_bytes();
    let mut start = 0;

    // Skip leading zeros, but add a 0x00 prefix if MSB is set (to avoid sign extension)
    while start < 3 && bytes[start] == 0 {
        start += 1;
    }

    if bytes[start] & 0x80 != 0 {
        // Need to add a leading 0x00 to indicate positive
        let mut result = [0u8; 5];
        result[1..].copy_from_slice(&bytes);
        (result, 5 - start)
    } else {
        let mut result = [0u8; 5];
        result[1..].copy_from_slice(&bytes);
        (result, 4 - start)
    }
}

/// Encode an unsigned 64-bit integer.
///
/// Returns a stack-allocated array and the number of valid bytes.
/// The valid bytes are at the END of the array (for reverse-buffer compatibility).
#[inline]
fn encode_integer64_stack(value: u64) -> ([u8; 9], usize) {
    if value == 0 {
        return ([0; 9], 1);
    }

    let bytes = value.to_be_bytes();
    let mut start = 0;

    // Skip leading zeros, but add a 0x00 prefix if MSB is set
    while start < 7 && bytes[start] == 0 {
        start += 1;
    }

    if bytes[start] & 0x80 != 0 {
        // Need to add a leading 0x00 to indicate positive
        let mut result = [0u8; 9];
        result[1..].copy_from_slice(&bytes);
        (result, 9 - start)
    } else {
        let mut result = [0u8; 9];
        result[1..].copy_from_slice(&bytes);
        (result, 8 - start)
    }
}

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

    /// Helper to extract the valid bytes from stack-based integer encoding
    fn encode_integer(value: i32) -> Vec<u8> {
        let (arr, len) = encode_integer_stack(value);
        arr[4 - len..].to_vec()
    }

    /// Helper to extract the valid bytes from stack-based unsigned32 encoding
    fn encode_unsigned32(value: u32) -> Vec<u8> {
        let (arr, len) = encode_unsigned32_stack(value);
        arr[5 - len..].to_vec()
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn oversized_length_does_not_modify_buffer() {
        let mut buf = EncodeBuf::new();
        buf.push_byte(0xaa);
        let before = buf.len();

        assert!(buf.push_length(u32::MAX as usize + 1).is_err());
        assert_eq!(buf.len(), before);
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn oversized_octet_string_does_not_modify_buffer() {
        let mut buf = EncodeBuf::new();
        buf.push_byte(0xaa);
        let before = buf.len();

        assert!(
            buf.push_octet_string_with_len(&[0xbb], u32::MAX as usize + 1)
                .is_err()
        );
        assert_eq!(buf.len(), before);
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn oversized_constructed_length_rolls_back() {
        let mut buf = EncodeBuf::new();
        buf.push_integer(7);
        let start_len = buf.len();
        buf.push_null();

        assert!(
            buf.finish_constructed(tag::universal::SEQUENCE, start_len, u32::MAX as usize + 1)
                .is_err()
        );
        assert_eq!(buf.len(), start_len);
    }

    #[test]
    fn test_encode_integer() {
        assert_eq!(encode_integer(0), vec![0]);
        assert_eq!(encode_integer(1), vec![1]);
        assert_eq!(encode_integer(127), vec![127]);
        assert_eq!(encode_integer(128), vec![0, 128]);
        assert_eq!(encode_integer(-1), vec![0xFF]);
        assert_eq!(encode_integer(-128), vec![0x80]);
        assert_eq!(encode_integer(-129), vec![0xFF, 0x7F]);
    }

    #[test]
    fn test_encode_unsigned32() {
        assert_eq!(encode_unsigned32(0), vec![0]);
        assert_eq!(encode_unsigned32(127), vec![127]);
        assert_eq!(encode_unsigned32(128), vec![0, 128]);
        assert_eq!(encode_unsigned32(255), vec![0, 255]);
        assert_eq!(encode_unsigned32(256), vec![1, 0]);
    }

    #[test]
    fn test_encode_null() {
        let mut buf = EncodeBuf::new();
        buf.push_null();
        let bytes = buf.finish();
        assert_eq!(&bytes[..], &[0x05, 0x00]);
    }

    #[test]
    fn test_encode_integer_value() {
        let mut buf = EncodeBuf::new();
        buf.push_integer(42);
        let bytes = buf.finish();
        assert_eq!(&bytes[..], &[0x02, 0x01, 0x2A]);
    }

    #[test]
    fn test_encode_sequence() {
        let mut buf = EncodeBuf::new();
        buf.push_sequence(|buf| {
            // Reverse buffer: push in reverse order for forward output
            buf.push_integer(2);
            buf.push_integer(1);
            Ok(())
        })
        .unwrap();
        let bytes = buf.finish();
        // SEQUENCE { INTEGER 1, INTEGER 2 }
        assert_eq!(
            &bytes[..],
            &[0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02]
        );
    }

    #[test]
    fn push_oid_rejects_invalid_oids() {
        let invalid = [
            crate::oid::Oid::empty(),
            crate::oid::Oid::from_slice(&[1]),
            crate::oid::Oid::new(std::iter::repeat_n(1, crate::oid::MAX_OID_LEN + 1)),
            crate::oid::Oid::from_slice(&[3, 0]),
            crate::oid::Oid::from_slice(&[0, 40]),
            crate::oid::Oid::from_slice(&[1, 40]),
        ];

        for oid in invalid {
            let mut buf = EncodeBuf::new();
            assert!(matches!(
                &*buf.push_oid(&oid).unwrap_err(),
                crate::Error::InvalidOid(_)
            ));
            assert!(buf.is_empty());
        }
    }

    #[test]
    fn fallible_constructed_encoding_rolls_back() {
        let mut buf = EncodeBuf::new();
        buf.push_integer(7);
        let original_len = buf.len();
        let result = buf.push_sequence(|buf| {
            buf.push_null();
            buf.push_oid(&crate::oid::Oid::empty())
        });
        assert!(result.is_err());
        assert_eq!(buf.len(), original_len);
    }
}