2 * Copyright (c) 2013, 2015, 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 * March 15, 2013 Allan Nathanson <ajn@apple.com>
32 #include <SystemConfiguration/SystemConfiguration.h>
33 #include <SystemConfiguration/SCPrivate.h>
34 #include <SystemConfiguration/SCValidation.h>
37 #include <dns_sd_private.h>
42 static CFMutableArrayRef mirror_keys
= NULL
;
43 static CFMutableArrayRef mirror_patterns
= NULL
;
44 static SCDynamicStoreRef store_host
= NULL
;
45 static SCDynamicStoreRef store_sim
= NULL
;
55 __private_extern__ os_log_t
56 __log_SimulatorSupport()
58 static os_log_t log
= NULL
;
61 log
= os_log_create("com.apple.SystemConfiguration", "SimulatorSupport");
69 #pragma mark iOS Simulator Support
73 mirror(SCDynamicStoreRef store
, CFArrayRef changes
, void *info
)
75 CFDictionaryRef content_host
;
76 CFDictionaryRef content_sim
;
80 n
= CFArrayGetCount(changes
);
86 // capture "host" content
88 content_host
= SCDynamicStoreCopyMultiple(store_host
, changes
, NULL
);
90 content_host
= (CFDictionaryRef
)info
;
93 // capture [current] "sim" content
94 content_sim
= SCDynamicStoreCopyMultiple(store_sim
, changes
, NULL
);
98 for (i
= 0; i
< n
; i
++) {
100 CFPropertyListRef val
;
102 key
= CFArrayGetValueAtIndex(changes
, i
);
103 val
= (content_host
!= NULL
) ? CFDictionaryGetValue(content_host
, key
) : NULL
;
105 // if "host" content changed
106 cache_SCDynamicStoreSetValue(store_sim
, key
, val
);
108 // if no "host" content
109 val
= (content_sim
!= NULL
) ? CFDictionaryGetValue(content_sim
, key
) : NULL
;
111 // if we need to remove the "sim" content
112 cache_SCDynamicStoreRemoveValue(store_sim
, key
);
114 // if no "sim" content to remove, just notify
115 cache_SCDynamicStoreNotifyValue(store_sim
, key
);
119 cache_write(store_sim
);
123 if ((info
== NULL
) && (content_host
!= NULL
)) {
124 CFRelease(content_host
);
126 if (content_sim
!= NULL
) {
127 CFRelease(content_sim
);
140 mirror_keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
141 mirror_patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
143 // Plugin:InterfaceNamer
144 key
= SCDynamicStoreKeyCreate(NULL
,
145 CFSTR("%@" "InterfaceNamer"),
146 kSCDynamicStoreDomainPlugin
);
147 CFArrayAppendValue(mirror_keys
, key
);
151 // key = SCDynamicStoreKeyCreateComputerName(NULL);
152 // CFArrayAppendValue(mirror_keys, key);
156 // key = SCDynamicStoreKeyCreateHostNames(NULL);
157 // CFArrayAppendValue(mirror_keys, key);
160 // Setup:/Network/Global/.*
161 pattern
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
162 kSCDynamicStoreDomainSetup
,
164 CFArrayAppendValue(mirror_patterns
, pattern
);
167 // Setup:/Network/Service/.*
168 // State:/Network/Service/.*
169 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
173 CFArrayAppendValue(mirror_patterns
, pattern
);
176 // Setup:/Network/Interface/.*
177 // State:/Network/Interface/.*
178 pattern
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
182 CFArrayAppendValue(mirror_patterns
, pattern
);
185 // State:/Network/MulticastDNS
186 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
187 kSCDynamicStoreDomainState
,
189 CFSTR(kDNSServiceCompMulticastDNS
));
190 CFArrayAppendValue(mirror_keys
, key
);
193 // State:/Network/PrivateDNS
194 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
195 kSCDynamicStoreDomainState
,
197 CFSTR(kDNSServiceCompPrivateDNS
));
198 CFArrayAppendValue(mirror_keys
, key
);
210 prime_SimulatorSupport()
212 CFDictionaryRef content_host
;
215 SC_log(LOG_DEBUG
, "prime() called");
217 // copy current content from base OS store to _Sim store
218 content_host
= SCDynamicStoreCopyMultiple(store_host
, mirror_keys
, mirror_patterns
);
219 CFRelease(mirror_keys
);
221 CFRelease(mirror_patterns
);
222 mirror_patterns
= NULL
;
224 if (content_host
== NULL
) {
228 n
= CFDictionaryGetCount(content_host
);
231 const void * keys_host_q
[N_QUICK
];
232 const void ** keys_host
= keys_host_q
;
234 if (n
> (CFIndex
)(sizeof(keys_host_q
) / sizeof(CFStringRef
))) {
235 keys_host
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
238 CFDictionaryGetKeysAndValues(content_host
, keys_host
, NULL
);
240 changes
= CFArrayCreate(NULL
, keys_host
, n
, &kCFTypeArrayCallBacks
);
241 mirror(store_host
, changes
, (void *)content_host
);
244 if (keys_host
!= keys_host_q
) {
245 CFAllocatorDeallocate(NULL
, keys_host
);
249 CFRelease(content_host
);
256 load_SimulatorSupport(CFBundleRef bundle
, Boolean bundleVerbose
)
259 CFMutableDictionaryRef options
;
260 CFRunLoopSourceRef rls
;
262 SC_log(LOG_DEBUG
, "load() called");
263 SC_log(LOG_DEBUG
, " bundle ID = %@", CFBundleGetIdentifier(bundle
));
268 // setup SCDynamicStore mirroring (from "host")
269 options
= CFDictionaryCreateMutable(NULL
,
271 &kCFTypeDictionaryKeyCallBacks
,
272 &kCFTypeDictionaryValueCallBacks
);
273 CFDictionarySetValue(options
, kSCDynamicStoreUseHostServer
, kCFBooleanTrue
);
274 store_host
= SCDynamicStoreCreateWithOptions(NULL
,
275 CFSTR("SimulatorSupport(host)"),
280 assert(store_host
!= NULL
);
282 ok
= SCDynamicStoreSetNotificationKeys(store_host
, mirror_keys
, mirror_patterns
);
285 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store_host
, 0);
288 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
291 // setup SCDynamicStore mirroring (to "iOS Simulator")
292 store_sim
= SCDynamicStoreCreate(NULL
,
293 CFSTR("SimulatorSupport(sim)"),
305 #pragma mark Standalone test code
309 main(int argc
, char **argv
)
312 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
314 load_SimulatorSupport(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
315 prime_SimulatorSupport();