blob: 1882a1525f7c04bd28eee1179c5f1e686cbdfed5 [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/sctp_utils.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <stdint.h>
15
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020016#include "api/priority.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/byte_buffer.h"
18#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000020
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000021namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000022
23// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000024// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000025
Peter Boström0c4e06b2015-10-07 12:23:21 +020026static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
27static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000028
29enum DataChannelOpenMessageChannelType {
30 DCOMCT_ORDERED_RELIABLE = 0x00,
31 DCOMCT_ORDERED_PARTIAL_RTXS = 0x01,
32 DCOMCT_ORDERED_PARTIAL_TIME = 0x02,
33 DCOMCT_UNORDERED_RELIABLE = 0x80,
34 DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81,
35 DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
36};
37
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020038// Values of priority in the DC open protocol message.
39// These are compared against an integer, so are enum, not enum class.
40enum DataChannelPriority {
41 DCO_PRIORITY_VERY_LOW = 128,
42 DCO_PRIORITY_LOW = 256,
43 DCO_PRIORITY_MEDIUM = 512,
44 DCO_PRIORITY_HIGH = 1024,
45};
46
jbaucheec21bd2016-03-20 06:15:43 -070047bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -070048 // Format defined at
49 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
jbaucheec21bd2016-03-20 06:15:43 -070050 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010051 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
deadbeefab9b2d12015-10-14 11:33:11 -070052 return false;
53 }
jbaucheec21bd2016-03-20 06:15:43 -070054
55 uint8_t message_type = payload[0];
deadbeefab9b2d12015-10-14 11:33:11 -070056 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
57}
58
jbaucheec21bd2016-03-20 06:15:43 -070059bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000060 std::string* label,
61 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000062 // Format defined at
63 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
64
jbauchf1f87202016-03-30 06:43:37 -070065 rtc::ByteBufferReader buffer(payload.data<char>(), payload.size());
Peter Boström0c4e06b2015-10-07 12:23:21 +020066 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000067 if (!buffer.ReadUInt8(&message_type)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010068 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000069 return false;
70 }
71 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
73 << message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000074 return false;
75 }
76
Peter Boström0c4e06b2015-10-07 12:23:21 +020077 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000078 if (!buffer.ReadUInt8(&channel_type)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000080 return false;
81 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000082
Peter Boström0c4e06b2015-10-07 12:23:21 +020083 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000084 if (!buffer.ReadUInt16(&priority)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_WARNING)
86 << "Could not read OPEN message reliabilility prioirty.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000087 return false;
88 }
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020089 // Parse priority as defined in
90 // https://w3c.github.io/webrtc-priority/#rtcdatachannel-processing-steps
91 if (priority <= DCO_PRIORITY_VERY_LOW) {
92 config->priority = Priority::kVeryLow;
93 } else if (priority <= DCO_PRIORITY_LOW) {
94 config->priority = Priority::kLow;
95 } else if (priority <= DCO_PRIORITY_MEDIUM) {
96 config->priority = Priority::kMedium;
97 } else {
98 config->priority = Priority::kHigh;
99 }
100
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000102 if (!buffer.ReadUInt32(&reliability_param)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
wu@webrtc.org97077a32013-10-25 21:18:33 +0000104 return false;
105 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000107 if (!buffer.ReadUInt16(&label_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(LS_WARNING) << "Could not read OPEN message label length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000109 return false;
110 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000112 if (!buffer.ReadUInt16(&protocol_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100113 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000114 return false;
115 }
Yves Gerey665174f2018-06-19 15:03:05 +0200116 if (!buffer.ReadString(label, (size_t)label_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100117 RTC_LOG(LS_WARNING) << "Could not read OPEN message label";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000118 return false;
119 }
120 if (!buffer.ReadString(&config->protocol, protocol_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000122 return false;
123 }
124
125 config->ordered = true;
126 switch (channel_type) {
127 case DCOMCT_UNORDERED_RELIABLE:
128 case DCOMCT_UNORDERED_PARTIAL_RTXS:
129 case DCOMCT_UNORDERED_PARTIAL_TIME:
130 config->ordered = false;
131 }
132
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200133 config->maxRetransmits = absl::nullopt;
134 config->maxRetransmitTime = absl::nullopt;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000135 switch (channel_type) {
136 case DCOMCT_ORDERED_PARTIAL_RTXS:
137 case DCOMCT_UNORDERED_PARTIAL_RTXS:
138 config->maxRetransmits = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000139 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000140 case DCOMCT_ORDERED_PARTIAL_TIME:
141 case DCOMCT_UNORDERED_PARTIAL_TIME:
142 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000143 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000144 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000145 return true;
146}
147
jbaucheec21bd2016-03-20 06:15:43 -0700148bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
149 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100150 RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000151 return false;
152 }
jbaucheec21bd2016-03-20 06:15:43 -0700153
154 uint8_t message_type = payload[0];
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000155 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
157 << message_type;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000158 return false;
159 }
160 return true;
161}
162
163bool WriteDataChannelOpenMessage(const std::string& label,
164 const DataChannelInit& config,
jbaucheec21bd2016-03-20 06:15:43 -0700165 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000166 // Format defined at
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200167 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1
Peter Boström0c4e06b2015-10-07 12:23:21 +0200168 uint8_t channel_type = 0;
169 uint32_t reliability_param = 0;
170 uint16_t priority = 0;
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +0200171 // Set priority according to
172 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-12#section-6.4
173 if (config.priority) {
174 switch (*config.priority) {
175 case Priority::kVeryLow:
176 priority = DCO_PRIORITY_VERY_LOW;
177 break;
178 case Priority::kLow:
179 priority = DCO_PRIORITY_LOW;
180 break;
181 case Priority::kMedium:
182 priority = DCO_PRIORITY_MEDIUM;
183 break;
184 case Priority::kHigh:
185 priority = DCO_PRIORITY_HIGH;
186 break;
187 }
188 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000189 if (config.ordered) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200190 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000191 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200192 reliability_param = *config.maxRetransmits;
193 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000194 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200195 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000196 } else {
197 channel_type = DCOMCT_ORDERED_RELIABLE;
198 }
199 } else {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200200 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000201 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200202 reliability_param = *config.maxRetransmits;
203 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000204 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200205 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000206 } else {
207 channel_type = DCOMCT_UNORDERED_RELIABLE;
208 }
209 }
210
Yves Gerey665174f2018-06-19 15:03:05 +0200211 rtc::ByteBufferWriter buffer(NULL,
Danil Chapovalov7b46e172019-11-14 17:40:23 +0100212 20 + label.length() + config.protocol.length());
jbaucheec21bd2016-03-20 06:15:43 -0700213 // TODO(tommi): Add error handling and check resulting length.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000214 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
215 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000216 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000217 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200218 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
219 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000220 buffer.WriteString(label);
221 buffer.WriteString(config.protocol);
222 payload->SetData(buffer.Data(), buffer.Length());
223 return true;
224}
225
jbaucheec21bd2016-03-20 06:15:43 -0700226void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
227 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
228 payload->SetData(&data, sizeof(data));
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000229}
jbaucheec21bd2016-03-20 06:15:43 -0700230
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700231cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) {
232 switch (type) {
233 case DataMessageType::kText:
234 return cricket::DMT_TEXT;
235 case DataMessageType::kBinary:
236 return cricket::DMT_BINARY;
237 case DataMessageType::kControl:
238 return cricket::DMT_CONTROL;
239 default:
240 return cricket::DMT_NONE;
241 }
242 return cricket::DMT_NONE;
243}
244
245DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) {
246 switch (type) {
247 case cricket::DMT_TEXT:
248 return DataMessageType::kText;
249 case cricket::DMT_BINARY:
250 return DataMessageType::kBinary;
251 case cricket::DMT_CONTROL:
252 return DataMessageType::kControl;
253 case cricket::DMT_NONE:
254 default:
255 RTC_NOTREACHED();
256 }
257 return DataMessageType::kControl;
258}
259
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000260} // namespace webrtc