From a80e64014c21e092e35080baf29b2611d18c486a Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Tue, 27 Aug 2019 11:28:20 +0200 Subject: Unbox callback closures Accepted the more verbose type signatures and added a callback to request new key-material. --- src/main.rs | 5 +++-- src/router/device.rs | 60 +++++++++++++++++++++++++-------------------------- src/router/peer.rs | 38 +++++++++++++++++--------------- src/router/types.rs | 16 ++++++++++++-- src/router/workers.rs | 18 ++++++++-------- 5 files changed, 77 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ae90251..5c58b24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,9 @@ fn main() { let router = router::Device::new( 4, - |t: &PeerTimer, data: bool| t.a.reset(Duration::from_millis(1000)), - |t: &PeerTimer, data: bool| t.b.reset(Duration::from_millis(1000)), + |t: &PeerTimer, data: bool, sent: bool| t.a.reset(Duration::from_millis(1000)), + |t: &PeerTimer, data: bool, sent: bool| t.b.reset(Duration::from_millis(1000)), + |t: &PeerTimer| println!("new key requested"), ); let pt = PeerTimer { diff --git a/src/router/device.rs b/src/router/device.rs index d1a7d93..3670cb5 100644 --- a/src/router/device.rs +++ b/src/router/device.rs @@ -14,20 +14,23 @@ use super::anti_replay::AntiReplay; use super::peer; use super::peer::{Peer, PeerInner}; -use super::types::{Callback, Opaque}; - -pub struct DeviceInner { - // callbacks (used for timers) - pub event_recv: Box>, // authenticated message received - pub event_send: Box>, // authenticated message send - pub event_new_handshake: (), // called when a new handshake is required +use super::types::{Callback, KeyCallback, Opaque}; +pub struct DeviceInner, R: Callback, K: KeyCallback> { + // threading and workers pub stopped: AtomicBool, pub injector: Injector<()>, // parallel enc/dec task injector pub threads: Vec>, // join handles of worker threads - pub recv: spin::RwLock>>, // receiver id -> decryption state - pub ipv4: spin::RwLock>>>, // ipv4 cryptkey routing - pub ipv6: spin::RwLock>>>, // ipv6 cryptkey routing + + // unboxed callbacks (used for timers and handshake requests) + pub event_send: S, // called when authenticated message send + pub event_recv: R, // called when authenticated message received + pub event_need_key: K, // called when new key material is required + + // routing + pub recv: spin::RwLock>>, // receiver id -> decryption state + pub ipv4: spin::RwLock>>>, // ipv4 cryptkey routing + pub ipv6: spin::RwLock>>>, // ipv6 cryptkey routing } pub struct EncryptionState { @@ -38,17 +41,19 @@ pub struct EncryptionState { // (birth + reject-after-time - keepalive-timeout - rekey-timeout) } -pub struct DecryptionState { +pub struct DecryptionState, R: Callback, K: KeyCallback> { pub key: [u8; 32], pub keypair: Weak, pub protector: spin::Mutex, - pub peer: Weak>, + pub peer: Weak>, pub death: Instant, // time when the key can no longer be used for decryption } -pub struct Device(Arc>); +pub struct Device, R: Callback, K: KeyCallback>( + Arc>, +); -impl Drop for Device { +impl, R: Callback, K: KeyCallback> Drop for Device { fn drop(&mut self) { // mark device as stopped let device = &self.0; @@ -64,16 +69,17 @@ impl Drop for Device { } } -impl Device { - pub fn new, F2: Callback>( +impl, R: Callback, K: KeyCallback> Device { + pub fn new( workers: usize, - event_recv: F1, - event_send: F2, - ) -> Device { + event_recv: R, + event_send: S, + event_need_key: K, + ) -> Device { Device(Arc::new(DeviceInner { - event_recv: Box::new(event_recv), - event_send: Box::new(event_send), - event_new_handshake: (), + event_recv, + event_send, + event_need_key, threads: vec![], stopped: AtomicBool::new(false), injector: Injector::new(), @@ -88,7 +94,7 @@ impl Device { /// # Returns /// /// A atomic ref. counted peer (with liftime matching the device) - pub fn new_peer(&self, opaque: T) -> Peer { + pub fn new_peer(&self, opaque: T) -> Peer { peer::new_peer(self.0.clone(), opaque) } @@ -98,13 +104,7 @@ impl Device { /// /// - pt_msg: IP packet to cryptkey route /// - /// # Returns - /// - /// A peer reference for the peer if no key-pair is currently valid for the destination. - /// This indicates that a handshake should be initated (see the handshake module). - /// If this occurs the packet is copied to an internal buffer - /// and retransmission can be attempted using send_run_queue - pub fn send(&self, pt_msg: &mut [u8]) -> Arc> { + pub fn send(&self, pt_msg: &mut [u8]) { unimplemented!(); } diff --git a/src/router/peer.rs b/src/router/peer.rs index 71f387a..0598b3a 100644 --- a/src/router/peer.rs +++ b/src/router/peer.rs @@ -20,7 +20,7 @@ use super::device::DeviceInner; use super::device::EncryptionState; use super::workers::{worker_inbound, worker_outbound, JobInbound, JobOutbound}; -use super::types::Opaque; +use super::types::{Opaque, Callback, KeyCallback}; const MAX_STAGED_PACKETS: usize = 128; @@ -31,14 +31,14 @@ pub struct KeyWheel { retired: Option, // retired id (previous id, after confirming key-pair) } -pub struct PeerInner { +pub struct PeerInner, R: Callback, K: KeyCallback> { pub stopped: AtomicBool, pub opaque: T, - pub device: Arc>, + pub device: Arc>, pub thread_outbound: spin::Mutex>>, pub thread_inbound: spin::Mutex>>, pub queue_outbound: SyncSender, - pub queue_inbound: SyncSender>, + pub queue_inbound: SyncSender>, pub staged_packets: spin::Mutex; MAX_STAGED_PACKETS], Wrapping>>, // packets awaiting handshake pub rx_bytes: AtomicU64, // received bytes pub tx_bytes: AtomicU64, // transmitted bytes @@ -47,13 +47,13 @@ pub struct PeerInner { pub endpoint: spin::Mutex>>, } -pub struct Peer(Arc>); +pub struct Peer, R: Callback, K: KeyCallback>(Arc>); -fn treebit_list( - peer: &Arc>, - table: &spin::RwLock>>>, - callback: Box R>, -) -> Vec +fn treebit_list, R: Callback, K: KeyCallback>( + peer: &Arc>, + table: &spin::RwLock>>>, + callback: Box O>, +) -> Vec where A: Address, { @@ -69,9 +69,9 @@ where res } -fn treebit_remove( - peer: &Peer, - table: &spin::RwLock>>>, +fn treebit_remove, R: Callback, K: KeyCallback>( + peer: &Peer, + table: &spin::RwLock>>>, ) { let mut m = table.write(); @@ -93,7 +93,7 @@ fn treebit_remove( } } -impl Drop for Peer { +impl, R: Callback, K: KeyCallback> Drop for Peer { fn drop(&mut self) { // mark peer as stopped @@ -148,7 +148,11 @@ impl Drop for Peer { } } -pub fn new_peer(device: Arc>, opaque: T) -> Peer { +pub fn new_peer, R: Callback, K: KeyCallback>( + device: Arc>, + opaque: T +) -> Peer { + // allocate in-order queues let (send_inbound, recv_inbound) = sync_channel(MAX_STAGED_PACKETS); let (send_outbound, recv_outbound) = sync_channel(MAX_STAGED_PACKETS); @@ -199,8 +203,8 @@ pub fn new_peer(device: Arc>, opaque: T) -> Peer { Peer(peer) } -impl Peer { - fn new(inner: PeerInner) -> Peer { +impl, R: Callback, K: KeyCallback> Peer { + fn new(inner: PeerInner) -> Peer { Peer(Arc::new(inner)) } diff --git a/src/router/types.rs b/src/router/types.rs index 0b5e162..2ed011b 100644 --- a/src/router/types.rs +++ b/src/router/types.rs @@ -2,6 +2,18 @@ pub trait Opaque: Send + Sync + 'static {} impl Opaque for T where T: Send + Sync + 'static {} -pub trait Callback: Fn(&T, bool) -> () + Sync + Send + 'static {} +/// A send/recv callback takes 3 arguments: +/// +/// * `0`, a reference to the opaque value assigned to the peer +/// * `1`, a bool indicating whether the message contained data (not just keepalive) +/// * `2`, a bool indicating whether the message was transmitted (i.e. did the peer have an associated endpoint?) +pub trait Callback: Fn(&T, bool, bool) -> () + Sync + Send + 'static {} -impl Callback for F where F: Fn(&T, bool) -> () + Sync + Send + 'static {} +impl Callback for F where F: Fn(&T, bool, bool) -> () + Sync + Send + 'static {} + +/// A key callback takes 1 argument +/// +/// * `0`, a reference to the opaque value assigned to the peer +pub trait KeyCallback: Fn(&T) -> () + Sync + Send + 'static {} + +impl KeyCallback for F where F: Fn(&T) -> () + Sync + Send + 'static {} \ No newline at end of file diff --git a/src/router/workers.rs b/src/router/workers.rs index 4942491..4f39fb2 100644 --- a/src/router/workers.rs +++ b/src/router/workers.rs @@ -10,7 +10,7 @@ use std::sync::mpsc::{sync_channel, Receiver, TryRecvError}; use std::sync::{Arc, Weak}; use std::thread; -use super::types::{Opaque, Callback}; +use super::types::{Opaque, Callback, KeyCallback}; #[derive(PartialEq)] enum Operation { @@ -34,7 +34,7 @@ pub struct JobInner { pub type JobBuffer = Arc>; pub type JobParallel = (Arc>, JobBuffer); -pub type JobInbound = (Weak>, JobBuffer); +pub type JobInbound = (Weak>, JobBuffer); pub type JobOutbound = JobBuffer; /* Strategy for workers acquiring a new job: @@ -82,10 +82,10 @@ fn wait_recv(stopped: &AtomicBool, recv: &Receiver) -> Result( - device: Arc>, // related device - peer: Arc>, // related peer - recv: Receiver>, // in order queue +pub fn worker_inbound, R: Callback, K: KeyCallback>( + device: Arc>, // related device + peer: Arc>, // related peer + recv: Receiver>, // in order queue ) { loop { match wait_recv(&peer.stopped, &recv) { @@ -110,9 +110,9 @@ pub fn worker_inbound( } } -pub fn worker_outbound( - device: Arc>, // related device - peer: Arc>, // related peer +pub fn worker_outbound, R: Callback, K: KeyCallback>( + device: Arc>, // related device + peer: Arc>, // related peer recv: Receiver, // in order queue ) { loop { -- cgit v1.2.3-59-g8ed1b