68e79585421e1b02370aaaac7f3a05facb93b630
[metze/wireshark/wip.git] / gtk / capture_info_dlg.c
1 /* capture_info_dlg.c
2  * Routines for packet capture info dialog
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_LIBPCAP
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include <time.h>
37
38 #include <gtk/gtk.h>
39
40 #include <epan/packet.h>
41
42 #include "../capture.h"
43 #include "../capture_info.h"
44 #include "../globals.h"
45 #include "../capture_ui_utils.h"
46 #include "../capture-pcap-util.h"
47
48 #include "gtk/dlg_utils.h"
49 #include "gtk/gui_utils.h"
50 #include "gtk/main.h"
51 #include "gtk/help_dlg.h"
52 #include "gtk/stock_icons.h"
53
54 #ifdef HAVE_AIRPCAP
55 #include <airpcap.h>
56 #include "airpcap_loader.h"
57 #include "airpcap_gui_utils.h"
58 #include "airpcap_dlg.h"
59 #endif
60
61
62 /* a single capture counter value (with title, pointer to value and GtkWidgets) */
63 /* as the packet_counts is a struct, not an array, keep a pointer to the */
64 /* corresponding value packet_counts, to speed up (and simplify) output of values */
65 typedef struct {
66     const gchar *title;
67     gint        *value_ptr;
68     GtkWidget   *label, *value_lb, *percent_pb, *percent_lb;
69 } capture_info_counts_t;
70
71 /* all data we need to know of this dialog, after creation finished */
72 typedef struct {
73     GtkWidget               *cap_w;
74     GtkWidget               *running_time_lb;
75     capture_info_counts_t   counts[PACKET_COUNTS_SIZE];
76     guint                   timer_id;
77     time_t                  start_time;
78 } capture_info_ui_t;
79
80
81
82 /* calculate the percentage of the current packet type */
83 static float
84 pct(gint num, gint denom) {
85   if (denom) {
86     return (float) (num * 100.0 / denom);
87   } else {
88     return 0.0;
89   }
90 }
91
92 static gboolean
93 capture_info_delete_cb(GtkWidget *w _U_, GdkEvent *event _U_, gpointer data) {
94   capture_opts *capture_opts = data;
95 #ifdef HAVE_AIRPCAP
96   airpcap_set_toolbar_stop_capture(airpcap_if_active);
97 #endif
98
99   capture_stop(capture_opts);
100   return TRUE;
101 }
102
103 static gint
104 capture_info_ui_update_cb(gpointer data)
105 {
106   capture_info *cinfo = data;
107   capture_info_ui_t *info = cinfo->ui;
108
109   if (!info) /* ...which might happen on slow displays? */
110       return 1;
111
112   cinfo->running_time = time(NULL) - info->start_time;
113   capture_info_ui_update(cinfo);
114   return 1;   /* call the timer again */
115 }
116
117
118 /* create the capture info dialog */
119 /* will keep pointers to the fields in the counts parameter */
120 void capture_info_ui_create(
121 capture_info    *cinfo,
122 capture_opts    *capture_opts)
123 {
124   unsigned int      i;
125   GtkWidget         *main_vb, *stop_bt, *counts_tb;
126   GtkWidget         *counts_fr, *running_tb, *running_label, *bbox, *ci_help;
127   capture_info_ui_t *info;
128   gchar             *cap_w_title;
129   gchar             *title_iface;
130   gchar             *descr;
131   GtkTooltips       *tooltips;
132
133   tooltips = gtk_tooltips_new ();
134
135   info = g_malloc0(sizeof(capture_info_ui_t));
136   info->counts[0].title = "Total";
137   info->counts[0].value_ptr = &(cinfo->counts->total);
138   info->counts[1].title = "SCTP";
139   info->counts[1].value_ptr = &(cinfo->counts->sctp);
140   info->counts[2].title = "TCP";
141   info->counts[2].value_ptr = &(cinfo->counts->tcp);
142   info->counts[3].title = "UDP";
143   info->counts[3].value_ptr = &(cinfo->counts->udp);
144   info->counts[4].title = "ICMP";
145   info->counts[4].value_ptr = &(cinfo->counts->icmp);
146   info->counts[5].title = "ARP";
147   info->counts[5].value_ptr = &(cinfo->counts->arp);
148   info->counts[6].title = "OSPF";
149   info->counts[6].value_ptr = &(cinfo->counts->ospf);
150   info->counts[7].title = "GRE";
151   info->counts[7].value_ptr = &(cinfo->counts->gre);
152   info->counts[8].title = "NetBIOS";
153   info->counts[8].value_ptr = &(cinfo->counts->netbios);
154   info->counts[9].title = "IPX";
155   info->counts[9].value_ptr = &(cinfo->counts->ipx);
156   info->counts[10].title = "VINES";
157   info->counts[10].value_ptr = &(cinfo->counts->vines);
158   info->counts[11].title = "Other";
159   info->counts[11].value_ptr = &(cinfo->counts->other);
160
161   /*
162    * Create the dialog window, with a title that includes the interface.
163    *
164    * If we have a descriptive name for the interface, show that,
165    * rather than its raw name.  On NT 5.x (2K/XP/Server2K3), the
166    * interface name is something like "\Device\NPF_{242423..."
167    * which is pretty useless to the normal user.  On other platforms,
168    * it might be less cryptic, but if a more descriptive name is
169    * available, we should still use that.
170    */
171   descr = get_interface_descriptive_name(capture_opts->iface);
172   title_iface = g_strdup_printf("Wireshark: Capture from %s", descr);
173   g_free(descr);
174   cap_w_title = create_user_window_title(title_iface);
175   g_free(title_iface);
176   info->cap_w = dlg_window_new(cap_w_title);
177   g_free(cap_w_title);
178
179   /* Container for capture display widgets */
180   main_vb = gtk_vbox_new(FALSE, 1);
181   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
182   gtk_container_add(GTK_CONTAINER(info->cap_w), main_vb);
183   gtk_widget_show(main_vb);
184
185   counts_fr = gtk_frame_new("Captured Packets");
186   gtk_box_pack_start(GTK_BOX(main_vb), counts_fr, FALSE, FALSE, 3);
187   gtk_widget_show(counts_fr);
188
189   /* Individual statistic elements */
190   counts_tb = gtk_table_new(PACKET_COUNTS_SIZE, 4, TRUE);
191   gtk_container_add(GTK_CONTAINER(counts_fr), counts_tb);
192   gtk_container_border_width(GTK_CONTAINER(counts_tb), 5);
193   gtk_widget_show(counts_tb);
194
195   gtk_table_set_row_spacings(GTK_TABLE(counts_tb), 0);
196   gtk_table_set_col_spacings(GTK_TABLE(counts_tb), 5);
197
198   for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
199       info->counts[i].label = gtk_label_new(info->counts[i].title);
200       gtk_misc_set_alignment(GTK_MISC(info->counts[i].label), 0.0f, 0.5f);
201
202       info->counts[i].value_lb = gtk_label_new("0");
203       gtk_misc_set_alignment(GTK_MISC(info->counts[i].value_lb), 0.5f, 0.5f);
204
205       if (i == 0) {
206           /* do not build a progress bar for the "total" row */
207           /* (as this could suggest a "buffer full" to the user) */
208           /* simply put a label here */
209           info->counts[i].percent_pb = gtk_label_new("% of total");
210       } else {
211           /* build a progress bar in the other rows */
212           info->counts[i].percent_pb = gtk_progress_bar_new();
213
214           /* downsize the default size of this progress bar in x direction (def:150), */
215           /* otherwise it will become too large and the dialog will look ugly */
216           /* XXX: use a TreeView instead of a table in order to fix this */
217           gtk_widget_set_size_request(info->counts[i].percent_pb, 70, -1);
218       }
219
220       info->counts[i].percent_lb = gtk_label_new("0.0%");
221       gtk_misc_set_alignment(GTK_MISC(info->counts[i].percent_lb), 1.0f, 0.5f);
222
223       gtk_table_attach_defaults(GTK_TABLE(counts_tb),
224                                 info->counts[i].label, 0, 1, i, i + 1);
225       gtk_table_attach_defaults(GTK_TABLE(counts_tb),
226                                 info->counts[i].value_lb, 1, 2, i, i + 1);
227           gtk_table_attach_defaults(GTK_TABLE(counts_tb),
228                                                                 info->counts[i].percent_pb, 2, 3, i, i + 1);
229           gtk_table_attach_defaults(GTK_TABLE(counts_tb),
230                                                                 info->counts[i].percent_lb, 3, 4, i, i + 1);
231
232       gtk_widget_show(info->counts[i].label);
233       gtk_widget_show(info->counts[i].value_lb);
234       gtk_widget_show(info->counts[i].percent_pb);
235       /* don't show percentages for the "total" row */
236       if (i != 0) {
237         gtk_widget_show(info->counts[i].percent_lb);
238       }
239   }
240
241   /* Running time */
242   running_tb = gtk_table_new(1, 4, TRUE);
243   gtk_box_pack_start(GTK_BOX(main_vb), running_tb, FALSE, FALSE, 3);
244   gtk_widget_show(running_tb);
245
246   running_label = gtk_label_new("Running");
247   gtk_misc_set_alignment(GTK_MISC(running_label), 0.0f, 0.0f);
248   gtk_widget_show(running_label);
249   gtk_table_attach_defaults(GTK_TABLE(running_tb),
250                                 running_label, 0, 1, 0, 1);
251
252   info->running_time_lb = gtk_label_new("00:00:00");
253   gtk_misc_set_alignment(GTK_MISC(info->running_time_lb), 0.0f, 0.0f);
254   gtk_widget_show(info->running_time_lb);
255   gtk_table_attach(GTK_TABLE(running_tb),
256                        info->running_time_lb,
257                        1, 2, 0, 1, 0, 0, 5, 0);
258
259   /* allow user to either click a stop button, or the close button on
260         the window to stop a capture in progress. */
261   bbox = dlg_button_row_new(WIRESHARK_STOCK_CAPTURE_STOP, GTK_STOCK_HELP, NULL);
262   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 3);
263   gtk_widget_show(bbox);
264
265   stop_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_CAPTURE_STOP);
266   window_set_cancel_button(info->cap_w, stop_bt, NULL);
267   g_signal_connect(stop_bt, "clicked", G_CALLBACK(capture_info_delete_cb), capture_opts);
268   g_signal_connect(info->cap_w, "delete_event", G_CALLBACK(capture_info_delete_cb), capture_opts);
269
270   ci_help = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
271   gtk_tooltips_set_tip (tooltips, ci_help, ("Get help about this dialog"), NULL);
272   g_signal_connect(ci_help, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_CAPTURE_INFO_DIALOG);
273
274   gtk_widget_show(info->cap_w);
275   window_present(info->cap_w);
276
277   info->start_time = time(NULL);
278
279   cinfo->ui = info;
280
281   /* update the dialog once a second, even if no packets rushing in */
282   info->timer_id = gtk_timeout_add(1000, (GtkFunction)capture_info_ui_update_cb,(gpointer)cinfo);
283 }
284
285
286 /* update the capture info dialog */
287 /* As this function is a bit time critical while capturing, */
288 /* prepare everything possible in the capture_info_ui_create() function above! */
289 void capture_info_ui_update(
290 capture_info    *cinfo)
291 {
292   unsigned int      i;
293   gchar             label_str[64];
294   capture_info_ui_t *info = cinfo->ui;
295
296
297   if (!info) /* ...which might happen on slow displays? */
298     return;
299
300   /* display running time */
301   g_snprintf(label_str, sizeof(label_str), "%02ld:%02ld:%02ld",
302            (long)(cinfo->running_time/3600), (long)((cinfo->running_time%3600)/60),
303            (long)(cinfo->running_time%60));
304   gtk_label_set(GTK_LABEL(info->running_time_lb), label_str);
305
306   /* if we have new packets, update all rows */
307   if (cinfo->new_packets) {
308
309     for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
310         g_snprintf(label_str, sizeof(label_str), "%d",
311                  *info->counts[i].value_ptr);
312         gtk_label_set(GTK_LABEL(info->counts[i].value_lb), label_str);
313
314         /* don't try to update the "total" row progress bar */
315         if (i != 0) {
316             gtk_progress_bar_update(GTK_PROGRESS_BAR(info->counts[i].percent_pb),
317                      (gfloat) (pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr) / 100.0));
318         }
319
320         g_snprintf(label_str, sizeof(label_str), "%.1f%%",
321                  pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr));
322
323         gtk_label_set(GTK_LABEL(info->counts[i].percent_lb), label_str);
324     }
325   }
326 }
327
328
329 /* destroy the capture info dialog again */
330 void capture_info_ui_destroy(
331 capture_info    *cinfo)
332 {
333   capture_info_ui_t *info = cinfo->ui;
334
335   if (!info) /* ...which probably shouldn't happen */
336       return;
337
338   gtk_timeout_remove(info->timer_id);
339
340   /* called from capture engine, so it's ok to destroy the dialog here */
341   gtk_grab_remove(GTK_WIDGET(info->cap_w));
342   window_destroy(GTK_WIDGET(info->cap_w));
343   g_free(info);
344   cinfo->ui = NULL;
345 }
346
347
348 #endif /* HAVE_LIBPCAP */