On OS X, get the interface type from the System Configuration framework.
[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  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #include "config.h"
27
28 #ifdef HAVE_LIBPCAP
29
30 #include <string.h>
31
32 #include <glib.h>
33
34 #include <epan/prefs.h>
35 #include <epan/to_str.h>
36
37 #include "../capture_ui_utils.h"
38
39 #include "ui/capture_globals.h"
40 #include "ui/iface_lists.h"
41
42 capture_options global_capture_opts;
43
44 /*
45  * Used when sorting an interface list into alphabetical order by
46  * their friendly names.
47  */
48 gint
49 if_list_comparator_alph(const void *first_arg, const void *second_arg)
50 {
51     const if_info_t *first = (const if_info_t *)first_arg, *second = (const if_info_t *)second_arg;
52
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);
56     } else {
57         return 0;
58     }
59 }
60
61 /*
62  * Fetch the list of local interfaces with capture_interface_list()
63  * and set the list of "all interfaces" in *capture_opts to include
64  * those interfaces.
65  */
66 void
67 scan_local_interfaces(void)
68 {
69     GList             *if_entry, *lt_entry, *if_list;
70     if_info_t         *if_info, *temp;
71     char              *if_string;
72     gchar             *descr;
73     if_capabilities_t *caps=NULL;
74     gint              linktype_count;
75     gboolean          monitor_mode;
76     GSList            *curr_addr;
77     int               ips = 0, i, err;
78     guint             count = 0, j;
79     if_addr_t         *addr, *temp_addr;
80     link_row          *link = NULL;
81     data_link_info_t  *data_link_info;
82     interface_t       device;
83     GString           *ip_str;
84     interface_options interface_opts;
85     gboolean          found = FALSE;
86
87
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);
93             }
94         }
95     }
96
97     /* Scan through the list and build a list of strings to display. */
98     if_list = capture_interface_list(&err, NULL);
99     count = 0;
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("");
103         ips = 0;
104         if (strstr(if_info->name, "rpcap:")) {
105             continue;
106         }
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);
110         } else {
111             device.friendly_name = NULL;
112         }
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? */
121
122         /* Do we have a user-supplied description? */
123         descr = capture_dev_user_descr_find(if_info->name);
124         if (descr != NULL) {
125             /* Yes, we have a user-supplied description; use it. */
126             if_string = g_strdup_printf("%s: %s", descr, if_info->name);
127             g_free(descr);
128         } else {
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 */
133 #ifdef _WIN32
134                 /*
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.
138                  */
139                 if_string = g_strdup_printf("%s", if_info->friendly_name);
140 #else
141                 /*
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.
146                  */
147                 if_string = g_strdup_printf("%s: %s", if_info->friendly_name, if_info->name);
148 #endif
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);
152             } else {
153                 /* No. */
154                 if_string = g_strdup(if_info->name);
155             }
156         }
157         device.display_name = if_string;
158         device.selected = FALSE;
159         if (prefs_is_capture_device_hidden(if_info->name)) {
160             device.hidden = TRUE;
161         }
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));
167             if (ips != 0) {
168                 g_string_append(ip_str, "\n");
169             }
170             addr = (if_addr_t *)curr_addr->data;
171             if (addr) {
172                 temp_addr->ifat_type = addr->ifat_type;
173                 switch (addr->ifat_type) {
174                     case IF_AT_IPv4:
175                         temp_addr->addr.ip4_addr = addr->addr.ip4_addr;
176                         g_string_append(ip_str, ip_to_str((guint8 *)&addr->addr.ip4_addr));
177                         break;
178                     case IF_AT_IPv6:
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));
181                         break;
182                     default:
183                         /* In case we add non-IP addresses */
184                         break;
185                 }
186             } else {
187                 g_free(temp_addr);
188                 temp_addr = NULL;
189             }
190             if (temp_addr) {
191                 temp->addrs = g_slist_append(temp->addrs, temp_addr);
192             }
193         }
194 #ifdef HAVE_PCAP_REMOTE
195         device.local = TRUE;
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;
205 #endif
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;
209 #endif
210         linktype_count = 0;
211         device.links = NULL;
212         if (caps != NULL) {
213 #if defined(HAVE_PCAP_CREATE)
214             device.monitor_mode_enabled = monitor_mode;
215             device.monitor_mode_supported = caps->can_set_rfmon;
216 #endif
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;
221                 }
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);
226                 } else {
227                     link->dlt = -1;
228                     link->name = g_strdup_printf("%s (not supported)", data_link_info->name);
229                 }
230                 device.links = g_list_append(device.links, link);
231                 linktype_count++;
232             }
233         } else {
234 #if defined(HAVE_PCAP_CREATE)
235             device.monitor_mode_enabled = FALSE;
236             device.monitor_mode_supported = FALSE;
237 #endif
238             device.active_dlt = -1;
239         }
240         device.addresses = g_strdup(ip_str->str);
241         device.no_addresses = ips;
242         device.local = TRUE;
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;
247         }
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;
250         }
251         if (capture_dev_user_snaplen_find(if_info->name) == -1) {
252             device.snaplen = global_capture_opts.default_options.snaplen;
253         } else {
254             device.snaplen = (guint)capture_dev_user_snaplen_find(if_info->name);
255         }
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;
260         }
261 #endif
262
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;
270 #endif
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;
277                     }
278                     device.selected = TRUE;
279                     global_capture_opts.num_selected++;
280                     break;
281                 }
282             }
283         }
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;
287         } else {
288             g_array_insert_val(global_capture_opts.all_ifaces, count, device);
289         }
290         if (caps != NULL) {
291             free_if_capabilities(caps);
292         }
293
294         g_string_free(ip_str, TRUE);
295         count++;
296     }
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);
301         found = FALSE;
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) {
305                 found = TRUE;
306                 break;
307             }
308         }
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;
317 #endif
318 #if defined(HAVE_PCAP_CREATE)
319             device.monitor_mode_enabled = interface_opts.monitor_mode;
320             device.monitor_mode_supported = FALSE;
321 #endif
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;
330             device.links        = NULL;
331             device.local        = TRUE;
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;
338
339             g_array_append_val(global_capture_opts.all_ifaces, device);
340             global_capture_opts.num_selected++;
341         }
342     }
343 }
344
345 /*
346  * Get the global interface list.  Generate it if we haven't
347  * done so already.
348  */
349 void
350 fill_in_local_interfaces(void)
351 {
352     static gboolean initialized = FALSE;
353
354     if (!initialized) {
355         scan_local_interfaces();
356         initialized = TRUE;
357     }
358 }
359
360 void
361 hide_interface(gchar* new_hide)
362 {
363     gchar       *tok;
364     guint       i;
365     interface_t device;
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);
371         }
372     }
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);
375         found = FALSE;
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--;
382                 }
383                 found = TRUE;
384                 break;
385             }
386         }
387         if (!found) {
388             device.hidden = FALSE;
389         }
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);
392     }
393 }
394 #endif /* HAVE_LIBPCAP */
395
396 /*
397  * Editor modelines
398  *
399  * Local Variables:
400  * c-basic-offset: 4
401  * tab-width: 8
402  * indent-tabs-mode: nil
403  * End:
404  *
405  * ex: set shiftwidth=4 tabstop=8 expandtab:
406  * :indentSize=4:tabSize=8:noTabs=true:
407  */