default_net/interface/
types.rs

1use std::convert::TryFrom;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Type of Network Interface
7#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum InterfaceType {
10    /// Unknown interface type
11    Unknown,
12    /// The network interface using an Ethernet connection
13    Ethernet,
14    /// The network interface using a Token-Ring connection
15    TokenRing,
16    /// The network interface using a Fiber Distributed Data Interface (FDDI) connection
17    Fddi,
18    /// The network interface using a basic rate interface Integrated Services Digital Network (ISDN) connection
19    BasicIsdn,
20    /// The network interface using a primary rate interface Integrated Services Digital Network (ISDN) connection
21    PrimaryIsdn,
22    /// The network interface using a Point-To-Point protocol (PPP) connection
23    Ppp,
24    /// The loopback interface (often used for testing)
25    Loopback,
26    /// The network interface using an Ethernet 3 megabit/second connection
27    Ethernet3Megabit,
28    /// The network interface using a Serial Line Internet Protocol (SLIP) connection
29    Slip,
30    /// The network interface using asynchronous transfer mode (ATM) for data transmission
31    Atm,
32    /// The network interface using a modem
33    GenericModem,
34    /// The network interface using a Fast Ethernet connection over twisted pair and provides a data rate of 100 megabits per second (100BASE-T)
35    FastEthernetT,
36    /// The network interface using a connection configured for ISDN and the X.25 protocol.
37    Isdn,
38    /// The network interface using a Fast Ethernet connection over optical fiber and provides a data rate of 100 megabits per second (100Base-FX)
39    FastEthernetFx,
40    /// The network interface using a wireless LAN connection (IEEE 802.11)
41    Wireless80211,
42    /// The network interface using an Asymmetric Digital Subscriber Line (ADSL)
43    AsymmetricDsl,
44    /// The network interface using a Rate Adaptive Digital Subscriber Line (RADSL)
45    RateAdaptDsl,
46    /// The network interface using a Symmetric Digital Subscriber Line (SDSL)
47    SymmetricDsl,
48    /// The network interface using a Very High Data Rate Digital Subscriber Line (VDSL)
49    VeryHighSpeedDsl,
50    /// The network interface using the Internet Protocol (IP) in combination with asynchronous transfer mode (ATM) for data transmission
51    IPOverAtm,
52    /// The network interface using a gigabit Ethernet connection and provides a data rate of 1,000 megabits per second (1 gigabit per second)
53    GigabitEthernet,
54    /// The network interface using a tunnel connection
55    Tunnel,
56    /// The network interface using a Multirate Digital Subscriber Line
57    MultiRateSymmetricDsl,
58    /// The network interface using a High Performance Serial Bus
59    HighPerformanceSerialBus,
60    /// The network interface using a mobile broadband interface for WiMax devices
61    Wman,
62    /// The network interface using a mobile broadband interface for GSM-based devices
63    Wwanpp,
64    /// The network interface using a mobile broadband interface for CDMA-based devices
65    Wwanpp2,
66}
67
68impl InterfaceType {
69    /// Returns OS-specific value of InterfaceType
70    #[cfg(target_os = "windows")]
71    pub fn value(&self) -> u32 {
72        match *self {
73            InterfaceType::Unknown => 1,
74            InterfaceType::Ethernet => 6,
75            InterfaceType::TokenRing => 9,
76            InterfaceType::Fddi => 15,
77            InterfaceType::BasicIsdn => 20,
78            InterfaceType::PrimaryIsdn => 21,
79            InterfaceType::Ppp => 23,
80            InterfaceType::Loopback => 24,
81            InterfaceType::Ethernet3Megabit => 26,
82            InterfaceType::Slip => 28,
83            InterfaceType::Atm => 37,
84            InterfaceType::GenericModem => 48,
85            InterfaceType::FastEthernetT => 62,
86            InterfaceType::Isdn => 63,
87            InterfaceType::FastEthernetFx => 69,
88            InterfaceType::Wireless80211 => 71,
89            InterfaceType::AsymmetricDsl => 94,
90            InterfaceType::RateAdaptDsl => 95,
91            InterfaceType::SymmetricDsl => 96,
92            InterfaceType::VeryHighSpeedDsl => 97,
93            InterfaceType::IPOverAtm => 114,
94            InterfaceType::GigabitEthernet => 117,
95            InterfaceType::Tunnel => 131,
96            InterfaceType::MultiRateSymmetricDsl => 143,
97            InterfaceType::HighPerformanceSerialBus => 144,
98            InterfaceType::Wman => 237,
99            InterfaceType::Wwanpp => 243,
100            InterfaceType::Wwanpp2 => 244,
101        }
102    }
103    /// Returns OS-specific value of InterfaceType
104    #[cfg(any(target_os = "linux", target_os = "android"))]
105    pub fn value(&self) -> u32 {
106        match *self {
107            InterfaceType::Ethernet => 1,
108            InterfaceType::TokenRing => 4,
109            InterfaceType::Fddi => 774,
110            InterfaceType::Ppp => 512,
111            InterfaceType::Loopback => 772,
112            InterfaceType::Ethernet3Megabit => 2,
113            InterfaceType::Slip => 256,
114            InterfaceType::Atm => 19,
115            InterfaceType::Wireless80211 => 801,
116            InterfaceType::Tunnel => 768,
117            _ => u32::MAX,
118        }
119    }
120    /// Returns OS-specific value of InterfaceType
121    #[cfg(any(
122        target_os = "macos",
123        target_os = "openbsd",
124        target_os = "freebsd",
125        target_os = "netbsd",
126        target_os = "ios"
127    ))]
128    pub fn value(&self) -> u32 {
129        // TODO
130        match *self {
131            _ => 0,
132        }
133    }
134    /// Returns name of InterfaceType
135    pub fn name(&self) -> String {
136        match *self {
137            InterfaceType::Unknown => String::from("Unknown"),
138            InterfaceType::Ethernet => String::from("Ethernet"),
139            InterfaceType::TokenRing => String::from("Token Ring"),
140            InterfaceType::Fddi => String::from("FDDI"),
141            InterfaceType::BasicIsdn => String::from("Basic ISDN"),
142            InterfaceType::PrimaryIsdn => String::from("Primary ISDN"),
143            InterfaceType::Ppp => String::from("PPP"),
144            InterfaceType::Loopback => String::from("Loopback"),
145            InterfaceType::Ethernet3Megabit => String::from("Ethernet 3 megabit"),
146            InterfaceType::Slip => String::from("SLIP"),
147            InterfaceType::Atm => String::from("ATM"),
148            InterfaceType::GenericModem => String::from("Generic Modem"),
149            InterfaceType::FastEthernetT => String::from("Fast Ethernet T"),
150            InterfaceType::Isdn => String::from("ISDN"),
151            InterfaceType::FastEthernetFx => String::from("Fast Ethernet FX"),
152            InterfaceType::Wireless80211 => String::from("Wireless IEEE 802.11"),
153            InterfaceType::AsymmetricDsl => String::from("Asymmetric DSL"),
154            InterfaceType::RateAdaptDsl => String::from("Rate Adaptive DSL"),
155            InterfaceType::SymmetricDsl => String::from("Symmetric DSL"),
156            InterfaceType::VeryHighSpeedDsl => String::from("Very High Data Rate DSL"),
157            InterfaceType::IPOverAtm => String::from("IP over ATM"),
158            InterfaceType::GigabitEthernet => String::from("Gigabit Ethernet"),
159            InterfaceType::Tunnel => String::from("Tunnel"),
160            InterfaceType::MultiRateSymmetricDsl => String::from("Multi-Rate Symmetric DSL"),
161            InterfaceType::HighPerformanceSerialBus => String::from("High Performance Serial Bus"),
162            InterfaceType::Wman => String::from("WMAN"),
163            InterfaceType::Wwanpp => String::from("WWANPP"),
164            InterfaceType::Wwanpp2 => String::from("WWANPP2"),
165        }
166    }
167}
168
169impl TryFrom<u32> for InterfaceType {
170    type Error = ();
171    fn try_from(v: u32) -> Result<Self, Self::Error> {
172        match v {
173            x if x == InterfaceType::Unknown.value() => Ok(InterfaceType::Unknown),
174            x if x == InterfaceType::Ethernet.value() => Ok(InterfaceType::Ethernet),
175            x if x == InterfaceType::TokenRing.value() => Ok(InterfaceType::TokenRing),
176            x if x == InterfaceType::Fddi.value() => Ok(InterfaceType::Fddi),
177            x if x == InterfaceType::BasicIsdn.value() => Ok(InterfaceType::BasicIsdn),
178            x if x == InterfaceType::PrimaryIsdn.value() => Ok(InterfaceType::PrimaryIsdn),
179            x if x == InterfaceType::Ppp.value() => Ok(InterfaceType::Ppp),
180            x if x == InterfaceType::Loopback.value() => Ok(InterfaceType::Loopback),
181            x if x == InterfaceType::Ethernet3Megabit.value() => {
182                Ok(InterfaceType::Ethernet3Megabit)
183            }
184            x if x == InterfaceType::Slip.value() => Ok(InterfaceType::Slip),
185            x if x == InterfaceType::Atm.value() => Ok(InterfaceType::Atm),
186            x if x == InterfaceType::GenericModem.value() => Ok(InterfaceType::GenericModem),
187            x if x == InterfaceType::FastEthernetT.value() => Ok(InterfaceType::FastEthernetT),
188            x if x == InterfaceType::Isdn.value() => Ok(InterfaceType::Isdn),
189            x if x == InterfaceType::FastEthernetFx.value() => Ok(InterfaceType::FastEthernetFx),
190            x if x == InterfaceType::Wireless80211.value() => Ok(InterfaceType::Wireless80211),
191            x if x == InterfaceType::AsymmetricDsl.value() => Ok(InterfaceType::AsymmetricDsl),
192            x if x == InterfaceType::RateAdaptDsl.value() => Ok(InterfaceType::RateAdaptDsl),
193            x if x == InterfaceType::SymmetricDsl.value() => Ok(InterfaceType::SymmetricDsl),
194            x if x == InterfaceType::VeryHighSpeedDsl.value() => {
195                Ok(InterfaceType::VeryHighSpeedDsl)
196            }
197            x if x == InterfaceType::IPOverAtm.value() => Ok(InterfaceType::IPOverAtm),
198            x if x == InterfaceType::GigabitEthernet.value() => Ok(InterfaceType::GigabitEthernet),
199            x if x == InterfaceType::Tunnel.value() => Ok(InterfaceType::Tunnel),
200            x if x == InterfaceType::MultiRateSymmetricDsl.value() => {
201                Ok(InterfaceType::MultiRateSymmetricDsl)
202            }
203            x if x == InterfaceType::HighPerformanceSerialBus.value() => {
204                Ok(InterfaceType::HighPerformanceSerialBus)
205            }
206            x if x == InterfaceType::Wman.value() => Ok(InterfaceType::Wman),
207            x if x == InterfaceType::Wwanpp.value() => Ok(InterfaceType::Wwanpp),
208            x if x == InterfaceType::Wwanpp2.value() => Ok(InterfaceType::Wwanpp2),
209            _ => Err(()),
210        }
211    }
212}