From 777d3bbc639dd113f94955235f06f096093feb8d Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 15 Apr 2026 00:37:05 +0800 Subject: [PATCH] linux's tun is ok, need fix win --- .vscode/settings.json | 2 +- src/network/arp.rs | 4 +- src/network/tun_linux.rs | 196 ++++++++++++++++++++++----------------- 3 files changed, 113 insertions(+), 89 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fb0827b..442c566 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.features": ["tun"] + "rust-analyzer.cargo.features": ["tun"] } \ No newline at end of file diff --git a/src/network/arp.rs b/src/network/arp.rs index d8e4f26..eec98de 100755 --- a/src/network/arp.rs +++ b/src/network/arp.rs @@ -177,7 +177,7 @@ impl ArpTable { let entries = Arc::new(DashMap::new()); let res = Self { entries: entries.clone(), - ttl: Duration::from_secs(60), + ttl: Duration::from_secs(100), pending_packet_buffer: ArpWaitList::new(), }; @@ -186,7 +186,7 @@ impl ArpTable { let ttl = res.ttl; tokio::spawn(async move { loop { - tokio::time::sleep(Duration::from_secs(30)).await; + tokio::time::sleep(Duration::from_secs(100)).await; let now = Instant::now(); entries.retain(|_, entry| now.duration_since(entry.last_seen) < ttl); } diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index 7b5333b..7bac715 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -4,6 +4,8 @@ use bytes::BytesMut; #[cfg(feature = "tun")] use bytes::{BytesMut}; +#[cfg(feature = "tun")] +use etherparse::{NetSlice, PacketBuilder, SlicedPacket, TransportSlice}; #[cfg(not(feature = "tun"))] use etherparse::{IpSlice, LinkSlice, NetSlice, SlicedPacket, TransportSlice}; use etherparse::{Ethernet2Header}; @@ -33,6 +35,8 @@ use tracing::{debug, error, info, warn}; #[cfg(feature = "tun")] use crate::caculate_crc; use crate::get_edge; +#[cfg(feature = "tun")] +use crate::network::parse_dns_payload; #[cfg(not(feature = "tun"))] use crate::network::{ARP_REPLY, ArpHdr, EthHdr, parse_dns_payload}; use crate::network::{Node, send_packet_to_net}; @@ -720,18 +724,25 @@ impl TunTapPacketHandler for Iface { let data = header.split_off(14); - match IpHeaders::from_slice(&data) { - Ok((iphdr, _payload)) => { - //use crate::network::{ArpRequestInfo, ArpResponse, send_arp_request}; + let Ok(sliced_packet) = SlicedPacket::from_ip(&data) else { + error!("failed to parse ip packet"); + return Ok(()); + }; + let Some(net) = sliced_packet.net else { + error!("failed to get ip packet"); + return Ok(()); + }; - let Some(ipv4hdr) = iphdr.ipv4() else { - debug!("ipv6 packet ignored"); - return Ok(()); - }; - let dstip = u32::from_be_bytes(ipv4hdr.0.destination); - debug!("packet dst ip: {:?}", ipv4hdr.0.destination); - let src = u32::from_be_bytes(ipv4hdr.0.source); - debug!("packet src ip: {:?}", ipv4hdr.0.source); + + match net { + NetSlice::Ipv4(ipv4) => { + + let dstip = u32::from_be_bytes(ipv4.header().destination()); + // let dstip = u32::from_be_bytes(ipv4hdr.0.destination); + debug!("packet dst ip: {:?}", ip_to_string(&dstip)); + let src = u32::from_be_bytes(ipv4.header().source()); + //let src = u32::from_be_bytes(ipv4hdr.0.source); + debug!("packet src ip: {:?}", ip_to_string(&src)); // packet should be sent to dev debug!("got {} bytes from tun", data.len()); if (!eee.config.allow_routing.load(Ordering::Relaxed)) && (src != eee.device_config.get_ip()) { @@ -742,87 +753,100 @@ impl TunTapPacketHandler for Iface { debug!("drop tun packet due to not authed"); return Ok(()); } - if dstip == DNS_IP { - // should do the dns request - // println!("request for dns"); - let addr = format!("{}:15353", eee.server_ip); - // println!("send dns to {}", addr); - if let Err(e) = eee.udp_sock_for_dns.send_to(&data, &addr).await { - error!("failed to send request to 15353: {}", e); - } - return Ok(()); - } - match eee.arp_table.get(dstip) { - Some(mac) => { - let pkt_size = data.len() + 14; - let mut etherheader = Ethernet2Header::default(); - etherheader.destination = mac; - etherheader.ether_type = etherparse::EtherType::IPV4; - etherheader.source = src_mac; - // let mut packet = Vec::with_capacity(14 + data.len() + 4); + if let Some(transport) = sliced_packet.transport { + match transport { + TransportSlice::Tcp(tcp) => { - header.copy_from_slice(ðerheader.to_bytes()[..]); + } + TransportSlice::Udp(udp) => { + // - let crc = caculate_crc(&data); - header.unsplit(data); + if dstip == DNS_IP { + // should do the dns request + // println!("request for dns"); - - // packet.extend_from_slice(ðerheader.to_bytes()[..]); - // packet.extend_from_slice(&data); - header.extend_from_slice(&crc.to_be_bytes()); - // packet.extend_from_slice(&crc.to_be_bytes()); - - // let pkt_size = packet.len(); - // println!("sending data with mac"); - - // let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else { - let Ok(encrypted) = eee.encryptor.load().encrypt(&header) else { - error!("failed to encrypt packet request"); - return Ok(()); - }; - - let data = SdlData { - is_p2p: true, - network_id: eee.network_id.load(Ordering::Relaxed), - ttl: SDLAN_DEFAULT_TTL as u32, - src_mac: Vec::from(src_mac), - dst_mac: Vec::from(mac), - data: Bytes::from(encrypted), - session_token: eee.session_token.get(), - identity_id: eee.identity_id.load(), - }; - let msg = - encode_to_udp_message(Some(data), PacketType::Data as u8).unwrap(); - let size = msg.len(); - send_packet_to_net(eee, mac, &msg, pkt_size as u64).await; - } - None => { - header.unsplit(data); - eee.arp_table.add_to_arp_wait_list(dstip, header); - debug!( - "find ip: {:?} => {:?}", - src.to_be_bytes(), - dstip.to_be_bytes() - ); - debug!("no mac found for ip {:?}, sending arp request", dstip.to_be_bytes()); - // let _ = eee.send_arp_request(dstip, dstip).await; - - if eee.device_config.contains(&Ipv4Addr::from_bits(dstip)) { - let _ = eee.send_arp_request(dstip, dstip).await; - } else { - if let Some((_, real_ip)) = eee.route_table.lookup(dstip) { - let real_ip = u32::from_be_bytes(real_ip.octets()); - let _ = eee.send_arp_request(dstip, real_ip).await; + parse_dns_payload(eee, udp.payload(), &data, src, udp.source_port()).await; + // edge.udp_sock_for_dns.send_to() + return Ok(()); } } - + _other => { + + } } - } + match eee.arp_table.get(dstip) { + Some(mac) => { + let pkt_size = data.len() + 14; + let mut etherheader = Ethernet2Header::default(); + etherheader.destination = mac; + etherheader.ether_type = etherparse::EtherType::IPV4; + etherheader.source = src_mac; + // let mut packet = Vec::with_capacity(14 + data.len() + 4); + + header.copy_from_slice(ðerheader.to_bytes()[..]); + + let crc = caculate_crc(&data); + header.unsplit(data); + + + // packet.extend_from_slice(ðerheader.to_bytes()[..]); + // packet.extend_from_slice(&data); + header.extend_from_slice(&crc.to_be_bytes()); + // packet.extend_from_slice(&crc.to_be_bytes()); + + // let pkt_size = packet.len(); + // println!("sending data with mac"); + + // let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else { + let Ok(encrypted) = eee.encryptor.load().encrypt(&header) else { + error!("failed to encrypt packet request"); + return Ok(()); + }; + + let data = SdlData { + is_p2p: true, + network_id: eee.network_id.load(Ordering::Relaxed), + ttl: SDLAN_DEFAULT_TTL as u32, + src_mac: Vec::from(src_mac), + dst_mac: Vec::from(mac), + data: Bytes::from(encrypted), + session_token: eee.session_token.get(), + identity_id: eee.identity_id.load(), + }; + let msg = + encode_to_udp_message(Some(data), PacketType::Data as u8).unwrap(); + let size = msg.len(); + send_packet_to_net(eee, mac, &msg, pkt_size as u64).await; + } + None => { + header.unsplit(data); + eee.arp_table.add_to_arp_wait_list(dstip, header); + debug!( + "find ip: {:?} => {:?}", + src.to_be_bytes(), + dstip.to_be_bytes() + ); + debug!("no mac found for ip {:?}, sending arp request", dstip.to_be_bytes()); + // let _ = eee.send_arp_request(dstip, dstip).await; + + if eee.device_config.contains(&Ipv4Addr::from_bits(dstip)) { + let _ = eee.send_arp_request(dstip, dstip).await; + } else { + if let Some((_, real_ip)) = eee.route_table.lookup(dstip) { + let real_ip = u32::from_be_bytes(real_ip.octets()); + let _ = eee.send_arp_request(dstip, real_ip).await; + } + } + + } + } + + } } - Err(e) => { - error!("failed to parse ip packet: {}", e.to_string()); + NetSlice::Ipv6(ipv6) => { + } } Ok(()) @@ -1062,7 +1086,6 @@ pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> { Ok(()) } - pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> { let res = Command::new("route") .arg("add") @@ -1125,9 +1148,10 @@ pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) { return; } - let ip = data.target_ip; + let ip = data.origin_ip; let mac = data.target_mac.try_into().unwrap(); + edge.arp_table.set(ip, mac); edge.arp_table.arp_arrived(ip, mac).await; } @@ -1147,6 +1171,7 @@ pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) { write_arp_to_device(edge, src_mac, src_ip); } +#[cfg(not(feature="tun"))] pub fn write_arp_to_device(edge: &Node, src_mac: Mac, src_ip: u32) { let dst_mac = edge.device_config.get_mac(); let dst_ip = edge.device_config.get_ip(); @@ -1172,5 +1197,4 @@ pub fn write_arp_to_device(edge: &Node, src_mac: Mac, src_ip: u32) { if let Err(_e) = edge.device.send(&data) { error!("failed to write arp response to device"); } - } \ No newline at end of file