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.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 entries = Arc::new(DashMap::new());
|
||||||
let res = Self {
|
let res = Self {
|
||||||
entries: entries.clone(),
|
entries: entries.clone(),
|
||||||
ttl: Duration::from_secs(60),
|
ttl: Duration::from_secs(100),
|
||||||
pending_packet_buffer: ArpWaitList::new(),
|
pending_packet_buffer: ArpWaitList::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -186,7 +186,7 @@ impl ArpTable {
|
|||||||
let ttl = res.ttl;
|
let ttl = res.ttl;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
tokio::time::sleep(Duration::from_secs(100)).await;
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
entries.retain(|_, entry| now.duration_since(entry.last_seen) < ttl);
|
entries.retain(|_, entry| now.duration_since(entry.last_seen) < ttl);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,8 @@ use bytes::BytesMut;
|
|||||||
#[cfg(feature = "tun")]
|
#[cfg(feature = "tun")]
|
||||||
use bytes::{BytesMut};
|
use bytes::{BytesMut};
|
||||||
|
|
||||||
|
#[cfg(feature = "tun")]
|
||||||
|
use etherparse::{NetSlice, PacketBuilder, SlicedPacket, TransportSlice};
|
||||||
#[cfg(not(feature = "tun"))]
|
#[cfg(not(feature = "tun"))]
|
||||||
use etherparse::{IpSlice, LinkSlice, NetSlice, SlicedPacket, TransportSlice};
|
use etherparse::{IpSlice, LinkSlice, NetSlice, SlicedPacket, TransportSlice};
|
||||||
use etherparse::{Ethernet2Header};
|
use etherparse::{Ethernet2Header};
|
||||||
@ -33,6 +35,8 @@ use tracing::{debug, error, info, warn};
|
|||||||
#[cfg(feature = "tun")]
|
#[cfg(feature = "tun")]
|
||||||
use crate::caculate_crc;
|
use crate::caculate_crc;
|
||||||
use crate::get_edge;
|
use crate::get_edge;
|
||||||
|
#[cfg(feature = "tun")]
|
||||||
|
use crate::network::parse_dns_payload;
|
||||||
#[cfg(not(feature = "tun"))]
|
#[cfg(not(feature = "tun"))]
|
||||||
use crate::network::{ARP_REPLY, ArpHdr, EthHdr, parse_dns_payload};
|
use crate::network::{ARP_REPLY, ArpHdr, EthHdr, parse_dns_payload};
|
||||||
use crate::network::{Node, send_packet_to_net};
|
use crate::network::{Node, send_packet_to_net};
|
||||||
@ -720,18 +724,25 @@ impl TunTapPacketHandler for Iface {
|
|||||||
|
|
||||||
let data = header.split_off(14);
|
let data = header.split_off(14);
|
||||||
|
|
||||||
match IpHeaders::from_slice(&data) {
|
let Ok(sliced_packet) = SlicedPacket::from_ip(&data) else {
|
||||||
Ok((iphdr, _payload)) => {
|
error!("failed to parse ip packet");
|
||||||
//use crate::network::{ArpRequestInfo, ArpResponse, send_arp_request};
|
|
||||||
|
|
||||||
let Some(ipv4hdr) = iphdr.ipv4() else {
|
|
||||||
debug!("ipv6 packet ignored");
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
let dstip = u32::from_be_bytes(ipv4hdr.0.destination);
|
let Some(net) = sliced_packet.net else {
|
||||||
debug!("packet dst ip: {:?}", ipv4hdr.0.destination);
|
error!("failed to get ip packet");
|
||||||
let src = u32::from_be_bytes(ipv4hdr.0.source);
|
return Ok(());
|
||||||
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
|
// packet should be sent to dev
|
||||||
debug!("got {} bytes from tun", data.len());
|
debug!("got {} bytes from tun", data.len());
|
||||||
if (!eee.config.allow_routing.load(Ordering::Relaxed)) && (src != eee.device_config.get_ip()) {
|
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");
|
debug!("drop tun packet due to not authed");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(transport) = sliced_packet.transport {
|
||||||
|
match transport {
|
||||||
|
TransportSlice::Tcp(tcp) => {
|
||||||
|
|
||||||
|
}
|
||||||
|
TransportSlice::Udp(udp) => {
|
||||||
|
//
|
||||||
|
|
||||||
if dstip == DNS_IP {
|
if dstip == DNS_IP {
|
||||||
// should do the dns request
|
// should do the dns request
|
||||||
// println!("request for dns");
|
// println!("request for dns");
|
||||||
let addr = format!("{}:15353", eee.server_ip);
|
|
||||||
// println!("send dns to {}", addr);
|
parse_dns_payload(eee, udp.payload(), &data, src, udp.source_port()).await;
|
||||||
if let Err(e) = eee.udp_sock_for_dns.send_to(&data, &addr).await {
|
// edge.udp_sock_for_dns.send_to()
|
||||||
error!("failed to send request to 15353: {}", e);
|
|
||||||
}
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
_other => {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match eee.arp_table.get(dstip) {
|
match eee.arp_table.get(dstip) {
|
||||||
Some(mac) => {
|
Some(mac) => {
|
||||||
|
|
||||||
let pkt_size = data.len() + 14;
|
let pkt_size = data.len() + 14;
|
||||||
let mut etherheader = Ethernet2Header::default();
|
let mut etherheader = Ethernet2Header::default();
|
||||||
etherheader.destination = mac;
|
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(())
|
Ok(())
|
||||||
@ -1062,7 +1086,6 @@ pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
|
pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
|
||||||
let res = Command::new("route")
|
let res = Command::new("route")
|
||||||
.arg("add")
|
.arg("add")
|
||||||
@ -1125,9 +1148,10 @@ pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ip = data.target_ip;
|
let ip = data.origin_ip;
|
||||||
let mac = data.target_mac.try_into().unwrap();
|
let mac = data.target_mac.try_into().unwrap();
|
||||||
|
|
||||||
|
edge.arp_table.set(ip, mac);
|
||||||
edge.arp_table.arp_arrived(ip, mac).await;
|
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);
|
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) {
|
pub fn write_arp_to_device(edge: &Node, src_mac: Mac, src_ip: u32) {
|
||||||
let dst_mac = edge.device_config.get_mac();
|
let dst_mac = edge.device_config.get_mac();
|
||||||
let dst_ip = edge.device_config.get_ip();
|
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) {
|
if let Err(_e) = edge.device.send(&data) {
|
||||||
error!("failed to write arp response to device");
|
error!("failed to write arp response to device");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user