Skip to main content

rusty_cotp/
api.rs

1use std::collections::VecDeque;
2
3use rusty_tpkt::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 CotpConnectInformation {
23    pub initiator_reference: u16,
24    pub calling_tsap_id: Option<Vec<u8>>,
25    pub called_tsap_id: Option<Vec<u8>>,
26}
27
28impl Default for CotpConnectInformation {
29    fn default() -> Self {
30        Self { initiator_reference: rand::random(), calling_tsap_id: None, called_tsap_id: None }
31    }
32}
33
34#[derive(PartialEq, Debug)]
35pub struct CotpAcceptInformation {
36    pub responder_reference: u16,
37}
38
39impl Default for CotpAcceptInformation {
40    fn default() -> Self {
41        Self { responder_reference: rand::random() }
42    }
43}
44
45pub enum CotpRecvResult {
46    Closed,
47    Data(Vec<u8>),
48}
49
50pub trait CotpResponder: Send {
51    fn accept(self, options: CotpAcceptInformation) -> impl std::future::Future<Output = Result<impl CotpConnection, CotpError>> + Send;
52}
53
54pub trait CotpConnection: Send {
55    fn split(self) -> impl std::future::Future<Output = Result<(impl CotpReader, impl CotpWriter), CotpError>> + Send;
56}
57
58pub trait CotpReader: Send {
59    fn recv(&mut self) -> impl std::future::Future<Output = Result<CotpRecvResult, CotpError>> + Send;
60}
61
62pub trait CotpWriter: Send {
63    fn send(&mut self, input: &mut VecDeque<Vec<u8>>) -> impl std::future::Future<Output = Result<(), CotpError>> + Send;
64}