White space changes.
[obnox/wireshark/wip.git] / tap-rpcprogs.c
index 51454e8582337c05c68e77bbdc182ddd789a4c86..accc64c00dcead7dbeaaae68239526a535c9139b 100644 (file)
@@ -1,29 +1,29 @@
 /* tap-rpcprogs.c
  * rpcstat   2002 Ronnie Sahlberg
  *
- * $Id: tap-rpcprogs.c,v 1.6 2004/05/17 20:06:32 sahlberg 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.
  */
 
-/* This module provides rpc call/reply SRT statistics to tethereal.
- * It is only used by tethereal and not ethereal
+/* This module provides rpc call/reply SRT statistics to tshark.
+ * It is only used by tshark and not wireshark
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include <string.h>
 #include "epan/packet_info.h"
-#include "tap.h"
-#include "packet-rpc.h"
-#include "register.h"
+#include <epan/tap.h>
+#include <epan/stat_cmd_args.h>
+#include <epan/dissectors/packet-rpc.h>
+
+#define MICROSECS_PER_SEC   1000000
+#define NANOSECS_PER_SEC    1000000000
 
 /* used to keep track of statistics for a specific program/version */
 typedef struct _rpc_program_t {
@@ -57,9 +60,9 @@ static rpc_program_t *prog_list=NULL;
 static int already_enabled=0;
 
 static int
-rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, void *pri)
+rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
 {
-       rpc_call_info_value *ri=pri;
+       const rpc_call_info_value *ri=pri;
        nstime_t delta;
        rpc_program_t *rp=NULL;
 
@@ -127,19 +130,14 @@ rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, v
                }
        }
 
-       
+
        /* we are only interested in reply packets */
-       if(ri->request){
+       if(ri->request || !rp){
                return 0;
        }
 
        /* calculate time delta between request and reply */
-       delta.secs=pinfo->fd->abs_secs-ri->req_time.secs;
-       delta.nsecs=pinfo->fd->abs_usecs*1000-ri->req_time.nsecs;
-       if(delta.nsecs<0){
-               delta.nsecs+=1000000000;
-               delta.secs--;
-       }
+       nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
 
        if((rp->max.secs==0)
        && (rp->max.nsecs==0) ){
@@ -166,11 +164,11 @@ rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, v
                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++;
@@ -182,37 +180,31 @@ rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, v
 static void
 rpcprogs_draw(void *dummy _U_)
 {
-#ifdef G_HAVE_UINT64
        guint64 td;
-#else
-       guint32 td;
-#endif
        rpc_program_t *rp;
        char str[64];
 
        printf("\n");
-       printf("===================================================================\n");
+       printf("==========================================================\n");
        printf("ONC-RPC Program Statistics:\n");
-       printf("Program    Version  Calls   Min SRT   Max SRT   Avg SRT\n");
+       printf("Program    Version  Calls    Min SRT    Max SRT    Avg SRT\n");
        for(rp=prog_list;rp;rp=rp->next){
-               /* scale it to units of 10us.*/
-               /* for long captures with a large tot time, this can overflow on 32bit */
-               td=(int)rp->tot.secs;
-               td=td*100000+(int)rp->tot.nsecs/10000;
-               if(rp->num){
-                       td/=rp->num;
-               } else {
-                       td=0;
+               /* Only display procs with non-zero calls */
+               if(rp->num==0){
+                       continue;
                }
+               /* Scale the average SRT in units of 1us and round to the nearest us. */
+               td = ((guint64)(rp->tot.secs)) * NANOSECS_PER_SEC + rp->tot.nsecs;
+               td = ((td / rp->num) + 500) / 1000;
 
-               sprintf(str,"%s(%d)",rpc_prog_name(rp->program),rp->program);
-               printf("%-15s %2d %6d %3d.%05d %3d.%05d %3d.%05d\n",
+               g_snprintf(str, sizeof(str), "%s(%d)",rpc_prog_name(rp->program),rp->program);
+               printf("%-15s %2d %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
                        str,
                        rp->version,
                        rp->num,
-                       (int)rp->min.secs,rp->min.nsecs/10000,
-                       (int)rp->max.secs,rp->max.nsecs/10000,
-                       td/100000, td%100000
+                       (int)(rp->min.secs),(rp->min.nsecs+500)/1000,
+                       (int)(rp->max.secs),(rp->max.nsecs+500)/1000,
+                       td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
                );
        }
        printf("===================================================================\n");
@@ -220,7 +212,7 @@ rpcprogs_draw(void *dummy _U_)
 
 
 static void
-rpcprogs_init(char *optarg _U_)
+rpcprogs_init(const char *optarg _U_, void* userdata _U_)
 {
        GString *error_string;
 
@@ -229,9 +221,9 @@ rpcprogs_init(char *optarg _U_)
        }
        already_enabled=1;
 
-       error_string=register_tap_listener("rpc", NULL, NULL, NULL, rpcprogs_packet, rpcprogs_draw);
+       error_string=register_tap_listener("rpc", NULL, NULL, 0, NULL, rpcprogs_packet, rpcprogs_draw);
        if(error_string){
-               fprintf(stderr,"tethereal: Couldn't register rpc,programs tap: %s\n",
+               fprintf(stderr,"tshark: Couldn't register rpc,programs tap: %s\n",
                    error_string->str);
                g_string_free(error_string, TRUE);
                exit(1);
@@ -242,7 +234,7 @@ rpcprogs_init(char *optarg _U_)
 void
 register_tap_listener_rpcprogs(void)
 {
-       register_ethereal_tap("rpc,programs", rpcprogs_init);
+       register_stat_cmd_arg("rpc,programs", rpcprogs_init, NULL);
 }