sctp_rs/
types.rs

1//! Types used by the Public APIs
2
3/// SCTP Association ID Type
4pub type AssociationId = i32;
5
6/// Flags used by `sctp_bindx`.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum BindxFlags {
9    /// Add the addresses passed (corresponding to `SCTP_BINDX_ADD_ADDR`)
10    Add,
11
12    /// Remove the addresses passed (corresponding to `SCTP_BINDX_REM_ADDR`)
13    Remove,
14}
15
16/// SocketToAssociation: One-to-Many or One-to-One style Socket
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum SocketToAssociation {
19    /// One Association per Socket (TCP Style Socket.)
20    OneToOne,
21
22    /// Many Associations per Socket (UDP Style Socket.)
23    OneToMany,
24}
25
26/// NotificationOrData: A type returned by a `sctp_recv` call.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum NotificationOrData {
29    /// SCTP Notification received by an `sctp_recv` call.
30    Notification(Notification),
31
32    /// SCTP Data Received by an `sctp_recv` call.
33    Data(ReceivedData),
34}
35
36/// Structure Representing SCTP Received Data.
37///
38/// This structure is returned by the `sctp_recv` API call. This contains in addition to 'received'
39/// data, any ancillary data that is received during the underlying system call.
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct ReceivedData {
42    /// Received Message Payload.
43    pub payload: Vec<u8>,
44
45    /// Optional ancillary information about the received payload.
46    pub rcv_info: Option<RcvInfo>,
47
48    /// Optional ancillary information about the next call to `sctp_recv`.
49    pub nxt_info: Option<NxtInfo>,
50}
51
52/// Structure Represnting Data to be Sent.
53///
54/// This structure contains actual paylod and optional ancillary data.
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct SendData {
57    /// Received Message Payload.
58    pub payload: Vec<u8>,
59
60    /// Optional ancillary information used to send the data.
61    pub snd_info: Option<SendInfo>,
62}
63
64/// Structure representing Ancilliary Send Information (See Section 5.3.4 of RFC 6458)
65#[repr(C)]
66#[derive(Debug, Default, Clone, PartialEq, Eq)]
67pub struct SendInfo {
68    /// Stream ID of the stream to send the data on.
69    pub sid: u16,
70
71    /// Flags to be used while sending the data.
72    pub flags: u16,
73
74    /// Application Protocol ID to be used while sending the data.
75    pub ppid: u32,
76
77    /// Opaque context to be used while sending the data.
78    pub context: u32,
79
80    /// Association ID of the SCTP Association to be used while sending the data.
81    pub assoc_id: AssociationId,
82}
83
84/// Structure Representing Ancillary Receive Information (See Section 5.3.5 of RFC 6458)
85#[repr(C)]
86#[derive(Debug, Default, Clone, PartialEq, Eq)]
87pub struct RcvInfo {
88    /// Stream ID on which the data is received.
89    pub sid: u16,
90
91    /// Stream Sequence Number received in the data.
92    pub ssn: u16,
93
94    /// Flags for the received data.
95    pub flags: u16,
96
97    /// Application Protocol ID used by the sender while sending the data.
98    pub ppid: u32,
99
100    /// Transaction sequence number.
101    pub tsn: u32,
102
103    /// Cumulative sequence number.
104    pub cumtsn: u32,
105
106    /// Opaque context.
107    pub context: u32,
108
109    /// SCTP Association ID.
110    pub assoc_id: AssociationId,
111}
112
113/// Structure representing Ancillary next information (See Section 5.3.5)
114#[repr(C)]
115#[derive(Debug, Default, Clone, PartialEq, Eq)]
116pub struct NxtInfo {
117    /// Stream ID for the next received data.
118    pub sid: u16,
119
120    /// Flags for the next received data.
121    pub flags: u16,
122
123    /// Application protocol ID.
124    pub ppid: u32,
125
126    /// Length of the message to be used in the next `sctp_recv` call.
127    pub length: u32,
128
129    /// SCTP Association ID.
130    pub assoc_id: AssociationId,
131}
132
133#[derive(Debug, Clone, PartialEq, Eq)]
134/// An `enum` representing the notifications received on the SCTP Sockets.
135pub enum Notification {
136    /// Association Change Notification. See Section 6.1.1 of RFC 6458.
137    AssociationChange(AssociationChange),
138
139    /// Shutdown Notification. See Section 6.1.5 of RFC 6458.
140    Shutdown(Shutdown),
141    /// A Catchall Notification type for the Notifications that are not supported
142    Unsupported,
143}
144
145/// AssociationChange: Structure returned as notification for Association Change.
146///
147/// To subscribe to this notification type, An application should call `sctp_subscribe_event` using
148/// the [`Event`] type as [`Event::Association`].
149#[repr(C)]
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct AssociationChange {
152    /// Type of the Notification always `SCTP_ASSOC_CHAGE`
153    pub ev_type: Event,
154
155    /// Notification Flags. Unused currently.
156    pub flags: u16,
157
158    /// Length of the notification data.
159    pub length: u32,
160
161    /// Association Change state. See also [`AssocChangeState`].
162    pub state: AssocChangeState,
163
164    /// Error when state is an error state and error information is available.
165    pub error: u16,
166
167    /// Maximum number of outbound streams.
168    pub ob_streams: u16,
169
170    /// Maximum number of inbound streams.
171    pub ib_streams: u16,
172
173    /// Association ID for the event.
174    pub assoc_id: AssociationId,
175
176    /// Additional data for the event.
177    pub info: Vec<u8>,
178}
179
180/// Shutdown: Structure rreturned as notification for Shutdown Event.
181///
182///To subscribe to this notification type, An application should call `sctp_subscribe_event` using
183///the [`Event`] ty[e as [`Event::Shutdown`]
184#[repr(C)]
185#[derive(Debug, Clone, PartialEq, Eq)]
186pub struct Shutdown {
187    /// Type of the Notification always `SCTP_SHUTDOWN`
188    pub ev_type: Event,
189
190    /// Notification Flags. Unused currently.
191    pub flags: u16,
192
193    /// Length of the notification data.
194    pub length: u32,
195
196    /// Association ID for the event.
197    pub assoc_id: AssociationId,
198}
199
200/// Event: Used for Subscribing for SCTP Events
201///
202/// See [`sctp_subscribe_events`][`crate::Listener::sctp_subscribe_event`] for the usage.
203#[repr(u16)]
204#[derive(Debug, Clone, PartialEq, Eq)]
205pub enum Event {
206    /// Event to receive ancillary information with every `sctp_recv`.
207    DataIo = (1 << 15),
208
209    /// Event related to association change.
210    Association,
211
212    /// Event related to peer address change.
213    Address,
214
215    /// Event related to send failure.
216    SendFailure,
217
218    /// Event related to error received from the peer.
219    PeerError,
220
221    /// Event related to indicate peer shutdown.
222    Shutdown,
223
224    /// Event related to indicate partial delivery.
225    PartialDelivery,
226
227    /// Event related to indicate peer's partial indication.
228    AdaptationLayer,
229
230    /// Authentication event.
231    Authentication,
232
233    /// Event related to sender having no outstanding user data.
234    SenderDry,
235
236    /// Event related to stream reset.
237    StreamReset,
238
239    /// Event related to association reset.
240    AssociationReset,
241
242    /// Event related to stream change.
243    StreamChange,
244
245    /// Send Failure Event indication. (The actual received information is different from the one
246    /// received in the `SendFailed` event.)
247    SendFailureEvent,
248
249    /// Unknown Event: Used only when unknwon value is received as a `Notification`.
250    Unknown,
251}
252
253impl Event {
254    pub(crate) fn from_u16(val: u16) -> Self {
255        match val {
256            0x8000 => Event::DataIo,
257            0x8001 => Event::Association,
258            0x8002 => Event::Address,
259            0x8003 => Event::SendFailure,
260            0x8004 => Event::PeerError,
261            0x8005 => Event::Shutdown,
262            0x8006 => Event::PartialDelivery,
263            0x8007 => Event::AdaptationLayer,
264            0x8008 => Event::Authentication,
265            0x8009 => Event::SenderDry,
266            0x800A => Event::StreamReset,
267            0x800B => Event::AssociationReset,
268            0x800C => Event::StreamChange,
269            0x800D => Event::SendFailureEvent,
270            _ => Event::Unknown,
271        }
272    }
273}
274
275/// SubscribeEventAssocId: AssociationID Used for Event Subscription
276///
277/// Note: repr should be same as `AssociationId` (ie. `i32`)
278#[repr(i32)]
279#[derive(Debug, Clone, Copy, PartialEq, Eq)]
280pub enum SubscribeEventAssocId {
281    /// Subscribe to Future Association IDs
282    Future,
283
284    /// Subscribe to Current Association IDs
285    Current,
286
287    /// Subscribe to ALL Association IDs
288    All,
289
290    /// Subscribe to Association ID with a given value.
291    Value(AssociationId),
292}
293
294impl From<SubscribeEventAssocId> for AssociationId {
295    fn from(value: SubscribeEventAssocId) -> Self {
296        match value {
297            SubscribeEventAssocId::Future => 0 as Self,
298            SubscribeEventAssocId::Current => 1 as Self,
299            SubscribeEventAssocId::All => 2 as Self,
300            SubscribeEventAssocId::Value(v) => v,
301        }
302    }
303}
304
305/// Association Change States
306#[repr(u16)]
307#[derive(Clone, Debug, PartialEq, Eq)]
308pub enum AssocChangeState {
309    /// SCTP communication up.
310    CommUp = 0,
311
312    /// SCTP communication lost.
313    CommLost,
314
315    /// SCTP communication restarted.
316    Restart,
317
318    /// Shutdown complete.
319    ShutdownComplete,
320
321    /// Cannot start association.
322    CannotStartAssoc,
323
324    /// Unknown State: This value indicates an error
325    Unknown,
326}
327
328impl AssocChangeState {
329    pub(crate) fn from_u16(val: u16) -> Self {
330        match val {
331            0 => AssocChangeState::CommUp,
332            1 => AssocChangeState::CommLost,
333            2 => AssocChangeState::Restart,
334            3 => AssocChangeState::ShutdownComplete,
335            4 => AssocChangeState::CannotStartAssoc,
336            _ => AssocChangeState::Unknown,
337        }
338    }
339}
340
341/// Constants related to `enum sctp_cmsg_type`
342#[repr(i32)]
343#[derive(Debug, Clone, PartialEq, Eq)]
344pub enum CmsgType {
345    Init = 0,
346    SndRcv,
347    SndInfo,
348    RcvInfo,
349    NxtInfo,
350    PrInfo,
351    AuthInfo,
352    DstAddrV4,
353    DstAddrV6,
354}
355
356/// Constants related to `enum sctp_sstat_state`
357#[repr(i32)]
358#[derive(Debug, Clone, Default, PartialEq, Eq)]
359pub enum ConnState {
360    #[default]
361    Empty = 0,
362    Closed,
363    CookieWait,
364    CookieEchoed,
365    Established,
366    ShutdownPending,
367    ShutdownSent,
368    ShutdownReceived,
369    ShutdownAckSent,
370
371    Unknown, // Should never be seen.
372}
373
374impl ConnState {
375    fn from_i32(val: i32) -> Self {
376        match val {
377            0 => Self::Empty,
378            1 => Self::Closed,
379            2 => Self::CookieWait,
380            3 => Self::CookieEchoed,
381            4 => Self::Established,
382            5 => Self::ShutdownPending,
383            6 => Self::ShutdownSent,
384            7 => Self::ShutdownReceived,
385            8 => Self::ShutdownAckSent,
386            _ => Self::Unknown,
387        }
388    }
389}
390
391/// PeerAddress: Structure representing SCTP Peer Address.
392#[derive(Debug, Clone, Copy, PartialEq, Eq)]
393pub struct PeerAddress {
394    pub assoc_id: AssociationId,
395    pub address: std::net::SocketAddr,
396    pub state: i32,
397    pub cwnd: u32,
398    pub srtt: u32,
399    pub rto: u32,
400    pub mtu: u32,
401}
402
403/// ConnStatus: Status of an SCTP Connection
404#[derive(Debug, Clone, PartialEq, Eq)]
405pub struct ConnStatus {
406    pub assoc_id: AssociationId,
407    pub state: ConnState,
408    pub rwnd: u32,
409    pub unacked_data: u16,
410    pub pending_data: u16,
411    pub instreams: u16,
412    pub outstreams: u16,
413    pub fragmentation_pt: u32,
414    pub peer_primary: PeerAddress,
415}
416
417pub(crate) mod internal;