bgpkit_parser/parser/mrt/messages/
mod.rs

1use crate::models::{AsnLength, Bgp4MpEnum, Bgp4MpType, MrtMessage, TableDumpV2Message};
2use bytes::Bytes;
3
4pub(crate) mod bgp4mp;
5pub(crate) mod table_dump;
6pub(crate) mod table_dump_v2;
7
8impl MrtMessage {
9    pub fn encode(&self, sub_type: u16) -> Bytes {
10        let msg_bytes: Bytes = match self {
11            MrtMessage::TableDumpMessage(m) => m.encode(),
12            MrtMessage::TableDumpV2Message(m) => match m {
13                TableDumpV2Message::PeerIndexTable(p) => p.encode(),
14                TableDumpV2Message::RibAfi(r) => r.encode(),
15                TableDumpV2Message::RibGeneric(_) => {
16                    todo!("RibGeneric message is not supported yet");
17                }
18            },
19            MrtMessage::Bgp4Mp(m) => {
20                let msg_type = Bgp4MpType::try_from(sub_type).unwrap();
21
22                match m {
23                    Bgp4MpEnum::StateChange(msg) => {
24                        let asn_len = match matches!(msg_type, Bgp4MpType::StateChangeAs4) {
25                            true => AsnLength::Bits32,
26                            false => AsnLength::Bits16,
27                        };
28                        msg.encode(asn_len)
29                    }
30                    Bgp4MpEnum::Message(msg) => {
31                        let add_path = matches!(
32                            msg_type,
33                            Bgp4MpType::MessageAddpath
34                                | Bgp4MpType::MessageAs4Addpath
35                                | Bgp4MpType::MessageLocalAddpath
36                                | Bgp4MpType::MessageLocalAs4Addpath
37                        );
38                        let asn_len = match matches!(
39                            msg_type,
40                            Bgp4MpType::MessageAs4
41                                | Bgp4MpType::MessageAs4Addpath
42                                | Bgp4MpType::MessageLocalAs4Addpath
43                                | Bgp4MpType::MessageAs4Local
44                        ) {
45                            true => AsnLength::Bits32,
46                            false => AsnLength::Bits16,
47                        };
48                        msg.encode(add_path, asn_len)
49                    }
50                }
51            }
52        };
53
54        msg_bytes
55    }
56}