Skip to content

Commit 03d6d8f

Browse files
committed
Alias types for unit and duration
1 parent 4122f81 commit 03d6d8f

File tree

8 files changed

+35
-52
lines changed

8 files changed

+35
-52
lines changed

boringtun/src/device/api.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ use super::drop_privileges::get_saved_ids;
66
use super::{AllowedIP, Device, Error, SocketAddr};
77
use crate::device::Action;
88
use crate::serialization::KeyBytes;
9-
use crate::sleepyinstant::ClockImpl;
9+
use crate::sleepyinstant::ClockUnit;
1010
use crate::x25519;
1111
use embedded_time::duration::{Nanoseconds, Seconds};
12-
use embedded_time::Clock;
1312
use hex::encode as encode_hex;
1413
use libc::*;
1514
use std::convert::TryFrom;
@@ -193,10 +192,9 @@ fn api_get(writer: &mut BufWriter<&UnixStream>, d: &Device) -> i32 {
193192
}
194193

195194
if let Some(time) = p.time_since_last_handshake() {
196-
let secs = Seconds::<<ClockImpl as Clock>::T>::try_from(time).unwrap();
195+
let secs = Seconds::<ClockUnit>::try_from(time).unwrap();
197196
writeln!(writer, "last_handshake_time_sec={secs}");
198-
let sub =
199-
Nanoseconds::<<ClockImpl as Clock>::T>::try_from(time).unwrap() % Seconds(1u32);
197+
let sub = Nanoseconds::<ClockUnit>::try_from(time).unwrap() % Seconds(1u32);
200198
writeln!(writer, "last_handshake_time_nsec={sub}");
201199
}
202200

boringtun/src/device/peer.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Copyright (c) 2019 Cloudflare, Inc. All rights reserved.
22
// SPDX-License-Identifier: BSD-3-Clause
33

4-
use embedded_time::duration::Generic;
54
use parking_lot::RwLock;
65
use socket2::{Domain, Protocol, Type};
76

87
use crate::device::{AllowedIps, Error};
98
use crate::noise::{Tunn, TunnResult};
10-
use crate::sleepyinstant::ClockImpl;
11-
use embedded_time::Clock;
9+
use crate::sleepyinstant::ClockDuration;
1210
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6};
1311
use std::str::FromStr;
1412

@@ -154,7 +152,7 @@ impl Peer {
154152
self.allowed_ips.iter().map(|(_, ip, cidr)| (ip, cidr))
155153
}
156154

157-
pub fn time_since_last_handshake(&self) -> Option<Generic<<ClockImpl as Clock>::T>> {
155+
pub fn time_since_last_handshake(&self) -> Option<ClockDuration> {
158156
self.tunnel.time_since_last_handshake()
159157
}
160158

boringtun/src/ffi/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,7 @@ pub unsafe extern "C" fn wireguard_stats(tunnel: *const Mutex<RawMutex, Tunn>) -
395395
let (time, tx_bytes, rx_bytes, estimated_loss, estimated_rtt) = tunnel.stats();
396396
stats {
397397
time_since_last_handshake: time
398-
.map(|t| {
399-
Seconds::<<ClockImpl as Clock>::T>::try_from(t)
400-
.unwrap()
401-
.integer() as i64
402-
})
398+
.map(|t| Seconds::<ClockUnit>::try_from(t).unwrap().integer() as i64)
403399
.unwrap_or(-1),
404400
tx_bytes,
405401
rx_bytes,

boringtun/src/noise/handshake.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::{HandshakeInit, HandshakeResponse, PacketCookieReply};
55
use crate::noise::errors::WireGuardError;
66
use crate::noise::session::Session;
7-
use crate::sleepyinstant::{ClockImpl, Instant};
7+
use crate::sleepyinstant::{ClockDuration, ClockImpl, ClockUnit, Instant};
88
use crate::x25519;
99
use aead::{Aead, Payload};
1010
use alloc::borrow::ToOwned;
@@ -14,7 +14,7 @@ use blake2::{Blake2s256, Blake2sMac, Digest};
1414
use chacha20poly1305::XChaCha20Poly1305;
1515
use core::convert::TryFrom;
1616
use core::convert::TryInto;
17-
use embedded_time::duration::{Generic, Milliseconds, Nanoseconds, Seconds};
17+
use embedded_time::duration::{Milliseconds, Nanoseconds, Seconds};
1818
use embedded_time::fixed_point::FixedPoint;
1919
use embedded_time::Clock;
2020
use ring::aead::{Aad, LessSafeKey, Nonce, UnboundKey, CHACHA20_POLY1305};
@@ -168,7 +168,7 @@ struct Tai64N {
168168
#[derive(Debug)]
169169
/// This struct computes a [Tai64N](https://cr.yp.to/libtai/tai64.html) timestamp from current system time
170170
struct TimeStamper {
171-
duration_at_start: Generic<<ClockImpl as Clock>::T>,
171+
duration_at_start: ClockDuration,
172172
instant_at_start: Instant,
173173
}
174174

@@ -189,9 +189,9 @@ impl TimeStamper {
189189
const TAI64_BASE: u64 = (1u64 << 62) + 37;
190190
let mut ext_stamp = [0u8; 12];
191191
let stamp = Instant::now().duration_since(self.instant_at_start) + self.duration_at_start;
192-
let secs = Seconds::<<ClockImpl as Clock>::T>::try_from(stamp).unwrap();
192+
let secs = Seconds::<ClockUnit>::try_from(stamp).unwrap();
193193
ext_stamp[0..8].copy_from_slice(&(secs.integer() + TAI64_BASE).to_be_bytes());
194-
let sub = Nanoseconds::<<ClockImpl as Clock>::T>::try_from(stamp).unwrap() % Seconds(1u32);
194+
let sub = Nanoseconds::<ClockUnit>::try_from(stamp).unwrap() % Seconds(1u32);
195195
ext_stamp[8..12].copy_from_slice(&(sub.integer() as u32).to_be_bytes());
196196
ext_stamp
197197
}

boringtun/src/noise/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ use crate::noise::rate_limiter::RateLimiter;
1414
use crate::noise::timers::{TimerName, Timers};
1515
use crate::x25519;
1616

17-
use crate::sleepyinstant::ClockImpl;
17+
use crate::sleepyinstant::ClockDuration;
1818
use alloc::collections::VecDeque;
1919
use alloc::sync::Arc;
2020
use alloc::vec::Vec;
2121
use core::convert::{TryFrom, TryInto};
2222
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
23-
use embedded_time::duration::Generic;
24-
use embedded_time::Clock;
2523

2624
/// The default value to use for rate limiting, when no other rate limiter is defined
2725
const PEER_HANDSHAKE_RATE_LIMIT: u64 = 10;
@@ -577,15 +575,7 @@ impl Tunn {
577575
/// * Time since last handshake in seconds
578576
/// * Data bytes sent
579577
/// * Data bytes received
580-
pub fn stats(
581-
&self,
582-
) -> (
583-
Option<Generic<<ClockImpl as Clock>::T>>,
584-
usize,
585-
usize,
586-
f32,
587-
Option<u32>,
588-
) {
578+
pub fn stats(&self) -> (Option<ClockDuration>, usize, usize, f32, Option<u32>) {
589579
let time = self.time_since_last_handshake();
590580
let tx_bytes = self.tx_bytes;
591581
let rx_bytes = self.rx_bytes;

boringtun/src/noise/rate_limiter.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ use core::convert::TryFrom;
66
use core::net::IpAddr;
77
use core::sync::atomic::{AtomicUsize, Ordering};
88

9-
use crate::sleepyinstant::Instant;
9+
use crate::sleepyinstant::{ClockUnit, Instant};
1010

11-
use crate::sleepyinstant::ClockImpl;
1211
use aead::array::Array;
1312
use aead::{AeadInOut, KeyInit};
1413
#[cfg(feature = "ariel-os")]
1514
use ariel_os_lock::RawMutex;
1615
use chacha20poly1305::{Key, XChaCha20Poly1305};
1716
use embedded_time::duration::Seconds;
1817
use embedded_time::fixed_point::FixedPoint;
19-
use embedded_time::Clock;
2018
use lock_api::Mutex;
2119
#[cfg(feature = "std")]
2220
use parking_lot::RawMutex;
@@ -102,11 +100,9 @@ impl RateLimiter {
102100

103101
// The current cookie for a given IP is the MAC(responder.changing_secret_every_two_minutes, initiator.ip_address)
104102
// First we derive the secret from the current time, the value of cur_counter would change with time.
105-
let cur_counter = Seconds::<<ClockImpl as Clock>::T>::try_from(
106-
Instant::now().duration_since(self.start_time),
107-
)
108-
.unwrap()
109-
/ COOKIE_REFRESH.integer() as <ClockImpl as Clock>::T;
103+
let cur_counter =
104+
Seconds::<ClockUnit>::try_from(Instant::now().duration_since(self.start_time)).unwrap()
105+
/ COOKIE_REFRESH.integer() as ClockUnit;
110106

111107
// Next we derive the cookie
112108
b2s_keyed_mac_16_2(

boringtun/src/noise/timers.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
use super::errors::WireGuardError;
55
use crate::noise::{Tunn, TunnResult};
6-
use crate::sleepyinstant::ClockImpl;
76
use crate::sleepyinstant::Instant;
7+
use crate::sleepyinstant::{ClockDuration, ClockUnit};
88
use core::mem;
99
use core::ops::{Index, IndexMut};
10-
use embedded_time::duration::{Generic, Seconds};
11-
use embedded_time::Clock;
10+
use embedded_time::duration::Seconds;
1211

1312
// Some constants, represent time in seconds
1413
// https://www.wireguard.com/papers/wireguard.pdf#page=14
@@ -50,8 +49,8 @@ pub struct Timers {
5049
is_initiator: bool,
5150
/// Start time of the tunnel
5251
time_started: Instant,
53-
timers: [Generic<<ClockImpl as Clock>::T>; TimerName::Top as usize],
54-
pub(super) session_timers: [Generic<<ClockImpl as Clock>::T>; super::N_SESSIONS],
52+
timers: [ClockDuration; TimerName::Top as usize],
53+
pub(super) session_timers: [ClockDuration; super::N_SESSIONS],
5554
/// Did we receive data without sending anything back?
5655
want_keepalive: bool,
5756
/// Did we send data without hearing back?
@@ -92,14 +91,14 @@ impl Timers {
9291
}
9392

9493
impl Index<TimerName> for Timers {
95-
type Output = Generic<<ClockImpl as Clock>::T>;
96-
fn index(&self, index: TimerName) -> &Generic<<ClockImpl as Clock>::T> {
94+
type Output = ClockDuration;
95+
fn index(&self, index: TimerName) -> &ClockDuration {
9796
&self.timers[index as usize]
9897
}
9998
}
10099

101100
impl IndexMut<TimerName> for Timers {
102-
fn index_mut(&mut self, index: TimerName) -> &mut Generic<<ClockImpl as Clock>::T> {
101+
fn index_mut(&mut self, index: TimerName) -> &mut ClockDuration {
103102
&mut self.timers[index as usize]
104103
}
105104
}
@@ -145,7 +144,7 @@ impl Tunn {
145144
self.timers.clear();
146145
}
147146

148-
fn update_session_timers(&mut self, time_now: Generic<<ClockImpl as Clock>::T>) {
147+
fn update_session_timers(&mut self, time_now: ClockDuration) {
149148
let timers = &mut self.timers;
150149

151150
for (i, t) in timers.session_timers.iter_mut().enumerate() {
@@ -286,7 +285,7 @@ impl Tunn {
286285
// Persistent KEEPALIVE
287286
if persistent_keepalive > 0
288287
&& (now - self.timers[TimePersistentKeepalive]
289-
>= Seconds::<<ClockImpl as Clock>::T>(persistent_keepalive as _))
288+
>= Seconds::<ClockUnit>(persistent_keepalive as _))
290289
{
291290
tracing::debug!("KEEPALIVE(PERSISTENT_KEEPALIVE)");
292291
self.timer_tick(TimePersistentKeepalive);
@@ -307,7 +306,7 @@ impl Tunn {
307306
TunnResult::Done
308307
}
309308

310-
pub fn time_since_last_handshake(&self) -> Option<Generic<<ClockImpl as Clock>::T>> {
309+
pub fn time_since_last_handshake(&self) -> Option<ClockDuration> {
311310
let current_session = self.current;
312311
if self.sessions[current_session % super::N_SESSIONS].is_some() {
313312
let duration_since_tun_start = Instant::now().duration_since(self.timers.time_started);

boringtun/src/sleepyinstant/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ pub struct Instant {
4343
t: embedded_time::Instant<ClockImpl>,
4444
}
4545

46+
/// The underlying unit of time for the clock.
47+
pub type ClockUnit = <ClockImpl as Clock>::T;
48+
49+
/// A span of time between two instants of the clock.
50+
pub type ClockDuration = Generic<ClockUnit>;
51+
4652
impl Instant {
4753
/// Returns an instant corresponding to "now".
4854
pub fn now() -> Self {
@@ -57,12 +63,12 @@ impl Instant {
5763
/// # Panics
5864
///
5965
/// panics when `earlier` was later than `self`.
60-
pub fn duration_since(&self, earlier: Instant) -> Generic<<ClockImpl as Clock>::T> {
66+
pub fn duration_since(&self, earlier: Instant) -> ClockDuration {
6167
self.t.checked_duration_since(&earlier.t).unwrap()
6268
}
6369

6470
/// Returns the amount of time elapsed since this instant was created.
65-
pub fn elapsed(&self) -> Generic<<ClockImpl as Clock>::T> {
71+
pub fn elapsed(&self) -> ClockDuration {
6672
Self::now().duration_since(*self)
6773
}
6874
}

0 commit comments

Comments
 (0)