This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt.lua
274 lines (244 loc) · 7.24 KB
/
mqtt.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
-- mqtt.lua
--
-- Copyright (c) 2018 Baptiste Assmann [email protected]
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
local mqtt = { _version = "0.0.2" }
-- get string from a buffer, whose size is encoded over 2 bytes, MSB LSB way
-- the buffer must be in hexa
-- it returns the string and the size of the string
local function getfield(buf, bufsize, pos)
local msb = tonumber(string.sub(buf, pos, pos + 1), 16)
pos = pos + 2
local lsb = tonumber(string.sub(buf, pos, pos + 1), 16)
pos = pos + 2
local len = msb * 256 + lsb
-- ensure we don't read too further in the buffer
-- len * 2 because the string is encoded in hex
-- -1 because string index starts at 1
if pos + len * 2 - 1 > bufsize then
return '', 0
end
local str = ''
local i = pos
while i < pos + len * 2 do
str = str .. string.char(tonumber(string.sub(buf, i, i + 1), 16))
i = i + 2
end
-- +4 because the size is encoded on 2 bytes
return str, len * 2 + 4
end
-- parse flags and return a flag structure with all bits set
local function parseflags(flags)
if type(flags) ~= 'number' then
error("expected argument of type number, got " .. type(flags))
end
local flag = {}
flag['username'] = false
flag['password'] = false
flag['willretain'] = false
flag['willqos'] = false
flag['will'] = false
flag['clean'] = false
if flags >= 128 then
flag['username'] = true
flags = flags - 128
end
if flags >= 64 then
flag['password'] = true
flags = flags - 64
end
if flags >= 32 then
flag['willretain'] = true
flags = flags - 32
end
if flags >= 8 then
flag['willqos'] = true
flags = flags - 8
end
if flags >= 4 then
flag['will'] = true
flags = flags - 4
end
if flags >= 2 then
flag['clean'] = true
flags = flags - 2
end
return flag
end
-- dump the content of a mqtt packet to stdout
local function dump_pkt(pkt)
local str = ''
if pkt['packettype'] == 1 then
str = 'CONNECT '
end
str = str .. pkt['protocolname'] .. ': '
str = str .. pkt['clientid']
if pkt['username'] ~= nil then
str = str .. ', ' .. pkt['username']
end
if pkt['password'] ~= nil then
str = str .. '/' .. pkt['password']
end
print(str)
end
-- buffer must be an hexa representation of a CONNECT packet payload
parse = function(buffer)
local pkt = {}
local cur = 1
local len = 0
local buffersize = string.len(buffer)
pkt['error'] = false
pkt['errormessage'] = ''
-- fixed header
-- byte 1: packet type is bits from 4 to 7, so we must substract bits from 0 to 3
pkt['type'] = tonumber(string.sub(buffer, cur, cur + 1), 16) - 15
if pkt['type'] ~= 1 then
pkt['error'] = true
pkt['errormessage'] = 'Wrong packet type: ' .. pkt['type']
return pkt
end
if cur + 2 - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + 2
-- byte 2: remaininig length (len of "payload + payload size")
-- http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718023
local multiplier = 1
local remaininglength = 0
local encodedByte = tonumber(string.sub(buffer, cur, cur + 1), 16)
if cur + 2 - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + 2
if encodedByte >= 128 then
while (encodedByte >= 128) do
remaininglength = remaininglength + (encodedByte * multiplier)
multiplier = multiplier * 128
if (multiplier > 128 * 128 * 128) then
pkt['error'] = true
pkt['errormessage'] = 'announced packet size too big'
return pkt
end
if cur + 2 - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + 2
encodedByte = tonumber(string.sub(buffer, cur, cur + 1), 16)
end
else
remaininglength = remaininglength + (encodedByte * multiplier)
end
if remaininglength * 2 + cur - 1 ~= buffersize then
pkt['error'] = true
pkt['errormessage'] = 'announced packet size sem to be wrong, can\'t parse it.'
return pkt
end
-- variable header
-- protocol name
pkt['protocolname'], len = getfield(buffer, buffersize, cur)
if cur + len - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + len
-- protocol level
pkt['protocollevel'] = tonumber(string.sub(buffer, cur, cur + 1), 16)
if cur + 2 - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + 2
-- flags
pkt['flag'] = parseflags(tonumber(string.sub(buffer, cur, cur + 1), 16))
if cur + 2 - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + 2
-- keepalive
-- encoded over 2 bytes, MSB + LSB
pkt['keepalive'] = tonumber(string.sub(buffer, cur, cur + 1), 16) * 256 + tonumber(string.sub(buffer, cur + 2, cur + 2 + 1), 16)
if cur + 4 - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + 4
-- payload
-- These fields, if present, MUST appear in the following order:
-- Client Identifier
-- Will Topic
-- Will Message
-- User Name
-- Password
-- client identifier
pkt['clientid'], len = getfield(buffer, buffersize, cur)
if cur + len - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + len
-- will topic
-- (ignored for now)
if pkt['flag']['will'] then
-- skip will topic
-- size encoded over 2 bytes, MSB + LSB + data len
len = 4 + tonumber(string.sub(buffer, cur, cur + 1), 16) * 256 + tonumber(string.sub(buffer, cur + 2, cur + 2 + 1), 16)
if cur + len - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + len
-- skip will message
-- size encoded over 2 bytes, MSB + LSB + data len
len = 4 + tonumber(string.sub(buffer, cur, cur + 1), 16) * 256 + tonumber(string.sub(buffer, cur + 2, cur + 2 + 1), 16)
if cur + len - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + len
end
-- username
if pkt['flag']['username'] then
pkt['username'], len = getfield(buffer, buffersize, cur)
if cur + len - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + len
end
-- password
if pkt['flag']['password'] then
pkt['password'], len = getfield(buffer, buffersize, cur)
if cur + len - 1 > buffersize then
pkt['error'] = true
pkt['errormessage'] = 'Error when parsing the buffer'
return pkt
end
cur = cur + len
end
return pkt
end
function mqtt.parse(buffer)
if type(buffer) ~= "string" then
error("expected argument of type string, got " .. type(buffer))
end
return ( parse(buffer) )
end
return mqtt