From 761c46064d7510303f08cde27c9e13b07293f3af Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Wed, 9 Oct 2019 15:08:26 +0200 Subject: Restructure IO traits. --- src/router/device.rs | 55 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 23 deletions(-) (limited to 'src/router/device.rs') diff --git a/src/router/device.rs b/src/router/device.rs index d126959..989c2c2 100644 --- a/src/router/device.rs +++ b/src/router/device.rs @@ -17,21 +17,23 @@ use super::constants::*; use super::ip::*; use super::messages::{TransportHeader, TYPE_TRANSPORT}; use super::peer::{new_peer, Peer, PeerInner}; -use super::types::{Callbacks, Opaque, RouterError}; +use super::types::{Callbacks, RouterError}; use super::workers::{worker_parallel, JobParallel, Operation}; use super::SIZE_MESSAGE_PREFIX; -use super::super::types::{Bind, KeyPair, Tun}; +use super::super::types::{KeyPair, Endpoint, bind, tun}; -pub struct DeviceInner { - // IO & timer callbacks - pub tun: T, - pub bind: B, +pub struct DeviceInner> { + // inbound writer (TUN) + pub inbound: T, + + // outbound writer (Bind) + pub outbound: RwLock>, // routing - pub recv: RwLock>>>, // receiver id -> decryption state - pub ipv4: RwLock>>>, // ipv4 cryptkey routing - pub ipv6: RwLock>>>, // ipv6 cryptkey routing + pub recv: RwLock>>>, // receiver id -> decryption state + pub ipv4: RwLock>>>, // ipv4 cryptkey routing + pub ipv6: RwLock>>>, // ipv6 cryptkey routing // work queues pub queue_next: AtomicUsize, // next round-robin index @@ -45,20 +47,20 @@ pub struct EncryptionState { pub death: Instant, // (birth + reject-after-time - keepalive-timeout - rekey-timeout) } -pub struct DecryptionState { +pub struct DecryptionState> { pub keypair: Arc, pub confirmed: AtomicBool, pub protector: Mutex, - pub peer: Arc>, + pub peer: Arc>, pub death: Instant, // time when the key can no longer be used for decryption } -pub struct Device { - state: Arc>, // reference to device state +pub struct Device> { + state: Arc>, // reference to device state handles: Vec>, // join handles for workers } -impl Drop for Device { +impl> Drop for Device { fn drop(&mut self) { debug!("router: dropping device"); @@ -83,10 +85,10 @@ impl Drop for Device { } #[inline(always)] -fn get_route( - device: &Arc>, +fn get_route>( + device: &Arc>, packet: &[u8], -) -> Option>> { +) -> Option>> { // ensure version access within bounds if packet.len() < 1 { return None; @@ -122,12 +124,12 @@ fn get_route( } } -impl Device { - pub fn new(num_workers: usize, tun: T, bind: B) -> Device { +impl> Device { + pub fn new(num_workers: usize, tun: T) -> Device { // allocate shared device state let mut inner = DeviceInner { - tun, - bind, + inbound: tun, + outbound: RwLock::new(None), queues: Mutex::new(Vec::with_capacity(num_workers)), queue_next: AtomicUsize::new(0), recv: RwLock::new(HashMap::new()), @@ -159,7 +161,7 @@ impl Device { /// # Returns /// /// A atomic ref. counted peer (with liftime matching the device) - pub fn new_peer(&self, opaque: C::Opaque) -> Peer { + pub fn new_peer(&self, opaque: C::Opaque) -> Peer { new_peer(self.state.clone(), opaque) } @@ -199,7 +201,7 @@ impl Device { /// # Returns /// /// - pub fn recv(&self, src: B::Endpoint, msg: Vec) -> Result<(), RouterError> { + pub fn recv(&self, src: E, msg: Vec) -> Result<(), RouterError> { // parse / cast let (header, _) = match LayoutVerified::new_from_prefix(&msg[..]) { Some(v) => v, @@ -231,4 +233,11 @@ impl Device { Ok(()) } + + /// Set outbound writer + /// + /// + pub fn set_outbound_writer(&self, new : B) { + *self.state.outbound.write() = Some(new); + } } -- cgit v1.2.3-59-g8ed1b