wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 4 | * 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.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/sctp_utils.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stddef.h> |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 14 | |
| 15 | #include <cstdint> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 16 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 18 | #include "api/priority.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "rtc_base/byte_buffer.h" |
| 20 | #include "rtc_base/copy_on_write_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 22 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 23 | namespace webrtc { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 24 | |
| 25 | // Format defined at |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 26 | // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 27 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 28 | static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03; |
| 29 | static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 30 | |
| 31 | enum 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 Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 40 | // Values of priority in the DC open protocol message. |
| 41 | // These are compared against an integer, so are enum, not enum class. |
| 42 | enum DataChannelPriority { |
| 43 | DCO_PRIORITY_VERY_LOW = 128, |
| 44 | DCO_PRIORITY_LOW = 256, |
| 45 | DCO_PRIORITY_MEDIUM = 512, |
| 46 | DCO_PRIORITY_HIGH = 1024, |
| 47 | }; |
| 48 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 49 | bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 50 | // Format defined at |
Tommi | 5bbfb00 | 2023-03-04 15:47:53 | [diff] [blame] | 51 | // https://www.rfc-editor.org/rfc/rfc8832#section-5.1 |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 52 | if (payload.size() < 1) { |
Tommi | 5bbfb00 | 2023-03-04 15:47:53 | [diff] [blame] | 53 | RTC_DLOG(LS_WARNING) << "Could not read OPEN message type."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 54 | return false; |
| 55 | } |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 56 | |
| 57 | uint8_t message_type = payload[0]; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 58 | return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE; |
| 59 | } |
| 60 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 61 | bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 62 | std::string* label, |
| 63 | DataChannelInit* config) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 64 | // Format defined at |
| 65 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 66 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 67 | rtc::ByteBufferReader buffer(payload.data<char>(), payload.size()); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 68 | uint8_t message_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 69 | if (!buffer.ReadUInt8(&message_type)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 70 | RTC_LOG(LS_WARNING) << "Could not read OPEN message type."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 74 | RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " |
| 75 | << message_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 79 | uint8_t channel_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 80 | if (!buffer.ReadUInt8(&channel_type)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 81 | RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 82 | return false; |
| 83 | } |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 84 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 85 | uint16_t priority; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 86 | if (!buffer.ReadUInt16(&priority)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 87 | RTC_LOG(LS_WARNING) |
| 88 | << "Could not read OPEN message reliabilility prioirty."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 89 | return false; |
| 90 | } |
Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 91 | // 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öm | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 103 | uint32_t reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 104 | if (!buffer.ReadUInt32(&reliability_param)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 105 | RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param."; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 106 | return false; |
| 107 | } |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 108 | uint16_t label_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 109 | if (!buffer.ReadUInt16(&label_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 110 | RTC_LOG(LS_WARNING) << "Could not read OPEN message label length."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 111 | return false; |
| 112 | } |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 113 | uint16_t protocol_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 114 | if (!buffer.ReadUInt16(&protocol_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 115 | RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 116 | return false; |
| 117 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 118 | if (!buffer.ReadString(label, (size_t)label_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 119 | RTC_LOG(LS_WARNING) << "Could not read OPEN message label"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | if (!buffer.ReadString(&config->protocol, protocol_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 123 | RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 124 | 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 Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 135 | config->maxRetransmits = absl::nullopt; |
| 136 | config->maxRetransmitTime = absl::nullopt; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 137 | switch (channel_type) { |
| 138 | case DCOMCT_ORDERED_PARTIAL_RTXS: |
| 139 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 140 | config->maxRetransmits = reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 141 | break; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 142 | case DCOMCT_ORDERED_PARTIAL_TIME: |
| 143 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 144 | config->maxRetransmitTime = reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 145 | break; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 146 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 150 | bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) { |
| 151 | if (payload.size() < 1) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 152 | RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type."; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 153 | return false; |
| 154 | } |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 155 | |
| 156 | uint8_t message_type = payload[0]; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 157 | if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 158 | RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: " |
| 159 | << message_type; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | bool WriteDataChannelOpenMessage(const std::string& label, |
| 166 | const DataChannelInit& config, |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 167 | rtc::CopyOnWriteBuffer* payload) { |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 168 | return WriteDataChannelOpenMessage(label, config.protocol, config.priority, |
| 169 | config.ordered, config.maxRetransmits, |
| 170 | config.maxRetransmitTime, payload); |
| 171 | } |
| 172 | |
| 173 | bool 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.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 180 | // Format defined at |
Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 181 | // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1 |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 182 | uint8_t channel_type = 0; |
| 183 | uint32_t reliability_param = 0; |
| 184 | uint16_t priority = 0; |
Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 185 | // Set priority according to |
| 186 | // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-12#section-6.4 |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 187 | if (opt_priority) { |
| 188 | switch (*opt_priority) { |
Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 189 | 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 | } |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 203 | if (ordered) { |
| 204 | if (max_retransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 205 | channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 206 | reliability_param = *max_retransmits; |
| 207 | } else if (max_retransmit_time) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 208 | channel_type = DCOMCT_ORDERED_PARTIAL_TIME; |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 209 | reliability_param = *max_retransmit_time; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 210 | } else { |
| 211 | channel_type = DCOMCT_ORDERED_RELIABLE; |
| 212 | } |
| 213 | } else { |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 214 | if (max_retransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 215 | channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS; |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 216 | reliability_param = *max_retransmits; |
| 217 | } else if (max_retransmit_time) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 218 | channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 219 | reliability_param = *max_retransmit_time; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 220 | } else { |
| 221 | channel_type = DCOMCT_UNORDERED_RELIABLE; |
| 222 | } |
| 223 | } |
| 224 | |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 225 | rtc::ByteBufferWriter buffer(NULL, 20 + label.length() + protocol.length()); |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 226 | // TODO(tommi): Add error handling and check resulting length. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 227 | buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); |
| 228 | buffer.WriteUInt8(channel_type); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 229 | buffer.WriteUInt16(priority); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 230 | buffer.WriteUInt32(reliability_param); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 231 | buffer.WriteUInt16(static_cast<uint16_t>(label.length())); |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 232 | buffer.WriteUInt16(static_cast<uint16_t>(protocol.length())); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 233 | buffer.WriteString(label); |
Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 234 | buffer.WriteString(protocol); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 235 | payload->SetData(buffer.Data(), buffer.Length()); |
| 236 | return true; |
| 237 | } |
| 238 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 239 | void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) { |
| 240 | uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE; |
| 241 | payload->SetData(&data, sizeof(data)); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 242 | } |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 243 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 244 | } // namespace webrtc |