Get rid of a Clist and move the functon to display floats with two decimals to gui_utils.
[metze/wireshark/wip.git] / proto_hier_stats.c
index f98d8d75a6f145b4f0252fdb1e3baf9b9568b134..40651fc6aeea556cd753110aac39d7c4a773c263 100644 (file)
@@ -1,10 +1,10 @@
 /* proto_hier_stats.c
  * Routines for calculating statistics based on protocol.
  *
- * $Id: proto_hier_stats.c,v 1.24 2004/01/31 02:25:43 ulfl 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
@@ -34,6 +34,7 @@
 #include <wtap.h>
 
 #include <stdio.h>
+#include <string.h>
 #include <glib.h>
 
 /* Update the progress bar this many times when scanning the packet list. */
@@ -42,6 +43,7 @@
 #define STAT_NODE_STATS(n)   ((ph_stats_node_t*)(n)->data)
 #define STAT_NODE_HFINFO(n)  (STAT_NODE_STATS(n)->hfinfo)
 
+
 static GNode*
 find_stat_node(GNode *parent_stat_node, header_field_info *needle_hfinfo)
 {
@@ -86,21 +88,33 @@ process_node(proto_node *ptree_node, GNode *parent_stat_node, ph_stats_t *ps, gu
        finfo = PITEM_FINFO(ptree_node);
        g_assert(finfo);
 
-       stat_node = find_stat_node(parent_stat_node, finfo->hfinfo);
-
-       /* Assert that the finfo is related to a protocol, not a field. */
-       g_assert(finfo->hfinfo->parent == -1);
-
-       stats = STAT_NODE_STATS(stat_node);
-       stats->num_pkts_total++;
-       stats->num_bytes_total += pkt_len;
+       /* If the field info isn't related to a protocol but to a field,
+        * don't count them, as they don't belong to any protocol.
+        * (happens e.g. for toplevel tree item of desegmentation "[Reassembled TCP Segments]") */
+       if (finfo->hfinfo->parent != -1) {
+               /* Skip this element, use parent status node */
+               stat_node = parent_stat_node;
+               stats = STAT_NODE_STATS(stat_node);
+       } else {
+               stat_node = find_stat_node(parent_stat_node, finfo->hfinfo);
+
+               stats = STAT_NODE_STATS(stat_node);
+               stats->num_pkts_total++;
+               stats->num_bytes_total += pkt_len;
+       }
 
        proto_sibling_node = ptree_node->next;
 
        if (proto_sibling_node) {
+               /* If the name does not exist for this proto_sibling_node, then it is
+                * not a normal protocol in the top-level tree.  It was instead
+                * added as a normal tree such as IPv6's Hop-by-hop Option Header and
+                * should be skipped when creating the protocol hierarchy display. */
+               if(strlen(proto_sibling_node->finfo->hfinfo->name) == 0 && ptree_node->next)
+                       proto_sibling_node = proto_sibling_node->next;
+
                process_node(proto_sibling_node, stat_node, ps, pkt_len);
-       }
-       else {
+       } else {
                stats->num_pkts_last++;
                stats->num_bytes_last += pkt_len;
        }
@@ -129,30 +143,38 @@ process_frame(frame_data *frame, column_info *cinfo, ph_stats_t* ps)
        guint8                          pd[WTAP_MAX_PACKET_SIZE];
        int                             err;
        gchar                           *err_info;
+       double                          cur_time;
 
        /* Load the frame from the capture file */
        if (!wtap_seek_read(cfile.wth, frame->file_off, &phdr, pd,
            frame->cap_len, &err, &err_info)) {
-               simple_dialog(ESD_TYPE_ERROR, NULL,
+               simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    cf_read_error_message(err, err_info), cfile.filename);
                return FALSE;   /* failure */
        }
 
-       /* Dissect the frame */
+       /* Dissect the frame   tree  not visible */
        edt = epan_dissect_new(TRUE, FALSE);
        epan_dissect_run(edt, &phdr, pd, frame, cinfo);
 
        /* Get stats from this protocol tree */
        process_tree(edt->tree, ps, frame->pkt_len);
 
+       /* Update times */
+       cur_time = nstime_to_sec(&frame->abs_ts);
+       if (cur_time < ps->first_time) {
+         ps->first_time = cur_time;
+       }
+       if (cur_time > ps->last_time){
+         ps->last_time = cur_time;
+       }
+
        /* Free our memory. */
        epan_dissect_free(edt);
 
        return TRUE;    /* success */
 }
 
-
-
 ph_stats_t*
 ph_stats_new(void)
 {
@@ -162,7 +184,7 @@ ph_stats_new(void)
        progdlg_t       *progbar = NULL;
        gboolean        stop_flag;
        int             count;
-       float           prog_val;
+       float           progbar_val;
        GTimeVal        start_time;
        gchar           status_str[100];
        int             progbar_nextstep;
@@ -173,6 +195,8 @@ ph_stats_new(void)
        ps->tot_packets = 0;
        ps->tot_bytes = 0;
        ps->stats_tree = g_node_new(NULL);
+       ps->first_time = 0.0;
+       ps->last_time = 0.0;
 
        /* Update the progress bar when it gets to this value. */
        progbar_nextstep = 0;
@@ -181,6 +205,8 @@ ph_stats_new(void)
        progbar_quantum = cfile.count/N_PROGBAR_UPDATES;
        /* Count of packets at which we've looked. */
        count = 0;
+       /* Progress so far. */
+       progbar_val = 0.0;
 
        stop_flag = FALSE;
        g_get_current_time(&start_time);
@@ -189,6 +215,17 @@ ph_stats_new(void)
        tot_bytes = 0;
 
        for (frame = cfile.plist; frame != NULL; frame = frame->next) {
+               /* Create the progress bar if necessary.
+                  We check on every iteration of the loop, so that
+                  it takes no longer than the standard time to create
+                  it (otherwise, for a large file, we might take
+                  considerably longer than that standard time in order
+                  to get to the next progress bar step). */
+               if (progbar == NULL)
+                       progbar = delayed_create_progress_dlg(
+                           "Computing", "protocol hierarchy statistics", 
+                           TRUE, &stop_flag, &start_time, progbar_val);
+
                /* Update the progress bar, but do it only N_PROGBAR_UPDATES
                   times; when we update it, we have to run the GTK+ main
                   loop to get it to repaint what's pending, and doing so
@@ -201,18 +238,12 @@ ph_stats_new(void)
                         */
                        g_assert(cfile.count > 0);
 
-                       prog_val = (gfloat) count / cfile.count;
-
-                       if (progbar == NULL)
-                               /* Create the progress bar if necessary */
-                               progbar = delayed_create_progress_dlg(
-                                   "Computing", "protocol hierarchy statistics", 
-                                   &stop_flag, &start_time, prog_val);
+                       progbar_val = (gfloat) count / cfile.count;
 
                        if (progbar != NULL) {
                                g_snprintf(status_str, sizeof(status_str),
                                        "%4u of %u frames", count, cfile.count);
-                               update_progress_dlg(progbar, prog_val, status_str);
+                               update_progress_dlg(progbar, progbar_val, status_str);
                        }
 
                        progbar_nextstep += progbar_quantum;
@@ -230,7 +261,15 @@ ph_stats_new(void)
                   probably do so for other loops (see "file.c") that
                   look only at those packets. */
                if (frame->flags.passed_dfilter) {
-                       if (!process_frame(frame, &cfile.cinfo, ps)) {
+
+                       if (tot_packets == 0) {
+                               double cur_time = nstime_to_sec(&frame->abs_ts);
+                               ps->first_time = cur_time;
+                               ps->last_time = cur_time;
+                       }
+                       
+                       /* we don't care about colinfo */
+                       if (!process_frame(frame, NULL, ps)) {
                                /*
                                 * Give up, and set "stop_flag" so we
                                 * just abort rather than popping up