aaaa.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use std::io::Read;
  2. use std::net::Ipv6Addr;
  3. use log::*;
  4. use crate::wire::*;
  5. /// A **AAAA** record, which contains an `Ipv6Address`.
  6. ///
  7. /// # References
  8. ///
  9. /// - [RFC 3596](https://tools.ietf.org/html/rfc3596) — DNS Extensions to
  10. /// Support IP Version 6 (October 2003)
  11. #[derive(PartialEq, Debug, Copy, Clone)]
  12. pub struct AAAA {
  13. /// The IPv6 address contained in the packet.
  14. pub address: Ipv6Addr,
  15. }
  16. impl Wire for AAAA {
  17. const NAME: &'static str = "AAAA";
  18. const RR_TYPE: u16 = 28;
  19. #[cfg_attr(feature = "with_mutagen", ::mutagen::mutate)]
  20. fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
  21. if stated_length != 16 {
  22. warn!("Length is incorrect (stated length {:?}, but should be sixteen)", stated_length);
  23. let mandated_length = MandatedLength::Exactly(16);
  24. return Err(WireError::WrongRecordLength { stated_length, mandated_length });
  25. }
  26. let mut buf = [0_u8; 16];
  27. c.read_exact(&mut buf)?;
  28. let address = Ipv6Addr::from(buf);
  29. trace!("Parsed IPv6 address -> {:?}", address);
  30. Ok(Self { address })
  31. }
  32. }
  33. #[cfg(test)]
  34. mod test {
  35. use super::*;
  36. use pretty_assertions::assert_eq;
  37. #[test]
  38. fn parses() {
  39. let buf = &[
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // IPv6 address
  42. ];
  43. assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
  44. AAAA { address: Ipv6Addr::new(0,0,0,0,0,0,0,0) });
  45. }
  46. #[test]
  47. fn record_too_long() {
  48. let buf = &[
  49. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  50. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, // IPv6 address
  51. 0x09, // Unexpected extra byte
  52. ];
  53. assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)),
  54. Err(WireError::WrongRecordLength { stated_length: 17, mandated_length: MandatedLength::Exactly(16) }));
  55. }
  56. #[test]
  57. fn record_too_short() {
  58. let buf = &[
  59. 0x05, 0x05, 0x05, 0x05, 0x05, // Five arbitrary bytes
  60. ];
  61. assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)),
  62. Err(WireError::WrongRecordLength { stated_length: 5, mandated_length: MandatedLength::Exactly(16) }));
  63. }
  64. #[test]
  65. fn record_empty() {
  66. assert_eq!(AAAA::read(0, &mut Cursor::new(&[])),
  67. Err(WireError::WrongRecordLength { stated_length: 0, mandated_length: MandatedLength::Exactly(16) }));
  68. }
  69. #[test]
  70. fn buffer_ends_abruptly() {
  71. let buf = &[
  72. 0x05, 0x05, 0x05, 0x05, 0x05, // Five arbitrary bytes
  73. ];
  74. assert_eq!(AAAA::read(16, &mut Cursor::new(buf)),
  75. Err(WireError::IO));
  76. }
  77. }