flex_dns/rdata/
hip.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/// # Host identity protocol (HIP) Record
8/// This record is used to associate a HIP public key with a domain name.
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub struct Hip<'a> {
11    /// The usage field is an 8-bit value that defines the semantics of the
12    /// certificate association data field.
13    pub usage: u8,
14    /// The selector field is an 8-bit value that defines the type of data in the
15    /// certificate association data field.
16    pub selector: u8,
17    /// The matching type field is an 8-bit value that defines the semantics of
18    /// the certificate association data field.
19    pub matching_type: u8,
20    /// The certificate association data field is a variable-length string of octets
21    /// that contains the certificate association data.
22    pub certificate_association_data: Characters<'a>,
23}
24
25impl<'a> RDataParse<'a> for Hip<'a> {
26    #[inline]
27    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
28        let usage = u8::parse(rdata, i)?;
29        let selector = u8::parse(rdata, i)?;
30        let matching_type = u8::parse(rdata, i)?;
31        let certificate_association_data = Characters::parse(rdata, i)?;
32
33        Ok(Self {
34            usage,
35            selector,
36            matching_type,
37            certificate_association_data,
38        })
39    }
40}
41
42impl<'a> WriteBytes for Hip<'a> {
43    #[inline]
44    fn write<
45        const PTR_STORAGE: usize,
46        const DNS_SECTION: usize,
47        B: MutBuffer + Buffer,
48    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
49        let mut bytes = 0;
50
51        bytes += self.usage.write(message)?;
52        bytes += self.selector.write(message)?;
53        bytes += self.matching_type.write(message)?;
54        bytes += self.certificate_association_data.write(message)?;
55
56        Ok(bytes)
57    }
58}
59
60#[cfg(test)]
61mod test {
62    use crate::rdata::testutils::parse_write_test;
63
64    use super::*;
65
66    parse_write_test!(
67        7,
68        [
69            0x0e, // usage
70            0x63, // selector
71            0x43, // matching type
72            0x03, // digest len
73            b'w', b'w', b'w', // digest
74        ],
75        Hip {
76            usage: 14,
77            selector: 99,
78            matching_type: 67,
79            certificate_association_data: unsafe { Characters::new_unchecked(b"www") },
80        },
81    );
82}