2 * Copyright (c) 2013, 2015 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 * March 15, 2013 Allan Nathanson <ajn@apple.com>
32 #include <SystemConfiguration/SystemConfiguration.h>
33 #include <SystemConfiguration/SCPrivate.h>
34 #include <SystemConfiguration/SCValidation.h>
37 #ifndef kDNSServiceCompMulticastDNS
38 #define kDNSServiceCompMulticastDNS "MulticastDNS"
40 #ifndef kDNSServiceCompPrivateDNS
41 #define kDNSServiceCompPrivateDNS "PrivateDNS"
47 static CFMutableArrayRef mirror_keys
= NULL
;
48 static CFMutableArrayRef mirror_patterns
= NULL
;
49 static SCDynamicStoreRef store_host
= NULL
;
50 static SCDynamicStoreRef store_sim
= NULL
;
54 #pragma mark iOS Simulator Support
58 mirror(SCDynamicStoreRef store
, CFArrayRef changes
, void *info
)
60 CFDictionaryRef content_host
;
61 CFDictionaryRef content_sim
;
65 n
= CFArrayGetCount(changes
);
71 // capture "host" content
73 content_host
= SCDynamicStoreCopyMultiple(store_host
, changes
, NULL
);
75 content_host
= (CFDictionaryRef
)info
;
78 // capture [current] "sim" content
79 content_sim
= SCDynamicStoreCopyMultiple(store_sim
, changes
, NULL
);
83 for (i
= 0; i
< n
; i
++) {
85 CFPropertyListRef val
;
87 key
= CFArrayGetValueAtIndex(changes
, i
);
88 val
= (content_host
!= NULL
) ? CFDictionaryGetValue(content_host
, key
) : NULL
;
90 // if "host" content changed
91 cache_SCDynamicStoreSetValue(store_sim
, key
, val
);
93 // if no "host" content
94 val
= (content_sim
!= NULL
) ? CFDictionaryGetValue(content_sim
, key
) : NULL
;
96 // if we need to remove the "sim" content
97 cache_SCDynamicStoreRemoveValue(store_sim
, key
);
99 // if no "sim" content to remove, just notify
100 cache_SCDynamicStoreNotifyValue(store_sim
, key
);
104 cache_write(store_sim
);
108 if ((info
== NULL
) && (content_host
!= NULL
)) {
109 CFRelease(content_host
);
111 if (content_sim
!= NULL
) {
112 CFRelease(content_sim
);
125 mirror_keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
126 mirror_patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
128 // Plugin:InterfaceNamer
129 key
= SCDynamicStoreKeyCreate(NULL
,
130 CFSTR("%@" "InterfaceNamer"),
131 kSCDynamicStoreDomainPlugin
);
132 CFArrayAppendValue(mirror_keys
, key
);
136 // key = SCDynamicStoreKeyCreateComputerName(NULL);
137 // CFArrayAppendValue(mirror_keys, key);
141 // key = SCDynamicStoreKeyCreateHostNames(NULL);
142 // CFArrayAppendValue(mirror_keys, key);
145 // Setup:/Network/Global/.*
146 pattern
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
147 kSCDynamicStoreDomainSetup
,
149 CFArrayAppendValue(mirror_patterns
, pattern
);
152 // Setup:/Network/Service/.*
153 // State:/Network/Service/.*
154 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
158 CFArrayAppendValue(mirror_patterns
, pattern
);
161 // Setup:/Network/Interface/.*
162 // State:/Network/Interface/.*
163 pattern
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
167 CFArrayAppendValue(mirror_patterns
, pattern
);
170 // State:/Network/MulticastDNS
171 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
172 kSCDynamicStoreDomainState
,
174 CFSTR(kDNSServiceCompMulticastDNS
));
175 CFArrayAppendValue(mirror_keys
, key
);
178 // State:/Network/PrivateDNS
179 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
180 kSCDynamicStoreDomainState
,
182 CFSTR(kDNSServiceCompPrivateDNS
));
183 CFArrayAppendValue(mirror_keys
, key
);
195 prime_SimulatorSupport()
197 CFDictionaryRef content_host
;
200 SC_log(LOG_DEBUG
, "prime() called");
202 // copy current content from base OS store to _Sim store
203 content_host
= SCDynamicStoreCopyMultiple(store_host
, mirror_keys
, mirror_patterns
);
204 CFRelease(mirror_keys
);
206 CFRelease(mirror_patterns
);
207 mirror_patterns
= NULL
;
209 if (content_host
== NULL
) {
213 n
= CFDictionaryGetCount(content_host
);
216 const void * keys_host_q
[N_QUICK
];
217 const void ** keys_host
= keys_host_q
;
219 if (n
> (CFIndex
)(sizeof(keys_host_q
) / sizeof(CFStringRef
))) {
220 keys_host
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
223 CFDictionaryGetKeysAndValues(content_host
, keys_host
, NULL
);
225 changes
= CFArrayCreate(NULL
, keys_host
, n
, &kCFTypeArrayCallBacks
);
226 mirror(store_host
, changes
, (void *)content_host
);
229 if (keys_host
!= keys_host_q
) {
230 CFAllocatorDeallocate(NULL
, keys_host
);
234 CFRelease(content_host
);
241 load_SimulatorSupport(CFBundleRef bundle
, Boolean bundleVerbose
)
244 CFMutableDictionaryRef options
;
245 CFRunLoopSourceRef rls
;
247 SC_log(LOG_DEBUG
, "load() called");
248 SC_log(LOG_DEBUG
, " bundle ID = %@", CFBundleGetIdentifier(bundle
));
253 // setup SCDynamicStore mirroring (from "host")
254 options
= CFDictionaryCreateMutable(NULL
,
256 &kCFTypeDictionaryKeyCallBacks
,
257 &kCFTypeDictionaryValueCallBacks
);
258 CFDictionarySetValue(options
, kSCDynamicStoreUseHostServer
, kCFBooleanTrue
);
259 store_host
= SCDynamicStoreCreateWithOptions(NULL
,
260 CFSTR("SimulatorSupport(host)"),
265 assert(store_host
!= NULL
);
267 ok
= SCDynamicStoreSetNotificationKeys(store_host
, mirror_keys
, mirror_patterns
);
270 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store_host
, 0);
273 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
276 // setup SCDynamicStore mirroring (to "iOS Simulator")
277 store_sim
= SCDynamicStoreCreate(NULL
,
278 CFSTR("SimulatorSupport(sim)"),
290 #pragma mark Standalone test code
294 main(int argc
, char **argv
)
297 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
299 load_SimulatorSupport(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
300 prime_SimulatorSupport();