Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/decode/domain_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,82 @@ impl<'a, 'b: 'a> Decoder<'a, 'b> {
}
}
}

pub(super) fn domain_name_without_compression(&mut self) -> DecodeResult<DomainName> {
let mut domain_name = DomainName::default();

loop {
match self.domain_name_length()? {
DomainNameLength::Compressed(offset) => {
return Err(DecodeError::DomainNameCompressed(offset))
}
DomainNameLength::Label(0) => return Ok(domain_name),
DomainNameLength::Label(length) => {
self.domain_name_label(&mut domain_name, length)?
}
}
}
}
}

impl_decode!(DomainName, domain_name);
impl_decode!(
DomainName,
domain_name_without_compression,
decode_without_compression
);

#[test]
fn domain_name_without_compression_1() {
let domain_name: DomainName = "example.org.".parse().unwrap();
let mut decoder = Decoder::main(bytes::Bytes::copy_from_slice(
&b"\x07example\x03org\0\x07example\x03org\0"[..],
));
assert_eq!(
decoder.domain_name_without_compression().unwrap(),
domain_name
);
assert_eq!(
decoder.domain_name_without_compression().unwrap(),
domain_name
);
assert!(decoder.is_finished().unwrap());
}

#[test]
fn domain_name_without_compression_2() {
let domain_name: DomainName = "example.org.".parse().unwrap();
let mut decoder = Decoder::main(bytes::Bytes::copy_from_slice(
&b"\x07example\x03org\0\x07example\x03org\0"[..],
));
assert_eq!(
decoder.domain_name_without_compression().unwrap(),
domain_name
);
assert_eq!(
decoder.domain_name_without_compression().unwrap(),
domain_name
);
assert!(decoder.is_finished().unwrap());
}

#[test]
fn domain_name_without_compression_3() {
let domain_name: DomainName = "example.org.".parse().unwrap();
let mut decoder = Decoder::main(bytes::Bytes::copy_from_slice(
&b"\x07example\x03org\0\x07example\x03org\0\xc0\r"[..],
));
assert_eq!(
decoder.domain_name_without_compression().unwrap(),
domain_name
);
assert_eq!(
decoder.domain_name_without_compression().unwrap(),
domain_name
);
assert_eq!(
decoder.domain_name_without_compression(),
Err(DecodeError::DomainNameCompressed(13))
);
assert!(decoder.is_finished().unwrap());
}
2 changes: 2 additions & 0 deletions src/decode/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@ pub enum DecodeError {
TagError(#[from] TagError),
#[error("ECH length mismatch. Expected {0} got {1}")]
ECHLengthMismatch(usize, usize),
#[error("The domain name is compressed: {0}")]
DomainNameCompressed(u16),
}
5 changes: 4 additions & 1 deletion src/decode/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
macro_rules! impl_decode {
($i:path, $m:ident) => {
impl_decode!($i, $m, decode);
};
($i:path, $m:ident, $n:ident) => {
impl $i {
pub fn decode(bytes: bytes::Bytes) -> crate::DecodeResult<$i> {
pub fn $n(bytes: bytes::Bytes) -> crate::DecodeResult<$i> {
let mut decoder = crate::decode::Decoder::main(bytes);
decoder.$m()
}
Expand Down
45 changes: 45 additions & 0 deletions src/encode/domain_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ impl Encoder {
self.merge_domain_name_index(domain_name_index, 0)?;
Ok(())
}

pub(super) fn domain_name_without_compression(
&mut self,
domain_name: &DomainName,
) -> EncodeResult<()> {
for (label, _) in domain_name.iter() {
self.label(&label)?;
}
self.string_with_len("")?;
Ok(())
}
}

impl DomainName {
Expand Down Expand Up @@ -98,3 +109,37 @@ impl<'a> Iterator for DomainNameIter<'a> {
}
}
impl_encode!(DomainName, domain_name);
impl_encode!(
DomainName,
domain_name_without_compression,
encode_without_compression
);

#[test]
fn domain_name_without_compression_1() {
let domain_name: DomainName = "example.org.".parse().unwrap();
let mut encoder = Encoder::default();
encoder
.domain_name_without_compression(&domain_name)
.unwrap();
encoder
.domain_name_without_compression(&domain_name)
.unwrap();
assert_eq!(
encoder.bytes,
&b"\x07example\x03org\0\x07example\x03org\0"[..]
);
}

#[test]
fn domain_name_without_compression_2() {
let domain_name = DomainName::default();
let mut encoder = Encoder::default();
encoder
.domain_name_without_compression(&domain_name)
.unwrap();
encoder
.domain_name_without_compression(&domain_name)
.unwrap();
assert_eq!(encoder.bytes, &b"\0\0"[..]);
}
5 changes: 4 additions & 1 deletion src/encode/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ macro_rules! impl_encode_without_result {

macro_rules! impl_encode {
($i:path, $m:ident) => {
impl_encode!($i, $m, encode);
};
($i:path, $m:ident, $n:ident) => {
impl $i {
pub fn encode(&self) -> crate::EncodeResult<bytes::BytesMut> {
pub fn $n(&self) -> crate::EncodeResult<bytes::BytesMut> {
let mut encoder = crate::encode::Encoder::default();
encoder.$m(self)?;
Ok(encoder.bytes)
Expand Down