net.stun: Support for xor-relayed-address attribute

This commit is contained in:
Matthew Wild 2022-03-05 11:30:55 +00:00
parent 9df580f997
commit fa68985f24

View file

@ -197,13 +197,18 @@ function packet_methods:deserialize(bytes)
return self; return self;
end 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 if type(attr_type) == "string" then
attr_type = assert(attribute_lookup[attr_type:lower()], "unknown attribute: "..attr_type); attr_type = assert(attribute_lookup[attr_type:lower()], "unknown attribute: "..attr_type);
end end
for _, attribute in ipairs(self.attributes) do for _, attribute in ipairs(self.attributes) do
if struct.unpack(">I2", attribute) == attr_type then 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 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)); self:add_attribute("xor-peer-address", self:_pack_address(family, parsed_ip.packed, port or 0, true));
end 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) function packet_methods:add_message_integrity(key)
-- Add attribute with a dummy value so we can artificially increase -- Add attribute with a dummy value so we can artificially increase
-- the packet 'length' -- the packet 'length'