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.
31 #ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
39 #include "capture-pcap-util.h"
42 #include "capture-wpcap.h"
45 #include "compat_macros.h"
46 #include "simple_dialog.h"
48 #include "capture_dlg.h"
49 #include "capture_if_details_dlg.h"
50 #include "capture_errs.h"
51 #include "capture_ui_utils.h"
53 #include <epan/prefs.h>
55 #include "gui_utils.h"
56 #include "dlg_utils.h"
64 #include "webbrowser.h"
67 #include "../image/toolbar/capture_airpcap_16.xpm"
69 #include "../image/toolbar/capture_ethernet_16.xpm"
71 /* new buttons to be used instead of labels for 'Capture','Prepare',' */
72 #include "../image/toolbar/capture_capture_16.xpm"
73 #include "../image/toolbar/capture_prepare_16.xpm"
74 #include "../image/toolbar/capture_details_16.xpm"
79 #include "airpcap_loader.h"
80 #include "airpcap_gui_utils.h"
81 #include "airpcap_dlg.h"
84 #ifdef NEED_G_ASCII_STRCASECMP_H
85 #include "../epan/g_ascii_strcasecmp.h"
89 * Keep a static pointer to the current "Capture Interfaces" window, if
90 * any, so that if somebody tries to do "Capture:Start" while there's
91 * already a "Capture Interfaces" window up, we just pop up the existing
92 * one, rather than creating a new one.
94 static GtkWidget *cap_if_w;
96 static GtkWidget *cap_air_w;
99 GList *if_data = NULL;
108 * Timeout, in milliseconds, for reads from the stream of captured packets.
110 #define CAP_READ_TIMEOUT 250
113 /* the "runtime" data of one interface */
114 typedef struct if_dlg_data_s {
115 GtkWidget *device_lb;
120 GtkWidget *capture_bt;
121 GtkWidget *prepare_bt;
123 GtkWidget *details_bt;
125 guint32 last_packets;
131 /* start capture button was pressed */
133 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
135 if_dlg_data_t *if_dlg_data = if_data;
138 airpcap_if_active = get_airpcap_if_from_name(airpcap_if_list, if_dlg_data->if_info.name);
139 airpcap_if_selected = airpcap_if_active;
142 if (capture_opts->iface)
143 g_free(capture_opts->iface);
144 if (capture_opts->iface_descr)
145 g_free(capture_opts->iface_descr);
147 capture_opts->iface = g_strdup(if_dlg_data->device);
148 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
150 /* XXX - remove this? */
151 if (capture_opts->save_file) {
152 g_free(capture_opts->save_file);
153 capture_opts->save_file = NULL;
156 /* stop capturing from all interfaces, we are going to do real work now ... */
157 window_destroy(cap_if_w);
159 capture_start_cb(NULL, NULL);
163 /* prepare capture button was pressed */
165 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
167 if_dlg_data_t *if_dlg_data = if_data;
169 if (capture_opts->iface)
170 g_free(capture_opts->iface);
171 if (capture_opts->iface_descr)
172 g_free(capture_opts->iface_descr);
174 capture_opts->iface = g_strdup(if_dlg_data->device);
175 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
177 /* stop capturing from all interfaces, we are going to do real work now ... */
178 window_destroy(cap_if_w);
180 capture_prep_cb(NULL, NULL);
185 /* capture details button was pressed */
187 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
189 if_dlg_data_t *if_dlg_data = if_data;
192 capture_if_details_open(if_dlg_data->device);
196 /* update a single interface */
198 update_if(if_dlg_data_t *if_dlg_data, if_stat_cache_t *sc)
200 struct pcap_stat stats;
206 * Note that some versions of libpcap, on some versions of UN*X,
207 * pcap_stats() returns the number of packets since the last
210 * That's a bug, and should be fixed; "pcap_stats()" is supposed
211 * to work the same way on all platforms.
214 if(capture_stats(sc, if_dlg_data->device, &stats)) {
215 diff = stats.ps_recv - if_dlg_data->last_packets;
216 if_dlg_data->last_packets = stats.ps_recv;
218 str = g_strdup_printf("%u", if_dlg_data->last_packets);
219 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
221 str = g_strdup_printf("%u", diff);
222 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
225 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
226 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
228 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
229 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
234 /* update all interfaces */
236 update_all(gpointer data)
240 if_stat_cache_t *sc = data;
246 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
247 update_if(curr->data, sc);
254 /* a live capture has started or stopped */
256 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
262 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
264 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
265 if_dlg_data_t *if_dlg_data = curr->data;
267 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
268 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
274 /* the window was closed, cleanup things */
276 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data)
280 if_stat_cache_t *sc = user_data;
282 gtk_timeout_remove(timer_id);
284 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
290 free_interface_list(if_list);
292 /* Note that we no longer have a "Capture Options" dialog box. */
295 capture_stat_stop(sc);
298 airpcap_set_toolbar_stop_capture(airpcap_if_active);
303 combo_channel_new(void)
305 GtkWidget* channel_cb;
309 channel_cb = gtk_combo_new();
310 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(channel_cb)->entry), "1");
314 popdown = g_list_append(popdown, "1");
315 popdown = g_list_append(popdown, "2");
316 popdown = g_list_append(popdown, "3");
317 popdown = g_list_append(popdown, "4");
318 popdown = g_list_append(popdown, "5");
319 popdown = g_list_append(popdown, "6");
320 popdown = g_list_append(popdown, "7");
321 popdown = g_list_append(popdown, "8");
322 popdown = g_list_append(popdown, "9");
323 popdown = g_list_append(popdown, "10");
324 popdown = g_list_append(popdown, "11");
325 popdown = g_list_append(popdown, "12");
326 popdown = g_list_append(popdown, "13");
327 popdown = g_list_append(popdown, "14");
329 gtk_combo_set_popdown_strings( GTK_COMBO(channel_cb), popdown) ;
331 #if GTK_MAJOR_VERSION < 2
332 gtk_widget_set_usize( GTK_WIDGET(channel_cb),
336 gtk_widget_set_size_request( GTK_WIDGET(channel_cb),
346 * Sorts the Interface List in alphabetical order
348 gint if_list_comparator_alph (const void *first_arg, const void *second_arg){
349 const if_info_t *first = first_arg, *second = second_arg;
351 if (first != NULL && first->description != NULL &&
352 second != NULL && second->description != NULL) {
353 return g_ascii_strcasecmp(first->description, second->description);
359 /* start getting capture stats from all interfaces */
361 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
371 GtkWidget *decryption_cm;
376 #if GTK_MAJOR_VERSION < 2
377 GtkAccelGroup *accel_group;
379 GtkTooltips *tooltips;
382 GtkRequisition requisition;
384 if_dlg_data_t *if_dlg_data;
390 GString *if_tool_str = g_string_new("");
394 if (cap_if_w != NULL) {
395 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
396 reactivate_window(cap_if_w);
401 /* Is WPcap loaded? */
405 detailed_err = cant_load_winpcap_err("Wireshark");
406 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
407 g_free(detailed_err);
412 /* LOAD THE INTERFACES */
413 if_list = capture_interface_list(&err, &err_str);
414 if_list = g_list_sort (if_list, if_list_comparator_alph);
415 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
416 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
422 /* LOAD AIRPCAP INTERFACES */
423 airpcap_if_list = get_airpcap_interface_list(&err, &err_str);
424 if (airpcap_if_list == NULL)
425 airpcap_if_active = airpcap_if_selected = NULL;
427 decryption_cm = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_KEY);
428 update_decryption_mode_list(decryption_cm);
430 if (airpcap_if_list == NULL && err == CANT_GET_AIRPCAP_INTERFACE_LIST) {
432 /* XXX - Do we need to show an error here? */
433 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
438 /* If no airpcap interface is present, gray everything */
439 if (airpcap_if_active == NULL) {
440 if (airpcap_if_list == NULL) {
441 /*No airpcap device found */
442 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
444 /* default adapter is not airpcap... or is airpcap but is not found*/
445 airpcap_set_toolbar_stop_capture(airpcap_if_active);
446 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
450 airpcap_set_toolbar_start_capture(airpcap_if_active);
453 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Capture Interfaces");
455 tooltips = gtk_tooltips_new();
457 #if GTK_MAJOR_VERSION < 2
458 /* Accelerator group for the accelerators (or, as they're called in
459 Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
460 Ctrl+<key> is an accelerator). */
461 accel_group = gtk_accel_group_new();
462 gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
465 main_sw = gtk_scrolled_window_new(NULL, NULL);
466 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(main_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
467 gtk_container_add(GTK_CONTAINER(cap_if_w), main_sw);
469 main_vb = gtk_vbox_new(FALSE, 0);
470 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
471 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(main_sw), main_vb);
474 if_tb = gtk_table_new(1,9, FALSE);
475 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
476 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
477 gtk_box_pack_start(GTK_BOX(main_vb), if_tb, FALSE, FALSE, 0);
482 /* This is the icon column, used to display which kind of interface we have */
483 if_lb = gtk_label_new("");
484 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
488 * On Windows, device names are generally not meaningful - NT 5
489 * uses long blobs with GUIDs in them, for example - so we don't
490 * bother showing them.
492 if_lb = gtk_label_new("Device");
493 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
496 if_lb = gtk_label_new("Description");
497 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
499 if_lb = gtk_label_new(" IP ");
500 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
502 if_lb = gtk_label_new("Packets");
503 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
505 if_lb = gtk_label_new(" Packets/s ");
506 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 5, 6, row, row+1);
508 stop_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_STOP);
509 gtk_tooltips_set_tip(tooltips, stop_bt,
510 "Stop a running capture.", NULL);
512 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 9, row, row+1);
514 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 8, row, row+1);
516 SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
519 gtk_widget_size_request(stop_bt, &requisition);
520 height += requisition.height + 15;
522 /* Start gathering statistics (using dumpcap) */
523 sc = capture_stat_start(if_list);
525 /* List the interfaces */
526 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
527 g_string_assign(if_tool_str, "");
528 if_info = curr->data;
529 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
530 if_dlg_data->if_info = *if_info;
532 /* Kind of adaptor (icon) */
534 if(get_airpcap_if_from_name(airpcap_if_list,if_info->name) != NULL)
535 icon = xpm_to_widget(capture_airpcap_16_xpm);
537 icon = xpm_to_widget(capture_ethernet_16_xpm);
539 icon = xpm_to_widget(capture_ethernet_16_xpm);
542 gtk_table_attach_defaults(GTK_TABLE(if_tb), icon, 0, 1, row, row+1);
545 if_dlg_data->device_lb = gtk_label_new(if_info->name);
546 if_dlg_data->device = if_info->name;
548 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
549 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 1, 2, row, row+1);
551 g_string_append(if_tool_str, "Device: ");
552 g_string_append(if_tool_str, if_info->name);
553 g_string_append(if_tool_str, "\n");
556 if (if_info->description != NULL)
557 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
559 if_dlg_data->descr_lb = gtk_label_new("");
560 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
561 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 2, 3, row, row+1);
563 if (if_info->description) {
564 g_string_append(if_tool_str, "Description: ");
565 g_string_append(if_tool_str, if_info->description);
566 g_string_append(if_tool_str, "\n");
570 /* only the first IP address will be shown */
571 g_string_append(if_tool_str, "IP: ");
572 curr_ip = g_slist_nth(if_info->ip_addr, 0);
574 ip_addr = (if_addr_t *)curr_ip->data;
575 switch (ip_addr->type) {
578 tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
582 tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
586 g_assert_not_reached();
589 if_dlg_data->ip_lb = gtk_label_new(tmp_str);
590 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
591 g_string_append(if_tool_str, tmp_str);
593 if_dlg_data->ip_lb = gtk_label_new("unknown");
594 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
595 g_string_append(if_tool_str, "unknown");
597 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 3, 4, row, row+1);
598 g_string_append(if_tool_str, "\n");
601 if_dlg_data->curr_lb = gtk_label_new("-");
602 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 4, 5, row, row+1);
605 if_dlg_data->last_lb = gtk_label_new("-");
606 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 5, 6, row, row+1);
609 if_dlg_data->capture_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_START);
610 SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
611 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
612 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
615 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 6, 7, row, row+1);
618 if_dlg_data->prepare_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_OPTIONS);
619 SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
620 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
621 "Open the capture options dialog with this interface selected.", NULL);
622 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 7, 8, row, row+1);
626 if_dlg_data->details_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_DETAILS);
627 SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
628 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
629 "Open the capture details dialog of this interface.", NULL);
630 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 8, 9, row, row+1);
633 if_data = g_list_append(if_data, if_dlg_data);
637 /* Lets add up 10 rows of interfaces, otherwise the window may become too high */
638 gtk_widget_size_request(GTK_WIDGET(if_dlg_data->prepare_bt), &requisition);
639 height += requisition.height;
643 g_string_free(if_tool_str, TRUE);
645 /* Button row: close button */
646 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
647 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
649 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
651 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
653 close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
654 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
655 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
657 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
658 help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
659 SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
662 gtk_widget_size_request(GTK_WIDGET(close_bt), &requisition);
663 /* height + static offset + what GTK-Wimp needs in addition per interface */
664 height += requisition.height + 20 + ifs;
665 gtk_window_set_default_size(GTK_WINDOW(cap_if_w), -1, height);
667 gtk_widget_grab_default(close_bt);
669 SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
670 SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, sc);
672 gtk_widget_show_all(cap_if_w);
673 window_present(cap_if_w);
675 set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
677 /* update the interface list every 1000ms */
678 timer_id = gtk_timeout_add(1000, update_all, sc);
682 #endif /* HAVE_LIBPCAP */