Skip to content

Commit d255eba

Browse files
committed
Use RawNl80211Elements (#1)
1 parent 9b632a2 commit d255eba

4 files changed

Lines changed: 64 additions & 19 deletions

File tree

examples/dump_nl80211_scan.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::env::args;
44

55
use futures::stream::TryStreamExt;
6-
use netlink_packet_core::{DecodeError, ErrorContext};
6+
use netlink_packet_core::{DecodeError, Emitable, ErrorContext, Parseable};
77

88
fn main() -> Result<(), Box<dyn std::error::Error>> {
99
let argv: Vec<_> = args().collect();
@@ -41,5 +41,21 @@ async fn dump_scan(if_index: u32) {
4141
assert!(!msgs.is_empty());
4242
for msg in msgs {
4343
println!("{msg:?}");
44+
45+
println!("Information Elements");
46+
let ie: Vec<_> =
47+
msg.payload.attributes.iter().filter_map(|attr| match attr {
48+
wl_nl80211::Nl80211Attr::Bss(info) => {
49+
let ies: Vec<_> = info.iter().filter_map(|info| match info {
50+
wl_nl80211::Nl80211BssInfo::BeaconInformationElements(ie)| wl_nl80211::Nl80211BssInfo::InformationElements(ie) | wl_nl80211::Nl80211BssInfo::ProbeResponseInformationElements(ie) =>
51+
Some(wl_nl80211::Nl80211Elements::parse(ie).unwrap()),
52+
_ => None,
53+
}).collect();
54+
Some(ies)
55+
},
56+
_ => None,
57+
}).flatten().collect();
58+
59+
println!("{ie:?}");
4460
}
4561
}

src/element.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,41 @@ use crate::{
1010
Nl80211ElementHtCap,
1111
};
1212

13-
pub(crate) struct Nl80211Elements(Vec<Nl80211Element>);
13+
/// Unparsed representation of IEEE 802.11-2020 `9.4.2 Elements`
14+
#[derive(Debug, PartialEq, Eq, Clone)]
15+
pub struct RawNl80211Elements(pub Vec<u8>);
16+
17+
impl<T: AsRef<[u8]> + ?Sized> Parseable<T> for RawNl80211Elements {
18+
fn parse(raw: &T) -> Result<Self, DecodeError> {
19+
let v = Vec::from(raw.as_ref());
20+
Ok(RawNl80211Elements::from(v))
21+
}
22+
}
23+
24+
impl Parseable<RawNl80211Elements> for Nl80211Elements {
25+
fn parse(raw: &RawNl80211Elements) -> Result<Self, DecodeError> {
26+
Nl80211Elements::parse(&raw.0)
27+
}
28+
}
29+
30+
impl Emitable for RawNl80211Elements {
31+
fn buffer_len(&self) -> usize {
32+
self.0.len()
33+
}
34+
35+
fn emit(&self, buffer: &mut [u8]) {
36+
buffer.copy_from_slice(self.0.as_slice());
37+
}
38+
}
39+
40+
impl From<Vec<u8>> for RawNl80211Elements {
41+
fn from(value: Vec<u8>) -> Self {
42+
RawNl80211Elements(value)
43+
}
44+
}
45+
46+
#[derive(Debug, PartialEq, Eq, Clone)]
47+
pub struct Nl80211Elements(Vec<Nl80211Element>);
1448

1549
impl<T: AsRef<[u8]> + ?Sized> Parseable<T> for Nl80211Elements {
1650
fn parse(buf: &T) -> Result<Self, DecodeError> {

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use self::command::Nl80211Command;
3535
#[cfg(feature = "tokio_socket")]
3636
pub use self::connection::new_connection;
3737
pub use self::connection::new_connection_with_socket;
38-
pub use self::element::Nl80211Element;
38+
pub use self::element::{Nl80211Element, Nl80211Elements, RawNl80211Elements};
3939
pub use self::error::Nl80211Error;
4040
pub use self::ext_cap::{
4141
Nl80211ExtendedCapability, Nl80211IfTypeExtCapa, Nl80211IfTypeExtCapas,
@@ -95,7 +95,6 @@ pub use self::wiphy::{
9595
Nl80211WowlanTrigerPatternSupport, Nl80211WowlanTriggersSupport,
9696
};
9797

98-
pub(crate) use self::element::Nl80211Elements;
9998
pub(crate) use self::feature::Nl80211ExtFeatures;
10099
pub(crate) use self::handle::nl80211_execute;
101100
pub(crate) use self::iface::Nl80211InterfaceTypes;

src/scan/bss_info.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use netlink_packet_core::{
3939

4040
use crate::{
4141
bytes::{write_i32, write_u16, write_u32, write_u64},
42-
Nl80211Element, Nl80211Elements,
42+
RawNl80211Elements,
4343
};
4444

4545
bitflags::bitflags! {
@@ -155,15 +155,15 @@ pub enum Nl80211BssInfo {
155155
/// Beacon interval of the (I)BSS
156156
BeaconInterval(u16),
157157
Capability(Nl80211BssCapabilities),
158-
InformationElements(Vec<Nl80211Element>),
158+
InformationElements(RawNl80211Elements),
159159
SignalMbm(i32),
160160
SignalUnspec(u8),
161161
Status(u32),
162162
SeenMsAgo(u32),
163-
BeaconInformationElements(Vec<Nl80211Element>),
163+
BeaconInformationElements(RawNl80211Elements),
164164
ChanWidth(u32),
165165
BeaconTsf(u64),
166-
ProbeResponseInformationElements(Vec<Nl80211Element>),
166+
ProbeResponseInformationElements(RawNl80211Elements),
167167
/// `CLOCK_BOOTTIME` timestamp when this entry was last updated by a
168168
/// received frame. The value is expected to be accurate to about 10ms.
169169
/// (u64, nanoseconds)
@@ -189,9 +189,7 @@ impl Nla for Nl80211BssInfo {
189189
Self::BeaconTsf(_) | Self::Tsf(_) | Self::LastSeenBootTime(_) => 8,
190190
Self::InformationElements(v)
191191
| Self::BeaconInformationElements(v)
192-
| Self::ProbeResponseInformationElements(v) => {
193-
Nl80211Elements::from(v).buffer_len()
194-
}
192+
| Self::ProbeResponseInformationElements(v) => v.buffer_len(),
195193
Self::Capability(_) => Nl80211BssCapabilities::LENGTH,
196194
Self::UseFor(_) => Nl80211BssUseFor::LENGTH,
197195
Self::Other(attr) => attr.value_len(),
@@ -237,9 +235,7 @@ impl Nla for Nl80211BssInfo {
237235
}
238236
Self::InformationElements(v)
239237
| Self::BeaconInformationElements(v)
240-
| Self::ProbeResponseInformationElements(v) => {
241-
Nl80211Elements::from(v).emit(buffer)
242-
}
238+
| Self::ProbeResponseInformationElements(v) => v.emit(buffer),
243239
Self::Capability(v) => v.emit(buffer),
244240
Self::UseFor(v) => v.emit(buffer),
245241
Self::Other(ref attr) => attr.emit(buffer),
@@ -284,13 +280,13 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
284280
Self::Capability(Nl80211BssCapabilities::parse(payload)?)
285281
}
286282
NL80211_BSS_BEACON_IES => Self::BeaconInformationElements(
287-
Nl80211Elements::parse(payload)?.into(),
288-
),
289-
NL80211_BSS_INFORMATION_ELEMENTS => Self::InformationElements(
290-
Nl80211Elements::parse(payload)?.into(),
283+
RawNl80211Elements::parse(payload)?,
291284
),
285+
NL80211_BSS_INFORMATION_ELEMENTS => {
286+
Self::InformationElements(RawNl80211Elements::parse(payload)?)
287+
}
292288
NL80211_BSS_PRESP_DATA => Self::ProbeResponseInformationElements(
293-
Nl80211Elements::parse(payload)?.into(),
289+
RawNl80211Elements::parse(payload)?,
294290
),
295291
NL80211_BSS_SIGNAL_MBM => {
296292
let err_msg =

0 commit comments

Comments
 (0)