# HG changeset patch # User Matthew Wild # Date 1646479855 0 # Node ID 0602245fc84e3517d8d6dd8a7d1377f647aecbd6 # Parent 5417ec7e2ee8684274fcc1a57d790199be133343 net.stun: Support for xor-relayed-address attribute diff -r 5417ec7e2ee8 -r 0602245fc84e net/stun.lua --- a/net/stun.lua Sat Mar 05 11:15:25 2022 +0000 +++ b/net/stun.lua Sat Mar 05 11:30:55 2022 +0000 @@ -197,13 +197,18 @@ 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 @@ 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'