flex_dns/rdata/
sshfp.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::characters::Characters;
3use crate::parse::Parse;
4use crate::rdata::{RData, RDataParse};
5use crate::write::WriteBytes;
6
7/// # SSH public key fingerprint record
8/// This record is used to store the fingerprint of an SSH public key.
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub struct SshFp<'a> {
11    /// The algorithm used to generate the fingerprint.
12    pub algorithm: u8,
13    /// The fingerprint type.
14    pub fingerprint_type: u8,
15    /// The fingerprint data.
16    pub data: Characters<'a>,
17}
18
19impl<'a> RDataParse<'a> for SshFp<'a> {
20    #[inline]
21    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
22        let algorithm = u8::parse(rdata.buffer, i)?;
23        let fingerprint_type = u8::parse(rdata.buffer, i)?;
24        let data = Characters::parse(rdata.buffer, i)?;
25
26        Ok(Self {
27            algorithm,
28            fingerprint_type,
29            data,
30        })
31    }
32}
33
34impl<'a> WriteBytes for SshFp<'a> {
35    #[inline]
36    fn write<
37        const PTR_STORAGE: usize,
38        const DNS_SECTION: usize,
39        B: MutBuffer + Buffer,
40    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
41        let mut bytes = 0;
42
43        bytes += self.algorithm.write(message)?;
44        bytes += self.fingerprint_type.write(message)?;
45        bytes += self.data.write(message)?;
46
47        Ok(bytes)
48    }
49}
50
51#[cfg(test)]
52mod test {
53    use crate::rdata::testutils::parse_write_test;
54
55    use super::*;
56
57    parse_write_test!(
58        6,
59        [
60            0x01, // algorithm
61            0x02, // fingerprint type
62            0x03, // length of "www"
63            b'w', b'w', b'w', // "www"
64        ],
65        SshFp {
66            algorithm: 1,
67            fingerprint_type: 2,
68            data: unsafe { Characters::new_unchecked(b"www") },
69        },
70    );
71}