add UpdateMsg

This commit is contained in:
Masato Imai
2025-02-25 15:30:20 +00:00
parent e3b56ae1a0
commit 9dba98cfe0
8 changed files with 116 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use crate::packets::{keepalive::KeepaliveMessage, open::OpenMessage};
use crate::packets::{keepalive::KeepaliveMessage, open::OpenMessage, update::UpdateMessage};
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub enum Event {
@ -6,5 +6,6 @@ pub enum Event {
TcpConnectionConfirmed,
BgpOpen(OpenMessage),
KeepaliveMsg(KeepaliveMessage),
UpdateMsg(UpdateMessage),
Established,
}

View File

@ -7,6 +7,7 @@ mod error;
mod event;
mod event_queue;
mod packets;
mod path_attribute;
pub mod peer;
mod routing;
mod state;

View File

@ -45,6 +45,7 @@ impl From<Header> for BytesMut {
pub enum MessageType {
Open,
Keepalive,
Update,
}
impl TryFrom<u8> for MessageType {
@ -53,6 +54,7 @@ impl TryFrom<u8> for MessageType {
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
1 => Ok(Self::Open),
2 => Ok(Self::Update),
4 => Ok(Self::Keepalive),
_ => Err(Self::Error::from(anyhow::anyhow!(
"Invalid message type: {}",
@ -66,6 +68,7 @@ impl From<MessageType> for u8 {
fn from(value: MessageType) -> Self {
match value {
MessageType::Open => 1,
MessageType::Update => 2,
MessageType::Keepalive => 4,
}
}

View File

@ -8,12 +8,14 @@ use super::{
header::{Header, MessageType},
keepalive::KeepaliveMessage,
open::OpenMessage,
update::UpdateMessage,
};
#[derive(Debug)]
pub enum Message {
Open(OpenMessage),
Keepalive(KeepaliveMessage),
Update(UpdateMessage),
}
impl TryFrom<BytesMut> for Message {
@ -40,6 +42,10 @@ impl TryFrom<BytesMut> for Message {
let keepalive_message = KeepaliveMessage::try_from(bytes)?;
Ok(Self::Keepalive(keepalive_message))
}
MessageType::Update => {
let update_message = UpdateMessage::try_from(bytes)?;
Ok(Self::Update(update_message))
}
}
}
}
@ -49,6 +55,7 @@ impl From<Message> for BytesMut {
match message {
Message::Open(open) => open.into(),
Message::Keepalive(keepalive) => keepalive.into(),
Message::Update(update) => update.into(),
}
}
}

View File

@ -2,3 +2,4 @@ pub mod header;
pub mod keepalive;
pub mod message;
pub mod open;
pub mod update;

74
src/packets/update.rs Normal file
View File

@ -0,0 +1,74 @@
use std::net::Ipv4Addr;
use std::sync::Arc;
use crate::bgp_type::AutonomousSystemNumber;
use crate::error::ConvertBytesToBgpMessageError;
use crate::path_attribute::{AsPath, Origin, PathAttribute};
use crate::routing::Ipv4Network;
use bytes::BytesMut;
use super::header::Header;
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub struct UpdateMessage {
header: Header,
pub withdrawn_routes: Vec<Ipv4Network>,
withdrawn_routes_length: u16, // octets
pub path_attributes: Arc<Vec<PathAttribute>>,
path_attributes_length: u16, // octets
pub network_layer_reachability_information: Vec<Ipv4Network>,
}
impl UpdateMessage {
pub fn new(
path_attributes: Arc<Vec<PathAttribute>>,
network_layer_reachability_information: Vec<Ipv4Network>,
withdrawn_routes: Vec<Ipv4Network>,
) -> Self {
todo!();
}
}
impl From<UpdateMessage> for BytesMut {
fn from(update: UpdateMessage) -> Self {
todo!();
}
}
impl TryFrom<BytesMut> for UpdateMessage {
type Error = ConvertBytesToBgpMessageError;
fn try_from(bytes: BytesMut) -> Result<Self, Self::Error> {
todo!();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn convert_bytes_to_update_message_and_update_message_to_bytes() {
let some_as: AutonomousSystemNumber = 64513.into();
let some_ip: Ipv4Addr = "10.0.100.3".parse().unwrap();
let local_as: AutonomousSystemNumber = 64514.into();
let local_ip: Ipv4Addr = "10.200.100.3".parse().unwrap();
let update_message_path_attributes = Arc::new(vec![
PathAttribute::Origin(Origin::Igp),
PathAttribute::AsPath(AsPath::AsSequence(vec![some_as, local_as])),
PathAttribute::NextHop(local_ip),
]);
let update_message = UpdateMessage::new(
update_message_path_attributes,
vec!["10.100.220.0.24".parse().unwrap()],
vec![],
);
let update_message_bytes: BytesMut = update_message.clone().into();
let update_message2: UpdateMessage = update_message_bytes.try_into().unwrap();
assert_eq!(update_message, update_message2);
}
}

25
src/path_attribute.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::bgp_type::AutonomousSystemNumber;
use std::collections::BTreeSet;
use std::net::Ipv4Addr;
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum PathAttribute {
Origin(Origin),
AsPath(AsPath),
NextHop(Ipv4Addr),
DontKnow(Vec<u8>),
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum Origin {
Igp,
Egp,
Incomplete,
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum AsPath {
AsSequence(Vec<AutonomousSystemNumber>),
AsSet(BTreeSet<AutonomousSystemNumber>),
}

View File

@ -53,6 +53,9 @@ impl Peer {
Message::Keepalive(keepalive) => {
self.event_queue.enqueue(Event::KeepaliveMsg(keepalive));
}
Message::Update(update) => {
self.event_queue.enqueue(Event::UpdateMsg(update));
}
}
}