如何添加到 Lua DissectorTable?
How to add to a Lua DissectorTable?
我正在为 Wireshark 编写一个复杂协议的 Lua 解析器。该协议有一个包含 msgType 字段的消息 header。我想为每种消息类型编写一个子解析器,每个子解析器都存储在一个单独的源文件中。
我的 top-level 脚本是 general.lua,它解析消息 header 并创建解析器 table:
DissectorTable.new("myProtocol.Message")
dofile(DATA_DIR.."cplane.lua")
cplane.lua 是消息类型 'cplane' 的子解析器,包含代码:
my_dissector_table = DissectorTable.get("myProtocol.Message")
my_dissector_table:add(0x02, myProtocol_cplane_proto)
两个脚本都在 Wireshark 插件目录的同一个子目录中。
当我加载插件时出现错误:
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins.4...."]:170: bad
argument #1 to 'dofile' (dofile: file does not exist)
我该如何解决这个问题?问题与脚本的加载顺序有关吗?是否需要调用 dofile()?
没有必要使用dofile
,因为插件目录中的所有脚本都已加载。然而加载的顺序不是固定的(至少,没有记录是固定的)。当前 Lua 插件在其他解析器之后加载,因此尝试在 "global scope" 中查找解析器表仅适用于内置解析器,例如 tcp.port
:
local myproto = Proto("myproto", "My Protocol")
function myproto.dissector(tvb, pinfo, tree)
...
end
-- Register with a built-in dissector table
DissectorTable.get("tcp.port"):add(1234, myproto)
要注册自定义解析器表,必须推迟注册。在 C 解剖器中,您可以将注册放在 proto_reg_handoff_PROTOABBREV
中(其中 PROTOABBREV
应该相应地替换),但在 Lua 中没有这样的功能。
最接近的是 "init" 例程(Proto
class、proto.init
的 属性。在打开捕获文件时,在剖析任何数据包之前调用这些。示例:
function myproto.init()
DissectorTable.get("your-custom-table"):add(1234, myproto)
end
Lua: Error during loading: [string "C:\Program Files
(x86)\Wireshark\plugins.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
答案:此错误表示未找到 Dissector table。原因可能是路径不正确,或者文件执行的顺序。
Lua: Error during loading: [string "C:\Program Files
(x86)\Wireshark\plugins.4...."]:170: bad argument #1 to 'dofile'
(dofile: file does not exist)
回答:对我来说,输入完全正确的路径后这个错误就消失了
我正在为 Wireshark 编写一个复杂协议的 Lua 解析器。该协议有一个包含 msgType 字段的消息 header。我想为每种消息类型编写一个子解析器,每个子解析器都存储在一个单独的源文件中。
我的 top-level 脚本是 general.lua,它解析消息 header 并创建解析器 table:
DissectorTable.new("myProtocol.Message")
dofile(DATA_DIR.."cplane.lua")
cplane.lua 是消息类型 'cplane' 的子解析器,包含代码:
my_dissector_table = DissectorTable.get("myProtocol.Message")
my_dissector_table:add(0x02, myProtocol_cplane_proto)
两个脚本都在 Wireshark 插件目录的同一个子目录中。
当我加载插件时出现错误:
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins.4...."]:170: bad
argument #1 to 'dofile' (dofile: file does not exist)
我该如何解决这个问题?问题与脚本的加载顺序有关吗?是否需要调用 dofile()?
没有必要使用dofile
,因为插件目录中的所有脚本都已加载。然而加载的顺序不是固定的(至少,没有记录是固定的)。当前 Lua 插件在其他解析器之后加载,因此尝试在 "global scope" 中查找解析器表仅适用于内置解析器,例如 tcp.port
:
local myproto = Proto("myproto", "My Protocol")
function myproto.dissector(tvb, pinfo, tree)
...
end
-- Register with a built-in dissector table
DissectorTable.get("tcp.port"):add(1234, myproto)
要注册自定义解析器表,必须推迟注册。在 C 解剖器中,您可以将注册放在 proto_reg_handoff_PROTOABBREV
中(其中 PROTOABBREV
应该相应地替换),但在 Lua 中没有这样的功能。
最接近的是 "init" 例程(Proto
class、proto.init
的 属性。在打开捕获文件时,在剖析任何数据包之前调用这些。示例:
function myproto.init()
DissectorTable.get("your-custom-table"):add(1234, myproto)
end
Lua: Error during loading: [string "C:\Program Files (x86)\Wireshark\plugins.4...."]:9: bad argument #1 to 'get' (DissectorTable_get: no such dissector_table)
答案:此错误表示未找到 Dissector table。原因可能是路径不正确,或者文件执行的顺序。
Lua: Error during loading: [string "C:\Program Files (x86)\Wireshark\plugins.4...."]:170: bad argument #1 to 'dofile' (dofile: file does not exist)
回答:对我来说,输入完全正确的路径后这个错误就消失了