2002-11-01 05:29:36 +00:00
|
|
|
/* tap-protohierstat.c
|
|
|
|
* protohierstat 2002 Ronnie Sahlberg
|
|
|
|
*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
2002-11-01 05:29:36 +00:00
|
|
|
*
|
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
|
|
|
*
|
2002-11-01 05:29:36 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-04-18 04:34:10 +00:00
|
|
|
*
|
2002-11-01 05:29:36 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2007-04-18 04:34:10 +00:00
|
|
|
*
|
2002-11-01 05:29:36 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2006-05-31 17:38:42 +00:00
|
|
|
/* This module provides ProtocolHierarchyStatistics for tshark */
|
2002-11-01 05:29:36 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "epan/packet_info.h"
|
|
|
|
#include "epan/epan_dissect.h"
|
|
|
|
#include "epan/proto.h"
|
2004-09-29 00:06:36 +00:00
|
|
|
#include <epan/tap.h>
|
2005-08-20 20:06:05 +00:00
|
|
|
#include <epan/stat_cmd_args.h>
|
2002-11-01 05:29:36 +00:00
|
|
|
#include "register.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
rs=g_malloc(sizeof(phs_t));
|
|
|
|
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;
|
|
|
|
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
|
|
|
{
|
|
|
|
phs_t *rs=prs;
|
|
|
|
phs_t *tmprs;
|
|
|
|
proto_tree *tree;
|
|
|
|
field_info *fi;
|
|
|
|
|
|
|
|
if(!edt){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!edt->tree){
|
|
|
|
return 0;
|
|
|
|
}
|
2003-12-04 10:59:34 +00:00
|
|
|
if(!edt->tree->first_child){
|
2002-11-01 05:29:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-04 10:59:34 +00:00
|
|
|
for(tree=edt->tree->first_child;tree;tree=tree->next){
|
2002-11-01 05:29:36 +00:00
|
|
|
fi=PITEM_FINFO(tree);
|
|
|
|
|
|
|
|
/* first time we saw a protocol at this leaf */
|
|
|
|
if(rs->protocol==-1){
|
2003-12-03 09:28:26 +00:00
|
|
|
rs->protocol=fi->hfinfo->id;
|
|
|
|
rs->proto_name=fi->hfinfo->abbrev;
|
2002-11-01 05:29:36 +00:00
|
|
|
rs->frames=1;
|
|
|
|
rs->bytes=pinfo->fd->pkt_len;
|
|
|
|
rs->child=new_phs_t(rs);
|
|
|
|
rs=rs->child;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find this protocol in the list of siblings */
|
|
|
|
for(tmprs=rs;tmprs;tmprs=tmprs->sibling){
|
2003-12-03 09:28:26 +00:00
|
|
|
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 */
|
|
|
|
if(!tmprs){
|
|
|
|
for(tmprs=rs;tmprs->sibling;tmprs=tmprs->sibling)
|
|
|
|
;
|
|
|
|
tmprs->sibling=new_phs_t(rs->parent);
|
|
|
|
rs=tmprs->sibling;
|
2003-12-03 09:28:26 +00:00
|
|
|
rs->protocol=fi->hfinfo->id;
|
|
|
|
rs->proto_name=fi->hfinfo->abbrev;
|
2002-11-01 05:29:36 +00:00
|
|
|
} else {
|
|
|
|
rs=tmprs;
|
|
|
|
}
|
|
|
|
|
|
|
|
rs->frames++;
|
|
|
|
rs->bytes+=pinfo->fd->pkt_len;
|
|
|
|
|
|
|
|
if(!rs->child){
|
|
|
|
rs->child=new_phs_t(rs);
|
|
|
|
}
|
|
|
|
rs=rs->child;
|
|
|
|
}
|
|
|
|
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];
|
2002-11-01 05:29:36 +00:00
|
|
|
for(;rs;rs=rs->sibling){
|
|
|
|
if(rs->protocol==-1){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
str[0]=0;
|
2005-10-12 00:34:51 +00:00
|
|
|
stroff=0;
|
2002-11-01 05:29:36 +00:00
|
|
|
for(i=0;i<indentation;i++){
|
2005-10-12 00:34:51 +00:00
|
|
|
if(i>15){
|
|
|
|
stroff+=g_snprintf(str+stroff, MAXPHSLINE-stroff, "...");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stroff+=g_snprintf(str+stroff, MAXPHSLINE-stroff, " ");
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
2008-10-31 10:30:17 +00:00
|
|
|
stroff+=g_snprintf(str+stroff, MAXPHSLINE-stroff, "%s", rs->proto_name);
|
2007-12-06 08:32:38 +00:00
|
|
|
printf("%-40s frames:%d bytes:%" G_GINT64_MODIFIER "d\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)
|
|
|
|
{
|
|
|
|
phs_t *rs=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
|
2006-02-11 13:05:24 +00:00
|
|
|
protohierstat_init(const char *optarg, void* userdata _U_)
|
2002-11-01 05:29:36 +00:00
|
|
|
{
|
|
|
|
phs_t *rs;
|
|
|
|
int pos=0;
|
2005-08-06 03:44:55 +00:00
|
|
|
const char *filter=NULL;
|
2003-04-23 08:20:06 +00:00
|
|
|
GString *error_string;
|
2002-11-01 05:29:36 +00:00
|
|
|
|
|
|
|
if(!strcmp("io,phs",optarg)){
|
|
|
|
filter="frame";
|
|
|
|
} else if(sscanf(optarg,"io,phs,%n",&pos)==0){
|
|
|
|
if(pos){
|
|
|
|
filter=optarg+pos;
|
|
|
|
} else {
|
|
|
|
/* We must use a filter to guarantee that edt->tree
|
|
|
|
will be populated. "frame" matches everything so
|
|
|
|
that one is used instead of no filter.
|
|
|
|
*/
|
2007-04-18 04:34:10 +00:00
|
|
|
filter="frame";
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-05-31 17:38:42 +00:00
|
|
|
fprintf(stderr, "tshark: invalid \"-z io,phs[,<filter>]\" argument\n");
|
2002-11-01 05:29:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rs=new_phs_t(NULL);
|
|
|
|
|
|
|
|
if(filter){
|
2008-02-03 15:38:20 +00:00
|
|
|
rs->filter=g_strdup(filter);
|
2002-11-01 05:29:36 +00:00
|
|
|
} else {
|
|
|
|
rs->filter=NULL;
|
|
|
|
}
|
|
|
|
|
2003-04-23 08:20:06 +00:00
|
|
|
error_string=register_tap_listener("frame", rs, filter, NULL, protohierstat_packet, protohierstat_draw);
|
|
|
|
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);
|
|
|
|
|
2006-05-31 17:38:42 +00:00
|
|
|
fprintf(stderr, "tshark: Couldn't register io,phs tap: %s\n",
|
2003-04-23 08:20:06 +00:00
|
|
|
error_string->str);
|
|
|
|
g_string_free(error_string, TRUE);
|
2002-11-01 05:29:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
register_tap_listener_protohierstat(void)
|
|
|
|
{
|
2006-02-11 13:05:24 +00:00
|
|
|
register_stat_cmd_arg("io,phs", protohierstat_init, NULL);
|
2002-11-01 05:29:36 +00:00
|
|
|
}
|
|
|
|
|