added packet and changed sdlansock struct

This commit is contained in:
asxalex 2024-02-21 16:41:27 +08:00
parent 46d44e9f96
commit d915e75e12
7 changed files with 71 additions and 13 deletions

View File

@ -16,3 +16,6 @@ pub enum SDLanSNOPER {
SDLanSNAddSkip,
SDLanSNAdded,
}
pub const AF_INET: u8 = 2;
pub const AF_INET6: u8 = 10;

View File

@ -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,

View File

@ -3,3 +3,6 @@ pub use common::*;
mod register_super;
pub use register_super::*;
mod packet;
pub use packet::*;

10
src/packet/packet.rs Normal file
View File

@ -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],
}

View File

@ -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],
}),

View File

@ -27,6 +27,8 @@ pub struct Peer {
// 最近一次合法时间
pub last_valid_timestamp: AtomicU64,
pub v6_info: Mutex<V6Info>,
// 该节点锁属的网络
pub network_id: Mutex<String>,
}
@ -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<u8>,
// pub v6: Vec<u8>,
#[sqlx(try_from = "Vec<u8>")]
@ -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<u8>")]
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],
}) {

View File

@ -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<SdlanSock> {
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,
)
}