Software /
code /
prosody
Changeset
12371:9a8b0c5b4b14
net.stun: Add xor-peer-address helper
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 05 Mar 2022 11:04:25 +0000 |
parents | 12370:9889b1815d31 |
children | 12372:1ba451c10f41 |
files | net/stun.lua |
diffstat | 1 files changed, 16 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/net/stun.lua Sat Mar 05 11:03:44 2022 +0000 +++ b/net/stun.lua Sat Mar 05 11:04:25 2022 +0000 @@ -5,6 +5,7 @@ local struct = require "util.struct"; local bit32 = require"util.bitcompat"; local sxor = require"util.strbitop".sxor; +local new_ip = require "util.ip".new_ip; --- Public helpers @@ -221,18 +222,33 @@ }; end +function packet_methods:_pack_address(family, addr, port, xor) + if xor then + port = bit32.bxor(port, 0x2112); + addr = sxor(addr, magic_cookie..self.transaction_id); + end + local family_port = struct.pack("x>BI2", family, port); + return family_port..addr +end function packet_methods:get_mapped_address() local data = self:get_attribute("mapped-address"); if not data then return; end return self:_unpack_address(data, false); end + function packet_methods:get_xor_mapped_address() local data = self:get_attribute("xor-mapped-address"); if not data then return; end return self:_unpack_address(data, true); end +function packet_methods:add_xor_peer_address(address, port) + local parsed_ip = assert(new_ip(address)); + local family = assert(addr_family_lookup[parsed_ip.proto], "Unknown IP address family: "..parsed_ip.proto); + self:add_attribute("xor-peer-address", self:_pack_address(family, parsed_ip.packed, port or 0, true)); +end + function packet_methods:add_message_integrity(key) -- Add attribute with a dummy value so we can artificially increase -- the packet 'length'