Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
due to libc crate updated, ifreq struct added
  • Loading branch information
ssrlive committed Nov 5, 2023
1 parent e762c41 commit 50463c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 91 deletions.
36 changes: 18 additions & 18 deletions src/platform/macos/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ impl Device {
return Err(io::Error::last_os_error().into());
}

let addr = sockaddr_ctl {
let addr = libc::sockaddr_ctl {
sc_id: info.ctl_id,
sc_len: mem::size_of::<sockaddr_ctl>() as _,
sc_len: mem::size_of::<libc::sockaddr_ctl>() as _,
sc_family: AF_SYSTEM as _,
ss_sysaddr: AF_SYS_CONTROL as _,
sc_unit: id as c_uint,
sc_reserved: [0; 5],
};

let address = &addr as *const sockaddr_ctl as *const sockaddr;
let address = &addr as *const libc::sockaddr_ctl as *const sockaddr;
if libc::connect(tun.0, address, mem::size_of_val(&addr) as socklen_t) < 0 {
return Err(io::Error::last_os_error().into());
}
Expand Down Expand Up @@ -133,11 +133,11 @@ impl Device {

/// Prepare a new request.
/// # Safety
pub unsafe fn request(&self) -> ifreq {
let mut req: ifreq = mem::zeroed();
pub unsafe fn request(&self) -> libc::ifreq {
let mut req: libc::ifreq = mem::zeroed();
ptr::copy_nonoverlapping(
self.name.as_ptr() as *const c_char,
req.ifrn.name.as_mut_ptr(),
req.ifr_name.as_mut_ptr(),
self.name.len(),
);

Expand Down Expand Up @@ -228,9 +228,9 @@ impl D for Device {
}

if value {
req.ifru.flags |= (IFF_UP | IFF_RUNNING) as c_short;
req.ifr_ifru.ifru_flags |= (IFF_UP | IFF_RUNNING) as c_short;
} else {
req.ifru.flags &= !(IFF_UP as c_short);
req.ifr_ifru.ifru_flags &= !(IFF_UP as c_short);
}

if siocsifflags(self.ctl.as_raw_fd(), &req) < 0 {
Expand All @@ -249,14 +249,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.addr).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_addr).map(Into::into)
}
}

fn set_address(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.addr = SockAddr::from(value).into();
req.ifr_ifru.ifru_addr = SockAddr::from(value).into();

if siocsifaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -274,14 +274,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.dstaddr).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_dstaddr).map(Into::into)
}
}

fn set_destination(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.dstaddr = SockAddr::from(value).into();
req.ifr_ifru.ifru_dstaddr = SockAddr::from(value).into();

if siocsifdstaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -299,14 +299,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::new(&req.ifru.broadaddr).map(Into::into)
SockAddr::new(&req.ifr_ifru.ifru_broadaddr).map(Into::into)
}
}

fn set_broadcast(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.broadaddr = SockAddr::from(value).into();
req.ifr_ifru.ifru_broadaddr = SockAddr::from(value).into();

if siocsifbrdaddr(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -324,14 +324,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

SockAddr::unchecked(&req.ifru.addr).map(Into::into)
SockAddr::unchecked(&req.ifr_ifru.ifru_addr).map(Into::into)
}
}

fn set_netmask(&mut self, value: Ipv4Addr) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.addr = SockAddr::from(value).into();
req.ifr_ifru.ifru_addr = SockAddr::from(value).into();

if siocsifnetmask(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand All @@ -349,14 +349,14 @@ impl D for Device {
return Err(io::Error::last_os_error().into());
}

Ok(req.ifru.mtu)
Ok(req.ifr_ifru.ifru_mtu)
}
}

fn set_mtu(&mut self, value: i32) -> Result<()> {
unsafe {
let mut req = self.request();
req.ifru.mtu = value;
req.ifr_ifru.ifru_mtu = value;

if siocsifmtu(self.ctl.as_raw_fd(), &req) < 0 {
return Err(io::Error::last_os_error().into());
Expand Down
74 changes: 1 addition & 73 deletions src/platform/macos/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! Bindings to internal macOS stuff.

use ioctl::*;
use libc::{c_char, c_int, c_short, c_uint, c_ushort, c_void, sockaddr, IFNAMSIZ};
use libc::{c_char, c_uint, ifreq, sockaddr, IFNAMSIZ};

pub const UTUN_CONTROL_NAME: &str = "com.apple.net.utun_control";

Expand All @@ -27,78 +27,6 @@ pub struct ctl_info {
pub ctl_name: [c_char; 96],
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct sockaddr_ctl {
pub sc_len: c_char,
pub sc_family: c_char,
pub ss_sysaddr: c_ushort,
pub sc_id: c_uint,
pub sc_unit: c_uint,
pub sc_reserved: [c_uint; 5],
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ifrn {
pub name: [c_char; IFNAMSIZ],
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifdevmtu {
pub current: c_int,
pub min: c_int,
pub max: c_int,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ifku {
pub ptr: *mut c_void,
pub value: c_int,
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifkpi {
pub module_id: c_uint,
pub type_: c_uint,
pub ifku: ifku,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub union ifru {
pub addr: sockaddr,
pub dstaddr: sockaddr,
pub broadaddr: sockaddr,

pub flags: c_short,
pub metric: c_int,
pub mtu: c_int,
pub phys: c_int,
pub media: c_int,
pub intval: c_int,
pub data: *mut c_void,
pub devmtu: ifdevmtu,
pub wake_flags: c_uint,
pub route_refcnt: c_uint,
pub cap: [c_int; 2],
pub functional_type: c_uint,
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifreq {
pub ifrn: ifrn,
pub ifru: ifru,
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Copy, Clone)]
Expand Down

0 comments on commit 50463c8

Please sign in to comment.