From fa68985f24c2160d4cb2e634a0e0df857e4a666e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 5 Mar 2022 11:30:55 +0000 Subject: net.stun: Support for xor-relayed-address attribute --- net/stun.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/net/stun.lua b/net/stun.lua index b8e96d0e..2c35786f 100644 --- a/net/stun.lua +++ b/net/stun.lua @@ -197,13 +197,18 @@ function packet_methods:deserialize(bytes) return self; end -function packet_methods:get_attribute(attr_type) +function packet_methods:get_attribute(attr_type, idx) + idx = math.max(idx or 1, 1); if type(attr_type) == "string" then attr_type = assert(attribute_lookup[attr_type:lower()], "unknown attribute: "..attr_type); end for _, attribute in ipairs(self.attributes) do if struct.unpack(">I2", attribute) == attr_type then - return attribute:sub(5); + if idx == 1 then + return attribute:sub(5); + else + idx = idx - 1; + end end end end @@ -249,6 +254,19 @@ function packet_methods:add_xor_peer_address(address, port) self:add_attribute("xor-peer-address", self:_pack_address(family, parsed_ip.packed, port or 0, true)); end +function packet_methods:get_xor_relayed_address(idx) + local data = self:get_attribute("xor-relayed-address", idx); + if not data then return; end + return self:_unpack_address(data, true); +end + +function packet_methods:get_xor_relayed_addresses() + return { + self:get_xor_relayed_address(1); + self:get_xor_relayed_address(2); + }; +end + function packet_methods:add_message_integrity(key) -- Add attribute with a dummy value so we can artificially increase -- the packet 'length' -- cgit v1.2.3