Skip to content

Commit

Permalink
Add High-level API
Browse files Browse the repository at this point in the history
  • Loading branch information
luozijun committed Feb 21, 2018
1 parent 1356ce1 commit c41a1a5
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 44 deletions.
1 change: 0 additions & 1 deletion system-configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ readme = "../README.md"

[dependencies]
core-foundation = "0.5"

system-configuration-sys = { path = "../system-configuration-sys", version = "0.1" }
30 changes: 30 additions & 0 deletions system-configuration/examples/network_configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern crate system_configuration;

use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkInterface,
SCNetworkService};

// This example will output network-global-service, network-global-interface, network-global-router,
// network-service-order-list, network-services and network-interfaces to stdout.

fn main() {
println!("Global Service:\n{:?}\n", SCNetworkGlobal.service());

println!("Global Interface:\n{:?}\n", SCNetworkGlobal.interface());

println!("Global Service Router:\n{:?}\n", SCNetworkGlobal.router());

println!("\n-listnetworkserviceorder:");
for service in SCNetworkService::list_order() {
println!("{:?}", service);
}

println!("\n-listallnetworkservices:");
for service in SCNetworkService::list() {
println!("{:?}", service);
}

println!("\n-listallnetworkinterface:");
for interface in SCNetworkInterface::list() {
println!("{:?}", interface);
}
}
86 changes: 43 additions & 43 deletions system-configuration/examples/set_dns.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
extern crate system_configuration;

extern crate core_foundation;
use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkServiceDNS};

use core_foundation::array::CFArray;
use core_foundation::base::TCFType;
use core_foundation::dictionary::CFDictionary;
use core_foundation::propertylist::CFPropertyList;
use core_foundation::string::{CFString, CFStringRef};

use system_configuration::dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder};
use std::net::{IpAddr, Ipv4Addr};

// This example will change the DNS settings on the primary
// network interface to 8.8.8.8 and 8.8.4.4

fn main() {
let store = SCDynamicStoreBuilder::new("my-test-dyn-store").build();
let primary_service_uuid = get_primary_service_uuid(&store).expect("No PrimaryService active");
println!("PrimaryService UUID: {}", primary_service_uuid);

let primary_service_path = CFString::new(&format!(
"State:/Network/Service/{}/DNS",
primary_service_uuid
));
println!("PrimaryService path: {}", primary_service_path);

let dns_dictionary = create_dns_dictionary(&[
CFString::from_static_string("8.8.8.8"),
CFString::from_static_string("8.8.4.4"),
]);

let success = store.set(primary_service_path, dns_dictionary);
println!("success? {}", success);
}
// Usage:

fn get_primary_service_uuid(store: &SCDynamicStore) -> Option<CFString> {
let dictionary = store
.get("State:/Network/Global/IPv4")
.and_then(CFPropertyList::downcast_into::<CFDictionary>);
if let Some(dictionary) = dictionary {
dictionary
.find2(&CFString::from_static_string("PrimaryService"))
.map(|ptr| unsafe { CFString::wrap_under_get_rule(ptr as CFStringRef) })
} else {
None
}
}
// $ cargo build --example set_dns
// $ sudo ../target/debug/examples/set_dns

fn create_dns_dictionary(addresses: &[CFString]) -> CFDictionary {
let key = CFString::from_static_string("ServerAddresses");
let value = CFArray::from_CFTypes(addresses);
CFDictionary::from_CFType_pairs(&[(key, value)])
fn main() {
let addrs = vec![
IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)),
IpAddr::V4(Ipv4Addr::new(8, 8, 4, 4)),
];

let global_service = SCNetworkGlobal.service().expect("No PrimaryService active");
let global_interface = global_service
.interface()
.expect("No PrimaryInterface active");

println!("Global Service:");
println!("\tid: {:?}", global_service.id());
println!("\tname: {:?}", global_service.name());
println!("\tenabled: {:?}", global_service.enabled());
println!("\tdns: {:?}", global_service.dns());
println!("\tinterface: {:?}", global_interface.name().unwrap());

println!(
"Set dns to {:?} on {:?} service ...",
addrs,
global_service.name()
);

let dns = SCNetworkServiceDNS::new((None, None), (None, Some(addrs)));

println!("Success: {:?}", global_service.set_dns(dns));

// Check
// networksetup -getdnsservers "Wi-Fi"
// scutil --dns
// dig
println!("{:?}", global_service.dns());

println!(
"\n\nUse Command `networksetup -setdnsservers \"{}\" \"Empty\"` to restore DNS setting. ",
global_service.name()
);
}
1 change: 1 addition & 0 deletions system-configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ extern crate core_foundation;
extern crate system_configuration_sys;

pub mod dynamic_store;
pub mod network_configuration;
Loading

0 comments on commit c41a1a5

Please sign in to comment.