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