wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 28 | #include "talk/app/webrtc/sctputils.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 29 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 30 | #include "webrtc/base/buffer.h" |
| 31 | #include "webrtc/base/bytebuffer.h" |
| 32 | #include "webrtc/base/logging.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 33 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 34 | namespace webrtc { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 35 | |
| 36 | // Format defined at |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 37 | // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 38 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 39 | static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03; |
| 40 | static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 41 | |
| 42 | enum DataChannelOpenMessageChannelType { |
| 43 | DCOMCT_ORDERED_RELIABLE = 0x00, |
| 44 | DCOMCT_ORDERED_PARTIAL_RTXS = 0x01, |
| 45 | DCOMCT_ORDERED_PARTIAL_TIME = 0x02, |
| 46 | DCOMCT_UNORDERED_RELIABLE = 0x80, |
| 47 | DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81, |
| 48 | DCOMCT_UNORDERED_PARTIAL_TIME = 0x82, |
| 49 | }; |
| 50 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame^] | 51 | bool IsOpenMessage(const rtc::Buffer& payload) { |
| 52 | // Format defined at |
| 53 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 54 | |
| 55 | rtc::ByteBuffer buffer(payload); |
| 56 | uint8_t message_type; |
| 57 | if (!buffer.ReadUInt8(&message_type)) { |
| 58 | LOG(LS_WARNING) << "Could not read OPEN message type."; |
| 59 | return false; |
| 60 | } |
| 61 | return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE; |
| 62 | } |
| 63 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 64 | bool ParseDataChannelOpenMessage(const rtc::Buffer& payload, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 65 | std::string* label, |
| 66 | DataChannelInit* config) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 67 | // Format defined at |
| 68 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 69 | |
Karl Wiberg | 9478437 | 2015-04-20 12:03:07 | [diff] [blame] | 70 | rtc::ByteBuffer buffer(payload); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 71 | uint8_t message_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 72 | if (!buffer.ReadUInt8(&message_type)) { |
| 73 | LOG(LS_WARNING) << "Could not read OPEN message type."; |
| 74 | return false; |
| 75 | } |
| 76 | if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { |
| 77 | LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " |
| 78 | << message_type; |
| 79 | return false; |
| 80 | } |
| 81 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 82 | uint8_t channel_type; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 83 | if (!buffer.ReadUInt8(&channel_type)) { |
| 84 | LOG(LS_WARNING) << "Could not read OPEN message channel type."; |
| 85 | return false; |
| 86 | } |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 87 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 88 | uint16_t priority; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 89 | if (!buffer.ReadUInt16(&priority)) { |
| 90 | LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty."; |
| 91 | return false; |
| 92 | } |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 93 | uint32_t reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 94 | if (!buffer.ReadUInt32(&reliability_param)) { |
| 95 | LOG(LS_WARNING) << "Could not read OPEN message reliabilility param."; |
| 96 | return false; |
| 97 | } |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 98 | uint16_t label_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 99 | if (!buffer.ReadUInt16(&label_length)) { |
| 100 | LOG(LS_WARNING) << "Could not read OPEN message label length."; |
| 101 | return false; |
| 102 | } |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 103 | uint16_t protocol_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 104 | if (!buffer.ReadUInt16(&protocol_length)) { |
| 105 | LOG(LS_WARNING) << "Could not read OPEN message protocol length."; |
| 106 | return false; |
| 107 | } |
| 108 | if (!buffer.ReadString(label, (size_t) label_length)) { |
| 109 | LOG(LS_WARNING) << "Could not read OPEN message label"; |
| 110 | return false; |
| 111 | } |
| 112 | if (!buffer.ReadString(&config->protocol, protocol_length)) { |
| 113 | LOG(LS_WARNING) << "Could not read OPEN message protocol."; |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | config->ordered = true; |
| 118 | switch (channel_type) { |
| 119 | case DCOMCT_UNORDERED_RELIABLE: |
| 120 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 121 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 122 | config->ordered = false; |
| 123 | } |
| 124 | |
| 125 | config->maxRetransmits = -1; |
| 126 | config->maxRetransmitTime = -1; |
| 127 | switch (channel_type) { |
| 128 | case DCOMCT_ORDERED_PARTIAL_RTXS: |
| 129 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 130 | config->maxRetransmits = reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 131 | break; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 132 | case DCOMCT_ORDERED_PARTIAL_TIME: |
| 133 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 134 | config->maxRetransmitTime = reliability_param; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 135 | break; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 136 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 140 | bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) { |
Karl Wiberg | 9478437 | 2015-04-20 12:03:07 | [diff] [blame] | 141 | rtc::ByteBuffer buffer(payload); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 142 | uint8_t message_type; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 143 | if (!buffer.ReadUInt8(&message_type)) { |
| 144 | LOG(LS_WARNING) << "Could not read OPEN_ACK message type."; |
| 145 | return false; |
| 146 | } |
| 147 | if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) { |
| 148 | LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: " |
| 149 | << message_type; |
| 150 | return false; |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | bool WriteDataChannelOpenMessage(const std::string& label, |
| 156 | const DataChannelInit& config, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 157 | rtc::Buffer* payload) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 158 | // Format defined at |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 159 | // 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] | 160 | uint8_t channel_type = 0; |
| 161 | uint32_t reliability_param = 0; |
| 162 | uint16_t priority = 0; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 163 | if (config.ordered) { |
| 164 | if (config.maxRetransmits > -1) { |
| 165 | channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; |
| 166 | reliability_param = config.maxRetransmits; |
| 167 | } else if (config.maxRetransmitTime > -1) { |
| 168 | channel_type = DCOMCT_ORDERED_PARTIAL_TIME; |
| 169 | reliability_param = config.maxRetransmitTime; |
| 170 | } else { |
| 171 | channel_type = DCOMCT_ORDERED_RELIABLE; |
| 172 | } |
| 173 | } else { |
| 174 | if (config.maxRetransmits > -1) { |
| 175 | channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS; |
| 176 | reliability_param = config.maxRetransmits; |
| 177 | } else if (config.maxRetransmitTime > -1) { |
| 178 | channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; |
| 179 | reliability_param = config.maxRetransmitTime; |
| 180 | } else { |
| 181 | channel_type = DCOMCT_UNORDERED_RELIABLE; |
| 182 | } |
| 183 | } |
| 184 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 185 | rtc::ByteBuffer buffer( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 186 | NULL, 20 + label.length() + config.protocol.length(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 187 | rtc::ByteBuffer::ORDER_NETWORK); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 188 | buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); |
| 189 | buffer.WriteUInt8(channel_type); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 190 | buffer.WriteUInt16(priority); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 191 | buffer.WriteUInt32(reliability_param); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 192 | buffer.WriteUInt16(static_cast<uint16_t>(label.length())); |
| 193 | buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length())); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 194 | buffer.WriteString(label); |
| 195 | buffer.WriteString(config.protocol); |
| 196 | payload->SetData(buffer.Data(), buffer.Length()); |
| 197 | return true; |
| 198 | } |
| 199 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 200 | void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) { |
| 201 | rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 202 | buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE); |
| 203 | payload->SetData(buffer.Data(), buffer.Length()); |
| 204 | } |
| 205 | } // namespace webrtc |