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;
187 temp.extcap = g_strdup(if_info->extcap);
189 /* Is this interface hidden and, if so, should we include it anyway? */
191 descr = capture_dev_user_descr_find(if_info->name);
192 device.display_name = get_iface_display_name(descr, if_info);
194 device.selected = FALSE;
195 if (prefs_is_capture_device_hidden(if_info->name)) {
196 device.hidden = TRUE;
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));
204 g_string_append(ip_str, "\n");
206 addr = (if_addr_t *)curr_addr->data;
209 char* temp_addr_str = NULL;
210 temp_addr->ifat_type = addr->ifat_type;
211 switch (addr->ifat_type) {
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);
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);
225 /* In case we add non-IP addresses */
228 wmem_free(NULL, temp_addr_str);
234 temp.addrs = g_slist_append(temp.addrs, temp_addr);
237 #ifdef HAVE_PCAP_REMOTE
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;
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;
256 #if defined(HAVE_PCAP_CREATE)
257 device.monitor_mode_enabled = monitor_mode;
258 device.monitor_mode_supported = caps->can_set_rfmon;
261 * Process the list of link-layer header types.
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);
271 link->name = g_strdup_printf("%s (not supported)", data_link_info->name);
273 device.links = g_list_append(device.links, link);
278 * Set the active DLT for the device appropriately.
280 set_active_dlt(&device, global_capture_opts.default_options.linktype);
282 #if defined(HAVE_PCAP_CREATE)
283 device.monitor_mode_enabled = FALSE;
284 device.monitor_mode_supported = FALSE;
286 device.active_dlt = -1;
288 device.addresses = g_strdup(ip_str->str);
289 device.no_addresses = ips;
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;
296 if (!capture_dev_user_snaplen_find(if_info->name, &device.has_snaplen,
298 device.has_snaplen = global_capture_opts.default_options.has_snaplen;
299 device.snaplen = global_capture_opts.default_options.snaplen;
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;
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++;
317 /* Extcap devices start with no cached args */
318 device.external_cap_args_settings = NULL;
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;
324 g_array_insert_val(global_capture_opts.all_ifaces, count, device);
327 free_if_capabilities(caps);
330 g_string_free(ip_str, TRUE);
333 free_interface_list(if_list);
336 * Pipes and stdin are not really discoverable interfaces, so re-add them to
337 * the list of all interfaces (all_ifaces).
339 for (j = 0; j < global_capture_opts.ifaces->len; j++) {
340 interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, j);
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) {
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;
362 #if defined(HAVE_PCAP_CREATE)
363 device.monitor_mode_enabled = interface_opts->monitor_mode;
364 device.monitor_mode_supported = FALSE;
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;
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);
384 g_array_append_val(global_capture_opts.all_ifaces, device);
385 global_capture_opts.num_selected++;
389 g_hash_table_destroy(selected_devices);
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.
399 fill_in_local_interfaces(void(*update_cb)(void))
404 static gboolean initialized = FALSE;
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");
411 /* do the actual work */
412 scan_local_interfaces(update_cb);
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));
420 g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_INFO, "fill_in_local_interfaces() ends, taking %.3fs", elapsed);
424 hide_interface(gchar* new_hide)
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);
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);
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--;
451 device->hidden = FALSE;
454 g_list_free(hidden_devices);
459 update_local_interfaces(void)
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);
472 device->hidden = prefs_is_capture_device_hidden(device->name);
473 fill_from_ifaces(device);
476 #endif /* HAVE_LIBPCAP */
484 * indent-tabs-mode: nil
487 * ex: set shiftwidth=4 tabstop=8 expandtab:
488 * :indentSize=4:tabSize=8:noTabs=true: