extcap: remove conditional compilation.
[metze/wireshark/wip.git] / ui / iface_lists.c
1 /* iface_lists.c
2  * Code to manage the global list of interfaces and to update widgets/windows
3  * displaying items from those lists
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #ifdef HAVE_LIBPCAP
27
28 #include <string.h>
29
30 #include <glib.h>
31
32 #include <epan/prefs.h>
33 #include <epan/to_str.h>
34
35 #include "ui/capture_ui_utils.h"
36 #include "ui/capture_globals.h"
37 #include "ui/iface_lists.h"
38 #include "../log.h"
39
40 /*
41  * Used when sorting an interface list into alphabetical order by
42  * their friendly names.
43  */
44 gint
45 if_list_comparator_alph(const void *first_arg, const void *second_arg)
46 {
47     const if_info_t *first = (const if_info_t *)first_arg, *second = (const if_info_t *)second_arg;
48
49     if (first != NULL && first->friendly_name != NULL &&
50         second != NULL && second->friendly_name != NULL) {
51         return g_ascii_strcasecmp(first->friendly_name, second->friendly_name);
52     } else {
53         return 0;
54     }
55 }
56
57 /*
58  * Try to populate the given device with options (like capture filter) from
59  * the capture options that are in use for an existing capture interface.
60  * Returns TRUE if the interface is selected for capture and FALSE otherwise.
61  */
62 static gboolean
63 fill_from_ifaces (interface_t *device)
64 {
65     interface_options *interface_opts;
66     guint i;
67
68     for (i = 0; i < global_capture_opts.ifaces->len; i++) {
69         interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, i);
70         if (strcmp(interface_opts->name, device->name) != 0) {
71             continue;
72         }
73
74 #if defined(HAVE_PCAP_CREATE)
75         device->buffer = interface_opts->buffer_size;
76         device->monitor_mode_enabled = interface_opts->monitor_mode;
77 #endif
78         device->pmode = interface_opts->promisc_mode;
79         device->has_snaplen = interface_opts->has_snaplen;
80         device->snaplen = interface_opts->snaplen;
81         g_free(device->cfilter);
82         device->cfilter = g_strdup(interface_opts->cfilter);
83         device->timestamp_type = g_strdup(interface_opts->timestamp_type);
84         if (interface_opts->linktype != -1) {
85             device->active_dlt = interface_opts->linktype;
86         }
87         return TRUE;
88     }
89     return FALSE;
90 }
91
92 /*
93  * Fetch the list of local interfaces with capture_interface_list()
94  * and set the list of "all interfaces" in *capture_opts to include
95  * those interfaces.
96  */
97 void
98 scan_local_interfaces(void (*update_cb)(void))
99 {
100     GList             *if_entry, *lt_entry, *if_list;
101     if_info_t         *if_info, temp;
102     gchar             *descr;
103     if_capabilities_t *caps=NULL;
104     gint              linktype_count;
105     gboolean          monitor_mode;
106     GSList            *curr_addr;
107     int               ips = 0, i;
108     guint             count = 0, j;
109     if_addr_t         *addr, *temp_addr;
110     link_row          *link = NULL;
111     data_link_info_t  *data_link_info;
112     interface_t       device;
113     GString           *ip_str;
114     interface_options *interface_opts;
115     gboolean          found = FALSE;
116     static gboolean   running = FALSE;
117     GHashTable        *selected_devices;
118
119     if (running) {
120         /* scan_local_interfaces internally calls update_cb to process UI events
121            to avoid stuck UI while running possibly slow operations. A side effect
122            of this is that new interface changes can be detected before completing
123            the last one.
124            This return avoids recursive scan_local_interfaces operation. */
125         return;
126     }
127     running = TRUE;
128
129     /*
130      * Clear list of known interfaces (all_ifaces) that will be re-discovered on
131      * scanning, but remember their selection state.
132      *
133      * XXX shouldn't this copy settings (like capture filter) from the "old"
134      * device to the "new" device? Refreshing the interfaces list should
135      * probably just remove disappeared devices and add discovered devices.
136      */
137     selected_devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
138     if (global_capture_opts.all_ifaces->len > 0) {
139         for (i = (int)global_capture_opts.all_ifaces->len-1; i >= 0; i--) {
140             device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
141             if (device.local && device.type != IF_PIPE && device.type != IF_STDIN) {
142                 global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, i);
143                 /*
144                  * Device is about to be destroyed, unmark as selected. It will
145                  * be reselected on rediscovery.
146                  */
147                 if (device.selected) {
148                     gchar *device_name = g_strdup(device.name);
149                     /* g_hash_table_add() only exists since 2.32. */
150                     g_hash_table_replace(selected_devices, device_name, device_name);
151                     global_capture_opts.num_selected--;
152                 }
153
154                 capture_opts_free_interface_t(&device);
155             }
156         }
157     }
158
159     /* Retrieve list of interface information (if_info_t) into if_list. */
160     g_free(global_capture_opts.ifaces_err_info);
161     if_list = capture_interface_list(&global_capture_opts.ifaces_err,
162                                      &global_capture_opts.ifaces_err_info,
163                                      update_cb);
164     count = 0;
165
166     /*
167      * For each discovered interface name, create a new device and add extra
168      * information (like supported DLTs, assigned IP addresses).
169      */
170     for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
171         memset(&device, 0, sizeof(device));
172         if_info = (if_info_t *)if_entry->data;
173         ip_str = g_string_new("");
174         ips = 0;
175         if (strstr(if_info->name, "rpcap:")) {
176             continue;
177         }
178         device.name = g_strdup(if_info->name);
179         device.friendly_name = g_strdup(if_info->friendly_name);
180         device.hidden = FALSE;
181         memset(&temp, 0, sizeof(temp));
182         temp.name = g_strdup(if_info->name);
183         temp.friendly_name = g_strdup(if_info->friendly_name);
184         temp.vendor_description = g_strdup(if_info->vendor_description);
185         temp.loopback = if_info->loopback;
186         temp.type = if_info->type;
187         temp.extcap = g_strdup(if_info->extcap);
188
189         /* Is this interface hidden and, if so, should we include it anyway? */
190
191         descr = capture_dev_user_descr_find(if_info->name);
192         device.display_name = get_iface_display_name(descr, if_info);
193         g_free(descr);
194         device.selected = FALSE;
195         if (prefs_is_capture_device_hidden(if_info->name)) {
196             device.hidden = TRUE;
197         }
198         device.type = if_info->type;
199         monitor_mode = prefs_capture_device_monitor_mode(if_info->name);
200         caps = capture_get_if_capabilities(if_info->name, monitor_mode, NULL, NULL, update_cb);
201         for (; (curr_addr = g_slist_nth(if_info->addrs, ips)) != NULL; ips++) {
202             temp_addr = (if_addr_t *)g_malloc0(sizeof(if_addr_t));
203             if (ips != 0) {
204                 g_string_append(ip_str, "\n");
205             }
206             addr = (if_addr_t *)curr_addr->data;
207             if (addr) {
208                 address addr_str;
209                 char* temp_addr_str = NULL;
210                 temp_addr->ifat_type = addr->ifat_type;
211                 switch (addr->ifat_type) {
212                     case IF_AT_IPv4:
213                         temp_addr->addr.ip4_addr = addr->addr.ip4_addr;
214                         set_address(&addr_str, AT_IPv4, 4, &addr->addr.ip4_addr);
215                         temp_addr_str = address_to_str(NULL, &addr_str);
216                         g_string_append(ip_str, temp_addr_str);
217                         break;
218                     case IF_AT_IPv6:
219                         memcpy(temp_addr->addr.ip6_addr, addr->addr.ip6_addr, sizeof(addr->addr));
220                         set_address(&addr_str, AT_IPv6, 16, addr->addr.ip6_addr);
221                         temp_addr_str = address_to_str(NULL, &addr_str);
222                         g_string_append(ip_str, temp_addr_str);
223                         break;
224                     default:
225                         /* In case we add non-IP addresses */
226                         break;
227                 }
228                 wmem_free(NULL, temp_addr_str);
229             } else {
230                 g_free(temp_addr);
231                 temp_addr = NULL;
232             }
233             if (temp_addr) {
234                 temp.addrs = g_slist_append(temp.addrs, temp_addr);
235             }
236         }
237 #ifdef HAVE_PCAP_REMOTE
238         device.local = TRUE;
239         device.remote_opts.src_type = CAPTURE_IFLOCAL;
240         device.remote_opts.remote_host_opts.remote_host = g_strdup(global_capture_opts.default_options.remote_host);
241         device.remote_opts.remote_host_opts.remote_port = g_strdup(global_capture_opts.default_options.remote_port);
242         device.remote_opts.remote_host_opts.auth_type = global_capture_opts.default_options.auth_type;
243         device.remote_opts.remote_host_opts.auth_username = g_strdup(global_capture_opts.default_options.auth_username);
244         device.remote_opts.remote_host_opts.auth_password = g_strdup(global_capture_opts.default_options.auth_password);
245         device.remote_opts.remote_host_opts.datatx_udp = global_capture_opts.default_options.datatx_udp;
246         device.remote_opts.remote_host_opts.nocap_rpcap = global_capture_opts.default_options.nocap_rpcap;
247         device.remote_opts.remote_host_opts.nocap_local = global_capture_opts.default_options.nocap_local;
248 #endif
249 #ifdef HAVE_PCAP_SETSAMPLING
250         device.remote_opts.sampling_method = global_capture_opts.default_options.sampling_method;
251         device.remote_opts.sampling_param  = global_capture_opts.default_options.sampling_param;
252 #endif
253         linktype_count = 0;
254         device.links = NULL;
255         if (caps != NULL) {
256 #if defined(HAVE_PCAP_CREATE)
257             device.monitor_mode_enabled = monitor_mode;
258             device.monitor_mode_supported = caps->can_set_rfmon;
259 #endif
260             /*
261              * Process the list of link-layer header types.
262              */
263             for (lt_entry = caps->data_link_types; lt_entry != NULL; lt_entry = g_list_next(lt_entry)) {
264                 data_link_info = (data_link_info_t *)lt_entry->data;
265                 link = (link_row *)g_malloc(sizeof(link_row));
266                 if (data_link_info->description != NULL) {
267                     link->dlt = data_link_info->dlt;
268                     link->name = g_strdup(data_link_info->description);
269                 } else {
270                     link->dlt = -1;
271                     link->name = g_strdup_printf("%s (not supported)", data_link_info->name);
272                 }
273                 device.links = g_list_append(device.links, link);
274                 linktype_count++;
275             }
276
277             /*
278              * Set the active DLT for the device appropriately.
279              */
280             set_active_dlt(&device, global_capture_opts.default_options.linktype);
281         } else {
282 #if defined(HAVE_PCAP_CREATE)
283             device.monitor_mode_enabled = FALSE;
284             device.monitor_mode_supported = FALSE;
285 #endif
286             device.active_dlt = -1;
287         }
288         device.addresses = g_strdup(ip_str->str);
289         device.no_addresses = ips;
290         device.local = TRUE;
291         device.if_info = temp;
292         device.last_packets = 0;
293         if (!capture_dev_user_pmode_find(if_info->name, &device.pmode)) {
294             device.pmode = global_capture_opts.default_options.promisc_mode;
295         }
296         if (!capture_dev_user_snaplen_find(if_info->name, &device.has_snaplen,
297                                            &device.snaplen)) {
298             device.has_snaplen = global_capture_opts.default_options.has_snaplen;
299             device.snaplen = global_capture_opts.default_options.snaplen;
300         }
301         device.cfilter      = g_strdup(global_capture_opts.default_options.cfilter);
302         device.timestamp_type = g_strdup(global_capture_opts.default_options.timestamp_type);
303 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
304         if ((device.buffer = capture_dev_user_buffersize_find(if_info->name)) == -1) {
305             device.buffer = global_capture_opts.default_options.buffer_size;
306         }
307 #endif
308
309         /* Copy interface options for active capture devices. */
310         gboolean selected = fill_from_ifaces(&device);
311         /* Restore device selection (for next capture). */
312         if (!device.selected && (selected || g_hash_table_lookup(selected_devices, device.name))) {
313             device.selected = TRUE;
314             global_capture_opts.num_selected++;
315         }
316
317         /* Extcap devices start with no cached args */
318         device.external_cap_args_settings = NULL;
319
320         if (global_capture_opts.all_ifaces->len <= count) {
321             g_array_append_val(global_capture_opts.all_ifaces, device);
322             count = global_capture_opts.all_ifaces->len;
323         } else {
324             g_array_insert_val(global_capture_opts.all_ifaces, count, device);
325         }
326         if (caps != NULL) {
327             free_if_capabilities(caps);
328         }
329
330         g_string_free(ip_str, TRUE);
331         count++;
332     }
333     free_interface_list(if_list);
334
335     /*
336      * Pipes and stdin are not really discoverable interfaces, so re-add them to
337      * the list of all interfaces (all_ifaces).
338      */
339     for (j = 0; j < global_capture_opts.ifaces->len; j++) {
340         interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, j);
341
342         found = FALSE;
343         for (i = 0; i < (int)global_capture_opts.all_ifaces->len; i++) {
344             device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
345             if (strcmp(device.name, interface_opts->name) == 0) {
346                 found = TRUE;
347                 break;
348             }
349         }
350         if (!found) {  /* new interface, maybe a pipe */
351             memset(&device, 0, sizeof(device));
352             device.name         = g_strdup(interface_opts->name);
353             device.display_name = interface_opts->descr ?
354                 g_strdup_printf("%s: %s", device.name, interface_opts->descr) :
355                 g_strdup(device.name);
356             device.hidden       = FALSE;
357             device.selected     = TRUE;
358             device.type         = interface_opts->if_type;
359 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
360             device.buffer = interface_opts->buffer_size;
361 #endif
362 #if defined(HAVE_PCAP_CREATE)
363             device.monitor_mode_enabled = interface_opts->monitor_mode;
364             device.monitor_mode_supported = FALSE;
365 #endif
366             device.pmode = interface_opts->promisc_mode;
367             device.has_snaplen = interface_opts->has_snaplen;
368             device.snaplen = interface_opts->snaplen;
369             device.cfilter = g_strdup(interface_opts->cfilter);
370             device.timestamp_type = g_strdup(interface_opts->timestamp_type);
371             device.active_dlt = interface_opts->linktype;
372             device.addresses    = NULL;
373             device.no_addresses = 0;
374             device.last_packets = 0;
375             device.links        = NULL;
376             device.local        = TRUE;
377             device.if_info.name = g_strdup(interface_opts->name);
378             device.if_info.friendly_name = NULL;
379             device.if_info.vendor_description = g_strdup(interface_opts->descr);
380             device.if_info.addrs = NULL;
381             device.if_info.loopback = FALSE;
382             device.if_info.extcap = g_strdup(interface_opts->extcap);
383
384             g_array_append_val(global_capture_opts.all_ifaces, device);
385             global_capture_opts.num_selected++;
386         }
387     }
388
389     g_hash_table_destroy(selected_devices);
390     running = FALSE;
391 }
392
393 /*
394  * Get the global interface list.  Generate it if we haven't done so
395  * already.  This can be quite time consuming the first time, so
396  * record how long it takes in the info log.
397  */
398 void
399 fill_in_local_interfaces(void(*update_cb)(void))
400 {
401     GTimeVal start_time;
402     GTimeVal end_time;
403     float elapsed;
404     static gboolean initialized = FALSE;
405
406     /* record the time we started, so we can log total time later */
407     g_get_current_time(&start_time);
408     g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() starts");
409
410     if (!initialized) {
411         /* do the actual work */
412         scan_local_interfaces(update_cb);
413         initialized = TRUE;
414     }
415     /* log how long it took */
416     g_get_current_time(&end_time);
417     elapsed = (float) ((end_time.tv_sec - start_time.tv_sec) +
418                        ((end_time.tv_usec - start_time.tv_usec) / 1e6));
419
420     g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() ends, taking %.3fs", elapsed);
421 }
422
423 void
424 hide_interface(gchar* new_hide)
425 {
426     gchar       *tok;
427     guint       i;
428     interface_t *device;
429     gboolean    found = FALSE;
430     GList       *hidden_devices = NULL, *entry;
431     if (new_hide != NULL) {
432         for (tok = strtok (new_hide, ","); tok; tok = strtok(NULL, ",")) {
433             hidden_devices = g_list_append(hidden_devices, tok);
434         }
435     }
436     for (i = 0; i < global_capture_opts.all_ifaces->len; i++) {
437         device = &g_array_index(global_capture_opts.all_ifaces, interface_t, i);
438         found = FALSE;
439         for (entry = hidden_devices; entry != NULL; entry = g_list_next(entry)) {
440             if (strcmp((char *)entry->data, device->name)==0) {
441                 device->hidden = TRUE;
442                 if (device->selected) {
443                     device->selected = FALSE;
444                     global_capture_opts.num_selected--;
445                 }
446                 found = TRUE;
447                 break;
448             }
449         }
450         if (!found) {
451             device->hidden = FALSE;
452         }
453     }
454     g_list_free(hidden_devices);
455     g_free(new_hide);
456 }
457
458 void
459 update_local_interfaces(void)
460 {
461     interface_t *device;
462     gchar *descr;
463     guint i;
464
465     for (i = 0; i < global_capture_opts.all_ifaces->len; i++) {
466         device = &g_array_index(global_capture_opts.all_ifaces, interface_t, i);
467         device->type = capture_dev_user_linktype_find(device->name);
468         g_free(device->display_name);
469         descr = capture_dev_user_descr_find(device->name);
470         device->display_name = get_iface_display_name(descr, &device->if_info);
471         g_free (descr);
472         device->hidden = prefs_is_capture_device_hidden(device->name);
473         fill_from_ifaces(device);
474     }
475 }
476 #endif /* HAVE_LIBPCAP */
477
478 /*
479  * Editor modelines
480  *
481  * Local Variables:
482  * c-basic-offset: 4
483  * tab-width: 8
484  * indent-tabs-mode: nil
485  * End:
486  *
487  * ex: set shiftwidth=4 tabstop=8 expandtab:
488  * :indentSize=4:tabSize=8:noTabs=true:
489  */