Lua错误"尝试调用全局",在使用loadstring()时出现Wireshark解剖器

我正在尝试在Wireshark解剖器内部使用loadstring动态定义的自定义函数调用。

在另一个类似的Lua脚本内部使用此方法时,它可以正常工作:

function ftest(s)
  return s..s
end

k="knock "
fcallstr="rs = ftest(k)"
loadstring(fcallstr)()
print("now rs = " .. rs)

--现在rs = knock knock

但是如果我尝试根据解剖器示例执行相同的操作([https:// wiki.wireshark.org/Lua/Dissectors] (https://wiki.wireshark.org/Lua/Dissectors)),我会收到错误:

function mycustomfnc(buffer,subtree)
  print(os.time().."  ".." FUNCTION REACHED! ")
  subtree:add(buffer(4,2),"Plus 5th and 6th bytes!: "..buffer(4,2):uint())
end

--声明我们的协议
trivial_proto = Proto("trivial","Trivial Protocol")
--创建一个用于解剖的函数
function trivial_proto.dissector(buffer,pinfo,tree)
    pinfo.cols.protocol = "TRIVIAL"
    local subtree = tree:add(trivial_proto,buffer(),"Trivial Protocol Data")
    subtree:add(buffer(0,2),"The first two bytes: " .. buffer(0,2):uint())
    subtree = subtree:add(buffer(2,2),"The next two bytes")
    subtree:add(buffer(2,1),"The 3rd byte: " .. buffer(2,1):uint())
    subtree:add(buffer(3,1),"The 4th byte: " .. buffer(3,1):uint())

    --这个可以正常工作
    --mycustomfnc(buffer, subtree)

    --这个失败了:
    srcfncall="mycustomfnc(buffer,subtree)"
    loadstring(srcfncall)()
    --结果:
    --Lua错误:[string"mycustomfnc(buffer,subtree)"]:1:尝试调用全局'mycustomfnc'(空值)

end

--加载udp.port表
udp_table = DissectorTable.get("udp.port")
--注册我们的协议以处理udp端口xxxx
udp_table:add(5001,trivial_proto)

我在这里缺少什么?谢谢您的帮助。

原文链接 https://stackoverflow.com/questions/70144563

点赞