1
+ // Copyright 2017 Amagicom AB.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6
+ // option. This file may not be copied, modified, or distributed
7
+ // except according to those terms.
8
+
9
+ //! Bindings to [`SCNetworkConfiguration`].
10
+ //!
11
+ //! See the examples directory for examples how to use this module.
12
+ //!
13
+ //! [`SCNetworkConfiguration`]: https://developer.apple.com/documentation/systemconfiguration/scnetworkconfiguration
14
+
1
15
use core_foundation:: array:: CFArray ;
2
16
use core_foundation:: base:: { CFType , TCFType } ;
3
17
use core_foundation:: base:: kCFAllocatorDefault;
@@ -11,13 +25,20 @@ use system_configuration_sys::preferences::SCPreferencesCreate;
11
25
use std:: { fmt, ptr} ;
12
26
use std:: net:: IpAddr ;
13
27
28
+ /// MTU
14
29
#[ derive( Debug ) ]
15
30
pub struct SCNetworkInterfaceMTU {
31
+ /// the current MTU setting for the interface.
16
32
pub current : u32 ,
33
+ /// the minimum MTU setting for the interface. If negative, the minimum setting could not
34
+ /// be determined.
17
35
pub min : Option < u32 > ,
36
+ /// the maximum MTU setting for the interface. If negative, the maximum setting could not
37
+ /// be determined.
18
38
pub max : Option < u32 > ,
19
39
}
20
40
41
+ /// DNS
21
42
#[ derive( Debug ) ]
22
43
pub struct SCNetworkServiceDNS {
23
44
state_domain_name : Option < String > ,
@@ -27,6 +48,7 @@ pub struct SCNetworkServiceDNS {
27
48
}
28
49
29
50
impl SCNetworkServiceDNS {
51
+ /// DNS Constructor
30
52
pub fn new (
31
53
domain_name : ( Option < String > , Option < String > ) ,
32
54
server_addresses : ( Option < Vec < IpAddr > > , Option < Vec < IpAddr > > ) ,
@@ -38,14 +60,16 @@ impl SCNetworkServiceDNS {
38
60
setup_server_addresses : server_addresses. 1 ,
39
61
}
40
62
}
41
-
63
+
64
+ /// Returns DomainName (state and setup)
42
65
pub fn domain_name ( & self ) -> ( Option < String > , Option < String > ) {
43
66
(
44
67
self . state_domain_name . clone ( ) ,
45
68
self . setup_domain_name . clone ( ) ,
46
69
)
47
70
}
48
71
72
+ /// Returns ServerAddresses (state and setup)
49
73
pub fn server_addresses ( & self ) -> ( Option < Vec < IpAddr > > , Option < Vec < IpAddr > > ) {
50
74
(
51
75
self . state_server_addresses . clone ( ) ,
@@ -54,6 +78,7 @@ impl SCNetworkServiceDNS {
54
78
}
55
79
}
56
80
81
+ /// Global network object
57
82
pub struct SCNetworkGlobal ;
58
83
59
84
impl SCNetworkGlobal {
@@ -75,6 +100,7 @@ impl SCNetworkGlobal {
75
100
return None ;
76
101
}
77
102
103
+ /// Returns primary network service
78
104
pub fn service ( & self ) -> Option < SCNetworkService > {
79
105
if let Some ( service_id) = SCNetworkGlobal :: query ( "ng_service" , "PrimaryService" ) {
80
106
for _service in SCNetworkService :: list ( ) {
@@ -87,6 +113,7 @@ impl SCNetworkGlobal {
87
113
return None ;
88
114
}
89
115
116
+ /// Returns primary network interface
90
117
pub fn interface ( & self ) -> Option < SCNetworkInterface > {
91
118
if let Some ( ifname) = SCNetworkGlobal :: query ( "ng_interface" , "PrimaryInterface" ) {
92
119
for iface in SCNetworkInterface :: list ( ) {
@@ -101,6 +128,7 @@ impl SCNetworkGlobal {
101
128
return None ;
102
129
}
103
130
131
+ /// Returns default route on primary network service.
104
132
pub fn router ( & self ) -> Option < IpAddr > {
105
133
if let Some ( router_str) = SCNetworkGlobal :: query ( "ng_interface_router" , "Router" ) {
106
134
if let Ok ( router_ip) = router_str. parse :: < IpAddr > ( ) {
@@ -115,9 +143,11 @@ impl SCNetworkGlobal {
115
143
// pub fn proxies(&self) ;
116
144
}
117
145
146
+ /// Network service object.
118
147
pub struct SCNetworkService ( pub SCNetworkServiceRef ) ;
119
148
120
149
impl SCNetworkService {
150
+ /// Returns all available network services for the specified preferences.
121
151
pub fn list ( ) -> Vec < SCNetworkService > {
122
152
let prefs = unsafe {
123
153
SCPreferencesCreate (
@@ -137,6 +167,7 @@ impl SCNetworkService {
137
167
. collect :: < Vec < SCNetworkService > > ( )
138
168
}
139
169
170
+ /// Returns the user-specified ordering of network services within the specified set.
140
171
pub fn list_order ( ) -> Vec < SCNetworkService > {
141
172
let prefs = unsafe {
142
173
SCPreferencesCreate (
@@ -162,19 +193,23 @@ impl SCNetworkService {
162
193
services
163
194
}
164
195
196
+ /// Returns the identifier for this network service.
165
197
pub fn id ( & self ) -> String {
166
198
unsafe { CFString :: wrap_under_get_rule ( SCNetworkServiceGetServiceID ( self . 0 ) ) } . to_string ( )
167
199
}
168
200
201
+ /// Returns the user-specified name associated with this network service.
169
202
pub fn name ( & self ) -> String {
170
203
unsafe { CFString :: wrap_under_get_rule ( SCNetworkServiceGetName ( self . 0 ) ) } . to_string ( )
171
204
}
172
205
206
+ /// Returns this network service is enabled or disabled.
173
207
pub fn enabled ( & self ) -> bool {
174
208
let ret = unsafe { SCNetworkServiceGetEnabled ( self . 0 ) } ;
175
209
ret == 1
176
210
}
177
211
212
+ /// Returns the DNS infomation on this network service
178
213
pub fn dns ( & self ) -> SCNetworkServiceDNS {
179
214
let store = SCDynamicStoreBuilder :: new ( "ns_dns" ) . build ( ) ;
180
215
@@ -229,6 +264,7 @@ impl SCNetworkService {
229
264
}
230
265
}
231
266
267
+ /// Setting DNS on this network service
232
268
pub fn set_dns ( & self , dns : SCNetworkServiceDNS ) -> bool {
233
269
let store = SCDynamicStoreBuilder :: new ( "ns_dns_set" ) . build ( ) ;
234
270
@@ -265,6 +301,7 @@ impl SCNetworkService {
265
301
return true ;
266
302
}
267
303
304
+ /// Returns the network interface associated with this network service.
268
305
pub fn interface ( & self ) -> Option < SCNetworkInterface > {
269
306
let interface_ptr = unsafe { SCNetworkServiceGetInterface ( self . 0 ) } ;
270
307
if interface_ptr. is_null ( ) {
@@ -295,9 +332,11 @@ impl fmt::Debug for SCNetworkService {
295
332
}
296
333
}
297
334
335
+ /// network interface
298
336
pub struct SCNetworkInterface ( pub SCNetworkInterfaceRef ) ;
299
337
300
338
impl SCNetworkInterface {
339
+ /// Returns all network-capable interfaces on the system.
301
340
pub fn list ( ) -> Vec < SCNetworkInterface > {
302
341
let array: CFArray < SCNetworkInterfaceRef > =
303
342
unsafe { CFArray :: wrap_under_get_rule ( SCNetworkInterfaceCopyAll ( ) ) } ;
@@ -309,6 +348,7 @@ impl SCNetworkInterface {
309
348
. collect :: < Vec < SCNetworkInterface > > ( )
310
349
}
311
350
351
+ /// Returns the current MTU setting and the range of allowable values
312
352
pub fn mtu ( & self ) -> Option < SCNetworkInterfaceMTU > {
313
353
let mut current = 0i32 ;
314
354
let mut min = 0i32 ;
@@ -327,6 +367,7 @@ impl SCNetworkInterface {
327
367
}
328
368
}
329
369
370
+ /// Returns the BSD interface or device name
330
371
pub fn bsd_name ( & self ) -> Option < String > {
331
372
unsafe {
332
373
let str_ptr = SCNetworkInterfaceGetBSDName ( self . 0 ) ;
@@ -338,10 +379,12 @@ impl SCNetworkInterface {
338
379
}
339
380
}
340
381
382
+ /// Returns the BSD interface or device name
341
383
pub fn name ( & self ) -> Option < String > {
342
384
self . bsd_name ( )
343
385
}
344
386
387
+ /// Returns the network interface type
345
388
pub fn type_ ( & self ) -> Option < String > {
346
389
unsafe {
347
390
let str_ptr = SCNetworkInterfaceGetInterfaceType ( self . 0 ) ;
@@ -353,6 +396,7 @@ impl SCNetworkInterface {
353
396
}
354
397
}
355
398
399
+ /// Returns a displayable link layer address
356
400
pub fn hwaddr ( & self ) -> Option < String > {
357
401
unsafe {
358
402
let str_ptr = SCNetworkInterfaceGetHardwareAddressString ( self . 0 ) ;
@@ -364,6 +408,7 @@ impl SCNetworkInterface {
364
408
}
365
409
}
366
410
411
+ /// Returns the configuration settings associated
367
412
pub fn config ( & self ) -> Option < CFDictionary > {
368
413
unsafe {
369
414
let config_ptr = SCNetworkInterfaceGetConfiguration ( self . 0 ) ;
0 commit comments