From 7e805780387944592e52ad27c16daedec7912a59 Mon Sep 17 00:00:00 2001 From: asxalex Date: Thu, 22 Feb 2024 16:20:30 +0800 Subject: [PATCH] added register packet handle --- docs/protocol.md | 10 ++++++++-- src/packet/common.rs | 4 ++++ src/packet/mod.rs | 3 +++ src/packet/register.rs | 15 +++++++++++++++ src/utils/helper.rs | 8 ++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/packet/register.rs diff --git a/docs/protocol.md b/docs/protocol.md index 963a762..b1295dd 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -63,6 +63,12 @@ sdlan协议的总体格式如下: "v4": [1,2,3,4], // 如果family为10,则v6有用,一个16字节的数组,表示ipv6地址 "v6": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] - } + }, + // data为二进制的字节数组,为aes加密之后的tun流量。 + "data": [] } -``` \ No newline at end of file +``` + +其中,`src_ip`为tun的ip数据包的来源ip,`dst_ip`为tun的ip数据包的目的ip。均为大端序的32位无符号整数。`sock`为转发时由sn填充的发送客户端的信息,发送短不需要处理。`data`为tun的数据流量,使用`network_pass`进行加密后的二进制数据。 + +在发送packet数据包之前,消息体部分的数据,为json化之后的packet使用`header_pass`进行aes加密后的二进制数据。 \ No newline at end of file diff --git a/src/packet/common.rs b/src/packet/common.rs index b6fdbe9..818cb7a 100644 --- a/src/packet/common.rs +++ b/src/packet/common.rs @@ -81,6 +81,8 @@ pub enum PacketType { PKTRegisterSuper, // 数据转发 PKTPacket, + // 打洞消息 + PKTRegister, } impl std::convert::From for PacketType { @@ -89,6 +91,7 @@ impl std::convert::From for PacketType { // 0 => Self::PacketInvalid, 1 => Self::PKTRegisterSuper, 2 => Self::PKTPacket, + 3 => Self::PKTRegister, _ => Self::PKTInvalid, } } @@ -100,6 +103,7 @@ impl PacketType { Self::PKTInvalid => 0, Self::PKTRegisterSuper => 1, Self::PKTPacket => 2, + Self::PKTRegister => 3, } } } diff --git a/src/packet/mod.rs b/src/packet/mod.rs index ad46f52..0bc9909 100644 --- a/src/packet/mod.rs +++ b/src/packet/mod.rs @@ -6,3 +6,6 @@ pub use register_super::*; mod packet; pub use packet::*; + +mod register; +pub use register::*; diff --git a/src/packet/register.rs b/src/packet/register.rs new file mode 100644 index 0000000..b1e837e --- /dev/null +++ b/src/packet/register.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +use crate::peer::{IpSubnet, SdlanSock}; + +#[derive(Serialize, Deserialize)] +pub struct Register { + // 源ip + pub src_ip: u32, + // 目的ip + pub dst_ip: u32, + // supernode转发时开到的发送者的外网ip信息 + pub sock: SdlanSock, + // 发送者的tun的ip信息 + pub dev_addr: IpSubnet, +} diff --git a/src/utils/helper.rs b/src/utils/helper.rs index a298936..00f1685 100644 --- a/src/utils/helper.rs +++ b/src/utils/helper.rs @@ -101,6 +101,14 @@ pub fn is_multicast(ip: u32) -> bool { first >= 224 && first <= 239 } +pub fn is_multi_broadcast(ip: u32) -> bool { + if ip == 0xffffffff { + return true; + } + let first = ((ip >> 24) & 0xff) as u8; + first >= 224 && first <= 239 +} + pub fn ip_to_string(ip: u32) -> String { format!( "{}.{}.{}.{}",