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)]
10 ProtocolError(String),
11
12 #[error("COTP over TPKT Protocol Stack Error - {}", .0)]
14 ProtocolStackError(#[from] TpktError),
15
16 #[error("COTP IO Error: {:?}", .0)]
18 IoError(#[from] std::io::Error),
19
20 #[error("COTP Error: {}", .0)]
22 InternalError(String),
23}
24
25#[derive(PartialEq, Clone, Debug)]
27pub struct CotpProtocolInformation {
28 initiator_reference: u16,
29 responder_reference: u16,
30 calling_tsap_id: Option<Vec<u8>>,
31 called_tsap_id: Option<Vec<u8>>,
32}
33
34impl CotpProtocolInformation {
35 pub(crate) fn new(initiator_reference: u16, responder_reference: u16, calling_tsap_id: Option<Vec<u8>>, called_tsap_id: Option<Vec<u8>>) -> Self {
36 CotpProtocolInformation { initiator_reference, responder_reference, calling_tsap_id, called_tsap_id }
37 }
38
39 pub fn initiator(calling_tsap_id: Option<Vec<u8>>, called_tsap_id: Option<Vec<u8>>) -> Self {
41 CotpProtocolInformation { initiator_reference: rand::random(), responder_reference: 0, calling_tsap_id, called_tsap_id }
42 }
43
44 pub fn responder(self) -> Self {
46 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() }
47 }
48
49 pub fn initiator_reference(&self) -> u16 {
50 self.initiator_reference
51 }
52
53 pub fn responder_reference(&self) -> u16 {
55 self.responder_reference
56 }
57
58 pub fn calling_tsap_id(&self) -> Option<&Vec<u8>> {
59 self.calling_tsap_id.as_ref()
60 }
61
62 pub fn called_tsap_id(&self) -> Option<&Vec<u8>> {
63 self.called_tsap_id.as_ref()
64 }
65}
66
67impl ProtocolInformation for CotpProtocolInformation {}
68
69pub trait CotpResponder: Send {
71 fn accept(self, options: CotpProtocolInformation) -> impl std::future::Future<Output = Result<impl CotpConnection, CotpError>> + Send;
73}
74
75pub trait CotpConnection: Send {
77 fn get_protocol_infomation_list(&self) -> &Vec<Box<dyn ProtocolInformation>>;
79
80 fn split(self) -> impl std::future::Future<Output = Result<(impl CotpReader, impl CotpWriter), CotpError>> + Send;
82}
83
84pub trait CotpReader: Send {
85 fn recv(&mut self) -> impl std::future::Future<Output = Result<Option<Vec<u8>>, CotpError>> + Send;
92}
93
94pub trait CotpWriter: Send {
95 fn send(&mut self, input: &mut VecDeque<Vec<u8>>) -> impl std::future::Future<Output = Result<(), CotpError>> + Send;
100}