rusnmp 0.1.0

A lightweight, async SNMP v1/v2c/v3 client library for Rust with minimal dependencies.
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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! BER (Basic Encoding Rules) codec for SNMP PDUs.

use crate::error::{Result, SnmpError};
use crate::types::{Oid, Value, VarBind, Version};

// ASN.1 tag constants
const TAG_INTEGER: u8 = 0x02;
const TAG_OCTET_STRING: u8 = 0x04;
const TAG_NULL: u8 = 0x05;
const TAG_OID: u8 = 0x06;
const TAG_SEQUENCE: u8 = 0x30;

// SNMP application tags
const TAG_IPADDRESS: u8 = 0x40;
const TAG_COUNTER32: u8 = 0x41;
const TAG_GAUGE32: u8 = 0x42;
const TAG_TIMETICKS: u8 = 0x43;
const TAG_OPAQUE: u8 = 0x44;
const TAG_COUNTER64: u8 = 0x46;

// SNMP exception tags (context-specific, primitive)
const TAG_NO_SUCH_OBJECT: u8 = 0x80;
const TAG_NO_SUCH_INSTANCE: u8 = 0x81;
const TAG_END_OF_MIB_VIEW: u8 = 0x82;

// PDU types (context-specific, constructed)
const PDU_GET: u8 = 0xA0;
const PDU_GETNEXT: u8 = 0xA1;
const PDU_RESPONSE: u8 = 0xA2;
const PDU_SET: u8 = 0xA3;
const PDU_GETBULK: u8 = 0xA5;

// --- Encoder ---

pub struct BerEncoder {
    buf: Vec<u8>,
}

impl BerEncoder {
    pub fn new() -> Self {
        Self { buf: Vec::with_capacity(512) }
    }

    pub fn into_bytes(self) -> Vec<u8> {
        self.buf
    }

    pub fn buf_extend(&mut self, data: &[u8]) {
        self.buf.extend_from_slice(data);
    }

    pub fn write_sequence(&mut self, content: &[u8]) {
        self.buf.push(TAG_SEQUENCE);
        self.write_length(content.len());
        self.buf.extend_from_slice(content);
    }

    pub fn write_tagged(&mut self, tag: u8, content: &[u8]) {
        self.buf.push(tag);
        self.write_length(content.len());
        self.buf.extend_from_slice(content);
    }

    pub fn write_integer(&mut self, val: i64) {
        let bytes = encode_integer_bytes(val);
        self.buf.push(TAG_INTEGER);
        self.write_length(bytes.len());
        self.buf.extend_from_slice(&bytes);
    }

    pub fn write_octet_string(&mut self, data: &[u8]) {
        self.buf.push(TAG_OCTET_STRING);
        self.write_length(data.len());
        self.buf.extend_from_slice(data);
    }

    pub fn write_null(&mut self) {
        self.buf.push(TAG_NULL);
        self.buf.push(0x00);
    }

    pub fn write_oid(&mut self, oid: &Oid) {
        let encoded = encode_oid(oid);
        self.buf.push(TAG_OID);
        self.write_length(encoded.len());
        self.buf.extend_from_slice(&encoded);
    }

    fn write_length(&mut self, len: usize) {
        if len < 0x80 {
            self.buf.push(len as u8);
        } else if len <= 0xFF {
            self.buf.push(0x81);
            self.buf.push(len as u8);
        } else if len <= 0xFFFF {
            self.buf.push(0x82);
            self.buf.push((len >> 8) as u8);
            self.buf.push(len as u8);
        } else {
            self.buf.push(0x83);
            self.buf.push((len >> 16) as u8);
            self.buf.push((len >> 8) as u8);
            self.buf.push(len as u8);
        }
    }
}

fn encode_integer_bytes(val: i64) -> Vec<u8> {
    if val == 0 {
        return vec![0x00];
    }
    let mut bytes = val.to_be_bytes().to_vec();
    // Strip leading 0x00 or 0xFF that are redundant
    if val > 0 {
        while bytes.len() > 1 && bytes[0] == 0x00 && bytes[1] & 0x80 == 0 {
            bytes.remove(0);
        }
    } else {
        while bytes.len() > 1 && bytes[0] == 0xFF && bytes[1] & 0x80 != 0 {
            bytes.remove(0);
        }
    }
    bytes
}

fn encode_oid(oid: &Oid) -> Vec<u8> {
    let c = oid.components();
    if c.len() < 2 {
        return vec![];
    }
    let mut out = vec![(c[0] * 40 + c[1]) as u8];
    for &sub in &c[2..] {
        encode_sub_id(&mut out, sub);
    }
    out
}

fn encode_sub_id(out: &mut Vec<u8>, mut val: u32) {
    if val == 0 {
        out.push(0);
        return;
    }
    let mut tmp = Vec::new();
    while val > 0 {
        tmp.push((val & 0x7F) as u8);
        val >>= 7;
    }
    tmp.reverse();
    for (i, b) in tmp.iter().enumerate() {
        if i < tmp.len() - 1 {
            out.push(b | 0x80);
        } else {
            out.push(*b);
        }
    }
}

// --- Decoder ---

pub struct BerDecoder<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> BerDecoder<'a> {
    pub fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    pub fn remaining(&self) -> usize {
        self.data.len() - self.pos
    }

    pub fn peek_tag(&self) -> Result<u8> {
        if self.pos >= self.data.len() {
            return Err(SnmpError::Decode("unexpected end of data".into()));
        }
        Ok(self.data[self.pos])
    }

    pub fn read_tag(&mut self) -> Result<u8> {
        let tag = self.peek_tag()?;
        self.pos += 1;
        Ok(tag)
    }

    pub fn read_length(&mut self) -> Result<usize> {
        if self.pos >= self.data.len() {
            return Err(SnmpError::Decode("unexpected end of data".into()));
        }
        let first = self.data[self.pos];
        self.pos += 1;
        if first < 0x80 {
            Ok(first as usize)
        } else {
            let num_bytes = (first & 0x7F) as usize;
            if self.pos + num_bytes > self.data.len() {
                return Err(SnmpError::Decode("length overflow".into()));
            }
            let mut len: usize = 0;
            for i in 0..num_bytes {
                len = (len << 8) | self.data[self.pos + i] as usize;
            }
            self.pos += num_bytes;
            Ok(len)
        }
    }

    pub fn read_raw(&mut self, len: usize) -> Result<&'a [u8]> {
        if self.pos + len > self.data.len() {
            return Err(SnmpError::Decode("data too short".into()));
        }
        let slice = &self.data[self.pos..self.pos + len];
        self.pos += len;
        Ok(slice)
    }

    pub fn read_tlv(&mut self) -> Result<(u8, &'a [u8])> {
        let tag = self.read_tag()?;
        let len = self.read_length()?;
        let data = self.read_raw(len)?;
        Ok((tag, data))
    }

    /// Read a TLV and return the full encoded bytes (tag + length + value).
    pub fn read_tlv_with_header(&mut self) -> Result<(u8, Vec<u8>)> {
        let start = self.pos;
        let tag = self.read_tag()?;
        let len = self.read_length()?;
        let _ = self.read_raw(len)?;
        let end = self.pos;
        let full = self.data[start..end].to_vec();
        Ok((tag, full))
    }

    pub fn read_sequence(&mut self) -> Result<BerDecoder<'a>> {
        let tag = self.read_tag()?;
        if tag != TAG_SEQUENCE {
            return Err(SnmpError::Decode(format!("expected SEQUENCE, got 0x{:02X}", tag)));
        }
        let len = self.read_length()?;
        let data = self.read_raw(len)?;
        Ok(BerDecoder::new(data))
    }

    pub fn read_integer(&mut self) -> Result<i64> {
        let (tag, data) = self.read_tlv()?;
        if tag != TAG_INTEGER {
            return Err(SnmpError::Decode(format!("expected INTEGER, got 0x{:02X}", tag)));
        }
        Ok(decode_integer(data))
    }

    pub fn read_octet_string(&mut self) -> Result<&'a [u8]> {
        let (tag, data) = self.read_tlv()?;
        if tag != TAG_OCTET_STRING {
            return Err(SnmpError::Decode(format!("expected OCTET STRING, got 0x{:02X}", tag)));
        }
        Ok(data)
    }

    pub fn read_oid(&mut self) -> Result<Oid> {
        let (tag, data) = self.read_tlv()?;
        if tag != TAG_OID {
            return Err(SnmpError::Decode(format!("expected OID, got 0x{:02X}", tag)));
        }
        decode_oid(data)
    }

    pub fn read_value(&mut self) -> Result<Value> {
        let (tag, data) = self.read_tlv()?;
        match tag {
            TAG_INTEGER => Ok(Value::Integer(decode_integer(data))),
            TAG_OCTET_STRING => Ok(Value::OctetString(data.to_vec())),
            TAG_NULL => Ok(Value::Null),
            TAG_OID => Ok(Value::ObjectIdentifier(decode_oid(data)?)),
            TAG_IPADDRESS => {
                if data.len() == 4 {
                    Ok(Value::IpAddress([data[0], data[1], data[2], data[3]]))
                } else {
                    Ok(Value::OctetString(data.to_vec()))
                }
            }
            TAG_COUNTER32 => Ok(Value::Counter32(decode_unsigned32(data))),
            TAG_GAUGE32 => Ok(Value::Gauge32(decode_unsigned32(data))),
            TAG_TIMETICKS => Ok(Value::TimeTicks(decode_unsigned32(data))),
            TAG_OPAQUE => Ok(Value::Opaque(data.to_vec())),
            TAG_COUNTER64 => Ok(Value::Counter64(decode_unsigned64(data))),
            TAG_NO_SUCH_OBJECT => Ok(Value::NoSuchObject),
            TAG_NO_SUCH_INSTANCE => Ok(Value::NoSuchInstance),
            TAG_END_OF_MIB_VIEW => Ok(Value::EndOfMibView),
            _ => Ok(Value::OctetString(data.to_vec())),
        }
    }
}

fn decode_integer(data: &[u8]) -> i64 {
    if data.is_empty() {
        return 0;
    }
    let mut val: i64 = if data[0] & 0x80 != 0 { -1 } else { 0 };
    for &b in data {
        val = (val << 8) | b as i64;
    }
    val
}

fn decode_unsigned32(data: &[u8]) -> u32 {
    let mut val: u32 = 0;
    for &b in data {
        val = (val << 8) | b as u32;
    }
    val
}

fn decode_unsigned64(data: &[u8]) -> u64 {
    let mut val: u64 = 0;
    for &b in data {
        val = (val << 8) | b as u64;
    }
    val
}

fn decode_oid(data: &[u8]) -> Result<Oid> {
    if data.is_empty() {
        return Ok(Oid::from_slice(&[]));
    }
    let first = data[0];
    let mut components = vec![(first / 40) as u32, (first % 40) as u32];
    let mut i = 1;
    while i < data.len() {
        let mut sub: u32 = 0;
        loop {
            if i >= data.len() {
                return Err(SnmpError::Decode("truncated OID sub-identifier".into()));
            }
            let b = data[i];
            i += 1;
            sub = (sub << 7) | (b & 0x7F) as u32;
            if b & 0x80 == 0 {
                break;
            }
        }
        components.push(sub);
    }
    Ok(Oid(components))
}

// --- PDU encoding/decoding ---

/// Encode an SNMPv1/v2c GET request.
pub fn encode_get_request(version: Version, community: &[u8], request_id: i32, oids: &[Oid]) -> Vec<u8> {
    encode_pdu(version, community, request_id, PDU_GET, oids, &[])
}

/// Encode an SNMPv1/v2c GETNEXT request.
pub fn encode_getnext_request(version: Version, community: &[u8], request_id: i32, oids: &[Oid]) -> Vec<u8> {
    encode_pdu(version, community, request_id, PDU_GETNEXT, oids, &[])
}

/// Encode an SNMPv1/v2c GETBULK request.
pub fn encode_getbulk_request(
    version: Version,
    community: &[u8],
    request_id: i32,
    non_repeaters: i32,
    max_repetitions: i32,
    oids: &[Oid],
) -> Vec<u8> {
    let varbind_list = encode_varbind_list_null(oids);

    let mut pdu = BerEncoder::new();
    pdu.write_integer(request_id as i64);
    pdu.write_integer(non_repeaters as i64);
    pdu.write_integer(max_repetitions as i64);
    pdu.write_sequence(&varbind_list);
    let pdu_bytes = pdu.into_bytes();

    let mut msg = BerEncoder::new();
    msg.write_integer(version.to_i64());
    msg.write_octet_string(community);
    msg.write_tagged(PDU_GETBULK, &pdu_bytes);
    let msg_bytes = msg.into_bytes();

    let mut out = BerEncoder::new();
    out.write_sequence(&msg_bytes);
    out.into_bytes()
}

/// Encode an SNMPv1/v2c SET request.
pub fn encode_set_request(version: Version, community: &[u8], request_id: i32, varbinds: &[VarBind]) -> Vec<u8> {
    let varbind_list = encode_varbind_list(varbinds);

    let mut pdu = BerEncoder::new();
    pdu.write_integer(request_id as i64);
    pdu.write_integer(0); // error-status
    pdu.write_integer(0); // error-index
    pdu.write_sequence(&varbind_list);
    let pdu_bytes = pdu.into_bytes();

    let mut msg = BerEncoder::new();
    msg.write_integer(version.to_i64());
    msg.write_octet_string(community);
    msg.write_tagged(PDU_SET, &pdu_bytes);
    let msg_bytes = msg.into_bytes();

    let mut out = BerEncoder::new();
    out.write_sequence(&msg_bytes);
    out.into_bytes()
}

fn encode_pdu(version: Version, community: &[u8], request_id: i32, pdu_type: u8, oids: &[Oid], _varbinds: &[VarBind]) -> Vec<u8> {
    let varbind_list = encode_varbind_list_null(oids);

    let mut pdu = BerEncoder::new();
    pdu.write_integer(request_id as i64);
    pdu.write_integer(0); // error-status
    pdu.write_integer(0); // error-index
    pdu.write_sequence(&varbind_list);
    let pdu_bytes = pdu.into_bytes();

    let mut msg = BerEncoder::new();
    msg.write_integer(version.to_i64());
    msg.write_octet_string(community);
    msg.write_tagged(pdu_type, &pdu_bytes);
    let msg_bytes = msg.into_bytes();

    let mut out = BerEncoder::new();
    out.write_sequence(&msg_bytes);
    out.into_bytes()
}

fn encode_varbind_list_null(oids: &[Oid]) -> Vec<u8> {
    let mut inner = BerEncoder::new();
    for oid in oids {
        let mut vb = BerEncoder::new();
        vb.write_oid(oid);
        vb.write_null();
        let vb_bytes = vb.into_bytes();
        inner.write_sequence(&vb_bytes);
    }
    inner.into_bytes()
}

fn encode_varbind_list(varbinds: &[VarBind]) -> Vec<u8> {
    let mut inner = BerEncoder::new();
    for vb in varbinds {
        let mut enc = BerEncoder::new();
        enc.write_oid(&vb.oid);
        encode_value(&mut enc, &vb.value);
        let vb_bytes = enc.into_bytes();
        inner.write_sequence(&vb_bytes);
    }
    inner.into_bytes()
}

fn encode_value(enc: &mut BerEncoder, value: &Value) {
    match value {
        Value::Integer(v) => enc.write_integer(*v),
        Value::OctetString(b) => enc.write_octet_string(b),
        Value::Null => enc.write_null(),
        Value::ObjectIdentifier(oid) => enc.write_oid(oid),
        Value::IpAddress(ip) => enc.write_tagged(TAG_IPADDRESS, ip),
        Value::Counter32(v) => enc.write_tagged(TAG_COUNTER32, &encode_unsigned32(*v)),
        Value::Gauge32(v) => enc.write_tagged(TAG_GAUGE32, &encode_unsigned32(*v)),
        Value::TimeTicks(v) => enc.write_tagged(TAG_TIMETICKS, &encode_unsigned32(*v)),
        Value::Opaque(b) => enc.write_tagged(TAG_OPAQUE, b),
        Value::Counter64(v) => enc.write_tagged(TAG_COUNTER64, &encode_unsigned64(*v)),
        _ => enc.write_null(),
    }
}

fn encode_unsigned32(val: u32) -> Vec<u8> {
    encode_integer_bytes(val as i64)
}

fn encode_unsigned64(val: u64) -> Vec<u8> {
    if val == 0 {
        return vec![0x00];
    }
    let bytes = val.to_be_bytes();
    let start = bytes.iter().position(|&b| b != 0).unwrap_or(7);
    // Add leading zero if high bit set (to keep it positive)
    if bytes[start] & 0x80 != 0 {
        let mut out = vec![0x00];
        out.extend_from_slice(&bytes[start..]);
        out
    } else {
        bytes[start..].to_vec()
    }
}

/// Decoded SNMP response.
#[derive(Debug)]
pub struct SnmpResponse {
    pub version: Version,
    pub community: Vec<u8>,
    pub request_id: i32,
    pub error_status: u32,
    pub error_index: u32,
    pub varbinds: Vec<VarBind>,
}

/// Decode an SNMPv1/v2c response message.
pub fn decode_response(data: &[u8]) -> Result<SnmpResponse> {
    let mut dec = BerDecoder::new(data);
    let mut msg = dec.read_sequence()?;

    let ver = msg.read_integer()?;
    let version = match ver {
        0 => Version::V1,
        1 => Version::V2c,
        3 => Version::V3,
        _ => return Err(SnmpError::Decode(format!("unknown version: {}", ver))),
    };

    let community = msg.read_octet_string()?.to_vec();

    // PDU (context-specific constructed)
    let pdu_tag = msg.read_tag()?;
    if pdu_tag != PDU_RESPONSE {
        return Err(SnmpError::Decode(format!("expected Response PDU (0xA2), got 0x{:02X}", pdu_tag)));
    }
    let pdu_len = msg.read_length()?;
    let pdu_data = msg.read_raw(pdu_len)?;
    let mut pdu = BerDecoder::new(pdu_data);

    let request_id = pdu.read_integer()? as i32;
    let error_status = pdu.read_integer()? as u32;
    let error_index = pdu.read_integer()? as u32;

    // VarBind list
    let mut vbl = pdu.read_sequence()?;
    let mut varbinds = Vec::new();
    while vbl.remaining() > 0 {
        let mut vb_dec = vbl.read_sequence()?;
        let oid = vb_dec.read_oid()?;
        let value = vb_dec.read_value()?;
        varbinds.push(VarBind { oid, value });
    }

    Ok(SnmpResponse {
        version,
        community,
        request_id,
        error_status,
        error_index,
        varbinds,
    })
}

/// Public wrapper for encoding a Value (used by v3_client).
pub fn encode_value_pub(enc: &mut BerEncoder, value: &Value) {
    encode_value(enc, value);
}