]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCNetworkProtocol.c
configd-888.1.2.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCNetworkProtocol.c
1 /*
2 * Copyright (c) 2004-2008, 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 * May 13, 2004 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <CoreFoundation/CFRuntime.h>
34 #include "SCNetworkConfigurationInternal.h"
35
36 #include <pthread.h>
37
38
39 static CFStringRef __SCNetworkProtocolCopyDescription (CFTypeRef cf);
40 static void __SCNetworkProtocolDeallocate (CFTypeRef cf);
41 static Boolean __SCNetworkProtocolEqual (CFTypeRef cf1, CFTypeRef cf2);
42 static CFHashCode __SCNetworkProtocolHash (CFTypeRef cf);
43
44
45 const CFStringRef kSCNetworkProtocolTypeDNS = CFSTR("DNS");
46 const CFStringRef kSCNetworkProtocolTypeIPv4 = CFSTR("IPv4");
47 const CFStringRef kSCNetworkProtocolTypeIPv6 = CFSTR("IPv6");
48 const CFStringRef kSCNetworkProtocolTypeProxies = CFSTR("Proxies");
49 #if !TARGET_OS_IPHONE
50 const CFStringRef kSCNetworkProtocolTypeSMB = CFSTR("SMB");
51 #endif // !TARGET_OS_IPHONE
52
53
54 static CFTypeID __kSCNetworkProtocolTypeID = _kCFRuntimeNotATypeID;
55
56
57 static const CFRuntimeClass __SCNetworkProtocolClass = {
58 0, // version
59 "SCNetworkProtocol", // className
60 NULL, // init
61 NULL, // copy
62 __SCNetworkProtocolDeallocate, // dealloc
63 __SCNetworkProtocolEqual, // equal
64 __SCNetworkProtocolHash, // hash
65 NULL, // copyFormattingDesc
66 __SCNetworkProtocolCopyDescription // copyDebugDesc
67 };
68
69
70 static pthread_once_t initialized = PTHREAD_ONCE_INIT;
71
72
73 static CFStringRef
74 __SCNetworkProtocolCopyDescription(CFTypeRef cf)
75 {
76 CFAllocatorRef allocator = CFGetAllocator(cf);
77 CFMutableStringRef result;
78 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)cf;
79
80 result = CFStringCreateMutable(allocator, 0);
81 CFStringAppendFormat(result, NULL, CFSTR("<SCNetworkProtocol %p [%p]> {"), cf, allocator);
82 CFStringAppendFormat(result, NULL, CFSTR("id = %@"), protocolPrivate->entityID);
83 CFStringAppendFormat(result, NULL, CFSTR(", service = %p"), protocolPrivate->service);
84 CFStringAppendFormat(result, NULL,
85 CFSTR(", prefs = %p"),
86 ((SCNetworkServicePrivateRef)protocolPrivate->service)->prefs);
87 CFStringAppendFormat(result, NULL, CFSTR("}"));
88
89 return result;
90 }
91
92
93 static void
94 __SCNetworkProtocolDeallocate(CFTypeRef cf)
95 {
96 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)cf;
97
98 /* release resources */
99 CFRelease(protocolPrivate->entityID);
100 CFRelease(protocolPrivate->service);
101
102 return;
103 }
104
105
106 static Boolean
107 __SCNetworkProtocolEqual(CFTypeRef cf1, CFTypeRef cf2)
108 {
109 SCNetworkProtocolPrivateRef p1 = (SCNetworkProtocolPrivateRef)cf1;
110 SCNetworkProtocolPrivateRef p2 = (SCNetworkProtocolPrivateRef)cf2;
111
112 if (p1 == p2)
113 return TRUE;
114
115 if (!CFEqual(p1->entityID, p2->entityID))
116 return FALSE; // if not the same protocol type
117
118 if (p1->service == p2->service)
119 return TRUE; // if both point to the same service
120
121 if ((p1->service != NULL) && (p2->service != NULL) && CFEqual(p1->service, p2->service))
122 return TRUE; // if both effectively point to the same service
123
124 return FALSE;
125 }
126
127
128 static CFHashCode
129 __SCNetworkProtocolHash(CFTypeRef cf)
130 {
131 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)cf;
132
133 return CFHash(protocolPrivate->entityID);
134 }
135
136
137 static void
138 __SCNetworkProtocolInitialize(void)
139 {
140 __kSCNetworkProtocolTypeID = _CFRuntimeRegisterClass(&__SCNetworkProtocolClass);
141 return;
142 }
143
144
145 __private_extern__ SCNetworkProtocolPrivateRef
146 __SCNetworkProtocolCreatePrivate(CFAllocatorRef allocator,
147 CFStringRef entityID,
148 SCNetworkServiceRef service)
149 {
150 SCNetworkProtocolPrivateRef protocolPrivate;
151 uint32_t size;
152
153 /* initialize runtime */
154 pthread_once(&initialized, __SCNetworkProtocolInitialize);
155
156 /* allocate target */
157 size = sizeof(SCNetworkProtocolPrivate) - sizeof(CFRuntimeBase);
158 protocolPrivate = (SCNetworkProtocolPrivateRef)_CFRuntimeCreateInstance(allocator,
159 __kSCNetworkProtocolTypeID,
160 size,
161 NULL);
162 if (protocolPrivate == NULL) {
163 return NULL;
164 }
165
166 /* initialize non-zero/NULL members */
167 protocolPrivate->entityID = CFStringCreateCopy(NULL, entityID);
168 protocolPrivate->service = CFRetain(service);
169
170 return protocolPrivate;
171 }
172
173
174 __private_extern__ Boolean
175 __SCNetworkProtocolIsValidType(CFStringRef protocolType)
176 {
177 int i;
178 static const CFStringRef *valid_types[] = {
179 &kSCNetworkProtocolTypeDNS,
180 &kSCNetworkProtocolTypeIPv4,
181 &kSCNetworkProtocolTypeIPv6,
182 &kSCNetworkProtocolTypeProxies,
183 #if !TARGET_OS_IPHONE
184 &kSCNetworkProtocolTypeSMB,
185 #endif // !TARGET_OS_IPHONE
186 };
187
188 for (i = 0; i < sizeof(valid_types)/sizeof(valid_types[0]); i++) {
189 if (CFEqual(protocolType, *valid_types[i])) {
190 // if known/valid protocol type
191 return TRUE;
192 }
193 }
194
195 if (CFStringFindWithOptions(protocolType,
196 CFSTR("."),
197 CFRangeMake(0, CFStringGetLength(protocolType)),
198 0,
199 NULL)) {
200 // if user-defined protocol type (e.g. com.apple.myProtocol)
201 return TRUE;
202 }
203
204 return FALSE;
205 }
206
207
208 static CFStringRef
209 copyProtocolConfigurationPath(SCNetworkProtocolPrivateRef protocolPrivate)
210 {
211 CFStringRef path;
212 SCNetworkServicePrivateRef servicePrivate;
213
214 servicePrivate = (SCNetworkServicePrivateRef)protocolPrivate->service;
215 path = SCPreferencesPathKeyCreateNetworkServiceEntity(NULL, // allocator
216 servicePrivate->serviceID, // service
217 protocolPrivate->entityID); // entity
218 return path;
219 }
220
221
222 #pragma mark -
223 #pragma mark SCNetworkProtocol APIs
224
225
226 CFTypeID
227 SCNetworkProtocolGetTypeID()
228 {
229 pthread_once(&initialized, __SCNetworkProtocolInitialize); /* initialize runtime */
230 return __kSCNetworkProtocolTypeID;
231 }
232
233
234 CFDictionaryRef
235 SCNetworkProtocolGetConfiguration(SCNetworkProtocolRef protocol)
236 {
237 CFDictionaryRef config;
238 CFStringRef path;
239 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)protocol;
240 SCNetworkServicePrivateRef servicePrivate = (SCNetworkServicePrivateRef)protocolPrivate->service;
241
242 if (!isA_SCNetworkProtocol(protocol)) {
243 _SCErrorSet(kSCStatusInvalidArgument);
244 return NULL;
245 }
246
247 path = copyProtocolConfigurationPath(protocolPrivate);
248 config = __getPrefsConfiguration(servicePrivate->prefs, path);
249 CFRelease(path);
250
251 return config;
252 }
253
254
255 Boolean
256 SCNetworkProtocolGetEnabled(SCNetworkProtocolRef protocol)
257 {
258 Boolean enabled;
259 CFStringRef path;
260 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)protocol;
261 SCNetworkServicePrivateRef servicePrivate = (SCNetworkServicePrivateRef)protocolPrivate->service;
262
263 if (!isA_SCNetworkProtocol(protocol)) {
264 _SCErrorSet(kSCStatusInvalidArgument);
265 return FALSE;
266 }
267
268 path = copyProtocolConfigurationPath(protocolPrivate);
269 enabled = __getPrefsEnabled(servicePrivate->prefs, path);
270 CFRelease(path);
271
272 return enabled;
273 }
274
275
276 CFStringRef
277 SCNetworkProtocolGetProtocolType(SCNetworkProtocolRef protocol)
278 {
279 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)protocol;
280
281 if (!isA_SCNetworkProtocol(protocol)) {
282 _SCErrorSet(kSCStatusInvalidArgument);
283 return NULL;
284 }
285
286 return protocolPrivate->entityID;
287 }
288
289
290 Boolean
291 SCNetworkProtocolSetConfiguration(SCNetworkProtocolRef protocol, CFDictionaryRef config)
292 {
293 Boolean ok;
294 CFStringRef path;
295 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)protocol;
296 SCNetworkServicePrivateRef servicePrivate = (SCNetworkServicePrivateRef)protocolPrivate->service;
297
298 if (!isA_SCNetworkProtocol(protocol)) {
299 _SCErrorSet(kSCStatusInvalidArgument);
300 return FALSE;
301 }
302
303 path = copyProtocolConfigurationPath(protocolPrivate);
304 ok = __setPrefsConfiguration(servicePrivate->prefs, path, config, TRUE);
305 CFRelease(path);
306
307 if (ok) {
308 SC_log(LOG_DEBUG, "SCNetworkProtocolSetConfiguration(): %@ --> %@",
309 protocol,
310 config != NULL ? config : (CFDictionaryRef)CFSTR("NULL"));
311 }
312
313 return ok;
314 }
315
316
317 Boolean
318 SCNetworkProtocolSetEnabled(SCNetworkProtocolRef protocol, Boolean enabled)
319 {
320 Boolean ok;
321 CFStringRef path;
322 SCNetworkProtocolPrivateRef protocolPrivate = (SCNetworkProtocolPrivateRef)protocol;
323 SCNetworkServicePrivateRef servicePrivate = (SCNetworkServicePrivateRef)protocolPrivate->service;
324
325 if (!isA_SCNetworkProtocol(protocol)) {
326 _SCErrorSet(kSCStatusInvalidArgument);
327 return FALSE;
328 }
329
330 path = copyProtocolConfigurationPath(protocolPrivate);
331 ok = __setPrefsEnabled(servicePrivate->prefs, path, enabled);
332 CFRelease(path);
333
334 if (ok) {
335 SC_log(LOG_DEBUG, "SCNetworkProtocolSetEnabled(): %@ -> %s",
336 protocol,
337 enabled ? "Enabled" : "Disabled");
338 }
339
340 return ok;
341 }