Lua 中的 Wireshark 解析器 - 用户数据

发布于 2024-12-13 11:16:32 字数 777 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初懵 2024-12-20 11:16:32

ReverseOrder3Numbers 并没有什么真正的问题(除了可能缺少一些 local 限定符)。您应该更新您的问题以包含调用 add 的代码。

您可能不小心使用了 tree.add( ... ) 而不是 tree:add( ... ) (请注意 tree 后面的冒号>)。

There's nothing really wrong with ReverseOrder3Numbers (other than perhaps some missing local qualifiers). You should update your question to include the code that invokes add.

You might've accidentally used tree.add( ... ) instead of tree:add( ... ) (note the colon after tree).

没︽人懂的悲伤 2024-12-20 11:16:32

调用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. Use tree.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.

画骨成沙 2024-12-20 11:16:32

目前尚不清楚您传递给函数 ReverseOrder3Numbers 的内容。但由于您使用 uint 方法访问这些参数,我假设这些参数是 tvb:range(x,y) 结果。如果要更改各个值内的数字顺序,可以使用字节顺序感知方法:

function ReverseOrder3Numbers(hex_1, hex_2, hex_3)
local hex_1_int = hex_1:le_uint()
local hex_2_int = hex_2:le_uint()
local hex_3_int = hex_3:le_uint()
...
end

如果要更改添加到树中的数据的字节顺序,您应该使用 < 的字节顺序感知版本代码>添加方法。

tree:le_add(f_MyProtoField, tvb:range(x,y), ReverseOrder3Numbers(...))

我不知道为什么 le 在一种情况下是后缀,在另一种情况下是前缀。

It's not clear what you pass to the function ReverseOrder3Numbers. But since you access theses parameeters with the uint method I assume that the parameters are tvb: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:

function ReverseOrder3Numbers(hex_1, hex_2, hex_3)
local hex_1_int = hex_1:le_uint()
local hex_2_int = hex_2:le_uint()
local hex_3_int = hex_3:le_uint()
...
end

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.

tree:le_add(f_MyProtoField, tvb:range(x,y), ReverseOrder3Numbers(...))

I don't know the reason why le is suffix in the one case and a prefix in the other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文