Skip to content

Commit 0a88465

Browse files
author
luozijun
committed
Update
1 parent 1bb3afb commit 0a88465

File tree

3 files changed

+98
-79
lines changed

3 files changed

+98
-79
lines changed

system-configuration/examples/network_configuration.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
extern crate system_configuration;
22

3-
use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkInterface,
4-
SCNetworkService};
3+
use system_configuration::network_configuration::{global_router, SCNetworkService, SCNetworkInterface};
4+
use system_configuration::dynamic_store::{SCDynamicStoreBuilder};
55

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

99
fn main() {
10-
println!("Global Service:\n{:?}\n", SCNetworkGlobal.service());
10+
let store = SCDynamicStoreBuilder::new("session_name").build();
11+
1112

12-
println!("Global Interface:\n{:?}\n", SCNetworkGlobal.interface());
13-
14-
println!("Global Service Router:\n{:?}\n", SCNetworkGlobal.router());
13+
println!("Global Service:\n{:?}\n", SCNetworkService::global(&store));
14+
println!("Global Interface:\n{:?}\n", SCNetworkInterface::global(&store));
15+
println!("Global Service Router:\n{:?}\n", global_router(&store));
1516

1617
println!("\n-listnetworkserviceorder:");
1718
for service in SCNetworkService::list_order() {

system-configuration/examples/set_dns.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate system_configuration;
22

3-
use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkServiceDNS};
3+
use system_configuration::network_configuration::{SCNetworkGlobal, SCNetworkServiceDns};
44

55
use std::net::{IpAddr, Ipv4Addr};
66

@@ -36,7 +36,7 @@ fn main() {
3636
global_service.name()
3737
);
3838

39-
let dns = SCNetworkServiceDNS::new((None, None), (None, Some(addrs)));
39+
let dns = SCNetworkServiceDns::new((None, None), (None, Some(addrs)));
4040

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

system-configuration/src/network_configuration.rs

Lines changed: 89 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core_foundation::base::kCFAllocatorDefault;
1818
use core_foundation::dictionary::CFDictionary;
1919
use core_foundation::string::{CFString, CFStringRef};
2020

21-
use dynamic_store::SCDynamicStoreBuilder;
21+
use dynamic_store::{SCDynamicStoreBuilder, SCDynamicStore};
2222
pub use system_configuration_sys::network_configuration::*;
2323
use system_configuration_sys::preferences::SCPreferencesCreate;
2424

@@ -27,33 +27,33 @@ use std::net::IpAddr;
2727

2828
/// MTU
2929
#[derive(Debug)]
30-
pub struct SCNetworkInterfaceMTU {
30+
pub struct SCNetworkInterfaceMtu {
3131
/// the current MTU setting for the interface.
3232
pub current: u32,
33-
/// the minimum MTU setting for the interface. If negative, the minimum setting could not
33+
/// the minimum MTU setting for the interface. If None, the minimum setting could not
3434
/// be determined.
3535
pub min: Option<u32>,
36-
/// the maximum MTU setting for the interface. If negative, the maximum setting could not
36+
/// the maximum MTU setting for the interface. If None, the maximum setting could not
3737
/// be determined.
3838
pub max: Option<u32>,
3939
}
4040

4141
/// DNS
4242
#[derive(Debug)]
43-
pub struct SCNetworkServiceDNS {
43+
pub struct SCNetworkServiceDns {
4444
state_domain_name: Option<String>,
4545
setup_domain_name: Option<String>,
4646
state_server_addresses: Option<Vec<IpAddr>>,
4747
setup_server_addresses: Option<Vec<IpAddr>>,
4848
}
4949

50-
impl SCNetworkServiceDNS {
50+
impl SCNetworkServiceDns {
5151
/// DNS Constructor
5252
pub fn new(
5353
domain_name: (Option<String>, Option<String>),
5454
server_addresses: (Option<Vec<IpAddr>>, Option<Vec<IpAddr>>),
55-
) -> SCNetworkServiceDNS {
56-
SCNetworkServiceDNS {
55+
) -> SCNetworkServiceDns {
56+
SCNetworkServiceDns {
5757
state_domain_name: domain_name.0,
5858
setup_domain_name: domain_name.1,
5959
state_server_addresses: server_addresses.0,
@@ -78,75 +78,64 @@ impl SCNetworkServiceDNS {
7878
}
7979
}
8080

81-
/// Global network object
82-
pub struct SCNetworkGlobal;
81+
fn global_query(store: &SCDynamicStore, key: &str) -> Option<String> {
82+
let path = CFString::from_static_string("State:/Network/Global/IPv4");
8383

84-
impl SCNetworkGlobal {
85-
fn query(session_name: &str, key: &str) -> Option<String> {
86-
let store = SCDynamicStoreBuilder::new(session_name).build();
87-
let path = CFString::from_static_string("State:/Network/Global/IPv4");
88-
89-
if let Some(value) = store.get(path.clone()) {
90-
if let Some(dict) = value.downcast_into::<CFDictionary>() {
91-
if let Some(val) = dict.find2(&CFString::new(key)) {
92-
let value = unsafe { CFType::wrap_under_get_rule(val) };
93-
if let Some(value) = value.downcast::<CFString>() {
94-
return Some(value.to_string());
95-
}
84+
if let Some(value) = store.get(path.clone()) {
85+
if let Some(dict) = value.downcast_into::<CFDictionary>() {
86+
if let Some(val) = dict.find2(&CFString::new(key)) {
87+
let value = unsafe { CFType::wrap_under_get_rule(val) };
88+
if let Some(value) = value.downcast::<CFString>() {
89+
return Some(value.to_string());
9690
}
9791
}
9892
}
99-
100-
return None;
10193
}
10294

103-
/// Returns primary network service
104-
pub fn service(&self) -> Option<SCNetworkService> {
105-
if let Some(service_id) = SCNetworkGlobal::query("ng_service", "PrimaryService") {
106-
for _service in SCNetworkService::list() {
107-
if _service.id() == service_id {
108-
return Some(_service);
109-
}
110-
}
111-
}
95+
return None;
96+
}
11297

113-
return None;
114-
}
11598

116-
/// Returns primary network interface
117-
pub fn interface(&self) -> Option<SCNetworkInterface> {
118-
if let Some(ifname) = SCNetworkGlobal::query("ng_interface", "PrimaryInterface") {
119-
for iface in SCNetworkInterface::list() {
120-
if let Some(bsd_name) = iface.bsd_name() {
121-
if bsd_name == ifname {
122-
return Some(iface);
123-
}
124-
}
125-
}
99+
/// Returns default route on primary network service.
100+
pub fn global_router(store: &SCDynamicStore) -> Option<IpAddr> {
101+
// let store = SCDynamicStoreBuilder::new(session_name).build();
102+
if let Some(router_str) = global_query(store, "Router") {
103+
if let Ok(router_ip) = router_str.parse::<IpAddr>() {
104+
return Some(router_ip);
126105
}
127-
128-
return None;
129106
}
130107

131-
/// Returns default route on primary network service.
132-
pub fn router(&self) -> Option<IpAddr> {
133-
if let Some(router_str) = SCNetworkGlobal::query("ng_interface_router", "Router") {
134-
if let Ok(router_ip) = router_str.parse::<IpAddr>() {
135-
return Some(router_ip);
136-
}
137-
}
108+
return None;
109+
}
138110

139-
return None;
140-
}
111+
// pub fn netinfo(&self);
112+
// pub fn proxies(&self) ;
141113

142-
// pub fn netinfo(&self);
143-
// pub fn proxies(&self) ;
114+
115+
declare_TCFType!{
116+
/// Network service object.
117+
SCNetworkService, SCNetworkServiceRef
144118
}
145119

146-
/// Network service object.
147-
pub struct SCNetworkService(pub SCNetworkServiceRef);
120+
impl_TCFType!(SCNetworkService, SCNetworkServiceRef, SCNetworkServiceGetTypeID);
121+
122+
148123

149124
impl SCNetworkService {
125+
/// Returns primary network service
126+
pub fn global(store: &SCDynamicStore) -> Option<Self> {
127+
// let store = SCDynamicStoreBuilder::new(session_name).build();
128+
if let Some(service_id) = global_query(store, "PrimaryService") {
129+
for service in SCNetworkService::list() {
130+
if service.id() == service_id {
131+
return Some(service);
132+
}
133+
}
134+
}
135+
136+
return None;
137+
}
138+
150139
/// Returns all available network services for the specified preferences.
151140
pub fn list() -> Vec<SCNetworkService> {
152141
let prefs = unsafe {
@@ -163,7 +152,11 @@ impl SCNetworkService {
163152
array
164153
.get_all_values()
165154
.iter()
166-
.map(|service_ptr| SCNetworkService(*service_ptr as _))
155+
.map(|service_ptr| {
156+
unsafe {
157+
SCNetworkService::wrap_under_get_rule(*service_ptr as _)
158+
}
159+
})
167160
.collect::<Vec<SCNetworkService>>()
168161
}
169162

@@ -187,7 +180,7 @@ impl SCNetworkService {
187180
for id in array.get_all_values().iter() {
188181
let id_ptr: CFStringRef = *id as _;
189182
let service_ptr: SCNetworkServiceRef = unsafe { SCNetworkServiceCopy(prefs, id_ptr) };
190-
services.push(SCNetworkService(service_ptr));
183+
services.push(unsafe { SCNetworkService::wrap_under_get_rule(service_ptr) });
191184
}
192185

193186
services
@@ -210,7 +203,7 @@ impl SCNetworkService {
210203
}
211204

212205
/// Returns the DNS infomation on this network service
213-
pub fn dns(&self) -> SCNetworkServiceDNS {
206+
pub fn dns(&self) -> SCNetworkServiceDns {
214207
let store = SCDynamicStoreBuilder::new("ns_dns").build();
215208

216209
let query = |path: String| -> (Option<String>, Option<Vec<IpAddr>>) {
@@ -256,7 +249,7 @@ impl SCNetworkService {
256249
let (setup_domain_name, setup_server_addresses) =
257250
query(format!("Setup:/Network/Service/{}/DNS", self.id()));
258251

259-
SCNetworkServiceDNS {
252+
SCNetworkServiceDns {
260253
state_domain_name: state_domain_name,
261254
state_server_addresses: state_server_addresses,
262255
setup_domain_name: setup_domain_name,
@@ -265,7 +258,7 @@ impl SCNetworkService {
265258
}
266259

267260
/// Setting DNS on this network service
268-
pub fn set_dns(&self, dns: SCNetworkServiceDNS) -> bool {
261+
pub fn set_dns(&self, dns: SCNetworkServiceDns) -> bool {
269262
let store = SCDynamicStoreBuilder::new("ns_dns_set").build();
270263

271264
if dns.setup_server_addresses.is_some() {
@@ -307,7 +300,7 @@ impl SCNetworkService {
307300
if interface_ptr.is_null() {
308301
None
309302
} else {
310-
Some(SCNetworkInterface(interface_ptr))
303+
Some(unsafe { SCNetworkInterface::wrap_under_get_rule(interface_ptr) })
311304
}
312305
}
313306
}
@@ -332,10 +325,31 @@ impl fmt::Debug for SCNetworkService {
332325
}
333326
}
334327

335-
/// network interface
336-
pub struct SCNetworkInterface(pub SCNetworkInterfaceRef);
328+
329+
declare_TCFType!{
330+
/// Network interface object.
331+
SCNetworkInterface, SCNetworkInterfaceRef
332+
}
333+
334+
impl_TCFType!(SCNetworkInterface, SCNetworkInterfaceRef, SCNetworkInterfaceGetTypeID);
335+
337336

338337
impl SCNetworkInterface {
338+
/// Returns primary network interface
339+
pub fn global(store: &SCDynamicStore) -> Option<Self> {
340+
if let Some(ifname) = global_query(store, "PrimaryInterface") {
341+
for iface in SCNetworkInterface::list() {
342+
if let Some(bsd_name) = iface.bsd_name() {
343+
if bsd_name == ifname {
344+
return Some(iface);
345+
}
346+
}
347+
}
348+
}
349+
350+
return None;
351+
}
352+
339353
/// Returns all network-capable interfaces on the system.
340354
pub fn list() -> Vec<SCNetworkInterface> {
341355
let array: CFArray<SCNetworkInterfaceRef> =
@@ -344,12 +358,16 @@ impl SCNetworkInterface {
344358
array
345359
.get_all_values()
346360
.iter()
347-
.map(|interface_ptr| SCNetworkInterface(*interface_ptr as _))
361+
.map(|interface_ptr| {
362+
unsafe {
363+
SCNetworkInterface::wrap_under_get_rule(*interface_ptr as _)
364+
}
365+
})
348366
.collect::<Vec<SCNetworkInterface>>()
349367
}
350368

351369
/// Returns the current MTU setting and the range of allowable values
352-
pub fn mtu(&self) -> Option<SCNetworkInterfaceMTU> {
370+
pub fn mtu(&self) -> Option<SCNetworkInterfaceMtu> {
353371
let mut current = 0i32;
354372
let mut min = 0i32;
355373
let mut max = 0i32;
@@ -359,7 +377,7 @@ impl SCNetworkInterface {
359377
if ret_code == 0 {
360378
None
361379
} else {
362-
Some(SCNetworkInterfaceMTU {
380+
Some(SCNetworkInterfaceMtu {
363381
current: current as u32,
364382
min: if min < 0 { None } else { Some(min as u32) },
365383
max: if max < 0 { None } else { Some(max as u32) },

0 commit comments

Comments
 (0)