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