blob: d6ea601fc37b7b19d6df963811d6c64dfec07627 [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
jbauchf1f87202016-03-30 13:43:3752 rtc::ByteBufferReader buffer(payload.data<char>(), payload.size());
Peter Boström0c4e06b2015-10-07 10:23:2153 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0254 if (!buffer.ReadUInt8(&message_type)) {
55 LOG(LS_WARNING) << "Could not read OPEN message type.";
56 return false;
57 }
58 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
59 LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
60 << message_type;
61 return false;
62 }
63
Peter Boström0c4e06b2015-10-07 10:23:2164 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:0265 if (!buffer.ReadUInt8(&channel_type)) {
66 LOG(LS_WARNING) << "Could not read OPEN message channel type.";
67 return false;
68 }
wu@webrtc.org97077a32013-10-25 21:18:3369
Peter Boström0c4e06b2015-10-07 10:23:2170 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:0271 if (!buffer.ReadUInt16(&priority)) {
72 LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty.";
73 return false;
74 }
Peter Boström0c4e06b2015-10-07 10:23:2175 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:3376 if (!buffer.ReadUInt32(&reliability_param)) {
77 LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
78 return false;
79 }
Peter Boström0c4e06b2015-10-07 10:23:2180 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:0281 if (!buffer.ReadUInt16(&label_length)) {
82 LOG(LS_WARNING) << "Could not read OPEN message label length.";
83 return false;
84 }
Peter Boström0c4e06b2015-10-07 10:23:2185 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:0286 if (!buffer.ReadUInt16(&protocol_length)) {
87 LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
88 return false;
89 }
90 if (!buffer.ReadString(label, (size_t) label_length)) {
91 LOG(LS_WARNING) << "Could not read OPEN message label";
92 return false;
93 }
94 if (!buffer.ReadString(&config->protocol, protocol_length)) {
95 LOG(LS_WARNING) << "Could not read OPEN message protocol.";
96 return false;
97 }
98
99 config->ordered = true;
100 switch (channel_type) {
101 case DCOMCT_UNORDERED_RELIABLE:
102 case DCOMCT_UNORDERED_PARTIAL_RTXS:
103 case DCOMCT_UNORDERED_PARTIAL_TIME:
104 config->ordered = false;
105 }
106
107 config->maxRetransmits = -1;
108 config->maxRetransmitTime = -1;
109 switch (channel_type) {
110 case DCOMCT_ORDERED_PARTIAL_RTXS:
111 case DCOMCT_UNORDERED_PARTIAL_RTXS:
112 config->maxRetransmits = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33113 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02114 case DCOMCT_ORDERED_PARTIAL_TIME:
115 case DCOMCT_UNORDERED_PARTIAL_TIME:
116 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33117 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02118 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02119 return true;
120}
121
jbaucheec21bd2016-03-20 13:15:43122bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
123 if (payload.size() < 1) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58124 LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
125 return false;
126 }
jbaucheec21bd2016-03-20 13:15:43127
128 uint8_t message_type = payload[0];
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58129 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
130 LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
131 << message_type;
132 return false;
133 }
134 return true;
135}
136
137bool WriteDataChannelOpenMessage(const std::string& label,
138 const DataChannelInit& config,
jbaucheec21bd2016-03-20 13:15:43139 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02140 // Format defined at
wu@webrtc.org97077a32013-10-25 21:18:33141 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
Peter Boström0c4e06b2015-10-07 10:23:21142 uint8_t channel_type = 0;
143 uint32_t reliability_param = 0;
144 uint16_t priority = 0;
wu@webrtc.org1d1ffc92013-10-16 18:12:02145 if (config.ordered) {
146 if (config.maxRetransmits > -1) {
147 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
148 reliability_param = config.maxRetransmits;
149 } else if (config.maxRetransmitTime > -1) {
150 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
151 reliability_param = config.maxRetransmitTime;
152 } else {
153 channel_type = DCOMCT_ORDERED_RELIABLE;
154 }
155 } else {
156 if (config.maxRetransmits > -1) {
157 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
158 reliability_param = config.maxRetransmits;
159 } else if (config.maxRetransmitTime > -1) {
160 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
161 reliability_param = config.maxRetransmitTime;
162 } else {
163 channel_type = DCOMCT_UNORDERED_RELIABLE;
164 }
165 }
166
jbauchf1f87202016-03-30 13:43:37167 rtc::ByteBufferWriter buffer(
wu@webrtc.org1d1ffc92013-10-16 18:12:02168 NULL, 20 + label.length() + config.protocol.length(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52169 rtc::ByteBuffer::ORDER_NETWORK);
jbaucheec21bd2016-03-20 13:15:43170 // TODO(tommi): Add error handling and check resulting length.
wu@webrtc.org1d1ffc92013-10-16 18:12:02171 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
172 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02173 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33174 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 10:23:21175 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
176 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
wu@webrtc.org1d1ffc92013-10-16 18:12:02177 buffer.WriteString(label);
178 buffer.WriteString(config.protocol);
179 payload->SetData(buffer.Data(), buffer.Length());
180 return true;
181}
182
jbaucheec21bd2016-03-20 13:15:43183void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
184 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
185 payload->SetData(&data, sizeof(data));
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58186}
jbaucheec21bd2016-03-20 13:15:43187
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58188} // namespace webrtc