2023-02-15 10:14:13 +00:00
|
|
|
//! Connection handling and send/receive queues
|
|
|
|
|
2023-02-15 14:50:54 +00:00
|
|
|
pub mod handshake;
|
2023-05-23 16:20:08 +00:00
|
|
|
pub mod packet;
|
2023-05-17 10:05:13 +00:00
|
|
|
pub mod socket;
|
2023-02-15 14:50:54 +00:00
|
|
|
|
2023-05-27 08:57:15 +00:00
|
|
|
use ::std::{rc::Rc, vec::Vec};
|
2023-02-15 14:50:54 +00:00
|
|
|
|
2023-05-17 10:05:13 +00:00
|
|
|
pub use crate::connection::{
|
|
|
|
handshake::Handshake,
|
|
|
|
packet::{ConnectionID as ID, Packet, PacketData},
|
|
|
|
};
|
2023-02-15 14:50:54 +00:00
|
|
|
|
2023-05-24 15:30:15 +00:00
|
|
|
use crate::{
|
|
|
|
enc::{
|
|
|
|
hkdf::HkdfSha3,
|
|
|
|
sym::{CipherKind, CipherRecv, CipherSend},
|
|
|
|
},
|
|
|
|
inner::ThreadTracker,
|
2023-02-26 09:44:21 +00:00
|
|
|
};
|
|
|
|
|
2023-05-22 13:05:17 +00:00
|
|
|
/// strong typedef for receiving connection id
|
2023-05-26 13:02:21 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2023-05-22 13:05:17 +00:00
|
|
|
pub struct IDRecv(pub ID);
|
|
|
|
/// strong typedef for sending connection id
|
2023-05-26 13:02:21 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2023-05-22 13:05:17 +00:00
|
|
|
pub struct IDSend(pub ID);
|
|
|
|
|
2023-02-15 14:50:54 +00:00
|
|
|
/// Version of the fenrir protocol in use
|
|
|
|
#[derive(::num_derive::FromPrimitive, Debug, Copy, Clone)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum ProtocolVersion {
|
|
|
|
/// First Fenrir Protocol Version
|
|
|
|
V0 = 0,
|
|
|
|
}
|
2023-02-25 14:36:14 +00:00
|
|
|
impl ProtocolVersion {
|
|
|
|
/// actual length of the protocol version field
|
|
|
|
pub const fn len() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
/// Serialize into raw bytes
|
|
|
|
pub fn serialize(&self, out: &mut u8) {
|
|
|
|
*out = *self as u8;
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 21:25:52 +00:00
|
|
|
|
|
|
|
/// A single connection and its data
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Connection {
|
2023-05-22 13:05:17 +00:00
|
|
|
/// Receiving Connection ID
|
|
|
|
pub id_recv: IDRecv,
|
|
|
|
/// Sending Connection ID
|
|
|
|
pub id_send: IDSend,
|
2023-02-25 21:25:52 +00:00
|
|
|
/// The main hkdf used for all secrets in this connection
|
2023-02-26 09:44:21 +00:00
|
|
|
pub hkdf: HkdfSha3,
|
|
|
|
/// Cipher for decrypting data
|
|
|
|
pub cipher_recv: CipherRecv,
|
|
|
|
/// Cipher for encrypting data
|
|
|
|
pub cipher_send: CipherSend,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Role: used to set the correct secrets
|
|
|
|
/// * Server: Connection is Incoming
|
|
|
|
/// * Client: Connection is Outgoing
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum Role {
|
|
|
|
/// Server: we receive the connection
|
|
|
|
Server = 0,
|
|
|
|
/// Client: we initate the connection
|
|
|
|
Client,
|
2023-02-25 21:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Connection {
|
2023-03-01 17:20:03 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
hkdf: HkdfSha3,
|
|
|
|
cipher: CipherKind,
|
|
|
|
role: Role,
|
|
|
|
rand: &::ring::rand::SystemRandom,
|
|
|
|
) -> Self {
|
2023-02-26 09:44:21 +00:00
|
|
|
let (secret_recv, secret_send) = match role {
|
|
|
|
Role::Server => {
|
|
|
|
(hkdf.get_secret(b"to_server"), hkdf.get_secret(b"to_client"))
|
|
|
|
}
|
|
|
|
Role::Client => {
|
|
|
|
(hkdf.get_secret(b"to_client"), hkdf.get_secret(b"to_server"))
|
|
|
|
}
|
|
|
|
};
|
2023-05-26 13:02:21 +00:00
|
|
|
let cipher_recv = CipherRecv::new(cipher, secret_recv);
|
|
|
|
let cipher_send = CipherSend::new(cipher, secret_send, rand);
|
2023-02-26 09:44:21 +00:00
|
|
|
|
|
|
|
Self {
|
2023-05-22 13:05:17 +00:00
|
|
|
id_recv: IDRecv(ID::Handshake),
|
|
|
|
id_send: IDSend(ID::Handshake),
|
2023-02-26 09:44:21 +00:00
|
|
|
hkdf,
|
|
|
|
cipher_recv,
|
|
|
|
cipher_send,
|
|
|
|
}
|
2023-02-25 21:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-17 10:05:13 +00:00
|
|
|
|
|
|
|
// PERF: Arc<RwLock<ConnList>> loks a bit too much, need to find
|
|
|
|
// faster ways to do this
|
|
|
|
pub(crate) struct ConnList {
|
2023-05-24 15:30:15 +00:00
|
|
|
thread_id: ThreadTracker,
|
|
|
|
connections: Vec<Option<Rc<Connection>>>,
|
2023-05-17 10:05:13 +00:00
|
|
|
/// Bitmap to track which connection ids are used or free
|
|
|
|
ids_used: Vec<::bitmaps::Bitmap<1024>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConnList {
|
2023-05-24 15:30:15 +00:00
|
|
|
pub(crate) fn new(thread_id: ThreadTracker) -> Self {
|
2023-05-27 08:57:15 +00:00
|
|
|
let bitmap_id = ::bitmaps::Bitmap::<1024>::new();
|
2023-05-26 13:02:21 +00:00
|
|
|
const INITIAL_CAP: usize = 128;
|
|
|
|
let mut ret = Self {
|
2023-05-24 15:30:15 +00:00
|
|
|
thread_id,
|
2023-05-26 13:02:21 +00:00
|
|
|
connections: Vec::with_capacity(INITIAL_CAP),
|
2023-05-17 10:05:13 +00:00
|
|
|
ids_used: vec![bitmap_id],
|
2023-05-26 13:02:21 +00:00
|
|
|
};
|
|
|
|
ret.connections.resize_with(INITIAL_CAP, || None);
|
|
|
|
ret
|
2023-05-17 10:05:13 +00:00
|
|
|
}
|
2023-05-27 08:57:15 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
let mut total: usize = 0;
|
|
|
|
for bitmap in self.ids_used.iter() {
|
|
|
|
total = total + bitmap.len()
|
|
|
|
}
|
|
|
|
total
|
|
|
|
}
|
2023-05-26 13:02:21 +00:00
|
|
|
/// Only *Reserve* a connection,
|
|
|
|
/// without actually tracking it in self.connections
|
2023-05-17 10:05:13 +00:00
|
|
|
pub(crate) fn reserve_first(
|
|
|
|
&mut self,
|
|
|
|
mut conn: Connection,
|
2023-05-24 15:30:15 +00:00
|
|
|
) -> Rc<Connection> {
|
2023-05-17 10:05:13 +00:00
|
|
|
// uhm... bad things are going on here:
|
|
|
|
// * id must be initialized, but only because:
|
|
|
|
// * rust does not understand that after the `!found` id is always
|
|
|
|
// initialized
|
|
|
|
// * `ID::new_u64` is really safe only with >0, but here it always is
|
|
|
|
// ...we should probably rewrite it in better, safer rust
|
2023-05-26 13:02:21 +00:00
|
|
|
let mut id_in_thread: usize = 0;
|
2023-05-17 10:05:13 +00:00
|
|
|
let mut found = false;
|
|
|
|
for (i, b) in self.ids_used.iter_mut().enumerate() {
|
|
|
|
match b.first_false_index() {
|
|
|
|
Some(idx) => {
|
|
|
|
b.set(idx, true);
|
2023-05-26 13:02:21 +00:00
|
|
|
id_in_thread = (i * 1024) + idx;
|
2023-05-17 10:05:13 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
let mut new_bitmap = ::bitmaps::Bitmap::<1024>::new();
|
|
|
|
new_bitmap.set(0, true);
|
2023-05-26 13:02:21 +00:00
|
|
|
id_in_thread = self.ids_used.len() * 1024;
|
2023-05-17 10:05:13 +00:00
|
|
|
self.ids_used.push(new_bitmap);
|
|
|
|
}
|
2023-05-26 13:02:21 +00:00
|
|
|
// make sure we have enough space in self.connections
|
|
|
|
let curr_capacity = self.connections.capacity();
|
|
|
|
if self.connections.capacity() <= id_in_thread {
|
|
|
|
// Fill with "None", assure 64 connections without reallocations
|
|
|
|
let multiple = 64 + curr_capacity - 1;
|
|
|
|
let new_capacity = multiple - (multiple % curr_capacity);
|
|
|
|
self.connections.resize_with(new_capacity, || None);
|
|
|
|
}
|
|
|
|
// calculate the actual connection ID
|
|
|
|
let actual_id = ((id_in_thread as u64) * (self.thread_id.total as u64))
|
2023-05-24 15:30:15 +00:00
|
|
|
+ (self.thread_id.id as u64);
|
|
|
|
let new_id = IDRecv(ID::new_u64(actual_id));
|
2023-05-22 13:05:17 +00:00
|
|
|
conn.id_recv = new_id;
|
2023-05-26 13:02:21 +00:00
|
|
|
// Return the new connection without tracking it
|
|
|
|
Rc::new(conn)
|
|
|
|
}
|
|
|
|
/// NOTE: does NOT check if the connection has been previously reserved!
|
|
|
|
pub(crate) fn track(&mut self, conn: Rc<Connection>) -> Result<(), ()> {
|
|
|
|
let conn_id = match conn.id_recv {
|
|
|
|
IDRecv(ID::Handshake) => {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
IDRecv(ID::ID(conn_id)) => conn_id,
|
|
|
|
};
|
|
|
|
let id_in_thread: usize =
|
|
|
|
(conn_id.get() / (self.thread_id.total as u64)) as usize;
|
|
|
|
self.connections[id_in_thread] = Some(conn);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
pub(crate) fn delete(&mut self, id: IDRecv) {
|
|
|
|
if let IDRecv(ID::ID(raw_id)) = id {
|
|
|
|
let id_in_thread: usize =
|
|
|
|
(raw_id.get() / (self.thread_id.total as u64)) as usize;
|
|
|
|
let vec_index = id_in_thread / 1024;
|
|
|
|
let bitmask_index = id_in_thread % 1024;
|
|
|
|
if let Some(bitmask) = self.ids_used.get_mut(vec_index) {
|
|
|
|
bitmask.set(bitmask_index, false);
|
|
|
|
self.connections[id_in_thread] = None;
|
|
|
|
}
|
2023-05-17 10:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 13:26:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
use ::std::collections::HashMap;
|
|
|
|
|
|
|
|
pub(crate) struct AuthServerConnections {
|
|
|
|
conn_map : HashMap<
|
|
|
|
pub id: IDSend,
|
|
|
|
}
|
|
|
|
*/
|