encode_to_tcp_message, changed the packet_id to the struct, not the parameter of the function
This commit is contained in:
parent
31845c6707
commit
46170feb5b
@ -589,7 +589,7 @@ impl Node {
|
||||
|
||||
pub async fn send_unregister_super(&self) -> Result<()> {
|
||||
let content =
|
||||
encode_to_tcp_message::<SdlEmpty>(None, 0, PacketType::UnRegisterSuper as u8).unwrap();
|
||||
encode_to_tcp_message::<SdlEmpty>(None, PacketType::UnRegisterSuper as u8).unwrap();
|
||||
|
||||
let conn = get_quic_write_conn();
|
||||
let _ = conn.send(content).await;
|
||||
|
||||
@ -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::<SdlEmpty>(None, 0, PacketType::Ping as u8) else {
|
||||
let Ok(msg) = encode_to_tcp_message::<SdlEmpty>(None, PacketType::Ping as u8) else {
|
||||
error!("failed to encode ping");
|
||||
return;
|
||||
};
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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<T: Message>(
|
||||
msg: Option<T>,
|
||||
_packet_id: u32,
|
||||
// _packet_id: u32,
|
||||
packet_type: u8,
|
||||
) -> Result<Vec<u8>> {
|
||||
let mut raw_data = Vec::new();
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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<ReadWriterHandle> = 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<IpAddr>, 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();
|
||||
|
||||
@ -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,
|
||||
|
||||
|
||||
@ -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::*;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user