From 066e655c301acc0093487965c3ac0223978716a9 Mon Sep 17 00:00:00 2001 From: lu <1791778603@qq.com> Date: Tue, 12 Mar 2024 23:52:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmacos=E4=B8=8A=E5=B7=B2?= =?UTF-8?q?=E7=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/handle/tun_tap/tun_handler.rs | 9 +++++++++ vnt/tun/src/macos/device.rs | 9 +++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/vnt/src/handle/tun_tap/tun_handler.rs b/vnt/src/handle/tun_tap/tun_handler.rs index c849309..3e50361 100644 --- a/vnt/src/handle/tun_tap/tun_handler.rs +++ b/vnt/src/handle/tun_tap/tun_handler.rs @@ -83,11 +83,20 @@ pub fn start( mut up_counter: SingleU64Adder, ) -> io::Result<()> { let worker = { + #[cfg(target_os = "macos")] + let current_device = current_device.clone(); let device = device.clone(); stop_manager.add_listener("tun_device".into(), move || { if let Err(e) = device.shutdown() { log::warn!("{:?}", e); } + #[cfg(target_os = "macos")] + { + let ip = current_device.load().virtual_ip; + if let Ok(udp) = std::net::UdpSocket::bind("0.0.0.0:0"){ + let _ = udp.send_to(b"stop",format!("{:?}:1234",ip)); + } + } })? }; if parallel > 1 { diff --git a/vnt/tun/src/macos/device.rs b/vnt/tun/src/macos/device.rs index 66bcbbc..5b971af 100644 --- a/vnt/tun/src/macos/device.rs +++ b/vnt/tun/src/macos/device.rs @@ -235,7 +235,7 @@ impl IFace for Device { } fn shutdown(&self) -> io::Result<()> { - self.enabled(false) + Ok(()) } fn set_ip(&self, address: Ipv4Addr, mask: Ipv4Addr) -> io::Result<()> { @@ -281,6 +281,11 @@ impl IFace for Device { } fn write(&self, buf: &[u8]) -> io::Result { - self.tun.write(buf) + let mut packet = Vec::::with_capacity(4 + buf.len()); + packet.push(0); + packet.push(0); + packet.extend_from_slice(&(libc::PF_INET as u16).to_be_bytes()); + packet.extend_from_slice(buf); + self.tun.write(&packet) } }