2 * protohierstat 2002 Ronnie Sahlberg
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 /* This module provides ProtocolHierarchyStatistics for tshark */
32 #include "epan/packet_info.h"
33 #include "epan/epan_dissect.h"
34 #include "epan/proto.h"
36 #include <epan/stat_cmd_args.h>
38 typedef struct _phs_t {
39 struct _phs_t *sibling;
41 struct _phs_t *parent;
44 const char *proto_name;
51 new_phs_t(phs_t *parent)
68 protohierstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
70 phs_t *rs=(phs_t *)prs;
81 if(!edt->tree->first_child){
85 for(node=edt->tree->first_child;node;node=node->next){
88 /* first time we saw a protocol at this leaf */
90 rs->protocol=fi->hfinfo->id;
91 rs->proto_name=fi->hfinfo->abbrev;
93 rs->bytes=pinfo->fd->pkt_len;
94 rs->child=new_phs_t(rs);
99 /* find this protocol in the list of siblings */
100 for(tmprs=rs;tmprs;tmprs=tmprs->sibling){
101 if(tmprs->protocol==fi->hfinfo->id){
106 /* not found, then we must add it to the end of the list */
108 for(tmprs=rs;tmprs->sibling;tmprs=tmprs->sibling)
110 tmprs->sibling=new_phs_t(rs->parent);
112 rs->protocol=fi->hfinfo->id;
113 rs->proto_name=fi->hfinfo->abbrev;
119 rs->bytes+=pinfo->fd->pkt_len;
122 rs->child=new_phs_t(rs);
130 phs_draw(phs_t *rs, int indentation)
133 #define MAXPHSLINE 80
134 char str[MAXPHSLINE];
135 for(;rs;rs=rs->sibling){
136 if(rs->protocol==-1){
141 for(i=0;i<indentation;i++){
143 stroff+=g_snprintf(str+stroff, MAXPHSLINE-stroff, "...");
146 stroff+=g_snprintf(str+stroff, MAXPHSLINE-stroff, " ");
148 g_snprintf(str+stroff, MAXPHSLINE-stroff, "%s", rs->proto_name);
149 printf("%-40s frames:%d bytes:%" G_GINT64_MODIFIER "d\n",str, rs->frames, rs->bytes);
150 phs_draw(rs->child, indentation+1);
155 protohierstat_draw(void *prs)
157 phs_t *rs=(phs_t *)prs;
160 printf("===================================================================\n");
161 printf("Protocol Hierarchy Statistics\n");
162 printf("Filter: %s\n\n",rs->filter?rs->filter:"");
164 printf("===================================================================\n");
169 protohierstat_init(const char *optarg, void* userdata _U_)
173 const char *filter=NULL;
174 GString *error_string;
176 if(strcmp("io,phs",optarg)==0){
178 } else if(sscanf(optarg,"io,phs,%n",&pos)==0){
183 fprintf(stderr, "tshark: invalid \"-z io,phs[,<filter>]\" argument\n");
190 rs->filter=g_strdup(filter);
195 error_string=register_tap_listener("frame", rs, filter, TL_REQUIRES_PROTO_TREE, NULL, protohierstat_packet, protohierstat_draw);
197 /* error, we failed to attach to the tap. clean up */
201 fprintf(stderr, "tshark: Couldn't register io,phs tap: %s\n",
203 g_string_free(error_string, TRUE);
210 register_tap_listener_protohierstat(void)
212 register_stat_cmd_arg("io,phs", protohierstat_init, NULL);