1 //! Logic to read and write resource record (streams)
4 use alloc::string::String;
7 use crate::query::QueryBuf;
9 pub(crate) fn read_u8(inp: &mut &[u8]) -> Result<u8, ()> {
10 let res = *inp.get(0).ok_or(())?;
14 pub(crate) fn read_u16(inp: &mut &[u8]) -> Result<u16, ()> {
15 if inp.len() < 2 { return Err(()); }
16 let mut bytes = [0; 2];
17 bytes.copy_from_slice(&inp[..2]);
19 Ok(u16::from_be_bytes(bytes))
21 pub(crate) fn read_u32(inp: &mut &[u8]) -> Result<u32, ()> {
22 if inp.len() < 4 { return Err(()); }
23 let mut bytes = [0; 4];
24 bytes.copy_from_slice(&inp[..4]);
26 Ok(u32::from_be_bytes(bytes))
29 fn do_read_wire_packet_labels(inp: &mut &[u8], wire_packet: &[u8], name: &mut String, recursion_limit: usize) -> Result<(), ()> {
31 let len = read_u8(inp)? as usize;
33 if name.is_empty() { *name += "."; }
35 } else if len >= 0xc0 && recursion_limit > 0 {
36 let offs = ((len & !0xc0) << 8) | read_u8(inp)? as usize;
37 if offs >= wire_packet.len() { return Err(()); }
38 do_read_wire_packet_labels(&mut &wire_packet[offs..], wire_packet, name, recursion_limit - 1)?;
41 if inp.len() <= len { return Err(()); }
42 *name += core::str::from_utf8(&inp[..len]).map_err(|_| ())?;
45 if name.len() > 255 { return Err(()); }
50 fn read_wire_packet_labels(inp: &mut &[u8], wire_packet: &[u8], name: &mut String) -> Result<(), ()> {
51 do_read_wire_packet_labels(inp, wire_packet, name, 255)
54 pub(crate) fn read_wire_packet_name(inp: &mut &[u8], wire_packet: &[u8]) -> Result<Name, ()> {
55 let mut name = String::with_capacity(1024);
56 read_wire_packet_labels(inp, wire_packet, &mut name)?;
60 pub(crate) trait Writer { fn write(&mut self, buf: &[u8]); }
61 impl Writer for Vec<u8> { fn write(&mut self, buf: &[u8]) { self.extend_from_slice(buf); } }
62 impl Writer for QueryBuf { fn write(&mut self, buf: &[u8]) { self.extend_from_slice(buf); } }
63 #[cfg(feature = "validation")]
64 impl Writer for ring::digest::Context { fn write(&mut self, buf: &[u8]) { self.update(buf); } }
65 pub(crate) fn write_name<W: Writer>(out: &mut W, name: &str) {
66 let canonical_name = name.to_ascii_lowercase();
67 if canonical_name == "." {
70 for label in canonical_name.split(".") {
71 out.write(&(label.len() as u8).to_be_bytes());
72 out.write(label.as_bytes());
76 pub(crate) fn name_len(name: &Name) -> u16 {
77 if name.as_str() == "." {
81 for label in name.split(".") {
82 res += 1 + label.len();
88 pub(crate) fn parse_wire_packet_rr(inp: &mut &[u8], wire_packet: &[u8]) -> Result<(RR, u32), ()> {
89 let name = read_wire_packet_name(inp, wire_packet)?;
90 let ty = read_u16(inp)?;
91 let class = read_u16(inp)?;
92 if class != 1 { return Err(()); } // We only support the INternet
93 let ttl = read_u32(inp)?;
94 let data_len = read_u16(inp)? as usize;
95 if inp.len() < data_len { return Err(()); }
96 let data = &inp[..data_len];
97 *inp = &inp[data_len..];
100 A::TYPE => RR::A(A::read_from_data(name, data, wire_packet)?),
101 AAAA::TYPE => RR::AAAA(AAAA::read_from_data(name, data, wire_packet)?),
102 NS::TYPE => RR::NS(NS::read_from_data(name, data, wire_packet)?),
103 Txt::TYPE => RR::Txt(Txt::read_from_data(name, data, wire_packet)?),
104 CName::TYPE => RR::CName(CName::read_from_data(name, data, wire_packet)?),
105 DName::TYPE => RR::DName(DName::read_from_data(name, data, wire_packet)?),
106 TLSA::TYPE => RR::TLSA(TLSA::read_from_data(name, data, wire_packet)?),
107 DnsKey::TYPE => RR::DnsKey(DnsKey::read_from_data(name, data, wire_packet)?),
108 DS::TYPE => RR::DS(DS::read_from_data(name, data, wire_packet)?),
109 RRSig::TYPE => RR::RRSig(RRSig::read_from_data(name, data, wire_packet)?),
115 pub(crate) fn parse_rr(inp: &mut &[u8]) -> Result<RR, ()> {
116 parse_wire_packet_rr(inp, &[]).map(|(rr, _)| rr)
119 /// Parse a stream of [`RR`]s from the format described in [RFC 9102](https://www.rfc-editor.org/rfc/rfc9102.html).
121 /// Note that this is only the series of `AuthenticationChain` records, and does not read the
122 /// `ExtSupportLifetime` field at the start of a `DnssecChainExtension`.
123 pub fn parse_rr_stream(mut inp: &[u8]) -> Result<Vec<RR>, ()> {
124 let mut res = Vec::with_capacity(32);
125 while !inp.is_empty() {
126 res.push(parse_rr(&mut inp)?);
131 /// Writes the given resource record in its wire encoding to the given `Vec`.
133 /// An [RFC 9102](https://www.rfc-editor.org/rfc/rfc9102.html) `AuthenticationChain` is simply a
134 /// series of such records with no additional bytes in between.
135 pub fn write_rr<RR: Record>(rr: &RR, ttl: u32, out: &mut Vec<u8>) {
136 write_name(out, rr.name());
137 out.extend_from_slice(&rr.ty().to_be_bytes());
138 out.extend_from_slice(&1u16.to_be_bytes()); // The INternet class
139 out.extend_from_slice(&ttl.to_be_bytes());
140 rr.write_u16_len_prefixed_data(out);