Fix the wireless settings button for AirPCap devices in the
[obnox/wireshark/wip.git] / gtk / capture_if_dlg.c
1 /* capture_if_dlg.c
2  * Routines for the capture interface dialog
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #ifdef HAVE_LIBPCAP
32
33 #include <string.h>
34
35 #ifdef __linux__
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #endif
39
40 #include <epan/prefs.h>
41
42 #include "../capture_errs.h"
43 #include "../capture_ifinfo.h"
44 #include "../simple_dialog.h"
45 #include "../capture.h"
46 #include "../capture-pcap-util.h"
47 #include "../capture_ui_utils.h"
48 #include "wsutil/file_util.h"
49 #include <wiretap/wtap.h>
50
51 #ifdef _WIN32
52 #include "../capture-wpcap.h"
53 #include "gtk/capture_if_details_dlg_win32.h"
54 #endif
55
56 #include "gtk/stock_icons.h"
57 #include "gtk/capture_dlg.h"
58 #include "gtk/capture_if_dlg.h"
59 #include "gtk/recent.h"
60 #include "gtk/gui_utils.h"
61 #include "gtk/dlg_utils.h"
62 #include "gtk/main.h"
63 #include "gtk/main_toolbar.h"
64 #include "gtk/help_dlg.h"
65 #include "gtk/keys.h"
66 #include "gtk/webbrowser.h"
67 #include "gtk/capture_globals.h"
68 #include "gtk/network_icons.h"
69 #include "gtk/main_welcome.h"
70 #include "gtk/menus.h"
71
72 #include "gtk/old-gtk-compat.h"
73
74 #ifdef HAVE_AIRPCAP
75 #include "../image/toolbar/capture_airpcap_16.xpm"
76 #endif
77
78 #ifdef _WIN32
79 #include "../image/toolbar/capture_ethernet_16.xpm"
80 #include "../image/toolbar/modem_16.xpm"
81 #endif
82
83 #include "../image/toolbar/network_virtual_16.xpm"
84
85 /* new buttons to be used instead of labels for 'Capture','Prepare',' */
86 /*#include "../image/toolbar/capture_capture_16.xpm"*/
87 /*#include "../image/toolbar/capture_prepare_16.xpm"*/
88 /*#include "../image/toolbar/capture_details_16.xpm"*/
89
90
91 #ifdef HAVE_AIRPCAP
92 #include "airpcap.h"
93 #include "airpcap_loader.h"
94 #include "airpcap_gui_utils.h"
95 #include "airpcap_dlg.h"
96 #endif
97
98 #define CAPTURE_IF_IP_ADDR_LABEL      "capture_if_ip_addr_label"
99 #define CAPTURE_IF_SELECTED_IP_ADDR   "capture_if_selected_ip_addr"
100
101 /*
102  * Keep a static pointer to the current "Capture Interfaces" window, if
103  * any, so that if somebody tries to do "Capture:Start" while there's
104  * already a "Capture Interfaces" window up, we just pop up the existing
105  * one, rather than creating a new one.
106  */
107 static GtkWidget *cap_if_w;
108
109 static GList     *if_data_list = NULL;
110
111 static guint     timer_id;
112
113 static GtkWidget *stop_bt, *capture_bt, *options_bt;
114
115 static GList     *if_list;
116
117 static guint     currently_selected = 0;
118
119 static if_stat_cache_t   *sc;
120
121 /*
122  * Timeout, in milliseconds, for reads from the stream of captured packets.
123  */
124 #define CAP_READ_TIMEOUT        250
125
126
127 /* the "runtime" data of one interface */
128 typedef struct if_dlg_data_s {
129     GtkWidget   *device_lb;
130     GtkWidget   *descr_lb;
131     GtkWidget   *ip_lb;
132     GtkWidget   *curr_lb;
133     GtkWidget   *last_lb;
134     GtkWidget   *choose_bt;
135 #ifdef _WIN32
136     GtkWidget   *details_bt;
137 #endif
138     guint32     last_packets;
139     gchar       *device;
140     if_info_t   if_info;
141     gboolean    selected;
142 } if_dlg_data_t;
143
144 static gboolean gbl_capture_in_progress = FALSE;
145
146 void
147 update_selected_interface(gchar *name, gboolean activate)
148 {
149   guint ifs;
150   GList *curr;
151   if_dlg_data_t *temp;
152
153   for (ifs = 0; ifs < g_list_length(if_data_list); ifs++) {
154     curr = g_list_nth(if_data_list, ifs);
155     temp = (if_dlg_data_t *)(curr->data);
156     if (strcmp(name, temp->if_info.name) == 0) {
157       if (activate) {
158         gtk_toggle_button_set_active((GtkToggleButton *)temp->choose_bt, TRUE);
159       } else {
160         gtk_toggle_button_set_active((GtkToggleButton *)temp->choose_bt, FALSE);
161       }
162       break;
163     }
164   }
165 }
166
167 static void
168 store_selected(GtkWidget *choose_bt, gpointer if_data)
169 {
170   if_dlg_data_t *if_dlg_data = if_data, *temp;
171   GList *curr;
172   unsigned int ifs, i;
173   gboolean found;
174   cap_settings_t cap_settings;
175   interface_options interface_opts;
176
177   for (ifs = 0; ifs < g_list_length(if_data_list); ifs++) {
178     curr = g_list_nth(if_data_list, ifs);
179     temp = (if_dlg_data_t *)(curr->data);
180     found = FALSE;
181     if (strcmp(if_dlg_data->if_info.name, temp->if_info.name) == 0) {
182       temp->selected ^=1;
183       if_data_list = g_list_remove(if_data_list, curr->data);
184       if_data_list = g_list_insert(if_data_list, temp, ifs);
185       
186       for (i = 0; i < global_capture_opts.ifaces->len; i++) {
187         if (strcmp(g_array_index(global_capture_opts.ifaces, interface_options, i).name, temp->if_info.name) == 0) {
188           found = TRUE;
189           if (!temp->selected) {
190             interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, i);
191             global_capture_opts.ifaces = g_array_remove_index(global_capture_opts.ifaces, i);
192             if (gtk_widget_is_focus(choose_bt) && get_welcome_window()) {
193               change_interface_selection(interface_opts.name, FALSE);
194             }
195             if (gtk_widget_is_focus(choose_bt) && dlg_window_present()) {
196               enable_selected_interface(interface_opts.name, FALSE);
197             }
198             g_free(interface_opts.name);
199             g_free(interface_opts.descr);
200             g_free(interface_opts.cfilter);
201 #ifdef HAVE_PCAP_REMOTE
202             g_free(interface_opts.remote_host);
203             g_free(interface_opts.remote_port);
204             g_free(interface_opts.auth_username);
205             g_free(interface_opts.auth_password);
206 #endif
207             break;
208           }
209         } 
210       } 
211       if (!found && temp->selected) {
212         interface_opts.name = g_strdup(temp->if_info.name);
213         interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
214         interface_opts.linktype = capture_dev_user_linktype_find(interface_opts.name);
215         interface_opts.cfilter = g_strdup(global_capture_opts.default_options.cfilter);
216         interface_opts.has_snaplen = global_capture_opts.default_options.has_snaplen;
217         interface_opts.snaplen = global_capture_opts.default_options.snaplen;
218         cap_settings = capture_get_cap_settings (interface_opts.name);;
219         interface_opts.promisc_mode = global_capture_opts.default_options.promisc_mode;
220 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
221         interface_opts.buffer_size =  global_capture_opts.default_options.buffer_size;
222 #endif
223         interface_opts.monitor_mode = cap_settings.monitor_mode;
224 #ifdef HAVE_PCAP_REMOTE
225         interface_opts.src_type = global_capture_opts.default_options.src_type;
226         interface_opts.remote_host = g_strdup(global_capture_opts.default_options.remote_host);
227         interface_opts.remote_port = g_strdup(global_capture_opts.default_options.remote_port);
228         interface_opts.auth_type = global_capture_opts.default_options.auth_type;
229         interface_opts.auth_username = g_strdup(global_capture_opts.default_options.auth_username);
230         interface_opts.auth_password = g_strdup(global_capture_opts.default_options.auth_password);
231         interface_opts.datatx_udp = global_capture_opts.default_options.datatx_udp;
232         interface_opts.nocap_rpcap = global_capture_opts.default_options.nocap_rpcap;
233         interface_opts.nocap_local = global_capture_opts.default_options.nocap_local;
234 #endif
235 #ifdef HAVE_PCAP_SETSAMPLING
236         interface_opts.sampling_method = global_capture_opts.default_options.sampling_method;
237         interface_opts.sampling_param  = global_capture_opts.default_options.sampling_param;
238 #endif
239         g_array_append_val(global_capture_opts.ifaces, interface_opts);
240         if (gtk_widget_is_focus(choose_bt) && get_welcome_window() != NULL) {
241           change_interface_selection(g_strdup(temp->if_info.name), TRUE);
242         }
243         if (gtk_widget_is_focus(choose_bt) && dlg_window_present()) {
244           enable_selected_interface(interface_opts.name, TRUE);
245         }
246       }
247       
248       if (temp->selected)
249         currently_selected += 1;
250       else
251         currently_selected -= 1;
252       break;
253     }
254   }
255   if (cap_if_w) {
256 #ifdef USE_THREADS
257     gtk_widget_set_sensitive(capture_bt, !gbl_capture_in_progress && (currently_selected > 0));
258 #else
259     gtk_widget_set_sensitive(capture_bt, !gbl_capture_in_progress && (currently_selected == 1));
260 #endif
261   }
262 }
263
264 /* start capture button was pressed */
265 static void
266 #ifdef HAVE_AIRPCAP
267 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data)
268 #else
269 capture_do_cb(GtkWidget *capture_bt _U_, gpointer if_data _U_)
270 #endif
271 {
272   if_dlg_data_t *temp;
273   GList *curr;
274   int ifs;
275 #ifdef HAVE_AIRPCAP
276   if_dlg_data_t *if_dlg_data = if_data;
277
278   airpcap_if_active = get_airpcap_if_from_name(airpcap_if_list, if_dlg_data->if_info.name);
279   airpcap_if_selected = airpcap_if_active;
280 #endif
281
282   for (ifs = 0; (curr = g_list_nth(if_data_list, ifs)); ifs++) {
283     temp = (if_dlg_data_t *)(curr->data);
284     gtk_widget_set_sensitive(temp->choose_bt, FALSE);
285   }
286
287   /* XXX - remove this? */
288   if (global_capture_opts.save_file) {
289     g_free(global_capture_opts.save_file);
290     global_capture_opts.save_file = NULL;
291   }
292
293   if (global_capture_opts.ifaces->len > 1) {
294     global_capture_opts.use_pcapng = TRUE;
295   }
296   /* stop capturing from all interfaces, we are going to do real work now ... */
297   window_destroy(cap_if_w);
298
299   capture_start_cb(NULL, NULL);
300 }
301
302
303 /* prepare capture button was pressed */
304 static void
305 capture_prepare_cb(GtkWidget *prepare_bt _U_, gpointer if_data _U_)
306 {
307   /* stop capturing from all interfaces, we are going to do real work now ... */
308   window_destroy(cap_if_w);
309   if (global_capture_opts.ifaces->len > 1) {
310     global_capture_opts.use_pcapng = TRUE;
311   }
312   capture_prep_cb(NULL, NULL);
313 }
314
315
316 #ifdef _WIN32
317 /* capture details button was pressed */
318 static void
319 capture_details_cb(GtkWidget *details_bt _U_, gpointer if_data)
320 {
321   if_dlg_data_t *if_dlg_data = if_data;
322
323
324   capture_if_details_open(if_dlg_data->device);
325 }
326 #endif
327
328 /* update a single interface */
329 static void
330 update_if(if_dlg_data_t *if_dlg_data, if_stat_cache_t *sc)
331 {
332   struct pcap_stat stats;
333   gchar *str;
334   guint diff;
335
336
337   /*
338    * Note that some versions of libpcap, on some versions of UN*X,
339    * pcap_stats() returns the number of packets since the last
340    * pcap_stats call.
341    *
342    * That's a bug, and should be fixed; "pcap_stats()" is supposed
343    * to work the same way on all platforms.
344    */
345   if (sc) {
346     if (capture_stats(sc, if_dlg_data->device, &stats)) {
347       diff = stats.ps_recv - if_dlg_data->last_packets;
348       if_dlg_data->last_packets = stats.ps_recv;
349
350       str = g_strdup_printf("%u", if_dlg_data->last_packets);
351       gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), str);
352       g_free(str);
353       str = g_strdup_printf("%u", diff);
354       gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), str);
355       g_free(str);
356
357       gtk_widget_set_sensitive(if_dlg_data->curr_lb, diff);
358       gtk_widget_set_sensitive(if_dlg_data->last_lb, diff);
359     } else {
360       gtk_label_set_text(GTK_LABEL(if_dlg_data->curr_lb), "error");
361       gtk_label_set_text(GTK_LABEL(if_dlg_data->last_lb), "error");
362     }
363   }
364 }
365
366 /* update all interfaces */
367 static gboolean
368 update_all(gpointer data)
369 {
370     GList *curr;
371     int ifs;
372     if_stat_cache_t *sc = data;
373
374     if (!cap_if_w) {
375         return FALSE;
376     }
377
378     for (ifs = 0; (curr = g_list_nth(if_data_list, ifs)); ifs++) {
379         update_if(curr->data, sc);
380     }
381
382     return TRUE;
383 }
384
385 /* a live capture has started or stopped */
386 void
387 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress)
388 {
389   gbl_capture_in_progress = capture_in_progress;
390   if (cap_if_w) {
391     gtk_widget_set_sensitive(stop_bt, capture_in_progress);
392 #ifdef USE_THREADS
393     gtk_widget_set_sensitive(capture_bt, !capture_in_progress && (currently_selected > 0));
394 #else
395     gtk_widget_set_sensitive(capture_bt, !capture_in_progress && (currently_selected == 1));
396 #endif
397   }
398 }
399
400
401 /* the window was closed, cleanup things */
402 static void
403 capture_if_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
404 {
405     GList *curr;
406     int ifs;
407
408     g_source_remove(timer_id);
409
410     for (ifs = 0; (curr = g_list_nth(if_data_list, ifs)); ifs++) {
411         g_free(curr->data);
412     }
413
414     if_data_list = NULL;
415
416     free_interface_list(if_list);
417
418     /* Note that we no longer have a "Capture Options" dialog box. */
419     cap_if_w = NULL;
420
421     capture_stat_stop(sc);
422
423 #ifdef HAVE_AIRPCAP
424     airpcap_set_toolbar_stop_capture(airpcap_if_active);
425 #endif
426 }
427
428
429 /*
430  * Sorts the Interface List in alphabetical order
431  */
432 gint if_list_comparator_alph (const void *first_arg, const void *second_arg){
433   const if_info_t *first = first_arg, *second = second_arg;
434
435   if (first != NULL && first->description != NULL &&
436       second != NULL && second->description != NULL) {
437     return g_ascii_strcasecmp(first->description, second->description);
438   } else {
439     return 0;
440   }
441 }
442
443
444 /*
445  * Used to retrieve the interface icon.
446  * This is hideously platform-dependent.
447  */
448 GtkWidget * capture_get_if_icon(const if_info_t* if_info)
449 {
450 #if defined(_WIN32)
451   /*
452    * Much digging failed to reveal any obvious way to get something such
453    * as the SNMP MIB-II ifType value for an interface:
454    *
455    *    http://www.iana.org/assignments/ianaiftype-mib
456    *
457    * by making some NDIS request.
458    */
459   if ( if_info->description && ( strstr(if_info->description,"generic dialup") != NULL ||
460        strstr(if_info->description,"PPP/SLIP") != NULL ) ) {
461     return xpm_to_widget(modem_16_xpm);
462   }
463
464   if ( if_info->description && ( strstr(if_info->description,"Wireless") != NULL ||
465        strstr(if_info->description,"802.11") != NULL || strstr(if_info->description,"AirPcap") != NULL ) ) {
466     return pixbuf_to_widget(network_wireless_pb_data);
467   }
468
469   if ( strstr(if_info->name,"airpcap") != NULL ) {
470     return pixbuf_to_widget(network_wireless_pb_data);
471   }
472
473   if ( if_info->description && strstr(if_info->description, "Bluetooth") != NULL ) {
474     return pixbuf_to_widget(network_bluetooth_pb_data);
475   }
476 #elif defined(__APPLE__)
477   /*
478    * XXX - yes, fetching all the network addresses for an interface
479    * gets you an AF_LINK address, of type "struct sockaddr_dl", and,
480    * yes, that includes an SNMP MIB-II ifType value.
481    *
482    * However, it's IFT_ETHER, i.e. Ethernet, for AirPort interfaces,
483    * not IFT_IEEE80211 (which isn't defined in OS X in any case).
484    *
485    * Perhaps some other BSD-flavored OSes won't make this mistake;
486    * however, FreeBSD 7.0 and OpenBSD 4.2, at least, appear to have
487    * made the same mistake, at least for my Belkin ZyDAS stick.
488    *
489    * On Mac OS X, one might be able to get the information one wants from
490    * IOKit.
491    */
492   if ( strcmp(if_info->name, "en1") == 0) {
493     return pixbuf_to_widget(network_wireless_pb_data);
494   }
495
496   /*
497    * XXX - PPP devices have names beginning with "ppp" and an IFT_ of
498    * IFT_PPP, but they could be dial-up, or PPPoE, or mobile phone modem,
499    * or VPN, or... devices.  One might have to dive into the bowels of
500    * IOKit to find out.
501    */
502
503   /*
504    * XXX - there's currently no support for raw Bluetooth capture,
505    * and IP-over-Bluetooth devices just look like fake Ethernet
506    * devices.  There's also Bluetooth modem support, but that'll
507    * probably just give you a device that looks like a PPP device.
508    */
509 #elif defined(__linux__)
510   /*
511    * Look for /sys/class/net/{device}/wireless.
512    */
513   ws_statb64 statb;
514   char *wireless_path;
515
516   wireless_path = g_strdup_printf("/sys/class/net/%s/wireless", if_info->name);
517   if (wireless_path != NULL) {
518     if (ws_stat64(wireless_path, &statb) == 0) {
519       g_free(wireless_path);
520       return pixbuf_to_widget(network_wireless_pb_data);
521     }
522     g_free(wireless_path);
523   }
524
525   /*
526    * Bluetooth devices.
527    *
528    * XXX - this is for raw Bluetooth capture; what about IP-over-Bluetooth
529    * devices?
530    */
531   if ( strstr(if_info->name,"bluetooth") != NULL) {
532     return pixbuf_to_widget(network_bluetooth_pb_data);
533   }
534
535   /*
536    * USB devices.
537    */
538   if ( strstr(if_info->name,"usbmon") != NULL ) {
539     return pixbuf_to_widget(network_usb_pb_data);
540   }
541 #endif
542
543   /*
544    * TODO: find a better icon!
545    * Bridge, NAT, or host-only interfaces on VMWare hosts have the name
546    * vmnet[0-9]+ or VMnet[0-9+ on Windows. Guests might use a native
547    * (LANCE or E1000) driver or the vmxnet driver. These devices have an
548    * IFT_ of IFT_ETHER, so we have to check the name.
549    */
550   if ( g_ascii_strncasecmp(if_info->name, "vmnet", 5) == 0) {
551     return xpm_to_widget(network_virtual_16_xpm);
552   }
553
554   if ( g_ascii_strncasecmp(if_info->name, "vmxnet", 6) == 0) {
555     return xpm_to_widget(network_virtual_16_xpm);
556   }
557
558   if ( if_info->description && strstr(if_info->description, "VMware") != NULL ) {
559     return xpm_to_widget(network_virtual_16_xpm);
560   }
561
562   return pixbuf_to_widget(network_wired_pb_data);
563 }
564
565
566 static int
567 get_ip_addr_count(GSList *addr_list)
568 {
569   GSList *curr_addr;
570   if_addr_t *addr;
571   int count;
572
573   count = 0;
574   for (curr_addr = addr_list; curr_addr != NULL;
575        curr_addr = g_slist_next(curr_addr)) {
576     addr = (if_addr_t *)curr_addr->data;
577     switch (addr->ifat_type) {
578
579     case IF_AT_IPv4:
580     case IF_AT_IPv6:
581       count++;
582       break;
583
584     default:
585       /* In case we add non-IP addresses */
586       break;
587     }
588   }
589   return count;
590 }
591
592 static const gchar *
593 set_ip_addr_label(GSList *addr_list, GtkWidget *ip_lb, guint selected_ip_addr)
594 {
595   GSList *curr_addr;
596   if_addr_t *addr;
597   const gchar *addr_str = NULL;
598
599   curr_addr = g_slist_nth(addr_list, selected_ip_addr);
600   if (curr_addr) {
601     addr = (if_addr_t *)curr_addr->data;
602     switch (addr->ifat_type) {
603
604     case IF_AT_IPv4:
605       addr_str = ip_to_str((guint8 *)&addr->addr.ip4_addr);
606       break;
607
608     case IF_AT_IPv6:
609       addr_str = ip6_to_str((struct e_in6_addr *)&addr->addr.ip6_addr);
610       break;
611
612     default:
613       /* Ignore non-IP addresses, in case we ever support them */
614       break;
615     }
616   }
617
618   if (addr_str) {
619     gtk_label_set_text(GTK_LABEL(ip_lb), addr_str);
620   } else {
621     gtk_label_set_text(GTK_LABEL(ip_lb), "none");
622   }
623   g_object_set_data(G_OBJECT(ip_lb), CAPTURE_IF_SELECTED_IP_ADDR, GINT_TO_POINTER(selected_ip_addr));
624
625   return addr_str;
626 }
627
628
629 static gboolean
630 ip_label_enter_cb(GtkWidget *eb, GdkEventCrossing *event _U_, gpointer user_data _U_)
631 {
632     gtk_widget_set_state(eb, GTK_STATE_PRELIGHT);
633
634     return FALSE;
635 }
636
637
638 static gboolean
639 ip_label_leave_cb(GtkWidget *eb, GdkEvent *event _U_, gpointer user_data _U_)
640 {
641     gtk_widget_set_state(eb, GTK_STATE_NORMAL);
642
643     return FALSE;
644 }
645
646
647 static gboolean
648 ip_label_press_cb(GtkWidget *widget, GdkEvent *event _U_, gpointer data)
649 {
650   GtkWidget *ip_lb = g_object_get_data(G_OBJECT(widget), CAPTURE_IF_IP_ADDR_LABEL);
651   GSList *addr_list = data;
652   GSList *curr_addr, *start_addr;
653   if_addr_t *addr;
654   guint selected_ip_addr = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(ip_lb), CAPTURE_IF_SELECTED_IP_ADDR));
655
656   /* Select next IP address */
657   start_addr = g_slist_nth(addr_list, selected_ip_addr);
658   for (;;) {
659     selected_ip_addr++;
660     if (g_slist_length(addr_list) <= selected_ip_addr) {
661       /* Wrap around */
662       selected_ip_addr = 0;
663     }
664     curr_addr = g_slist_nth(addr_list, selected_ip_addr);
665     if (curr_addr == start_addr) {
666       /* We wrapped all the way around */
667       break;
668     }
669
670     addr = (if_addr_t *)curr_addr->data;
671     switch (addr->ifat_type) {
672
673     case IF_AT_IPv4:
674     case IF_AT_IPv6:
675       goto found;
676
677     default:
678       /* In case we add non-IP addresses */
679       break;
680     }
681   }
682
683 found:
684   set_ip_addr_label(addr_list, ip_lb, selected_ip_addr);
685
686   return FALSE;
687 }
688
689 static void
690 capture_if_stop_cb(GtkWidget *w _U_, gpointer d _U_)
691 {
692   guint ifs;
693   GList *curr;
694   if_dlg_data_t *if_data;
695
696   for (ifs = 0; ifs < g_list_length(if_data_list); ifs++) {
697     curr = g_list_nth(if_data_list, ifs);
698     if_data = (if_dlg_data_t *)(curr->data);
699     gtk_widget_set_sensitive(if_data->choose_bt, TRUE);
700   }
701   capture_stop_cb(NULL, NULL);
702 }
703
704
705 /* start getting capture stats from all interfaces */
706 void
707 capture_if_cb(GtkWidget *w _U_, gpointer d _U_)
708 {
709   GtkWidget         *main_vb,
710                     *main_sw,
711                     *bbox,
712                     *close_bt,
713                     *help_bt,
714                     *icon;
715
716 #ifdef HAVE_AIRPCAP
717   GtkWidget         *decryption_cb;
718 #endif
719
720   GtkWidget         *if_tb;
721   GtkWidget         *if_lb;
722   GtkWidget         *eb;
723   int               err;
724   gchar             *err_str;
725   GtkRequisition    requisition;
726   int               row, height;
727   if_dlg_data_t     *if_dlg_data = NULL;
728   int               ifs;
729   GList             *curr;
730   if_info_t         *if_info;
731   GString           *if_tool_str = g_string_new("");
732   const gchar       *addr_str;
733   gchar             *user_descr;
734   int               preselected = 0, i;
735   interface_options interface_opts;
736   gboolean      found = FALSE;
737
738   if (cap_if_w != NULL) {
739     /* There's already a "Capture Interfaces" dialog box; reactivate it. */
740     reactivate_window(cap_if_w);
741     return;
742   }
743
744 #ifdef _WIN32
745   /* Is WPcap loaded? */
746   if (!has_wpcap) {
747     char *detailed_err;
748
749     detailed_err = cant_load_winpcap_err("Wireshark");
750     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", detailed_err);
751     g_free(detailed_err);
752     return;
753   }
754 #endif
755   preselected = global_capture_opts.ifaces->len;
756   /* LOAD THE INTERFACES */
757   if_list = capture_interface_list(&err, &err_str);
758   if_list = g_list_sort (if_list, if_list_comparator_alph);
759   if (if_list == NULL) {
760     switch (err) {
761
762     case CANT_GET_INTERFACE_LIST:
763       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
764       g_free(err_str);
765       break;
766
767     case NO_INTERFACES_FOUND:
768       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
769                     "There are no interfaces on which a capture can be done.");
770       break;
771     }
772     return;
773   }
774
775 #ifdef HAVE_AIRPCAP
776   /* LOAD AIRPCAP INTERFACES */
777   airpcap_if_list = get_airpcap_interface_list(&err, &err_str);
778   if (airpcap_if_list == NULL)
779     airpcap_if_active = airpcap_if_selected = NULL;
780
781   decryption_cb = g_object_get_data(G_OBJECT(airpcap_tb),AIRPCAP_TOOLBAR_DECRYPTION_KEY);
782   update_decryption_mode_list(decryption_cb);
783
784   if (airpcap_if_list == NULL && err == CANT_GET_AIRPCAP_INTERFACE_LIST) {
785 #if 0
786     /* XXX - Do we need to show an error here? */
787     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_str);
788 #endif
789     g_free(err_str);
790   }
791
792   /* If no airpcap interface is present, gray everything */
793   if (airpcap_if_active == NULL) {
794     if (airpcap_if_list == NULL) {
795       /*No airpcap device found */
796       airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
797     } else {
798       /* default adapter is not airpcap... or is airpcap but is not found*/
799       airpcap_set_toolbar_stop_capture(airpcap_if_active);
800       airpcap_enable_toolbar_widgets(airpcap_tb,FALSE);
801     }
802   }
803
804   airpcap_set_toolbar_start_capture(airpcap_if_active);
805 #endif
806
807   cap_if_w = dlg_window_new("Wireshark: Capture Interfaces");  /* transient_for top_level */
808   gtk_window_set_destroy_with_parent (GTK_WINDOW(cap_if_w), TRUE);
809
810   main_sw = gtk_scrolled_window_new(NULL, NULL);
811   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(main_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
812   gtk_container_add(GTK_CONTAINER(cap_if_w), main_sw);
813
814   main_vb = gtk_vbox_new(FALSE, 0);
815   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
816   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(main_sw), main_vb);
817
818   if_tb = gtk_table_new(1,9, FALSE);
819   gtk_table_set_row_spacings(GTK_TABLE(if_tb), 3);
820   gtk_table_set_col_spacings(GTK_TABLE(if_tb), 3);
821   gtk_box_pack_start(GTK_BOX(main_vb), if_tb, FALSE, FALSE, 0);
822
823   row = 0;
824   height = 0;
825
826   /* This is the icon column, used to display which kind of interface we have */
827   if_lb = gtk_label_new("");
828   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 0, 1, row, row+1);
829
830 #ifndef _WIN32
831   /*
832    * On Windows, device names are generally not meaningful - NT 5
833    * uses long blobs with GUIDs in them, for example - so we don't
834    * bother showing them.
835    */
836   if_lb = gtk_label_new("Device");
837   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 1, 4, row, row+1);
838 #endif
839   if_lb = gtk_label_new("Description");
840   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 4, 5, row, row+1);
841
842   if_lb = gtk_label_new(" IP ");
843   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 5, 6, row, row+1);
844
845   if_lb = gtk_label_new("Packets");
846   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 6, 7, row, row+1);
847
848   if_lb = gtk_label_new(" Packets/s ");
849   gtk_table_attach_defaults(GTK_TABLE(if_tb), if_lb, 7, 8, row, row+1);
850   row++;
851
852   height += 30;
853   /* Start gathering statistics (using dumpcap) */
854   sc = capture_stat_start(if_list);
855
856   /* List the interfaces */
857   currently_selected = 0;
858   for (ifs = 0; (curr = g_list_nth(if_list, ifs)); ifs++) {
859     g_string_assign(if_tool_str, "");
860     if_info = curr->data;
861
862     /* Continue if capture device is hidden */
863     if (prefs_is_capture_device_hidden(if_info->name)) {
864       continue;
865     }
866
867     if_dlg_data = g_malloc0(sizeof(if_dlg_data_t));
868
869     if (preselected > 0) {
870       found = FALSE;
871       for (i = 0; i < (gint)global_capture_opts.ifaces->len; i++) {
872         interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, i);
873         if ((interface_opts.name == NULL) ||
874             (strcmp(interface_opts.name, (char*)if_info->name) != 0)) {
875           continue;
876         } else {
877           found = TRUE;
878           currently_selected++;
879           preselected--;
880           break;
881         }
882       }
883       if_dlg_data->selected = found;
884     } else {
885       if_dlg_data->selected = FALSE;
886     }
887     if_dlg_data->if_info = *if_info;
888
889     if_dlg_data->choose_bt = gtk_check_button_new();
890     gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->choose_bt, 0, 1, row, row+1);
891     if (gbl_capture_in_progress) {
892       gtk_widget_set_sensitive(if_dlg_data->choose_bt, FALSE);
893     } else {
894       gtk_widget_set_sensitive(if_dlg_data->choose_bt, TRUE);
895     }
896     gtk_toggle_button_set_active((GtkToggleButton *)if_dlg_data->choose_bt, if_dlg_data->selected);
897     g_signal_connect(if_dlg_data->choose_bt, "toggled", G_CALLBACK(store_selected), if_dlg_data);
898      /* Kind of adaptor (icon) */
899 #ifdef HAVE_AIRPCAP
900     if (get_airpcap_if_from_name(airpcap_if_list,if_info->name) != NULL)
901       icon = xpm_to_widget(capture_airpcap_16_xpm);
902     else
903       icon = capture_get_if_icon(if_info);
904 #else
905     icon = capture_get_if_icon(if_info);
906 #endif
907     gtk_table_attach_defaults(GTK_TABLE(if_tb), icon, 1, 2, row, row+1);
908
909       /* device name */
910     if_dlg_data->device_lb = gtk_label_new(if_info->name);
911     if_dlg_data->device = if_info->name;
912 #ifndef _WIN32
913     gtk_misc_set_alignment(GTK_MISC(if_dlg_data->device_lb), 0.0f, 0.5f);
914     gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->device_lb, 2, 4, row, row+1);
915 #endif
916     g_string_append(if_tool_str, "Device: ");
917     g_string_append(if_tool_str, if_info->name);
918     g_string_append(if_tool_str, "\n");
919
920     /* description */
921     user_descr = capture_dev_user_descr_find(if_info->name);
922     if (user_descr) {
923       if_dlg_data->descr_lb = gtk_label_new(user_descr);
924       g_free (user_descr);
925     } else {
926       if (if_info->description)
927         if_dlg_data->descr_lb = gtk_label_new(if_info->description);
928       else
929         if_dlg_data->descr_lb = gtk_label_new("");
930     }
931     gtk_misc_set_alignment(GTK_MISC(if_dlg_data->descr_lb), 0.0f, 0.5f);
932     gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->descr_lb, 4, 5, row, row+1);
933
934     if (if_info->description) {
935       g_string_append(if_tool_str, "Description: ");
936       g_string_append(if_tool_str, if_info->description);
937       g_string_append(if_tool_str, "\n");
938     }
939
940     /* IP address */
941     /* Only one IP address will be shown, start with the first */
942     g_string_append(if_tool_str, "IP: ");
943     if_dlg_data->ip_lb = gtk_label_new("");
944     addr_str = set_ip_addr_label (if_info->addrs, if_dlg_data->ip_lb, 0);
945     if (addr_str) {
946       gtk_widget_set_sensitive(if_dlg_data->ip_lb, TRUE);
947       g_string_append(if_tool_str, addr_str);
948     } else {
949       gtk_widget_set_sensitive(if_dlg_data->ip_lb, FALSE);
950       g_string_append(if_tool_str, "none");
951     }
952     eb = gtk_event_box_new ();
953     gtk_container_add(GTK_CONTAINER(eb), if_dlg_data->ip_lb);
954     gtk_table_attach_defaults(GTK_TABLE(if_tb), eb, 5, 6, row, row+1);
955     if (get_ip_addr_count(if_info->addrs) > 1) {
956       /* More than one IP address, make it possible to toggle */
957       g_object_set_data(G_OBJECT(eb), CAPTURE_IF_IP_ADDR_LABEL, if_dlg_data->ip_lb);
958       g_signal_connect(eb, "enter-notify-event", G_CALLBACK(ip_label_enter_cb), NULL);
959       g_signal_connect(eb, "leave-notify-event", G_CALLBACK(ip_label_leave_cb), NULL);
960       g_signal_connect(eb, "button-press-event", G_CALLBACK(ip_label_press_cb), if_info->addrs);
961     }
962     g_string_append(if_tool_str, "\n");
963
964     /* packets */
965     if_dlg_data->curr_lb = gtk_label_new("-");
966     gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->curr_lb, 6, 7, row, row+1);
967
968     /* packets/s */
969     if_dlg_data->last_lb = gtk_label_new("-");
970     gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->last_lb, 7, 8, row, row+1);
971
972     /* details button */
973 #ifdef _WIN32
974     if_dlg_data->details_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_CAPTURE_DETAILS);
975     gtk_widget_set_tooltip_text(if_dlg_data->details_bt, "Open the capture details dialog of this interface.");
976     gtk_table_attach_defaults(GTK_TABLE(if_tb), if_dlg_data->details_bt, 8, 9, row, row+1);
977     if (capture_if_has_details(if_dlg_data->device)) {
978       g_signal_connect(if_dlg_data->details_bt, "clicked", G_CALLBACK(capture_details_cb), if_dlg_data);
979     } else {
980       gtk_widget_set_sensitive(if_dlg_data->details_bt, FALSE);
981     }
982 #endif
983
984     if_data_list = g_list_append(if_data_list, if_dlg_data);
985     
986     row++;
987     if (row <= 10) {
988         /* Lets add up 10 rows of interfaces, otherwise the window may become too high */
989       gtk_widget_get_preferred_size(GTK_WIDGET(if_dlg_data->choose_bt), &requisition, NULL);
990       height += requisition.height;
991     } 
992   }
993
994   g_string_free(if_tool_str, TRUE);
995
996   /* Button row: close, help, stop, start, and options button */
997   bbox = dlg_button_row_new(GTK_STOCK_HELP, WIRESHARK_STOCK_CAPTURE_START, WIRESHARK_STOCK_CAPTURE_OPTIONS, WIRESHARK_STOCK_CAPTURE_STOP, GTK_STOCK_CLOSE, NULL);
998
999   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
1000   help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
1001   g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)(HELP_CAPTURE_INTERFACES_DIALOG));
1002
1003   stop_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_CAPTURE_STOP);
1004   g_signal_connect(stop_bt, "clicked", G_CALLBACK(capture_if_stop_cb), NULL);
1005   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
1006   window_set_cancel_button(cap_if_w, close_bt, window_cancel_button_cb);
1007   gtk_widget_set_tooltip_text(close_bt, "Close this window.");
1008   options_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_CAPTURE_OPTIONS);
1009   g_signal_connect(options_bt, "clicked", G_CALLBACK(capture_prepare_cb), if_dlg_data);
1010   capture_bt = g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_CAPTURE_START);
1011   g_signal_connect(capture_bt, "clicked", G_CALLBACK(capture_do_cb), if_dlg_data);
1012   gtk_widget_get_preferred_size(GTK_WIDGET(close_bt), &requisition, NULL);
1013   /* height + static offset + what the GTK MS Windows Engine needs in addition per interface */
1014   height += requisition.height + 40 + ifs;
1015   gtk_window_set_default_size(GTK_WINDOW(cap_if_w), -1, height);
1016
1017   gtk_widget_grab_default(close_bt);
1018
1019   g_signal_connect(cap_if_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
1020   g_signal_connect(cap_if_w, "destroy", G_CALLBACK(capture_if_destroy_cb), sc);
1021
1022   gtk_widget_show_all(cap_if_w);
1023   window_present(cap_if_w);
1024
1025   set_capture_if_dialog_for_capture_in_progress(gbl_capture_in_progress);
1026
1027   /* update the interface list every 1000ms */
1028   timer_id = g_timeout_add(1000, update_all, sc);
1029 }
1030
1031 gboolean interfaces_dialog_window_present(void)
1032 {
1033   return (cap_if_w?TRUE:FALSE);
1034 }
1035
1036 void refresh_if_window(void)
1037 {
1038   capture_if_destroy_cb(NULL, NULL);
1039   capture_if_cb(NULL, NULL);
1040 }
1041
1042 void select_all_interfaces(gboolean enable)
1043 {
1044   if_dlg_data_t *temp;
1045   guint ifs;
1046   GList *curr;
1047
1048   for (ifs = 0; ifs < g_list_length(if_data_list); ifs++) {
1049     curr = g_list_nth(if_data_list, ifs);
1050     temp = (if_dlg_data_t *)(curr->data);
1051     update_selected_interface(temp->if_info.name, enable);
1052  } 
1053 }
1054
1055 void destroy_if_window(void)
1056 {
1057   if (cap_if_w) {
1058     window_destroy(cap_if_w);
1059   }
1060 }
1061 #else /* HAVE_LIBPCAP */
1062
1063 void
1064 set_capture_if_dialog_for_capture_in_progress(gboolean capture_in_progress _U_)
1065 {
1066 }
1067
1068 #endif /* HAVE_LIBPCAP */