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