2002-11-01 05:29:36 +00:00
|
|
|
/* tap-protohierstat.c
|
|
|
|
* protohierstat 2002 Ronnie Sahlberg
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2002-11-01 05:29:36 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2007-04-18 04:34:10 +00:00
|
|
|
*
|
2018-04-30 07:47:58 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2002-11-01 05:29:36 +00:00
|
|
|
|
2006-05-31 17:38:42 +00:00
|
|
|
/* This module provides ProtocolHierarchyStatistics for tshark */
|
2002-11-01 05:29:36 +00:00
|
|
|
|
2012-09-20 01:29:52 +00:00
|
|
|
#include "config.h"
|
2002-11-01 05:29:36 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-11-10 15:59:37 +00:00
|
|
|
#include <stdlib.h>
|
2002-11-01 05:29:36 +00:00
|
|
|
#include <string.h>
|
2013-11-10 15:59:37 +00:00
|
|
|
|
2002-11-01 05:29:36 +00:00
|
|
|
#include "epan/epan_dissect.h"
|
2004-09-29 00:06:36 +00:00
|
|
|
#include <epan/tap.h>
|
2014-11-14 18:51:40 +00:00
|
|
|
#include <epan/stat_tap_ui.h>
|
2002-11-01 05:29:36 +00:00
|
|
|
|
2019-01-01 00:55:23 +00:00
|
|
|
#include <ui/cmdarg_err.h>
|
2018-12-30 02:24:56 +00:00
|
|
|
|
2013-11-14 06:21:02 +00:00
|
|
|
void register_tap_listener_protohierstat(void);
|
|
|
|
|
2002-11-01 05:29:36 +00:00
|
|
|
typedef struct _phs_t {
|
|
|
|
struct _phs_t *sibling;
|
|
|
|
struct _phs_t *child;
|
|
|
|
struct _phs_t *parent;
|
|
|
|
char *filter;
|
|
|
|
int protocol;
|
2005-06-24 01:32:42 +00:00
|
|
|
const char *proto_name;
|
2002-11-01 05:29:36 +00:00
|
|
|
guint32 frames;
|
2007-04-18 04:34:10 +00:00
|
|
|
guint64 bytes;
|
2002-11-01 05:29:36 +00:00
|
|
|
} phs_t;
|
|
|
|
|
|
|
|
|
|
|
|
static phs_t *
|
|
|
|
new_phs_t(phs_t *parent)
|
|
|
|
{
|
|
|
|
phs_t *rs;
|
2014-10-13 19:04:21 +00:00
|
|
|
rs = g_new(phs_t, 1);
|
|
|
|
rs->sibling = NULL;
|
|
|
|
rs->child = NULL;
|
|
|
|
rs->parent = parent;
|
|
|
|
rs->filter = NULL;
|
|
|
|
rs->protocol = -1;
|
|
|
|
rs->proto_name = NULL;
|
|
|
|
rs->frames = 0;
|
|
|
|
rs->bytes = 0;
|
2002-11-01 05:29:36 +00:00
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2005-01-01 02:57:02 +00:00
|
|
|
protohierstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
|
2002-11-01 05:29:36 +00:00
|
|
|
{
|
2014-10-13 19:04:21 +00:00
|
|
|
phs_t *rs = (phs_t *)prs;
|
2002-11-01 05:29:36 +00:00
|
|
|
phs_t *tmprs;
|
2009-08-09 17:33:23 +00:00
|
|
|
proto_node *node;
|
2002-11-01 05:29:36 +00:00
|
|
|
field_info *fi;
|
|
|
|
|
2014-10-13 19:04:21 +00:00
|
|
|
if (!edt) {
|
2002-11-01 05:29:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-13 19:04:21 +00:00
|
|
|
if (!edt->tree) {
|
2002-11-01 05:29:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-13 19:04:21 +00:00
|
|
|
if (!edt->tree->first_child) {
|
2002-11-01 05:29:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-13 19:04:21 +00:00
|
|
|
for (node=edt->tree->first_child; node; node=node->next) {
|
|
|
|
fi = PNODE_FINFO(node);
|
2002-11-01 05:29:36 +00:00
|
|
|
|
|
|
|
/* first time we saw a protocol at this leaf */
|
2014-10-13 19:04:21 +00:00
|
|
|
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);
|
|
|
|
rs = rs->child;
|
2002-11-01 05:29:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find this protocol in the list of siblings */
|
2014-10-13 19:04:21 +00:00
|
|
|
for (tmprs=rs; tmprs; tmprs=tmprs->sibling) {
|
|
|
|
if (tmprs->protocol == fi->hfinfo->id) {
|
2002-11-01 05:29:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found, then we must add it to the end of the list */
|
2014-10-13 19:04:21 +00:00
|
|
|
if (!tmprs) {
|
|
|
|
for (tmprs=rs; tmprs->sibling; tmprs=tmprs->sibling)
|
2002-11-01 05:29:36 +00:00
|
|
|
;
|
2014-10-13 19:04:21 +00:00
|
|
|
tmprs->sibling = new_phs_t(rs->parent);
|
|
|
|
rs = tmprs->sibling;
|
|
|
|
rs->protocol = fi->hfinfo->id;
|
|
|
|
rs->proto_name = fi->hfinfo->abbrev;
|
2002-11-01 05:29:36 +00:00
|
|
|
} else {
|
2014-10-13 19:04:21 +00:00
|
|
|
rs = tmprs;
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rs->frames++;
|
2014-10-13 19:04:21 +00:00
|
|
|
rs->bytes += pinfo->fd->pkt_len;
|
2002-11-01 05:29:36 +00:00
|
|
|
|
2014-10-13 19:04:21 +00:00
|
|
|
if (!rs->child) {
|
|
|
|
rs->child = new_phs_t(rs);
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
2014-10-13 19:04:21 +00:00
|
|
|
rs = rs->child;
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
phs_draw(phs_t *rs, int indentation)
|
|
|
|
{
|
2005-10-12 00:34:51 +00:00
|
|
|
int i, stroff;
|
|
|
|
#define MAXPHSLINE 80
|
|
|
|
char str[MAXPHSLINE];
|
2014-10-13 19:04:21 +00:00
|
|
|
for (;rs;rs = rs->sibling) {
|
|
|
|
if (rs->protocol == -1) {
|
2002-11-01 05:29:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-13 19:04:21 +00:00
|
|
|
str[0] = 0;
|
|
|
|
stroff = 0;
|
|
|
|
for (i=0; i<indentation; i++) {
|
|
|
|
if (i > 15) {
|
|
|
|
stroff += g_snprintf(str+stroff, MAXPHSLINE-stroff, "...");
|
2005-10-12 00:34:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-13 19:04:21 +00:00
|
|
|
stroff += g_snprintf(str+stroff, MAXPHSLINE-stroff, " ");
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
2011-02-18 22:43:48 +00:00
|
|
|
g_snprintf(str+stroff, MAXPHSLINE-stroff, "%s", rs->proto_name);
|
2015-02-09 18:42:51 +00:00
|
|
|
printf("%-40s frames:%u bytes:%" G_GINT64_MODIFIER "u\n", str, rs->frames, rs->bytes);
|
2002-11-01 05:29:36 +00:00
|
|
|
phs_draw(rs->child, indentation+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
protohierstat_draw(void *prs)
|
|
|
|
{
|
2014-10-13 19:04:21 +00:00
|
|
|
phs_t *rs = (phs_t *)prs;
|
2002-11-01 05:29:36 +00:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("===================================================================\n");
|
|
|
|
printf("Protocol Hierarchy Statistics\n");
|
2014-10-13 19:04:21 +00:00
|
|
|
printf("Filter: %s\n\n", rs->filter ? rs->filter : "");
|
|
|
|
phs_draw(rs, 0);
|
2002-11-01 05:29:36 +00:00
|
|
|
printf("===================================================================\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2014-10-13 19:04:21 +00:00
|
|
|
protohierstat_init(const char *opt_arg, void *userdata _U_)
|
2002-11-01 05:29:36 +00:00
|
|
|
{
|
|
|
|
phs_t *rs;
|
2014-10-13 19:04:21 +00:00
|
|
|
int pos = 0;
|
|
|
|
const char *filter = NULL;
|
2016-06-19 09:01:56 +00:00
|
|
|
GString *error_string;
|
2002-11-01 05:29:36 +00:00
|
|
|
|
2014-10-13 19:04:21 +00:00
|
|
|
if (strcmp("io,phs", opt_arg) == 0) {
|
2009-06-05 22:42:47 +00:00
|
|
|
/* No arguments */
|
2014-10-13 19:04:21 +00:00
|
|
|
} else if (sscanf(opt_arg, "io,phs,%n", &pos) == 0) {
|
|
|
|
if (pos) {
|
|
|
|
filter = opt_arg+pos;
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-30 02:24:56 +00:00
|
|
|
cmdarg_err("invalid \"-z io,phs[,<filter>]\" argument");
|
2002-11-01 05:29:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-10-13 19:04:21 +00:00
|
|
|
rs = new_phs_t(NULL);
|
2017-08-26 08:30:47 +00:00
|
|
|
rs->filter = g_strdup(filter);
|
2002-11-01 05:29:36 +00:00
|
|
|
|
2018-07-21 00:07:19 +00:00
|
|
|
error_string = register_tap_listener("frame", rs, filter, TL_REQUIRES_PROTO_TREE, NULL, protohierstat_packet, protohierstat_draw, NULL);
|
2014-10-13 19:04:21 +00:00
|
|
|
if (error_string) {
|
2002-11-01 05:29:36 +00:00
|
|
|
/* error, we failed to attach to the tap. clean up */
|
|
|
|
g_free(rs->filter);
|
|
|
|
g_free(rs);
|
|
|
|
|
2018-12-30 02:24:56 +00:00
|
|
|
cmdarg_err("Couldn't register io,phs tap: %s",
|
2016-06-19 09:01:56 +00:00
|
|
|
error_string->str);
|
|
|
|
g_string_free(error_string, TRUE);
|
2002-11-01 05:29:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:42:26 +00:00
|
|
|
static stat_tap_ui protohierstat_ui = {
|
2014-11-14 18:31:04 +00:00
|
|
|
REGISTER_STAT_GROUP_GENERIC,
|
|
|
|
NULL,
|
|
|
|
"io,phs",
|
|
|
|
protohierstat_init,
|
|
|
|
0,
|
|
|
|
NULL
|
|
|
|
};
|
2002-11-01 05:29:36 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
register_tap_listener_protohierstat(void)
|
|
|
|
{
|
2014-11-14 18:42:26 +00:00
|
|
|
register_stat_tap_ui(&protohierstat_ui, NULL);
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 19:04:21 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://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:
|
|
|
|
*/
|