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