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 | |
| 28 | #include "talk/media/sctp/sctputils.h" |
| 29 | |
| 30 | #include "talk/app/webrtc/datachannelinterface.h" |
| 31 | #include "talk/base/buffer.h" |
| 32 | #include "talk/base/bytebuffer.h" |
| 33 | #include "talk/base/logging.h" |
| 34 | |
| 35 | namespace cricket { |
| 36 | |
| 37 | // Format defined at |
| 38 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 39 | |
| 40 | static const uint8 DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03; |
| 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 | |
| 51 | bool ParseDataChannelOpenMessage( |
| 52 | const talk_base::Buffer& payload, |
| 53 | std::string* label, |
| 54 | webrtc::DataChannelInit* config) { |
| 55 | // Format defined at |
| 56 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 57 | |
| 58 | talk_base::ByteBuffer buffer(payload.data(), payload.length()); |
| 59 | |
| 60 | uint8 message_type; |
| 61 | if (!buffer.ReadUInt8(&message_type)) { |
| 62 | LOG(LS_WARNING) << "Could not read OPEN message type."; |
| 63 | return false; |
| 64 | } |
| 65 | if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) { |
| 66 | LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: " |
| 67 | << message_type; |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | uint8 channel_type; |
| 72 | if (!buffer.ReadUInt8(&channel_type)) { |
| 73 | LOG(LS_WARNING) << "Could not read OPEN message channel type."; |
| 74 | return false; |
| 75 | } |
| 76 | uint16 reliability_param; |
| 77 | if (!buffer.ReadUInt16(&reliability_param)) { |
| 78 | LOG(LS_WARNING) << "Could not read OPEN message reliabilility param."; |
| 79 | return false; |
| 80 | } |
| 81 | uint16 priority; |
| 82 | if (!buffer.ReadUInt16(&priority)) { |
| 83 | LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty."; |
| 84 | return false; |
| 85 | } |
| 86 | uint16 label_length; |
| 87 | if (!buffer.ReadUInt16(&label_length)) { |
| 88 | LOG(LS_WARNING) << "Could not read OPEN message label length."; |
| 89 | return false; |
| 90 | } |
| 91 | uint16 protocol_length; |
| 92 | if (!buffer.ReadUInt16(&protocol_length)) { |
| 93 | LOG(LS_WARNING) << "Could not read OPEN message protocol length."; |
| 94 | return false; |
| 95 | } |
| 96 | if (!buffer.ReadString(label, (size_t) label_length)) { |
| 97 | LOG(LS_WARNING) << "Could not read OPEN message label"; |
| 98 | return false; |
| 99 | } |
| 100 | if (!buffer.ReadString(&config->protocol, protocol_length)) { |
| 101 | LOG(LS_WARNING) << "Could not read OPEN message protocol."; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | config->ordered = true; |
| 106 | switch (channel_type) { |
| 107 | case DCOMCT_UNORDERED_RELIABLE: |
| 108 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 109 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 110 | config->ordered = false; |
| 111 | } |
| 112 | |
| 113 | config->maxRetransmits = -1; |
| 114 | config->maxRetransmitTime = -1; |
| 115 | switch (channel_type) { |
| 116 | case DCOMCT_ORDERED_PARTIAL_RTXS: |
| 117 | case DCOMCT_UNORDERED_PARTIAL_RTXS: |
| 118 | config->maxRetransmits = reliability_param; |
| 119 | |
| 120 | case DCOMCT_ORDERED_PARTIAL_TIME: |
| 121 | case DCOMCT_UNORDERED_PARTIAL_TIME: |
| 122 | config->maxRetransmitTime = reliability_param; |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool WriteDataChannelOpenMessage( |
| 129 | const std::string& label, |
| 130 | const webrtc::DataChannelInit& config, |
| 131 | talk_base::Buffer* payload) { |
| 132 | // Format defined at |
| 133 | // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04 |
| 134 | // TODO(pthatcher) |
| 135 | |
| 136 | uint8 channel_type = 0; |
| 137 | uint16 reliability_param = 0; |
| 138 | uint16 priority = 0; |
| 139 | if (config.ordered) { |
| 140 | if (config.maxRetransmits > -1) { |
| 141 | channel_type = DCOMCT_ORDERED_PARTIAL_RTXS; |
| 142 | reliability_param = config.maxRetransmits; |
| 143 | } else if (config.maxRetransmitTime > -1) { |
| 144 | channel_type = DCOMCT_ORDERED_PARTIAL_TIME; |
| 145 | reliability_param = config.maxRetransmitTime; |
| 146 | } else { |
| 147 | channel_type = DCOMCT_ORDERED_RELIABLE; |
| 148 | } |
| 149 | } else { |
| 150 | if (config.maxRetransmits > -1) { |
| 151 | channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS; |
| 152 | reliability_param = config.maxRetransmits; |
| 153 | } else if (config.maxRetransmitTime > -1) { |
| 154 | channel_type = DCOMCT_UNORDERED_PARTIAL_TIME; |
| 155 | reliability_param = config.maxRetransmitTime; |
| 156 | } else { |
| 157 | channel_type = DCOMCT_UNORDERED_RELIABLE; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | talk_base::ByteBuffer buffer( |
| 162 | NULL, 20 + label.length() + config.protocol.length(), |
| 163 | talk_base::ByteBuffer::ORDER_NETWORK); |
| 164 | buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE); |
| 165 | buffer.WriteUInt8(channel_type); |
| 166 | buffer.WriteUInt16(reliability_param); |
| 167 | buffer.WriteUInt16(priority); |
| 168 | buffer.WriteUInt16(static_cast<uint16>(label.length())); |
| 169 | buffer.WriteUInt16(static_cast<uint16>(config.protocol.length())); |
| 170 | buffer.WriteString(label); |
| 171 | buffer.WriteString(config.protocol); |
| 172 | payload->SetData(buffer.Data(), buffer.Length()); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | } // namespace cricket |