@@ -18,7 +18,7 @@ use core_foundation::base::kCFAllocatorDefault;
18
18
use core_foundation:: dictionary:: CFDictionary ;
19
19
use core_foundation:: string:: { CFString , CFStringRef } ;
20
20
21
- use dynamic_store:: SCDynamicStoreBuilder ;
21
+ use dynamic_store:: { SCDynamicStoreBuilder , SCDynamicStore } ;
22
22
pub use system_configuration_sys:: network_configuration:: * ;
23
23
use system_configuration_sys:: preferences:: SCPreferencesCreate ;
24
24
@@ -27,33 +27,33 @@ use std::net::IpAddr;
27
27
28
28
/// MTU
29
29
#[ derive( Debug ) ]
30
- pub struct SCNetworkInterfaceMTU {
30
+ pub struct SCNetworkInterfaceMtu {
31
31
/// the current MTU setting for the interface.
32
32
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
34
34
/// be determined.
35
35
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
37
37
/// be determined.
38
38
pub max : Option < u32 > ,
39
39
}
40
40
41
41
/// DNS
42
42
#[ derive( Debug ) ]
43
- pub struct SCNetworkServiceDNS {
43
+ pub struct SCNetworkServiceDns {
44
44
state_domain_name : Option < String > ,
45
45
setup_domain_name : Option < String > ,
46
46
state_server_addresses : Option < Vec < IpAddr > > ,
47
47
setup_server_addresses : Option < Vec < IpAddr > > ,
48
48
}
49
49
50
- impl SCNetworkServiceDNS {
50
+ impl SCNetworkServiceDns {
51
51
/// DNS Constructor
52
52
pub fn new (
53
53
domain_name : ( Option < String > , Option < String > ) ,
54
54
server_addresses : ( Option < Vec < IpAddr > > , Option < Vec < IpAddr > > ) ,
55
- ) -> SCNetworkServiceDNS {
56
- SCNetworkServiceDNS {
55
+ ) -> SCNetworkServiceDns {
56
+ SCNetworkServiceDns {
57
57
state_domain_name : domain_name. 0 ,
58
58
setup_domain_name : domain_name. 1 ,
59
59
state_server_addresses : server_addresses. 0 ,
@@ -78,75 +78,64 @@ impl SCNetworkServiceDNS {
78
78
}
79
79
}
80
80
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" ) ;
83
83
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 ( ) ) ;
96
90
}
97
91
}
98
92
}
99
-
100
- return None ;
101
93
}
102
94
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
+ }
112
97
113
- return None ;
114
- }
115
98
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) ;
126
105
}
127
-
128
- return None ;
129
106
}
130
107
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
+ }
138
110
139
- return None ;
140
- }
111
+ // pub fn netinfo(&self) ;
112
+ // pub fn proxies(&self) ;
141
113
142
- // pub fn netinfo(&self);
143
- // pub fn proxies(&self) ;
114
+
115
+ declare_TCFType ! {
116
+ /// Network service object.
117
+ SCNetworkService , SCNetworkServiceRef
144
118
}
145
119
146
- /// Network service object.
147
- pub struct SCNetworkService ( pub SCNetworkServiceRef ) ;
120
+ impl_TCFType ! ( SCNetworkService , SCNetworkServiceRef , SCNetworkServiceGetTypeID ) ;
121
+
122
+
148
123
149
124
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
+
150
139
/// Returns all available network services for the specified preferences.
151
140
pub fn list ( ) -> Vec < SCNetworkService > {
152
141
let prefs = unsafe {
@@ -163,7 +152,11 @@ impl SCNetworkService {
163
152
array
164
153
. get_all_values ( )
165
154
. 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
+ } )
167
160
. collect :: < Vec < SCNetworkService > > ( )
168
161
}
169
162
@@ -187,7 +180,7 @@ impl SCNetworkService {
187
180
for id in array. get_all_values ( ) . iter ( ) {
188
181
let id_ptr: CFStringRef = * id as _ ;
189
182
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) } ) ;
191
184
}
192
185
193
186
services
@@ -210,7 +203,7 @@ impl SCNetworkService {
210
203
}
211
204
212
205
/// Returns the DNS infomation on this network service
213
- pub fn dns ( & self ) -> SCNetworkServiceDNS {
206
+ pub fn dns ( & self ) -> SCNetworkServiceDns {
214
207
let store = SCDynamicStoreBuilder :: new ( "ns_dns" ) . build ( ) ;
215
208
216
209
let query = |path : String | -> ( Option < String > , Option < Vec < IpAddr > > ) {
@@ -256,7 +249,7 @@ impl SCNetworkService {
256
249
let ( setup_domain_name, setup_server_addresses) =
257
250
query ( format ! ( "Setup:/Network/Service/{}/DNS" , self . id( ) ) ) ;
258
251
259
- SCNetworkServiceDNS {
252
+ SCNetworkServiceDns {
260
253
state_domain_name : state_domain_name,
261
254
state_server_addresses : state_server_addresses,
262
255
setup_domain_name : setup_domain_name,
@@ -265,7 +258,7 @@ impl SCNetworkService {
265
258
}
266
259
267
260
/// Setting DNS on this network service
268
- pub fn set_dns ( & self , dns : SCNetworkServiceDNS ) -> bool {
261
+ pub fn set_dns ( & self , dns : SCNetworkServiceDns ) -> bool {
269
262
let store = SCDynamicStoreBuilder :: new ( "ns_dns_set" ) . build ( ) ;
270
263
271
264
if dns. setup_server_addresses . is_some ( ) {
@@ -307,7 +300,7 @@ impl SCNetworkService {
307
300
if interface_ptr. is_null ( ) {
308
301
None
309
302
} else {
310
- Some ( SCNetworkInterface ( interface_ptr) )
303
+ Some ( unsafe { SCNetworkInterface :: wrap_under_get_rule ( interface_ptr) } )
311
304
}
312
305
}
313
306
}
@@ -332,10 +325,31 @@ impl fmt::Debug for SCNetworkService {
332
325
}
333
326
}
334
327
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
+
337
336
338
337
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
+
339
353
/// Returns all network-capable interfaces on the system.
340
354
pub fn list ( ) -> Vec < SCNetworkInterface > {
341
355
let array: CFArray < SCNetworkInterfaceRef > =
@@ -344,12 +358,16 @@ impl SCNetworkInterface {
344
358
array
345
359
. get_all_values ( )
346
360
. 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
+ } )
348
366
. collect :: < Vec < SCNetworkInterface > > ( )
349
367
}
350
368
351
369
/// 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 > {
353
371
let mut current = 0i32 ;
354
372
let mut min = 0i32 ;
355
373
let mut max = 0i32 ;
@@ -359,7 +377,7 @@ impl SCNetworkInterface {
359
377
if ret_code == 0 {
360
378
None
361
379
} else {
362
- Some ( SCNetworkInterfaceMTU {
380
+ Some ( SCNetworkInterfaceMtu {
363
381
current : current as u32 ,
364
382
min : if min < 0 { None } else { Some ( min as u32 ) } ,
365
383
max : if max < 0 { None } else { Some ( max as u32 ) } ,
0 commit comments