Skip to main content

rlibdns/rr_data/
nsec3param_rr_data.rs

1use std::any::Any;
2use std::collections::HashMap;
3use std::fmt;
4use std::fmt::Formatter;
5use crate::messages::inter::rr_types::RRTypes;
6use crate::rr_data::inter::rr_data::{RRData, RRDataError};
7use crate::utils::hex;
8use crate::zone::inter::zone_rr_data::ZoneRRData;
9use crate::zone::zone_reader::{ErrorKind, ZoneReaderError};
10
11#[derive(Clone, Debug)]
12pub struct NSec3ParamRRData {
13    algorithm: u8,
14    flags: u8,
15    iterations: u16,
16    salt: Vec<u8>
17}
18
19impl Default for NSec3ParamRRData {
20
21    fn default() -> Self {
22        Self {
23            algorithm: 0,
24            flags: 0,
25            iterations: 0,
26            salt: Vec::new()
27        }
28    }
29}
30
31impl RRData for NSec3ParamRRData {
32
33    fn from_bytes(buf: &[u8], off: usize) -> Result<Self, RRDataError> {
34        let length = u16::from_be_bytes([buf[off], buf[off+1]]) as usize;
35        if length == 0 {
36            return Ok(Default::default());
37        }
38
39        let algorithm = buf[off+2];
40        let flags = buf[off+3];
41        let iterations = u16::from_be_bytes([buf[off+4], buf[off+5]]);
42
43        let salt_length = buf[off+6] as usize;
44        let salt = buf[off + 7..off + 7 + salt_length].to_vec();
45
46        Ok(Self {
47            algorithm,
48            flags,
49            iterations,
50            salt
51        })
52    }
53
54    fn to_bytes_compressed(&self, _compression_data: &mut HashMap<String, usize>, _off: usize) -> Result<Vec<u8>, RRDataError> {
55        self.to_bytes()
56    }
57
58    fn to_bytes(&self) -> Result<Vec<u8>, RRDataError> {
59        let mut buf = vec![0u8; 7];
60
61        buf[2] = self.algorithm;
62        buf[3] = self.flags;
63        buf.splice(4..6, self.iterations.to_be_bytes());
64
65        buf[6] = self.salt.len() as u8;
66        buf.extend_from_slice(&self.salt);
67
68        buf.splice(0..2, ((buf.len()-2) as u16).to_be_bytes());
69
70        Ok(buf)
71    }
72
73    fn get_type(&self) -> RRTypes {
74        RRTypes::NSec3Param
75    }
76
77    fn upcast(self) -> Box<dyn RRData> {
78        Box::new(self)
79    }
80
81    fn as_any(&self) -> &dyn Any {
82        self
83    }
84
85    fn as_any_mut(&mut self) -> &mut dyn Any {
86        self
87    }
88
89    fn clone_box(&self) -> Box<dyn RRData> {
90        Box::new(self.clone())
91    }
92}
93
94impl NSec3ParamRRData {
95
96    pub fn new(algorithm: u8, flags: u8, iterations: u16, salt: Vec<u8>) -> Self {
97        Self {
98            algorithm,
99            flags,
100            iterations,
101            salt
102        }
103    }
104
105    pub fn set_algorithm(&mut self, algorithm: u8) {
106        self.algorithm = algorithm;
107    }
108
109    pub fn get_algorithm(&self) -> u8 {
110        self.algorithm
111    }
112
113    pub fn set_flags(&mut self, flags: u8) {
114        self.flags = flags;
115    }
116
117    pub fn get_flags(&self) -> u8 {
118        self.flags
119    }
120
121    pub fn set_iterations(&mut self, iterations: u16) {
122        self.iterations = iterations;
123    }
124
125    pub fn get_iterations(&self) -> u16 {
126        self.iterations
127    }
128
129    pub fn set_salt(&mut self, salt: &[u8]) {
130        self.salt = salt.to_vec();
131    }
132
133    pub fn get_salt(&self) -> &[u8] {
134        &self.salt
135    }
136}
137
138impl ZoneRRData for NSec3ParamRRData {
139
140    fn set_data(&mut self, index: usize, value: &str) -> Result<(), ZoneReaderError> {
141        Ok(match index {
142            0 => self.algorithm = value.parse().map_err(|_| ZoneReaderError::new(ErrorKind::FormErr, &format!("unable to parse algorithm param for record type {}", self.get_type())))?,
143            1 => self.flags = value.parse().map_err(|_| ZoneReaderError::new(ErrorKind::FormErr, &format!("unable to parse flags param for record type {}", self.get_type())))?,
144            2 => self.iterations = value.parse().map_err(|_| ZoneReaderError::new(ErrorKind::FormErr, &format!("unable to parse iterations param for record type {}", self.get_type())))?,
145            3 => self.salt = hex::decode(value).map_err(|_| ZoneReaderError::new(ErrorKind::FormErr, &format!("unable to parse salt param for record type {}", self.get_type())))?,
146            _ => return Err(ZoneReaderError::new(ErrorKind::ExtraRRData, &format!("extra record data found for record type {}", self.get_type())))
147        })
148    }
149
150    fn upcast(self) -> Box<dyn ZoneRRData> {
151        Box::new(self)
152    }
153}
154
155impl fmt::Display for NSec3ParamRRData {
156
157    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
158        write!(f, "{:<8}{} {} {} {}", self.get_type().to_string(),
159               self.algorithm,
160               self.flags,
161               self.iterations,
162               hex::encode(&self.salt))
163    }
164}
165
166#[test]
167fn test() {
168    let buf = vec![ 0x0, 0x5, 0x1, 0x0, 0x0, 0x0, 0x0 ];
169    let record = NSec3ParamRRData::from_bytes(&buf, 0).unwrap();
170    assert_eq!(buf, record.to_bytes().unwrap());
171}