1 tapping with stats_tree
3 Let's suppose that you want to write a tap only to keep counters, and you
4 don't want to get involved with GUI programming or maybe you'd like to make
5 it a plugin. A stats_tree might be the way to go. The stats_tree module takes
6 care of the representation (GUI for wireshark and text for tshark) of the
7 tap data. So there's very little code to write to make a tap listener usable
8 from both wireshark and tshark.
10 First, you should add the TAP to the dissector in question as described in
13 Once the dissector in question is "tapped" you have to write the stats tree
14 code which is made of three parts:
16 The init callback routine:
17 which will be executed before any packet is passed to the tap. Here you
18 should create the "static" nodes of your tree. As well as initialize your
21 The (per)packet callback routine:
22 As the tap_packet callback is going to be called for every packet, it
23 should be used to increment the counters.
26 It is called at the destruction of the stats_tree and might be used to
29 Other than that the stats_tree should be registered.
31 If you want to make it a plugin, stats_tree_register() should be called by
32 plugin_register_tap_listener() read README.plugin for other information
33 regarding wireshark plugins.
35 If you want it as part of the dissector stats_tree_register() can be called
36 either by proto_register_xxx() or if you prefer by proto_reg_handoff_xxx().
39 A small example of a very basic stats_tree plugin follows.
41 ----- example stats_tree plugin ------
42 /* udpterm_stats_tree.c
43 * A small example of stats_tree plugin that counts udp packets by termination
44 * 2005, Luis E. G. Ontanon
46 * Wireshark - Network traffic analyzer
47 * By Gerald Combs <gerald@wireshark.org>
48 * Copyright 1998 Gerald Combs
50 * SPDX-License-Identifier: GPL-2.0+
61 #include <epan/stats_tree.h>
62 #include <epan/dissectors/udp.h>
64 static int st_udp_term;
65 static gchar* st_str_udp_term = "UDP terminations";
67 /* this one initializes the tree, creating the root nodes */
68 extern void udp_term_stats_tree_init(stats_tree* st) {
69 /* we create a node under which we'll add every termination */
70 st_udp_term = stats_tree_create_node(st, st_str_udp_term, 0, TRUE);
73 /* this one will be called with every udp packet */
74 extern int udp_term_stats_tree_packet(stats_tree *st, /* st as it was passed to us */
75 packet_info *pinfo, /* we'll fetch the addresses from here */
76 epan_dissect_t *edt _U_, /* unused */
77 const void *p) /* we'll use this to fetch the ports */
79 static guint8 str[128];
80 e_udphdr* udphdr = (e_udphdr*) p;
82 /* we increment by one (tick) the root node */
83 tick_stat_node(st, st_str_udp_term, 0, FALSE);
85 /* we then tick a node for this src_addr:src_port
86 if the node doesn't exists it will be created */
87 g_snprintf(str, sizeof(str),"%s:%u",address_to_str(&pinfo->net_src),udphdr->sport);
88 tick_stat_node(st, str, st_udp_term, FALSE);
90 /* same thing for dst */
91 g_snprintf(str, sizeof(str),"%s:%u",address_to_str(&pinfo->net_dst),udphdr->dport);
92 tick_stat_node(st, str, st_udp_term, FALSE);
98 WS_DLL_PUBLIC_DEF const gchar version[] = "0.0";
100 WS_DLL_PUBLIC_DEF void plugin_register_tap_listener(void) {
102 stats_tree_register_plugin("udp", /* the proto we are going to "tap" */
103 "udp_terms", /* the abbreviation for this tree (to be used as -z udp_terms,tree) */
104 st_str_udp_term, /* the name of the menu and window (use "/" for sub menus)*/
105 0, /* tap listener flags for per-packet callback */
106 udp_term_stats_tree_packet, /* the per packet callback */
107 udp_term_stats_tree_init, /* the init callback */
108 NULL ); /* the cleanup callback (in this case there isn't) */
117 every stats_tree callback has a stats_tree* parameter (st), stats_tree is an obscure
118 data structure which should be passed to the api functions.
120 stats_tree_register( tapname, abbr, name, flags, packet_cb, init_cb, cleanup_cb);
121 registers a new stats tree
123 stats_tree_register_plugin(tapname, abbr, name, flags, packet_cb, init_cb, cleanup_cb);
124 registers a new stats tree from a plugin
126 stats_tree_parent_id_by_name( st, parent_name)
127 returns the id of a candidate parent node given its name
133 All the functions that operate on nodes return a parent_id
135 stats_tree_create_node(st, name, parent_id, with_children)
136 Creates a node in the tree (to be used in the in init_cb)
137 name: the name of the new node
138 parent_id: the id of the parent_node (NULL for root)
139 with_children: TRUE if this node will have "dynamically created" children
140 (i.e. it will be a candidate parent)
143 stats_tree_create_node_by_pname(st, name, parent_name, with_children);
144 As before but creates a node using its parent's name
147 stats_tree_create_range_node(st, name, parent_id, ...)
148 stats_tree_create_range_node_string(st, name, parent_id, num_str_ranges, str_ranges)
149 stats_tree_range_node_with_pname(st, name, parent_name, ...)
150 Creates a node in the tree, that will contain a ranges list.
152 stats_tree_create_range_node(st,name,parent_id,
153 "-99","100-199","200-299","300-399","400-", NULL);
155 stats_tree_tick_range( st, name, parent_id, value_in_range);
156 stats_tree_tick_range_by_pname(st,name,parent_name,value_in_range)
157 Increases by one the ranged node and the sub node to whose range the value belongs
160 stats_tree_create_pivot(st, name, parent_id);
161 stats_tree_create_pivot_by_pname(st, name, parent_name);
162 Creates a "pivot node"
164 stats_tree_tick_pivot(st, pivot_id, pivoted_string);
165 Each time a pivot node will be ticked it will get increased, and, it will
166 increase (or create) the children named as pivoted_string
169 the following will either increase or create a node (with value 1) when called
171 tick_stat_node(st,name,parent_id,with_children)
172 increases by one a stat_node
174 increase_stat_node(st,name,parent_id,with_children,value)
175 increases by value a stat_node
177 set_stat_node(st,name,parent_id,with_children,value)
178 sets the value of a stat_node
180 zero_stat_node(st,name,parent_id,with_children)
181 resets to zero a stat_node
183 Averages work by tracking both the number of items added to node (the ticking
184 action) and the value of each item added to the node. This is done
185 automatically for ranged nodes; for other node types you need to call one of
186 the functions below to associate item values with each tick.
188 avg_stat_node_add_value_notick(st,name,parent_id,with_children,value)
189 avg_stat_node_add_value(st,name,parent_id,with_children,value)
191 The difference between the above functions is whether the item count is
192 increased or not. To properly compute the average you need to either call
193 avg_stat_node_add_value or avg_stat_node_add_value_notick combined
194 tick_stat_node. The later sequence allows for plug-ins which are compatible
195 with older wireshark versions which ignores avg_stat_node_add_value because
196 it does not understand the command. This would result in 0 counts for all
197 nodes. It is preferred to use avg_stat_node_add_value if you are not writing
200 avg_stat_node_add_value is used the same way as tick_stat_node with the
201 exception that you now specify an additional value associated with the tick.
203 Do not mix increase_stat_node, set_stat_node or zero_stat_node
204 with avg_stat_node_add_value as this will lead to incorrect results for the
207 stats_tree now also support setting flags per node to control the behaviour
208 of these nodes. This can be done using the stat_node_set_flags and
209 stat_node_clear_flags functions. Currently these flags are defined:
211 ST_FLG_DEF_NOEXPAND: By default the top-level nodes in a tree are
212 automatically expanded in the GUI. Setting this flag on
213 such a node prevents the node from automatically expanding.
214 ST_FLG_SORT_TOP: Nodes with this flag is sorted separately from nodes
215 without this flag (in effect partitioning tree into a top and
216 bottom half. Each half is sorted normally. Top always appear
219 You can find more examples of these in $srcdir/plugins/stats_tree/pinfo_stats_tree.c