From 993edf3892f498f2079a0b5ba668ee6ddc8ad10e Mon Sep 17 00:00:00 2001 From: tiannian Date: Sat, 22 Oct 2022 11:52:53 +0800 Subject: [PATCH] fix clippy --- auip/src/interface/utils.rs | 4 +- auip/src/storage/dynamic/addrs.rs | 10 +--- auip/src/storage/dynamic/arp.rs | 11 +--- pkt/src/layer2/address.rs | 2 +- pkt/src/layer3/ipv4/address.rs | 4 +- pkt/src/layer3/ipv4/cidr.rs | 4 +- taptun/src/lib.rs | 63 ++-------------------- taptun/src/open.rs | 52 +++++++++++++++++++ taptun/src/taptun.rs | 86 +++++++++++++++++-------------- 9 files changed, 113 insertions(+), 123 deletions(-) create mode 100644 taptun/src/open.rs diff --git a/auip/src/interface/utils.rs b/auip/src/interface/utils.rs index f4d5b0b..83a52ed 100644 --- a/auip/src/interface/utils.rs +++ b/auip/src/interface/utils.rs @@ -46,10 +46,10 @@ pub fn build_and_record_arp( log::debug!("Send packet: {}", layer2_pkt); - return Ok(Action::SendArp(layer2_pkt.into_inner())); + Ok(Action::SendArp(layer2_pkt.into_inner())) } else { log::debug!("Ip address mismatch, Drop it."); - return Ok(Action::NoAction); + Ok(Action::NoAction) } } diff --git a/auip/src/storage/dynamic/addrs.rs b/auip/src/storage/dynamic/addrs.rs index 0722b34..c0986de 100644 --- a/auip/src/storage/dynamic/addrs.rs +++ b/auip/src/storage/dynamic/addrs.rs @@ -6,20 +6,12 @@ use auip_pkt::{ use crate::{AddrsStorage, Error, Result}; +#[derive(Default, Debug)] pub struct Addrs { pub mac_addr: layer2::Address, pub ip_addrs: Vec, } -impl Default for Addrs { - fn default() -> Self { - Self { - mac_addr: Default::default(), - ip_addrs: Vec::new(), - } - } -} - impl AddrsStorage for Addrs { fn mac_addr(&self) -> &layer2::Address { &self.mac_addr diff --git a/auip/src/storage/dynamic/arp.rs b/auip/src/storage/dynamic/arp.rs index b417366..1f7d47c 100644 --- a/auip/src/storage/dynamic/arp.rs +++ b/auip/src/storage/dynamic/arp.rs @@ -3,18 +3,11 @@ use auip_pkt::{layer2, layer3}; use crate::{ArpStorage, Result}; +#[derive(Debug, Default)] pub struct Arp { pub map: BTreeMap, } -impl Default for Arp { - fn default() -> Self { - Self { - map: BTreeMap::new(), - } - } -} - impl ArpStorage for Arp { fn set_map(&mut self, mac: layer2::Address, ip_addr: layer3::ipv4::Address) -> Result<()> { self.map.insert(ip_addr, mac); @@ -22,6 +15,6 @@ impl ArpStorage for Arp { } fn mac_addr(&self, ip_addr: &layer3::ipv4::Address) -> Result> { - Ok(self.map.get(ip_addr).map(|a| *a)) + Ok(self.map.get(ip_addr).copied()) } } diff --git a/pkt/src/layer2/address.rs b/pkt/src/layer2/address.rs index 63442cd..bd3a399 100644 --- a/pkt/src/layer2/address.rs +++ b/pkt/src/layer2/address.rs @@ -51,7 +51,7 @@ impl Address { } pub fn parse(s: &str) -> Result { - let mut segments = s.split(":"); + let mut segments = s.split(':'); let mut inner = [0u8; 6]; diff --git a/pkt/src/layer3/ipv4/address.rs b/pkt/src/layer3/ipv4/address.rs index 2d2d59a..ceb364f 100644 --- a/pkt/src/layer3/ipv4/address.rs +++ b/pkt/src/layer3/ipv4/address.rs @@ -35,13 +35,13 @@ impl Address { } pub fn parse(s: &str) -> Result { - let mut segments = s.split("."); + let mut segments = s.split('.'); let mut inner = [0u8; 4]; for i in inner.iter_mut() { if let Some(seg) = segments.next() { - *i = u8::from_str_radix(seg, 10)?; + *i = seg.parse::()?; } else { return Err(Error::ParseIpv4AddressFailed); } diff --git a/pkt/src/layer3/ipv4/cidr.rs b/pkt/src/layer3/ipv4/cidr.rs index 0a37643..e707a43 100644 --- a/pkt/src/layer3/ipv4/cidr.rs +++ b/pkt/src/layer3/ipv4/cidr.rs @@ -24,13 +24,13 @@ impl Cidr { } pub fn parse(s: &str) -> Result { - let mut segments = s.split("/"); + let mut segments = s.split('/'); let addr = segments.next().ok_or(Error::ParseIpv4CidrFailed)?; let mk = segments.next().ok_or(Error::ParseIpv4CidrFailed)?; let address = Address::parse(addr)?; - let prefix_len = u8::from_str_radix(mk, 10)?; + let prefix_len = mk.parse::()?; Ok(Self { address, diff --git a/taptun/src/lib.rs b/taptun/src/lib.rs index bc75162..9e395bf 100644 --- a/taptun/src/lib.rs +++ b/taptun/src/lib.rs @@ -1,63 +1,8 @@ mod error; -use std::{ - fs::File, - io::{Read, Write}, -}; +pub use error::*; -use auip::Device; -pub use error::{Error, Result}; +mod open; +pub use open::open_device; mod taptun; -pub use taptun::open_device; - -pub struct TapTunDevice { - pub rx_buffer: [u8; 1536], - pub len: usize, - pub file: File, -} - -impl TapTunDevice { - pub fn new_tap(ifname: &str) -> Result { - let file = open_device(ifname, true)?; - - Ok(Self { - rx_buffer: [0u8; 1536], - len: 0, - file, - }) - } - - pub fn new_tun(ifname: &str) -> Result { - let file = open_device(ifname, false)?; - - Ok(Self { - rx_buffer: [0u8; 1536], - len: 0, - file, - }) - } - - pub fn poll_read(&mut self) { - let len = self.file.read(&mut self.rx_buffer).unwrap(); - self.len = len; - } -} - -impl Device for TapTunDevice { - fn medium(&self) -> auip::Medium { - auip::Medium::Ethernet - } - - fn recv(&mut self) -> auip::Result> { - if self.len == 0 { - Ok(None) - } else { - Ok(Some(&self.rx_buffer[..self.len])) - } - } - - fn send(&mut self, buffer: &[u8]) -> auip::Result<()> { - self.file.write_all(buffer).unwrap(); - Ok(()) - } -} +pub use taptun::*; diff --git a/taptun/src/open.rs b/taptun/src/open.rs new file mode 100644 index 0000000..493b0bd --- /dev/null +++ b/taptun/src/open.rs @@ -0,0 +1,52 @@ +use libc::{IFF_NO_PI, IFF_TAP, IFF_TUN}; + +use crate::{Error, Result}; +use std::fs::{File, OpenOptions}; +use std::os::unix::io::AsRawFd; + +const TUNSETIFF: libc::c_ulong = 0x400454CA; + +#[repr(C)] +#[derive(Debug)] +struct Ifreq { + ifr_name: [libc::c_char; libc::IF_NAMESIZE], + ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */ +} + +fn ifreq_new(name: &str) -> Ifreq { + let mut ifreq = Ifreq { + ifr_name: [0; libc::IF_NAMESIZE], + ifr_data: 0, + }; + // set name. + for (i, byte) in name.as_bytes().iter().enumerate() { + ifreq.ifr_name[i] = *byte as libc::c_char + } + ifreq +} + +fn ifreq_ioctl(fd: libc::c_int, cmd: libc::c_ulong, ifreq: &mut Ifreq) -> libc::c_int { + unsafe { libc::ioctl(fd, cmd as _, ifreq as *mut Ifreq) } +} + +pub fn open_device(name: &str, is_tap: bool) -> Result { + let file = OpenOptions::new() + .read(true) + .write(true) + .open("/dev/net/tun")?; + let mut ifreq = ifreq_new(name); + + if is_tap { + ifreq.ifr_data = IFF_TAP | IFF_NO_PI; + } else { + ifreq.ifr_data = IFF_TUN | IFF_NO_PI; + } + + let fd = file.as_raw_fd(); + let res = ifreq_ioctl(fd, TUNSETIFF, &mut ifreq); + if res == -1 { + Err(Error::StdIOError(std::io::Error::last_os_error())) + } else { + Ok(file) + } +} diff --git a/taptun/src/taptun.rs b/taptun/src/taptun.rs index 493b0bd..7980b3a 100644 --- a/taptun/src/taptun.rs +++ b/taptun/src/taptun.rs @@ -1,52 +1,60 @@ -use libc::{IFF_NO_PI, IFF_TAP, IFF_TUN}; +use std::{ + fs::File, + io::{Read, Write}, +}; -use crate::{Error, Result}; -use std::fs::{File, OpenOptions}; -use std::os::unix::io::AsRawFd; +use auip::Device; -const TUNSETIFF: libc::c_ulong = 0x400454CA; +use crate::{open_device, Result}; -#[repr(C)] -#[derive(Debug)] -struct Ifreq { - ifr_name: [libc::c_char; libc::IF_NAMESIZE], - ifr_data: libc::c_int, /* ifr_ifindex or ifr_mtu */ +pub struct TapTunDevice { + pub rx_buffer: [u8; 1536], + pub len: usize, + pub file: File, } -fn ifreq_new(name: &str) -> Ifreq { - let mut ifreq = Ifreq { - ifr_name: [0; libc::IF_NAMESIZE], - ifr_data: 0, - }; - // set name. - for (i, byte) in name.as_bytes().iter().enumerate() { - ifreq.ifr_name[i] = *byte as libc::c_char +impl TapTunDevice { + pub fn new_tap(ifname: &str) -> Result { + let file = open_device(ifname, true)?; + + Ok(Self { + rx_buffer: [0u8; 1536], + len: 0, + file, + }) + } + + pub fn new_tun(ifname: &str) -> Result { + let file = open_device(ifname, false)?; + + Ok(Self { + rx_buffer: [0u8; 1536], + len: 0, + file, + }) } - ifreq -} -fn ifreq_ioctl(fd: libc::c_int, cmd: libc::c_ulong, ifreq: &mut Ifreq) -> libc::c_int { - unsafe { libc::ioctl(fd, cmd as _, ifreq as *mut Ifreq) } + pub fn poll_read(&mut self) { + let len = self.file.read(&mut self.rx_buffer).unwrap(); + self.len = len; + } } -pub fn open_device(name: &str, is_tap: bool) -> Result { - let file = OpenOptions::new() - .read(true) - .write(true) - .open("/dev/net/tun")?; - let mut ifreq = ifreq_new(name); - - if is_tap { - ifreq.ifr_data = IFF_TAP | IFF_NO_PI; - } else { - ifreq.ifr_data = IFF_TUN | IFF_NO_PI; +impl Device for TapTunDevice { + fn medium(&self) -> auip::Medium { + auip::Medium::Ethernet + } + + fn recv(&mut self) -> auip::Result> { + if self.len == 0 { + Ok(None) + } else { + Ok(Some(&self.rx_buffer[..self.len])) + } } - let fd = file.as_raw_fd(); - let res = ifreq_ioctl(fd, TUNSETIFF, &mut ifreq); - if res == -1 { - Err(Error::StdIOError(std::io::Error::last_os_error())) - } else { - Ok(file) + fn send(&mut self, buffer: &[u8]) -> auip::Result<()> { + self.file.write_all(buffer).unwrap(); + Ok(()) } }