diff --git a/src/config.rs b/src/config.rs index 02999fe..ae55984 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,3 +16,6 @@ pub enum SDLanSNOPER { SDLanSNAddSkip, SDLanSNAdded, } + +pub const AF_INET: u8 = 2; +pub const AF_INET6: u8 = 10; diff --git a/src/packet/common.rs b/src/packet/common.rs index 876514b..07522f2 100644 --- a/src/packet/common.rs +++ b/src/packet/common.rs @@ -62,7 +62,7 @@ impl<'a> Common<'a> { Ok((common, &value[5 + id_len..])) } - fn new(id: &'a str) -> Self { + pub fn new(id: &'a str) -> Self { return Common { id, version: 1, diff --git a/src/packet/mod.rs b/src/packet/mod.rs index 57333c4..ad46f52 100644 --- a/src/packet/mod.rs +++ b/src/packet/mod.rs @@ -3,3 +3,6 @@ pub use common::*; mod register_super; pub use register_super::*; + +mod packet; +pub use packet::*; diff --git a/src/packet/packet.rs b/src/packet/packet.rs new file mode 100644 index 0000000..bbbb9bc --- /dev/null +++ b/src/packet/packet.rs @@ -0,0 +1,10 @@ +use crate::peer::SdlanSock; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct Packet<'a> { + pub src_ip: u32, + pub dst_ip: u32, + pub sock: SdlanSock, + pub data: &'a [u8], +} diff --git a/src/packet/register_super.rs b/src/packet/register_super.rs index 59ea651..9e792d6 100644 --- a/src/packet/register_super.rs +++ b/src/packet/register_super.rs @@ -49,8 +49,8 @@ mod test { sock: Some(SdlanSock { family: 0, port: 1, - has_v6: true, - v6_port: 2345, + // has_v6: true, + // v6_port: 2345, v4: [0; 4], v6: [1; 16], }), diff --git a/src/peer.rs b/src/peer.rs index 4998997..0e841c7 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -27,6 +27,8 @@ pub struct Peer { // 最近一次合法时间 pub last_valid_timestamp: AtomicU64, + pub v6_info: Mutex, + // 该节点锁属的网络 pub network_id: Mutex, } @@ -39,11 +41,15 @@ impl Peer { sock: Mutex::new(SdlanSock { family: 0, port: 0, - has_v6: false, - v6_port: 0, + // has_v6: false, + // v6_port: 0, v4: [0; 4], v6: [0; 16], }), + v6_info: Mutex::new(V6Info { + port: 0, + v6: [0; 16], + }), pub_key: Mutex::new(vec![]), timeout: 0, last_seen: AtomicU64::new(0), @@ -110,8 +116,8 @@ impl IpSubnet { pub struct SdlanSock { pub family: u8, pub port: u16, - pub has_v6: bool, - pub v6_port: u16, + // pub has_v6: bool, + // pub v6_port: u16, // pub v4: Vec, // pub v6: Vec, #[sqlx(try_from = "Vec")] @@ -120,6 +126,14 @@ pub struct SdlanSock { pub v6: [u8; 16], } +#[derive(Debug, Serialize, Deserialize, PartialEq, sqlx::FromRow)] +pub struct V6Info { + pub port: u16, + + #[sqlx(try_from = "Vec")] + pub v6: [u8; 16], +} + /* /// SdlanSock: 对端对外的ip信息,包括ipv4和ipv6 #[derive(Debug, Serialize, Deserialize, PartialEq, sqlx::FromRow)] @@ -236,8 +250,8 @@ mod test { if let Some(p3) = pm.find_peer_by_sock(&SdlanSock { family: 0, port: 20, - has_v6: true, - v6_port: 30, + // has_v6: true, + // v6_port: 30, v4: [1; 4], v6: [1; 16], }) { diff --git a/src/utils/helper.rs b/src/utils/helper.rs index 7203bfe..9214238 100644 --- a/src/utils/helper.rs +++ b/src/utils/helper.rs @@ -1,4 +1,5 @@ use super::SDLanError; +use crate::config; use dashmap::DashMap; use std::net::{IpAddr, SocketAddr}; use std::path::Path; @@ -71,15 +72,42 @@ pub fn get_sdlan_sock_from_socketaddr(addr: SocketAddr) -> Result { IpAddr::V4(ipv4) => { let v4: u32 = ipv4.into(); let res = SdlanSock { - family: 0, + family: config::AF_INET, port: port, - has_v6: false, - v6_port: 0, + // has_v6: false, + // v6_port: 0, v4: v4.to_be_bytes(), v6: [0; 16], }; Ok(res) } - IpAddr::V6(_ipv6) => Err(SDLanError::NormalError("ipv6 found")), + IpAddr::V6(ipv6) => { + let res = SdlanSock { + family: config::AF_INET6, + port: port, + v4: [0; 4], + v6: ipv6.octets(), + }; + Ok(res) + } } } + +pub fn is_broadcast(ip: u32) -> bool { + ip == 0xffffffff +} + +pub fn is_multicast(ip: u32) -> bool { + let first = ((ip >> 24) & 0xff) as u8; + first >= 224 && first <= 239 +} + +pub fn ip_to_string(ip: u32) -> String { + format!( + "{}.{}.{}.{}", + ((ip >> 24) & 0xff) as u8, + ((ip >> 16) & 0xff) as u8, + ((ip >> 8) & 0xff) as u8, + (ip & 0xff) as u8, + ) +}