"tvb(offset):string()" 是什么意思?

What does "tvb(offset):string()" mean?

这几天,我正在学习如何使用Lua编写WireShark plugin.But我不知道tvb(offset):string是什么意思。为什么使用 tostring(tvb(offset)) 是错误的?谢谢你的回答

function weibo.dissector(tvb, pinfo, tree)
    local proto_type = tvb(23, 1):uint();
    if(proto_type ~= 0x06) then
        return
    end

    local offset = get_payload_offset(tvb, proto_type)
    local data = tvb(offset):string();
    local i, j = string.find(data, "weibo")
    if(i) then
        pinfo.cols.protocol = weibo.name
        local subtree = tree:add(weibo, tvb(offset+i-1))
        subtree:append_text(", ptn_pos: " .. i .. "-" .. j)
    end
end

tvb 是 Testy Virtual Buffer.

A Tvb ("Testy Virtual Buffer") represents the packet's buffer. It is passed as an argument to listeners and dissectors, and can be used to extract information (via TvbRange) from the packet's data. Beware that Tvbs are usable only by the current listener or dissector call and are destroyed as soon as the listener/dissector returns, so references to them are unusable once the function has returned.

作为 tvb(offset) 的呼叫 returns a TvbRange

A range of bytes within a Tvb that is used to extract data. A TvbRange is created from tvb:__call() or tvb:range([offset] [,length]).

备注: tvb是个Luatable。如果您像 tvb() 那样调用它,则会调用 __call() metamethod

https://wiki.wireshark.org/LuaAPI/Tvb#tvb:__call.28.29

tvb:__call()

Description

Creates a TvbRange from a subset of this Tvb. Same as tvb:range(). Cannot be directly called.

Returns

userdata : The new TvbRange

TvbRange 的众多方法之一是 tvbrange:string()

tvbrange:string()

Description

Gets a string from the TvbRange

Returns

string : The string, containing all bytes in the TvbRange including all zeroes (e.g., "a[=20=]0bc[=20=]0")

另一方面,

tostring() 是 Lua 的标准函数之一。

来自https://www.lua.org/manual/5.3/manual.html#pdf-tostring

Receives a value of any type and converts it to a string in a human-readable format. (For complete control of how numbers are converted, use string.format.) If the metatable of v has a __tostring field, then tostring calls the corresponding value with v as argument, and uses the result of the call as its result.

由于 tvb 实现了 __tostring() tostring(tvb) 将 return 该元方法的 return 值。

tvb:__tostring()

Description

Gets a string representation of the Tvb. Cannot be directly called.

Returns

string : The string representation