diff options
author | Guy Harris <guy@alum.mit.edu> | 2012-11-22 06:02:49 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2012-11-22 06:02:49 +0000 |
commit | bd976ae6c06b2111bd82df16b77739731dc17402 (patch) | |
tree | 406d65d00129abb45868150687f267a139bbf670 /capture_unix_ifnames.c | |
parent | b9e8e95ffe9f352cde5847d458081826523cf46f (diff) | |
download | wireshark-bd976ae6c06b2111bd82df16b77739731dc17402.tar.gz |
On UN*X, if an interface has a description, use it as the "friendly
name". If it doesn't have a description, on OS X, use the System
Configuration framework to attempt to get a "friendly name" for
interfaces.
If a loopback device doesn't have a friendly name, give it "Loopback" as
the friendly name.
Move the "turn a CFString into a mallocated C string" routine into
common code, as it's used in more than one place.
svn path=/trunk/; revision=46131
Diffstat (limited to 'capture_unix_ifnames.c')
-rw-r--r-- | capture_unix_ifnames.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/capture_unix_ifnames.c b/capture_unix_ifnames.c new file mode 100644 index 0000000000..e2584226da --- /dev/null +++ b/capture_unix_ifnames.c @@ -0,0 +1,116 @@ +/* capture_unix_ifnames.c + * Routines supporting the use of UN*X friendly interface names, if any, + * within Wireshark + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 2001 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include <glib.h> + +#include "capture_unix_ifnames.h" + +/* + * Given an interface name, find the "friendly name" for the interface. + */ + +#ifdef __APPLE__ + +#include <CoreFoundation/CoreFoundation.h> +#include <SystemConfiguration/SystemConfiguration.h> + +#include "cfutils.h" + +/* + * On OS X, we do that by getting all the interfaces that the System + * Configuration framework knows about, look for the one with a "BSD + * name" matching the interface name, and, if we find it, return its + * "localized display name", if it has one. + */ +char * +get_unix_interface_friendly_name(const char *ifname) +{ + CFStringRef ifname_CFString; + CFArrayRef interfaces; + CFIndex num_interfaces; + CFIndex i; + SCNetworkInterfaceRef interface; + CFStringRef bsdname_CFString; + CFStringRef friendly_name_CFString; + char *friendly_name = NULL; + + interfaces = SCNetworkInterfaceCopyAll(); + if (interfaces == NULL) { + /* + * Couldn't get a list of interfaces. + */ + return NULL; + } + + ifname_CFString = CFStringCreateWithCString(kCFAllocatorDefault, + ifname, kCFStringEncodingUTF8); + if (ifname_CFString == NULL) { + /* + * Couldn't convert the interface name to a CFString. + */ + CFRelease(interfaces); + return NULL; + } + + num_interfaces = CFArrayGetCount(interfaces); + for (i = 0; i < num_interfaces; i++) { + interface = CFArrayGetValueAtIndex(interfaces, i); + bsdname_CFString = SCNetworkInterfaceGetBSDName(interface); + if (bsdname_CFString == NULL) { + /* + * This interface has no BSD name, so it's not + * a regular network interface. + */ + continue; + } + if (CFStringCompare(ifname_CFString, bsdname_CFString, 0) == 0) { + /* + * This is the interface. + */ + friendly_name_CFString = SCNetworkInterfaceGetLocalizedDisplayName(interface); + if (friendly_name_CFString != NULL) + friendly_name = CFString_to_C_string(friendly_name_CFString); + break; + } + } + + CFRelease(interfaces); + return friendly_name; +} + +#else /* __APPLE__ */ + +/* + * Nothing supported on other platforms. + */ +char * +get_unix_interface_friendly_name(const char *ifname _U_) +{ + return NULL; +} + +#endif /* __APPLE__ */ |