aaaa.rs 2.9 KB

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