aboutsummaryrefslogtreecommitdiffstats
path: root/src/noise/timestamp.rs
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-07-28 17:09:27 +0200
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-07-28 17:09:27 +0200
commit2c81abbe7973dfbe6113d66f9d92b6b4ad3b0afa (patch)
tree62f32c732564544edf1ca5456cbd84e87385ae38 /src/noise/timestamp.rs
parentAdded ability to remove peer from device (diff)
downloadwireguard-rs-2c81abbe7973dfbe6113d66f9d92b6b4ad3b0afa.tar.xz
wireguard-rs-2c81abbe7973dfbe6113d66f9d92b6b4ad3b0afa.zip
Restructured for wireguard-rs
Diffstat (limited to 'src/noise/timestamp.rs')
-rw-r--r--src/noise/timestamp.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/noise/timestamp.rs b/src/noise/timestamp.rs
new file mode 100644
index 0000000..0996f8b
--- /dev/null
+++ b/src/noise/timestamp.rs
@@ -0,0 +1,34 @@
+use std::time::{SystemTime, UNIX_EPOCH};
+
+const TAI64_EPOCH: u64 = 0x4000000000000000;
+
+pub type TAI64N = [u8; 12];
+
+pub fn zero() -> TAI64N {
+ [0u8; 12]
+}
+
+pub fn now() -> TAI64N {
+ // get system time as duration
+ let sysnow = SystemTime::now();
+ let delta = sysnow.duration_since(UNIX_EPOCH).unwrap();
+
+ // convert to tai64n
+ let tai64_secs = delta.as_secs() + TAI64_EPOCH;
+ let tai64_nano = delta.subsec_nanos();
+
+ // serialize
+ let mut res = [0u8; 12];
+ res[..8].copy_from_slice(&tai64_secs.to_be_bytes()[..]);
+ res[8..].copy_from_slice(&tai64_nano.to_be_bytes()[..]);
+ res
+}
+
+pub fn compare(old: &TAI64N, new: &TAI64N) -> bool {
+ for i in 0..12 {
+ if new[i] > old[i] {
+ return true;
+ }
+ }
+ return false;
+}