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