Skip to content

Commit a75deee

Browse files
author
luozijun
committed
Add High-level API
1 parent 1356ce1 commit a75deee

File tree

6 files changed

+590
-43
lines changed

6 files changed

+590
-43
lines changed

system-configuration/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ readme = "../README.md"
1111

1212
[dependencies]
1313
core-foundation = "0.5"
14-
1514
system-configuration-sys = { path = "../system-configuration-sys", version = "0.1" }
15+
16+
[features]
17+
nightly = []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extern crate core_foundation;
2+
extern crate system_configuration;
3+
4+
use core_foundation::base::kCFAllocatorDefault;
5+
6+
use system_configuration::dynamic_store::SCDynamicStoreBuilder;
7+
use system_configuration::network_configuration::{global_router, SCNetworkInterface,
8+
SCNetworkService};
9+
use system_configuration::preferences::SCPreferences;
10+
11+
// This example will output network-global-service, network-global-interface, network-global-router,
12+
// network-service-order-list, network-services and network-interfaces to stdout.
13+
14+
fn main() {
15+
let session_name = "session_name";
16+
let prefs = SCPreferences::new(unsafe { kCFAllocatorDefault }, session_name, None);
17+
let store = SCDynamicStoreBuilder::new(session_name).build();
18+
19+
let service = SCNetworkService::global(&prefs, &store).unwrap();
20+
println!("Global Service:\n{:?}\n", service);
21+
println!("Global Interface:\n{:?}\n", service.interface());
22+
println!("Global Service Router:\n{:?}\n", global_router(&store));
23+
24+
println!("\n-listnetworkserviceorder:");
25+
for service in SCNetworkService::list_order(&prefs) {
26+
println!("{:?}", service);
27+
}
28+
29+
println!("\n-listallnetworkservices:");
30+
for service in SCNetworkService::list(&prefs) {
31+
println!("{:?}", service);
32+
}
33+
34+
println!("\n-listallnetworkinterface:");
35+
for interface in SCNetworkInterface::list() {
36+
println!("{:?}", interface);
37+
}
38+
}
+54-41
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,66 @@
1+
extern crate core_foundation;
12
extern crate system_configuration;
23

3-
extern crate core_foundation;
4+
use core_foundation::base::kCFAllocatorDefault;
5+
6+
7+
use system_configuration::dynamic_store::SCDynamicStoreBuilder;
8+
use system_configuration::network_configuration::SCNetworkService;
9+
use system_configuration::preferences::SCPreferences;
410

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};
1011

11-
use system_configuration::dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder};
12+
use std::net::{IpAddr, Ipv4Addr};
13+
1214

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

18+
// Usage:
19+
20+
// $ cargo build --example set_dns
21+
// $ sudo ../target/debug/examples/set_dns
22+
1623
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-
}
24+
let addrs = vec![
25+
IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)),
26+
IpAddr::V4(Ipv4Addr::new(8, 8, 4, 4)),
27+
];
3528

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-
}
29+
let session_name = "session_name";
30+
let store = SCDynamicStoreBuilder::new(session_name).build();
31+
let prefs = SCPreferences::new(unsafe { kCFAllocatorDefault }, session_name, None);
32+
33+
let global_service =
34+
SCNetworkService::global(&prefs, &store).expect("No PrimaryService active");
35+
let global_interface = global_service
36+
.interface()
37+
.expect("No PrimaryInterface active");
38+
39+
println!("Global Service:");
40+
println!("\tid: {:?}", global_service.id());
41+
println!("\tname: {:?}", global_service.name());
42+
println!("\tenabled: {:?}", global_service.enabled());
43+
println!("\tdns: {:?}", global_service.dns(&store));
44+
println!("\tinterface: {:?}", global_interface.name().unwrap());
45+
46+
println!(
47+
"Set dns to {:?} on {:?} service ...",
48+
addrs,
49+
global_service.name()
50+
);
51+
52+
53+
println!(
54+
"Success: {:?}",
55+
global_service.set_dns_server_addresses(&store, Some(addrs))
56+
);
57+
58+
// Check
59+
// `networksetup -getdnsservers "Wi-Fi"` Or `scutil --dns` Or `dig`
60+
println!("{:?}", global_service.dns(&store));
4861

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)])
62+
println!(
63+
"\n\nUse Command `networksetup -setdnsservers \"{}\" \"Empty\"` to restore DNS setting. ",
64+
global_service.name()
65+
);
5366
}

system-configuration/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
//!
1717
//! [SystemConfiguration]: https://developer.apple.com/documentation/systemconfiguration?language=objc
1818
//! [`system-configuration-sys`]: https://crates.io/crates/system-configuration-sys
19-
19+
#![cfg_attr(all(feature = "nightly", test), feature(test))]
2020
#![deny(missing_docs)]
2121

2222
#[macro_use]
2323
extern crate core_foundation;
2424
extern crate system_configuration_sys;
25+
#[cfg(all(feature = "nightly", test))]
26+
extern crate test;
2527

2628
pub mod dynamic_store;
29+
pub mod preferences;
30+
pub mod network_configuration;

0 commit comments

Comments
 (0)