1

How can I access the name property of a ProtoField after I declare it?

For example, something along the lines of:

myproto = Proto("myproto", "My Proto")

myproto.fields.foo = ProtoField.int8("myproto.foo", "Foo", base.DEC)

print(myproto.fields.foo.name)

Where I get the output:

Foo

2 Answers 2

1

An alternate method that's a bit more terse:

local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* myproto")

print(string.sub(fieldString, i + 2, j - (1 + string.len("myproto")))

EDIT: Or an even simpler solution that works for any protocol:

local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* ")

print(string.sub(fieldString, i + 2, j - 1))

Of course the 2nd method only works as long as there are no spaces in the field name. Since that's not necessarily always going to be the case, the 1st method is more robust. Here is the 1st method wrapped up in a function that ought to be usable by any dissector:

-- The field is the field whose name you want to print.
-- The proto is the name of the relevant protocol
function printFieldName(field, protoStr)

    local fieldString = tostring(field)
    local i, j = string.find(fieldString, ": .* " .. protoStr)

    print(string.sub(fieldString, i + 2, j - (1 + string.len(protoStr)))
end

... and here it is in use:

printFieldName(myproto.fields.foo, "myproto")
printFieldName(someproto.fields.bar, "someproto")
3
  • Thats a lot cleaner, but, I don't want to have to know that 'myproto' is part of the string. I am trying to find a way to pull the name without having to know any part of the field or its protocol beforehand. It needs to work for 'hisproto', 'someproto', 'otherprto', etc.
    – charlesw
    Aug 28, 2018 at 22:49
  • That still requires me know "myproto" or "someproto" at 'compile time' (Not sure what the proper term is with Lua). I was hoping for something that is just like printFieldName(myproto.fields.foo)
    – charlesw
    Aug 29, 2018 at 16:50
  • Sure, but you already need to know the name of the field with the relevant prefix, e.g., "myproto.fields.foo". I don't understand why this is a limitation but the knowing the field prefix isn't. Oh well. Aug 29, 2018 at 19:12
0

Ok, this is janky, and certainly not the 'right' way to do it, but it seems to work.

I discovered this after looking at the output of

print(tostring(myproto.fields.foo))

This seems to spit out the value of each of the members of ProtoField, but I couldn't figure out the correct way to access them. So, instead, I decided to parse the string. This function will return 'Foo', but could be adapted to return the other fields as well.

function getname(field) 

--First, convert the field into a string

--this is going to result in a long string with 
--a bunch of info we dont need

local fieldString= tostring(field)  
-- fieldString looks like:
-- ProtoField(188403): Foo  myproto.foo base.DEC 0000000000000000 00000000 (null) 

--Split the string on '.' characters
a,b=fieldString:match"([^.]*).(.*)" 
--Split the first half of the previous result (a) on ':' characters
a,b=a:match"([^.]*):(.*)"

--At this point, b will equal " Foo myproto" 
--and we want to strip out that abreviation "abvr" part

--Count the number of times spaces occur in the string
local spaceCount = select(2, string.gsub(b, " ", ""))

--Declare a counter
local counter = 0

--Declare the name we are going to return
local constructedName = ''

--Step though each word in (b) separated by spaces
for word in b:gmatch("%w+") do 
    --If we hav reached the last space, go ahead and return 
    if counter == spaceCount-1  then
        return constructedName
    end

    --Add the current word to our name 
    constructedName = constructedName .. word .. " "

    --Increment counter
    counter = counter+1
end
end 
1
  • I am going to avoid marking this as an accepted answer because, even though it works, it is nasty.
    – charlesw
    Aug 29, 2018 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.