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