From c701030a6831b669067a9aab7dd7bc5d7b8c0e0e Mon Sep 17 00:00:00 2001 From: weqe Date: Wed, 19 Jun 2024 20:41:42 +0300 Subject: [PATCH] Change Packet naming. --- holo-vrrp/src/debug.rs | 6 +++--- holo-vrrp/src/events.rs | 4 ++-- holo-vrrp/src/packet.rs | 48 +++++++++++++++++++++++++++++++++++------ holo-vrrp/src/tasks.rs | 6 +++--- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/holo-vrrp/src/debug.rs b/holo-vrrp/src/debug.rs index 95312777..b5705575 100644 --- a/holo-vrrp/src/debug.rs +++ b/holo-vrrp/src/debug.rs @@ -8,7 +8,7 @@ use std::net::IpAddr; use tracing::{debug, debug_span}; -use crate::packet::Packet; +use crate::packet::VRRPPacket; // VRRP debug messages. #[derive(Debug)] @@ -18,8 +18,8 @@ pub enum Debug<'a> { InstanceStart, InstanceStop(InstanceInactiveReason), // Network - PacketRx(&'a IpAddr, &'a Packet), - PacketTx(&'a IpAddr, &'a Packet), + PacketRx(&'a IpAddr, &'a VRRPPacket), + PacketTx(&'a IpAddr, &'a VRRPPacket), } // Reason why an VRRP instance is inactive. diff --git a/holo-vrrp/src/events.rs b/holo-vrrp/src/events.rs index 3508c073..9c41cf4b 100644 --- a/holo-vrrp/src/events.rs +++ b/holo-vrrp/src/events.rs @@ -8,14 +8,14 @@ use std::net::IpAddr; use crate::error::Error; use crate::instance::Instance; -use crate::packet::{DecodeResult, Packet}; +use crate::packet::{DecodeResult, VRRPPacket}; // ===== Network packet receipt ===== pub(crate) fn process_packet( _instance: &mut Instance, _src: IpAddr, - _packet: DecodeResult, + _packet: DecodeResult, ) -> Result<(), Error> { // TODO diff --git a/holo-vrrp/src/packet.rs b/holo-vrrp/src/packet.rs index a9787b1e..4168cbf5 100644 --- a/holo-vrrp/src/packet.rs +++ b/holo-vrrp/src/packet.rs @@ -42,7 +42,7 @@ pub type DecodeResult = Result; // #[derive(Clone, Debug, Eq, PartialEq)] #[derive(Deserialize, Serialize)] -pub struct Packet { +pub struct VRRPPacket { // version + type [4 bits each] ver_type: u8, vrid: u8, @@ -63,14 +63,33 @@ pub struct Packet { #[derive(Deserialize, Serialize)] pub enum DecodeError { ChecksumError, - PacketLengthError(u8), + PacketLengthError(PacketLengthError), IpTtlError(u8), VersionError(u8) } + +#[derive(Debug, Eq, PartialEq)] +#[derive(Deserialize, Serialize)] +pub enum PacketLengthError { + + // A maximum number of 16 IP addresses are allowed for + // VRRP. + AddressCount(u8), + + // specified on the vrrp-ietf. when length of the + // vrrp packet is less than 16 bytes. + TooLow(u8), + + // customized. while for addresscount we look for the count_ip + // field in the header, in case the total length of the IP address + // is not specified correctly there, we will also manually look + // if there are too many bytes in the whole packet. + TooHigh(u8), +} // ===== impl Packet ===== -impl Packet { +impl VRRPPacket { // Encodes VRRP packet into a bytes buffer. pub fn encode(&self) -> BytesMut { let mut buf = BytesMut::with_capacity(114); @@ -131,15 +150,32 @@ impl std::fmt::Display for DecodeError { DecodeError::ChecksumError => { write!(f, "Checksum is not valid") }, - DecodeError::PacketLengthError(rx_length) => { - write!(f, "Packet length too low: {rx_length}") - }, + DecodeError::IpTtlError(rx_ttl) => { write!(f, "TTL less than 255: {rx_ttl}") }, DecodeError::VersionError(rx_version) => { write!(f, "Invalid version: {rx_version}") } + DecodeError::PacketLengthError(err) => { + std::fmt::Display::fmt(err, f) + } + } + } +} + +impl std::fmt::Display for PacketLengthError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PacketLengthError::TooHigh(rx_len) => { + write!(f, "Too many bytes for VRRP packet: {rx_len}") + }, + PacketLengthError::TooLow(rx_len) => { + write!(f, "Not enough bytes for VRRP packets: {rx_len}") + }, + PacketLengthError::AddressCount(rx_count) => { + write!(f, "Too many IP addresses {rx_count}") + }, } } } diff --git a/holo-vrrp/src/tasks.rs b/holo-vrrp/src/tasks.rs index 3fcd6a87..db3b5a67 100644 --- a/holo-vrrp/src/tasks.rs +++ b/holo-vrrp/src/tasks.rs @@ -43,7 +43,7 @@ pub mod messages { use serde::{Deserialize, Serialize}; - use crate::packet::{DecodeError, Packet}; + use crate::packet::{DecodeError, VRRPPacket}; // Type aliases. pub type ProtocolInputMsg = input::ProtocolMsg; @@ -62,7 +62,7 @@ pub mod messages { #[derive(Debug, Deserialize, Serialize)] pub struct NetRxPacketMsg { pub src: IpAddr, - pub packet: Result, + pub packet: Result, } } @@ -77,7 +77,7 @@ pub mod messages { #[derive(Clone, Debug, Serialize)] pub struct NetTxPacketMsg { - pub packet: Packet, + pub packet: VRRPPacket, pub src: IpAddr, pub dst: IpAddr, }