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"
52 #include <epan/prefs.h>
54 #include "gui_utils.h"
55 #include "dlg_utils.h"
63 #include "webbrowser.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 {
111 GtkWidget *device_lb;
116 GtkWidget *capture_bt;
117 GtkWidget *prepare_bt;
119 GtkWidget *details_bt;
121 guint32 last_packets;
125 void update_if(if_dlg_data_t *if_dlg_data);
128 /* start capture button was pressed */
130 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
132 if_dlg_data_t *if_dlg_data = if_data;
135 airpcap_if_active = get_airpcap_if_from_description(airpcap_if_list, GTK_LABEL(if_dlg_data->descr_lb)->label);
136 airpcap_if_selected = airpcap_if_active;
139 if (capture_opts->iface)
140 g_free(capture_opts->iface);
142 capture_opts->iface = g_strdup(if_dlg_data->device);
144 /* XXX - remove this? */
145 if (capture_opts->save_file) {
146 g_free(capture_opts->save_file);
147 capture_opts->save_file = NULL;
150 /* stop capturing from all interfaces, we are going to do real work now ... */
151 window_destroy(cap_if_w);
153 capture_start_cb(NULL, NULL);
157 /* prepare capture button was pressed */
159 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
161 if_dlg_data_t *if_dlg_data = if_data;
163 if (capture_opts->iface)
164 g_free(capture_opts->iface);
166 capture_opts->iface = g_strdup(if_dlg_data->device);
168 /* stop capturing from all interfaces, we are going to do real work now ... */
169 window_destroy(cap_if_w);
171 capture_prep_cb(NULL, NULL);
176 /* capture details button was pressed */
178 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
180 if_dlg_data_t *if_dlg_data = if_data;
183 capture_if_details_open(if_dlg_data->device);
188 /* open a single interface */
190 open_if(gchar *name, if_dlg_data_t *if_dlg_data)
192 gchar open_err_str[CAPTURE_PCAP_ERRBUF_SIZE];
195 * XXX - on systems with BPF, the number of BPF devices limits the
196 * number of devices on which you can capture simultaneously.
200 * 1) this might fail if you run out of BPF devices
204 * 2) opening every interface could leave too few BPF devices
205 * for *other* programs.
207 * It also means the system could end up getting a lot of traffic
208 * that it has to pass through the networking stack and capture
209 * mechanism, so opening all the devices and presenting packet
210 * counts might not always be a good idea.
212 if_dlg_data->pch = pcap_open_live(name,
214 capture_opts->promisc_mode, CAP_READ_TIMEOUT,
217 if (if_dlg_data->pch != NULL) {
218 update_if(if_dlg_data);
220 printf("open_if: %s\n", open_err_str);
221 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
222 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
226 /* update a single interface */
228 update_if(if_dlg_data_t *if_dlg_data)
230 struct pcap_stat stats;
235 /* pcap_stats() stats values differ on libpcap and winpcap!
236 * libpcap: returns the number of packets since pcap_open_live
237 * winpcap: returns the number of packets since the last pcap_stats call
238 * XXX - if that's true, that's a bug, and should be fixed; "pcap_stats()"
239 * is supposed to work the same way on all platforms, including Windows.
240 * Note that the WinPcap 3.0 documentation says "The values represent
241 * packet statistics from the start of the run to the time of the call."
242 * (Note also that some versions of libpcap, on some versions of UN*X,
243 * have the same bug.)
245 if (if_dlg_data->pch) {
246 if(pcap_stats(if_dlg_data->pch, &stats) >= 0) {
248 diff = stats.ps_recv - if_dlg_data->last_packets;
249 if_dlg_data->last_packets = stats.ps_recv;
251 diff = stats.ps_recv;
252 if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
255 str = g_strdup_printf("%u", if_dlg_data->last_packets);
256 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
258 str = g_strdup_printf("%u", diff);
259 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
262 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
263 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
265 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
266 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
272 /* close a single interface */
274 close_if(if_dlg_data_t *if_dlg_data)
277 pcap_close(if_dlg_data->pch);
282 /* update all interfaces */
284 update_all(gpointer data)
294 for(ifs = 0; (curr = g_list_nth(data, ifs)); ifs++) {
295 update_if(curr->data);
302 /* a live capture has started or stopped */
304 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
310 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
312 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
313 if_dlg_data_t *if_dlg_data = curr->data;
315 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
316 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
322 /* the window was closed, cleanup things */
324 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
329 gtk_timeout_remove(timer_id);
331 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
332 if_dlg_data_t *if_dlg_data = curr->data;
334 close_if(if_dlg_data);
340 free_interface_list(if_list);
342 /* Note that we no longer have a "Capture Options" dialog box. */
346 airpcap_set_toolbar_stop_capture(airpcap_if_active);
351 combo_channel_new(void)
353 GtkWidget* channel_cb;
357 channel_cb = gtk_combo_new();
358 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(channel_cb)->entry), "1");
362 popdown = g_list_append(popdown, "1");
363 popdown = g_list_append(popdown, "2");
364 popdown = g_list_append(popdown, "3");
365 popdown = g_list_append(popdown, "4");
366 popdown = g_list_append(popdown, "5");
367 popdown = g_list_append(popdown, "6");
368 popdown = g_list_append(popdown, "7");
369 popdown = g_list_append(popdown, "8");
370 popdown = g_list_append(popdown, "9");
371 popdown = g_list_append(popdown, "10");
372 popdown = g_list_append(popdown, "11");
373 popdown = g_list_append(popdown, "12");
374 popdown = g_list_append(popdown, "13");
375 popdown = g_list_append(popdown, "14");
377 gtk_combo_set_popdown_strings( GTK_COMBO(channel_cb), popdown) ;
379 #if GTK_MAJOR_VERSION < 2
380 gtk_widget_set_usize( GTK_WIDGET(channel_cb),
384 gtk_widget_set_size_request( GTK_WIDGET(channel_cb),
393 /* start getting capture stats from all interfaces */
395 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
405 GtkWidget *decryption_cm;
410 #if GTK_MAJOR_VERSION < 2
411 GtkAccelGroup *accel_group;
413 GtkTooltips *tooltips;
415 char err_str[CAPTURE_PCAP_ERRBUF_SIZE];
416 gchar *cant_get_if_list_errstr;
417 GtkRequisition requisition;
419 if_dlg_data_t *if_dlg_data;
425 GString *if_tool_str = g_string_new("");
428 if (cap_if_w != NULL) {
429 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
430 reactivate_window(cap_if_w);
435 /* Is WPcap loaded? */
439 detailed_err = cant_load_winpcap_err("Wireshark");
440 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
441 g_free(detailed_err);
446 /* LOAD THE INTERFACES */
447 if_list = get_interface_list(&err, err_str);
448 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
449 cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
450 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
451 cant_get_if_list_errstr);
452 g_free(cant_get_if_list_errstr);
457 /* LOAD AIRPCAP INTERFACES */
458 /* load the airpcap interfaces */
459 airpcap_if_list = get_airpcap_interface_list(&err, err_str);
460 if(airpcap_if_list == NULL) airpcap_if_active = airpcap_if_selected = NULL;
462 decryption_cm = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_KEY);
463 update_decryption_mode_list(decryption_cm);
465 if (airpcap_if_list == NULL && err == CANT_GET_AIRPCAP_INTERFACE_LIST) {
466 cant_get_if_list_errstr = cant_get_airpcap_if_list_error_message(err_str);
467 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
468 cant_get_if_list_errstr);
469 g_free(cant_get_if_list_errstr);
472 /* If no airpcap interface is present, gray everything */
473 if(airpcap_if_active == NULL)
475 if(airpcap_if_list == NULL)
477 /*No airpcap device found */
478 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
482 /* default adapter is not airpcap... or is airpcap but is not found*/
483 airpcap_set_toolbar_stop_capture(airpcap_if_active);
484 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
488 airpcap_set_toolbar_start_capture(airpcap_if_active);
491 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Capture Interfaces");
493 tooltips = gtk_tooltips_new();
495 #if GTK_MAJOR_VERSION < 2
496 /* Accelerator group for the accelerators (or, as they're called in
497 Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
498 Ctrl+<key> is an accelerator). */
499 accel_group = gtk_accel_group_new();
500 gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
503 main_sw = gtk_scrolled_window_new(NULL, NULL);
504 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(main_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
505 gtk_container_add(GTK_CONTAINER(cap_if_w), main_sw);
507 main_vb = gtk_vbox_new(FALSE, 0);
508 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
509 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(main_sw), main_vb);
512 if_tb = gtk_table_new(1,9, FALSE);
513 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
514 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
515 gtk_box_pack_start(GTK_BOX(main_vb), if_tb, FALSE, FALSE, 0);
520 /* This is the icon column, used to display which kind of interface we have */
521 if_lb = gtk_label_new("");
522 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
526 * On Windows, device names are generally not meaningful - NT 5
527 * uses long blobs with GUIDs in them, for example - so we don't
528 * bother showing them.
530 if_lb = gtk_label_new("Device");
531 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
534 if_lb = gtk_label_new("Description");
535 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
537 if_lb = gtk_label_new(" IP ");
538 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
540 if_lb = gtk_label_new("Packets");
541 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
543 if_lb = gtk_label_new(" Packets/s ");
544 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 5, 6, row, row+1);
546 stop_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_STOP);
547 gtk_tooltips_set_tip(tooltips, stop_bt,
548 "Stop a running capture.", NULL);
550 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 9, row, row+1);
552 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 8, row, row+1);
554 SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
557 gtk_widget_size_request(stop_bt, &requisition);
558 height += requisition.height + 15;
560 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
561 g_string_assign(if_tool_str, "");
562 if_info = curr->data;
563 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
565 /* Kind of adaptor (icon) */
567 if(get_airpcap_if_from_description(airpcap_if_list,if_info->description) != NULL)
568 icon = xpm_to_widget(capture_airpcap_16_xpm);
570 icon = xpm_to_widget(capture_ethernet_16_xpm);
572 icon = xpm_to_widget(capture_ethernet_16_xpm);
575 gtk_table_attach_defaults(GTK_TABLE(if_tb), icon, 0, 1, row, row+1);
578 if_dlg_data->device_lb = gtk_label_new(if_info->name);
579 if_dlg_data->device = if_info->name;
581 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
582 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 1, 2, row, row+1);
584 g_string_append(if_tool_str, "Device: ");
585 g_string_append(if_tool_str, if_info->name);
586 g_string_append(if_tool_str, "\n");
589 if (if_info->description != NULL)
590 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
592 if_dlg_data->descr_lb = gtk_label_new("");
593 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
594 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 2, 3, row, row+1);
596 if (if_info->description) {
597 g_string_append(if_tool_str, "Description: ");
598 g_string_append(if_tool_str, if_info->description);
599 g_string_append(if_tool_str, "\n");
603 /* only the first IP address will be shown */
604 g_string_append(if_tool_str, "IP: ");
605 curr_ip = g_slist_nth(if_info->ip_addr, 0);
607 ip_addr = (if_addr_t *)curr_ip->data;
608 switch (ip_addr->type) {
611 tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
615 tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
619 g_assert_not_reached();
622 if_dlg_data->ip_lb = gtk_label_new(tmp_str);
623 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
624 g_string_append(if_tool_str, tmp_str);
626 if_dlg_data->ip_lb = gtk_label_new("unknown");
627 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
628 g_string_append(if_tool_str, "unknown");
630 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 3, 4, row, row+1);
631 g_string_append(if_tool_str, "\n");
634 if_dlg_data->curr_lb = gtk_label_new("-");
635 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 4, 5, row, row+1);
638 if_dlg_data->last_lb = gtk_label_new("-");
639 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 5, 6, row, row+1);
642 if_dlg_data->capture_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_START);
643 SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
644 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
645 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
648 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 6, 7, row, row+1);
651 if_dlg_data->prepare_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_OPTIONS);
652 SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
653 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
654 "Open the capture options dialog with this interface selected.", NULL);
655 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 7, 8, row, row+1);
659 if_dlg_data->details_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_DETAILS);
660 SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
661 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
662 "Open the capture details dialog of this interface.", NULL);
663 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 8, 9, row, row+1);
666 open_if(if_info->name, if_dlg_data);
668 if_data = g_list_append(if_data, if_dlg_data);
672 /* Lets add up 10 rows of interfaces, otherwise the window may become too high */
673 gtk_widget_size_request(GTK_WIDGET(if_dlg_data->prepare_bt), &requisition);
674 height += requisition.height;
678 g_string_free(if_tool_str, TRUE);
680 /* Button row: close button */
681 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
682 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
684 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
686 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
688 close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
689 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
690 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
692 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
693 help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
694 SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
697 gtk_widget_size_request(GTK_WIDGET(close_bt), &requisition);
698 /* height + static offset + what GTK-Wimp needs in addition per interface */
699 height += requisition.height + 20 + ifs;
700 gtk_window_set_default_size(GTK_WINDOW(cap_if_w), -1, height);
702 gtk_widget_grab_default(close_bt);
704 SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
705 SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, NULL);
707 gtk_widget_show_all(cap_if_w);
708 window_present(cap_if_w);
710 set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
712 /* update the interface list every 1000ms */
713 timer_id = gtk_timeout_add(1000, update_all, if_data);
717 #endif /* HAVE_LIBPCAP */