srv.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. use crate::strings::ReadLabels;
  2. use crate::wire::*;
  3. use log::{debug, warn};
  4. /// A **SRV** record, which contains an IP address as well as a port number,
  5. /// for specifying the location of services more precisely.
  6. ///
  7. /// # References
  8. ///
  9. /// - [RFC 2782](https://tools.ietf.org/html/rfc2782) — A DNS RR for specifying the location of services (February 2000)
  10. #[derive(PartialEq, Debug, Clone)]
  11. pub struct SRV {
  12. /// The priority of this host among all that get returned. Lower values
  13. /// are higher priority.
  14. pub priority: u16,
  15. /// A weight to choose among results with the same priority. Higher values
  16. /// are higher priority.
  17. pub weight: u16,
  18. /// The port the service is serving on.
  19. pub port: u16,
  20. /// The hostname of the machine the service is running on.
  21. pub target: String,
  22. }
  23. impl Wire for SRV {
  24. const NAME: &'static str = "SRV";
  25. const RR_TYPE: u16 = 33;
  26. #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
  27. fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
  28. let priority = c.read_u16::<BigEndian>()?;
  29. let weight = c.read_u16::<BigEndian>()?;
  30. let port = c.read_u16::<BigEndian>()?;
  31. let target = c.read_labels()?;
  32. let got_length = 3 * 2 + target.len() + 1;
  33. if got_length == len as usize {
  34. debug!("Length {} is correct", len);
  35. }
  36. else {
  37. warn!("Expected length {} but got {}", len, got_length);
  38. }
  39. Ok(SRV { priority, weight, port, target })
  40. }
  41. }
  42. #[cfg(test)]
  43. mod test {
  44. use super::*;
  45. #[test]
  46. fn parses() {
  47. let buf = &[ 0x00, 0x01, 0x00, 0x01, 0x92, 0x7c, 0x03, 0x61, 0x74,
  48. 0x61, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x04, 0x6e,
  49. 0x6f, 0x64, 0x65, 0x03, 0x64, 0x63, 0x31, 0x06, 0x63,
  50. 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x00, ];
  51. assert_eq!(SRV::read(33, &mut Cursor::new(buf)).unwrap(),
  52. SRV {
  53. priority: 1,
  54. weight: 1,
  55. port: 37500,
  56. target: String::from("ata.local.node.dc1.consul."),
  57. });
  58. }
  59. #[test]
  60. fn empty() {
  61. assert_eq!(SRV::read(0, &mut Cursor::new(&[])),
  62. Err(WireError::IO));
  63. }
  64. }