Skip to content

Commit

Permalink
Test only: Count noise output instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
twittner committed Jan 20, 2020
1 parent c241014 commit fdd5bb5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
32 changes: 28 additions & 4 deletions protocols/noise/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use futures::ready;
use futures::prelude::*;
use log::{debug, trace};
use snow;
use std::{fmt, io, pin::Pin, ops::DerefMut, task::{Context, Poll}};
use std::{fmt, io, pin::Pin, ops::DerefMut, sync::atomic::AtomicUsize, task::{Context, Poll}};

static OUTPUT_INSTANCES: AtomicUsize = AtomicUsize::new(0);

const MAX_NOISE_PKG_LEN: usize = 65535;
const MAX_WRITE_BUF_LEN: usize = 16384;
Expand Down Expand Up @@ -57,6 +59,7 @@ impl Buffer {

/// A passthrough enum for the two kinds of state machines in `snow`
pub(crate) enum SnowState {
Empty,
Transport(snow::TransportState),
Handshake(snow::HandshakeState)
}
Expand All @@ -66,27 +69,31 @@ impl SnowState {
match self {
SnowState::Handshake(session) => session.read_message(message, payload),
SnowState::Transport(session) => session.read_message(message, payload),
SnowState::Empty => unreachable!()
}
}

pub fn write_message(&mut self, message: &[u8], payload: &mut [u8]) -> Result<usize, snow::Error> {
match self {
SnowState::Handshake(session) => session.write_message(message, payload),
SnowState::Transport(session) => session.write_message(message, payload),
SnowState::Empty => unreachable!()
}
}

pub fn get_remote_static(&self) -> Option<&[u8]> {
match self {
SnowState::Handshake(session) => session.get_remote_static(),
SnowState::Transport(session) => session.get_remote_static(),
SnowState::Empty => unreachable!()
}
}

pub fn into_transport_mode(self) -> Result<snow::TransportState, snow::Error> {
match self {
SnowState::Handshake(session) => session.into_transport_mode(),
SnowState::Transport(_) => Err(snow::Error::State(snow::error::StateProblem::HandshakeAlreadyFinished)),
SnowState::Empty => unreachable!()
}
}
}
Expand All @@ -113,6 +120,8 @@ impl<T> fmt::Debug for NoiseOutput<T> {

impl<T> NoiseOutput<T> {
fn new(io: T, session: SnowState) -> Self {
let n = OUTPUT_INSTANCES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
log::info!("NoiseOutput instances = {}", n + 1);
NoiseOutput {
io,
session,
Expand All @@ -121,6 +130,21 @@ impl<T> NoiseOutput<T> {
write_state: WriteState::Init
}
}

pub(crate) fn set_session(&mut self, s: SnowState) {
self.session = s
}

pub(crate) fn take_session(&mut self) -> SnowState {
std::mem::replace(&mut self.session, SnowState::Empty)
}
}

impl<T> Drop for NoiseOutput<T> {
fn drop(&mut self) {
let n = OUTPUT_INSTANCES.fetch_sub(1, std::sync::atomic::Ordering::Acquire);
log::info!("NoiseOutput instances = {}", n - 1);
}
}

/// The various states of reading a noise session transitions through.
Expand Down Expand Up @@ -164,7 +188,7 @@ impl<T: AsyncRead + Unpin> AsyncRead for NoiseOutput<T> {
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, std::io::Error>> {
let mut this = self.deref_mut();
let this = self.deref_mut();

let buffer = this.buffer.borrow_mut();

Expand Down Expand Up @@ -260,7 +284,7 @@ impl<T: AsyncWrite + Unpin> AsyncWrite for NoiseOutput<T> {
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>>{
let mut this = self.deref_mut();
let this = self.deref_mut();

let buffer = this.buffer.borrow_mut();

Expand Down Expand Up @@ -347,7 +371,7 @@ impl<T: AsyncWrite + Unpin> AsyncWrite for NoiseOutput<T> {
mut self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<(), std::io::Error>> {
let mut this = self.deref_mut();
let this = self.deref_mut();

let buffer = this.buffer.borrow_mut();

Expand Down
7 changes: 5 additions & 2 deletions protocols/noise/src/io/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ impl<T> State<T>
Ok(dh_pk) => Some(dh_pk)
}
};
match self.io.session.into_transport_mode() {

let mut io = self.io;
match io.take_session().into_transport_mode() {
Err(e) => Err(e.into()),
Ok(s) => {
let remote = match (self.id_remote_pubkey, dh_remote_pubkey) {
Expand All @@ -325,7 +327,8 @@ impl<T> State<T>
}
}
};
Ok((remote, NoiseOutput { session: SnowState::Transport(s), .. self.io }))
io.set_session(SnowState::Transport(s));
Ok((remote, io))
}
}
}
Expand Down

0 comments on commit fdd5bb5

Please sign in to comment.