如何使用Lua Dissector在Wireshark中将数据从数据包转换为整数?

时间:2012-05-11 19:49:54

标签: lua wireshark wireshark-dissector

我正在尝试将数据包内的数据转换为int,但它无法正常工作。我能够正确地将字段添加到子树,但是希望能够以整数形式访问数据以执行其他工作。

我希望能够将下面的变量len用作int,但是当我尝试使用“tonumber”方法时,返回“Nil”。我可以使用“tostring”将其转换为字符串,但无法使用数字方法。

我看到一些使用以下代码转换为整数的示例:

    local len = buf(0,4):uint32()

但是当我在我的机器上运行它时会产生以下错误:

     Lua error: attempt to call method "uint32" (a nil value)

以下是我所拥有的代码,除了评论的地方外,所有内容都正确:

{rest of code}
-- myproto dissector function function 
function (my_proto.dissector (buf, pkt, root) 

    -- create subtree for myproto 
    subtree = root:add(my_proto, buf(0)) 
    -- add protocol fields to subtree 
    subtree:add(f_messageLength, buf(0,4))

    -- This line does not work as it returns a nil value
    local len = tonumber(buf(0,4))

    -- This line produces a "bad argument #1 to 'set' (string expected, got nil) error"
    -- add message len to info column
    pkt.cols.info:set((tostring(len))))
    end
end
{rest of code}

所以我的问题是如何将userdata类型转换为我可以使用的整数?

1 个答案:

答案 0 :(得分:2)

buf这是一个TvbRange对象,没有TvbRange.uint32()。您正在寻找TvbRange.uint()。试试这个更新:

function (my_proto.dissector (buf, pkt, root) 
    subtree = root:add(my_proto, buf(0)) 
    subtree:add(f_messageLength, buf(0,4))

    local len = buf(0,4):uint()
    pkt.cols.info:set(len)
end
相关问题