Skip to content

Commit 025fb70

Browse files
committed
Move binaries to examples directory
1 parent 122586c commit 025fb70

File tree

4 files changed

+132
-126
lines changed

4 files changed

+132
-126
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
extern crate system_configuration;
2+
3+
extern crate core_foundation;
4+
5+
use core_foundation::array::CFArray;
6+
use core_foundation::base::TCFType;
7+
use core_foundation::dictionary::CFDictionary;
8+
use core_foundation::propertylist::CFPropertyList;
9+
use core_foundation::string::{CFString, CFStringRef};
10+
11+
use system_configuration::dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder};
12+
13+
// This example will change the DNS settings on the primary
14+
// network interface to 8.8.8.8 and 8.8.4.4
15+
16+
fn 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+
36+
fn 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+
if let Some(dictionary) = dictionary {
41+
dictionary
42+
.find2(&CFString::from_static_string("PrimaryService"))
43+
.map(|ptr| unsafe { CFString::wrap_under_get_rule(ptr as CFStringRef) })
44+
} else {
45+
None
46+
}
47+
}
48+
49+
fn create_dns_dictionary(addresses: &[CFString]) -> CFDictionary {
50+
let key = CFString::from_static_string("ServerAddresses");
51+
let value = CFArray::from_CFTypes(addresses);
52+
CFDictionary::from_CFType_pairs(&[(key, value)])
53+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
extern crate system_configuration;
2+
3+
extern crate core_foundation;
4+
5+
use core_foundation::array::CFArray;
6+
use core_foundation::base::{CFType, TCFType};
7+
use core_foundation::dictionary::CFDictionary;
8+
use core_foundation::propertylist::CFPropertyList;
9+
use core_foundation::runloop::{CFRunLoop, kCFRunLoopCommonModes};
10+
use core_foundation::string::CFString;
11+
12+
use system_configuration::dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder,
13+
SCDynamicStoreCallBackContext};
14+
15+
// This example will watch the dynamic store for changes to any DNS setting. As soon as a change
16+
// is detected, it will be printed to stdout.
17+
18+
fn main() {
19+
let callback_context = SCDynamicStoreCallBackContext {
20+
callout: my_callback,
21+
info: Context { call_count: 0 },
22+
};
23+
24+
let store = SCDynamicStoreBuilder::new("my-watch-dns-store")
25+
.callback_context(callback_context)
26+
.build();
27+
28+
let watch_keys: CFArray<CFString> = CFArray::from_CFTypes(&[]);
29+
let watch_patterns =
30+
CFArray::from_CFTypes(&[CFString::from("(State|Setup):/Network/Service/.*/DNS")]);
31+
32+
if store.set_notification_keys(&watch_keys, &watch_patterns) {
33+
println!("Registered for notifications");
34+
} else {
35+
panic!("Unable to register notifications");
36+
}
37+
38+
let run_loop_source = store.create_run_loop_source();
39+
let run_loop = CFRunLoop::get_current();
40+
run_loop.add_source(&run_loop_source, unsafe { kCFRunLoopCommonModes });
41+
42+
println!("Entering run loop");
43+
CFRunLoop::run_current();
44+
}
45+
46+
/// This struct acts as a user provided context/payload to each notification callback.
47+
/// Here one can store any type of data or state needed in the callback function.
48+
#[derive(Debug)]
49+
struct Context {
50+
call_count: u64,
51+
}
52+
53+
fn my_callback(store: SCDynamicStore, changed_keys: CFArray<CFString>, context: &mut Context) {
54+
context.call_count += 1;
55+
println!("Callback call count: {}", context.call_count);
56+
57+
for key in changed_keys.iter() {
58+
if let Some(addresses) = get_dns(&store, key.clone()) {
59+
let addresses = addresses.iter().map(|s| s.to_string()).collect::<Vec<_>>();
60+
println!("{} changed DNS to {:?}", *key, addresses);
61+
} else {
62+
println!("{} removed DNS", *key);
63+
}
64+
}
65+
}
66+
67+
fn get_dns(store: &SCDynamicStore, path: CFString) -> Option<CFArray<CFString>> {
68+
let dictionary = store
69+
.get(path)
70+
.and_then(CFPropertyList::downcast_into::<CFDictionary>);
71+
if let Some(dictionary) = dictionary {
72+
dictionary
73+
.find2(&CFString::from_static_string("ServerAddresses"))
74+
.map(|ptr| unsafe { CFType::wrap_under_get_rule(ptr) })
75+
.and_then(CFType::downcast_into::<CFArray<CFString>>)
76+
} else {
77+
None
78+
}
79+
}

system-configuration/src/bin/set_dns.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

system-configuration/src/bin/watch_dns.rs

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)