diff options
author | Matthew Wild <mwild1@gmail.com> | 2022-03-05 11:03:44 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2022-03-05 11:03:44 +0000 |
commit | ac06bd8c9728eb49f63f3db81fde727d33a316f9 (patch) | |
tree | 74147fc7c143dda9894bcae8e3c5dbbc979d8ac6 | |
parent | 6e9d71259ca9d8904478aba47d42b7e217fce969 (diff) | |
download | prosody-ac06bd8c9728eb49f63f3db81fde727d33a316f9.tar.gz prosody-ac06bd8c9728eb49f63f3db81fde727d33a316f9.zip |
net.stun: Factor out address unpack, an operation common to multiple attributes
-rw-r--r-- | net/stun.lua | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/net/stun.lua b/net/stun.lua index 530b4e3b..80b60bda 100644 --- a/net/stun.lua +++ b/net/stun.lua @@ -207,11 +207,13 @@ function packet_methods:get_attribute(attr_type) 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 @@ function packet_methods:get_mapped_address() }; 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) |