Skip to main content

Name

Struct Name 

Source
pub struct Name<'a> { /* private fields */ }
Expand description

A Name represents a domain-name, which consists of character strings separated by dots.
Each section of a name is called label
ex: google.com consists of two labels google and com

A name is at most 255 octets long in the wire format, and each label at most 63 octets. Name::new checks these limits, but not the content of the labels. Use the is_valid_* methods to check a name against the rules of a given role:

§Text format

Name::new reads the presentation format of RFC 1035 section 5.1 and RFC 4343 section 2.1:

  • an unescaped . separates labels
  • \. is a dot inside a label and \\ is a backslash
  • \DDD, three decimal digits, is the octet with that value, and \X for any other non-digit X is the character X
  • a trailing dot marks the name as absolute and does not add a label. A name made only of dots is the root and has no labels
  • empty labels, as in a..b, are skipped

Name::new_unchecked splits the text at unescaped dots in the same way, but it does not interpret the escapes: the bytes of each label are kept as written, so a\.b becomes the four byte label a\.b. To build a name from raw label content, use Name::new_with_labels.

Display writes the same format, escaping . and \ inside labels (RFC 6763 section 4.3), so a name parsed back with Name::new is equal to the original.

The escapes exist only in text. In the wire format, labels are length-prefixed and hold any octet.

let name = Name::new(r"My\.Device._googlecast._tcp.local").unwrap();
assert_eq!(4, name.get_labels().len());
assert_eq!(b"My.Device", name.get_labels()[0].as_ref());
assert_eq!(r"My\.Device._googlecast._tcp.local", name.to_string());

Microsoft implementation allows unicode characters in the name content. To create a name with unicode characters, use Name::new_unchecked or Name::new_with_labels

Implementations§

Source§

impl<'a> Name<'a>

Source

pub fn new(name: &'a str) -> Result<Self>

Creates a new Name from text in the format described in the type docs.

Returns an error if an escape sequence is malformed, if a label is longer than 63 octets after unescaping, or if the encoded name is longer than 255 octets. The content of the labels is not checked against a naming rule, use the is_valid_* methods for that.

Source

pub fn new_unchecked(name: &'a str) -> Self

Create a new Name without checking for size limits or contents.

The text is split at unescaped dots, but escape sequences are not interpreted: the bytes of each label are kept as written (see the type docs). Use Name::new to unescape, or Name::new_with_labels to set the label content directly.

Source

pub fn new_with_labels(labels: &[Label<'a>]) -> Self

Creates a new Name with given labels

Allows construction of labels with . in them.

Verify if name ends with .local.

Source

pub fn iter(&'a self) -> Iter<'a, Label<'a>> ⓘ

Returns an Iter of this Name Labels

Source

pub fn is_subdomain_of(&self, other: &Name<'_>) -> bool

Returns true if self is a subdomain of other

Source

pub fn into_owned<'b>(self) -> Name<'b>

Transforms the inner data into its owned type

Source

pub fn without(&self, domain: &Name<'_>) -> Option<Name<'_>>

Returns the subdomain part of self, based on domain. If self is not a subdomain of domain, returns None

Example:

let name = Name::new_unchecked("sub.domain.local");
let domain = Name::new_unchecked("domain.local");

assert!(domain.without(&name).is_none());

let sub = name.without(&domain).unwrap();
assert_eq!(sub.to_string(), "sub")
Source

pub fn get_labels(&self) -> &[Label<'a>]

Get the labels that compose this name

Source

pub fn is_valid_hostname(&self) -> bool

Returns true if the name has at least one label and every label is a valid host name label, see Label::is_valid_hostname.

assert!(Name::new_unchecked("my-host.example.com").is_valid_hostname());
assert!(!Name::new_unchecked("_srv.example.com").is_valid_hostname());
assert!(!Name::new_unchecked(".").is_valid_hostname());
Source

pub fn is_valid_sub_type_name(&self) -> bool

Returns true if the name is a DNS-SD subtype name: <Subtype>._sub.<sn>._tcp|_udp.<domain> (RFC 6763 section 7.1).

The subtype can be any label (see Label::is_valid_instance_label) and does not need to start with an underscore. The rest of the name must be a valid service name, see Name::is_valid_service_name.

assert!(Name::new_unchecked("_printer._sub._http._tcp.local").is_valid_sub_type_name());
assert!(Name::new_unchecked("The Music Store._sub._http._tcp.local").is_valid_sub_type_name());
assert!(!Name::new_unchecked("_printer._http._tcp.local").is_valid_sub_type_name());
Source

pub fn is_valid_instance_name(&self) -> bool

Returns true if the name is a DNS-SD service instance name: <Instance>.<sn>._tcp|_udp.<domain> (RFC 6763 section 4.3).

The instance can be any label (see Label::is_valid_instance_label), including one that contains dots. The rest of the name must be a valid service name, see Name::is_valid_service_name.

assert!(Name::new_unchecked("My Printer._ipp._tcp.local").is_valid_instance_name());
// the escaped dot keeps `My. Printer` as a single label
assert!(Name::new(r"My\. Printer._ipp._tcp.local").unwrap().is_valid_instance_name());
assert!(!Name::new_unchecked("My Printer._ipp._ftp.local").is_valid_instance_name());
Source

pub fn is_valid_service_name(&self) -> bool

Returns true if the name is a DNS-SD service name: <sn>._tcp|_udp.<domain> (RFC 6763 sections 4.1.2 and 7).

This is a syntax check. It does not check that the service is registered.

assert!(Name::new_unchecked("_http._tcp.local").is_valid_service_name());
assert!(!Name::new_unchecked("http._tcp.local").is_valid_service_name());
assert!(!Name::new_unchecked("_http._ftp.local").is_valid_service_name());
Source

pub fn as_bytes(&self) -> impl Iterator<Item = &[u8]>

Returns the bytes of each of the labels that compose this Name

Trait Implementations§

Source§

impl<'a> Clone for Name<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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<'_>) -> FmtResult

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

impl Display for Name<'_>

Source§

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

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

impl<'a> Eq for Name<'a>

Source§

impl<'a> From<&'a [Label<'a>]> for Name<'a>

Source§

fn from(labels: &'a [Label<'a>]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for NS<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for MD<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for MF<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for CNAME<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for MB<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for MG<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for MR<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for PTR<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Name<'a>> for NSAP_PTR<'a>

Source§

fn from(value: Name<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a, const N: usize> From<[Label<'a>; N]> for Name<'a>

Source§

fn from(labels: [Label<'a>; N]) -> Self

Converts to this type from the input type.
Source§

impl Hash for Name<'_>

Source§

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

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 PartialEq for Name<'_>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a> TryFrom<&'a str> for Name<'a>

Source§

type Error = SimpleDnsError

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

fn try_from(value: &'a str) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> Freeze for Name<'a>

§

impl<'a> RefUnwindSafe for Name<'a>

§

impl<'a> Send for Name<'a>

§

impl<'a> Sync for Name<'a>

§

impl<'a> Unpin for Name<'a>

§

impl<'a> UnsafeUnpin for Name<'a>

§

impl<'a> UnwindSafe for Name<'a>

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

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§

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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.