Skip to content

Commit 311e66c

Browse files
author
luozijun
committed
Add document
1 parent c41a1a5 commit 311e66c

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

system-configuration/src/network_configuration.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
115
use core_foundation::array::CFArray;
216
use core_foundation::base::{CFType, TCFType};
317
use core_foundation::base::kCFAllocatorDefault;
@@ -11,13 +25,20 @@ use system_configuration_sys::preferences::SCPreferencesCreate;
1125
use std::{fmt, ptr};
1226
use std::net::IpAddr;
1327

28+
/// MTU
1429
#[derive(Debug)]
1530
pub struct SCNetworkInterfaceMTU {
31+
/// the current MTU setting for the interface.
1632
pub current: u32,
33+
/// the minimum MTU setting for the interface. If negative, the minimum setting could not
34+
/// be determined.
1735
pub min: Option<u32>,
36+
/// the maximum MTU setting for the interface. If negative, the maximum setting could not
37+
/// be determined.
1838
pub max: Option<u32>,
1939
}
2040

41+
/// DNS
2142
#[derive(Debug)]
2243
pub struct SCNetworkServiceDNS {
2344
state_domain_name: Option<String>,
@@ -27,6 +48,7 @@ pub struct SCNetworkServiceDNS {
2748
}
2849

2950
impl SCNetworkServiceDNS {
51+
/// DNS Constructor
3052
pub fn new(
3153
domain_name: (Option<String>, Option<String>),
3254
server_addresses: (Option<Vec<IpAddr>>, Option<Vec<IpAddr>>),
@@ -38,14 +60,16 @@ impl SCNetworkServiceDNS {
3860
setup_server_addresses: server_addresses.1,
3961
}
4062
}
41-
63+
64+
/// Returns DomainName (state and setup)
4265
pub fn domain_name(&self) -> (Option<String>, Option<String>) {
4366
(
4467
self.state_domain_name.clone(),
4568
self.setup_domain_name.clone(),
4669
)
4770
}
4871

72+
/// Returns ServerAddresses (state and setup)
4973
pub fn server_addresses(&self) -> (Option<Vec<IpAddr>>, Option<Vec<IpAddr>>) {
5074
(
5175
self.state_server_addresses.clone(),
@@ -54,6 +78,7 @@ impl SCNetworkServiceDNS {
5478
}
5579
}
5680

81+
/// Global network object
5782
pub struct SCNetworkGlobal;
5883

5984
impl SCNetworkGlobal {
@@ -75,6 +100,7 @@ impl SCNetworkGlobal {
75100
return None;
76101
}
77102

103+
/// Returns primary network service
78104
pub fn service(&self) -> Option<SCNetworkService> {
79105
if let Some(service_id) = SCNetworkGlobal::query("ng_service", "PrimaryService") {
80106
for _service in SCNetworkService::list() {
@@ -87,6 +113,7 @@ impl SCNetworkGlobal {
87113
return None;
88114
}
89115

116+
/// Returns primary network interface
90117
pub fn interface(&self) -> Option<SCNetworkInterface> {
91118
if let Some(ifname) = SCNetworkGlobal::query("ng_interface", "PrimaryInterface") {
92119
for iface in SCNetworkInterface::list() {
@@ -101,6 +128,7 @@ impl SCNetworkGlobal {
101128
return None;
102129
}
103130

131+
/// Returns default route on primary network service.
104132
pub fn router(&self) -> Option<IpAddr> {
105133
if let Some(router_str) = SCNetworkGlobal::query("ng_interface_router", "Router") {
106134
if let Ok(router_ip) = router_str.parse::<IpAddr>() {
@@ -115,9 +143,11 @@ impl SCNetworkGlobal {
115143
// pub fn proxies(&self) ;
116144
}
117145

146+
/// Network service object.
118147
pub struct SCNetworkService(pub SCNetworkServiceRef);
119148

120149
impl SCNetworkService {
150+
/// Returns all available network services for the specified preferences.
121151
pub fn list() -> Vec<SCNetworkService> {
122152
let prefs = unsafe {
123153
SCPreferencesCreate(
@@ -137,6 +167,7 @@ impl SCNetworkService {
137167
.collect::<Vec<SCNetworkService>>()
138168
}
139169

170+
/// Returns the user-specified ordering of network services within the specified set.
140171
pub fn list_order() -> Vec<SCNetworkService> {
141172
let prefs = unsafe {
142173
SCPreferencesCreate(
@@ -162,19 +193,23 @@ impl SCNetworkService {
162193
services
163194
}
164195

196+
/// Returns the identifier for this network service.
165197
pub fn id(&self) -> String {
166198
unsafe { CFString::wrap_under_get_rule(SCNetworkServiceGetServiceID(self.0)) }.to_string()
167199
}
168200

201+
/// Returns the user-specified name associated with this network service.
169202
pub fn name(&self) -> String {
170203
unsafe { CFString::wrap_under_get_rule(SCNetworkServiceGetName(self.0)) }.to_string()
171204
}
172205

206+
/// Returns this network service is enabled or disabled.
173207
pub fn enabled(&self) -> bool {
174208
let ret = unsafe { SCNetworkServiceGetEnabled(self.0) };
175209
ret == 1
176210
}
177211

212+
/// Returns the DNS infomation on this network service
178213
pub fn dns(&self) -> SCNetworkServiceDNS {
179214
let store = SCDynamicStoreBuilder::new("ns_dns").build();
180215

@@ -229,6 +264,7 @@ impl SCNetworkService {
229264
}
230265
}
231266

267+
/// Setting DNS on this network service
232268
pub fn set_dns(&self, dns: SCNetworkServiceDNS) -> bool {
233269
let store = SCDynamicStoreBuilder::new("ns_dns_set").build();
234270

@@ -265,6 +301,7 @@ impl SCNetworkService {
265301
return true;
266302
}
267303

304+
/// Returns the network interface associated with this network service.
268305
pub fn interface(&self) -> Option<SCNetworkInterface> {
269306
let interface_ptr = unsafe { SCNetworkServiceGetInterface(self.0) };
270307
if interface_ptr.is_null() {
@@ -295,9 +332,11 @@ impl fmt::Debug for SCNetworkService {
295332
}
296333
}
297334

335+
/// network interface
298336
pub struct SCNetworkInterface(pub SCNetworkInterfaceRef);
299337

300338
impl SCNetworkInterface {
339+
/// Returns all network-capable interfaces on the system.
301340
pub fn list() -> Vec<SCNetworkInterface> {
302341
let array: CFArray<SCNetworkInterfaceRef> =
303342
unsafe { CFArray::wrap_under_get_rule(SCNetworkInterfaceCopyAll()) };
@@ -309,6 +348,7 @@ impl SCNetworkInterface {
309348
.collect::<Vec<SCNetworkInterface>>()
310349
}
311350

351+
/// Returns the current MTU setting and the range of allowable values
312352
pub fn mtu(&self) -> Option<SCNetworkInterfaceMTU> {
313353
let mut current = 0i32;
314354
let mut min = 0i32;
@@ -327,6 +367,7 @@ impl SCNetworkInterface {
327367
}
328368
}
329369

370+
/// Returns the BSD interface or device name
330371
pub fn bsd_name(&self) -> Option<String> {
331372
unsafe {
332373
let str_ptr = SCNetworkInterfaceGetBSDName(self.0);
@@ -338,10 +379,12 @@ impl SCNetworkInterface {
338379
}
339380
}
340381

382+
/// Returns the BSD interface or device name
341383
pub fn name(&self) -> Option<String> {
342384
self.bsd_name()
343385
}
344386

387+
/// Returns the network interface type
345388
pub fn type_(&self) -> Option<String> {
346389
unsafe {
347390
let str_ptr = SCNetworkInterfaceGetInterfaceType(self.0);
@@ -353,6 +396,7 @@ impl SCNetworkInterface {
353396
}
354397
}
355398

399+
/// Returns a displayable link layer address
356400
pub fn hwaddr(&self) -> Option<String> {
357401
unsafe {
358402
let str_ptr = SCNetworkInterfaceGetHardwareAddressString(self.0);
@@ -364,6 +408,7 @@ impl SCNetworkInterface {
364408
}
365409
}
366410

411+
/// Returns the configuration settings associated
367412
pub fn config(&self) -> Option<CFDictionary> {
368413
unsafe {
369414
let config_ptr = SCNetworkInterfaceGetConfiguration(self.0);

0 commit comments

Comments
 (0)