2 tapping with stats_tree
4 Let's suppose that you want to write a tap only to keep counters, and you
5 don't want to get involved with GUI programming or maybe you'd like to make
6 it a plugin. A stats_tree might be the way to go. The stats_tree module takes
7 care of the representation (GUI for ethereal and text for tshark) of the
8 tap data. So there's very little code to write to make a tap listener usable
9 from both ethereal and tshark.
11 First, you should add the TAP to the dissector in question as described in
14 Once the dissector in question is "tapped" you have to write the stats tree
15 code which is made of three parts:
17 The init callback routine:
18 which will be executed before any packet is passed to the tap. Here you
19 should create the "static" nodes of your tree. As well as initialize your
22 The (per)packet callback routine:
23 As the tap_packet callback is going to be called for every packet, it
24 should be used to increment the counters.
27 It is called at the destruction of the stats_tree and might be used to
30 Other than that the stats_tree should be registered.
32 If you want to make it a plugin, stats_tree_register() should be called by
33 plugin_register_tap_listener() read README.plugin for other information
34 regarding ethereal plugins.
36 If you want it as part of the dissector stats_tree_register() can be called
37 either by proto_register_xxx() or if you prefer by proto_reg_handoff_xxx().
40 A small example of a very basic stats_tree plugin follows.
42 ----- example stats_tree plugin ------
43 /* udpterm_stats_tree.c
44 * A small example of stats_tree plugin that counts udp packets by termination
45 * 2005, Luis E. G. Ontanon
49 * Wireshark - Network traffic analyzer
50 * By Gerald Combs <gerald@wireshark.org>
51 * Copyright 1998 Gerald Combs
53 * This program is free software; you can redistribute it and/or
54 * modify it under the terms of the GNU General Public License
55 * as published by the Free Software Foundation; either version 2
56 * of the License, or (at your option) any later version.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program; if not, write to the Free Software
65 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
78 #include <epan/stats_tree.h>
79 #include <epan/dissectors/udp.h>
81 static int st_udp_term;
82 static gchar* st_str_udp_term = "UDP terminations";
84 /* this one initializes the tree, creating the root nodes */
85 extern void udp_term_stats_tree_init(stats_tree* st) {
86 /* we create a node under which we'll add every termination */
87 st_udp_term = stats_tree_create_node(st, st_str_udp_term, 0, TRUE);
90 /* this one will be called with every udp packet */
91 extern int udp_term_stats_tree_packet(stats_tree *st, /* st as it was passed to us */
92 packet_info *pinfo, /* we'll fetch the addreses from here */
93 epan_dissect_t *edt _U_, /* unused */
94 const void *p) /* we'll use this to fetch the ports */
96 static guint8 str[128];
97 e_udphdr* udphdr = (e_udphdr*) p;
99 /* we increment by one (tick) the root node */
100 stats_tree_tick_node(st, st_udp_term, 0, FALSE);
102 /* we then tick a node for this src_addr:src_port
103 if the node doesn't exists it will be created */
104 g_snprintf(str, sizeof(str),"%s:%u",address_to_str(&pinfo->net_src),udphdr->sport);
105 tick_stat_node(st, str, st_udp_term, FALSE);
107 /* same thing for dst */
108 g_snprintf(str, sizeof(str),"%s:%u",address_to_str(&pinfo->net_dst),udphdr->dport);
109 tick_stat_node(st, str, st_udp_term, FALSE);
114 #ifndef ENABLE_STATIC
115 G_MODULE_EXPORT const gchar version[] = "0.0";
117 G_MODULE_EXPORT void plugin_register_tap_listener(void) {
119 stats_tree_register("udp", /* the proto we are going to "tap" */
120 "udp_terms", /* the abbreviation for this tree (to be used as -z udp_terms,tree) */
121 st_str_udp_term, /* the name of the menu and window (use "/" for sub menus)*/
122 udp_term_stats_tree_init, /* the init callback */
123 ip_hosts_stats_tree_init, /* the per packet callback */
124 NULL ); /* the cleanup callback (in this case there isn't) */
133 every stats_tree callback has a stats_tree* parameter (st), stats_tree is an obscure
134 data structure which should be passed to the api functions.
136 stats_tree_register( tapname, abbr, name, packet_cb, init_cb, cleanup_cb);
137 registers a new stats tree
139 stats_tree_parent_id_by_name( st, parent_name)
140 returns the id of a candidate parent node given its name
146 All the functions that opearate on nodes return a parent_id
148 stats_tree_create_node(st, name, parent_id, with_children)
149 Creates a node in the tree (to be used in the in init_cb)
150 name: the name of the new node
151 parent_id: the id of the parent_node (NULL for root)
152 with_children: TRUE if this node will have "dynamically created" children
153 (i.e. it will be a candidate parrent)
156 stats_tree_create_node_by_pname(st, name, parent_name, with_children);
157 As before but creates a node using it's parent's name
160 stats_tree_create_range_node(st, name, parent_id, ...)
161 stats_tree_range_node_with_pname(st, name, parent_name, ...)
162 Creates a node in the tree, that will contain a ranges list.
164 stats_tree_create_range_node(st,name,parent_id,
165 "-99","100-199","200-299","300-399","400-", NULL);
167 stats_tree_tick_range( st, name, parent_id, value_in_range);
168 stats_tree_tick_range_by_pname(st,name,parent_name,value_in_range)
169 Increases by one the ranged node and the sub node to whose range the value belongs
172 stats_tree_create_pivot(st, name, parent_id);
173 stats_tree_create_pivot_by_pname(st, name, parent_name);
174 Creates a "pivot node"
176 stats_tree_tick_pivot(st, pivot_id, pivoted_string);
177 Each time a pivot node will be ticked it will get increased, and, it will
178 increase (or create) the children named as pivoted_string
181 the following will either increase or create a node (with value 1) when called
183 tick_stat_node(st,name,parent_id,with_children)
184 increases by one a stat_node
186 increase_stat_node(st,name,parent_id,with_children,value)
187 increases by value a stat_node
189 set_stat_node(st,name,parent_id,with_children,value)
190 sets the value of a stat_node
192 zero_stat_node(st,name,parent_id,with_children)
193 resets to zero a stat_node
196 You can find more examples of these in $srcdir/plugins/stats_tree/pinfo_stats_tree.c