2 * Copyright (c) 2011-2014 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>
40 #include "ip_plugin.h"
43 #define DEFAULT_MATCH_ORDER 200000 /* match order for the "default" proxy configuration */
46 #define PROXY_MATCH_ORDER_KEY CFSTR("__MATCH_ORDER__")
47 #define ORDER_KEY CFSTR("__ORDER__")
50 CFBooleanRef G_supplemental_proxies_follow_dns
= NULL
;
54 add_proxy(CFMutableArrayRef proxies
, CFMutableDictionaryRef proxy
)
60 n_proxies
= CFArrayGetCount(proxies
);
61 for (i
= 0; i
< n_proxies
; i
++) {
62 CFDictionaryRef match_proxy
;
64 match_proxy
= CFArrayGetValueAtIndex(proxies
, i
);
65 if (CFEqual(proxy
, match_proxy
)) {
71 order
= CFNumberCreate(NULL
, kCFNumberCFIndexType
, &n_proxies
);
72 CFDictionarySetValue(proxy
, ORDER_KEY
, order
);
75 CFArrayAppendValue(proxies
, proxy
);
81 add_supplemental(CFMutableArrayRef proxies
, CFDictionaryRef proxy
, uint32_t defaultOrder
)
88 domains
= CFDictionaryGetValue(proxy
, kSCPropNetProxiesSupplementalMatchDomains
);
89 n_domains
= isA_CFArray(domains
) ? CFArrayGetCount(domains
) : 0;
94 orders
= CFDictionaryGetValue(proxy
, kSCPropNetProxiesSupplementalMatchOrders
);
96 if (!isA_CFArray(orders
) || (n_domains
!= CFArrayGetCount(orders
))) {
102 * yes, this is a "supplemental" proxy configuration, expand
103 * the match domains and add each to the proxies list.
105 for (i
= 0; i
< n_domains
; i
++) {
106 CFStringRef match_domain
;
107 CFNumberRef match_order
;
108 CFMutableDictionaryRef match_proxy
;
110 match_domain
= CFArrayGetValueAtIndex(domains
, i
);
111 if (!isA_CFString(match_domain
)) {
115 match_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
117 // set supplemental proxy match "domain"
118 match_domain
= _SC_trimDomain(match_domain
);
119 if (match_domain
!= NULL
) {
120 CFDictionarySetValue(match_proxy
, kSCPropNetProxiesSupplementalMatchDomain
, match_domain
);
121 CFRelease(match_domain
);
123 CFDictionaryRemoveValue(match_proxy
, kSCPropNetProxiesSupplementalMatchDomain
);
126 // set supplemental proxy match "order"
127 match_order
= (orders
!= NULL
) ? CFArrayGetValueAtIndex(orders
, i
) : NULL
;
128 if (isA_CFNumber(match_order
)) {
129 CFDictionarySetValue(match_proxy
, PROXY_MATCH_ORDER_KEY
, match_order
);
133 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &defaultOrder
);
134 CFDictionarySetValue(match_proxy
, PROXY_MATCH_ORDER_KEY
, num
);
137 defaultOrder
++; // if multiple domains, maintain ordering
140 // remove keys we don't want in a supplemental proxy
141 CFDictionaryRemoveValue(match_proxy
, kSCPropNetProxiesSupplementalMatchDomains
);
142 CFDictionaryRemoveValue(match_proxy
, kSCPropNetProxiesSupplementalMatchOrders
);
143 CFDictionaryRemoveValue(match_proxy
, kSCPropInterfaceName
);
145 add_proxy(proxies
, match_proxy
);
146 CFRelease(match_proxy
);
157 add_supplemental_proxies(CFMutableArrayRef proxies
, CFDictionaryRef services
, CFArrayRef service_order
)
159 const void * keys_q
[N_QUICK
];
160 const void ** keys
= keys_q
;
164 const void * vals_q
[N_QUICK
];
165 const void ** vals
= vals_q
;
167 n_services
= isA_CFDictionary(services
) ? CFDictionaryGetCount(services
) : 0;
168 if (n_services
== 0) {
169 return; // if no services
172 if (n_services
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
173 keys
= CFAllocatorAllocate(NULL
, n_services
* sizeof(CFTypeRef
), 0);
174 vals
= CFAllocatorAllocate(NULL
, n_services
* sizeof(CFTypeRef
), 0);
177 n_order
= isA_CFArray(service_order
) ? CFArrayGetCount(service_order
) : 0;
179 CFDictionaryGetKeysAndValues(services
, keys
, vals
);
180 for (i
= 0; i
< n_services
; i
++) {
181 uint32_t defaultOrder
;
182 CFDictionaryRef proxy
;
183 CFMutableDictionaryRef proxyWithDNS
= NULL
;
184 CFDictionaryRef service
= (CFDictionaryRef
)vals
[i
];
186 if (!isA_CFDictionary(service
)) {
190 proxy
= CFDictionaryGetValue(service
, kSCEntNetProxies
);
191 if (!isA_CFDictionary(proxy
)) {
195 if ((G_supplemental_proxies_follow_dns
!= NULL
) && CFBooleanGetValue(G_supplemental_proxies_follow_dns
)) {
197 CFArrayRef matchDomains
;
198 CFArrayRef matchOrders
;
200 if (!CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomains
) &&
201 CFDictionaryGetValueIfPresent(service
, kSCEntNetDNS
, (const void **)&dns
) &&
202 isA_CFDictionary(dns
) &&
203 CFDictionaryGetValueIfPresent(dns
, kSCPropNetDNSSupplementalMatchDomains
, (const void **)&matchDomains
) &&
204 isA_CFArray(matchDomains
)) {
205 proxyWithDNS
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
206 CFDictionarySetValue(proxyWithDNS
, kSCPropNetProxiesSupplementalMatchDomains
, matchDomains
);
207 if (CFDictionaryGetValueIfPresent(dns
, kSCPropNetDNSSupplementalMatchOrders
, (const void **)&matchOrders
) &&
208 isA_CFArray(matchOrders
)) {
209 CFDictionarySetValue(proxyWithDNS
, kSCPropNetProxiesSupplementalMatchOrders
, matchOrders
);
211 CFDictionaryRemoveValue(proxyWithDNS
, kSCPropNetProxiesSupplementalMatchOrders
);
213 proxy
= proxyWithDNS
;
217 defaultOrder
= DEFAULT_MATCH_ORDER
218 - (DEFAULT_MATCH_ORDER
/ 2)
219 + ((DEFAULT_MATCH_ORDER
/ 1000) * (uint32_t)i
);
221 !CFArrayContainsValue(service_order
, CFRangeMake(0, n_order
), keys
[i
])) {
222 // push out services not specified in service order
223 defaultOrder
+= (DEFAULT_MATCH_ORDER
/ 1000) * n_services
;
226 add_supplemental(proxies
, proxy
, defaultOrder
);
227 if (proxyWithDNS
!= NULL
) CFRelease(proxyWithDNS
);
230 if (keys
!= keys_q
) {
231 CFAllocatorDeallocate(NULL
, keys
);
232 CFAllocatorDeallocate(NULL
, vals
);
239 static CFComparisonResult
240 compareBySearchOrder(const void *val1
, const void *val2
, void *context
)
242 CFDictionaryRef proxy1
= (CFDictionaryRef
)val1
;
243 CFDictionaryRef proxy2
= (CFDictionaryRef
)val2
;
246 uint32_t order1
= DEFAULT_MATCH_ORDER
;
247 uint32_t order2
= DEFAULT_MATCH_ORDER
;
249 num1
= CFDictionaryGetValue(proxy1
, PROXY_MATCH_ORDER_KEY
);
250 if (!isA_CFNumber(num1
) ||
251 !CFNumberGetValue(num1
, kCFNumberIntType
, &order1
)) {
252 order1
= DEFAULT_MATCH_ORDER
;
255 num2
= CFDictionaryGetValue(proxy2
, PROXY_MATCH_ORDER_KEY
);
256 if (!isA_CFNumber(num2
) ||
257 !CFNumberGetValue(num2
, kCFNumberIntType
, &order2
)) {
258 order2
= DEFAULT_MATCH_ORDER
;
261 if (order1
== order2
) {
262 // if same match "order", retain original ordering for configurations
263 if (CFDictionaryGetValueIfPresent(proxy1
, ORDER_KEY
, (const void **)&num1
) &&
264 CFDictionaryGetValueIfPresent(proxy2
, ORDER_KEY
, (const void **)&num2
) &&
265 isA_CFNumber(num1
) &&
266 isA_CFNumber(num2
) &&
267 CFNumberGetValue(num1
, kCFNumberIntType
, &order1
) &&
268 CFNumberGetValue(num2
, kCFNumberIntType
, &order2
)) {
269 if (order1
== order2
) {
270 return kCFCompareEqualTo
;
272 return (order1
< order2
) ? kCFCompareLessThan
: kCFCompareGreaterThan
;
276 return kCFCompareEqualTo
;
279 return (order1
< order2
) ? kCFCompareLessThan
: kCFCompareGreaterThan
;
283 static __inline__ Boolean
284 isSupplementalProxy(CFDictionaryRef proxy
)
286 if ((proxy
!= NULL
) &&
287 CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
296 copy_supplemental_proxies(CFArrayRef proxies
, Boolean skip
)
300 CFMutableArrayRef supplemental
= NULL
;
302 // iterate over services
304 n_proxies
= isA_CFArray(proxies
) ? CFArrayGetCount(proxies
) : 0;
305 for (i
= 0; i
< n_proxies
; i
++) {
306 CFDictionaryRef proxy
;
308 proxy
= CFArrayGetValueAtIndex(proxies
, i
);
309 if (!isSupplementalProxy(proxy
)) {
310 // if not supplemental proxy (i.e. no match domain)
314 // add [supplemental] proxy entry
315 if (supplemental
== NULL
) {
316 supplemental
= CFArrayCreateMutable(NULL
,
318 &kCFTypeArrayCallBacks
);
320 CFArrayAppendValue(supplemental
, proxy
);
328 service_order_copy_all(CFDictionaryRef services
, CFArrayRef service_order
)
330 const void * keys_q
[N_QUICK
];
331 const void ** keys
= keys_q
;
335 CFMutableArrayRef order
;
337 // ensure that we process all services in order
338 n_services
= isA_CFDictionary(services
) ? CFDictionaryGetCount(services
) : 0;
339 if (n_services
== 0) {
344 // ensure that we process all services in order
346 n_order
= isA_CFArray(service_order
) ? CFArrayGetCount(service_order
) : 0;
348 order
= CFArrayCreateMutableCopy(NULL
, 0, service_order
);
350 order
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
353 if (n_services
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
354 keys
= CFAllocatorAllocate(NULL
, n_services
* sizeof(CFTypeRef
), 0);
356 CFDictionaryGetKeysAndValues(services
, keys
, NULL
);
357 for (i
= 0; i
< n_services
; i
++) {
358 CFStringRef serviceID
= (CFStringRef
)keys
[i
];
360 if (!CFArrayContainsValue(order
, CFRangeMake(0, n_order
), serviceID
)) {
361 CFArrayAppendValue(order
, serviceID
);
365 if (keys
!= keys_q
) {
366 CFAllocatorDeallocate(NULL
, keys
);
373 static CFDictionaryRef
374 copy_app_layer_vpn_proxies(CFDictionaryRef services
, CFArrayRef order
, CFDictionaryRef services_info
)
376 CFMutableDictionaryRef app_layer_proxies
= NULL
;
380 if (!isA_CFDictionary(services_info
)) {
384 // iterate over services
386 n_order
= isA_CFArray(order
) ? CFArrayGetCount(order
) : 0;
387 for (i
= 0; i
< n_order
; i
++) {
388 CFMutableDictionaryRef newProxy
;
389 CFDictionaryRef proxy
;
390 CFDictionaryRef service
;
391 CFStringRef serviceID
;
392 CFNumberRef isServiceSpecific
;
395 serviceID
= CFArrayGetValueAtIndex(order
, i
);
396 service
= CFDictionaryGetValue(services
, serviceID
);
397 if (!isA_CFDictionary(service
)) {
402 proxy
= CFDictionaryGetValue(service
, kSCEntNetProxies
);
403 if (!isA_CFDictionary(proxy
)) {
408 isServiceSpecific
= CFDictionaryGetValue(proxy
, kSCPropNetProxiesServiceSpecific
);
409 if (!isA_CFNumber(isServiceSpecific
) || !CFNumberGetValue(isServiceSpecific
, kCFNumberIntType
, &boolValue
) || !boolValue
) {
410 // if not a service-specific proxy configuration
414 if ((app_layer_proxies
!= NULL
) &&
415 CFDictionaryContainsKey(app_layer_proxies
, serviceID
)) {
416 // if we've already processed this [app_layer_proxies] interface
420 // add [app_layer_proxies] proxy entry
421 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
422 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchDomains
);
423 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchOrders
);
424 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesServiceSpecific
);
425 if (app_layer_proxies
== NULL
) {
426 app_layer_proxies
= CFDictionaryCreateMutable(NULL
,
428 &kCFTypeDictionaryKeyCallBacks
,
429 &kCFTypeDictionaryValueCallBacks
);
431 CFDictionarySetValue(app_layer_proxies
, serviceID
, newProxy
);
435 return app_layer_proxies
;
439 static CFDictionaryRef
440 copy_scoped_proxies(CFDictionaryRef services
, CFArrayRef order
)
444 CFMutableDictionaryRef scoped
= NULL
;
446 // iterate over services
448 n_order
= isA_CFArray(order
) ? CFArrayGetCount(order
) : 0;
449 for (i
= 0; i
< n_order
; i
++) {
450 char if_name
[IF_NAMESIZE
];
451 CFStringRef interface
;
452 CFMutableDictionaryRef newProxy
;
453 CFDictionaryRef proxy
;
454 CFDictionaryRef service
;
455 CFStringRef serviceID
;
457 serviceID
= CFArrayGetValueAtIndex(order
, i
);
458 service
= CFDictionaryGetValue(services
, serviceID
);
459 if (!isA_CFDictionary(service
)) {
464 proxy
= CFDictionaryGetValue(service
, kSCEntNetProxies
);
465 if (!isA_CFDictionary(proxy
)) {
470 interface
= CFDictionaryGetValue(proxy
, kSCPropInterfaceName
);
471 if (interface
== NULL
) {
472 // if no [scoped] interface
475 if ((scoped
!= NULL
) &&
476 CFDictionaryContainsKey(scoped
, interface
)) {
477 // if we've already processed this [scoped] interface
481 if ((_SC_cfstring_to_cstring(interface
,
484 kCFStringEncodingASCII
) == NULL
) ||
485 ((my_if_nametoindex(if_name
)) == 0)) {
486 // if interface index not available
490 // add [scoped] proxy entry
491 // ... and remove keys we don't want in a [scoped] proxy
493 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
494 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchDomains
);
495 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchOrders
);
496 CFDictionaryRemoveValue(newProxy
, kSCPropInterfaceName
);
497 if (scoped
== NULL
) {
498 scoped
= CFDictionaryCreateMutable(NULL
,
500 &kCFTypeDictionaryKeyCallBacks
,
501 &kCFTypeDictionaryValueCallBacks
);
503 CFDictionarySetValue(scoped
, interface
, newProxy
);
505 CFRelease(interface
);
513 add_default_proxy(CFMutableArrayRef proxies
,
514 CFDictionaryRef defaultProxy
,
517 CFMutableDictionaryRef myDefault
;
518 uint32_t myOrder
= DEFAULT_MATCH_ORDER
;
519 CFNumberRef order
= NULL
;
521 if (defaultProxy
== NULL
) {
522 myDefault
= CFDictionaryCreateMutable(NULL
,
524 &kCFTypeDictionaryKeyCallBacks
,
525 &kCFTypeDictionaryValueCallBacks
);
527 myDefault
= CFDictionaryCreateMutableCopy(NULL
, 0, defaultProxy
);
528 CFDictionaryRemoveValue(myDefault
, kSCPropInterfaceName
);
529 order
= CFDictionaryGetValue(myDefault
, PROXY_MATCH_ORDER_KEY
);
532 // ensure that the default proxy has a search order
534 if (!isA_CFNumber(order
) ||
535 !CFNumberGetValue(order
, kCFNumberIntType
, &myOrder
)) {
536 myOrder
= DEFAULT_MATCH_ORDER
;
537 order
= CFNumberCreate(NULL
, kCFNumberIntType
, &myOrder
);
538 CFDictionarySetValue(myDefault
, PROXY_MATCH_ORDER_KEY
, order
);
543 // add the default proxy
545 add_proxy(proxies
, myDefault
);
546 CFRelease(myDefault
);
551 static CFComparisonResult
552 compareDomain(const void *val1
, const void *val2
, void *context
)
554 CFDictionaryRef proxy1
= (CFDictionaryRef
)val1
;
555 CFDictionaryRef proxy2
= (CFDictionaryRef
)val2
;
558 CFArrayRef labels1
= NULL
;
559 CFArrayRef labels2
= NULL
;
562 CFComparisonResult result
;
566 // "default" domains sort before "supplemental" domains
567 domain1
= CFDictionaryGetValue(proxy1
, kSCPropNetProxiesSupplementalMatchDomain
);
568 domain2
= CFDictionaryGetValue(proxy2
, kSCPropNetProxiesSupplementalMatchDomain
);
569 if (domain1
== NULL
) {
570 if (domain2
== NULL
) {
571 return kCFCompareEqualTo
;
573 return kCFCompareLessThan
;
574 } else if (domain2
== NULL
) {
575 return kCFCompareGreaterThan
;
578 // forward (A, AAAA) domains sort before reverse (PTR) domains
579 rev1
= CFStringHasSuffix(domain1
, CFSTR(".arpa"));
580 rev2
= CFStringHasSuffix(domain2
, CFSTR(".arpa"));
583 return kCFCompareGreaterThan
;
585 return kCFCompareLessThan
;
589 labels1
= CFStringCreateArrayBySeparatingStrings(NULL
, domain1
, CFSTR("."));
590 n1
= CFArrayGetCount(labels1
);
592 labels2
= CFStringCreateArrayBySeparatingStrings(NULL
, domain2
, CFSTR("."));
593 n2
= CFArrayGetCount(labels2
);
595 while ((n1
> 0) && (n2
> 0)) {
596 CFStringRef label1
= CFArrayGetValueAtIndex(labels1
, --n1
);
597 CFStringRef label2
= CFArrayGetValueAtIndex(labels2
, --n2
);
599 // compare domain labels
600 result
= CFStringCompare(label1
, label2
, kCFCompareCaseInsensitive
);
601 if (result
!= kCFCompareEqualTo
) {
606 // longer labels (corp.apple.com) sort before shorter labels (apple.com)
608 result
= kCFCompareLessThan
;
610 } else if (n1
< n2
) {
611 result
= kCFCompareGreaterThan
;
615 // sort by search order
616 result
= compareBySearchOrder(val1
, val2
, context
);
620 if (labels1
!= NULL
) CFRelease(labels1
);
621 if (labels2
!= NULL
) CFRelease(labels2
);
627 CF_RETURNS_RETAINED CFDictionaryRef
628 proxy_configuration_update(CFDictionaryRef defaultProxy
,
629 CFDictionaryRef services
,
630 CFArrayRef serviceOrder
,
631 CFDictionaryRef servicesInfo
)
634 CFMutableDictionaryRef myDefault
;
635 Boolean myOrderAdded
= FALSE
;
636 CFMutableDictionaryRef newProxy
= NULL
;
638 CFDictionaryRef proxy
;
639 CFMutableArrayRef proxies
;
641 // establish full list of proxies
643 proxies
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
645 // collect (and add) any "supplemental" proxy configurations
647 add_supplemental_proxies(proxies
, services
, serviceOrder
);
649 // add the "default" proxy
651 add_default_proxy(proxies
, defaultProxy
, &myOrderAdded
);
653 // sort proxies, cleanup
655 n_proxies
= CFArrayGetCount(proxies
);
657 CFArraySortValues(proxies
, CFRangeMake(0, n_proxies
), compareDomain
, NULL
);
662 for (i
= n_proxies
- 1; i
>= 0; i
--) {
663 proxy
= CFArrayGetValueAtIndex(proxies
, i
);
666 !CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
667 // remove non-supplemental proxy
668 CFArrayRemoveValueAtIndex(proxies
, i
);
673 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
674 CFDictionaryRemoveValue(newProxy
, PROXY_MATCH_ORDER_KEY
);
675 CFDictionaryRemoveValue(newProxy
, ORDER_KEY
);
676 CFArraySetValueAtIndex(proxies
, i
, newProxy
);
680 // update the default proxy
682 myDefault
= CFDictionaryCreateMutableCopy(NULL
,
684 CFArrayGetValueAtIndex(proxies
, 0));
685 if (myOrderAdded
&& (n_proxies
> 1)) {
686 CFDictionaryRef proxy
;
688 proxy
= CFArrayGetValueAtIndex(proxies
, 1);
689 if (CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
690 // if not a supplemental "default" proxy (a match domain name is
692 CFDictionaryRemoveValue(myDefault
, PROXY_MATCH_ORDER_KEY
);
695 CFArraySetValueAtIndex(proxies
, 0, myDefault
);
696 CFRelease(myDefault
);
698 // establish proxy configuration
701 CFDictionaryRef app_layer
;
702 CFDictionaryRef scoped
;
703 CFArrayRef serviceOrderAll
;
704 Boolean skip
= FALSE
;
705 CFArrayRef supplemental
;
707 proxy
= CFArrayGetValueAtIndex(proxies
, 0);
708 if (!CFDictionaryContainsKey(proxy
, kSCPropNetProxiesSupplementalMatchDomain
)) {
709 // if we have "a" default (non-supplemental) proxy
710 newProxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
711 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchDomains
);
712 CFDictionaryRemoveValue(newProxy
, kSCPropNetProxiesSupplementalMatchOrders
);
715 newProxy
= CFDictionaryCreateMutable(NULL
,
717 &kCFTypeDictionaryKeyCallBacks
,
718 &kCFTypeDictionaryValueCallBacks
);
721 serviceOrderAll
= service_order_copy_all(services
, serviceOrder
);
723 // collect (and add) any "supplemental" proxy configurations
725 supplemental
= copy_supplemental_proxies(proxies
, skip
);
726 if (supplemental
!= NULL
) {
727 CFDictionarySetValue(newProxy
, kSCPropNetProxiesSupplemental
, supplemental
);
728 CFRelease(supplemental
);
731 // collect (and add) any "scoped" proxy configurations
733 scoped
= copy_scoped_proxies(services
, serviceOrderAll
);
734 if (scoped
!= NULL
) {
735 CFDictionarySetValue(newProxy
, kSCPropNetProxiesScoped
, scoped
);
739 // collect (and add) any "services" based proxy configurations
741 app_layer
= copy_app_layer_vpn_proxies(services
, serviceOrderAll
, servicesInfo
);
742 if (app_layer
!= NULL
) {
743 CFDictionarySetValue(newProxy
, kSCPropNetProxiesServices
, app_layer
);
744 CFRelease(app_layer
);
747 if (serviceOrderAll
!= NULL
) {
748 CFRelease(serviceOrderAll
);
761 proxy_configuration_init(CFBundleRef bundle
)
763 CFDictionaryRef dict
;
765 dict
= CFBundleGetInfoDictionary(bundle
);
766 if (isA_CFDictionary(dict
)) {
767 G_supplemental_proxies_follow_dns
= CFDictionaryGetValue(dict
, CFSTR("SupplementalProxiesFollowSupplementalDNS"));
768 G_supplemental_proxies_follow_dns
= isA_CFBoolean(G_supplemental_proxies_follow_dns
);
776 #pragma mark Standalone test code
782 mergeDict(const void *key
, const void *value
, void *context
)
784 CFMutableDictionaryRef newDict
= (CFMutableDictionaryRef
)context
;
786 CFDictionarySetValue(newDict
, key
, value
);
792 split(const void * key
, const void * value
, void * context
)
794 CFArrayRef components
;
795 CFStringRef entity_id
;
796 CFStringRef service_id
;
797 CFMutableDictionaryRef state_dict
;
799 components
= CFStringCreateArrayBySeparatingStrings(NULL
, (CFStringRef
)key
, CFSTR("/"));
800 service_id
= CFArrayGetValueAtIndex(components
, 3);
801 entity_id
= CFArrayGetValueAtIndex(components
, 4);
802 state_dict
= (CFMutableDictionaryRef
)CFDictionaryGetValue(context
, service_id
);
803 if (state_dict
!= NULL
) {
804 state_dict
= CFDictionaryCreateMutableCopy(NULL
, 0, state_dict
);
806 state_dict
= CFDictionaryCreateMutable(NULL
,
808 &kCFTypeDictionaryKeyCallBacks
,
809 &kCFTypeDictionaryValueCallBacks
);
812 if (CFEqual(entity_id
, kSCEntNetIPv4
) ||
813 CFEqual(entity_id
, kSCEntNetIPv6
)) {
814 CFStringRef interface
;
816 interface
= CFDictionaryGetValue((CFDictionaryRef
)value
, kSCPropInterfaceName
);
817 if (interface
!= NULL
) {
818 CFDictionaryRef proxy
;
819 CFMutableDictionaryRef new_proxy
;
821 proxy
= CFDictionaryGetValue(state_dict
, kSCEntNetProxies
);
823 new_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
825 new_proxy
= CFDictionaryCreateMutable(NULL
,
827 &kCFTypeDictionaryKeyCallBacks
,
828 &kCFTypeDictionaryValueCallBacks
);
830 CFDictionarySetValue(new_proxy
, kSCPropInterfaceName
, interface
);
831 CFDictionarySetValue(state_dict
, kSCEntNetProxies
, new_proxy
);
832 CFRelease(new_proxy
);
834 } else if (CFEqual(entity_id
, kSCEntNetProxies
)) {
835 CFDictionaryRef proxy
;
837 proxy
= CFDictionaryGetValue(state_dict
, kSCEntNetProxies
);
840 CFMutableDictionaryRef new_proxy
;
842 // if we already have some Setup: or State: proxy content
843 domain
= CFArrayGetValueAtIndex(components
, 0);
844 if (CFEqual(domain
, kSCDynamicStoreDomainState
)) {
845 // if we've already seen the Setup: key
846 new_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, (CFDictionaryRef
)value
);
847 CFDictionaryApplyFunction(proxy
, mergeDict
, new_proxy
);
849 // if we've already seen the State: key
850 new_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, proxy
);
851 CFDictionaryApplyFunction((CFDictionaryRef
)value
, mergeDict
, new_proxy
);
853 CFDictionarySetValue(state_dict
, kSCEntNetProxies
, new_proxy
);
854 CFRelease(new_proxy
);
856 CFDictionarySetValue(state_dict
, kSCEntNetProxies
, (CFDictionaryRef
)value
);
859 CFDictionarySetValue(state_dict
, entity_id
, (CFDictionaryRef
)value
);
862 CFDictionarySetValue((CFMutableDictionaryRef
)context
, service_id
, state_dict
);
863 CFRelease(state_dict
);
864 CFRelease(components
);
870 main(int argc
, char **argv
)
872 CFDictionaryRef entities
;
874 CFDictionaryRef newProxy
= NULL
;
876 CFMutableArrayRef patterns
;
877 CFStringRef primary
= NULL
;
878 CFMutableDictionaryRef primary_proxy
= NULL
;
879 CFArrayRef service_order
= NULL
;
880 CFMutableDictionaryRef service_state_dict
;
881 CFDictionaryRef setup_global_ipv4
;
882 CFDictionaryRef state_global_ipv4
;
883 SCDynamicStoreRef store
;
886 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
888 store
= SCDynamicStoreCreate(NULL
, CFSTR("TEST"), NULL
, NULL
);
890 // get IPv4, IPv6, and Proxies entities
891 patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
892 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
893 kSCDynamicStoreDomainState
,
896 CFArrayAppendValue(patterns
, pattern
);
898 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
899 kSCDynamicStoreDomainState
,
902 CFArrayAppendValue(patterns
, pattern
);
904 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
905 kSCDynamicStoreDomainSetup
,
908 CFArrayAppendValue(patterns
, pattern
);
910 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
911 kSCDynamicStoreDomainState
,
914 CFArrayAppendValue(patterns
, pattern
);
916 entities
= SCDynamicStoreCopyMultiple(store
, NULL
, patterns
);
919 service_state_dict
= CFDictionaryCreateMutable(NULL
,
921 &kCFTypeDictionaryKeyCallBacks
,
922 &kCFTypeDictionaryValueCallBacks
);
923 CFDictionaryApplyFunction(entities
, split
, service_state_dict
);
926 // get primary service ID
927 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
928 kSCDynamicStoreDomainState
,
930 state_global_ipv4
= SCDynamicStoreCopyValue(store
, key
);
932 if (state_global_ipv4
!= NULL
) {
933 primary
= CFDictionaryGetValue(state_global_ipv4
, kSCDynamicStorePropNetPrimaryService
);
934 if (primary
!= NULL
) {
935 CFDictionaryRef service_dict
;
937 // get proxy configuration for primary service
938 service_dict
= CFDictionaryGetValue(service_state_dict
, primary
);
939 if (service_dict
!= NULL
) {
940 CFDictionaryRef service_proxy
;
942 service_proxy
= CFDictionaryGetValue(service_dict
, kSCEntNetProxies
);
943 if (service_proxy
!= NULL
) {
944 primary_proxy
= CFDictionaryCreateMutableCopy(NULL
, 0, service_proxy
);
945 CFDictionaryRemoveValue(primary_proxy
, kSCPropInterfaceName
);
952 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
953 kSCDynamicStoreDomainSetup
,
955 setup_global_ipv4
= SCDynamicStoreCopyValue(store
, key
);
957 if (setup_global_ipv4
!= NULL
) {
958 service_order
= CFDictionaryGetValue(setup_global_ipv4
, kSCPropNetServiceOrder
);
961 // update proxy configuration
962 proxy_configuration_init(CFBundleGetMainBundle());
963 newProxy
= proxy_configuration_update(primary_proxy
,
967 if (newProxy
!= NULL
) {
968 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), newProxy
);
973 if (setup_global_ipv4
!= NULL
) CFRelease(setup_global_ipv4
);
974 if (state_global_ipv4
!= NULL
) CFRelease(state_global_ipv4
);
975 CFRelease(service_state_dict
);