Software / code / prosody
Comparison
net/stun.lua @ 12371:9a8b0c5b4b14
net.stun: Add xor-peer-address helper
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 05 Mar 2022 11:04:25 +0000 |
| parent | 12370:9889b1815d31 |
| child | 12374:0602245fc84e |
comparison
equal
deleted
inserted
replaced
| 12370:9889b1815d31 | 12371:9a8b0c5b4b14 |
|---|---|
| 3 local net = require "util.net"; | 3 local net = require "util.net"; |
| 4 local random = require "util.random"; | 4 local random = require "util.random"; |
| 5 local struct = require "util.struct"; | 5 local struct = require "util.struct"; |
| 6 local bit32 = require"util.bitcompat"; | 6 local bit32 = require"util.bitcompat"; |
| 7 local sxor = require"util.strbitop".sxor; | 7 local sxor = require"util.strbitop".sxor; |
| 8 local new_ip = require "util.ip".new_ip; | |
| 8 | 9 |
| 9 --- Public helpers | 10 --- Public helpers |
| 10 | 11 |
| 11 -- Following draft-uberti-behave-turn-rest-00, convert a 'secret' string | 12 -- Following draft-uberti-behave-turn-rest-00, convert a 'secret' string |
| 12 -- into a username/password pair that can be used to auth to a TURN server | 13 -- into a username/password pair that can be used to auth to a TURN server |
| 219 port = port; | 220 port = port; |
| 220 address = net.ntop(addr); | 221 address = net.ntop(addr); |
| 221 }; | 222 }; |
| 222 end | 223 end |
| 223 | 224 |
| 225 function packet_methods:_pack_address(family, addr, port, xor) | |
| 226 if xor then | |
| 227 port = bit32.bxor(port, 0x2112); | |
| 228 addr = sxor(addr, magic_cookie..self.transaction_id); | |
| 229 end | |
| 230 local family_port = struct.pack("x>BI2", family, port); | |
| 231 return family_port..addr | |
| 232 end | |
| 224 | 233 |
| 225 function packet_methods:get_mapped_address() | 234 function packet_methods:get_mapped_address() |
| 226 local data = self:get_attribute("mapped-address"); | 235 local data = self:get_attribute("mapped-address"); |
| 227 if not data then return; end | 236 if not data then return; end |
| 228 return self:_unpack_address(data, false); | 237 return self:_unpack_address(data, false); |
| 229 end | 238 end |
| 239 | |
| 230 function packet_methods:get_xor_mapped_address() | 240 function packet_methods:get_xor_mapped_address() |
| 231 local data = self:get_attribute("xor-mapped-address"); | 241 local data = self:get_attribute("xor-mapped-address"); |
| 232 if not data then return; end | 242 if not data then return; end |
| 233 return self:_unpack_address(data, true); | 243 return self:_unpack_address(data, true); |
| 244 end | |
| 245 | |
| 246 function packet_methods:add_xor_peer_address(address, port) | |
| 247 local parsed_ip = assert(new_ip(address)); | |
| 248 local family = assert(addr_family_lookup[parsed_ip.proto], "Unknown IP address family: "..parsed_ip.proto); | |
| 249 self:add_attribute("xor-peer-address", self:_pack_address(family, parsed_ip.packed, port or 0, true)); | |
| 234 end | 250 end |
| 235 | 251 |
| 236 function packet_methods:add_message_integrity(key) | 252 function packet_methods:add_message_integrity(key) |
| 237 -- Add attribute with a dummy value so we can artificially increase | 253 -- Add attribute with a dummy value so we can artificially increase |
| 238 -- the packet 'length' | 254 -- the packet 'length' |