@@ -18,6 +18,54 @@ pub struct NsecRdata {
18
18
pub type_bit_maps : Vec < Rtype > ,
19
19
}
20
20
21
+ impl FromBytes < Result < Self , & ' static str > > for NsecRdata {
22
+ /// Reads the next_domain_name and type_bit_maps from the slice and returns a `NsecRdata` struct.
23
+
24
+ fn from_bytes ( bytes : & [ u8 ] , full_msg : & [ u8 ] ) -> Result < Self , & ' static str > {
25
+ let bytes_len = bytes. len ( ) ;
26
+
27
+ if bytes_len < 5 {
28
+ return Err ( "Format Error" ) ;
29
+ }
30
+
31
+ let domain_result = DomainName :: from_bytes ( bytes, full_msg) ;
32
+
33
+ match domain_result {
34
+ Ok ( _) => { }
35
+ Err ( e) => {
36
+ return Err ( e) ;
37
+ }
38
+ }
39
+
40
+ let ( next_domain_name, rest_bytes) = domain_result. unwrap ( ) ;
41
+
42
+ let mut decoded_types = Vec :: new ( ) ;
43
+ let mut offset = 0 ;
44
+
45
+ while offset < rest_bytes. len ( ) {
46
+ let window_number = rest_bytes[ offset] ;
47
+ let bitmap_length = rest_bytes[ offset + 1 ] as usize ;
48
+ let bitmap = & rest_bytes[ offset + 2 ..offset + 2 + bitmap_length] ;
49
+ for i in 0 ..bitmap. len ( ) {
50
+ let byte = bitmap[ i] ;
51
+ for j in 0 ..8 {
52
+ let rr_type = window_number as u16 * 256 + i as u16 * 8 + j as u16 ;
53
+ let bit_mask = 1 << ( 7 - j) ;
54
+ if byte & bit_mask != 0 {
55
+ decoded_types. push ( Rtype :: from_int_to_rtype ( rr_type) ) ;
56
+ }
57
+ }
58
+ }
59
+ // Move the offset to the next window block
60
+ offset += 2 + bitmap_length;
61
+ }
62
+
63
+ let nsec_rdata = NsecRdata :: new ( next_domain_name, decoded_types) ;
64
+
65
+ Ok ( nsec_rdata)
66
+ }
67
+ }
68
+
21
69
impl NsecRdata {
22
70
/// Creates a new `NsecRdata` with next_domain_name and type_bit_maps
23
71
pub fn new ( next_domain_name : DomainName , type_bit_maps : Vec < Rtype > ) -> Self {
0 commit comments