simple_dns/dns/rdata/
null.rs

1use std::borrow::Cow;
2
3use crate::dns::{WireFormat, MAX_NULL_LENGTH};
4
5use super::RR;
6
7/// NULL resources are used to represent any kind of information.
8#[derive(Debug, PartialEq, Eq, Hash, Clone)]
9pub struct NULL<'a> {
10    length: u16,
11    data: Cow<'a, [u8]>,
12}
13
14impl<'a> RR for NULL<'a> {
15    const TYPE_CODE: u16 = 10;
16}
17
18impl<'a> NULL<'a> {
19    /// Creates a new NULL rdata
20    pub fn new(data: &'a [u8]) -> crate::Result<Self> {
21        if data.len() > MAX_NULL_LENGTH {
22            return Err(crate::SimpleDnsError::InvalidDnsPacket);
23        }
24
25        Ok(Self {
26            length: data.len() as u16,
27            data: Cow::Borrowed(data),
28        })
29    }
30
31    /// get a read only reference to internal data
32    pub fn get_data(&'_ self) -> &'_ [u8] {
33        &self.data
34    }
35
36    /// Transforms the inner data into its owned type
37    pub fn into_owned<'b>(self) -> NULL<'b> {
38        NULL {
39            length: self.length,
40            data: self.data.into_owned().into(),
41        }
42    }
43}
44
45impl<'a> WireFormat<'a> for NULL<'a> {
46    fn parse(data: &'a [u8], position: &mut usize) -> crate::Result<Self>
47    where
48        Self: Sized,
49    {
50        let data = &data[*position..];
51        *position += data.len();
52        Self::new(data)
53    }
54
55    fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
56        out.write_all(&self.data)
57            .map_err(crate::SimpleDnsError::from)
58    }
59
60    fn len(&self) -> usize {
61        self.length as usize
62    }
63}