From Clemens Auer:
[obnox/wireshark/wip.git] / tap-iostat.c
index 758455f006d83180837ee11b23c6511270a0a0b4..e5b354d5b10a77b15a1968b8f9b6477f576001da 100644 (file)
@@ -6,17 +6,17 @@
  * 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.
@@ -37,7 +37,7 @@
 #include "epan/packet_info.h"
 #include <epan/tap.h>
 #include <epan/stat_cmd_args.h>
-#include "register.h"
+#include <epan/strutil.h>
 
 
 typedef struct _io_stat_t {
@@ -45,7 +45,7 @@ typedef struct _io_stat_t {
        guint32 num_items;
        struct _io_stat_item_t *items;
        const char **filters;
-} io_stat_t;   
+} io_stat_t;
 
 #define CALC_TYPE_BYTES        0
 #define CALC_TYPE_COUNT        1
@@ -61,14 +61,14 @@ typedef struct _io_stat_item_t {
        gint32 time;            /* unit is ms since start of capture */
        int calc_type;
        int hf_index;
-       guint32 frames;
-       guint32 num;
-       guint32 counter;
+       guint64 frames;
+       guint64 num;
+       guint64 counter;
 } io_stat_item_t;
 
 
 static int
-iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *dummy _U_)
+iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
 {
        io_stat_item_t *mit = arg;
        io_stat_item_t *it;
@@ -76,7 +76,7 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
        GPtrArray *gp;
        guint i;
 
-       current_time=((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
+       current_time=(gint32) ((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
 
        /* the prev item before the main one is always the last interval we saw packets for */
        it=mit->prev;
@@ -127,12 +127,18 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                case FT_UINT32:
                                        it->counter+=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
                                        break;
+                               case FT_UINT64:
+                                       it->counter+=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       break;
                                case FT_INT8:
                                case FT_INT16:
                                case FT_INT24:
                                case FT_INT32:
                                        it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
                                        break;
+                               case FT_INT64:
+                                       it->counter+=(gint64)fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       break;
                                }
                        }
                }
@@ -141,7 +147,7 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
                if(gp){
                        int type;
-                       guint32 val;
+                       guint64 val;
                        nstime_t *new_time;
 
                        type=proto_registrar_get_ftype(it->hf_index);
@@ -156,7 +162,15 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                                it->counter=val;
                                        } else if(val<it->counter){
                                                it->counter=val;
-                                       }                               
+                                       }
+                                       break;
+                               case FT_UINT64:
+                                       val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       if((it->frames==1)&&(i==0)){
+                                               it->counter=val;
+                                       } else if(val<it->counter){
+                                               it->counter=val;
+                                       }
                                        break;
                                case FT_INT8:
                                case FT_INT16:
@@ -169,14 +183,22 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                                it->counter=val;
                                        }
                                        break;
+                               case FT_INT64:
+                                       val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       if((it->frames==1)&&(i==0)){
+                                               it->counter=val;
+                                       } else if((gint64)val<(gint64)(it->counter)){
+                                               it->counter=val;
+                                       }
+                                       break;
                                case FT_RELATIVE_TIME:
                                        new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
-                                       val=new_time->secs*1000+new_time->nsecs/1000000;
+                                       val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val<it->counter){
                                                it->counter=val;
-                                       }                               
+                                       }
                                        break;
                                }
                        }
@@ -186,7 +208,7 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
                if(gp){
                        int type;
-                       guint32 val;
+                       guint64 val;
                        nstime_t *new_time;
 
                        type=proto_registrar_get_ftype(it->hf_index);
@@ -201,7 +223,15 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                                it->counter=val;
                                        } else if(val>it->counter){
                                                it->counter=val;
-                                       }                               
+                                       }
+                                       break;
+                               case FT_UINT64:
+                                       val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       if((it->frames==1)&&(i==0)){
+                                               it->counter=val;
+                                       } else if(val>it->counter){
+                                               it->counter=val;
+                                       }
                                        break;
                                case FT_INT8:
                                case FT_INT16:
@@ -212,16 +242,24 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                                it->counter=val;
                                        } else if((gint32)val>(gint32)(it->counter)){
                                                it->counter=val;
-                                       }                               
+                                       }
+                                       break;
+                               case FT_INT64:
+                                       val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       if((it->frames==1)&&(i==0)){
+                                               it->counter=val;
+                                       } else if((gint64)val>(gint64)(it->counter)){
+                                               it->counter=val;
+                                       }
                                        break;
                                case FT_RELATIVE_TIME:
                                        new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
-                                       val=new_time->secs*1000+new_time->nsecs/1000000;
+                                       val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val>it->counter){
                                                it->counter=val;
-                                       }                               
+                                       }
                                        break;
                                }
                        }
@@ -231,7 +269,7 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
                if(gp){
                        int type;
-                       guint32 val;
+                       guint64 val;
                        nstime_t *new_time;
 
                        type=proto_registrar_get_ftype(it->hf_index);
@@ -245,6 +283,11 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                        val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
                                        it->counter+=val;
                                        break;
+                               case FT_UINT64:
+                               case FT_INT64:
+                                       val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
+                                       it->counter+=val;
+                                       break;
                                case FT_INT8:
                                case FT_INT16:
                                case FT_INT24:
@@ -254,7 +297,7 @@ iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void
                                        break;
                                case FT_RELATIVE_TIME:
                                        new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
-                                       val=new_time->secs*1000+new_time->nsecs/1000000;
+                                       val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
                                        it->counter+=val;
                                        break;
                                }
@@ -272,10 +315,11 @@ iostat_draw(void *arg)
        io_stat_item_t *mit = arg;
        io_stat_t *iot;
        io_stat_item_t **items;
-       guint32 *frames;
-       guint32 *counters;
-       guint32 *num;
-       guint32 i,more_items;
+       guint64 *frames;
+       guint64 *counters;
+       guint64 *num;
+       guint32 i;
+       gboolean more_items;
        gint t;
 
        iot=mit->parent;
@@ -283,13 +327,14 @@ iostat_draw(void *arg)
        printf("\n");
        printf("===================================================================\n");
        printf("IO Statistics\n");
-       printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
+       if(iot->interval!=G_MAXINT32)
+               printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
        for(i=0;i<iot->num_items;i++){
-               printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
+               printf("Column #%u: %s\n",i,iot->filters[i]?iot->filters[i]:"");
        }
        printf("                ");
        for(i=0;i<iot->num_items;i++){
-               printf("|   Column #%-2d   ",i);
+               printf("|   Column #%-2u   ",i);
        }
        printf("\n");
        printf("Time            ");
@@ -318,9 +363,9 @@ iostat_draw(void *arg)
        printf("\n");
 
        items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
-       frames=g_malloc(sizeof(guint32)*iot->num_items);
-       counters=g_malloc(sizeof(guint32)*iot->num_items);
-       num=g_malloc(sizeof(guint32)*iot->num_items);
+       frames=g_malloc(sizeof(guint64)*iot->num_items);
+       counters=g_malloc(sizeof(guint64)*iot->num_items);
+       num=g_malloc(sizeof(guint64)*iot->num_items);
        /* preset all items at the first interval */
        for(i=0;i<iot->num_items;i++){
                items[i]=&iot->items[i];
@@ -329,7 +374,7 @@ iostat_draw(void *arg)
        /* loop the items until we run out of them all */
        t=0;
        do {
-               more_items=0;
+               more_items=FALSE;
                for(i=0;i<iot->num_items;i++){
                        frames[i]=0;
                        counters[i]=0;
@@ -347,24 +392,29 @@ iostat_draw(void *arg)
                        }
 
                        if(items[i]){
-                               more_items=1;
+                               more_items=TRUE;
                        }
                }
 
                if(more_items){
-                       printf("%03d.%03d-%03d.%03d  ",
-                               t/1000,t%1000,
-                               (t+iot->interval)/1000,(t+iot->interval)%1000);
+                       if(iot->interval==G_MAXINT32) {
+                               printf("000.000-         ");
+                       } else {
+                               printf("%03d.%03d-%03d.%03d  ",
+                                       t/1000,t%1000,
+                                       (t+iot->interval)/1000,
+                                       (t+iot->interval)%1000);
+                       }
                        for(i=0;i<iot->num_items;i++){
                                switch(iot->items[i].calc_type){
                                case CALC_TYPE_BYTES:
-                                       printf("%6d %9d ",frames[i],counters[i]);
+                                       printf("%6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u ",frames[i], counters[i]);
                                        break;
                                case CALC_TYPE_COUNT:
-                                       printf("        %8d ", counters[i]);
+                                       printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
                                        break;
                                case CALC_TYPE_SUM:
-                                       printf("        %8d ", counters[i]);
+                                       printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
                                        break;
                                case CALC_TYPE_MIN:
                                        switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
@@ -372,16 +422,18 @@ iostat_draw(void *arg)
                                        case FT_UINT16:
                                        case FT_UINT24:
                                        case FT_UINT32:
-                                               printf("        %8u ", counters[i]);
+                                       case FT_UINT64:
+                                               printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
                                                break;
                                        case FT_INT8:
                                        case FT_INT16:
                                        case FT_INT24:
                                        case FT_INT32:
-                                               printf("        %8d ", counters[i]);
+                                       case FT_INT64:
+                                               printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
                                                break;
                                        case FT_RELATIVE_TIME:
-                                               printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
+                                               printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
                                                break;
                                        }
                                        break;
@@ -391,16 +443,18 @@ iostat_draw(void *arg)
                                        case FT_UINT16:
                                        case FT_UINT24:
                                        case FT_UINT32:
-                                               printf("        %8u ", counters[i]);
+                                       case FT_UINT64:
+                                               printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
                                                break;
                                        case FT_INT8:
                                        case FT_INT16:
                                        case FT_INT24:
                                        case FT_INT32:
-                                               printf("        %8d ", counters[i]);
+                                       case FT_INT64:
+                                               printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
                                                break;
                                        case FT_RELATIVE_TIME:
-                                               printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
+                                               printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
                                                break;
                                        }
                                        break;
@@ -413,17 +467,19 @@ iostat_draw(void *arg)
                                        case FT_UINT16:
                                        case FT_UINT24:
                                        case FT_UINT32:
-                                               printf("        %8u ", counters[i]/num[i]);
+                                       case FT_UINT64:
+                                               printf(" %15" G_GINT64_MODIFIER "u ", counters[i]/num[i]);
                                                break;
                                        case FT_INT8:
                                        case FT_INT16:
                                        case FT_INT24:
                                        case FT_INT32:
-                                               printf("        %8d ", counters[i]/num[i]);
+                                       case FT_INT64:
+                                               printf(" %15" G_GINT64_MODIFIER "d ", counters[i]/num[i]);
                                                break;
                                        case FT_RELATIVE_TIME:
                                                counters[i]/=num[i];
-                                               printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
+                                               printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
                                                break;
                                        }
                                        break;
@@ -481,14 +537,12 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
        io->filters[i]=filter;
        flt=filter;
 
-       if(!filter){
-               filter="";
-       }
        field=NULL;
        hfi=NULL;
        for(j=0; calc_type_table[j].func_name; j++){
                namelen=strlen(calc_type_table[j].func_name);
-               if(strncmp(filter, calc_type_table[j].func_name, namelen) == 0
+               if(filter
+                   && strncmp(filter, calc_type_table[j].func_name, namelen) == 0
                    && *(filter+namelen)=='('){
                        io->items[i].calc_type=calc_type_table[j].calc_type;
 
@@ -504,7 +558,7 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
                                    calc_type_table[j].func_name);
                                exit(10);
                        }
-                       field=malloc(parenp-p+1);
+                       field=g_malloc(parenp-p+1);
                        if(!field){
                                fprintf(stderr, "tshark: Out of memory.\n");
                                exit(10);
@@ -517,25 +571,27 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
                        if(!hfi){
                                fprintf(stderr, "tshark: There is no field named '%s'.\n",
                                    field);
-                               free(field);
+                               g_free(field);
                                exit(10);
                        }
-       
+
                        io->items[i].hf_index=hfi->id;
                        break;
                }
        }
-       if(io->items[i].calc_type!=CALC_TYPE_BYTES){
+       if(hfi && io->items[i].calc_type!=CALC_TYPE_BYTES){
                /* check that the type is compatible */
                switch(hfi->type){
                case FT_UINT8:
                case FT_UINT16:
                case FT_UINT24:
                case FT_UINT32:
+               case FT_UINT64:
                case FT_INT8:
                case FT_INT16:
                case FT_INT24:
                case FT_INT32:
+               case FT_INT64:
                        /* these types support all calculations */
                        break;
                case FT_RELATIVE_TIME:
@@ -555,20 +611,6 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
                                exit(10);
                        }
                        break;
-               case FT_UINT64:
-               case FT_INT64:
-                       /*
-                        * XXX - support this if gint64/guint64 are
-                        * available?
-                        */
-                       if(io->items[i].calc_type!=CALC_TYPE_COUNT){
-                               fprintf(stderr,
-                                   "tshark: %s is a 64-bit integer, so %s(*) calculations are not supported on it.",
-                                   field,
-                                   calc_type_table[j].func_name);
-                               exit(10);
-                       }
-                       break;
                default:
                        /*
                         * XXX - support all operations on floating-point
@@ -583,7 +625,7 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
                        }
                        break;
                }
-               free(field);
+               g_free(field);
        }
 
 /*
@@ -593,7 +635,7 @@ CALC_TYPE_MAX       4
 CALC_TYPE_AVG  5
 */
 
-       error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
+       error_string=register_tap_listener("frame", &io->items[i], flt, TL_REQUIRES_PROTO_TREE, NULL, iostat_packet, i?NULL:iostat_draw);
        if(error_string){
                g_free(io->items);
                g_free(io);
@@ -604,18 +646,21 @@ CALC_TYPE_AVG     5
        }
 }
 
-void
+static void
 iostat_init(const char *optarg, void* userdata _U_)
 {
        float interval_float;
-       gint32 interval; 
-       int pos=0;
+       gint32 interval;
+       int idx=0;
        io_stat_t *io;
        const char *filter=NULL;
 
-       if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
-               if(pos){
-                       filter=optarg+pos;
+       if(sscanf(optarg,"io,stat,%f%n",&interval_float,&idx)==1){
+               if(idx){
+                       if(*(optarg+idx)==',')
+                               filter=optarg+idx+1;
+                       else
+                               filter=optarg+idx;
                } else {
                        filter=NULL;
                }
@@ -624,14 +669,21 @@ iostat_init(const char *optarg, void* userdata _U_)
                exit(1);
        }
 
+       /* if interval is 0, calculate statistics over the whole file
+        * by setting the interval to G_MAXINT32
+        */
+       if(interval_float==0) {
+               interval=G_MAXINT32;
+       } else {
+               /* make interval be number of ms */
+               interval=(gint32)(interval_float*1000.0+0.9);
+       }
 
-       /* make interval be number of ms */
-       interval=(gint32)(interval_float*1000.0+0.9);   
        if(interval<1){
-               fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds.\n");
+               fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds or 0.\n");
                exit(10);
        }
-       
+
        io=g_malloc(sizeof(io_stat_t));
        io->interval=interval;
        if((!filter)||(filter[0]==0)){
@@ -655,7 +707,7 @@ iostat_init(const char *optarg, void* userdata _U_)
                io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
                io->filters=g_malloc(sizeof(char *)*io->num_items);
 
-               /* for each filter, register a tap listener */          
+               /* for each filter, register a tap listener */
                i=0;
                str=filter;
                do{
@@ -663,19 +715,17 @@ iostat_init(const char *optarg, void* userdata _U_)
                        if(pos==str){
                                register_io_tap(io, i, NULL);
                        } else if(pos==NULL) {
-                               tmp=g_malloc(strlen(str)+1);
-                               strcpy(tmp,str);
+                               tmp=g_strdup(str);
                                register_io_tap(io, i, tmp);
                        } else {
                                tmp=g_malloc((pos-str)+1);
-                               strncpy(tmp,str,(pos-str));
-                               tmp[pos-str]=0;
+                               g_strlcpy(tmp,str,(pos-str)+1);
                                register_io_tap(io, i, tmp);
                        }
                        str=pos+1;
-                       i++;                    
+                       i++;
                } while(pos);
-       }                       
+       }
 }
 
 void