2 * Copyright (c) 2011, 2013 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 * January 3, 2011 Allan Nathanson <ajn@apple.com>
31 #include <TargetConditionals.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <CoreFoundation/CoreFoundation.h>
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include <SystemConfiguration/SCValidation.h>
42 #define DEFAULT_MATCH_ORDER 200000 /* match order for the "default" proxy configuration */
45 #define PROXY_MATCH_ORDER_KEY CFSTR("__MATCH_ORDER__")
46 #define ORDER_KEY CFSTR("__ORDER__")
49 CFBooleanRef G_supplemental_proxies_follow_dns
= NULL
;
53 add_proxy(CFMutableArrayRef proxies
, CFMutableDictionaryRef proxy
)
59 n_proxies
= CFArrayGetCount(proxies
);
60 for (i
= 0; i
< n_proxies
; i
++) {
61 CFDictionaryRef match_proxy
;
63 match_proxy
= CFArrayGetValueAtIndex(proxies
, i
);
64 if (CFEqual(proxy
, match_proxy
)) {
70 order
= CFNumberCreate(NULL
, kCFNumberCFIndexType
, &n_proxies
);
71 CFDictionarySetValue(proxy
, ORDER_KEY
, order
);
74 CFArrayAppendValue(proxies
, proxy
);
80 add_supplemental(CFMutableArrayRef proxies
, CFDictionaryRef proxy
, uint32_t defaultOrder
)
87 domains
= CFDictionaryGetValue(proxy
, kSCPropNetProxiesSupplementalMatchDomains
);
88 n_domains
= isA_CFArray(domains
) ? CFArrayGetCount(domains
) : 0;
93 orders
= CFDictionaryGetValue(proxy
, kSCPropNetProxiesSupplementalMatchOrders
);
95 if (!isA_CFArray(orders
) || (n_domains
!= CFArrayGetCount(orders
))) {
101 * yes, this is a "supplemental" proxy configuration, expand
102 * the match domains and add each to the proxies list.
104 for (i
= 0; i
< n_domains
; i
++) {
105 CFStringRef match_domain
;
106 CFNumberRef match_order
;
107 CFMutableDictionaryRef match_proxy
;
109 match_domain
= CFArrayGetValueAtIndex(domains
, i
);
110 if (!isA_CFString(match_domain
)) {
114 match_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
116 // set supplemental proxy match "domain"
117 match_domain
= _SC_trimDomain(match_domain
);
118 if (match_domain
!= NULL
) {
119 CFDictionarySetValue(match_proxy
, kSCPropNetProxiesSupplementalMatchDomain
, match_domain
);
120 CFRelease(match_domain
);
122 CFDictionaryRemoveValue(match_proxy
, kSCPropNetProxiesSupplementalMatchDomain
);
125 // set supplemental proxy match "order"
126 match_order
= (orders
!= NULL
) ? CFArrayGetValueAtIndex(orders
, i
) : NULL
;
127 if (isA_CFNumber(match_order
)) {
128 CFDictionarySetValue(match_proxy
, PROXY_MATCH_ORDER_KEY
, match_order
);
132 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &defaultOrder
);
133 CFDictionarySetValue(match_proxy
, PROXY_MATCH_ORDER_KEY
, num
);
136 defaultOrder
++; // if multiple domains, maintain ordering
139 // remove keys we don't want in a supplemental proxy
140 CFDictionaryRemoveValue(match_proxy
, kSCPropNetProxiesSupplementalMatchDomains
);
141 CFDictionaryRemoveValue(match_proxy
, kSCPropNetProxiesSupplementalMatchOrders
);
142 CFDictionaryRemoveValue(match_proxy
, kSCPropInterfaceName
);
144 add_proxy(proxies
, match_proxy
);
145 CFRelease(match_proxy
);
156 add_supplemental_proxies(CFMutableArrayRef proxies
, CFDictionaryRef services
, CFArrayRef service_order
)
158 const void * keys_q
[N_QUICK
];
159 const void ** keys
= keys_q
;
163 const void * vals_q
[N_QUICK
];
164 const void ** vals
= vals_q
;
166 n_services
= isA_CFDictionary(services
) ? CFDictionaryGetCount(services
) : 0;
167 if (n_services
== 0) {
168 return; // if no services
171 if (n_services
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
172 keys
= CFAllocatorAllocate(NULL
, n_services
* sizeof(CFTypeRef
), 0);
173 vals
= CFAllocatorAllocate(NULL
, n_services
* sizeof(CFTypeRef
), 0);
176 n_order
= isA_CFArray(service_order
) ? CFArrayGetCount(service_order
) : 0;
178 CFDictionaryGetKeysAndValues(services
, keys
, vals
);
179 for (i
= 0; i
< n_services
; i
++) {
180 uint32_t defaultOrder
;
181 CFDictionaryRef proxy
;
182 CFMutableDictionaryRef proxyWithDNS
= NULL
;
183 CFDictionaryRef service
= (CFDictionaryRef
)vals
[i
];
185 if (!isA_CFDictionary(service
)) {
189 proxy
= CFDictionaryGetValue(service
, kSCEntNetProxies
);
190 if (!isA_CFDictionary(proxy
)) {
194 if ((G_supplemental_proxies_follow_dns
!= NULL
) && CFBooleanGetValue(G_supplemental_proxies_follow_dns
)) {
196 CFArrayRef matchDomains
;
197 CFArrayRef matchOrders
;
199 if (!CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomains
) &&
200 CFDictionaryGetValueIfPresent(service
, kSCEntNetDNS
, (const void **)&dns
) &&
201 isA_CFDictionary(dns
) &&
202 CFDictionaryGetValueIfPresent(dns
, kSCPropNetDNSSupplementalMatchDomains
, (const void **)&matchDomains
) &&
203 isA_CFArray(matchDomains
)) {
204 proxyWithDNS
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
205 CFDictionarySetValue(proxyWithDNS
, kSCPropNetProxiesSupplementalMatchDomains
, matchDomains
);
206 if (CFDictionaryGetValueIfPresent(dns
, kSCPropNetDNSSupplementalMatchOrders
, (const void **)&matchOrders
) &&
207 isA_CFArray(matchOrders
)) {
208 CFDictionarySetValue(proxyWithDNS
, kSCPropNetProxiesSupplementalMatchOrders
, matchOrders
);
210 CFDictionaryRemoveValue(proxyWithDNS
, kSCPropNetProxiesSupplementalMatchOrders
);
212 proxy
= proxyWithDNS
;
216 defaultOrder
= DEFAULT_MATCH_ORDER
217 - (DEFAULT_MATCH_ORDER
/ 2)
218 + ((DEFAULT_MATCH_ORDER
/ 1000) * i
);
220 !CFArrayContainsValue(service_order
, CFRangeMake(0, n_order
), keys
[i
])) {
221 // push out services not specified in service order
222 defaultOrder
+= (DEFAULT_MATCH_ORDER
/ 1000) * n_services
;
225 add_supplemental(proxies
, proxy
, defaultOrder
);
226 if (proxyWithDNS
!= NULL
) CFRelease(proxyWithDNS
);
229 if (keys
!= keys_q
) {
230 CFAllocatorDeallocate(NULL
, keys
);
231 CFAllocatorDeallocate(NULL
, vals
);
238 static CFComparisonResult
239 compareBySearchOrder(const void *val1
, const void *val2
, void *context
)
241 CFDictionaryRef proxy1
= (CFDictionaryRef
)val1
;
242 CFDictionaryRef proxy2
= (CFDictionaryRef
)val2
;
245 uint32_t order1
= DEFAULT_MATCH_ORDER
;
246 uint32_t order2
= DEFAULT_MATCH_ORDER
;
248 num1
= CFDictionaryGetValue(proxy1
, PROXY_MATCH_ORDER_KEY
);
249 if (!isA_CFNumber(num1
) ||
250 !CFNumberGetValue(num1
, kCFNumberIntType
, &order1
)) {
251 order1
= DEFAULT_MATCH_ORDER
;
254 num2
= CFDictionaryGetValue(proxy2
, PROXY_MATCH_ORDER_KEY
);
255 if (!isA_CFNumber(num2
) ||
256 !CFNumberGetValue(num2
, kCFNumberIntType
, &order2
)) {
257 order2
= DEFAULT_MATCH_ORDER
;
260 if (order1
== order2
) {
261 // if same match "order", retain original ordering for configurations
262 if (CFDictionaryGetValueIfPresent(proxy1
, ORDER_KEY
, (const void **)&num1
) &&
263 CFDictionaryGetValueIfPresent(proxy2
, ORDER_KEY
, (const void **)&num2
) &&
264 isA_CFNumber(num1
) &&
265 isA_CFNumber(num2
) &&
266 CFNumberGetValue(num1
, kCFNumberIntType
, &order1
) &&
267 CFNumberGetValue(num2
, kCFNumberIntType
, &order2
)) {
268 if (order1
== order2
) {
269 return kCFCompareEqualTo
;
271 return (order1
< order2
) ? kCFCompareLessThan
: kCFCompareGreaterThan
;
275 return kCFCompareEqualTo
;
278 return (order1
< order2
) ? kCFCompareLessThan
: kCFCompareGreaterThan
;
282 static __inline__ Boolean
283 isSupplementalProxy(CFDictionaryRef proxy
)
285 if ((proxy
!= NULL
) &&
286 CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
295 copy_supplemental_proxies(CFArrayRef proxies
, Boolean skip
)
299 CFMutableArrayRef supplemental
= NULL
;
301 // iterate over services
303 n_proxies
= isA_CFArray(proxies
) ? CFArrayGetCount(proxies
) : 0;
304 for (i
= 0; i
< n_proxies
; i
++) {
305 CFDictionaryRef proxy
;
307 proxy
= CFArrayGetValueAtIndex(proxies
, i
);
308 if (!isSupplementalProxy(proxy
)) {
309 // if not supplemental proxy (i.e. no match domain)
313 // add [supplemental] proxy entry
314 if (supplemental
== NULL
) {
315 supplemental
= CFArrayCreateMutable(NULL
,
317 &kCFTypeArrayCallBacks
);
319 CFArrayAppendValue(supplemental
, proxy
);
327 service_order_copy_all(CFDictionaryRef services
, CFArrayRef service_order
)
329 const void * keys_q
[N_QUICK
];
330 const void ** keys
= keys_q
;
334 CFMutableArrayRef order
;
336 // ensure that we process all services in order
337 n_services
= isA_CFDictionary(services
) ? CFDictionaryGetCount(services
) : 0;
338 if (n_services
== 0) {
343 // ensure that we process all services in order
345 n_order
= isA_CFArray(service_order
) ? CFArrayGetCount(service_order
) : 0;
347 order
= CFArrayCreateMutableCopy(NULL
, 0, service_order
);
349 order
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
352 if (n_services
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
353 keys
= CFAllocatorAllocate(NULL
, n_services
* sizeof(CFTypeRef
), 0);
355 CFDictionaryGetKeysAndValues(services
, keys
, NULL
);
356 for (i
= 0; i
< n_services
; i
++) {
357 CFStringRef serviceID
= (CFStringRef
)keys
[i
];
359 if (!CFArrayContainsValue(order
, CFRangeMake(0, n_order
), serviceID
)) {
360 CFArrayAppendValue(order
, serviceID
);
364 if (keys
!= keys_q
) {
365 CFAllocatorDeallocate(NULL
, keys
);
372 static CFDictionaryRef
373 copy_app_layer_vpn_proxies(CFDictionaryRef services
, CFArrayRef order
, CFDictionaryRef services_info
)
375 CFMutableDictionaryRef app_layer_proxies
= NULL
;
379 if (!isA_CFDictionary(services_info
)) {
383 // iterate over services
385 n_order
= isA_CFArray(order
) ? CFArrayGetCount(order
) : 0;
386 for (i
= 0; i
< n_order
; i
++) {
387 CFMutableDictionaryRef newProxy
;
388 CFDictionaryRef proxy
;
389 CFDictionaryRef service
;
390 CFStringRef serviceID
;
394 serviceID
= CFArrayGetValueAtIndex(order
, i
);
395 service
= CFDictionaryGetValue(services
, serviceID
);
396 if (!isA_CFDictionary(service
)) {
401 proxy
= CFDictionaryGetValue(service
, kSCEntNetProxies
);
402 if (!isA_CFDictionary(proxy
)) {
407 vpn_key
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
408 kSCDynamicStoreDomainSetup
,
411 vpn
= CFDictionaryGetValue(services_info
, vpn_key
);
414 if (!isA_CFDictionary(vpn
) || !CFDictionaryContainsKey(vpn
, kSCPropNetVPNAppRules
)) {
415 // if not app-layer vpn
419 if ((app_layer_proxies
!= NULL
) &&
420 CFDictionaryContainsKey(app_layer_proxies
, serviceID
)) {
421 // if we've already processed this [app_layer_proxies] interface
425 // add [app_layer_proxies] proxy entry
426 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
427 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchDomains
);
428 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchOrders
);
429 if (app_layer_proxies
== NULL
) {
430 app_layer_proxies
= CFDictionaryCreateMutable(NULL
,
432 &kCFTypeDictionaryKeyCallBacks
,
433 &kCFTypeDictionaryValueCallBacks
);
435 CFDictionarySetValue(app_layer_proxies
, serviceID
, newProxy
);
439 return app_layer_proxies
;
443 static CFDictionaryRef
444 copy_scoped_proxies(CFDictionaryRef services
, CFArrayRef order
)
448 CFMutableDictionaryRef scoped
= NULL
;
450 // iterate over services
452 n_order
= isA_CFArray(order
) ? CFArrayGetCount(order
) : 0;
453 for (i
= 0; i
< n_order
; i
++) {
454 char if_name
[IF_NAMESIZE
];
455 CFStringRef interface
;
456 CFMutableDictionaryRef newProxy
;
457 CFDictionaryRef proxy
;
458 CFDictionaryRef service
;
459 CFStringRef serviceID
;
461 serviceID
= CFArrayGetValueAtIndex(order
, i
);
462 service
= CFDictionaryGetValue(services
, serviceID
);
463 if (!isA_CFDictionary(service
)) {
468 proxy
= CFDictionaryGetValue(service
, kSCEntNetProxies
);
469 if (!isA_CFDictionary(proxy
)) {
474 interface
= CFDictionaryGetValue(proxy
, kSCPropInterfaceName
);
475 if (interface
== NULL
) {
476 // if no [scoped] interface
479 if ((scoped
!= NULL
) &&
480 CFDictionaryContainsKey(scoped
, interface
)) {
481 // if we've already processed this [scoped] interface
485 if ((_SC_cfstring_to_cstring(interface
,
488 kCFStringEncodingASCII
) == NULL
) ||
489 ((if_nametoindex(if_name
)) == 0)) {
490 // if interface index not available
494 // add [scoped] proxy entry
495 // ... and remove keys we don't want in a [scoped] proxy
497 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
498 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchDomains
);
499 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchOrders
);
500 CFDictionaryRemoveValue(newProxy
, kSCPropInterfaceName
);
501 if (scoped
== NULL
) {
502 scoped
= CFDictionaryCreateMutable(NULL
,
504 &kCFTypeDictionaryKeyCallBacks
,
505 &kCFTypeDictionaryValueCallBacks
);
507 CFDictionarySetValue(scoped
, interface
, newProxy
);
509 CFRelease(interface
);
517 add_default_proxy(CFMutableArrayRef proxies
,
518 CFDictionaryRef defaultProxy
,
521 CFMutableDictionaryRef myDefault
;
522 uint32_t myOrder
= DEFAULT_MATCH_ORDER
;
523 CFNumberRef order
= NULL
;
525 if (defaultProxy
== NULL
) {
526 myDefault
= CFDictionaryCreateMutable(NULL
,
528 &kCFTypeDictionaryKeyCallBacks
,
529 &kCFTypeDictionaryValueCallBacks
);
531 myDefault
= CFDictionaryCreateMutableCopy(NULL
, 0, defaultProxy
);
532 CFDictionaryRemoveValue(myDefault
, kSCPropInterfaceName
);
533 order
= CFDictionaryGetValue(myDefault
, PROXY_MATCH_ORDER_KEY
);
536 // ensure that the default proxy has a search order
538 if (!isA_CFNumber(order
) ||
539 !CFNumberGetValue(order
, kCFNumberIntType
, &myOrder
)) {
540 myOrder
= DEFAULT_MATCH_ORDER
;
541 order
= CFNumberCreate(NULL
, kCFNumberIntType
, &myOrder
);
542 CFDictionarySetValue(myDefault
, PROXY_MATCH_ORDER_KEY
, order
);
547 // add the default proxy
549 add_proxy(proxies
, myDefault
);
550 CFRelease(myDefault
);
555 static CFComparisonResult
556 compareDomain(const void *val1
, const void *val2
, void *context
)
558 CFDictionaryRef proxy1
= (CFDictionaryRef
)val1
;
559 CFDictionaryRef proxy2
= (CFDictionaryRef
)val2
;
562 CFArrayRef labels1
= NULL
;
563 CFArrayRef labels2
= NULL
;
566 CFComparisonResult result
;
570 // "default" domains sort before "supplemental" domains
571 domain1
= CFDictionaryGetValue(proxy1
, kSCPropNetProxiesSupplementalMatchDomain
);
572 domain2
= CFDictionaryGetValue(proxy2
, kSCPropNetProxiesSupplementalMatchDomain
);
573 if (domain1
== NULL
) {
574 if (domain2
== NULL
) {
575 return kCFCompareEqualTo
;
577 return kCFCompareLessThan
;
578 } else if (domain2
== NULL
) {
579 return kCFCompareGreaterThan
;
582 // forward (A, AAAA) domains sort before reverse (PTR) domains
583 rev1
= CFStringHasSuffix(domain1
, CFSTR(".arpa"));
584 rev2
= CFStringHasSuffix(domain2
, CFSTR(".arpa"));
587 return kCFCompareGreaterThan
;
589 return kCFCompareLessThan
;
593 labels1
= CFStringCreateArrayBySeparatingStrings(NULL
, domain1
, CFSTR("."));
594 n1
= CFArrayGetCount(labels1
);
596 labels2
= CFStringCreateArrayBySeparatingStrings(NULL
, domain2
, CFSTR("."));
597 n2
= CFArrayGetCount(labels2
);
599 while ((n1
> 0) && (n2
> 0)) {
600 CFStringRef label1
= CFArrayGetValueAtIndex(labels1
, --n1
);
601 CFStringRef label2
= CFArrayGetValueAtIndex(labels2
, --n2
);
603 // compare domain labels
604 result
= CFStringCompare(label1
, label2
, kCFCompareCaseInsensitive
);
605 if (result
!= kCFCompareEqualTo
) {
610 // longer labels (corp.apple.com) sort before shorter labels (apple.com)
612 result
= kCFCompareLessThan
;
614 } else if (n1
< n2
) {
615 result
= kCFCompareGreaterThan
;
619 // sort by search order
620 result
= compareBySearchOrder(val1
, val2
, context
);
624 if (labels1
!= NULL
) CFRelease(labels1
);
625 if (labels2
!= NULL
) CFRelease(labels2
);
631 CF_RETURNS_RETAINED CFDictionaryRef
632 proxy_configuration_update(CFDictionaryRef defaultProxy
,
633 CFDictionaryRef services
,
634 CFArrayRef serviceOrder
,
635 CFDictionaryRef servicesInfo
)
638 CFMutableDictionaryRef myDefault
;
639 Boolean myOrderAdded
= FALSE
;
640 CFMutableDictionaryRef newProxy
= NULL
;
642 CFDictionaryRef proxy
;
643 CFMutableArrayRef proxies
;
645 // establish full list of proxies
647 proxies
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
649 // collect (and add) any "supplemental" proxy configurations
651 add_supplemental_proxies(proxies
, services
, serviceOrder
);
653 // add the "default" proxy
655 add_default_proxy(proxies
, defaultProxy
, &myOrderAdded
);
657 // sort proxies, cleanup
659 n_proxies
= CFArrayGetCount(proxies
);
661 CFArraySortValues(proxies
, CFRangeMake(0, n_proxies
), compareDomain
, NULL
);
666 for (i
= n_proxies
- 1; i
>= 0; i
--) {
667 proxy
= CFArrayGetValueAtIndex(proxies
, i
);
670 !CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
671 // remove non-supplemental proxy
672 CFArrayRemoveValueAtIndex(proxies
, i
);
677 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
678 CFDictionaryRemoveValue(newProxy
, PROXY_MATCH_ORDER_KEY
);
679 CFDictionaryRemoveValue(newProxy
, ORDER_KEY
);
680 CFArraySetValueAtIndex(proxies
, i
, newProxy
);
684 // update the default proxy
686 myDefault
= CFDictionaryCreateMutableCopy(NULL
,
688 CFArrayGetValueAtIndex(proxies
, 0));
689 if (myOrderAdded
&& (n_proxies
> 1)) {
690 CFDictionaryRef proxy
;
692 proxy
= CFArrayGetValueAtIndex(proxies
, 1);
693 if (CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
694 // if not a supplemental "default" proxy (a match domain name is
696 CFDictionaryRemoveValue(myDefault
, PROXY_MATCH_ORDER_KEY
);
699 CFArraySetValueAtIndex(proxies
, 0, myDefault
);
700 CFRelease(myDefault
);
702 // establish proxy configuration
705 CFDictionaryRef app_layer
;
706 CFDictionaryRef scoped
;
707 CFArrayRef serviceOrderAll
;
708 Boolean skip
= FALSE
;
709 CFArrayRef supplemental
;
711 proxy
= CFArrayGetValueAtIndex(proxies
, 0);
712 if (!CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
713 // if we have "a" default (non-supplemental) proxy
714 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
715 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchDomains
);
716 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchOrders
);
719 newProxy
= CFDictionaryCreateMutable(NULL
,
721 &kCFTypeDictionaryKeyCallBacks
,
722 &kCFTypeDictionaryValueCallBacks
);
725 serviceOrderAll
= service_order_copy_all(services
, serviceOrder
);
727 // collect (and add) any "supplemental" proxy configurations
729 supplemental
= copy_supplemental_proxies(proxies
, skip
);
730 if (supplemental
!= NULL
) {
731 CFDictionarySetValue(newProxy
, kSCPropNetProxiesSupplemental
, supplemental
);
732 CFRelease(supplemental
);
735 // collect (and add) any "scoped" proxy configurations
737 scoped
= copy_scoped_proxies(services
, serviceOrderAll
);
738 if (scoped
!= NULL
) {
739 CFDictionarySetValue(newProxy
, kSCPropNetProxiesScoped
, scoped
);
743 // collect (and add) any "services" based proxy configurations
745 app_layer
= copy_app_layer_vpn_proxies(services
, serviceOrderAll
, servicesInfo
);
746 if (app_layer
!= NULL
) {
747 CFDictionarySetValue(newProxy
, kSCPropNetProxiesServices
, app_layer
);
748 CFRelease(app_layer
);
751 if (serviceOrderAll
!= NULL
) {
752 CFRelease(serviceOrderAll
);
765 proxy_configuration_init(CFBundleRef bundle
)
767 CFDictionaryRef dict
;
769 dict
= CFBundleGetInfoDictionary(bundle
);
770 if (isA_CFDictionary(dict
)) {
771 G_supplemental_proxies_follow_dns
= CFDictionaryGetValue(dict
, CFSTR("SupplementalProxiesFollowSupplementalDNS"));
772 G_supplemental_proxies_follow_dns
= isA_CFBoolean(G_supplemental_proxies_follow_dns
);
780 #pragma mark Standalone test code
786 mergeDict(const void *key
, const void *value
, void *context
)
788 CFMutableDictionaryRef newDict
= (CFMutableDictionaryRef
)context
;
790 CFDictionarySetValue(newDict
, key
, value
);
796 split(const void * key
, const void * value
, void * context
)
798 CFArrayRef components
;
799 CFStringRef entity_id
;
800 CFStringRef service_id
;
801 CFMutableDictionaryRef state_dict
;
803 components
= CFStringCreateArrayBySeparatingStrings(NULL
, (CFStringRef
)key
, CFSTR("/"));
804 service_id
= CFArrayGetValueAtIndex(components
, 3);
805 entity_id
= CFArrayGetValueAtIndex(components
, 4);
806 state_dict
= (CFMutableDictionaryRef
)CFDictionaryGetValue(context
, service_id
);
807 if (state_dict
!= NULL
) {
808 state_dict
= CFDictionaryCreateMutableCopy(NULL
, 0, state_dict
);
810 state_dict
= CFDictionaryCreateMutable(NULL
,
812 &kCFTypeDictionaryKeyCallBacks
,
813 &kCFTypeDictionaryValueCallBacks
);
816 if (CFEqual(entity_id
, kSCEntNetIPv4
) ||
817 CFEqual(entity_id
, kSCEntNetIPv6
)) {
818 CFStringRef interface
;
820 interface
= CFDictionaryGetValue((CFDictionaryRef
)value
, kSCPropInterfaceName
);
821 if (interface
!= NULL
) {
822 CFDictionaryRef proxy
;
823 CFMutableDictionaryRef new_proxy
;
825 proxy
= CFDictionaryGetValue(state_dict
, kSCEntNetProxies
);
827 new_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
829 new_proxy
= CFDictionaryCreateMutable(NULL
,
831 &kCFTypeDictionaryKeyCallBacks
,
832 &kCFTypeDictionaryValueCallBacks
);
834 CFDictionarySetValue(new_proxy
, kSCPropInterfaceName
, interface
);
835 CFDictionarySetValue(state_dict
, kSCEntNetProxies
, new_proxy
);
836 CFRelease(new_proxy
);
838 } else if (CFEqual(entity_id
, kSCEntNetProxies
)) {
839 CFDictionaryRef proxy
;
841 proxy
= CFDictionaryGetValue(state_dict
, kSCEntNetProxies
);
844 CFMutableDictionaryRef new_proxy
;
846 // if we already have some Setup: or State: proxy content
847 domain
= CFArrayGetValueAtIndex(components
, 0);
848 if (CFEqual(domain
, kSCDynamicStoreDomainState
)) {
849 // if we've already seen the Setup: key
850 new_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, (CFDictionaryRef
)value
);
851 CFDictionaryApplyFunction(proxy
, mergeDict
, new_proxy
);
853 // if we've already seen the State: key
854 new_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
855 CFDictionaryApplyFunction((CFDictionaryRef
)value
, mergeDict
, new_proxy
);
857 CFDictionarySetValue(state_dict
, kSCEntNetProxies
, new_proxy
);
858 CFRelease(new_proxy
);
860 CFDictionarySetValue(state_dict
, kSCEntNetProxies
, (CFDictionaryRef
)value
);
863 CFDictionarySetValue(state_dict
, entity_id
, (CFDictionaryRef
)value
);
866 CFDictionarySetValue((CFMutableDictionaryRef
)context
, service_id
, state_dict
);
867 CFRelease(state_dict
);
868 CFRelease(components
);
874 main(int argc
, char **argv
)
876 CFDictionaryRef entities
;
878 CFDictionaryRef newProxy
= NULL
;
880 CFMutableArrayRef patterns
;
881 CFStringRef primary
= NULL
;
882 CFMutableDictionaryRef primary_proxy
= NULL
;
883 CFArrayRef service_order
= NULL
;
884 CFMutableDictionaryRef service_state_dict
;
885 CFDictionaryRef setup_global_ipv4
;
886 CFDictionaryRef state_global_ipv4
;
887 SCDynamicStoreRef store
;
890 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
892 store
= SCDynamicStoreCreate(NULL
, CFSTR("TEST"), NULL
, NULL
);
894 // get IPv4, IPv6, and Proxies entities
895 patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
896 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
897 kSCDynamicStoreDomainState
,
900 CFArrayAppendValue(patterns
, pattern
);
902 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
903 kSCDynamicStoreDomainState
,
906 CFArrayAppendValue(patterns
, pattern
);
908 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
909 kSCDynamicStoreDomainSetup
,
912 CFArrayAppendValue(patterns
, pattern
);
914 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
915 kSCDynamicStoreDomainState
,
918 CFArrayAppendValue(patterns
, pattern
);
920 entities
= SCDynamicStoreCopyMultiple(store
, NULL
, patterns
);
923 service_state_dict
= CFDictionaryCreateMutable(NULL
,
925 &kCFTypeDictionaryKeyCallBacks
,
926 &kCFTypeDictionaryValueCallBacks
);
927 CFDictionaryApplyFunction(entities
, split
, service_state_dict
);
930 // get primary service ID
931 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
932 kSCDynamicStoreDomainState
,
934 state_global_ipv4
= SCDynamicStoreCopyValue(store
, key
);
936 if (state_global_ipv4
!= NULL
) {
937 primary
= CFDictionaryGetValue(state_global_ipv4
, kSCDynamicStorePropNetPrimaryService
);
938 if (primary
!= NULL
) {
939 CFDictionaryRef service_dict
;
941 // get proxy configuration for primary service
942 service_dict
= CFDictionaryGetValue(service_state_dict
, primary
);
943 if (service_dict
!= NULL
) {
944 CFDictionaryRef service_proxy
;
946 service_proxy
= CFDictionaryGetValue(service_dict
, kSCEntNetProxies
);
947 if (service_proxy
!= NULL
) {
948 primary_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, service_proxy
);
949 CFDictionaryRemoveValue(primary_proxy
, kSCPropInterfaceName
);
956 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
957 kSCDynamicStoreDomainSetup
,
959 setup_global_ipv4
= SCDynamicStoreCopyValue(store
, key
);
961 if (setup_global_ipv4
!= NULL
) {
962 service_order
= CFDictionaryGetValue(setup_global_ipv4
, kSCPropNetServiceOrder
);
965 // update proxy configuration
966 proxy_configuration_init(CFBundleGetMainBundle());
967 newProxy
= proxy_configuration_update(primary_proxy
,
971 if (newProxy
!= NULL
) {
972 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), newProxy
);
977 if (setup_global_ipv4
!= NULL
) CFRelease(setup_global_ipv4
);
978 if (state_global_ipv4
!= NULL
) CFRelease(state_global_ipv4
);
979 CFRelease(service_state_dict
);