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};
|
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");
|
match net {
|
||||||
return Ok(());
|
NetSlice::Ipv4(ipv4) => {
|
||||||
};
|
|
||||||
let dstip = u32::from_be_bytes(ipv4hdr.0.destination);
|
let dstip = u32::from_be_bytes(ipv4.header().destination());
|
||||||
debug!("packet dst ip: {:?}", ipv4hdr.0.destination);
|
// let dstip = u32::from_be_bytes(ipv4hdr.0.destination);
|
||||||
let src = u32::from_be_bytes(ipv4hdr.0.source);
|
debug!("packet dst ip: {:?}", ip_to_string(&dstip));
|
||||||
debug!("packet src ip: {:?}", ipv4hdr.0.source);
|
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,87 +753,100 @@ 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 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;
|
if let Some(transport) = sliced_packet.transport {
|
||||||
let mut etherheader = Ethernet2Header::default();
|
match transport {
|
||||||
etherheader.destination = mac;
|
TransportSlice::Tcp(tcp) => {
|
||||||
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()[..]);
|
}
|
||||||
|
TransportSlice::Udp(udp) => {
|
||||||
|
//
|
||||||
|
|
||||||
let crc = caculate_crc(&data);
|
if dstip == DNS_IP {
|
||||||
header.unsplit(data);
|
// should do the dns request
|
||||||
|
// println!("request for dns");
|
||||||
|
|
||||||
|
parse_dns_payload(eee, udp.payload(), &data, src, udp.source_port()).await;
|
||||||
// packet.extend_from_slice(ðerheader.to_bytes()[..]);
|
// edge.udp_sock_for_dns.send_to()
|
||||||
// packet.extend_from_slice(&data);
|
return Ok(());
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_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) => {
|
NetSlice::Ipv6(ipv6) => {
|
||||||
error!("failed to parse ip packet: {}", e.to_string());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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