blob: f7458405eac2957bb95e1d1b90732ee13901d727 [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:021/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org1d1ffc92013-10-16 18:12:023 *
kjellanderb24317b2016-02-10 15:54:434 * 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:029 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/sctp_utils.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:0212
Yves Gerey3e707812018-11-28 15:47:4913#include <stddef.h>
14#include <stdint.h>
15
Harald Alvestrand5761e7b2021-01-29 14:45:0816#include "absl/types/optional.h"
Harald Alvestrandfd5ae7f2020-05-16 06:37:4917#include "api/priority.h"
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/byte_buffer.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0819#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "rtc_base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:0222
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5823namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:0224
25// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5826// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:0227
Peter Boström0c4e06b2015-10-07 10:23:2128static 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:0230
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 06:37:4940// 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 13:15:4349bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 18:33:1150 // Format defined at
51 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
jbaucheec21bd2016-03-20 13:15:4352 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 10:09:2553 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
deadbeefab9b2d12015-10-14 18:33:1154 return false;
55 }
jbaucheec21bd2016-03-20 13:15:4356
57 uint8_t message_type = payload[0];
deadbeefab9b2d12015-10-14 18:33:1158 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
59}
60
jbaucheec21bd2016-03-20 13:15:4361bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5862 std::string* label,
63 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:0264 // Format defined at
65 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
66
jbauchf1f87202016-03-30 13:43:3767 rtc::ByteBufferReader buffer(payload.data<char>(), payload.size());
Peter Boström0c4e06b2015-10-07 10:23:2168 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0269 if (!buffer.ReadUInt8(&message_type)) {
Mirko Bonadei675513b2017-11-09 10:09:2570 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:0271 return false;
72 }
73 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 10:09:2574 RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
75 << message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0276 return false;
77 }
78
Peter Boström0c4e06b2015-10-07 10:23:2179 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0280 if (!buffer.ReadUInt8(&channel_type)) {
Mirko Bonadei675513b2017-11-09 10:09:2581 RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:0282 return false;
83 }
wu@webrtc.org97077a32013-10-25 21:18:3384
Peter Boström0c4e06b2015-10-07 10:23:2185 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:0286 if (!buffer.ReadUInt16(&priority)) {
Mirko Bonadei675513b2017-11-09 10:09:2587 RTC_LOG(LS_WARNING)
88 << "Could not read OPEN message reliabilility prioirty.";
wu@webrtc.org1d1ffc92013-10-16 18:12:0289 return false;
90 }
Harald Alvestrandfd5ae7f2020-05-16 06:37:4991 // 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 10:23:21103 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33104 if (!buffer.ReadUInt32(&reliability_param)) {
Mirko Bonadei675513b2017-11-09 10:09:25105 RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
wu@webrtc.org97077a32013-10-25 21:18:33106 return false;
107 }
Peter Boström0c4e06b2015-10-07 10:23:21108 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02109 if (!buffer.ReadUInt16(&label_length)) {
Mirko Bonadei675513b2017-11-09 10:09:25110 RTC_LOG(LS_WARNING) << "Could not read OPEN message label length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02111 return false;
112 }
Peter Boström0c4e06b2015-10-07 10:23:21113 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02114 if (!buffer.ReadUInt16(&protocol_length)) {
Mirko Bonadei675513b2017-11-09 10:09:25115 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02116 return false;
117 }
Yves Gerey665174f2018-06-19 13:03:05118 if (!buffer.ReadString(label, (size_t)label_length)) {
Mirko Bonadei675513b2017-11-09 10:09:25119 RTC_LOG(LS_WARNING) << "Could not read OPEN message label";
wu@webrtc.org1d1ffc92013-10-16 18:12:02120 return false;
121 }
122 if (!buffer.ReadString(&config->protocol, protocol_length)) {
Mirko Bonadei675513b2017-11-09 10:09:25123 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02124 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 11:09:30135 config->maxRetransmits = absl::nullopt;
136 config->maxRetransmitTime = absl::nullopt;
wu@webrtc.org1d1ffc92013-10-16 18:12:02137 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:33141 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02142 case DCOMCT_ORDERED_PARTIAL_TIME:
143 case DCOMCT_UNORDERED_PARTIAL_TIME:
144 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33145 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02146 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02147 return true;
148}
149
jbaucheec21bd2016-03-20 13:15:43150bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
151 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 10:09:25152 RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58153 return false;
154 }
jbaucheec21bd2016-03-20 13:15:43155
156 uint8_t message_type = payload[0];
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58157 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 10:09:25158 RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
159 << message_type;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58160 return false;
161 }
162 return true;
163}
164
165bool WriteDataChannelOpenMessage(const std::string& label,
166 const DataChannelInit& config,
jbaucheec21bd2016-03-20 13:15:43167 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02168 // Format defined at
Harald Alvestrandf3736ed2019-04-08 11:09:30169 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1
Peter Boström0c4e06b2015-10-07 10:23:21170 uint8_t channel_type = 0;
171 uint32_t reliability_param = 0;
172 uint16_t priority = 0;
Harald Alvestrandfd5ae7f2020-05-16 06:37:49173 // 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:02191 if (config.ordered) {
Harald Alvestrandf3736ed2019-04-08 11:09:30192 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02193 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 11:09:30194 reliability_param = *config.maxRetransmits;
195 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02196 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 11:09:30197 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02198 } else {
199 channel_type = DCOMCT_ORDERED_RELIABLE;
200 }
201 } else {
Harald Alvestrandf3736ed2019-04-08 11:09:30202 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02203 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 11:09:30204 reliability_param = *config.maxRetransmits;
205 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02206 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 11:09:30207 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02208 } else {
209 channel_type = DCOMCT_UNORDERED_RELIABLE;
210 }
211 }
212
Yves Gerey665174f2018-06-19 13:03:05213 rtc::ByteBufferWriter buffer(NULL,
Danil Chapovalov7b46e172019-11-14 16:40:23214 20 + label.length() + config.protocol.length());
jbaucheec21bd2016-03-20 13:15:43215 // TODO(tommi): Add error handling and check resulting length.
wu@webrtc.org1d1ffc92013-10-16 18:12:02216 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
217 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02218 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33219 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 10:23:21220 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:02222 buffer.WriteString(label);
223 buffer.WriteString(config.protocol);
224 payload->SetData(buffer.Data(), buffer.Length());
225 return true;
226}
227
jbaucheec21bd2016-03-20 13:15:43228void 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:58231}
jbaucheec21bd2016-03-20 13:15:43232
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58233} // namespace webrtc