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>
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 "gui_utils.h"
53 #include "dlg_utils.h"
61 * Keep a static pointer to the current "Capture Interfaces" window, if
62 * any, so that if somebody tries to do "Capture:Start" while there's
63 * already a "Capture Interfaces" window up, we just pop up the existing
64 * one, rather than creating a new one.
66 static GtkWidget *cap_if_w;
68 GList *if_data = NULL;
77 * Timeout, in milliseconds, for reads from the stream of captured packets.
79 #define CAP_READ_TIMEOUT 250
82 /* the "runtime" data of one interface */
83 typedef struct if_dlg_data_s {
90 GtkWidget *capture_bt;
91 GtkWidget *prepare_bt;
93 GtkWidget *details_bt;
99 void update_if(if_dlg_data_t *if_dlg_data);
102 /* start capture button was pressed */
104 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
106 if_dlg_data_t *if_dlg_data = if_data;
108 if (capture_opts->iface)
109 g_free(capture_opts->iface);
111 capture_opts->iface = g_strdup(if_dlg_data->device);
113 /* XXX - remove this? */
114 if (capture_opts->save_file) {
115 g_free(capture_opts->save_file);
116 capture_opts->save_file = NULL;
119 /* stop capturing from all interfaces, we are going to do real work now ... */
120 window_destroy(cap_if_w);
122 capture_start_cb(NULL, NULL);
126 /* prepare capture button was pressed */
128 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data)
130 if_dlg_data_t *if_dlg_data = if_data;
132 if (capture_opts->iface)
133 g_free(capture_opts->iface);
135 capture_opts->iface = g_strdup(if_dlg_data->device);
137 /* stop capturing from all interfaces, we are going to do real work now ... */
138 window_destroy(cap_if_w);
140 capture_prep_cb(NULL, NULL);
145 /* capture details button was pressed */
147 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
149 if_dlg_data_t *if_dlg_data = if_data;
152 capture_if_details_open(if_dlg_data->device);
157 /* open a single interface */
159 open_if(gchar *name, if_dlg_data_t *if_dlg_data)
161 gchar open_err_str[CAPTURE_PCAP_ERRBUF_SIZE];
164 * XXX - on systems with BPF, the number of BPF devices limits the
165 * number of devices on which you can capture simultaneously.
169 * 1) this might fail if you run out of BPF devices
173 * 2) opening every interface could leave too few BPF devices
174 * for *other* programs.
176 * It also means the system could end up getting a lot of traffic
177 * that it has to pass through the networking stack and capture
178 * mechanism, so opening all the devices and presenting packet
179 * counts might not always be a good idea.
181 if_dlg_data->pch = pcap_open_live(name,
183 capture_opts->promisc_mode, CAP_READ_TIMEOUT,
186 if (if_dlg_data->pch != NULL) {
187 update_if(if_dlg_data);
189 printf("open_if: %s\n", open_err_str);
190 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
191 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
195 /* update a single interface */
197 update_if(if_dlg_data_t *if_dlg_data)
199 struct pcap_stat stats;
204 /* pcap_stats() stats values differ on libpcap and winpcap!
205 * libpcap: returns the number of packets since pcap_open_live
206 * winpcap: returns the number of packets since the last pcap_stats call
207 * XXX - if that's true, that's a bug, and should be fixed; "pcap_stats()"
208 * is supposed to work the same way on all platforms, including Windows.
209 * Note that the WinPcap 3.0 documentation says "The values represent
210 * packet statistics from the start of the run to the time of the call."
211 * (Note also that some versions of libpcap, on some versions of UN*X,
212 * have the same bug.)
214 if (if_dlg_data->pch) {
215 if(pcap_stats(if_dlg_data->pch, &stats) >= 0) {
217 diff = stats.ps_recv - if_dlg_data->last_packets;
218 if_dlg_data->last_packets = stats.ps_recv;
220 diff = stats.ps_recv;
221 if_dlg_data->last_packets = stats.ps_recv + if_dlg_data->last_packets;
224 str = g_strdup_printf("%u", if_dlg_data->last_packets);
225 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
227 str = g_strdup_printf("%u", diff);
228 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
231 gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
232 gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
234 gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
235 gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
241 /* close a single interface */
243 close_if(if_dlg_data_t *if_dlg_data)
246 pcap_close(if_dlg_data->pch);
251 /* update all interfaces */
253 update_all(gpointer data)
263 for(ifs = 0; (curr = g_list_nth(data, ifs)); ifs++) {
264 update_if(curr->data);
271 /* a live capture has started or stopped */
273 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
279 gtk_widget_set_sensitive(stop_bt, capture_in_progress);
281 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
282 if_dlg_data_t *if_dlg_data = curr->data;
284 gtk_widget_set_sensitive(if_dlg_data->capture_bt, !capture_in_progress);
285 gtk_widget_set_sensitive(if_dlg_data->prepare_bt, !capture_in_progress);
291 /* the window was closed, cleanup things */
293 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
298 gtk_timeout_remove(timer_id);
300 for(ifs = 0; (curr = g_list_nth(if_data, ifs)); ifs++) {
301 if_dlg_data_t *if_dlg_data = curr->data;
303 close_if(if_dlg_data);
309 free_interface_list(if_list);
311 /* Note that we no longer have a "Capture Options" dialog box. */
316 /* start getting capture stats from all interfaces */
318 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
320 GtkWidget *main_vb, *main_sw, *bbox, *close_bt, *help_bt;
324 #if GTK_MAJOR_VERSION < 2
325 GtkAccelGroup *accel_group;
327 GtkTooltips *tooltips;
329 char err_str[CAPTURE_PCAP_ERRBUF_SIZE];
330 gchar *cant_get_if_list_errstr;
331 GtkRequisition requisition;
333 if_dlg_data_t *if_dlg_data;
339 GString *if_tool_str = g_string_new("");
342 if (cap_if_w != NULL) {
343 /* There's already a "Capture Interfaces" dialog box; reactivate it. */
344 reactivate_window(cap_if_w);
349 /* Is WPcap loaded? */
353 detailed_err = cant_load_winpcap_err("Ethereal");
354 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
355 g_free(detailed_err);
360 if_list = get_interface_list(&err, err_str);
361 if (if_list == NULL && err == CANT_GET_INTERFACE_LIST) {
362 cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
363 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s",
364 cant_get_if_list_errstr);
365 g_free(cant_get_if_list_errstr);
369 cap_if_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Capture Interfaces");
371 tooltips = gtk_tooltips_new();
373 #if GTK_MAJOR_VERSION < 2
374 /* Accelerator group for the accelerators (or, as they're called in
375 Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
376 Ctrl+<key> is an accelerator). */
377 accel_group = gtk_accel_group_new();
378 gtk_window_add_accel_group(GTK_WINDOW(cap_if_w), accel_group);
381 main_sw = gtk_scrolled_window_new(NULL, NULL);
382 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(main_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
383 gtk_container_add(GTK_CONTAINER(cap_if_w), main_sw);
385 main_vb = gtk_vbox_new(FALSE, 0);
386 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
387 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(main_sw), main_vb);
390 if_tb = gtk_table_new(6,1, FALSE);
391 gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
392 gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
393 gtk_box_pack_start(GTK_BOX(main_vb), if_tb, FALSE, FALSE, 0);
400 * On Windows, device names are generally not meaningful - NT 5
401 * uses long blobs with GUIDs in them, for example - so we don't
402 * bother showing them.
404 if_lb = gtk_label_new("Device");
405 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
408 if_lb = gtk_label_new("Description");
409 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 2, row, row+1);
411 if_lb = gtk_label_new(" IP ");
412 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 2, 3, row, row+1);
414 if_lb = gtk_label_new("Packets");
415 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 3, 4, row, row+1);
417 if_lb = gtk_label_new(" Packets/s ");
418 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
420 stop_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_STOP);
421 gtk_tooltips_set_tip(tooltips, stop_bt,
422 "Stop a running capture.", NULL);
424 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 8, row, row+1);
426 gtk_table_attach_defaults(GTK_TABLE(if_tb), stop_bt, 5, 7, row, row+1);
428 SIGNAL_CONNECT(stop_bt, "clicked", capture_stop_cb, NULL);
431 gtk_widget_size_request(stop_bt, &requisition);
432 height += requisition.height + 15;
434 for(ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
435 g_string_assign(if_tool_str, "");
436 if_info = curr->data;
437 if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
440 if_dlg_data->device_lb = gtk_label_new(if_info->name);
441 if_dlg_data->device = if_info->name;
443 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0, 0.5);
444 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 0, 1, row, row+1);
446 g_string_append(if_tool_str, "Device: ");
447 g_string_append(if_tool_str, if_info->name);
448 g_string_append(if_tool_str, "\n");
451 if (if_info->description != NULL)
452 if_dlg_data->descr_lb = gtk_label_new(if_info->description);
454 if_dlg_data->descr_lb = gtk_label_new("");
455 gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0, 0.5);
456 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 1, 2, row, row+1);
458 if (if_info->description) {
459 g_string_append(if_tool_str, "Description: ");
460 g_string_append(if_tool_str, if_info->description);
461 g_string_append(if_tool_str, "\n");
465 /* only the first IP address will be shown */
466 g_string_append(if_tool_str, "IP: ");
467 curr_ip = g_slist_nth(if_info->ip_addr, 0);
469 ip_addr = (if_addr_t *)curr_ip->data;
470 switch (ip_addr->type) {
473 tmp_str = ip_to_str((guint8 *)&ip_addr->ip_addr.ip4_addr);
477 tmp_str = ip6_to_str((struct e_in6_addr *)&ip_addr->ip_addr.ip6_addr);
481 g_assert_not_reached();
484 if_dlg_data->ip_lb = gtk_label_new(tmp_str);
485 gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
486 g_string_append(if_tool_str, tmp_str);
488 if_dlg_data->ip_lb = gtk_label_new("unknown");
489 gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
490 g_string_append(if_tool_str, "unknown");
492 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->ip_lb, 2, 3, row, row+1);
493 g_string_append(if_tool_str, "\n");
496 if_dlg_data->curr_lb = gtk_label_new("-");
497 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 3, 4, row, row+1);
500 if_dlg_data->last_lb = gtk_label_new("-");
501 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 4, 5, row, row+1);
504 if_dlg_data->capture_bt = gtk_button_new_with_label("Capture");
505 SIGNAL_CONNECT(if_dlg_data->capture_bt, "clicked", capture_do_cb, if_dlg_data);
506 tmp_str = g_strdup_printf("Immediately start a capture from this interface:\n\n%s", if_tool_str->str);
507 gtk_tooltips_set_tip(tooltips, if_dlg_data->capture_bt,
510 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->capture_bt, 5, 6, row, row+1);
513 if_dlg_data->prepare_bt = gtk_button_new_with_label("Prepare");
514 SIGNAL_CONNECT(if_dlg_data->prepare_bt, "clicked", capture_prepare_cb, if_dlg_data);
515 gtk_tooltips_set_tip(tooltips, if_dlg_data->prepare_bt,
516 "Open the capture options dialog with this interface selected.", NULL);
517 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->prepare_bt, 6, 7, row, row+1);
521 if_dlg_data->details_bt = gtk_button_new_with_label("Details");
522 SIGNAL_CONNECT(if_dlg_data->details_bt, "clicked", capture_details_cb, if_dlg_data);
523 gtk_tooltips_set_tip(tooltips, if_dlg_data->details_bt,
524 "Open the capture details dialog of this interface.", NULL);
525 gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 7, 8, row, row+1);
528 open_if(if_info->name, if_dlg_data);
530 if_data = g_list_append(if_data, if_dlg_data);
534 /* Lets add up 10 rows of interfaces, otherwise the window may become too high */
535 gtk_widget_size_request(GTK_WIDGET(if_dlg_data->prepare_bt), &requisition);
536 height += requisition.height;
540 g_string_free(if_tool_str, TRUE);
542 /* Button row: close button */
543 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
544 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
546 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
548 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
550 close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
551 window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
552 gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
554 if(topic_available(HELP_CAPTURE_INTERFACES_DIALOG)) {
555 help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
556 SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_CAPTURE_INTERFACES_DIALOG);
559 gtk_widget_size_request(GTK_WIDGET(close_bt), &requisition);
560 /* height + static offset + what GTK-Wimp needs in addition per interface */
561 height += requisition.height + 26 + ifs;
562 gtk_window_set_default_size(GTK_WINDOW(cap_if_w), -1, height);
564 gtk_widget_grab_default(close_bt);
566 SIGNAL_CONNECT(cap_if_w, "delete_event", window_delete_event_cb, NULL);
567 SIGNAL_CONNECT(cap_if_w, "destroy", capture_if_destroy_cb, NULL);
569 gtk_widget_show_all(cap_if_w);
570 window_present(cap_if_w);
572 set_capture_if_dialog_for_capture_in_progress(is_capture_in_progress());
574 /* update the interface list every 1000ms */
575 timer_id = gtk_timeout_add(1000, update_all, if_data);
579 #endif /* HAVE_LIBPCAP */