From Pascal Quantin: show decoding of the Power Headroom Level field as specified...
[obnox/wireshark/wip.git] / tap-iostat.c
index d36faa05c43a9f3c30c8322c9337b8b9f819997c..1a9f69f2543b4859838f46e9efc1fb92de6b751e 100644 (file)
@@ -3,8 +3,8 @@
  *
  * $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
@@ -35,7 +35,9 @@
 #include <string.h>
 #include "epan/epan_dissect.h"
 #include "epan/packet_info.h"
-#include "tap.h"
+#include <epan/tap.h>
+#include <epan/stat_cmd_args.h>
+#include <epan/strutil.h>
 #include "register.h"
 
 
@@ -43,7 +45,7 @@ typedef struct _io_stat_t {
        gint32 interval;        /* unit is ms */
        guint32 num_items;
        struct _io_stat_item_t *items;
-       char **filters;
+       const char **filters;
 } io_stat_t;   
 
 #define CALC_TYPE_BYTES        0
@@ -67,14 +69,15 @@ typedef struct _io_stat_item_t {
 
 
 static int
-iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_, 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;
        gint32 current_time;
        GPtrArray *gp;
        guint i;
 
-       current_time=((pinfo->fd->rel_secs*1000)+(pinfo->fd->rel_usecs/1000));
+       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;
@@ -118,7 +121,20 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
                if(gp){
                        for(i=0;i<gp->len;i++){
-                               it->counter+=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
+                               switch(proto_registrar_get_ftype(it->hf_index)){
+                               case FT_UINT8:
+                               case FT_UINT16:
+                               case FT_UINT24:
+                               case FT_UINT32:
+                                       it->counter+=fvalue_get_uinteger(&((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;
+                               }
                        }
                }
                break;
@@ -136,7 +152,7 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                                case FT_UINT16:
                                case FT_UINT24:
                                case FT_UINT32:
-                                       val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
+                                       val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val<it->counter){
@@ -147,16 +163,16 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                                case FT_INT16:
                                case FT_INT24:
                                case FT_INT32:
-                                       val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
+                                       val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if((gint32)val<(gint32)(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=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val<it->counter){
@@ -181,7 +197,7 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                                case FT_UINT16:
                                case FT_UINT24:
                                case FT_UINT32:
-                                       val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
+                                       val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if(val>it->counter){
@@ -192,7 +208,7 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                                case FT_INT16:
                                case FT_INT24:
                                case FT_INT32:
-                                       val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
+                                       val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
                                        if((it->frames==1)&&(i==0)){
                                                it->counter=val;
                                        } else if((gint32)val>(gint32)(it->counter)){
@@ -201,7 +217,7 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                                        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){
@@ -227,16 +243,19 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
                                case FT_UINT16:
                                case FT_UINT24:
                                case FT_UINT32:
+                                       val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
+                                       it->counter+=val;
+                                       break;
                                case FT_INT8:
                                case FT_INT16:
                                case FT_INT24:
                                case FT_INT32:
-                                       val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
+                                       val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
                                        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=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
                                        it->counter+=val;
                                        break;
                                }
@@ -249,8 +268,9 @@ iostat_packet(io_stat_item_t *mit, packet_info *pinfo, epan_dissect_t *edt _U_,
 }
 
 static void
-iostat_draw(io_stat_item_t *mit)
+iostat_draw(void *arg)
 {
+       io_stat_item_t *mit = arg;
        io_stat_t *iot;
        io_stat_item_t **items;
        guint32 *frames;
@@ -264,7 +284,8 @@ iostat_draw(io_stat_item_t *mit)
        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]:"");
        }
@@ -333,9 +354,14 @@ iostat_draw(io_stat_item_t *mit)
                }
 
                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:
@@ -426,43 +452,30 @@ iostat_draw(io_stat_item_t *mit)
 }
 
 
-static int
-get_calc_field(char *filter, char **flt)
-{
-       char field[256];
-       int i;
-       header_field_info *hfi;
-
-       *flt="";
-       for(i=0;filter[i];i++){
-               if(i>=255){
-                       fprintf(stderr,"get_calc_field(): Too long field name: %s\n", filter);
-                       exit(10);
-               }
-               if(filter[i]==')'){
-                       break;
-               }
-               field[i]=filter[i];
-               field[i+1]=0;
-       }
-       if(filter[i]==')'){
-               *flt=&filter[i+1];
-       }
+typedef struct {
+       const char *func_name;
+       int calc_type;
+} calc_type_ent_t;
 
-       hfi=proto_registrar_get_byname(field);
-       if(!hfi){
-               fprintf(stderr, "get_calc_field(): No such field %s\n", field);
-               exit(10);
-       }
-       
-       return hfi->id;
-}
+static calc_type_ent_t calc_type_table[] = {
+       { "COUNT", CALC_TYPE_COUNT },
+       { "SUM", CALC_TYPE_SUM },
+       { "MIN", CALC_TYPE_MIN },
+       { "MAX", CALC_TYPE_MAX },
+       { "AVG", CALC_TYPE_AVG },
+       { NULL, 0 }
+};
 
 static void
-register_io_tap(io_stat_t *io, int i, char *filter)
+register_io_tap(io_stat_t *io, int i, const char *filter)
 {
        GString *error_string;
-       char *flt;
+       const char *flt;
+       int j;
+       size_t namelen;
+       const char *p, *parenp;
+       char *field;
+       header_field_info *hfi;
 
        io->items[i].prev=&io->items[i];
        io->items[i].next=NULL;
@@ -475,33 +488,51 @@ register_io_tap(io_stat_t *io, int i, char *filter)
        io->filters[i]=filter;
        flt=filter;
 
-       if(!filter){
-               filter="";
-       }
-       if(!strncmp("COUNT(", filter, 6)){
-               io->items[i].calc_type=CALC_TYPE_COUNT;
-               io->items[i].hf_index=get_calc_field(filter+6, &flt);
-       } else if (!strncmp("SUM(", filter, 4)){
-               io->items[i].calc_type=CALC_TYPE_SUM;
-               io->items[i].hf_index=get_calc_field(filter+4, &flt);
-               switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
-               case FT_UINT8:
-               case FT_UINT16:
-               case FT_UINT24:
-               case FT_UINT32:
-               case FT_INT8:
-               case FT_INT16:
-               case FT_INT24:
-               case FT_INT32:
+       field=NULL;
+       hfi=NULL;
+       for(j=0; calc_type_table[j].func_name; j++){
+               namelen=strlen(calc_type_table[j].func_name);
+                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;
+
+                       p=filter+namelen+1;
+                       parenp=strchr(p, ')');
+                       if(!parenp){
+                               fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
+                               exit(10);
+                       }
+                       /* bail out if there was no field specified */
+                       if(parenp==p){
+                               fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
+                                   calc_type_table[j].func_name);
+                               exit(10);
+                       }
+                       field=g_malloc(parenp-p+1);
+                       if(!field){
+                               fprintf(stderr, "tshark: Out of memory.\n");
+                               exit(10);
+                       }
+                       memcpy(field, p, parenp-p);
+                       field[parenp-p] = '\0';
+                       flt=parenp + 1;
+
+                       hfi=proto_registrar_get_byname(field);
+                       if(!hfi){
+                               fprintf(stderr, "tshark: There is no field named '%s'.\n",
+                                   field);
+                               g_free(field);
+                               exit(10);
+                       }
+       
+                       io->items[i].hf_index=hfi->id;
                        break;
-               default:
-                       fprintf(stderr, "register_io_tap(): Invalid field type. SUM(x) only supports 8,16,24 and 32 byte integer fields\n");
-                       exit(10);
                }
-       } else if (!strncmp("MIN(", filter, 4)){
-               io->items[i].calc_type=CALC_TYPE_MIN;
-               io->items[i].hf_index=get_calc_field(filter+4, &flt);
-               switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
+       }
+       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:
@@ -510,48 +541,54 @@ register_io_tap(io_stat_t *io, int i, char *filter)
                case FT_INT16:
                case FT_INT24:
                case FT_INT32:
-               case FT_RELATIVE_TIME:
+                       /* these types support all calculations */
                        break;
-               default:
-                       fprintf(stderr, "register_io_tap(): Invalid field type. MIN(x) only supports 8,16,24 and 32 byte integer fields and relative time fields\n");
-                       exit(10);
-               }
-       } else if (!strncmp("MAX(", filter, 4)){
-               io->items[i].calc_type=CALC_TYPE_MAX;
-               io->items[i].hf_index=get_calc_field(filter+4, &flt);
-               switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
-               case FT_UINT8:
-               case FT_UINT16:
-               case FT_UINT24:
-               case FT_UINT32:
-               case FT_INT8:
-               case FT_INT16:
-               case FT_INT24:
-               case FT_INT32:
                case FT_RELATIVE_TIME:
+                       /* this type only supports SUM, COUNT, MAX, MIN, AVG */
+                       switch(io->items[i].calc_type){
+                       case CALC_TYPE_SUM:
+                       case CALC_TYPE_COUNT:
+                       case CALC_TYPE_MAX:
+                       case CALC_TYPE_MIN:
+                       case CALC_TYPE_AVG:
+                               break;
+                       default:
+                               fprintf(stderr,
+                                   "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
+                                   field,
+                                   calc_type_table[j].func_name);
+                               exit(10);
+                       }
                        break;
-               default:
-                       fprintf(stderr, "register_io_tap(): Invalid field type. MAX(x) only supports 8,16,24 and 32 byte integer fields and relative time fields\n");
-                       exit(10);
-               }
-       } else if (!strncmp("AVG(", filter, 4)){
-               io->items[i].calc_type=CALC_TYPE_AVG;
-               io->items[i].hf_index=get_calc_field(filter+4, &flt);
-               switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
-               case FT_UINT8:
-               case FT_UINT16:
-               case FT_UINT24:
-               case FT_UINT32:
-               case FT_INT8:
-               case FT_INT16:
-               case FT_INT24:
-               case FT_INT32:
-               case FT_RELATIVE_TIME:
+               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:
-                       fprintf(stderr, "register_io_tap(): Invalid field type. AVG(x) only supports 8,16,24 and 32 byte integer fields and relative time fields\n");
-                       exit(10);
+                       /*
+                        * XXX - support all operations on floating-point
+                        * numbers?
+                        */
+                       if(io->items[i].calc_type!=CALC_TYPE_COUNT){
+                               fprintf(stderr,
+                                   "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
+                                   field,
+                                   calc_type_table[j].func_name);
+                               exit(10);
+                       }
+                       break;
                }
+               g_free(field);
        }
 
 /*
@@ -561,25 +598,25 @@ CALC_TYPE_MAX     4
 CALC_TYPE_AVG  5
 */
 
-       error_string=register_tap_listener("frame", &io->items[i], flt, NULL, (void*)iostat_packet, i?NULL:(void*)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);
-               fprintf(stderr, "tethereal: Couldn't register io,stat tap: %s\n",
+               fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
                    error_string->str);
                g_string_free(error_string, TRUE);
                exit(1);
        }
 }
 
-void
-iostat_init(char *optarg)
+static void
+iostat_init(const char *optarg, void* userdata _U_)
 {
        float interval_float;
        gint32 interval; 
        int pos=0;
        io_stat_t *io;
-       char *filter=NULL;
+       const char *filter=NULL;
 
        if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
                if(pos){
@@ -588,15 +625,22 @@ iostat_init(char *optarg)
                        filter=NULL;
                }
        } else {
-               fprintf(stderr, "tethereal: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
+               fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
                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, "tethereal:iostat_init()  interval must be >=0.001 seconds\n");
+               fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds or 0.\n");
                exit(10);
        }
        
@@ -609,7 +653,8 @@ iostat_init(char *optarg)
 
                register_io_tap(io, 0, NULL);
        } else {
-               char *str,*pos,*tmp;
+               const char *str,*pos;
+               char *tmp;
                int i;
                /* find how many ',' separated filters we have */
                str=filter;
@@ -630,13 +675,11 @@ iostat_init(char *optarg)
                        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;
@@ -648,6 +691,5 @@ iostat_init(char *optarg)
 void
 register_tap_listener_iostat(void)
 {
-       register_ethereal_tap("io,stat,", iostat_init);
+       register_stat_cmd_arg("io,stat,", iostat_init, NULL);
 }
-