Skip to main content

rusty_cotp/
api.rs

1use std::collections::VecDeque;
2
3use rusty_tpkt::{ProtocolInformation, TpktError};
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum CotpError {
8    #[error("COTP Protocol Error - {}", .0)]
9    ProtocolError(String),
10
11    #[error("COTP over TPKT Protocol Stack Error - {}", .0)]
12    ProtocolStackError(#[from] TpktError),
13
14    #[error("COTP IO Error: {:?}", .0)]
15    IoError(#[from] std::io::Error),
16
17    #[error("COTP Error: {}", .0)]
18    InternalError(String),
19}
20
21#[derive(PartialEq, Clone, Debug)]
22pub struct CotpProtocolInformation {
23    initiator_reference: u16,
24    responder_reference: u16,
25    calling_tsap_id: Option<Vec<u8>>,
26    called_tsap_id: Option<Vec<u8>>,
27}
28
29impl CotpProtocolInformation {
30    pub(crate) fn new(initiator_reference: u16, responder_reference: u16, calling_tsap_id: Option<Vec<u8>>, called_tsap_id: Option<Vec<u8>>) -> Self {
31        CotpProtocolInformation { initiator_reference, responder_reference, calling_tsap_id, called_tsap_id }
32    }
33
34    pub fn initiator(calling_tsap_id: Option<Vec<u8>>, called_tsap_id: Option<Vec<u8>>) -> Self {
35        CotpProtocolInformation { initiator_reference: rand::random(), responder_reference: 0, calling_tsap_id, called_tsap_id }
36    }
37
38    pub fn responder(self) -> Self {
39        CotpProtocolInformation { initiator_reference: self.initiator_reference, responder_reference: rand::random(), calling_tsap_id: self.calling_tsap_id.clone(), called_tsap_id: self.calling_tsap_id.clone() }
40    }
41
42    pub fn initiator_reference(&self) -> u16 {
43        self.initiator_reference
44    }
45
46    /// This will be 0 for the first request from the initiator.
47    pub fn responder_reference(&self) -> u16 {
48        self.responder_reference
49    }
50
51    pub fn calling_tsap_id(&self) -> Option<&Vec<u8>> {
52        self.calling_tsap_id.as_ref()
53    }
54
55    pub fn called_tsap_id(&self) -> Option<&Vec<u8>> {
56        self.called_tsap_id.as_ref()
57    }
58}
59
60impl ProtocolInformation for CotpProtocolInformation {}
61
62pub enum CotpRecvResult {
63    Closed,
64    Data(Vec<u8>),
65}
66
67pub trait CotpResponder: Send {
68    fn accept(self, options: CotpProtocolInformation) -> impl std::future::Future<Output = Result<impl CotpConnection, CotpError>> + Send;
69}
70
71pub trait CotpConnection: Send {
72    fn get_protocol_infomation_list(&self) -> &Vec<Box<dyn ProtocolInformation>>;
73
74    fn split(self) -> impl std::future::Future<Output = Result<(impl CotpReader, impl CotpWriter), CotpError>> + Send;
75}
76
77pub trait CotpReader: Send {
78    /// Reads from a COTP connection. There are three outcomes.
79    /// * Some(data) - Data was read.
80    /// * None - The underlying connection was closed normally.
81    /// * TpktError - May indicate a packet was malformed, there was an IO error or some other internal failure occurred.
82    /// 
83    /// This operation is cancel safe.
84    fn recv(&mut self) -> impl std::future::Future<Output = Result<CotpRecvResult, CotpError>> + Send;
85}
86
87pub trait CotpWriter: Send {
88    /// Writes to a COTP 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.
89    /// 
90    /// This operation is cancel safe as long as the data in the input buffer is not dropped.
91    /// The Veque is intended to be used as a FIFO buffer stored on the caller and reused.
92    fn send(&mut self, input: &mut VecDeque<Vec<u8>>) -> impl std::future::Future<Output = Result<(), CotpError>> + Send;
93}