blob: 4073905de7e8ccfcefaf646f48df7242e583c1b0 [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
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
35namespace cricket {
36
37// Format defined at
wu@webrtc.org97077a32013-10-25 21:18:33 +000038// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000039
40static const uint8 DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
41
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
51bool 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 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000076
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000077 uint16 priority;
78 if (!buffer.ReadUInt16(&priority)) {
79 LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty.";
80 return false;
81 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000082 uint32 reliability_param;
83 if (!buffer.ReadUInt32(&reliability_param)) {
84 LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
85 return false;
86 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000087 uint16 label_length;
88 if (!buffer.ReadUInt16(&label_length)) {
89 LOG(LS_WARNING) << "Could not read OPEN message label length.";
90 return false;
91 }
92 uint16 protocol_length;
93 if (!buffer.ReadUInt16(&protocol_length)) {
94 LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
95 return false;
96 }
97 if (!buffer.ReadString(label, (size_t) label_length)) {
98 LOG(LS_WARNING) << "Could not read OPEN message label";
99 return false;
100 }
101 if (!buffer.ReadString(&config->protocol, protocol_length)) {
102 LOG(LS_WARNING) << "Could not read OPEN message protocol.";
103 return false;
104 }
105
106 config->ordered = true;
107 switch (channel_type) {
108 case DCOMCT_UNORDERED_RELIABLE:
109 case DCOMCT_UNORDERED_PARTIAL_RTXS:
110 case DCOMCT_UNORDERED_PARTIAL_TIME:
111 config->ordered = false;
112 }
113
114 config->maxRetransmits = -1;
115 config->maxRetransmitTime = -1;
116 switch (channel_type) {
117 case DCOMCT_ORDERED_PARTIAL_RTXS:
118 case DCOMCT_UNORDERED_PARTIAL_RTXS:
119 config->maxRetransmits = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000120 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000121 case DCOMCT_ORDERED_PARTIAL_TIME:
122 case DCOMCT_UNORDERED_PARTIAL_TIME:
123 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000124 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000125 }
126
127 return true;
128}
129
130bool WriteDataChannelOpenMessage(
131 const std::string& label,
132 const webrtc::DataChannelInit& config,
133 talk_base::Buffer* payload) {
134 // Format defined at
wu@webrtc.org97077a32013-10-25 21:18:33 +0000135 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000136 uint8 channel_type = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000137 uint32 reliability_param = 0;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000138 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);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000166 buffer.WriteUInt16(priority);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000167 buffer.WriteUInt32(reliability_param);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000168 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