Use shared decode_xor_relayed_address in allocate_smoke (remove local duplicate)

This commit is contained in:
ghost 2025-09-26 16:07:46 +02:00
parent 850354781d
commit 5bbeb8fc55

View File

@ -1,20 +1,11 @@
use bytes::BytesMut; use bytes::BytesMut;
use niom_turn::constants::*; use niom_turn::constants::*;
use niom_turn::stun;
use std::net::SocketAddr; use std::net::SocketAddr;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
use std::time::Duration; use std::time::Duration;
fn decode_xor_relayed_address_local(value: &[u8], _trans: &[u8;12]) -> Option<std::net::SocketAddr> { // Use shared decoder from library: niom_turn::stun::decode_xor_relayed_address
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))
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
@ -95,8 +86,8 @@ async fn main() -> anyhow::Result<()> {
println!("attr type=0x{:04x} len={}", atype, alen); println!("attr type=0x{:04x} len={}", atype, alen);
println!("raw: {}", hex::encode(&resp[offset..offset+alen])); println!("raw: {}", hex::encode(&resp[offset..offset+alen]));
if atype == ATTR_XOR_RELAYED_ADDRESS { if atype == ATTR_XOR_RELAYED_ADDRESS {
// XOR-RELAYED-ADDRESS: decode via local helper // XOR-RELAYED-ADDRESS: decode via shared library function
if let Some(sa) = decode_xor_relayed_address_local(&resp[offset..offset+alen], &trans) { if let Some(sa) = niom_turn::stun::decode_xor_relayed_address(&resp[offset..offset+alen], &trans) {
relay_addr_opt = Some(sa); relay_addr_opt = Some(sa);
} }
} }