Comparison

net/stun.lua @ 12370:9889b1815d31

net.stun: Factor out address unpack, an operation common to multiple attributes
author Matthew Wild <mwild1@gmail.com>
date Sat, 05 Mar 2022 11:03:44 +0000
parent 12369:f2ae9c6d1d9f
child 12371:9a8b0c5b4b14
comparison
equal deleted inserted replaced
12369:f2ae9c6d1d9f 12370:9889b1815d31
205 return attribute:sub(5); 205 return attribute:sub(5);
206 end 206 end
207 end 207 end
208 end 208 end
209 209
210 function packet_methods:get_mapped_address() 210 function packet_methods:_unpack_address(data, xor)
211 local data = self:get_attribute("mapped-address");
212 if not data then return; end
213 local family, port = struct.unpack("x>BI2", data); 211 local family, port = struct.unpack("x>BI2", data);
214 local addr = data:sub(5); 212 local addr = data:sub(5);
213 if xor then
214 port = bit32.bxor(port, 0x2112);
215 addr = sxor(addr, magic_cookie..self.transaction_id);
216 end
215 return { 217 return {
216 family = addr_families[family] or "unknown"; 218 family = addr_families[family] or "unknown";
217 port = port; 219 port = port;
218 address = net.ntop(addr); 220 address = net.ntop(addr);
219 }; 221 };
220 end 222 end
221 223
224
225 function packet_methods:get_mapped_address()
226 local data = self:get_attribute("mapped-address");
227 if not data then return; end
228 return self:_unpack_address(data, false);
229 end
222 function packet_methods:get_xor_mapped_address() 230 function packet_methods:get_xor_mapped_address()
223 local data = self:get_attribute("xor-mapped-address"); 231 local data = self:get_attribute("xor-mapped-address");
224 if not data then return; end 232 if not data then return; end
225 local family, port = struct.unpack("x>BI2", data); 233 return self:_unpack_address(data, true);
226 local addr = sxor(data:sub(5), magic_cookie..self.transaction_id);
227 return {
228 family = addr_families[family] or "unknown";
229 port = bit32.bxor(port, 0x2112);
230 address = net.ntop(addr);
231 address_raw = data:sub(5);
232 };
233 end 234 end
234 235
235 function packet_methods:add_message_integrity(key) 236 function packet_methods:add_message_integrity(key)
236 -- Add attribute with a dummy value so we can artificially increase 237 -- Add attribute with a dummy value so we can artificially increase
237 -- the packet 'length' 238 -- the packet 'length'