simple_dns/dns/rdata/
dnskey.rs1use crate::{bytes_buffer::BytesBuffer, dns::WireFormat, lib::Cow, lib::Write};
2
3use super::RR;
4
5#[derive(Debug, PartialEq, Eq, Hash, Clone)]
7pub struct DNSKEY<'a> {
8 pub flags: u16,
10 pub protocol: u8,
12 pub algorithm: u8,
14 pub public_key: Cow<'a, [u8]>,
16}
17
18impl RR for DNSKEY<'_> {
19 const TYPE_CODE: u16 = 48;
20}
21
22impl<'a> WireFormat<'a> for DNSKEY<'a> {
23 const MINIMUM_LEN: usize = 4;
24
25 fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
26 where
27 Self: Sized,
28 {
29 let flags = data.get_u16()?;
30 let protocol = data.get_u8()?;
31 let algorithm = data.get_u8()?;
32 let public_key = Cow::Borrowed(data.get_remaining());
33
34 Ok(Self {
35 flags,
36 protocol,
37 algorithm,
38 public_key,
39 })
40 }
41
42 fn write_to<T: Write>(&self, out: &mut T) -> crate::Result<()> {
43 out.write_all(&self.flags.to_be_bytes())?;
44 out.write_all(&[self.protocol])?;
45 out.write_all(&[self.algorithm])?;
46 out.write_all(&self.public_key)?;
47
48 Ok(())
49 }
50
51 fn len(&self) -> usize {
52 self.public_key.len() + Self::MINIMUM_LEN
53 }
54}
55
56impl DNSKEY<'_> {
57 pub fn into_owned<'b>(self) -> DNSKEY<'b> {
59 DNSKEY {
60 flags: self.flags,
61 protocol: self.protocol,
62 algorithm: self.algorithm,
63 public_key: Cow::Owned(self.public_key.into_owned()),
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use crate::lib::{vec, Vec};
72
73 #[test]
74 fn parse_and_write_dnskey() {
75 let flags = 12345u16;
76 let protocol = 8u8;
77 let algorithm = 2u8;
78 let public_key = vec![1, 2, 3, 4, 5];
79 let rdata = DNSKEY {
80 flags,
81 protocol,
82 algorithm,
83 public_key: Cow::Owned(public_key),
84 };
85 let mut writer = Vec::new();
86 rdata.write_to(&mut writer).unwrap();
87 let rdata = DNSKEY::parse(&mut (&writer[..]).into()).unwrap();
88 assert_eq!(rdata.flags, flags);
89 assert_eq!(rdata.protocol, protocol);
90 assert_eq!(rdata.algorithm, algorithm);
91 assert_eq!(&*rdata.public_key, &[1, 2, 3, 4, 5]);
92 }
93
94 #[test]
95 #[cfg(feature = "std")]
96 fn parse_sample() -> Result<(), Box<dyn std::error::Error>> {
97 use crate::{rdata::RData, ResourceRecord};
98 let sample_file = std::fs::read("samples/zonefile/DNSKEY.sample")?;
99
100 let sample_rdata = match ResourceRecord::parse(&mut (&sample_file[..]).into())?.rdata {
101 RData::DNSKEY(rdata) => rdata,
102 _ => unreachable!(),
103 };
104
105 assert_eq!(sample_rdata.flags, 256);
106 assert_eq!(sample_rdata.protocol, 3);
107 assert_eq!(sample_rdata.algorithm, 5);
108 assert_eq!(
109 *sample_rdata.public_key,
110 *b"\x01\x03\xd2\x2a\x6c\xa7\x7f\x35\xb8\x93\x20\x6f\xd3\x5e\x4c\x50\x6d\x83\x78\x84\x37\x09\xb9\x7e\x04\x16\x47\xe1\xbf\xf4\x3d\x8d\x64\xc6\x49\xaf\x1e\x37\x19\x73\xc9\xe8\x91\xfc\xe3\xdf\x51\x9a\x8c\x84\x0a\x63\xee\x42\xa6\xd2\xeb\xdd\xbb\x97\x03\x5d\x21\x5a\xa4\xe4\x17\xb1\xfa\x45\xfa\x11\xa9\x74\x1e\xa2\x09\x8c\x1d\xfa\x5f\xb5\xfe\xb3\x32\xfd\x4b\xc8\x15\x20\x89\xae\xf3\x6b\xa6\x44\xcc\xe2\x41\x3b\x3b\x72\xbe\x18\xcb\xef\x8d\xa2\x53\xf4\xe9\x3d\x21\x03\x86\x6d\x92\x34\xa2\xe2\x8d\xf5\x29\xa6\x7d\x54\x68\xdb\xef\xe3"
111 );
112
113 Ok(())
114 }
115}