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
use std::convert::TryInto;
use std::fmt;

use neli::nlattr::AttrHandle;
use neli::nlattr::Nlattr;

use super::attributes::Attribute;
use super::error::AttrParseError;
use super::netlink::AttributeParser;
use super::netlink::PayloadParser;

#[derive(Debug, Clone, Default)]
/// Interface information returned from netlink.
pub struct WirelessInterface {
    // Network interface index.
    pub index: u32,
    // Network interface name.
    pub name: String,
    // Network SSID.
    pub ssid: Option<String>,
    // Interface MAC address (BSSID).
    pub mac: Option<MacAddress>,
    // Channel frequency in MHz.
    pub frequency: Option<u32>,
    // Center frequency of the first part of the channel, used for anything but 20 MHz bandwidth.
    pub center_frequency1: Option<u32>,
    // Center frequency of the second part of the channel, used only for 80+80 MHz bandwidth.
    pub center_frequency2: Option<u32>,
    // Wireless channel width.
    pub channel_width: Option<ChannelWidth>,
    // Transmit power level (s16) in dBm.
    pub tx_power: Option<u32>,
}

impl AttributeParser for WirelessInterface {
    fn parse(handle: AttrHandle<Attribute>) -> Result<Self, AttrParseError> {
        let mut interface = WirelessInterface::default();
        for attr in handle.iter() {
            match attr.nla_type {
                Attribute::Ifindex => {
                    interface.index = u32::parse(&attr)?;
                }
                Attribute::Ifname => {
                    interface.name = String::from_utf8_lossy(&attr.payload)
                        .trim_matches('\0')
                        .to_string();
                }
                Attribute::Ssid => {
                    interface.ssid = Some(String::from_utf8_lossy(&attr.payload).to_string());
                }
                Attribute::Mac => interface.mac = Some(MacAddress::parse(&attr)?),
                Attribute::WiphyFreq => {
                    interface.frequency = Some(u32::parse(&attr)?);
                }
                Attribute::CenterFreq1 => {
                    interface.center_frequency1 = Some(u32::parse(&attr)?);
                }
                Attribute::CenterFreq2 => {
                    interface.center_frequency2 = Some(u32::parse(&attr)?);
                }
                Attribute::ChannelWidth => {
                    let attr_channel_width = u32::parse(&attr)?;
                    interface.channel_width = Some(attr_channel_width.into());
                }
                Attribute::WiphyTxPowerLevel => {
                    interface.tx_power = Some(u32::parse(&attr)?);
                }
                _ => (),
            }
        }
        Ok(interface)
    }
}

#[derive(Debug, Copy, Clone)]
pub struct MacAddress {
    address_bytes: [u8; 6],
}

impl MacAddress {
    pub fn as_bytes(&self) -> [u8; 6] {
        self.address_bytes
    }
}

impl fmt::Display for MacAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let hex: Vec<String> = self
            .address_bytes
            .iter()
            .map(|x| format!("{:X?}", x))
            .collect();
        let hex = hex.join(":");
        write!(f, "{}", hex)
    }
}

impl PayloadParser for MacAddress {
    fn parse(attr: &Nlattr<Attribute, Vec<u8>>) -> Result<Self, AttrParseError> {
        let payload: &[u8] = &attr.payload;
        let payload = payload
            .try_into()
            .map_err(|e| AttrParseError::new(e, attr.nla_type.clone()))?;
        Ok(MacAddress {
            address_bytes: payload,
        })
    }
}

#[derive(Debug, Copy, Clone)]
pub enum ChannelWidth {
    Width20NoHT,
    Width20,
    Width40,
    Width80,
    Width80P80,
    Width160,
    Width5,
    Width10,
    Width1,
    Width2,
    Width4,
    Width8,
    Width16,
    Unknown,
}

impl From<u32> for ChannelWidth {
    fn from(attr_channel_width: u32) -> Self {
        match attr_channel_width {
            0 => ChannelWidth::Width20NoHT,
            1 => ChannelWidth::Width20,
            2 => ChannelWidth::Width40,
            3 => ChannelWidth::Width80,
            4 => ChannelWidth::Width80P80,
            5 => ChannelWidth::Width160,
            6 => ChannelWidth::Width5,
            7 => ChannelWidth::Width10,
            8 => ChannelWidth::Width1,
            9 => ChannelWidth::Width2,
            10 => ChannelWidth::Width4,
            11 => ChannelWidth::Width8,
            12 => ChannelWidth::Width16,
            _ => ChannelWidth::Unknown,
        }
    }
}

impl From<ChannelWidth> for u32 {
    fn from(attr_channel_width: ChannelWidth) -> Self {
        match attr_channel_width {
            ChannelWidth::Width20NoHT => 20,
            ChannelWidth::Width20 => 20,
            ChannelWidth::Width40 => 40,
            ChannelWidth::Width80 => 80,
            ChannelWidth::Width80P80 => 80,
            ChannelWidth::Width160 => 160,
            ChannelWidth::Width5 => 5,
            ChannelWidth::Width10 => 10,
            ChannelWidth::Width1 => 1,
            ChannelWidth::Width2 => 2,
            ChannelWidth::Width4 => 4,
            ChannelWidth::Width8 => 8,
            ChannelWidth::Width16 => 16,
            ChannelWidth::Unknown => 0,
        }
    }
}

impl fmt::Display for ChannelWidth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let channel_width = match self {
            ChannelWidth::Width20NoHT => "20 MHz non-HT",
            ChannelWidth::Width20 => "20 MHz HT",
            ChannelWidth::Width40 => "40 MHz",
            ChannelWidth::Width80 => "80 MHz",
            ChannelWidth::Width80P80 => "80+80 MHz",
            ChannelWidth::Width160 => "160 MHz",
            ChannelWidth::Width5 => "5 MHz OFDM",
            ChannelWidth::Width10 => "10 MHz OFDM",
            ChannelWidth::Width1 => "1 MHz OFDM",
            ChannelWidth::Width2 => "2 MHz OFDM",
            ChannelWidth::Width4 => "4 MHz OFDM",
            ChannelWidth::Width8 => "8 MHz OFDM",
            ChannelWidth::Width16 => "16 MHz OFDM",
            ChannelWidth::Unknown => "Unknown channel width",
        };
        write!(f, "{}", channel_width)
    }
}