Software /
code /
prosody
Annotate
net/stun.lua @ 12356:0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 04 Mar 2022 15:23:32 +0000 |
child | 12359:f81488951f81 |
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"; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 --- Private helpers |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 -- XORs a string with another string |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local function sxor(x, y) |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local r = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 for i = 1, #x do |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 r[i] = string.char(bit32.bxor(x:byte(i), y:byte(i))); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 return table.concat(r); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 --- Public helpers |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 -- 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
|
21 -- 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
|
22 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
|
23 ttl = ttl or 86400; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local username; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 if opt_username then |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 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
|
27 else |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 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
|
29 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 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
|
31 return username, password, ttl; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 -- 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
|
35 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
|
36 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
|
37 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 --- Packet building/parsing |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 local packet_methods = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 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
|
43 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 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
|
45 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local methods = { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 binding = 0x001; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 -- TURN |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 allocate = 0x003; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 refresh = 0x004; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 send = 0x006; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 data = 0x007; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 create_permission = 0x008; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 channel_bind = 0x009; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local method_lookup = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 for name, value in pairs(methods) do |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 method_lookup[name] = value; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 method_lookup[value] = name; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 local classes = { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 request = 0; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 indication = 1; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 success = 2; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 error = 3; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 local class_lookup = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 for name, value in pairs(classes) do |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 class_lookup[name] = value; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 class_lookup[value] = name; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local attributes = { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 ["mapped-address"] = 0x0001; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 ["username"] = 0x0006; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 ["message-integrity"] = 0x0008; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 ["error-code"] = 0x0009; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 ["unknown-attributes"] = 0x000A; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 ["realm"] = 0x0014; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 ["nonce"] = 0x0015; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 ["xor-mapped-address"] = 0x0020; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 ["software"] = 0x8022; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 ["alternate-server"] = 0x8023; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 ["fingerprint"] = 0x8028; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 ["message-integrity-sha256"] = 0x001C; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 ["password-algorithm"] = 0x001D; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 ["userhash"] = 0x001E; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 ["password-algorithms"] = 0x8002; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 ["alternate-domains"] = 0x8003; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 -- TURN |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 ["requested-transport"] = 0x0019; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 local attribute_lookup = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 for name, value in pairs(attributes) do |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 attribute_lookup[name] = value; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 attribute_lookup[value] = name; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 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
|
102 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
|
103 local header = struct.pack(">I2I2", |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 self.type, |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 length |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 )..magic_cookie..self.transaction_id; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 return header; |
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:serialize() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 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
|
112 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
|
113 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 function packet_methods:is_request() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 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
|
117 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 function packet_methods:is_indication() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 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
|
121 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 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
|
124 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
|
125 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 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
|
128 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
|
129 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 function packet_methods:get_method() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 local method = bit32.bor( |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 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
|
134 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
|
135 bit32.band(self.type, 0x000F) |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 ); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 return method, method_lookup[method]; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
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 function packet_methods:get_class() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 local class = bit32.bor( |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 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
|
143 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
|
144 ); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 return class, class_lookup[class]; |
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 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 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
|
149 if type(method) == "string" then |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 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
|
151 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 if type(class) == "string" then |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 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
|
154 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 self.type = bit32.bor( |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 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
|
157 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
|
158 bit32.band(method, 0x000F), |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 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
|
160 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
|
161 ); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 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
|
165 local len = #value; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 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
|
167 return struct.pack(">I2I2", |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 attr_type, len |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 )..value..padding; |
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 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 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
|
177 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 function packet_methods:deserialize(bytes) |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 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
|
181 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
|
182 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
|
183 self.type = type; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 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
|
185 self.attributes = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 local pos = 21; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 while pos < #bytes do |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 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
|
189 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
|
190 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
|
191 if attr_len == 0 then |
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); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 pos = pos + 20; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 else |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 return self; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 function packet_methods:get_attribute(attr_type) |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 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
|
207 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
|
208 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 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
|
210 if struct.unpack(">I2", attribute) == attr_type then |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 return attribute:sub(5); |
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 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 local addr_families = { "IPv4", "IPv6" }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 function packet_methods:get_mapped_address() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 local data = self:get_attribute("mapped-address"); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 if not data then return; end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 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
|
221 local addr = data:sub(5); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 return { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 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
|
224 port = port; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 address = net.ntop(addr); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 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
|
230 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
|
231 if not data then return; end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 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
|
233 local addr = sxor(data:sub(5), magic_cookie..self.transaction_id); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 return { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 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
|
236 port = bit32.bxor(port, 0x2112); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 address = net.ntop(addr); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 address_raw = data:sub(5); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 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
|
243 -- 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
|
244 -- the packet 'length' |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 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
|
246 -- 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
|
247 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
|
248 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
|
249 self.attributes[#self.attributes] = nil; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 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
|
251 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
|
252 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 do |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 local transports = { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 udp = 0x11; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 }; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 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
|
259 local transport_code = transports[transport]; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 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
|
261 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
|
262 transport_code, 0x00, 0x00, 0x00 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 )); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 function packet_methods:get_error() |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 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
|
269 if not err_attr then |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 return nil; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 return (class*100)+number, msg; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 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
|
279 local p = setmetatable({ |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 transaction_id = random.bytes(12); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 length = 0; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 attributes = {}; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 }, packet_mt); |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 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
|
285 return p; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 end |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 return { |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 new_packet = new_packet; |
0f77e28df5c8
net.stun: New library that implements STUN/TURN parsing/serialization
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 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
|
291 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
|
292 }; |