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/// Two normal outcomes may occur when reading from a socket. It may return data, or it may be closed. This enum represents both possible state.
18pub enum TpktRecvResult {
19 /// The connection has closed normally.
20 Closed,
21 /// The following data was returned.
22 Data(Vec<u8>),
23}
24
25/// A trait representing a TPKT connection. There is no distinction between a client and a server connection once they are established.
26pub trait TpktConnection: Send {
27 /// Splits a connection into reader and writer components. This must be done before the connection is used.
28 fn split(self) -> impl std::future::Future<Output = Result<(impl TpktReader, impl TpktWriter), TpktError>> + Send;
29}
30
31/// A trait representing the read half of TPKT connection.
32pub trait TpktReader: Send {
33 /// Reads from a TPKT connection. There are three outcomes.
34 /// * TpktRecvResult::Data(_) - Data was read from the socket.
35 /// * TpktRecvResult::Closed - The underlying TCP connection was closed normally.
36 /// * TpktError - May indicate a TPKT packet was malformed, there was an IO error or some other internal failure occurred.
37 fn recv(&mut self) -> impl std::future::Future<Output = Result<TpktRecvResult, TpktError>> + Send;
38}
39
40/// A trait representing the write half of TPKT connection.
41pub trait TpktWriter: Send {
42 /// 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.
43 fn send(&mut self, input: &mut VecDeque<Vec<u8>>) -> impl std::future::Future<Output = Result<(), TpktError>> + Send;
44}