[Automatic manuf and enterprise-numbers update for 2010-04-04]
[obnox/wireshark/wip.git] / tap-rpcstat.c
index fd54089f2974f47c4b92e1d69c86be9928fd6415..1f43ad74a7860465493d17d5700757e5823530be 100644 (file)
@@ -1,10 +1,10 @@
 /* tap-rpcstat.c
  * rpcstat   2002 Ronnie Sahlberg
  *
- * $Id: tap-rpcstat.c,v 1.2 2002/09/26 01:13:02 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
@@ -22,8 +22,8 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
-/* This module provides rpc call/reply RTT statistics to tethereal.
- * It is only used by tethereal and not ethereal
+/* This module provides rpc call/reply RTT statistics to tshark.
+ * It is only used by tshark and not wireshark
  *
  * It serves as an example on how to use the tap api.
  */
 
 #include <string.h>
 #include "epan/packet_info.h"
-#include "tap.h"
-#include "tap-rpcstat.h"
-#include "packet-rpc.h"
-
+#include <epan/tap.h>
+#include <epan/stat_cmd_args.h>
+#include <epan/dissectors/packet-rpc.h>
+#include "register.h"
 
 /* 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;
@@ -56,7 +56,8 @@ 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;
        guint32 program;
        guint32 version;
        guint32 num_procedures;
@@ -65,19 +66,20 @@ typedef struct _rpcstat_t {
 
 
 
-/* This callback is never used by tethereal but it is here for completeness.
+/* This callback is never used by tshark but it is here for completeness.
  * When registering below, we could just have left this function as NULL.
  *
- * When used by ethereal, this function will be called whenever we would need
- * to reset all state. Such as when ethereal opens a new file, when it
+ * When used by wireshark, this function will be called whenever we would need
+ * to reset all state. Such as when wireshark opens a new file, when it
  * starts a new capture, when it rescans the packetlist after some prefs have
  * changed etc.
  * So if your aplication has some state it needs to clean up in those
  * situations, here is a good place to put that code.
  */
 static void
-rpcstat_reset(rpcstat_t *rs)
+rpcstat_reset(void *prs)
 {
+       rpcstat_t *rs=prs;
        guint32 i;
 
        for(i=0;i<rs->num_procedures;i++){
@@ -100,7 +102,7 @@ rpcstat_reset(rpcstat_t *rs)
  * later.
  *
  * This function should be as lightweight as possible since it executes together
- * with the normal ethereal dissectors. Try to push as much processing as
+ * with the normal wireshark dissectors. Try to push as much processing as
  * possible into (*draw) instead since that function executes asynchronously
  * and does not affect the main threads performance.
  *
@@ -123,8 +125,10 @@ rpcstat_reset(rpcstat_t *rs)
  * !0: state has changed, call (*draw) sometime later
  */
 static int
-rpcstat_packet(rpcstat_t *rs, packet_info *pinfo, rpc_call_info_value *ri)
+rpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
 {
+       rpcstat_t *rs=prs;
+       const rpc_call_info_value *ri=pri;
        nstime_t delta;
        rpc_procedure_t *rp;
 
@@ -144,21 +148,14 @@ rpcstat_packet(rpcstat_t *rs, packet_info *pinfo, rpc_call_info_value *ri)
        rp=&(rs->procedures[ri->proc]);
 
        /* 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) ){
+       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;
        }
@@ -183,39 +180,37 @@ rpcstat_packet(rpcstat_t *rs, packet_info *pinfo, rpc_call_info_value *ri)
                rp->tot.nsecs-=1000000000;
                rp->tot.secs++;
        }
+
        rp->num++;
 
        return 1;
 }
 
-/* This callback is used when tethereal wants us to draw/update our
- * data to the output device. Since this is tethereal only output is
+/* This callback is used when tshark wants us to draw/update our
+ * data to the output device. Since this is tshark only output is
  * stdout.
- * Tethereal will only call this callback once, which is when tethereal has
+ * TShark will only call this callback once, which is when tshark has
  * finished reading all packets and exists.
- * If used with ethereal this may be called any time, perhaps once every 3 
+ * If used with wireshark this may be called any time, perhaps once every 3 
  * seconds or so.
  * This function may even be called in parallell with (*reset) or (*draw)
  * so make sure there are no races. The data in the rpcstat_t can thus change
  * beneath us. Beware.
  */
 static void
-rpcstat_draw(rpcstat_t *rs)
+rpcstat_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 RTT Statistics:\n", rs->prog, rs->version);
+       printf("Filter: %s\n",rs->filter?rs->filter:"");
        printf("Procedure        Calls   Min RTT   Max RTT   Avg RTT\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=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;
@@ -223,7 +218,7 @@ rpcstat_draw(rpcstat_t *rs)
                        td=0;
                }
 
-               printf("%-15s %6d %3d.%05d %3d.%05d %3d.%05d\n",
+               printf("%-15s %6d %3d.%05d %3d.%05d %3" G_GINT64_MODIFIER "u.%05" 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,
@@ -268,29 +263,48 @@ rpcstat_find_procs(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
 /* When called, this function will create a new instance of rpcstat.
  * program and version are whick onc-rpc program/version we want to
  * collect statistics for.
- * This function is called from tethereal when it parses the -Z rpc, arguments
+ * This function is called from tshark when it parses the -z rpc, arguments
  * and it creates a new instance to store statistics in and registers this
  * new instance for the rpc tap.
  */
-void
-rpcstat_init(guint32 program, guint32 version, char *filter)
+static void
+rpcstat_init(const char *optarg, void* userdata _U_)
 {
        rpcstat_t *rs;
        guint32 i;
+       int program, version;
+       int pos=0;
+       const char *filter=NULL;
+       GString *error_string;
+
+       if(sscanf(optarg,"rpc,rtt,%d,%d,%n",&program,&version,&pos)==2){
+               if(pos){
+                       filter=optarg+pos;
+               } else {
+                       filter=NULL;
+               }
+       } else {
+               fprintf(stderr, "tshark: invalid \"-z rpc,rtt,<program>,<version>[,<filter>]\" argument\n");
+               exit(1);
+       }
 
        rs=g_malloc(sizeof(rpcstat_t));
        rs->prog=rpc_prog_name(program);
        rs->program=program;
        rs->version=version;
-
+       if(filter){
+               rs->filter=g_strdup(filter);
+       } else {
+               rs->filter=NULL;
+       }
        rpc_program=program;
        rpc_version=version;
        rpc_min_proc=-1;
        rpc_max_proc=-1;
        g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_procs, NULL);
        if(rpc_min_proc==-1){
-               fprintf(stderr,"tethereal: Invalid -Z rpc,rrt,%d,%d\n",rpc_program,rpc_version);
-               fprintf(stderr,"   Program:%d version:%d is not supported by tethereal.\n", rpc_program, rpc_version);
+               fprintf(stderr,"tshark: Invalid -z rpc,rrt,%d,%d\n",rpc_program,rpc_version);
+               fprintf(stderr,"   Program:%d version:%d isn't supported by tshark.\n", rpc_program, rpc_version);
                exit(1);
        }
 
@@ -318,16 +332,24 @@ rpcstat_init(guint32 program, guint32 version, char *filter)
  *
  */
 
-       if(register_tap_listener("rpc", rs, filter, (void*)rpcstat_reset, (void*)rpcstat_packet, (void*)rpcstat_draw)){
+       error_string=register_tap_listener("rpc", rs, filter, 0, rpcstat_reset, rpcstat_packet, rpcstat_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: rpcstat_init() failed to attach to tap.\n");
+               fprintf(stderr, "tshark: Couldn't register rpc,rtt tap: %s\n",
+                   error_string->str);
+               g_string_free(error_string, TRUE);
                exit(1);
        }
 }
 
 
-
+void
+register_tap_listener_rpcstat(void)
+{
+       register_stat_cmd_arg("rpc,rtt,", rpcstat_init,NULL);
+}