summaryrefslogtreecommitdiffstats
path: root/ui/cli/tap-protohierstat.c
blob: 2628fdcf3ccf2d063508915b2f61b2fd7d5010b3 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* tap-protohierstat.c
 * protohierstat   2002 Ronnie Sahlberg
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/* This module provides ProtocolHierarchyStatistics for tshark */

#include "config.h"

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

#include "epan/epan_dissect.h"
#include <epan/tap.h>
#include <epan/stat_tap_ui.h>

#include <wsutil/cmdarg_err.h>
#include "tap-protohierstat.h"

int pc_proto_id = -1;

void register_tap_listener_protohierstat(void);

phs_t *
new_phs_t(phs_t *parent, const char *filter)
{
	phs_t *rs;
	rs = g_new(phs_t, 1);
	rs->sibling    = NULL;
	rs->child      = NULL;
	rs->parent     = parent;
	rs->filter     = NULL;
	if (filter != NULL) {
		rs->filter = g_strdup(filter);
	}
	rs->protocol   = -1;
	rs->proto_name = NULL;
	rs->frames     = 0;
	rs->bytes      = 0;
	return rs;
}

void
free_phs(phs_t *rs)
{
	if (!rs) {
		return;
	}
	if (rs->filter) {
		g_free(rs->filter);
		rs->filter = NULL;
	}
	if (rs->sibling)
	{
		free_phs(rs->sibling);
		rs->sibling = NULL;
	}
	if (rs->child)
	{
		free_phs(rs->child);
		rs->child = NULL;
	}
	g_free(rs);
}

tap_packet_status
protohierstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_, tap_flags_t flags _U_)
{
	phs_t *rs = (phs_t *)prs;
	phs_t *tmprs;
	proto_node *node;
	field_info *fi;

	if (!edt) {
		return TAP_PACKET_DONT_REDRAW;
	}
	if (!edt->tree) {
		return TAP_PACKET_DONT_REDRAW;
	}
	if (!edt->tree->first_child) {
		return TAP_PACKET_DONT_REDRAW;
	}

	for (node=edt->tree->first_child; node; node=node->next) {
		fi = PNODE_FINFO(node);

		/*
		 * If the first child is a tree of comments, skip over it.
		 * This keeps us from having a top-level "pkt_comment"
		 * entry that represents a nonexistent protocol,
		 * and matches how the GUI treats comments.
		 */
		if (G_UNLIKELY(fi->hfinfo->id == pc_proto_id)) {
			continue;
		}

		/* first time we saw a protocol at this leaf */
		if (rs->protocol == -1) {
			rs->protocol = fi->hfinfo->id;
			rs->proto_name = fi->hfinfo->abbrev;
			rs->frames = 1;
			rs->bytes = pinfo->fd->pkt_len;
			rs->child = new_phs_t(rs, NULL);
			rs = rs->child;
			continue;
		}

		/* find this protocol in the list of siblings */
		for (tmprs=rs; tmprs; tmprs=tmprs->sibling) {
			if (tmprs->protocol == fi->hfinfo->id) {
				break;
			}
		}

		/* not found, then we must add it to the end of the list */
		if (!tmprs) {
			for (tmprs=rs; tmprs->sibling; tmprs=tmprs->sibling)
				;
			tmprs->sibling = new_phs_t(rs->parent, NULL);
			rs = tmprs->sibling;
			rs->protocol = fi->hfinfo->id;
			rs->proto_name = fi->hfinfo->abbrev;
		} else {
			rs = tmprs;
		}

		rs->frames++;
		rs->bytes += pinfo->fd->pkt_len;

		if (!rs->child) {
			rs->child = new_phs_t(rs, NULL);
		}
		rs = rs->child;
	}
	return TAP_PACKET_REDRAW;
}

static void
phs_draw(phs_t *rs, int indentation)
{
	int i, stroff;
#define MAXPHSLINE 80
	char str[MAXPHSLINE];
	for (;rs;rs = rs->sibling) {
		if (rs->protocol == -1) {
			return;
		}
		str[0] = 0;
		stroff = 0;
		for (i=0; i<indentation; i++) {
			if (i > 15) {
				stroff += snprintf(str+stroff, MAXPHSLINE-stroff, "...");
				break;
			}
			stroff += snprintf(str+stroff, MAXPHSLINE-stroff, "  ");
		}
		snprintf(str+stroff, MAXPHSLINE-stroff, "%s", rs->proto_name);
		printf("%-40s frames:%u bytes:%" PRIu64 "\n", str, rs->frames, rs->bytes);
		phs_draw(rs->child, indentation+1);
	}
}

static void
protohierstat_draw(void *prs)
{
	phs_t *rs = (phs_t *)prs;

	printf("\n");
	printf("===================================================================\n");
	printf("Protocol Hierarchy Statistics\n");
	printf("Filter: %s\n\n", rs->filter ? rs->filter : "");
	phs_draw(rs, 0);
	printf("===================================================================\n");
}


static void
protohierstat_init(const char *opt_arg, void *userdata _U_)
{
	phs_t *rs;
	int pos = 0;
	const char *filter = NULL;
	GString *error_string;

	if (strcmp("io,phs", opt_arg) == 0) {
		/* No arguments */
	} else if (sscanf(opt_arg, "io,phs,%n", &pos) == 0) {
		if (pos) {
			filter = opt_arg+pos;
		}
	} else {
		cmdarg_err("invalid \"-z io,phs[,<filter>]\" argument");
		exit(1);
	}

	pc_proto_id = proto_registrar_get_id_byname("pkt_comment");

	rs = new_phs_t(NULL, filter);

	error_string = register_tap_listener("frame", rs, filter, TL_REQUIRES_PROTO_TREE, NULL, protohierstat_packet, protohierstat_draw, NULL);
	if (error_string) {
		/* error, we failed to attach to the tap. clean up */
		free_phs(rs);

		cmdarg_err("Couldn't register io,phs tap: %s",
			error_string->str);
		g_string_free(error_string, TRUE);
		exit(1);
	}
}

static stat_tap_ui protohierstat_ui = {
	REGISTER_STAT_GROUP_GENERIC,
	NULL,
	"io,phs",
	protohierstat_init,
	0,
	NULL
};

void
register_tap_listener_protohierstat(void)
{
	register_stat_tap_ui(&protohierstat_ui, 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:
 */