Wireshark lua脚本分析CoAP选项

时间:2019-06-12 06:52:04

标签: lua wireshark coap

我正在编写lua脚本来剖析coap协议。但是,如果有多个相同的选项,我将无法获得2nd或更高版本的coap选项(URI-Path)。

do
 local test_proto = Proto("test_proto", "Test Protocol")
 local test_uripath = ProtoField.string("test.uripath", "Uri-Path")
 test_proto.fields = {test_uripath}
 local coap_uripath = Field.new("coap.opt.uri_path")
 function test_proto.dissector(tvbuffer, pinfo, treeitem)
  local subtree = treeitem:add(test_proto)
  subtree:add(test_uripath, tostring(coap_uripath().value))
 end
register_postdissector(test_proto)
end

即使coap URI-Path选项具有如下所示的多个值,也只会在子树上显示第一个URI-Path。

Opt Name: #1: URI-Path: XXX
Opt Name: #2: URI-Path: YYY

我只能使用coap.opt.uri_path获得XXX。如何获得第二个或更高版本的相同选项字段?

1 个答案:

答案 0 :(得分:0)

如果您对所有字段感兴趣,而不仅是第一个字段,那么您将需要处理整个表。例如:

do
    local test_proto = Proto("test_proto", "Test Protocol")
    local test_uripath = ProtoField.string("test.uripath", "Uri-Path")
    test_proto.fields = {test_uripath}

    local coap_uripath = Field.new("coap.opt.uri_path")

    function test_proto.dissector(tvbuffer, pinfo, treeitem)
        local subtree = treeitem:add(test_proto)
        local coap_uripath_table = { coap_uripath() }

        for i,uripath in ipairs(coap_uripath_table) do
            subtree:add(test_uripath, tostring(uripath.value))
        end
    end

    register_postdissector(test_proto)
end

另请参阅:
https://osqa-ask.wireshark.org/questions/35682/lua-accessing-multiple-smb2msg_id-values
https://osqa-ask.wireshark.org/questions/1579/fetching-multiple-named-values-with-lua