ptr.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use crate::strings::ReadLabels;
  2. use crate::wire::*;
  3. /// A **PTR** record, which holds a _pointer_ to a canonical name. This is
  4. /// most often used for reverse DNS lookups.
  5. ///
  6. /// # Encoding
  7. ///
  8. /// The text encoding is not specified, but this crate treats it as UTF-8.
  9. /// Invalid bytes are turned into the replacement character.
  10. ///
  11. /// # References
  12. ///
  13. /// - [RFC 1035 §3.3.14](https://tools.ietf.org/html/rfc1035) — Domain Names, Implementation and Specification (November 1987)
  14. #[derive(PartialEq, Debug, Clone)]
  15. pub struct PTR {
  16. /// The CNAME contained in the record.
  17. pub cname: String,
  18. }
  19. impl Wire for PTR {
  20. const NAME: &'static str = "PTR";
  21. const RR_TYPE: u16 = 12;
  22. fn read(_len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
  23. let cname = c.read_labels()?;
  24. Ok(PTR { cname })
  25. }
  26. }
  27. #[cfg(test)]
  28. mod test {
  29. use super::*;
  30. #[test]
  31. fn parses() {
  32. let buf = &[ 0x03, 0x64, 0x6e, 0x73, 0x06, 0x67,
  33. 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x00 ];
  34. assert_eq!(PTR::read(12, &mut Cursor::new(buf)).unwrap(),
  35. PTR {
  36. cname: String::from("dns.google."),
  37. });
  38. }
  39. #[test]
  40. fn empty() {
  41. assert_eq!(PTR::read(0, &mut Cursor::new(&[])),
  42. Err(WireError::IO));
  43. }
  44. }