2 * Copyright (c) 2013, 2015-2017 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
)
76 CFDictionaryRef content_host
;
77 CFDictionaryRef content_sim
;
81 n
= CFArrayGetCount(changes
);
87 // capture "host" content
89 content_host
= SCDynamicStoreCopyMultiple(store_host
, changes
, NULL
);
91 content_host
= (CFDictionaryRef
)info
;
94 // capture [current] "sim" content
95 content_sim
= SCDynamicStoreCopyMultiple(store_sim
, changes
, NULL
);
99 for (i
= 0; i
< n
; i
++) {
101 CFPropertyListRef val
;
103 key
= CFArrayGetValueAtIndex(changes
, i
);
104 val
= (content_host
!= NULL
) ? CFDictionaryGetValue(content_host
, key
) : NULL
;
106 // if "host" content changed
107 cache_SCDynamicStoreSetValue(store_sim
, key
, val
);
109 // if no "host" content
110 val
= (content_sim
!= NULL
) ? CFDictionaryGetValue(content_sim
, key
) : NULL
;
112 // if we need to remove the "sim" content
113 cache_SCDynamicStoreRemoveValue(store_sim
, key
);
115 // if no "sim" content to remove, just notify
116 cache_SCDynamicStoreNotifyValue(store_sim
, key
);
120 cache_write(store_sim
);
124 if ((info
== NULL
) && (content_host
!= NULL
)) {
125 CFRelease(content_host
);
127 if (content_sim
!= NULL
) {
128 CFRelease(content_sim
);
141 mirror_keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
142 mirror_patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
144 // Plugin:InterfaceNamer
145 key
= SCDynamicStoreKeyCreate(NULL
,
146 CFSTR("%@" "InterfaceNamer"),
147 kSCDynamicStoreDomainPlugin
);
148 CFArrayAppendValue(mirror_keys
, key
);
152 // key = SCDynamicStoreKeyCreateComputerName(NULL);
153 // CFArrayAppendValue(mirror_keys, key);
157 // key = SCDynamicStoreKeyCreateHostNames(NULL);
158 // CFArrayAppendValue(mirror_keys, key);
161 // Setup:/Network/Global/.*
162 pattern
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
163 kSCDynamicStoreDomainSetup
,
165 CFArrayAppendValue(mirror_patterns
, pattern
);
168 // Setup:/Network/Service/.*
169 // State:/Network/Service/.*
170 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
174 CFArrayAppendValue(mirror_patterns
, pattern
);
177 // Setup:/Network/Interface/.*
178 // State:/Network/Interface/.*
179 pattern
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
183 CFArrayAppendValue(mirror_patterns
, pattern
);
186 // State:/Network/MulticastDNS
187 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
188 kSCDynamicStoreDomainState
,
190 CFSTR(kDNSServiceCompMulticastDNS
));
191 CFArrayAppendValue(mirror_keys
, key
);
194 // State:/Network/PrivateDNS
195 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
196 kSCDynamicStoreDomainState
,
198 CFSTR(kDNSServiceCompPrivateDNS
));
199 CFArrayAppendValue(mirror_keys
, key
);
211 prime_SimulatorSupport()
213 CFDictionaryRef content_host
;
216 SC_log(LOG_DEBUG
, "prime() called");
218 // copy current content from base OS store to _Sim store
219 content_host
= SCDynamicStoreCopyMultiple(store_host
, mirror_keys
, mirror_patterns
);
220 CFRelease(mirror_keys
);
222 CFRelease(mirror_patterns
);
223 mirror_patterns
= NULL
;
225 if (content_host
== NULL
) {
229 n
= CFDictionaryGetCount(content_host
);
232 const void * keys_host_q
[N_QUICK
];
233 const void ** keys_host
= keys_host_q
;
235 if (n
> (CFIndex
)(sizeof(keys_host_q
) / sizeof(CFStringRef
))) {
236 keys_host
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
239 CFDictionaryGetKeysAndValues(content_host
, keys_host
, NULL
);
241 changes
= CFArrayCreate(NULL
, keys_host
, n
, &kCFTypeArrayCallBacks
);
242 mirror(store_host
, changes
, (void *)content_host
);
245 if (keys_host
!= keys_host_q
) {
246 CFAllocatorDeallocate(NULL
, keys_host
);
250 CFRelease(content_host
);
257 load_SimulatorSupport(CFBundleRef bundle
, Boolean bundleVerbose
)
259 #pragma unused(bundleVerbose)
261 CFMutableDictionaryRef options
;
262 CFRunLoopSourceRef rls
;
264 SC_log(LOG_DEBUG
, "load() called");
265 SC_log(LOG_DEBUG
, " bundle ID = %@", CFBundleGetIdentifier(bundle
));
270 // setup SCDynamicStore mirroring (from "host")
271 options
= CFDictionaryCreateMutable(NULL
,
273 &kCFTypeDictionaryKeyCallBacks
,
274 &kCFTypeDictionaryValueCallBacks
);
275 CFDictionarySetValue(options
, kSCDynamicStoreUseHostServer
, kCFBooleanTrue
);
276 store_host
= SCDynamicStoreCreateWithOptions(NULL
,
277 CFSTR("SimulatorSupport(host)"),
282 assert(store_host
!= NULL
);
284 ok
= SCDynamicStoreSetNotificationKeys(store_host
, mirror_keys
, mirror_patterns
);
287 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store_host
, 0);
290 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
293 // setup SCDynamicStore mirroring (to "iOS Simulator")
294 store_sim
= SCDynamicStoreCreate(NULL
,
295 CFSTR("SimulatorSupport(sim)"),
307 #pragma mark Standalone test code
311 main(int argc
, char **argv
)
314 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
316 load_SimulatorSupport(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
317 prime_SimulatorSupport();