blob: 54742c27a7559ae8dff36b42d960e9e0e1bfdf46 [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>
Harald Alvestrandc24a2182022-02-23 13:44:5914
15#include <cstdint>
Yves Gerey3e707812018-11-28 15:47:4916
Harald Alvestrand5761e7b2021-01-29 14:45:0817#include "absl/types/optional.h"
Harald Alvestrandfd5ae7f2020-05-16 06:37:4918#include "api/priority.h"
Steve Anton10542f22019-01-11 17:11:0019#include "rtc_base/byte_buffer.h"
20#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
Tommi5bbfb002023-03-04 15:47:5351 // https://www.rfc-editor.org/rfc/rfc8832#section-5.1
jbaucheec21bd2016-03-20 13:15:4352 if (payload.size() < 1) {
Tommi5bbfb002023-03-04 15:47:5353 RTC_DLOG(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) {
Tommi492296c2023-03-12 15:59:25168 return WriteDataChannelOpenMessage(label, config.protocol, config.priority,
169 config.ordered, config.maxRetransmits,
170 config.maxRetransmitTime, payload);
171}
172
173bool WriteDataChannelOpenMessage(const std::string& label,
174 const std::string& protocol,
175 absl::optional<Priority> opt_priority,
176 bool ordered,
177 absl::optional<int> max_retransmits,
178 absl::optional<int> max_retransmit_time,
179 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02180 // Format defined at
Harald Alvestrandf3736ed2019-04-08 11:09:30181 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1
Peter Boström0c4e06b2015-10-07 10:23:21182 uint8_t channel_type = 0;
183 uint32_t reliability_param = 0;
184 uint16_t priority = 0;
Harald Alvestrandfd5ae7f2020-05-16 06:37:49185 // Set priority according to
186 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-12#section-6.4
Tommi492296c2023-03-12 15:59:25187 if (opt_priority) {
188 switch (*opt_priority) {
Harald Alvestrandfd5ae7f2020-05-16 06:37:49189 case Priority::kVeryLow:
190 priority = DCO_PRIORITY_VERY_LOW;
191 break;
192 case Priority::kLow:
193 priority = DCO_PRIORITY_LOW;
194 break;
195 case Priority::kMedium:
196 priority = DCO_PRIORITY_MEDIUM;
197 break;
198 case Priority::kHigh:
199 priority = DCO_PRIORITY_HIGH;
200 break;
201 }
202 }
Tommi492296c2023-03-12 15:59:25203 if (ordered) {
204 if (max_retransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02205 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
Tommi492296c2023-03-12 15:59:25206 reliability_param = *max_retransmits;
207 } else if (max_retransmit_time) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02208 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
Tommi492296c2023-03-12 15:59:25209 reliability_param = *max_retransmit_time;
wu@webrtc.org1d1ffc92013-10-16 18:12:02210 } else {
211 channel_type = DCOMCT_ORDERED_RELIABLE;
212 }
213 } else {
Tommi492296c2023-03-12 15:59:25214 if (max_retransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02215 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
Tommi492296c2023-03-12 15:59:25216 reliability_param = *max_retransmits;
217 } else if (max_retransmit_time) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02218 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
Tommi492296c2023-03-12 15:59:25219 reliability_param = *max_retransmit_time;
wu@webrtc.org1d1ffc92013-10-16 18:12:02220 } else {
221 channel_type = DCOMCT_UNORDERED_RELIABLE;
222 }
223 }
224
Tommi492296c2023-03-12 15:59:25225 rtc::ByteBufferWriter buffer(NULL, 20 + label.length() + protocol.length());
jbaucheec21bd2016-03-20 13:15:43226 // TODO(tommi): Add error handling and check resulting length.
wu@webrtc.org1d1ffc92013-10-16 18:12:02227 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
228 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02229 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33230 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 10:23:21231 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
Tommi492296c2023-03-12 15:59:25232 buffer.WriteUInt16(static_cast<uint16_t>(protocol.length()));
wu@webrtc.org1d1ffc92013-10-16 18:12:02233 buffer.WriteString(label);
Tommi492296c2023-03-12 15:59:25234 buffer.WriteString(protocol);
wu@webrtc.org1d1ffc92013-10-16 18:12:02235 payload->SetData(buffer.Data(), buffer.Length());
236 return true;
237}
238
jbaucheec21bd2016-03-20 13:15:43239void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
240 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
241 payload->SetData(&data, sizeof(data));
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58242}
jbaucheec21bd2016-03-20 13:15:43243
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58244} // namespace webrtc