2 * Copyright (c) 2004-2016 Apple 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 13, 2004 Allan Nathanson <ajn@apple.com>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <CoreFoundation/CFRuntime.h>
34 #include "SCNetworkConfigurationInternal.h"
35 #include "SCPreferencesInternal.h"
39 #define EXTERNAL_ID_DOMAIN_PREFIX "_"
41 static CFStringRef
__SCNetworkServiceCopyDescription (CFTypeRef cf
);
42 static void __SCNetworkServiceDeallocate (CFTypeRef cf
);
43 static Boolean
__SCNetworkServiceEqual (CFTypeRef cf1
, CFTypeRef cf2
);
44 static CFHashCode
__SCNetworkServiceHash (CFTypeRef cf
);
47 static CFTypeID __kSCNetworkServiceTypeID
= _kCFRuntimeNotATypeID
;
50 static const CFRuntimeClass __SCNetworkServiceClass
= {
52 "SCNetworkService", // className
55 __SCNetworkServiceDeallocate
, // dealloc
56 __SCNetworkServiceEqual
, // equal
57 __SCNetworkServiceHash
, // hash
58 NULL
, // copyFormattingDesc
59 __SCNetworkServiceCopyDescription
// copyDebugDesc
63 static pthread_once_t initialized
= PTHREAD_ONCE_INIT
;
67 __SCNetworkServiceCopyDescription(CFTypeRef cf
)
69 CFAllocatorRef allocator
= CFGetAllocator(cf
);
70 CFMutableStringRef result
;
71 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)cf
;
73 result
= CFStringCreateMutable(allocator
, 0);
74 CFStringAppendFormat(result
, NULL
, CFSTR("<SCNetworkService %p [%p]> {"), cf
, allocator
);
75 CFStringAppendFormat(result
, NULL
, CFSTR("id = %@"), servicePrivate
->serviceID
);
76 if (servicePrivate
->prefs
!= NULL
) {
77 CFStringAppendFormat(result
, NULL
, CFSTR(", prefs = %p"), servicePrivate
->prefs
);
78 } else if (servicePrivate
->store
!= NULL
) {
79 CFStringAppendFormat(result
, NULL
, CFSTR(", store = %p"), servicePrivate
->store
);
81 if (servicePrivate
->name
!= NULL
) {
82 CFStringAppendFormat(result
, NULL
, CFSTR(", name = %@"), servicePrivate
->name
);
84 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
91 __SCNetworkServiceDeallocate(CFTypeRef cf
)
93 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)cf
;
95 /* release resources */
97 CFRelease(servicePrivate
->serviceID
);
98 if (servicePrivate
->interface
!= NULL
) CFRelease(servicePrivate
->interface
);
99 if (servicePrivate
->prefs
!= NULL
) CFRelease(servicePrivate
->prefs
);
100 if (servicePrivate
->store
!= NULL
) CFRelease(servicePrivate
->store
);
101 if (servicePrivate
->name
!= NULL
) CFRelease(servicePrivate
->name
);
102 if (servicePrivate
->externalIDs
!= NULL
) CFRelease(servicePrivate
->externalIDs
);
109 __SCNetworkServiceEqual(CFTypeRef cf1
, CFTypeRef cf2
)
111 SCNetworkServicePrivateRef s1
= (SCNetworkServicePrivateRef
)cf1
;
112 SCNetworkServicePrivateRef s2
= (SCNetworkServicePrivateRef
)cf2
;
117 if (s1
->prefs
!= s2
->prefs
)
118 return FALSE
; // if not the same prefs
120 if (!CFEqual(s1
->serviceID
, s2
->serviceID
))
121 return FALSE
; // if not the same service identifier
128 __SCNetworkServiceHash(CFTypeRef cf
)
130 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)cf
;
132 return CFHash(servicePrivate
->serviceID
);
137 __SCNetworkServiceInitialize(void)
139 __kSCNetworkServiceTypeID
= _CFRuntimeRegisterClass(&__SCNetworkServiceClass
);
144 __private_extern__ SCNetworkServicePrivateRef
145 __SCNetworkServiceCreatePrivate(CFAllocatorRef allocator
,
146 SCPreferencesRef prefs
,
147 CFStringRef serviceID
,
148 SCNetworkInterfaceRef interface
)
150 SCNetworkServicePrivateRef servicePrivate
;
153 /* initialize runtime */
154 pthread_once(&initialized
, __SCNetworkServiceInitialize
);
156 /* allocate target */
157 size
= sizeof(SCNetworkServicePrivate
) - sizeof(CFRuntimeBase
);
158 servicePrivate
= (SCNetworkServicePrivateRef
)_CFRuntimeCreateInstance(allocator
,
159 __kSCNetworkServiceTypeID
,
162 if (servicePrivate
== NULL
) {
166 /* initialize non-zero/NULL members */
167 servicePrivate
->prefs
= (prefs
!= NULL
) ? CFRetain(prefs
): NULL
;
168 servicePrivate
->serviceID
= CFStringCreateCopy(NULL
, serviceID
);
169 servicePrivate
->interface
= (interface
!= NULL
) ? CFRetain(interface
) : NULL
;
171 return servicePrivate
;
176 #pragma mark Service ordering
180 _SCNetworkServiceCompare(const void *val1
, const void *val2
, void *context
)
184 CFArrayRef order
= (CFArrayRef
)context
;
185 SCNetworkServiceRef s1
= (SCNetworkServiceRef
)val1
;
186 SCNetworkServiceRef s2
= (SCNetworkServiceRef
)val2
;
188 id1
= SCNetworkServiceGetServiceID(s1
);
189 id2
= SCNetworkServiceGetServiceID(s2
);
196 range
= CFRangeMake(0, CFArrayGetCount(order
));
197 o1
= CFArrayGetFirstIndexOfValue(order
, range
, id1
);
198 o2
= CFArrayGetFirstIndexOfValue(order
, range
, id2
);
201 return (o2
!= kCFNotFound
) ? kCFCompareGreaterThan
: kCFCompareLessThan
;
202 } else if (o1
< o2
) {
203 return (o1
!= kCFNotFound
) ? kCFCompareLessThan
: kCFCompareGreaterThan
;
207 return CFStringCompare(id1
, id2
, 0);
212 #pragma mark SCNetworkService APIs
218 __private_extern__ CFArrayRef
/* of SCNetworkServiceRef's */
219 __SCNetworkServiceCopyAllEnabled(SCPreferencesRef prefs
)
221 CFMutableArrayRef array
= NULL
;
226 sets
= SCNetworkSetCopyAll(prefs
);
231 n_sets
= CFArrayGetCount(sets
);
232 for (i_sets
= 0; i_sets
< n_sets
; i_sets
++) {
238 set
= CFArrayGetValueAtIndex(sets
, i_sets
);
239 services
= SCNetworkSetCopyServices(set
);
240 if (services
== NULL
) {
244 n_services
= CFArrayGetCount(services
);
245 for (i_services
= 0; i_services
< n_services
; i_services
++) {
246 SCNetworkServiceRef service
;
248 service
= CFArrayGetValueAtIndex(services
, i_services
);
249 if (!SCNetworkServiceGetEnabled(service
)) {
254 if ((array
== NULL
) ||
255 !CFArrayContainsValue(array
,
256 CFRangeMake(0, CFArrayGetCount(array
)),
259 array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
261 CFArrayAppendValue(array
, service
);
272 __private_extern__ Boolean
273 __SCNetworkServiceExistsForInterface(CFArrayRef services
, SCNetworkInterfaceRef interface
)
278 n
= isA_CFArray(services
) ? CFArrayGetCount(services
) : 0;
279 for (i
= 0; i
< n
; i
++) {
280 SCNetworkServiceRef service
;
281 SCNetworkInterfaceRef service_interface
;
283 service
= CFArrayGetValueAtIndex(services
, i
);
285 service_interface
= SCNetworkServiceGetInterface(service
);
286 while (service_interface
!= NULL
) {
287 if (CFEqual(interface
, service_interface
)) {
291 service_interface
= SCNetworkInterfaceGetInterface(service_interface
);
300 mergeDict(const void *key
, const void *value
, void *context
)
302 CFMutableDictionaryRef newDict
= (CFMutableDictionaryRef
)context
;
304 CFDictionarySetValue(newDict
, key
, value
);
309 static CF_RETURNS_RETAINED CFDictionaryRef
310 _protocolTemplate(SCNetworkServiceRef service
, CFStringRef protocolType
)
312 SCNetworkInterfaceRef interface
;
313 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
314 CFDictionaryRef
template = NULL
;
316 interface
= servicePrivate
->interface
;
317 if (interface
!= NULL
) {
318 SCNetworkInterfaceRef childInterface
;
319 CFStringRef childInterfaceType
= NULL
;
320 CFStringRef interfaceType
;
323 interfaceType
= SCNetworkInterfaceGetInterfaceType(servicePrivate
->interface
);
324 childInterface
= SCNetworkInterfaceGetInterface(servicePrivate
->interface
);
325 if (childInterface
!= NULL
) {
326 childInterfaceType
= SCNetworkInterfaceGetInterfaceType(childInterface
);
329 template = __copyProtocolTemplate(interfaceType
, childInterfaceType
, protocolType
);
330 if (template != NULL
) {
331 CFDictionaryRef overrides
;
333 // move to the interface at the lowest layer
334 while (childInterface
!= NULL
) {
335 interface
= childInterface
;
336 childInterface
= SCNetworkInterfaceGetInterface(interface
);
339 overrides
= __SCNetworkInterfaceGetTemplateOverrides(interface
, protocolType
);
340 if (overrides
!= NULL
) {
341 CFMutableDictionaryRef newTemplate
;
343 newTemplate
= CFDictionaryCreateMutableCopy(NULL
, 0, template);
344 CFDictionaryApplyFunction(overrides
, mergeDict
, newTemplate
);
346 template = newTemplate
;
351 if (template == NULL
) {
352 template = CFDictionaryCreate(NULL
,
356 &kCFTypeDictionaryKeyCallBacks
,
357 &kCFTypeDictionaryValueCallBacks
);
365 SCNetworkServiceAddProtocolType(SCNetworkServiceRef service
, CFStringRef protocolType
)
367 CFDictionaryRef entity
;
368 CFDictionaryRef newEntity
= NULL
;
371 SCNetworkProtocolRef protocol
;
372 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
374 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
375 _SCErrorSet(kSCStatusInvalidArgument
);
379 if (!__SCNetworkProtocolIsValidType(protocolType
)) {
380 _SCErrorSet(kSCStatusInvalidArgument
);
384 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
385 servicePrivate
->serviceID
, // service
386 protocolType
); // entity
388 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
389 if (entity
!= NULL
) {
390 // if "protocol" already exists
391 _SCErrorSet(kSCStatusKeyExists
);
395 newEntity
= CFDictionaryCreate(NULL
,
399 &kCFTypeDictionaryKeyCallBacks
,
400 &kCFTypeDictionaryValueCallBacks
);
401 ok
= SCPreferencesPathSetValue(servicePrivate
->prefs
, path
, newEntity
);
402 CFRelease(newEntity
);
407 protocol
= SCNetworkServiceCopyProtocol(service
, protocolType
);
408 assert(protocol
!= NULL
);
410 newEntity
= _protocolTemplate(service
, protocolType
);
411 ok
= SCNetworkProtocolSetConfiguration(protocol
, newEntity
);
412 CFRelease(newEntity
);
418 SC_log(LOG_DEBUG
, "SCNetworkServiceAddProtocolType(): %@, %@", service
, protocolType
);
426 CFArrayRef
/* of SCNetworkServiceRef's */
427 SCNetworkServiceCopyAll(SCPreferencesRef prefs
)
429 CFMutableArrayRef array
;
432 CFDictionaryRef services
;
434 path
= SCPreferencesPathKeyCreateNetworkServices(NULL
);
435 services
= SCPreferencesPathGetValue(prefs
, path
);
438 if ((services
!= NULL
) && !isA_CFDictionary(services
)) {
442 array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
444 n
= (services
!= NULL
) ? CFDictionaryGetCount(services
) : 0;
447 const void * keys_q
[N_QUICK
];
448 const void ** keys
= keys_q
;
449 const void * vals_q
[N_QUICK
];
450 const void ** vals
= vals_q
;
452 if (n
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
453 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
454 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
456 CFDictionaryGetKeysAndValues(services
, keys
, vals
);
457 for (i
= 0; i
< n
; i
++) {
458 CFDictionaryRef entity
;
459 SCNetworkServicePrivateRef servicePrivate
;
461 if (!isA_CFDictionary(vals
[i
])) {
462 SC_log(LOG_INFO
, "error w/service \"%@\"", keys
[i
]);
466 entity
= CFDictionaryGetValue(vals
[i
], kSCEntNetInterface
);
467 if (!isA_CFDictionary(entity
)) {
469 SC_log(LOG_INFO
, "no \"%@\" entity for service \"%@\"",
475 if (__SCNetworkInterfaceEntityIsPPTP(entity
)) {
476 SC_log(LOG_INFO
, "PPTP services are no longer supported");
480 servicePrivate
= __SCNetworkServiceCreatePrivate(NULL
, prefs
, keys
[i
], NULL
);
481 assert(servicePrivate
!= NULL
);
482 CFArrayAppendValue(array
, (SCNetworkServiceRef
)servicePrivate
);
483 CFRelease(servicePrivate
);
485 if (keys
!= keys_q
) {
486 CFAllocatorDeallocate(NULL
, keys
);
487 CFAllocatorDeallocate(NULL
, vals
);
496 CFArrayRef
/* of SCNetworkInterfaceRef's */
497 __SCNetworkServiceCopyAllInterfaces(SCPreferencesRef prefs
)
499 CFMutableArrayRef interfaces
= NULL
;
500 CFArrayRef services
= NULL
;
501 CFIndex servicesCount
= 0;
502 SCNetworkServiceRef service
= NULL
;
503 SCNetworkInterfaceRef interface
= NULL
;
505 services
= SCNetworkServiceCopyAll(prefs
);
506 if (services
== NULL
) {
510 servicesCount
= CFArrayGetCount(services
);
511 if (servicesCount
== 0) {
515 interfaces
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
517 for (CFIndex idx
= 0; idx
< servicesCount
; idx
++) {
518 service
= CFArrayGetValueAtIndex(services
, idx
);
519 interface
= SCNetworkServiceGetInterface(service
);
521 if (isA_SCNetworkInterface(interface
) == NULL
) {
524 CFArrayAppendValue(interfaces
, interface
);
527 if (CFArrayGetCount(interfaces
) == 0) {
528 // Do not return an empty array
529 CFRelease(interfaces
);
535 if (services
!= NULL
) {
543 * build a list of all of a services entity types that are associated
544 * with the services interface. The list will include :
546 * - entity types associated with the interface type (Ethernet, FireWire, PPP, ...)
547 * - entity types associated with the interface sub-type (PPPSerial, PPPoE, L2TP, PPTP, ...)
548 * - entity types associated with the hardware device (Ethernet, AirPort, FireWire, Modem, ...)
551 _copyInterfaceEntityTypes(CFDictionaryRef protocols
)
553 CFDictionaryRef interface
;
554 CFMutableSetRef interface_entity_types
;
556 interface_entity_types
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
558 interface
= CFDictionaryGetValue(protocols
, kSCEntNetInterface
);
559 if (isA_CFDictionary(interface
)) {
560 CFStringRef entities
[] = { kSCPropNetInterfaceType
,
561 kSCPropNetInterfaceSubType
,
562 kSCPropNetInterfaceHardware
};
565 // include the "Interface" entity itself
566 CFSetAddValue(interface_entity_types
, kSCEntNetInterface
);
568 // include the entities associated with the interface
569 for (i
= 0; i
< sizeof(entities
)/sizeof(entities
[0]); i
++) {
572 entity
= CFDictionaryGetValue(interface
, entities
[i
]);
573 if (isA_CFString(entity
)) {
574 CFSetAddValue(interface_entity_types
, entity
);
579 * and, because we've found some misguided network preference code
580 * developers leaving [PPP] entity dictionaries around even though
581 * they are unused and/or unneeded...
583 CFSetAddValue(interface_entity_types
, kSCEntNetPPP
);
586 return interface_entity_types
;
591 SCNetworkServiceCopy(SCPreferencesRef prefs
, CFStringRef serviceID
)
593 CFDictionaryRef entity
;
595 SCNetworkServicePrivateRef servicePrivate
;
597 if (!isA_CFString(serviceID
)) {
598 _SCErrorSet(kSCStatusInvalidArgument
);
602 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
603 serviceID
, // service
604 kSCEntNetInterface
); // entity
605 entity
= SCPreferencesPathGetValue(prefs
, path
);
608 if (!isA_CFDictionary(entity
)) {
609 // a "service" must have an "interface"
610 _SCErrorSet(kSCStatusNoKey
);
614 if (__SCNetworkInterfaceEntityIsPPTP(entity
)) {
615 SC_log(LOG_INFO
, "PPTP services are no longer supported");
616 _SCErrorSet(kSCStatusNoKey
);
620 servicePrivate
= __SCNetworkServiceCreatePrivate(NULL
, prefs
, serviceID
, NULL
);
621 return (SCNetworkServiceRef
)servicePrivate
;
626 _SCNetworkServiceCopyActive(SCDynamicStoreRef store
, CFStringRef serviceID
)
628 SCNetworkServicePrivateRef servicePrivate
;
630 if (!isA_CFString(serviceID
)) {
631 _SCErrorSet(kSCStatusInvalidArgument
);
635 servicePrivate
= __SCNetworkServiceCreatePrivate(NULL
, NULL
, serviceID
, NULL
);
636 assert(servicePrivate
!= NULL
);
638 servicePrivate
->store
= CFRetain(store
);
640 return (SCNetworkServiceRef
)servicePrivate
;
645 SCNetworkServiceCopyProtocol(SCNetworkServiceRef service
, CFStringRef protocolType
)
647 CFSetRef non_protocol_entities
;
649 CFDictionaryRef protocols
;
650 SCNetworkProtocolPrivateRef protocolPrivate
= NULL
;
651 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
653 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
654 _SCErrorSet(kSCStatusInvalidArgument
);
658 if (!isA_CFString(protocolType
)) {
659 _SCErrorSet(kSCStatusInvalidArgument
);
663 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
664 servicePrivate
->serviceID
, // service
666 protocols
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
669 if (!isA_CFDictionary(protocols
)) {
671 _SCErrorSet(kSCStatusFailed
);
675 non_protocol_entities
= _copyInterfaceEntityTypes(protocols
);
676 if (CFSetContainsValue(non_protocol_entities
, protocolType
)) {
677 // if the "protocolType" matches an interface entity type
678 _SCErrorSet(kSCStatusInvalidArgument
);
682 if (!CFDictionaryContainsKey(protocols
, protocolType
)) {
683 // if the "protocolType" entity does not exist
684 _SCErrorSet(kSCStatusNoKey
);
688 protocolPrivate
= __SCNetworkProtocolCreatePrivate(NULL
, protocolType
, service
);
692 CFRelease(non_protocol_entities
);
694 return (SCNetworkProtocolRef
)protocolPrivate
;
698 CFArrayRef
/* of SCNetworkProtocolRef's */
699 SCNetworkServiceCopyProtocols(SCNetworkServiceRef service
)
701 CFMutableArrayRef array
;
703 CFSetRef non_protocol_entities
;
705 CFDictionaryRef protocols
;
706 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
708 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
709 _SCErrorSet(kSCStatusInvalidArgument
);
713 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
714 servicePrivate
->serviceID
, // service
716 protocols
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
719 if (!isA_CFDictionary(protocols
)) {
721 _SCErrorSet(kSCStatusFailed
);
725 non_protocol_entities
= _copyInterfaceEntityTypes(protocols
);
727 array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
729 n
= CFDictionaryGetCount(protocols
);
732 const void * keys_q
[N_QUICK
];
733 const void ** keys
= keys_q
;
734 const void * vals_q
[N_QUICK
];
735 const void ** vals
= vals_q
;
737 if (n
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
738 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
739 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
741 CFDictionaryGetKeysAndValues(protocols
, keys
, vals
);
742 for (i
= 0; i
< n
; i
++) {
743 SCNetworkProtocolPrivateRef protocolPrivate
;
745 if (!isA_CFDictionary(vals
[i
])) {
746 // if it's not a dictionary then it can't be a protocol entity
750 if (CFSetContainsValue(non_protocol_entities
, keys
[i
])) {
751 // skip any non-protocol (interface) entities
755 protocolPrivate
= __SCNetworkProtocolCreatePrivate(NULL
, keys
[i
], service
);
756 CFArrayAppendValue(array
, (SCNetworkProtocolRef
)protocolPrivate
);
758 CFRelease(protocolPrivate
);
760 if (keys
!= keys_q
) {
761 CFAllocatorDeallocate(NULL
, keys
);
762 CFAllocatorDeallocate(NULL
, vals
);
766 CFRelease(non_protocol_entities
);
773 __SCNetworkServiceSetInterfaceEntity(SCNetworkServiceRef service
,
774 SCNetworkInterfaceRef interface
)
776 CFDictionaryRef entity
;
779 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
781 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
782 servicePrivate
->serviceID
, // service
783 kSCEntNetInterface
); // entity
784 entity
= __SCNetworkInterfaceCopyInterfaceEntity(interface
);
785 ok
= SCPreferencesPathSetValue(servicePrivate
->prefs
, path
, entity
);
794 SCNetworkServiceCreate(SCPreferencesRef prefs
, SCNetworkInterfaceRef interface
)
796 CFArrayRef components
;
797 CFArrayRef interface_config
;
798 CFStringRef interface_name
;
799 SCNetworkInterfaceRef newInterface
;
802 CFStringRef serviceID
;
803 SCNetworkServicePrivateRef servicePrivate
;
804 CFArrayRef supported_protocols
;
806 if (!isA_SCNetworkInterface(interface
)) {
807 _SCErrorSet(kSCStatusInvalidArgument
);
811 // only allow network interfaces which support one or more protocols
812 // to be added to a service. The one exception is that we allow
813 // third-party interface types to be configured.
814 supported_protocols
= SCNetworkInterfaceGetSupportedProtocolTypes(interface
);
815 if (supported_protocols
== NULL
) {
816 CFStringRef interface_type
;
818 interface_type
= SCNetworkInterfaceGetInterfaceType(interface
);
819 if (CFStringFind(interface_type
, CFSTR("."), 0).location
== kCFNotFound
) {
820 _SCErrorSet(kSCStatusInvalidArgument
);
825 // do not allow creation of a network service if the interface is a
826 // member of a bond or bridge
827 if (__SCNetworkInterfaceIsMember(prefs
, interface
)) {
828 _SCErrorSet(kSCStatusKeyExists
);
832 // establish the service
833 prefix
= SCPreferencesPathKeyCreateNetworkServices(NULL
);
834 path
= __SCPreferencesPathCreateUniqueChild_WithMoreSCFCompatibility(prefs
, prefix
);
835 if (path
== NULL
) path
= SCPreferencesPathCreateUniqueChild(prefs
, prefix
);
841 components
= CFStringCreateArrayBySeparatingStrings(NULL
, path
, CFSTR("/"));
844 serviceID
= CFArrayGetValueAtIndex(components
, 2);
845 servicePrivate
= __SCNetworkServiceCreatePrivate(NULL
, prefs
, serviceID
, NULL
);
846 CFRelease(components
);
848 // duplicate the interface and associate the copy with the new service
849 newInterface
= (SCNetworkInterfaceRef
)__SCNetworkInterfaceCreateCopy(NULL
,
853 servicePrivate
->interface
= newInterface
;
855 // establish "default" configuration(s) for the interface
856 for (interface
= newInterface
;
858 interface
= SCNetworkInterfaceGetInterface(interface
)) {
859 SCNetworkInterfaceRef childInterface
;
860 CFStringRef childInterfaceType
= NULL
;
861 CFDictionaryRef config
;
862 CFStringRef interfaceType
;
864 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
865 childInterface
= SCNetworkInterfaceGetInterface(interface
);
866 if (childInterface
!= NULL
) {
867 childInterfaceType
= SCNetworkInterfaceGetInterfaceType(childInterface
);
870 config
= __copyInterfaceTemplate(interfaceType
, childInterfaceType
);
871 if (config
!= NULL
) {
872 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeBluetooth
) ||
873 CFEqual(interfaceType
, kSCNetworkInterfaceTypeIrDA
) ||
874 CFEqual(interfaceType
, kSCNetworkInterfaceTypeModem
) ||
875 CFEqual(interfaceType
, kSCNetworkInterfaceTypeSerial
) ||
876 CFEqual(interfaceType
, kSCNetworkInterfaceTypeWWAN
)) {
877 CFDictionaryRef overrides
;
879 overrides
= __SCNetworkInterfaceGetTemplateOverrides(interface
, kSCNetworkInterfaceTypeModem
);
881 // a ConnectionScript (and related keys) from the interface
882 // should trump the settings from the configuration template.
883 if (overrides
!= NULL
) {
884 CFMutableDictionaryRef newConfig
;
886 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
887 if (CFDictionaryContainsKey(overrides
, kSCPropNetModemConnectionScript
)) {
888 CFDictionaryRemoveValue(newConfig
, kSCPropNetModemConnectionPersonality
);
889 CFDictionaryRemoveValue(newConfig
, kSCPropNetModemConnectionScript
);
890 CFDictionaryRemoveValue(newConfig
, kSCPropNetModemDeviceVendor
);
891 CFDictionaryRemoveValue(newConfig
, kSCPropNetModemDeviceModel
);
893 CFDictionaryApplyFunction(overrides
, mergeDict
, newConfig
);
897 } else if (CFEqual(interfaceType
, kSCNetworkInterfaceTypePPP
) ||
898 CFEqual(interfaceType
, kSCNetworkInterfaceTypeVPN
)) {
899 CFDictionaryRef overrides
;
901 overrides
= __SCNetworkInterfaceGetTemplateOverrides(interface
, kSCNetworkInterfaceTypePPP
);
902 if (overrides
!= NULL
) {
903 CFMutableDictionaryRef newConfig
;
905 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
906 CFDictionaryApplyFunction(overrides
, mergeDict
, newConfig
);
912 if (!__SCNetworkInterfaceSetConfiguration(interface
, NULL
, config
, TRUE
)) {
913 SC_log(LOG_INFO
, "__SCNetworkInterfaceSetConfiguration failed(), interface=%@, type=NULL",
920 // add the interface [entity] to the service
921 (void) __SCNetworkServiceSetInterfaceEntity((SCNetworkServiceRef
)servicePrivate
,
922 servicePrivate
->interface
);
924 // push the [deep] interface configuration into the service.
925 interface_config
= __SCNetworkInterfaceCopyDeepConfiguration(NULL
, servicePrivate
->interface
);
926 __SCNetworkInterfaceSetDeepConfiguration(NULL
, servicePrivate
->interface
, interface_config
);
927 if (interface_config
!= NULL
) CFRelease(interface_config
);
929 // set the service name to match that of the associated interface
931 // Note: It might seem a bit odd to call SCNetworkServiceGetName
932 // followed by an immediate call to SCNetworkServiceSetName. The
933 // trick here is that if no name has previously been set, the
934 // "get" function will return the name of the associated interface.
936 // ... and we "set" a name to ensure that applications that do
937 // not use the APIs will still find a UserDefinedName property
938 // in the SCDynamicStore.
940 interface_name
= SCNetworkServiceGetName((SCNetworkServiceRef
)servicePrivate
);
941 if (interface_name
!= NULL
) {
942 (void) SCNetworkServiceSetName((SCNetworkServiceRef
)servicePrivate
,
946 SC_log(LOG_DEBUG
, "SCNetworkServiceCreate(): %@", servicePrivate
);
948 return (SCNetworkServiceRef
)servicePrivate
;
953 SCNetworkServiceEstablishDefaultConfiguration(SCNetworkServiceRef service
)
956 SCNetworkInterfaceRef interface
;
958 CFArrayRef protocolTypes
;
959 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
961 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
962 _SCErrorSet(kSCStatusInvalidArgument
);
966 interface
= SCNetworkServiceGetInterface(service
);
967 if (interface
== NULL
) {
971 protocolTypes
= SCNetworkInterfaceGetSupportedProtocolTypes(interface
);
972 n
= (protocolTypes
!= NULL
) ? CFArrayGetCount(protocolTypes
) : 0;
973 for (i
= 0; i
< n
; i
++) {
975 CFDictionaryRef newEntity
= NULL
;
977 SCNetworkProtocolRef protocol
= NULL
;
978 CFStringRef protocolType
;
980 protocolType
= CFArrayGetValueAtIndex(protocolTypes
, i
);
981 ok
= SCNetworkServiceAddProtocolType(service
, protocolType
);
982 if (!ok
&& (SCError() != kSCStatusKeyExists
)) {
983 // could not add protocol
987 protocol
= SCNetworkServiceCopyProtocol(service
, protocolType
);
988 if (protocol
== NULL
) {
989 // oops, somethings wrong (should never happen)
993 newEntity
= _protocolTemplate(service
, protocolType
);
994 ok
= SCNetworkProtocolSetConfiguration(protocol
, newEntity
);
996 // could not set default configuration
1000 enabled
= !CFDictionaryContainsKey(newEntity
, kSCResvInactive
);
1001 ok
= SCNetworkProtocolSetEnabled(protocol
, enabled
);
1003 // could not enable/disable protocol
1009 if (newEntity
!= NULL
) CFRelease(newEntity
);
1010 if (protocol
!= NULL
) CFRelease(protocol
);
1018 SCNetworkServiceGetEnabled(SCNetworkServiceRef service
)
1022 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1024 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1025 _SCErrorSet(kSCStatusInvalidArgument
);
1029 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1030 servicePrivate
->serviceID
, // service
1032 enabled
= __getPrefsEnabled(servicePrivate
->prefs
, path
);
1039 SCNetworkInterfaceRef
1040 SCNetworkServiceGetInterface(SCNetworkServiceRef service
)
1042 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1044 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1045 _SCErrorSet(kSCStatusInvalidArgument
);
1049 if (servicePrivate
->interface
== NULL
) {
1050 CFDictionaryRef entity
;
1053 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1054 servicePrivate
->serviceID
, // service
1055 kSCEntNetInterface
); // entity
1056 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
1059 if (isA_CFDictionary(entity
)) {
1060 servicePrivate
->interface
= _SCNetworkInterfaceCreateWithEntity(NULL
, entity
, service
);
1064 return servicePrivate
->interface
;
1069 SCNetworkServiceGetName(SCNetworkServiceRef service
)
1071 CFDictionaryRef entity
;
1072 SCNetworkInterfaceRef interface
;
1073 CFStringRef name
= NULL
;
1075 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1076 Boolean useSystemInterfaces
= TRUE
;
1078 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1079 _SCErrorSet(kSCStatusInvalidArgument
);
1083 if (servicePrivate
->name
!= NULL
) {
1084 return servicePrivate
->name
;
1087 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1088 servicePrivate
->serviceID
, // service
1090 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
1093 useSystemInterfaces
= ((__SCPreferencesUsingDefaultPrefs(servicePrivate
->prefs
)) &&
1094 (__SCPreferencesGetLimitSCNetworkConfiguration(servicePrivate
->prefs
) == FALSE
));
1096 if (isA_CFDictionary(entity
)) {
1097 name
= CFDictionaryGetValue(entity
, kSCPropUserDefinedName
);
1098 if (isA_CFString(name
)) {
1099 servicePrivate
->name
= CFRetain(name
);
1100 if (!useSystemInterfaces
) {
1101 return servicePrivate
->name
;
1106 interface
= SCNetworkServiceGetInterface(service
);
1107 while (interface
!= NULL
) {
1108 SCNetworkInterfaceRef childInterface
;
1109 CFStringRef interfaceType
;
1111 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
1112 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeVPN
)) {
1116 childInterface
= SCNetworkInterfaceGetInterface(interface
);
1117 if ((childInterface
== NULL
) ||
1118 CFEqual(childInterface
, kSCNetworkInterfaceIPv4
)) {
1122 interface
= childInterface
;
1125 if (interface
!= NULL
) {
1127 CFStringRef interface_name
= NULL
;
1128 CFStringRef suffix
= NULL
;
1131 // check if the [stored] service name matches the non-localized interface
1132 // name. If so, return the localized name.
1134 // Also, the older "Built-in XXX" interface names are too long for the
1135 // current UI. If we find that the [stored] service name matches the older
1136 // name, return the newer (and shorter) localized name.
1138 // Note: the user/admin will no longer be able to set the service name
1139 // to "Built-in Ethernet".
1141 for (i
= 0; i
< 3; i
++) {
1142 if (servicePrivate
->name
== NULL
) {
1143 // if no [stored] service name to compare
1149 // compare the non-localized interface name
1150 interface_name
= __SCNetworkInterfaceGetNonLocalizedDisplayName(interface
);
1151 if (interface_name
!= NULL
) {
1152 CFRetain(interface_name
);
1155 #if !TARGET_OS_IPHONE
1157 // compare the older "Built-in XXX" localized name
1158 interface_name
= __SCNetworkInterfaceCopyXLocalizedDisplayName(interface
);
1161 // compare the older "Built-in XXX" non-localized name
1162 interface_name
= __SCNetworkInterfaceCopyXNonLocalizedDisplayName(interface
);
1164 #else // !TARGET_OS_IPHONE
1167 #endif // !TARGET_OS_IPHONE
1170 if (interface_name
!= NULL
) {
1171 Boolean match
= FALSE
;
1173 if (CFEqual(name
, interface_name
)) {
1174 // if service name matches the OLD localized
1177 } else if (CFStringHasPrefix(name
, interface_name
)) {
1178 CFIndex prefixLen
= CFStringGetLength(interface_name
);
1179 CFIndex suffixLen
= CFStringGetLength(name
);
1181 suffix
= CFStringCreateWithSubstring(NULL
,
1183 CFRangeMake(prefixLen
, suffixLen
- prefixLen
));
1186 CFRelease(interface_name
);
1189 CFRelease(servicePrivate
->name
);
1190 servicePrivate
->name
= NULL
;
1197 // if the service name has not been set, use the localized interface name
1199 if (servicePrivate
->name
== NULL
) {
1200 interface_name
= SCNetworkInterfaceGetLocalizedDisplayName(interface
);
1201 if (interface_name
!= NULL
) {
1202 if (suffix
!= NULL
) {
1203 servicePrivate
->name
= CFStringCreateWithFormat(NULL
,
1209 servicePrivate
->name
= CFRetain(interface_name
);
1213 if (suffix
!= NULL
) CFRelease(suffix
);
1216 return servicePrivate
->name
;
1221 SCNetworkServiceGetServiceID(SCNetworkServiceRef service
)
1223 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1225 if (!isA_SCNetworkService(service
)) {
1226 _SCErrorSet(kSCStatusInvalidArgument
);
1230 return servicePrivate
->serviceID
;
1235 SCNetworkServiceGetTypeID(void)
1237 pthread_once(&initialized
, __SCNetworkServiceInitialize
); /* initialize runtime */
1238 return __kSCNetworkServiceTypeID
;
1243 SCNetworkServiceRemove(SCNetworkServiceRef service
)
1247 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1250 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1251 _SCErrorSet(kSCStatusInvalidArgument
);
1255 // remove service from all sets
1257 sets
= SCNetworkSetCopyAll(servicePrivate
->prefs
);
1262 n
= CFArrayGetCount(sets
);
1263 for (i
= 0; i
< n
; i
++) {
1264 SCNetworkSetRef set
;
1266 set
= CFArrayGetValueAtIndex(sets
, i
);
1267 ok
= SCNetworkSetRemoveService(set
, service
);
1268 if (!ok
&& (SCError() != kSCStatusNoKey
)) {
1278 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1279 servicePrivate
->serviceID
, // service
1281 ok
= SCPreferencesPathRemoveValue(servicePrivate
->prefs
, path
);
1285 SC_log(LOG_DEBUG
, "SCNetworkServiceRemove(): %@", service
);
1293 SCNetworkServiceRemoveProtocolType(SCNetworkServiceRef service
, CFStringRef protocolType
)
1295 CFDictionaryRef entity
;
1298 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1300 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1301 _SCErrorSet(kSCStatusInvalidArgument
);
1305 if (!__SCNetworkProtocolIsValidType(protocolType
)) {
1306 _SCErrorSet(kSCStatusInvalidArgument
);
1310 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1311 servicePrivate
->serviceID
, // service
1312 protocolType
); // entity
1314 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
1315 if (entity
== NULL
) {
1316 // if "protocol" does not exist
1317 _SCErrorSet(kSCStatusNoKey
);
1321 ok
= SCPreferencesPathRemoveValue(servicePrivate
->prefs
, path
);
1326 SC_log(LOG_DEBUG
, "SCNetworkServiceRemoveProtocolType(): %@, %@", service
, protocolType
);
1335 SCNetworkServiceSetEnabled(SCNetworkServiceRef service
, Boolean enabled
)
1339 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1341 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1342 _SCErrorSet(kSCStatusInvalidArgument
);
1346 // make sure that we do not enable a network service if the
1347 // associated interface is a member of a bond or bridge.
1349 SCNetworkInterfaceRef interface
;
1351 interface
= SCNetworkServiceGetInterface(service
);
1352 if ((interface
!= NULL
) &&
1353 __SCNetworkInterfaceIsMember(servicePrivate
->prefs
, interface
)) {
1354 _SCErrorSet(kSCStatusKeyExists
);
1359 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1360 servicePrivate
->serviceID
, // service
1362 ok
= __setPrefsEnabled(servicePrivate
->prefs
, path
, enabled
);
1366 SC_log(LOG_DEBUG
, "SCNetworkServiceSetEnabled(): %@ -> %s",
1368 enabled
? "Enabled" : "Disabled");
1376 SCNetworkServiceSetName(SCNetworkServiceRef service
, CFStringRef name
)
1378 CFDictionaryRef entity
;
1381 CFStringRef saveName
= NULL
;
1382 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1384 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1385 _SCErrorSet(kSCStatusInvalidArgument
);
1390 if (!isA_CFString(name
)) {
1391 _SCErrorSet(kSCStatusInvalidArgument
);
1394 saveName
= CFRetain(name
);
1398 SCNetworkInterfaceRef interface
;
1400 interface
= SCNetworkServiceGetInterface(service
);
1401 while (interface
!= NULL
) {
1402 SCNetworkInterfaceRef childInterface
;
1404 childInterface
= SCNetworkInterfaceGetInterface(interface
);
1405 if (childInterface
== NULL
) {
1409 interface
= childInterface
;
1412 if (interface
!= NULL
) {
1413 CFStringRef interface_name
;
1415 interface_name
= SCNetworkInterfaceGetLocalizedDisplayName(interface
);
1416 if (interface_name
!= NULL
) {
1417 if (CFEqual(name
, interface_name
)) {
1418 // if service name matches the localized interface name
1419 // then store the non-localized name.
1420 interface_name
= __SCNetworkInterfaceGetNonLocalizedDisplayName(interface
);
1421 if (interface_name
!= NULL
) {
1422 CFRelease(saveName
);
1423 saveName
= CFRetain(interface_name
);
1425 } else if (CFStringHasPrefix(name
, interface_name
)) {
1426 CFIndex prefixLen
= CFStringGetLength(interface_name
);
1428 CFIndex suffixLen
= CFStringGetLength(name
);
1430 // if service name matches the localized interface name plus
1431 // a few extra characters) then store the non-localized name with
1433 suffix
= CFStringCreateWithSubstring(NULL
,
1435 CFRangeMake(prefixLen
, suffixLen
- prefixLen
));
1436 interface_name
= __SCNetworkInterfaceGetNonLocalizedDisplayName(interface
);
1437 if (interface_name
!= NULL
) {
1438 CFRelease(saveName
);
1439 saveName
= CFStringCreateWithFormat(NULL
,
1451 #define PREVENT_DUPLICATE_SERVICE_NAMES
1452 #ifdef PREVENT_DUPLICATE_SERVICE_NAMES
1456 // ensure that each service is uniquely named within its sets
1458 sets
= SCNetworkSetCopyAll(servicePrivate
->prefs
);
1463 set_count
= CFArrayGetCount(sets
);
1464 for (set_index
= 0; set_index
< set_count
; set_index
++) {
1465 CFIndex service_index
;
1466 Boolean isDup
= FALSE
;
1467 Boolean isMember
= FALSE
;
1468 CFIndex service_count
;
1469 CFArrayRef services
;
1470 SCNetworkSetRef set
= CFArrayGetValueAtIndex(sets
, set_index
);
1472 services
= SCNetworkSetCopyServices(set
);
1474 service_count
= CFArrayGetCount(services
);
1475 for (service_index
= 0; service_index
< service_count
; service_index
++) {
1476 CFStringRef otherID
;
1477 CFStringRef otherName
;
1478 SCNetworkServiceRef otherService
;
1480 otherService
= CFArrayGetValueAtIndex(services
, service_index
);
1482 otherID
= SCNetworkServiceGetServiceID(otherService
);
1483 if (CFEqual(servicePrivate
->serviceID
, otherID
)) {
1484 // if the service is a member of this set
1489 otherName
= SCNetworkServiceGetName(otherService
);
1490 if ((otherName
!= NULL
) && CFEqual(name
, otherName
)) {
1496 CFRelease(services
);
1498 if (isMember
&& isDup
) {
1500 * if this service is a member of the set and
1501 * the "name" is not unique.
1504 if (saveName
!= NULL
) CFRelease(saveName
);
1505 _SCErrorSet(kSCStatusKeyExists
);
1513 #endif /* PREVENT_DUPLICATE_SERVICE_NAMES */
1515 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1516 servicePrivate
->serviceID
, // service
1518 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
1519 if (isA_CFDictionary(entity
) ||
1520 ((entity
== NULL
) && (name
!= NULL
))) {
1521 CFMutableDictionaryRef newEntity
;
1523 if (entity
!= NULL
) {
1524 newEntity
= CFDictionaryCreateMutableCopy(NULL
, 0, entity
);
1526 newEntity
= CFDictionaryCreateMutable(NULL
,
1528 &kCFTypeDictionaryKeyCallBacks
,
1529 &kCFTypeDictionaryValueCallBacks
);
1531 if (saveName
!= NULL
) {
1532 CFDictionarySetValue(newEntity
, kSCPropUserDefinedName
, saveName
);
1534 CFDictionaryRemoveValue(newEntity
, kSCPropUserDefinedName
);
1536 ok
= SCPreferencesPathSetValue(servicePrivate
->prefs
, path
, newEntity
);
1537 CFRelease(newEntity
);
1540 if (saveName
!= NULL
) CFRelease(saveName
);
1542 if (servicePrivate
->name
!= NULL
) CFRelease(servicePrivate
->name
);
1543 if (name
!= NULL
) CFRetain(name
);
1544 servicePrivate
->name
= name
;
1547 SC_log(LOG_DEBUG
, "SCNetworkServiceSetName(): %@", service
);
1555 #pragma mark SCNetworkService SPIs
1558 SCNetworkServicePrimaryRank
1559 SCNetworkServiceGetPrimaryRank(SCNetworkServiceRef service
)
1561 CFDictionaryRef entity
;
1564 SCNetworkServicePrimaryRank rank
= kSCNetworkServicePrimaryRankDefault
;
1565 CFStringRef rankStr
= NULL
;
1566 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1568 if (!isA_SCNetworkService(service
)) {
1569 _SCErrorSet(kSCStatusInvalidArgument
);
1573 if (servicePrivate
->prefs
!= NULL
) {
1574 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
,
1575 servicePrivate
->serviceID
,
1577 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
1579 if (isA_CFDictionary(entity
)) {
1580 rankStr
= CFDictionaryGetValue(entity
, kSCPropNetServicePrimaryRank
);
1581 ok
= __str_to_rank(rankStr
, &rank
);
1583 } else if (servicePrivate
->store
!= NULL
) {
1584 path
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
1585 kSCDynamicStoreDomainState
,
1586 servicePrivate
->serviceID
,
1588 entity
= SCDynamicStoreCopyValue(servicePrivate
->store
, path
);
1590 if (entity
!= NULL
) {
1591 if (isA_CFDictionary(entity
)) {
1592 rankStr
= CFDictionaryGetValue(entity
, kSCPropNetServicePrimaryRank
);
1593 ok
= __str_to_rank(rankStr
, &rank
);
1598 _SCErrorSet(kSCStatusInvalidArgument
);
1603 rank
= kSCNetworkServicePrimaryRankDefault
;
1604 _SCErrorSet(kSCStatusInvalidArgument
);
1605 } else if (rank
== kSCNetworkServicePrimaryRankDefault
) {
1606 _SCErrorSet(kSCStatusOK
);
1614 SCNetworkServiceSetPrimaryRank(SCNetworkServiceRef service
,
1615 SCNetworkServicePrimaryRank newRank
)
1618 CFDictionaryRef entity
;
1619 CFMutableDictionaryRef newEntity
;
1620 CFStringRef path
= NULL
;
1621 CFStringRef rankStr
= NULL
;
1622 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1624 if (!isA_SCNetworkService(service
)) {
1625 _SCErrorSet(kSCStatusInvalidArgument
);
1629 ok
= __rank_to_str(newRank
, &rankStr
);
1631 _SCErrorSet(kSCStatusInvalidArgument
);
1635 if (servicePrivate
->prefs
!= NULL
) {
1637 case kSCNetworkServicePrimaryRankDefault
:
1638 case kSCNetworkServicePrimaryRankNever
:
1639 case kSCNetworkServicePrimaryRankScoped
:
1640 path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
,
1641 servicePrivate
->serviceID
,
1643 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, path
);
1644 if (entity
!= NULL
) {
1645 if (!isA_CFDictionary(entity
)) {
1647 _SCErrorSet(kSCStatusFailed
);
1650 newEntity
= CFDictionaryCreateMutableCopy(NULL
, 0, entity
);
1652 newEntity
= CFDictionaryCreateMutable(NULL
,
1654 &kCFTypeDictionaryKeyCallBacks
,
1655 &kCFTypeDictionaryValueCallBacks
);
1657 if (rankStr
!= NULL
) {
1658 CFDictionarySetValue(newEntity
, kSCPropNetServicePrimaryRank
, rankStr
);
1660 CFDictionaryRemoveValue(newEntity
, kSCPropNetServicePrimaryRank
);
1662 if (CFDictionaryGetCount(newEntity
) > 0) {
1663 ok
= SCPreferencesPathSetValue(servicePrivate
->prefs
, path
, newEntity
);
1665 ok
= SCPreferencesPathRemoveValue(servicePrivate
->prefs
, path
);
1667 CFRelease(newEntity
);
1673 _SCErrorSet(kSCStatusInvalidArgument
);
1676 } else if (servicePrivate
->store
!= NULL
) {
1677 path
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
1678 kSCDynamicStoreDomainState
,
1679 servicePrivate
->serviceID
,
1681 entity
= SCDynamicStoreCopyValue(servicePrivate
->store
, path
);
1682 if (entity
!= NULL
) {
1683 if (!isA_CFDictionary(entity
)) {
1686 _SCErrorSet(kSCStatusFailed
);
1689 newEntity
= CFDictionaryCreateMutableCopy(NULL
, 0, entity
);
1692 newEntity
= CFDictionaryCreateMutable(NULL
,
1694 &kCFTypeDictionaryKeyCallBacks
,
1695 &kCFTypeDictionaryValueCallBacks
);
1697 if (rankStr
!= NULL
) {
1698 CFDictionarySetValue(newEntity
, kSCPropNetServicePrimaryRank
, rankStr
);
1700 CFDictionaryRemoveValue(newEntity
, kSCPropNetServicePrimaryRank
);
1702 if (CFDictionaryGetCount(newEntity
) > 0) {
1703 ok
= SCDynamicStoreSetValue(servicePrivate
->store
, path
, newEntity
);
1705 ok
= SCDynamicStoreRemoveValue(servicePrivate
->store
, path
);
1707 CFRelease(newEntity
);
1712 _SCErrorSet(kSCStatusInvalidArgument
);
1718 if (path
!= NULL
) CFRelease(path
);
1724 _SCNetworkServiceIsVPN(SCNetworkServiceRef service
)
1726 SCNetworkInterfaceRef interface
;
1727 CFStringRef interfaceType
;
1729 interface
= SCNetworkServiceGetInterface(service
);
1730 if (interface
== NULL
) {
1734 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
1735 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypePPP
)) {
1736 interface
= SCNetworkInterfaceGetInterface(interface
);
1737 if (interface
== NULL
) {
1741 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
1742 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeL2TP
)) {
1745 #pragma GCC diagnostic push
1746 #pragma GCC diagnostic ignored "-Wdeprecated"
1747 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypePPTP
)) {
1750 #pragma GCC diagnostic pop
1751 } else if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeVPN
)) {
1753 } else if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeIPSec
)) {
1762 SCNetworkServiceSetExternalID(SCNetworkServiceRef service
, CFStringRef identifierDomain
, CFStringRef identifier
)
1764 CFStringRef prefs_path
;
1765 CFDictionaryRef service_dictionary
;
1766 SCNetworkServicePrivateRef service_private
= (SCNetworkServicePrivateRef
)service
;
1767 Boolean success
= FALSE
;
1768 CFStringRef prefixed_domain
;
1770 if (!isA_SCNetworkService(service
) || (service_private
->prefs
== NULL
) || !isA_CFString(identifierDomain
)) {
1771 _SCErrorSet(kSCStatusInvalidArgument
);
1775 if (identifier
!= NULL
&& !isA_CFString(identifier
)) {
1776 _SCErrorSet(kSCStatusInvalidArgument
);
1780 prefixed_domain
= CFStringCreateWithFormat(NULL
, 0, CFSTR("%s%@"), EXTERNAL_ID_DOMAIN_PREFIX
, identifierDomain
);
1782 prefs_path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
,
1783 service_private
->serviceID
,
1786 service_dictionary
= SCPreferencesPathGetValue(service_private
->prefs
, prefs_path
);
1787 if (isA_CFDictionary(service_dictionary
) || ((service_dictionary
== NULL
) && (identifier
!= NULL
))) {
1788 CFMutableDictionaryRef new_service_dictionary
;
1790 if (service_dictionary
!= NULL
) {
1791 new_service_dictionary
= CFDictionaryCreateMutableCopy(NULL
, 0, service_dictionary
);
1793 new_service_dictionary
= CFDictionaryCreateMutable(NULL
,
1795 &kCFTypeDictionaryKeyCallBacks
,
1796 &kCFTypeDictionaryValueCallBacks
);
1799 if (identifier
!= NULL
) {
1800 CFDictionarySetValue(new_service_dictionary
, prefixed_domain
, identifier
);
1802 CFDictionaryRemoveValue(new_service_dictionary
, prefixed_domain
);
1804 success
= SCPreferencesPathSetValue(service_private
->prefs
, prefs_path
, new_service_dictionary
);
1805 CFRelease(new_service_dictionary
);
1807 CFRelease(prefs_path
);
1809 if (identifier
!= NULL
) {
1810 if (service_private
->externalIDs
== NULL
) {
1811 service_private
->externalIDs
= CFDictionaryCreateMutable(NULL
,
1813 &kCFTypeDictionaryKeyCallBacks
,
1814 &kCFTypeDictionaryValueCallBacks
);
1816 CFDictionarySetValue(service_private
->externalIDs
, prefixed_domain
, identifier
);
1818 if (service_private
->externalIDs
!= NULL
) {
1819 CFDictionaryRemoveValue(service_private
->externalIDs
, prefixed_domain
);
1823 CFRelease(prefixed_domain
);
1826 _SCErrorSet(kSCStatusFailed
);
1834 SCNetworkServiceCopyExternalID(SCNetworkServiceRef service
, CFStringRef identifierDomain
)
1836 CFStringRef identifier
= NULL
;
1837 CFStringRef prefixed_domain
;
1838 SCNetworkServicePrivateRef service_private
= (SCNetworkServicePrivateRef
)service
;
1840 if (!isA_SCNetworkService(service
) || (service_private
->prefs
== NULL
) || !isA_CFString(identifierDomain
)) {
1841 _SCErrorSet(kSCStatusInvalidArgument
);
1845 prefixed_domain
= CFStringCreateWithFormat(NULL
, 0, CFSTR("%s%@"), EXTERNAL_ID_DOMAIN_PREFIX
, identifierDomain
);
1847 if (service_private
->externalIDs
!= NULL
) {
1848 identifier
= CFDictionaryGetValue(service_private
->externalIDs
, prefixed_domain
);
1849 if (identifier
!= NULL
) {
1850 CFRetain(identifier
);
1854 if (identifier
== NULL
) {
1855 CFStringRef prefs_path
;
1856 CFDictionaryRef service_dictionary
;
1858 prefs_path
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
,
1859 service_private
->serviceID
,
1862 service_dictionary
= SCPreferencesPathGetValue(service_private
->prefs
, prefs_path
);
1863 if (isA_CFDictionary(service_dictionary
)) {
1864 identifier
= CFDictionaryGetValue(service_dictionary
, prefixed_domain
);
1865 if (identifier
!= NULL
) {
1866 CFRetain(identifier
);
1867 if (service_private
->externalIDs
== NULL
) {
1868 service_private
->externalIDs
= CFDictionaryCreateMutable(NULL
,
1870 &kCFTypeDictionaryKeyCallBacks
,
1871 &kCFTypeDictionaryValueCallBacks
);
1873 CFDictionarySetValue(service_private
->externalIDs
, prefixed_domain
, identifier
);
1876 CFRelease(prefs_path
);
1879 CFRelease(prefixed_domain
);
1881 if (identifier
== NULL
) {
1882 _SCErrorSet(kSCStatusNoKey
);
1890 CFStringRef oldServiceID
;
1891 CFStringRef newServiceID
;
1892 } serviceContext
, *serviceContextRef
;
1896 replaceServiceID(const void *value
, void *context
)
1898 CFStringRef link
= NULL
;
1899 CFStringRef oldLink
;
1900 CFMutableArrayRef newServiceOrder
;
1902 serviceContextRef service_context
= (serviceContextRef
)context
;
1903 CFArrayRef serviceOrder
= NULL
;
1904 SCNetworkSetRef set
= (SCNetworkSetRef
)value
;
1905 SCNetworkSetPrivateRef setPrivate
= (SCNetworkSetPrivateRef
)set
;
1907 // update service order
1908 serviceOrder
= SCNetworkSetGetServiceOrder(set
);
1909 if ((isA_CFArray(serviceOrder
) != NULL
) &&
1910 CFArrayContainsValue(serviceOrder
,
1911 CFRangeMake(0, CFArrayGetCount(serviceOrder
)),
1912 service_context
->oldServiceID
)) {
1914 CFIndex serviceOrderIndex
;
1916 // replacing all instances of old service ID with new one
1917 newServiceOrder
= CFArrayCreateMutableCopy(NULL
, 0, serviceOrder
);
1918 count
= CFArrayGetCount(newServiceOrder
);
1919 for (serviceOrderIndex
= 0; serviceOrderIndex
< count
; serviceOrderIndex
++) {
1920 CFStringRef serviceID
;
1922 serviceID
= CFArrayGetValueAtIndex(newServiceOrder
, serviceOrderIndex
);
1923 if (CFEqual(serviceID
, service_context
->oldServiceID
)) {
1924 CFArraySetValueAtIndex(newServiceOrder
, serviceOrderIndex
, service_context
->newServiceID
);
1927 SCNetworkSetSetServiceOrder(set
, newServiceOrder
);
1928 CFRelease(newServiceOrder
);
1931 // check if service with old serviceID is part of the set
1932 path
= SCPreferencesPathKeyCreateSetNetworkServiceEntity(NULL
, // allocator
1933 setPrivate
->setID
, // set
1934 service_context
->oldServiceID
, // service
1936 oldLink
= SCPreferencesPathGetLink(setPrivate
->prefs
, path
);
1937 if (oldLink
== NULL
) {
1938 // don't make any changes if service with old serviceID is not found
1942 // remove link between "set" and old "service"
1943 SCPreferencesPathRemoveValue(setPrivate
->prefs
, path
);
1946 // create the link between "set" and the "service"
1947 path
= SCPreferencesPathKeyCreateSetNetworkServiceEntity(NULL
, // allocator
1948 setPrivate
->setID
, // set
1949 service_context
->newServiceID
, // service
1951 link
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1952 service_context
->newServiceID
, // service
1954 SCPreferencesPathSetLink(setPrivate
->prefs
, path
, link
);
1970 _SCNetworkServiceSetServiceID(SCNetworkServiceRef service
, CFStringRef newServiceID
)
1972 CFArrayRef allSets
= NULL
;
1973 CFDictionaryRef entity
;
1974 CFStringRef newPath
;
1976 CFStringRef oldPath
= NULL
;
1977 serviceContext service_context
;
1978 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
1980 if (!isA_SCNetworkService(service
) || (servicePrivate
->prefs
== NULL
)) {
1981 _SCErrorSet(kSCStatusInvalidArgument
);
1985 if (!isA_CFString(newServiceID
)) {
1986 _SCErrorSet(kSCStatusInvalidArgument
);
1990 if (CFEqual(newServiceID
, servicePrivate
->serviceID
)) {
1991 // no work needs to be done if new service ID is equal to current service ID
1995 newPath
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
1996 newServiceID
, // service
1998 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, newPath
);
1999 if (isA_CFDictionary(entity
)) {
2000 // if the new service already exists
2001 _SCErrorSet(kSCStatusKeyExists
);
2005 oldPath
= SCPreferencesPathKeyCreateNetworkServiceEntity(NULL
, // allocator
2006 servicePrivate
->serviceID
, // service
2008 entity
= SCPreferencesPathGetValue(servicePrivate
->prefs
, oldPath
);
2009 if (!isA_CFDictionary(entity
)) {
2010 // if the service has already been removed
2011 _SCErrorSet(kSCStatusNoKey
);
2015 ok
= SCPreferencesPathSetValue(servicePrivate
->prefs
, newPath
, entity
);
2018 ok
= SCPreferencesPathRemoveValue(servicePrivate
->prefs
, oldPath
);
2021 allSets
= SCNetworkSetCopyAll(servicePrivate
->prefs
);
2023 service_context
.newServiceID
= newServiceID
;
2024 service_context
.oldServiceID
= servicePrivate
->serviceID
;
2026 // find all sets w/oldServiceID and update
2027 // ... and update the serviceOrder
2028 CFArrayApplyFunction(allSets
,
2029 CFRangeMake(0, CFArrayGetCount(allSets
)),
2033 if (servicePrivate
->interface
!= NULL
) {
2034 SCNetworkInterfaceRef newInterface
;
2036 // duplicate the interface and associate the copy with the new service ID
2037 newInterface
= (SCNetworkInterfaceRef
)__SCNetworkInterfaceCreateCopy(NULL
,
2038 servicePrivate
->interface
,
2039 servicePrivate
->prefs
,
2041 CFRelease(servicePrivate
->interface
);
2042 servicePrivate
->interface
= newInterface
;
2045 SC_log(LOG_DEBUG
, "_SCNetworkServiceSetServiceID(): %@ --> %@", service
, newServiceID
);
2047 // replace serviceID with new one
2048 CFRetain(newServiceID
);
2049 CFRelease(servicePrivate
->serviceID
);
2050 servicePrivate
->serviceID
= newServiceID
;
2054 if (oldPath
!= NULL
) {
2057 if (newPath
!= NULL
) {
2060 if (allSets
!= NULL
) {
2067 #define kVPNProtocolPayloadInfo CFSTR("com.apple.payload")
2068 #define kSCEntNetLoginWindowEAPOL CFSTR("EAPOL.LoginWindow")
2071 copyInterfaceConfiguration(SCNetworkServiceRef oldService
, SCNetworkServiceRef newService
)
2073 SCNetworkInterfaceRef oldInterface
;
2074 SCNetworkInterfaceRef newInterface
;
2076 oldInterface
= SCNetworkServiceGetInterface(oldService
);
2077 newInterface
= SCNetworkServiceGetInterface(newService
);
2079 while (oldInterface
!= NULL
) {
2080 CFDictionaryRef configuration
;
2081 CFStringRef interfaceType
;
2083 if (newInterface
== NULL
) {
2084 // oops ... interface layering does not match
2088 // copy interface configuration
2089 configuration
= SCNetworkInterfaceGetConfiguration(oldInterface
);
2091 if ((configuration
!= NULL
) ||
2092 (SCError() == kSCStatusOK
)) {
2093 if (!SCNetworkInterfaceSetConfiguration(newInterface
, configuration
)) {
2094 SC_log(LOG_INFO
, "problem setting interface configuration");
2099 // special case: PPP/L2TP + IPSec
2100 interfaceType
= SCNetworkInterfaceGetInterfaceType(oldInterface
);
2101 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypePPP
)) {
2102 SCNetworkInterfaceRef childInterface
;
2104 childInterface
= SCNetworkInterfaceGetInterface(oldInterface
);
2105 if (childInterface
!= NULL
) {
2106 CFStringRef childInterfaceType
;
2108 childInterfaceType
= SCNetworkInterfaceGetInterfaceType(childInterface
);
2110 if (CFEqual(childInterfaceType
, kSCNetworkInterfaceTypeL2TP
)) {
2111 configuration
= SCNetworkInterfaceGetExtendedConfiguration(oldInterface
, kSCEntNetIPSec
);
2112 if ((configuration
!= NULL
) ||
2113 (SCError() == kSCStatusOK
)) {
2114 if (!SCNetworkInterfaceSetExtendedConfiguration(newInterface
, kSCEntNetIPSec
, configuration
)) {
2115 SC_log(LOG_INFO
, "problem setting child interface configuration");
2122 // special case: 802.1x
2123 configuration
= SCNetworkInterfaceGetExtendedConfiguration(oldInterface
, kSCEntNetEAPOL
);
2124 if ((configuration
!= NULL
) ||
2125 (SCError() == kSCStatusOK
)) {
2126 (void) SCNetworkInterfaceSetExtendedConfiguration(newInterface
, kSCEntNetEAPOL
, configuration
);
2129 // special case: Managed Client
2130 configuration
= SCNetworkInterfaceGetExtendedConfiguration(oldInterface
, kVPNProtocolPayloadInfo
);
2131 if ((configuration
!= NULL
) ||
2132 (SCError() == kSCStatusOK
)) {
2133 (void) SCNetworkInterfaceSetExtendedConfiguration(newInterface
, kVPNProtocolPayloadInfo
, configuration
);
2136 // special case: Network Pref
2137 configuration
= SCNetworkInterfaceGetExtendedConfiguration(oldInterface
, kSCValNetPPPAuthProtocolEAP
);
2138 if ((configuration
!= NULL
) ||
2139 (SCError() == kSCStatusOK
)) {
2140 (void) SCNetworkInterfaceSetExtendedConfiguration(newInterface
, kSCValNetPPPAuthProtocolEAP
, configuration
);
2143 // special case: Remote Pref
2144 configuration
= SCNetworkInterfaceGetExtendedConfiguration(oldInterface
, kSCEntNetLoginWindowEAPOL
);
2145 if ((configuration
!= NULL
) ||
2146 (SCError() == kSCStatusOK
)) {
2147 (void) SCNetworkInterfaceSetExtendedConfiguration(newInterface
, kSCEntNetLoginWindowEAPOL
, configuration
);
2150 // special case: Network Extension
2151 configuration
= SCNetworkInterfaceGetExtendedConfiguration(oldInterface
, kSCNetworkInterfaceTypeIPSec
);
2152 if ((configuration
!= NULL
) ||
2153 (SCError() == kSCStatusOK
)) {
2154 (void) SCNetworkInterfaceSetExtendedConfiguration(newInterface
, kSCNetworkInterfaceTypeIPSec
, configuration
);
2157 oldInterface
= SCNetworkInterfaceGetInterface(oldInterface
);
2158 newInterface
= SCNetworkInterfaceGetInterface(newInterface
);
2166 __SCNetworkServiceAddProtocolToService(SCNetworkServiceRef service
, CFStringRef protocolType
, CFDictionaryRef configuration
, Boolean enabled
)
2169 SCNetworkProtocolRef protocol
;
2171 protocol
= SCNetworkServiceCopyProtocol(service
, protocolType
);
2173 if ((protocol
== NULL
) &&
2174 (SCError() == kSCStatusNoKey
)) {
2175 ok
= SCNetworkServiceAddProtocolType(service
, protocolType
);
2177 protocol
= SCNetworkServiceCopyProtocol(service
, protocolType
);
2180 if (protocol
!= NULL
) {
2181 SCNetworkProtocolSetConfiguration(protocol
, configuration
);
2182 SCNetworkProtocolSetEnabled(protocol
, enabled
);
2183 CFRelease(protocol
);
2192 __SCNetworkServiceMigrateNew(SCPreferencesRef prefs
,
2193 SCNetworkServiceRef service
,
2194 CFDictionaryRef bsdMapping
,
2195 CFDictionaryRef setMapping
,
2196 CFDictionaryRef serviceSetMapping
)
2198 CFStringRef deviceName
= NULL
;
2200 SCNetworkInterfaceRef interface
= NULL
;
2201 CFDictionaryRef interfaceEntity
= NULL
;
2202 CFMutableDictionaryRef interfaceEntityMutable
= NULL
;
2203 SCNetworkSetRef newSet
= NULL
;
2204 SCPreferencesRef ni_prefs
= NULL
;
2205 SCNetworkInterfaceRef ni_interface
= NULL
;
2206 SCNetworkInterfaceRef oldInterface
= NULL
;
2207 SCNetworkSetRef oldSet
= NULL
;
2208 SCNetworkServiceRef newService
= NULL
;
2209 CFStringRef serviceID
= NULL
;
2210 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
) service
;
2211 CFArrayRef setList
= NULL
;
2212 Boolean success
= FALSE
;
2213 CFStringRef targetDeviceName
= NULL
;
2214 CFStringRef userDefinedName
= NULL
;
2215 CFStringRef userDefinedNameInterface
= NULL
;
2216 CFArrayRef protocols
= NULL
;
2217 CFStringRef subType
;
2219 if ((isA_SCNetworkService(service
) == NULL
) ||
2220 (isA_SCNetworkInterface(servicePrivate
->interface
) == NULL
) ||
2221 (servicePrivate
->prefs
== NULL
)) {
2224 serviceID
= servicePrivate
->serviceID
;
2226 newService
= SCNetworkServiceCopy(prefs
, serviceID
);
2227 if (newService
!= NULL
) {
2228 // Cannot add service if it already exists
2229 SC_log(LOG_INFO
, "Service already exists");
2233 oldInterface
= SCNetworkServiceGetInterface(service
);
2234 interfaceEntity
= __SCNetworkInterfaceCopyInterfaceEntity(oldInterface
);
2235 if (interfaceEntity
== NULL
) {
2236 SC_log(LOG_INFO
, "No interface entity");
2239 interfaceEntityMutable
= CFDictionaryCreateMutableCopy(NULL
, 0, interfaceEntity
);
2241 if (isA_CFDictionary(bsdMapping
) != NULL
) {
2242 deviceName
= CFDictionaryGetValue(interfaceEntityMutable
, kSCPropNetInterfaceDeviceName
);
2243 if (isA_CFString(deviceName
) != NULL
) {
2244 targetDeviceName
= CFDictionaryGetValue(bsdMapping
, deviceName
);
2245 if (targetDeviceName
!= NULL
) {
2247 CFDictionarySetValue(interfaceEntityMutable
, kSCPropNetInterfaceDeviceName
, targetDeviceName
);
2248 ni_prefs
= __SCPreferencesCreateNIPrefsFromPrefs(prefs
);
2249 ni_interface
= __SCNetworkInterfaceCreateWithNIPreferencesUsingBSDName(NULL
, ni_prefs
, targetDeviceName
);
2250 if (ni_interface
!= NULL
) {
2251 userDefinedNameInterface
= __SCNetworkInterfaceGetUserDefinedName(ni_interface
);
2255 if (userDefinedNameInterface
== NULL
) {
2256 userDefinedNameInterface
= CFDictionaryGetValue(interfaceEntityMutable
, kSCPropUserDefinedName
);
2259 subType
= CFDictionaryGetValue(interfaceEntityMutable
, kSCPropNetInterfaceSubType
);
2260 interface
= _SCNetworkInterfaceCreateWithEntity(NULL
, interfaceEntityMutable
, NULL
);
2261 if (userDefinedNameInterface
!= NULL
) {
2262 __SCNetworkInterfaceSetUserDefinedName(interface
, userDefinedNameInterface
);
2264 // Supporting PPPoE subtype
2265 if (subType
!= NULL
&&
2266 CFEqual(subType
, kSCValNetInterfaceSubTypePPPoE
)) {
2267 SCNetworkInterfaceRef childInterface
= SCNetworkInterfaceGetInterface(interface
);
2268 if (childInterface
!= NULL
) {
2269 __SCNetworkInterfaceSetUserDefinedName(childInterface
, userDefinedNameInterface
);
2272 newService
= SCNetworkServiceCreate(prefs
, interface
);
2273 if (newService
== NULL
) {
2274 SC_log(LOG_INFO
, "SCNetworkServiceCreate() failed");
2278 enabled
= SCNetworkServiceGetEnabled(service
);
2279 if (!SCNetworkServiceSetEnabled(newService
, enabled
)) {
2280 SCNetworkServiceRemove(newService
);
2281 SC_log(LOG_INFO
, "SCNetworkServiceSetEnabled() failed");
2285 if (!SCNetworkServiceEstablishDefaultConfiguration(newService
)) {
2286 SCNetworkServiceRemove(newService
);
2287 SC_log(LOG_INFO
, "SCNetworkServiceEstablishDefaultConfiguration() failed");
2292 _SCNetworkServiceSetServiceID(newService
, serviceID
);
2294 userDefinedName
= SCNetworkServiceGetName(service
);
2295 if ((userDefinedName
!= NULL
) &&
2296 !SCNetworkServiceSetName(newService
, userDefinedName
)) {
2297 SC_log(LOG_INFO
, "SCNetworkServiceSetName(, %@) failed", userDefinedName
);
2300 // Determine which sets to add service
2301 if (setMapping
!= NULL
&&
2302 serviceSetMapping
!= NULL
) {
2303 setList
= CFDictionaryGetValue(serviceSetMapping
, service
);
2304 if (setList
!= NULL
) {
2305 for (CFIndex idx
= 0; idx
< CFArrayGetCount(setList
); idx
++) {
2306 oldSet
= CFArrayGetValueAtIndex(setList
, idx
);
2307 newSet
= CFDictionaryGetValue(setMapping
, oldSet
);
2308 if (newSet
== NULL
) {
2312 if (!SCNetworkSetAddService(newSet
, newService
)) {
2313 SC_log(LOG_INFO
, "SCNetworkSetAddService() failed");
2319 protocols
= SCNetworkServiceCopyProtocols(service
);
2320 if (protocols
!= NULL
) {
2322 for (CFIndex idx
= 0; idx
< CFArrayGetCount(protocols
); idx
++) {
2323 SCNetworkProtocolRef protocol
= CFArrayGetValueAtIndex(protocols
, idx
);
2324 CFDictionaryRef configuration
= SCNetworkProtocolGetConfiguration(protocol
);
2325 CFStringRef protocolType
= SCNetworkProtocolGetProtocolType(protocol
);
2326 enabled
= SCNetworkProtocolGetEnabled(protocol
);
2327 __SCNetworkServiceAddProtocolToService(newService
, protocolType
, configuration
, enabled
);
2329 CFRelease(protocols
);
2332 copyInterfaceConfiguration(service
, newService
);
2336 if (interface
!= NULL
) {
2337 CFRelease(interface
);
2339 if (interfaceEntity
!= NULL
) {
2340 CFRelease(interfaceEntity
);
2342 if (interfaceEntityMutable
!= NULL
) {
2343 CFRelease(interfaceEntityMutable
);
2345 if (newService
!= NULL
) {
2346 CFRelease(newService
);
2348 if (ni_prefs
!= NULL
) {
2349 CFRelease(ni_prefs
);
2351 if (ni_interface
!= NULL
) {
2352 CFRelease(ni_interface
);
2360 __SCNetworkServiceCreate(SCPreferencesRef prefs
,
2361 SCNetworkInterfaceRef interface
,
2362 CFStringRef userDefinedName
)
2364 SCNetworkSetRef currentSet
= NULL
;
2366 SCNetworkServiceRef service
= NULL
;
2368 if (interface
== NULL
) {
2372 if (userDefinedName
== NULL
) {
2373 userDefinedName
= __SCNetworkInterfaceGetUserDefinedName(interface
);
2374 if (userDefinedName
== NULL
) {
2375 SC_log(LOG_INFO
, "No userDefinedName");
2379 service
= SCNetworkServiceCreate(prefs
, interface
);
2380 if (service
== NULL
) {
2381 SC_log(LOG_INFO
, "SCNetworkServiceCreate() failed: %s", SCErrorString(SCError()));
2383 ok
= SCNetworkServiceSetName(service
, userDefinedName
);
2385 SC_log(LOG_INFO
, "SCNetworkServiceSetName() failed: %s", SCErrorString(SCError()));
2386 SCNetworkServiceRemove(service
);
2390 ok
= SCNetworkServiceEstablishDefaultConfiguration(service
);
2392 SC_log(LOG_INFO
, "SCNetworkServiceEstablishDefaultConfiguration() failed: %s", SCErrorString(SCError()));
2393 SCNetworkServiceRemove(service
);
2397 currentSet
= SCNetworkSetCopyCurrent(prefs
);
2398 if (currentSet
== NULL
) {
2399 SC_log(LOG_INFO
, "No current set");
2400 if (service
!= NULL
) {
2401 SCNetworkServiceRemove(service
);
2405 if (service
!= NULL
) {
2406 ok
= SCNetworkSetAddService(currentSet
, service
);
2408 SC_log(LOG_INFO
, "Could not add service to the current set");
2409 SCNetworkServiceRemove(service
);
2415 if (service
!= NULL
) {
2418 if (currentSet
!= NULL
) {
2419 CFRelease(currentSet
);
2424 __private_extern__ Boolean
2425 __SCNetworkServiceIsPPTP(SCNetworkServiceRef service
)
2427 CFStringRef intfSubtype
;
2428 SCNetworkServicePrivateRef servicePrivate
= (SCNetworkServicePrivateRef
)service
;
2430 if (servicePrivate
== NULL
|| servicePrivate
->interface
== NULL
) {
2434 intfSubtype
= __SCNetworkInterfaceGetEntitySubType(servicePrivate
->interface
);
2435 if (intfSubtype
== NULL
) {
2439 #pragma GCC diagnostic push
2440 #pragma GCC diagnostic ignored "-Wdeprecated"
2441 if (CFEqual(intfSubtype
, kSCValNetInterfaceSubTypePPTP
)) {
2444 #pragma GCC diagnostic pop