# HG changeset patch # User Matthew Wild # Date 1646478224 0 # Node ID 9889b1815d319f596bec46183032a2955f826ba2 # Parent f2ae9c6d1d9fe8d45a1c35a4e18367141961461f net.stun: Factor out address unpack, an operation common to multiple attributes diff -r f2ae9c6d1d9f -r 9889b1815d31 net/stun.lua --- a/net/stun.lua Sat Mar 05 11:00:08 2022 +0000 +++ b/net/stun.lua Sat Mar 05 11:03:44 2022 +0000 @@ -207,11 +207,13 @@ end end -function packet_methods:get_mapped_address() - local data = self:get_attribute("mapped-address"); - if not data then return; end +function packet_methods:_unpack_address(data, xor) local family, port = struct.unpack("x>BI2", data); local addr = data:sub(5); + if xor then + port = bit32.bxor(port, 0x2112); + addr = sxor(addr, magic_cookie..self.transaction_id); + end return { family = addr_families[family] or "unknown"; port = port; @@ -219,17 +221,16 @@ }; end + +function packet_methods:get_mapped_address() + local data = self:get_attribute("mapped-address"); + if not data then return; end + return self:_unpack_address(data, false); +end function packet_methods:get_xor_mapped_address() local data = self:get_attribute("xor-mapped-address"); if not data then return; end - local family, port = struct.unpack("x>BI2", data); - local addr = sxor(data:sub(5), magic_cookie..self.transaction_id); - return { - family = addr_families[family] or "unknown"; - port = bit32.bxor(port, 0x2112); - address = net.ntop(addr); - address_raw = data:sub(5); - }; + return self:_unpack_address(data, true); end function packet_methods:add_message_integrity(key)