2 * Routines for the capture interface 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.
29 #ifdef HAVE_SYS_WAIT_H
30 # include <sys/wait.h>
38 #include <epan/prefs.h>
40 #include "../globals.h"
41 #include "../capture-pcap-util.h"
42 #include "../simple_dialog.h"
43 #include "../capture.h"
44 #include "../capture_errs.h"
45 #include "../capture_ui_utils.h"
46 #include <wiretap/wtap.h>
49 #include "../capture-wpcap.h"
50 #include "gtk/capture_if_details_dlg_win32.h"
53 #include "gtk/stock_icons.h"
54 #include "gtk/capture_dlg.h"
55 #include "gtk/recent.h"
56 #include "gtk/gui_utils.h"
57 #include "gtk/dlg_utils.h"
59 #include "gtk/main_toolbar.h"
60 #include "gtk/help_dlg.h"
62 #include "gtk/webbrowser.h"
63 #include "gtk/capture_globals.h"
66 #include "../image/toolbar/capture_airpcap_16.xpm"
68 #include "../image/toolbar/capture_ethernet_16.xpm"
70 /* new buttons to be used instead of labels for 'Capture','Prepare',' */
71 #include "../image/toolbar/capture_capture_16.xpm"
72 #include "../image/toolbar/capture_prepare_16.xpm"
73 #include "../image/toolbar/capture_details_16.xpm"
78 #include "airpcap_loader.h"
79 #include "airpcap_gui_utils.h"
80 #include "airpcap_dlg.h"
84 * Keep a static pointer to the current "Capture Interfaces" window, if
85 * any, so that if somebody tries to do "Capture:Start" while there's
86 * already a "Capture Interfaces" window up, we just pop up the existing
87 * one, rather than creating a new one.
89 static GtkWidget *cap_if_w;
91 static GtkWidget *cap_air_w;
94 GList *if_data = NULL;
103 * Timeout, in milliseconds, for reads from the stream of captured packets.
105 #define CAP_READ_TIMEOUT 250
108 /* the "runtime" data of one interface */
109 typedef struct if_dlg_data_s {
110 GtkWidget *device_lb;
115 GtkWidget *capture_bt;
116 GtkWidget *prepare_bt;
118 GtkWidget *details_bt;
120 guint32 last_packets;
126 /* start capture button was pressed */
128 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
130 if_dlg_data_t *if_dlg_data = if_data;
133 airpcap_if_active = get_airpcap_if_from_name(airpcap_if_list, if_dlg_data->if_info.name);
134 airpcap_if_selected = airpcap_if_active;
137 if (global_capture_opts.iface)
138 g_free(global_capture_opts.iface);
139 if (global_capture_opts.iface_descr)
140 g_free(global_capture_opts.iface_descr);
142 global_capture_opts.iface = g_strdup(if_dlg_data->device);
143 global_capture_opts.iface_descr = get_interface_descriptive_name(global_capture_opts.iface);
145 /* XXX - remove this? */
146 if (global_capture_opts.save_file) {
147 g_free(global_capture_opts.save_file);
148 global_capture_opts.save_file = NULL;
151 /* stop capturing from all interfaces, we are going to do real work now ... */
152 window_destroy(cap_if_w);
154 capture_start_cb(NULL, NULL);
158 /* prepare capture button was pressed */
160 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
162 if_dlg_data_t *if_dlg_data = if_data;
164 if (global_capture_opts.iface)
165 g_free(global_capture_opts.iface);
166 if (global_capture_opts.iface_descr)
167 g_free(global_capture_opts.iface_descr);
169 global_capture_opts.iface = g_strdup(if_dlg_data->device);
170 global_capture_opts.iface_descr = get_interface_descriptive_name(global_capture_opts.iface);
172 /* stop capturing from all interfaces, we are going to do real work now ... */
173 window_destroy(cap_if_w);
175 capture_prep_cb(NULL, NULL);
180 /* capture details button was pressed */
182 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
184 if_dlg_data_t *if_dlg_data = if_data;
187 capture_if_details_open(if_dlg_data->device);
191 /* update a single interface */
193 update_if(if_dlg_data_t *if_dlg_data, if_stat_cache_t *sc)
195 struct pcap_stat stats;
201 * Note that some versions of libpcap, on some versions of UN*X,
202 * pcap_stats() returns the number of packets since the last
205 * That's a bug, and should be fixed; "pcap_stats()" is supposed
206 * to work the same way on all platforms.
209 if(capture_stats(sc, if_dlg_data->device, &stats)) {
210 diff = stats.ps_recv - if_dlg_data->last_packets;
211 if_dlg_data->last_packets = stats.ps_recv;
213 str = g_strdup_printf("%u", if_dlg_data->last_packets);
214 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
216 str = g_strdup_printf("%u", diff);
217 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
220 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
221 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
223 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
224 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
229 /* update all interfaces */
231 update_all(gpointer data)
235 if_stat_cache_t *sc = data;
241 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
242 update_if(curr->data, sc);
248 gboolean g_capture_in_progress = FALSE;
250 /* a live capture has started or stopped */
252 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
257 g_capture_in_progress = capture_in_progress;
260 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
262 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
263 if_dlg_data_t *if_dlg_data = curr->data;
265 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
266 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
272 /* the window was closed, cleanup things */
274 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data)
278 if_stat_cache_t *sc = user_data;
280 gtk_timeout_remove(timer_id);
282 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
288 free_interface_list(if_list);
290 /* Note that we no longer have a "Capture Options" dialog box. */
293 capture_stat_stop(sc);
296 airpcap_set_toolbar_stop_capture(airpcap_if_active);
302 combo_channel_new(void)
304 GtkWidget* channel_cb;
308 channel_cb = gtk_combo_new();
309 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(channel_cb)->entry), "1");
313 popdown = g_list_append(popdown, "1");
314 popdown = g_list_append(popdown, "2");
315 popdown = g_list_append(popdown, "3");
316 popdown = g_list_append(popdown, "4");
317 popdown = g_list_append(popdown, "5");
318 popdown = g_list_append(popdown, "6");
319 popdown = g_list_append(popdown, "7");
320 popdown = g_list_append(popdown, "8");
321 popdown = g_list_append(popdown, "9");
322 popdown = g_list_append(popdown, "10");
323 popdown = g_list_append(popdown, "11");
324 popdown = g_list_append(popdown, "12");
325 popdown = g_list_append(popdown, "13");
326 popdown = g_list_append(popdown, "14");
328 gtk_combo_set_popdown_strings( GTK_COMBO(channel_cb), popdown) ;
329 g_list_free(popdown);
330 gtk_widget_set_size_request( GTK_WIDGET(channel_cb),
339 * Sorts the Interface List in alphabetical order
341 gint if_list_comparator_alph (const void *first_arg, const void *second_arg){
342 const if_info_t *first = first_arg, *second = second_arg;
344 if (first != NULL && first->description != NULL &&
345 second != NULL && second->description != NULL) {
346 return g_ascii_strcasecmp(first->description, second->description);
352 /* start getting capture stats from all interfaces */
354 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
364 GtkWidget *decryption_cm;
369 GtkTooltips *tooltips;
372 GtkRequisition requisition;
374 if_dlg_data_t *if_dlg_data;
380 GString *if_tool_str = g_string_new("");
381 const gchar *addr_str;
385 if (cap_if_w != NULL) {
386 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
387 reactivate_window(cap_if_w);
392 /* Is WPcap loaded? */
396 detailed_err = cant_load_winpcap_err("Wireshark");
397 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
398 g_free(detailed_err);
403 /* LOAD THE INTERFACES */
404 if_list = capture_interface_list(&err, &err_str);
405 if_list = g_list_sort (if_list, if_list_comparator_alph);
406 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
407 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
413 /* LOAD AIRPCAP INTERFACES */
414 airpcap_if_list = get_airpcap_interface_list(&err, &err_str);
415 if (airpcap_if_list == NULL)
416 airpcap_if_active = airpcap_if_selected = NULL;
418 decryption_cm = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_DECRYPTION_KEY);
419 update_decryption_mode_list(decryption_cm);
421 if (airpcap_if_list == NULL && err == CANT_GET_AIRPCAP_INTERFACE_LIST) {
423 /* XXX - Do we need to show an error here? */
424 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
429 /* If no airpcap interface is present, gray everything */
430 if (airpcap_if_active == NULL) {
431 if (airpcap_if_list == NULL) {
432 /*No airpcap device found */
433 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
435 /* default adapter is not airpcap... or is airpcap but is not found*/
436 airpcap_set_toolbar_stop_capture(airpcap_if_active);
437 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
441 airpcap_set_toolbar_start_capture(airpcap_if_active);
444 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Capture Interfaces");
446 tooltips = gtk_tooltips_new();
448 main_sw = gtk_scrolled_window_new(NULL, NULL);
449 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(main_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
450 gtk_container_add(GTK_CONTAINER(cap_if_w), main_sw);
452 main_vb = gtk_vbox_new(FALSE, 0);
453 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
454 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(main_sw), main_vb);
457 if_tb = gtk_table_new(1,9, FALSE);
458 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
459 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
460 gtk_box_pack_start(GTK_BOX(main_vb), if_tb, FALSE, FALSE, 0);
465 /* This is the icon column, used to display which kind of interface we have */
466 if_lb = gtk_label_new("");
467 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
471 * On Windows, device names are generally not meaningful - NT 5
472 * uses long blobs with GUIDs in them, for example - so we don't
473 * bother showing them.
475 if_lb = gtk_label_new("Device");
476 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
479 if_lb = gtk_label_new("Description");
480 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
482 if_lb = gtk_label_new(" IP ");
483 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
485 if_lb = gtk_label_new("Packets");
486 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
488 if_lb = gtk_label_new(" Packets/s ");
489 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 5, 6, row, row+1);
491 stop_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_CAPTURE_STOP);
492 gtk_tooltips_set_tip(tooltips, stop_bt,
493 "Stop a running capture.", NULL);
495 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 9, row, row+1);
497 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 8, row, row+1);
499 g_signal_connect(stop_bt, "clicked", G_CALLBACK(capture_stop_cb), NULL);
502 gtk_widget_size_request(stop_bt, &requisition);
503 height += requisition.height + 15;
505 /* Start gathering statistics (using dumpcap) */
506 sc = capture_stat_start(if_list);
508 /* List the interfaces */
509 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
510 g_string_assign(if_tool_str, "");
511 if_info = curr->data;
513 /* Continue if capture device is hidden */
514 if (prefs_is_capture_device_hidden(if_info->name)) {
518 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
519 if_dlg_data->if_info = *if_info;
521 /* Kind of adaptor (icon) */
523 if(get_airpcap_if_from_name(airpcap_if_list,if_info->name) != NULL)
524 icon = xpm_to_widget(capture_airpcap_16_xpm);
526 icon = xpm_to_widget(capture_ethernet_16_xpm);
528 icon = xpm_to_widget(capture_ethernet_16_xpm);
531 gtk_table_attach_defaults(GTK_TABLE(if_tb), icon, 0, 1, row, row+1);
534 if_dlg_data->device_lb = gtk_label_new(if_info->name);
535 if_dlg_data->device = if_info->name;
537 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
538 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 1, 2, row, row+1);
540 g_string_append(if_tool_str, "Device: ");
541 g_string_append(if_tool_str, if_info->name);
542 g_string_append(if_tool_str, "\n");
545 if (if_info->description != NULL)
546 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
548 if_dlg_data->descr_lb = gtk_label_new("");
549 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
550 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 2, 3, row, row+1);
552 if (if_info->description) {
553 g_string_append(if_tool_str, "Description: ");
554 g_string_append(if_tool_str, if_info->description);
555 g_string_append(if_tool_str, "\n");
559 /* only the first IP address will be shown */
560 g_string_append(if_tool_str, "IP: ");
561 curr_ip = g_slist_nth(if_info->ip_addr, 0);
563 ip_addr = (if_addr_t *)curr_ip->data;
564 switch (ip_addr->type) {
567 addr_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
571 addr_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
575 g_assert_not_reached();
578 if_dlg_data->ip_lb = gtk_label_new(addr_str);
579 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
580 g_string_append(if_tool_str, addr_str);
582 if_dlg_data->ip_lb = gtk_label_new("unknown");
583 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
584 g_string_append(if_tool_str, "unknown");
586 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 3, 4, row, row+1);
587 g_string_append(if_tool_str, "\n");
590 if_dlg_data->curr_lb = gtk_label_new("-");
591 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 4, 5, row, row+1);
594 if_dlg_data->last_lb = gtk_label_new("-");
595 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 5, 6, row, row+1);
598 if_dlg_data->capture_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_CAPTURE_START);
599 g_signal_connect(if_dlg_data->capture_bt, "clicked", G_CALLBACK(capture_do_cb), if_dlg_data);
600 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
601 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
604 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 6, 7, row, row+1);
607 if_dlg_data->prepare_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_CAPTURE_OPTIONS);
608 g_signal_connect(if_dlg_data->prepare_bt, "clicked", G_CALLBACK(capture_prepare_cb), if_dlg_data);
609 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
610 "Open the capture options dialog with this interface selected.", NULL);
611 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 7, 8, row, row+1);
615 if_dlg_data->details_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_CAPTURE_DETAILS);
616 g_signal_connect(if_dlg_data->details_bt, "clicked", G_CALLBACK(capture_details_cb), if_dlg_data);
617 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
618 "Open the capture details dialog of this interface.", NULL);
619 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 8, 9, row, row+1);
622 if_data = g_list_append(if_data, if_dlg_data);
626 /* Lets add up 10 rows of interfaces, otherwise the window may become too high */
627 gtk_widget_size_request(GTK_WIDGET(if_dlg_data->prepare_bt), &requisition);
628 height += requisition.height;
632 g_string_free(if_tool_str, TRUE);
634 /* Button row: close and help button */
635 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
636 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
638 close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
639 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
640 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
642 help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
643 g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)(HELP_CAPTURE_INTERFACES_DIALOG));
645 gtk_widget_size_request(GTK_WIDGET(close_bt), &requisition);
646 /* height + static offset + what the GTK MS Windows Engine needs in addition per interface */
647 height += requisition.height + 20 + ifs;
648 gtk_window_set_default_size(GTK_WINDOW(cap_if_w), -1, height);
650 gtk_widget_grab_default(close_bt);
652 g_signal_connect(cap_if_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
653 g_signal_connect(cap_if_w, "destroy", G_CALLBACK(capture_if_destroy_cb), sc);
655 gtk_widget_show_all(cap_if_w);
656 window_present(cap_if_w);
658 set_capture_if_dialog_for_capture_in_progress(g_capture_in_progress);
660 /* update the interface list every 1000ms */
661 timer_id = gtk_timeout_add(1000, update_all, sc);
664 #else /* HAVE_LIBPCAP */
667 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress _U_)
671 #endif /* HAVE_LIBPCAP */