comparison net/stun.lua @ 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 7a2f036f73b3
children f2ae9c6d1d9f
comparison
equal deleted inserted replaced
12367:7a2f036f73b3 12368:d82c8efc6dd5
32 local packet_methods = {}; 32 local packet_methods = {};
33 local packet_mt = { __index = packet_methods }; 33 local packet_mt = { __index = packet_methods };
34 34
35 local magic_cookie = string.char(0x21, 0x12, 0xA4, 0x42); 35 local magic_cookie = string.char(0x21, 0x12, 0xA4, 0x42);
36 36
37 local function lookup_table(t)
38 local lookup = {};
39 for k, v in pairs(t) do
40 lookup[k] = v;
41 lookup[v] = k;
42 end
43 return lookup;
44 end
45
37 local methods = { 46 local methods = {
38 binding = 0x001; 47 binding = 0x001;
39 -- TURN 48 -- TURN
40 allocate = 0x003; 49 allocate = 0x003;
41 refresh = 0x004; 50 refresh = 0x004;
42 send = 0x006; 51 send = 0x006;
43 data = 0x007; 52 data = 0x007;
44 ["create-permission"] = 0x008; 53 ["create-permission"] = 0x008;
45 ["channel-bind"] = 0x009; 54 ["channel-bind"] = 0x009;
46 }; 55 };
47 local method_lookup = {}; 56 local method_lookup = lookup_table(methods);
48 for name, value in pairs(methods) do
49 method_lookup[name] = value;
50 method_lookup[value] = name;
51 end
52 57
53 local classes = { 58 local classes = {
54 request = 0; 59 request = 0;
55 indication = 1; 60 indication = 1;
56 success = 2; 61 success = 2;
57 error = 3; 62 error = 3;
58 }; 63 };
59 local class_lookup = {}; 64 local class_lookup = lookup_table(classes);
60 for name, value in pairs(classes) do 65
61 class_lookup[name] = value; 66 local addr_families = { "IPv4", "IPv6" };
62 class_lookup[value] = name; 67 local addr_family_lookup = lookup_table(addr_families);
63 end
64 68
65 local attributes = { 69 local attributes = {
66 ["mapped-address"] = 0x0001; 70 ["mapped-address"] = 0x0001;
67 ["username"] = 0x0006; 71 ["username"] = 0x0006;
68 ["message-integrity"] = 0x0008; 72 ["message-integrity"] = 0x0008;
81 ["alternate-domains"] = 0x8003; 85 ["alternate-domains"] = 0x8003;
82 86
83 -- TURN 87 -- TURN
84 ["requested-transport"] = 0x0019; 88 ["requested-transport"] = 0x0019;
85 }; 89 };
86 local attribute_lookup = {}; 90 local attribute_lookup = lookup_table(attributes);
87 for name, value in pairs(attributes) do
88 attribute_lookup[name] = value;
89 attribute_lookup[value] = name;
90 end
91 91
92 function packet_methods:serialize_header(length) 92 function packet_methods:serialize_header(length)
93 assert(#self.transaction_id == 12, "invalid transaction id length"); 93 assert(#self.transaction_id == 12, "invalid transaction id length");
94 local header = struct.pack(">I2I2", 94 local header = struct.pack(">I2I2",
95 self.type, 95 self.type,
202 return attribute:sub(5); 202 return attribute:sub(5);
203 end 203 end
204 end 204 end
205 end 205 end
206 206
207 local addr_families = { "IPv4", "IPv6" };
208 function packet_methods:get_mapped_address() 207 function packet_methods:get_mapped_address()
209 local data = self:get_attribute("mapped-address"); 208 local data = self:get_attribute("mapped-address");
210 if not data then return; end 209 if not data then return; end
211 local family, port = struct.unpack("x>BI2", data); 210 local family, port = struct.unpack("x>BI2", data);
212 local addr = data:sub(5); 211 local addr = data:sub(5);