Skip to content

Commit d73b3cc

Browse files
committed
Add ToBytes and complementary functions for NsecRdata
1 parent bad5945 commit d73b3cc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/message/rdata/nsec_rdata.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ pub struct NsecRdata {
1818
pub type_bit_maps: Vec<Rtype>,
1919
}
2020

21+
impl ToBytes for NsecRdata{
22+
/// Returns a `Vec<u8>` of bytes that represents the NSEC RDATA.
23+
fn to_bytes(&self) -> Vec<u8> {
24+
let mut bytes: Vec<u8> = Vec::new();
25+
26+
let next_domain_name_bytes = self.get_next_domain_name().to_bytes();
27+
28+
for byte in next_domain_name_bytes.as_slice() {
29+
bytes.push(*byte);
30+
}
31+
32+
bytes
33+
}
34+
}
35+
2136
impl FromBytes<Result<Self, &'static str>> for NsecRdata {
2237
/// Reads the next_domain_name and type_bit_maps from the slice and returns a `NsecRdata` struct.
2338
@@ -120,4 +135,22 @@ impl NsecRdata{
120135
pub fn set_type_bit_maps(&mut self, type_bit_maps: Vec<Rtype>) {
121136
self.type_bit_maps = type_bit_maps;
122137
}
138+
}
139+
140+
impl NsecRdata{
141+
/// Complementary functions for to_bytes
142+
fn add_rtype_to_bitmap(rtype: &Rtype, bitmap: &mut Vec<u8>) {
143+
// Calculate the offset and bit for the specific Qtype
144+
let rr_type = Rtype::from_rtype_to_int(*rtype);
145+
let offset = (rr_type % 256) / 8;
146+
let bit = 7 - (rr_type % 8);
147+
148+
// Ensure the bitmap has enough space
149+
if offset >= bitmap.len() as u16 {
150+
bitmap.resize((offset + 1) as usize, 0);
151+
}
152+
153+
// Set the bit in the bitmap
154+
bitmap[offset as usize] |= 1 << bit;
155+
}
123156
}

0 commit comments

Comments
 (0)