Skip to content

Commit

Permalink
Proper initialization of error.rs and packet.rs
Browse files Browse the repository at this point in the history
initialize validations and tests in packet.rs
  • Loading branch information
Paul-weqe committed Jun 19, 2024
1 parent 2f0b189 commit 73e6c9e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 11 deletions.
38 changes: 33 additions & 5 deletions holo-vrrp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@

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
VridError,
AddressListError(Vec<IpAddr>, Vec<IpAddr>),
IntervalError
}

// BGP I/O errors.
// VRRP I/O errors.
#[derive(Debug)]
pub enum IoError {
SocketError(std::io::Error),
Expand All @@ -34,6 +38,21 @@ impl Error {
match self {
Error::IoError(error) => {
error.log();
},
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)
});
}
}
}
Expand All @@ -43,6 +62,15 @@ 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::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")
}
}
}
}
Expand Down Expand Up @@ -133,4 +161,4 @@ fn with_source<E: std::error::Error>(error: E) -> String {
} else {
error.to_string()
}
}
}
84 changes: 78 additions & 6 deletions holo-vrrp/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// SPDX-License-Identifier: MIT
//

use std::net::{IpAddr, Ipv4Addr};

//use bitflags::bitflags;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use holo_utils::bytes::{BytesExt, BytesMutExt};
//use holo_utils::bytes::TLS_BUF;
//use num_derive::FromPrimitive;
//use num_traits::FromPrimitive;
Expand Down Expand Up @@ -40,35 +43,104 @@ pub type DecodeResult<T> = Result<T, DecodeError>;
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Deserialize, Serialize)]
pub struct Packet {
// TODO
// version + type [4 bits each]
ver_type: u8,
vrid: u8,
priority: u8,
count_ip: u8,
auth_type: u8,
adver_int: u8,
checksum: u16,
ip_addresses: Vec<Ipv4Addr>,

// the following two are only used for backward compatibility.
auth_data: u32,
auth_data2: u32
}

// VRRP decode errors.
#[derive(Debug, Eq, PartialEq)]
#[derive(Deserialize, Serialize)]
pub enum DecodeError {
// TODO
ChecksumError,
PacketLengthError(u8),
IpTtlError(u8),
VersionError(u8)
}

// ===== impl Packet =====

impl Packet {
// Encodes VRRP packet into a bytes buffer.
pub fn encode(&self) -> BytesMut {
todo!()
let mut buf = BytesMut::with_capacity(114);
buf.put_u8(self.ver_type);
buf.put_u8(self.vrid);
buf.put_u8(self.priority);
buf.put_u8(self.count_ip);
buf.put_u8(self.auth_type);
buf.put_u8(self.adver_int);
buf.put_u16(self.checksum);
for addr in &self.ip_addresses {
buf.put_ipv4(addr);
}
buf.put_u32(self.auth_data);
buf.put_u32(self.auth_data2);
buf
}

// Decodes VRRP packet from a bytes buffer.
pub fn decode(_data: &[u8]) -> Result<Self, DecodeError> {
todo!()
pub fn decode(data: &[u8]) -> Result<Self, DecodeError> {
let mut buf: Bytes = Bytes::copy_from_slice(data);
let ver_type = buf.get_u8();
let vrid = buf.get_u8();
let priority = buf.get_u8();
let count_ip = buf.get_u8();
let auth_type = buf.get_u8();
let adver_int = buf.get_u8();
let checksum = buf.get_u16();

let mut ip_addresses: Vec<Ipv4Addr> = vec![];
for addr in 0..count_ip {
ip_addresses.push(buf.get_ipv4());
}
let auth_data = buf.get_u32();
let auth_data2 = buf.get_u32();

Ok(Self {
ver_type,
vrid,
priority,
count_ip,
auth_type,
adver_int,
checksum,
ip_addresses,
auth_data,
auth_data2
})

}
}

// ===== impl DecodeError =====

impl std::fmt::Display for DecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
match self {
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}")
}
}
}
}

Expand Down

0 comments on commit 73e6c9e

Please sign in to comment.