netdev/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    /// Proprietary virtual/internal interface
35    ProprietaryVirtual,
36    /// The network interface using a Fast Ethernet connection over twisted pair and provides a data rate of 100 megabits per second (100BASE-T)
37    FastEthernetT,
38    /// The network interface using a connection configured for ISDN and the X.25 protocol.
39    Isdn,
40    /// The network interface using a Fast Ethernet connection over optical fiber and provides a data rate of 100 megabits per second (100Base-FX)
41    FastEthernetFx,
42    /// The network interface using a wireless LAN connection (IEEE 802.11)
43    Wireless80211,
44    /// The network interface using an Asymmetric Digital Subscriber Line (ADSL)
45    AsymmetricDsl,
46    /// The network interface using a Rate Adaptive Digital Subscriber Line (RADSL)
47    RateAdaptDsl,
48    /// The network interface using a Symmetric Digital Subscriber Line (SDSL)
49    SymmetricDsl,
50    /// The network interface using a Very High Data Rate Digital Subscriber Line (VDSL)
51    VeryHighSpeedDsl,
52    /// The network interface using the Internet Protocol (IP) in combination with asynchronous transfer mode (ATM) for data transmission
53    IPOverAtm,
54    /// The network interface using a gigabit Ethernet connection and provides a data rate of 1,000 megabits per second (1 gigabit per second)
55    GigabitEthernet,
56    /// The network interface using a tunnel connection
57    Tunnel,
58    /// The network interface using a Multirate Digital Subscriber Line
59    MultiRateSymmetricDsl,
60    /// The network interface using a High Performance Serial Bus
61    HighPerformanceSerialBus,
62    /// The network interface using a mobile broadband interface for WiMax devices
63    Wman,
64    /// The network interface using a mobile broadband interface for GSM-based devices
65    Wwanpp,
66    /// The network interface using a mobile broadband interface for CDMA-based devices
67    Wwanpp2,
68    /// Transparent bridge interface
69    Bridge,
70    /// Controller Area Network
71    Can,
72    /// Peer-to-Peer Wireless (Wi-Fi Direct / AWDL)
73    PeerToPeerWireless,
74    /// Unknown interface type with a specific value
75    UnknownWithValue(u32),
76}
77
78impl InterfaceType {
79    /// Returns OS-specific value of InterfaceType
80    #[cfg(target_os = "windows")]
81    pub fn value(&self) -> u32 {
82        match *self {
83            InterfaceType::Unknown => 1,
84            InterfaceType::Ethernet => 6,
85            InterfaceType::TokenRing => 9,
86            InterfaceType::Fddi => 15,
87            InterfaceType::BasicIsdn => 20,
88            InterfaceType::PrimaryIsdn => 21,
89            InterfaceType::Ppp => 23,
90            InterfaceType::Loopback => 24,
91            InterfaceType::Ethernet3Megabit => 26,
92            InterfaceType::Slip => 28,
93            InterfaceType::Atm => 37,
94            InterfaceType::GenericModem => 48,
95            InterfaceType::ProprietaryVirtual => 53,
96            InterfaceType::FastEthernetT => 62,
97            InterfaceType::Isdn => 63,
98            InterfaceType::FastEthernetFx => 69,
99            InterfaceType::Wireless80211 => 71,
100            InterfaceType::AsymmetricDsl => 94,
101            InterfaceType::RateAdaptDsl => 95,
102            InterfaceType::SymmetricDsl => 96,
103            InterfaceType::VeryHighSpeedDsl => 97,
104            InterfaceType::IPOverAtm => 114,
105            InterfaceType::GigabitEthernet => 117,
106            InterfaceType::Tunnel => 131,
107            InterfaceType::MultiRateSymmetricDsl => 143,
108            InterfaceType::HighPerformanceSerialBus => 144,
109            InterfaceType::Wman => 237,
110            InterfaceType::Wwanpp => 243,
111            InterfaceType::Wwanpp2 => 244,
112            InterfaceType::UnknownWithValue(v) => v,
113            _ => u32::MAX,
114        }
115    }
116    /// Returns OS-specific value of InterfaceType
117    #[cfg(any(target_os = "linux", target_os = "android"))]
118    pub fn value(&self) -> u32 {
119        use crate::os::linux::arp;
120
121        match *self {
122            InterfaceType::Ethernet => arp::ARPHRD_ETHER,
123            InterfaceType::TokenRing => arp::ARPHRD_IEEE802,
124            InterfaceType::Fddi => arp::ARPHRD_FDDI,
125            InterfaceType::Ppp => arp::ARPHRD_PPP,
126            InterfaceType::Loopback => arp::ARPHRD_LOOPBACK,
127            InterfaceType::Ethernet3Megabit => arp::ARPHRD_EETHER,
128            InterfaceType::Slip => arp::ARPHRD_SLIP,
129            InterfaceType::Atm => arp::ARPHRD_ATM,
130            InterfaceType::Wireless80211 => arp::ARPHRD_IEEE80211,
131            InterfaceType::Tunnel => arp::ARPHRD_TUNNEL,
132            InterfaceType::Isdn => arp::ARPHRD_X25,
133            InterfaceType::HighPerformanceSerialBus => arp::ARPHRD_IEEE1394,
134            InterfaceType::Can => arp::ARPHRD_CAN,
135            InterfaceType::UnknownWithValue(v) => v,
136            _ => u32::MAX,
137        }
138    }
139    /// Returns OS-specific value of InterfaceType
140    #[cfg(any(
141        target_vendor = "apple",
142        target_os = "openbsd",
143        target_os = "freebsd",
144        target_os = "netbsd"
145    ))]
146    pub fn value(&self) -> u32 {
147        match *self {
148            InterfaceType::Ethernet => 0x6,
149            InterfaceType::TokenRing => 0x9,
150            InterfaceType::Fddi => 0xf,
151            InterfaceType::Isdn => 0x14,
152            InterfaceType::PrimaryIsdn => 0x15,
153            InterfaceType::Ppp => 0x17,
154            InterfaceType::Loopback => 0x18,
155            InterfaceType::Ethernet3Megabit => 0x1a,
156            InterfaceType::Slip => 0x1c,
157            InterfaceType::Atm => 0x25,
158            InterfaceType::GenericModem => 0x30,
159            InterfaceType::Wireless80211 => 0x47,
160            InterfaceType::AsymmetricDsl => 0x95,
161            InterfaceType::RateAdaptDsl => 0x96,
162            InterfaceType::SymmetricDsl => 0x97,
163            InterfaceType::IPOverAtm => 0x31,
164            InterfaceType::HighPerformanceSerialBus => 0x90,
165            InterfaceType::UnknownWithValue(v) => v,
166            _ => u32::MAX,
167        }
168    }
169    /// Returns name of InterfaceType
170    pub fn name(&self) -> String {
171        match *self {
172            InterfaceType::Unknown => String::from("Unknown"),
173            InterfaceType::Ethernet => String::from("Ethernet"),
174            InterfaceType::TokenRing => String::from("Token Ring"),
175            InterfaceType::Fddi => String::from("FDDI"),
176            InterfaceType::BasicIsdn => String::from("Basic ISDN"),
177            InterfaceType::PrimaryIsdn => String::from("Primary ISDN"),
178            InterfaceType::Ppp => String::from("PPP"),
179            InterfaceType::Loopback => String::from("Loopback"),
180            InterfaceType::Ethernet3Megabit => String::from("Ethernet 3 megabit"),
181            InterfaceType::Slip => String::from("SLIP"),
182            InterfaceType::Atm => String::from("ATM"),
183            InterfaceType::GenericModem => String::from("Generic Modem"),
184            InterfaceType::ProprietaryVirtual => String::from("Proprietary Virtual/Internal"),
185            InterfaceType::FastEthernetT => String::from("Fast Ethernet T"),
186            InterfaceType::Isdn => String::from("ISDN"),
187            InterfaceType::FastEthernetFx => String::from("Fast Ethernet FX"),
188            InterfaceType::Wireless80211 => String::from("Wireless IEEE 802.11"),
189            InterfaceType::AsymmetricDsl => String::from("Asymmetric DSL"),
190            InterfaceType::RateAdaptDsl => String::from("Rate Adaptive DSL"),
191            InterfaceType::SymmetricDsl => String::from("Symmetric DSL"),
192            InterfaceType::VeryHighSpeedDsl => String::from("Very High Data Rate DSL"),
193            InterfaceType::IPOverAtm => String::from("IP over ATM"),
194            InterfaceType::GigabitEthernet => String::from("Gigabit Ethernet"),
195            InterfaceType::Tunnel => String::from("Tunnel"),
196            InterfaceType::MultiRateSymmetricDsl => String::from("Multi-Rate Symmetric DSL"),
197            InterfaceType::HighPerformanceSerialBus => String::from("High Performance Serial Bus"),
198            InterfaceType::Bridge => String::from("Bridge"),
199            InterfaceType::Wman => String::from("WMAN"),
200            InterfaceType::Wwanpp => String::from("WWANPP"),
201            InterfaceType::Wwanpp2 => String::from("WWANPP2"),
202            InterfaceType::Can => String::from("CAN"),
203            InterfaceType::PeerToPeerWireless => String::from("Peer-to-Peer Wireless"),
204            InterfaceType::UnknownWithValue(v) => format!("Unknown ({})", v),
205        }
206    }
207}
208
209impl TryFrom<u32> for InterfaceType {
210    type Error = ();
211    fn try_from(v: u32) -> Result<Self, Self::Error> {
212        match v {
213            x if x == InterfaceType::Unknown.value() => Ok(InterfaceType::Unknown),
214            x if x == InterfaceType::Ethernet.value() => Ok(InterfaceType::Ethernet),
215            x if x == InterfaceType::TokenRing.value() => Ok(InterfaceType::TokenRing),
216            x if x == InterfaceType::Fddi.value() => Ok(InterfaceType::Fddi),
217            x if x == InterfaceType::BasicIsdn.value() => Ok(InterfaceType::BasicIsdn),
218            x if x == InterfaceType::PrimaryIsdn.value() => Ok(InterfaceType::PrimaryIsdn),
219            x if x == InterfaceType::Ppp.value() => Ok(InterfaceType::Ppp),
220            x if x == InterfaceType::Loopback.value() => Ok(InterfaceType::Loopback),
221            x if x == InterfaceType::Ethernet3Megabit.value() => {
222                Ok(InterfaceType::Ethernet3Megabit)
223            }
224            x if x == InterfaceType::Slip.value() => Ok(InterfaceType::Slip),
225            x if x == InterfaceType::Atm.value() => Ok(InterfaceType::Atm),
226            x if x == InterfaceType::GenericModem.value() => Ok(InterfaceType::GenericModem),
227            x if x == InterfaceType::ProprietaryVirtual.value() => {
228                Ok(InterfaceType::ProprietaryVirtual)
229            }
230            x if x == InterfaceType::FastEthernetT.value() => Ok(InterfaceType::FastEthernetT),
231            x if x == InterfaceType::Isdn.value() => Ok(InterfaceType::Isdn),
232            x if x == InterfaceType::FastEthernetFx.value() => Ok(InterfaceType::FastEthernetFx),
233            x if x == InterfaceType::Wireless80211.value() => Ok(InterfaceType::Wireless80211),
234            x if x == InterfaceType::AsymmetricDsl.value() => Ok(InterfaceType::AsymmetricDsl),
235            x if x == InterfaceType::RateAdaptDsl.value() => Ok(InterfaceType::RateAdaptDsl),
236            x if x == InterfaceType::SymmetricDsl.value() => Ok(InterfaceType::SymmetricDsl),
237            x if x == InterfaceType::VeryHighSpeedDsl.value() => {
238                Ok(InterfaceType::VeryHighSpeedDsl)
239            }
240            x if x == InterfaceType::IPOverAtm.value() => Ok(InterfaceType::IPOverAtm),
241            x if x == InterfaceType::GigabitEthernet.value() => Ok(InterfaceType::GigabitEthernet),
242            x if x == InterfaceType::Tunnel.value() => Ok(InterfaceType::Tunnel),
243            x if x == InterfaceType::MultiRateSymmetricDsl.value() => {
244                Ok(InterfaceType::MultiRateSymmetricDsl)
245            }
246            x if x == InterfaceType::HighPerformanceSerialBus.value() => {
247                Ok(InterfaceType::HighPerformanceSerialBus)
248            }
249            x if x == InterfaceType::Wman.value() => Ok(InterfaceType::Wman),
250            x if x == InterfaceType::Wwanpp.value() => Ok(InterfaceType::Wwanpp),
251            x if x == InterfaceType::Wwanpp2.value() => Ok(InterfaceType::Wwanpp2),
252            x if x == InterfaceType::Can.value() => Ok(InterfaceType::Can),
253            _ => Ok(InterfaceType::UnknownWithValue(v)),
254        }
255    }
256}