Skip to content

Commit

Permalink
Merge pull request #42 from Paul-weqe/vrrp-consts
Browse files Browse the repository at this point in the history
vrrp: move consts to local packets
  • Loading branch information
rwestphal authored Jan 15, 2025
2 parents 542dd70 + e9c8252 commit 7b59dd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
16 changes: 1 addition & 15 deletions holo-vrrp/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,5 @@ use std::net::Ipv4Addr;
// valid vrrp versions
pub const VALID_VRRP_VERSIONS: [u8; 1] = [2];
pub const VRRP_PROTO_NUMBER: i32 = 112;
// maximum size of vrrp header
pub const VRRP_HDR_MAX: usize = 96;
// minimum size of vrrp header.
pub const VRRP_HDR_MIN: usize = 16;
// maximum size of IP + vrrp header.
// For when we use the layer 2 socket
pub const IP_VRRP_HDR_MAX: usize = 130;
pub const VRRP_MULTICAST_ADDRESS: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 18);

// max size of ip + vrrp header maximum
// number of virtual IP addresses that can be on a VRRP header.
pub const VRRP_IP_COUNT_MAX: usize = 20;

// ==== IP ====

pub const IP_HDR_MIN: usize = 20;
pub const VRRP_MULTICAST_ADDRESS: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 18);
19 changes: 13 additions & 6 deletions holo-vrrp/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
use holo_utils::bytes::{BytesExt, BytesMutExt};
use serde::{Deserialize, Serialize};

use crate::consts::*;

// Type aliases.
pub type DecodeResult<T> = Result<T, DecodeError>;

Expand Down Expand Up @@ -135,6 +133,10 @@ pub enum DecodeError {
// ===== impl Packet =====

impl VrrpHdr {
const MAX_LEN: usize = 96;
const MIN_LEN: usize = 16;
const MAX_IP_COUNT: usize = 20;

// Encodes VRRP packet into a bytes buffer.
pub fn encode(&self) -> BytesMut {
let mut buf = BytesMut::with_capacity(114);
Expand Down Expand Up @@ -169,8 +171,8 @@ impl VrrpHdr {
let auth_type = buf.get_u8();
let adver_int = buf.get_u8();

if !(VRRP_HDR_MIN..=VRRP_HDR_MAX).contains(&pkt_size)
|| count_ip as usize > VRRP_IP_COUNT_MAX
if !(Self::MIN_LEN..=Self::MAX_LEN).contains(&pkt_size)
|| count_ip as usize > Self::MAX_IP_COUNT
|| (count_ip * 4) + 16 != pkt_size as u8
{
return Err(DecodeError::PacketLengthError { vrid });
Expand Down Expand Up @@ -213,6 +215,8 @@ impl VrrpHdr {
}

impl Ipv4Hdr {
const MIN_LEN: usize = 20;

pub fn encode(&self) -> BytesMut {
let mut buf = BytesMut::new();

Expand Down Expand Up @@ -269,7 +273,7 @@ impl Ipv4Hdr {
let mut options: Option<u32> = None;
let mut padding: Option<u8> = None;

if ihl > IP_HDR_MIN as u8 {
if ihl > Self::MIN_LEN as u8 {
let opt_pad = buf.get_u32();
options = Some(opt_pad >> 8);
padding = Some((opt_pad & 0xFF) as u8);
Expand Down Expand Up @@ -318,8 +322,11 @@ impl EthernetHdr {
}

impl VrrpPacket {
// maximum size of IP + vrrp header.
const MAX_LEN: usize = 130;

pub fn encode(&self) -> BytesMut {
let mut buf = BytesMut::with_capacity(IP_VRRP_HDR_MAX);
let mut buf = BytesMut::with_capacity(Self::MAX_LEN);
buf.put(self.ip.encode());
buf.put(self.vrrp.encode());
buf
Expand Down

0 comments on commit 7b59dd2

Please sign in to comment.