Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
tiannian committed Oct 22, 2022
1 parent 93368f5 commit 993edf3
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 123 deletions.
4 changes: 2 additions & 2 deletions auip/src/interface/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
10 changes: 1 addition & 9 deletions auip/src/storage/dynamic/addrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cidr>,
}

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
Expand Down
11 changes: 2 additions & 9 deletions auip/src/storage/dynamic/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ use auip_pkt::{layer2, layer3};

use crate::{ArpStorage, Result};

#[derive(Debug, Default)]
pub struct Arp {
pub map: BTreeMap<layer3::ipv4::Address, layer2::Address>,
}

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);
Ok(())
}

fn mac_addr(&self, ip_addr: &layer3::ipv4::Address) -> Result<Option<layer2::Address>> {
Ok(self.map.get(ip_addr).map(|a| *a))
Ok(self.map.get(ip_addr).copied())
}
}
2 changes: 1 addition & 1 deletion pkt/src/layer2/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Address {
}

pub fn parse(s: &str) -> Result<Self> {
let mut segments = s.split(":");
let mut segments = s.split(':');

let mut inner = [0u8; 6];

Expand Down
4 changes: 2 additions & 2 deletions pkt/src/layer3/ipv4/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ impl Address {
}

pub fn parse(s: &str) -> Result<Self> {
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::<u8>()?;
} else {
return Err(Error::ParseIpv4AddressFailed);
}
Expand Down
4 changes: 2 additions & 2 deletions pkt/src/layer3/ipv4/cidr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ impl Cidr {
}

pub fn parse(s: &str) -> Result<Self> {
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::<u8>()?;

Ok(Self {
address,
Expand Down
63 changes: 4 additions & 59 deletions taptun/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
let file = open_device(ifname, true)?;

Ok(Self {
rx_buffer: [0u8; 1536],
len: 0,
file,
})
}

pub fn new_tun(ifname: &str) -> Result<Self> {
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<Option<&[u8]>> {
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::*;
52 changes: 52 additions & 0 deletions taptun/src/open.rs
Original file line number Diff line number Diff line change
@@ -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<File> {
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)
}
}
86 changes: 47 additions & 39 deletions taptun/src/taptun.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
let file = open_device(ifname, true)?;

Ok(Self {
rx_buffer: [0u8; 1536],
len: 0,
file,
})
}

pub fn new_tun(ifname: &str) -> Result<Self> {
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<File> {
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<Option<&[u8]>> {
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(())
}
}

0 comments on commit 993edf3

Please sign in to comment.