summaryrefslogtreecommitdiffstats
path: root/ui/cli/tap-stats_tree.c
blob: 64557e40d48a5a93e0b5401a07f73de6f95a8cc7 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* tap-stats_tree.c
 * tshark's tap implememntation of stats_tree
 * 2005, Luis E. G. Ontanon
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <glib.h>

#include <wsutil/report_message.h>

#include <epan/stats_tree_priv.h>
#include <epan/stat_tap_ui.h>

void register_tap_listener_stats_tree_stat(void);

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

struct _tree_pres {
	void **dummy;
};

struct _tree_cfg_pres {
	gchar *init_string;
};

static void
draw_stats_tree(void *psp)
{
	stats_tree *st = (stats_tree *)psp;
	GString *s;

	s= stats_tree_format_as_str(st, ST_FORMAT_PLAIN, stats_tree_get_default_sort_col(st),
				    stats_tree_is_default_sort_DESC(st));

	printf("%s", s->str);
	g_string_free(s, TRUE);
}

static void
init_stats_tree(const char *opt_arg, void *userdata _U_)
{
	char *abbr = stats_tree_get_abbr(opt_arg);
	GString	*error_string;
	stats_tree_cfg *cfg = NULL;
	stats_tree *st = NULL;
	const char* filter = NULL;
	size_t len;

	if (abbr) {
		cfg = stats_tree_get_cfg_by_abbr(abbr);

		if (cfg != NULL) {
			len = strlen(cfg->pr->init_string);
			if (strncmp(opt_arg, cfg->pr->init_string, len) == 0) {
				if (opt_arg[len] == ',') {
					filter = opt_arg + len + 1;
				}
				st = stats_tree_new(cfg, NULL, filter);
			} else {
				report_failure("Wrong stats_tree (%s) found when looking at ->init_string", abbr);
				return;
			}
		} else {
			report_failure("no such stats_tree (%s) found in stats_tree registry", abbr);
			return;
		}

		g_free(abbr);

	} else {
		report_failure("could not obtain stats_tree from arg '%s'", opt_arg);
		return;
	}

	error_string = register_tap_listener(st->cfg->tapname,
					     st,
					     st->filter,
					     st->cfg->flags,
					     stats_tree_reset,
					     stats_tree_packet,
					     draw_stats_tree,
					     NULL);

	if (error_string) {
		report_failure("stats_tree for: %s failed to attach to the tap: %s", cfg->name, error_string->str);
		return;
	}

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

}

static void
register_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
{
	stats_tree_cfg *cfg = (stats_tree_cfg *)v;
	stat_tap_ui ui_info;

	cfg->pr = wmem_new(wmem_epan_scope(), tree_cfg_pres);
	cfg->pr->init_string = wmem_strdup_printf(wmem_epan_scope(), "%s,tree", cfg->abbr);

	ui_info.group = REGISTER_STAT_GROUP_GENERIC;
	ui_info.title = NULL;
	ui_info.cli_string = cfg->pr->init_string;
	ui_info.tap_init_cb = init_stats_tree;
	ui_info.nparams = 0;
	ui_info.params = NULL;
	register_stat_tap_ui(&ui_info, NULL);

}

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,
                free_tree_presentation, NULL);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */