xref: /webrtc/mdns/src/message/resource/soa.rs (revision ffe74184)
1 use super::*;
2 use crate::error::Result;
3 use crate::message::name::*;
4 use crate::message::packer::*;
5 
6 // An SOAResource is an SOA Resource record.
7 #[derive(Default, Debug, Clone, PartialEq, Eq)]
8 pub struct SoaResource {
9     pub ns: Name,
10     pub mbox: Name,
11     pub serial: u32,
12     pub refresh: u32,
13     pub retry: u32,
14     pub expire: u32,
15 
16     // min_ttl the is the default TTL of Resources records which did not
17     // contain a TTL value and the TTL of negative responses. (RFC 2308
18     // Section 4)
19     pub min_ttl: u32,
20 }
21 
22 impl fmt::Display for SoaResource {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result23     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24         write!(
25             f,
26             "dnsmessage.SOAResource{{ns: {}, mbox: {}, serial: {}, refresh: {}, retry: {}, expire: {}, min_ttl: {}}}",
27             self.ns,
28             self.mbox,
29             self.serial,
30             self.refresh,
31             self.retry,
32             self.expire,
33             self.min_ttl,
34         )
35     }
36 }
37 
38 impl ResourceBody for SoaResource {
real_type(&self) -> DnsType39     fn real_type(&self) -> DnsType {
40         DnsType::Soa
41     }
42 
43     // pack appends the wire format of the SOAResource to msg.
pack( &self, mut msg: Vec<u8>, compression: &mut Option<HashMap<String, usize>>, compression_off: usize, ) -> Result<Vec<u8>>44     fn pack(
45         &self,
46         mut msg: Vec<u8>,
47         compression: &mut Option<HashMap<String, usize>>,
48         compression_off: usize,
49     ) -> Result<Vec<u8>> {
50         msg = self.ns.pack(msg, compression, compression_off)?;
51         msg = self.mbox.pack(msg, compression, compression_off)?;
52         msg = pack_uint32(msg, self.serial);
53         msg = pack_uint32(msg, self.refresh);
54         msg = pack_uint32(msg, self.retry);
55         msg = pack_uint32(msg, self.expire);
56         Ok(pack_uint32(msg, self.min_ttl))
57     }
58 
unpack(&mut self, msg: &[u8], mut off: usize, _length: usize) -> Result<usize>59     fn unpack(&mut self, msg: &[u8], mut off: usize, _length: usize) -> Result<usize> {
60         off = self.ns.unpack(msg, off)?;
61         off = self.mbox.unpack(msg, off)?;
62 
63         let (serial, off) = unpack_uint32(msg, off)?;
64         self.serial = serial;
65 
66         let (refresh, off) = unpack_uint32(msg, off)?;
67         self.refresh = refresh;
68 
69         let (retry, off) = unpack_uint32(msg, off)?;
70         self.retry = retry;
71 
72         let (expire, off) = unpack_uint32(msg, off)?;
73         self.expire = expire;
74 
75         let (min_ttl, off) = unpack_uint32(msg, off)?;
76         self.min_ttl = min_ttl;
77 
78         Ok(off)
79     }
80 }
81