2 * Routines for packet capture info dialog
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include "gtk/compat_macros.h"
38 #include <epan/packet.h>
40 #include "capture_info.h"
42 #include "capture_ui_utils.h"
43 #include "dlg_utils.h"
44 #include "gui_utils.h"
46 #include "capture-pcap-util.h"
50 #include "airpcap_loader.h"
51 #include "airpcap_gui_utils.h"
52 #include "airpcap_dlg.h"
56 /* a single capture counter value (with title, pointer to value and GtkWidgets) */
57 /* as the packet_counts is a struct, not an array, keep a pointer to the */
58 /* corresponding value packet_counts, to speed up (and simplify) output of values */
62 GtkWidget *label, *value_lb, *percent_pb, *percent_lb;
63 } capture_info_counts_t;
65 /* all data we need to know of this dialog, after creation finished */
68 GtkWidget *running_time_lb;
69 capture_info_counts_t counts[PACKET_COUNTS_SIZE];
76 /* calculate the percentage of the current packet type */
78 pct(gint num, gint denom) {
80 return (float) (num * 100.0 / denom);
87 capture_info_delete_cb(GtkWidget *w _U_, GdkEvent *event _U_, gpointer data _U_) {
89 airpcap_set_toolbar_stop_capture(airpcap_if_active);
92 capture_stop(capture_opts);
97 capture_info_ui_update_cb(gpointer data)
99 capture_info *cinfo = data;
100 capture_info_ui_t *info = cinfo->ui;
102 cinfo->running_time = time(NULL) - info->start_time;
103 capture_info_ui_update(cinfo);
104 return 1; /* call the timer again */
108 /* create the capture info dialog */
109 /* will keep pointers to the fields in the counts parameter */
110 void capture_info_ui_create(
115 GtkWidget *main_vb, *stop_bt, *counts_tb;
116 GtkWidget *counts_fr, *running_tb, *running_label, *bbox;
117 capture_info_ui_t *info;
122 info = g_malloc0(sizeof(capture_info_ui_t));
123 info->counts[0].title = "Total";
124 info->counts[0].value_ptr = &(cinfo->counts->total);
125 info->counts[1].title = "SCTP";
126 info->counts[1].value_ptr = &(cinfo->counts->sctp);
127 info->counts[2].title = "TCP";
128 info->counts[2].value_ptr = &(cinfo->counts->tcp);
129 info->counts[3].title = "UDP";
130 info->counts[3].value_ptr = &(cinfo->counts->udp);
131 info->counts[4].title = "ICMP";
132 info->counts[4].value_ptr = &(cinfo->counts->icmp);
133 info->counts[5].title = "ARP";
134 info->counts[5].value_ptr = &(cinfo->counts->arp);
135 info->counts[6].title = "OSPF";
136 info->counts[6].value_ptr = &(cinfo->counts->ospf);
137 info->counts[7].title = "GRE";
138 info->counts[7].value_ptr = &(cinfo->counts->gre);
139 info->counts[8].title = "NetBIOS";
140 info->counts[8].value_ptr = &(cinfo->counts->netbios);
141 info->counts[9].title = "IPX";
142 info->counts[9].value_ptr = &(cinfo->counts->ipx);
143 info->counts[10].title = "VINES";
144 info->counts[10].value_ptr = &(cinfo->counts->vines);
145 info->counts[11].title = "Other";
146 info->counts[11].value_ptr = &(cinfo->counts->other);
149 * Create the dialog window, with a title that includes the interface.
151 * If we have a descriptive name for the interface, show that,
152 * rather than its raw name. On NT 5.x (2K/XP/Server2K3), the
153 * interface name is something like "\Device\NPF_{242423..."
154 * which is pretty useless to the normal user. On other platforms,
155 * it might be less cryptic, but if a more descriptive name is
156 * available, we should still use that.
158 descr = get_interface_descriptive_name(iface);
159 title_iface = g_strdup_printf("Wireshark: Capture from %s", descr);
161 cap_w_title = create_user_window_title(title_iface);
163 info->cap_w = dlg_window_new(cap_w_title);
166 /* Container for capture display widgets */
167 main_vb = gtk_vbox_new(FALSE, 1);
168 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
169 gtk_container_add(GTK_CONTAINER(info->cap_w), main_vb);
170 gtk_widget_show(main_vb);
172 counts_fr = gtk_frame_new("Captured Packets");
173 gtk_box_pack_start(GTK_BOX(main_vb), counts_fr, FALSE, FALSE, 3);
174 gtk_widget_show(counts_fr);
176 /* Individual statistic elements */
177 counts_tb = gtk_table_new(PACKET_COUNTS_SIZE, 4, TRUE);
178 gtk_container_add(GTK_CONTAINER(counts_fr), counts_tb);
179 gtk_container_border_width(GTK_CONTAINER(counts_tb), 5);
180 gtk_widget_show(counts_tb);
182 gtk_table_set_row_spacings(GTK_TABLE(counts_tb), 0);
183 gtk_table_set_col_spacings(GTK_TABLE(counts_tb), 5);
185 for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
186 info->counts[i].label = gtk_label_new(info->counts[i].title);
187 gtk_misc_set_alignment(GTK_MISC(info->counts[i].label), 0.0f, 0.5f);
189 info->counts[i].value_lb = gtk_label_new("0");
190 gtk_misc_set_alignment(GTK_MISC(info->counts[i].value_lb), 0.5f, 0.5f);
193 /* do not build a progress bar for the "total" row */
194 /* (as this could suggest a "buffer full" to the user) */
195 /* simply put a label here */
196 info->counts[i].percent_pb = gtk_label_new("% of total");
198 /* build a progress bar in the other rows */
199 info->counts[i].percent_pb = gtk_progress_bar_new();
201 /* downsize the default size of this progress bar in x direction (def:150), */
202 /* otherwise it will become too large and the dialog will look ugly */
203 /* XXX: use a TreeView instead of a table in order to fix this */
204 WIDGET_SET_SIZE(info->counts[i].percent_pb, 70, -1);
207 info->counts[i].percent_lb = gtk_label_new("0.0%");
208 gtk_misc_set_alignment(GTK_MISC(info->counts[i].percent_lb), 1.0f, 0.5f);
210 gtk_table_attach_defaults(GTK_TABLE(counts_tb),
211 info->counts[i].label, 0, 1, i, i + 1);
212 gtk_table_attach_defaults(GTK_TABLE(counts_tb),
213 info->counts[i].value_lb, 1, 2, i, i + 1);
214 gtk_table_attach_defaults(GTK_TABLE(counts_tb),
215 info->counts[i].percent_pb, 2, 3, i, i + 1);
216 gtk_table_attach_defaults(GTK_TABLE(counts_tb),
217 info->counts[i].percent_lb, 3, 4, i, i + 1);
219 gtk_widget_show(info->counts[i].label);
220 gtk_widget_show(info->counts[i].value_lb);
221 gtk_widget_show(info->counts[i].percent_pb);
222 /* don't show percentages for the "total" row */
224 gtk_widget_show(info->counts[i].percent_lb);
229 running_tb = gtk_table_new(1, 4, TRUE);
230 gtk_box_pack_start(GTK_BOX(main_vb), running_tb, FALSE, FALSE, 3);
231 gtk_widget_show(running_tb);
233 running_label = gtk_label_new("Running");
234 gtk_misc_set_alignment(GTK_MISC(running_label), 0.0f, 0.0f);
235 gtk_widget_show(running_label);
236 gtk_table_attach_defaults(GTK_TABLE(running_tb),
237 running_label, 0, 1, 0, 1);
239 info->running_time_lb = gtk_label_new("00:00:00");
240 gtk_misc_set_alignment(GTK_MISC(info->running_time_lb), 0.0f, 0.0f);
241 gtk_widget_show(info->running_time_lb);
242 gtk_table_attach(GTK_TABLE(running_tb),
243 info->running_time_lb,
244 1, 2, 0, 1, 0, 0, 5, 0);
246 /* allow user to either click a stop button, or the close button on
247 the window to stop a capture in progress. */
248 bbox = dlg_button_row_new(GTK_STOCK_STOP, NULL);
249 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 3);
250 gtk_widget_show(bbox);
252 stop_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_STOP);
253 window_set_cancel_button(info->cap_w, stop_bt, NULL);
254 SIGNAL_CONNECT(stop_bt, "clicked", capture_info_delete_cb, capture_opts);
256 SIGNAL_CONNECT(info->cap_w, "delete_event", capture_info_delete_cb,
259 gtk_widget_show(info->cap_w);
260 window_present(info->cap_w);
262 info->start_time = time(NULL);
266 /* update the dialog once a second, even if no packets rushing in */
267 info->timer_id = gtk_timeout_add(1000, (GtkFunction)capture_info_ui_update_cb,(gpointer)cinfo);
271 /* update the capture info dialog */
272 /* As this function is a bit time critical while capturing, */
273 /* prepare everything possible in the capture_info_ui_create() function above! */
274 void capture_info_ui_update(
279 capture_info_ui_t *info = cinfo->ui;
282 /* display running time */
283 g_snprintf(label_str, sizeof(label_str), "%02ld:%02ld:%02ld",
284 (long)(cinfo->running_time/3600), (long)((cinfo->running_time%3600)/60),
285 (long)(cinfo->running_time%60));
286 gtk_label_set(GTK_LABEL(info->running_time_lb), label_str);
288 /* if we have new packets, update all rows */
289 if (cinfo->new_packets) {
291 for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
292 g_snprintf(label_str, sizeof(label_str), "%d",
293 *info->counts[i].value_ptr);
294 gtk_label_set(GTK_LABEL(info->counts[i].value_lb), label_str);
296 /* don't try to update the "total" row progress bar */
298 gtk_progress_bar_update(GTK_PROGRESS_BAR(info->counts[i].percent_pb),
299 (gfloat) (pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr) / 100.0));
302 g_snprintf(label_str, sizeof(label_str), "%.1f%%",
303 pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr));
305 gtk_label_set(GTK_LABEL(info->counts[i].percent_lb), label_str);
311 /* destroy the capture info dialog again */
312 void capture_info_ui_destroy(
315 capture_info_ui_t *info = cinfo->ui;
317 gtk_timeout_remove(info->timer_id);
319 /* called from capture engine, so it's ok to destroy the dialog here */
320 gtk_grab_remove(GTK_WIDGET(info->cap_w));
321 window_destroy(GTK_WIDGET(info->cap_w));
326 #endif /* HAVE_LIBPCAP */