blob: f7458405eac2957bb95e1d1b90732ee13901d727 [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 Alvestrand5761e7b2021-01-29 14:45:08 +000016#include "absl/types/optional.h"
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020017#include "api/priority.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/byte_buffer.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000019#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000022
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000023namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000024
25// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000026// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000027
Peter Boström0c4e06b2015-10-07 12:23:21 +020028static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
29static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000030
31enum DataChannelOpenMessageChannelType {
32 DCOMCT_ORDERED_RELIABLE = 0x00,
33 DCOMCT_ORDERED_PARTIAL_RTXS = 0x01,
34 DCOMCT_ORDERED_PARTIAL_TIME = 0x02,
35 DCOMCT_UNORDERED_RELIABLE = 0x80,
36 DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81,
37 DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
38};
39
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020040// Values of priority in the DC open protocol message.
41// These are compared against an integer, so are enum, not enum class.
42enum DataChannelPriority {
43 DCO_PRIORITY_VERY_LOW = 128,
44 DCO_PRIORITY_LOW = 256,
45 DCO_PRIORITY_MEDIUM = 512,
46 DCO_PRIORITY_HIGH = 1024,
47};
48
jbaucheec21bd2016-03-20 06:15:43 -070049bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -070050 // Format defined at
51 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
jbaucheec21bd2016-03-20 06:15:43 -070052 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
deadbeefab9b2d12015-10-14 11:33:11 -070054 return false;
55 }
jbaucheec21bd2016-03-20 06:15:43 -070056
57 uint8_t message_type = payload[0];
deadbeefab9b2d12015-10-14 11:33:11 -070058 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
59}
60
jbaucheec21bd2016-03-20 06:15:43 -070061bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000062 std::string* label,
63 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000064 // Format defined at
65 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
66
jbauchf1f87202016-03-30 06:43:37 -070067 rtc::ByteBufferReader buffer(payload.data<char>(), payload.size());
Peter Boström0c4e06b2015-10-07 12:23:21 +020068 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000069 if (!buffer.ReadUInt8(&message_type)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010070 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000071 return false;
72 }
73 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010074 RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
75 << message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000076 return false;
77 }
78
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000080 if (!buffer.ReadUInt8(&channel_type)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000082 return false;
83 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000084
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000086 if (!buffer.ReadUInt16(&priority)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010087 RTC_LOG(LS_WARNING)
88 << "Could not read OPEN message reliabilility prioirty.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000089 return false;
90 }
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +020091 // Parse priority as defined in
92 // https://w3c.github.io/webrtc-priority/#rtcdatachannel-processing-steps
93 if (priority <= DCO_PRIORITY_VERY_LOW) {
94 config->priority = Priority::kVeryLow;
95 } else if (priority <= DCO_PRIORITY_LOW) {
96 config->priority = Priority::kLow;
97 } else if (priority <= DCO_PRIORITY_MEDIUM) {
98 config->priority = Priority::kMedium;
99 } else {
100 config->priority = Priority::kHigh;
101 }
102
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000104 if (!buffer.ReadUInt32(&reliability_param)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100105 RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
wu@webrtc.org97077a32013-10-25 21:18:33 +0000106 return false;
107 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000109 if (!buffer.ReadUInt16(&label_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100110 RTC_LOG(LS_WARNING) << "Could not read OPEN message label length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000111 return false;
112 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200113 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000114 if (!buffer.ReadUInt16(&protocol_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000116 return false;
117 }
Yves Gerey665174f2018-06-19 15:03:05 +0200118 if (!buffer.ReadString(label, (size_t)label_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(LS_WARNING) << "Could not read OPEN message label";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000120 return false;
121 }
122 if (!buffer.ReadString(&config->protocol, protocol_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000124 return false;
125 }
126
127 config->ordered = true;
128 switch (channel_type) {
129 case DCOMCT_UNORDERED_RELIABLE:
130 case DCOMCT_UNORDERED_PARTIAL_RTXS:
131 case DCOMCT_UNORDERED_PARTIAL_TIME:
132 config->ordered = false;
133 }
134
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200135 config->maxRetransmits = absl::nullopt;
136 config->maxRetransmitTime = absl::nullopt;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000137 switch (channel_type) {
138 case DCOMCT_ORDERED_PARTIAL_RTXS:
139 case DCOMCT_UNORDERED_PARTIAL_RTXS:
140 config->maxRetransmits = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000141 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000142 case DCOMCT_ORDERED_PARTIAL_TIME:
143 case DCOMCT_UNORDERED_PARTIAL_TIME:
144 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000145 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000146 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000147 return true;
148}
149
jbaucheec21bd2016-03-20 06:15:43 -0700150bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
151 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000153 return false;
154 }
jbaucheec21bd2016-03-20 06:15:43 -0700155
156 uint8_t message_type = payload[0];
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000157 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100158 RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
159 << message_type;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000160 return false;
161 }
162 return true;
163}
164
165bool WriteDataChannelOpenMessage(const std::string& label,
166 const DataChannelInit& config,
jbaucheec21bd2016-03-20 06:15:43 -0700167 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000168 // Format defined at
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200169 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1
Peter Boström0c4e06b2015-10-07 12:23:21 +0200170 uint8_t channel_type = 0;
171 uint32_t reliability_param = 0;
172 uint16_t priority = 0;
Harald Alvestrandfd5ae7f2020-05-16 08:37:49 +0200173 // Set priority according to
174 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-12#section-6.4
175 if (config.priority) {
176 switch (*config.priority) {
177 case Priority::kVeryLow:
178 priority = DCO_PRIORITY_VERY_LOW;
179 break;
180 case Priority::kLow:
181 priority = DCO_PRIORITY_LOW;
182 break;
183 case Priority::kMedium:
184 priority = DCO_PRIORITY_MEDIUM;
185 break;
186 case Priority::kHigh:
187 priority = DCO_PRIORITY_HIGH;
188 break;
189 }
190 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000191 if (config.ordered) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200192 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000193 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200194 reliability_param = *config.maxRetransmits;
195 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000196 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200197 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000198 } else {
199 channel_type = DCOMCT_ORDERED_RELIABLE;
200 }
201 } else {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200202 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000203 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200204 reliability_param = *config.maxRetransmits;
205 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000206 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200207 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000208 } else {
209 channel_type = DCOMCT_UNORDERED_RELIABLE;
210 }
211 }
212
Yves Gerey665174f2018-06-19 15:03:05 +0200213 rtc::ByteBufferWriter buffer(NULL,
Danil Chapovalov7b46e172019-11-14 17:40:23 +0100214 20 + label.length() + config.protocol.length());
jbaucheec21bd2016-03-20 06:15:43 -0700215 // TODO(tommi): Add error handling and check resulting length.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000216 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
217 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000218 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000219 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
221 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000222 buffer.WriteString(label);
223 buffer.WriteString(config.protocol);
224 payload->SetData(buffer.Data(), buffer.Length());
225 return true;
226}
227
jbaucheec21bd2016-03-20 06:15:43 -0700228void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
229 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
230 payload->SetData(&data, sizeof(data));
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000231}
jbaucheec21bd2016-03-20 06:15:43 -0700232
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000233} // namespace webrtc