1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/* capture_info_dlg.c
* Routines for packet capture info dialog
*
* $Id: capture_info_dlg.c 43009 2012-06-02 23:53:04Z etxrab $
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_LIBPCAP
#include <string.h>
#include <time.h>
#include <gtk/gtk.h>
#include <epan/packet.h>
#include "../capture.h"
#include "../capture_info.h"
#include "../capture_ui_utils.h"
#include "../capture-pcap-util.h"
#include "ui/gtk/dlg_utils.h"
#include "ui/gtk/gui_utils.h"
#include "ui/gtk/main.h"
#include "ui/gtk/help_dlg.h"
#include "ui/gtk/stock_icons.h"
#ifdef HAVE_AIRPCAP
#include <airpcap.h>
#include "airpcap_loader.h"
#include "airpcap_gui_utils.h"
#include "airpcap_dlg.h"
#endif
/* a single capture counter value (with title, pointer to value and GtkWidgets) */
/* as the packet_counts is a struct, not an array, keep a pointer to the */
/* corresponding value packet_counts, to speed up (and simplify) output of values */
typedef struct {
const gchar *title;
gint *value_ptr;
GtkWidget *label, *value_lb, *percent_pb, *percent_lb;
} capture_info_counts_t;
/* all data we need to know of this dialog, after creation finished */
typedef struct {
GtkWidget *cap_w;
GtkWidget *running_time_lb;
capture_info_counts_t counts[PACKET_COUNTS_SIZE];
guint timer_id;
time_t start_time;
} capture_info_ui_t;
/* Workhorse for stopping capture */
static void
capture_info_stop(capture_options *capture_opts)
{
#ifdef HAVE_AIRPCAP
airpcap_set_toolbar_stop_capture(airpcap_if_active);
#endif
capture_stop(capture_opts);
}
/* "delete-event" signal callback. Note different signature than "clicked" signal callback */
static gboolean
capture_info_delete_cb(GtkWidget *w _U_, GdkEvent *event _U_, gpointer data) {
capture_info_stop(data);
return TRUE;
}
/* "clicked" signal callback */
static void
capture_info_stop_clicked_cb(GtkButton *w _U_, gpointer data) {
capture_info_stop(data);
}
static gboolean
capture_info_ui_update_cb(gpointer data)
{
capture_info *cinfo = data;
capture_info_ui_t *info = cinfo->ui;
if (!info) /* ...which might happen on slow displays? */
return TRUE;
cinfo->running_time = time(NULL) - info->start_time;
capture_info_ui_update(cinfo);
return TRUE; /* call the timer again */
}
/* create the capture info dialog */
/* will keep pointers to the fields in the counts parameter */
void capture_info_ui_create(
capture_info *cinfo,
capture_options *capture_opts)
{
unsigned int i;
GtkWidget *main_vb, *stop_bt, *counts_tb;
GtkWidget *counts_fr, *running_tb, *running_label, *bbox, *ci_help;
capture_info_ui_t *info;
gchar *cap_w_title;
gchar *title_iface;
gchar *descr;
GString *str;
info = g_malloc0(sizeof(capture_info_ui_t));
info->counts[0].title = "Total";
info->counts[0].value_ptr = &(cinfo->counts->total);
info->counts[1].title = "SCTP";
info->counts[1].value_ptr = &(cinfo->counts->sctp);
info->counts[2].title = "TCP";
info->counts[2].value_ptr = &(cinfo->counts->tcp);
info->counts[3].title = "UDP";
info->counts[3].value_ptr = &(cinfo->counts->udp);
info->counts[4].title = "ICMP";
info->counts[4].value_ptr = &(cinfo->counts->icmp);
info->counts[5].title = "ARP";
info->counts[5].value_ptr = &(cinfo->counts->arp);
info->counts[6].title = "OSPF";
info->counts[6].value_ptr = &(cinfo->counts->ospf);
info->counts[7].title = "GRE";
info->counts[7].value_ptr = &(cinfo->counts->gre);
info->counts[8].title = "NetBIOS";
info->counts[8].value_ptr = &(cinfo->counts->netbios);
info->counts[9].title = "IPX";
info->counts[9].value_ptr = &(cinfo->counts->ipx);
info->counts[10].title = "VINES";
info->counts[10].value_ptr = &(cinfo->counts->vines);
info->counts[11].title = "Other";
info->counts[11].value_ptr = &(cinfo->counts->other);
info->counts[12].title = "I2C Events";
info->counts[12].value_ptr = &(cinfo->counts->i2c_event);
info->counts[13].title = "I2C Data";
info->counts[13].value_ptr = &(cinfo->counts->i2c_data);
/*
* Create the dialog window, with a title that includes the interface.
*
* If we have a descriptive name for the interface, show that,
* rather than its raw name. On NT 5.x (2K/XP/Server2K3), the
* interface name is something like "\Device\NPF_{242423..."
* which is pretty useless to the normal user. On other platforms,
* it might be less cryptic, but if a more descriptive name is
* available, we should still use that.
*/
str = g_string_new("");
#ifdef _WIN32
if (capture_opts->ifaces->len < 2) {
#else
if (capture_opts->ifaces->len < 4) {
#endif
for (i = 0; i < capture_opts->ifaces->len; i++) {
interface_options interface_opts;
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
descr = get_interface_descriptive_name(interface_opts.name);
if (i > 0) {
if (capture_opts->ifaces->len > 2) {
g_string_append_printf(str, ",");
}
g_string_append_printf(str, " ");
if (i == capture_opts->ifaces->len - 1) {
g_string_append_printf(str, "and ");
}
}
g_string_append_printf(str, "%s", descr);
g_free(descr);
}
} else {
g_string_append_printf(str, "%u interfaces", capture_opts->ifaces->len);
}
title_iface = g_strdup_printf("Wireshark: Capture from %s", str->str);
g_string_free(str, TRUE);
cap_w_title = create_user_window_title(title_iface);
g_free(title_iface);
info->cap_w = dlg_window_new(cap_w_title);
g_free(cap_w_title);
/* Container for capture display widgets */
main_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 1, FALSE);
gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
gtk_container_add(GTK_CONTAINER(info->cap_w), main_vb);
gtk_widget_show(main_vb);
counts_fr = gtk_frame_new("Captured Packets");
gtk_box_pack_start(GTK_BOX(main_vb), counts_fr, FALSE, FALSE, 3);
gtk_widget_show(counts_fr);
/* Individual statistic elements */
counts_tb = gtk_table_new(PACKET_COUNTS_SIZE, 4, TRUE);
gtk_container_add(GTK_CONTAINER(counts_fr), counts_tb);
gtk_container_set_border_width(GTK_CONTAINER(counts_tb), 5);
gtk_widget_show(counts_tb);
gtk_table_set_row_spacings(GTK_TABLE(counts_tb), 0);
gtk_table_set_col_spacings(GTK_TABLE(counts_tb), 5);
for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
info->counts[i].label = gtk_label_new(info->counts[i].title);
gtk_misc_set_alignment(GTK_MISC(info->counts[i].label), 0.0f, 0.5f);
info->counts[i].value_lb = gtk_label_new("0");
gtk_misc_set_alignment(GTK_MISC(info->counts[i].value_lb), 0.5f, 0.5f);
if (i == 0) {
/* do not build a progress bar for the "total" row */
/* (as this could suggest a "buffer full" to the user) */
/* simply put a label here */
info->counts[i].percent_pb = gtk_label_new("% of total");
} else {
/* build a progress bar in the other rows */
info->counts[i].percent_pb = gtk_progress_bar_new();
/* downsize the default size of this progress bar in x direction (def:150), */
/* otherwise it will become too large and the dialog will look ugly */
/* XXX: use a TreeView instead of a table in order to fix this */
gtk_widget_set_size_request(info->counts[i].percent_pb, 70, -1);
}
info->counts[i].percent_lb = gtk_label_new("0.0%");
gtk_misc_set_alignment(GTK_MISC(info->counts[i].percent_lb), 1.0f, 0.5f);
gtk_table_attach_defaults(GTK_TABLE(counts_tb),
info->counts[i].label, 0, 1, i, i + 1);
gtk_table_attach_defaults(GTK_TABLE(counts_tb),
info->counts[i].value_lb, 1, 2, i, i + 1);
gtk_table_attach_defaults(GTK_TABLE(counts_tb),
info->counts[i].percent_pb, 2, 3, i, i + 1);
gtk_table_attach_defaults(GTK_TABLE(counts_tb),
info->counts[i].percent_lb, 3, 4, i, i + 1);
gtk_widget_show(info->counts[i].label);
gtk_widget_show(info->counts[i].value_lb);
gtk_widget_show(info->counts[i].percent_pb);
/* don't show percentages for the "total" row */
if (i != 0) {
gtk_widget_show(info->counts[i].percent_lb);
}
}
/* Running time */
running_tb = gtk_table_new(1, 4, TRUE);
gtk_box_pack_start(GTK_BOX(main_vb), running_tb, FALSE, FALSE, 3);
gtk_widget_show(running_tb);
running_label = gtk_label_new("Running");
gtk_misc_set_alignment(GTK_MISC(running_label), 0.0f, 0.0f);
gtk_widget_show(running_label);
gtk_table_attach_defaults(GTK_TABLE(running_tb),
running_label, 0, 1, 0, 1);
info->running_time_lb = gtk_label_new("00:00:00");
gtk_misc_set_alignment(GTK_MISC(info->running_time_lb), 0.0f, 0.0f);
gtk_widget_show(info->running_time_lb);
gtk_table_attach(GTK_TABLE(running_tb),
info->running_time_lb,
1, 2, 0, 1, 0, 0, 5, 0);
/* allow user to either click a stop button, or the close button on
the window to stop a capture in progress. */
bbox = dlg_button_row_new(WIRESHARK_STOCK_CAPTURE_STOP, GTK_STOCK_HELP, NULL);
gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 3);
gtk_widget_show(bbox);
stop_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_CAPTURE_STOP);
window_set_cancel_button(info->cap_w, stop_bt, NULL);
g_signal_connect(stop_bt, "clicked", G_CALLBACK(capture_info_stop_clicked_cb), capture_opts);
g_signal_connect(info->cap_w, "delete_event", G_CALLBACK(capture_info_delete_cb), capture_opts);
ci_help = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
gtk_widget_set_tooltip_text(ci_help, "Get help about this dialog");
g_signal_connect(ci_help, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_CAPTURE_INFO_DIALOG);
gtk_widget_show(info->cap_w);
window_present(info->cap_w);
info->start_time = time(NULL);
cinfo->ui = info;
/* update the dialog once a second, even if no packets rushing in */
info->timer_id = g_timeout_add(1000, capture_info_ui_update_cb,cinfo);
}
/* update the capture info dialog */
/* As this function is a bit time critical while capturing, */
/* prepare everything possible in the capture_info_ui_create() function above! */
void capture_info_ui_update(
capture_info *cinfo)
{
unsigned int i;
gchar label_str[64];
capture_info_ui_t *info = cinfo->ui;
if (!info) /* ...which might happen on slow displays? */
return;
/* display running time */
g_snprintf(label_str, sizeof(label_str), "%02ld:%02ld:%02ld",
(long)(cinfo->running_time/3600), (long)((cinfo->running_time%3600)/60),
(long)(cinfo->running_time%60));
gtk_label_set_text(GTK_LABEL(info->running_time_lb), label_str);
/* if we have new packets, update all rows */
if (cinfo->new_packets) {
float pb_frac;
for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
g_snprintf(label_str, sizeof(label_str), "%d", *info->counts[i].value_ptr);
gtk_label_set_text(GTK_LABEL(info->counts[i].value_lb), label_str);
pb_frac = (*info->counts[0].value_ptr != 0) ?
((float)*info->counts[i].value_ptr / *info->counts[0].value_ptr) : 0.0f;
/* don't try to update the "total" row progress bar */
if (i != 0) {
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(info->counts[i].percent_pb), pb_frac);
g_snprintf(label_str, sizeof(label_str), "%.1f%%", pb_frac * 100.0);
gtk_label_set_text(GTK_LABEL(info->counts[i].percent_lb), label_str);
}
}
}
}
/* destroy the capture info dialog again */
void capture_info_ui_destroy(
capture_info *cinfo)
{
capture_info_ui_t *info = cinfo->ui;
if (!info) /* ...which probably shouldn't happen */
return;
g_source_remove(info->timer_id);
/* called from capture engine, so it's ok to destroy the dialog here */
gtk_grab_remove(GTK_WIDGET(info->cap_w));
window_destroy(GTK_WIDGET(info->cap_w));
g_free(info);
cinfo->ui = NULL;
}
#endif /* HAVE_LIBPCAP */
|