blob: f2d1b0f54af921d79f6bebbef238efde76a2684b [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:021/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org1d1ffc92013-10-16 18:12:023 *
kjellanderb24317b2016-02-10 15:54:434 * 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.org1d1ffc92013-10-16 18:12:029 */
10
Henrik Kjellander15583c12016-02-10 09:53:1211#include "webrtc/api/sctputils.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:0212
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5213#include "webrtc/base/bytebuffer.h"
jbaucheec21bd2016-03-20 13:15:4314#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5215#include "webrtc/base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:0216
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5817namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:0218
19// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5820// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:0221
Peter Boström0c4e06b2015-10-07 10:23:2122static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
23static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
wu@webrtc.org1d1ffc92013-10-16 18:12:0224
25enum 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
jbaucheec21bd2016-03-20 13:15:4334bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 18:33:1135 // Format defined at
36 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
jbaucheec21bd2016-03-20 13:15:4337 if (payload.size() < 1) {
deadbeefab9b2d12015-10-14 18:33:1138 LOG(LS_WARNING) << "Could not read OPEN message type.";
39 return false;
40 }
jbaucheec21bd2016-03-20 13:15:4341
42 uint8_t message_type = payload[0];
deadbeefab9b2d12015-10-14 18:33:1143 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
44}
45
jbaucheec21bd2016-03-20 13:15:4346bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:5847 std::string* label,
48 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:0249 // Format defined at
50 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
51
jbaucheec21bd2016-03-20 13:15:4352 // 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öm0c4e06b2015-10-07 10:23:2155 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0256 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öm0c4e06b2015-10-07 10:23:2166 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0267 if (!buffer.ReadUInt8(&channel_type)) {
68 LOG(LS_WARNING) << "Could not read OPEN message channel type.";
69 return false;
70 }
wu@webrtc.org97077a32013-10-25 21:18:3371
Peter Boström0c4e06b2015-10-07 10:23:2172 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:0273 if (!buffer.ReadUInt16(&priority)) {
74 LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty.";
75 return false;
76 }
Peter Boström0c4e06b2015-10-07 10:23:2177 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:3378 if (!buffer.ReadUInt32(&reliability_param)) {
79 LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
80 return false;
81 }
Peter Boström0c4e06b2015-10-07 10:23:2182 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:0283 if (!buffer.ReadUInt16(&label_length)) {
84 LOG(LS_WARNING) << "Could not read OPEN message label length.";
85 return false;
86 }
Peter Boström0c4e06b2015-10-07 10:23:2187 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:0288 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.org97077a32013-10-25 21:18:33115 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02116 case DCOMCT_ORDERED_PARTIAL_TIME:
117 case DCOMCT_UNORDERED_PARTIAL_TIME:
118 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33119 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02120 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02121 return true;
122}
123
jbaucheec21bd2016-03-20 13:15:43124bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
125 if (payload.size() < 1) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58126 LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
127 return false;
128 }
jbaucheec21bd2016-03-20 13:15:43129
130 uint8_t message_type = payload[0];
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58131 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
139bool WriteDataChannelOpenMessage(const std::string& label,
140 const DataChannelInit& config,
jbaucheec21bd2016-03-20 13:15:43141 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02142 // Format defined at
wu@webrtc.org97077a32013-10-25 21:18:33143 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
Peter Boström0c4e06b2015-10-07 10:23:21144 uint8_t channel_type = 0;
145 uint32_t reliability_param = 0;
146 uint16_t priority = 0;
wu@webrtc.org1d1ffc92013-10-16 18:12:02147 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.orgd4e598d2014-07-29 17:36:52169 rtc::ByteBuffer buffer(
wu@webrtc.org1d1ffc92013-10-16 18:12:02170 NULL, 20 + label.length() + config.protocol.length(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52171 rtc::ByteBuffer::ORDER_NETWORK);
jbaucheec21bd2016-03-20 13:15:43172 // TODO(tommi): Add error handling and check resulting length.
wu@webrtc.org1d1ffc92013-10-16 18:12:02173 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
174 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02175 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33176 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 10:23:21177 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
178 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
wu@webrtc.org1d1ffc92013-10-16 18:12:02179 buffer.WriteString(label);
180 buffer.WriteString(config.protocol);
181 payload->SetData(buffer.Data(), buffer.Length());
182 return true;
183}
184
jbaucheec21bd2016-03-20 13:15:43185void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
186 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
187 payload->SetData(&data, sizeof(data));
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58188}
jbaucheec21bd2016-03-20 13:15:43189
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58190} // namespace webrtc