Peter
Peter

Reputation: 239

How to convert data from packet to integer in Wireshark using a Lua Dissector?

I am trying to convert the data inside a packet to an int but it is not working. I able to correctly add the fields to the subtree, but would like have access to the data as an integer to perform some other work.

I want to be able to use the variable len below as an int, but when I try to use the "tonumber" method, "Nil" is returned. I can convert it to a string using "tostring" but get nowhere with to number method.

I saw some examples that use the following code to convert to an integer:

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

But that produces the following error when I run it on my machine:

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

Here is the code I have that does everything correctly except where commented:

{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}

So my question is how do I convert the userdata type to an integer I can work with?

Upvotes: 1

Views: 4194

Answers (1)

tony19
tony19

Reputation: 138276

buf here is a TvbRange object, and there is no TvbRange.uint32(). You're looking for TvbRange.uint(). Try this update:

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

Upvotes: 3

Related Questions