Add support for AirPcap, an upcoming wireless product from CACE. Support
[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  * 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 #include <string.h>
30
31 #ifdef HAVE_LIBPCAP
32
33 #include <gtk/gtk.h>
34 #include "gtk/compat_macros.h"
35
36 #include <time.h>
37
38 #include <epan/packet.h>
39 #include "capture.h"
40 #include "capture_info.h"
41 #include "globals.h"
42 #include "capture_ui_utils.h"
43 #include "dlg_utils.h"
44 #include "gui_utils.h"
45 #include "main.h"
46 #include "capture-pcap-util.h"
47
48 #ifdef HAVE_AIRPCAP
49 #include <airpcap.h>
50 #include "airpcap_loader.h"
51 #include "airpcap_gui_utils.h"
52 #include "airpcap_dlg.h"
53 #endif
54
55
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 */
59 typedef struct {
60     const gchar *title;
61     gint        *value_ptr;
62     GtkWidget   *label, *value_lb, *percent_pb, *percent_lb;
63 } capture_info_counts_t;
64
65 /* all data we need to know of this dialog, after creation finished */
66 typedef struct {
67     GtkWidget               *cap_w;
68     GtkWidget               *running_time_lb;
69     capture_info_counts_t   counts[PACKET_COUNTS_SIZE];
70     guint                   timer_id;
71     time_t                  start_time;
72 } capture_info_ui_t;
73
74
75
76 /* calculate the percentage of the current packet type */
77 static float
78 pct(gint num, gint denom) {
79   if (denom) {
80     return (float) (num * 100.0 / denom);
81   } else {
82     return 0.0;
83   }
84 }
85
86 static gboolean
87 capture_info_delete_cb(GtkWidget *w _U_, GdkEvent *event _U_, gpointer data _U_) {
88 #ifdef HAVE_AIRPCAP
89   airpcap_set_toolbar_stop_capture(airpcap_if_active);
90 #endif
91
92   capture_stop(capture_opts);
93   return TRUE;
94 }
95
96 static gint
97 capture_info_ui_update_cb(gpointer data)
98 {
99   capture_info *cinfo = data;
100   capture_info_ui_t *info = cinfo->ui;
101
102   cinfo->running_time = time(NULL) - info->start_time;
103   capture_info_ui_update(cinfo);
104   return 1;   /* call the timer again */
105 }
106
107
108 /* create the capture info dialog */
109 /* will keep pointers to the fields in the counts parameter */
110 void capture_info_ui_create(
111 capture_info    *cinfo,
112 const gchar     *iface)
113 {
114   unsigned int      i;
115   GtkWidget         *main_vb, *stop_bt, *counts_tb;
116   GtkWidget         *counts_fr, *running_tb, *running_label, *bbox;
117   capture_info_ui_t *info;
118   gchar             *cap_w_title;
119   gchar             *title_iface;
120   gchar             *descr;
121
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);
147
148   /*
149    * Create the dialog window, with a title that includes the interface.
150    *
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.
157    */
158   descr = get_interface_descriptive_name(iface);
159   title_iface = g_strdup_printf("Wireshark: Capture from %s", descr);
160   g_free(descr);
161   cap_w_title = create_user_window_title(title_iface);
162   g_free(title_iface);
163   info->cap_w = dlg_window_new(cap_w_title);
164   g_free(cap_w_title);
165
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);
171
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);
175
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);
181
182   gtk_table_set_row_spacings(GTK_TABLE(counts_tb), 0);
183   gtk_table_set_col_spacings(GTK_TABLE(counts_tb), 5);
184
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);
188
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);
191
192       if (i == 0) {
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");
197       } else {
198           /* build a progress bar in the other rows */
199           info->counts[i].percent_pb = gtk_progress_bar_new();
200
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);
205       }
206
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);
209
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);
218
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 */
223       if (i != 0) {
224         gtk_widget_show(info->counts[i].percent_lb);
225       }
226   }
227
228   /* Running time */
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);
232
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);
238
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);
245
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);
251
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);
255
256   SIGNAL_CONNECT(info->cap_w, "delete_event", capture_info_delete_cb,
257                  capture_opts);
258
259   gtk_widget_show(info->cap_w);
260   window_present(info->cap_w);
261
262   info->start_time = time(NULL);
263
264   cinfo->ui = info;
265
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);
268 }
269
270
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(
275 capture_info    *cinfo)
276 {
277   unsigned int      i;
278   gchar             label_str[64];
279   capture_info_ui_t *info = cinfo->ui;
280
281
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);
287
288   /* if we have new packets, update all rows */
289   if (cinfo->new_packets) {
290
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);
295
296         /* don't try to update the "total" row progress bar */
297         if (i != 0) {
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));
300         }
301
302         g_snprintf(label_str, sizeof(label_str), "%.1f%%",
303                  pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr));
304
305         gtk_label_set(GTK_LABEL(info->counts[i].percent_lb), label_str);
306     }
307   }
308 }
309
310
311 /* destroy the capture info dialog again */
312 void capture_info_ui_destroy(
313 capture_info    *cinfo)
314 {
315   capture_info_ui_t *info = cinfo->ui;
316
317   gtk_timeout_remove(info->timer_id);
318
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));
322   g_free(info);
323 }
324
325
326 #endif /* HAVE_LIBPCAP */