From 150d1c8e67dc4c6d428fdfacac041dace05eeb0c Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 13 Apr 2026 19:32:39 +0800 Subject: [PATCH] fix windows's read tun, fill first 14 bytes with 0's --- .vscode/settings.json | 2 +- src/lib.rs | 3 +++ src/network/async_main.rs | 6 +++--- src/network/tun_linux.rs | 17 ++--------------- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9eb94d8..fb0827b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", + // "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", // "rust-analyzer.cargo.features": ["tun"] } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a0dcadd..1cc6e88 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,9 @@ use sdlan_sn_rs::{ utils::{create_or_load_uuid, get_sdlan_sock_from_socketaddr, Result, SDLanError}, }; +#[cfg(all(feature = "tun", target_os = "windows"))] +compile_error!("tun feature is not supported on windows"); + #[derive(Clone)] pub enum ConnectionInfo { diff --git a/src/network/async_main.rs b/src/network/async_main.rs index d8e1ee2..aefdab9 100755 --- a/src/network/async_main.rs +++ b/src/network/async_main.rs @@ -255,7 +255,7 @@ async fn loop_tap(eee: &'static Node, cancel: CancellationToken) { error!("loop_tap exited"); } -#[cfg(feature = "tun")] +#[cfg(any(feature = "tun", target_os = "windows"))] fn get_data_from_tun_with_layer2_zeroed(eee: &Node) -> BytesMut { let mut temp = BytesMut::zeroed(1514); // let mut temp = BytesMut::with_capacity(1514); @@ -282,9 +282,9 @@ fn get_data_from_tap_with_layer2(eee: &Node) -> BytesMut { async fn get_tun_flow(eee: &'static Node, tx: Sender) { loop { let buf = tokio::task::spawn_blocking(|| { - #[cfg(feature = "tun")] + #[cfg(any(feature = "tun", target_os = "windows"))] let data = get_data_from_tun_with_layer2_zeroed(eee); - #[cfg(not(feature = "tun"))] + #[cfg(all(not(feature = "tun"), not(target_os="windows")))] let data = get_data_from_tap_with_layer2(eee); data diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index 7d33de8..84d6e1b 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -328,7 +328,7 @@ impl TunTapPacketHandler for Iface { if u32::from_be_bytes(ipv4.destination) == DNS_IP { // should send to dns - parse_dns_payload(edge, &headers.payload); + parse_dns_payload(edge, &headers.payload.slice()); if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await { error!("failed to send request to 15353: {}", e); } @@ -1029,18 +1029,5 @@ pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) { } } -/* fn parse_dns_payload(edge: &Node, payload: &[u8]) { - match dns_parse::parse(Bytes::from(payload)) { - Ok(packet) => { - packet[0]. - if let Some(questions) = packet.questions.first() { - debug!("got dns query for {:?}", questions.qname); - } - } - Err(e) => { - error!("failed to parse dns packet: {}", e.to_string()); - } - } -} -*/ \ No newline at end of file +} \ No newline at end of file