From 0f2fe2e535186cb17bc034aeb644eec83a3bc63c Mon Sep 17 00:00:00 2001 From: weqe Date: Mon, 8 Jul 2024 23:18:12 +0300 Subject: [PATCH] cargo fmt changes --- holo-vrrp/src/error.rs | 6 +- holo-vrrp/src/instance.rs | 14 ++--- holo-vrrp/src/network.rs | 67 +++++++++++----------- holo-vrrp/src/packet.rs | 105 ++++++++++++++++++++-------------- holo-vrrp/src/tasks.rs | 18 +++--- holo-vrrp/tests/packet/arp.rs | 16 +++--- holo-vrrp/tests/packet/mod.rs | 2 +- 7 files changed, 119 insertions(+), 109 deletions(-) diff --git a/holo-vrrp/src/error.rs b/holo-vrrp/src/error.rs index 9bcb1fb0..e9c5d2ad 100644 --- a/holo-vrrp/src/error.rs +++ b/holo-vrrp/src/error.rs @@ -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}; diff --git a/holo-vrrp/src/instance.rs b/holo-vrrp/src/instance.rs index 319a674c..d62fb452 100644 --- a/holo-vrrp/src/instance.rs +++ b/holo-vrrp/src/instance.rs @@ -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)] @@ -104,7 +102,7 @@ impl Instance { Instance { config: Default::default(), state: InstanceState::new(), - timer: VrrpTimer::Null + timer: VrrpTimer::Null, } } } diff --git a/holo-vrrp/src/network.rs b/holo-vrrp/src/network.rs index e603278a..dd348e6c 100644 --- a/holo-vrrp/src/network.rs +++ b/holo-vrrp/src/network.rs @@ -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; @@ -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; @@ -26,10 +26,8 @@ pub(crate) fn socket_vrrp(ifname: &str) -> Result { 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) } @@ -43,18 +41,21 @@ pub fn socket_arp(ifname: &str) -> Result { #[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 {}) } } @@ -65,7 +66,6 @@ pub(crate) async fn send_packet_vrrp( _dst: IpAddr, packet: VrrpPacket, ) -> Result { - let buf: &[u8] = &packet.encode(); let saddr = SocketAddrV4::new(Ipv4Addr::new(224, 0, 0, 8), 0); @@ -85,20 +85,29 @@ pub fn send_packet_arp( eth_frame: EthernetFrame, arp_packet: ArpPacket, ) -> Result { - 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(), @@ -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"))] @@ -152,7 +156,6 @@ pub(crate) async fn write_loop( // if let Err(error) = send_packet_arp(&socket_arp).await { // error.log(); // } - } } } diff --git a/holo-vrrp/src/packet.rs b/holo-vrrp/src/packet.rs index 8411a789..1c27491d 100644 --- a/holo-vrrp/src/packet.rs +++ b/holo-vrrp/src/packet.rs @@ -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 = Result; @@ -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, } } } @@ -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)] @@ -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)] @@ -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); } } @@ -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, @@ -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 { + pub fn decode(data: &[u8]) -> DecodeResult { if data.len() != 28 { - return Err(DecodeError::PacketLengthError) + return Err(DecodeError::PacketLengthError); } let mut buf = Bytes::copy_from_slice(data); @@ -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, @@ -452,7 +470,6 @@ impl ArpPacket { target_proto_address, }) } - } pub mod checksum { diff --git a/holo-vrrp/src/tasks.rs b/holo-vrrp/src/tasks.rs index 31abd032..4d33a231 100644 --- a/holo-vrrp/src/tasks.rs +++ b/holo-vrrp/src/tasks.rs @@ -160,28 +160,24 @@ pub(crate) fn net_tx( } } -fn set_timer( - instance: &mut Instance -) { - +fn set_timer(instance: &mut Instance) { match instance.state.state { - crate::instance::State::Initialize => { instance.timer = VrrpTimer::Null; - }, + } crate::instance::State::Backup => { let timer = TimeoutTask::new( Duration::from_secs(instance.state.master_down_interval as u64), - move || async move { } + move || async move {}, ); instance.timer = VrrpTimer::MasterDownTimer(timer); - }, + } crate::instance::State::Master => { let timer = TimeoutTask::new( Duration::from_secs(instance.config.advertise_interval as u64), - move || async move { } + move || async move {}, ); instance.timer = VrrpTimer::AdverTimer(timer); - }, + } } -} \ No newline at end of file +} diff --git a/holo-vrrp/tests/packet/arp.rs b/holo-vrrp/tests/packet/arp.rs index bfee3c9b..d3ee624e 100644 --- a/holo-vrrp/tests/packet/arp.rs +++ b/holo-vrrp/tests/packet/arp.rs @@ -6,9 +6,8 @@ use holo_vrrp::packet::{ArpPacket, DecodeError}; - /* -ARP packet => +ARP packet => hw_type: 1, @@ -16,23 +15,22 @@ proto_type: 0x0800, hw_length: 6, proto_length: 4, operation: 1, -sender_hw_address: [0xd4, 0xb1, 0x08, 0x4c, 0xbb, 0xf9], // src mac +sender_hw_address: [0xd4, 0xb1, 0x08, 0x4c, 0xbb, 0xf9], // src mac sender_proto_address: [192, 168, 100, 1], // src ip -pub target_hw_address: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // src mac +pub target_hw_address: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // src mac pub target_proto_address: [192, 168, 100, 16] // src ip */ fn valid_pkt_data() -> [u8; 28] { [ - - 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xd4, 0xb1, 0x08, 0x4c, 0xbb, 0xf9, - 0xc0, 0xa8, 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x64, 0x10 + 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xd4, 0xb1, 0x08, 0x4c, + 0xbb, 0xf9, 0xc0, 0xa8, 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xc0, 0xa8, 0x64, 0x10, ] } - #[test] -fn encode_valid_pkt(){ +fn encode_valid_pkt() { let pkt_wrapped = ArpPacket::decode(&valid_pkt_data()); assert!(pkt_wrapped.is_ok()); } diff --git a/holo-vrrp/tests/packet/mod.rs b/holo-vrrp/tests/packet/mod.rs index a21841f3..6a4cd235 100644 --- a/holo-vrrp/tests/packet/mod.rs +++ b/holo-vrrp/tests/packet/mod.rs @@ -4,6 +4,6 @@ // SPDX-License-Identifier: MIT // +mod arp; mod ipv4; mod vrrp; -mod arp; \ No newline at end of file