Use shared decode_xor_relayed_address in allocate_smoke (remove local duplicate)
This commit is contained in:
parent
850354781d
commit
5bbeb8fc55
@ -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<()> {
|
||||||
@ -94,12 +85,12 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
if offset + alen > total { break; }
|
if offset + alen > total { break; }
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
offset += alen;
|
offset += alen;
|
||||||
let pad = (4 - (alen % 4)) % 4;
|
let pad = (4 - (alen % 4)) % 4;
|
||||||
offset += pad;
|
offset += pad;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user