From 5bbeb8fc55a51677abbcca4aa832fbdf47b13a0a Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 26 Sep 2025 16:07:46 +0200 Subject: [PATCH] Use shared decode_xor_relayed_address in allocate_smoke (remove local duplicate) --- src/bin/allocate_smoke.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/bin/allocate_smoke.rs b/src/bin/allocate_smoke.rs index 55a4a7f..1073811 100644 --- a/src/bin/allocate_smoke.rs +++ b/src/bin/allocate_smoke.rs @@ -1,20 +1,11 @@ use bytes::BytesMut; use niom_turn::constants::*; +use niom_turn::stun; use std::net::SocketAddr; use tokio::net::UdpSocket; use std::time::Duration; -fn decode_xor_relayed_address_local(value: &[u8], _trans: &[u8;12]) -> Option { - if value.len() < 8 { return None; } - if value[1] != FAMILY_IPV4 { return None; } - let xport = u16::from_be_bytes([value[2], value[3]]); - let port = xport ^ ((MAGIC_COOKIE_U32 >> 16) as u16); - let cookie_bytes = MAGIC_COOKIE_U32.to_be_bytes(); - let mut ipb = [0u8;4]; - for i in 0..4 { ipb[i] = value[4 + i] ^ cookie_bytes[i]; } - let ip = std::net::Ipv4Addr::from(ipb); - Some(std::net::SocketAddr::new(std::net::IpAddr::V4(ip), port)) -} +// Use shared decoder from library: niom_turn::stun::decode_xor_relayed_address #[tokio::main] async fn main() -> anyhow::Result<()> { @@ -94,12 +85,12 @@ async fn main() -> anyhow::Result<()> { if offset + alen > total { break; } println!("attr type=0x{:04x} len={}", atype, alen); println!("raw: {}", hex::encode(&resp[offset..offset+alen])); - if atype == ATTR_XOR_RELAYED_ADDRESS { - // XOR-RELAYED-ADDRESS: decode via local helper - if let Some(sa) = decode_xor_relayed_address_local(&resp[offset..offset+alen], &trans) { - relay_addr_opt = Some(sa); + if atype == ATTR_XOR_RELAYED_ADDRESS { + // XOR-RELAYED-ADDRESS: decode via shared library function + if let Some(sa) = niom_turn::stun::decode_xor_relayed_address(&resp[offset..offset+alen], &trans) { + relay_addr_opt = Some(sa); + } } - } offset += alen; let pad = (4 - (alen % 4)) % 4; offset += pad;