Skip to content

Commit c41a1a5

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

File tree

5 files changed

+481
-44
lines changed

5 files changed

+481
-44
lines changed

system-configuration/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ readme = "../README.md"
1111

1212
[dependencies]
1313
core-foundation = "0.5"
14-
1514
system-configuration-sys = { path = "../system-configuration-sys", version = "0.1" }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate system_configuration;
2+
3+
use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkInterface,
4+
SCNetworkService};
5+
6+
// This example will output network-global-service, network-global-interface, network-global-router,
7+
// network-service-order-list, network-services and network-interfaces to stdout.
8+
9+
fn main() {
10+
println!("Global Service:\n{:?}\n", SCNetworkGlobal.service());
11+
12+
println!("Global Interface:\n{:?}\n", SCNetworkGlobal.interface());
13+
14+
println!("Global Service Router:\n{:?}\n", SCNetworkGlobal.router());
15+
16+
println!("\n-listnetworkserviceorder:");
17+
for service in SCNetworkService::list_order() {
18+
println!("{:?}", service);
19+
}
20+
21+
println!("\n-listallnetworkservices:");
22+
for service in SCNetworkService::list() {
23+
println!("{:?}", service);
24+
}
25+
26+
println!("\n-listallnetworkinterface:");
27+
for interface in SCNetworkInterface::list() {
28+
println!("{:?}", interface);
29+
}
30+
}
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
extern crate system_configuration;
22

3-
extern crate core_foundation;
3+
use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkServiceDNS};
44

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};
10-
11-
use system_configuration::dynamic_store::{SCDynamicStore, SCDynamicStoreBuilder};
5+
use std::net::{IpAddr, Ipv4Addr};
126

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

16-
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-
}
10+
// Usage:
3511

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-
}
12+
// $ cargo build --example set_dns
13+
// $ sudo ../target/debug/examples/set_dns
4814

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)])
15+
fn main() {
16+
let addrs = vec![
17+
IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)),
18+
IpAddr::V4(Ipv4Addr::new(8, 8, 4, 4)),
19+
];
20+
21+
let global_service = SCNetworkGlobal.service().expect("No PrimaryService active");
22+
let global_interface = global_service
23+
.interface()
24+
.expect("No PrimaryInterface active");
25+
26+
println!("Global Service:");
27+
println!("\tid: {:?}", global_service.id());
28+
println!("\tname: {:?}", global_service.name());
29+
println!("\tenabled: {:?}", global_service.enabled());
30+
println!("\tdns: {:?}", global_service.dns());
31+
println!("\tinterface: {:?}", global_interface.name().unwrap());
32+
33+
println!(
34+
"Set dns to {:?} on {:?} service ...",
35+
addrs,
36+
global_service.name()
37+
);
38+
39+
let dns = SCNetworkServiceDNS::new((None, None), (None, Some(addrs)));
40+
41+
println!("Success: {:?}", global_service.set_dns(dns));
42+
43+
// Check
44+
// networksetup -getdnsservers "Wi-Fi"
45+
// scutil --dns
46+
// dig
47+
println!("{:?}", global_service.dns());
48+
49+
println!(
50+
"\n\nUse Command `networksetup -setdnsservers \"{}\" \"Empty\"` to restore DNS setting. ",
51+
global_service.name()
52+
);
5353
}

system-configuration/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ extern crate core_foundation;
2424
extern crate system_configuration_sys;
2525

2626
pub mod dynamic_store;
27+
pub mod network_configuration;

0 commit comments

Comments
 (0)