diff --git a/Cargo.toml b/Cargo.toml index b70f281..15fb565 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "awdl-frame-parser" -version = "0.3.2" +version = "0.3.3" edition = "2021" description = "A parser for AWDL data and action frames." authors = ["Frostie314159"] diff --git a/src/action_frame.rs b/src/action_frame.rs index 4f90a16..2947647 100644 --- a/src/action_frame.rs +++ b/src/action_frame.rs @@ -9,7 +9,7 @@ use core::fmt::Debug; use core::{fmt::Debug, iter::repeat}; use crate::{ - common::LabelIterator, + common::ReadLabelIterator, tlvs::{dns_sd::ReadValueIterator, sync_elect::ReadMACIterator, AWDLTLV}, }; @@ -49,7 +49,7 @@ impl<'a> AWDLActionFrame<'a> { pub fn get_named_tlvs( &'a self, ) -> impl Iterator< - Item = AWDLTLV<'a, ReadMACIterator<'a>, LabelIterator<'a>, ReadValueIterator<'a>>, + Item = AWDLTLV<'a, ReadMACIterator<'a>, ReadLabelIterator<'a>, ReadValueIterator<'a>>, > + Clone { repeat(()).scan(0, |offset, _| self.tagged_data.gread(offset).ok()) } diff --git a/src/common/awdl_dns_name.rs b/src/common/awdl_dns_name.rs index e6571a8..5f9fcfa 100644 --- a/src/common/awdl_dns_name.rs +++ b/src/common/awdl_dns_name.rs @@ -12,27 +12,27 @@ use crate::tlvs::RawAWDLTLV; use super::{awdl_dns_compression::AWDLDnsCompression, awdl_str::AWDLStr}; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub struct LabelIterator<'a> { +pub struct ReadLabelIterator<'a> { bytes: &'a [u8], offset: usize, } -impl<'a> LabelIterator<'a> { +impl<'a> ReadLabelIterator<'a> { pub const fn new(bytes: &'a [u8]) -> Self { Self { bytes, offset: 0 } } } -impl MeasureWith<()> for LabelIterator<'_> { +impl MeasureWith<()> for ReadLabelIterator<'_> { fn measure_with(&self, _ctx: &()) -> usize { self.bytes.len() } } -impl<'a> Iterator for LabelIterator<'a> { +impl<'a> Iterator for ReadLabelIterator<'a> { type Item = AWDLStr<'a>; fn next(&mut self) -> Option { self.bytes.gread(&mut self.offset).ok() } } -impl ExactSizeIterator for LabelIterator<'_> { +impl ExactSizeIterator for ReadLabelIterator<'_> { fn len(&self) -> usize { repeat(()) .scan(0usize, |offset, _| { @@ -88,7 +88,7 @@ where + 2 } } -impl<'a> TryFromCtx<'a> for AWDLDnsName> { +impl<'a> TryFromCtx<'a> for AWDLDnsName> { type Error = scroll::Error; fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> { let mut offset = 0; @@ -97,7 +97,7 @@ impl<'a> TryFromCtx<'a> for AWDLDnsName> { AWDLDnsCompression::from_representation(from.gread_with(&mut offset, NETWORK)?); Ok(( Self { - labels: LabelIterator::new(label_bytes), + labels: ReadLabelIterator::new(label_bytes), domain, }, offset, @@ -137,7 +137,7 @@ fn test_dns_name() { 0x04, b'a', b'w', b'd', b'l', 0x04, b'a', b'w', b'd', b'l', 0xc0, 0x0c, ] .as_slice(); - let dns_name = bytes.pread::>(0).unwrap(); + let dns_name = bytes.pread::>(0).unwrap(); assert_eq!( dns_name, AWDLDnsName { diff --git a/src/common/mod.rs b/src/common/mod.rs index 7a1f3e4..26a691b 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -4,6 +4,6 @@ mod awdl_str; mod awdl_version; pub use awdl_dns_compression::AWDLDnsCompression; -pub use awdl_dns_name::{AWDLDnsName, LabelIterator}; +pub use awdl_dns_name::{AWDLDnsName, ReadLabelIterator}; pub use awdl_str::AWDLStr; pub use awdl_version::AWDLVersion; diff --git a/src/tlvs/dns_sd/arpa_tlv.rs b/src/tlvs/dns_sd/arpa_tlv.rs index c4447bc..12e6f39 100644 --- a/src/tlvs/dns_sd/arpa_tlv.rs +++ b/src/tlvs/dns_sd/arpa_tlv.rs @@ -3,7 +3,7 @@ use scroll::{ Pread, Pwrite, }; -use crate::common::{AWDLDnsName, AWDLStr, LabelIterator}; +use crate::common::{AWDLDnsName, AWDLStr, ReadLabelIterator}; #[derive(Clone, Debug, Default, PartialEq, Eq)] /// A TLV containing the hostname of the peer. Used for reverse DNS. @@ -24,7 +24,7 @@ where self.arpa.measure_with(ctx) + 1 } } -impl<'a> TryFromCtx<'a> for ArpaTLV<'a, LabelIterator<'a>> { +impl<'a> TryFromCtx<'a> for ArpaTLV<'a, ReadLabelIterator<'a>> { type Error = scroll::Error; fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> { let mut offset = 0; @@ -57,7 +57,7 @@ fn test_arpa_tlv() { let bytes = &include_bytes!("../../../test_bins/arpa_tlv.bin")[3..]; - let arpa_tlv = bytes.pread::>(0).unwrap(); + let arpa_tlv = bytes.pread::>(0).unwrap(); assert_eq!(arpa_tlv.arpa.domain, AWDLDnsCompression::Local); assert_eq!( arpa_tlv.arpa.labels.collect::>(), @@ -65,7 +65,7 @@ fn test_arpa_tlv() { ); let mut buf = vec![0x00; arpa_tlv.measure_with(&())]; buf.as_mut_slice() - .pwrite::>(arpa_tlv, 0) + .pwrite::>(arpa_tlv, 0) .unwrap(); assert_eq!(buf, bytes); } diff --git a/src/tlvs/dns_sd/service_response_tlv/dns_record.rs b/src/tlvs/dns_sd/service_response_tlv/dns_record.rs index 1b52280..369db62 100644 --- a/src/tlvs/dns_sd/service_response_tlv/dns_record.rs +++ b/src/tlvs/dns_sd/service_response_tlv/dns_record.rs @@ -4,7 +4,7 @@ use scroll::{ Pread, Pwrite, NETWORK, }; -use crate::common::{AWDLDnsName, AWDLStr, LabelIterator}; +use crate::common::{AWDLDnsName, AWDLStr, ReadLabelIterator}; serializable_enum! { #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)] @@ -136,7 +136,7 @@ where }) + 1 } } -impl<'a> TryFromCtx<'a> for AWDLDnsRecord<'a, LabelIterator<'a>> { +impl<'a> TryFromCtx<'a> for AWDLDnsRecord<'a, ReadLabelIterator<'a>> { type Error = scroll::Error; fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> { let mut offset = 0; @@ -148,7 +148,7 @@ impl<'a> TryFromCtx<'a> for AWDLDnsRecord<'a, LabelIterator<'a>> { domain_name: from.gread(&mut offset)?, }, AWDLDnsRecordType::TXT => Self::TXT { - txt_record: LabelIterator::new(&from[offset..]), + txt_record: ReadLabelIterator::new(&from[offset..]), }, AWDLDnsRecordType::SRV => Self::SRV { priority: from.gread_with(&mut offset, NETWORK)?, diff --git a/src/tlvs/dns_sd/service_response_tlv/mod.rs b/src/tlvs/dns_sd/service_response_tlv/mod.rs index 48378b6..663e38b 100644 --- a/src/tlvs/dns_sd/service_response_tlv/mod.rs +++ b/src/tlvs/dns_sd/service_response_tlv/mod.rs @@ -1,6 +1,6 @@ pub mod dns_record; -use crate::common::{AWDLDnsName, AWDLStr, LabelIterator}; +use crate::common::{AWDLDnsName, AWDLStr, ReadLabelIterator}; use dns_record::AWDLDnsRecord; @@ -43,7 +43,7 @@ where 6 + self.name.measure_with(ctx) + self.record.measure_with(ctx) } } -impl<'a> TryFromCtx<'a> for ServiceResponseTLV<'a, LabelIterator<'a>> { +impl<'a> TryFromCtx<'a> for ServiceResponseTLV<'a, ReadLabelIterator<'a>> { type Error = scroll::Error; fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> { let mut offset = 0; @@ -81,7 +81,7 @@ mod service_response_tests { use scroll::{ctx::MeasureWith, Pread, Pwrite}; use crate::{ - common::{AWDLDnsCompression, AWDLDnsName, LabelIterator}, + common::{AWDLDnsCompression, AWDLDnsName, ReadLabelIterator}, tlvs::dns_sd::{dns_record::AWDLDnsRecord, ServiceResponseTLV}, }; @@ -89,7 +89,7 @@ mod service_response_tests { fn test_service_response_tlv_ptr() { let bytes = &include_bytes!("../../../../test_bins/service_response_tlv_ptr.bin")[3..]; - let service_response_tlv = bytes.pread::>(0).unwrap(); + let service_response_tlv = bytes.pread::>(0).unwrap(); assert_eq!( service_response_tlv, @@ -116,7 +116,7 @@ mod service_response_tests { fn test_service_response_tlv_srv() { let bytes = &include_bytes!("../../../../test_bins/service_response_tlv_srv.bin")[3..]; - let service_response_tlv = bytes.pread::>(0).unwrap(); + let service_response_tlv = bytes.pread::>(0).unwrap(); assert_eq!( service_response_tlv, @@ -146,7 +146,7 @@ mod service_response_tests { fn test_service_response_tlv_txt() { let bytes = &include_bytes!("../../../../test_bins/service_response_tlv_txt.bin")[3..]; - let service_response_tlv = bytes.pread::>(0).unwrap(); + let service_response_tlv = bytes.pread::>(0).unwrap(); assert_eq!( service_response_tlv, diff --git a/src/tlvs/mod.rs b/src/tlvs/mod.rs index baf9044..15d6688 100644 --- a/src/tlvs/mod.rs +++ b/src/tlvs/mod.rs @@ -15,7 +15,7 @@ use scroll::{ }; use tlv_rs::{raw_tlv::RawTLV, TLV}; -use crate::common::{AWDLStr, LabelIterator}; +use crate::common::{AWDLStr, ReadLabelIterator}; use self::{ data_path::{DataPathStateTLV, HTCapabilitiesTLV, IEEE80211ContainerTLV}, @@ -125,7 +125,7 @@ where } } impl<'a> TryFromCtx<'a> - for AWDLTLV<'a, ReadMACIterator<'a>, LabelIterator<'a>, ReadValueIterator<'a>> + for AWDLTLV<'a, ReadMACIterator<'a>, ReadLabelIterator<'a>, ReadValueIterator<'a>> { type Error = scroll::Error; fn try_from_ctx(from: &'a [u8], _ctx: ()) -> Result<(Self, usize), Self::Error> { @@ -294,3 +294,6 @@ where } } } + +/// Default [AWDLTLV] returned by reading. +pub type DefaultAWDLTLV<'a> = AWDLTLV<'a, ReadMACIterator<'a>, ReadLabelIterator<'a>, ReadValueIterator<'a>>;