Skip to content

Commit b396f6d

Browse files
committed
Add temporary watch_dns bin and update set_dns
1 parent 6754c94 commit b396f6d

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

system-configuration/src/bin/set_dns.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use core_foundation::base::TCFType;
99
use core_foundation::dictionary::CFDictionary;
1010
use core_foundation::string::{CFString, CFStringRef};
1111

12-
use system_configuration::dynamic_store::SCDynamicStore;
12+
use system_configuration::dynamic_store::SCDynamicStoreBuilder;
1313

1414
fn main() {
1515
unsafe {
16-
let store = SCDynamicStore::create("my-test-dyn-store");
16+
let store = SCDynamicStoreBuilder::new("my-test-dyn-store").build();
1717
println!("Created dynamic store");
1818

1919
let ipv4_dict = store
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
extern crate system_configuration;
2+
extern crate system_configuration_sys;
3+
4+
extern crate core_foundation;
5+
extern crate core_foundation_sys;
6+
7+
8+
use core_foundation::array::CFArray;
9+
use core_foundation::dictionary::CFDictionary;
10+
use core_foundation::runloop::CFRunLoop;
11+
use core_foundation::string::CFString;
12+
use core_foundation_sys::runloop::kCFRunLoopCommonModes;
13+
14+
use system_configuration::dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder,
15+
SCDynamicStoreCallBackContext};
16+
17+
use std::env;
18+
19+
#[derive(Debug)]
20+
struct Payload {
21+
i: u64,
22+
service_path: CFString,
23+
}
24+
25+
impl Drop for Payload {
26+
fn drop(&mut self) {
27+
println!("Payload Drop");
28+
}
29+
}
30+
31+
fn main() {
32+
let service_id = env::args()
33+
.skip(1)
34+
.next()
35+
.expect("Give service uuid as first argument");
36+
let service_path = CFString::from(&format!("State:/Network/Service/{}/DNS", service_id)[..]);
37+
38+
let watch_keys = CFArray::from_CFTypes(&[service_path.clone()]);
39+
let watch_patterns: CFArray<CFString> = CFArray::from_CFTypes(&[]);
40+
41+
let callback_context = SCDynamicStoreCallBackContext {
42+
callout: my_callback,
43+
info: Payload {
44+
i: 0,
45+
service_path: service_path,
46+
},
47+
};
48+
49+
let store = SCDynamicStoreBuilder::new("my-watch-dns-store")
50+
.callback_context(callback_context)
51+
.build();
52+
println!("Created dynamic store");
53+
54+
if store.set_notification_keys(&watch_keys, &watch_patterns) {
55+
println!("Registered for notifications");
56+
} else {
57+
panic!("Unable to register notifications");
58+
}
59+
let run_loop_source = store.create_run_loop_source();
60+
61+
let run_loop = CFRunLoop::get_current();
62+
run_loop.add_source(&run_loop_source, unsafe { kCFRunLoopCommonModes });
63+
println!("Entering run loop");
64+
CFRunLoop::run_current();
65+
}
66+
67+
fn my_callback(store: SCDynamicStore, _changed_keys: CFArray<CFString>, payload: &mut Payload) {
68+
println!("my_callback2 (payload: {:?})", payload);
69+
payload.i += 1;
70+
71+
if payload.i > 1 {
72+
// Only reset DNS on first callback for now. To not get stuck in infinite loop.
73+
return;
74+
}
75+
76+
let server_addresses_key = CFString::from_static_string("ServerAddresses");
77+
let server_address_1 = CFString::from_static_string("192.168.1.1");
78+
let server_addresses_value = CFArray::from_CFTypes(&[server_address_1]);
79+
80+
let dns_dictionary =
81+
CFDictionary::from_CFType_pairs(&[(server_addresses_key, server_addresses_value)]);
82+
83+
let success = store.set(payload.service_path.clone(), &dns_dictionary);
84+
println!("callback: {}", success);
85+
}

0 commit comments

Comments
 (0)