Skip to content

Commit

Permalink
Assimilated timeout definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
voidc committed Aug 28, 2020
1 parent 272953e commit 34d8ed5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
13 changes: 5 additions & 8 deletions src/onion/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use tokio::time;
use tokio::time::Duration;

/// timeout applied if there is no traffic on a circuit
pub(crate) const TIMEOUT_IDLE: u64 = 120;
pub(crate) const IDLE_TIMEOUT: Duration = Duration::from_secs(120);
/// timeout applied for a teardown operation
const TIMEOUT_TEARDOWN: u64 = 5;
const TEARDOWN_TIMEOUT: Duration = Duration::from_secs(5);

pub(crate) type CircuitId = u16;

Expand Down Expand Up @@ -51,7 +51,7 @@ impl Circuit {
}

pub(crate) async fn teardown_with_timeout(&self, rng: &rand::SystemRandom) {
match time::timeout(Duration::from_secs(TIMEOUT_TEARDOWN), {
match time::timeout(TEARDOWN_TIMEOUT, {
// NOTE: Ignore any errors
self.socket().await.teardown(self.id, rng)
})
Expand Down Expand Up @@ -148,10 +148,7 @@ impl CircuitHandler {
})
} else {
trace!("Incoming handshake failed post-handshake: unable to derive key");
let _ = time::timeout(Duration::from_secs(TIMEOUT_TEARDOWN), {
socket.teardown(circuit_id, &rng)
})
.await;
let _ = time::timeout(TEARDOWN_TIMEOUT, socket.teardown(circuit_id, &rng)).await;
Err(anyhow!(
"Incoming handshake failed post-handshake: unable to derive key"
))
Expand All @@ -174,7 +171,7 @@ impl CircuitHandler {

async fn respond_loop(&mut self) -> Result<()> {
loop {
let mut delay = time::delay_for(Duration::from_secs(TIMEOUT_IDLE));
let mut delay = time::delay_for(IDLE_TIMEOUT);

match &mut self.state {
State::Default => {
Expand Down
20 changes: 6 additions & 14 deletions src/onion/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use tokio::net::TcpStream;
use tokio::prelude::*;
use tokio::time::{timeout, Duration, Elapsed};

/// timeout apllied during a read on the socket
const TIMEOUT_READ: u64 = 5;
/// timeout apllied during a write on the socket
const TIMEOUT_WRITE: u64 = 2;
/// timeout applied during a read on the socket
const READ_TIMEOUT: Duration = Duration::from_secs(5);
/// timeout applied during a write on the socket
const WRITE_TIMEOUT: Duration = Duration::from_secs(2);

#[derive(Error, Debug)]
pub(crate) enum OnionSocketError {
Expand Down Expand Up @@ -90,11 +90,7 @@ impl<S> OnionSocket<S> {

impl<S: AsyncRead + Unpin> OnionSocket<S> {
async fn read_buf_from_stream(&mut self) -> SocketResult<usize> {
Ok(timeout(
Duration::from_secs(TIMEOUT_READ),
self.stream.read_exact(&mut self.buf),
)
.await??)
Ok(timeout(READ_TIMEOUT, self.stream.read_exact(&mut self.buf)).await??)
}

/// Listends for incoming `CIRCUIT CREATE` messages and returns the circuit id and key in this
Expand Down Expand Up @@ -137,11 +133,7 @@ impl<S: AsyncRead + Unpin> OnionSocket<S> {

impl<S: AsyncWrite + Unpin> OnionSocket<S> {
async fn write_buf_to_stream(&mut self) -> SocketResult<()> {
Ok(timeout(
Duration::from_secs(TIMEOUT_WRITE),
self.stream.write_all(self.buf.as_ref()),
)
.await??)
Ok(timeout(WRITE_TIMEOUT, self.stream.write_all(self.buf.as_ref())).await??)
}

async fn encrypt_and_send_opaque<K: ToBytes>(
Expand Down
7 changes: 2 additions & 5 deletions src/onion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use tokio::stream;

const TEST_IP: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
static PORT_COUNTER: AtomicU16 = AtomicU16::new(42000);
const TIME_ERROR_TIMEOUT: u64 = 4;
const ERROR_TIMEOUT: Duration = Duration::from_secs(TIME_ERROR_TIMEOUT);
const ERROR_TIMEOUT: Duration = Duration::from_secs(4);

pub(crate) fn read_rsa_keypair<P: AsRef<Path>>(path: P) -> Result<(RsaPrivateKey, RsaPublicKey)> {
let private_key = RsaPrivateKey::from_pem_file(path)?;
Expand Down Expand Up @@ -269,9 +268,7 @@ async fn test_timeout() -> Result<()> {
Err(_) => panic!("Expected ready event, got timeout"),
}

let mut delay = time::delay_for(Duration::from_secs(
circuit::TIMEOUT_IDLE + TIME_ERROR_TIMEOUT,
));
let mut delay = time::delay_for(circuit::IDLE_TIMEOUT + ERROR_TIMEOUT);
tokio::select! {
_ = handler_task => Ok(()),
_ = &mut delay => {
Expand Down

0 comments on commit 34d8ed5

Please sign in to comment.