2 * Code to manage the global list of interfaces and to update widgets/windows
3 * displaying items from those lists
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include <epan/prefs.h>
35 #include <epan/to_str.h>
37 #include "../capture_ui_utils.h"
39 #include "ui/capture_globals.h"
40 #include "ui/iface_lists.h"
42 capture_options global_capture_opts;
45 * Used when sorting an interface list into alphabetical order by
46 * their friendly names.
49 if_list_comparator_alph(const void *first_arg, const void *second_arg)
51 const if_info_t *first = (const if_info_t *)first_arg, *second = (const if_info_t *)second_arg;
53 if (first != NULL && first->friendly_name != NULL &&
54 second != NULL && second->friendly_name != NULL) {
55 return g_ascii_strcasecmp(first->friendly_name, second->friendly_name);
62 * Fetch the list of local interfaces with capture_interface_list()
63 * and set the list of "all interfaces" in *capture_opts to include
67 scan_local_interfaces(void)
69 GList *if_entry, *lt_entry, *if_list;
70 if_info_t *if_info, *temp;
73 if_capabilities_t *caps=NULL;
75 gboolean monitor_mode;
79 if_addr_t *addr, *temp_addr;
80 link_row *link = NULL;
81 data_link_info_t *data_link_info;
84 interface_options interface_opts;
85 gboolean found = FALSE;
88 if (global_capture_opts.all_ifaces->len > 0) {
89 for (i = (int)global_capture_opts.all_ifaces->len-1; i >= 0; i--) {
90 device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
91 if (device.local && device.type != IF_PIPE && device.type != IF_STDIN) {
92 global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, i);
97 /* Scan through the list and build a list of strings to display. */
98 if_list = capture_interface_list(&err, NULL);
100 for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
101 if_info = (if_info_t *)if_entry->data;
102 ip_str = g_string_new("");
104 if (strstr(if_info->name, "rpcap:")) {
107 device.name = g_strdup(if_info->name);
108 if (if_info->friendly_name != NULL) {
109 device.friendly_name = g_strdup(if_info->friendly_name);
111 device.friendly_name = NULL;
113 device.hidden = FALSE;
114 device.locked = FALSE;
115 temp = (if_info_t *)g_malloc0(sizeof(if_info_t));
116 temp->name = g_strdup(if_info->name);
117 temp->friendly_name = g_strdup(if_info->friendly_name);
118 temp->vendor_description = g_strdup(if_info->vendor_description);
119 temp->loopback = if_info->loopback;
120 /* Is this interface hidden and, if so, should we include it anyway? */
122 /* Do we have a user-supplied description? */
123 descr = capture_dev_user_descr_find(if_info->name);
125 /* Yes, we have a user-supplied description; use it. */
126 if_string = g_strdup_printf("%s: %s", descr, if_info->name);
129 /* No, we don't have a user-supplied description; did we get
130 one from the OS or libpcap? */
131 if (if_info->friendly_name != NULL) {
132 /* We have a friendly name from the OS, use it */
135 * On Windows, if we have a friendly name, just show it,
136 * don't show the name, as that's a string made out of
137 * the device GUID, and not at all friendly.
139 if_string = g_strdup_printf("%s", if_info->friendly_name);
142 * On UN*X, if we have a friendly name, show it along
143 * with the interface name; the interface name is short
144 * and somewhat friendly, and many UN*X users are used
145 * to interface names, so we should show it.
147 if_string = g_strdup_printf("%s: %s", if_info->friendly_name, if_info->name);
149 } else if (if_info->vendor_description != NULL) {
150 /* We have a device description from libpcap - use it. */
151 if_string = g_strdup_printf("%s: %s", if_info->vendor_description, if_info->name);
154 if_string = g_strdup(if_info->name);
157 device.display_name = if_string;
158 device.selected = FALSE;
159 if (prefs_is_capture_device_hidden(if_info->name)) {
160 device.hidden = TRUE;
162 device.type = if_info->type;
163 monitor_mode = prefs_capture_device_monitor_mode(if_info->name);
164 caps = capture_get_if_capabilities(if_info->name, monitor_mode, NULL);
165 for (; (curr_addr = g_slist_nth(if_info->addrs, ips)) != NULL; ips++) {
166 temp_addr = (if_addr_t *)g_malloc0(sizeof(if_addr_t));
168 g_string_append(ip_str, "\n");
170 addr = (if_addr_t *)curr_addr->data;
172 temp_addr->ifat_type = addr->ifat_type;
173 switch (addr->ifat_type) {
175 temp_addr->addr.ip4_addr = addr->addr.ip4_addr;
176 g_string_append(ip_str, ip_to_str((guint8 *)&addr->addr.ip4_addr));
179 memcpy(temp_addr->addr.ip6_addr, addr->addr.ip6_addr, sizeof(addr->addr));
180 g_string_append(ip_str, ip6_to_str((struct e_in6_addr *)&addr->addr.ip6_addr));
183 /* In case we add non-IP addresses */
191 temp->addrs = g_slist_append(temp->addrs, temp_addr);
194 #ifdef HAVE_PCAP_REMOTE
196 device.remote_opts.src_type = CAPTURE_IFLOCAL;
197 device.remote_opts.remote_host_opts.remote_host = g_strdup(global_capture_opts.default_options.remote_host);
198 device.remote_opts.remote_host_opts.remote_port = g_strdup(global_capture_opts.default_options.remote_port);
199 device.remote_opts.remote_host_opts.auth_type = global_capture_opts.default_options.auth_type;
200 device.remote_opts.remote_host_opts.auth_username = g_strdup(global_capture_opts.default_options.auth_username);
201 device.remote_opts.remote_host_opts.auth_password = g_strdup(global_capture_opts.default_options.auth_password);
202 device.remote_opts.remote_host_opts.datatx_udp = global_capture_opts.default_options.datatx_udp;
203 device.remote_opts.remote_host_opts.nocap_rpcap = global_capture_opts.default_options.nocap_rpcap;
204 device.remote_opts.remote_host_opts.nocap_local = global_capture_opts.default_options.nocap_local;
206 #ifdef HAVE_PCAP_SETSAMPLING
207 device.remote_opts.sampling_method = global_capture_opts.default_options.sampling_method;
208 device.remote_opts.sampling_param = global_capture_opts.default_options.sampling_param;
213 #if defined(HAVE_PCAP_CREATE)
214 device.monitor_mode_enabled = monitor_mode;
215 device.monitor_mode_supported = caps->can_set_rfmon;
217 for (lt_entry = caps->data_link_types; lt_entry != NULL; lt_entry = g_list_next(lt_entry)) {
218 data_link_info = (data_link_info_t *)lt_entry->data;
219 if (linktype_count == 0) {
220 device.active_dlt = data_link_info->dlt;
222 link = (link_row *)g_malloc(sizeof(link_row));
223 if (data_link_info->description != NULL) {
224 link->dlt = data_link_info->dlt;
225 link->name = g_strdup_printf("%s", data_link_info->description);
228 link->name = g_strdup_printf("%s (not supported)", data_link_info->name);
230 device.links = g_list_append(device.links, link);
234 #if defined(HAVE_PCAP_CREATE)
235 device.monitor_mode_enabled = FALSE;
236 device.monitor_mode_supported = FALSE;
238 device.active_dlt = -1;
240 device.addresses = g_strdup(ip_str->str);
241 device.no_addresses = ips;
243 device.if_info = *temp;
244 device.last_packets = 0;
245 if ((device.pmode = capture_dev_user_pmode_find(if_info->name)) == -1) {
246 device.pmode = global_capture_opts.default_options.promisc_mode;
248 if ((device.has_snaplen = capture_dev_user_hassnap_find(if_info->name)) == -1) {
249 device.has_snaplen = global_capture_opts.default_options.has_snaplen;
251 if (capture_dev_user_snaplen_find(if_info->name) == -1) {
252 device.snaplen = global_capture_opts.default_options.snaplen;
254 device.snaplen = (guint)capture_dev_user_snaplen_find(if_info->name);
256 device.cfilter = g_strdup(global_capture_opts.default_options.cfilter);
257 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
258 if ((device.buffer = capture_dev_user_buffersize_find(if_info->name)) == -1) {
259 device.buffer = global_capture_opts.default_options.buffer_size;
263 if (global_capture_opts.ifaces->len > 0) {
264 for (j = 0; j < global_capture_opts.ifaces->len; j++) {
265 interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, j);
266 if (strcmp(interface_opts.name, device.name) == 0) {
267 #if defined(HAVE_PCAP_CREATE)
268 device.buffer = interface_opts.buffer_size;
269 device.monitor_mode_enabled = interface_opts.monitor_mode;
271 device.pmode = interface_opts.promisc_mode;
272 device.has_snaplen = interface_opts.has_snaplen;
273 device.snaplen = interface_opts.snaplen;
274 device.cfilter = g_strdup(interface_opts.cfilter);
275 if (interface_opts.linktype != -1) {
276 device.active_dlt = interface_opts.linktype;
278 device.selected = TRUE;
279 global_capture_opts.num_selected++;
284 if (global_capture_opts.all_ifaces->len <= count) {
285 g_array_append_val(global_capture_opts.all_ifaces, device);
286 count = global_capture_opts.all_ifaces->len;
288 g_array_insert_val(global_capture_opts.all_ifaces, count, device);
291 free_if_capabilities(caps);
294 g_string_free(ip_str, TRUE);
297 free_interface_list(if_list);
298 /* see whether there are additional interfaces in ifaces */
299 for (j = 0; j < global_capture_opts.ifaces->len; j++) {
300 interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, j);
302 for (i = 0; i < (int)global_capture_opts.all_ifaces->len; i++) {
303 device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
304 if (strcmp(device.name, interface_opts.name) == 0) {
309 if (!found) { /* new interface, maybe a pipe */
310 device.name = g_strdup(interface_opts.name);
311 device.display_name = g_strdup_printf("%s: %s", device.name, interface_opts.descr);
312 device.hidden = FALSE;
313 device.selected = TRUE;
314 device.type = IF_PIPE;
315 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
316 device.buffer = interface_opts.buffer_size;
318 #if defined(HAVE_PCAP_CREATE)
319 device.monitor_mode_enabled = interface_opts.monitor_mode;
320 device.monitor_mode_supported = FALSE;
322 device.pmode = interface_opts.promisc_mode;
323 device.has_snaplen = interface_opts.has_snaplen;
324 device.snaplen = interface_opts.snaplen;
325 device.cfilter = g_strdup(interface_opts.cfilter);
326 device.active_dlt = interface_opts.linktype;
327 device.addresses = NULL;
328 device.no_addresses = 0;
329 device.last_packets = 0;
332 device.locked = FALSE;
333 device.if_info.name = g_strdup(interface_opts.name);
334 device.if_info.friendly_name = NULL;
335 device.if_info.vendor_description = g_strdup(interface_opts.descr);
336 device.if_info.addrs = NULL;
337 device.if_info.loopback = FALSE;
339 g_array_append_val(global_capture_opts.all_ifaces, device);
340 global_capture_opts.num_selected++;
346 * Get the global interface list. Generate it if we haven't
350 fill_in_local_interfaces(void)
352 static gboolean initialized = FALSE;
355 scan_local_interfaces();
361 hide_interface(gchar* new_hide)
366 gboolean found = FALSE;
367 GList *hidden_devices = NULL, *entry;
368 if (new_hide != NULL) {
369 for (tok = strtok (new_hide, ","); tok; tok = strtok(NULL, ",")) {
370 hidden_devices = g_list_append(hidden_devices, tok);
373 for (i = 0; i < global_capture_opts.all_ifaces->len; i++) {
374 device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
376 for (entry = hidden_devices; entry != NULL; entry = g_list_next(entry)) {
377 if (strcmp((char *)entry->data, device.name)==0) {
378 device.hidden = TRUE;
379 if (device.selected) {
380 device.selected = FALSE;
381 global_capture_opts.num_selected--;
388 device.hidden = FALSE;
390 global_capture_opts.all_ifaces = g_array_remove_index(global_capture_opts.all_ifaces, i);
391 g_array_insert_val(global_capture_opts.all_ifaces, i, device);
394 #endif /* HAVE_LIBPCAP */
402 * indent-tabs-mode: nil
405 * ex: set shiftwidth=4 tabstop=8 expandtab:
406 * :indentSize=4:tabSize=8:noTabs=true: