Skip to content

Commit

Permalink
Change Packet naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-weqe committed Jun 19, 2024
1 parent 73e6c9e commit c701030
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
6 changes: 3 additions & 3 deletions holo-vrrp/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions holo-vrrp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
_packet: DecodeResult<VRRPPacket>,
) -> Result<(), Error> {
// TODO

Expand Down
48 changes: 42 additions & 6 deletions holo-vrrp/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub type DecodeResult<T> = Result<T, DecodeError>;
//
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Deserialize, Serialize)]
pub struct Packet {
pub struct VRRPPacket {
// version + type [4 bits each]
ver_type: u8,
vrid: u8,
Expand All @@ -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);
Expand Down Expand Up @@ -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}")
},
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions holo-vrrp/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -62,7 +62,7 @@ pub mod messages {
#[derive(Debug, Deserialize, Serialize)]
pub struct NetRxPacketMsg {
pub src: IpAddr,
pub packet: Result<Packet, DecodeError>,
pub packet: Result<VRRPPacket, DecodeError>,
}
}

Expand All @@ -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,
}
Expand Down

0 comments on commit c701030

Please sign in to comment.