2 * protohierstat 2002 Ronnie Sahlberg
4 * $Id: tap-protohierstat.c,v 1.3 2003/04/23 08:20:02 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 /* This module provides ProtocolHierarchyStatistics for tethereal */
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
38 #include "epan/packet_info.h"
39 #include "epan/epan_dissect.h"
40 #include "epan/proto.h"
44 typedef struct _phs_t {
45 struct _phs_t *sibling;
47 struct _phs_t *parent;
57 new_phs_t(phs_t *parent)
60 rs=g_malloc(sizeof(phs_t));
74 protohierstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt, void *dummy _U_)
87 if(!edt->tree->children){
91 for(tree=edt->tree->children;tree;tree=tree->next){
94 /* first time we saw a protocol at this leaf */
96 rs->protocol=fi->hfinfo->id;
97 rs->proto_name=fi->hfinfo->abbrev;
99 rs->bytes=pinfo->fd->pkt_len;
100 rs->child=new_phs_t(rs);
105 /* find this protocol in the list of siblings */
106 for(tmprs=rs;tmprs;tmprs=tmprs->sibling){
107 if(tmprs->protocol==fi->hfinfo->id){
112 /* not found, then we must add it to the end of the list */
114 for(tmprs=rs;tmprs->sibling;tmprs=tmprs->sibling)
116 tmprs->sibling=new_phs_t(rs->parent);
118 rs->protocol=fi->hfinfo->id;
119 rs->proto_name=fi->hfinfo->abbrev;
125 rs->bytes+=pinfo->fd->pkt_len;
128 rs->child=new_phs_t(rs);
136 phs_draw(phs_t *rs, int indentation)
140 for(;rs;rs=rs->sibling){
141 if(rs->protocol==-1){
145 for(i=0;i<indentation;i++){
148 strcat(str, rs->proto_name);
149 printf("%-40s frames:%d bytes:%d\n",str, rs->frames, rs->bytes);
150 phs_draw(rs->child, indentation+1);
155 protohierstat_draw(void *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(char *optarg)
174 GString *error_string;
176 if(!strcmp("io,phs",optarg)){
178 } else if(sscanf(optarg,"io,phs,%n",&pos)==0){
182 /* We must use a filter to guarantee that edt->tree
183 will be populated. "frame" matches everything so
184 that one is used instead of no filter.
189 fprintf(stderr, "tethereal: invalid \"-z io,phs[,<filter>]\" argument\n");
196 rs->filter=g_malloc(strlen(filter)+1);
197 strcpy(rs->filter, filter);
202 error_string=register_tap_listener("frame", rs, filter, NULL, protohierstat_packet, protohierstat_draw);
204 /* error, we failed to attach to the tap. clean up */
208 fprintf(stderr, "tethereal: Couldn't register io,phs tap: %s\n",
210 g_string_free(error_string, TRUE);
217 register_tap_listener_protohierstat(void)
219 register_ethereal_tap("io,phs", protohierstat_init);