2 * Copyright (c) 2013-2016, 2018, 2020 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * October 7, 2013 Allan Nathanson <ajn@apple.com>
37 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
41 void * volatile fn_WeakFunction = (void *)&(WiFiManagerClientCreate);
42 Boolean haveFramework;
44 haveFramework = (fn_WeakFunction != NULL) ? TRUE : FALSE;
47 #endif // TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
51 is_expensive(SCNetworkInterfaceRef _Nonnull interface)
53 CFBooleanRef expensive;
54 CFStringRef interfaceType;
56 while (interface != NULL) {
57 SCNetworkInterfaceRef child;
59 child = SCNetworkInterfaceGetInterface(interface);
66 // by default, don't set/clear expensive
68 interfaceType = SCNetworkInterfaceGetInterfaceType(interface);
69 if (_SCNetworkInterfaceIsTethered(interface)) {
70 // if tethered (to iOS) interface
71 expensive = kCFBooleanTrue;
72 } else if (_SCNetworkInterfaceIsBluetoothPAN(interface)) {
73 // if BT-PAN interface
74 expensive = kCFBooleanTrue;
75 } else if (CFEqual(interfaceType, kSCNetworkInterfaceTypeWWAN)) {
76 // if WWAN [Ethernet] interface
77 expensive = kCFBooleanTrue;
85 ifexpensive_set(int s, const char * name, uint32_t expensive)
87 #if defined(SIOCGIFEXPENSIVE) && defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
91 memset(&ifr, 0, sizeof(ifr));
92 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
93 ret = ioctl(s, SIOCGIFEXPENSIVE, &ifr);
94 if ((ret == -1) && (errno != EPERM)) {
95 SC_log(LOG_ERR, "%s: ioctl(SIOCGIFEXPENSIVE) failed: %s", name, strerror(errno));
99 if (ifr.ifr_expensive == expensive) {
104 ifr.ifr_expensive = expensive;
105 ret = ioctl(s, SIOCSIFEXPENSIVE, &ifr);
106 if ((ret == -1) && (errno != EPERM)) {
107 SC_log(LOG_ERR, "%s: ioctl(SIOCSIFEXPENSIVE) failed: %s", name, strerror(errno));
111 #else // defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
113 #endif // defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
119 interface_update_expensive(const char *if_name)
121 CFBooleanRef expensive;
122 SCNetworkInterfaceRef interface;
123 CFStringRef interface_name;
126 interface_name = CFStringCreateWithCString(NULL, if_name, kCFStringEncodingUTF8);
127 interface = _SCNetworkInterfaceCreateWithBSDName(NULL, interface_name, kIncludeNoVirtualInterfaces);
128 CFRelease(interface_name);
129 if (interface == NULL) {
132 expensive = is_expensive(interface);
133 CFRelease(interface);
134 if (expensive == NULL) {
137 // mark ... or clear ... the [if_name] interface as "expensive"
138 s = dgram_socket(AF_INET);
142 CFBooleanGetValue(expensive) ? 1 : 0);
153 dgram_socket(int domain)
157 s = socket(domain, SOCK_DGRAM, 0);
159 SC_log(LOG_ERR, "socket() failed: %s", strerror(errno));
166 main(int argc, char **argv)
168 CFBooleanRef expensive;
171 SCPrint(TRUE, stderr, CFSTR("usage: %s <interface>\n"), argv[0]);
175 expensive = interface_update_expensive(argv[1]);
176 if (expensive != NULL) {
177 SCPrint(TRUE, stdout, CFSTR("%s: set expensive to %@\n"), argv[1], expensive);
179 SCPrint(TRUE, stdout, CFSTR("%s: not changing expensive\n"), argv[1]);