0

I have a program which checks for conditions some variable field, like

if(tostring(field) == '0') then {do something}
if(tostring(field) == '1') then {do something}  
if(tostring(field) == '2') then {do something}  

But, i think lua is interpreting '0' and '1' as TRUE/FALSE values and not checking the corresponding if conditions properly. The condition executes properly for field == '2' condition.
How can i overcome this case? How can i make it work for check conditions '0' and '1'?
Thank You in advance!
In case you are wondering why i tagged wireshark, the if check condition is checking for a field in pcap file.
My lua code for reference is as follows:

#!/usr/bin/lua

do
    local pkts = 0
    local stat = {}
    local file = io.open("luawrite","w")
    local function init_listener()
            local tap = Listener.new("wlan")
            local src_addr = Field.new("wlan.sa")
            local type = Field.new("wlan.fc.type")
            local sub_type = Field.new("wlan.fc.subtype")
            local frame_length = Field.new("frame.len")
            local data_rate = Field.new("wlan.data_rate")
            function tap.reset()
                    pkts = 0;
            end

            function tap.packet(pinfo, tvb)
                    local client = src_addr()
                    local stype = sub_type()
                    local ty = type()
                    local ts = tostring(pinfo.rel_ts)
                    local fl = frame_length()
                    rate = data_rate()
                    if(tostring(ty) == '0')  then
                            file:write(tostring(ts), "\t", tostring(fl), "\t", tostring(rate), "\n")
                    end
            end
    end
    init_listener()
end  

The condition i am referring to 7th line from last line. If i give the condition tostring(ty) == '2', it works properly.

2
  • What is the output of file:write(_G.type(ty), '\t', tostring(ty)) in wireshark? Commented Jun 19, 2013 at 21:18
  • 1
    Sanity check: have you verified that the type evaluates to something other than 2? Perhaps your packet capture contains only type 2. Commented Jun 20, 2013 at 4:06

2 Answers 2

1

From the manual:

The condition expression of a control structure can return any value. Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).

Both the number 0 and the empty string evaluate to true, so it's definitely not mistaking the string "0" for false. I might avoid redefining type. Also, I think frame_type returns a number so you can get rid of the tostring() in the condition.

do
    local pkts = 0
    local stat = {}
    local file = io.open("luawrite","w")

    local function init_listener()
        local tap = Listener.new("wlan")
        local src_addr = Field.new("wlan.sa")
        -- Changed function from type to frame_type
        local frame_type = Field.new("wlan.fc.type")
        local sub_type = Field.new("wlan.fc.subtype")
        local frame_length = Field.new("frame.len")
        local data_rate = Field.new("wlan.data_rate")
        function tap.reset()
            pkts = 0;
        end

        function tap.packet(pinfo, tvb)
            local client = src_addr()
            local stype = sub_type()
            local ty = frame_type()
            local ts = tostring(pinfo.rel_ts)
            local fl = frame_length()
            rate = data_rate()
            -- skip the tostring
            if ty == 0 then
                file:write(tostring(ts), "\t", tostring(fl), "\t", tostring(rate), "\n")
            end
        end
    end
    init_listener()
end

If all else fails, try writing a line regardless of the frame type and write the frame type with it:

function tap.packet(pinfo, tvb)
    local client = src_addr()
    local stype = sub_type()
    local ty = frame_type()
    local ts = tostring(pinfo.rel_ts)
    local fl = frame_length()
    rate = data_rate()               
    file:write(tostring(ty), "\t", tostring(ts), "\t", tostring(rate), "\n")
end

Then you can see which frame types you're receiving.

Sign up to request clarification or add additional context in comments.

Comments

1

As Corbin said, it's definitely not interpreting "0" and "1" as TRUE/FALSE. Wireshark runs the stock Lua interpreter, and Lua only considers nil and the boolean value of false as false values. Of course you're not really checking if they're false values, you're doing a string comparison of the string-ified value of the "wlan.fc.type" field value to the string "0" or the string "1" or whatever. The actual value you get for the "wlan.fc.type" field is a number, so as Corbin said there's no need to convert it to a string and string-compare it to a string of "0" or whatever... just compare them as numbers.

Regardless, stringifying both of them should have worked too (just been less efficient), so the odds are there simply isn't an 802.11 packet with a wlan.fc.type of 0 in your capture. A wlan.fc.type of 0 is a management frame, a 1 is a control frame, and a 2 is a data frame. So the odds are you're only capturing 802.11 data packets, which is why your comparison of the string-ified wlan.fc.type to the string "2" succeeds.

One way to find out is to open your capture file in wireshark, and put in a display filter for "wlan.fc.type == 0" and see if any packets are shown. If not, then you don't have any management packets.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.