Lua: populate a field from DissectorTable
I'm dissecting a protocol where one of fields specifies the request kind. Dissectors for request kinds are registered in a DissectorTable
:
-- proto.lua
local kinds_table = DissectorTable.new("proto.request_kind", "Proto Requests", ftypes.UINT8)
-- proto_kindA.lua
local kindA = Proto("PROTO-A", "Protocol Request Kind A")
func kindA.init()
DissectorTable.get("proto.request_kind"):set(0x55, kindA)
end
Main dissector then uses
kinds_table:try(kind, cmd:tvb(), pinfo, tree)
to render the subprotocol.
Everything is fine except the "kind" field in the protocol: it would be very useful to populate it with the name of the protocol. How do I get it from kinds_table
or in some other way?
I could keep a map of kinds in the main file, though it's error-prone: there are 6 tables for kinds, each containing ~200 entries, so I hope there is a better way to do so.
I'm struggling to understand exactly what you're hoping to do. Could you elaborate a bit more and maybe even provide some sample code of what you'd like to do?
I'm trying to do what Wireshark does in dissecting, say, decoding
Protocol
field in IP. IP dissector delegates to aip.proto
table.Is it possible to fill a field similar to IP
Protocol
without having a list of all possible protocols hardcoded in the parent dissector? The name of the protocol is in theDissectorTable
, the only question is how to get it out.I think I understand now.
I'm not sure how you could get the name from the proto; however, it might be possible to do something like so? (NOTE: This is all untested.)
Create a new file such as
kind_name.lua
that initially has an emptykind_name_vals
lookup table, as in:In each subdissector file, add a line to pull in that file:
During each subdissector initialization, add the particular kind entry to the table:
Abuse the return value of the subdissector and instead of each subdissector returning the number of bytes dissected, it could return the kind value, e.g., 0x55 for "PROTO-A".
Finally, in the main dissector, if the
kinds_table:try()
returns a value greater than 0, use it to look up the name in thekind_name_vals
table and add it or append it to thekind
tree item.This approach wouldn ...(more)
Actually, it seems
pinfo.private
isn't read-only at all. I guess I don't understand what affectWSLUA_ATTRIBUTE_ROREG()
does, but perhaps it should be changed toWSLUA_ATTRIBUTE_RWREG()
for this field. See wslua_pinfo.c.Ref: https://ask.wireshark.org/question/10...
In any case, I just tried it in my own Lua dissectors and it seems to work just fine. In the subdissector, I set
pinfo.private.name = "FOO"
, and then in the calling dissector, I just added a quick block of code to test it:In the Lua console, the following was printed:
I still don't know if this helps in your particular case but this might be an easier way to pass the kind name information from the subdissector back to the calling dissector.
Wait, all scripts have a shared global namespace? Then I'll figure it out.
I thought every script is loaded in a separate Lua VM.