Changeset

12368:d82c8efc6dd5

net.stun: Add lookup table generation helper, reduces code duplication
author Matthew Wild <mwild1@gmail.com>
date Sat, 05 Mar 2022 10:59:19 +0000
parents 12367:7a2f036f73b3
children 12369:f2ae9c6d1d9f
files net/stun.lua
diffstat 1 files changed, 15 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/net/stun.lua	Sat Mar 05 10:57:47 2022 +0000
+++ b/net/stun.lua	Sat Mar 05 10:59:19 2022 +0000
@@ -34,6 +34,15 @@
 
 local magic_cookie = string.char(0x21, 0x12, 0xA4, 0x42);
 
+local function lookup_table(t)
+	local lookup = {};
+	for k, v in pairs(t) do
+		lookup[k] = v;
+		lookup[v] = k;
+	end
+	return lookup;
+end
+
 local methods = {
 	binding = 0x001;
 	-- TURN
@@ -44,11 +53,7 @@
 	["create-permission"] = 0x008;
 	["channel-bind"] = 0x009;
 };
-local method_lookup = {};
-for name, value in pairs(methods) do
-	method_lookup[name] = value;
-	method_lookup[value] = name;
-end
+local method_lookup = lookup_table(methods);
 
 local classes = {
 	request = 0;
@@ -56,11 +61,10 @@
 	success = 2;
 	error = 3;
 };
-local class_lookup = {};
-for name, value in pairs(classes) do
-	class_lookup[name] = value;
-	class_lookup[value] = name;
-end
+local class_lookup = lookup_table(classes);
+
+local addr_families = { "IPv4", "IPv6" };
+local addr_family_lookup = lookup_table(addr_families);
 
 local attributes = {
 	["mapped-address"] = 0x0001;
@@ -83,11 +87,7 @@
 	-- TURN
 	["requested-transport"] = 0x0019;
 };
-local attribute_lookup = {};
-for name, value in pairs(attributes) do
-	attribute_lookup[name] = value;
-	attribute_lookup[value] = name;
-end
+local attribute_lookup = lookup_table(attributes);
 
 function packet_methods:serialize_header(length)
 	assert(#self.transaction_id == 12, "invalid transaction id length");
@@ -204,7 +204,6 @@
 	end
 end
 
-local addr_families = { "IPv4", "IPv6" };
 function packet_methods:get_mapped_address()
 	local data = self:get_attribute("mapped-address");
 	if not data then return; end