linux's tun is ok, need fix win
This commit is contained in:
parent
5136e9427b
commit
777d3bbc63
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,4 +1,4 @@
|
||||
{
|
||||
// "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu",
|
||||
// "rust-analyzer.cargo.features": ["tun"]
|
||||
"rust-analyzer.cargo.features": ["tun"]
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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 Some(ipv4hdr) = iphdr.ipv4() else {
|
||||
debug!("ipv6 packet ignored");
|
||||
let Ok(sliced_packet) = SlicedPacket::from_ip(&data) else {
|
||||
error!("failed to parse ip packet");
|
||||
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);
|
||||
let Some(net) = sliced_packet.net else {
|
||||
error!("failed to get ip packet");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
|
||||
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,19 +753,31 @@ impl TunTapPacketHandler for Iface {
|
||||
debug!("drop tun packet due to not authed");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(transport) = sliced_packet.transport {
|
||||
match transport {
|
||||
TransportSlice::Tcp(tcp) => {
|
||||
|
||||
}
|
||||
TransportSlice::Udp(udp) => {
|
||||
//
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
@ -821,8 +844,9 @@ impl TunTapPacketHandler for Iface {
|
||||
}
|
||||
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user