mrt_rs/records/rip.rs
1use byteorder::{BigEndian, ReadBytesExt};
2use std::io::{Error, Read};
3use std::net::{Ipv4Addr, Ipv6Addr};
4
5use crate::Header;
6use crate::AFI;
7
8/// The RIP struct represents the data contained in an MRT record type of RIP.
9#[derive(Debug)]
10pub struct RIP {
11 /// The IPv4 address of the router from which this message was received.
12 pub remote: Ipv4Addr,
13
14 /// The IPv4 address of the interface at which this message was received.
15 pub local: Ipv4Addr,
16
17 /// The message that has been received.
18 pub message: Vec<u8>,
19}
20
21impl RIP {
22 ///
23 /// # Summary
24 /// Used to parse RIP MRT records.
25 ///
26 /// # Panics
27 /// This function does not panic.
28 ///
29 /// # Errors
30 /// Any IO error will be returned while reading from the stream.
31 /// If an ill-formatted stream or header is provided behavior will be undefined.
32 ///
33 /// # Safety
34 /// This function does not make use of unsafe code.
35 ///
36 pub fn parse(header: &Header, mut stream: impl Read) -> Result<RIP, Error> {
37 // The fixed size of the header consisting of two IPv4 addresses.
38 let length = (header.length - 2 * AFI::IPV4.size()) as usize;
39 let mut record = RIP {
40 remote: Ipv4Addr::from(stream.read_u32::<BigEndian>()?),
41 local: Ipv4Addr::from(stream.read_u32::<BigEndian>()?),
42 message: vec![0; length as usize],
43 };
44
45 // Fill the entire buffer.
46 stream.read_exact(&mut record.message)?;
47 Ok(record)
48 }
49}
50
51/// The RIP struct represents the data contained in an MRT record type of RIP.
52#[derive(Debug)]
53pub struct RIPNG {
54 /// The IPv6 address of the router from which this message was received.
55 pub remote: Ipv6Addr,
56
57 /// The IPv6 address of the interface at which this message was received.
58 pub local: Ipv6Addr,
59
60 /// The message that has been received.
61 pub message: Vec<u8>,
62}
63
64impl RIPNG {
65 ///
66 /// # Summary
67 /// Used to parse RIPNG MRT records.
68 ///
69 /// # Panics
70 /// This function does not panic.
71 ///
72 /// # Errors
73 /// Any IO error will be returned while reading from the stream.
74 /// If an ill-formatted stream or header is provided behavior will be undefined.
75 ///
76 /// # Safety
77 /// This function does not make use of unsafe code.
78 ///
79 pub fn parse(header: &Header, mut stream: impl Read) -> Result<RIPNG, Error> {
80 // The fixed size of the header consisting of two IPv4 addresses.
81 let length = (header.length - 2 * AFI::IPV6.size()) as usize;
82 let mut record = RIPNG {
83 remote: Ipv6Addr::from(stream.read_u128::<BigEndian>()?),
84 local: Ipv6Addr::from(stream.read_u128::<BigEndian>()?),
85 message: vec![0; length as usize],
86 };
87
88 // Fill the entire buffer.
89 stream.read_exact(&mut record.message)?;
90 Ok(record)
91 }
92}