This is a static, read-only archive of Wireshark Q&A. Older questions and answers from October 2017 and earlier can be found at osqa-ask.wireshark.org.
0

LUA: dissector wide global table

asked Nov 22 '17

updated Jan 30 '19

Hi,

I need to implement latency and concurrency analysis via Lua, similar to what is available in many other (builtin) dissectors. Typically the entry in the tree is called "Time from request", and the C dissectors simply allocate a global table of the requests to track, match the responses and display the result eventually.

However, I'm having a hard time trying to figure out how wireshark-lua lets me create a proto object, where I can add an object-wide table for reference across individual packet dissections (or even really global variables).

Also, there seems to be not a single sample of wireshark Lua where this is demonstrated.

For example, "self" seems to be missing in all object methods (?), and global variables stay "nil" (even if initialized) between packets...

Any help in figuring out how to implement a global table would be really appreciated.

thanks!

1 Answer

0

answered Nov 23 '17

updated Nov 23 '17

You have to declare a global table in the part of your Lua code where you register the protocol fields, as this part is run once before any dissection is started. Once this is done, you can access that table from your .dissector function. You must not attempt to register that global table as protocol field, it is just your working table. The .dissector function may use values from that table to populate registered protocol fields which are marked as "generated", i.e. those which are not physically present in the packet data but the dissector calculates (generates) them from physical data of the packet being dissected as well as other information sources. The protocol fields of this kind are marked as such by [ ] around them in the dissection tree pane.

The point is that the .dissector function is run over the packet data several times - first in sequence when the file is loaded, with each selection of the packet in the packet list pane, with each application of a display filter... and the global table does not become part of packet data. So whenever the .dissector function dissects the packet, it must create all fields again and hook them at the correct place to the dissection tree.

You also have to bear in mind the above when handling the contents of the global table. In general, you must check whether the item you are going to insert doesn't exist already, as in most cases you actually fill the table only during the initial sequential pass of Wireshark through the file.

Comments

The normal term for "synthetic" fields is "generated" as in generated by the dissector.

User 98587 ( Nov 23 '17 )

Thank you for reminding me, @User 98587, I've edited the Answer accordingly.

User 73968 ( Nov 23 '17 )

Thanks! I defined the base reference in the .init of the dissector, this seems to have done the trick. I keep my data in a rbtree for quickly checking if the entry already exists, seems to work so far.

One follow up question: In packet-rpc.c, this fragment adds a "hyperlink" to another frame, but there is nothing in the source code that is obvious how to make this happen:

2527    if(rpc_call->rep_num){
2528        proto_item *tmp_item;
2529
2530        tmp_item=proto_tree_add_uint_format(rpc_tree, hf_rpc_reqframe,
2531            tvb, 0, 0, rpc_call->rep_num,
2532            "The reply to this request is in frame %u",
2533            rpc_call->rep_num);
2534        PROTO_ITEM_SET_GENERATED(tmp_item);
2535    }


2641    if (rpc_call->req_num) {
2642        proto_item *tmp_item;
2643
2644        tmp_item=proto_tree_add_uint_format(rpc_tree, hf_rpc_repframe,
2645            tvb, 0, 0, rpc_call->req_num,
2646            "This is a reply to a request in frame %u",
2647            rpc_call->req_num);
2648        PROTO_ITEM_SET_GENERATED(tmp_item);
2649
2650        nstime_delta(&ns, &pinfo->abs_ts, &rpc_call->req_time);
2651        tmp_item=proto_tree_add_time(rpc_tree, hf_rpc_time, tvb, offset, 0,
2652            &ns);
2653        PROTO_ITEM_SET_GENERATED(tmp_item);
2654
2655        col_append_fstr(pinfo->cinfo, COL_INFO," (Call In %d)", rpc_call->req_num);
2656    }

In the wireshark view, these entries get a blue hyperlink, and a double-click will set the focus to the referred frame.

How is this achieved in the c dissector, and can LUA do the same?

User 31219 ( Nov 24 '17 )

Because the hf fieldtype is set to "FT_FRAMENUM":

{ &hf_rpc_reqframe, {
  "Request Frame", "rpc.reqframe", FT_FRAMENUM, BASE_NONE,
  NULL, 0, NULL, HFILL }},
{ &hf_rpc_repframe, {
  "Reply Frame", "rpc.repframe", FT_FRAMENUM, BASE_NONE,
  NULL, 0, NULL, HFILL }},
User 98587 ( Nov 25 '17 )
cc-by-sa Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.