Skip to content

Commit 7e8771b

Browse files
luozijunLuoZijun
luozijun
authored andcommitted
Add High-level API
1 parent 368db5c commit 7e8771b

File tree

6 files changed

+543
-44
lines changed

6 files changed

+543
-44
lines changed

system-configuration/Cargo.toml

Lines changed: 3 additions & 1 deletion
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 = []
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate core_foundation;
2+
extern crate system_configuration;
3+
4+
use core_foundation::string::CFString;
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 cf_session_name = CFString::new(&session_name);
17+
18+
let prefs = SCPreferences::default(&cf_session_name);
19+
let store = SCDynamicStoreBuilder::new(session_name).build();
20+
21+
let service = SCNetworkService::global(&prefs, &store).unwrap();
22+
println!("Global Service:\n{:?}\n", service);
23+
println!("Global Interface:\n{:?}\n", service.interface());
24+
println!("Global Service Router:\n{:?}\n", global_router(&store));
25+
26+
println!("\n-listnetworkserviceorder:");
27+
for service in SCNetworkService::list_order(&prefs) {
28+
println!("{:?}", service);
29+
}
30+
31+
println!("\n-listallnetworkservices:");
32+
for service in SCNetworkService::list(&prefs) {
33+
println!("{:?}", service);
34+
}
35+
36+
println!("\n-listallnetworkinterface:");
37+
for interface in SCNetworkInterface::list() {
38+
println!("{:?}", interface);
39+
}
40+
}
Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,67 @@
1+
extern crate core_foundation;
12
extern crate system_configuration;
23

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

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

system-configuration/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +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;
2729
pub mod preferences;
30+
pub mod network_configuration;

0 commit comments

Comments
 (0)