From Harald Welte:
[obnox/wireshark/wip.git] / tap-dcerpcstat.c
index 8433125ff09c8aacb89191156c75811717814376..5fea8fc085f131fe5e4b3ce33e4689d1785b6c09 100644 (file)
@@ -1,22 +1,22 @@
 /* tap-dcerpcstat.c
  * dcerpcstat   2002 Ronnie Sahlberg
  *
- * $Id: tap-dcerpcstat.c,v 1.3 2002/10/31 22:16:01 guy Exp $
+ * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
- * 
+ *
  * 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.
- * 
+ *
  * 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.
- * 
+ *
  * 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.
 
 #include <string.h>
 #include "epan/packet_info.h"
-#include "tap.h"
-#include "packet-dcerpc.h"
-#include "register.h"
+#include <epan/tap.h>
+#include <epan/stat_cmd_args.h>
+#include <epan/dissectors/packet-dcerpc.h>
+
+#define MICROSECS_PER_SEC   1000000
+#define NANOSECS_PER_SEC    1000000000
 
 /* used to keep track of statistics for a specific procedure */
 typedef struct _rpc_procedure_t {
-       char *proc;
+       const char *proc;
        int num;
        nstime_t min;
        nstime_t max;
@@ -49,7 +52,7 @@ typedef struct _rpc_procedure_t {
 
 /* used to keep track of the statistics for an entire program interface */
 typedef struct _rpcstat_t {
-       char *prog;
+       const char *prog;
        char *filter;
        e_uuid_t uuid;
        guint16 ver;
@@ -60,9 +63,9 @@ typedef struct _rpcstat_t {
 
 
 static int
-dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *pri)
+dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
 {
-       dcerpc_info *ri=pri;
+       const dcerpc_info *ri=pri;
        rpcstat_t *rs=prs;
        nstime_t delta;
        rpc_procedure_t *rp;
@@ -80,7 +83,7 @@ dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *
        }
 
        /* we are only interested in reply packets */
-       if(ri->request){
+       if(ri->ptype != PDU_RESP){
                return 0;
        }
 
@@ -103,21 +106,14 @@ dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *
        rp=&(rs->procedures[ri->call_data->opnum]);
 
        /* calculate time delta between request and reply */
-       delta.secs=pinfo->fd->abs_secs-ri->call_data->req_time.secs;
-       delta.nsecs=pinfo->fd->abs_usecs*1000-ri->call_data->req_time.nsecs;
-       if(delta.nsecs<0){
-               delta.nsecs+=1000000000;
-               delta.secs--;
-       }
+       nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->call_data->req_time);
 
-       if((rp->max.secs==0)
-       && (rp->max.nsecs==0) ){
+       if(rp->num==0){
                rp->max.secs=delta.secs;
                rp->max.nsecs=delta.nsecs;
        }
 
-       if((rp->min.secs==0)
-       && (rp->min.nsecs==0) ){
+       if(rp->num==0){
                rp->min.secs=delta.secs;
                rp->min.nsecs=delta.nsecs;
        }
@@ -135,13 +131,14 @@ dcerpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, void *
                rp->max.secs=delta.secs;
                rp->max.nsecs=delta.nsecs;
        }
-       
+
        rp->tot.secs += delta.secs;
        rp->tot.nsecs += delta.nsecs;
-       if(rp->tot.nsecs>1000000000){
-               rp->tot.nsecs-=1000000000;
+       if(rp->tot.nsecs > NANOSECS_PER_SEC){
+               rp->tot.nsecs -= NANOSECS_PER_SEC;
                rp->tot.secs++;
        }
+
        rp->num++;
 
        return 1;
@@ -152,53 +149,65 @@ dcerpcstat_draw(void *prs)
 {
        rpcstat_t *rs=prs;
        guint32 i;
-#ifdef G_HAVE_UINT64
        guint64 td;
-#else
-       guint32 td;
-#endif
        printf("\n");
-       printf("===================================================================\n");
-       printf("%s Version %d.%d RTT Statistics:\n", rs->prog, rs->ver&0xff,rs->ver>>8);
+       printf("=======================================================================\n");
+       printf("%s Major Version %u SRT Statistics:\n", rs->prog, rs->ver);
        printf("Filter: %s\n",rs->filter?rs->filter:"");
-       printf("Procedure                  Calls   Min RTT   Max RTT   Avg RTT\n");
+       printf("Procedure                        Calls    Min SRT    Max SRT    Avg SRT\n");
+       
        for(i=0;i<rs->num_procedures;i++){
-               /* scale it to units of 10us.*/
-               /* for long captures with a large tot time, this can overflow on 32bit */
-               td=(int)rs->procedures[i].tot.secs;
-               td=td*100000+(int)rs->procedures[i].tot.nsecs/10000;
-               if(rs->procedures[i].num){
-                       td/=rs->procedures[i].num;
-               } else {
-                       td=0;
+               /* Only display procs with non-zero calls */
+               if(rs->procedures[i].num==0){
+                       continue;
                }
+               /* Scale the average SRT in units of 1us and round to the nearest us. */
+               td = ((guint64)(rs->procedures[i].tot.secs)) * NANOSECS_PER_SEC + rs->procedures[i].tot.nsecs;
+               td = ((td / rs->procedures[i].num) + 500) / 1000;
 
-               printf("%-25s %6d %3d.%05d %3d.%05d %3d.%05d\n",
+               printf("%-31s %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
                        rs->procedures[i].proc,
                        rs->procedures[i].num,
-                       (int)rs->procedures[i].min.secs,rs->procedures[i].min.nsecs/10000,
-                       (int)rs->procedures[i].max.secs,rs->procedures[i].max.nsecs/10000,
-                       td/100000, td%100000
+                       (int)(rs->procedures[i].min.secs),(rs->procedures[i].min.nsecs+500)/1000,
+                       (int)(rs->procedures[i].max.secs),(rs->procedures[i].max.nsecs+500)/1000,
+                       td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
                );
        }
-       printf("===================================================================\n");
+       printf("=======================================================================\n");
 }
 
 
 
 static void
-dcerpcstat_init(char *optarg)
+dcerpcstat_init(const char *optarg, void* userdata _U_)
 {
        rpcstat_t *rs;
        guint32 i, max_procs;
        dcerpc_sub_dissector *procs;
        e_uuid_t uuid;
-       int d1,d2,d3,d40,d41,d42,d43,d44,d45,d46,d47;
+       guint d1,d2,d3,d40,d41,d42,d43,d44,d45,d46,d47;
        int major, minor;
+       guint16 ver;
        int pos=0;
-        char *filter=NULL;
-    
-       if(sscanf(optarg,"dcerpc,rtt,%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x,%d.%d%n", &d1,&d2,&d3,&d40,&d41,&d42,&d43,&d44,&d45,&d46,&d47,&major,&minor,&pos)==13){
+        const char *filter=NULL;
+        GString *error_string;
+
+       /*
+        * XXX - DCE RPC statistics are maintained only by major version,
+        * not by major and minor version, so the minor version number is
+        * ignored.
+        *
+        * Should we just stop supporting minor version numbers here?
+        * Or should we allow it to be omitted?  Or should we keep
+        * separate statistics for different minor version numbers,
+        * and allow the minor version number to be omitted, and
+        * report aggregate statistics for all minor version numbers
+        * if it's omitted?
+        */
+       if(sscanf(optarg,
+               "dcerpc,srt,%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x,%d.%d%n",
+               &d1,&d2,&d3,&d40,&d41,&d42,&d43,&d44,&d45,&d46,&d47,
+               &major,&minor,&pos)==13){
                uuid.Data1=d1;
                uuid.Data2=d2;
                uuid.Data3=d3;
@@ -216,25 +225,32 @@ dcerpcstat_init(char *optarg)
                        filter=NULL;
                }
        } else {
-               fprintf(stderr, "tethereal: invalid \"-z dcerpc,rtt,<uuid>,<major version>.<minor version>[,<filter>]\" argument\n");
+               fprintf(stderr, "tshark: invalid \"-z dcerpc,srt,<uuid>,<major version>.<minor version>[,<filter>]\" argument\n");
                exit(1);
        }
-
+       if (major < 0 || major > 65535) {
+               fprintf(stderr,"tshark: dcerpcstat_init() Major version number %d is invalid - must be positive and <= 65535\n", major);
+               exit(1);
+       }
+       if (minor < 0 || minor > 65535) {
+               fprintf(stderr,"tshark: dcerpcstat_init() Minor version number %d is invalid - must be positive and <= 65535\n", minor);
+               exit(1);
+       }
+       ver = major;
 
        rs=g_malloc(sizeof(rpcstat_t));
-       rs->prog=dcerpc_get_proto_name(&uuid, (minor<<8)|(major&0xff) );
+       rs->prog=dcerpc_get_proto_name(&uuid, ver);
        if(!rs->prog){
                g_free(rs);
-               fprintf(stderr,"tethereal: dcerpcstat_init() Protocol with uuid:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x v%d.%d not supported\n",uuid.Data1,uuid.Data2,uuid.Data3,uuid.Data4[0],uuid.Data4[1],uuid.Data4[2],uuid.Data4[3],uuid.Data4[4],uuid.Data4[5],uuid.Data4[6],uuid.Data4[7],major,minor);
+               fprintf(stderr,"tshark: dcerpcstat_init() Protocol with uuid:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x v%u not supported\n",uuid.Data1,uuid.Data2,uuid.Data3,uuid.Data4[0],uuid.Data4[1],uuid.Data4[2],uuid.Data4[3],uuid.Data4[4],uuid.Data4[5],uuid.Data4[6],uuid.Data4[7],ver);
                exit(1);
        }
-       procs=dcerpc_get_proto_sub_dissector(&uuid, (minor<<8)|(major&0xff) );
+       procs=dcerpc_get_proto_sub_dissector(&uuid, ver);
        rs->uuid=uuid;
-       rs->ver=(minor<<8)|(major&0xff);
+       rs->ver=ver;
 
        if(filter){
-               rs->filter=g_malloc(strlen(filter)+1);
-               strcpy(rs->filter, filter);
+               rs->filter=g_strdup(filter);
        } else {
                rs->filter=NULL;
        }
@@ -254,7 +270,7 @@ dcerpcstat_init(char *optarg)
                                rs->procedures[i].proc=procs[j].name;
                        }
                }
-               rs->procedures[i].num=0;        
+               rs->procedures[i].num=0;
                rs->procedures[i].min.secs=0;
                rs->procedures[i].min.nsecs=0;
                rs->procedures[i].max.secs=0;
@@ -263,13 +279,16 @@ dcerpcstat_init(char *optarg)
                rs->procedures[i].tot.nsecs=0;
        }
 
-       if(register_tap_listener("dcerpc", rs, filter, NULL, dcerpcstat_packet, dcerpcstat_draw)){
+       error_string=register_tap_listener("dcerpc", rs, filter, 0, NULL, dcerpcstat_packet, dcerpcstat_draw);
+       if(error_string){
                /* error, we failed to attach to the tap. clean up */
                g_free(rs->procedures);
                g_free(rs->filter);
                g_free(rs);
 
-               fprintf(stderr,"tethereal: dcerpcstat_init() failed to attach to tap.\n");
+               fprintf(stderr, "tshark: Couldn't register dcerpc,srt tap: %s\n",
+                   error_string->str);
+               g_string_free(error_string, TRUE);
                exit(1);
        }
 }
@@ -277,5 +296,5 @@ dcerpcstat_init(char *optarg)
 void
 register_tap_listener_dcerpcstat(void)
 {
-       register_ethereal_tap("dcerpc,rtt,", dcerpcstat_init, NULL, NULL);
+       register_stat_cmd_arg("dcerpc,srt,", dcerpcstat_init,NULL);
 }