Enable IPv4 configuration on macOS
This enables getting and setting the IPv4 address on tun interfaces on macOS
This commit is contained in:
parent
101470d17c
commit
8007e88b53
2 changed files with 88 additions and 21 deletions
|
|
@ -1,15 +1,16 @@
|
||||||
use fehler::throws;
|
use fehler::throws;
|
||||||
use libc::c_char;
|
use libc::c_char;
|
||||||
use std::io::Error;
|
use socket2::{Domain, SockAddr, Socket, Type};
|
||||||
use std::net::Ipv4Addr;
|
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||||
use std::os::fd::AsRawFd;
|
use std::os::fd::{AsRawFd, RawFd};
|
||||||
|
use std::{io::Error, mem};
|
||||||
|
|
||||||
mod kern_control;
|
mod kern_control;
|
||||||
mod sys;
|
mod sys;
|
||||||
|
|
||||||
pub use super::queue::TunQueue;
|
pub use super::queue::TunQueue;
|
||||||
|
|
||||||
use super::ifname_to_string;
|
use super::{ifname_to_string, string_to_ifname};
|
||||||
use kern_control::SysControlSocket;
|
use kern_control::SysControlSocket;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -53,12 +54,33 @@ impl TunInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[throws]
|
#[throws]
|
||||||
pub fn set_ipv4_addr(&self, _addr: Ipv4Addr) {
|
fn ifreq(&self) -> sys::ifreq {
|
||||||
todo!()
|
let mut iff: sys::ifreq = unsafe { mem::zeroed() };
|
||||||
|
iff.ifr_name = string_to_ifname(&self.name()?);
|
||||||
|
iff
|
||||||
|
}
|
||||||
|
|
||||||
|
#[throws]
|
||||||
|
pub fn set_ipv4_addr(&self, addr: Ipv4Addr) {
|
||||||
|
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||||
|
|
||||||
|
let mut iff = self.ifreq()?;
|
||||||
|
iff.ifr_ifru.ifru_addr = unsafe { *addr.as_ptr() };
|
||||||
|
|
||||||
|
self.perform(|fd| unsafe { sys::if_set_addr(fd, &iff) })?;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[throws]
|
#[throws]
|
||||||
pub fn ipv4_addr(&self) -> Ipv4Addr {
|
pub fn ipv4_addr(&self) -> Ipv4Addr {
|
||||||
todo!()
|
let mut iff = self.ifreq()?;
|
||||||
|
self.perform(|fd| unsafe { sys::if_get_addr(fd, &mut iff) })?;
|
||||||
|
let addr = unsafe { *(&iff.ifr_ifru.ifru_addr as *const _ as *const sys::sockaddr_in) };
|
||||||
|
Ipv4Addr::from(u32::from_be(addr.sin_addr.s_addr))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[throws]
|
||||||
|
fn perform<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
|
||||||
|
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
|
||||||
|
perform(socket.as_raw_fd())?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
pub use libc::{c_void, socklen_t, SYSPROTO_CONTROL, IFNAMSIZ, sockaddr_ctl, AF_SYSTEM, AF_SYS_CONTROL};
|
use std::mem;
|
||||||
use nix::ioctl_readwrite;
|
|
||||||
|
use libc::{c_char, c_int, c_short, c_uint, c_ulong, sockaddr};
|
||||||
|
pub use libc::{
|
||||||
|
c_void, sockaddr_ctl, sockaddr_in, socklen_t, AF_SYSTEM, AF_SYS_CONTROL, IFNAMSIZ,
|
||||||
|
SYSPROTO_CONTROL,
|
||||||
|
};
|
||||||
|
use nix::{ioctl_read_bad, ioctl_readwrite, ioctl_write_ptr_bad, request_code_write};
|
||||||
|
|
||||||
pub const UTUN_CONTROL_NAME: &str = "com.apple.net.utun_control";
|
pub const UTUN_CONTROL_NAME: &str = "com.apple.net.utun_control";
|
||||||
pub const UTUN_OPT_IFNAME: libc::c_int = 2;
|
pub const UTUN_OPT_IFNAME: libc::c_int = 2;
|
||||||
|
|
@ -7,29 +13,68 @@ pub const UTUN_OPT_IFNAME: libc::c_int = 2;
|
||||||
pub const MAX_KCTL_NAME: usize = 96;
|
pub const MAX_KCTL_NAME: usize = 96;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct ctl_info {
|
pub struct ctl_info {
|
||||||
pub ctl_id: u32,
|
pub ctl_id: u32,
|
||||||
pub ctl_name: [u8; MAX_KCTL_NAME],
|
pub ctl_name: [u8; MAX_KCTL_NAME],
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct ifreq {}
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct ifkpi {
|
||||||
|
pub ifk_module_id: c_uint,
|
||||||
|
pub ifk_type: c_uint,
|
||||||
|
pub ifk_ptr: *mut c_void,
|
||||||
|
}
|
||||||
|
|
||||||
ioctl_readwrite!(resolve_ctl_info, b'N', 3, ctl_info);
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct ifdevmtu {
|
||||||
|
pub ifdm_current: c_int,
|
||||||
|
pub ifdm_min: c_int,
|
||||||
|
pub ifdm_max: c_int,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub union ifr_ifru {
|
||||||
|
pub ifru_addr: sockaddr,
|
||||||
|
pub ifru_dstaddr: sockaddr,
|
||||||
|
pub ifru_broadaddr: sockaddr,
|
||||||
|
pub ifru_flags: c_short,
|
||||||
|
pub ifru_metric: c_int,
|
||||||
|
pub ifru_mtu: c_int,
|
||||||
|
pub ifru_phys: c_int,
|
||||||
|
pub ifru_media: c_int,
|
||||||
|
pub ifru_intval: c_int,
|
||||||
|
pub ifru_data: *mut c_char,
|
||||||
|
pub ifru_devmtu: ifdevmtu,
|
||||||
|
pub ifru_kpi: ifkpi,
|
||||||
|
pub ifru_wake_flags: u32,
|
||||||
|
pub ifru_route_refcnt: u32,
|
||||||
|
pub ifru_cap: [c_int; 2],
|
||||||
|
pub ifru_functional_type: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct ifreq {
|
||||||
|
pub ifr_name: [c_char; IFNAMSIZ],
|
||||||
|
pub ifr_ifru: ifr_ifru,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const SIOCSIFADDR: c_ulong = request_code_write!(b'i', 12, mem::size_of::<ifreq>());
|
||||||
|
|
||||||
/// Copied from https://github.com/rust-lang/socket2/blob/61314a231f73964b3db969ef72c0e9479df320f3/src/sys/unix.rs#L168-L178
|
|
||||||
/// getsockopt is not exposed by socket2
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! syscall {
|
macro_rules! syscall {
|
||||||
($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
|
($call: ident ( $($arg: expr),* $(,)* ) ) => {{
|
||||||
#[allow(unused_unsafe)]
|
match unsafe { ::libc::$call($($arg, )*) } {
|
||||||
let res = unsafe { libc::$fn($($arg, )*) };
|
-1 => Err(::std::io::Error::last_os_error()),
|
||||||
if res == -1 {
|
res => Ok(res),
|
||||||
Err(std::io::Error::last_os_error())
|
|
||||||
} else {
|
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use syscall;
|
pub use syscall;
|
||||||
|
|
||||||
|
ioctl_readwrite!(resolve_ctl_info, b'N', 3, ctl_info);
|
||||||
|
ioctl_read_bad!(if_get_addr, libc::SIOCGIFADDR, ifreq);
|
||||||
|
ioctl_write_ptr_bad!(if_set_addr, SIOCSIFADDR, ifreq);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue