forked from diffusiondata/wireshark-dissector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpt.messages.lua
467 lines (390 loc) · 16.3 KB
/
dpt.messages.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
-- Messages package
-- This package describes and parses different message types sent by Diffusion.
-- Package header
local master = diffusion or {}
if master.messages ~= nil then
return master.messages
end
local RD, FD = diffusion.utilities.RD, diffusion.utilities.FD
local parseTopicHeader = diffusion.parse.parseTopicHeader
local parseRecordFields = diffusion.parse.parseRecordFields
local parseField = diffusion.parse.parseField
local parseAckId = diffusion.parse.parseAckId
-- -----------------------------------
-- A 'class' to process message types
local MessageType = {}
function MessageType:new( id, name, fixedHeaderCount)
local result = { id = id, name = name, fixedHeaderCount = fixedHeaderCount }
setmetatable( result, self )
self.__index = self
return result
end
-- Iterate across an array of MessageType object, and produce a table of the same indexed by the ID
function MessageType.index( typeArray )
local result = {}
for i, type in ipairs(typeArray) do
result[type.id] = type
end
return result
end
-- Populate the headers tree with separate fixed headers and user headers
function MessageType:markupHeaders( headerRange )
-- Find the RD marking the fixed|user boundary
local headerBreak = headerRange:bytes():indexn( FD, self.fixedHeaderCount -1 )
if headerBreak == -1 then
-- no user headers, only fixed headers
return { fixedHeaders = { range = headerRange, string = headerRange:string():escapeDiff() } }
else
-- fixed headers and user headers
local fixedHeaderRange = headerRange:range( 0, headerBreak )
local userHeaderRange = headerRange:range( headerBreak +1 )
return { fixedHeaders = { range = fixedHeaderRange, string = fixedHeaderRange:string():escapeDiff() },
userHeaders = { range = userHeaderRange, string = userHeaderRange:string():escapeDiff() } }
end
end
function MessageType:markupBody( messageDetails, bodyRange )
-- the payload, everything after the headers
if messageDetails.msgEncoding == 0 then
local bytes = bodyRange:bytes()
local bytesLen = bytes:len()
local recs = { range = bodyRange }
local recordStart = 0
local pos = 0
local idx = 1
-- On each record delimiter add the previous record to result
while pos < bytesLen do
local byte = bytes:get_index(pos)
if byte == RD then
local recordRange = bodyRange:range( recordStart, pos - recordStart )
recs[idx] = { range = recordRange, string = recordRange:string():toRecordString(), fields = parseRecordFields( recordRange ) }
idx = idx + 1
pos = pos + 1
recordStart = pos
else
pos = pos + 1
end
end
-- Records are delimited so treat the end as another delimiter
-- Special handling is needed to get an empty range at the end for a trailing empty record and populate it with an empty field
if pos - recordStart == 0 then
local emptyRange = bodyRange:range( recordStart - 1, 0 )
local emptyFields = { num = 1 }
emptyFields[1] = { range = emptyRange, string = "" }
recs[idx] = { range = emptyRange, string = "", fields = emptyFields }
else
local recordRange = bodyRange:range( recordStart )
recs[idx] = { range = recordRange, string = recordRange:string():toRecordString(), fields = parseRecordFields( recordRange ) }
end
recs.num = idx
return recs
else
return {}
end
end
function MessageType:getDescription()
return self.name
end
-- Functionality specific to Topic Loads
local topicLoadType = MessageType:new( 0x14, "Topic Load", 1 )
function topicLoadType:markupHeaders( headerRange )
-- Parse topic
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
if alias ~= nil then
self.loadDescription = string.format( "aliasing %s => topic '%s'", alias, topic )
else
self.loadDescription = topic
end
local userHeaderObject
if headerRange ~= nil then
userHeaderObject = { range = headerRange, string = headerRange:string():escapeDiff() }
end
return { topic = info, userHeader = userHeaderObject }
end
function topicLoadType:getDescription()
return string.format( "%s, %s", self.name, self.loadDescription )
end
-- Functionality specific to Delta Messages
local deltaType = MessageType:new( 0x15, "Delta", 1 )
function deltaType:markupHeaders( headerRange )
-- Parse topic
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
if topic ~= nil then
self.topicDescription = string.format( "Topic: '%s'", topic )
else
self.topicDescription = string.format( "Unknown alias: '%s'", alias )
end
local userHeaderObject
if headerRange ~= nil then
userHeaderObject = { range = headerRange, string = headerRange:string():escapeDiff() }
return { topic = info, userHeaders = userHeaderObject }
end
return { topic = info, userHeaders = userHeaderObject }
end
function deltaType:getDescription()
return string.format( "%s, %s", self.name, self.topicDescription )
end
-- Functionality specific to Subscriptions - the info column, mostly
local subscribeType = MessageType:new( 0x16, "Subscribe", 1 )
function subscribeType:markupHeaders( headerRange )
-- A single header, with a topic-selector
self.subscriptionDescription = string.format( "Subscribe to '%s'", headerRange:string() )
return { fixedHeaders = { range = headerRange, string = headerRange:string() } }
end
function subscribeType:getDescription()
return self.subscriptionDescription
end
-- Functionality specific to Command Messages
local commandMessageType = MessageType:new( 0x24, "Command Message", 2 )
function commandMessageType:markupHeaders( headerRange )
-- Parse topic
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
local commandEndIndex = headerRange:bytes():index( FD )
local commandRange
local commandObject
local parametersObject
if commandEndIndex > -1 then
commandRange = headerRange:range( 0, commandEndIndex )
commandObject = { range = commandRange, string = commandRange:string() }
--Parse parameters
local parametersRange = headerRange:range( commandEndIndex + 1 )
parametersObject = { range = parametersRange, string = parametersRange:string():escapeDiff() }
else
commandRange = headerRange:range( 0 )
commandObject = { range = commandRange, string = commandRange:string() }
end
if topic ~= nil then
self.commandDescription = string.format ( "Command Message Topic: '%s', Command: '%s'", topic, commandRange:string() )
elseif alias ~= nil then
self.commandDescription = string.format ( "Command Message Alias: '%s', Command: '%s'", alias, commandRange:string() )
else
self.commandDescription = string.format ( "Command Message Topic: Unknown, Command: '%s'", commandRange:string() )
end
return { topic = info, command = commandObject, parameters = parametersObject }
end
function commandMessageType:getDescription()
return self.commandDescription
end
-- Functionality specific to Command Topic Load
local commandTopicLoadType = MessageType:new( 0x28, "Command Topic Load", 3 )
function commandTopicLoadType:markupHeaders( headerRange )
-- Parse topic
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
-- Parse command topic category
local commandTopicCategoryEndIndex = headerRange:bytes():index( FD )
local commandTopicCategoryRange = headerRange:range( 0, commandTopicCategoryEndIndex )
local commandTopicCategoryObject = { range = commandTopicCategoryRange, string = commandTopicCategoryRange:string() }
-- Parse command Topic Type
headerRange = headerRange:range( commandTopicCategoryEndIndex + 1 )
local commandTopicTypeEndIndex = headerRange:bytes():index( FD )
local commandRange, commandTopicTypeObject, parametersObject, commandTopicTypeRange
if commandTopicTypeEndIndex > -1 then
commandTopicTypeRange = headerRange:range( 0, commandTopicTypeEndIndex )
commandTopicTypeObject = { range = commandTopicTypeRange, string = commandTopicTypeRange:string() }
--Parse parameters
local parametersRange = headerRange:range( commandTopicTypeEndIndex + 1 )
parametersObject = { range = parametersRange, string = parametersRange:string():escapeDiff() }
else
commandTopicTypeRange = headerRange:range( 0 )
commandTopicTypeObject = { range = commandTopicTypeRange, string = commandTopicTypeRange:string() }
end
if topic ~= nil then
self.commandTopicLoadDescription = string.format ( "Command Load Topic: %s Topic Category: %s", topic, commandTopicCategoryRange:string() )
elseif alias ~= nil then
self.commandTopicLoadDescription = string.format ( "Command Load Alias: %s Topic Category: %s", alias, commandTopicCategoryRange:string() )
else
self.commandTopicLoadDescription = string.format ( "Command Load Topic: Unknown Topic Category: %s", commandTopicCategoryRange:string() )
end
return { topic = info, parameters = parametersObject, commandCategory = commandTopicCategoryObject, commandTopicType = commandTopicTypeObject }
end
function commandTopicLoadType:getDescription( messageDetails )
return self.commandTopicLoadDescription
end
-- Functionality specific to Command Topic Notification
local commandTopicNotificationType = MessageType:new( 0x29, "Command Topic Notification", 2 )
function commandTopicNotificationType:markupHeaders( headerRange )
-- Parse topic
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
-- Parse notification type
local notificationTypeEndIndex = headerRange:bytes():index( FD )
local notificationTypeRange, notificationTypeObject, parametersObject
if notificationTypeEndIndex > -1 then
notificationTypeRange = headerRange:range( 0, notificationTypeEndIndex )
notificationTypeObject = { range = notificationTypeRange, string = notificationTypeRange:string() }
--Parse parameters
local parametersRange = headerRange:range( notificationTypeEndIndex + 1 )
parametersObject = { range = parametersRange, string = parametersRange:string():escapeDiff() }
else
notificationTypeRange = headerRange:range( 0 )
notificationTypeObject = { range = notificationTypeRange, string = notificationTypeRange:string() }
end
if topic ~= nil then
self.commandTopicLoadDescription = string.format ( "Command Notification Topic: %s Notification Type: %s", topic, notificationTypeRange:string() )
elseif alias ~= nil then
self.commandTopicLoadDescription = string.format ( "Command Notification Alias: %s Notification Type: %s", alias, notificationTypeRange:string() )
else
self.commandTopicLoadDescription = string.format ( "Command Notification Topic: Unknown Notification Type: %s", notificationTypeRange:string() )
end
return { topic = info, parameters = parametersObject, notificationType = notificationTypeObject }
end
function commandTopicNotificationType:getDescription()
return self.commandTopicLoadDescription
end
local fetchType = MessageType:new( 0x21, "Fetch", 1 )
function fetchType:markupHeaders( headerRange )
local info
headerRange, info = parseTopicHeader( headerRange )
self.fetchDescription = string.format( "Fetch '%s'", info.topic.string )
return { topic = info }
end
function fetchType:getDescription( )
return self.fetchDescription
end
local fetchReplyType = MessageType:new( 0x22, "Fetch Reply", 1 )
function fetchReplyType:markupHeaders( headerRange )
local info
headerRange, info = parseTopicHeader( headerRange )
self.fetchDescription = string.format( "Fetch reply '%s'", info.topic.string )
return { topic = info }
end
function fetchReplyType:getDescription( )
return self.fetchDescription
end
local pingServer = MessageType:new( 0x18, "Ping Server", 2 )
function pingServer:markupHeaders( headerRange )
local timestampRange
timestampRange, headerRange = parseField( headerRange )
if headerRange ~= nil then
local messageQueueRange, headerRange = parseField( headerRange )
return { timestamp = { range = timestampRange, string = timestampRange:string() },
queueSize = { range = messageQueueRange, string = messageQueueRange:string() } }
else
return { timestamp = { range = timestampRange, string = timestampRange:string() } }
end
end
local pingClient = MessageType:new( 0x19, "Ping Client", 1 )
function pingClient:markupHeaders( headerRange )
local timestampRange, messageQueueRange
timestampRange, headerRange = parseField( headerRange )
return { timestamp = { range = timestampRange, string = timestampRange:string() } }
end
local topicLoadAckType = MessageType:new( 0x1e, "Topic Load - ACK Required", 2)
function topicLoadAckType:markupHeaders( headerRange )
return topicLoadType.markupHeaders( self, headerRange )
end
local deltaAckType = MessageType:new( 0x1f, "Delta - ACK Required", 2 )
function deltaAckType:markupHeaders( headerRange )
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
if topic ~= nil then
self.topicDescription = string.format( "Topic: '%s'", topic )
else
self.topicDescription = string.format( "Unknown alias: '%s'", alias )
end
local ackIdObject
ackIdObject, headerRange = parseAckId( headerRange )
self.ackDescription = string.format( "Ack ID %s", ackIdObject.string )
local userHeaderObject
if headerRange ~= nil then
userHeaderObject = { range = headerRange, string = headerRange:string():escapeDiff() }
end
return { topic = info, ackId = ackIdObject, userHeader = userHeaderObject }
end
function deltaAckType:getDescription()
return string.format( "%s, %s, %s", self.name, self.topicDescription, self.ackDescription )
end
local topicLoadAckType = MessageType:new( 0x1e, "Topic Load - ACK Required", 2 )
function topicLoadAckType:markupHeaders( headerRange )
local info, topic, alias
headerRange, info = parseTopicHeader( headerRange )
topic = info.topic.string
alias = info.alias.string
if alias ~= nil then
self.loadDescription = string.format( "aliasing %s => topic '%s'", alias, topic )
elseif topic == nil then
self.loadDescription = "Bad topic"
else
self.loadDescription = topic
end
local ackIdObject
ackIdObject, headerRange = parseAckId( headerRange )
self.ackDescription = string.format( "Ack ID %s", ackIdObject.string )
local userHeaderObject
if headerRange ~= nil then
userHeaderObject = { range = headerRange, string = headerRange:string():escapeDiff() }
end
return { topic = info, ackId = ackIdObject, userHeader = userHeaderObject }
end
function topicLoadAckType:getDescription()
return string.format( "%s, %s, %s", self.name, self.loadDescription, self.ackDescription )
end
local ackType = MessageType:new( 0x20, "ACK - acknowledge", 1 )
function ackType:markupHeaders( headerRange )
local ackIdObject
ackIdObject, headerRange = parseAckId( headerRange )
self.ackDescription = string.format( "Ack ID %s", ackIdObject.string )
return { ackId = ackIdObject }
end
function ackType:getDescription()
return string.format( "%s, %s", self.name, self.ackDescription )
end
-- The messageType table
local messageTypesByValue = MessageType.index( {
topicLoadType,
deltaType,
subscribeType,
MessageType:new( 0x17, "Unsubscribe", 1 ),
pingServer,
pingClient,
MessageType:new( 0x1a, "Credentials", 2 ),
MessageType:new( 0x1b, "Credentials Rejected", 2 ),
MessageType:new( 0x1c, "Abort Notification", 0 ),
MessageType:new( 0x1d, "Close Request", 0 ),
topicLoadAckType,
deltaAckType,
ackType,
fetchType,
fetchReplyType,
MessageType:new( 0x23, "Topic Status Notification", 2 ),
commandMessageType,
commandTopicLoadType,
commandTopicNotificationType,
MessageType:new( 0x30, "Cancel Fragmented Message Set", 1 )
})
local function messageTypeLookup(byte)
if byte >= 0x40 then
return messageTypesByValue[byte - 0x40]
else
return messageTypesByValue[byte]
end
end
local function nameByID(byte)
if byte >= 0x40 then
return messageTypesByValue[byte - 0x40].name .. " fragmented"
else
return messageTypesByValue[byte].name
end
end
-- Package footer
master.messages = {
messageTypeLookup = messageTypeLookup,
nameByID = nameByID
}
diffusion = master
return master.messages