From b51c78bdfbde84eecce13dd96aad5943c10992f5 Mon Sep 17 00:00:00 2001 From: asxalex Date: Wed, 13 Nov 2024 19:24:33 +0800 Subject: [PATCH] guess port at -10 to 10 --- .cargo/config.toml | 2 ++ .gitignore | 5 ++++- Makefile | 10 ++++++++-- src/network/async_main.rs | 10 +++++----- src/network/packet.rs | 34 ++++++++++++++++++++++++---------- 5 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..7d7cf07 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.x86_64-unknown-linux-musl] +linker = "x86_64-linux-musl-gcc" diff --git a/.gitignore b/.gitignore index 5b68cf1..faa8f9f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ .client .output sdlan -sdlan.exe \ No newline at end of file +sdlan.exe +*.bak +*.a +*.o diff --git a/Makefile b/Makefile index 4a2d3e8..0e0ac3b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ -linux: +self: RUSTFLAGS="-L ." cargo build --features "tap" --release +linux: + RUSTFLAGS="-L ." cargo build --features "tap" --release --target x86_64-unknown-linux-musl + linux-tun: RUSTFLAGS="-L ." cargo build --release @@ -12,4 +15,7 @@ pb: mv src/pb/_.rs src/pb/message.rs libtun-so: - cd src/network && gcc -fPIC -shared -o libtuntap.so tuntap.c && cd - \ No newline at end of file + cd src/network && gcc -fPIC -shared -o libtuntap.so tuntap.c && cd - + +libtun-a: + cd src/network && gcc -c tuntap.c && ar rcs libtuntap.a tuntap.o && cd - \ No newline at end of file diff --git a/src/network/async_main.rs b/src/network/async_main.rs index ad24f6d..fd63915 100644 --- a/src/network/async_main.rs +++ b/src/network/async_main.rs @@ -189,7 +189,7 @@ async fn handle_tcp_message(msg: SdlanTcp) { async fn handle_tcp_command(_edge: &Node, _cmdtype: u8, _cmdprotobuf: &[u8]) {} -async fn handle_tcp_event(edge: &Node, eventtype: EventType, eventprotobuf: &[u8]) { +async fn handle_tcp_event(edge: &'static Node, eventtype: EventType, eventprotobuf: &[u8]) { match eventtype { EventType::SendRegister => { let Ok(reg) = SdlSendRegisterEvent::decode(eventprotobuf) else { @@ -459,7 +459,7 @@ async fn send_stun_request(eee: &Node) { } } -pub async fn loop_socket_v6(eee: &Node, socket: Arc, cancel: CancellationToken) { +pub async fn loop_socket_v6(eee: &'static Node, socket: Arc, cancel: CancellationToken) { debug!("loop sock v6"); loop { tokio::select! { @@ -487,7 +487,7 @@ pub async fn loop_socket_v6(eee: &Node, socket: Arc, cancel: Cancellatio } pub async fn loop_socket_v4( - eee: &Node, + eee: &'static Node, socket: &Socket, cancel: CancellationToken, is_multicast_sock: bool, @@ -603,7 +603,7 @@ async fn edge_send_packet_to_net(eee: &Node, data: Vec) { } } -pub async fn send_packet_to_net(eee: &Node, dst_mac: Mac, pkt: &[u8], size: u64) { +pub async fn send_packet_to_net(eee: &'static Node, dst_mac: Mac, pkt: &[u8], size: u64) { let (dest_sock, is_p2p) = find_peer_destination(eee, dst_mac).await; if is_p2p { eee.stats.tx_p2p.fetch_add(size, Ordering::Relaxed); @@ -619,7 +619,7 @@ pub async fn send_packet_to_net(eee: &Node, dst_mac: Mac, pkt: &[u8], size: u64) } } -async fn find_peer_destination(eee: &Node, dst_mac: Mac) -> (SdlanSock, bool) { +async fn find_peer_destination(eee: &'static Node, dst_mac: Mac) -> (SdlanSock, bool) { if is_multi_broadcast(&dst_mac) { return ( eee.config.super_nodes[eee.config.super_node_index.load(Ordering::Relaxed) as usize] diff --git a/src/network/packet.rs b/src/network/packet.rs index 3f74f74..a168a22 100644 --- a/src/network/packet.rs +++ b/src/network/packet.rs @@ -29,7 +29,7 @@ use tracing::{debug, error, info}; use super::{EdgePeer, Node}; pub async fn read_and_parse_packet( - eee: &Node, + eee: &'static Node, sock: &Socket, timeout: Option, _is_multicast_sock: bool, @@ -86,7 +86,7 @@ pub async fn read_and_parse_packet( Ok(()) } -pub async fn handle_packet(eee: &Node, addr: SocketAddr, buf: &[u8]) -> Result<()> { +pub async fn handle_packet(eee: &'static Node, addr: SocketAddr, buf: &[u8]) -> Result<()> { if buf.len() < 1 { return Err(SDLanError::NormalError("buf length error")); } @@ -228,7 +228,7 @@ pub async fn handle_packet(eee: &Node, addr: SocketAddr, buf: &[u8]) -> Result<( } pub async fn handle_packet_peer_info( - eee: &Node, + eee: &'static Node, // cmn: Common<'_>, body: &[u8], //sender_sock: &SdlanSock, @@ -254,6 +254,7 @@ pub async fn handle_packet_peer_info( error!("failed to convert v4"); return Ok(()); }; + let remote_nat = v4.nat_type; let mut v6: [u8; 16] = [0; 16]; let mut v6_port = 0; @@ -411,7 +412,7 @@ async fn handle_packet_register_ack( } async fn handle_packet_register( - eee: &Node, + eee: &'static Node, // cmn: Common<'_>, body: &[u8], from_sn: bool, @@ -513,7 +514,7 @@ async fn handle_packet_packet( */ pub async fn check_peer_registration_needed( - eee: &Node, + eee: &'static Node, from_sn: bool, src_mac: Mac, _v6_info: &Option, @@ -555,7 +556,7 @@ pub async fn check_peer_registration_needed( } async fn check_known_peer_sock_change( - eee: &Node, + eee: &'static Node, from_sn: bool, mac: Mac, // v6_info: &Option, @@ -592,7 +593,7 @@ async fn check_known_peer_sock_change( } async fn register_with_new_peer( - eee: &Node, + eee: &'static Node, from_sn: bool, mac: Mac, v6_info: &Option, @@ -667,11 +668,11 @@ async fn register_with_new_peer( } } -async fn register_with_local_peers(eee: &Node) { +async fn register_with_local_peers(eee: &'static Node) { send_register(eee, &eee.multicast_sock, BROADCAST_MAC, &None).await; } -async fn send_register(eee: &Node, sock: &SdlanSock, mac: Mac, _v6_info: &Option) { +async fn send_register(eee: &'static Node, sock: &SdlanSock, mac: Mac, _v6_info: &Option) { if !eee.config.allow_p2p { debug!("skipping register as p2p is disabled"); return; @@ -706,6 +707,19 @@ async fn send_register(eee: &Node, sock: &SdlanSock, mac: Mac, _v6_info: &Option ) .await; } + let mut target_sock_v4 = sock.deepcopy(); + tokio::spawn(async move { + for i in 1..=10 { + target_sock_v4.port += i; + let _ = send_to_sock(eee, &msg, &target_sock_v4).await; + tokio::time::sleep(Duration::from_millis(500)).await; + } + for i in 1..=10 { + target_sock_v4.port -= i; + let _ = send_to_sock(eee, &msg, &target_sock_v4).await; + tokio::time::sleep(Duration::from_millis(500)).await; + } + }); /* let key = eee.get_header_key(); if key.len() > 0 { @@ -985,7 +999,7 @@ fn peer_set_p2p_confirmed(eee: &Node, src_mac: Mac, sender_sock: &SdlanSock) { eee.known_peers.insert_peer(src_mac, scan); } -pub async fn check_query_peer_info(eee: &Node, mac: Mac) { +pub async fn check_query_peer_info(eee: &'static Node, mac: Mac) { let scan: Arc; let now = get_current_timestamp(); match eee.pending_peers.get_peer(&mac) {