sctp-rs 0.3.1

Idiomatic Rust APIs for Linux SCTP Stack.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Types used by the Public APIs

/// SCTP Association ID Type
pub type AssociationId = i32;

/// Flags used by `sctp_bindx`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BindxFlags {
    /// Add the addresses passed (corresponding to `SCTP_BINDX_ADD_ADDR`)
    Add,

    /// Remove the addresses passed (corresponding to `SCTP_BINDX_REM_ADDR`)
    Remove,
}

/// SocketToAssociation: One-to-Many or One-to-One style Socket
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SocketToAssociation {
    /// One Association per Socket (TCP Style Socket.)
    OneToOne,

    /// Many Associations per Socket (UDP Style Socket.)
    OneToMany,
}

/// NotificationOrData: A type returned by a `sctp_recv` call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotificationOrData {
    /// SCTP Notification received by an `sctp_recv` call.
    Notification(Notification),

    /// SCTP Data Received by an `sctp_recv` call.
    Data(ReceivedData),
}

/// Structure Representing SCTP Received Data.
///
/// This structure is returned by the `sctp_recv` API call. This contains in addition to 'received'
/// data, any ancillary data that is received during the underlying system call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReceivedData {
    /// Received Message Payload.
    pub payload: Vec<u8>,

    /// Optional ancillary information about the received payload.
    pub rcv_info: Option<RcvInfo>,

    /// Optional ancillary information about the next call to `sctp_recv`.
    pub nxt_info: Option<NxtInfo>,
}

/// Structure Represnting Data to be Sent.
///
/// This structure contains actual paylod and optional ancillary data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SendData {
    /// Received Message Payload.
    pub payload: Vec<u8>,

    /// Optional ancillary information used to send the data.
    pub snd_info: Option<SendInfo>,
}

/// Structure representing Ancilliary Send Information (See Section 5.3.4 of RFC 6458)
#[repr(C)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SendInfo {
    /// Stream ID of the stream to send the data on.
    pub sid: u16,

    /// Flags to be used while sending the data.
    pub flags: u16,

    /// Application Protocol ID to be used while sending the data.
    pub ppid: u32,

    /// Opaque context to be used while sending the data.
    pub context: u32,

    /// Association ID of the SCTP Association to be used while sending the data.
    pub assoc_id: AssociationId,
}

/// Structure Representing Ancillary Receive Information (See Section 5.3.5 of RFC 6458)
#[repr(C)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RcvInfo {
    /// Stream ID on which the data is received.
    pub sid: u16,

    /// Stream Sequence Number received in the data.
    pub ssn: u16,

    /// Flags for the received data.
    pub flags: u16,

    /// Application Protocol ID used by the sender while sending the data.
    pub ppid: u32,

    /// Transaction sequence number.
    pub tsn: u32,

    /// Cumulative sequence number.
    pub cumtsn: u32,

    /// Opaque context.
    pub context: u32,

    /// SCTP Association ID.
    pub assoc_id: AssociationId,
}

/// Structure representing Ancillary next information (See Section 5.3.5)
#[repr(C)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct NxtInfo {
    /// Stream ID for the next received data.
    pub sid: u16,

    /// Flags for the next received data.
    pub flags: u16,

    /// Application protocol ID.
    pub ppid: u32,

    /// Length of the message to be used in the next `sctp_recv` call.
    pub length: u32,

    /// SCTP Association ID.
    pub assoc_id: AssociationId,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// An `enum` representing the notifications received on the SCTP Sockets.
pub enum Notification {
    /// Association Change Notification. See Section 6.1.1 of RFC 6458.
    AssociationChange(AssociationChange),

    /// Shutdown Notification. See Section 6.1.5 of RFC 6458.
    Shutdown(Shutdown),
    /// A Catchall Notification type for the Notifications that are not supported
    Unsupported,
}

/// AssociationChange: Structure returned as notification for Association Change.
///
/// To subscribe to this notification type, An application should call `sctp_subscribe_event` using
/// the [`Event`] type as [`Event::Association`].
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssociationChange {
    /// Type of the Notification always `SCTP_ASSOC_CHAGE`
    pub ev_type: Event,

    /// Notification Flags. Unused currently.
    pub flags: u16,

    /// Length of the notification data.
    pub length: u32,

    /// Association Change state. See also [`AssocChangeState`].
    pub state: AssocChangeState,

    /// Error when state is an error state and error information is available.
    pub error: u16,

    /// Maximum number of outbound streams.
    pub ob_streams: u16,

    /// Maximum number of inbound streams.
    pub ib_streams: u16,

    /// Association ID for the event.
    pub assoc_id: AssociationId,

    /// Additional data for the event.
    pub info: Vec<u8>,
}

/// Shutdown: Structure rreturned as notification for Shutdown Event.
///
///To subscribe to this notification type, An application should call `sctp_subscribe_event` using
///the [`Event`] ty[e as [`Event::Shutdown`]
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Shutdown {
    /// Type of the Notification always `SCTP_SHUTDOWN`
    pub ev_type: Event,

    /// Notification Flags. Unused currently.
    pub flags: u16,

    /// Length of the notification data.
    pub length: u32,

    /// Association ID for the event.
    pub assoc_id: AssociationId,
}

/// Event: Used for Subscribing for SCTP Events
///
/// See [`sctp_subscribe_events`][`crate::Listener::sctp_subscribe_event`] for the usage.
#[repr(u16)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
    /// Event to receive ancillary information with every `sctp_recv`.
    DataIo = (1 << 15),

    /// Event related to association change.
    Association,

    /// Event related to peer address change.
    Address,

    /// Event related to send failure.
    SendFailure,

    /// Event related to error received from the peer.
    PeerError,

    /// Event related to indicate peer shutdown.
    Shutdown,

    /// Event related to indicate partial delivery.
    PartialDelivery,

    /// Event related to indicate peer's partial indication.
    AdaptationLayer,

    /// Authentication event.
    Authentication,

    /// Event related to sender having no outstanding user data.
    SenderDry,

    /// Event related to stream reset.
    StreamReset,

    /// Event related to association reset.
    AssociationReset,

    /// Event related to stream change.
    StreamChange,

    /// Send Failure Event indication. (The actual received information is different from the one
    /// received in the `SendFailed` event.)
    SendFailureEvent,

    /// Unknown Event: Used only when unknwon value is received as a `Notification`.
    Unknown,
}

impl Event {
    pub(crate) fn from_u16(val: u16) -> Self {
        match val {
            0x8000 => Event::DataIo,
            0x8001 => Event::Association,
            0x8002 => Event::Address,
            0x8003 => Event::SendFailure,
            0x8004 => Event::PeerError,
            0x8005 => Event::Shutdown,
            0x8006 => Event::PartialDelivery,
            0x8007 => Event::AdaptationLayer,
            0x8008 => Event::Authentication,
            0x8009 => Event::SenderDry,
            0x800A => Event::StreamReset,
            0x800B => Event::AssociationReset,
            0x800C => Event::StreamChange,
            0x800D => Event::SendFailureEvent,
            _ => Event::Unknown,
        }
    }
}

/// SubscribeEventAssocId: AssociationID Used for Event Subscription
///
/// Note: repr should be same as `AssociationId` (ie. `i32`)
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SubscribeEventAssocId {
    /// Subscribe to Future Association IDs
    Future,

    /// Subscribe to Current Association IDs
    Current,

    /// Subscribe to ALL Association IDs
    All,

    /// Subscribe to Association ID with a given value.
    Value(AssociationId),
}

impl From<SubscribeEventAssocId> for AssociationId {
    fn from(value: SubscribeEventAssocId) -> Self {
        match value {
            SubscribeEventAssocId::Future => 0 as Self,
            SubscribeEventAssocId::Current => 1 as Self,
            SubscribeEventAssocId::All => 2 as Self,
            SubscribeEventAssocId::Value(v) => v,
        }
    }
}

/// Association Change States
#[repr(u16)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AssocChangeState {
    /// SCTP communication up.
    CommUp = 0,

    /// SCTP communication lost.
    CommLost,

    /// SCTP communication restarted.
    Restart,

    /// Shutdown complete.
    ShutdownComplete,

    /// Cannot start association.
    CannotStartAssoc,

    /// Unknown State: This value indicates an error
    Unknown,
}

impl AssocChangeState {
    pub(crate) fn from_u16(val: u16) -> Self {
        match val {
            0 => AssocChangeState::CommUp,
            1 => AssocChangeState::CommLost,
            2 => AssocChangeState::Restart,
            3 => AssocChangeState::ShutdownComplete,
            4 => AssocChangeState::CannotStartAssoc,
            _ => AssocChangeState::Unknown,
        }
    }
}

/// Constants related to `enum sctp_cmsg_type`
#[repr(i32)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CmsgType {
    Init = 0,
    SndRcv,
    SndInfo,
    RcvInfo,
    NxtInfo,
    PrInfo,
    AuthInfo,
    DstAddrV4,
    DstAddrV6,
}

/// Constants related to `enum sctp_sstat_state`
#[repr(i32)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum ConnState {
    #[default]
    Empty = 0,
    Closed,
    CookieWait,
    CookieEchoed,
    Established,
    ShutdownPending,
    ShutdownSent,
    ShutdownReceived,
    ShutdownAckSent,

    Unknown, // Should never be seen.
}

impl ConnState {
    fn from_i32(val: i32) -> Self {
        match val {
            0 => Self::Empty,
            1 => Self::Closed,
            2 => Self::CookieWait,
            3 => Self::CookieEchoed,
            4 => Self::Established,
            5 => Self::ShutdownPending,
            6 => Self::ShutdownSent,
            7 => Self::ShutdownReceived,
            8 => Self::ShutdownAckSent,
            _ => Self::Unknown,
        }
    }
}

/// PeerAddress: Structure representing SCTP Peer Address.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PeerAddress {
    pub assoc_id: AssociationId,
    pub address: std::net::SocketAddr,
    pub state: i32,
    pub cwnd: u32,
    pub srtt: u32,
    pub rto: u32,
    pub mtu: u32,
}

/// ConnStatus: Status of an SCTP Connection
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnStatus {
    pub assoc_id: AssociationId,
    pub state: ConnState,
    pub rwnd: u32,
    pub unacked_data: u16,
    pub pending_data: u16,
    pub instreams: u16,
    pub outstreams: u16,
    pub fragmentation_pt: u32,
    pub peer_primary: PeerAddress,
}

pub(crate) mod internal;