From Sebastien Tandel:
[obnox/wireshark/wip.git] / epan / stats_tree.c
1 /* stats_tree.c
2  * API for a counter tree for Wireshark
3  * 2004, Luis E. G. Ontanon
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 <epan/ws_strsplit.h>
33 #include <string.h>
34
35 #include "stats_tree.h"
36
37 /*
38 TODO: 
39    - sort out the sorting issue
40  
41  */
42
43 /* used to contain the registered stat trees */
44 static GHashTable* registry = NULL;
45
46 /* writes into the buffers pointed by value, rate and percent
47    the string representations of a node*/
48 extern void stats_tree_get_strs_from_node(const stat_node* node, guint8* value, guint8* rate, guint8* percent) {
49         float f;
50         
51         if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
52         
53         if (rate) {
54                 *rate = '\0';
55                 if (node->st->elapsed > 0.0) {
56                         f = ((float)node->counter) / (float)node->st->elapsed;
57                         g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
58                 }
59         }
60         
61         if (percent) {
62                 *percent = '\0';
63                 if (node->parent->counter > 0) {
64                         f = (float)(((float)node->counter * 100.0) / node->parent->counter);
65                         g_snprintf(percent,NUM_BUF_SIZE,"%.2f%%",f);
66                 }
67         }
68 }
69
70
71 /* a text representation of a node
72 if buffer is NULL returns a newly allocated string */
73 extern guint8* stats_tree_node_to_str(const stat_node* node,
74                                                                 guint8* buffer, guint len) {
75         if (buffer) {
76                 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
77                 return buffer;
78         } else {
79                 return g_strdup_printf("%s: %i",node->name, node->counter);
80         }
81 }
82
83 extern guint stats_tree_branch_max_namelen(const stat_node* node, guint indent) {
84         stat_node* child;
85         guint maxlen = 0;
86         guint len;
87         
88         indent = indent > INDENT_MAX ? INDENT_MAX : indent;
89
90         if (node->children) {
91                 for (child = node->children; child; child = child->next ) {
92                         len = stats_tree_branch_max_namelen(child,indent+1); 
93                         maxlen = len > maxlen ? len : maxlen;
94                 }
95         }
96         
97         len = strlen(node->name) + indent;
98         maxlen = len > maxlen ? len : maxlen;
99         
100         return maxlen;
101 }
102
103 static gchar* format;
104
105 /* populates the given GString with a tree representation of a branch given by node,
106 using indent spaces as initial indentation */
107 extern void stats_tree_branch_to_str(const stat_node* node, GString* s, guint indent) {
108         stat_node* child;
109         static gchar indentation[INDENT_MAX+1];
110         static gchar value[NUM_BUF_SIZE];
111         static gchar rate[NUM_BUF_SIZE];
112         static gchar percent[NUM_BUF_SIZE];
113         
114         guint i = 0;
115         
116         if (indent == 0) {
117                 format = g_strdup_printf(" %%s%%-%us%%12s   %%12s    %%12s\n",stats_tree_branch_max_namelen(node,0));
118         }
119         
120         stats_tree_get_strs_from_node(node, value, rate, percent);
121         
122         indent = indent > INDENT_MAX ? INDENT_MAX : indent;
123         
124         /* fill indentation with indent spaces */
125         if (indent > 0) {
126                 while(i<indent)
127                         indentation[i++] = ' ';
128         }
129         
130         indentation[i++] = '\0';
131         
132         g_string_sprintfa(s,format,
133                                           indentation,node->name,value,rate,percent);
134                 
135         if (node->children) {
136                 for (child = node->children; child; child = child->next ) {
137                         stats_tree_branch_to_str(child,s,indent+1);
138                 }
139         }
140         
141         if (indent == 0) {
142                 g_free(format);
143         }
144 }
145
146
147 /* frees the resources allocated by a stat_tree node */
148 static void free_stat_node( stat_node* node ) {
149         stat_node* child;
150     stat_node* next;
151         
152         if (node->children) {
153         for (child = node->children; child; child = next ) {
154             /* child->next will be gone after free_stat_node, so cache it here */
155             next = child->next;
156                         free_stat_node(child);
157         }
158         }
159         
160         if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
161         
162         if (node->hash) g_hash_table_destroy(node->hash);
163
164         if (node->rng) g_free(node->rng);
165         
166         if (node->name) g_free(node->name);
167         
168         g_free(node);
169 }
170
171 /* destroys the whole tree instance */
172 extern void stats_tree_free(stats_tree* st) {
173         stat_node* child;
174         stat_node* next;
175         
176         g_free(st->filter);
177         g_hash_table_destroy(st->names);
178         g_ptr_array_free(st->parents,TRUE);
179         
180     for (child = st->root.children; child; child = next ) {
181         /* child->next will be gone after free_stat_node, so cache it here */
182         next = child->next;
183                 free_stat_node(child);
184     }
185         
186         if (st->cfg->free_tree_pr)
187                 st->cfg->free_tree_pr(st);
188         
189         if (st->cfg->cleanup)
190                 st->cfg->cleanup(st);
191         
192         g_free(st);
193 }
194
195
196 /* reset a node to its original state */
197 static void reset_stat_node(stat_node* node) {
198         stat_node* child;
199         
200         if (node->children) {
201                 for (child = node->children; child; child = child->next ) 
202                         reset_stat_node(child);
203         }
204         
205         node->counter = 0;
206         
207         if(node->st->cfg->reset_node) {
208                 node->st->cfg->reset_node(node);
209         }
210         
211 }
212
213 /* reset the whole stats_tree */
214 extern void stats_tree_reset(void* p) {
215         stats_tree* st = p;
216     
217         st->start = -1.0;
218         st->elapsed = 0.0;
219     
220         reset_stat_node(&st->root);
221         
222         if (st->cfg->reset_tree) {
223                 st->cfg->reset_tree(st);
224         }
225 }
226
227 extern void stats_tree_reinit(void* p) {
228         stats_tree* st = p;
229         stat_node* child;
230         stat_node* next;
231         
232         for (child = st->root.children; child; child = next) {
233         /* child->next will be gone after free_stat_node, so cache it here */
234         next = child->next;
235                 free_stat_node(child);
236         }
237         
238         st->root.children = NULL;
239         st->root.counter = 0;
240         
241         if (st->cfg->init) {
242                 st->cfg->init(st);
243         }
244 }
245
246 /* register a new stats_tree */
247 extern void stats_tree_register(const guint8* tapname,
248                                                                 const guint8* abbr, 
249                                                                 const guint8* name,
250                                                                 stat_tree_packet_cb packet,
251                                                                 stat_tree_init_cb init,
252                                                                 stat_tree_cleanup_cb cleanup) {
253         
254         stats_tree_cfg* cfg = g_malloc( sizeof(stats_tree_cfg) );
255
256         /* at the very least the abbrev and the packet function should be given */ 
257         g_assert( tapname && abbr && packet );
258
259         cfg->tapname = g_strdup(tapname);
260         cfg->abbr = g_strdup(abbr);
261         cfg->name = name ? g_strdup(name) : g_strdup(abbr);
262         
263         cfg->packet = packet;
264         cfg->init = init;
265         cfg->cleanup = cleanup;
266         
267         /* these have to be filled in by implementations */
268         cfg->setup_node_pr = NULL;
269         cfg->new_tree_pr = NULL;
270         cfg->free_node_pr = NULL;
271         cfg->free_tree_pr = NULL;
272         cfg->draw_node = NULL;
273         cfg->draw_tree = NULL;
274         cfg->reset_node = NULL;
275         cfg->reset_tree = NULL;
276
277         if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
278
279         g_hash_table_insert(registry,cfg->abbr,cfg);
280         
281 }
282
283 extern stats_tree* stats_tree_new(stats_tree_cfg* cfg, tree_pres* pr,char* filter) {
284         stats_tree* st = g_malloc(sizeof(stats_tree));
285
286         st->cfg = cfg;
287         st->pr = pr;
288
289         st->names = g_hash_table_new(g_str_hash,g_str_equal);
290         st->parents = g_ptr_array_new();
291         st->filter = g_strdup(filter);
292         
293         st->start = -1.0;
294         st->elapsed = 0.0;
295
296         st->root.counter = 0;
297         st->root.name = g_strdup(cfg->name);
298         st->root.st = st;
299         st->root.parent = NULL;
300         st->root.children = NULL;
301         st->root.next = NULL;
302         st->root.hash = NULL;
303         st->root.pr = NULL;
304         
305         g_ptr_array_add(st->parents,&st->root);
306         
307         return st;
308 }       
309
310 /* will be the tap packet cb */
311 extern int stats_tree_packet(void* p, packet_info* pinfo, epan_dissect_t *edt, const void *pri) {
312         stats_tree* st = p;
313         double now = nstime_to_msec(&pinfo->fd->rel_ts);
314         
315         if (st->start < 0.0) st->start = now;
316         
317         st->elapsed = now - st->start;
318         
319         if (st->cfg->packet)
320                 return st->cfg->packet(st,pinfo,edt,pri);
321         else
322                 return 0;
323 }
324
325 extern stats_tree_cfg* stats_tree_get_cfg_by_abbr(guint8* abbr) {
326         return g_hash_table_lookup(registry,abbr);
327 }
328
329
330 struct _stats_tree_pres_cbs {
331         void (*setup_node_pr)(stat_node*);
332         void (*free_node_pr)(stat_node*);
333         void (*draw_node)(stat_node*);
334         void (*reset_node)(stat_node*);
335         tree_pres* (*new_tree_pr)(stats_tree*);
336         void (*free_tree_pr)(stats_tree*);
337         void (*draw_tree)(stats_tree*);
338         void (*reset_tree)(stats_tree*);
339 };
340
341 static void setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p) {
342         stats_tree_cfg* cfg = v;
343         struct _stats_tree_pres_cbs *d = p;
344         
345         cfg->in_use = FALSE;
346         cfg->setup_node_pr = d->setup_node_pr;
347         cfg->new_tree_pr = d->new_tree_pr;
348         cfg->free_node_pr = d->free_node_pr;
349         cfg->free_tree_pr = d->free_tree_pr;
350         cfg->draw_node = d->draw_node;
351         cfg->draw_tree = d->draw_tree;
352         cfg->reset_node = d->reset_node;
353         cfg->reset_tree = d->reset_tree;
354         
355 }
356
357 extern void stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
358                                                                         void (*setup_node_pr)(stat_node*),
359                                                                         void (*free_node_pr)(stat_node*),
360                                                                         void (*draw_node)(stat_node*),
361                                                                         void (*reset_node)(stat_node*),
362                                                                         tree_pres* (*new_tree_pr)(stats_tree*),
363                                                                         void (*free_tree_pr)(stats_tree*),
364                                                                         void (*draw_tree)(stats_tree*),
365                                                                         void (*reset_tree)(stats_tree*),
366                                                                         void* data) {
367         static struct _stats_tree_pres_cbs d;
368         
369         d.setup_node_pr = setup_node_pr;
370         d.new_tree_pr = new_tree_pr;
371         d.free_node_pr = free_node_pr;
372         d.free_tree_pr = free_tree_pr;
373         d.draw_node = draw_node;
374         d.draw_tree = draw_tree;
375         d.reset_node = reset_node;
376         d.reset_tree = reset_tree;
377         
378         if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
379         
380         if (registry_iterator && registry)
381                 g_hash_table_foreach(registry,registry_iterator,data);
382         
383 }
384
385
386 /* creates a stat_tree node
387 *    name: the name of the stats_tree node
388 *    parent_name: the name of the ALREADY REGISTERED parent
389 *    with_hash: whether or not it should keep a hash with it's children names
390 *    as_named_node: whether or not it has to be registered in the root namespace
391 */
392 static stat_node*  new_stat_node(stats_tree* st,
393                                                                  const gchar* name,
394                                                                  int parent_id,
395                                                                  gboolean with_hash,
396                                                                  gboolean as_parent_node) {
397         
398         stat_node *node = g_malloc (sizeof(stat_node));
399         stat_node* last_chld = NULL;
400         
401         node->counter = 0;
402         node->name = g_strdup(name);
403         node->children = NULL;
404         node->next = NULL;
405         node->st = (stats_tree*) st;
406         node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
407         node->parent = NULL;
408         node->rng  =  NULL;
409
410         if (as_parent_node) {
411                 g_hash_table_insert(st->names,
412                                                         node->name,
413                                                         node);
414                 
415                 g_ptr_array_add(st->parents,node);
416                 
417                 node->id = st->parents->len - 1;
418         } else {
419                 node->id = -1;
420         }
421         
422         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
423                 node->parent = g_ptr_array_index(st->parents,parent_id);
424         } else {
425                 /* ??? should we set the parent to be root ??? */
426                 g_assert_not_reached();
427         }
428         
429         if (node->parent->children) {
430                 /* insert as last child */
431                 
432                 for (last_chld = node->parent->children;
433                          last_chld->next;
434                          last_chld = last_chld->next ) ;
435                 
436                 last_chld->next = node;
437                 
438         } else {
439                 /* insert as first child */
440                 node->parent->children = node;
441         }
442         
443         if(node->parent->hash) {
444                 g_hash_table_insert(node->parent->hash,node->name,node);
445         }
446         
447         if (st->cfg->setup_node_pr) {
448                 st->cfg->setup_node_pr(node);
449         } else {
450                 node->pr = NULL;
451         }
452         
453         return node;
454 }
455 /***/
456
457 extern int stats_tree_create_node(stats_tree* st, const gchar* name, int parent_id, gboolean with_hash) {
458         stat_node* node = new_stat_node(st,name,parent_id,with_hash,TRUE);
459         
460         if (node) 
461                 return node->id;
462         else
463                 return 0;
464 }
465
466 /* XXX: should this be a macro? */
467 extern int stats_tree_create_node_by_pname(stats_tree* st,
468                                                                                    const gchar* name,
469                                                                                    const gchar* parent_name,
470                                                                                    gboolean with_children) {
471         return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
472 }
473
474
475
476 /*
477  * Increases by delta the counter of the node whose name is given
478  * if the node does not exist yet it's created (with counter=1)
479  * using parent_name as parent node.
480  * with_hash=TRUE to indicate that the created node will have a parent
481  */
482 extern int stats_tree_manip_node(manip_node_mode mode, stats_tree* st, const guint8* name, int parent_id, gboolean with_hash, gint value) {
483         stat_node* node = NULL;
484         stat_node* parent = NULL;
485         
486         g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
487         
488         parent = g_ptr_array_index(st->parents,parent_id);
489         
490         if( parent->hash ) {
491                 node = g_hash_table_lookup(parent->hash,name);
492         } else {
493                 node = g_hash_table_lookup(st->names,name);
494         }
495         
496         if ( node == NULL ) 
497                 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
498         
499         switch (mode) {
500                 case MN_INCREASE: node->counter += value; break;
501                 case MN_SET: node->counter = value; break;
502         }
503         
504         if (node) 
505                 return node->id;
506         else
507                 return -1;
508 }
509
510
511 extern guint8* stats_tree_get_abbr(const guint8* optarg) {
512         guint i;
513
514         /* XXX: this fails when tshark is given any options
515            after the -z */
516         g_assert(optarg != NULL);
517         
518         for (i=0; optarg[i] && optarg[i] != ','; i++);
519         
520         if (optarg[i] == ',') {
521                 return g_strndup(optarg,i);
522         } else {
523                 return NULL;
524         }
525 }
526
527
528 /*
529  * This function accepts an input string which should define a long integer range.
530  * The normal result is a struct containing the floor and ceil value of this
531  * range.
532  *
533  * It is allowed to define a range string in the following ways :
534  *
535  * "0-10" -> { 0, 10 }
536  * "-0" -> { G_MININT, 0 }
537  * "0-" -> { 0, G_MAXINT }
538  * "-" -> { G_MININT, G_MAXINT }
539  *
540  * Note that this function is robust to buggy input string. If in some cases it
541  * returns NULL, it but may also return a pair with undefined values.
542  *
543  */
544 static range_pair_t* get_range(guint8* rngstr) {
545         gchar** split;
546         range_pair_t* rng;
547
548         split = g_strsplit((gchar*)rngstr,"-",2);
549
550         /* empty string */
551         if (split[0] == NULL) {
552           g_strfreev(split);
553           return NULL;
554         }
555
556         /* means we have a non empty string 
557          * which does not contain a delimiter */
558         if (split[1] == NULL) {
559           g_strfreev(split);
560           return NULL;
561         }
562
563         rng = g_malloc(sizeof(range_pair_t));
564
565         /* string == "X-?" */
566         if (*(split[0]) != '\0') {
567             rng->floor = strtol(split[0],NULL,10);
568         } else
569           /* string == "-?" */
570           rng->floor = G_MININT;
571
572         /* string != "?-" */
573         if (*(split[1]) != '\0') {
574           rng->ceil  = strtol(split[1],NULL,10);
575         } else
576           /* string == "?-" */
577           rng->ceil = G_MAXINT;
578
579         g_strfreev(split);
580
581         return rng;
582 }
583
584
585 extern int stats_tree_create_range_node(stats_tree* st,
586                                                                 const gchar* name,
587                                                                 int parent_id,
588                                                                 ...) {
589         va_list list;
590         guint8* curr_range;
591         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
592         stat_node* range_node = NULL;
593
594         va_start( list, parent_id );
595         while (( curr_range = va_arg(list, guint8*) )) {
596                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
597                 range_node->rng = get_range(curr_range);
598         }
599         va_end( list );
600
601         return rng_root->id;
602 }
603
604 /****/
605 extern int stats_tree_parent_id_by_name(stats_tree* st, const gchar* parent_name) {
606         stat_node* node = g_hash_table_lookup(st->names,parent_name);
607         
608         if (node)
609                 return node->id;
610         else
611                 return 0; /* XXX: this is the root shoud we return -1 instead?*/
612 }
613
614
615 extern int stats_tree_range_node_with_pname(stats_tree* st,
616                                                                                           const gchar* name,
617                                                                                           const gchar* parent_name,
618                                                                                           ...) {
619         va_list list;
620         guint8* curr_range;
621         stat_node* range_node = NULL;
622         int parent_id = stats_tree_parent_id_by_name(st,parent_name);
623         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
624
625         va_start( list, parent_name );
626         while (( curr_range = va_arg(list, guint8*) )) {
627                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
628                 range_node->rng = get_range(curr_range);
629         }
630         va_end( list );
631         
632         return rng_root->id;
633 }       
634
635
636 extern int stats_tree_tick_range(stats_tree* st,
637                                                  const gchar* name,
638                                                  int parent_id,
639                                                  int value_in_range) {
640         
641         stat_node* node = NULL;
642         stat_node* parent = NULL;
643         stat_node* child = NULL;
644         gint floor, ceil;
645         
646         if (parent_id >= 0 && parent_id < (int) st->parents->len) {
647                 parent = g_ptr_array_index(st->parents,parent_id);
648         } else {
649                 g_assert_not_reached();
650         }
651         
652         if( parent->hash ) {
653                 node = g_hash_table_lookup(parent->hash,name);
654         } else {
655                 node = g_hash_table_lookup(st->names,name);
656         }
657         
658         if ( node == NULL ) 
659                 g_assert_not_reached();
660         
661         for ( child = node->children; child; child = child->next) {
662                 floor =  child->rng->floor;
663                 ceil = child->rng->ceil;
664                 
665                 if ( value_in_range >= floor && value_in_range <= ceil ) {
666                         child->counter++;
667                         return node->id;
668                 }
669         }
670         
671         return node->id;
672 }
673
674 extern int stats_tree_create_pivot(stats_tree* st,
675                                                          const gchar* name,
676                                                          int parent_id) {
677         stat_node* node = new_stat_node(st,name,parent_id,TRUE,TRUE);
678         
679         if (node) 
680                 return node->id;
681         else
682                 return 0;
683 }
684
685 extern int stats_tree_create_pivot_by_pname(stats_tree* st,
686                                                          const gchar* name,
687                                                          const gchar* parent_name) {
688         int parent_id = stats_tree_parent_id_by_name(st,parent_name);
689         stat_node* node;
690         
691         node = new_stat_node(st,name,parent_id,TRUE,TRUE);
692         
693         if (node) 
694                 return node->id;
695         else
696                 return 0;
697 }
698
699 extern int stats_tree_tick_pivot(stats_tree* st,
700                                           int pivot_id,
701                                           const gchar* pivot_value) {
702         
703         stat_node* parent = g_ptr_array_index(st->parents,pivot_id);
704         
705         parent->counter++;
706         stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
707         
708         return pivot_id;
709 }
710