rust-dnsbox/lib/dnsbox-base/src/ser/rrdata.rs

141 lines
3.3 KiB
Rust
Raw Normal View History

2020-03-07 15:57:47 +00:00
use crate::common_types::{classes, Class, Type};
2019-07-01 15:43:34 +00:00
use crate::errors::*;
use crate::records::UnknownRecord;
use crate::ser::packet::{DnsPacketData, DnsPacketWriteContext};
2020-03-07 15:57:47 +00:00
use crate::ser::text::{DnsTextContext, DnsTextData, DnsTextFormatter};
use bytes::Bytes;
2017-12-29 21:25:43 +00:00
use std::any::Any;
2017-12-26 21:23:47 +00:00
use std::borrow::Cow;
2017-12-26 16:30:08 +00:00
use std::fmt;
use std::io::Cursor;
2019-07-01 15:43:34 +00:00
pub use dnsbox_derive::RRData;
2017-12-26 16:30:08 +00:00
pub trait RRDataPacket {
2020-03-07 15:57:47 +00:00
fn deserialize_rr_data(
ttl: u32,
rr_class: Class,
rr_type: Type,
data: &mut Cursor<Bytes>,
) -> Result<Self>
2017-12-26 16:30:08 +00:00
where
2020-03-07 15:57:47 +00:00
Self: Sized;
2017-12-26 21:23:47 +00:00
fn rr_type(&self) -> Type;
2017-12-27 14:24:11 +00:00
2020-03-07 15:57:47 +00:00
fn serialize_rr_data(
&self,
context: &mut DnsPacketWriteContext,
packet: &mut Vec<u8>,
) -> Result<()>;
2017-12-26 16:30:08 +00:00
}
impl<T: DnsPacketData + StaticRRData> RRDataPacket for T {
2020-03-07 15:57:47 +00:00
fn deserialize_rr_data(
_ttl: u32,
rr_class: Class,
rr_type: Type,
data: &mut Cursor<Bytes>,
) -> Result<Self> {
2019-07-01 15:43:34 +00:00
failure::ensure!(rr_type == T::TYPE, "type mismatch");
2017-12-26 16:30:08 +00:00
if T::CLASS != classes::ANY {
2020-03-07 15:57:47 +00:00
failure::ensure!(
rr_class == T::CLASS,
"class mismatch: got {}, need {}",
rr_class,
T::CLASS
);
2017-12-26 16:30:08 +00:00
}
T::deserialize(data)
}
2017-12-26 21:23:47 +00:00
fn rr_type(&self) -> Type {
T::TYPE
}
2017-12-27 14:24:11 +00:00
2020-03-07 15:57:47 +00:00
fn serialize_rr_data(
&self,
context: &mut DnsPacketWriteContext,
packet: &mut Vec<u8>,
) -> Result<()> {
2017-12-27 14:24:11 +00:00
self.serialize(context, packet)
}
2017-12-26 16:30:08 +00:00
}
2017-12-26 21:23:47 +00:00
pub trait RRDataText {
2017-12-26 21:23:51 +00:00
fn dns_parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Self>
2017-12-26 16:30:08 +00:00
where
2020-03-07 15:57:47 +00:00
Self: Sized;
2017-12-21 12:32:14 +00:00
2017-12-26 16:30:08 +00:00
// format might fail if there is no (known) text representation.
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result;
2017-12-26 21:23:47 +00:00
2017-12-27 11:41:54 +00:00
fn rr_type_txt(&self) -> Cow<'static, str>;
2017-12-21 12:32:14 +00:00
}
2017-12-26 21:23:47 +00:00
impl<T: DnsTextData + StaticRRData> RRDataText for T {
2017-12-26 21:23:51 +00:00
fn dns_parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Self>
2017-12-26 21:23:47 +00:00
where
Self: Sized,
{
2019-07-01 15:43:34 +00:00
failure::ensure!(context.record_type() == Some(T::TYPE), "type mismatch");
2020-03-07 15:57:47 +00:00
let rr_class = context
.zone_class()
.expect("require zone CLASS to parse record");
2017-12-26 21:23:47 +00:00
if T::CLASS != classes::ANY {
2020-03-07 15:57:47 +00:00
failure::ensure!(
rr_class == T::CLASS,
"class mismatch: got {}, need {}",
rr_class,
T::CLASS
);
2017-12-26 21:23:47 +00:00
}
2019-07-01 15:43:34 +00:00
failure::ensure!(context.last_ttl().is_some(), "require TTL to parse record");
2017-12-26 21:23:51 +00:00
T::dns_parse(context, data)
2017-12-26 21:23:47 +00:00
}
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result {
self.dns_format(f)
2017-12-21 12:32:14 +00:00
}
2017-12-26 21:23:47 +00:00
2017-12-27 11:41:54 +00:00
fn rr_type_txt(&self) -> Cow<'static, str> {
Cow::Borrowed(T::NAME)
2017-12-26 21:23:47 +00:00
}
}
2017-12-29 21:25:43 +00:00
pub trait RRData: RRDataPacket + RRDataText + fmt::Debug + 'static {
2019-09-08 13:34:28 +00:00
fn clone_box(&self) -> Box<dyn RRData>;
2017-12-29 21:25:43 +00:00
2019-09-08 13:34:28 +00:00
fn as_any(&self) -> &dyn Any;
// (type, rrdata)
fn text(&self) -> Result<(String, String)> {
let mut buf = String::new();
match self.dns_format_rr_data(&mut DnsTextFormatter::new(&mut buf)) {
2020-03-07 15:57:47 +00:00
Ok(()) => return Ok((self.rr_type_txt().into(), buf)),
Err(_) => (),
}
let mut raw = Vec::new();
self.serialize_rr_data(&mut DnsPacketWriteContext::new(), &mut raw)?;
let ur = UnknownRecord::new(self.rr_type(), raw.into());
// formatting UnknownRecord should not fail
buf.clear();
2020-03-07 15:57:47 +00:00
ur.dns_format_rr_data(&mut DnsTextFormatter::new(&mut buf))
.expect("formatting UnknownRecord must not fail");
Ok((ur.rr_type_txt().into(), buf))
}
2017-12-27 17:38:02 +00:00
}
2019-09-08 13:34:28 +00:00
impl Clone for Box<dyn RRData> {
2017-12-27 17:38:02 +00:00
fn clone(&self) -> Self {
self.clone_box()
}
2017-12-21 12:32:14 +00:00
}
2017-12-16 20:58:18 +00:00
2017-12-21 12:32:14 +00:00
pub trait StaticRRData: RRData {
const TYPE: Type;
const NAME: &'static str;
2017-12-26 16:30:08 +00:00
// classes::ANY marks class independent types
const CLASS: Class;
2017-12-16 20:58:18 +00:00
}