From 50463c825b4a9808d75c8e3b9bc1d50ffab1df7a Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Sun, 5 Nov 2023 23:18:25 +0800 Subject: [PATCH] clean code due to libc crate updated, ifreq struct added --- src/platform/macos/device.rs | 36 +++++++++--------- src/platform/macos/sys.rs | 74 +----------------------------------- 2 files changed, 19 insertions(+), 91 deletions(-) diff --git a/src/platform/macos/device.rs b/src/platform/macos/device.rs index 6af3336c..c0daf5ed 100644 --- a/src/platform/macos/device.rs +++ b/src/platform/macos/device.rs @@ -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::() as _, + sc_len: mem::size_of::() 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()); } @@ -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(), ); @@ -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 { @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/src/platform/macos/sys.rs b/src/platform/macos/sys.rs index 94412cdc..35e1d009 100644 --- a/src/platform/macos/sys.rs +++ b/src/platform/macos/sys.rs @@ -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"; @@ -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)]