Lua 中的 Wireshark 解析器 - 用户数据
我是 Lua 新手,正在为 Wireshark 构建一个自定义解析器。我的情况是这样的:
wireshark数据由十六进制数字组成,例如4321 8765 CBA9。我想要结束的是(在剖析之后):CBA9 8765 4321。
到目前为止,我所做的是在 Lua 中创建一个小函数,它将单独获取这些数字,将它们转换为字符串,并将它们放入正确的顺序。
function ReverseOrder3Numbers(hex_1, hex_2, hex_3)
local hex_1_int = hex_1:uint()
local hex_2_int = hex_2:uint()
local hex_3_int = hex_3:uint()
word1 = string.format("%04X", hex_1_int)
word2 = string.format("%04X", hex_2_int)
word3 = string.format("%04X", hex_3_int)
combined_string = "0x" .. word3 .. word2 .. word1
output = combined_string
return output
end
然而,一旦我将这堆数据添加到树中,我就会收到一条错误消息:Lua Error: ...: Calling 'add' on bad self (userdata Expected, got string)。
我该如何解决这个问题?我需要完全不同的方法吗?我并不是在寻找任何复杂或奇特的东西。我所需要做的就是我所描述的。任何帮助将不胜感激。
I am new to Lua, and I am building a custom dissector for Wireshark. My situation is this:
The wireshark data consists of hex numbers such as 4321 8765 CBA9. What I would like to wind up with is (after it has been dissected) : CBA9 8765 4321.
What I have done so far is create a small function in Lua that will take these numbers individually, convert them to strings, and places them in the correct order.
function ReverseOrder3Numbers(hex_1, hex_2, hex_3)
local hex_1_int = hex_1:uint()
local hex_2_int = hex_2:uint()
local hex_3_int = hex_3:uint()
word1 = string.format("%04X", hex_1_int)
word2 = string.format("%04X", hex_2_int)
word3 = string.format("%04X", hex_3_int)
combined_string = "0x" .. word3 .. word2 .. word1
output = combined_string
return output
end
However, once I go to add this bunch to the tree, I get an error saying Lua Error: ...: calling 'add' on bad self (userdata expected, got string).
How can I get around this? Do I need a different approach entirely? I am not looking for anything complex or fancy. All I need to do is what I described. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ReverseOrder3Numbers
并没有什么真正的问题(除了可能缺少一些local
限定符)。您应该更新您的问题以包含调用add
的代码。您可能不小心使用了
tree.add( ... )
而不是tree:add( ... )
(请注意tree
后面的冒号>)。There's nothing really wrong with
ReverseOrder3Numbers
(other than perhaps some missinglocal
qualifiers). You should update your question to include the code that invokesadd
.You might've accidentally used
tree.add( ... )
instead oftree:add( ... )
(note the colon aftertree
).调用
tree:add()
将向对象“tree”发送“tree”本身的直接链接作为第一个隐式参数。无论您将多少参数附加到此调用中,或者根本不附加任何参数。如果您的“add”方法不支持自链接,请使用tree.add()
语法。在这种情况下,“self”应该链接到“add”方法内的“tree”对象。Call
tree:add()
will send to the object 'tree' the direct link to 'tree' itself as first implicitly argument. And no matter how much args you will attach to this call or no one at all. Usetree.add()
sintax if your 'add' method doesn't support self-link. In this case 'self' should be linked to the 'tree' object inside the 'add' method.目前尚不清楚您传递给函数
ReverseOrder3Numbers
的内容。但由于您使用uint
方法访问这些参数,我假设这些参数是tvb:range(x,y)
结果。如果要更改各个值内的数字顺序,可以使用字节顺序感知方法:如果要更改添加到树中的数据的字节顺序,您应该使用 < 的字节顺序感知版本代码>添加方法。
我不知道为什么
le
在一种情况下是后缀,在另一种情况下是前缀。It's not clear what you pass to the function
ReverseOrder3Numbers
. But since you access theses parameeters with theuint
method I assume that the parameters aretvb:range(x,y)
results. If you want to change the order of the digits inside the individual values, you can use the endianess-aware methods:If you want to change the endianess of data that is added to the tree you should use the endianess-aware version of the
add
method.I don't know the reason why
le
is suffix in the one case and a prefix in the other.