add new method to UpdateMessage

This commit is contained in:
Masato Imai
2025-02-25 16:43:00 +00:00
parent 9dba98cfe0
commit 297f3075e3
3 changed files with 75 additions and 2 deletions

View File

@ -7,7 +7,7 @@ use crate::path_attribute::{AsPath, Origin, PathAttribute};
use crate::routing::Ipv4Network;
use bytes::BytesMut;
use super::header::Header;
use super::header::{Header, MessageType};
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub struct UpdateMessage {
@ -25,7 +25,37 @@ impl UpdateMessage {
network_layer_reachability_information: Vec<Ipv4Network>,
withdrawn_routes: Vec<Ipv4Network>,
) -> Self {
todo!();
let path_attributes_length =
path_attributes.iter().map(|p| p.bytes_len()).sum::<usize>() as u16;
let network_layer_reachability_information_length = network_layer_reachability_information
.iter()
.map(|r| r.bytes_len())
.sum::<usize>() as u16;
let withdrawn_routes_length = withdrawn_routes
.iter()
.map(|w| w.bytes_len())
.sum::<usize>() as u16;
let header_minimum_length = 19u16;
let header = Header::new(
header_minimum_length
+ path_attributes_length
+ network_layer_reachability_information_length
+ withdrawn_routes_length
+ 4,
MessageType::Update,
);
Self {
header,
withdrawn_routes,
withdrawn_routes_length,
path_attributes,
path_attributes_length,
network_layer_reachability_information,
}
}
}

View File

@ -23,3 +23,33 @@ pub enum AsPath {
AsSequence(Vec<AutonomousSystemNumber>),
AsSet(BTreeSet<AutonomousSystemNumber>),
}
impl PathAttribute {
pub fn bytes_len(&self) -> usize {
let path_attribute_value_length = match self {
PathAttribute::Origin(_) => 1,
PathAttribute::AsPath(a) => a.bytes_len(),
PathAttribute::NextHop(_) => 4,
PathAttribute::DontKnow(v) => v.len(),
};
let length = path_attribute_value_length + 2;
if path_attribute_value_length > 255 {
length + 2
} else {
length + 1
}
}
}
impl AsPath {
fn bytes_len(&self) -> usize {
let as_bytes_length = match self {
AsPath::AsSequence(as_sequence) => 2 * as_sequence.len(),
AsPath::AsSet(as_set) => 2 * as_set.len(),
};
2 + as_bytes_length
}
}

View File

@ -49,6 +49,19 @@ impl FromStr for Ipv4Network {
}
}
impl Ipv4Network {
pub fn bytes_len(&self) -> usize {
match self.prefix() {
0 => 1,
1..=8 => 2,
9..=16 => 3,
17..=24 => 4,
25..=32 => 5,
_ => panic!("Invalid prefix length: {:?}", self.prefix()),
}
}
}
impl LocRib {
pub async fn new(config: &Config) -> Result<Self> {
todo!();