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