blob: a64383720f85a3d86a25ef80fdba1defa51bc8f3 [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001/*
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.orgaebb1ad2014-01-14 10:00:58 +000028#include "talk/app/webrtc/sctputils.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000029
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030#include "webrtc/base/buffer.h"
31#include "webrtc/base/bytebuffer.h"
32#include "webrtc/base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000033
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000034namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000035
36// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000037// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000038
Peter Boström0c4e06b2015-10-07 12:23:21 +020039static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
40static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000041
42enum 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.orgd4e598d2014-07-29 17:36:52 +000051bool ParseDataChannelOpenMessage(const rtc::Buffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000052 std::string* label,
53 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000054 // Format defined at
55 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
56
Karl Wiberg94784372015-04-20 14:03:07 +020057 rtc::ByteBuffer buffer(payload);
Peter Boström0c4e06b2015-10-07 12:23:21 +020058 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000059 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öm0c4e06b2015-10-07 12:23:21 +020069 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000070 if (!buffer.ReadUInt8(&channel_type)) {
71 LOG(LS_WARNING) << "Could not read OPEN message channel type.";
72 return false;
73 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000074
Peter Boström0c4e06b2015-10-07 12:23:21 +020075 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000076 if (!buffer.ReadUInt16(&priority)) {
77 LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty.";
78 return false;
79 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020080 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +000081 if (!buffer.ReadUInt32(&reliability_param)) {
82 LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
83 return false;
84 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000086 if (!buffer.ReadUInt16(&label_length)) {
87 LOG(LS_WARNING) << "Could not read OPEN message label length.";
88 return false;
89 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020090 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000091 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.org97077a32013-10-25 21:18:33 +0000118 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000119 case DCOMCT_ORDERED_PARTIAL_TIME:
120 case DCOMCT_UNORDERED_PARTIAL_TIME:
121 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000122 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000123 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000124 return true;
125}
126
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000127bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) {
Karl Wiberg94784372015-04-20 14:03:07 +0200128 rtc::ByteBuffer buffer(payload);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200129 uint8_t message_type;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000130 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
142bool WriteDataChannelOpenMessage(const std::string& label,
143 const DataChannelInit& config,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000144 rtc::Buffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000145 // Format defined at
wu@webrtc.org97077a32013-10-25 21:18:33 +0000146 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 uint8_t channel_type = 0;
148 uint32_t reliability_param = 0;
149 uint16_t priority = 0;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000150 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.orgd4e598d2014-07-29 17:36:52 +0000172 rtc::ByteBuffer buffer(
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000173 NULL, 20 + label.length() + config.protocol.length(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::ByteBuffer::ORDER_NETWORK);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000175 buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
176 buffer.WriteUInt8(channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000177 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000178 buffer.WriteUInt32(reliability_param);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200179 buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
180 buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000181 buffer.WriteString(label);
182 buffer.WriteString(config.protocol);
183 payload->SetData(buffer.Data(), buffer.Length());
184 return true;
185}
186
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) {
188 rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000189 buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE);
190 payload->SetData(buffer.Data(), buffer.Length());
191}
192} // namespace webrtc