Lua错误地解释检查条件。

我有一个程序,检查某些变量字段的条件,例如

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

但是,我认为 lua 把 '0' 和 '1' 解释为 TRUE/FALSE 值,没有正确地检查相应的 if 条件。该条件适用于 field == '2' 的条件。

我该怎么解决这种情况?如何使其适用于检查条件 '0' 和 '1'?

提前感谢!

如果你想知道我为什么标记了 Wireshark,if 检查条件正在检查 pcap 文件中的字段。

我参考的 Lua 代码如下:

#!/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

我参考的是倒数第 7 行的条件。如果我给出条件 tostring(ty) == '2',它就能正常工作。

点赞
用户7625
用户7625

从[手册](http://www.lua.org/manual/5.2/manual.html#3.3.4)中:

控制结构的条件表达式可以返回任何值。 false和nil都被认为是假的。所有与nil不同的值 false都被认为是真的(特别是,数字0和 空字符串也是真的)。

数字0和空字符串都计算为true,因此它绝对不会将字符串“0”误认为false。我可能会避免重新定义'type'。此外,我认为frame_type返回一个数字,因此您可以在条件中消除“tostring()”。

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")
        -- 将函数从类型更改为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()
            -- 跳过tostring
            if ty == 0 then
                file:write(tostring(ts), "\t", tostring(fl), "\t", tostring(rate), "\n")
            end
        end
    end
    init_listener()
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()
    file:write(tostring(ty), "\t", tostring(ts), "\t", tostring(rate), "\n")
end

然后您可以看到接收到哪些帧类型。

2013-06-19 20:22:11
用户3395060
用户3395060

正如 Corbin 所说的那样,Wireshark 绝对没有将“0”和“1”解释为 TRUE/FALSE。Wireshark 运行标准的 Lua 解释器,而 Lua 只将 nil 和布尔值 false 视为 false _值_。当然,你其实并没有检查它们是 false 值,你只是将“wlan.fc.type”字段值的字符串化值与字符串“0”或字符串“1”进行字符串比较。实际的“wlan.fc.type”字段值是一个数字,所以就像 Corbin 说的那样,没有必要将其转换为字符串并将其与“0”之类的字符串进行字符串比较...只需将它们比较为数字。

无论如何,将它们都作为字符串化值应该也能够工作(只是效率更低),所以很有可能在你的捕获中根本没有一个 wlan.fc.type 的值为 0 的 802.11 数据包。wlan.fc.type 为 0 表示管理帧、1 表示控制帧,2 表示数据帧。因此,你只捕获了 802.11 数据包,这就是为什么你将 wlan.fc.type 进行字符串化后与字符串“2”进行比较成功的原因。

查找的方法之一是在 Wireshark 中打开你的捕获文件,然后设置一个显示过滤器,“wlan.fc.type == 0”,看看是否会显示任何数据包。如果没有显示出来 ,那么你就没有任何管理数据包。

2014-03-09 01:01:28