2 * Copyright (c) 2013, 2015-2018 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 <TargetConditionals.h>
33 #include <SystemConfiguration/SystemConfiguration.h>
34 #include <SystemConfiguration/SCPrivate.h>
35 #include <SystemConfiguration/SCValidation.h>
38 #include <dns_sd_private.h>
42 #if TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC
45 static CFMutableArrayRef mirror_keys
= NULL
;
46 static CFMutableArrayRef mirror_patterns
= NULL
;
47 static SCDynamicStoreRef store_host
= NULL
;
48 static SCDynamicStoreRef store_sim
= NULL
;
58 __private_extern__ os_log_t
59 __log_SimulatorSupport(void)
61 static os_log_t log
= NULL
;
64 log
= os_log_create("com.apple.SystemConfiguration", "SimulatorSupport");
72 #pragma mark iOS Simulator Support
76 mirror(SCDynamicStoreRef store
, CFArrayRef changes
, void *info
)
79 CFDictionaryRef content_host
;
80 CFDictionaryRef content_sim
;
84 n
= CFArrayGetCount(changes
);
90 // capture "host" content
92 content_host
= SCDynamicStoreCopyMultiple(store_host
, changes
, NULL
);
94 content_host
= (CFDictionaryRef
)info
;
97 // capture [current] "sim" content
98 content_sim
= SCDynamicStoreCopyMultiple(store_sim
, changes
, NULL
);
102 for (i
= 0; i
< n
; i
++) {
104 CFPropertyListRef val
;
106 key
= CFArrayGetValueAtIndex(changes
, i
);
107 val
= (content_host
!= NULL
) ? CFDictionaryGetValue(content_host
, key
) : NULL
;
109 // if "host" content changed
110 cache_SCDynamicStoreSetValue(store_sim
, key
, val
);
112 // if no "host" content
113 val
= (content_sim
!= NULL
) ? CFDictionaryGetValue(content_sim
, key
) : NULL
;
115 // if we need to remove the "sim" content
116 cache_SCDynamicStoreRemoveValue(store_sim
, key
);
118 // if no "sim" content to remove, just notify
119 cache_SCDynamicStoreNotifyValue(store_sim
, key
);
123 cache_write(store_sim
);
127 if ((info
== NULL
) && (content_host
!= NULL
)) {
128 CFRelease(content_host
);
130 if (content_sim
!= NULL
) {
131 CFRelease(content_sim
);
144 mirror_keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
145 mirror_patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
147 // Plugin:InterfaceNamer
148 key
= SCDynamicStoreKeyCreate(NULL
,
149 CFSTR("%@" "InterfaceNamer"),
150 kSCDynamicStoreDomainPlugin
);
151 CFArrayAppendValue(mirror_keys
, key
);
155 // key = SCDynamicStoreKeyCreateComputerName(NULL);
156 // CFArrayAppendValue(mirror_keys, key);
160 // key = SCDynamicStoreKeyCreateHostNames(NULL);
161 // CFArrayAppendValue(mirror_keys, key);
164 // Setup:/Network/Global/.*
165 pattern
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
166 kSCDynamicStoreDomainSetup
,
168 CFArrayAppendValue(mirror_patterns
, pattern
);
171 // Setup:/Network/Service/.*
172 // State:/Network/Service/.*
173 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
177 CFArrayAppendValue(mirror_patterns
, pattern
);
180 // Setup:/Network/Interface/.*
181 // State:/Network/Interface/.*
182 pattern
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
186 CFArrayAppendValue(mirror_patterns
, pattern
);
189 // State:/Network/MulticastDNS
190 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
191 kSCDynamicStoreDomainState
,
193 CFSTR(kDNSServiceCompMulticastDNS
));
194 CFArrayAppendValue(mirror_keys
, key
);
197 // State:/Network/PrivateDNS
198 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
199 kSCDynamicStoreDomainState
,
201 CFSTR(kDNSServiceCompPrivateDNS
));
202 CFArrayAppendValue(mirror_keys
, key
);
214 prime_SimulatorSupport()
216 CFDictionaryRef content_host
;
219 SC_log(LOG_DEBUG
, "prime() called");
221 // copy current content from base OS store to _Sim store
222 content_host
= SCDynamicStoreCopyMultiple(store_host
, mirror_keys
, mirror_patterns
);
223 CFRelease(mirror_keys
);
225 CFRelease(mirror_patterns
);
226 mirror_patterns
= NULL
;
228 if (content_host
== NULL
) {
232 n
= CFDictionaryGetCount(content_host
);
235 const void * keys_host_q
[N_QUICK
];
236 const void ** keys_host
= keys_host_q
;
238 if (n
> (CFIndex
)(sizeof(keys_host_q
) / sizeof(CFStringRef
))) {
239 keys_host
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
242 CFDictionaryGetKeysAndValues(content_host
, keys_host
, NULL
);
244 changes
= CFArrayCreate(NULL
, keys_host
, n
, &kCFTypeArrayCallBacks
);
245 mirror(store_host
, changes
, (void *)content_host
);
248 if (keys_host
!= keys_host_q
) {
249 CFAllocatorDeallocate(NULL
, keys_host
);
253 CFRelease(content_host
);
260 load_SimulatorSupport(CFBundleRef bundle
, Boolean bundleVerbose
)
262 #pragma unused(bundleVerbose)
264 CFMutableDictionaryRef options
;
265 CFRunLoopSourceRef rls
;
267 SC_log(LOG_DEBUG
, "load() called");
268 SC_log(LOG_DEBUG
, " bundle ID = %@", CFBundleGetIdentifier(bundle
));
273 // setup SCDynamicStore mirroring (from "host")
274 options
= CFDictionaryCreateMutable(NULL
,
276 &kCFTypeDictionaryKeyCallBacks
,
277 &kCFTypeDictionaryValueCallBacks
);
278 CFDictionarySetValue(options
, kSCDynamicStoreUseHostServer
, kCFBooleanTrue
);
279 store_host
= SCDynamicStoreCreateWithOptions(NULL
,
280 CFSTR("SimulatorSupport(host)"),
285 assert(store_host
!= NULL
);
287 ok
= SCDynamicStoreSetNotificationKeys(store_host
, mirror_keys
, mirror_patterns
);
290 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store_host
, 0);
293 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
296 // setup SCDynamicStore mirroring (to "iOS Simulator")
297 store_sim
= SCDynamicStoreCreate(NULL
,
298 CFSTR("SimulatorSupport(sim)"),
310 #pragma mark Standalone test code
314 main(int argc
, char **argv
)
317 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
319 load_SimulatorSupport(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
320 prime_SimulatorSupport();
328 #endif // TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC