2017-12-21 12:32:14 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use errors::*;
|
2017-12-27 14:24:11 +00:00
|
|
|
use ser::packet::{DnsPacketData, DnsPacketWriteContext};
|
2017-12-26 21:23:51 +00:00
|
|
|
use ser::text::{DnsTextData, DnsTextFormatter, DnsTextContext, next_field};
|
2017-12-21 12:32:14 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
2017-12-26 21:23:47 +00:00
|
|
|
/// timestamp in seconds since epoch (ignoring leap seconds)
|
2017-12-21 12:32:14 +00:00
|
|
|
///
|
2017-12-26 21:23:47 +00:00
|
|
|
/// Is expected to wrap around.
|
2017-12-21 12:32:14 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Time(u32);
|
|
|
|
|
|
|
|
impl DnsPacketData for Time {
|
|
|
|
fn deserialize(data: &mut Cursor<Bytes>) -> Result<Self> {
|
|
|
|
Ok(Time(DnsPacketData::deserialize(data)?))
|
|
|
|
}
|
2017-12-27 14:24:11 +00:00
|
|
|
|
|
|
|
fn serialize(&self, context: &mut DnsPacketWriteContext, packet: &mut Vec<u8>) -> Result<()> {
|
|
|
|
self.0.serialize(context, packet)
|
|
|
|
}
|
2017-12-21 12:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsTextData for Time {
|
2017-12-26 21:23:51 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> ::errors::Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
let field = next_field(data)?;
|
|
|
|
let epoch = field.parse::<u32>();
|
|
|
|
if field.len() == 14 && epoch.is_err() {
|
|
|
|
unimplemented!()
|
|
|
|
} else {
|
|
|
|
Ok(Time(epoch?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|