1use core_foundation::{
2 array::CFArray,
3 base::{TCFType, ToVoid},
4 dictionary::CFDictionary,
5 propertylist::CFPropertyList,
6 string::{CFString, CFStringRef},
7};
8use system_configuration::{
9 dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder},
10 sys::schema_definitions::{kSCDynamicStorePropNetPrimaryService, kSCPropNetDNSServerAddresses},
11};
12
13fn main() {
17 let store = SCDynamicStoreBuilder::new("my-test-dyn-store").build();
18 let primary_service_uuid = get_primary_service_uuid(&store).expect("No PrimaryService active");
19 println!("PrimaryService UUID: {}", primary_service_uuid);
20
21 let primary_service_path = CFString::new(&format!(
22 "State:/Network/Service/{}/DNS",
23 primary_service_uuid
24 ));
25 println!("PrimaryService path: {}", primary_service_path);
26
27 let dns_dictionary = create_dns_dictionary(&[
28 CFString::from_static_string("8.8.8.8"),
29 CFString::from_static_string("8.8.4.4"),
30 ]);
31
32 let success = store.set(primary_service_path, dns_dictionary);
33 println!("success? {}", success);
34}
35
36fn get_primary_service_uuid(store: &SCDynamicStore) -> Option<CFString> {
37 let dictionary = store
38 .get("State:/Network/Global/IPv4")
39 .and_then(CFPropertyList::downcast_into::<CFDictionary>)?;
40 dictionary
41 .find(unsafe { kSCDynamicStorePropNetPrimaryService }.to_void())
42 .map(|ptr| unsafe { CFString::wrap_under_get_rule(*ptr as CFStringRef) })
43}
44
45fn create_dns_dictionary(addresses: &[CFString]) -> CFDictionary {
46 let key = unsafe { CFString::wrap_under_get_rule(kSCPropNetDNSServerAddresses) };
47 let value = CFArray::from_CFTypes(addresses);
48 let typed_dict = CFDictionary::from_CFType_pairs(&[(key, value)]);
49 unsafe { CFDictionary::wrap_under_get_rule(typed_dict.as_concrete_TypeRef()) }
50}