Struct trust_dns_resolver::Name

source ·
pub struct Name { /* private fields */ }
Expand description

A domain name

Implementations§

source§

impl Name

source

pub fn new() -> Name

Create a new domain::Name, i.e. label

source

pub fn root() -> Name

Returns the root label, i.e. no labels, can probably make this better in the future.

source

pub fn is_root(&self) -> bool

Returns true if there are no labels, i.e. it’s empty.

In DNS the root is represented by .

§Examples
use trust_dns_proto::rr::domain::Name;

let root = Name::root();
assert_eq!(&root.to_string(), ".");
source

pub fn is_fqdn(&self) -> bool

Returns true if the name is a fully qualified domain name.

If this is true, it has effects like only querying for this single name, as opposed to building up a search list in resolvers.

warning: this interface is unstable and may change in the future

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let name = Name::from_str("www").unwrap();
assert!(!name.is_fqdn());

let name = Name::from_str("www.example.com").unwrap();
assert!(!name.is_fqdn());

let name = Name::from_str("www.example.com.").unwrap();
assert!(name.is_fqdn());
source

pub fn set_fqdn(&mut self, val: bool)

Specifies this name is a fully qualified domain name

warning: this interface is unstable and may change in the future

source

pub fn iter(&self) -> LabelIter<'_>

Returns an iterator over the labels

source

pub fn append_label<L>(self, label: L) -> Result<Name, ProtoError>
where L: IntoLabel,

Appends the label to the end of this name

§Example
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let name = Name::from_str("www.example").unwrap();
let name = name.append_label("com").unwrap();
assert_eq!(name, Name::from_str("www.example.com").unwrap());
source

pub fn from_labels<I, L>(labels: I) -> Result<Name, ProtoError>
where I: IntoIterator<Item = L>, L: IntoLabel,

Creates a new Name from the specified labels

§Arguments
  • labels - vector of items which will be stored as Strings.
§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

// From strings, uses utf8 conversion
let from_labels = Name::from_labels(vec!["www", "example", "com"]).unwrap();
assert_eq!(from_labels, Name::from_str("www.example.com").unwrap());

// Force a set of bytes into labels (this is none-standard and potentially dangerous)
let from_labels = Name::from_labels(vec!["bad chars".as_bytes(), "example".as_bytes(), "com".as_bytes()]).unwrap();
assert_eq!(from_labels.iter().next(), Some(&b"bad chars"[..]));

let root = Name::from_labels(Vec::<&str>::new()).unwrap();
assert!(root.is_root());
source

pub fn append_name(self, other: &Name) -> Result<Name, ProtoError>

Appends other to self, returning a new Name

Carries forward is_fqdn from other.

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let local = Name::from_str("www").unwrap();
let domain = Name::from_str("example.com").unwrap();
assert!(!domain.is_fqdn());

let name = local.clone().append_name(&domain).unwrap();
assert_eq!(name, Name::from_str("www.example.com").unwrap());
assert!(!name.is_fqdn());

// see also `Name::append_domain`
let domain = Name::from_str("example.com.").unwrap();
assert!(domain.is_fqdn());
let name = local.append_name(&domain).unwrap();
assert_eq!(name, Name::from_str("www.example.com.").unwrap());
assert!(name.is_fqdn());
source

pub fn append_domain(self, domain: &Name) -> Result<Name, ProtoError>

Appends the domain to self, making the new Name an FQDN

This is an alias for append_name with the added effect of marking the new Name as a fully-qualified-domain-name.

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let local = Name::from_str("www").unwrap();
let domain = Name::from_str("example.com").unwrap();
let name = local.append_domain(&domain).unwrap();
assert_eq!(name, Name::from_str("www.example.com").unwrap());
assert!(name.is_fqdn())
source

pub fn to_lowercase(&self) -> Name

Creates a new Name with all labels lowercased

§Examples
use std::cmp::Ordering;
use std::str::FromStr;

use trust_dns_proto::rr::domain::{Label, Name};

let example_com = Name::from_ascii("Example.Com").unwrap();
assert_eq!(example_com.cmp_case(&Name::from_str("example.com").unwrap()), Ordering::Less);
assert!(example_com.to_lowercase().eq_case(&Name::from_str("example.com").unwrap()));
source

pub fn base_name(&self) -> Name

Trims off the first part of the name, to help with searching for the domain piece

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let example_com = Name::from_str("example.com.").unwrap();
assert_eq!(example_com.base_name(), Name::from_str("com.").unwrap());
assert_eq!(Name::from_str("com.").unwrap().base_name(), Name::root());
assert_eq!(Name::root().base_name(), Name::root());
source

pub fn trim_to(&self, num_labels: usize) -> Name

Trims to the number of labels specified

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let example_com = Name::from_str("example.com.").unwrap();
assert_eq!(example_com.trim_to(2), Name::from_str("example.com.").unwrap());
assert_eq!(example_com.trim_to(1), Name::from_str("com.").unwrap());
assert_eq!(example_com.trim_to(0), Name::root());
assert_eq!(example_com.trim_to(3), Name::from_str("example.com.").unwrap());
source

pub fn zone_of_case(&self, name: &Name) -> bool

same as zone_of allows for case sensitive call

source

pub fn zone_of(&self, name: &Name) -> bool

returns true if the name components of self are all present at the end of name

§Example
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let name = Name::from_str("www.example.com").unwrap();
let name = Name::from_str("www.example.com").unwrap();
let zone = Name::from_str("example.com").unwrap();
let another = Name::from_str("example.net").unwrap();
assert!(zone.zone_of(&name));
assert!(!name.zone_of(&zone));
assert!(!another.zone_of(&name));
source

pub fn num_labels(&self) -> u8

Returns the number of labels in the name, discounting *.

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let root = Name::root();
assert_eq!(root.num_labels(), 0);

let example_com = Name::from_str("example.com").unwrap();
assert_eq!(example_com.num_labels(), 2);

let star_example_com = Name::from_str("*.example.com.").unwrap();
assert_eq!(star_example_com.num_labels(), 2);
source

pub fn len(&self) -> usize

returns the length in bytes of the labels. ‘.’ counts as 1

This can be used as an estimate, when serializing labels, they will often be compressed and/or escaped causing the exact length to be different.

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

assert_eq!(Name::from_str("www.example.com.").unwrap().len(), 16);
assert_eq!(Name::from_str(".").unwrap().len(), 1);
assert_eq!(Name::root().len(), 1);
source

pub fn is_empty(&self) -> bool

Returns whether the length of the labels, in bytes is 0. In practice, since ‘.’ counts as 1, this is never the case so the method returns false.

source

pub fn parse(local: &str, origin: Option<&Name>) -> Result<Name, ProtoError>

attempts to parse a name such as "example.com." or "subdomain.example.com."

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;

let name = Name::from_str("example.com.").unwrap();
assert_eq!(name.base_name(), Name::from_str("com.").unwrap());
assert_eq!(name.iter().next(), Some(&b"example"[..]));
source

pub fn from_ascii<S>(name: S) -> Result<Name, ProtoError>
where S: AsRef<str>,

Will convert the string to a name only allowing ascii as valid input

This method will also preserve the case of the name where that’s desirable

§Examples
use trust_dns_proto::rr::Name;

let bytes_name = Name::from_labels(vec!["WWW".as_bytes(), "example".as_bytes(), "COM".as_bytes()]).unwrap();
let ascii_name = Name::from_ascii("WWW.example.COM.").unwrap();
let lower_name = Name::from_ascii("www.example.com.").unwrap();

assert!(bytes_name.eq_case(&ascii_name));
assert!(!lower_name.eq_case(&ascii_name));

// escaped values
let bytes_name = Name::from_labels(vec!["email.name".as_bytes(), "example".as_bytes(), "com".as_bytes()]).unwrap();
let name = Name::from_ascii("email\\.name.example.com.").unwrap();

assert_eq!(bytes_name, name);

let bytes_name = Name::from_labels(vec!["bad.char".as_bytes(), "example".as_bytes(), "com".as_bytes()]).unwrap();
let name = Name::from_ascii("bad\\056char.example.com.").unwrap();

assert_eq!(bytes_name, name);
source

pub fn from_utf8<S>(name: S) -> Result<Name, ProtoError>
where S: AsRef<str>,

Will convert the string to a name using IDNA, punycode, to encode the UTF8 as necessary

When making names IDNA compatible, there is a side-effect of lowercasing the name.

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::Name;

let bytes_name = Name::from_labels(vec!["WWW".as_bytes(), "example".as_bytes(), "COM".as_bytes()]).unwrap();

// from_str calls through to from_utf8
let utf8_name = Name::from_str("WWW.example.COM.").unwrap();
let lower_name = Name::from_str("www.example.com.").unwrap();

assert!(!bytes_name.eq_case(&utf8_name));
assert!(lower_name.eq_case(&utf8_name));
source

pub fn from_str_relaxed<S>(name: S) -> Result<Name, ProtoError>
where S: AsRef<str>,

First attempts to decode via from_utf8, if that fails IDNA checks, then falls back to ascii decoding.

§Examples
use std::str::FromStr;
use trust_dns_proto::rr::Name;

// Ok, underscore in the beginning of a name
assert!(Name::from_utf8("_allows.example.com.").is_ok());

// Error, underscore in the end
assert!(Name::from_utf8("dis_allowed.example.com.").is_err());

// Ok, relaxed mode
assert!(Name::from_str_relaxed("allow_in_.example.com.").is_ok());
source

pub fn emit_as_canonical( &self, encoder: &mut BinEncoder<'_>, canonical: bool, ) -> Result<(), ProtoError>

Emits the canonical version of the name to the encoder.

In canonical form, there will be no pointers written to the encoder (i.e. no compression).

source

pub fn emit_with_lowercase( &self, encoder: &mut BinEncoder<'_>, lowercase: bool, ) -> Result<(), ProtoError>

Writes the labels, as lower case, to the encoder

§Arguments
  • encoder - encoder for writing this name
  • lowercase - if true the name will be lowercased, otherwise it will not be changed when writing
source

pub fn cmp_case(&self, other: &Name) -> Ordering

Case sensitive comparison

source

pub fn eq_case(&self, other: &Name) -> bool

Compares the Names, in a case sensitive manner

source

pub fn to_ascii(&self) -> String

Converts this name into an ascii safe string.

If the name is an IDNA name, then the name labels will be returned with the xn-- prefix. see to_utf8 or the Display impl for methods which convert labels to utf8.

source

pub fn to_utf8(&self) -> String

Converts the Name labels to the utf8 String form.

This converts the name to an unescaped format, that could be used with parse. If, the name is is followed by the final ., e.g. as in www.example.com., which represents a fully qualified Name.

source

pub fn parse_arpa_name(&self) -> Result<IpNet, ProtoError>

Converts a *.arpa Name in a PTR record back into an IpNet if possible.

source

pub fn is_localhost(&self) -> bool

Returns true if the Name is either localhost or in the localhost zone.

§Example
use std::str::FromStr;
use trust_dns_proto::rr::Name;

let name = Name::from_str("localhost").unwrap();
assert!(name.is_localhost());

let name = Name::from_str("localhost.").unwrap();
assert!(name.is_localhost());

let name = Name::from_str("my.localhost.").unwrap();
assert!(name.is_localhost());
source

pub fn is_wildcard(&self) -> bool

True if the first label of this name is the wildcard, i.e. ‘*’

§Example
use std::str::FromStr;
use trust_dns_proto::rr::Name;

let name = Name::from_str("www.example.com").unwrap();
assert!(!name.is_wildcard());

let name = Name::from_str("*.example.com").unwrap();
assert!(name.is_wildcard());

let name = Name::root();
assert!(!name.is_wildcard());
source

pub fn into_wildcard(self) -> Name

Converts a name to a wildcard, by replacing the first label with *

§Example
use std::str::FromStr;
use trust_dns_proto::rr::Name;

let name = Name::from_str("www.example.com").unwrap().into_wildcard();
assert_eq!(name, Name::from_str("*.example.com.").unwrap());

// does nothing if the root
let name = Name::root().into_wildcard();
assert_eq!(name, Name::root());

Trait Implementations§

source§

impl<'r> BinDecodable<'r> for Name

source§

fn read(decoder: &mut BinDecoder<'r>) -> Result<Name, ProtoError>

parses the chain of labels this has a max of 255 octets, with each label being less than 63. all names will be stored lowercase internally. This will consume the portions of the Vec which it is reading…

source§

fn from_bytes(bytes: &'r [u8]) -> Result<Self, ProtoError>

Returns the object in binary form
source§

impl BinEncodable for Name

source§

fn emit(&self, encoder: &mut BinEncoder<'_>) -> Result<(), ProtoError>

Write the type to the stream
source§

fn to_bytes(&self) -> Result<Vec<u8>, ProtoError>

Returns the object in binary form
source§

impl Clone for Name

source§

fn clone(&self) -> Name

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Name

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Name

source§

fn default() -> Name

Returns the “default value” for a type. Read more
source§

impl Display for Name

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a LowerName> for Name

source§

fn from(name: &'a LowerName) -> Name

Converts to this type from the input type.
source§

impl From<IpAddr> for Name

source§

fn from(addr: IpAddr) -> Name

Converts to this type from the input type.
source§

impl From<Ipv4Addr> for Name

source§

fn from(addr: Ipv4Addr) -> Name

Converts to this type from the input type.
source§

impl From<Ipv6Addr> for Name

source§

fn from(addr: Ipv6Addr) -> Name

Converts to this type from the input type.
source§

impl From<LowerName> for Name

source§

fn from(name: LowerName) -> Name

Converts to this type from the input type.
source§

impl FromStr for Name

source§

fn from_str(s: &str) -> Result<Name, <Name as FromStr>::Err>

Uses the Name::from_utf8 conversion on this string, see Name::from_ascii for ascii only, or for preserving case

§

type Err = ProtoError

The associated error which can be returned from parsing.
source§

impl Hash for Name

source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> IntoIterator for &'a Name

§

type Item = &'a [u8]

The type of the elements being iterated over.
§

type IntoIter = LabelIter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> <&'a Name as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

impl Ord for Name

source§

fn cmp(&self, other: &Name) -> Ordering

Case insensitive comparison, see Name::cmp_case for case sensitive comparisons

RFC 4034 DNSSEC Resource Records March 2005

6.1.  Canonical DNS Name Order

 For the purposes of DNS security, owner names are ordered by treating
 individual labels as unsigned left-justified octet strings.  The
 absence of a octet sorts before a zero value octet, and uppercase
 US-ASCII letters are treated as if they were lowercase US-ASCII
 letters.

 To compute the canonical ordering of a set of DNS names, start by
 sorting the names according to their most significant (rightmost)
 labels.  For names in which the most significant label is identical,
 continue sorting according to their next most significant label, and
 so forth.

 For example, the following names are sorted in canonical DNS name
 order.  The most significant label is "example".  At this level,
 "example" sorts first, followed by names ending in "a.example", then
 by names ending "z.example".  The names within each level are sorted
 in the same way.

           example
           a.example
           yljkjljk.a.example
           Z.a.example
           zABC.a.EXAMPLE
           z.example
           \001.z.example
           *.z.example
           \200.z.example
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Name

source§

fn eq(&self, other: &Name) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Name

source§

fn partial_cmp(&self, other: &Name) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryParseIp for Name

source§

fn try_parse_ip(&self) -> Option<RData>

Always returns none for Name, it assumes something that is already a name, wants to be a name

source§

impl Eq for Name

Auto Trait Implementations§

§

impl Freeze for Name

§

impl RefUnwindSafe for Name

§

impl Send for Name

§

impl Sync for Name

§

impl Unpin for Name

§

impl UnwindSafe for Name

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoName for T
where T: Into<Name>,

source§

fn into_name(self) -> Result<Name, ProtoError>

Convert this into Name
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more