First time here? Check out the FAQ!
THIS IS A TEST INSTANCE. Feel free to ask and answer questions, but take care to avoid triggering too many notifications.
0

help with LUA + ipv4 + append_text

  • retag add tags

in a LUA script I've multiple TLV tree items. Inside each tree item I'm able to visualize different type of variables, ie ipv4 addresses:

f.tlvvalue_ipv4 = ProtoField.ipv4 ("myproto.tlvvalue_ipv4", "TLV Value")
tlv_tree:add (f.tlvvalue_ipv4, buffer(offset, tlvlen))

Now I would like to repeat the same information (ipv4 address) in the append_text as well so it get visible avoiding to expand that specific tree item first.

For string I can do:

local tlvvalue_string=buffer(offset+4, tlvlen):string()
tlv_tree:append_text (", Value: " .. tlvvalue_string)

but not sure how to visualize the ipv4 or numbers...

So suppose buffer(offset+4, tlvlen) points to an ipv4.... how to print it with the append_text ?

Appreciated any help!!!

sezb51's avatar
9
sezb51
asked 2021-07-27 19:01:20 +0000
edit flag offensive 0 remove flag close merge delete

Comments

From a google search I found the following: "To print the address in the display tree, you need to find a way to convert those bytes to a string, or else find a different datatree:add() method that accepts the ipv4 format data. (If you can find the latter, that would be the better solution.)"

Is there any function that converts hex strings into printable ipv4 string ?

ie:

  local myip1=buffer(offset, tlvlen)
  local myip2=buffer(offset, tlvlen):bytes():tohex()
  tlv_tree:append_text (", myip1: " .. myip1 .. ", myip2: " .. myip2)

does provide:

  myip1: c0a81f21, myip2: C0A81F21

Thx

sezb51's avatar sezb51 (2021-07-28 12:38:04 +0000) edit
add a comment see more comments

1 Answer

0

There are at least 2 ways to achieve this.

  1. Use treeitem.text. For example:

    local ti = tlv_tree:add (f.tlvvalue_ipv4, buffer(offset, tlvlen))
    tlv_tree:append_text (", myip: " .. ti.text)

  2. Format the data from the buffer. For example:

    tlv_tree:append_text (string.format(", myip: %u.%u.%u.%u",
    buffer(offset, 1):uint(), buffer(offset + 1, 1):uint(),
    buffer(offset + 2, 1):uint(), buffer(offset + 3, 1):uint()))

Solution #1 has the advantage of displaying the resolved IP address for you as well, assuming resolution is enabled. If you don't want the resolved IP address displayed, then you can use Solution #2.

cmaynard's avatar
11.1k
cmaynard
answered 2021-07-28 16:43:13 +0000
edit flag offensive 0 remove flag delete link

Comments

Thank you so much! Both approaches works very nicely and actually I think to make use in other scenario as well.

Just as extra question... the treeitem.text return to me "TLV Value: 192.168.31.33" where the "TLV Value:" came from the ProtoField.ipv4 definition.... is there a quick way to extract from li.text just the ipv4 only ? :)

sezb51's avatar sezb51 (2021-07-28 19:15:54 +0000) edit

To eliminate the "TLV Value: " prefix from being displayed, you can try this:

tlv_tree:append_text ("," .. string.sub(ti.text, string.len("TLV Value: ")))

Ref: https://www.lua.org/manual/5.2/manual...

cmaynard's avatar cmaynard (2021-07-28 22:06:09 +0000) edit
1

It works like a charm!

sezb51's avatar sezb51 (2021-07-29 05:36:55 +0000) edit
add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer