Cleanup header file define guards.
[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   /* Ignored packet count */
286   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.ignored_count);
287   add_string_to_table(table, &row, "Ignored packets:", string_buff);
288
289   /* Traffic */
290   list = simple_list_new(4, titles);
291   gtk_container_add(GTK_CONTAINER(main_vb), list);
292
293   /* Packet count */
294   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
295   if (summary.dfilter) {
296     g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
297   } else {
298     g_strlcpy(string_buff2, string_buff, SUM_STR_MAX);
299   }
300   g_snprintf(string_buff3, SUM_STR_MAX, "%i", summary.marked_count);
301   add_string_to_list(list, "Packets", string_buff, string_buff2, string_buff3);
302
303   /* Time between first and last */
304   if (seconds > 0) {
305     g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
306   } else {
307     string_buff[0] = '\0';
308   }
309   if (summary.dfilter && disp_seconds > 0) {
310     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
311   } else {
312     string_buff2[0] = '\0';
313   }
314   if (summary.marked_count && marked_seconds > 0) {
315     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f sec", marked_seconds);
316   } else {
317     string_buff3[0] = '\0';
318   }
319   add_string_to_list(list, "Between first and last packet", string_buff, string_buff2, string_buff3);
320
321   /* Packets per second */
322   if (seconds > 0) {
323     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
324   } else {
325     string_buff[0] = '\0';
326   }
327   if(summary.dfilter && disp_seconds > 0) {
328     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
329   } else {
330     string_buff2[0] = '\0';
331   }
332   if(summary.marked_count && marked_seconds > 0) {
333     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f", summary.marked_count/marked_seconds);
334   } else {
335     string_buff3[0] = '\0';
336   }
337   add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2, string_buff3);
338
339   /* Packet size */
340   if (summary.packet_count > 1) {
341     g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
342                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
343                (float) ((gint64) summary.bytes)/summary.packet_count);
344   } else {
345     string_buff[0] = '\0';
346   }
347   if (summary.dfilter && summary.filtered_count > 1) {
348     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
349                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
350                (float) ((gint64) summary.filtered_bytes)/summary.filtered_count);
351   } else {
352     string_buff2[0] = '\0';
353   }
354   if (summary.marked_count > 1) {
355     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f bytes",
356                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
357                (float) ((gint64) summary.marked_bytes)/summary.marked_count);
358   } else {
359     string_buff3[0] = '\0';
360   }
361   add_string_to_list(list, "Avg. packet size", string_buff, string_buff2, string_buff3);
362
363   /* Byte count */
364   g_snprintf(string_buff, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.bytes);
365   if (summary.dfilter && summary.filtered_count > 0) {
366     g_snprintf(string_buff2, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.filtered_bytes);
367   } else {
368     string_buff2[0] = '\0';
369   }
370   if (summary.marked_count) {
371     g_snprintf(string_buff3, SUM_STR_MAX, "%" G_GINT64_MODIFIER "u", summary.marked_bytes);
372   } else {
373     string_buff3[0] = '\0';
374   }
375   add_string_to_list(list, "Bytes", string_buff, string_buff2, string_buff3);
376
377   /* Bytes per second */
378   if (seconds > 0){
379     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
380     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes)/seconds);
381   } else {
382     string_buff[0] = '\0';
383   }
384   if (summary.dfilter && disp_seconds > 0) {
385     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
386     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", ((gint64) summary.filtered_bytes)/disp_seconds);
387   } else {
388     string_buff2[0] = '\0';
389   }
390   if (summary.marked_count && marked_seconds > 0) {
391     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
392     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f", ((gint64) summary.marked_bytes)/marked_seconds);
393   } else {
394     string_buff3[0] = '\0';
395   }
396   add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2, string_buff3);
397
398   /* MBit per second */
399   if (seconds > 0) {
400     g_snprintf(string_buff, SUM_STR_MAX, "%.3f",
401                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
402                ((gint64) summary.bytes) * 8.0 / (seconds * 1000.0 * 1000.0));
403   } else {
404     string_buff[0] = '\0';
405   }
406   if (summary.dfilter && disp_seconds > 0) {
407     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f",
408                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
409                ((gint64) summary.filtered_bytes) * 8.0 / (disp_seconds * 1000.0 * 1000.0));
410   } else {
411     string_buff2[0] = '\0';
412   }
413   if (summary.marked_count && marked_seconds > 0) {
414     g_snprintf(string_buff3, SUM_STR_MAX, "%.3f",
415                /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
416                ((gint64) summary.marked_bytes) * 8.0 / (marked_seconds * 1000.0 * 1000.0));
417   } else {
418     string_buff3[0] = '\0';
419   }
420   add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2, string_buff3);
421
422
423   /* Button row. */
424   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
425   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
426
427   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
428   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
429
430   help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
431   g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_STATS_SUMMARY_DIALOG);
432
433   gtk_widget_grab_focus(close_bt);
434
435   g_signal_connect(sum_open_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
436
437   gtk_widget_show_all(sum_open_w);
438   window_present(sum_open_w);
439 }