simple_dns/dns/rdata/
null.rs

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