Annotate

net/stun.lua @ 13254:a2ba3f06dcf4 0.12 0.12.4

util.prosodyctl.check: Correct modern replacement for 'disallow_s2s' The code would have suggested adding to modules_enabled instead of modules_disabled
author Kim Alvefur <zash@zash.se>
date Sun, 27 Aug 2023 15:46:19 +0200
parent 12374:0602245fc84e
child 12974:ba409c67353b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local base64 = require "util.encodings".base64;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local hashes = require "util.hashes";
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local net = require "util.net";
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local random = require "util.random";
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local struct = require "util.struct";
12360
0801db678f5e net.stun: Use util.bitcompat to deal with bit module variances across Lua versions
Kim Alvefur <zash@zash.se>
parents: 12359
diff changeset
6 local bit32 = require"util.bitcompat";
12359
f81488951f81 net.stun: Use util.strbitop
Kim Alvefur <zash@zash.se>
parents: 12356
diff changeset
7 local sxor = require"util.strbitop".sxor;
12371
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
8 local new_ip = require "util.ip".new_ip;
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 --- Public helpers
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 -- Following draft-uberti-behave-turn-rest-00, convert a 'secret' string
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 -- into a username/password pair that can be used to auth to a TURN server
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local function get_user_pass_from_secret(secret, ttl, opt_username)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 ttl = ttl or 86400;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local username;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if opt_username then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 username = ("%d:%s"):format(os.time() + ttl, opt_username);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 else
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 username = ("%d"):format(os.time() + ttl);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local password = base64.encode(hashes.hmac_sha1(secret, username));
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return username, password, ttl;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Following RFC 8489 9.2, convert credentials to a HMAC key for signing
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local function get_long_term_auth_key(realm, username, password)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return hashes.md5(username..":"..realm..":"..password);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 --- Packet building/parsing
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local packet_methods = {};
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local packet_mt = { __index = packet_methods };
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local magic_cookie = string.char(0x21, 0x12, 0xA4, 0x42);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
12368
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
38 local function lookup_table(t)
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
39 local lookup = {};
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
40 for k, v in pairs(t) do
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
41 lookup[k] = v;
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
42 lookup[v] = k;
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
43 end
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
44 return lookup;
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
45 end
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
46
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local methods = {
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 binding = 0x001;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 -- TURN
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 allocate = 0x003;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 refresh = 0x004;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 send = 0x006;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 data = 0x007;
12367
7a2f036f73b3 net.stun: Hyphenate method names for consistency
Matthew Wild <mwild1@gmail.com>
parents: 12360
diff changeset
54 ["create-permission"] = 0x008;
7a2f036f73b3 net.stun: Hyphenate method names for consistency
Matthew Wild <mwild1@gmail.com>
parents: 12360
diff changeset
55 ["channel-bind"] = 0x009;
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 };
12368
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
57 local method_lookup = lookup_table(methods);
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local classes = {
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 request = 0;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 indication = 1;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 success = 2;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 error = 3;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 };
12368
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
65 local class_lookup = lookup_table(classes);
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
66
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
67 local addr_families = { "IPv4", "IPv6" };
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
68 local addr_family_lookup = lookup_table(addr_families);
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local attributes = {
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 ["mapped-address"] = 0x0001;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 ["username"] = 0x0006;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 ["message-integrity"] = 0x0008;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 ["error-code"] = 0x0009;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 ["unknown-attributes"] = 0x000A;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 ["realm"] = 0x0014;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 ["nonce"] = 0x0015;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 ["xor-mapped-address"] = 0x0020;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 ["software"] = 0x8022;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 ["alternate-server"] = 0x8023;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 ["fingerprint"] = 0x8028;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 ["message-integrity-sha256"] = 0x001C;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 ["password-algorithm"] = 0x001D;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 ["userhash"] = 0x001E;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 ["password-algorithms"] = 0x8002;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 ["alternate-domains"] = 0x8003;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 -- TURN
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 ["requested-transport"] = 0x0019;
12369
f2ae9c6d1d9f net.stun: Name some more attributes from TURN
Matthew Wild <mwild1@gmail.com>
parents: 12368
diff changeset
90 ["xor-peer-address"] = 0x0012;
f2ae9c6d1d9f net.stun: Name some more attributes from TURN
Matthew Wild <mwild1@gmail.com>
parents: 12368
diff changeset
91 ["data"] = 0x0013;
f2ae9c6d1d9f net.stun: Name some more attributes from TURN
Matthew Wild <mwild1@gmail.com>
parents: 12368
diff changeset
92 ["xor-relayed-address"] = 0x0016;
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 };
12368
d82c8efc6dd5 net.stun: Add lookup table generation helper, reduces code duplication
Matthew Wild <mwild1@gmail.com>
parents: 12367
diff changeset
94 local attribute_lookup = lookup_table(attributes);
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 function packet_methods:serialize_header(length)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 assert(#self.transaction_id == 12, "invalid transaction id length");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local header = struct.pack(">I2I2",
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 self.type,
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 length
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 )..magic_cookie..self.transaction_id;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 return header;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 function packet_methods:serialize()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local payload = table.concat(self.attributes);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 return self:serialize_header(#payload)..payload;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 function packet_methods:is_request()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 return bit32.band(self.type, 0x0110) == 0x0000;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 function packet_methods:is_indication()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 return bit32.band(self.type, 0x0110) == 0x0010;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 function packet_methods:is_success_resp()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 return bit32.band(self.type, 0x0110) == 0x0100;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 function packet_methods:is_err_resp()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 return bit32.band(self.type, 0x0110) == 0x0110;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 function packet_methods:get_method()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 local method = bit32.bor(
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 bit32.rshift(bit32.band(self.type, 0x3E00), 2),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 bit32.rshift(bit32.band(self.type, 0x00E0), 1),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 bit32.band(self.type, 0x000F)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 );
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 return method, method_lookup[method];
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 function packet_methods:get_class()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local class = bit32.bor(
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 bit32.rshift(bit32.band(self.type, 0x0100), 7),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 bit32.rshift(bit32.band(self.type, 0x0010), 4)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 );
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 return class, class_lookup[class];
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 function packet_methods:set_type(method, class)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 if type(method) == "string" then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 method = assert(method_lookup[method:lower()], "unknown method: "..method);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 if type(class) == "string" then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 class = assert(classes[class], "unknown class: "..class);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 self.type = bit32.bor(
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 bit32.lshift(bit32.band(method, 0x1F80), 2),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 bit32.lshift(bit32.band(method, 0x0070), 1),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 bit32.band(method, 0x000F),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 bit32.lshift(bit32.band(class, 0x0002), 7),
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 bit32.lshift(bit32.band(class, 0x0001), 4)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 );
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local function _serialize_attribute(attr_type, value)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 local len = #value;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 local padding = string.rep("\0", (4 - len)%4);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 return struct.pack(">I2I2",
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 attr_type, len
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 )..value..padding;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 function packet_methods:add_attribute(attr_type, value)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 if type(attr_type) == "string" then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 attr_type = assert(attributes[attr_type], "unknown attribute: "..attr_type);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 table.insert(self.attributes, _serialize_attribute(attr_type, value));
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 function packet_methods:deserialize(bytes)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 local type, len, cookie = struct.unpack(">I2I2I4", bytes);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 assert(#bytes == (len + 20), "incorrect packet length");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 assert(cookie == 0x2112A442, "invalid magic cookie");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 self.type = type;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 self.transaction_id = bytes:sub(9, 20);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 self.attributes = {};
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 local pos = 21;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 while pos < #bytes do
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local attr_hdr = bytes:sub(pos, pos+3);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 assert(#attr_hdr == 4, "packet truncated in attribute header");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 local attr_type, attr_len = struct.unpack(">I2I2", attr_hdr); --luacheck: ignore 211/attr_type
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 if attr_len == 0 then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 table.insert(self.attributes, attr_hdr);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 pos = pos + 20;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 else
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 local data = bytes:sub(pos + 4, pos + 3 + attr_len);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 assert(#data == attr_len, "packet truncated in attribute value");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 table.insert(self.attributes, attr_hdr..data);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 local n_padding = (4 - attr_len)%4;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 pos = pos + 4 + attr_len + n_padding;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 return self;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
12374
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
200 function packet_methods:get_attribute(attr_type, idx)
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
201 idx = math.max(idx or 1, 1);
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if type(attr_type) == "string" then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 attr_type = assert(attribute_lookup[attr_type:lower()], "unknown attribute: "..attr_type);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 for _, attribute in ipairs(self.attributes) do
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 if struct.unpack(">I2", attribute) == attr_type then
12374
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
207 if idx == 1 then
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
208 return attribute:sub(5);
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
209 else
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
210 idx = idx - 1;
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
211 end
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
12370
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
216 function packet_methods:_unpack_address(data, xor)
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 local family, port = struct.unpack("x>BI2", data);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 local addr = data:sub(5);
12370
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
219 if xor then
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
220 port = bit32.bxor(port, 0x2112);
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
221 addr = sxor(addr, magic_cookie..self.transaction_id);
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
222 end
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 return {
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 family = addr_families[family] or "unknown";
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 port = port;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 address = net.ntop(addr);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 };
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
12371
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
230 function packet_methods:_pack_address(family, addr, port, xor)
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
231 if xor then
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
232 port = bit32.bxor(port, 0x2112);
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
233 addr = sxor(addr, magic_cookie..self.transaction_id);
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
234 end
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
235 local family_port = struct.pack("x>BI2", family, port);
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
236 return family_port..addr
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
237 end
12370
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
238
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
239 function packet_methods:get_mapped_address()
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
240 local data = self:get_attribute("mapped-address");
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
241 if not data then return; end
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
242 return self:_unpack_address(data, false);
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
243 end
12371
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
244
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 function packet_methods:get_xor_mapped_address()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 local data = self:get_attribute("xor-mapped-address");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 if not data then return; end
12370
9889b1815d31 net.stun: Factor out address unpack, an operation common to multiple attributes
Matthew Wild <mwild1@gmail.com>
parents: 12369
diff changeset
248 return self:_unpack_address(data, true);
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250
12371
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
251 function packet_methods:add_xor_peer_address(address, port)
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
252 local parsed_ip = assert(new_ip(address));
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
253 local family = assert(addr_family_lookup[parsed_ip.proto], "Unknown IP address family: "..parsed_ip.proto);
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
254 self:add_attribute("xor-peer-address", self:_pack_address(family, parsed_ip.packed, port or 0, true));
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
255 end
9a8b0c5b4b14 net.stun: Add xor-peer-address helper
Matthew Wild <mwild1@gmail.com>
parents: 12370
diff changeset
256
12374
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
257 function packet_methods:get_xor_relayed_address(idx)
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
258 local data = self:get_attribute("xor-relayed-address", idx);
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
259 if not data then return; end
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
260 return self:_unpack_address(data, true);
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
261 end
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
262
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
263 function packet_methods:get_xor_relayed_addresses()
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
264 return {
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
265 self:get_xor_relayed_address(1);
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
266 self:get_xor_relayed_address(2);
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
267 };
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
268 end
0602245fc84e net.stun: Support for xor-relayed-address attribute
Matthew Wild <mwild1@gmail.com>
parents: 12371
diff changeset
269
12356
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 function packet_methods:add_message_integrity(key)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 -- Add attribute with a dummy value so we can artificially increase
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 -- the packet 'length'
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 self:add_attribute("message-integrity", string.rep("\0", 20));
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 -- Get the packet data, minus the message-integrity attribute itself
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 local pkt = self:serialize():sub(1, -25);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 local hash = hashes.hmac_sha1(key, pkt, false);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 self.attributes[#self.attributes] = nil;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 assert(#hash == 20, "invalid hash length");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 self:add_attribute("message-integrity", hash);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 do
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 local transports = {
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 udp = 0x11;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 };
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 function packet_methods:add_requested_transport(transport)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 local transport_code = transports[transport];
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 assert(transport_code, "unsupported transport: "..tostring(transport));
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 self:add_attribute("requested-transport", string.char(
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 transport_code, 0x00, 0x00, 0x00
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 ));
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 function packet_methods:get_error()
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 local err_attr = self:get_attribute("error-code");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 if not err_attr then
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 return nil;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 local number = err_attr:byte(4);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 local class = bit32.band(0x07, err_attr:byte(3));
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 local msg = err_attr:sub(5);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 return (class*100)+number, msg;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 local function new_packet(method, class)
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 local p = setmetatable({
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 transaction_id = random.bytes(12);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 length = 0;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 attributes = {};
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 }, packet_mt);
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 p:set_type(method or "binding", class or "request");
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 return p;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 end
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 return {
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 new_packet = new_packet;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 get_user_pass_from_secret = get_user_pass_from_secret;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 get_long_term_auth_key = get_long_term_auth_key;
0f77e28df5c8 net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 };