use bytes::Bytes; use errors::*; use ser::packet::{DnsPacketData, DnsPacketWriteContext}; use ser::text::{DnsTextData, DnsTextFormatter, DnsTextContext, next_field}; use std::fmt; use std::io::Cursor; /// timestamp in seconds since epoch (ignoring leap seconds) /// /// Is expected to wrap around. #[derive(Clone, PartialEq, Eq, Debug)] pub struct Time(pub u32); impl DnsPacketData for Time { fn deserialize(data: &mut Cursor) -> Result { Ok(Time(DnsPacketData::deserialize(data)?)) } fn serialize(&self, context: &mut DnsPacketWriteContext, packet: &mut Vec) -> Result<()> { self.0.serialize(context, packet) } } struct Tm { year: u16, // 1970...9999 month: u8, // 01...12 day: u8, // 01..31 hour: u8, // 00..23 minute: u8, // 00..59 second: u8, // 00..59 } impl Tm { // 0..365 fn dayofyear(&self) -> u16 { let is_leap_year = (0 == self.year % 4) && ((0 != self.year % 100) || (0 == self.year % 400)); static MONTH_START: [u16; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; let leap_add = if self.month > 2 && is_leap_year { 1 } else { 0 }; MONTH_START[self.month as usize - 1] + leap_add + (self.day as u16 - 1) } fn epoch(&self) -> u64 { let yday = self.dayofyear(); self.second as u64 + 60 * self.minute as u64 + 3600 * self.hour as u64 + 86400 * yday as u64 + 31536000 * (self.year as u64 - 1970) + 86400 * ((self.year as u64 - 1969) / 4) - 86400 * ((self.year as u64 - 1901) / 100) + 86400 * ((self.year as u64 - 1900 + 299) / 400) } } impl ::std::str::FromStr for Tm { type Err = ::failure::Error; fn from_str(s: &str) -> ::errors::Result { ensure!(s.len() == 14, "Tm string must be exactly 14 digits long"); ensure!(s.as_bytes().iter().all(|&b| b >= b'0' && b <= b'9'), "Tm string must be exactly 14 digits long"); let year = s[0..4].parse::()?; ensure!(year >= 1970, "year must be >= 1970"); ensure!(year <= 9999, "year must be <= 9999"); fn p(s: &str, min: u8, max: u8, name: &'static str) -> ::errors::Result { let v = s.parse::()?; ensure!(v >= min && v <= max, "{} {} out of range {}-{}", name, v, min, max); Ok(v) } let month = p(&s[4..6], 1, 12, "month")?; let day = p(&s[6..8], 1, 31, "day")?; let hour = p(&s[8..10], 0, 23, "hour")?; let minute = p(&s[10..12], 0, 59, "minute")?; let second = p(&s[12..14], 0, 59, "second")?; if 2 == month { let is_leap_year = (0 == year % 4) && ((0 != year % 100) || (0 == year % 400)); ensure!(day < 30, "day {} out of range in february", day); ensure!(is_leap_year || day < 29, "day {} out of range in february (not a leap year)", day); } else { static DAYS_IN_MONTHS: [u8; 12] = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; let max_days = DAYS_IN_MONTHS[month as usize - 1]; ensure!(day <= max_days, "day {} out of range for month {}", day, month); } Ok(Tm{ year, month, day, hour, minute, second }) } } impl DnsTextData for Time { fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> ::errors::Result { let field = next_field(data)?; let epoch = field.parse::(); if field.len() == 14 && epoch.is_err() { let tm = field.parse::()?; Ok(Time(tm.epoch() as u32)) } else { Ok(Time(epoch?)) } } fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result { write!(f, "{}", self.0) } } pub const TIME48_MAX: u64 = 0xffff_ffff_ffff; /// 48-bit timestamp in seconds since epoch (ignoring leap seconds) #[derive(Clone, PartialEq, Eq, Debug)] pub struct Time48(pub u64); impl DnsPacketData for Time48 { fn deserialize(data: &mut Cursor) -> Result { let high16 = u16::deserialize(data)? as u64; let low32 = u32::deserialize(data)? as u64; Ok(Time48(high16 | low32)) } fn serialize(&self, context: &mut DnsPacketWriteContext, packet: &mut Vec) -> Result<()> { ensure!(self.0 <= TIME48_MAX, "time48 overflow"); let high16 = (self.0 >> 32) as u16; let low32 = self.0 as u32; high16.serialize(context, packet)?; low32.serialize(context, packet)?; Ok(()) } } impl DnsTextData for Time48 { fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> ::errors::Result { let field = next_field(data)?; let epoch = field.parse::()?; ensure!(epoch <= TIME48_MAX, "time48 overflow"); Ok(Time48(epoch)) } fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result { write!(f, "{}", self.0) } }