Skip to content

Commit

Permalink
Change ToDname/ToRelativeDname to have try_to_*name and to_*name pairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
partim committed Mar 21, 2024
1 parent e177063 commit b5d259d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
77 changes: 67 additions & 10 deletions src/base/name/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ pub trait ToDname: ToLabelIter {
/// labels of the name and adds them one by one to [`Dname`]. This will
/// work for any name but an optimized implementation can be provided for
/// some types of names.
///
/// [`Dname`]: struct.Dname.html
fn to_dname<Octets>(
fn try_to_dname<Octets>(
&self,
) -> Result<Dname<Octets>, BuilderAppendError<Octets>>
where
Expand All @@ -131,8 +129,22 @@ pub trait ToDname: ToLabelIter {
Ok(unsafe { Dname::from_octets_unchecked(builder.freeze()) })
}

/// Converts the name into a single, uncompressed name.
///
/// This is the same as [`try_to_dname`][ToDname::try_to_dname] but for
/// builder types with an unrestricted buffer.
fn to_dname<Octets>(&self) -> Dname<Octets>
where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder:
OctetsBuilder<AppendError = Infallible>,
<Octets as FromBuilder>::Builder: EmptyBuilder,
{
infallible(self.try_to_dname())
}

/// Converts the name into a single name in canonical form.
fn to_canonical_dname<Octets>(
fn try_to_canonical_dname<Octets>(
&self,
) -> Result<Dname<Octets>, BuilderAppendError<Octets>>
where
Expand All @@ -146,6 +158,21 @@ pub trait ToDname: ToLabelIter {
Ok(unsafe { Dname::from_octets_unchecked(builder.freeze()) })
}

/// Converts the name into a single name in canonical form.
///
/// This is the same as
/// [`try_to_canonical_dname`][ToDname::try_to_canonical_dname] but for
/// builder types with an unrestricted buffer.
fn to_canonical_dname<Octets>(&self) -> Dname<Octets>
where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder:
OctetsBuilder<AppendError = Infallible>,
<Octets as FromBuilder>::Builder: EmptyBuilder,
{
infallible(self.try_to_canonical_dname())
}

/// Returns an octets slice of the content if possible.
///
/// If a value stores the domain name as one single octets sequence, it
Expand Down Expand Up @@ -202,13 +229,13 @@ pub trait ToDname: ToLabelIter {
/// Returns the domain name assembled into a `Vec<u8>`.
#[cfg(feature = "std")]
fn to_vec(&self) -> Dname<std::vec::Vec<u8>> {
infallible(self.to_dname())
self.to_dname()
}

/// Returns the domain name assembled into a bytes value.
#[cfg(feature = "bytes")]
fn to_bytes(&self) -> Dname<Bytes> {
infallible(self.to_dname())
self.to_dname()
}

/// Tests whether `self` and `other` are equal.
Expand Down Expand Up @@ -353,7 +380,7 @@ pub trait ToRelativeDname: ToLabelIter {
/// some types of names.
///
/// [`RelativeDname`]: struct.RelativeDname.html
fn to_relative_dname<Octets>(
fn try_to_relative_dname<Octets>(
&self,
) -> Result<RelativeDname<Octets>, BuilderAppendError<Octets>>
where
Expand All @@ -367,8 +394,23 @@ pub trait ToRelativeDname: ToLabelIter {
Ok(unsafe { RelativeDname::from_octets_unchecked(builder.freeze()) })
}

/// Converts the name into a single, continous name.
///
/// This is the same as
/// [`try_to_relative_dname`][ToRelativeDname::try_to_relative_dname]
/// but for builder types with an unrestricted buffer.
fn to_relative_dname<Octets>(&self) -> RelativeDname<Octets>
where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder:
OctetsBuilder<AppendError = Infallible>,
<Octets as FromBuilder>::Builder: EmptyBuilder,
{
infallible(self.try_to_relative_dname())
}

/// Converts the name into a single name in canonical form.
fn to_canonical_relative_dname<Octets>(
fn try_to_canonical_relative_dname<Octets>(
&self,
) -> Result<RelativeDname<Octets>, BuilderAppendError<Octets>>
where
Expand All @@ -382,6 +424,21 @@ pub trait ToRelativeDname: ToLabelIter {
Ok(unsafe { RelativeDname::from_octets_unchecked(builder.freeze()) })
}

/// Converts the name into a single name in canonical form.
///
/// This is the same as
/// [`try_to_canonical_relative_dname`][ToRelativeDname::try_to_canonical_relative_dname]
/// but for builder types with an unrestricted buffer.
fn to_canonical_relative_dname<Octets>(&self) -> RelativeDname<Octets>
where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder:
OctetsBuilder<AppendError = Infallible>,
<Octets as FromBuilder>::Builder: EmptyBuilder,
{
infallible(self.try_to_canonical_relative_dname())
}

/// Returns a byte slice of the content if possible.
///
/// This method can is used to optimize comparision operations between
Expand Down Expand Up @@ -434,13 +491,13 @@ pub trait ToRelativeDname: ToLabelIter {
/// Returns the domain name assembled into a `Vec<u8>`.
#[cfg(feature = "std")]
fn to_vec(&self) -> RelativeDname<std::vec::Vec<u8>> {
infallible(self.to_relative_dname())
self.to_relative_dname()
}

/// Returns the domain name assembled into a bytes value.
#[cfg(feature = "bytes")]
fn to_bytes(&self) -> RelativeDname<Bytes> {
infallible(self.to_relative_dname())
self.to_relative_dname()
}

/// Returns whether the name is empty.
Expand Down
11 changes: 2 additions & 9 deletions src/net/client/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::base::{
Dname, Header, Message, MessageBuilder, ParsedDname, StaticCompressor,
Ttl,
};
use crate::dep::octseq::{octets::OctetsInto, Octets};
use crate::dep::octseq::Octets;
use crate::net::client::clock::{Clock, Elapsed, SystemClock};
use crate::net::client::request::{
ComposeRequest, Error, GetResponse, SendRequest,
Expand Down Expand Up @@ -803,15 +803,8 @@ impl Key {
where
TDN: ToDname,
{
let mut qname: Dname<Vec<u8>> =
qname.to_dname().expect("to_dname should not fail");

// Make sure qname is canonical.
qname.make_canonical();
let qname: Dname<Bytes> = qname.octets_into();

Self {
qname,
qname: qname.to_canonical_dname(),
qclass,
qtype,
addo: AdDo::new(ad, dnssec_ok),
Expand Down
4 changes: 2 additions & 2 deletions src/resolv/lookup/srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl SrvItem {
srv.priority(),
srv.weight(),
srv.port(),
srv.target().to_dname().unwrap(),
srv.target().try_to_dname().unwrap(),
),
fallback: false,
resolved: None,
Expand All @@ -306,7 +306,7 @@ impl SrvItem {

fn fallback(name: impl ToDname, fallback_port: u16) -> Self {
SrvItem {
srv: Srv::new(0, 0, fallback_port, name.to_dname().unwrap()),
srv: Srv::new(0, 0, fallback_port, name.try_to_dname().unwrap()),
fallback: true,
resolved: None,
}
Expand Down
2 changes: 1 addition & 1 deletion src/tsig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ where
algorithm: Algorithm,
) -> Option<Self::Key> {
// XXX This seems a bit wasteful.
let name = name.to_dname().unwrap();
let name = name.try_to_dname().unwrap();
self.get(&(name, algorithm)).cloned()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/zonefile/inplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,13 @@ impl<'a> EntryScanner<'a> {
fn scan_control(&mut self) -> Result<ScannedEntry, EntryError> {
let ctrl = self.scan_string()?;
if ctrl.eq_ignore_ascii_case("$ORIGIN") {
let origin = self.scan_dname()?.to_dname().unwrap();
let origin = self.scan_dname()?.to_dname();
self.zonefile.buf.require_line_feed()?;
Ok(ScannedEntry::Origin(origin))
} else if ctrl.eq_ignore_ascii_case("$INCLUDE") {
let path = self.scan_string()?;
let origin = if !self.zonefile.buf.is_line_feed() {
Some(self.scan_dname()?.to_dname().unwrap())
Some(self.scan_dname()?.to_dname())
} else {
None
};
Expand Down
4 changes: 2 additions & 2 deletions tests/net/stelline/parse_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,13 @@ impl<'a> EntryScanner<'a> {
fn scan_control(&mut self) -> Result<ScannedEntry, EntryError> {
let ctrl = self.scan_string()?;
if ctrl.eq_ignore_ascii_case("$ORIGIN") {
let origin = self.scan_dname()?.to_dname().unwrap();
let origin = self.scan_dname()?.to_dname();
self.zonefile.buf.require_line_feed()?;
Ok(ScannedEntry::Origin(origin))
} else if ctrl.eq_ignore_ascii_case("$INCLUDE") {
let path = self.scan_string()?;
let origin = if !self.zonefile.buf.is_line_feed() {
Some(self.scan_dname()?.to_dname().unwrap())
Some(self.scan_dname()?.to_dname())
} else {
None
};
Expand Down

0 comments on commit b5d259d

Please sign in to comment.