use alloc::string::ToString;
use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{
error::ProtoResult,
rr::{RData, RecordData, RecordType, domain::Name},
serialize::{
binary::{BinDecodable, BinDecoder, BinEncodable, BinEncoder, DecodeError, RDataEncoding},
txt::{ParseError, parse_ttl},
},
};
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
#[non_exhaustive]
pub struct SOA {
pub mname: Name,
pub rname: Name,
pub serial: u32,
pub refresh: i32,
pub retry: i32,
pub expire: i32,
pub minimum: u32,
}
impl SOA {
pub fn new(
mname: Name,
rname: Name,
serial: u32,
refresh: i32,
retry: i32,
expire: i32,
minimum: u32,
) -> Self {
Self {
mname,
rname,
serial,
refresh,
retry,
expire,
minimum,
}
}
pub(crate) fn from_tokens<'i, I: Iterator<Item = &'i str>>(
mut tokens: I,
origin: Option<&Name>,
) -> Result<Self, ParseError> {
let mname: Name = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("mname".to_string()))
.and_then(|s| Name::parse(s, origin).map_err(ParseError::from))?;
let rname: Name = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("rname".to_string()))
.and_then(|s| Name::parse(s, origin).map_err(ParseError::from))?;
let serial: u32 = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("serial".to_string()))
.and_then(parse_ttl)?;
let refresh: i32 = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("refresh".to_string()))
.and_then(parse_ttl)?
.try_into()
.map_err(|_e| ParseError::from("refresh outside i32 range"))?;
let retry: i32 = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("retry".to_string()))
.and_then(parse_ttl)?
.try_into()
.map_err(|_e| ParseError::from("retry outside i32 range"))?;
let expire: i32 = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("expire".to_string()))
.and_then(parse_ttl)?
.try_into()
.map_err(|_e| ParseError::from("expire outside i32 range"))?;
let minimum: u32 = tokens
.next()
.ok_or_else(|| ParseError::MissingToken("minimum".to_string()))
.and_then(parse_ttl)?;
Ok(Self::new(
mname, rname, serial, refresh, retry, expire, minimum,
))
}
pub fn increment_serial(&mut self) {
self.serial += 1; }
}
impl BinEncodable for SOA {
fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
let mut encoder = encoder.with_rdata_behavior(RDataEncoding::StandardRecord);
self.mname.emit(&mut encoder)?;
self.rname.emit(&mut encoder)?;
encoder.emit_u32(self.serial)?;
encoder.emit_i32(self.refresh)?;
encoder.emit_i32(self.retry)?;
encoder.emit_i32(self.expire)?;
encoder.emit_u32(self.minimum)?;
Ok(())
}
}
impl<'r> BinDecodable<'r> for SOA {
fn read(decoder: &mut BinDecoder<'r>) -> Result<Self, DecodeError> {
Ok(Self {
mname: Name::read(decoder)?,
rname: Name::read(decoder)?,
serial: decoder.read_u32()?.unverified(),
refresh: decoder.read_i32()?.unverified(),
retry: decoder.read_i32()?.unverified(),
expire: decoder.read_i32()?.unverified(),
minimum: decoder.read_u32()?.unverified(),
})
}
}
impl RecordData for SOA {
fn try_borrow(data: &RData) -> Option<&Self> {
match data {
RData::SOA(soa) => Some(soa),
_ => None,
}
}
fn record_type(&self) -> RecordType {
RecordType::SOA
}
fn into_rdata(self) -> RData {
RData::SOA(self)
}
}
impl fmt::Display for SOA {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(
f,
"{mname} {rname} {serial} {refresh} {retry} {expire} {min}",
mname = self.mname,
rname = self.rname,
serial = self.serial,
refresh = self.refresh,
retry = self.retry,
expire = self.expire,
min = self.minimum
)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::dbg_macro, clippy::print_stdout)]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::println;
use crate::{rr::RecordDataDecodable, serialize::binary::Restrict};
use super::*;
#[test]
fn test() {
use core::str::FromStr;
let rdata = SOA::new(
Name::from_str("m.example.com.").unwrap(),
Name::from_str("r.example.com.").unwrap(),
1,
2,
3,
4,
5,
);
let mut bytes = Vec::new();
let mut encoder: BinEncoder<'_> = BinEncoder::new(&mut bytes);
assert!(rdata.emit(&mut encoder).is_ok());
let bytes = encoder.into_bytes();
let len = bytes.len() as u16;
#[cfg(feature = "std")]
println!("bytes: {bytes:?}");
let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let read_rdata = SOA::read_data(&mut decoder, Restrict::new(len)).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}
#[test]
fn test_parse() {
use core::str::FromStr;
let soa_tokens = vec![
"hickory-dns.org.",
"root.hickory-dns.org.",
"199609203",
"8h",
"120m",
"7d",
"24h",
];
let parsed_soa = SOA::from_tokens(
soa_tokens.into_iter(),
Some(&Name::from_str("example.com.").unwrap()),
)
.expect("failed to parse tokens");
let expected_soa = SOA::new(
"hickory-dns.org.".parse().unwrap(),
"root.hickory-dns.org.".parse().unwrap(),
199609203,
28800,
7200,
604800,
86400,
);
assert_eq!(parsed_soa, expected_soa);
}
}