From 46170feb5bbb28c00e84b32a43bcb27d70d76b3c Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 6 Mar 2026 16:21:39 +0800 Subject: [PATCH] encode_to_tcp_message, changed the packet_id to the struct, not the parameter of the function --- src/network/node.rs | 2 +- src/network/packet.rs | 5 ++--- src/network/tun_linux.rs | 28 +++++++++++++++++++++++++++- src/pb/mod.rs | 2 +- src/quic/mod.rs | 3 +-- src/tcp/quic.rs | 27 ++++++++++++++++++--------- src/tcp/tcp_codec.rs | 6 ++++-- src/utils/mod.rs | 2 +- 8 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/network/node.rs b/src/network/node.rs index d94350b..668f817 100755 --- a/src/network/node.rs +++ b/src/network/node.rs @@ -589,7 +589,7 @@ impl Node { pub async fn send_unregister_super(&self) -> Result<()> { let content = - encode_to_tcp_message::(None, 0, PacketType::UnRegisterSuper as u8).unwrap(); + encode_to_tcp_message::(None, PacketType::UnRegisterSuper as u8).unwrap(); let conn = get_quic_write_conn(); let _ = conn.send(content).await; diff --git a/src/network/packet.rs b/src/network/packet.rs index 5069601..6ea1aba 100755 --- a/src/network/packet.rs +++ b/src/network/packet.rs @@ -845,7 +845,6 @@ async fn check_identity_is_ok(eee: &Node, identity: u32, protocol: u8, port: u16 // let packet_id = edge.get_next_packet_id(); let data = encode_to_tcp_message( Some(policy_request), - 0, PacketType::PolicyRequest as u8, ) .unwrap(); @@ -1220,7 +1219,7 @@ async fn send_query_peer(eee: &Node, dst_mac: Mac) -> Result<()> { let Ok(content) = encode_to_tcp_message( Some(query), - eee.get_next_packet_id(), + // eee.get_next_packet_id(), PacketType::QueryInfo as u8, ) else { error!("failed to encode query"); @@ -1231,7 +1230,7 @@ async fn send_query_peer(eee: &Node, dst_mac: Mac) -> Result<()> { } pub async fn ping_to_sn() { - let Ok(msg) = encode_to_tcp_message::(None, 0, PacketType::Ping as u8) else { + let Ok(msg) = encode_to_tcp_message::(None, PacketType::Ping as u8) else { error!("failed to encode ping"); return; }; diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index caf50b9..8634e92 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -222,11 +222,37 @@ impl TunTapPacketHandler for Iface { return Ok(()); }; - if let Some(eth) = headers.link { + use etherparse::EtherType; + if let Some(hdr) = eth.ethernet2() { use bytes::Bytes; + if hdr.ether_type == EtherType::ARP { + use crate::network::{ARP_REPLY, ARP_REQUEST, ArpHdr}; + + let arp = ArpHdr::from_slice(&data); + match arp.opcode { + ARP_REQUEST => { + use crate::{network::ArpRequest, pb::{SdlArpRequest, encode_to_tcp_message}, tcp::get_quic_write_conn}; + let dest_ip = ((arp.dipaddr[0] as u32) << 16) + arp.dipaddr[1] as u32; + + let request = SdlArpRequest { + pkt_id: edge.get_next_packet_id(), + target_ip: dest_ip, + }; + + let req = encode_to_tcp_message(Some(request), PacketType::ArpRequest as u8).unwrap(); + let conn = get_quic_write_conn(); + debug!("sending arp request"); + let _ = conn.send(req).await; + return Ok(()); + } + _other => { + // just do the following logic + } + } + } if let Some(ip) = headers.net { match ip { diff --git a/src/pb/mod.rs b/src/pb/mod.rs index 29a01fb..0f2de3f 100755 --- a/src/pb/mod.rs +++ b/src/pb/mod.rs @@ -7,7 +7,7 @@ use sdlan_sn_rs::utils::Result; // tcp message has two-byte of size at header pub fn encode_to_tcp_message( msg: Option, - _packet_id: u32, + // _packet_id: u32, packet_type: u8, ) -> Result> { let mut raw_data = Vec::new(); diff --git a/src/quic/mod.rs b/src/quic/mod.rs index 14e22ee..ca5a50c 100644 --- a/src/quic/mod.rs +++ b/src/quic/mod.rs @@ -4,7 +4,6 @@ use std::path::Path; use std::sync::Arc; use quinn::Endpoint; -use quinn::TransportConfig; use quinn::crypto::rustls::QuicClientConfig; use rustls::crypto::CryptoProvider; use rustls::crypto::ring; @@ -12,7 +11,7 @@ use rustls::pki_types::CertificateDer; use rustls::pki_types::PrivateKeyDer; use rustls::pki_types::ServerName; -use rustls_pemfile::{certs, pkcs8_private_keys, private_key}; +use rustls_pemfile::{certs, private_key}; pub fn quic_init() -> Endpoint { let default_provider = ring::default_provider(); diff --git a/src/tcp/quic.rs b/src/tcp/quic.rs index 99b0354..d22067c 100644 --- a/src/tcp/quic.rs +++ b/src/tcp/quic.rs @@ -4,12 +4,12 @@ use futures_util::pin_mut; use prost::Message; use quinn::SendStream; use sdlan_sn_rs::{config::AF_INET, peer::{SdlanSock, V6Info}, utils::{Result, SDLanError, get_current_timestamp, ip_to_string, rsa_decrypt}}; -use tokio::{io::BufReader, net::TcpStream, sync::mpsc::{Receiver, Sender, channel}}; +use tokio::{sync::mpsc::{Receiver, Sender, channel}}; use tokio_util::sync::CancellationToken; use tracing::{debug, error}; -use crate::{ConnectionInfo, ConnectionState, config::{NULL_MAC, TCP_PING_TIME}, get_edge, network::{Node, RegisterSuperFeedback, StartStopInfo, check_peer_registration_needed, handle_packet_peer_info}, pb::{SdlPolicyRequest, SdlPolicyResponse, SdlRegisterSuper, SdlRegisterSuperAck, SdlRegisterSuperNak, SdlSendRegisterEvent, encode_to_tcp_message}, tcp::{EventType, NakMsgCode, NatType, PacketType, RuleInfo, SdlanTcp, read_a_packet, send_stun_request, set_identity_cache}}; +use crate::{ConnectionInfo, ConnectionState, config::{NULL_MAC, TCP_PING_TIME}, get_edge, network::{Node, RegisterSuperFeedback, StartStopInfo, check_peer_registration_needed, handle_packet_peer_info}, pb::{SdlArpResponse, SdlPolicyResponse, SdlRegisterSuper, SdlRegisterSuperAck, SdlRegisterSuperNak, SdlSendRegisterEvent, encode_to_tcp_message}, tcp::{EventType, NakMsgCode, NatType, PacketType, RuleInfo, SdlanTcp, read_a_packet, send_stun_request, set_identity_cache}}; static GLOBAL_QUIC_HANDLE: OnceLock = OnceLock::new(); @@ -111,7 +111,6 @@ async fn handle_tcp_message(msg: SdlanTcp) { debug!("got tcp message: {:?}", msg.packet_type); match msg.packet_type { PacketType::RegisterSuperACK => { - let Ok(ack) = SdlRegisterSuperAck::decode(&msg.current_packet[..]) else { error!("failed to decode REGISTER_SUPER_ACK"); return; @@ -143,7 +142,7 @@ async fn handle_tcp_message(msg: SdlanTcp) { let ip = ip_to_string(&edge.device_config.get_ip()); // debug!("aes key is {:?}, ip is {}/{}", aes, ip, dev.net_bit_len,); println!("assigned ip: {}", ip); - let hostname = edge.hostname.read().unwrap().clone(); + // let hostname = edge.hostname.read().unwrap().clone(); // println!("network is: {}.{}", hostname, dev.network_domain); /* edge.device_config @@ -179,6 +178,20 @@ async fn handle_tcp_message(msg: SdlanTcp) { // println!("nat type is: {:?}", nattype); }); } + PacketType::ArpResponse => { + let Ok(resp) = SdlArpResponse::decode(&msg.current_packet[..]) else { + error!("failed to decode ARP RESPONSE"); + return; + }; + if resp.target_mac.len() != 6 { + // invalid target_mac + error!("invalid target_mac"); + return; + } + + // TODO: construct the arp reply, and write to tun; + + } PacketType::PolicyReply => { let Ok(policy) = SdlPolicyResponse::decode(&msg.current_packet[..]) else { error!("failed to decode POLICY RESPONSE"); @@ -200,6 +213,7 @@ async fn handle_tcp_message(msg: SdlanTcp) { port, }); } + set_identity_cache(identity, infos); } @@ -673,14 +687,9 @@ async fn on_connected_callback(local_ip: Option, stream: &mut SendStream println!("register super: {:?}", register_super); // debug!("send register super: {:?}", register_super); - let packet_id = match pkt_id { - Some(id) => id, - None => edge.get_next_packet_id(), - }; // let packet_id = edge.get_next_packet_id(); let data = encode_to_tcp_message( Some(register_super), - packet_id, PacketType::RegisterSuper as u8, ) .unwrap(); diff --git a/src/tcp/tcp_codec.rs b/src/tcp/tcp_codec.rs index 1268df2..1ae2443 100755 --- a/src/tcp/tcp_codec.rs +++ b/src/tcp/tcp_codec.rs @@ -2,8 +2,7 @@ use std::sync::atomic::Ordering; use quinn::RecvStream; use tokio::{ - io::{AsyncReadExt, BufReader}, - net::tcp::OwnedReadHalf, + io::{AsyncReadExt}, }; use num_enum::TryFromPrimitive; @@ -96,6 +95,9 @@ pub enum PacketType { Welcome = 0x4f, + ArpRequest = 0x50, + ArpResponse = 0x51, + PolicyRequest = 0xb0, PolicyReply = 0xb1, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 62981ac..fb60be9 100755 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -6,7 +6,7 @@ pub use command::*; mod socks; use rand::Rng; -use sdlan_sn_rs::utils::{Mac, Result, SDLanError, save_to_file}; +use sdlan_sn_rs::utils::{Mac, Result, SDLanError}; use serde::{Deserialize, Serialize}; pub use socks::*;