2013-10-16 18:12:02 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-10-16 18:12:02 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-10-16 18:12:02 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-02-10 10:53:12 +01:00
|
|
|
#include "webrtc/api/sctputils.h"
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
#include "webrtc/base/buffer.h"
|
|
|
|
|
#include "webrtc/base/bytebuffer.h"
|
|
|
|
|
#include "webrtc/base/logging.h"
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
namespace webrtc {
|
2013-10-16 18:12:02 +00:00
|
|
|
|
|
|
|
|
// Format defined at
|
2014-01-14 10:00:58 +00:00
|
|
|
// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-01#section
|
2013-10-16 18:12:02 +00:00
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
static const uint8_t DATA_CHANNEL_OPEN_MESSAGE_TYPE = 0x03;
|
|
|
|
|
static const uint8_t DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE = 0x02;
|
2013-10-16 18:12:02 +00:00
|
|
|
|
|
|
|
|
enum DataChannelOpenMessageChannelType {
|
|
|
|
|
DCOMCT_ORDERED_RELIABLE = 0x00,
|
|
|
|
|
DCOMCT_ORDERED_PARTIAL_RTXS = 0x01,
|
|
|
|
|
DCOMCT_ORDERED_PARTIAL_TIME = 0x02,
|
|
|
|
|
DCOMCT_UNORDERED_RELIABLE = 0x80,
|
|
|
|
|
DCOMCT_UNORDERED_PARTIAL_RTXS = 0x81,
|
|
|
|
|
DCOMCT_UNORDERED_PARTIAL_TIME = 0x82,
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-14 11:33:11 -07:00
|
|
|
bool IsOpenMessage(const rtc::Buffer& payload) {
|
|
|
|
|
// Format defined at
|
|
|
|
|
// http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
|
|
|
|
|
|
|
|
|
|
rtc::ByteBuffer buffer(payload);
|
|
|
|
|
uint8_t message_type;
|
|
|
|
|
if (!buffer.ReadUInt8(&message_type)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message type.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return message_type == DATA_CHANNEL_OPEN_MESSAGE_TYPE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
bool ParseDataChannelOpenMessage(const rtc::Buffer& payload,
|
2014-01-14 10:00:58 +00:00
|
|
|
std::string* label,
|
|
|
|
|
DataChannelInit* config) {
|
2013-10-16 18:12:02 +00:00
|
|
|
// Format defined at
|
|
|
|
|
// http://tools.ietf.org/html/draft-jesup-rtcweb-data-protocol-04
|
|
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
rtc::ByteBuffer buffer(payload);
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t message_type;
|
2013-10-16 18:12:02 +00:00
|
|
|
if (!buffer.ReadUInt8(&message_type)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message type.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (message_type != DATA_CHANNEL_OPEN_MESSAGE_TYPE) {
|
|
|
|
|
LOG(LS_WARNING) << "Data Channel OPEN message of unexpected type: "
|
|
|
|
|
<< message_type;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t channel_type;
|
2013-10-16 18:12:02 +00:00
|
|
|
if (!buffer.ReadUInt8(&channel_type)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message channel type.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-10-25 21:18:33 +00:00
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint16_t priority;
|
2013-10-16 18:12:02 +00:00
|
|
|
if (!buffer.ReadUInt16(&priority)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message reliabilility prioirty.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint32_t reliability_param;
|
2013-10-25 21:18:33 +00:00
|
|
|
if (!buffer.ReadUInt32(&reliability_param)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message reliabilility param.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint16_t label_length;
|
2013-10-16 18:12:02 +00:00
|
|
|
if (!buffer.ReadUInt16(&label_length)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message label length.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint16_t protocol_length;
|
2013-10-16 18:12:02 +00:00
|
|
|
if (!buffer.ReadUInt16(&protocol_length)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message protocol length.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!buffer.ReadString(label, (size_t) label_length)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message label";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!buffer.ReadString(&config->protocol, protocol_length)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN message protocol.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config->ordered = true;
|
|
|
|
|
switch (channel_type) {
|
|
|
|
|
case DCOMCT_UNORDERED_RELIABLE:
|
|
|
|
|
case DCOMCT_UNORDERED_PARTIAL_RTXS:
|
|
|
|
|
case DCOMCT_UNORDERED_PARTIAL_TIME:
|
|
|
|
|
config->ordered = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config->maxRetransmits = -1;
|
|
|
|
|
config->maxRetransmitTime = -1;
|
|
|
|
|
switch (channel_type) {
|
|
|
|
|
case DCOMCT_ORDERED_PARTIAL_RTXS:
|
|
|
|
|
case DCOMCT_UNORDERED_PARTIAL_RTXS:
|
|
|
|
|
config->maxRetransmits = reliability_param;
|
2013-10-25 21:18:33 +00:00
|
|
|
break;
|
2013-10-16 18:12:02 +00:00
|
|
|
case DCOMCT_ORDERED_PARTIAL_TIME:
|
|
|
|
|
case DCOMCT_UNORDERED_PARTIAL_TIME:
|
|
|
|
|
config->maxRetransmitTime = reliability_param;
|
2013-10-25 21:18:33 +00:00
|
|
|
break;
|
2013-10-16 18:12:02 +00:00
|
|
|
}
|
2014-01-14 10:00:58 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
bool ParseDataChannelOpenAckMessage(const rtc::Buffer& payload) {
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
rtc::ByteBuffer buffer(payload);
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t message_type;
|
2014-01-14 10:00:58 +00:00
|
|
|
if (!buffer.ReadUInt8(&message_type)) {
|
|
|
|
|
LOG(LS_WARNING) << "Could not read OPEN_ACK message type.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (message_type != DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE) {
|
|
|
|
|
LOG(LS_WARNING) << "Data Channel OPEN_ACK message of unexpected type: "
|
|
|
|
|
<< message_type;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-10-16 18:12:02 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
bool WriteDataChannelOpenMessage(const std::string& label,
|
|
|
|
|
const DataChannelInit& config,
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::Buffer* payload) {
|
2013-10-16 18:12:02 +00:00
|
|
|
// Format defined at
|
2013-10-25 21:18:33 +00:00
|
|
|
// http://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-00#section-6.1
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t channel_type = 0;
|
|
|
|
|
uint32_t reliability_param = 0;
|
|
|
|
|
uint16_t priority = 0;
|
2013-10-16 18:12:02 +00:00
|
|
|
if (config.ordered) {
|
|
|
|
|
if (config.maxRetransmits > -1) {
|
|
|
|
|
channel_type = DCOMCT_ORDERED_PARTIAL_RTXS;
|
|
|
|
|
reliability_param = config.maxRetransmits;
|
|
|
|
|
} else if (config.maxRetransmitTime > -1) {
|
|
|
|
|
channel_type = DCOMCT_ORDERED_PARTIAL_TIME;
|
|
|
|
|
reliability_param = config.maxRetransmitTime;
|
|
|
|
|
} else {
|
|
|
|
|
channel_type = DCOMCT_ORDERED_RELIABLE;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (config.maxRetransmits > -1) {
|
|
|
|
|
channel_type = DCOMCT_UNORDERED_PARTIAL_RTXS;
|
|
|
|
|
reliability_param = config.maxRetransmits;
|
|
|
|
|
} else if (config.maxRetransmitTime > -1) {
|
|
|
|
|
channel_type = DCOMCT_UNORDERED_PARTIAL_TIME;
|
|
|
|
|
reliability_param = config.maxRetransmitTime;
|
|
|
|
|
} else {
|
|
|
|
|
channel_type = DCOMCT_UNORDERED_RELIABLE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::ByteBuffer buffer(
|
2013-10-16 18:12:02 +00:00
|
|
|
NULL, 20 + label.length() + config.protocol.length(),
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::ByteBuffer::ORDER_NETWORK);
|
2013-10-16 18:12:02 +00:00
|
|
|
buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
|
|
|
|
|
buffer.WriteUInt8(channel_type);
|
|
|
|
|
buffer.WriteUInt16(priority);
|
2013-10-25 21:18:33 +00:00
|
|
|
buffer.WriteUInt32(reliability_param);
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
buffer.WriteUInt16(static_cast<uint16_t>(label.length()));
|
|
|
|
|
buffer.WriteUInt16(static_cast<uint16_t>(config.protocol.length()));
|
2013-10-16 18:12:02 +00:00
|
|
|
buffer.WriteString(label);
|
|
|
|
|
buffer.WriteString(config.protocol);
|
|
|
|
|
payload->SetData(buffer.Data(), buffer.Length());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
void WriteDataChannelOpenAckMessage(rtc::Buffer* payload) {
|
|
|
|
|
rtc::ByteBuffer buffer(rtc::ByteBuffer::ORDER_NETWORK);
|
2014-01-14 10:00:58 +00:00
|
|
|
buffer.WriteUInt8(DATA_CHANNEL_OPEN_ACK_MESSAGE_TYPE);
|
|
|
|
|
payload->SetData(buffer.Data(), buffer.Length());
|
|
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|