如何处理 Wireshark Lua 解剖器中的位字段?

How to handle bit fields in Wireshark Lua dissector?

我需要在 Wireshark lua 解析器中解析位映射八位组。八位字节的格式为:

bit 0:     Concatenation (0=No concatenation, 1=Concatenation)
bits 1..3: Reserved
bits 4..7: Version

我已经成功剖析了它:

Concatenation_F = ProtoField.uint8("Concatenation", "Concatenation", base.DEC, NULL, 0x1)
Version_F = ProtoField.uint8("Version", "Version", base.DEC, NULL, 0xF0)

my_protocol.fields = { Concatenation_F,
                   Version_F
}

<snip>

local Concatenation_range = buffer(0,1)
local Version_range = buffer(0,1)

local Concatenation = Concatenation_F:uint()
local Version = Version_range:uint()

subtree:add(Concatenation_F, Concatenation_range, Concatenation)
subtree:add(Version_F, Version_range, Version)

可行,但我想显示串联字段的含义,例如:

但为此我需要获取连接位的值。我该怎么做?

有2种解法。通常,您只需引入一个值字符串并在 ProtoField 调用中使用它。例如:

local yesno_types = {
    [0] = "No",
    [1] = "Yes"
}

Concatenation_F = ProtoField.uint8("Concatenation", "Concatenation", base.DEC, yesno_types, 0x1)

参考第11.6.7节。 Wireshark Developer's Guide 的 ProtoField 了解更多信息。

但如果您仍想获取位域的值,则可以使用 Lua BitOp 支持来实现,该支持已经可供您使用。所以,像这样:

local function get_concat(x) return bit.band(x, 0x01) end

local concat = get_concat(buffer(0, 1):uint())