blob: 1a2c282ff68103f5ecd16e6cdb6d1be5a3234614 [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:02 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/sctputils.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000012
kjellander194e3bc2016-03-19 12:12:52 -070013#include "webrtc/base/buffer.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000014#include "webrtc/base/bytebuffer.h"
15#include "webrtc/base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000016
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000017namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000018
19// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000020// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000021
Peter Boström0c4e06b2015-10-07 12:23:21 +020022static 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:02 +000024
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
kjellander194e3bc2016-03-19 12:12:52 -070034bool IsOpenMessage(const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -070035 // Format defined at
36 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
kjellander194e3bc2016-03-19 12:12:52 -070037
38 rtc::ByteBuffer buffer(payload);
39 uint8_t message_type;
40 if (!buffer.ReadUInt8(&message_type)) {
deadbeefab9b2d12015-10-14 11:33:11 -070041 LOG(LS_WARNING) << "Could not read OPEN message type.";
42 return false;
43 }
44 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
45}
46
kjellander194e3bc2016-03-19 12:12:52 -070047bool ParseDataChannelOpenMessage(const rtc::Buffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000048 std::string* label,
49 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000050 // Format defined at
51 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
52
kjellander194e3bc2016-03-19 12:12:52 -070053 rtc::ByteBuffer buffer(payload);
Peter Boström0c4e06b2015-10-07 12:23:21 +020054 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000055 if (!buffer.ReadUInt8(&message_type)) {
56 LOG(LS_WARNING) << "Could not read OPEN message type.";
57 return false;
58 }
59 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
60 LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
61 << message_type;
62 return false;
63 }
64
Peter Boström0c4e06b2015-10-07 12:23:21 +020065 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000066 if (!buffer.ReadUInt8(&channel_type)) {
67 LOG(LS_WARNING) << "Could not read OPEN message channel type.";
68 return false;
69 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000070
Peter Boström0c4e06b2015-10-07 12:23:21 +020071 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000072 if (!buffer.ReadUInt16(&priority)) {
73 LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty.";
74 return false;
75 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020076 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +000077 if (!buffer.ReadUInt32(&reliability_param)) {
78 LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
79 return false;
80 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000082 if (!buffer.ReadUInt16(&label_length)) {
83 LOG(LS_WARNING) << "Could not read OPEN message label length.";
84 return false;
85 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000087 if (!buffer.ReadUInt16(&protocol_length)) {
88 LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
89 return false;
90 }
91 if (!buffer.ReadString(label, (size_t) label_length)) {
92 LOG(LS_WARNING) << "Could not read OPEN message label";
93 return false;
94 }
95 if (!buffer.ReadString(&config->protocol, protocol_length)) {
96 LOG(LS_WARNING) << "Could not read OPEN message protocol.";
97 return false;
98 }
99
100 config->ordered = true;
101 switch (channel_type) {
102 case DCOMCT_UNORDERED_RELIABLE:
103 case DCOMCT_UNORDERED_PARTIAL_RTXS:
104 case DCOMCT_UNORDERED_PARTIAL_TIME:
105 config->ordered = false;
106 }
107
108 config->maxRetransmits = -1;
109 config->maxRetransmitTime = -1;
110 switch (channel_type) {
111 case DCOMCT_ORDERED_PARTIAL_RTXS:
112 case DCOMCT_UNORDERED_PARTIAL_RTXS:
113 config->maxRetransmits = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000114 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000115 case DCOMCT_ORDERED_PARTIAL_TIME:
116 case DCOMCT_UNORDERED_PARTIAL_TIME:
117 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000118 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000119 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000120 return true;
121}
122
kjellander194e3bc2016-03-19 12:12:52 -0700123bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) {
124 rtc::ByteBuffer buffer(payload);
125 uint8_t message_type;
126 if (!buffer.ReadUInt8(&message_type)) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000127 LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
128 return false;
129 }
130 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
131 LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
132 << message_type;
133 return false;
134 }
135 return true;
136}
137
138bool WriteDataChannelOpenMessage(const std::string& label,
139 const DataChannelInit& config,
kjellander194e3bc2016-03-19 12:12:52 -0700140 rtc::Buffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000141 // Format defined at
wu@webrtc.org97077a32013-10-25 21:18:33 +0000142 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 uint8_t channel_type = 0;
144 uint32_t reliability_param = 0;
145 uint16_t priority = 0;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000146 if (config.ordered) {
147 if (config.maxRetransmits > -1) {
148 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
149 reliability_param = config.maxRetransmits;
150 } else if (config.maxRetransmitTime > -1) {
151 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
152 reliability_param = config.maxRetransmitTime;
153 } else {
154 channel_type = DCOMCT_ORDERED_RELIABLE;
155 }
156 } else {
157 if (config.maxRetransmits > -1) {
158 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
159 reliability_param = config.maxRetransmits;
160 } else if (config.maxRetransmitTime > -1) {
161 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
162 reliability_param = config.maxRetransmitTime;
163 } else {
164 channel_type = DCOMCT_UNORDERED_RELIABLE;
165 }
166 }
167
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000168 rtc::ByteBuffer buffer(
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000169 NULL, 20 + label.length() + config.protocol.length(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000170 rtc::ByteBuffer::ORDER_NETWORK);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000171 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
172 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000173 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000174 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200175 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:02 +0000177 buffer.WriteString(label);
178 buffer.WriteString(config.protocol);
179 payload->SetData(buffer.Data(), buffer.Length());
180 return true;
181}
182
kjellander194e3bc2016-03-19 12:12:52 -0700183void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) {
184 rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK);
185 buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE);
186 payload->SetData(buffer.Data(), buffer.Length());
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000187}
188} // namespace webrtc