Skip to main content

rusty_tpkt/
api.rs

1use std::{any::Any, collections::VecDeque, fmt::Debug};
2
3use dyn_clone::DynClone;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum TpktError {
8    /// Indicates issues with parsing of incoming packets or protocol violations with input user data.
9    #[error("TPKT Protocol Error - {}", .0)]
10    ProtocolError(String),
11
12    /// Indicates issues with the underlying TCP socket or hardware.
13    #[error("TPKT IO Error: {:?}", .0)]
14    IoError(#[from] std::io::Error),
15
16    /// Usually indicates a bug or an unhandled error condition.
17    #[error("TPKT Error: {}", .0)]
18    InternalError(String),
19}
20
21/// Information regarding the protocol stack. This is useful for authentication and logging.
22pub trait ProtocolInformation: Any + Send + Debug + DynClone {}
23
24dyn_clone::clone_trait_object!(ProtocolInformation);
25
26/// A trait representing a TPKT connection. There is no distinction between a client and a server connection once they are established.
27pub trait TpktConnection: Send {
28    fn get_protocol_infomation_list(&self) -> &Vec<Box<dyn ProtocolInformation>>;
29
30    /// Splits a connection into reader and writer components. This must be done before the connection is used.
31    fn split(self) -> impl std::future::Future<Output = Result<(impl TpktReader, impl TpktWriter), TpktError>> + Send;
32}
33
34/// A trait representing the read half of TPKT connection.
35pub trait TpktReader: Send {
36    /// Reads from a TPKT connection. There are three outcomes.
37    /// * Some(data) - Data was read.
38    /// * None - The underlying connection was closed normally.
39    /// * TpktError - May indicate a packet was malformed, there was an IO error or some other internal failure occurred.
40    /// 
41    /// This operation is cancel safe.
42    fn recv(&mut self) -> impl std::future::Future<Output = Result<Option<Vec<u8>>, TpktError>> + Send;
43}
44
45/// A trait representing the write half of TPKT connection.
46pub trait TpktWriter: Send {
47    /// Writes to a TPKT connection. This uses a VedDeque as a buffer. This is to ensure the operation is cancel safe so long as the buffer is not dropped while it has data.
48    /// 
49    /// This operation is cancel safe as long as the data in the input buffer is not dropped.
50    /// The Veque is intended to be used as a FIFO buffer stored on the caller and reused.
51    fn send(&mut self, input: &mut VecDeque<Vec<u8>>) -> impl std::future::Future<Output = Result<(), TpktError>> + Send;
52}