Skip to main content

mdns_proto/wire/record/
aaaa.rs

1//! AAAA record (IPv6 address, RFC 3596).
2
3use core::net::Ipv6Addr;
4
5use crate::error::{BufferTooShortDetail, ParseError};
6
7/// Parsed AAAA record rdata.
8#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
9// `AAAA` is the canonical DNS record-type name; keep the verbatim spelling.
10#[allow(clippy::upper_case_acronyms)]
11pub struct AAAA {
12  addr: Ipv6Addr,
13}
14
15impl AAAA {
16  /// Parses a 16-byte IPv6 address from rdata.
17  ///
18  /// `rdata` MUST be exactly 16 bytes (RFC 3596 ยง2.2).  An
19  /// oversize slice would previously have been silently truncated.
20  pub fn try_from_rdata(rdata: &[u8]) -> Result<Self, ParseError> {
21    if rdata.len() != 16 {
22      return Err(ParseError::BufferTooShort(BufferTooShortDetail::new(
23        16,
24        0,
25        rdata.len(),
26      )));
27    }
28    let arr: &[u8; 16] = rdata
29      .first_chunk::<16>()
30      .ok_or_else(|| ParseError::BufferTooShort(BufferTooShortDetail::new(16, 0, rdata.len())))?;
31    Ok(Self {
32      addr: Ipv6Addr::from(*arr),
33    })
34  }
35
36  /// Returns the parsed IPv6 address.
37  #[inline(always)]
38  pub const fn addr(&self) -> Ipv6Addr {
39    self.addr
40  }
41}
42
43#[cfg(test)]
44#[allow(clippy::unwrap_used)]
45mod tests;