pub struct Name<'a> { /* private fields */ }
Expand description
A domain name represented as an inverted list of labels.
Implementations§
source§impl<'a> Name<'a>
impl<'a> Name<'a>
sourcepub fn parse(buff: &'a [u8], pos: usize) -> Result<(Name<'a>, usize), ParseError>
pub fn parse(buff: &'a [u8], pos: usize) -> Result<(Name<'a>, usize), ParseError>
Parse from the specified buff
, starting at position pos
.
Errors
It will error if the buffer does not contain a valid domain name. If the domain name
has been compressed the buffer should include all previous bytes from the DNS packet
to be considered valid. Jump pointers should only point backwards inside the buf
.
sourcepub fn serialize(&self, packet: &mut Vec<u8, Global>)
pub fn serialize(&self, packet: &mut Vec<u8, Global>)
Serialize the Name and append it tho the end of the provided packet
sourcepub fn new() -> Name<'a>
pub fn new() -> Name<'a>
Create a new, empty, domain name.
let name = Name::new();
assert_eq!(name.to_string(), "".to_string())
sourcepub fn tld(&self) -> Option<&str>
pub fn tld(&self) -> Option<&str>
Obtain the top level domain (TLD) of the provided domain name.
let mut name = Name::try_from("example.com").unwrap();
assert_eq!(name.tld(), Some("com"))
sourcepub fn push_label(&mut self, label: Cow<'a, str>) -> Result<(), NameError>
pub fn push_label(&mut self, label: Cow<'a, str>) -> Result<(), NameError>
Push a new label to the end of the domain name, as a subdomain of the current one.
Error
Will error if the label is not a valid DNS label, or if the resulting Domain name is too big.
let mut name = Name::new();
name.push_label("com".into()).unwrap();
name.push_label("example".into()).unwrap();
assert_eq!(name.to_string(), "example.com.".to_string())
sourcepub fn label_count(&self) -> usize
pub fn label_count(&self) -> usize
Get the number of labels in the domain name.
let mut name = Name::try_from("example.com").unwrap();
assert_eq!(2, name.label_count())
sourcepub fn is_subdomain(&self, sub: &Name<'_>) -> bool
pub fn is_subdomain(&self, sub: &Name<'_>) -> bool
Check if sub
is a subdomain of the current domain name.
let mut name = Name::try_from("example.com").unwrap();
let mut sub = Name::try_from("subdomain.example.com").unwrap();
assert!(name.is_subdomain(&sub))
sourcepub fn iter_human(&self) -> impl DoubleEndedIterator
pub fn iter_human(&self) -> impl DoubleEndedIterator
Return an iterator over the labels in human order.
let mut name = Name::try_from("subdomain.example.com").unwrap();
let mut human = name.iter_human();
assert_eq!(human.next(), Some("subdomain"));
assert_eq!(human.next(), Some("example"));
assert_eq!(human.next(), Some("com"));
sourcepub fn iter_hierarchy(&self) -> impl DoubleEndedIterator
pub fn iter_hierarchy(&self) -> impl DoubleEndedIterator
Return an iterator over the labels in hierarchical order.
let mut name = Name::try_from("subdomain.example.com").unwrap();
let mut hierarchy = name.iter_hierarchy();
assert_eq!(hierarchy.next(), Some("com"));
assert_eq!(hierarchy.next(), Some("example"));
assert_eq!(hierarchy.next(), Some("subdomain"));