aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2022-03-05 11:04:25 +0000
committerMatthew Wild <mwild1@gmail.com>2022-03-05 11:04:25 +0000
commitd405c119185c10ee2eeb803307de144f3431f160 (patch)
tree16efceca131f3e661e4a7f108888c11527820674 /net
parentac06bd8c9728eb49f63f3db81fde727d33a316f9 (diff)
downloadprosody-d405c119185c10ee2eeb803307de144f3431f160.tar.gz
prosody-d405c119185c10ee2eeb803307de144f3431f160.zip
net.stun: Add xor-peer-address helper
Diffstat (limited to 'net')
-rw-r--r--net/stun.lua16
1 files changed, 16 insertions, 0 deletions
diff --git a/net/stun.lua b/net/stun.lua
index 80b60bda..b8e96d0e 100644
--- a/net/stun.lua
+++ b/net/stun.lua
@@ -5,6 +5,7 @@ local random = require "util.random";
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 @@ function packet_methods:_unpack_address(data, xor)
};
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'