87f8a8342415ef4b21964352c00bbfbce9a1c97d
[metze/wireshark/wip.git] / ui / 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 #include "../capture-pcap-util.h"
45 #ifdef HAVE_LIBPCAP
46 #include "../capture.h"
47 #include "ui/gtk/main.h"
48 #include "ui/gtk/capture_globals.h"
49 #endif
50
51 #include "ui/gtk/summary_dlg.h"
52 #include "ui/gtk/dlg_utils.h"
53 #include "ui/gtk/gui_utils.h"
54 #include "ui/gtk/help_dlg.h"
55
56 #define SUM_STR_MAX     1024
57 #define FILTER_SNIP_LEN 50
58 #define SHB_STR_SNIP_LEN 50
59
60
61 static void
62 add_string_to_table_sensitive(GtkWidget *list, guint *row, const gchar *title, const gchar *value, gboolean sensitive)
63 {
64     GtkWidget *label;
65     gchar     *indent;
66
67     if(strlen(value) != 0) {
68         indent = g_strdup_printf("   %s", title);
69     } else {
70         indent = g_strdup(title);
71     }
72     label = gtk_label_new(indent);
73     if (strlen(value) == 0) {
74       gchar *message = g_strdup_printf("<span weight=\"bold\">%s</span>", title);
75       gtk_label_set_markup(GTK_LABEL(label), message);
76       g_free (message);
77     }
78     g_free(indent);
79     gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
80     gtk_widget_set_sensitive(label, sensitive);
81     gtk_table_attach_defaults(GTK_TABLE(list), label, 0, 1, *row, *row+1);
82
83     label = gtk_label_new(value);
84     gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
85     gtk_widget_set_sensitive(label, sensitive);
86     gtk_table_attach_defaults(GTK_TABLE(list), label, 1, 2, *row, *row+1);
87
88     *row = *row + 1;
89 }
90
91 static void
92 add_string_to_table(GtkWidget *list, guint *row, gchar *title, gchar *value)
93 {
94     add_string_to_table_sensitive(list, row, title, value, TRUE);
95 }
96
97
98 static void
99 add_string_to_list(GtkWidget *list, gchar *title, gchar *captured, gchar *displayed, gchar *marked)
100 {
101     simple_list_append(list, 0, title, 1, captured, 2, displayed, 3, marked, -1);
102 }
103
104 static void
105 time_to_string(char *string_buff, gulong string_buff_size, time_t ti_time)
106 {
107   struct tm *ti_tm;
108
109 #ifdef _MSC_VER
110   /* calling localtime() on MSVC 2005 with huge values causes it to crash */
111   /* XXX - find the exact value that still does work */
112   /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
113   if (ti_time > 2000000000) {
114       ti_tm = NULL;
115   } else
116 #endif
117   ti_tm = localtime(&ti_time);
118   if (ti_tm == NULL) {
119     g_snprintf(string_buff, string_buff_size, "Not representable");
120     return;
121   }
122   g_snprintf(string_buff, string_buff_size,
123              "%04d-%02d-%02d %02d:%02d:%02d",
124              ti_tm->tm_year + 1900,
125              ti_tm->tm_mon + 1,
126              ti_tm->tm_mday,
127              ti_tm->tm_hour,
128              ti_tm->tm_min,
129              ti_tm->tm_sec);
130 }
131
132 void
133 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
134 {
135   summary_tally summary;
136   GtkWidget     *sum_open_w,
137                 *main_vb, *bbox, *close_bt, *help_bt;
138   GtkWidget     *table, *scrolled_window;
139   GtkWidget     *list, *treeview;
140   GtkListStore  *store;
141   GtkTreeIter    iter;
142   GtkCellRenderer *renderer;
143   GtkTreeViewColumn *column;
144 #ifdef HAVE_LIBPCAP
145   const char    *dl_description;
146 #endif
147   static const char *titles[] = { "Traffic", "Captured", "Displayed", "Marked" };
148
149   gchar         string_buff[SUM_STR_MAX];
150   gchar         string_buff2[SUM_STR_MAX];
151   gchar         string_buff3[SUM_STR_MAX];
152   gchar         string_buff4[SUM_STR_MAX];
153   gchar         string_buff5[SUM_STR_MAX];
154
155   double        seconds;
156   double        disp_seconds;
157   double        marked_seconds;
158   guint         offset;
159   guint         snip;
160   guint         row;
161   gchar        *str_dup;
162   gchar        *str_work;
163
164   unsigned int  elapsed_time;
165   iface_options iface;
166   unsigned int  i;
167
168   /* initial computations */
169   summary_fill_in(&cfile, &summary);
170 #ifdef HAVE_LIBPCAP
171   summary_fill_in_capture(&cfile, &global_capture_opts, &summary);
172 #endif
173   seconds = summary.stop_time - summary.start_time;
174   disp_seconds = summary.filtered_stop - summary.filtered_start;
175   marked_seconds = summary.marked_stop - summary.marked_start;
176
177   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Summary");
178
179   /* Container for each row of widgets */
180   main_vb = gtk_vbox_new(FALSE, 12);
181   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
182   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
183
184   /* table */
185   table = gtk_table_new(1, 2, FALSE);
186   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
187   gtk_table_set_row_spacings(GTK_TABLE(table), 3);
188   gtk_container_add(GTK_CONTAINER(main_vb), table);
189   row = 0;
190
191
192   /* File */
193   add_string_to_table(table, &row, "File", "");
194
195   /* filename */
196   g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.filename);
197   add_string_to_table(table, &row, "Name:", string_buff);
198
199   /* length */
200   g_snprintf(string_buff, SUM_STR_MAX, "%" G_GINT64_MODIFIER "d bytes", summary.file_length);
201   add_string_to_table(table, &row, "Length:", string_buff);
202
203   /* format */
204   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.file_type));
205   add_string_to_table(table, &row, "Format:", string_buff);
206
207   /* encapsulation */
208   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_encap_string(summary.encap_type));
209   add_string_to_table(table, &row, "Encapsulation:", string_buff);
210
211   if (summary.has_snap) {
212     /* snapshot length */
213     g_snprintf(string_buff, SUM_STR_MAX, "%u bytes", summary.snap);
214     add_string_to_table(table, &row, "Packet size limit:", string_buff);
215   }
216
217
218   /* Time */
219   add_string_to_table(table, &row, "", "");
220   add_string_to_table(table, &row, "Time", "");
221
222   /* start time */
223   time_to_string(string_buff, SUM_STR_MAX, (time_t)summary.start_time);
224   add_string_to_table(table, &row, "First packet:", string_buff);
225
226   /* stop time */
227   time_to_string(string_buff, SUM_STR_MAX, (time_t)summary.stop_time);
228   add_string_to_table(table, &row, "Last packet:", string_buff);
229
230   /* elapsed seconds */
231   elapsed_time = (unsigned int)summary.elapsed_time;
232   if(elapsed_time/86400) {
233       g_snprintf(string_buff, SUM_STR_MAX, "%02u days %02u:%02u:%02u",
234         elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
235   } else {
236       g_snprintf(string_buff, SUM_STR_MAX, "%02u:%02u:%02u",
237         elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
238   }
239   add_string_to_table(table, &row, "Elapsed:", string_buff);
240
241
242   /* Capture */
243   add_string_to_table(table, &row, "", "");
244   add_string_to_table_sensitive(table, &row, "Capture", "", (summary.ifaces->len > 0));
245   if(summary.shb_hardware){
246           /* trucate the string to a reasonable length */
247           g_snprintf(string_buff, SHB_STR_SNIP_LEN, "%s",summary.shb_hardware);
248       add_string_to_table(table, &row, "Capture HW:",string_buff);
249   }
250   if(summary.shb_os){
251           /* trucate the strings to a reasonable length */
252           g_snprintf(string_buff, SHB_STR_SNIP_LEN, "%s",summary.shb_os);
253       add_string_to_table(table, &row, "OS:", string_buff);
254   }
255   if(summary.shb_user_appl){
256           /* trucate the string to a reasonable length */
257           g_snprintf(string_buff, SHB_STR_SNIP_LEN, "%s",summary.shb_user_appl);
258       add_string_to_table(table, &row, "Capture application:", string_buff);
259   }
260   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
261   gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 5);
262   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
263                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
264   gtk_widget_set_size_request(scrolled_window, -1, 120);
265
266   treeview = gtk_tree_view_new();
267   renderer = gtk_cell_renderer_text_new();
268   column = gtk_tree_view_column_new_with_attributes("Interface", renderer, "text", 0, NULL);
269   gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
270   renderer = gtk_cell_renderer_text_new();
271   column = gtk_tree_view_column_new_with_attributes("Dropped Packets", renderer, "text", 1, NULL);
272   gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
273   renderer = gtk_cell_renderer_text_new();
274   column = gtk_tree_view_column_new_with_attributes("Capture Filter", renderer, "text", 2, NULL);
275   gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
276   renderer = gtk_cell_renderer_text_new();
277   column = gtk_tree_view_column_new_with_attributes("Link type", renderer, "text", 3, NULL);
278   gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
279   renderer = gtk_cell_renderer_text_new();
280   column = gtk_tree_view_column_new_with_attributes("Packet size limit", renderer, "text", 4, NULL);
281   gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
282       
283   store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
284   for (i = 0; i < summary.ifaces->len; i++) {
285     iface = g_array_index(summary.ifaces, iface_options, i);
286     /* interface */
287     if (iface.descr) {
288       g_snprintf(string_buff, SUM_STR_MAX, "%s", iface.descr);
289     } else if (iface.name) {
290       g_snprintf(string_buff, SUM_STR_MAX, "%s", iface.name);
291     } else {
292       g_snprintf(string_buff, SUM_STR_MAX, "unknown");
293     }
294     /* Dropped count */
295     if (iface.drops_known) {
296       g_snprintf(string_buff2, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", iface.drops);
297     } else {
298       g_snprintf(string_buff2, SUM_STR_MAX, "unknown");
299     }
300 #ifdef HAVE_LIBPCAP
301     /* Capture filter */
302     if (iface.cfilter && iface.cfilter[0] != '\0') {
303       g_snprintf(string_buff3, SUM_STR_MAX, "%s", iface.cfilter);
304     } else {
305       if (iface.name) {
306         g_snprintf(string_buff3, SUM_STR_MAX, "none");
307       } else {
308         g_snprintf(string_buff3, SUM_STR_MAX, "unknown");
309       }
310     }
311     dl_description = pcap_datalink_val_to_description(iface.linktype);
312     if (dl_description != NULL)
313       g_snprintf(string_buff4, SUM_STR_MAX, "%s", dl_description);
314     else
315       g_snprintf(string_buff4, SUM_STR_MAX, "DLT %d", iface.linktype);
316 #else
317     g_snprintf(string_buff3, SUM_STR_MAX, "unknown");
318     g_snprintf(string_buff4, SUM_STR_MAX, "unknown");
319 #endif
320     g_snprintf(string_buff5, SUM_STR_MAX, "%u bytes", iface.snap);
321     gtk_list_store_append(store, &iter);
322     gtk_list_store_set(store, &iter, 0, string_buff, 1, string_buff2, 2, string_buff3, 3, string_buff4, 4, string_buff5,-1);
323   }
324   gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(store));
325   g_object_unref (store);
326   gtk_container_add(GTK_CONTAINER(scrolled_window), treeview);
327   gtk_container_add(GTK_CONTAINER(main_vb),scrolled_window);
328   gtk_widget_show_all (scrolled_window);
329   table = gtk_table_new(1, 2, FALSE);
330   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
331   gtk_table_set_row_spacings(GTK_TABLE(table), 3);
332   gtk_container_add(GTK_CONTAINER(main_vb), table);
333   row = 0;
334
335
336   /* Data */
337   add_string_to_table(table, &row, "", "");
338   add_string_to_table(table, &row, "Display", "");
339
340   if (summary.dfilter) {
341     /* Display filter */
342     /* limit each row to some reasonable length */
343     str_dup = g_strdup_printf("%s", summary.dfilter);
344     str_work = g_strdup(str_dup);
345     offset = 0;
346     snip = 0;
347     while(strlen(str_work) > FILTER_SNIP_LEN) {
348         str_work[FILTER_SNIP_LEN] = '\0';
349         add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
350         g_free(str_work);
351         offset+=FILTER_SNIP_LEN;
352         str_work = g_strdup(&str_dup[offset]);
353         snip++;
354     }
355
356     add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
357     g_free(str_work);
358     g_free(str_dup);
359   } else {
360     /* Display filter */
361     add_string_to_table(table, &row, "Display filter:", "none");
362   }
363
364   /* Ignored packet count */
365   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.ignored_count);
366   add_string_to_table(table, &row, "Ignored packets:", string_buff);
367
368   /* Traffic */
369   list = simple_list_new(4, titles);
370   gtk_container_add(GTK_CONTAINER(main_vb), list);
371
372   /* Packet count */
373   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
374   if (summary.dfilter) {
375     g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
376   } else {
377     g_strlcpy(string_buff2, string_buff, SUM_STR_MAX);
378   }
379   g_snprintf(string_buff3, SUM_STR_MAX, "%i", summary.marked_count);
380   add_string_to_list(list, "Packets", string_buff, string_buff2, string_buff3);
381
382   /* Time between first and last */
383   if (seconds > 0) {
384     g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
385   } else {
386     string_buff[0] = '\0';
387   }
388   if (summary.dfilter && disp_seconds > 0) {
389     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
390   } else {
391     string_buff2[0] = '\0';
392   }
393   if (summary.marked_count && marked_seconds > 0) {
394     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f sec", marked_seconds);
395   } else {
396     string_buff3[0] = '\0';
397   }
398   add_string_to_list(list, "Between first and last packet", string_buff, string_buff2, string_buff3);
399
400   /* Packets per second */
401   if (seconds > 0) {
402     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
403   } else {
404     string_buff[0] = '\0';
405   }
406   if(summary.dfilter && disp_seconds > 0) {
407     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
408   } else {
409     string_buff2[0] = '\0';
410   }
411   if(summary.marked_count && marked_seconds > 0) {
412     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f", summary.marked_count/marked_seconds);
413   } else {
414     string_buff3[0] = '\0';
415   }
416   add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2, string_buff3);
417
418   /* Packet size */
419   if (summary.packet_count > 1) {
420     g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
421                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
422                (float) ((gint64) summary.bytes)/summary.packet_count);
423   } else {
424     string_buff[0] = '\0';
425   }
426   if (summary.dfilter && summary.filtered_count > 1) {
427     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
428                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
429                (float) ((gint64) summary.filtered_bytes)/summary.filtered_count);
430   } else {
431     string_buff2[0] = '\0';
432   }
433   if (summary.marked_count > 1) {
434     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f bytes",
435                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
436                (float) ((gint64) summary.marked_bytes)/summary.marked_count);
437   } else {
438     string_buff3[0] = '\0';
439   }
440   add_string_to_list(list, "Avg. packet size", string_buff, string_buff2, string_buff3);
441
442   /* Byte count */
443   g_snprintf(string_buff, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.bytes);
444   if (summary.dfilter && summary.filtered_count > 0) {
445     g_snprintf(string_buff2, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.filtered_bytes);
446   } else {
447     string_buff2[0] = '\0';
448   }
449   if (summary.marked_count) {
450     g_snprintf(string_buff3, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.marked_bytes);
451   } else {
452     string_buff3[0] = '\0';
453   }
454   add_string_to_list(list, "Bytes", string_buff, string_buff2, string_buff3);
455
456   /* Bytes per second */
457   if (seconds > 0){
458     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
459     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes)/seconds);
460   } else {
461     string_buff[0] = '\0';
462   }
463   if (summary.dfilter && disp_seconds > 0) {
464     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
465     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", ((gint64) summary.filtered_bytes)/disp_seconds);
466   } else {
467     string_buff2[0] = '\0';
468   }
469   if (summary.marked_count && marked_seconds > 0) {
470     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
471     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f", ((gint64) summary.marked_bytes)/marked_seconds);
472   } else {
473     string_buff3[0] = '\0';
474   }
475   add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2, string_buff3);
476
477   /* MBit per second */
478   if (seconds > 0) {
479     g_snprintf(string_buff, SUM_STR_MAX, "%.3f",
480                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
481                ((gint64) summary.bytes) * 8.0 / (seconds * 1000.0 * 1000.0));
482   } else {
483     string_buff[0] = '\0';
484   }
485   if (summary.dfilter && disp_seconds > 0) {
486     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f",
487                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
488                ((gint64) summary.filtered_bytes) * 8.0 / (disp_seconds * 1000.0 * 1000.0));
489   } else {
490     string_buff2[0] = '\0';
491   }
492   if (summary.marked_count && marked_seconds > 0) {
493     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f",
494                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
495                ((gint64) summary.marked_bytes) * 8.0 / (marked_seconds * 1000.0 * 1000.0));
496   } else {
497     string_buff3[0] = '\0';
498   }
499   add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2, string_buff3);
500
501
502   /* Button row. */
503   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
504   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
505
506   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
507   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
508
509   help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
510   g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_STATS_SUMMARY_DIALOG);
511
512   gtk_widget_grab_focus(close_bt);
513
514   g_signal_connect(sum_open_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
515
516   gtk_widget_show_all(sum_open_w);
517   window_present(sum_open_w);
518 }