This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tarantool15.dissector.lua
382 lines (303 loc) · 11.3 KB
/
tarantool15.dissector.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
-- declare the protocol
tarantool_proto = Proto("tarantool","Tarantool")
function leb128Unpack(buffer, offset)
-- see http://en.wikipedia.org/wiki/LEB128#Decode_unsigned_integer
debug('-- leb128Unpack --')
local result = 0
local shift = 0
local used = 1
while true do
local byte = buffer(offset, 1):le_uint();
debug('byte: ' .. byte .. ' ' .. string.format('%04X', byte))
local bit7 = buffer(offset, 1):bitfield(0, 1)
offset = offset + 1
local tmp = (bit7 == 0) and byte or (byte - 128) -- reset 7th bit (byte & 0x80)
result = result * 128 + tmp -- result |= (low order 7 bits of byte << shift);
if ( bit7 == 0) then
break
end
shift = shift + 7
used = used + 1
end
return result, used
end
function add_one_tuple(buffer, subtree, num)
debug('-- add_one_tuple --')
--[[
<tuple> ::= <cardinality><field>+
<cardinality> ::= <int32>
<field> ::= <int32_varint><data>
<data> ::= <int8>+
<int32_varint> ::= <int8>+
]]
local data_length = 4 -- for cardinality
local cardinality = buffer(0,4):le_uint()
local array = {}
for i=1,cardinality do
debug('offset:'.. data_length)
local field_length, used = leb128Unpack(buffer, data_length)
debug('f,u:'.. field_length .. ' '..used)
array[i] = {
['start'] = data_length + used,
['length'] = field_length,
['title'] = "Data (length: " .. field_length .. ')'
}
data_length = data_length + field_length + used
end
local tree = subtree:add( tarantool_proto, buffer(0, data_length),"Tuple #" .. num .. " (cardinality: "..cardinality..')')
for i,v in ipairs(array) do
tree:add(buffer(v.start, v.length), v.title)
end
return data_length
end
function add_tuples(buffer, subtree, name, count)
-- local count = count_buffer(0,4):le_uint()
local tuples = subtree:add( tarantool_proto, buffer(), "Tuples")
-- tuples:add( count_buffer(0,4), "Count: " .. count )
local offset = 0
for i=1,count do
offset = offset + add_one_tuple( buffer(offset), tuples, i )
end
end
function add_fqtuple(buffer, subtree, name, count)
local tuples = subtree:add( tarantool_proto, buffer(), "fq_tuples (count: " .. count ..')' )
local offset = 0
for i=1,count do
local size = buffer(0,4):le_uint()
tuples:add( buffer(offset,4), 'tuple size: ' .. size )
offset = offset + add_one_tuple( buffer(offset + 4), tuples, i )
offset = offset + 4
end
end
function select_request_body(buffer, subtree)
--[[
<select_request_body> ::= <namespace_no><index_no>
<offset><limit><count><tuple>+
]]
local tree = subtree:add( tarantool_proto, buffer(),"Select body")
local namespace_no = buffer(0,4):le_uint()
local index_no = buffer(4, 4):le_uint()
local offset = buffer(8, 4):le_uint()
local limit = buffer(12,4):le_uint()
local count = buffer(16,4):le_uint()
if (limit == 4294967295) then
limit = limit .. ' (no limit)'
end
tree:add( buffer(0, 4), "Namespace # " .. namespace_no )
tree:add( buffer(4, 4), "Index # " .. index_no )
tree:add( buffer(8, 4), "Offset # " .. offset )
tree:add( buffer(12,4), "Limit # " .. limit )
tree:add( buffer(16,4), "Tuples count: " .. count )
add_tuples(buffer(20, buffer:len() - 20), tree, 'tuple', count)
end
function requestName(reqid)
local requests = {
[13] = "INSERT",
[17] = "SELECT",
[19] = "UPDATE",
[20] = "DELETE(obsolete)",
[21] = "DELETE",
[22] = "CALL",
[65280] = "PING",
}
return requests[reqid] or 'UNKNOWN'
end
function decodeErrorCode(buf)
local completion_status = buf(0,1):le_uint()
local error_code = buf(1):le_uint()
local result = "Code " .. completion_status
if ( completion_status == 0 ) then
return "0 (Ok)"
elseif ( completion_status == 1 ) then -- try again
return "1 (Try again) " .. 'code: ' .. error_code
elseif ( completion_status == 2 ) then
return "2 (Error)"
else
return completion_status .. " (Unknown error code) " .. ' code: ' .. error_code
end
end
function insert_request_body(buffer, subtree)
--[[
<insert_request_body> ::= <space_no><flags><tuple>
]]
local tree = subtree:add( tarantool_proto, buffer(),"Insert body")
local namespace_no = buffer(0,4):le_uint()
local flags = buffer(4, 4):le_uint()
tree:add( buffer(0, 4), "Namespace # " .. namespace_no )
tree:add( buffer(4, 4), "Flags # " .. flags )
add_one_tuple(buffer(8), tree, 0)
end
function update_request_body(buffer, subtree)
subtree:add( buffer,"Update data" )
end
function deletev13_request_body(buffer, subtree)
--[[
<delete_request_body> ::= <namespace_no><tuple>
]]
local tree = subtree:add( tarantool_proto, buffer(),"Delete body (v1.3)")
local namespace_no = buffer(0,4):le_uint()
tree:add( buffer(0, 4), "Namespace # " .. namespace_no )
add_one_tuple(buffer(4), tree, 1)
end
function delete_request_body(buffer, subtree)
subtree:add( buffer,"Delete data" )
end
function call_request_body(buffer, subtree)
--[[
<call_request_body> ::= <flags><proc_name><tuple>
]]
local tree = subtree:add( tarantool_proto, buffer,"Call data" )
local flags = buffer(0,4):le_uint()
tree:add( buffer(0, 4), "Namespace # " .. flags )
local field_length, used = leb128Unpack(buffer, 4)
local name = buffer(5, field_length):string()
tree:add( buffer(5, field_length), "name " .. name )
add_one_tuple(buffer(4 + 1 + field_length), tree, 0)
end
function ping_request_body(buffer, subtree)
subtree:add( buffer,"ping data" )
end
function unknown_request_body(buffer, subtree)
subtree:add( buffer,"Unknown command data" )
end
function unknown_response_body(buffer, subtree)
subtree:add( buffer,"Unknown response data" )
end
function insert_reponse_body(buffer, subtree)
--[[
<insert_response_body> ::= <count> | <count><fq_tuple>
]]
local tree = subtree:add( tarantool_proto, buffer(),"Insert response")
local count = buffer(0,4):le_uint()
tree:add( buffer(0, 4), "Affected rows " .. count )
if ( buffer:len() > 4 ) then
-- subtree:add( buffer(4),"Insert response data" )
add_fqtuple( buffer(4), subtree, "Select tuples", count)
end
end
function select_reponse_body(buffer, subtree)
--[[
<select_response_body> ::= <count><fq_tuple>*
]]
local tree = subtree:add( tarantool_proto, buffer(),"Select response")
local count = buffer(0,4):le_uint()
tree:add( buffer(0, 4), "Count: " .. count )
if ( buffer:len() > 4 ) then
add_fqtuple( buffer(4), subtree, "Select tuples", count)
end
end
function call_reponse_body(buffer, subtree)
--[[
<call_response_body> ::= <select_response_body>
]]
local tree = subtree:add( tarantool_proto, buffer(),"Call response")
local count = buffer(0,4):le_uint()
tree:add( buffer(0, 4), "Count: " .. count )
if ( buffer:len() > 4 ) then
add_fqtuple( buffer(4), subtree, "Call tuples", count)
end
end
function requestfunction(reqid)
local requests = {
[13] = insert_request_body,
[17] = select_request_body,
[19] = update_request_body,
[20] = deletev13_request_body, -- old delete
[21] = delete_request_body,
[22] = call_request_body,
[65280] = ping_request_body,
}
if (requests[reqid] == nil) then
return unknown_request_body
else
return requests[reqid]
end
end
function responsefunction(reqid)
local requests = {
[13] = insert_response_body,
[17] = select_reponse_body,
[19] = unknown_response_body,
[20] = unknown_response_body, -- old delete
[21] = unknown_response_body,
[22] = call_reponse_body,
[65280] = unknown_response_body,
}
if (requests[reqid] == nil) then
return unknown_request_body
else
return requests[reqid]
end
end
function readHeader(buffer, subtree)
--[[
<header> ::= <type><body_length><request_id>
]]
local req_type = buffer(0,4):le_uint()
local length = buffer(4,4):le_uint()
local req_id = buffer(8,4):le_uint()
local header = subtree:add( tarantool_proto, buffer(),"Header")
header:add( buffer(0,4),"Request Type: " .. req_type .. ' (' .. requestName(req_type) .. ')' )
header:add( buffer(4,4),"Body length: " .. length )
header:add( buffer(8,4),"Request ID: " .. req_id .. '[' .. string.format('%08X', req_id) .. ']' )
return buffer(12, buffer:len() - 12)
end
function request(buffer, subtree)
--[[
<request> ::= <header><request_body>
]]
local req_type = buffer(0,4):le_uint()
buffer = readHeader(buffer, subtree)
local requestfunction = requestfunction(req_type)
requestfunction(buffer, subtree)
end
function response(buffer, subtree)
--[[
<response> ::= <header><return_code>{<response_body>
]]
local req_type = buffer(0,4):le_uint()
buffer = readHeader(buffer, subtree)
if ( buffer:len() > 0 ) then
local code = buffer(0,4):le_uint()
if (code == 0) then
subtree:add( buffer(0,4),"Return code: " .. decodeErrorCode(buffer(0,4)) )
local requestfunction = responsefunction(req_type)
-- subtree:add( buffer(4),"Data" )
requestfunction(buffer(4), subtree)
else
subtree:add( buffer(0,4),"Return code: " .. decodeErrorCode(buffer(0,4)) )
end
end
end
-- create a function to dissect it
function tarantool_proto.dissector(buffer, pinfo, tree)
pinfo.cols.protocol = "TARANTOOL"
local body_length = buffer(4,4):le_uint()
local request_length = body_length + 12 -- 12 - header length
if (pinfo.src_port == 33013) then
-- answer, should have a response code
-- request_length = request_length + 4
end
-- debug('buffer: ' .. buffer:len())
-- debug('length: ' .. body_length)
if (buffer:len() < request_length) then
-- debug('reassemble required: ' .. (request_length - buffer:len()) )
pinfo.desegment_len = request_length - buffer:len()
pinfo.desegment_offset = 0
return 0
end
if (pinfo.src_port ~= 33013) then
-- debug('parsing')
local subtree = tree:add(tarantool_proto,buffer(),"Tarantool protocol data")
-- subtree:add( buffer(0,4),"Request Type: " .. buffer(0,4):le_uint() .. ' ' .. requestName(buffer(0,4):le_uint()) )
request(buffer, subtree)
else
local subtree = tree:add(tarantool_proto,buffer(),"Tarantool protocol data (response)")
response(buffer, subtree)
end
return request_length
end
-- load the udp.port table
tcp_table = DissectorTable.get("tcp.port")
-- register our protocol to handle udp port 7777
tcp_table:add(33013,tarantool_proto)