Skip to main content

systemconfiguration/
bond_interface.rs

1use crate::{
2    bridge::{self, HandleArray},
3    error::Result,
4    ffi,
5    network_interface::NetworkInterface,
6    preferences::Preferences,
7    PropertyList, SystemConfigurationError,
8};
9
10#[derive(Clone)]
11/// Wraps `SCBondInterfaceRef`.
12pub struct BondInterface {
13    raw: bridge::OwnedHandle,
14}
15
16impl std::fmt::Debug for BondInterface {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        f.debug_struct("BondInterface").finish_non_exhaustive()
19    }
20}
21
22#[derive(Clone)]
23/// Wraps `SCBondStatusRef`.
24pub struct BondStatus {
25    raw: bridge::OwnedHandle,
26}
27
28impl std::fmt::Debug for BondStatus {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        f.debug_struct("BondStatus").finish_non_exhaustive()
31    }
32}
33
34impl BondInterface {
35    /// Wraps `SCBondInterfaceCopyAll`.
36    pub fn copy_all(preferences: &Preferences) -> Vec<Self> {
37        let raw =
38            unsafe { ffi::network_interface::sc_bond_interface_copy_all(preferences.as_ptr()) };
39        bridge::take_handle_array(raw, Self::from_owned_handle)
40    }
41
42    /// Wraps `SCBondInterfaceCopyAvailableMemberInterfaces`.
43    pub fn copy_available_member_interfaces(preferences: &Preferences) -> Vec<NetworkInterface> {
44        let raw = unsafe {
45            ffi::network_interface::sc_bond_interface_copy_available_member_interfaces(
46                preferences.as_ptr(),
47            )
48        };
49        bridge::take_handle_array(raw, NetworkInterface::from_owned_handle)
50    }
51
52    /// Wraps `SCBondInterfaceCreate`.
53    pub fn create(preferences: &Preferences) -> Result<Self> {
54        let raw = unsafe { ffi::network_interface::sc_bond_interface_create(preferences.as_ptr()) };
55        let raw = bridge::owned_handle_or_last("sc_bond_interface_create", raw)?;
56        Ok(Self { raw })
57    }
58
59    /// Wraps `SCBondInterfaceCopyMemberInterfaces`.
60    pub fn member_interfaces(&self) -> Vec<NetworkInterface> {
61        let raw = unsafe {
62            ffi::network_interface::sc_bond_interface_copy_member_interfaces(self.raw.as_ptr())
63        };
64        bridge::take_handle_array(raw, NetworkInterface::from_owned_handle)
65    }
66
67    /// Wraps `SCBondInterfaceCopyOptions`.
68    pub fn options(&self) -> Option<PropertyList> {
69        unsafe {
70            bridge::OwnedHandle::from_raw(ffi::network_interface::sc_bond_interface_copy_options(
71                self.raw.as_ptr(),
72            ))
73        }
74        .map(PropertyList::from_owned_handle)
75    }
76
77    /// Wraps `SCBondInterfaceRemove`.
78    pub fn remove(&self) -> Result<()> {
79        let ok = unsafe { ffi::network_interface::sc_bond_interface_remove(self.raw.as_ptr()) };
80        bridge::bool_result("sc_bond_interface_remove", ok)
81    }
82
83    /// Wraps `SCBondInterfaceSetLocalizedDisplayName`.
84    pub fn set_localized_display_name(&self, name: &str) -> Result<()> {
85        let name = bridge::cstring(name, "sc_bond_interface_set_localized_display_name")?;
86        let ok = unsafe {
87            ffi::network_interface::sc_bond_interface_set_localized_display_name(
88                self.raw.as_ptr(),
89                name.as_ptr(),
90            )
91        };
92        bridge::bool_result("sc_bond_interface_set_localized_display_name", ok)
93    }
94
95    /// Wraps `SCBondInterfaceSetMemberInterfaces`.
96    pub fn set_member_interfaces(&self, members: &[NetworkInterface]) -> Result<()> {
97        let members = HandleArray::new(members, NetworkInterface::as_ptr);
98        let ok = unsafe {
99            ffi::network_interface::sc_bond_interface_set_member_interfaces(
100                self.raw.as_ptr(),
101                members.as_ptr(),
102                members.count(),
103            )
104        };
105        bridge::bool_result("sc_bond_interface_set_member_interfaces", ok)
106    }
107
108    /// Wraps `SCBondInterfaceSetOptions`.
109    pub fn set_options(&self, options: &PropertyList) -> Result<()> {
110        let ok = unsafe {
111            ffi::network_interface::sc_bond_interface_set_options(
112                self.raw.as_ptr(),
113                options.as_ptr(),
114            )
115        };
116        bridge::bool_result("sc_bond_interface_set_options", ok)
117    }
118
119    /// Wraps `SCBondInterfaceCopyStatus`.
120    pub fn status(&self) -> Option<BondStatus> {
121        unsafe {
122            bridge::OwnedHandle::from_raw(ffi::network_interface::sc_bond_interface_copy_status(
123                self.raw.as_ptr(),
124            ))
125        }
126        .map(BondStatus::from_owned_handle)
127    }
128
129    /// Wraps a helper on `SCBondInterfaceRef`.
130    pub fn as_network_interface(&self) -> NetworkInterface {
131        NetworkInterface::from_owned_handle(self.raw.clone())
132    }
133
134    pub(crate) fn from_owned_handle(raw: bridge::OwnedHandle) -> Self {
135        Self { raw }
136    }
137}
138
139impl BondStatus {
140    /// Wraps `SCBondStatusGetTypeID`.
141    pub fn type_id() -> u64 {
142        unsafe { ffi::network_interface::sc_bond_status_get_type_id() }
143    }
144
145    /// Wraps `SCBondStatusCopyDeviceAggregationStatusKey`.
146    pub fn device_aggregation_status_key() -> Result<String> {
147        bridge::take_optional_string(unsafe {
148            ffi::network_interface::sc_bond_status_copy_device_aggregation_status_key()
149        })
150        .ok_or_else(|| {
151            SystemConfigurationError::null(
152                "sc_bond_status_copy_device_aggregation_status_key",
153                "bridge returned null bond aggregation-status key",
154            )
155        })
156    }
157
158    /// Wraps `SCBondStatusCopyDeviceCollectingKey`.
159    pub fn device_collecting_key() -> Result<String> {
160        bridge::take_optional_string(unsafe {
161            ffi::network_interface::sc_bond_status_copy_device_collecting_key()
162        })
163        .ok_or_else(|| {
164            SystemConfigurationError::null(
165                "sc_bond_status_copy_device_collecting_key",
166                "bridge returned null bond collecting-status key",
167            )
168        })
169    }
170
171    /// Wraps `SCBondStatusCopyDeviceDistributingKey`.
172    pub fn device_distributing_key() -> Result<String> {
173        bridge::take_optional_string(unsafe {
174            ffi::network_interface::sc_bond_status_copy_device_distributing_key()
175        })
176        .ok_or_else(|| {
177            SystemConfigurationError::null(
178                "sc_bond_status_copy_device_distributing_key",
179                "bridge returned null bond distributing-status key",
180            )
181        })
182    }
183
184    /// Wraps `SCBondStatusCopyMemberInterfaces`.
185    pub fn member_interfaces(&self) -> Vec<NetworkInterface> {
186        let raw = unsafe {
187            ffi::network_interface::sc_bond_status_copy_member_interfaces(self.raw.as_ptr())
188        };
189        bridge::take_handle_array(raw, NetworkInterface::from_owned_handle)
190    }
191
192    /// Wraps `SCBondStatusCopyInterfaceStatus`.
193    pub fn interface_status(&self, interface: Option<&NetworkInterface>) -> Option<PropertyList> {
194        let raw = unsafe {
195            ffi::network_interface::sc_bond_status_copy_interface_status(
196                self.raw.as_ptr(),
197                interface.map_or(std::ptr::null_mut(), NetworkInterface::as_ptr),
198            )
199        };
200        unsafe { bridge::OwnedHandle::from_raw(raw) }.map(PropertyList::from_owned_handle)
201    }
202
203    /// Wraps a helper on `SCBondStatusRef`.
204    pub fn bond_status(&self) -> Result<PropertyList> {
205        self.interface_status(None).ok_or_else(|| {
206            SystemConfigurationError::null(
207                "sc_bond_status_copy_interface_status",
208                "bridge returned null bond status dictionary",
209            )
210        })
211    }
212
213    pub(crate) fn from_owned_handle(raw: bridge::OwnedHandle) -> Self {
214        Self { raw }
215    }
216}