Fix some issues potentially similar in nature to the one found in
[metze/wireshark/wip.git] / capture_unix_ifnames.c
1 /* capture_unix_ifnames.c
2  * Routines supporting the use of UN*X friendly interface names, if any,
3  * within Wireshark
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 2001 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 #include <glib.h>
29
30 #include "capture_unix_ifnames.h"
31
32 /*
33  * Given an interface name, find the "friendly name" for the interface.
34  */
35
36 #ifdef __APPLE__
37
38 #include <CoreFoundation/CoreFoundation.h>
39 #include <SystemConfiguration/SystemConfiguration.h>
40
41 #include "cfutils.h"
42
43 /*
44  * On OS X, we do that by getting all the interfaces that the System
45  * Configuration framework knows about, look for the one with a "BSD
46  * name" matching the interface name, and, if we find it, return its
47  * "localized display name", if it has one.
48  */
49 char *
50 get_unix_interface_friendly_name(const char *ifname)
51 {
52         CFStringRef ifname_CFString;
53         CFArrayRef interfaces;
54         CFIndex num_interfaces;
55         CFIndex i;
56         SCNetworkInterfaceRef interface;
57         CFStringRef bsdname_CFString;
58         CFStringRef friendly_name_CFString;
59         char *friendly_name = NULL;
60
61         interfaces = SCNetworkInterfaceCopyAll();
62         if (interfaces == NULL) {
63                 /*
64                  * Couldn't get a list of interfaces.
65                  */
66                 return NULL;
67         }
68
69         ifname_CFString = CFStringCreateWithCString(kCFAllocatorDefault,
70             ifname, kCFStringEncodingUTF8);
71         if (ifname_CFString == NULL) {
72                 /*
73                  * Couldn't convert the interface name to a CFString.
74                  */
75                 CFRelease(interfaces);
76                 return NULL;
77         }
78
79         num_interfaces = CFArrayGetCount(interfaces);
80         for (i = 0; i < num_interfaces; i++) {
81                 interface = CFArrayGetValueAtIndex(interfaces, i);
82                 bsdname_CFString = SCNetworkInterfaceGetBSDName(interface);
83                 if (bsdname_CFString == NULL) {
84                         /*
85                          * This interface has no BSD name, so it's not
86                          * a regular network interface.
87                          */
88                         continue;
89                 }
90                 if (CFStringCompare(ifname_CFString, bsdname_CFString, 0) == 0) {
91                         /*
92                          * This is the interface.
93                          */
94                         friendly_name_CFString = SCNetworkInterfaceGetLocalizedDisplayName(interface);
95                         if (friendly_name_CFString != NULL)
96                                 friendly_name = CFString_to_C_string(friendly_name_CFString);
97                         break;
98                 }
99         }
100
101         CFRelease(interfaces);
102         return friendly_name;
103 }
104
105 #else /* __APPLE__ */
106
107 /*
108  * Nothing supported on other platforms.
109  */
110 char *
111 get_unix_interface_friendly_name(const char *ifname _U_)
112 {
113         return NULL;
114 }
115
116 #endif /* __APPLE__ */