1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
//! Bindings for [`SCNetworkConfiguration`].
//!
//! [`SCNetworkConfiguration`]: https://developer.apple.com/documentation/systemconfiguration/scnetworkconfiguration?language=objc
use core_foundation::{array::CFArray, base::TCFType, string::CFString};
use system_configuration_sys::network_configuration::{
SCNetworkInterfaceCopyAll, SCNetworkInterfaceGetBSDName, SCNetworkInterfaceGetInterfaceType,
SCNetworkInterfaceGetLocalizedDisplayName, SCNetworkInterfaceGetTypeID, SCNetworkInterfaceRef,
};
core_foundation::declare_TCFType!(
/// Represents a network interface.
///
/// See [`SCNetworkInterfaceRef`] and its [methods] for details.
///
/// [`SCNetworkInterfaceRef`]: https://developer.apple.com/documentation/systemconfiguration/scnetworkinterfaceref?language=objc
/// [methods]: https://developer.apple.com/documentation/systemconfiguration/scnetworkconfiguration?language=objc
SCNetworkInterface,
SCNetworkInterfaceRef
);
core_foundation::impl_TCFType!(
SCNetworkInterface,
SCNetworkInterfaceRef,
SCNetworkInterfaceGetTypeID
);
// TODO: implement all the other methods a SCNetworkInterface has
impl SCNetworkInterface {
/// Get type of the network interface, if the type is recognized, returns `None` otherwise.
///
/// See [`SCNetworkInterfaceGetInterfaceType`] for details.
///
/// [`SCNetworkInterfaceGetInterfaceType`]: https://developer.apple.com/documentation/systemconfiguration/1517371-scnetworkinterfacegetinterfacety?language=objc
pub fn interface_type(&self) -> Option<SCNetworkInterfaceType> {
SCNetworkInterfaceType::from_cfstring(&self.interface_type_string()?)
}
/// Returns the raw interface type identifier.
///
/// See [`SCNetworkInterfaceGetInterfaceType`] for details.
///
/// [`SCNetworkInterfaceGetInterfaceType`]: https://developer.apple.com/documentation/systemconfiguration/1517371-scnetworkinterfacegetinterfacety?language=objc
pub fn interface_type_string(&self) -> Option<CFString> {
unsafe {
let ptr = SCNetworkInterfaceGetInterfaceType(self.0);
if ptr.is_null() {
None
} else {
Some(CFString::wrap_under_get_rule(ptr))
}
}
}
/// Returns the _BSD_ name for the interface, such as `en0`.
///
/// See [`SCNetworkInterfaceGetBSDName`] for details.
///
/// [`SCNetworkInterfaceGetBSDName`]: https://developer.apple.com/documentation/systemconfiguration/1516854-scnetworkinterfacegetbsdname?language=objc
pub fn bsd_name(&self) -> Option<CFString> {
unsafe {
let ptr = SCNetworkInterfaceGetBSDName(self.0);
if ptr.is_null() {
None
} else {
Some(CFString::wrap_under_get_rule(ptr))
}
}
}
/// Returns the localized display name for the interface.
///
/// See [`SCNetworkInterfaceGetLocalizedDisplayName`] for details.
///
/// [`SCNetworkInterfaceGetLocalizedDisplayName`]: https://developer.apple.com/documentation/systemconfiguration/1517060-scnetworkinterfacegetlocalizeddi?language=objc
pub fn display_name(&self) -> Option<CFString> {
unsafe {
let ptr = SCNetworkInterfaceGetLocalizedDisplayName(self.0);
if ptr.is_null() {
None
} else {
Some(CFString::wrap_under_get_rule(ptr))
}
}
}
}
/// Represents the possible network interface types.
///
/// See [_Network Interface Types_] documentation for details.
///
/// [_Network Interface Types_]: https://developer.apple.com/documentation/systemconfiguration/scnetworkconfiguration/network_interface_types?language=objc
#[derive(Debug)]
pub enum SCNetworkInterfaceType {
/// A 6to4 interface.
SixToFour,
/// Bluetooth interface.
Bluetooth,
/// Bridge interface.
Bridge,
/// Ethernet bond interface.
Bond,
/// Ethernet interface.
Ethernet,
/// FireWire interface.
FireWire,
/// IEEE80211 interface.
IEEE80211,
/// IPSec interface.
IPSec,
/// IrDA interface.
IrDA,
/// L2TP interface.
L2TP,
/// Modem interface.
Modem,
/// PPP interface.
PPP,
/// PPTP interface.
///
/// Deprecated, one should use the PPP variant.
PPTP,
/// Serial interface.
Serial,
/// VLAN interface.
VLAN,
/// WWAN interface.
WWAN,
/// IPv4 interface.
IPv4,
}
impl SCNetworkInterfaceType {
/// Tries to construct a type by matching it to string constants used to identify a network
/// interface type. If no constants match it, `None` is returned.
pub fn from_cfstring(type_id: &CFString) -> Option<Self> {
use system_configuration_sys::network_configuration::*;
let id_is_equal_to = |const_str| -> bool {
let const_str = unsafe { CFString::wrap_under_get_rule(const_str) };
&const_str == type_id
};
unsafe {
if id_is_equal_to(kSCNetworkInterfaceType6to4) {
Some(SCNetworkInterfaceType::SixToFour)
} else if id_is_equal_to(kSCNetworkInterfaceTypeBluetooth) {
Some(SCNetworkInterfaceType::Bluetooth)
} else if id_is_equal_to(kSCNetworkInterfaceTypeBridge) {
Some(SCNetworkInterfaceType::Bridge)
} else if id_is_equal_to(kSCNetworkInterfaceTypeBond) {
Some(SCNetworkInterfaceType::Bond)
} else if id_is_equal_to(kSCNetworkInterfaceTypeEthernet) {
Some(SCNetworkInterfaceType::Ethernet)
} else if id_is_equal_to(kSCNetworkInterfaceTypeFireWire) {
Some(SCNetworkInterfaceType::FireWire)
} else if id_is_equal_to(kSCNetworkInterfaceTypeIEEE80211) {
Some(SCNetworkInterfaceType::IEEE80211)
} else if id_is_equal_to(kSCNetworkInterfaceTypeIPSec) {
Some(SCNetworkInterfaceType::IPSec)
} else if id_is_equal_to(kSCNetworkInterfaceTypeIrDA) {
Some(SCNetworkInterfaceType::IrDA)
} else if id_is_equal_to(kSCNetworkInterfaceTypeL2TP) {
Some(SCNetworkInterfaceType::L2TP)
} else if id_is_equal_to(kSCNetworkInterfaceTypeModem) {
Some(SCNetworkInterfaceType::Modem)
} else if id_is_equal_to(kSCNetworkInterfaceTypePPP) {
Some(SCNetworkInterfaceType::PPP)
} else if id_is_equal_to(kSCNetworkInterfaceTypePPTP) {
Some(SCNetworkInterfaceType::PPTP)
} else if id_is_equal_to(kSCNetworkInterfaceTypeSerial) {
Some(SCNetworkInterfaceType::Serial)
} else if id_is_equal_to(kSCNetworkInterfaceTypeVLAN) {
Some(SCNetworkInterfaceType::VLAN)
} else if id_is_equal_to(kSCNetworkInterfaceTypeWWAN) {
Some(SCNetworkInterfaceType::WWAN)
} else if id_is_equal_to(kSCNetworkInterfaceTypeIPv4) {
Some(SCNetworkInterfaceType::IPv4)
} else {
None
}
}
}
}
/// Retrieve all current network interfaces
///
/// See [`SCNetworkInterfaceCopyAll`] for more details.
///
/// [`SCNetworkInterfaceCopyAll`]: https://developer.apple.com/documentation/systemconfiguration/1517090-scnetworkinterfacecopyall?language=objc
pub fn get_interfaces() -> CFArray<SCNetworkInterface> {
unsafe { CFArray::<SCNetworkInterface>::wrap_under_create_rule(SCNetworkInterfaceCopyAll()) }
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_get_all_interfaces() {
let _ = get_interfaces();
}
#[test]
fn test_get_type() {
for iface in get_interfaces().into_iter() {
if iface.interface_type().is_none() {
panic!(
"Interface {:?} ({:?}) has unrecognized type {:?}",
iface.display_name(),
iface.bsd_name(),
iface.interface_type_string()
)
}
}
}
}