2 * Copyright (c) 2013 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 Boolean _verbose
= FALSE
;
48 static CFMutableArrayRef mirror_keys
= NULL
;
49 static CFMutableArrayRef mirror_patterns
= NULL
;
50 static SCDynamicStoreRef store_host
= NULL
;
51 static SCDynamicStoreRef store_sim
= NULL
;
55 #pragma mark iOS Simulator Support
59 mirror(SCDynamicStoreRef store
, CFArrayRef changes
, void *info
)
61 CFDictionaryRef content_host
;
62 CFDictionaryRef content_sim
;
66 n
= CFArrayGetCount(changes
);
72 // capture "host" content
74 content_host
= SCDynamicStoreCopyMultiple(store_host
, changes
, NULL
);
76 content_host
= (CFDictionaryRef
)info
;
79 // capture [current] "sim" content
80 content_sim
= SCDynamicStoreCopyMultiple(store_sim
, changes
, NULL
);
84 for (i
= 0; i
< n
; i
++) {
86 CFPropertyListRef val
;
88 key
= CFArrayGetValueAtIndex(changes
, i
);
89 val
= (content_host
!= NULL
) ? CFDictionaryGetValue(content_host
, key
) : NULL
;
91 // if "host" content changed
92 cache_SCDynamicStoreSetValue(store_sim
, key
, val
);
94 // if no "host" content
95 val
= (content_sim
!= NULL
) ? CFDictionaryGetValue(content_sim
, key
) : NULL
;
97 // if we need to remove the "sim" content
98 cache_SCDynamicStoreRemoveValue(store_sim
, key
);
100 // if no "sim" content to remove, just notify
101 cache_SCDynamicStoreNotifyValue(store_sim
, key
);
105 cache_write(store_sim
);
109 if ((info
== NULL
) && (content_host
!= NULL
)) {
110 CFRelease(content_host
);
112 if (content_sim
!= NULL
) {
113 CFRelease(content_sim
);
126 mirror_keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
127 mirror_patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
129 // Plugin:InterfaceNamer
130 key
= SCDynamicStoreKeyCreate(NULL
,
131 CFSTR("%@" "InterfaceNamer"),
132 kSCDynamicStoreDomainPlugin
);
133 CFArrayAppendValue(mirror_keys
, key
);
137 // key = SCDynamicStoreKeyCreateComputerName(NULL);
138 // CFArrayAppendValue(mirror_keys, key);
142 // key = SCDynamicStoreKeyCreateHostNames(NULL);
143 // CFArrayAppendValue(mirror_keys, key);
146 // Setup:/Network/Global/.*
147 pattern
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
148 kSCDynamicStoreDomainSetup
,
150 CFArrayAppendValue(mirror_patterns
, pattern
);
153 // Setup:/Network/Service/.*
154 // State:/Network/Service/.*
155 pattern
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
159 CFArrayAppendValue(mirror_patterns
, pattern
);
162 // Setup:/Network/Interface/.*
163 // State:/Network/Interface/.*
164 pattern
= SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL
,
168 CFArrayAppendValue(mirror_patterns
, pattern
);
171 // State:/Network/MulticastDNS
172 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
173 kSCDynamicStoreDomainState
,
175 CFSTR(kDNSServiceCompMulticastDNS
));
176 CFArrayAppendValue(mirror_keys
, key
);
179 // State:/Network/PrivateDNS
180 key
= SCDynamicStoreKeyCreate(NULL
, CFSTR("%@/%@/%@"),
181 kSCDynamicStoreDomainState
,
183 CFSTR(kDNSServiceCompPrivateDNS
));
184 CFArrayAppendValue(mirror_keys
, key
);
196 prime_SimulatorSupport()
198 CFDictionaryRef content_host
;
201 SCLog(_verbose
, LOG_DEBUG
, CFSTR("prime() called"));
203 // copy current content from base OS store to _Sim store
204 content_host
= SCDynamicStoreCopyMultiple(store_host
, mirror_keys
, mirror_patterns
);
205 CFRelease(mirror_keys
);
207 CFRelease(mirror_patterns
);
208 mirror_patterns
= NULL
;
210 if (content_host
== NULL
) {
214 n
= CFDictionaryGetCount(content_host
);
217 const void * keys_host_q
[N_QUICK
];
218 const void ** keys_host
= keys_host_q
;
220 if (n
> (CFIndex
)(sizeof(keys_host_q
) / sizeof(CFStringRef
))) {
221 keys_host
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
224 CFDictionaryGetKeysAndValues(content_host
, keys_host
, NULL
);
226 changes
= CFArrayCreate(NULL
, keys_host
, n
, &kCFTypeArrayCallBacks
);
227 mirror(store_host
, changes
, (void *)content_host
);
230 if (keys_host
!= keys_host_q
) {
231 CFAllocatorDeallocate(NULL
, keys_host
);
235 CFRelease(content_host
);
242 load_SimulatorSupport(CFBundleRef bundle
, Boolean bundleVerbose
)
245 CFMutableDictionaryRef options
;
246 CFRunLoopSourceRef rls
;
252 SCLog(_verbose
, LOG_DEBUG
, CFSTR("load() called"));
253 SCLog(_verbose
, LOG_DEBUG
, CFSTR(" bundle ID = %@"), CFBundleGetIdentifier(bundle
));
258 // setup SCDynamicStore mirroring (from "host")
259 options
= CFDictionaryCreateMutable(NULL
,
261 &kCFTypeDictionaryKeyCallBacks
,
262 &kCFTypeDictionaryValueCallBacks
);
263 CFDictionarySetValue(options
, kSCDynamicStoreUseHostServer
, kCFBooleanTrue
);
264 store_host
= SCDynamicStoreCreateWithOptions(NULL
,
265 CFSTR("SimulatorSupport(host)"),
270 assert(store_host
!= NULL
);
272 ok
= SCDynamicStoreSetNotificationKeys(store_host
, mirror_keys
, mirror_patterns
);
275 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store_host
, 0);
278 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
281 // setup SCDynamicStore mirroring (to "iOS Simulator")
282 store_sim
= SCDynamicStoreCreate(NULL
,
283 CFSTR("SimulatorSupport(sim)"),
295 #pragma mark Standalone test code
299 main(int argc
, char **argv
)
302 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
304 load_SimulatorSupport(CFBundleGetMainBundle(), (argc
> 1) ? TRUE
: FALSE
);
305 prime_SimulatorSupport();