Skip to content

Commit

Permalink
refine code for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Oct 11, 2023
1 parent 57fe788 commit 1cc06c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
33 changes: 11 additions & 22 deletions src/platform/macos/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{
},
};
use libc::{
self, c_char, c_short, c_uint, c_void, sockaddr, socklen_t, AF_INET, IFF_RUNNING, IFF_UP,
IFNAMSIZ, SOCK_DGRAM,
self, c_char, c_short, c_uint, c_void, sockaddr, socklen_t, AF_INET, AF_SYSTEM, AF_SYS_CONTROL,
IFF_RUNNING, IFF_UP, IFNAMSIZ, PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL, UTUN_OPT_IFNAME,
};
use std::{
ffi::CStr,
Expand Down Expand Up @@ -70,8 +70,7 @@ impl Device {
}

let mut device = unsafe {
let tun = Fd::new(libc::socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL))
.map_err(|_| io::Error::last_os_error())?;
let tun = Fd::new(libc::socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL))?;

let mut info = ctl_info {
ctl_id: 0,
Expand All @@ -91,37 +90,27 @@ impl Device {
let addr = sockaddr_ctl {
sc_id: info.ctl_id,
sc_len: mem::size_of::<sockaddr_ctl>() as _,
sc_family: AF_SYSTEM,
ss_sysaddr: AF_SYS_CONTROL,
sc_family: AF_SYSTEM as _,
ss_sysaddr: AF_SYS_CONTROL as _,
sc_unit: id as c_uint,
sc_reserved: [0; 5],
};

if libc::connect(
tun.0,
&addr as *const sockaddr_ctl as *const sockaddr,
mem::size_of_val(&addr) as socklen_t,
) < 0
{
let address = &addr as *const 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());
}

let mut name = [0u8; 64];
let mut name_len: socklen_t = 64;

if libc::getsockopt(
tun.0,
SYSPROTO_CONTROL,
UTUN_OPT_IFNAME,
&mut name as *mut _ as *mut c_void,
&mut name_len as *mut socklen_t,
) < 0
{
let optval = &mut name as *mut _ as *mut c_void;
let optlen = &mut name_len as *mut socklen_t;
if libc::getsockopt(tun.0, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, optval, optlen) < 0 {
return Err(io::Error::last_os_error().into());
}

let ctl = Fd::new(libc::socket(AF_INET, SOCK_DGRAM, 0))
.map_err(|_| io::Error::last_os_error())?;
let ctl = Fd::new(libc::socket(AF_INET, SOCK_DGRAM, 0))?;

Device {
name: CStr::from_ptr(name.as_ptr() as *const c_char)
Expand Down
5 changes: 0 additions & 5 deletions src/platform/macos/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
use ioctl::*;
use libc::{c_char, c_int, c_short, c_uint, c_ushort, c_void, sockaddr, IFNAMSIZ};

pub const AF_SYS_CONTROL: c_ushort = 2;
pub const AF_SYSTEM: c_char = 32;
pub const PF_SYSTEM: c_int = AF_SYSTEM as c_int;
pub const SYSPROTO_CONTROL: c_int = 2;
pub const UTUN_OPT_IFNAME: c_int = 2;
pub const UTUN_CONTROL_NAME: &str = "com.apple.net.utun_control";

#[allow(non_camel_case_types)]
Expand Down

0 comments on commit 1cc06c8

Please sign in to comment.