Fix a bug in counting DATA chunks.
[obnox/wireshark/wip.git] / gtk / summary_dlg.c
1 /* summary_dlg.c
2  * Routines for capture file summary window
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <wtap.h>
34 #include <time.h>
35
36 #include "globals.h"
37 #include "file.h"
38 #ifdef HAVE_LIBPCAP
39 #include <pcap.h>
40 #include "capture.h"
41 #include "main.h"
42 #endif
43 #include "summary.h"
44 #include "summary_dlg.h"
45 #include "dlg_utils.h"
46 #include "ui_util.h"
47 #include "compat_macros.h"
48 #include "help_dlg.h"
49
50 #define SUM_STR_MAX     1024
51 #define FILTER_SNIP_LEN 50
52
53
54 static void
55 add_string_to_table_sensitive(GtkWidget *list, guint *row, gchar *title, gchar *value, gboolean sensitive)
56 {
57     GtkWidget *label;
58     gchar     *indent;
59
60     if(strlen(value) != 0) {
61         indent = g_strdup_printf("   %s", title);
62     } else {
63         indent = g_strdup(title);
64     }
65     label = gtk_label_new(indent);
66     g_free(indent);
67     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
68     gtk_widget_set_sensitive(label, sensitive);
69     gtk_table_attach_defaults(GTK_TABLE(list), label, 0, 1, *row, *row+1);
70
71     label = gtk_label_new(value);
72     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
73     gtk_widget_set_sensitive(label, sensitive);
74     gtk_table_attach_defaults(GTK_TABLE(list), label, 1, 2, *row, *row+1);
75
76     *row = *row + 1;
77 }
78
79 static void
80 add_string_to_table(GtkWidget *list, guint *row, gchar *title, gchar *value)
81 {
82     add_string_to_table_sensitive(list, row, title, value, TRUE);
83 }
84
85
86 static void
87 add_string_to_list(GtkWidget *list, gchar *title, gchar *captured, gchar *displayed)
88 {
89     simple_list_append(list, 0, title, 1, captured, 2, displayed, -1);
90 }
91
92 void
93 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
94 {
95   summary_tally summary;
96   GtkWidget     *sum_open_w,
97                 *main_vb, *bbox, *close_bt, *help_bt;
98   GtkWidget     *table;
99   GtkWidget     *list;
100   char          *titles[] = { "Traffic", "Captured", "Displayed" };
101
102   gchar         string_buff[SUM_STR_MAX];
103   gchar         string_buff2[SUM_STR_MAX];
104
105   double        seconds;
106   double        disp_seconds;
107   guint         offset;
108   guint         snip;
109   guint         row;
110   gchar        *str_dup;
111   gchar        *str_work;
112
113   time_t        ti_time;
114   struct tm    *ti_tm;
115   unsigned int  elapsed_time;
116
117   /* initial computations */
118   summary_fill_in(&cfile, &summary);
119 #ifdef HAVE_LIBPCAP
120   summary_fill_in_capture(capture_opts, &summary);
121 #endif
122   seconds = summary.stop_time - summary.start_time;
123   disp_seconds = summary.filtered_stop - summary.filtered_start;
124
125   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Summary");
126
127   /* Container for each row of widgets */
128   main_vb = gtk_vbox_new(FALSE, 12);
129   gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
130   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
131
132   /* table */
133   table = gtk_table_new(1, 2, FALSE);
134   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
135   gtk_table_set_row_spacings(GTK_TABLE(table), 3);
136   gtk_container_add(GTK_CONTAINER(main_vb), table);
137   row = 0;
138
139
140   /* File */
141   add_string_to_table(table, &row, "File", "");
142
143   /* filename */
144   g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.filename);
145   add_string_to_table(table, &row, "Name:", string_buff);
146
147   /* length */
148   g_snprintf(string_buff, SUM_STR_MAX, "%lu bytes", summary.file_length);
149   add_string_to_table(table, &row, "Length:", string_buff);
150
151   /* format */
152   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.encap_type));
153   add_string_to_table(table, &row, "Format:", string_buff);
154
155   if (summary.has_snap) {
156     /* snapshot length */
157     g_snprintf(string_buff, SUM_STR_MAX, "%u bytes", summary.snap);
158     add_string_to_table(table, &row, "Packet size limit:", string_buff);
159   }
160
161
162   /* Time */
163   add_string_to_table(table, &row, "", "");
164   add_string_to_table(table, &row, "Time", "");
165
166   /* start time */
167   ti_time = (time_t)summary.start_time;
168   ti_tm = localtime(&ti_time);
169   g_snprintf(string_buff, SUM_STR_MAX,
170              "%04d-%02d-%02d %02d:%02d:%02d",
171              ti_tm->tm_year + 1900,
172              ti_tm->tm_mon + 1,
173              ti_tm->tm_mday,
174              ti_tm->tm_hour,
175              ti_tm->tm_min,
176              ti_tm->tm_sec);
177   add_string_to_table(table, &row, "First packet:", string_buff);
178
179   /* stop time */
180   ti_time = (time_t)summary.stop_time;
181   ti_tm = localtime(&ti_time);
182   g_snprintf(string_buff, SUM_STR_MAX,
183              "%04d-%02d-%02d %02d:%02d:%02d",
184              ti_tm->tm_year + 1900,
185              ti_tm->tm_mon + 1,
186              ti_tm->tm_mday,
187              ti_tm->tm_hour,
188              ti_tm->tm_min,
189              ti_tm->tm_sec);
190   add_string_to_table(table, &row, "Last packet:", string_buff);
191
192   /* elapsed seconds */
193   elapsed_time = (unsigned int)summary.elapsed_time;
194   if(elapsed_time/86400) {
195       g_snprintf(string_buff, SUM_STR_MAX, "%02u days %02u:%02u:%02u", 
196         elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
197   } else {
198       g_snprintf(string_buff, SUM_STR_MAX, "%02u:%02u:%02u", 
199         elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
200   }
201   add_string_to_table(table, &row, "Elapsed:", string_buff);
202
203
204   /* Capture */
205   add_string_to_table(table, &row, "", "");
206   add_string_to_table_sensitive(table, &row, "Capture", "", (summary.iface != NULL));
207
208   /* interface */
209   if (summary.iface) {
210     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.iface_descr);
211   } else {
212     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
213   }
214   add_string_to_table_sensitive(table, &row, "Interface:", string_buff, (summary.iface) != NULL);
215
216   /* Dropped count */
217   if (summary.drops_known) {
218     g_snprintf(string_buff, SUM_STR_MAX, "%u", summary.drops);
219   } else {
220     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
221   }
222   add_string_to_table_sensitive(table, &row, "Dropped packets:", string_buff, (summary.iface != NULL));
223
224 #ifdef HAVE_LIBPCAP
225   /* Capture filter */
226   if (summary.cfilter && summary.cfilter[0] != '\0') {
227     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.cfilter);
228   } else {
229     if(summary.iface) {
230       g_snprintf(string_buff, SUM_STR_MAX, "none");
231     } else {
232       g_snprintf(string_buff, SUM_STR_MAX, "unknown");
233     }
234   }
235   add_string_to_table_sensitive(table, &row, "Capture filter:", string_buff, (summary.iface != NULL));
236 #endif
237
238
239   /* Data */
240   add_string_to_table(table, &row, "", "");
241   add_string_to_table(table, &row, "Display", "");
242
243   if (summary.dfilter) {
244     /* Display filter */
245     /* limit each row to some reasonable length */
246     str_dup = g_strdup_printf("%s", summary.dfilter);
247     str_work = g_strdup(str_dup);
248     offset = 0;
249     snip = 0;
250     while(strlen(str_work) > FILTER_SNIP_LEN) {
251         str_work[FILTER_SNIP_LEN] = '\0';
252         add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
253         g_free(str_work);
254         offset+=FILTER_SNIP_LEN;
255         str_work = g_strdup(&str_dup[offset]);
256         snip++;
257     }
258     
259     add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
260     g_free(str_work);
261     g_free(str_dup);
262   } else {
263     /* Display filter */
264     add_string_to_table(table, &row, "Display filter:", "none");
265   }
266
267   /* Marked Packet count */
268   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.marked_count);
269   add_string_to_table(table, &row, "Marked packets:", string_buff);
270
271
272   /* Traffic */
273   list = simple_list_new(3, titles);
274   gtk_container_add(GTK_CONTAINER(main_vb), list);
275
276   g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
277   if(summary.dfilter) {
278     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
279   } else {
280     strcpy(string_buff2, "");
281   }
282   add_string_to_list(list, "Between first and last packet", string_buff, string_buff2);
283
284   /* Packet count */
285   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
286   if(summary.dfilter) {
287     g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
288   } else {
289     strcpy(string_buff2, "");
290   }
291   add_string_to_list(list, "Packets", string_buff, string_buff2);
292
293   /* Packets per second */
294   if (seconds > 0){
295     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
296   } else {
297     strcpy(string_buff, "");
298   }
299   if(summary.dfilter && disp_seconds > 0){
300     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
301   } else {
302     strcpy(string_buff2, "");
303   }
304   add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2);
305
306   /* Packet size */
307   if (summary.packet_count > 0){
308     g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
309       (float)summary.bytes/summary.packet_count);
310   } else {
311     strcpy(string_buff, "");
312   }
313   if (summary.dfilter && summary.filtered_count > 0){
314     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
315           (float) summary.filtered_bytes/summary.filtered_count);
316   } else {
317     strcpy(string_buff2, "");
318   }
319   add_string_to_list(list, "Avg. packet size", string_buff, string_buff2);
320
321   /* Byte count */
322   g_snprintf(string_buff, SUM_STR_MAX, "%d", summary.bytes);
323   if (summary.dfilter && summary.filtered_count > 0){
324     g_snprintf(string_buff2, SUM_STR_MAX, "%d", summary.filtered_bytes);
325   } else {
326     strcpy(string_buff2, "");
327   }
328   add_string_to_list(list, "Bytes", string_buff, string_buff2);
329
330   /* Bytes per second */
331   if (seconds > 0){
332     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.bytes/seconds);
333   } else {
334     strcpy(string_buff, "");
335   }
336   if (summary.dfilter && disp_seconds > 0){
337     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_bytes/disp_seconds);
338   } else {
339     strcpy(string_buff2, "");
340   }
341   add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2);
342
343   /* MBit per second */
344   if (seconds > 0){
345     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.bytes * 8.0 / (seconds * 1000.0 * 1000.0));
346   } else {
347     strcpy(string_buff, "");
348   }
349   if (summary.dfilter && disp_seconds > 0){
350     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", 
351           summary.filtered_bytes * 8.0 / (disp_seconds * 1000.0 * 1000.0));
352   } else {
353     strcpy(string_buff2, "");
354   }
355   add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2);
356
357   
358   /* Button row. */
359   if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
360     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
361   } else {
362     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
363   }
364   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
365
366   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
367   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
368
369   if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
370     help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
371     SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_STATS_SUMMARY_DIALOG);
372   }
373
374   gtk_widget_grab_focus(close_bt);
375
376   SIGNAL_CONNECT(sum_open_w, "delete_event", window_delete_event_cb, NULL);
377
378   gtk_widget_show_all(sum_open_w);
379   window_present(sum_open_w);
380 }