-
Notifications
You must be signed in to change notification settings - Fork 4
/
msgpack_rpc_1_x.lua
184 lines (166 loc) · 5.77 KB
/
msgpack_rpc_1_x.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
--
-- The MIT License (MIT)
-- Copyright 2015 Sony Corporation
--
do
local PORTS = {9003, 37800} -- if you want to change port, modify this variable.
msgpack_rpc_proto = Proto("msgpack-rpc", "MessagePack-RPC")
local f = msgpack_rpc_proto.fields
f.type_field = ProtoField.uint8("msgpack-rpc.type", "Type")
f.msgid_field = ProtoField.uint32("msgpack-rpc.msgid", "Msgid")
f.method_field = ProtoField.string("msgpack-rpc.method", "Method")
f.params_field = ProtoField.string("msgpack-rpc.params", "Params")
f.error_field = ProtoField.string("msgpack-rpc.error", "Error")
f.result_field = ProtoField.string("msgpack-rpc.result", "Result")
local mp = require "MessagePack"
function table_print(tt)
local len = 0
local is_array = true
for key, value in pairs(tt) do
if "number" ~= type(key) then
is_array = false
end
len = len + 1
end
local sb = {}
if is_array then
table.insert(sb, "[")
else
table.insert(sb, "{")
end
local i = 1
for key, value in pairs(tt) do
if is_array then
table.insert(sb, stringify(value))
else
table.insert(sb, stringify(key) .. ": " .. stringify(value))
end
if i < len then
table.insert(sb, ", ")
end
i = i + 1
end
if is_array then
table.insert(sb, "]")
else
table.insert(sb, "}")
end
return table.concat(sb)
end
function stringify(obj)
if "nil" == type(obj) then
return tostring(nil)
elseif "table" == type(obj) then
return table_print(obj)
elseif "string" == type(obj) then
return "\'" .. obj .. "\'"
else
return tostring(obj)
end
end
function is_array(ar)
local is_array = true
if "table" == type(ar) then
for key, value in pairs(ar) do
if "number" ~= type(key) then
is_array = false
end
end
else
is_array = false
end
return is_array
end
function is_number(num)
if "number" == type(num) then
return true
end
return false
end
function is_string(str)
if "string" == type(str) then
return true
end
return false
end
local str
local substr
function msgpack_rpc_proto.dissector(tvb, pinfo, tree)
local proto_name = "MessagePack-RPC"
local b = tvb():bytes()
str = ""
for i = 0, b:len() - 1 do
str = str .. string.char(b:get_index(i))
end
substr = str:sub(1, 1)
if substr ~= string.char(0x94) and substr ~= string.char(0x93) then
return
end
local flag, ret = pcall(mp.unpack, str)
if not flag then
if ret:find("missing bytes$") then
pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
return
else
error(ret)
end
end
substr = str:sub(2, str:len())
local n = 1
local pair = {}
for csr, val in mp.unpacker(substr) do
pair[n] = {csr, val}
n = n + 1
end
local subtree = tree:add(msgpack_rpc_proto, tvb(), "MessagePack-RPC Protocol")
-- type
if pair[1][2] == 0 then -- requiest
subtree:add(f.type_field, tvb(pair[1][1], pair[2][1] - pair[1][1]), pair[1][2]):append_text(" (Request)")
-- msgid
if not is_number(pair[2][2]) then
return
end
subtree:add(f.msgid_field, tvb(pair[2][1], pair[3][1] - pair[2][1]), pair[2][2])
-- method
if not is_string(pair[3][2]) then
return
end
subtree:add(f.method_field, tvb(pair[3][1], pair[4][1] - pair[3][1]), pair[3][2], "Method: " .. stringify(pair[3][2]))
-- params
if not is_array(pair[4][2]) then
proto_name = proto_name .. " (dirty)"
end
subtree:add(f.params_field, tvb(pair[4][1], tvb:len() - pair[4][1]), stringify(pair[4][2]))
elseif pair[1][2] == 1 then -- response
subtree:add(f.type_field, tvb(pair[1][1], pair[2][1] - pair[1][1]), pair[1][2]):append_text(" (Response)")
-- msgid
if not is_number(pair[2][2]) then
return
end
subtree:add(f.msgid_field, tvb(pair[2][1], pair[3][1] - pair[2][1]), pair[2][2])
-- error
subtree:add(f.error_field, tvb(pair[3][1], pair[4][1] - pair[3][1]), stringify(pair[3][2]))
-- result
subtree:add(f.result_field, tvb(pair[4][1], tvb:len() - pair[4][1]), stringify(pair[4][2]))
elseif pair[1][2] == 2 then -- notify
subtree:add(f.type_field, tvb(pair[1][1], pair[2][1] - pair[1][1]), pair[1][2]):append_text(" (Notify)")
-- method
if not is_string(pair[2][2]) then
return
end
subtree:add(f.method_field, tvb(pair[2][1], pair[3][1] - pair[2][1]), pair[2][2], "Method: " .. stringify(pair[2][2]))
-- params
if not is_array(pair[3][2]) then
proto_name = proto_name .. " (dirty)"
end
subtree:add(f.params_field, tvb(pair[3][1], tvb:len() - pair[3][1]), stringify(pair[3][2]))
else
return
end
pinfo.cols.protocol = proto_name
end
tcp_table = DissectorTable.get("tcp.port")
for i, port in ipairs(PORTS) do
tcp_table:add(port, msgpack_rpc_proto)
end
end