diff --git a/message.proto b/message.proto index 348fe24..ddc66e6 100644 --- a/message.proto +++ b/message.proto @@ -71,7 +71,8 @@ message SDLSendRegisterEvent { bytes dst_mac = 1; uint32 nat_ip = 2; uint32 nat_port = 3; - optional SDLV6Info v6_info = 4; + uint32 nat_type = 4; + optional SDLV6Info v6_info = 5; } message SDLNetworkShutdownEvent { diff --git a/src/network/node.rs b/src/network/node.rs index 6ddd1da..afaa0d2 100644 --- a/src/network/node.rs +++ b/src/network/node.rs @@ -595,6 +595,8 @@ pub struct EdgePeer { // pub mac: Mac, pub dev_addr: IpSubnet, + pub nat_type: RwLock, + // 对端对外开放的ip和端口信息 pub sock: RwLock, // peer's ipv6 info @@ -644,6 +646,11 @@ impl EdgePeer { last_seen: AtomicU64::new(0), _last_valid_timestamp: AtomicU64::new(now), last_sent_query: AtomicU64::new(0), + nat_type: RwLock::new(NatType::Invalid), } } + + pub fn get_nat_type(&self) -> NatType { + *self.nat_type.read().unwrap() + } } diff --git a/src/network/packet.rs b/src/network/packet.rs index a168a22..ee10597 100644 --- a/src/network/packet.rs +++ b/src/network/packet.rs @@ -1,5 +1,6 @@ use std::{net::SocketAddr, sync::atomic::Ordering, time::Duration}; +use crate::tcp::NatType; use crate::{network::TunTapPacketHandler, utils::mac_to_string}; use crate::{ @@ -254,7 +255,11 @@ pub async fn handle_packet_peer_info( error!("failed to convert v4"); return Ok(()); }; - let remote_nat = v4.nat_type; + let remote_nat_byte = v4.nat_type as u8; + let remote_nat = match NatType::try_from(remote_nat_byte) { + Ok(nat) => nat, + Err(_) => NatType::Invalid, + }; let mut v6: [u8; 16] = [0; 16]; let mut v6_port = 0; @@ -277,6 +282,7 @@ pub async fn handle_packet_peer_info( v4: v4_u32, v6: [0; 16], }; + *(edgeinfo.nat_type.write().unwrap()) = remote_nat; *(edgeinfo.sock.write().unwrap()) = sock.deepcopy(); info!( "Rx PEERINFO for {}: is at {}", @@ -683,6 +689,8 @@ async fn send_register(eee: &'static Node, sock: &SdlanSock, mac: Mac, _v6_info: return; } + let self_nat = eee.get_nat_type(); + let register = SdlRegister { network_id: network_id, src_mac: Vec::from(eee.device_config.get_mac()), @@ -707,6 +715,7 @@ async fn send_register(eee: &'static Node, sock: &SdlanSock, mac: Mac, _v6_info: ) .await; } + /* let mut target_sock_v4 = sock.deepcopy(); tokio::spawn(async move { for i in 1..=10 { @@ -720,6 +729,7 @@ async fn send_register(eee: &'static Node, sock: &SdlanSock, mac: Mac, _v6_info: tokio::time::sleep(Duration::from_millis(500)).await; } }); + */ /* let key = eee.get_header_key(); if key.len() > 0 { diff --git a/src/pb/message.rs b/src/pb/message.rs index c4ec1f0..3c85a29 100644 --- a/src/pb/message.rs +++ b/src/pb/message.rs @@ -105,7 +105,9 @@ pub struct SdlSendRegisterEvent { pub nat_ip: u32, #[prost(uint32, tag = "3")] pub nat_port: u32, - #[prost(message, optional, tag = "4")] + #[prost(uint32, tag = "4")] + pub nat_type: u32, + #[prost(message, optional, tag = "5")] pub v6_info: ::core::option::Option, } #[allow(clippy::derive_partial_eq_without_eq)] diff --git a/src/tcp/tcp_codec.rs b/src/tcp/tcp_codec.rs index 355e551..e6297cc 100644 --- a/src/tcp/tcp_codec.rs +++ b/src/tcp/tcp_codec.rs @@ -44,6 +44,7 @@ pub enum NatType { PortRestricted = 3, ConeRestricted = 4, Symmetric = 5, + Invalid = 0xff, } #[derive(Debug, Copy, Clone, TryFromPrimitive)]