2 * Routines for the capture interface dialog
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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>
41 #include "pcap-util.h"
44 #include "capture-wpcap.h"
47 #include "compat_macros.h"
48 #include "simple_dialog.h"
50 #include "capture_dlg.h"
51 #include "capture_if_details_dlg.h"
52 #include "capture_errs.h"
54 #include "gui_utils.h"
55 #include "dlg_utils.h"
63 * Keep a static pointer to the current "Capture Interfaces" window, if
64 * any, so that if somebody tries to do "Capture:Start" while there's
65 * already a "Capture Interfaces" window up, we just pop up the existing
66 * one, rather than creating a new one.
68 static GtkWidget *cap_if_w;
70 GList *if_data = NULL;
79 * Timeout, in milliseconds, for reads from the stream of captured packets.
81 #define CAP_READ_TIMEOUT 250
84 /* the "runtime" data of one interface */
85 typedef struct if_dlg_data_s {
92 GtkWidget *capture_bt;
93 GtkWidget *prepare_bt;
95 GtkWidget *details_bt;
101 void update_if(if_dlg_data_t *if_dlg_data);
104 /* start capture button was pressed */
106 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
108 if_dlg_data_t *if_dlg_data = if_data;
110 if (capture_opts->iface)
111 g_free(capture_opts->iface);
113 capture_opts->iface = g_strdup(if_dlg_data->device);
115 /* XXX - remove this? */
116 if (capture_opts->save_file) {
117 g_free(capture_opts->save_file);
118 capture_opts->save_file = NULL;
121 /* stop capturing from all interfaces, we are going to do real work now ... */
122 window_destroy(cap_if_w);
124 capture_start_cb(NULL, NULL);
128 /* prepare capture button was pressed */
130 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
132 if_dlg_data_t *if_dlg_data = if_data;
134 if (capture_opts->iface)
135 g_free(capture_opts->iface);
137 capture_opts->iface = g_strdup(if_dlg_data->device);
139 /* stop capturing from all interfaces, we are going to do real work now ... */
140 window_destroy(cap_if_w);
142 capture_prep_cb(NULL, NULL);
147 /* capture details button was pressed */
149 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
151 if_dlg_data_t *if_dlg_data = if_data;
154 capture_if_details_open(if_dlg_data->device);
159 /* open a single interface */
161 open_if(gchar *name, if_dlg_data_t *if_dlg_data)
163 gchar open_err_str[PCAP_ERRBUF_SIZE];
166 * XXX - on systems with BPF, the number of BPF devices limits the
167 * number of devices on which you can capture simultaneously.
171 * 1) this might fail if you run out of BPF devices
175 * 2) opening every interface could leave too few BPF devices
176 * for *other* programs.
178 * It also means the system could end up getting a lot of traffic
179 * that it has to pass through the networking stack and capture
180 * mechanism, so opening all the devices and presenting packet
181 * counts might not always be a good idea.
183 if_dlg_data->pch = pcap_open_live(name,
185 capture_opts->promisc_mode, CAP_READ_TIMEOUT,
188 if (if_dlg_data->pch != NULL) {
189 update_if(if_dlg_data);
191 printf("open_if: %s\n", open_err_str);
192 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
193 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
197 /* update a single interface */
199 update_if(if_dlg_data_t *if_dlg_data)
201 struct pcap_stat stats;
206 /* pcap_stats() stats values differ on libpcap and winpcap!
207 * libpcap: returns the number of packets since pcap_open_live
208 * winpcap: returns the number of packets since the last pcap_stats call
209 * XXX - if that's true, that's a bug, and should be fixed; "pcap_stats()"
210 * is supposed to work the same way on all platforms, including Windows.
211 * Note that the WinPcap 3.0 documentation says "The values represent
212 * packet statistics from the start of the run to the time of the call."
213 * (Note also that some versions of libpcap, on some versions of UN*X,
214 * have the same bug.)
216 if (if_dlg_data->pch) {
217 if(pcap_stats(if_dlg_data->pch, &stats) >= 0) {
219 diff = stats.ps_recv - if_dlg_data->last_packets;
220 if_dlg_data->last_packets = stats.ps_recv;
222 diff = stats.ps_recv;
223 if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
226 str = g_strdup_printf("%u", if_dlg_data->last_packets);
227 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
229 str = g_strdup_printf("%u", diff);
230 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
233 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
234 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
236 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
237 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
243 /* close a single interface */
245 close_if(if_dlg_data_t *if_dlg_data)
248 pcap_close(if_dlg_data->pch);
253 /* update all interfaces */
255 update_all(gpointer data)
265 for(ifs = 0; (curr = g_list_nth(data, ifs)); ifs++) {
266 update_if(curr->data);
273 /* a live capture has started or stopped */
275 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
281 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
283 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
284 if_dlg_data_t *if_dlg_data = curr->data;
286 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
287 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
293 /* the window was closed, cleanup things */
295 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
300 gtk_timeout_remove(timer_id);
302 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
303 if_dlg_data_t *if_dlg_data = curr->data;
305 close_if(if_dlg_data);
311 free_interface_list(if_list);
313 /* Note that we no longer have a "Capture Options" dialog box. */
318 /* start getting capture stats from all interfaces */
320 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
322 GtkWidget *main_vb, *bbox, *close_bt, *help_bt;
326 #if GTK_MAJOR_VERSION < 2
327 GtkAccelGroup *accel_group;
329 GtkTooltips *tooltips;
331 char err_str[PCAP_ERRBUF_SIZE];
332 gchar *cant_get_if_list_errstr;
334 if_dlg_data_t *if_dlg_data;
340 GString *if_tool_str = g_string_new("");
343 if (cap_if_w != NULL) {
344 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
345 reactivate_window(cap_if_w);
350 /* Is WPcap loaded? */
354 detailed_err = cant_load_winpcap_err("Ethereal");
355 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
356 g_free(detailed_err);
361 if_list = get_interface_list(&err, err_str);
362 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
363 cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
364 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
365 cant_get_if_list_errstr);
366 g_free(cant_get_if_list_errstr);
370 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Capture Interfaces");
372 tooltips = gtk_tooltips_new();
374 #if GTK_MAJOR_VERSION < 2
375 /* Accelerator group for the accelerators (or, as they're called in
376 Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
377 Ctrl+<key> is an accelerator). */
378 accel_group = gtk_accel_group_new();
379 gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
382 main_vb = gtk_vbox_new(FALSE, 0);
383 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
384 gtk_container_add(GTK_CONTAINER(cap_if_w), main_vb);
387 if_tb = gtk_table_new(6,1, FALSE);
388 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
389 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
390 gtk_container_add(GTK_CONTAINER(main_vb), if_tb);
396 * On Windows, device names are generally not meaningful - NT 5
397 * uses long blobs with GUIDs in them, for example - so we don't
398 * bother showing them.
400 if_lb = gtk_label_new("Device");
401 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
404 if_lb = gtk_label_new("Description");
405 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
407 if_lb = gtk_label_new(" IP ");
408 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
410 if_lb = gtk_label_new("Packets");
411 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
413 if_lb = gtk_label_new(" Packets/s ");
414 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
416 stop_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_STOP);
417 gtk_tooltips_set_tip(tooltips, stop_bt,
418 "Stop a running capture.", NULL);
420 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 8, row, row+1);
422 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 7, row, row+1);
424 SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
428 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
429 g_string_assign(if_tool_str, "");
430 if_info = curr->data;
431 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
434 if_dlg_data->device_lb = gtk_label_new(if_info->name);
435 if_dlg_data->device = if_info->name;
437 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
438 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 0, 1, row, row+1);
440 g_string_append(if_tool_str, "Device: ");
441 g_string_append(if_tool_str, if_info->name);
442 g_string_append(if_tool_str, "\n");
445 if (if_info->description != NULL)
446 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
448 if_dlg_data->descr_lb = gtk_label_new("");
449 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
450 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 1, 2, row, row+1);
452 if (if_info->description) {
453 g_string_append(if_tool_str, "Description: ");
454 g_string_append(if_tool_str, if_info->description);
455 g_string_append(if_tool_str, "\n");
459 /* only the first IP address will be shown */
460 g_string_append(if_tool_str, "IP: ");
461 curr_ip = g_slist_nth(if_info->ip_addr, 0);
463 ip_addr = (if_addr_t *)curr_ip->data;
464 switch (ip_addr->type) {
467 tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
471 tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
475 g_assert_not_reached();
478 if_dlg_data->ip_lb = gtk_label_new(tmp_str);
479 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
480 g_string_append(if_tool_str, tmp_str);
482 if_dlg_data->ip_lb = gtk_label_new("unknown");
483 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
484 g_string_append(if_tool_str, "unknown");
486 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 2, 3, row, row+1);
487 g_string_append(if_tool_str, "\n");
490 if_dlg_data->curr_lb = gtk_label_new("-");
491 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 3, 4, row, row+1);
494 if_dlg_data->last_lb = gtk_label_new("-");
495 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 4, 5, row, row+1);
498 if_dlg_data->capture_bt = gtk_button_new_with_label("Capture");
499 SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
500 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
501 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
504 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 5, 6, row, row+1);
507 if_dlg_data->prepare_bt = gtk_button_new_with_label("Prepare");
508 SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
509 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
510 "Open the capture options dialog with this interface selected.", NULL);
511 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 6, 7, row, row+1);
515 if_dlg_data->details_bt = gtk_button_new_with_label("Details");
516 SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
517 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
518 "Open the capture details dialog of this interface.", NULL);
519 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 7, 8, row, row+1);
522 open_if(if_info->name, if_dlg_data);
524 if_data = g_list_append(if_data, if_dlg_data);
529 g_string_free(if_tool_str, TRUE);
531 /* Button row: close button */
532 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
533 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
535 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
537 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
539 close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
540 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
541 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
543 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
544 help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
545 SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
548 gtk_widget_grab_default(close_bt);
550 SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
551 SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, NULL);
553 gtk_widget_show_all(cap_if_w);
554 window_present(cap_if_w);
556 set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
558 /* update the interface list every 1000ms */
559 timer_id = gtk_timeout_add(1000, update_all, if_data);
563 #endif /* HAVE_LIBPCAP */