Ask Your Question
0

help with LUA + ipv4 + append_text

asked 2021-07-27 19:01:20 +0000

sezb51 gravatar image

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!!!

edit retag flag offensive 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 gravatar imagesezb51 ( 2021-07-28 12:38:04 +0000 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-07-28 16:43:13 +0000

cmaynard gravatar image

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.

edit flag offensive delete link more

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 gravatar imagesezb51 ( 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 gravatar imagecmaynard ( 2021-07-28 22:06:09 +0000 )edit
1

It works like a charm!

sezb51 gravatar imagesezb51 ( 2021-07-29 05:36:55 +0000 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-07-27 18:59:46 +0000

Seen: 102 times

Last updated: Jul 28