From 6aa9d9e08f41306f878b7e584dad211a7d4b901a Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Thu, 3 May 2018 14:29:59 -0700 Subject: global: time.rs -> timestamp.rs --- src/interface/peer_server.rs | 2 +- src/lib.rs | 2 +- src/peer.rs | 2 +- src/time.rs | 81 -------------------------------------------- src/timestamp.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 84 deletions(-) delete mode 100644 src/time.rs create mode 100644 src/timestamp.rs diff --git a/src/interface/peer_server.rs b/src/interface/peer_server.rs index aa4f53e..95970df 100644 --- a/src/interface/peer_server.rs +++ b/src/interface/peer_server.rs @@ -4,7 +4,7 @@ use cookie; use interface::{SharedPeer, SharedState, State, UtunPacket}; use message::{Message, Initiation, Response, CookieReply, Transport}; use peer::{Peer, SessionType, SessionTransition}; -use time::Timestamp; +use timestamp::Timestamp; use timer::{Timer, TimerMessage}; use byteorder::{ByteOrder, LittleEndian}; diff --git a/src/lib.rs b/src/lib.rs index 5302f19..5c39c68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ extern crate x25519_dalek; pub mod interface; pub mod peer; pub mod noise; -pub mod time; +pub mod timestamp; pub mod types; mod anti_replay; diff --git a/src/peer.rs b/src/peer.rs index 4899893..f01ac5e 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -14,7 +14,7 @@ use std::collections::VecDeque; use std::fmt::{self, Debug, Display, Formatter}; use std::time::{SystemTime, UNIX_EPOCH}; use hex; -use time::{Tai64n, Timestamp}; +use timestamp::{Tai64n, Timestamp}; use snow; use types::PeerInfo; use udp::Endpoint; diff --git a/src/time.rs b/src/time.rs deleted file mode 100644 index 0f1322c..0000000 --- a/src/time.rs +++ /dev/null @@ -1,81 +0,0 @@ -use byteorder::{ByteOrder, BigEndian}; -use std::ops::Deref; -use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; - -const TAI64N_BASE: i64 = 4611686018427387914; - -#[derive(PartialEq, PartialOrd)] -pub struct Tai64n { - tai64n: [u8; 12] -} - -impl Tai64n { - pub fn now() -> Tai64n { - let mut tai64n = [0u8; 12]; - let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); - BigEndian::write_i64(&mut tai64n[0..], TAI64N_BASE + now.as_secs() as i64); - BigEndian::write_i32(&mut tai64n[8..], now.subsec_nanos() as i32); - - Tai64n { tai64n } - } -} - -impl Deref for Tai64n { - type Target = [u8; 12]; - - fn deref(&self) -> &[u8; 12] { - &self.tai64n - } -} - -impl From<[u8; 12]> for Tai64n { - fn from(tai64n: [u8; 12]) -> Self { - Tai64n { tai64n } - } -} - -// TODO I don't like this. -lazy_static! { - pub static ref FOREVER: Duration = Duration::from_secs(0xffffffff); - pub static ref FOREVER_AGO: Instant = Instant::now() - Duration::from_secs(0xffffffff); -} - -pub struct Timestamp(Option); - -impl Default for Timestamp { - fn default() -> Self { - Timestamp(None) - } -} - -impl Deref for Timestamp { - type Target = Instant; - - fn deref(&self) -> &Self::Target { - match self.0 { - Some(ref time) => time, - None => &*FOREVER_AGO, - } - } -} - -impl Timestamp { - pub fn now() -> Self { - Timestamp(Some(Instant::now())) - } - - pub fn unset() -> Self { - Timestamp(None) - } - - pub fn is_set(&self) -> bool { - self.0.is_some() - } - - pub fn elapsed(&self) -> Duration { - match self.0 { - Some(ref time) => Instant::now().duration_since(*time), - None => *FOREVER, - } - } -} diff --git a/src/timestamp.rs b/src/timestamp.rs new file mode 100644 index 0000000..0f1322c --- /dev/null +++ b/src/timestamp.rs @@ -0,0 +1,81 @@ +use byteorder::{ByteOrder, BigEndian}; +use std::ops::Deref; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +const TAI64N_BASE: i64 = 4611686018427387914; + +#[derive(PartialEq, PartialOrd)] +pub struct Tai64n { + tai64n: [u8; 12] +} + +impl Tai64n { + pub fn now() -> Tai64n { + let mut tai64n = [0u8; 12]; + let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + BigEndian::write_i64(&mut tai64n[0..], TAI64N_BASE + now.as_secs() as i64); + BigEndian::write_i32(&mut tai64n[8..], now.subsec_nanos() as i32); + + Tai64n { tai64n } + } +} + +impl Deref for Tai64n { + type Target = [u8; 12]; + + fn deref(&self) -> &[u8; 12] { + &self.tai64n + } +} + +impl From<[u8; 12]> for Tai64n { + fn from(tai64n: [u8; 12]) -> Self { + Tai64n { tai64n } + } +} + +// TODO I don't like this. +lazy_static! { + pub static ref FOREVER: Duration = Duration::from_secs(0xffffffff); + pub static ref FOREVER_AGO: Instant = Instant::now() - Duration::from_secs(0xffffffff); +} + +pub struct Timestamp(Option); + +impl Default for Timestamp { + fn default() -> Self { + Timestamp(None) + } +} + +impl Deref for Timestamp { + type Target = Instant; + + fn deref(&self) -> &Self::Target { + match self.0 { + Some(ref time) => time, + None => &*FOREVER_AGO, + } + } +} + +impl Timestamp { + pub fn now() -> Self { + Timestamp(Some(Instant::now())) + } + + pub fn unset() -> Self { + Timestamp(None) + } + + pub fn is_set(&self) -> bool { + self.0.is_some() + } + + pub fn elapsed(&self) -> Duration { + match self.0 { + Some(ref time) => Instant::now().duration_since(*time), + None => *FOREVER, + } + } +} -- cgit v1.2.3-59-g8ed1b