sctp_proto/
lib.rs

1//! Low-level protocol logic for the SCTP protocol
2//!
3//! sctp-proto contains a fully deterministic implementation of SCTP protocol logic. It contains
4//! no networking code and does not get any relevant timestamps from the operating system. Most
5//! users may want to use the futures-based sctp-async API instead.
6//!
7//! The sctp-proto API might be of interest if you want to use it from a C or C++ project
8//! through C bindings or if you want to use a different event loop than the one tokio provides.
9//!
10//! The most important types are `Endpoint`, which conceptually represents the protocol state for
11//! a single socket and mostly manages configuration and dispatches incoming datagrams to the
12//! related `Association`. `Association` types contain the bulk of the protocol logic related to
13//! managing a single association and all the related state (such as streams).
14
15#![warn(rust_2018_idioms)]
16#![allow(dead_code)]
17#![allow(clippy::bool_to_int_with_if)]
18
19use bytes::Bytes;
20use std::time::Instant;
21use std::{
22    fmt,
23    net::{IpAddr, SocketAddr},
24    ops,
25};
26
27mod association;
28pub use crate::association::{
29    stats::AssociationStats,
30    stream::{ReliabilityType, Stream, StreamEvent, StreamId, StreamState},
31    Association, AssociationError, Event,
32};
33
34pub(crate) mod chunk;
35pub use crate::chunk::{
36    chunk_payload_data::{ChunkPayloadData, PayloadProtocolIdentifier},
37    ErrorCauseCode,
38};
39
40mod config;
41pub use crate::config::{ClientConfig, EndpointConfig, ServerConfig, TransportConfig};
42
43mod endpoint;
44pub use crate::endpoint::{AssociationHandle, ConnectError, DatagramEvent, Endpoint};
45
46mod error;
47pub use crate::error::Error;
48
49mod packet;
50
51mod shared;
52pub use crate::shared::{AssociationEvent, AssociationId, EcnCodepoint, EndpointEvent};
53
54pub(crate) mod param;
55
56pub(crate) mod queue;
57pub use crate::queue::reassembly_queue::{Chunk, Chunks};
58
59pub(crate) mod util;
60
61/// Whether an endpoint was the initiator of an association
62#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
63pub enum Side {
64    /// The initiator of an association
65    #[default]
66    Client = 0,
67    /// The acceptor of an association
68    Server = 1,
69}
70
71impl fmt::Display for Side {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        let s = match *self {
74            Side::Client => "Client",
75            Side::Server => "Server",
76        };
77        write!(f, "{}", s)
78    }
79}
80
81impl Side {
82    #[inline]
83    /// Shorthand for `self == Side::Client`
84    pub fn is_client(self) -> bool {
85        self == Side::Client
86    }
87
88    #[inline]
89    /// Shorthand for `self == Side::Server`
90    pub fn is_server(self) -> bool {
91        self == Side::Server
92    }
93}
94
95impl ops::Not for Side {
96    type Output = Side;
97    fn not(self) -> Side {
98        match self {
99            Side::Client => Side::Server,
100            Side::Server => Side::Client,
101        }
102    }
103}
104
105use crate::packet::PartialDecode;
106
107/// Payload in Incoming/outgoing Transmit
108#[derive(Debug)]
109pub enum Payload {
110    PartialDecode(PartialDecode),
111    RawEncode(Vec<Bytes>),
112}
113
114/// Incoming/outgoing Transmit
115#[derive(Debug)]
116pub struct Transmit {
117    /// Received/Sent time
118    pub now: Instant,
119    /// The socket this datagram should be sent to
120    pub remote: SocketAddr,
121    /// Explicit congestion notification bits to set on the packet
122    pub ecn: Option<EcnCodepoint>,
123    /// Optional local IP address for the datagram
124    pub local_ip: Option<IpAddr>,
125    /// Payload of the datagram
126    pub payload: Payload,
127}
128
129#[cfg(test)]
130mod test {
131    use std::sync::Arc;
132
133    use super::*;
134
135    #[test]
136    fn ensure_send_sync() {
137        fn is_send_sync(_a: impl Send + Sync) {}
138
139        let c = EndpointConfig::new();
140        let e = Endpoint::new(Arc::new(c), None);
141        is_send_sync(e);
142
143        let a = Association::default();
144        is_send_sync(a);
145    }
146}