From a86e7dd9245efb4ea958a510934aa06a3a3dd448 Mon Sep 17 00:00:00 2001 From: weqe Date: Wed, 19 Jun 2024 12:51:38 +0300 Subject: [PATCH] Initialize VRRP errors --- holo-vrrp/src/error.rs | 107 +++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/holo-vrrp/src/error.rs b/holo-vrrp/src/error.rs index ed08b6b6..33088731 100644 --- a/holo-vrrp/src/error.rs +++ b/holo-vrrp/src/error.rs @@ -6,25 +6,27 @@ use std::net::IpAddr; -use tracing::warn; +use tracing::{warn, warn_span}; -// BGP errors. +// VRRP errors. #[derive(Debug)] pub enum Error { // I/O errors IoError(IoError), - // TODO other errors + + // other errors + VersionError, + VridError, + AddressListError(Vec, Vec), + IntervalError } -// BGP I/O errors. +// VRRP I/O errors. #[derive(Debug)] pub enum IoError { - SocketError(std::io::Error), - MulticastJoinError(IpAddr, std::io::Error), - MulticastLeaveError(IpAddr, std::io::Error), - RecvError(std::io::Error), - RecvMissingSourceAddr, - SendError(std::io::Error), + ChecksumError(std::io::Error), + PacketLengthError(std::io::Error), + IpTtlError(std::io::Error) } // ===== impl Error ===== @@ -34,6 +36,26 @@ impl Error { match self { Error::IoError(error) => { error.log(); + }, + Error::VersionError => { + warn_span!("virtual_router").in_scope(|| { + warn!("{}", self); + }); + }, + Error::VridError => { + warn_span!("virtual_router").in_scope(|| { + warn!("{}", self) + }); + }, + Error::AddressListError(_, _) => { + warn_span!("virtual_router").in_scope(|| { + warn!("{}", self) + }); + }, + Error::IntervalError => { + warn_span!("virtual_router").in_scope(|| { + warn!("{}", self) + }); } } } @@ -43,6 +65,18 @@ impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Error::IoError(error) => error.fmt(f), + Error::VersionError => { + write!(f, "invalid version received") + }, + Error::VridError => { + write!(f, "virtual router id(VRID) not matching locally configured") + }, + Error::AddressListError(..) => { + write!(f, "received address list not matching local address list") + }, + Error::IntervalError => { + write!(f, "received advert interval not matching local configured advert interval") + } } } } @@ -67,19 +101,11 @@ impl From for Error { impl IoError { pub(crate) fn log(&self) { match self { - IoError::SocketError(error) => { + IoError::ChecksumError(error) + | IoError::PacketLengthError(error) + | IoError::IpTtlError(error) => { warn!(error = %with_source(error), "{}", self); } - IoError::MulticastJoinError(addr, error) - | IoError::MulticastLeaveError(addr, error) => { - warn!(?addr, error = %with_source(error), "{}", self); - } - IoError::RecvError(error) | IoError::SendError(error) => { - warn!(error = %with_source(error), "{}", self); - } - IoError::RecvMissingSourceAddr => { - warn!("{}", self); - } } } } @@ -87,26 +113,14 @@ impl IoError { impl std::fmt::Display for IoError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - IoError::SocketError(..) => { - write!(f, "failed to create raw IP socket") - } - IoError::MulticastJoinError(..) => { - write!(f, "failed to join multicast group") - } - IoError::MulticastLeaveError(..) => { - write!(f, "failed to leave multicast group") - } - IoError::RecvError(..) => { - write!(f, "failed to receive IP packet") - } - IoError::RecvMissingSourceAddr => { - write!( - f, - "failed to retrieve source address from received packet" - ) - } - IoError::SendError(..) => { - write!(f, "failed to send IP packet") + IoError::ChecksumError(..) => { + write!(f, "invalid VRRP checksum") + }, + IoError::PacketLengthError(..) => { + write!(f, "VRRP packet length not reaching minimum 50 bytes") + }, + IoError::IpTtlError(..) => { + write!(f, "VRRP pkt TTL must be 255") } } } @@ -115,12 +129,11 @@ impl std::fmt::Display for IoError { impl std::error::Error for IoError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { - IoError::SocketError(error) - | IoError::MulticastJoinError(_, error) - | IoError::MulticastLeaveError(_, error) - | IoError::RecvError(error) - | IoError::SendError(error) => Some(error), - _ => None, + IoError::ChecksumError(error) + | IoError::PacketLengthError(error) + | IoError::IpTtlError(error) => { + Some(error) + } } } }