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
use crate::parser::bmp::error::ParserBmpError;
use crate::parser::ReadUtils;
use num_traits::FromPrimitive;
use std::io::Cursor;

#[derive(Debug)]
pub struct InitiationMessage {
    pub tlvs: Vec<InitiationTlv>,
}

#[derive(Debug)]
pub struct InitiationTlv {
    pub info_type: InitiationTlvType,
    pub info_len: u16,
    pub info: String,
}

///Type-Length-Value Type
///
/// For more, see: https://datatracker.ietf.org/doc/html/rfc1213
#[derive(Debug, Primitive)]
pub enum InitiationTlvType {
    String = 0,
    SysDescr = 1,
    SysName = 2,
}

pub fn parse_initiation_message(
    reader: &mut Cursor<&[u8]>,
) -> Result<InitiationMessage, ParserBmpError> {
    let mut tlvs = vec![];
    let total = reader.get_ref().len() as u64;

    while total - reader.position() > 4 {
        let info_type: InitiationTlvType = InitiationTlvType::from_u16(reader.read_16b()?).unwrap();
        let info_len = reader.read_16b()?;
        if total - reader.position() < info_len as u64 {
            // not enough bytes to read
            break;
        }
        let info = reader.read_n_bytes_to_string(info_len as usize)?;
        tlvs.push(InitiationTlv {
            info_type,
            info_len,
            info,
        });
    }

    Ok(InitiationMessage { tlvs })
}