ns.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use crate::strings::ReadLabels;
  2. use crate::wire::*;
  3. use log::{warn, debug};
  4. /// A **NS** _(name server)_ record, which is used to point domains to name
  5. /// servers.
  6. ///
  7. /// # References
  8. ///
  9. /// - [RFC 1035 §3.3.11](https://tools.ietf.org/html/rfc1035) — Domain Names, Implementation and Specification (November 1987)
  10. #[derive(PartialEq, Debug, Clone)]
  11. pub struct NS {
  12. /// The address of a nameserver that provides this DNS response.
  13. pub nameserver: String,
  14. }
  15. impl Wire for NS {
  16. const NAME: &'static str = "NS";
  17. const RR_TYPE: u16 = 2;
  18. #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
  19. fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
  20. let nameserver = c.read_labels()?;
  21. if nameserver.len() + 1 == len as usize {
  22. debug!("Length {} is correct", nameserver.len() + 1);
  23. }
  24. else {
  25. warn!("Expected length {} but read {} bytes", len, nameserver.len() + 1);
  26. }
  27. Ok(NS { nameserver })
  28. }
  29. }
  30. #[cfg(test)]
  31. mod test {
  32. use super::*;
  33. #[test]
  34. fn parses() {
  35. let buf = &[
  36. 0x01, 0x61, 0x0c, 0x67, 0x74, 0x6c, 0x64, 0x2d, 0x73, 0x65, 0x72,
  37. 0x76, 0x65, 0x72, 0x73, 0x03, 0x6e, 0x65, 0x74, // nameserver
  38. 0x00, // nameserver terminator
  39. ];
  40. assert_eq!(NS::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
  41. NS {
  42. nameserver: String::from("a.gtld-servers.net."),
  43. });
  44. }
  45. #[test]
  46. fn empty() {
  47. assert_eq!(NS::read(0, &mut Cursor::new(&[])),
  48. Err(WireError::IO));
  49. }
  50. }