Work on callback structure for cryptkey router

This commit is contained in:
Mathias Hall-Andersen
2019-08-26 15:01:47 +02:00
parent 5f3c4d2e27
commit e16151419d
6 changed files with 97 additions and 59 deletions

View File

@@ -1,32 +1,33 @@
use arraydeque::{ArrayDeque, Wrapping};
use treebitmap::address::Address;
use treebitmap::IpLookupTable;
use crossbeam_deque::{Injector, Steal};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{sync_channel, SyncSender};
use std::sync::{Arc, Mutex, Weak};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak};
use std::thread;
use std::time::Instant;
use crossbeam_deque::{Injector, Steal};
use spin;
use treebitmap::IpLookupTable;
use super::super::constants::*;
use super::super::types::KeyPair;
use super::anti_replay::AntiReplay;
use super::peer;
use super::peer::{Peer, PeerInner};
use super::workers::worker_parallel;
pub struct DeviceInner {
use super::types::{Callback, Opaque};
pub struct DeviceInner<T: Opaque> {
// callbacks (used for timers)
pub event_recv: Box<dyn Callback<T>>, // authenticated message received
pub event_send: Box<dyn Callback<T>>, // authenticated message send
pub event_new_handshake: (), // called when a new handshake is required
pub stopped: AtomicBool,
pub injector: Injector<()>, // parallel enc/dec task injector
pub threads: Vec<thread::JoinHandle<()>>, // join handles of worker threads
pub recv: spin::RwLock<HashMap<u32, DecryptionState>>, // receiver id -> decryption state
pub ipv4: spin::RwLock<IpLookupTable<Ipv4Addr, Weak<PeerInner>>>, // ipv4 cryptkey routing
pub ipv6: spin::RwLock<IpLookupTable<Ipv6Addr, Weak<PeerInner>>>, // ipv6 cryptkey routing
pub recv: spin::RwLock<HashMap<u32, DecryptionState<T>>>, // receiver id -> decryption state
pub ipv4: spin::RwLock<IpLookupTable<Ipv4Addr, Weak<PeerInner<T>>>>, // ipv4 cryptkey routing
pub ipv6: spin::RwLock<IpLookupTable<Ipv6Addr, Weak<PeerInner<T>>>>, // ipv6 cryptkey routing
}
pub struct EncryptionState {
@@ -37,17 +38,17 @@ pub struct EncryptionState {
// (birth + reject-after-time - keepalive-timeout - rekey-timeout)
}
pub struct DecryptionState {
pub struct DecryptionState<T: Opaque> {
pub key: [u8; 32],
pub keypair: Weak<KeyPair>,
pub protector: spin::Mutex<AntiReplay>,
pub peer: Weak<PeerInner>,
pub peer: Weak<PeerInner<T>>,
pub death: Instant, // time when the key can no longer be used for decryption
}
pub struct Device(Arc<DeviceInner>);
pub struct Device<T: Opaque>(Arc<DeviceInner<T>>);
impl Drop for Device {
impl<T: Opaque> Drop for Device<T> {
fn drop(&mut self) {
// mark device as stopped
let device = &self.0;
@@ -63,9 +64,16 @@ impl Drop for Device {
}
}
impl Device {
pub fn new(workers: usize) -> Device {
impl<T: Opaque> Device<T> {
pub fn new<F1: Callback<T>, F2: Callback<T>>(
workers: usize,
event_recv: F1,
event_send: F2,
) -> Device<T> {
Device(Arc::new(DeviceInner {
event_recv: Box::new(event_recv),
event_send: Box::new(event_send),
event_new_handshake: (),
threads: vec![],
stopped: AtomicBool::new(false),
injector: Injector::new(),
@@ -80,8 +88,8 @@ impl Device {
/// # Returns
///
/// A atomic ref. counted peer (with liftime matching the device)
pub fn new_peer(&self) -> Peer {
peer::new_peer(self.0.clone())
pub fn new_peer(&self, opaque: T) -> Peer<T> {
peer::new_peer(self.0.clone(), opaque)
}
/// Cryptkey routes and sends a plaintext message (IP packet)
@@ -96,7 +104,7 @@ impl Device {
/// 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<Peer> {
pub fn send(&self, pt_msg: &mut [u8]) -> Arc<Peer<T>> {
unimplemented!();
}