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