From Steve Osselton:
[obnox/wireshark/wip.git] / tap-iostat.c
index 758455f006d83180837ee11b23c6511270a0a0b4..8ff9e3078c4098a398b11a543be92d39834b315e 100644 (file)
@@ -37,6 +37,7 @@
 #include "epan/packet_info.h"
 #include <epan/tap.h>
 #include <epan/stat_cmd_args.h>
+#include <epan/strutil.h>
 #include "register.h"
 
 
@@ -68,7 +69,7 @@ typedef struct _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 +77,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;
@@ -171,7 +172,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=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val<it->counter){
@@ -216,7 +217,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=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val>it->counter){
@@ -254,7 +255,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=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
                                        it->counter+=val;
                                        break;
                                }
@@ -283,7 +284,8 @@ 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]:"");
        }
@@ -352,9 +354,14 @@ iostat_draw(void *arg)
                }
 
                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:
@@ -481,14 +488,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 +509,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,7 +522,7 @@ 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);
                        }
        
@@ -525,7 +530,7 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
                        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:
@@ -583,7 +588,7 @@ register_io_tap(io_stat_t *io, int i, const char *filter)
                        }
                        break;
                }
-               free(field);
+               g_free(field);
        }
 
 /*
@@ -593,7 +598,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);
@@ -624,11 +629,18 @@ 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);
        }
        
@@ -663,13 +675,11 @@ 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;