aboutsummaryrefslogtreecommitdiffstats
path: root/tap-stats_tree.c
blob: 4c0f3a1925961a0dfeb635292d37a77e7c9b0c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* tap-stats_tree.c
 * tethereal's tap implememntation of stats_tree
 * 2005, Luis E. G. Ontanon
 *
 * $Id: $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include <glib.h>
#include <epan/stats_tree_priv.h>

/* actually unused */
struct _st_node_pres {
	void* dummy;
};

struct _tree_pres {
	void* dummy;
};

struct _tree_cfg_pres {
	guint8* init_string;	
};

static void draw_stats_tree(void *psp) {
	stats_tree *st = psp;
	GString* s;
	gchar* fmt;
	stat_node* child;
	
	s = g_string_new("\n===================================================================\n");
	fmt = g_strdup_printf(" %%s%%-%us%%12s\t%%12s\t%%12s\n",stats_branch_max_name_len(&st->root,0));
	g_string_sprintfa(s,fmt,"",st->cfg->name,"value","rate","percent");
	g_free(fmt);
	g_string_sprintfa(s,"-------------------------------------------------------------------\n");
	
	for (child = st->root.children; child; child = child->next ) {
		stat_branch_to_str(child,s,0);
	}
	
	s = g_string_append(s,"\n===================================================================\n");
	
	printf("%s",s->str);
	
}

static void  init_stats_tree(char *optarg) {
	guint8* abbr = get_st_abbr(optarg);
	GString	*error_string;
	stats_tree_cfg *cfg = NULL;
	stats_tree* st = NULL;
	
	if (abbr) {
		cfg = get_stats_tree_by_abbr(abbr);

		if (cfg != NULL) {
			if (strncmp (optarg, cfg->pr->init_string, strlen(cfg->pr->init_string)) == 0){
				st = new_stats_tree(cfg,NULL,((guint8*)optarg)+strlen(cfg->pr->init_string));
			} else {
				st->filter=NULL;
			}
		} else {
			g_error("no such stats_tree (%s) found in stats_tree registry",abbr);
		}
		
		g_free(abbr);
		
	} else {
		g_error("could not obtain stats_tree abbr (%s) from optarg '%s'",abbr,optarg);		
	}
	
	error_string = register_tap_listener( st->cfg->tapname,
										  st,
										  st->filter,
										  reset_stats_tree,
										  stats_tree_packet,
										  draw_stats_tree);
	
	if (error_string) {
		g_error("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
	}

	if (cfg->init) cfg->init(st);

}

void register_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_) {
	stats_tree_cfg* cfg = v;
	
	cfg->pr = g_malloc(sizeof(tree_cfg_pres));
	cfg->pr->init_string = g_strdup_printf("%s,tree",cfg->abbr);

	register_ethereal_tap(cfg->pr->init_string, init_stats_tree);
	
}

static void free_tree_presentation(stats_tree* st) {
	g_free(st->pr);
}


void
register_tap_listener_stats_tree_stat(void)
{
	stats_tree_presentation(register_stats_tree_tap,
							NULL, NULL, NULL, NULL, NULL,
							free_tree_presentation, NULL, NULL, NULL);
}