Diff

net/stun.lua @ 12374:0602245fc84e

net.stun: Support for xor-relayed-address attribute
author Matthew Wild <mwild1@gmail.com>
date Sat, 05 Mar 2022 11:30:55 +0000
parent 12371:9a8b0c5b4b14
child 12974:ba409c67353b
line wrap: on
line diff
--- 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'