This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

Dynamically create protofields

0

I would like to add protofields to my dissector so it is easier to filter on. However, the data for the dissector is contained in multiple lua files (as tables). I have the following code:

t = myproto.fields
for i in ipairs(tablename) do
   t.i = protofield.string(blahblah)
end

But this is not working. I've tried concatenating the i variable in a number of ways (t..i, t[i], etc) but it is not working. Is it possible to create dynamic variable names in lua like this?

asked 24 Nov '14, 07:12

hls's gravatar image

hls
16225
accept rate: 100%

edited 23 Dec '14, 01:20

Hadriel's gravatar image

Hadriel
2.7k2939


2 Answers:

0

I was able to get my code working, doing a combination of trial and error, as well as global tables.

tbl = string.upper(string.sub(file, 1, -5))
    _G["t"..tbl] = {}

I was then able to use that global variable to add a protofield string

_G["t"..tbl][i] = ProtoField.string(blahblah)

and finally, use that table to add to the fields table.

table.insert(myproto.fields,i,_G["t"..tbl][i])

Not the prettiest code, but it's currently working

answered 23 Dec '14, 09:35

hls's gravatar image

hls
16225
accept rate: 100%

1

What exact error message(s) are you getting?

Lua does indeed support variables for table indexes/keys, and the correct syntax would be:

t[i] = ProtoField.string(blahblah)

...but it's not going to work unless you did other stuff you're not showing in your question's Lua snippet. For example, "t = myproto.fields" won't return a table unless you've previously set myproto.fields to a table I believe; so using "t[i]" won't work because t isn't a Lua table.

You probably want to do this instead:

local t = {}
for i in ipairs(tablename) do
    t[i] = ProtoField.string(blahblah)
end
myproto.fields = t

answered 23 Dec '14, 01:20

Hadriel's gravatar image

Hadriel
2.7k2939
accept rate: 18%