blob: 7b67fc1839be3afe9cf43e7cdac20f102a6dc9f0 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/sctp_utils.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <stdint.h>
15
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/byte_buffer.h"
17#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000019
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000020namespace webrtc {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000021
22// Format defined at
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000023// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000024
Peter Boström0c4e06b2015-10-07 12:23:21 +020025static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
26static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000027
28enum DataChannelOpenMessageChannelType {
29 DCOMCT_ORDERED_RELIABLE = 0x00,
30 DCOMCT_ORDERED_PARTIAL_RTXS = 0x01,
31 DCOMCT_ORDERED_PARTIAL_TIME = 0x02,
32 DCOMCT_UNORDERED_RELIABLE = 0x80,
33 DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81,
34 DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
35};
36
jbaucheec21bd2016-03-20 06:15:43 -070037bool IsOpenMessage(const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -070038 // Format defined at
39 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
jbaucheec21bd2016-03-20 06:15:43 -070040 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010041 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
deadbeefab9b2d12015-10-14 11:33:11 -070042 return false;
43 }
jbaucheec21bd2016-03-20 06:15:43 -070044
45 uint8_t message_type = payload[0];
deadbeefab9b2d12015-10-14 11:33:11 -070046 return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
47}
48
jbaucheec21bd2016-03-20 06:15:43 -070049bool ParseDataChannelOpenMessage(const rtc::CopyOnWriteBuffer& payload,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000050 std::string* label,
51 DataChannelInit* config) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000052 // Format defined at
53 // http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
54
jbauchf1f87202016-03-30 06:43:37 -070055 rtc::ByteBufferReader buffer(payload.data<char>(), payload.size());
Peter Boström0c4e06b2015-10-07 12:23:21 +020056 uint8_t message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000057 if (!buffer.ReadUInt8(&message_type)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010058 RTC_LOG(LS_WARNING) << "Could not read OPEN message type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000059 return false;
60 }
61 if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010062 RTC_LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
63 << message_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000064 return false;
65 }
66
Peter Boström0c4e06b2015-10-07 12:23:21 +020067 uint8_t channel_type;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000068 if (!buffer.ReadUInt8(&channel_type)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(LS_WARNING) << "Could not read OPEN message channel type.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000070 return false;
71 }
wu@webrtc.org97077a32013-10-25 21:18:33 +000072
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 uint16_t priority;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000074 if (!buffer.ReadUInt16(&priority)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010075 RTC_LOG(LS_WARNING)
76 << "Could not read OPEN message reliabilility prioirty.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000077 return false;
78 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 uint32_t reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +000080 if (!buffer.ReadUInt32(&reliability_param)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
wu@webrtc.org97077a32013-10-25 21:18:33 +000082 return false;
83 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 uint16_t label_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000085 if (!buffer.ReadUInt16(&label_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_WARNING) << "Could not read OPEN message label length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000087 return false;
88 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000090 if (!buffer.ReadUInt16(&protocol_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000092 return false;
93 }
Yves Gerey665174f2018-06-19 15:03:05 +020094 if (!buffer.ReadString(label, (size_t)label_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010095 RTC_LOG(LS_WARNING) << "Could not read OPEN message label";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000096 return false;
97 }
98 if (!buffer.ReadString(&config->protocol, protocol_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010099 RTC_LOG(LS_WARNING) << "Could not read OPEN message protocol.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000100 return false;
101 }
102
103 config->ordered = true;
104 switch (channel_type) {
105 case DCOMCT_UNORDERED_RELIABLE:
106 case DCOMCT_UNORDERED_PARTIAL_RTXS:
107 case DCOMCT_UNORDERED_PARTIAL_TIME:
108 config->ordered = false;
109 }
110
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200111 config->maxRetransmits = absl::nullopt;
112 config->maxRetransmitTime = absl::nullopt;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000113 switch (channel_type) {
114 case DCOMCT_ORDERED_PARTIAL_RTXS:
115 case DCOMCT_UNORDERED_PARTIAL_RTXS:
116 config->maxRetransmits = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000117 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000118 case DCOMCT_ORDERED_PARTIAL_TIME:
119 case DCOMCT_UNORDERED_PARTIAL_TIME:
120 config->maxRetransmitTime = reliability_param;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000121 break;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000122 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000123 return true;
124}
125
jbaucheec21bd2016-03-20 06:15:43 -0700126bool ParseDataChannelOpenAckMessage(const rtc::CopyOnWriteBuffer& payload) {
127 if (payload.size() < 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100128 RTC_LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000129 return false;
130 }
jbaucheec21bd2016-03-20 06:15:43 -0700131
132 uint8_t message_type = payload[0];
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000133 if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100134 RTC_LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
135 << message_type;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000136 return false;
137 }
138 return true;
139}
140
141bool WriteDataChannelOpenMessage(const std::string& label,
142 const DataChannelInit& config,
jbaucheec21bd2016-03-20 06:15:43 -0700143 rtc::CopyOnWriteBuffer* payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000144 // Format defined at
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200145 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-5.1
Peter Boström0c4e06b2015-10-07 12:23:21 +0200146 uint8_t channel_type = 0;
147 uint32_t reliability_param = 0;
148 uint16_t priority = 0;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000149 if (config.ordered) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200150 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000151 channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200152 reliability_param = *config.maxRetransmits;
153 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000154 channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200155 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000156 } else {
157 channel_type = DCOMCT_ORDERED_RELIABLE;
158 }
159 } else {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200160 if (config.maxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000161 channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200162 reliability_param = *config.maxRetransmits;
163 } else if (config.maxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000164 channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200165 reliability_param = *config.maxRetransmitTime;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000166 } else {
167 channel_type = DCOMCT_UNORDERED_RELIABLE;
168 }
169 }
170
Yves Gerey665174f2018-06-19 15:03:05 +0200171 rtc::ByteBufferWriter buffer(NULL,
172 20 + label.length() + config.protocol.length(),
173 rtc::ByteBuffer::ORDER_NETWORK);
jbaucheec21bd2016-03-20 06:15:43 -0700174 // TODO(tommi): Add error handling and check resulting length.
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
jbaucheec21bd2016-03-20 06:15:43 -0700187void WriteDataChannelOpenAckMessage(rtc::CopyOnWriteBuffer* payload) {
188 uint8_t data = DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE;
189 payload->SetData(&data, sizeof(data));
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000190}
jbaucheec21bd2016-03-20 06:15:43 -0700191
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000192} // namespace webrtc