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"
85 * Keep a static pointer to the current "Capture Interfaces" window, if
86 * any, so that if somebody tries to do "Capture:Start" while there's
87 * already a "Capture Interfaces" window up, we just pop up the existing
88 * one, rather than creating a new one.
90 static GtkWidget *cap_if_w;
92 static GtkWidget *cap_air_w;
95 GList *if_data = NULL;
104 * Timeout, in milliseconds, for reads from the stream of captured packets.
106 #define CAP_READ_TIMEOUT 250
109 /* the "runtime" data of one interface */
110 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;
127 /* start capture button was pressed */
129 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
131 if_dlg_data_t *if_dlg_data = if_data;
134 airpcap_if_active = get_airpcap_if_from_name(airpcap_if_list, if_dlg_data->if_info.name);
135 airpcap_if_selected = airpcap_if_active;
138 if (capture_opts->iface)
139 g_free(capture_opts->iface);
140 if (capture_opts->iface_descr)
141 g_free(capture_opts->iface_descr);
143 capture_opts->iface = g_strdup(if_dlg_data->device);
144 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
146 /* XXX - remove this? */
147 if (capture_opts->save_file) {
148 g_free(capture_opts->save_file);
149 capture_opts->save_file = NULL;
152 /* stop capturing from all interfaces, we are going to do real work now ... */
153 window_destroy(cap_if_w);
155 capture_start_cb(NULL, NULL);
159 /* prepare capture button was pressed */
161 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
163 if_dlg_data_t *if_dlg_data = if_data;
165 if (capture_opts->iface)
166 g_free(capture_opts->iface);
167 if (capture_opts->iface_descr)
168 g_free(capture_opts->iface_descr);
170 capture_opts->iface = g_strdup(if_dlg_data->device);
171 capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
173 /* stop capturing from all interfaces, we are going to do real work now ... */
174 window_destroy(cap_if_w);
176 capture_prep_cb(NULL, NULL);
181 /* capture details button was pressed */
183 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
185 if_dlg_data_t *if_dlg_data = if_data;
188 capture_if_details_open(if_dlg_data->device);
192 /* update a single interface */
194 update_if(if_dlg_data_t *if_dlg_data, if_stat_cache_t *sc)
196 struct pcap_stat stats;
201 /* pcap_stats() stats values differ on libpcap and winpcap!
202 * libpcap: returns the number of packets since pcap_open_live
203 * winpcap: returns the number of packets since the last pcap_stats call
204 * XXX - if that's true, that's a bug, and should be fixed; "pcap_stats()"
205 * is supposed to work the same way on all platforms, including Windows.
206 * Note that the WinPcap 3.0 documentation says "The values represent
207 * packet statistics from the start of the run to the time of the call."
208 * (Note also that some versions of libpcap, on some versions of UN*X,
209 * have the same bug.)
212 if(capture_stats(sc, if_dlg_data->device, &stats)) {
214 diff = stats.ps_recv - if_dlg_data->last_packets;
215 if_dlg_data->last_packets = stats.ps_recv;
217 diff = stats.ps_recv;
218 if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
221 str = g_strdup_printf("%u", if_dlg_data->last_packets);
222 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
224 str = g_strdup_printf("%u", diff);
225 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
228 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
229 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
231 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
232 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
237 /* update all interfaces */
239 update_all(gpointer data)
243 if_stat_cache_t *sc = data;
249 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
250 update_if(curr->data, sc);
257 /* a live capture has started or stopped */
259 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
265 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
267 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
268 if_dlg_data_t *if_dlg_data = curr->data;
270 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
271 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
277 /* the window was closed, cleanup things */
279 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data)
283 if_stat_cache_t *sc = user_data;
285 gtk_timeout_remove(timer_id);
287 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
293 free_interface_list(if_list);
295 /* Note that we no longer have a "Capture Options" dialog box. */
298 capture_stat_stop(sc);
301 airpcap_set_toolbar_stop_capture(airpcap_if_active);
306 combo_channel_new(void)
308 GtkWidget* channel_cb;
312 channel_cb = gtk_combo_new();
313 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(channel_cb)->entry), "1");
317 popdown = g_list_append(popdown, "1");
318 popdown = g_list_append(popdown, "2");
319 popdown = g_list_append(popdown, "3");
320 popdown = g_list_append(popdown, "4");
321 popdown = g_list_append(popdown, "5");
322 popdown = g_list_append(popdown, "6");
323 popdown = g_list_append(popdown, "7");
324 popdown = g_list_append(popdown, "8");
325 popdown = g_list_append(popdown, "9");
326 popdown = g_list_append(popdown, "10");
327 popdown = g_list_append(popdown, "11");
328 popdown = g_list_append(popdown, "12");
329 popdown = g_list_append(popdown, "13");
330 popdown = g_list_append(popdown, "14");
332 gtk_combo_set_popdown_strings( GTK_COMBO(channel_cb), popdown) ;
334 #if GTK_MAJOR_VERSION < 2
335 gtk_widget_set_usize( GTK_WIDGET(channel_cb),
339 gtk_widget_set_size_request( GTK_WIDGET(channel_cb),
349 * Sorts the Interface List in alphabetical order
351 gint if_list_comparator_alph (const void *first_arg, const void *second_arg){
352 const if_info_t *first = first_arg, *second = second_arg;
354 if (first != NULL && first->description != NULL &&
355 second != NULL && second->description != NULL) {
356 return g_strcasecmp(first->description, second->description);
362 /* start getting capture stats from all interfaces */
364 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
374 GtkWidget *decryption_cm;
379 #if GTK_MAJOR_VERSION < 2
380 GtkAccelGroup *accel_group;
382 GtkTooltips *tooltips;
385 GtkRequisition requisition;
387 if_dlg_data_t *if_dlg_data;
393 GString *if_tool_str = g_string_new("");
397 if (cap_if_w != NULL) {
398 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
399 reactivate_window(cap_if_w);
404 /* Is WPcap loaded? */
408 detailed_err = cant_load_winpcap_err("Wireshark");
409 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
410 g_free(detailed_err);
415 /* LOAD THE INTERFACES */
416 if_list = capture_interface_list(&err, &err_str);
417 if_list = g_list_sort (if_list, if_list_comparator_alph);
418 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
419 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
425 /* LOAD AIRPCAP INTERFACES */
426 airpcap_if_list = get_airpcap_interface_list(&err, &err_str);
427 if (airpcap_if_list == NULL)
428 airpcap_if_active = airpcap_if_selected = NULL;
430 decryption_cm = OBJECT_GET_DATA(airpcap_tb,AIRPCAP_TOOLBAR_DECRYPTION_KEY);
431 update_decryption_mode_list(decryption_cm);
433 if (airpcap_if_list == NULL && err == CANT_GET_AIRPCAP_INTERFACE_LIST) {
435 /* XXX - Do we need to show an error here? */
436 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
441 /* If no airpcap interface is present, gray everything */
442 if (airpcap_if_active == NULL) {
443 if (airpcap_if_list == NULL) {
444 /*No airpcap device found */
445 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
447 /* default adapter is not airpcap... or is airpcap but is not found*/
448 airpcap_set_toolbar_stop_capture(airpcap_if_active);
449 airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
453 airpcap_set_toolbar_start_capture(airpcap_if_active);
456 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Capture Interfaces");
458 tooltips = gtk_tooltips_new();
460 #if GTK_MAJOR_VERSION < 2
461 /* Accelerator group for the accelerators (or, as they're called in
462 Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
463 Ctrl+<key> is an accelerator). */
464 accel_group = gtk_accel_group_new();
465 gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
468 main_sw = gtk_scrolled_window_new(NULL, NULL);
469 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(main_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
470 gtk_container_add(GTK_CONTAINER(cap_if_w), main_sw);
472 main_vb = gtk_vbox_new(FALSE, 0);
473 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
474 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(main_sw), main_vb);
477 if_tb = gtk_table_new(1,9, FALSE);
478 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
479 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
480 gtk_box_pack_start(GTK_BOX(main_vb), if_tb, FALSE, FALSE, 0);
485 /* This is the icon column, used to display which kind of interface we have */
486 if_lb = gtk_label_new("");
487 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
491 * On Windows, device names are generally not meaningful - NT 5
492 * uses long blobs with GUIDs in them, for example - so we don't
493 * bother showing them.
495 if_lb = gtk_label_new("Device");
496 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
499 if_lb = gtk_label_new("Description");
500 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
502 if_lb = gtk_label_new(" IP ");
503 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
505 if_lb = gtk_label_new("Packets");
506 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
508 if_lb = gtk_label_new(" Packets/s ");
509 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 5, 6, row, row+1);
511 stop_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_STOP);
512 gtk_tooltips_set_tip(tooltips, stop_bt,
513 "Stop a running capture.", NULL);
515 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 9, row, row+1);
517 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 6, 8, row, row+1);
519 SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
522 gtk_widget_size_request(stop_bt, &requisition);
523 height += requisition.height + 15;
525 /* Start gathering statistics (using dumpcap) */
526 sc = capture_stat_start(if_list);
528 /* List the interfaces */
529 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
530 g_string_assign(if_tool_str, "");
531 if_info = curr->data;
532 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
533 if_dlg_data->if_info = *if_info;
535 /* Kind of adaptor (icon) */
537 if(get_airpcap_if_from_name(airpcap_if_list,if_info->name) != NULL)
538 icon = xpm_to_widget(capture_airpcap_16_xpm);
540 icon = xpm_to_widget(capture_ethernet_16_xpm);
542 icon = xpm_to_widget(capture_ethernet_16_xpm);
545 gtk_table_attach_defaults(GTK_TABLE(if_tb), icon, 0, 1, row, row+1);
548 if_dlg_data->device_lb = gtk_label_new(if_info->name);
549 if_dlg_data->device = if_info->name;
551 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
552 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 1, 2, row, row+1);
554 g_string_append(if_tool_str, "Device: ");
555 g_string_append(if_tool_str, if_info->name);
556 g_string_append(if_tool_str, "\n");
559 if (if_info->description != NULL)
560 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
562 if_dlg_data->descr_lb = gtk_label_new("");
563 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
564 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 2, 3, row, row+1);
566 if (if_info->description) {
567 g_string_append(if_tool_str, "Description: ");
568 g_string_append(if_tool_str, if_info->description);
569 g_string_append(if_tool_str, "\n");
573 /* only the first IP address will be shown */
574 g_string_append(if_tool_str, "IP: ");
575 curr_ip = g_slist_nth(if_info->ip_addr, 0);
577 ip_addr = (if_addr_t *)curr_ip->data;
578 switch (ip_addr->type) {
581 tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
585 tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
589 g_assert_not_reached();
592 if_dlg_data->ip_lb = gtk_label_new(tmp_str);
593 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
594 g_string_append(if_tool_str, tmp_str);
596 if_dlg_data->ip_lb = gtk_label_new("unknown");
597 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
598 g_string_append(if_tool_str, "unknown");
600 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 3, 4, row, row+1);
601 g_string_append(if_tool_str, "\n");
604 if_dlg_data->curr_lb = gtk_label_new("-");
605 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 4, 5, row, row+1);
608 if_dlg_data->last_lb = gtk_label_new("-");
609 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 5, 6, row, row+1);
612 if_dlg_data->capture_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_START);
613 SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
614 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
615 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
618 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 6, 7, row, row+1);
621 if_dlg_data->prepare_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_OPTIONS);
622 SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
623 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
624 "Open the capture options dialog with this interface selected.", NULL);
625 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 7, 8, row, row+1);
629 if_dlg_data->details_bt = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_CAPTURE_DETAILS);
630 SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
631 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
632 "Open the capture details dialog of this interface.", NULL);
633 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 8, 9, row, row+1);
636 if_data = g_list_append(if_data, if_dlg_data);
640 /* Lets add up 10 rows of interfaces, otherwise the window may become too high */
641 gtk_widget_size_request(GTK_WIDGET(if_dlg_data->prepare_bt), &requisition);
642 height += requisition.height;
646 g_string_free(if_tool_str, TRUE);
648 /* Button row: close button */
649 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
650 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
652 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
654 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
656 close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
657 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
658 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
660 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
661 help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
662 SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
665 gtk_widget_size_request(GTK_WIDGET(close_bt), &requisition);
666 /* height + static offset + what GTK-Wimp needs in addition per interface */
667 height += requisition.height + 20 + ifs;
668 gtk_window_set_default_size(GTK_WINDOW(cap_if_w), -1, height);
670 gtk_widget_grab_default(close_bt);
672 SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
673 SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, sc);
675 gtk_widget_show_all(cap_if_w);
676 window_present(cap_if_w);
678 set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
680 /* update the interface list every 1000ms */
681 timer_id = gtk_timeout_add(1000, update_all, sc);
685 #endif /* HAVE_LIBPCAP */