]> git.saurik.com Git - apple/configd.git/blob - Plugins/KernelEventMonitor/ev_extra.m
configd-1109.101.1.tar.gz
[apple/configd.git] / Plugins / KernelEventMonitor / ev_extra.m
1 /*
2 * Copyright (c) 2013-2016, 2018, 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * October 7, 2013 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #include "eventmon.h"
33 #include "ev_extra.h"
34
35
36
37 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
38 static Boolean
39 haveMobileWiFi()
40 {
41 void * volatile fn_WeakFunction = (void *)&(WiFiManagerClientCreate);
42 Boolean haveFramework;
43
44 haveFramework = (fn_WeakFunction != NULL) ? TRUE : FALSE;
45 return haveFramework;
46 }
47 #endif // TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
48
49
50 static CFBooleanRef
51 is_expensive(SCNetworkInterfaceRef _Nonnull interface)
52 {
53 CFBooleanRef expensive;
54 CFStringRef interfaceType;
55
56 while (interface != NULL) {
57 SCNetworkInterfaceRef child;
58
59 child = SCNetworkInterfaceGetInterface(interface);
60 if (child == NULL) {
61 break;
62 }
63
64 interface = child;
65 }
66 // by default, don't set/clear expensive
67 expensive = NULL;
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;
78 }
79
80 return expensive;
81 }
82
83
84 static int
85 ifexpensive_set(int s, const char * name, uint32_t expensive)
86 {
87 #if defined(SIOCGIFEXPENSIVE) && defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
88 struct ifreq ifr;
89 int ret;
90
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));
96 return ret;
97 }
98
99 if (ifr.ifr_expensive == expensive) {
100 // if no change
101 return ret;
102 }
103
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));
108 }
109
110 return ret;
111 #else // defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
112 return 0;
113 #endif // defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
114 }
115
116
117 __private_extern__
118 CFBooleanRef
119 interface_update_expensive(const char *if_name)
120 {
121 CFBooleanRef expensive;
122 SCNetworkInterfaceRef interface;
123 CFStringRef interface_name;
124 int s;
125
126 interface_name = CFStringCreateWithCString(NULL, if_name, kCFStringEncodingUTF8);
127 interface = _SCNetworkInterfaceCreateWithBSDName(NULL, interface_name, kIncludeNoVirtualInterfaces);
128 CFRelease(interface_name);
129 if (interface == NULL) {
130 return NULL;
131 }
132 expensive = is_expensive(interface);
133 CFRelease(interface);
134 if (expensive == NULL) {
135 return NULL;
136 }
137 // mark ... or clear ... the [if_name] interface as "expensive"
138 s = dgram_socket(AF_INET);
139 if (s != -1) {
140 ifexpensive_set(s,
141 if_name,
142 CFBooleanGetValue(expensive) ? 1 : 0);
143 close(s);
144 }
145
146 return expensive;
147 }
148
149
150 #ifdef MAIN
151
152 int
153 dgram_socket(int domain)
154 {
155 int s;
156
157 s = socket(domain, SOCK_DGRAM, 0);
158 if (s == -1) {
159 SC_log(LOG_ERR, "socket() failed: %s", strerror(errno));
160 }
161
162 return s;
163 }
164
165 int
166 main(int argc, char **argv)
167 {
168 CFBooleanRef expensive;
169
170 if (argc < 1 + 1) {
171 SCPrint(TRUE, stderr, CFSTR("usage: %s <interface>\n"), argv[0]);
172 exit(1);
173 }
174
175 expensive = interface_update_expensive(argv[1]);
176 if (expensive != NULL) {
177 SCPrint(TRUE, stdout, CFSTR("%s: set expensive to %@\n"), argv[1], expensive);
178 } else {
179 SCPrint(TRUE, stdout, CFSTR("%s: not changing expensive\n"), argv[1]);
180 }
181
182 exit(0);
183 }
184 #endif // MAIN