Skip to main content

rusty_tpkt/
api.rs

1use std::collections::VecDeque;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum TpktError {
7    #[error("TPKT Protocol Error - {}", .0)]
8    ProtocolError(String),
9
10    #[error("TPKT IO Error: {:?}", .0)]
11    IoError(#[from] std::io::Error),
12
13    #[error("TPKT Error: {}", .0)]
14    InternalError(String),
15}
16
17/// A trait representing a TPKT connection. There is no distinction between a client and a server connection once they are established.
18pub trait TpktConnection: Send {
19    /// Splits a connection into reader and writer components. This must be done before the connection is used.
20    fn split(self) -> impl std::future::Future<Output = Result<(impl TpktReader, impl TpktWriter), TpktError>> + Send;
21}
22
23/// A trait representing the read half of TPKT connection.
24pub trait TpktReader: Send {
25    /// Reads from a TPKT connection. There are three outcomes.
26    /// * Some(data) - Data was read from the socket.
27    /// * None - The underlying TCP connection was closed normally.
28    /// * TpktError - May indicate a TPKT packet was malformed, there was an IO error or some other internal failure occurred.
29    fn recv(&mut self) -> impl std::future::Future<Output = Result<Option<Vec<u8>>, TpktError>> + Send;
30}
31
32/// A trait representing the write half of TPKT connection.
33pub trait TpktWriter: Send {
34    /// 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.
35    fn send(&mut self, input: &mut VecDeque<Vec<u8>>) -> impl std::future::Future<Output = Result<(), TpktError>> + Send;
36}