Skip to content

Commit

Permalink
Replace Nl80211Cmd with Nl80211Command
Browse files Browse the repository at this point in the history
API breakage:
 * Replaced `Nl80211Cmd` by `Nl80211Command`.
 * Removed these `Nl80211Message` functions:
    * `new_interface_get()`
    * `new_station_get()`
    * `new_wiphy_get()`
    * `new_scan_get()`

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Oct 3, 2024
1 parent dc5cc16 commit 5293284
Show file tree
Hide file tree
Showing 9 changed files with 759 additions and 827 deletions.
714 changes: 714 additions & 0 deletions src/command.rs

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/iface/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use futures::TryStream;
use netlink_packet_generic::GenlMessage;

use crate::{nl80211_execute, Nl80211Error, Nl80211Handle, Nl80211Message};
use crate::{
nl80211_execute, Nl80211Command, Nl80211Error, Nl80211Handle,
Nl80211Message,
};

pub struct Nl80211InterfaceGetRequest {
handle: Nl80211Handle,
Expand All @@ -20,7 +23,10 @@ impl Nl80211InterfaceGetRequest {
{
let Nl80211InterfaceGetRequest { mut handle } = self;

let nl80211_msg = Nl80211Message::new_interface_get();
let nl80211_msg = Nl80211Message {
cmd: Nl80211Command::GetInterface,
attributes: vec![],
};
nl80211_execute(&mut handle, nl80211_msg).await
}
}
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod attr;
mod channel;
mod command;
mod connection;
mod element;
mod error;
Expand All @@ -26,6 +27,7 @@ pub(crate) mod bytes;

pub use self::attr::Nl80211Attr;
pub use self::channel::Nl80211ChannelWidth;
pub use self::command::Nl80211Command;
#[cfg(feature = "tokio_socket")]
pub use self::connection::new_connection;
pub use self::connection::new_connection_with_socket;
Expand All @@ -42,7 +44,7 @@ pub use self::iface::{
Nl80211IfaceCombLimitAttribute, Nl80211InterfaceGetRequest,
Nl80211InterfaceHandle, Nl80211InterfaceType,
};
pub use self::message::{Nl80211Cmd, Nl80211Message};
pub use self::message::Nl80211Message;
pub use self::mlo::Nl80211MloLink;
pub use self::scan::{
Nl80211BssCapabilities, Nl80211BssInfo, Nl80211BssUseFor,
Expand Down Expand Up @@ -75,10 +77,9 @@ pub use self::wifi7::{
};
pub use self::wiphy::{
Nl80211Band, Nl80211BandInfo, Nl80211BandType, Nl80211BandTypes,
Nl80211CipherSuit, Nl80211Command, Nl80211Frequency, Nl80211FrequencyInfo,
Nl80211IfMode, Nl80211WiphyGetRequest, Nl80211WiphyHandle,
Nl80211WowlanTcpTrigerSupport, Nl80211WowlanTrigerPatternSupport,
Nl80211WowlanTrigersSupport,
Nl80211CipherSuit, Nl80211Frequency, Nl80211FrequencyInfo, Nl80211IfMode,
Nl80211WiphyGetRequest, Nl80211WiphyHandle, Nl80211WowlanTcpTrigerSupport,
Nl80211WowlanTrigerPatternSupport, Nl80211WowlanTrigersSupport,
};

pub(crate) use self::element::Nl80211Elements;
Expand Down
99 changes: 5 additions & 94 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,11 @@ use netlink_packet_utils::{
nla::NlasIterator, DecodeError, Emitable, Parseable, ParseableParametrized,
};

use crate::attr::Nl80211Attr;

const NL80211_CMD_GET_WIPHY: u8 = 1;
const NL80211_CMD_NEW_WIPHY: u8 = 3;
const NL80211_CMD_GET_INTERFACE: u8 = 5;
const NL80211_CMD_NEW_INTERFACE: u8 = 7;
const NL80211_CMD_GET_STATION: u8 = 17;
const NL80211_CMD_NEW_STATION: u8 = 19;
const NL80211_CMD_GET_SCAN: u8 = 32;
const NL80211_CMD_NEW_SCAN_RESULTS: u8 = 34;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Nl80211Cmd {
InterfaceGet,
InterfaceNew,
StationGet,
StationNew,
WiphyGet,
WiphyNew,
ScanGet,
}

impl From<Nl80211Cmd> for u8 {
fn from(cmd: Nl80211Cmd) -> Self {
match cmd {
Nl80211Cmd::InterfaceGet => NL80211_CMD_GET_INTERFACE,
Nl80211Cmd::InterfaceNew => NL80211_CMD_NEW_INTERFACE,
Nl80211Cmd::StationGet => NL80211_CMD_GET_STATION,
Nl80211Cmd::StationNew => NL80211_CMD_NEW_STATION,
Nl80211Cmd::WiphyGet => NL80211_CMD_GET_WIPHY,
Nl80211Cmd::WiphyNew => NL80211_CMD_NEW_WIPHY,
Nl80211Cmd::ScanGet => NL80211_CMD_GET_SCAN,
}
}
}
use crate::{Nl80211Attr, Nl80211Command};

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Nl80211Message {
pub cmd: Nl80211Cmd,
pub cmd: Nl80211Command,
pub attributes: Vec<Nl80211Attr>,
}

Expand All @@ -62,36 +28,6 @@ impl GenlFamily for Nl80211Message {
}
}

impl Nl80211Message {
pub fn new_interface_get() -> Self {
Nl80211Message {
cmd: Nl80211Cmd::InterfaceGet,
attributes: vec![],
}
}

pub fn new_station_get(attributes: Vec<Nl80211Attr>) -> Self {
Nl80211Message {
cmd: Nl80211Cmd::StationGet,
attributes,
}
}

pub fn new_wiphy_get() -> Self {
Nl80211Message {
cmd: Nl80211Cmd::WiphyGet,
attributes: vec![Nl80211Attr::SplitWiphyDump],
}
}

pub fn new_scan_get(attributes: Vec<Nl80211Attr>) -> Self {
Self {
cmd: Nl80211Cmd::ScanGet,
attributes,
}
}
}

impl Emitable for Nl80211Message {
fn buffer_len(&self) -> usize {
self.attributes.as_slice().buffer_len()
Expand All @@ -117,33 +53,8 @@ impl ParseableParametrized<[u8], GenlHeader> for Nl80211Message {
buffer: &[u8],
header: GenlHeader,
) -> Result<Self, DecodeError> {
Ok(match header.cmd {
NL80211_CMD_NEW_INTERFACE => Self {
cmd: Nl80211Cmd::InterfaceNew,
attributes: parse_nlas(buffer)?,
},
NL80211_CMD_GET_STATION => Self {
cmd: Nl80211Cmd::StationGet,
attributes: parse_nlas(buffer)?,
},
NL80211_CMD_NEW_STATION => Self {
cmd: Nl80211Cmd::StationNew,
attributes: parse_nlas(buffer)?,
},
NL80211_CMD_NEW_WIPHY => Self {
cmd: Nl80211Cmd::WiphyNew,
attributes: parse_nlas(buffer)?,
},
NL80211_CMD_NEW_SCAN_RESULTS => Self {
cmd: Nl80211Cmd::ScanGet,
attributes: parse_nlas(buffer)?,
},
cmd => {
return Err(DecodeError::from(format!(
"Unsupported nl80211 reply command: {}",
cmd
)))
}
})
let cmd = Nl80211Command::from(header.cmd);
let attributes = parse_nlas(buffer)?;
Ok(Self { cmd, attributes })
}
}
10 changes: 7 additions & 3 deletions src/scan/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use futures::TryStream;
use netlink_packet_generic::GenlMessage;

use crate::{
nl80211_execute, Nl80211Attr, Nl80211Error, Nl80211Handle, Nl80211Message,
nl80211_execute, Nl80211Attr, Nl80211Command, Nl80211Error, Nl80211Handle,
Nl80211Message,
};

pub struct Nl80211ScanGetRequest {
Expand All @@ -26,8 +27,11 @@ impl Nl80211ScanGetRequest {
if_index,
} = self;

let nlas = vec![Nl80211Attr::IfIndex(if_index)];
let nl80211_msg = Nl80211Message::new_scan_get(nlas);
let attributes = vec![Nl80211Attr::IfIndex(if_index)];
let nl80211_msg = Nl80211Message {
cmd: Nl80211Command::GetScan,
attributes,
};

nl80211_execute(&mut handle, nl80211_msg).await
}
Expand Down
12 changes: 8 additions & 4 deletions src/station/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use futures::TryStream;
use netlink_packet_generic::GenlMessage;

use crate::{
nl80211_execute, Nl80211Attr, Nl80211Error, Nl80211Handle, Nl80211Message,
nl80211_execute, Nl80211Attr, Nl80211Command, Nl80211Error, Nl80211Handle,
Nl80211Message,
};

const ETH_ALEN: usize = 6;
Expand Down Expand Up @@ -38,12 +39,15 @@ impl Nl80211StationGetRequest {
mac_address,
} = self;

let mut nlas = vec![Nl80211Attr::IfIndex(if_index)];
let mut attributes = vec![Nl80211Attr::IfIndex(if_index)];
if let Some(arr) = mac_address {
nlas.push(Nl80211Attr::Mac(arr))
attributes.push(Nl80211Attr::Mac(arr))
}

let nl80211_msg = Nl80211Message::new_station_get(nlas);
let nl80211_msg = Nl80211Message {
cmd: Nl80211Command::GetStation,
attributes,
};

nl80211_execute(&mut handle, nl80211_msg).await
}
Expand Down
Loading

0 comments on commit 5293284

Please sign in to comment.