d6df9b6b92ecc1e10daf5e7695acad88a48fea04
[obnox/wireshark/wip.git] / gtk / summary_dlg.c
1 /* summary_dlg.c
2  * Routines for capture file summary window
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #include <string.h>
33 #include <time.h>
34
35 #include <gtk/gtk.h>
36
37 #include <epan/strutil.h>
38
39 #include <wiretap/wtap.h>
40
41 #include "../globals.h"
42 #include "../file.h"
43 #include "../summary.h"
44 #ifdef HAVE_LIBPCAP
45 #include "../capture.h"
46 #include "gtk/main.h"
47 #include "gtk/capture_globals.h"
48 #endif
49
50 #include "gtk/summary_dlg.h"
51 #include "gtk/dlg_utils.h"
52 #include "gtk/gui_utils.h"
53 #include "gtk/help_dlg.h"
54
55
56 #define SUM_STR_MAX     1024
57 #define FILTER_SNIP_LEN 50
58
59
60 static void
61 add_string_to_table_sensitive(GtkWidget *list, guint *row, const gchar *title, const gchar *value, gboolean sensitive)
62 {
63     GtkWidget *label;
64     gchar     *indent;
65
66     if(strlen(value) != 0) {
67         indent = g_strdup_printf("   %s", title);
68     } else {
69         indent = g_strdup(title);
70     }
71     label = gtk_label_new(indent);
72     if (strlen(value) == 0) {
73       gchar *message = g_strdup_printf("<span weight=\"bold\">%s</span>", title);
74       gtk_label_set_markup(GTK_LABEL(label), message);
75       g_free (message);
76     }
77     g_free(indent);
78     gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
79     gtk_widget_set_sensitive(label, sensitive);
80     gtk_table_attach_defaults(GTK_TABLE(list), label, 0, 1, *row, *row+1);
81
82     label = gtk_label_new(value);
83     gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
84     gtk_widget_set_sensitive(label, sensitive);
85     gtk_table_attach_defaults(GTK_TABLE(list), label, 1, 2, *row, *row+1);
86
87     *row = *row + 1;
88 }
89
90 static void
91 add_string_to_table(GtkWidget *list, guint *row, gchar *title, gchar *value)
92 {
93     add_string_to_table_sensitive(list, row, title, value, TRUE);
94 }
95
96
97 static void
98 add_string_to_list(GtkWidget *list, gchar *title, gchar *captured, gchar *displayed, gchar *marked)
99 {
100     simple_list_append(list, 0, title, 1, captured, 2, displayed, 3, marked, -1);
101 }
102
103 void
104 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
105 {
106   summary_tally summary;
107   GtkWidget     *sum_open_w,
108                 *main_vb, *bbox, *close_bt, *help_bt;
109   GtkWidget     *table;
110   GtkWidget     *list;
111   static const char *titles[] = { "Traffic", "Captured", "Displayed", "Marked" };
112
113   gchar         string_buff[SUM_STR_MAX];
114   gchar         string_buff2[SUM_STR_MAX];
115   gchar         string_buff3[SUM_STR_MAX];
116
117   double        seconds;
118   double        disp_seconds;
119   double        marked_seconds;
120   guint         offset;
121   guint         snip;
122   guint         row;
123   gchar        *str_dup;
124   gchar        *str_work;
125
126   time_t        ti_time;
127   struct tm    *ti_tm;
128   unsigned int  elapsed_time;
129
130   /* initial computations */
131   summary_fill_in(&cfile, &summary);
132 #ifdef HAVE_LIBPCAP
133   summary_fill_in_capture(&global_capture_opts, &summary);
134 #endif
135   seconds = summary.stop_time - summary.start_time;
136   disp_seconds = summary.filtered_stop - summary.filtered_start;
137   marked_seconds = summary.marked_stop - summary.marked_start;
138
139   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Summary");
140
141   /* Container for each row of widgets */
142   main_vb = gtk_vbox_new(FALSE, 12);
143   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
144   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
145
146   /* table */
147   table = gtk_table_new(1, 2, FALSE);
148   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
149   gtk_table_set_row_spacings(GTK_TABLE(table), 3);
150   gtk_container_add(GTK_CONTAINER(main_vb), table);
151   row = 0;
152
153
154   /* File */
155   add_string_to_table(table, &row, "File", "");
156
157   /* filename */
158   g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.filename);
159   add_string_to_table(table, &row, "Name:", string_buff);
160
161   /* length */
162   g_snprintf(string_buff, SUM_STR_MAX, "%" G_GINT64_MODIFIER "d bytes", summary.file_length);
163   add_string_to_table(table, &row, "Length:", string_buff);
164
165   /* format */
166   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.file_type));
167   add_string_to_table(table, &row, "Format:", string_buff);
168
169   /* encapsulation */
170   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_encap_string(summary.encap_type));
171   add_string_to_table(table, &row, "Encapsulation:", string_buff);
172
173   if (summary.has_snap) {
174     /* snapshot length */
175     g_snprintf(string_buff, SUM_STR_MAX, "%u bytes", summary.snap);
176     add_string_to_table(table, &row, "Packet size limit:", string_buff);
177   }
178
179
180   /* Time */
181   add_string_to_table(table, &row, "", "");
182   add_string_to_table(table, &row, "Time", "");
183
184   /* start time */
185   ti_time = (time_t)summary.start_time;
186   ti_tm = localtime(&ti_time);
187   g_snprintf(string_buff, SUM_STR_MAX,
188              "%04d-%02d-%02d %02d:%02d:%02d",
189              ti_tm->tm_year + 1900,
190              ti_tm->tm_mon + 1,
191              ti_tm->tm_mday,
192              ti_tm->tm_hour,
193              ti_tm->tm_min,
194              ti_tm->tm_sec);
195   add_string_to_table(table, &row, "First packet:", string_buff);
196
197   /* stop time */
198   ti_time = (time_t)summary.stop_time;
199   ti_tm = localtime(&ti_time);
200   g_snprintf(string_buff, SUM_STR_MAX,
201              "%04d-%02d-%02d %02d:%02d:%02d",
202              ti_tm->tm_year + 1900,
203              ti_tm->tm_mon + 1,
204              ti_tm->tm_mday,
205              ti_tm->tm_hour,
206              ti_tm->tm_min,
207              ti_tm->tm_sec);
208   add_string_to_table(table, &row, "Last packet:", string_buff);
209
210   /* elapsed seconds */
211   elapsed_time = (unsigned int)summary.elapsed_time;
212   if(elapsed_time/86400) {
213       g_snprintf(string_buff, SUM_STR_MAX, "%02u days %02u:%02u:%02u",
214         elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
215   } else {
216       g_snprintf(string_buff, SUM_STR_MAX, "%02u:%02u:%02u",
217         elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
218   }
219   add_string_to_table(table, &row, "Elapsed:", string_buff);
220
221
222   /* Capture */
223   add_string_to_table(table, &row, "", "");
224   add_string_to_table_sensitive(table, &row, "Capture", "", (summary.iface != NULL));
225
226   /* interface */
227   if (summary.iface) {
228     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.iface_descr);
229   } else {
230     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
231   }
232   add_string_to_table_sensitive(table, &row, "Interface:", string_buff, (summary.iface) != NULL);
233
234   /* Dropped count */
235   if (summary.drops_known) {
236     g_snprintf(string_buff, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.drops);
237   } else {
238     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
239   }
240   add_string_to_table_sensitive(table, &row, "Dropped packets:", string_buff, (summary.iface != NULL));
241
242 #ifdef HAVE_LIBPCAP
243   /* Capture filter */
244   if (summary.cfilter && summary.cfilter[0] != '\0') {
245     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.cfilter);
246   } else {
247     if(summary.iface) {
248       g_snprintf(string_buff, SUM_STR_MAX, "none");
249     } else {
250       g_snprintf(string_buff, SUM_STR_MAX, "unknown");
251     }
252   }
253   add_string_to_table_sensitive(table, &row, "Capture filter:", string_buff, (summary.iface != NULL));
254 #endif
255
256
257   /* Data */
258   add_string_to_table(table, &row, "", "");
259   add_string_to_table(table, &row, "Display", "");
260
261   if (summary.dfilter) {
262     /* Display filter */
263     /* limit each row to some reasonable length */
264     str_dup = g_strdup_printf("%s", summary.dfilter);
265     str_work = g_strdup(str_dup);
266     offset = 0;
267     snip = 0;
268     while(strlen(str_work) > FILTER_SNIP_LEN) {
269         str_work[FILTER_SNIP_LEN] = '\0';
270         add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
271         g_free(str_work);
272         offset+=FILTER_SNIP_LEN;
273         str_work = g_strdup(&str_dup[offset]);
274         snip++;
275     }
276
277     add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
278     g_free(str_work);
279     g_free(str_dup);
280   } else {
281     /* Display filter */
282     add_string_to_table(table, &row, "Display filter:", "none");
283   }
284
285   /* Traffic */
286   list = simple_list_new(4, titles);
287   gtk_container_add(GTK_CONTAINER(main_vb), list);
288
289   /* Packet count */
290   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
291   if (summary.dfilter) {
292     g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
293   } else {
294     g_strlcpy(string_buff2, string_buff, SUM_STR_MAX);
295   }
296   g_snprintf(string_buff3, SUM_STR_MAX, "%i", summary.marked_count);
297   add_string_to_list(list, "Packets", string_buff, string_buff2, string_buff3);
298
299   /* Time between first and last */
300   if (seconds > 0) {
301     g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
302   } else {
303     string_buff[0] = '\0';
304   }
305   if (summary.dfilter && disp_seconds > 0) {
306     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
307   } else {
308     string_buff2[0] = '\0';
309   }
310   if (summary.marked_count && marked_seconds > 0) {
311     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f sec", marked_seconds);
312   } else {
313     string_buff3[0] = '\0';
314   }
315   add_string_to_list(list, "Between first and last packet", string_buff, string_buff2, string_buff3);
316
317   /* Packets per second */
318   if (seconds > 0) {
319     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
320   } else {
321     string_buff[0] = '\0';
322   }
323   if(summary.dfilter && disp_seconds > 0) {
324     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
325   } else {
326     string_buff2[0] = '\0';
327   }
328   if(summary.marked_count && marked_seconds > 0) {
329     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f", summary.marked_count/marked_seconds);
330   } else {
331     string_buff3[0] = '\0';
332   }
333   add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2, string_buff3);
334
335   /* Packet size */
336   if (summary.packet_count > 1) {
337     g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
338                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
339                (float) ((gint64) summary.bytes)/summary.packet_count);
340   } else {
341     string_buff[0] = '\0';
342   }
343   if (summary.dfilter && summary.filtered_count > 1) {
344     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
345                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
346                (float) ((gint64) summary.filtered_bytes)/summary.filtered_count);
347   } else {
348     string_buff2[0] = '\0';
349   }
350   if (summary.marked_count > 1) {
351     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f bytes",
352                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
353                (float) ((gint64) summary.marked_bytes)/summary.marked_count);
354   } else {
355     string_buff3[0] = '\0';
356   }
357   add_string_to_list(list, "Avg. packet size", string_buff, string_buff2, string_buff3);
358
359   /* Byte count */
360   g_snprintf(string_buff, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.bytes);
361   if (summary.dfilter && summary.filtered_count > 0) {
362     g_snprintf(string_buff2, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.filtered_bytes);
363   } else {
364     string_buff2[0] = '\0';
365   }
366   if (summary.marked_count) {
367     g_snprintf(string_buff3, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.marked_bytes);
368   } else {
369     string_buff3[0] = '\0';
370   }
371   add_string_to_list(list, "Bytes", string_buff, string_buff2, string_buff3);
372
373   /* Bytes per second */
374   if (seconds > 0){
375     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
376     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes)/seconds);
377   } else {
378     string_buff[0] = '\0';
379   }
380   if (summary.dfilter && disp_seconds > 0) {
381     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
382     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", ((gint64) summary.filtered_bytes)/disp_seconds);
383   } else {
384     string_buff2[0] = '\0';
385   }
386   if (summary.marked_count && marked_seconds > 0) {
387     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
388     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f", ((gint64) summary.marked_bytes)/marked_seconds);
389   } else {
390     string_buff3[0] = '\0';
391   }
392   add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2, string_buff3);
393
394   /* MBit per second */
395   if (seconds > 0) {
396     g_snprintf(string_buff, SUM_STR_MAX, "%.3f",
397                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
398                ((gint64) summary.bytes) * 8.0 / (seconds * 1000.0 * 1000.0));
399   } else {
400     string_buff[0] = '\0';
401   }
402   if (summary.dfilter && disp_seconds > 0) {
403     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f",
404                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
405                ((gint64) summary.filtered_bytes) * 8.0 / (disp_seconds * 1000.0 * 1000.0));
406   } else {
407     string_buff2[0] = '\0';
408   }
409   if (summary.marked_count && marked_seconds > 0) {
410     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f",
411                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
412                ((gint64) summary.marked_bytes) * 8.0 / (marked_seconds * 1000.0 * 1000.0));
413   } else {
414     string_buff3[0] = '\0';
415   }
416   add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2, string_buff3);
417
418
419   /* Button row. */
420   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
421   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
422
423   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
424   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
425
426   help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
427   g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_STATS_SUMMARY_DIALOG);
428
429   gtk_widget_grab_focus(close_bt);
430
431   g_signal_connect(sum_open_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
432
433   gtk_widget_show_all(sum_open_w);
434   window_present(sum_open_w);
435 }