2 * Copyright (c) 2004,2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * May 27, 2004 Allan Nathanson <ajn@apple.com>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <SystemConfiguration/SystemConfiguration.h>
34 #include <SystemConfiguration/SCValidation.h>
35 #include <SystemConfiguration/SCPrivate.h>
37 #include <sys/ioctl.h>
41 __private_extern__ CFDictionaryRef
42 __getPrefsConfiguration(SCPreferencesRef prefs
, CFStringRef path
)
44 CFDictionaryRef config
;
47 config
= SCPreferencesPathGetValue(prefs
, path
);
49 n
= isA_CFDictionary(config
) ? CFDictionaryGetCount(config
) : 0;
52 // ignore empty configuration entities
56 if (CFDictionaryContainsKey(config
, kSCResvInactive
)) {
57 // ignore [effectively] empty configuration entities
69 __private_extern__ Boolean
70 __setPrefsConfiguration(SCPreferencesRef prefs
,
72 CFDictionaryRef config
,
75 CFMutableDictionaryRef newConfig
;
78 if (!isA_CFDictionary(config
)) {
79 _SCErrorSet(kSCStatusInvalidArgument
);
83 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
86 CFDictionaryRef curConfig
;
88 // preserve enabled/disabled state
89 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
90 if (isA_CFDictionary(curConfig
) && CFDictionaryContainsKey(curConfig
, kSCResvInactive
)) {
91 // if currently disabled
92 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
94 // if currently enabled
95 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
99 // set new configuration
100 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
102 CFRelease(newConfig
);
107 __private_extern__ Boolean
108 __getPrefsEnabled(SCPreferencesRef prefs
, CFStringRef path
)
110 CFDictionaryRef config
;
112 config
= SCPreferencesPathGetValue(prefs
, path
);
113 if (isA_CFDictionary(config
) && CFDictionaryContainsKey(config
, kSCResvInactive
)) {
121 __private_extern__ Boolean
122 __setPrefsEnabled(SCPreferencesRef prefs
,
126 CFDictionaryRef curConfig
= NULL
;
127 CFMutableDictionaryRef newConfig
= NULL
;
130 // preserve current configuration
131 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
132 if (curConfig
!= NULL
) {
133 if (!isA_CFDictionary(curConfig
)) {
134 _SCErrorSet(kSCStatusFailed
);
137 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, curConfig
);
139 newConfig
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
144 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
147 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
150 // update configuration
151 if (CFDictionaryGetCount(newConfig
) == 0) {
152 CFRelease(newConfig
);
156 if (newConfig
== NULL
) {
157 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
159 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
162 if (newConfig
!= NULL
) CFRelease(newConfig
);
167 #define SYSTEMCONFIGURATION_BUNDLE_ID CFSTR("com.apple.SystemConfiguration")
168 #define SYSTEMCONFIGURATION_FRAMEWORK "SystemConfiguration.framework"
171 static CFDictionaryRef
176 CFDictionaryRef templates
;
178 CFStringRef xmlError
= NULL
;
179 CFDataRef xmlTemplates
= NULL
;
181 bundle
= CFBundleGetBundleWithIdentifier(SYSTEMCONFIGURATION_BUNDLE_ID
);
182 if (bundle
== NULL
) {
186 url
= CFBundleCopyResourceURL(bundle
, CFSTR("NetworkConfiguration"), CFSTR("plist"), NULL
);
191 ok
= CFURLCreateDataAndPropertiesFromResource(NULL
, url
, &xmlTemplates
, NULL
, NULL
, NULL
);
193 if (!ok
|| (xmlTemplates
== NULL
)) {
197 // convert the XML data into a property list
198 templates
= CFPropertyListCreateFromXMLData(NULL
, xmlTemplates
, kCFPropertyListImmutable
, &xmlError
);
199 CFRelease(xmlTemplates
);
200 if (templates
== NULL
) {
201 if (xmlError
!= NULL
) {
202 SCLog(TRUE
, LOG_DEBUG
, CFSTR("could not load SCNetworkConfiguration templates: %@"), xmlError
);
208 if (!isA_CFDictionary(templates
)) {
209 CFRelease(templates
);
217 __private_extern__ CFDictionaryRef
218 __copyInterfaceTemplate(CFStringRef interfaceType
,
219 CFStringRef childInterfaceType
)
221 CFDictionaryRef interface
= NULL
;
222 CFDictionaryRef interfaces
;
223 CFDictionaryRef templates
;
225 templates
= __copyTemplates();
226 if (templates
== NULL
) {
230 interfaces
= CFDictionaryGetValue(templates
, CFSTR("Interface"));
231 if (!isA_CFDictionary(interfaces
)) {
232 CFRelease(templates
);
236 if (childInterfaceType
== NULL
) {
237 interface
= CFDictionaryGetValue(interfaces
, interfaceType
);
239 CFStringRef expandedType
;
241 expandedType
= CFStringCreateWithFormat(NULL
,
246 interface
= CFDictionaryGetValue(interfaces
, expandedType
);
247 CFRelease(expandedType
);
250 if (isA_CFDictionary(interface
) && (CFDictionaryGetCount(interface
) > 0)) {
256 CFRelease(templates
);
262 __private_extern__ CFDictionaryRef
263 __copyProtocolTemplate(CFStringRef interfaceType
,
264 CFStringRef childInterfaceType
,
265 CFStringRef protocolType
)
267 CFDictionaryRef interface
= NULL
;
268 CFDictionaryRef protocol
= NULL
;
269 CFDictionaryRef protocols
;
270 CFDictionaryRef templates
;
272 templates
= __copyTemplates();
273 if (templates
== NULL
) {
277 protocols
= CFDictionaryGetValue(templates
, CFSTR("Protocol"));
278 if (!isA_CFDictionary(protocols
)) {
279 CFRelease(templates
);
283 if (childInterfaceType
== NULL
) {
284 interface
= CFDictionaryGetValue(protocols
, interfaceType
);
286 CFStringRef expandedType
;
288 expandedType
= CFStringCreateWithFormat(NULL
,
293 interface
= CFDictionaryGetValue(protocols
, expandedType
);
294 CFRelease(expandedType
);
297 if (isA_CFDictionary(interface
)) {
298 protocol
= CFDictionaryGetValue(interface
, protocolType
);
299 if (isA_CFDictionary(protocol
) && (CFDictionaryGetCount(protocol
) > 0)) {
306 CFRelease(templates
);
312 __private_extern__ Boolean
313 __createInterface(int s
, CFStringRef interface
)
317 bzero(&ifr
, sizeof(ifr
));
318 (void) _SC_cfstring_to_cstring(interface
,
320 sizeof(ifr
.ifr_name
),
321 kCFStringEncodingASCII
);
323 if (ioctl(s
, SIOCIFCREATE
, &ifr
) == -1) {
326 CFSTR("could not create interface \"%@\": %s"),
336 __private_extern__ Boolean
337 __destroyInterface(int s
, CFStringRef interface
)
341 bzero(&ifr
, sizeof(ifr
));
342 (void) _SC_cfstring_to_cstring(interface
,
344 sizeof(ifr
.ifr_name
),
345 kCFStringEncodingASCII
);
347 if (ioctl(s
, SIOCIFDESTROY
, &ifr
) == -1) {
350 CFSTR("could not destroy interface \"%@\": %s"),
360 __private_extern__ Boolean
361 __markInterfaceUp(int s
, CFStringRef interface
)
365 bzero(&ifr
, sizeof(ifr
));
366 (void) _SC_cfstring_to_cstring(interface
,
368 sizeof(ifr
.ifr_name
),
369 kCFStringEncodingASCII
);
371 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) == -1) {
374 CFSTR("could not get flags for interface \"%@\": %s"),
380 ifr
.ifr_flags
|= IFF_UP
;
381 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
)&ifr
) == -1) {
384 CFSTR("could not set flags for interface \"%@\": %s"),