Something is better than nothing, Sooner is better than later.
[obnox/wireshark/wip.git] / doc / README.stats_tree
1 $Id$
2 tapping with stats_tree
3
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 tethereal) of the
8 tap data. So there's very little code to write to make a tap listener usable
9 from both ethereal and tethereal.
10
11 First, you should add the TAP to the dissector in question as described in
12 README.tapping .
13
14 Once the dissector in question is "tapped" you have to write the stats tree
15 code which is made of three parts:
16
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
20    data.
21    
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.
25    
26 The cleanup callback:
27    It is called at the destruction of the stats_tree and might be used to
28    free ....
29
30 Other than that the stats_tree should be registered.
31
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.
35
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().
38
39
40 A small example of a very basic stats_tree plugin follows.
41
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
46  *
47  * $ ~Id:  $
48  *
49  * Ethereal - Network traffic analyzer
50  * By Gerald Combs <gerald@ethereal.org>
51  * Copyright 1998 Gerald Combs
52  *
53  *
54  * This program is free software; you can redistribute it and/or
55  * modify it under the terms of the GNU General Public License
56  * as published by the Free Software Foundation; either version 2
57  * of the License, or (at your option) any later version.
58  *
59  * This program is distributed in the hope that it will be useful,
60  * but WITHOUT ANY WARRANTY; without even the implied warranty of
61  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62  * GNU General Public License for more details.
63  *
64  * You should have received a copy of the GNU General Public License
65  * along with this program; if not, write to the Free Software
66  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
67  */
68
69 #ifdef HAVE_CONFIG_H
70 #include "config.h"
71 #endif
72
73 #ifndef ENABLE_STATIC
74 #include <gmodule.h>
75 #else
76 #include <glib.h>
77 #endif
78
79 #include <epan/stats_tree.h>
80 #include <epan/dissectors/udp.h>
81
82 static int st_udp_term;
83 static gchar* st_str_udp_term = "UDP terminations";
84
85 /* this one initializes the tree, creating the root nodes */
86 extern void udp_term_stats_tree_init(stats_tree* st) {
87         /* we create a node under which we'll add every termination */
88         st_udp_term = stats_tree_create_node(st, st_str_udp_term, 0, TRUE);     
89 }
90
91 /* this one will be called with every udp packet */
92 extern int udp_term_stats_tree_packet(stats_tree *st, /* st as it was passed to us */ 
93                                       packet_info *pinfo,  /* we'll fetch the addreses from here */
94                                       epan_dissect_t *edt _U_, /* unused */ 
95                                       const void *p) /* we'll use this to fetch the ports */
96 {
97         static guint8 str[128];
98         e_udphdr* udphdr = (e_udphdr*) p;
99         
100         /* we increment by one (tick) the root node */
101         stats_tree_tick_node(st, st_udp_term, 0, FALSE);
102         
103         /* we then tick a node for this src_addr:src_port
104            if the node doesn't exists it will be created */
105         g_snprintf(str, sizeof(str),"%s:%u",address_to_str(&pinfo->net_src),udphdr->sport);
106         tick_stat_node(st, str, st_udp_term, FALSE);
107
108         /* same thing for dst */
109         g_snprintf(str, sizeof(str),"%s:%u",address_to_str(&pinfo->net_dst),udphdr->dport);
110         tick_stat_node(st, str, st_udp_term, FALSE);
111         
112         return 1;
113 }
114
115 #ifndef ENABLE_STATIC
116 G_MODULE_EXPORT const gchar version[] = "0.0";
117
118 G_MODULE_EXPORT void plugin_register_tap_listener(void) {
119
120     stats_tree_register("udp", /* the proto we are going to "tap" */
121                         "udp_terms", /* the abbreviation for this tree (to be used as -z udp_terms,tree) */
122                         st_str_udp_term, /* the name of the menu and window (use "/" for sub menus)*/
123                         udp_term_stats_tree_init, /* the init callback */
124                         ip_hosts_stats_tree_init, /* the per packet callback */
125                         NULL ); /* the cleanup callback (in this case there isn't) */
126
127 }
128 #endif
129
130 ----- END ------
131
132 the stats_tree API
133 ==================
134  every stats_tree callback has a stats_tree* parameter (st), stats_tree is an obscure
135  data structure which should be passed to the api functions.
136
137 stats_tree_register( tapname, abbr, name,  packet_cb, init_cb, cleanup_cb);
138  registers a new stats tree
139
140 stats_tree_parent_id_by_name( st, parent_name)
141   returns the id of a candidate parent node given its name 
142
143
144 Node functions
145 ==============
146
147 All the functions that opearate on nodes return a parent_id
148
149 stats_tree_create_node(st, name, parent_id, with_children)
150   Creates a node in the tree (to be used in the in init_cb)
151     name: the name of the new node
152     parent_id: the id of the parent_node (NULL for root)
153     with_children: TRUE if this node will have "dynamically created" children
154                    (i.e. it will be a candidate parrent)
155
156
157 stats_tree_create_node_by_pname(st, name, parent_name, with_children);
158   As before but creates a node using it's parent's name
159
160
161 stats_tree_create_range_node(st, name, parent_id, ...)
162 stats_tree_range_node_with_pname(st, name, parent_name, ...)
163   Creates a node in the tree, that will contain a ranges list.
164     example:
165        stats_tree_create_range_node(st,name,parent_id,
166                                 "-99","100-199","200-299","300-399","400-", NULL);
167
168 stats_tree_tick_range( st, name,  parent_id, value_in_range);
169 stats_tree_tick_range_by_pname(st,name,parent_name,value_in_range)
170    Increases by one the ranged node and the sub node to whose range the value belongs
171
172
173 stats_tree_create_pivot(st, name, parent_id);
174 stats_tree_create_pivot_by_pname(st, name, parent_name);
175   Creates a "pivot node"
176
177 stats_tree_tick_pivot(st, pivot_id, pivoted_string);
178  Each time a pivot node will be ticked it will get increased, and, it will
179  increase (or create) the children named as pivoted_string
180
181
182 the following will either increase or create a node (with value 1) when called
183
184 tick_stat_node(st,name,parent_id,with_children)
185 increases by one a stat_node
186
187 increase_stat_node(st,name,parent_id,with_children,value)
188 increases by value a stat_node
189
190 set_stat_node(st,name,parent_id,with_children,value)
191 sets the value of a stat_node
192
193 zero_stat_node(st,name,parent_id,with_children)
194 resets to zero a stat_node
195
196
197 You can find more examples of these in $srcdir/plugins/stats_tree/pinfo_stats_tree.c
198
199 Luis E. G. Ontanon.