xref: /webrtc/mdns/src/message/resource/srv.rs (revision ffe74184)
1 use super::*;
2 use crate::error::Result;
3 use crate::message::name::*;
4 use crate::message::packer::*;
5 
6 // An SRVResource is an SRV Resource record.
7 #[derive(Default, Debug, Clone, PartialEq, Eq)]
8 pub struct SrvResource {
9     pub priority: u16,
10     pub weight: u16,
11     pub port: u16,
12     pub target: Name, // Not compressed as per RFC 2782.
13 }
14 
15 impl fmt::Display for SrvResource {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result16     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17         write!(
18             f,
19             "dnsmessage.SRVResource{{priority: {}, weight: {}, port: {}, target: {}}}",
20             self.priority, self.weight, self.port, self.target
21         )
22     }
23 }
24 
25 impl ResourceBody for SrvResource {
real_type(&self) -> DnsType26     fn real_type(&self) -> DnsType {
27         DnsType::Srv
28     }
29 
30     // pack appends the wire format of the SRVResource to msg.
pack( &self, mut msg: Vec<u8>, _compression: &mut Option<HashMap<String, usize>>, compression_off: usize, ) -> Result<Vec<u8>>31     fn pack(
32         &self,
33         mut msg: Vec<u8>,
34         _compression: &mut Option<HashMap<String, usize>>,
35         compression_off: usize,
36     ) -> Result<Vec<u8>> {
37         msg = pack_uint16(msg, self.priority);
38         msg = pack_uint16(msg, self.weight);
39         msg = pack_uint16(msg, self.port);
40         msg = self.target.pack(msg, &mut None, compression_off)?;
41         Ok(msg)
42     }
43 
unpack(&mut self, msg: &[u8], off: usize, _length: usize) -> Result<usize>44     fn unpack(&mut self, msg: &[u8], off: usize, _length: usize) -> Result<usize> {
45         let (priority, off) = unpack_uint16(msg, off)?;
46         self.priority = priority;
47 
48         let (weight, off) = unpack_uint16(msg, off)?;
49         self.weight = weight;
50 
51         let (port, off) = unpack_uint16(msg, off)?;
52         self.port = port;
53 
54         let off = self
55             .target
56             .unpack_compressed(msg, off, false /* allowCompression */)?;
57 
58         Ok(off)
59     }
60 }
61