12
12
//!
13
13
//! [`SCNetworkConfiguration`]: https://developer.apple.com/documentation/systemconfiguration/scnetworkconfiguration
14
14
15
+
15
16
use core_foundation:: array:: CFArray ;
16
17
use core_foundation:: base:: { CFType , TCFType } ;
17
- use core_foundation:: base:: kCFAllocatorDefault;
18
18
use core_foundation:: dictionary:: CFDictionary ;
19
19
use core_foundation:: string:: CFString ;
20
20
21
21
use dynamic_store:: SCDynamicStore ;
22
+ use preferences:: SCPreferences ;
23
+
22
24
pub use system_configuration_sys:: network_configuration:: * ;
23
- use system_configuration_sys:: preferences:: SCPreferencesCreate ;
24
25
25
- use std:: { fmt, ptr } ;
26
+ use std:: fmt;
26
27
use std:: net:: IpAddr ;
27
28
28
29
/// MTU
@@ -100,48 +101,45 @@ impl_TCFType!(
100
101
101
102
impl SCNetworkService {
102
103
/// Returns primary network service
103
- pub fn global ( store : & SCDynamicStore ) -> Option < Self > {
104
- if let Some ( service_id) = global_query ( store, "PrimaryService" ) {
105
- for service in SCNetworkService :: list ( ) {
106
- if service. id ( ) == service_id {
107
- return Some ( service) ;
108
- }
109
- }
104
+ pub fn global ( prefs : & SCPreferences , store : & SCDynamicStore ) -> Option < Self > {
105
+ match global_query ( store, "PrimaryService" ) {
106
+ Some ( service_id) => SCNetworkService :: from_id ( prefs, & service_id) ,
107
+ None => None ,
110
108
}
111
-
112
- return None ;
113
109
}
114
110
115
- /// Returns all available network services for the specified preferences.
116
- pub fn list ( ) -> Vec < SCNetworkService > {
117
- let prefs = unsafe {
118
- SCPreferencesCreate (
119
- kCFAllocatorDefault,
120
- CFString :: from_static_string ( "ns_list" ) . as_concrete_TypeRef ( ) ,
121
- ptr:: null ( ) ,
111
+ /// Returns network service for the specified preferences session and service ID.
112
+ pub fn from_id ( prefs : & SCPreferences , service_id : & str ) -> Option < SCNetworkService > {
113
+ let network_service_ref = unsafe {
114
+ SCNetworkServiceCopy (
115
+ prefs. as_concrete_TypeRef ( ) ,
116
+ CFString :: new ( service_id) . as_concrete_TypeRef ( ) ,
122
117
)
123
118
} ;
124
119
125
- let array: CFArray < CFType > =
126
- unsafe { CFArray :: wrap_under_get_rule ( SCNetworkServiceCopyAll ( prefs) ) } ;
120
+ if network_service_ref. is_null ( ) {
121
+ None
122
+ } else {
123
+ Some ( unsafe { SCNetworkService :: wrap_under_get_rule ( network_service_ref) } )
124
+ }
125
+ }
126
+
127
+ /// Returns all available network services for the specified preferences.
128
+ pub fn list ( prefs : & SCPreferences ) -> Vec < SCNetworkService > {
129
+ let array: CFArray < CFType > = unsafe {
130
+ CFArray :: wrap_under_get_rule ( SCNetworkServiceCopyAll ( prefs. as_concrete_TypeRef ( ) ) )
131
+ } ;
127
132
128
133
array
129
134
. iter ( )
130
135
. map ( |item| item. downcast :: < SCNetworkService > ( ) . unwrap ( ) )
131
136
. collect :: < Vec < SCNetworkService > > ( )
132
137
}
133
138
134
- /// Returns the user-specified ordering of network services within the specified set.
135
- pub fn list_order ( ) -> Vec < SCNetworkService > {
136
- let prefs = unsafe {
137
- SCPreferencesCreate (
138
- kCFAllocatorDefault,
139
- CFString :: from_static_string ( "ns_list_order" ) . as_concrete_TypeRef ( ) ,
140
- ptr:: null ( ) ,
141
- )
142
- } ;
143
-
144
- let netset = unsafe { SCNetworkSetCopyCurrent ( prefs) } ;
139
+ /// Returns the user-specified ordering of network services within the specified
140
+ /// preferences.
141
+ pub fn list_order ( prefs : & SCPreferences ) -> Vec < SCNetworkService > {
142
+ let netset = unsafe { SCNetworkSetCopyCurrent ( prefs. as_concrete_TypeRef ( ) ) } ;
145
143
146
144
let array: CFArray < CFType > =
147
145
unsafe { CFArray :: wrap_under_get_rule ( SCNetworkSetGetServiceOrder ( netset) ) } ;
@@ -150,13 +148,8 @@ impl SCNetworkService {
150
148
151
149
for item in array. iter ( ) {
152
150
if let Some ( id) = item. downcast :: < CFString > ( ) {
153
- let service_ref = unsafe {
154
- CFType :: wrap_under_get_rule ( SCNetworkServiceCopy (
155
- prefs,
156
- id. as_concrete_TypeRef ( ) ,
157
- ) )
158
- } ;
159
- if let Some ( serv) = service_ref. downcast :: < SCNetworkService > ( ) {
151
+ println ! ( "id: {:?}" , id) ;
152
+ if let Some ( serv) = SCNetworkService :: from_id ( prefs, id. to_string ( ) . as_str ( ) ) {
160
153
services. push ( serv) ;
161
154
}
162
155
}
0 commit comments