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:
Name::is_valid_hostnameName::is_valid_service_nameName::is_valid_instance_nameName::is_valid_sub_type_name
§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\Xfor 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>
impl<'a> Name<'a>
Sourcepub fn new(name: &'a str) -> Result<Self>
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.
Sourcepub fn new_unchecked(name: &'a str) -> Self
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.
Sourcepub fn new_with_labels(labels: &[Label<'a>]) -> Self
pub fn new_with_labels(labels: &[Label<'a>]) -> Self
Creates a new Name with given labels
Allows construction of labels with . in them.
Sourcepub fn is_link_local(&self) -> bool
pub fn is_link_local(&self) -> bool
Verify if name ends with .local.
Sourcepub fn is_subdomain_of(&self, other: &Name<'_>) -> bool
pub fn is_subdomain_of(&self, other: &Name<'_>) -> bool
Returns true if self is a subdomain of other
Sourcepub fn into_owned<'b>(self) -> Name<'b>
pub fn into_owned<'b>(self) -> Name<'b>
Transforms the inner data into its owned type
Sourcepub fn without(&self, domain: &Name<'_>) -> Option<Name<'_>>
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")Sourcepub fn get_labels(&self) -> &[Label<'a>]
pub fn get_labels(&self) -> &[Label<'a>]
Get the labels that compose this name
Sourcepub fn is_valid_hostname(&self) -> bool
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());Sourcepub fn is_valid_sub_type_name(&self) -> bool
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());Sourcepub fn is_valid_instance_name(&self) -> bool
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());Sourcepub fn is_valid_service_name(&self) -> bool
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).
- the first label must pass
Label::is_valid_service_label - the second label must be
_tcpor_udp, ignoring case - the remaining domain labels must pass
Label::is_valid_hostname
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());