systemconfiguration/
network_interface.rs1use serde::Deserialize;
2
3use crate::{bridge, error::Result, ffi, PropertyList, SystemConfigurationError};
4
5#[derive(Clone, Debug)]
6pub struct NetworkInterface {
7 raw: bridge::OwnedHandle,
8}
9
10#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
11pub struct NetworkInterfaceMtuInfo {
12 pub current: i32,
13 pub minimum: i32,
14 pub maximum: i32,
15}
16
17impl NetworkInterface {
18 pub fn copy_all() -> Result<Vec<Self>> {
19 let raw = unsafe { ffi::network_interface::sc_network_interface_copy_all() };
20 if raw.is_null() {
21 return Err(SystemConfigurationError::last("sc_network_interface_copy_all"));
22 }
23 Ok(bridge::take_handle_array(raw, Self::from_owned_handle))
24 }
25
26 pub fn supported_interface_types(&self) -> Vec<String> {
27 bridge::take_string_array(unsafe {
28 ffi::network_interface::sc_network_interface_copy_supported_interface_types(self.raw.as_ptr())
29 })
30 }
31
32 pub fn supported_protocol_types(&self) -> Vec<String> {
33 bridge::take_string_array(unsafe {
34 ffi::network_interface::sc_network_interface_copy_supported_protocol_types(self.raw.as_ptr())
35 })
36 }
37
38 pub fn create_layered_interface(&self, interface_type: &str) -> Result<Option<Self>> {
39 let interface_type = bridge::cstring(interface_type, "sc_network_interface_create_with_interface")?;
40 let raw = unsafe {
41 ffi::network_interface::sc_network_interface_create_with_interface(
42 self.raw.as_ptr(),
43 interface_type.as_ptr(),
44 )
45 };
46 Ok(unsafe { bridge::OwnedHandle::from_raw(raw) }.map(Self::from_owned_handle))
47 }
48
49 pub fn bsd_name(&self) -> Result<Option<String>> {
50 Ok(bridge::take_optional_string(unsafe {
51 ffi::network_interface::sc_network_interface_copy_bsd_name(self.raw.as_ptr())
52 }))
53 }
54
55 pub fn configuration(&self) -> Option<PropertyList> {
56 unsafe { bridge::OwnedHandle::from_raw(ffi::network_interface::sc_network_interface_copy_configuration(self.raw.as_ptr())) }
57 .map(PropertyList::from_owned_handle)
58 }
59
60 pub fn extended_configuration(&self, extended_type: &str) -> Result<Option<PropertyList>> {
61 let extended_type = bridge::cstring(extended_type, "sc_network_interface_copy_extended_configuration")?;
62 let raw = unsafe {
63 ffi::network_interface::sc_network_interface_copy_extended_configuration(
64 self.raw.as_ptr(),
65 extended_type.as_ptr(),
66 )
67 };
68 Ok(unsafe { bridge::OwnedHandle::from_raw(raw) }.map(PropertyList::from_owned_handle))
69 }
70
71 pub fn hardware_address_string(&self) -> Result<Option<String>> {
72 Ok(bridge::take_optional_string(unsafe {
73 ffi::network_interface::sc_network_interface_copy_hardware_address(self.raw.as_ptr())
74 }))
75 }
76
77 pub fn underlying_interface(&self) -> Option<Self> {
78 unsafe {
79 bridge::OwnedHandle::from_raw(ffi::network_interface::sc_network_interface_copy_underlying_interface(
80 self.raw.as_ptr(),
81 ))
82 }
83 .map(Self::from_owned_handle)
84 }
85
86 pub fn interface_type(&self) -> Result<Option<String>> {
87 Ok(bridge::take_optional_string(unsafe {
88 ffi::network_interface::sc_network_interface_copy_interface_type(self.raw.as_ptr())
89 }))
90 }
91
92 pub fn localized_display_name(&self) -> Result<Option<String>> {
93 Ok(bridge::take_optional_string(unsafe {
94 ffi::network_interface::sc_network_interface_copy_localized_display_name(self.raw.as_ptr())
95 }))
96 }
97
98 pub fn mtu_info(&self) -> Result<Option<NetworkInterfaceMtuInfo>> {
99 let raw = unsafe { ffi::network_interface::sc_network_interface_copy_mtu_info(self.raw.as_ptr()) };
100 if raw.is_null() {
101 return Ok(None);
102 }
103 bridge::parse_json("sc_network_interface_copy_mtu_info", raw).map(Some)
104 }
105
106 pub fn force_configuration_refresh(&self) -> Result<()> {
107 let ok = unsafe { ffi::network_interface::sc_network_interface_force_configuration_refresh(self.raw.as_ptr()) };
108 bridge::bool_result("sc_network_interface_force_configuration_refresh", ok)
109 }
110
111 pub(crate) fn from_owned_handle(raw: bridge::OwnedHandle) -> Self {
112 Self { raw }
113 }
114
115 pub(crate) fn as_ptr(&self) -> bridge::RawHandle {
116 self.raw.as_ptr()
117 }
118}