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