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