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
#[cfg(test)]
mod data_channel_test;
use crate::error::Result;
use crate::{
error::Error, message::message_channel_ack::*, message::message_channel_open::*, message::*,
};
use sctp::{
association::Association, chunk::chunk_payload_data::PayloadProtocolIdentifier, stream::*,
};
use util::marshal::*;
use bytes::{Buf, Bytes};
use derive_builder::Builder;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
const RECEIVE_MTU: usize = 8192;
#[derive(Eq, PartialEq, Default, Clone, Debug, Builder)]
pub struct Config {
#[builder(default)]
pub channel_type: ChannelType,
#[builder(default)]
pub negotiated: bool,
#[builder(default)]
pub priority: u16,
#[builder(default)]
pub reliability_parameter: u32,
#[builder(default)]
pub label: String,
#[builder(default)]
pub protocol: String,
}
#[derive(Debug, Default, Clone)]
pub struct DataChannel {
pub config: Config,
stream: Arc<Stream>,
messages_sent: Arc<AtomicUsize>,
messages_received: Arc<AtomicUsize>,
bytes_sent: Arc<AtomicUsize>,
bytes_received: Arc<AtomicUsize>,
}
impl DataChannel {
pub fn new(stream: Arc<Stream>, config: Config) -> Self {
Self {
config,
stream,
..Default::default()
}
}
pub async fn dial(
association: &Arc<Association>,
identifier: u16,
config: Config,
) -> Result<Self> {
let stream = association
.open_stream(identifier, PayloadProtocolIdentifier::Binary)
.await?;
Self::client(stream, config).await
}
pub async fn accept(association: &Arc<Association>, config: Config) -> Result<Self> {
let stream = association
.accept_stream()
.await
.ok_or(Error::ErrStreamClosed)?;
stream.set_default_payload_type(PayloadProtocolIdentifier::Binary);
Self::server(stream, config).await
}
pub async fn client(stream: Arc<Stream>, config: Config) -> Result<Self> {
if !config.negotiated {
let msg = Message::DataChannelOpen(DataChannelOpen {
channel_type: config.channel_type,
priority: config.priority,
reliability_parameter: config.reliability_parameter,
label: config.label.bytes().collect(),
protocol: config.protocol.bytes().collect(),
})
.marshal()?;
stream
.write_sctp(&msg, PayloadProtocolIdentifier::Dcep)
.await?;
}
Ok(DataChannel::new(stream, config))
}
pub async fn server(stream: Arc<Stream>, mut config: Config) -> Result<Self> {
let mut buf = vec![0u8; RECEIVE_MTU];
let (n, ppi) = stream.read_sctp(&mut buf).await?;
if ppi != PayloadProtocolIdentifier::Dcep {
return Err(Error::InvalidPayloadProtocolIdentifier(ppi as u8));
}
let mut read_buf = &buf[..n];
let msg = Message::unmarshal(&mut read_buf)?;
if let Message::DataChannelOpen(dco) = msg {
config.channel_type = dco.channel_type;
config.priority = dco.priority;
config.reliability_parameter = dco.reliability_parameter;
config.label = String::from_utf8(dco.label)?;
config.protocol = String::from_utf8(dco.protocol)?;
} else {
return Err(Error::InvalidMessageType(msg.message_type() as u8));
};
let data_channel = DataChannel::new(stream, config);
data_channel.write_data_channel_ack().await?;
data_channel.commit_reliability_params();
Ok(data_channel)
}
pub async fn read(&self, buf: &mut [u8]) -> Result<usize> {
self.read_data_channel(buf).await.map(|(n, _)| n)
}
pub async fn read_data_channel(&self, buf: &mut [u8]) -> Result<(usize, bool)> {
loop {
let (mut n, ppi) = match self.stream.read_sctp(buf).await {
Ok((n, ppi)) => (n, ppi),
Err(err) => {
self.stream.close().await?;
return Err(err.into());
}
};
let mut is_string = false;
match ppi {
PayloadProtocolIdentifier::Dcep => {
let mut data = &buf[..n];
match self.handle_dcep(&mut data).await {
Ok(()) => {}
Err(err) => {
log::error!("Failed to handle DCEP: {:?}", err);
}
}
continue;
}
PayloadProtocolIdentifier::String | PayloadProtocolIdentifier::StringEmpty => {
is_string = true;
}
_ => {}
};
match ppi {
PayloadProtocolIdentifier::StringEmpty | PayloadProtocolIdentifier::BinaryEmpty => {
n = 0;
}
_ => {}
};
self.messages_received.fetch_add(1, Ordering::SeqCst);
self.bytes_received.fetch_add(n, Ordering::SeqCst);
return Ok((n, is_string));
}
}
pub fn messages_sent(&self) -> usize {
self.messages_sent.load(Ordering::SeqCst)
}
pub fn messages_received(&self) -> usize {
self.messages_received.load(Ordering::SeqCst)
}
pub fn bytes_sent(&self) -> usize {
self.bytes_sent.load(Ordering::SeqCst)
}
pub fn bytes_received(&self) -> usize {
self.bytes_received.load(Ordering::SeqCst)
}
pub fn stream_identifier(&self) -> u16 {
self.stream.stream_identifier()
}
async fn handle_dcep<B>(&self, data: &mut B) -> Result<()>
where
B: Buf,
{
let msg = Message::unmarshal(data)?;
match msg {
Message::DataChannelOpen(_) => {
log::debug!("Received DATA_CHANNEL_OPEN");
let _ = self.write_data_channel_ack().await?;
}
Message::DataChannelAck(_) => {
log::debug!("Received DATA_CHANNEL_ACK");
self.commit_reliability_params();
}
};
Ok(())
}
pub async fn write(&self, data: &Bytes) -> Result<usize> {
self.write_data_channel(data, false).await
}
pub async fn write_data_channel(&self, data: &Bytes, is_string: bool) -> Result<usize> {
let data_len = data.len();
let ppi = match (is_string, data_len) {
(false, 0) => PayloadProtocolIdentifier::BinaryEmpty,
(false, _) => PayloadProtocolIdentifier::Binary,
(true, 0) => PayloadProtocolIdentifier::StringEmpty,
(true, _) => PayloadProtocolIdentifier::String,
};
self.messages_sent.fetch_add(1, Ordering::SeqCst);
self.bytes_sent.fetch_add(data_len, Ordering::SeqCst);
if data_len == 0 {
let _ = self
.stream
.write_sctp(&Bytes::from_static(&[0]), ppi)
.await?;
Ok(0)
} else {
Ok(self.stream.write_sctp(data, ppi).await?)
}
}
async fn write_data_channel_ack(&self) -> Result<usize> {
let ack = Message::DataChannelAck(DataChannelAck {}).marshal()?;
Ok(self
.stream
.write_sctp(&ack, PayloadProtocolIdentifier::Dcep)
.await?)
}
pub async fn close(&self) -> Result<()> {
Ok(self.stream.close().await?)
}
pub fn buffered_amount(&self) -> usize {
self.stream.buffered_amount()
}
pub fn buffered_amount_low_threshold(&self) -> usize {
self.stream.buffered_amount_low_threshold()
}
pub fn set_buffered_amount_low_threshold(&self, threshold: usize) {
self.stream.set_buffered_amount_low_threshold(threshold)
}
pub async fn on_buffered_amount_low(&self, f: OnBufferedAmountLowFn) {
self.stream.on_buffered_amount_low(f).await
}
fn commit_reliability_params(&self) {
let (unordered, reliability_type) = match self.config.channel_type {
ChannelType::Reliable => (false, ReliabilityType::Reliable),
ChannelType::ReliableUnordered => (true, ReliabilityType::Reliable),
ChannelType::PartialReliableRexmit => (false, ReliabilityType::Rexmit),
ChannelType::PartialReliableRexmitUnordered => (true, ReliabilityType::Rexmit),
ChannelType::PartialReliableTimed => (false, ReliabilityType::Timed),
ChannelType::PartialReliableTimedUnordered => (true, ReliabilityType::Timed),
};
self.stream.set_reliability_params(
unordered,
reliability_type,
self.config.reliability_parameter,
);
}
}