Rename "register_ethereal_tap()" to "register_tap_listener_cmd_arg()" as
[obnox/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 stats_tree_get_strs_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* stats_tree_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_tree_branch_max_namelen(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_tree_branch_max_namelen(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 stats_tree_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_tree_branch_max_namelen(node,0));
115         }
116         
117         stats_tree_get_strs_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                         stats_tree_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     stat_node* next;
148         
149         if (node->children) {
150         for (child = node->children; child; child = next ) {
151             /* child->next will be gone after free_stat_node, so cache it here */
152             next = child->next;
153                         free_stat_node(child);
154         }
155         }
156         
157         if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
158         
159         if (node->hash) g_hash_table_destroy(node->hash);
160
161         if (node->rng) g_free(node->rng);
162         
163         if (node->name) g_free(node->name);
164         
165         g_free(node);
166 }
167
168 /* destroys the whole tree instance */
169 extern void stats_tree_free(stats_tree* st) {
170         stat_node* child;
171         stat_node* next;
172         
173         g_free(st->filter);
174         g_hash_table_destroy(st->names);
175         g_ptr_array_free(st->parents,FALSE);
176         
177     for (child = st->root.children; child; child = next ) {
178         /* child->next will be gone after free_stat_node, so cache it here */
179         next = child->next;
180                 free_stat_node(child);
181     }
182         
183         if (st->cfg->free_tree_pr)
184                 st->cfg->free_tree_pr(st);
185         
186         if (st->cfg->cleanup)
187                 st->cfg->cleanup(st);
188         
189         g_free(st);
190 }
191
192
193 /* reset a node to its original state */
194 static void reset_stat_node(stat_node* node) {
195         stat_node* child;
196         
197         if (node->children) {
198                 for (child = node->children; child; child = child->next ) 
199                         reset_stat_node(child);
200         }
201         
202         node->counter = 0;
203         
204         if(node->st->cfg->reset_node) {
205                 node->st->cfg->reset_node(node);
206         }
207         
208 }
209
210 /* reset the whole stats_tree */
211 extern void stats_tree_reset(void* p) {
212         stats_tree* st = p;
213         reset_stat_node(&st->root);
214         
215         if (st->cfg->reset_tree) {
216                 st->cfg->reset_tree(st);
217         }
218 }
219
220 extern void stats_tree_reinit(void* p) {
221         stats_tree* st = p;
222         stat_node* child;
223         stat_node* next;
224         
225         for (child = st->root.children; child; child = next) {
226         /* child->next will be gone after free_stat_node, so cache it here */
227         next = child->next;
228                 free_stat_node(child);
229         }
230         
231         st->root.children = NULL;
232         st->root.counter = 0;
233         
234         if (st->cfg->init) {
235                 st->cfg->init(st);
236         }
237 }
238
239 /* register a new stats_tree */
240 extern void stats_tree_register(guint8* tapname,
241                                                                 guint8* abbr, 
242                                                                 guint8* name,
243                                                                 stat_tree_packet_cb packet,
244                                                                 stat_tree_init_cb init,
245                                                                 stat_tree_cleanup_cb cleanup) {
246         
247         stats_tree_cfg* cfg = g_malloc( sizeof(stats_tree_cfg) );
248
249         /* at the very least the abbrev and the packet function should be given */ 
250         g_assert( tapname && abbr && packet );
251
252         cfg->tapname = g_strdup(tapname);
253         cfg->abbr = g_strdup(abbr);
254         cfg->name = name ? g_strdup(name) : g_strdup(abbr);
255         
256         cfg->packet = packet;
257         cfg->init = init;
258         cfg->cleanup = cleanup;
259         
260         /* these have to be filled in by implementations */
261         cfg->setup_node_pr = NULL;
262         cfg->new_tree_pr = NULL;
263         cfg->free_node_pr = NULL;
264         cfg->free_tree_pr = NULL;
265         cfg->draw_node = NULL;
266         cfg->draw_tree = NULL;
267         cfg->reset_node = NULL;
268         cfg->reset_tree = NULL;
269
270         if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
271
272         g_hash_table_insert(registry,cfg->abbr,cfg);
273         
274 }
275
276 extern stats_tree* stats_tree_new(stats_tree_cfg* cfg, tree_pres* pr,char* filter) {
277         stats_tree* st = g_malloc(sizeof(stats_tree));
278
279         st->cfg = cfg;
280         st->pr = pr;
281
282         st->names = g_hash_table_new(g_str_hash,g_str_equal);
283         st->parents = g_ptr_array_new();
284         st->filter = filter;
285         
286         st->start = -1.0;
287         st->elapsed = 0.0;
288         st->highest_seen = 0;
289         
290         st->root.counter = 0;
291         st->root.name = g_strdup(cfg->name);
292         st->root.st = st;
293         st->root.parent = NULL;
294         st->root.children = NULL;
295         st->root.next = NULL;
296         st->root.hash = NULL;
297         st->root.pr = NULL;
298         
299         g_ptr_array_add(st->parents,&st->root);
300         
301         return st;
302 }       
303
304 /* will be the tap packet cb */
305 extern int stats_tree_packet(void* p, packet_info* pinfo, epan_dissect_t *edt, const void *pri) {
306         stats_tree* st = p;
307         float now;
308         
309         if (st->highest_seen >= pinfo->fd->num) return 0;
310         
311         st->highest_seen = pinfo->fd->num;
312         
313         now = (((float)pinfo->fd->rel_secs) + (((float)pinfo->fd->rel_usecs)/1000000) );
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 GHashTable* stat_tree_registry(void) {
326         return registry;
327 }
328
329 extern stats_tree_cfg* stats_tree_get_cfg_by_abbr(guint8* abbr) {
330         return g_hash_table_lookup(registry,abbr);
331 }
332
333
334 struct _stats_tree_pres_cbs {
335         void (*setup_node_pr)(stat_node*);
336         void (*free_node_pr)(stat_node*);
337         void (*draw_node)(stat_node*);
338         void (*reset_node)(stat_node*);
339         tree_pres* (*new_tree_pr)(stats_tree*);
340         void (*free_tree_pr)(stats_tree*);
341         void (*draw_tree)(stats_tree*);
342         void (*reset_tree)(stats_tree*);
343 };
344
345 static void setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p) {
346         stats_tree_cfg* cfg = v;
347         struct _stats_tree_pres_cbs *d = p;
348         
349         cfg->setup_node_pr = d->setup_node_pr;
350         cfg->new_tree_pr = d->new_tree_pr;
351         cfg->free_node_pr = d->free_node_pr;
352         cfg->free_tree_pr = d->free_tree_pr;
353         cfg->draw_node = d->draw_node;
354         cfg->draw_tree = d->draw_tree;
355         cfg->reset_node = d->reset_node;
356         cfg->reset_tree = d->reset_tree;
357         
358 }
359
360 extern void stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
361                                                                         void (*setup_node_pr)(stat_node*),
362                                                                         void (*free_node_pr)(stat_node*),
363                                                                         void (*draw_node)(stat_node*),
364                                                                         void (*reset_node)(stat_node*),
365                                                                         tree_pres* (*new_tree_pr)(stats_tree*),
366                                                                         void (*free_tree_pr)(stats_tree*),
367                                                                         void (*draw_tree)(stats_tree*),
368                                                                         void (*reset_tree)(stats_tree*),
369                                                                         void* data) {
370         static struct _stats_tree_pres_cbs d;
371         
372         d.setup_node_pr = setup_node_pr;
373         d.new_tree_pr = new_tree_pr;
374         d.free_node_pr = free_node_pr;
375         d.free_tree_pr = free_tree_pr;
376         d.draw_node = draw_node;
377         d.draw_tree = draw_tree;
378         d.reset_node = reset_node;
379         d.reset_tree = reset_tree;
380         
381         if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
382         
383         if (registry_iterator && registry)
384                 g_hash_table_foreach(registry,registry_iterator,data);
385         
386 }
387
388
389 /* creates a stat_tree node
390 *    name: the name of the stats_tree node
391 *    parent_name: the name of the ALREADY REGISTERED parent
392 *    with_hash: whether or not it should keep a hash with it's children names
393 *    as_named_node: whether or not it has to be registered in the root namespace
394 */
395 static stat_node*  new_stat_node(stats_tree* st,
396                                                                  const gchar* name,
397                                                                  int parent_id,
398                                                                  gboolean with_hash,
399                                                                  gboolean as_parent_node) {
400         
401         stat_node *node = g_malloc (sizeof(stat_node));
402         stat_node* last_chld = NULL;
403         
404         node->counter = 0;
405         node->name = g_strdup(name);
406         node->children = NULL;
407         node->next = NULL;
408         node->st = (stats_tree*) st;
409         node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
410         node->parent = NULL;
411         node->rng  =  NULL;
412
413         if (as_parent_node) {
414                 g_hash_table_insert(st->names,
415                                                         node->name,
416                                                         node);
417                 
418                 g_ptr_array_add(st->parents,node);
419                 
420                 node->id = st->parents->len - 1;
421         } else {
422                 node->id = -1;
423         }
424         
425         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
426                 node->parent = g_ptr_array_index(st->parents,parent_id);
427         } else {
428                 /* ??? should we set the parent to be root ??? */
429                 g_assert_not_reached();
430         }
431         
432         if (node->parent->children) {
433                 /* insert as last child */
434                 
435                 for (last_chld = node->parent->children;
436                          last_chld->next;
437                          last_chld = last_chld->next ) ;
438                 
439                 last_chld->next = node;
440                 
441         } else {
442                 /* insert as first child */
443                 node->parent->children = node;
444         }
445         
446         if(node->parent->hash) {
447                 g_hash_table_insert(node->parent->hash,node->name,node);
448         }
449         
450         if (st->cfg->setup_node_pr) {
451                 st->cfg->setup_node_pr(node);
452         } else {
453                 node->pr = NULL;
454         }
455         
456         return node;
457 }
458 /***/
459
460 extern int stats_tree_create_node(stats_tree* st, const gchar* name, int parent_id, gboolean with_hash) {
461         stat_node* node = new_stat_node(st,name,parent_id,with_hash,TRUE);
462         
463         if (node) 
464                 return node->id;
465         else
466                 return 0;
467 }
468
469 /* XXX: should this be a macro? */
470 extern int stats_tree_create_node_by_pname(stats_tree* st,
471                                                                                    const gchar* name,
472                                                                                    const gchar* parent_name,
473                                                                                    gboolean with_children) {
474         return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
475 }
476
477
478
479 /*
480  * Increases by delta the counter of the node whose name is given
481  * if the node does not exist yet it's created (with counter=1)
482  * using parent_name as parent node.
483  * with_hash=TRUE to indicate that the created node will have a parent
484  */
485 extern int stats_tree_manip_node(manip_node_mode mode, stats_tree* st, const guint8* name, int parent_id, gboolean with_hash, gint value) {
486         stat_node* node = NULL;
487         stat_node* parent = NULL;
488         
489         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
490                 parent = g_ptr_array_index(st->parents,parent_id);
491         } else {
492                 g_assert_not_reached();
493         }
494         
495         if( parent->hash ) {
496                 node = g_hash_table_lookup(parent->hash,name);
497         } else {
498                 node = g_hash_table_lookup(st->names,name);
499         }
500         
501         if ( node == NULL ) 
502                 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
503         
504         switch (mode) {
505                 case MN_INCREASE: node->counter += value; break;
506                 case MN_SET: node->counter = value; break;
507         }
508         
509         if (node) 
510                 return node->id;
511         else
512                 return -1;
513 }
514
515
516 extern guint8* stats_tree_get_abbr(const guint8* optarg) {
517         guint i;
518
519         /* XXX: this fails when tethereal is given any options
520            after the -z */
521         g_assert(optarg != NULL);
522         
523         for (i=0; optarg[i] && optarg[i] != ','; i++);
524         
525         if (optarg[i] == ',') {
526                 return g_strndup(optarg,i);
527         } else {
528                 return NULL;
529         }
530 }
531
532
533 static range_pair_t* get_range(guint8* rngstr) {
534         gchar** split;
535         range_pair_t* rng = g_malloc(sizeof(range_pair_t));
536         
537         split =  g_strsplit(rngstr,"-",2);
538
539         rng->floor = strtol(split[0],NULL,10);
540         rng->ceil  = strtol(split[1],NULL,10);
541         
542         if (rng->ceil == 0) rng->ceil = G_MAXINT;
543         if (rng->floor == 0) rng->floor = G_MININT;
544
545         g_strfreev(split);
546         
547         return rng;
548 }
549
550
551 extern int stats_tree_create_range_node(stats_tree* st,
552                                                                 const gchar* name,
553                                                                 int parent_id,
554                                                                 ...) {
555         va_list list;
556         guint8* curr_range;
557         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
558         stat_node* range_node = NULL;
559         
560         va_start( list, parent_id );
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 stats_tree_parent_id_by_name(stats_tree* st, const gchar* parent_name) {
572         stat_node* node = g_hash_table_lookup(st->names,parent_name);
573         
574         if (node)
575                 return node->id;
576         else
577                 return 0; /* XXX: this is the root shoud we return -1 instead?*/
578 }
579
580
581 extern int stats_tree_range_node_with_pname(stats_tree* st,
582                                                                                           const gchar* name,
583                                                                                           const gchar* parent_name,
584                                                                                           ...) {
585         va_list list;
586         guint8* curr_range;
587         stat_node* range_node = NULL;
588         int parent_id = stats_tree_parent_id_by_name(st,parent_name);
589         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
590
591         va_start( list, parent_name );
592         while (( curr_range = va_arg(list, guint8*) )) {
593                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
594                 range_node->rng = get_range(curr_range);
595         }
596         va_end( list );
597         
598         return rng_root->id;
599 }       
600
601
602 extern int stats_tree_tick_range(stats_tree* st,
603                                                  const gchar* name,
604                                                  int parent_id,
605                                                  int value_in_range) {
606         
607         stat_node* node = NULL;
608         stat_node* parent = NULL;
609         stat_node* child = NULL;
610         gint floor, ceil;
611         
612         if (parent_id >= 0 && parent_id < (int) st->parents->len) {
613                 parent = g_ptr_array_index(st->parents,parent_id);
614         } else {
615                 g_assert_not_reached();
616         }
617         
618         if( parent->hash ) {
619                 node = g_hash_table_lookup(parent->hash,name);
620         } else {
621                 node = g_hash_table_lookup(st->names,name);
622         }
623         
624         if ( node == NULL ) 
625                 return node->id;
626         
627         for ( child = node->children; child; child = child->next) {
628                 floor =  child->rng->floor;
629                 ceil = child->rng->ceil;
630                 
631                 if ( value_in_range >= floor && value_in_range <= ceil ) {
632                         child->counter++;
633                         return node->id;
634                 }
635         }
636         
637         return node->id;
638 }
639
640 extern int stats_tree_create_pivot(stats_tree* st,
641                                                          const gchar* name,
642                                                          int parent_id) {
643         stat_node* node = new_stat_node(st,name,parent_id,TRUE,TRUE);
644         
645         if (node) 
646                 return node->id;
647         else
648                 return 0;
649 }
650
651 extern int stats_tree_create_pivot_by_pname(stats_tree* st,
652                                                          const gchar* name,
653                                                          const gchar* parent_name) {
654         int parent_id = stats_tree_parent_id_by_name(st,parent_name);
655         stat_node* node;
656         
657         node = new_stat_node(st,name,parent_id,TRUE,TRUE);
658         
659         if (node) 
660                 return node->id;
661         else
662                 return 0;
663 }
664
665 extern int stats_tree_tick_pivot(stats_tree* st,
666                                           int pivot_id,
667                                           const gchar* pivot_value) {
668         
669         stat_node* parent = g_ptr_array_index(st->parents,pivot_id);
670         
671         parent->counter++;
672         stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
673         
674         return pivot_id;
675 }
676