|
| 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 | +} |
0 commit comments