You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rsh/src/sock/mod.rs

94 lines
2.0 KiB

//! Socket handlers
use super::*;
use std::collections::HashSet;
use std::net::{
SocketAddr,
};
use tokio::io::{
AsyncWrite,
AsyncRead
};
use tokio::task::JoinHandle;
use tokio::sync::{
mpsc,
oneshot,
};
use futures::Future;
use bytes::Bytes;
use cancel::*;
pub mod enc;
pub mod pipe;
/// Details of a newly accepted raw socket peer.
///
/// This connected will have been "accepted", but not yet trusted
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RawSockPeerAccepted
{
/// Address of socket.
pub addr: SocketAddr,
/// Trust this peer from the start? This should almost always be false.
pub auto_trust: bool,
}
/// Details of a connected, set-up raw socket connection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawSockPeerTrusted
{
/// The socket's details
pub sock_details: RawSockPeerAccepted,
/// Capabilities for this peer
pub cap_allow: HashSet<cap::RawSockCapability>,
}
/// A raw, received message from a `RawPeer`.
#[derive(Debug)]
pub struct RawMessage{
message_bytes: Bytes,
}
/// A connected raw peer, created and handled by `handle_new_socket_with_shutdown()`.
#[derive(Debug)]
pub struct RawPeer{
info: RawSockPeerTrusted,
rx: mpsc::Receiver<RawMessage>,
}
/// Handles a **newly connected** raw socket.
///
/// This will handle setting up socket peer encryption and validation.
pub fn handle_new_socket_with_shutdown<R, W, C: cancel::CancelFuture + 'static + Send>(
sock_details: RawSockPeerAccepted,
set_peer: oneshot::Sender<RawPeer>,
tx: W, rx: R,
shutdown: C
) -> JoinHandle<eyre::Result<()>>
where R: AsyncRead + Unpin + Send + 'static,
W: AsyncWrite + Unpin + Send + 'static
{
tokio::spawn(async move {
match {
with_cancel!(async move {
// Create empty cap
let mut sock_details = RawSockPeerTrusted {
sock_details,
cap_allow: HashSet::new(),
};
// Set up encryption
//TODO: Find caps for this peer.
Ok(())
}, shutdown)
} {
Ok(v) => v,
Err(x) => Err(eyre::Report::from(x)),
}
})
}