-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luozijun
committed
Feb 21, 2018
1 parent
1356ce1
commit c41a1a5
Showing
5 changed files
with
481 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.