simple_dns/dns/rdata/
nsec.rs

1use crate::{
2    bytes_buffer::BytesBuffer,
3    dns::WireFormat,
4    lib::{Cow, Vec},
5    lib::Write,
6    Name,
7};
8
9use super::RR;
10
11/// A NSEC record see [rfc4034](https://datatracker.ietf.org/doc/html/rfc4034#section-4)
12#[derive(Debug, PartialEq, Eq, Hash, Clone)]
13pub struct NSEC<'a> {
14    /// The next owner name in the canonical ordering of the zone
15    pub next_name: Name<'a>,
16    /// The type bit maps representing the RR types present at the NSEC RR's owner name
17    pub type_bit_maps: Vec<NsecTypeBitMap<'a>>,
18}
19
20/// A Type bit map entry in a NSEC record see [rfc4034](https://datatracker.ietf.org/doc/html/rfc4034#section-4.1.2)
21#[derive(Debug, PartialEq, Eq, Hash, Clone)]
22pub struct NsecTypeBitMap<'a> {
23    /// The window block number of this bit map
24    pub window_block: u8,
25    /// The bitmap containing the RR types present in this window block
26    pub bitmap: Cow<'a, [u8]>,
27}
28
29impl RR for NSEC<'_> {
30    const TYPE_CODE: u16 = 47;
31}
32
33impl<'a> WireFormat<'a> for NSEC<'a> {
34    const MINIMUM_LEN: usize = 0;
35    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
36    where
37        Self: Sized,
38    {
39        let next_name = Name::parse(data)?;
40        let mut type_bit_maps = Vec::new();
41        let mut prev_window_block = None;
42
43        while data.has_remaining() {
44            let window_block = data.get_u8()?;
45            if let Some(prev_window_block) = prev_window_block {
46                if window_block <= prev_window_block {
47                    return Err(crate::SimpleDnsError::InvalidDnsPacket);
48                }
49            }
50
51            prev_window_block = Some(window_block);
52
53            let bitmap_length = data.get_u8()? as usize;
54            if bitmap_length > 32 {
55                return Err(crate::SimpleDnsError::InvalidDnsPacket);
56            }
57
58            let bitmap = data.get_slice(bitmap_length)?;
59
60            type_bit_maps.push(NsecTypeBitMap {
61                window_block,
62                bitmap: Cow::Borrowed(bitmap),
63            });
64        }
65
66        Ok(Self {
67            next_name,
68            type_bit_maps,
69        })
70    }
71
72    fn write_to<T: Write>(&self, out: &mut T) -> crate::Result<()> {
73        self.next_name.write_to(out)?;
74
75        let mut sorted = self.type_bit_maps.clone();
76        sorted.sort_by(|a, b| a.window_block.cmp(&b.window_block));
77
78        for record in sorted.iter() {
79            out.write_all(&[record.window_block])?;
80            out.write_all(&[record.bitmap.len() as u8])?;
81            out.write_all(&record.bitmap)?;
82        }
83
84        Ok(())
85    }
86
87    fn len(&self) -> usize {
88        self.next_name.len()
89    }
90}
91
92impl NSEC<'_> {
93    /// Transforms the inner data into its owned type
94    pub fn into_owned<'b>(self) -> NSEC<'b> {
95        let type_bit_maps = self
96            .type_bit_maps
97            .into_iter()
98            .map(|x| NsecTypeBitMap {
99                window_block: x.window_block,
100                bitmap: x.bitmap.into_owned().into(),
101            })
102            .collect();
103        NSEC {
104            next_name: self.next_name.into_owned(),
105            type_bit_maps,
106        }
107    }
108}
109
110#[cfg(test)]
111mod tests {
112
113    use super::*;
114    use crate::lib::vec;
115
116    #[test]
117    fn parse_and_write_nsec() {
118        let nsec = NSEC {
119            next_name: Name::new("host.example.com.").unwrap(),
120            type_bit_maps: vec![NsecTypeBitMap {
121                window_block: 0,
122                bitmap: vec![64, 1, 0, 0, 0, 1].into(),
123            }],
124        };
125        let mut data = Vec::new();
126        nsec.write_to(&mut data).unwrap();
127
128        let nsec = NSEC::parse(&mut data[..].into()).unwrap();
129        assert_eq!(nsec.next_name, Name::new("host.example.com.").unwrap());
130        assert_eq!(nsec.type_bit_maps.len(), 1);
131        assert_eq!(nsec.type_bit_maps[0].window_block, 0);
132        assert_eq!(nsec.type_bit_maps[0].bitmap, vec![64, 1, 0, 0, 0, 1]);
133    }
134
135    #[test]
136    #[cfg(feature = "std")]
137    fn parse_sample() -> Result<(), Box<dyn std::error::Error>> {
138        use crate::{rdata::RData, ResourceRecord};
139        let sample_file = std::fs::read("samples/zonefile/NSEC.sample")?;
140
141        let sample_rdata = match ResourceRecord::parse(&mut sample_file[..].into())?.rdata {
142            RData::NSEC(rdata) => rdata,
143            _ => unreachable!(),
144        };
145
146        assert_eq!(
147            sample_rdata.next_name,
148            Name::new("host.example.com.").unwrap()
149        );
150        assert_eq!(sample_rdata.type_bit_maps.len(), 1);
151        assert_eq!(sample_rdata.type_bit_maps[0].window_block, 0);
152        assert_eq!(
153            sample_rdata.type_bit_maps[0].bitmap,
154            vec![64, 1, 0, 0, 0, 1]
155        );
156
157        Ok(())
158    }
159}