2 * Code to manage the global list of interfaces and to update widgets/windows
3 * displaying items from those lists
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #include <epan/prefs.h>
33 #include <epan/to_str.h>
35 #include "ui/capture_ui_utils.h"
36 #include "ui/capture_globals.h"
37 #include "ui/iface_lists.h"
41 * Used when sorting an interface list into alphabetical order by
42 * their friendly names.
45 if_list_comparator_alph(const void *first_arg, const void *second_arg)
47 const if_info_t *first = (const if_info_t *)first_arg, *second = (const if_info_t *)second_arg;
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);
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.
63 fill_from_ifaces (interface_t *device)
65 interface_options *interface_opts;
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) {
74 #if defined(HAVE_PCAP_CREATE)
75 device->buffer = interface_opts->buffer_size;
76 device->monitor_mode_enabled = interface_opts->monitor_mode;
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;
93 * Fetch the list of local interfaces with capture_interface_list()
94 * and set the list of "all interfaces" in *capture_opts to include
98 scan_local_interfaces(void (*update_cb)(void))
100 GList *if_entry, *lt_entry, *if_list;
101 if_info_t *if_info, temp;
103 if_capabilities_t *caps=NULL;
105 gboolean monitor_mode;
109 if_addr_t *addr, *temp_addr;
110 link_row *link = NULL;
111 data_link_info_t *data_link_info;
114 interface_options *interface_opts;
115 gboolean found = FALSE;
116 static gboolean running = FALSE;
117 GHashTable *selected_devices;
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
124 This return avoids recursive scan_local_interfaces operation. */
130 * Clear list of known interfaces (all_ifaces) that will be re-discovered on
131 * scanning, but remember their selection state.
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.
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);
144 * Device is about to be destroyed, unmark as selected. It will
145 * be reselected on rediscovery.
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--;
154 capture_opts_free_interface_t(&device);
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,
167 * For each discovered interface name, create a new device and add extra
168 * information (like supported DLTs, assigned IP addresses).
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("");
175 if (strstr(if_info->name, "rpcap:")) {
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;
188 temp.extcap = g_strdup(if_info->extcap);
190 /* Is this interface hidden and, if so, should we include it anyway? */
192 descr = capture_dev_user_descr_find(if_info->name);
193 device.display_name = get_iface_display_name(descr, if_info);
195 device.selected = FALSE;
196 if (prefs_is_capture_device_hidden(if_info->name)) {
197 device.hidden = TRUE;
199 device.type = if_info->type;
200 monitor_mode = prefs_capture_device_monitor_mode(if_info->name);
201 caps = capture_get_if_capabilities(if_info->name, monitor_mode, NULL, NULL, update_cb);
202 for (; (curr_addr = g_slist_nth(if_info->addrs, ips)) != NULL; ips++) {
203 temp_addr = (if_addr_t *)g_malloc0(sizeof(if_addr_t));
205 g_string_append(ip_str, "\n");
207 addr = (if_addr_t *)curr_addr->data;
210 char* temp_addr_str = NULL;
211 temp_addr->ifat_type = addr->ifat_type;
212 switch (addr->ifat_type) {
214 temp_addr->addr.ip4_addr = addr->addr.ip4_addr;
215 set_address(&addr_str, AT_IPv4, 4, &addr->addr.ip4_addr);
216 temp_addr_str = address_to_str(NULL, &addr_str);
217 g_string_append(ip_str, temp_addr_str);
220 memcpy(temp_addr->addr.ip6_addr, addr->addr.ip6_addr, sizeof(addr->addr));
221 set_address(&addr_str, AT_IPv6, 16, addr->addr.ip6_addr);
222 temp_addr_str = address_to_str(NULL, &addr_str);
223 g_string_append(ip_str, temp_addr_str);
226 /* In case we add non-IP addresses */
229 wmem_free(NULL, temp_addr_str);
235 temp.addrs = g_slist_append(temp.addrs, temp_addr);
238 #ifdef HAVE_PCAP_REMOTE
240 device.remote_opts.src_type = CAPTURE_IFLOCAL;
241 device.remote_opts.remote_host_opts.remote_host = g_strdup(global_capture_opts.default_options.remote_host);
242 device.remote_opts.remote_host_opts.remote_port = g_strdup(global_capture_opts.default_options.remote_port);
243 device.remote_opts.remote_host_opts.auth_type = global_capture_opts.default_options.auth_type;
244 device.remote_opts.remote_host_opts.auth_username = g_strdup(global_capture_opts.default_options.auth_username);
245 device.remote_opts.remote_host_opts.auth_password = g_strdup(global_capture_opts.default_options.auth_password);
246 device.remote_opts.remote_host_opts.datatx_udp = global_capture_opts.default_options.datatx_udp;
247 device.remote_opts.remote_host_opts.nocap_rpcap = global_capture_opts.default_options.nocap_rpcap;
248 device.remote_opts.remote_host_opts.nocap_local = global_capture_opts.default_options.nocap_local;
250 #ifdef HAVE_PCAP_SETSAMPLING
251 device.remote_opts.sampling_method = global_capture_opts.default_options.sampling_method;
252 device.remote_opts.sampling_param = global_capture_opts.default_options.sampling_param;
257 #if defined(HAVE_PCAP_CREATE)
258 device.monitor_mode_enabled = monitor_mode;
259 device.monitor_mode_supported = caps->can_set_rfmon;
262 * Process the list of link-layer header types.
264 for (lt_entry = caps->data_link_types; lt_entry != NULL; lt_entry = g_list_next(lt_entry)) {
265 data_link_info = (data_link_info_t *)lt_entry->data;
266 link = (link_row *)g_malloc(sizeof(link_row));
267 if (data_link_info->description != NULL) {
268 link->dlt = data_link_info->dlt;
269 link->name = g_strdup(data_link_info->description);
272 link->name = g_strdup_printf("%s (not supported)", data_link_info->name);
274 device.links = g_list_append(device.links, link);
279 * Set the active DLT for the device appropriately.
281 set_active_dlt(&device, global_capture_opts.default_options.linktype);
283 #if defined(HAVE_PCAP_CREATE)
284 device.monitor_mode_enabled = FALSE;
285 device.monitor_mode_supported = FALSE;
287 device.active_dlt = -1;
289 device.addresses = g_strdup(ip_str->str);
290 device.no_addresses = ips;
292 device.if_info = temp;
293 device.last_packets = 0;
294 if (!capture_dev_user_pmode_find(if_info->name, &device.pmode)) {
295 device.pmode = global_capture_opts.default_options.promisc_mode;
297 if (!capture_dev_user_snaplen_find(if_info->name, &device.has_snaplen,
299 device.has_snaplen = global_capture_opts.default_options.has_snaplen;
300 device.snaplen = global_capture_opts.default_options.snaplen;
302 device.cfilter = g_strdup(global_capture_opts.default_options.cfilter);
303 device.timestamp_type = g_strdup(global_capture_opts.default_options.timestamp_type);
304 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
305 if ((device.buffer = capture_dev_user_buffersize_find(if_info->name)) == -1) {
306 device.buffer = global_capture_opts.default_options.buffer_size;
310 /* Copy interface options for active capture devices. */
311 gboolean selected = fill_from_ifaces(&device);
312 /* Restore device selection (for next capture). */
313 if (!device.selected && (selected || g_hash_table_lookup(selected_devices, device.name))) {
314 device.selected = TRUE;
315 global_capture_opts.num_selected++;
319 /* Extcap devices start with no cached args */
320 device.external_cap_args_settings = NULL;
322 if (global_capture_opts.all_ifaces->len <= count) {
323 g_array_append_val(global_capture_opts.all_ifaces, device);
324 count = global_capture_opts.all_ifaces->len;
326 g_array_insert_val(global_capture_opts.all_ifaces, count, device);
329 free_if_capabilities(caps);
332 g_string_free(ip_str, TRUE);
335 free_interface_list(if_list);
338 * Pipes and stdin are not really discoverable interfaces, so re-add them to
339 * the list of all interfaces (all_ifaces).
341 for (j = 0; j < global_capture_opts.ifaces->len; j++) {
342 interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, j);
345 for (i = 0; i < (int)global_capture_opts.all_ifaces->len; i++) {
346 device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
347 if (strcmp(device.name, interface_opts->name) == 0) {
352 if (!found) { /* new interface, maybe a pipe */
353 memset(&device, 0, sizeof(device));
354 device.name = g_strdup(interface_opts->name);
355 device.display_name = interface_opts->descr ?
356 g_strdup_printf("%s: %s", device.name, interface_opts->descr) :
357 g_strdup(device.name);
358 device.hidden = FALSE;
359 device.selected = TRUE;
360 device.type = interface_opts->if_type;
361 #ifdef CAN_SET_CAPTURE_BUFFER_SIZE
362 device.buffer = interface_opts->buffer_size;
364 #if defined(HAVE_PCAP_CREATE)
365 device.monitor_mode_enabled = interface_opts->monitor_mode;
366 device.monitor_mode_supported = FALSE;
368 device.pmode = interface_opts->promisc_mode;
369 device.has_snaplen = interface_opts->has_snaplen;
370 device.snaplen = interface_opts->snaplen;
371 device.cfilter = g_strdup(interface_opts->cfilter);
372 device.timestamp_type = g_strdup(interface_opts->timestamp_type);
373 device.active_dlt = interface_opts->linktype;
374 device.addresses = NULL;
375 device.no_addresses = 0;
376 device.last_packets = 0;
379 device.if_info.name = g_strdup(interface_opts->name);
380 device.if_info.friendly_name = NULL;
381 device.if_info.vendor_description = g_strdup(interface_opts->descr);
382 device.if_info.addrs = NULL;
383 device.if_info.loopback = FALSE;
385 device.if_info.extcap = g_strdup(interface_opts->extcap);
388 g_array_append_val(global_capture_opts.all_ifaces, device);
389 global_capture_opts.num_selected++;
393 g_hash_table_destroy(selected_devices);
398 * Get the global interface list. Generate it if we haven't done so
399 * already. This can be quite time consuming the first time, so
400 * record how long it takes in the info log.
403 fill_in_local_interfaces(void(*update_cb)(void))
408 static gboolean initialized = FALSE;
410 /* record the time we started, so we can log total time later */
411 g_get_current_time(&start_time);
412 g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() starts");
415 /* do the actual work */
416 scan_local_interfaces(update_cb);
419 /* log how long it took */
420 g_get_current_time(&end_time);
421 elapsed = (float) ((end_time.tv_sec - start_time.tv_sec) +
422 ((end_time.tv_usec - start_time.tv_usec) / 1e6));
424 g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() ends, taking %.3fs", elapsed);
428 hide_interface(gchar* new_hide)
433 gboolean found = FALSE;
434 GList *hidden_devices = NULL, *entry;
435 if (new_hide != NULL) {
436 for (tok = strtok (new_hide, ","); tok; tok = strtok(NULL, ",")) {
437 hidden_devices = g_list_append(hidden_devices, tok);
440 for (i = 0; i < global_capture_opts.all_ifaces->len; i++) {
441 device = &g_array_index(global_capture_opts.all_ifaces, interface_t, i);
443 for (entry = hidden_devices; entry != NULL; entry = g_list_next(entry)) {
444 if (strcmp((char *)entry->data, device->name)==0) {
445 device->hidden = TRUE;
446 if (device->selected) {
447 device->selected = FALSE;
448 global_capture_opts.num_selected--;
455 device->hidden = FALSE;
458 g_list_free(hidden_devices);
463 update_local_interfaces(void)
469 for (i = 0; i < global_capture_opts.all_ifaces->len; i++) {
470 device = &g_array_index(global_capture_opts.all_ifaces, interface_t, i);
471 device->type = capture_dev_user_linktype_find(device->name);
472 g_free(device->display_name);
473 descr = capture_dev_user_descr_find(device->name);
474 device->display_name = get_iface_display_name(descr, &device->if_info);
476 device->hidden = prefs_is_capture_device_hidden(device->name);
477 fill_from_ifaces(device);
480 #endif /* HAVE_LIBPCAP */
488 * indent-tabs-mode: nil
491 * ex: set shiftwidth=4 tabstop=8 expandtab:
492 * :indentSize=4:tabSize=8:noTabs=true: