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_STAT_H
32 # include <sys/stat.h>
35 #ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h>
45 #include "pcap-util.h"
48 #include "capture-wpcap.h"
51 #include "compat_macros.h"
52 #include "simple_dialog.h"
54 #include "capture_dlg.h"
55 #include "capture_if_details_dlg.h"
57 #include "gui_utils.h"
58 #include "dlg_utils.h"
66 * Keep a static pointer to the current "Capture Interfaces" window, if
67 * any, so that if somebody tries to do "Capture:Start" while there's
68 * already a "Capture Interfaces" window up, we just pop up the existing
69 * one, rather than creating a new one.
71 static GtkWidget *cap_if_w;
73 GList *if_data = NULL;
82 * Timeout, in milliseconds, for reads from the stream of captured packets.
84 #define CAP_READ_TIMEOUT 250
87 /* the "runtime" data of one interface */
88 typedef struct if_dlg_data_s {
95 GtkWidget *capture_bt;
96 GtkWidget *prepare_bt;
98 GtkWidget *details_bt;
100 guint32 last_packets;
104 void update_if(if_dlg_data_t *if_dlg_data);
107 /* start capture button was pressed */
109 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
111 if_dlg_data_t *if_dlg_data = if_data;
113 if (capture_opts->iface)
114 g_free(capture_opts->iface);
116 capture_opts->iface = g_strdup(if_dlg_data->device);
118 /* XXX - remove this? */
119 if (capture_opts->save_file) {
120 g_free(capture_opts->save_file);
121 capture_opts->save_file = NULL;
124 /* stop capturing from all interfaces, we are going to do real work now ... */
125 window_destroy(cap_if_w);
127 capture_start_cb(NULL, NULL);
131 /* prepare capture button was pressed */
133 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
135 if_dlg_data_t *if_dlg_data = if_data;
137 if (capture_opts->iface)
138 g_free(capture_opts->iface);
140 capture_opts->iface = g_strdup(if_dlg_data->device);
142 /* stop capturing from all interfaces, we are going to do real work now ... */
143 window_destroy(cap_if_w);
145 capture_prep_cb(NULL, NULL);
150 /* capture details button was pressed */
152 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
154 if_dlg_data_t *if_dlg_data = if_data;
157 capture_if_details_open(if_dlg_data->device);
162 /* open a single interface */
164 open_if(gchar *name, if_dlg_data_t *if_dlg_data)
166 gchar open_err_str[PCAP_ERRBUF_SIZE];
169 * XXX - on systems with BPF, the number of BPF devices limits the
170 * number of devices on which you can capture simultaneously.
174 * 1) this might fail if you run out of BPF devices
178 * 2) opening every interface could leave too few BPF devices
179 * for *other* programs.
181 * It also means the system could end up getting a lot of traffic
182 * that it has to pass through the networking stack and capture
183 * mechanism, so opening all the devices and presenting packet
184 * counts might not always be a good idea.
186 if_dlg_data->pch = pcap_open_live(name,
188 capture_opts->promisc_mode, CAP_READ_TIMEOUT,
191 if (if_dlg_data->pch != NULL) {
192 update_if(if_dlg_data);
194 printf("open_if: %s\n", open_err_str);
195 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
196 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
200 /* update a single interface */
202 update_if(if_dlg_data_t *if_dlg_data)
204 struct pcap_stat stats;
209 /* pcap_stats() stats values differ on libpcap and winpcap!
210 * libpcap: returns the number of packets since pcap_open_live
211 * winpcap: returns the number of packets since the last pcap_stats call
212 * XXX - if that's true, that's a bug, and should be fixed; "pcap_stats()"
213 * is supposed to work the same way on all platforms, including Windows.
214 * Note that the WinPcap 3.0 documentation says "The values represent
215 * packet statistics from the start of the run to the time of the call."
216 * (Note also that some versions of libpcap, on some versions of UN*X,
217 * have the same bug.)
219 if (if_dlg_data->pch) {
220 if(pcap_stats(if_dlg_data->pch, &stats) >= 0) {
222 diff = stats.ps_recv - if_dlg_data->last_packets;
223 if_dlg_data->last_packets = stats.ps_recv;
225 diff = stats.ps_recv;
226 if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
229 str = g_strdup_printf("%u", if_dlg_data->last_packets);
230 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
232 str = g_strdup_printf("%u", diff);
233 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
236 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
237 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
239 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
240 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
246 /* close a single interface */
248 close_if(if_dlg_data_t *if_dlg_data)
251 pcap_close(if_dlg_data->pch);
256 /* update all interfaces */
258 update_all(gpointer data)
268 for(ifs = 0; (curr = g_list_nth(data, ifs)); ifs++) {
269 update_if(curr->data);
276 /* a live capture has started or stopped */
278 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
284 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
286 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
287 if_dlg_data_t *if_dlg_data = curr->data;
289 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
290 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
296 /* the window was closed, cleanup things */
298 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
303 gtk_timeout_remove(timer_id);
305 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
306 if_dlg_data_t *if_dlg_data = curr->data;
308 close_if(if_dlg_data);
314 free_interface_list(if_list);
316 /* Note that we no longer have a "Capture Options" dialog box. */
321 /* start getting capture stats from all interfaces */
323 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
325 GtkWidget *main_vb, *bbox, *close_bt, *help_bt;
329 #if GTK_MAJOR_VERSION < 2
330 GtkAccelGroup *accel_group;
332 GtkTooltips *tooltips;
334 char err_str[PCAP_ERRBUF_SIZE];
335 gchar *cant_get_if_list_errstr;
337 if_dlg_data_t *if_dlg_data;
343 GString *if_tool_str = g_string_new("");
346 if (cap_if_w != NULL) {
347 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
348 reactivate_window(cap_if_w);
353 /* Is WPcap loaded? */
355 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
356 "Unable to load WinPcap (wpcap.dll); Ethereal will not be able\n"
357 "to capture packets.\n\n"
358 "In order to capture packets, WinPcap must be installed; see\n"
360 " http://www.winpcap.org/\n"
364 " http://winpcap.mirror.ethereal.com/\n"
368 " http://www.mirrors.wiretapped.net/security/packet-capture/winpcap/\n"
370 "for a downloadable version of WinPcap and for instructions\n"
371 "on how to install WinPcap.");
376 if_list = get_interface_list(&err, err_str);
377 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
378 cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
379 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
380 cant_get_if_list_errstr);
381 g_free(cant_get_if_list_errstr);
385 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Capture Interfaces");
387 tooltips = gtk_tooltips_new();
389 #if GTK_MAJOR_VERSION < 2
390 /* Accelerator group for the accelerators (or, as they're called in
391 Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
392 Ctrl+<key> is an accelerator). */
393 accel_group = gtk_accel_group_new();
394 gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
397 main_vb = gtk_vbox_new(FALSE, 0);
398 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
399 gtk_container_add(GTK_CONTAINER(cap_if_w), main_vb);
402 if_tb = gtk_table_new(6,1, FALSE);
403 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
404 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
405 gtk_container_add(GTK_CONTAINER(main_vb), if_tb);
411 * On Windows, device names are generally not meaningful - NT 5
412 * uses long blobs with GUIDs in them, for example - so we don't
413 * bother showing them.
415 if_lb = gtk_label_new("Device");
416 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
419 if_lb = gtk_label_new("Description");
420 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
422 if_lb = gtk_label_new(" IP ");
423 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
425 if_lb = gtk_label_new("Packets");
426 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
428 if_lb = gtk_label_new(" Packets/s ");
429 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
431 stop_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_STOP);
432 gtk_tooltips_set_tip(tooltips, stop_bt,
433 "Stop a running capture.", NULL);
435 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 8, row, row+1);
437 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 7, row, row+1);
439 SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
443 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
444 g_string_assign(if_tool_str, "");
445 if_info = curr->data;
446 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
449 if_dlg_data->device_lb = gtk_label_new(if_info->name);
450 if_dlg_data->device = if_info->name;
452 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
453 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 0, 1, row, row+1);
455 g_string_append(if_tool_str, "Device: ");
456 g_string_append(if_tool_str, if_info->name);
457 g_string_append(if_tool_str, "\n");
460 if (if_info->description != NULL)
461 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
463 if_dlg_data->descr_lb = gtk_label_new("");
464 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
465 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 1, 2, row, row+1);
467 if (if_info->description) {
468 g_string_append(if_tool_str, "Description: ");
469 g_string_append(if_tool_str, if_info->description);
470 g_string_append(if_tool_str, "\n");
474 /* only the first IP address will be shown */
475 g_string_append(if_tool_str, "IP: ");
476 curr_ip = g_slist_nth(if_info->ip_addr, 0);
478 ip_addr = (if_addr_t *)curr_ip->data;
479 switch (ip_addr->type) {
482 tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
486 tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
490 g_assert_not_reached();
493 if_dlg_data->ip_lb = gtk_label_new(tmp_str);
494 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
495 g_string_append(if_tool_str, tmp_str);
497 if_dlg_data->ip_lb = gtk_label_new("unknown");
498 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
499 g_string_append(if_tool_str, "unknown");
501 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 2, 3, row, row+1);
502 g_string_append(if_tool_str, "\n");
505 if_dlg_data->curr_lb = gtk_label_new("-");
506 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 3, 4, row, row+1);
509 if_dlg_data->last_lb = gtk_label_new("-");
510 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 4, 5, row, row+1);
513 if_dlg_data->capture_bt = gtk_button_new_with_label("Capture");
514 SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
515 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
516 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
519 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 5, 6, row, row+1);
522 if_dlg_data->prepare_bt = gtk_button_new_with_label("Prepare");
523 SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
524 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
525 "Open the capture options dialog with this interface selected.", NULL);
526 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 6, 7, row, row+1);
530 if_dlg_data->details_bt = gtk_button_new_with_label("Details");
531 SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
532 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
533 "Open the capture details dialog of this interface.", NULL);
534 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 7, 8, row, row+1);
537 open_if(if_info->name, if_dlg_data);
539 if_data = g_list_append(if_data, if_dlg_data);
544 g_string_free(if_tool_str, TRUE);
546 /* Button row: close button */
547 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
548 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
550 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
552 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
554 close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
555 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
556 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
558 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
559 help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
560 SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
563 gtk_widget_grab_default(close_bt);
565 SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
566 SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, NULL);
568 gtk_widget_show_all(cap_if_w);
569 window_present(cap_if_w);
571 set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
573 /* update the interface list every 1000ms */
574 timer_id = gtk_timeout_add(1000, update_all, if_data);
578 #endif /* HAVE_LIBPCAP */