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