There was a design flaw that caused a crash on windows and
[metze/wireshark/wip.git] / epan / stats_tree.c
1 /* stats_tree.c
2  * API for a counter tree for ethereal
3  * 2004, Luis E. G. Ontanon
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/stats_tree_priv.h>
32 #include <string.h>
33
34 /*
35 TODO: 
36    - sort out the sorting issue
37  
38  */
39
40 /* used to contain the registered stat trees */
41 static GHashTable* registry = NULL;
42
43 /* writes into the buffers pointed by value, rate and percent
44    the string representations of a node*/
45 extern void get_strings_from_node(const stat_node* node, guint8* value, guint8* rate, guint8* percent) {
46         float f;
47         
48         if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
49         
50         if (rate) {
51                 *rate = '\0';
52                 if (node->st->elapsed > 0.0) {
53                         f = ((float)node->counter) / node->st->elapsed;
54                         g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
55                 }
56         }
57         
58         if (percent) {
59                 *percent = '\0';
60                 if (node->parent->counter > 0) {
61                         f = (float)(((float)node->counter * 100.0) / node->parent->counter);
62                         g_snprintf(percent,NUM_BUF_SIZE,"%.2f%%",f);
63                 }
64         }
65 }
66
67
68 /* a text representation of a node
69 if buffer is NULL returns a newly allocated string */
70 extern guint8* stat_node_to_str(const stat_node* node,
71                                                                 guint8* buffer, guint len) {
72         if (buffer) {
73                 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
74                 return buffer;
75         } else {
76                 return g_strdup_printf("%s: %i",node->name, node->counter);
77         }
78 }
79
80 extern guint stats_branch_max_name_len(const stat_node* node, guint indent) {
81         stat_node* child;
82         guint maxlen = 0;
83         guint len;
84         
85         indent = indent > INDENT_MAX ? INDENT_MAX : indent;
86
87         if (node->children) {
88                 for (child = node->children; child; child = child->next ) {
89                         len = stats_branch_max_name_len(child,indent+1); 
90                         maxlen = len > maxlen ? len : maxlen;
91                 }
92         }
93         
94         len = strlen(node->name) + indent;
95         maxlen = len > maxlen ? len : maxlen;
96         
97         return maxlen;
98 }
99
100 static gchar* format;
101
102 /* populates the given GString with a tree representation of a branch given by node,
103 using indent spaces as initial indentation */
104 extern void stat_branch_to_str(const stat_node* node, GString* s, guint indent) {
105         stat_node* child;
106         static gchar indentation[INDENT_MAX+1];
107         static gchar value[NUM_BUF_SIZE];
108         static gchar rate[NUM_BUF_SIZE];
109         static gchar percent[NUM_BUF_SIZE];
110         
111         guint i = 0;
112         
113         if (indent == 0) {
114                 format = g_strdup_printf(" %%s%%-%us%%12s   %%12s    %%12s\n",stats_branch_max_name_len(node,0));
115         }
116         
117         get_strings_from_node(node, value, rate, percent);
118         
119         indent = indent > INDENT_MAX ? INDENT_MAX : indent;
120         
121         /* fill indentation with indent spaces */
122         if (indent > 0) {
123                 while(i<indent)
124                         indentation[i++] = ' ';
125         }
126         
127         indentation[i++] = '\0';
128         
129         g_string_sprintfa(s,format,
130                                           indentation,node->name,value,rate,percent);
131                 
132         if (node->children) {
133                 for (child = node->children; child; child = child->next ) {
134                         stat_branch_to_str(child,s,indent+1);
135                 }
136         }
137         
138         if (indent == 0) {
139                 g_free(format);
140         }
141 }
142
143
144 /* frees the resources allocated by a stat_tree node */
145 static void free_stat_node( stat_node* node ) {
146         stat_node* child;
147         
148         if (node->children) {
149                 for (child = node->children; child; child = child->next ) 
150                         free_stat_node(child);
151         }
152         
153         if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
154         
155         if (node->hash) g_hash_table_destroy(node->hash);
156
157         if (node->rng) g_free(node->rng);
158         
159         if (node->name) g_free(node->name);
160         
161         g_free(node);
162 }
163
164 /* destroys the whole tree instance */
165 extern void free_stats_tree(stats_tree* st) {
166         stat_node* child;
167         
168         g_free(st->filter);
169         g_hash_table_destroy(st->names);
170         g_ptr_array_free(st->parents,FALSE);
171         
172         for (child = st->root.children; child; child = child->next ) 
173                 free_stat_node(child);
174         
175         if (st->cfg->free_tree_pr)
176                 st->cfg->free_tree_pr(st);
177                         
178         g_free(st);
179 }
180
181
182 /* reset a node to its original state */
183 static void reset_stat_node(stat_node* node) {
184         stat_node* child;
185         
186         if (node->children) {
187                 for (child = node->children; child; child = child->next ) 
188                         reset_stat_node(child);
189         }
190         
191         node->counter = 0;
192         
193         if(node->st->cfg->reset_node) {
194                 node->st->cfg->reset_node(node);
195         }
196         
197 }
198
199 /* reset the whole stats_tree */
200 extern void reset_stats_tree(void* p) {
201         stats_tree* st = p;
202         reset_stat_node(&st->root);
203         
204         if (st->cfg->reset_tree) {
205                 st->cfg->reset_tree(st);
206         }
207 }
208
209 extern void reinit_stats_tree(void* p) {
210         stats_tree* st = p;
211         stat_node* child;
212         
213         for (child = st->root.children; child; child = child->next) {
214                 free_stat_node(child);
215         }
216         
217         st->root.children = NULL;
218         st->root.counter = 0;
219         
220         if (st->cfg->init) {
221                 st->cfg->init(st);
222         }
223 }
224
225 /* register a new stats_tree */
226 extern void register_stats_tree(guint8* tapname,
227                                                                 guint8* abbr, 
228                                                                 guint8* name,
229                                                                 stat_tree_packet_cb packet,
230                                                                 stat_tree_init_cb init ) {
231         
232         stats_tree_cfg* cfg = g_malloc( sizeof(stats_tree_cfg) );
233
234         /* at the very least the abbrev and the packet function should be given */ 
235         g_assert( tapname && abbr && packet );
236
237         cfg->tapname = g_strdup(tapname);
238         cfg->abbr = g_strdup(abbr);
239         cfg->name = name ? g_strdup(name) : g_strdup(abbr);
240         
241         cfg->packet = packet;
242         cfg->init = init;
243         
244         /* these have to be filled in by implementations */
245         cfg->setup_node_pr = NULL;
246         cfg->new_tree_pr = NULL;
247         cfg->free_node_pr = NULL;
248         cfg->free_tree_pr = NULL;
249         cfg->draw_node = NULL;
250         cfg->draw_tree = NULL;
251         cfg->reset_node = NULL;
252         cfg->reset_tree = NULL;
253
254         if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
255
256         g_hash_table_insert(registry,cfg->abbr,cfg);
257         
258 }
259
260 extern stats_tree* new_stats_tree(stats_tree_cfg* cfg, tree_pres* pr,char* filter) {
261         stats_tree* st = g_malloc(sizeof(stats_tree));
262
263         st->cfg = cfg;
264         st->pr = pr;
265
266         st->names = g_hash_table_new(g_str_hash,g_str_equal);
267         st->parents = g_ptr_array_new();
268         st->filter = filter;
269         
270         st->start = -1.0;
271         st->elapsed = 0.0;
272
273         st->root.counter = 0;
274         st->root.name = g_strdup(cfg->name);
275         st->root.st = st;
276         st->root.parent = NULL;
277         st->root.children = NULL;
278         st->root.next = NULL;
279         st->root.hash = NULL;
280         st->root.pr = NULL;
281         
282         g_ptr_array_add(st->parents,&st->root);
283         
284         return st;
285 }       
286
287 /* will be the tap packet cb */
288 extern int stats_tree_packet(void* p, packet_info* pinfo, epan_dissect_t *edt, const void *pri) {
289         stats_tree* st = p;
290         
291         float now = (((float)pinfo->fd->rel_secs) + (((float)pinfo->fd->rel_usecs)/1000000) );
292         
293         if (st->start < 0.0) st->start = now;
294         
295         st->elapsed = now - st->start;
296         
297         if (st->cfg->packet)
298                 return st->cfg->packet(st,pinfo,edt,pri);
299         else
300                 return 0;
301 }
302
303 extern GHashTable* stat_tree_registry(void) {
304         return registry;
305 }
306
307 extern stats_tree_cfg* get_stats_tree_by_abbr(guint8* abbr) {
308         return g_hash_table_lookup(registry,abbr);
309 }
310
311
312 struct _stats_tree_pres_cbs {
313         void (*setup_node_pr)(stat_node*);
314         void (*free_node_pr)(stat_node*);
315         void (*draw_node)(stat_node*);
316         void (*reset_node)(stat_node*);
317         tree_pres* (*new_tree_pr)(stats_tree*);
318         void (*free_tree_pr)(stats_tree*);
319         void (*draw_tree)(stats_tree*);
320         void (*reset_tree)(stats_tree*);
321 };
322
323 static void setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p) {
324         stats_tree_cfg* cfg = v;
325         struct _stats_tree_pres_cbs *d = p;
326         
327         cfg->setup_node_pr = d->setup_node_pr;
328         cfg->new_tree_pr = d->new_tree_pr;
329         cfg->free_node_pr = d->free_node_pr;
330         cfg->free_tree_pr = d->free_tree_pr;
331         cfg->draw_node = d->draw_node;
332         cfg->draw_tree = d->draw_tree;
333         cfg->reset_node = d->reset_node;
334         cfg->reset_tree = d->reset_tree;
335         
336 }
337
338 extern void stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
339                                                                         void (*setup_node_pr)(stat_node*),
340                                                                         void (*free_node_pr)(stat_node*),
341                                                                         void (*draw_node)(stat_node*),
342                                                                         void (*reset_node)(stat_node*),
343                                                                         tree_pres* (*new_tree_pr)(stats_tree*),
344                                                                         void (*free_tree_pr)(stats_tree*),
345                                                                         void (*draw_tree)(stats_tree*),
346                                                                         void (*reset_tree)(stats_tree*),
347                                                                         void* data) {
348         struct _stats_tree_pres_cbs d = {setup_node_pr,free_node_pr,draw_node,reset_node,new_tree_pr,free_tree_pr,draw_tree,reset_tree};
349         
350         if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
351         
352         if (registry_iterator && registry)
353                 g_hash_table_foreach(registry,registry_iterator,data);
354         
355 }
356
357
358 /* creates a stat_tree node
359 *    name: the name of the stats_tree node
360 *    parent_name: the name of the ALREADY REGISTERED parent
361 *    with_hash: whether or not it should keep a hash with it's children names
362 *    as_named_node: whether or not it has to be registered in the root namespace
363 */
364 static stat_node*  new_stat_node(stats_tree* st,
365                                                                  const gchar* name,
366                                                                  int parent_id,
367                                                                  gboolean with_hash,
368                                                                  gboolean as_parent_node) {
369         
370         stat_node *node = g_malloc (sizeof(stat_node));
371         stat_node* last_chld = NULL;
372         
373         node->counter = 0;
374         node->name = g_strdup(name);
375         node->children = NULL;
376         node->next = NULL;
377         node->st = (stats_tree*) st;
378         node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
379         node->parent = NULL;
380         node->rng  =  NULL;
381
382         if (as_parent_node) {
383                 g_hash_table_insert(st->names,
384                                                         node->name,
385                                                         node);
386                 
387                 g_ptr_array_add(st->parents,node);
388                 
389                 node->id = st->parents->len - 1;
390         } else {
391                 node->id = -1;
392         }
393         
394         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
395                 node->parent = g_ptr_array_index(st->parents,parent_id);
396         } else {
397                 /* ??? should we set the parent to be root ??? */
398                 g_assert_not_reached();
399         }
400         
401         if (node->parent->children) {
402                 /* insert as last child */
403                 
404                 for (last_chld = node->parent->children;
405                          last_chld->next;
406                          last_chld = last_chld->next ) ;
407                 
408                 last_chld->next = node;
409                 
410         } else {
411                 /* insert as first child */
412                 node->parent->children = node;
413         }
414         
415         if(node->parent->hash) {
416                 g_hash_table_insert(node->parent->hash,node->name,node);
417         }
418         
419         if (st->cfg->setup_node_pr) {
420                 st->cfg->setup_node_pr(node);
421         } else {
422                 node->pr = NULL;
423         }
424         
425         return node;
426 }
427 /***/
428
429 extern int create_node(stats_tree* st, const gchar* name, int parent_id, gboolean with_hash) {
430         stat_node* node = new_stat_node(st,name,parent_id,with_hash,TRUE);
431         
432         if (node) 
433                 return node->id;
434         else
435                 return 0;
436 }
437
438 /* XXX: should this be a macro? */
439 extern int create_node_with_parent_name(stats_tree* st,
440                                                                                    const gchar* name,
441                                                                                    const gchar* parent_name,
442                                                                                    gboolean with_children) {
443         return create_node(st,name,get_parent_id_by_name(st,parent_name),with_children);
444 }
445
446
447
448 /*
449  * Increases by delta the counter of the node whose name is given
450  * if the node does not exist yet it's created (with counter=1)
451  * using parent_name as parent node.
452  * with_hash=TRUE to indicate that the created node will have a parent
453  */
454 extern int manip_stat_node(manip_node_mode mode, stats_tree* st, const guint8* name, int parent_id, gboolean with_hash, gint value) {
455         stat_node* node = NULL;
456         stat_node* parent = NULL;
457         
458         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
459                 parent = g_ptr_array_index(st->parents,parent_id);
460         } else {
461                 g_assert_not_reached();
462         }
463         
464         if( parent->hash ) {
465                 node = g_hash_table_lookup(parent->hash,name);
466         } else {
467                 node = g_hash_table_lookup(st->names,name);
468         }
469         
470         if ( node == NULL ) 
471                 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
472         
473         switch (mode) {
474                 case MN_INCREASE: node->counter += value; break;
475                 case MN_SET: node->counter = value; break;
476         }
477         
478         if (node) 
479                 return node->id;
480         else
481                 return -1;
482 }
483
484
485 extern guint8* get_st_abbr(const guint8* optarg) {
486         guint i;
487
488         /* XXX: this fails when tethereal is given any options
489            after the -z */
490         g_assert(optarg != NULL);
491         
492         for (i=0; optarg[i] && optarg[i] != ','; i++);
493         
494         if (optarg[i] == ',') {
495                 return g_strndup(optarg,i);
496         } else {
497                 return NULL;
498         }
499 }
500
501
502 static range_pair_t* get_range(guint8* rngstr) {
503         gchar** split;
504         range_pair_t* rng = g_malloc(sizeof(range_pair_t));
505         
506         split =  g_strsplit(rngstr,"-",2);
507
508         rng->floor = strtol(split[0],NULL,10);
509         rng->ceil  = strtol(split[1],NULL,10);
510         
511         if (rng->ceil == 0) rng->ceil = G_MAXINT;
512         if (rng->floor == 0) rng->floor = G_MININT;
513
514         g_strfreev(split);
515         
516         return rng;
517 }
518
519
520 extern int create_range_node(stats_tree* st,
521                                                                 const gchar* name,
522                                                                 int parent_id,
523                                                                 ...) {
524         va_list list;
525         guint8* curr_range;
526         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
527         stat_node* range_node = NULL;
528         
529         va_start( list, parent_id );
530         while (( curr_range = va_arg(list, guint8*) )) {
531                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
532                 range_node->rng = get_range(curr_range);
533         }
534         va_end( list );
535
536         return rng_root->id;
537 }
538
539 /****/
540 extern int get_parent_id_by_name(stats_tree* st, const gchar* parent_name) {
541         stat_node* node = g_hash_table_lookup(st->names,parent_name);
542         
543         if (node)
544                 return node->id;
545         else
546                 return 0; /* XXX: this is the root shoud we return -1 instead?*/
547 }
548
549
550 extern int create_range_node_with_parent_name(stats_tree* st,
551                                                                                           const gchar* name,
552                                                                                           const gchar* parent_name,
553                                                                                           ...) {
554         va_list list;
555         guint8* curr_range;
556         stat_node* range_node = NULL;
557         int parent_id = get_parent_id_by_name(st,parent_name);
558         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
559
560         va_start( list, parent_name );
561         while (( curr_range = va_arg(list, guint8*) )) {
562                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
563                 range_node->rng = get_range(curr_range);
564         }
565         va_end( list );
566         
567         return rng_root->id;
568 }       
569
570
571 extern int tick_range(stats_tree* st,
572                                                  const gchar* name,
573                                                  int parent_id,
574                                                  int value_in_range) {
575         
576         stat_node* node = NULL;
577         stat_node* parent = NULL;
578         stat_node* child = NULL;
579         gint floor, ceil;
580         
581         if (parent_id >= 0 && parent_id < (int) st->parents->len) {
582                 parent = g_ptr_array_index(st->parents,parent_id);
583         } else {
584                 g_assert_not_reached();
585         }
586         
587         if( parent->hash ) {
588                 node = g_hash_table_lookup(parent->hash,name);
589         } else {
590                 node = g_hash_table_lookup(st->names,name);
591         }
592         
593         if ( node == NULL ) 
594                 return node->id;
595         
596         for ( child = node->children; child; child = child->next) {
597                 floor =  child->rng->floor;
598                 ceil = child->rng->ceil;
599                 
600                 if ( value_in_range >= floor && value_in_range <= ceil ) {
601                         child->counter++;
602                         return node->id;
603                 }
604         }
605         
606         return node->id;
607 }
608
609 extern int create_pivot_node(stats_tree* st,
610                                                          const gchar* name,
611                                                          int parent_id) {
612         stat_node* node = new_stat_node(st,name,parent_id,TRUE,TRUE);
613         
614         if (node) 
615                 return node->id;
616         else
617                 return 0;
618 }
619
620 extern int create_pivot_node_with_parent_name(stats_tree* st,
621                                                          const gchar* name,
622                                                          const gchar* parent_name) {
623         int parent_id = get_parent_id_by_name(st,parent_name);
624         stat_node* node;
625         
626         node = new_stat_node(st,name,parent_id,TRUE,TRUE);
627         
628         if (node) 
629                 return node->id;
630         else
631                 return 0;
632 }
633
634 extern int tick_pivot(stats_tree* st,
635                                           int pivot_id,
636                                           const gchar* pivot_value) {
637         
638         stat_node* parent = g_ptr_array_index(st->parents,pivot_id);
639         
640         parent->counter++;
641         manip_stat_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
642         
643         return pivot_id;
644 }
645