]> git.saurik.com Git - apple/configd.git/blob - Plugins/KernelEventMonitor/ev_extra.m
configd-1061.0.2.tar.gz
[apple/configd.git] / Plugins / KernelEventMonitor / ev_extra.m
1 /*
2 * Copyright (c) 2013-2016, 2018 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 = NULL;
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
67 // assume NOT expensive
68 expensive = kCFBooleanFalse;
69
70 interfaceType = SCNetworkInterfaceGetInterfaceType(interface);
71 if (_SCNetworkInterfaceIsTethered(interface)) {
72 // if tethered (to iOS) interface
73 expensive = kCFBooleanTrue;
74 } else if (_SCNetworkInterfaceIsBluetoothPAN(interface)) {
75 // if BT-PAN interface
76 expensive = kCFBooleanTrue;
77 } else if (CFEqual(interfaceType, kSCNetworkInterfaceTypeWWAN)) {
78 // if WWAN [Ethernet] interface
79 expensive = kCFBooleanTrue;
80 }
81
82 return expensive;
83 }
84
85
86 static int
87 ifexpensive_set(int s, const char * name, uint32_t expensive)
88 {
89 #if defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
90 struct ifreq ifr;
91 int ret;
92
93 memset(&ifr, 0, sizeof(ifr));
94 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
95 ifr.ifr_expensive = expensive;
96 ret = ioctl(s, SIOCSIFEXPENSIVE, &ifr);
97 if ((ret == -1) && (errno != EPERM)) {
98 SC_log(LOG_ERR, "%s: ioctl(SIOCSIFEXPENSIVE) failed: %s", name, strerror(errno));
99 }
100
101 return ret;
102 #else // defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
103 return 0;
104 #endif // defined(SIOCSIFEXPENSIVE) && !defined(MAIN)
105 }
106
107
108 __private_extern__
109 CFBooleanRef
110 interface_update_expensive(const char *if_name)
111 {
112 CFBooleanRef expensive = NULL;
113 SCNetworkInterfaceRef interface;
114 CFStringRef interface_name;
115 int s;
116
117 interface_name = CFStringCreateWithCString(NULL, if_name, kCFStringEncodingMacRoman);
118 interface = _SCNetworkInterfaceCreateWithBSDName(NULL, interface_name, kIncludeNoVirtualInterfaces);
119 CFRelease(interface_name);
120
121 if (interface != NULL) {
122 expensive = is_expensive(interface);
123 CFRelease(interface);
124 }
125
126 // mark ... or clear ... the [if_name] interface as "expensive"
127 s = dgram_socket(AF_INET);
128 if (s != -1) {
129 ifexpensive_set(s,
130 if_name,
131 ((expensive != NULL) && CFBooleanGetValue(expensive)) ? 1 : 0);
132 close(s);
133 }
134
135 return expensive;
136 }
137
138
139 #ifdef MAIN
140
141 int
142 dgram_socket(int domain)
143 {
144 int s;
145
146 s = socket(domain, SOCK_DGRAM, 0);
147 if (s == -1) {
148 SC_log(LOG_ERR, "socket() failed: %s", strerror(errno));
149 }
150
151 return s;
152 }
153
154 int
155 main(int argc, char **argv)
156 {
157 CFBooleanRef expensive;
158
159 if (argc < 1 + 1) {
160 SCPrint(TRUE, stderr, CFSTR("usage: %s <interface>\n"), argv[0]);
161 exit(1);
162 }
163
164 expensive = interface_update_expensive(argv[1]);
165 if (expensive != NULL) {
166 SCPrint(TRUE, stdout, CFSTR("interface \"%s\": %@\n"), argv[1], expensive);
167 } else {
168 SCPrint(TRUE, stdout, CFSTR("interface \"%s\": could not determine \"expensive\" status\n"), argv[1]);
169 }
170
171 exit(0);
172 }
173 #endif // MAIN