Skip to content

Commit

Permalink
cargo fmt changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-weqe committed Jul 8, 2024
1 parent 6f7eaab commit 0f2fe2e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 109 deletions.
6 changes: 2 additions & 4 deletions holo-vrrp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
// SPDX-License-Identifier: MIT
//

use std::{
fmt::{Debug, Display},
net::IpAddr,
};
use std::fmt::{Debug, Display};
use std::net::IpAddr;

use tracing::{warn, warn_span};

Expand Down
14 changes: 6 additions & 8 deletions holo-vrrp/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ use std::net::Ipv4Addr;
use chrono::{DateTime, Utc};
use holo_utils::task::TimeoutTask;

use crate::{northbound::configuration::InstanceCfg, packet::VrrpPacket};
use crate::northbound::configuration::InstanceCfg;
use crate::packet::VrrpPacket;

#[derive(Debug)]
pub struct Instance {

// Instance configuration data.
pub config: InstanceCfg,

// Instance state data.
pub state: InstanceState,

// timers
// timers
pub timer: VrrpTimer,

}


#[derive(Debug)]
pub enum VrrpTimer {
Null,
AdverTimer(TimeoutTask),
MasterDownTimer(TimeoutTask)
MasterDownTimer(TimeoutTask),
}

#[derive(Debug)]
Expand Down Expand Up @@ -104,7 +102,7 @@ impl Instance {
Instance {
config: Default::default(),
state: InstanceState::new(),
timer: VrrpTimer::Null
timer: VrrpTimer::Null,
}
}
}
Expand Down
67 changes: 35 additions & 32 deletions holo-vrrp/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//

use std::io;
use std::net::{IpAddr, SocketAddrV4, Ipv4Addr};
use std::net::{IpAddr, Ipv4Addr, SocketAddrV4};
use std::os::fd::FromRawFd;
use std::sync::Arc;

Expand All @@ -16,7 +16,7 @@ use socket2::{Domain, Protocol, Type};
use tokio::sync::mpsc::error::SendError;

use crate::error::IoError;
use crate::packet::{VrrpPacket, ArpPacket, EthernetFrame};
use crate::packet::{ArpPacket, EthernetFrame, VrrpPacket};
use crate::tasks::messages::input::NetRxPacketMsg;
use crate::tasks::messages::output::NetTxPacketMsg;

Expand All @@ -26,10 +26,8 @@ pub(crate) fn socket_vrrp(ifname: &str) -> Result<Socket, std::io::Error> {
let socket = capabilities::raise(|| {
Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::from(112)))
})?;

capabilities::raise(|| {
socket.bind_device(Some(ifname.as_bytes()))
})?;

capabilities::raise(|| socket.bind_device(Some(ifname.as_bytes())))?;
socket.set_broadcast(true)?;
Ok(socket)
}
Expand All @@ -43,18 +41,21 @@ pub fn socket_arp(ifname: &str) -> Result<Socket, std::io::Error> {
#[cfg(not(feature = "testing"))]
{
let sock = capabilities::raise(|| {
Socket::new(Domain::PACKET, Type::RAW, Some(Protocol::from(ETH_P_ARP)))
Socket::new(
Domain::PACKET,
Type::RAW,
Some(Protocol::from(ETH_P_ARP)),
)
})?;
capabilities::raise(|| {
sock.bind_device(Some(ifname.as_bytes()));
sock.set_broadcast(true);
});
Ok(sock)

}
#[cfg(feature = "testing")]
{
Ok(Socket { })
Ok(Socket {})
}
}

Expand All @@ -65,7 +66,6 @@ pub(crate) async fn send_packet_vrrp(
_dst: IpAddr,
packet: VrrpPacket,
) -> Result<usize, IoError> {

let buf: &[u8] = &packet.encode();
let saddr = SocketAddrV4::new(Ipv4Addr::new(224, 0, 0, 8), 0);

Expand All @@ -85,20 +85,29 @@ pub fn send_packet_arp(
eth_frame: EthernetFrame,
arp_packet: ArpPacket,
) -> Result<usize, IoError> {
use std::{ffi::{CString, NulError}, os::{self, fd::AsRawFd}};
use std::ffi::{CString, NulError};
use std::os::fd::AsRawFd;
use std::os::{self};

use bytes::Buf;
use libc::{c_void, if_indextoname, if_nametoindex, sendto, sockaddr, sockaddr_ll};
use libc::{
c_void, if_indextoname, if_nametoindex, sendto, sockaddr, sockaddr_ll,
};

use crate::packet::ARPframe;
let mut arpframe = ARPframe::new(eth_frame, arp_packet);

let c_ifname = match CString::new(ifname.clone()){
let c_ifname = match CString::new(ifname.clone()) {
Ok(c_ifname) => c_ifname,
Err(err) => return Err(IoError::SocketError(std::io::Error::new(io::ErrorKind::NotFound, err))),
Err(err) => {
return Err(IoError::SocketError(std::io::Error::new(
io::ErrorKind::NotFound,
err,
)))
}
};
let ifindex = unsafe { libc::if_nametoindex(c_ifname.as_ptr()) };

let mut sa = sockaddr_ll {
sll_family: AF_PACKET as u16,
sll_protocol: 0x806_u16.to_be(),
Expand All @@ -110,26 +119,21 @@ pub fn send_packet_arp(
};

unsafe {
let ptr_sockaddr = std::mem::transmute::<*mut sockaddr_ll, *mut sockaddr>(&mut sa);

let ptr_sockaddr =
std::mem::transmute::<*mut sockaddr_ll, *mut sockaddr>(&mut sa);

match sendto(
sock.as_raw_fd(),
&mut arpframe as *mut _ as *const c_void,
std::mem::size_of_val(&arpframe),
0,
ptr_sockaddr,
std::mem::size_of_val(&sa) as u32
sock.as_raw_fd(),
&mut arpframe as *mut _ as *const c_void,
std::mem::size_of_val(&arpframe),
0,
ptr_sockaddr,
std::mem::size_of_val(&sa) as u32,
) {
-1 => {
Err(IoError::SendError(io::Error::last_os_error()))
},
fd => {
Ok(fd as usize)
}
-1 => Err(IoError::SendError(io::Error::last_os_error())),
fd => Ok(fd as usize),
}

}

}

#[cfg(not(feature = "testing"))]
Expand All @@ -152,7 +156,6 @@ pub(crate) async fn write_loop(
// if let Err(error) = send_packet_arp(&socket_arp).await {
// error.log();
// }

}
}
}
Expand Down
105 changes: 61 additions & 44 deletions holo-vrrp/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
// SPDX-License-Identifier: MIT
//

use crate::error::{self, Error, GlobalError, VirtualRouterError};
use std::net::Ipv4Addr;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use holo_utils::bytes::{BytesExt, BytesMutExt};
use serde::{Deserialize, Serialize};
use std::net::Ipv4Addr;

use crate::error::{self, Error, GlobalError, VirtualRouterError};

// Type aliases.
pub type DecodeResult<T> = Result<T, DecodeError>;
Expand Down Expand Up @@ -99,35 +101,34 @@ pub struct ARPframe {
pub ethertype: u16, // ether type

// ARP
pub hardware_type: u16, // network link type (0x1=ethernet)
pub protocol_type: u16, // upper-layer protocol for resolution
pub hw_addr_len: u8, // length of hardware address (bytes)
pub proto_addr_len: u8, // upper-layer protocol address length
pub opcode: u16, // operation (0x1=request, 0x2=reply)
pub sender_hw_addr: [u8; 6], // sender hardware address
pub hardware_type: u16, // network link type (0x1=ethernet)
pub protocol_type: u16, // upper-layer protocol for resolution
pub hw_addr_len: u8, // length of hardware address (bytes)
pub proto_addr_len: u8, // upper-layer protocol address length
pub opcode: u16, // operation (0x1=request, 0x2=reply)
pub sender_hw_addr: [u8; 6], // sender hardware address
pub sender_proto_addr: [u8; 4], // internetwork address of sender
pub target_hw_addr: [u8; 6], // hardware address of target
pub target_hw_addr: [u8; 6], // hardware address of target
pub target_proto_addr: [u8; 4], // internetwork address of target
}

impl ARPframe {
pub fn new(eth_pkt: EthernetFrame, arp_pkt: ArpPacket) -> Self {
Self {
dst_mac: eth_pkt.dst_mac,
src_mac: eth_pkt.src_mac,
ethertype: eth_pkt.ethertype.to_be(),
dst_mac: eth_pkt.dst_mac,
src_mac: eth_pkt.src_mac,
ethertype: eth_pkt.ethertype.to_be(),


hardware_type: arp_pkt.hw_type.to_be(),
protocol_type: arp_pkt.proto_type.to_be(),
hw_addr_len: arp_pkt.hw_length,
hardware_type: arp_pkt.hw_type.to_be(),
protocol_type: arp_pkt.proto_type.to_be(),
hw_addr_len: arp_pkt.hw_length,
proto_addr_len: arp_pkt.proto_length,
opcode: arp_pkt.operation.to_be(),
sender_hw_addr: arp_pkt.sender_hw_address,
sender_proto_addr: arp_pkt.sender_proto_address,
target_hw_addr: arp_pkt.target_hw_address,
target_proto_addr: arp_pkt.target_proto_address,
opcode: arp_pkt.operation.to_be(),

sender_hw_addr: arp_pkt.sender_hw_address,
sender_proto_addr: arp_pkt.sender_proto_address,
target_hw_addr: arp_pkt.target_hw_address,
target_proto_addr: arp_pkt.target_proto_address,
}
}
}
Expand All @@ -137,7 +138,7 @@ impl ARPframe {
pub struct EthernetFrame {
pub dst_mac: [u8; 6],
pub src_mac: [u8; 6],
pub ethertype: u16
pub ethertype: u16,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -148,10 +149,10 @@ pub struct ArpPacket {
pub hw_length: u8,
pub proto_length: u8,
pub operation: u16,
pub sender_hw_address: [u8; 6], // src mac
pub sender_hw_address: [u8; 6], // src mac
pub sender_proto_address: [u8; 4], // src ip
pub target_hw_address: [u8; 6], // src mac
pub target_proto_address: [u8; 4] // src ip
pub target_hw_address: [u8; 6], // src mac
pub target_proto_address: [u8; 4], // src ip
}

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -264,10 +265,7 @@ impl VrrpPacket {
}

pub(crate) fn generate_checksum(&mut self) {
self.checksum = checksum::calculate(
self.encode().chunk(),
3
);
self.checksum = checksum::calculate(self.encode().chunk(), 3);
}
}

Expand Down Expand Up @@ -389,9 +387,13 @@ impl EthernetFrame {
let mut buf = Bytes::copy_from_slice(data);
let mut dst_mac: [u8; 6] = [0u8; 6];
let mut src_mac: [u8; 6] = [0u8; 6];

for x in 0..6 { dst_mac[x] = buf.get_u8(); }
for x in 0..6 { src_mac[x] = buf.get_u8(); }

for x in 0..6 {
dst_mac[x] = buf.get_u8();
}
for x in 0..6 {
src_mac[x] = buf.get_u8();
}

Ok(Self {
dst_mac,
Expand All @@ -410,16 +412,24 @@ impl ArpPacket {
buf.put_u8(self.proto_length);
buf.put_u16(self.operation);

for x in self.sender_hw_address { buf.put_u8(x); }
for x in self.sender_proto_address { buf.put_u8(x); }
for x in self.target_hw_address { buf.put_u8(x) }
for x in self.target_proto_address { buf.put_u8(x) }
for x in self.sender_hw_address {
buf.put_u8(x);
}
for x in self.sender_proto_address {
buf.put_u8(x);
}
for x in self.target_hw_address {
buf.put_u8(x)
}
for x in self.target_proto_address {
buf.put_u8(x)
}
buf
}

pub fn decode(data: &[u8]) ->DecodeResult<Self> {
pub fn decode(data: &[u8]) -> DecodeResult<Self> {
if data.len() != 28 {
return Err(DecodeError::PacketLengthError)
return Err(DecodeError::PacketLengthError);
}
let mut buf = Bytes::copy_from_slice(data);

Expand All @@ -429,16 +439,24 @@ impl ArpPacket {
let proto_length = buf.get_u8();
let operation = buf.get_u16();
let mut sender_hw_address: [u8; 6] = [0_u8; 6];
for x in 0..6 { sender_hw_address[x] = buf.get_u8(); }
for x in 0..6 {
sender_hw_address[x] = buf.get_u8();
}

let mut sender_proto_address: [u8; 4] = [0_u8; 4];
for x in 0..4 { sender_proto_address[x] = buf.get_u8(); }
for x in 0..4 {
sender_proto_address[x] = buf.get_u8();
}

let mut target_hw_address: [u8; 6] = [0_u8; 6];
for x in 0..6 { target_hw_address[x] = buf.get_u8(); }
for x in 0..6 {
target_hw_address[x] = buf.get_u8();
}

let mut target_proto_address: [u8; 4] = [0_u8; 4];
for x in 0..4 { target_hw_address[x] = buf.get_u8(); }
for x in 0..4 {
target_hw_address[x] = buf.get_u8();
}

Ok(Self {
hw_type,
Expand All @@ -452,7 +470,6 @@ impl ArpPacket {
target_proto_address,
})
}

}

pub mod checksum {
Expand Down
Loading

0 comments on commit 0f2fe2e

Please sign in to comment.