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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/byte_buffer.h" |
| 17 | #include "rtc_base/copy_on_write_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/logging.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 19 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 20 | namespace webrtc { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 21 | |
| 22 | // Format defined at |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 23 | // 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] | 24 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 25 | static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03; |
| 26 | static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 27 | |
| 28 | enum DataChannelOpenMessageChannelType { |
| 29 | DCOMCT_ORDERED_RELIABLE = 0x00, |
| 30 | DCOMCT_ORDERED_PARTIAL_RTXS = 0x01, |
| 31 | DCOMCT_ORDERED_PARTIAL_TIME = 0x02, |
| 32 | DCOMCT_UNORDERED_RELIABLE = 0x80, |
| 33 | DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81, |
| 34 | DCOMCT_UNORDERED_PARTIAL_TIME = 0x82, |
| 35 | }; |
| 36 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 37 | bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 38 | // Format defined at |
| 39 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 40 | if (payload.size() < 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 41 | RTC_LOG(LS_WARNING) << "Could not read OPEN message type."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 42 | return false; |
| 43 | } |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 44 | |
| 45 | uint8_t message_type = payload[0]; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 46 | return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE; |
| 47 | } |
| 48 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 49 | bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 50 | std::string* label, |
| 51 | DataChannelInit* config) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 52 | // Format defined at |
| 53 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 54 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 55 | rtc::ByteBufferReader buffer(payload.data<char>(), payload.size()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 56 | uint8_t message_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 57 | if (!buffer.ReadUInt8(&message_type)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 58 | RTC_LOG(LS_WARNING) << "Could not read OPEN message type."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 62 | RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " |
| 63 | << message_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 67 | uint8_t channel_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 68 | if (!buffer.ReadUInt8(&channel_type)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 69 | RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 72 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 73 | uint16_t priority; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 74 | if (!buffer.ReadUInt16(&priority)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 75 | RTC_LOG(LS_WARNING) |
| 76 | << "Could not read OPEN message reliabilility prioirty."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 79 | uint32_t reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 80 | if (!buffer.ReadUInt32(&reliability_param)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 81 | RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param."; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | uint16_t label_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 85 | if (!buffer.ReadUInt16(&label_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 86 | RTC_LOG(LS_WARNING) << "Could not read OPEN message label length."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 89 | uint16_t protocol_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 90 | if (!buffer.ReadUInt16(&protocol_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 91 | RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | if (!buffer.ReadString(label, (size_t)label_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 95 | RTC_LOG(LS_WARNING) << "Could not read OPEN message label"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | if (!buffer.ReadString(&config->protocol, protocol_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 99 | RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol."; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | config->ordered = true; |
| 104 | switch (channel_type) { |
| 105 | case DCOMCT_UNORDERED_RELIABLE: |
| 106 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 107 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 108 | config->ordered = false; |
| 109 | } |
| 110 | |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 111 | config->maxRetransmits = absl::nullopt; |
| 112 | config->maxRetransmitTime = absl::nullopt; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 113 | switch (channel_type) { |
| 114 | case DCOMCT_ORDERED_PARTIAL_RTXS: |
| 115 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 116 | config->maxRetransmits = reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 117 | break; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 118 | case DCOMCT_ORDERED_PARTIAL_TIME: |
| 119 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 120 | config->maxRetransmitTime = reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 121 | break; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 122 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 126 | bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) { |
| 127 | if (payload.size() < 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 128 | RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type."; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 131 | |
| 132 | uint8_t message_type = payload[0]; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 133 | if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 134 | RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: " |
| 135 | << message_type; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool WriteDataChannelOpenMessage(const std::string& label, |
| 142 | const DataChannelInit& config, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 143 | rtc::CopyOnWriteBuffer* payload) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 144 | // Format defined at |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 145 | // 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] | 146 | uint8_t channel_type = 0; |
| 147 | uint32_t reliability_param = 0; |
| 148 | uint16_t priority = 0; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 149 | if (config.ordered) { |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 150 | if (config.maxRetransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 151 | channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 152 | reliability_param = *config.maxRetransmits; |
| 153 | } else if (config.maxRetransmitTime) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 154 | channel_type = DCOMCT_ORDERED_PARTIAL_TIME; |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 155 | reliability_param = *config.maxRetransmitTime; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 156 | } else { |
| 157 | channel_type = DCOMCT_ORDERED_RELIABLE; |
| 158 | } |
| 159 | } else { |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 160 | if (config.maxRetransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 161 | channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS; |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 162 | reliability_param = *config.maxRetransmits; |
| 163 | } else if (config.maxRetransmitTime) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 164 | channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 165 | reliability_param = *config.maxRetransmitTime; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 166 | } else { |
| 167 | channel_type = DCOMCT_UNORDERED_RELIABLE; |
| 168 | } |
| 169 | } |
| 170 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 171 | rtc::ByteBufferWriter buffer(NULL, |
Danil Chapovalov | 7b46e17 | 2019-11-14 17:40:23 +0100 | [diff] [blame] | 172 | 20 + label.length() + config.protocol.length()); |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 173 | // TODO(tommi): Add error handling and check resulting length. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 174 | buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); |
| 175 | buffer.WriteUInt8(channel_type); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 176 | buffer.WriteUInt16(priority); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 177 | buffer.WriteUInt32(reliability_param); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 178 | buffer.WriteUInt16(static_cast<uint16_t>(label.length())); |
| 179 | buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length())); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 180 | buffer.WriteString(label); |
| 181 | buffer.WriteString(config.protocol); |
| 182 | payload->SetData(buffer.Data(), buffer.Length()); |
| 183 | return true; |
| 184 | } |
| 185 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 186 | void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) { |
| 187 | uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE; |
| 188 | payload->SetData(&data, sizeof(data)); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 189 | } |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 190 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 191 | cricket::DataMessageType ToCricketDataMessageType(DataMessageType type) { |
| 192 | switch (type) { |
| 193 | case DataMessageType::kText: |
| 194 | return cricket::DMT_TEXT; |
| 195 | case DataMessageType::kBinary: |
| 196 | return cricket::DMT_BINARY; |
| 197 | case DataMessageType::kControl: |
| 198 | return cricket::DMT_CONTROL; |
| 199 | default: |
| 200 | return cricket::DMT_NONE; |
| 201 | } |
| 202 | return cricket::DMT_NONE; |
| 203 | } |
| 204 | |
| 205 | DataMessageType ToWebrtcDataMessageType(cricket::DataMessageType type) { |
| 206 | switch (type) { |
| 207 | case cricket::DMT_TEXT: |
| 208 | return DataMessageType::kText; |
| 209 | case cricket::DMT_BINARY: |
| 210 | return DataMessageType::kBinary; |
| 211 | case cricket::DMT_CONTROL: |
| 212 | return DataMessageType::kControl; |
| 213 | case cricket::DMT_NONE: |
| 214 | default: |
| 215 | RTC_NOTREACHED(); |
| 216 | } |
| 217 | return DataMessageType::kControl; |
| 218 | } |
| 219 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 220 | } // namespace webrtc |