2 * Copyright (c) 2004 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
= NULL
;
79 if (!isA_CFDictionary(config
)) {
80 _SCErrorSet(kSCStatusInvalidArgument
);
83 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
85 newConfig
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
89 CFDictionaryRef curConfig
;
92 * preserve enabled/disabled state
95 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
96 if (isA_CFDictionary(curConfig
) && CFDictionaryContainsKey(curConfig
, kSCResvInactive
)) {
97 // if currently disabled
98 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
100 // if currently enabled
101 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
106 * set new configuration
109 if (CFDictionaryGetCount(newConfig
) == 0) {
110 CFRelease(newConfig
);
114 if (newConfig
== NULL
) {
115 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
117 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
120 if (newConfig
!= NULL
) CFRelease(newConfig
);
125 __private_extern__ Boolean
126 __getPrefsEnabled(SCPreferencesRef prefs
, CFStringRef path
)
128 CFDictionaryRef config
;
130 config
= SCPreferencesPathGetValue(prefs
, path
);
131 if (isA_CFDictionary(config
) && CFDictionaryContainsKey(config
, kSCResvInactive
)) {
139 __private_extern__ Boolean
140 __setPrefsEnabled(SCPreferencesRef prefs
,
144 CFDictionaryRef curConfig
= NULL
;
145 CFMutableDictionaryRef newConfig
= NULL
;
149 * preserve current configuration
152 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
153 if (curConfig
!= NULL
) {
154 if (!isA_CFDictionary(curConfig
)) {
155 _SCErrorSet(kSCStatusFailed
);
158 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, curConfig
);
160 newConfig
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
165 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
168 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
172 * update configuration
175 if (CFDictionaryGetCount(newConfig
) == 0) {
176 CFRelease(newConfig
);
180 if (newConfig
== NULL
) {
181 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
183 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
186 if (newConfig
!= NULL
) CFRelease(newConfig
);
191 #define SYSTEMCONFIGURATION_BUNDLE_ID CFSTR("com.apple.SystemConfiguration")
192 #define SYSTEMCONFIGURATION_FRAMEWORK "SystemConfiguration.framework"
195 static CFDictionaryRef
200 CFDictionaryRef templates
;
202 CFStringRef xmlError
= NULL
;
203 CFDataRef xmlTemplates
= NULL
;
205 bundle
= CFBundleGetBundleWithIdentifier(SYSTEMCONFIGURATION_BUNDLE_ID
);
206 if (bundle
== NULL
) {
210 url
= CFBundleCopyResourceURL(bundle
, CFSTR("NetworkConfiguration"), CFSTR("plist"), NULL
);
215 ok
= CFURLCreateDataAndPropertiesFromResource(NULL
, url
, &xmlTemplates
, NULL
, NULL
, NULL
);
217 if (!ok
|| (xmlTemplates
== NULL
)) {
221 /* convert the XML data into a property list */
222 templates
= CFPropertyListCreateFromXMLData(NULL
, xmlTemplates
, kCFPropertyListImmutable
, &xmlError
);
223 CFRelease(xmlTemplates
);
224 if (templates
== NULL
) {
225 if (xmlError
!= NULL
) {
226 SCLog(TRUE
, LOG_DEBUG
, CFSTR("could not load SCNetworkConfiguration templates: %@"), xmlError
);
232 if (!isA_CFDictionary(templates
)) {
233 CFRelease(templates
);
241 __private_extern__ CFDictionaryRef
242 __copyInterfaceTemplate(CFStringRef interfaceType
,
243 CFStringRef childInterfaceType
)
245 CFDictionaryRef interface
= NULL
;
246 CFDictionaryRef interfaces
;
247 CFDictionaryRef templates
;
249 templates
= __copyTemplates();
250 if (templates
== NULL
) {
254 interfaces
= CFDictionaryGetValue(templates
, CFSTR("Interface"));
255 if (!isA_CFDictionary(interfaces
)) {
256 CFRelease(templates
);
260 if (childInterfaceType
== NULL
) {
261 interface
= CFDictionaryGetValue(interfaces
, interfaceType
);
263 CFStringRef expandedType
;
265 expandedType
= CFStringCreateWithFormat(NULL
,
270 interface
= CFDictionaryGetValue(interfaces
, expandedType
);
271 CFRelease(expandedType
);
274 if (isA_CFDictionary(interface
) && (CFDictionaryGetCount(interface
) > 0)) {
280 CFRelease(templates
);
286 __private_extern__ CFDictionaryRef
287 __copyProtocolTemplate(CFStringRef interfaceType
,
288 CFStringRef childInterfaceType
,
289 CFStringRef protocolType
)
291 CFDictionaryRef interface
= NULL
;
292 CFDictionaryRef protocol
= NULL
;
293 CFDictionaryRef protocols
;
294 CFDictionaryRef templates
;
296 templates
= __copyTemplates();
297 if (templates
== NULL
) {
301 protocols
= CFDictionaryGetValue(templates
, CFSTR("Protocol"));
302 if (!isA_CFDictionary(protocols
)) {
303 CFRelease(templates
);
307 if (childInterfaceType
== NULL
) {
308 interface
= CFDictionaryGetValue(protocols
, interfaceType
);
310 CFStringRef expandedType
;
312 expandedType
= CFStringCreateWithFormat(NULL
,
317 interface
= CFDictionaryGetValue(protocols
, expandedType
);
318 CFRelease(expandedType
);
321 if (isA_CFDictionary(interface
)) {
322 protocol
= CFDictionaryGetValue(interface
, protocolType
);
323 if (isA_CFDictionary(protocol
) && (CFDictionaryGetCount(protocol
) > 0)) {
330 CFRelease(templates
);
336 __private_extern__ Boolean
337 __createInterface(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
, SIOCIFCREATE
, &ifr
) == -1) {
350 CFSTR("could not create interface \"%@\": %s"),
360 __private_extern__ Boolean
361 __destroyInterface(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
, SIOCIFDESTROY
, &ifr
) == -1) {
374 CFSTR("could not destroy interface \"%@\": %s"),
384 __private_extern__ Boolean
385 __markInterfaceUp(int s
, CFStringRef interface
)
389 bzero(&ifr
, sizeof(ifr
));
390 (void) _SC_cfstring_to_cstring(interface
,
392 sizeof(ifr
.ifr_name
),
393 kCFStringEncodingASCII
);
395 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) == -1) {
398 CFSTR("could not get flags for interface \"%@\": %s"),
404 ifr
.ifr_flags
|= IFF_UP
;
405 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
)&ifr
) == -1) {
408 CFSTR("could not set flags for interface \"%@\": %s"),